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