Home | History | Annotate | Line # | Download | only in scsipi
scsipi_base.c revision 1.20.2.3
      1 /*	$NetBSD: scsipi_base.c,v 1.20.2.3 2000/01/16 17:49:42 he 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 			/*
    304 			 * If we're probing and the device indicates LUNs
    305 			 * aren't supported, shut up about it.
    306 			 */
    307 			if ((xs->flags & SCSI_PROBE) != 0 &&
    308 			    sense->add_sense_code == 0x25 &&
    309 			    sense->add_sense_code_qual == 0x00)
    310 				    	return (EINVAL);
    311 			error = EINVAL;
    312 			break;
    313 		case SKEY_UNIT_ATTENTION:
    314 			if (sense->add_sense_code == 0x29 &&
    315 			    sense->add_sense_code_qual == 0x00)
    316 				return (ERESTART); /* device or bus reset */
    317 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    318 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    319 			if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
    320 				/* XXX Should reupload any transient state. */
    321 				(sc_link->flags & SDEV_REMOVABLE) == 0)
    322 				return (ERESTART);
    323 			if ((xs->flags & SCSI_SILENT) != 0)
    324 				return (EIO);
    325 			error = EIO;
    326 			break;
    327 		case SKEY_WRITE_PROTECT:
    328 			error = EROFS;
    329 			break;
    330 		case SKEY_BLANK_CHECK:
    331 			error = 0;
    332 			break;
    333 		case SKEY_ABORTED_COMMAND:
    334 			error = ERESTART;
    335 			break;
    336 		case SKEY_VOLUME_OVERFLOW:
    337 			error = ENOSPC;
    338 			break;
    339 		default:
    340 			error = EIO;
    341 			break;
    342 		}
    343 
    344 #ifdef SCSIVERBOSE
    345 		if ((xs->flags & SCSI_SILENT) == 0)
    346 			scsipi_print_sense(xs, 0);
    347 #else
    348 		if (key) {
    349 			sc_link->sc_print_addr(sc_link);
    350 			printf("%s", error_mes[key - 1]);
    351 			if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    352 				switch (key) {
    353 				case SKEY_NOT_READY:
    354 				case SKEY_ILLEGAL_REQUEST:
    355 				case SKEY_UNIT_ATTENTION:
    356 				case SKEY_WRITE_PROTECT:
    357 					break;
    358 				case SKEY_BLANK_CHECK:
    359 					printf(", requested size: %d (decimal)",
    360 					    info);
    361 					break;
    362 				case SKEY_ABORTED_COMMAND:
    363 					if (xs->retries)
    364 						printf(", retrying");
    365 					printf(", cmd 0x%x, info 0x%x",
    366 					    xs->cmd->opcode, info);
    367 					break;
    368 				default:
    369 					printf(", info = %d (decimal)", info);
    370 				}
    371 			}
    372 			if (sense->extra_len != 0) {
    373 				int n;
    374 				printf(", data =");
    375 				for (n = 0; n < sense->extra_len; n++)
    376 					printf(" %02x",
    377 					    sense->cmd_spec_info[n]);
    378 			}
    379 			printf("\n");
    380 		}
    381 #endif
    382 		return (error);
    383 
    384 	/*
    385 	 * Not code 70, just report it
    386 	 */
    387 	default:
    388 		sc_link->sc_print_addr(sc_link);
    389 		printf("Sense Error Code 0x%x",
    390 			sense->error_code & SSD_ERRCODE);
    391 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    392 			struct scsipi_sense_data_unextended *usense =
    393 			    (struct scsipi_sense_data_unextended *)sense;
    394 			printf(" at block no. %d (decimal)",
    395 			    _3btol(usense->block));
    396 		}
    397 		printf("\n");
    398 		return (EIO);
    399 	}
    400 }
    401 
    402 /*
    403  * Find out from the device what its capacity is.
    404  */
    405 u_long
    406 scsipi_size(sc_link, flags)
    407 	struct scsipi_link *sc_link;
    408 	int flags;
    409 {
    410 	struct scsipi_read_cap_data rdcap;
    411 	struct scsipi_read_capacity scsipi_cmd;
    412 
    413 	/*
    414 	 * make up a scsipi command and ask the scsipi driver to do
    415 	 * it for you.
    416 	 */
    417 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    418 	scsipi_cmd.opcode = READ_CAPACITY;
    419 
    420 	/*
    421 	 * If the command works, interpret the result as a 4 byte
    422 	 * number of blocks
    423 	 */
    424 	if (scsipi_command(sc_link, (struct scsipi_generic *)&scsipi_cmd,
    425 	    sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
    426 	    2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
    427 		sc_link->sc_print_addr(sc_link);
    428 		printf("could not get size\n");
    429 		return (0);
    430 	}
    431 
    432 	return (_4btol(rdcap.addr) + 1);
    433 }
    434 
    435 /*
    436  * Get scsipi driver to send a "are you ready?" command
    437  */
    438 int
    439 scsipi_test_unit_ready(sc_link, flags)
    440 	struct scsipi_link *sc_link;
    441 	int flags;
    442 {
    443 	struct scsipi_test_unit_ready scsipi_cmd;
    444 
    445 	/* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
    446 	if (sc_link->quirks & ADEV_NOTUR)
    447 		return (0);
    448 
    449 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    450 	scsipi_cmd.opcode = TEST_UNIT_READY;
    451 
    452 	return (scsipi_command(sc_link,
    453 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
    454 	    0, 0, 2, 10000, NULL, flags));
    455 }
    456 
    457 /*
    458  * Do a scsipi operation asking a device what it is
    459  * Use the scsipi_cmd routine in the switch table.
    460  * XXX actually this is only used for scsi devices, because I have the feeling
    461  * that some atapi CDROM may not implement it, althouh it marked as mandatory
    462  * in the atapi specs.
    463  */
    464 int
    465 scsipi_inquire(sc_link, inqbuf, flags)
    466 	struct scsipi_link *sc_link;
    467 	struct scsipi_inquiry_data *inqbuf;
    468 	int flags;
    469 {
    470 	struct scsipi_inquiry scsipi_cmd;
    471 
    472 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    473 	scsipi_cmd.opcode = INQUIRY;
    474 	scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
    475 
    476 	return (scsipi_command(sc_link,
    477 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    478 	    (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data),
    479 	    2, 10000, NULL, SCSI_DATA_IN | flags));
    480 }
    481 
    482 /*
    483  * Prevent or allow the user to remove the media
    484  */
    485 int
    486 scsipi_prevent(sc_link, type, flags)
    487 	struct scsipi_link *sc_link;
    488 	int type, flags;
    489 {
    490 	struct scsipi_prevent scsipi_cmd;
    491 
    492 	if (sc_link->quirks & ADEV_NODOORLOCK)
    493 		return (0);
    494 
    495 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    496 	scsipi_cmd.opcode = PREVENT_ALLOW;
    497 	scsipi_cmd.how = type;
    498 	return (scsipi_command(sc_link,
    499 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    500 	    0, 0, 2, 5000, NULL, flags));
    501 }
    502 
    503 /*
    504  * Get scsipi driver to send a "start up" command
    505  */
    506 int
    507 scsipi_start(sc_link, type, flags)
    508 	struct scsipi_link *sc_link;
    509 	int type, flags;
    510 {
    511 	struct scsipi_start_stop scsipi_cmd;
    512 
    513 	if (sc_link->quirks & SDEV_NOSTARTUNIT)
    514 		return 0;
    515 
    516 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    517 	scsipi_cmd.opcode = START_STOP;
    518 	scsipi_cmd.byte2 = 0x00;
    519 	scsipi_cmd.how = type;
    520 	return (scsipi_command(sc_link,
    521 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    522 	    0, 0, 2, (type & SSS_START) ? 30000 : 10000, NULL, flags));
    523 }
    524 
    525 /*
    526  * This routine is called by the scsipi interrupt when the transfer is
    527  * complete.
    528  */
    529 void
    530 scsipi_done(xs)
    531 	struct scsipi_xfer *xs;
    532 {
    533 	struct scsipi_link *sc_link = xs->sc_link;
    534 	struct buf *bp;
    535 	int error;
    536 
    537 	SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
    538 #ifdef	SCSIDEBUG
    539 	if ((sc_link->flags & SDEV_DB1) != 0)
    540 		show_scsipi_cmd(xs);
    541 #endif /* SCSIDEBUG */
    542 
    543 	/*
    544 	 * If it's a user level request, bypass all usual completion
    545 	 * processing, let the user work it out..  We take
    546 	 * reponsibility for freeing the xs when the user returns.
    547 	 * (and restarting the device's queue).
    548 	 */
    549 	if ((xs->flags & SCSI_USER) != 0) {
    550 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
    551 		scsipi_user_done(xs); /* to take a copy of the sense etc. */
    552 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
    553 
    554 		/*
    555 		 * If this was an asynchronous operation (i.e. adapter
    556 		 * returned SUCCESSFULLY_QUEUED when the command was
    557 		 * submitted), we need to free the scsipi_xfer here.
    558 		 */
    559 		if (SCSIPI_XFER_ASYNC(xs))
    560 			scsipi_free_xs(xs, SCSI_NOSLEEP);
    561 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
    562 		return;
    563 	}
    564 
    565 	if (!SCSIPI_XFER_ASYNC(xs)) {
    566 		/*
    567 		 * if it's a normal upper level request, then ask
    568 		 * the upper level code to handle error checking
    569 		 * rather than doing it here at interrupt time
    570 		 */
    571 		wakeup(xs);
    572 		return;
    573 	}
    574 
    575 	/*
    576 	 * Go and handle errors now.
    577 	 * If it returns ERESTART then we should RETRY
    578 	 */
    579 retry:
    580 	error = sc_err1(xs, 1);
    581 	if (error == ERESTART) {
    582 		switch (scsipi_command_direct(xs)) {
    583 		case SUCCESSFULLY_QUEUED:
    584 			return;
    585 
    586 		case TRY_AGAIN_LATER:
    587 			xs->error = XS_BUSY;
    588 		case COMPLETE:
    589 			goto retry;
    590 		}
    591 	}
    592 
    593 	bp = xs->bp;
    594 	if (bp) {
    595 		if (error) {
    596 			bp->b_error = error;
    597 			bp->b_flags |= B_ERROR;
    598 			bp->b_resid = bp->b_bcount;
    599 		} else {
    600 			bp->b_error = 0;
    601 			bp->b_resid = xs->resid;
    602 		}
    603 	}
    604 	if (sc_link->device->done) {
    605 		/*
    606 		 * Tell the device the operation is actually complete.
    607 		 * No more will happen with this xfer.  This for
    608 		 * notification of the upper-level driver only; they
    609 		 * won't be returning any meaningful information to us.
    610 		 */
    611 		(*sc_link->device->done)(xs);
    612 	}
    613 	/*
    614 	 * If this was an asynchronous operation (i.e. adapter
    615 	 * returned SUCCESSFULLY_QUEUED when the command was
    616 	 * submitted), we need to free the scsipi_xfer here.
    617 	 */
    618 	if (SCSIPI_XFER_ASYNC(xs)) {
    619 		int s = splbio();
    620 		scsipi_free_xs(xs, SCSI_NOSLEEP);
    621 		splx(s);
    622 	}
    623 	if (bp)
    624 		biodone(bp);
    625 }
    626 
    627 int
    628 scsipi_execute_xs(xs)
    629 	struct scsipi_xfer *xs;
    630 {
    631 	int async;
    632 	int error;
    633 	int s;
    634 
    635 	xs->flags &= ~ITSDONE;
    636 	xs->error = XS_NOERROR;
    637 	xs->resid = xs->datalen;
    638 	xs->status = 0;
    639 
    640 retry:
    641 	/*
    642 	 * Do the transfer. If we are polling we will return:
    643 	 * COMPLETE,  Was poll, and scsipi_done has been called
    644 	 * TRY_AGAIN_LATER, Adapter short resources, try again
    645 	 *
    646 	 * if under full steam (interrupts) it will return:
    647 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
    648 	 * TRY_AGAIN_LATER, (as for polling)
    649 	 * After the wakeup, we must still check if it succeeded
    650 	 *
    651 	 * If we have a SCSI_NOSLEEP (typically because we have a buf)
    652 	 * we just return.  All the error proccessing and the buffer
    653 	 * code both expect us to return straight to them, so as soon
    654 	 * as the command is queued, return.
    655 	 */
    656 #ifdef SCSIDEBUG
    657 	if (xs->sc_link->flags & SDEV_DB3) {
    658 		printf("scsipi_exec_cmd: ");
    659 		show_scsipi_xs(xs);
    660 		printf("\n");
    661 	}
    662 #endif
    663 	async = SCSIPI_XFER_ASYNC(xs);
    664 	switch (scsipi_command_direct(xs)) {
    665 	case SUCCESSFULLY_QUEUED:
    666 		if (async) {
    667 			/* scsipi_done() will free the scsipi_xfer. */
    668 			return (EJUSTRETURN);
    669 		}
    670 #ifdef DIAGNOSTIC
    671 		if (xs->flags & SCSI_NOSLEEP)
    672 			panic("scsipi_execute_xs: NOSLEEP and POLL");
    673 #endif
    674 		s = splbio();
    675 		while ((xs->flags & ITSDONE) == 0)
    676 			tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
    677 		splx(s);
    678 	case COMPLETE:		/* Polling command completed ok */
    679 		if (xs->bp)
    680 			return (0);
    681 	doit:
    682 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
    683 		if ((error = sc_err1(xs, 0)) != ERESTART)
    684 			return (error);
    685 		goto retry;
    686 
    687 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
    688 		xs->error = XS_BUSY;
    689 		goto doit;
    690 
    691 	default:
    692 		panic("scsipi_execute_xs: invalid return code");
    693 	}
    694 
    695 #ifdef DIAGNOSTIC
    696 	panic("scsipi_execute_xs: impossible");
    697 #endif
    698 	return (EINVAL);
    699 }
    700 
    701 int
    702 sc_err1(xs, async)
    703 	struct scsipi_xfer *xs;
    704 	int async;
    705 {
    706 	int error;
    707 
    708 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
    709 
    710 	/*
    711 	 * If it has a buf, we might be working with
    712 	 * a request from the buffer cache or some other
    713 	 * piece of code that requires us to process
    714 	 * errors at inetrrupt time. We have probably
    715 	 * been called by scsipi_done()
    716 	 */
    717 	switch (xs->error) {
    718 	case XS_NOERROR:	/* nearly always hit this one */
    719 		error = 0;
    720 		break;
    721 
    722 	case XS_SENSE:
    723 	case XS_SHORTSENSE:
    724 		if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
    725 		    ERESTART)
    726 			goto retry;
    727 		SC_DEBUG(xs->sc_link, SDEV_DB3,
    728 		    ("scsipi_interpret_sense returned %d\n", error));
    729 		break;
    730 
    731 	case XS_BUSY:
    732 		if (xs->retries) {
    733 			if ((xs->flags & SCSI_POLL) != 0)
    734 				delay(1000000);
    735 			else if ((xs->flags & SCSI_NOSLEEP) == 0)
    736 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
    737 			else
    738 #if 0
    739 				timeout(scsipi_requeue, xs, hz);
    740 #else
    741 				goto lose;
    742 #endif
    743 		}
    744 	case XS_TIMEOUT:
    745 	retry:
    746 		if (xs->retries) {
    747 			xs->retries--;
    748 			xs->error = XS_NOERROR;
    749 			xs->flags &= ~ITSDONE;
    750 			return (ERESTART);
    751 		}
    752 	case XS_DRIVER_STUFFUP:
    753 	lose:
    754 		error = EIO;
    755 		break;
    756 
    757 	case XS_SELTIMEOUT:
    758 		/* XXX Disable device? */
    759 		error = EIO;
    760 		break;
    761 
    762 	case XS_RESET:
    763 		if (xs->retries) {
    764 			SC_DEBUG(xs->sc_link, SDEV_DB3,
    765 			    ("restarting command destroyed by reset\n"));
    766 			goto retry;
    767 		}
    768 		error = EIO;
    769 		break;
    770 
    771 	default:
    772 		(*xs->sc_link->sc_print_addr)(xs->sc_link);
    773 		printf("unknown error category from scsipi driver\n");
    774 		error = EIO;
    775 		break;
    776 	}
    777 
    778 	return (error);
    779 }
    780 
    781 /*
    782  * Add a reference to the adapter pointed to by the provided
    783  * link, enabling the adapter if necessary.
    784  */
    785 int
    786 scsipi_adapter_addref(link)
    787 	struct scsipi_link *link;
    788 {
    789 	struct scsipi_adapter *adapter = link->adapter;
    790 	int s, error = 0;
    791 
    792 	s = splbio();
    793 	if (adapter->scsipi_refcnt++ == 0 &&
    794 	    adapter->scsipi_enable != NULL) {
    795 		error = (*adapter->scsipi_enable)(link->adapter_softc, 1);
    796 		if (error)
    797 			adapter->scsipi_refcnt--;
    798 	}
    799 	splx(s);
    800 	return (error);
    801 }
    802 
    803 /*
    804  * Delete a reference to the adapter pointed to by the provided
    805  * link, disabling the adapter if possible.
    806  */
    807 void
    808 scsipi_adapter_delref(link)
    809 	struct scsipi_link *link;
    810 {
    811 	struct scsipi_adapter *adapter = link->adapter;
    812 	int s;
    813 
    814 	s = splbio();
    815 	if (adapter->scsipi_refcnt-- == 1 &&
    816 	    adapter->scsipi_enable != NULL)
    817 		(void) (*adapter->scsipi_enable)(link->adapter_softc, 0);
    818 	splx(s);
    819 }
    820 
    821 #ifdef	SCSIDEBUG
    822 /*
    823  * Given a scsipi_xfer, dump the request, in all it's glory
    824  */
    825 void
    826 show_scsipi_xs(xs)
    827 	struct scsipi_xfer *xs;
    828 {
    829 
    830 	printf("xs(%p): ", xs);
    831 	printf("flg(0x%x)", xs->flags);
    832 	printf("sc_link(%p)", xs->sc_link);
    833 	printf("retr(0x%x)", xs->retries);
    834 	printf("timo(0x%x)", xs->timeout);
    835 	printf("cmd(%p)", xs->cmd);
    836 	printf("len(0x%x)", xs->cmdlen);
    837 	printf("data(%p)", xs->data);
    838 	printf("len(0x%x)", xs->datalen);
    839 	printf("res(0x%x)", xs->resid);
    840 	printf("err(0x%x)", xs->error);
    841 	printf("bp(%p)", xs->bp);
    842 	show_scsipi_cmd(xs);
    843 }
    844 
    845 void
    846 show_scsipi_cmd(xs)
    847 	struct scsipi_xfer *xs;
    848 {
    849 	u_char *b = (u_char *) xs->cmd;
    850 	int i = 0;
    851 
    852 	(*xs->sc_link->sc_print_addr)(xs->sc_link);
    853 	printf("command: ");
    854 
    855 	if ((xs->flags & SCSI_RESET) == 0) {
    856 		while (i < xs->cmdlen) {
    857 			if (i)
    858 				printf(",");
    859 			printf("0x%x", b[i++]);
    860 		}
    861 		printf("-[%d bytes]\n", xs->datalen);
    862 		if (xs->datalen)
    863 			show_mem(xs->data, min(64, xs->datalen));
    864 	} else
    865 		printf("-RESET-\n");
    866 }
    867 
    868 void
    869 show_mem(address, num)
    870 	u_char *address;
    871 	int num;
    872 {
    873 	int x;
    874 
    875 	printf("------------------------------");
    876 	for (x = 0; x < num; x++) {
    877 		if ((x % 16) == 0)
    878 			printf("\n%03d: ", x);
    879 		printf("%02x ", *address++);
    880 	}
    881 	printf("\n------------------------------\n");
    882 }
    883 #endif /*SCSIDEBUG */
    884