Home | History | Annotate | Line # | Download | only in scsipi
scsipi_base.c revision 1.20.2.1.2.1
      1 /*	$NetBSD: scsipi_base.c,v 1.20.2.1.2.1 1999/06/21 01:19:12 thorpej 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 SCSI_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 	s = splbio();
    101 	while (sc_link->openings <= 0) {
    102 		SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
    103 		if ((flags & SCSI_NOSLEEP) != 0) {
    104 			splx(s);
    105 			return (0);
    106 		}
    107 		sc_link->flags |= SDEV_WAITING;
    108 		(void)tsleep(sc_link, PRIBIO, "getxs", 0);
    109 	}
    110 	SC_DEBUG(sc_link, SDEV_DB3, ("calling pool_get\n"));
    111 	xs = pool_get(&scsipi_xfer_pool,
    112 	    ((flags & SCSI_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    113 	if (xs != NULL)
    114 		sc_link->openings--;
    115 	else {
    116 		(*sc_link->sc_print_addr)(sc_link);
    117 		printf("cannot allocate scsipi xs\n");
    118 	}
    119 	splx(s);
    120 
    121 	SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
    122 
    123 	/*
    124 	 * zeroes out the command, as ATAPI may use longer commands
    125 	 * than SCSI
    126 	 */
    127 	if (xs != NULL) {
    128 		xs->flags = INUSE | flags;
    129 		TAILQ_INSERT_TAIL(&sc_link->pending_xfers, xs, device_q);
    130 		bzero(&xs->cmdstore, sizeof(xs->cmdstore));
    131 	}
    132 	return (xs);
    133 }
    134 
    135 /*
    136  * Given a scsipi_xfer struct, and a device (referenced through sc_link)
    137  * return the struct to the free pool and credit the device with it
    138  * If another process is waiting for an xs, do a wakeup, let it proceed
    139  *
    140  * MUST BE CALLED AT splbio()!!
    141  */
    142 void
    143 scsipi_free_xs(xs, flags)
    144 	struct scsipi_xfer *xs;
    145 	int flags;
    146 {
    147 	struct scsipi_link *sc_link = xs->sc_link;
    148 
    149 	TAILQ_REMOVE(&sc_link->pending_xfers, xs, device_q);
    150 	if (TAILQ_FIRST(&sc_link->pending_xfers) == NULL &&
    151 	    (sc_link->flags & SDEV_WAITDRAIN) != 0) {
    152 		sc_link->flags &= ~SDEV_WAITDRAIN;
    153 		wakeup(&sc_link->pending_xfers);
    154 	}
    155 	xs->flags &= ~INUSE;
    156 	pool_put(&scsipi_xfer_pool, xs);
    157 
    158 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_free_xs\n"));
    159 	/* if was 0 and someone waits, wake them up */
    160 	sc_link->openings++;
    161 	if ((sc_link->flags & SDEV_WAITING) != 0) {
    162 		sc_link->flags &= ~SDEV_WAITING;
    163 		wakeup(sc_link);
    164 	} else {
    165 		if (sc_link->device->start) {
    166 			SC_DEBUG(sc_link, SDEV_DB2,
    167 			    ("calling private start()\n"));
    168 			(*(sc_link->device->start))(sc_link->device_softc);
    169 		}
    170 	}
    171 }
    172 
    173 /*
    174  * Wait for a scsipi_link's pending xfers to drain.
    175  */
    176 void
    177 scsipi_wait_drain(sc_link)
    178 	struct scsipi_link *sc_link;
    179 {
    180 	int s;
    181 
    182 	s = splbio();
    183 	while (TAILQ_FIRST(&sc_link->pending_xfers) != NULL) {
    184 		sc_link->flags |= SDEV_WAITDRAIN;
    185 		(void) tsleep(&sc_link->pending_xfers, PRIBIO, "sxdrn", 0);
    186 	}
    187 	splx(s);
    188 }
    189 
    190 /*
    191  * Look at the returned sense and act on the error, determining
    192  * the unix error number to pass back.  (0 = report no error)
    193  *
    194  * THIS IS THE DEFAULT ERROR HANDLER FOR SCSI DEVICES
    195  */
    196 int
    197 scsipi_interpret_sense(xs)
    198 	struct scsipi_xfer *xs;
    199 {
    200 	struct scsipi_sense_data *sense;
    201 	struct scsipi_link *sc_link = xs->sc_link;
    202 	u_int8_t key;
    203 	u_int32_t info;
    204 	int error;
    205 #ifndef	SCSIVERBOSE
    206 	static char *error_mes[] = {
    207 		"soft error (corrected)",
    208 		"not ready", "medium error",
    209 		"non-media hardware failure", "illegal request",
    210 		"unit attention", "readonly device",
    211 		"no data found", "vendor unique",
    212 		"copy aborted", "command aborted",
    213 		"search returned equal", "volume overflow",
    214 		"verify miscompare", "unknown error key"
    215 	};
    216 #endif
    217 
    218 	sense = &xs->sense.scsi_sense;
    219 #ifdef	SCSIDEBUG
    220 	if ((sc_link->flags & SDEV_DB1) != 0) {
    221 		int count;
    222 		printf("code 0x%x valid 0x%x ",
    223 			sense->error_code & SSD_ERRCODE,
    224 			sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
    225 		printf("seg 0x%x key 0x%x ili 0x%x eom 0x%x fmark 0x%x\n",
    226 			sense->segment,
    227 			sense->flags & SSD_KEY,
    228 			sense->flags & SSD_ILI ? 1 : 0,
    229 			sense->flags & SSD_EOM ? 1 : 0,
    230 			sense->flags & SSD_FILEMARK ? 1 : 0);
    231 		printf("info: 0x%x 0x%x 0x%x 0x%x followed by %d extra bytes\n",
    232 			sense->info[0],
    233 			sense->info[1],
    234 			sense->info[2],
    235 			sense->info[3],
    236 			sense->extra_len);
    237 		printf("extra: ");
    238 		for (count = 0; count < ADD_BYTES_LIM(sense); count++)
    239 			printf("0x%x ", sense->cmd_spec_info[count]);
    240 		printf("\n");
    241 	}
    242 #endif	/* SCSIDEBUG */
    243 	/*
    244 	 * If the device has it's own error handler, call it first.
    245 	 * If it returns a legit error value, return that, otherwise
    246 	 * it wants us to continue with normal error processing.
    247 	 */
    248 	if (sc_link->device->err_handler) {
    249 		SC_DEBUG(sc_link, SDEV_DB2,
    250 		    ("calling private err_handler()\n"));
    251 		error = (*sc_link->device->err_handler)(xs);
    252 		if (error != SCSIRET_CONTINUE)
    253 			return (error);		/* error >= 0  better ? */
    254 	}
    255 	/* otherwise use the default */
    256 	switch (sense->error_code & SSD_ERRCODE) {
    257 		/*
    258 		 * If it's code 70, use the extended stuff and
    259 		 * interpret the key
    260 		 */
    261 	case 0x71:		/* delayed error */
    262 		sc_link->sc_print_addr(sc_link);
    263 		key = sense->flags & SSD_KEY;
    264 		printf(" DEFERRED ERROR, key = 0x%x\n", key);
    265 		/* FALLTHROUGH */
    266 	case 0x70:
    267 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
    268 			info = _4btol(sense->info);
    269 		else
    270 			info = 0;
    271 		key = sense->flags & SSD_KEY;
    272 
    273 		switch (key) {
    274 		case SKEY_NO_SENSE:
    275 		case SKEY_RECOVERED_ERROR:
    276 			if (xs->resid == xs->datalen && xs->datalen) {
    277 				/*
    278 				 * Why is this here?
    279 				 */
    280 				xs->resid = 0;	/* not short read */
    281 			}
    282 		case SKEY_EQUAL:
    283 			error = 0;
    284 			break;
    285 		case SKEY_NOT_READY:
    286 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    287 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    288 			if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
    289 				return (0);
    290 			if (sense->add_sense_code == 0x3A &&
    291 			    sense->add_sense_code_qual == 0x00)
    292 				error = ENODEV; /* Medium not present */
    293 			else
    294 				error = EIO;
    295 			if ((xs->flags & SCSI_SILENT) != 0)
    296 				return (error);
    297 			break;
    298 		case SKEY_ILLEGAL_REQUEST:
    299 			if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
    300 				return (0);
    301 			if ((xs->flags & SCSI_SILENT) != 0)
    302 				return (EIO);
    303 			error = EINVAL;
    304 			break;
    305 		case SKEY_UNIT_ATTENTION:
    306 			if (sense->add_sense_code == 0x29 &&
    307 			    sense->add_sense_code_qual == 0x00)
    308 				return (ERESTART); /* device or bus reset */
    309 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    310 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    311 			if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
    312 				/* XXX Should reupload any transient state. */
    313 				(sc_link->flags & SDEV_REMOVABLE) == 0)
    314 				return (ERESTART);
    315 			if ((xs->flags & SCSI_SILENT) != 0)
    316 				return (EIO);
    317 			error = EIO;
    318 			break;
    319 		case SKEY_WRITE_PROTECT:
    320 			error = EROFS;
    321 			break;
    322 		case SKEY_BLANK_CHECK:
    323 			error = 0;
    324 			break;
    325 		case SKEY_ABORTED_COMMAND:
    326 			error = ERESTART;
    327 			break;
    328 		case SKEY_VOLUME_OVERFLOW:
    329 			error = ENOSPC;
    330 			break;
    331 		default:
    332 			error = EIO;
    333 			break;
    334 		}
    335 
    336 #ifdef SCSIVERBOSE
    337 		if ((xs->flags & SCSI_SILENT) == 0)
    338 			scsipi_print_sense(xs, 0);
    339 #else
    340 		if (key) {
    341 			sc_link->sc_print_addr(sc_link);
    342 			printf("%s", error_mes[key - 1]);
    343 			if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    344 				switch (key) {
    345 				case SKEY_NOT_READY:
    346 				case SKEY_ILLEGAL_REQUEST:
    347 				case SKEY_UNIT_ATTENTION:
    348 				case SKEY_WRITE_PROTECT:
    349 					break;
    350 				case SKEY_BLANK_CHECK:
    351 					printf(", requested size: %d (decimal)",
    352 					    info);
    353 					break;
    354 				case SKEY_ABORTED_COMMAND:
    355 					if (xs->retries)
    356 						printf(", retrying");
    357 					printf(", cmd 0x%x, info 0x%x",
    358 					    xs->cmd->opcode, info);
    359 					break;
    360 				default:
    361 					printf(", info = %d (decimal)", info);
    362 				}
    363 			}
    364 			if (sense->extra_len != 0) {
    365 				int n;
    366 				printf(", data =");
    367 				for (n = 0; n < sense->extra_len; n++)
    368 					printf(" %02x",
    369 					    sense->cmd_spec_info[n]);
    370 			}
    371 			printf("\n");
    372 		}
    373 #endif
    374 		return (error);
    375 
    376 	/*
    377 	 * Not code 70, just report it
    378 	 */
    379 	default:
    380 		sc_link->sc_print_addr(sc_link);
    381 		printf("Sense Error Code 0x%x",
    382 			sense->error_code & SSD_ERRCODE);
    383 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    384 			struct scsipi_sense_data_unextended *usense =
    385 			    (struct scsipi_sense_data_unextended *)sense;
    386 			printf(" at block no. %d (decimal)",
    387 			    _3btol(usense->block));
    388 		}
    389 		printf("\n");
    390 		return (EIO);
    391 	}
    392 }
    393 
    394 /*
    395  * Find out from the device what its capacity is.
    396  */
    397 u_long
    398 scsipi_size(sc_link, flags)
    399 	struct scsipi_link *sc_link;
    400 	int flags;
    401 {
    402 	struct scsipi_read_cap_data rdcap;
    403 	struct scsipi_read_capacity scsipi_cmd;
    404 
    405 	/*
    406 	 * make up a scsipi command and ask the scsipi driver to do
    407 	 * it for you.
    408 	 */
    409 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    410 	scsipi_cmd.opcode = READ_CAPACITY;
    411 
    412 	/*
    413 	 * If the command works, interpret the result as a 4 byte
    414 	 * number of blocks
    415 	 */
    416 	if (scsipi_command(sc_link, (struct scsipi_generic *)&scsipi_cmd,
    417 	    sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
    418 	    2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
    419 		sc_link->sc_print_addr(sc_link);
    420 		printf("could not get size\n");
    421 		return (0);
    422 	}
    423 
    424 	return (_4btol(rdcap.addr) + 1);
    425 }
    426 
    427 /*
    428  * Get scsipi driver to send a "are you ready?" command
    429  */
    430 int
    431 scsipi_test_unit_ready(sc_link, flags)
    432 	struct scsipi_link *sc_link;
    433 	int flags;
    434 {
    435 	struct scsipi_test_unit_ready scsipi_cmd;
    436 
    437 	/* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
    438 	if (sc_link->quirks & ADEV_NOTUR)
    439 		return (0);
    440 
    441 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    442 	scsipi_cmd.opcode = TEST_UNIT_READY;
    443 
    444 	return (scsipi_command(sc_link,
    445 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
    446 	    0, 0, 2, 10000, NULL, flags));
    447 }
    448 
    449 /*
    450  * Do a scsipi operation asking a device what it is
    451  * Use the scsipi_cmd routine in the switch table.
    452  * XXX actually this is only used for scsi devices, because I have the feeling
    453  * that some atapi CDROM may not implement it, althouh it marked as mandatory
    454  * in the atapi specs.
    455  */
    456 int
    457 scsipi_inquire(sc_link, inqbuf, flags)
    458 	struct scsipi_link *sc_link;
    459 	struct scsipi_inquiry_data *inqbuf;
    460 	int flags;
    461 {
    462 	struct scsipi_inquiry scsipi_cmd;
    463 
    464 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    465 	scsipi_cmd.opcode = INQUIRY;
    466 	scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
    467 
    468 	return (scsipi_command(sc_link,
    469 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    470 	    (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data),
    471 	    2, 10000, NULL, SCSI_DATA_IN | flags));
    472 }
    473 
    474 /*
    475  * Prevent or allow the user to remove the media
    476  */
    477 int
    478 scsipi_prevent(sc_link, type, flags)
    479 	struct scsipi_link *sc_link;
    480 	int type, flags;
    481 {
    482 	struct scsipi_prevent scsipi_cmd;
    483 
    484 	if (sc_link->quirks & ADEV_NODOORLOCK)
    485 		return (0);
    486 
    487 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    488 	scsipi_cmd.opcode = PREVENT_ALLOW;
    489 	scsipi_cmd.how = type;
    490 	return (scsipi_command(sc_link,
    491 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    492 	    0, 0, 2, 5000, NULL, flags));
    493 }
    494 
    495 /*
    496  * Get scsipi driver to send a "start up" command
    497  */
    498 int
    499 scsipi_start(sc_link, type, flags)
    500 	struct scsipi_link *sc_link;
    501 	int type, flags;
    502 {
    503 	struct scsipi_start_stop scsipi_cmd;
    504 
    505 	if (sc_link->quirks & SDEV_NOSTARTUNIT)
    506 		return 0;
    507 
    508 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    509 	scsipi_cmd.opcode = START_STOP;
    510 	scsipi_cmd.byte2 = 0x00;
    511 	scsipi_cmd.how = type;
    512 	return (scsipi_command(sc_link,
    513 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    514 	    0, 0, 2, (type & SSS_START) ? 30000 : 10000, NULL, flags));
    515 }
    516 
    517 /*
    518  * This routine is called by the scsipi interrupt when the transfer is
    519  * complete.
    520  */
    521 void
    522 scsipi_done(xs)
    523 	struct scsipi_xfer *xs;
    524 {
    525 	struct scsipi_link *sc_link = xs->sc_link;
    526 	struct buf *bp;
    527 	int error;
    528 
    529 	SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
    530 #ifdef	SCSIDEBUG
    531 	if ((sc_link->flags & SDEV_DB1) != 0)
    532 		show_scsipi_cmd(xs);
    533 #endif /* SCSIDEBUG */
    534 
    535 	/*
    536 	 * If it's a user level request, bypass all usual completion
    537 	 * processing, let the user work it out..  We take
    538 	 * reponsibility for freeing the xs when the user returns.
    539 	 * (and restarting the device's queue).
    540 	 */
    541 	if ((xs->flags & SCSI_USER) != 0) {
    542 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
    543 		scsipi_user_done(xs); /* to take a copy of the sense etc. */
    544 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
    545 
    546 		/*
    547 		 * If this was an asynchronous operation (i.e. adapter
    548 		 * returned SUCCESSFULLY_QUEUED when the command was
    549 		 * submitted), we need to free the scsipi_xfer here.
    550 		 */
    551 		if (SCSIPI_XFER_ASYNC(xs))
    552 			scsipi_free_xs(xs, SCSI_NOSLEEP);
    553 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
    554 		return;
    555 	}
    556 
    557 	if (!SCSIPI_XFER_ASYNC(xs)) {
    558 		/*
    559 		 * if it's a normal upper level request, then ask
    560 		 * the upper level code to handle error checking
    561 		 * rather than doing it here at interrupt time
    562 		 */
    563 		wakeup(xs);
    564 		return;
    565 	}
    566 
    567 	/*
    568 	 * Go and handle errors now.
    569 	 * If it returns ERESTART then we should RETRY
    570 	 */
    571 retry:
    572 	error = sc_err1(xs, 1);
    573 	if (error == ERESTART) {
    574 		switch (scsipi_command_direct(xs)) {
    575 		case SUCCESSFULLY_QUEUED:
    576 			return;
    577 
    578 		case TRY_AGAIN_LATER:
    579 			xs->error = XS_BUSY;
    580 		case COMPLETE:
    581 			goto retry;
    582 		}
    583 	}
    584 
    585 	bp = xs->bp;
    586 	if (bp) {
    587 		if (error) {
    588 			bp->b_error = error;
    589 			bp->b_flags |= B_ERROR;
    590 			bp->b_resid = bp->b_bcount;
    591 		} else {
    592 			bp->b_error = 0;
    593 			bp->b_resid = xs->resid;
    594 		}
    595 	}
    596 	if (sc_link->device->done) {
    597 		/*
    598 		 * Tell the device the operation is actually complete.
    599 		 * No more will happen with this xfer.  This for
    600 		 * notification of the upper-level driver only; they
    601 		 * won't be returning any meaningful information to us.
    602 		 */
    603 		(*sc_link->device->done)(xs);
    604 	}
    605 	/*
    606 	 * If this was an asynchronous operation (i.e. adapter
    607 	 * returned SUCCESSFULLY_QUEUED when the command was
    608 	 * submitted), we need to free the scsipi_xfer here.
    609 	 */
    610 	if (SCSIPI_XFER_ASYNC(xs)) {
    611 		int s = splbio();
    612 		scsipi_free_xs(xs, SCSI_NOSLEEP);
    613 		splx(s);
    614 	}
    615 	if (bp)
    616 		biodone(bp);
    617 }
    618 
    619 int
    620 scsipi_execute_xs(xs)
    621 	struct scsipi_xfer *xs;
    622 {
    623 	int async;
    624 	int error;
    625 	int s;
    626 
    627 	xs->flags &= ~ITSDONE;
    628 	xs->error = XS_NOERROR;
    629 	xs->resid = xs->datalen;
    630 	xs->status = 0;
    631 
    632 retry:
    633 	/*
    634 	 * Do the transfer. If we are polling we will return:
    635 	 * COMPLETE,  Was poll, and scsipi_done has been called
    636 	 * TRY_AGAIN_LATER, Adapter short resources, try again
    637 	 *
    638 	 * if under full steam (interrupts) it will return:
    639 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
    640 	 * TRY_AGAIN_LATER, (as for polling)
    641 	 * After the wakeup, we must still check if it succeeded
    642 	 *
    643 	 * If we have a SCSI_NOSLEEP (typically because we have a buf)
    644 	 * we just return.  All the error proccessing and the buffer
    645 	 * code both expect us to return straight to them, so as soon
    646 	 * as the command is queued, return.
    647 	 */
    648 #ifdef SCSIDEBUG
    649 	if (xs->sc_link->flags & SDEV_DB3) {
    650 		printf("scsipi_exec_cmd: ");
    651 		show_scsipi_xs(xs);
    652 		printf("\n");
    653 	}
    654 #endif
    655 	async = SCSIPI_XFER_ASYNC(xs);
    656 	switch (scsipi_command_direct(xs)) {
    657 	case SUCCESSFULLY_QUEUED:
    658 		if (async) {
    659 			/* scsipi_done() will free the scsipi_xfer. */
    660 			return (EJUSTRETURN);
    661 		}
    662 #ifdef DIAGNOSTIC
    663 		if (xs->flags & SCSI_NOSLEEP)
    664 			panic("scsipi_execute_xs: NOSLEEP and POLL");
    665 #endif
    666 		s = splbio();
    667 		while ((xs->flags & ITSDONE) == 0)
    668 			tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
    669 		splx(s);
    670 	case COMPLETE:		/* Polling command completed ok */
    671 		if (xs->bp)
    672 			return (0);
    673 	doit:
    674 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
    675 		if ((error = sc_err1(xs, 0)) != ERESTART)
    676 			return (error);
    677 		goto retry;
    678 
    679 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
    680 		xs->error = XS_BUSY;
    681 		goto doit;
    682 
    683 	default:
    684 		panic("scsipi_execute_xs: invalid return code");
    685 	}
    686 
    687 #ifdef DIAGNOSTIC
    688 	panic("scsipi_execute_xs: impossible");
    689 #endif
    690 	return (EINVAL);
    691 }
    692 
    693 int
    694 sc_err1(xs, async)
    695 	struct scsipi_xfer *xs;
    696 	int async;
    697 {
    698 	int error;
    699 
    700 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
    701 
    702 	/*
    703 	 * If it has a buf, we might be working with
    704 	 * a request from the buffer cache or some other
    705 	 * piece of code that requires us to process
    706 	 * errors at inetrrupt time. We have probably
    707 	 * been called by scsipi_done()
    708 	 */
    709 	switch (xs->error) {
    710 	case XS_NOERROR:	/* nearly always hit this one */
    711 		error = 0;
    712 		break;
    713 
    714 	case XS_SENSE:
    715 	case XS_SHORTSENSE:
    716 		if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
    717 		    ERESTART)
    718 			goto retry;
    719 		SC_DEBUG(xs->sc_link, SDEV_DB3,
    720 		    ("scsipi_interpret_sense returned %d\n", error));
    721 		break;
    722 
    723 	case XS_BUSY:
    724 		if (xs->retries) {
    725 			if ((xs->flags & SCSI_POLL) != 0)
    726 				delay(1000000);
    727 			else if ((xs->flags & SCSI_NOSLEEP) == 0)
    728 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
    729 			else
    730 #if 0
    731 				timeout(scsipi_requeue, xs, hz);
    732 #else
    733 				goto lose;
    734 #endif
    735 		}
    736 	case XS_TIMEOUT:
    737 	retry:
    738 		if (xs->retries) {
    739 			xs->retries--;
    740 			xs->error = XS_NOERROR;
    741 			xs->flags &= ~ITSDONE;
    742 			return (ERESTART);
    743 		}
    744 	case XS_DRIVER_STUFFUP:
    745 	lose:
    746 		error = EIO;
    747 		break;
    748 
    749 	case XS_SELTIMEOUT:
    750 		/* XXX Disable device? */
    751 		error = EIO;
    752 		break;
    753 
    754 	case XS_RESET:
    755 		if (xs->retries) {
    756 			SC_DEBUG(xs->sc_link, SDEV_DB3,
    757 			    ("restarting command destroyed by reset\n"));
    758 			goto retry;
    759 		}
    760 		error = EIO;
    761 		break;
    762 
    763 	default:
    764 		(*xs->sc_link->sc_print_addr)(xs->sc_link);
    765 		printf("unknown error category from scsipi driver\n");
    766 		error = EIO;
    767 		break;
    768 	}
    769 
    770 	return (error);
    771 }
    772 
    773 /*
    774  * Add a reference to the adapter pointed to by the provided
    775  * link, enabling the adapter if necessary.
    776  */
    777 int
    778 scsipi_adapter_addref(link)
    779 	struct scsipi_link *link;
    780 {
    781 	struct scsipi_adapter *adapter = link->adapter;
    782 	int s, error = 0;
    783 
    784 	s = splbio();
    785 	if (adapter->scsipi_refcnt++ == 0 &&
    786 	    adapter->scsipi_enable != NULL) {
    787 		error = (*adapter->scsipi_enable)(link->adapter_softc, 1);
    788 		if (error)
    789 			adapter->scsipi_refcnt--;
    790 	}
    791 	splx(s);
    792 	return (error);
    793 }
    794 
    795 /*
    796  * Delete a reference to the adapter pointed to by the provided
    797  * link, disabling the adapter if possible.
    798  */
    799 void
    800 scsipi_adapter_delref(link)
    801 	struct scsipi_link *link;
    802 {
    803 	struct scsipi_adapter *adapter = link->adapter;
    804 	int s;
    805 
    806 	s = splbio();
    807 	if (adapter->scsipi_refcnt-- == 1 &&
    808 	    adapter->scsipi_enable != NULL)
    809 		(void) (*adapter->scsipi_enable)(link->adapter_softc, 0);
    810 	splx(s);
    811 }
    812 
    813 #ifdef	SCSIDEBUG
    814 /*
    815  * Given a scsipi_xfer, dump the request, in all it's glory
    816  */
    817 void
    818 show_scsipi_xs(xs)
    819 	struct scsipi_xfer *xs;
    820 {
    821 
    822 	printf("xs(%p): ", xs);
    823 	printf("flg(0x%x)", xs->flags);
    824 	printf("sc_link(%p)", xs->sc_link);
    825 	printf("retr(0x%x)", xs->retries);
    826 	printf("timo(0x%x)", xs->timeout);
    827 	printf("cmd(%p)", xs->cmd);
    828 	printf("len(0x%x)", xs->cmdlen);
    829 	printf("data(%p)", xs->data);
    830 	printf("len(0x%x)", xs->datalen);
    831 	printf("res(0x%x)", xs->resid);
    832 	printf("err(0x%x)", xs->error);
    833 	printf("bp(%p)", xs->bp);
    834 	show_scsipi_cmd(xs);
    835 }
    836 
    837 void
    838 show_scsipi_cmd(xs)
    839 	struct scsipi_xfer *xs;
    840 {
    841 	u_char *b = (u_char *) xs->cmd;
    842 	int i = 0;
    843 
    844 	(*xs->sc_link->sc_print_addr)(xs->sc_link);
    845 	printf("command: ");
    846 
    847 	if ((xs->flags & SCSI_RESET) == 0) {
    848 		while (i < xs->cmdlen) {
    849 			if (i)
    850 				printf(",");
    851 			printf("0x%x", b[i++]);
    852 		}
    853 		printf("-[%d bytes]\n", xs->datalen);
    854 		if (xs->datalen)
    855 			show_mem(xs->data, min(64, xs->datalen));
    856 	} else
    857 		printf("-RESET-\n");
    858 }
    859 
    860 void
    861 show_mem(address, num)
    862 	u_char *address;
    863 	int num;
    864 {
    865 	int x;
    866 
    867 	printf("------------------------------");
    868 	for (x = 0; x < num; x++) {
    869 		if ((x % 16) == 0)
    870 			printf("\n%03d: ", x);
    871 		printf("%02x ", *address++);
    872 	}
    873 	printf("\n------------------------------\n");
    874 }
    875 #endif /*SCSIDEBUG */
    876