Home | History | Annotate | Line # | Download | only in scsipi
scsipi_base.c revision 1.7
      1 /*	$NetBSD: scsipi_base.c,v 1.7 1998/08/11 05:47:43 scottr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995, 1997 Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Charles M. Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Originally written by Julian Elischer (julian (at) dialix.oz.au)
     34  */
     35 
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/buf.h>
     41 #include <sys/uio.h>
     42 #include <sys/malloc.h>
     43 #include <sys/pool.h>
     44 #include <sys/errno.h>
     45 #include <sys/device.h>
     46 #include <sys/proc.h>
     47 
     48 #include <dev/scsipi/scsipi_all.h>
     49 #include <dev/scsipi/scsi_all.h>
     50 #include <dev/scsipi/scsipi_disk.h>
     51 #include <dev/scsipi/scsipiconf.h>
     52 #include <dev/scsipi/scsipi_base.h>
     53 
     54 struct pool scsipi_xfer_pool;
     55 
     56 int	sc_err1 __P((struct scsipi_xfer *, int));
     57 
     58 /*
     59  * Called when a scsibus is attached to initialize global data.
     60  */
     61 void
     62 scsipi_init()
     63 {
     64 	static int scsipi_init_done;
     65 
     66 	if (scsipi_init_done)
     67 		return;
     68 	scsipi_init_done = 1;
     69 
     70 	/* Initialize the scsipi_xfer pool. */
     71 	pool_init(&scsipi_xfer_pool, sizeof(struct scsipi_xfer), 0,
     72 	    0, 0, "scxspl", 0, NULL, NULL, M_DEVBUF);
     73 }
     74 
     75 /*
     76  * Get a scsipi transfer structure for the caller. Charge the structure
     77  * to the device that is referenced by the sc_link structure. If the
     78  * sc_link structure has no 'credits' then the device already has the
     79  * maximum number or outstanding operations under way. In this stage,
     80  * wait on the structure so that when one is freed, we are awoken again
     81  * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
     82  * a NULL pointer, signifying that no slots were available
     83  * Note in the link structure, that we are waiting on it.
     84  */
     85 
     86 struct scsipi_xfer *
     87 scsipi_get_xs(sc_link, flags)
     88 	struct scsipi_link *sc_link;	/* who to charge the xs to */
     89 	int flags;			/* if this call can sleep */
     90 {
     91 	struct scsipi_xfer *xs;
     92 	int s;
     93 
     94 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_get_xs\n"));
     95 
     96 	s = splbio();
     97 	while (sc_link->openings <= 0) {
     98 		SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
     99 		if ((flags & SCSI_NOSLEEP) != 0) {
    100 			splx(s);
    101 			return (0);
    102 		}
    103 		sc_link->flags |= SDEV_WAITING;
    104 		(void)tsleep(sc_link, PRIBIO, "getxs", 0);
    105 	}
    106 	SC_DEBUG(sc_link, SDEV_DB3, ("calling pool_get\n"));
    107 	xs = pool_get(&scsipi_xfer_pool,
    108 	    ((flags & SCSI_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    109 	if (xs != NULL)
    110 		sc_link->openings--;
    111 	else {
    112 		(*sc_link->sc_print_addr)(sc_link);
    113 		printf("cannot allocate scsipi xs\n");
    114 	}
    115 	splx(s);
    116 
    117 	SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
    118 
    119 	/*
    120 	 * zeroes out the command, as ATAPI may use longer commands
    121 	 * than SCSI
    122 	 */
    123 	if (xs != NULL) {
    124 		xs->flags = INUSE | flags;
    125 		bzero(&xs->cmdstore, sizeof(xs->cmdstore));
    126 	}
    127 	return (xs);
    128 }
    129 
    130 /*
    131  * Given a scsipi_xfer struct, and a device (referenced through sc_link)
    132  * return the struct to the free pool and credit the device with it
    133  * If another process is waiting for an xs, do a wakeup, let it proceed
    134  *
    135  * MUST BE CALLED AT splbio()!!
    136  */
    137 void
    138 scsipi_free_xs(xs, flags)
    139 	struct scsipi_xfer *xs;
    140 	int flags;
    141 {
    142 	struct scsipi_link *sc_link = xs->sc_link;
    143 
    144 	xs->flags &= ~INUSE;
    145 	pool_put(&scsipi_xfer_pool, xs);
    146 
    147 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_free_xs\n"));
    148 	/* if was 0 and someone waits, wake them up */
    149 	sc_link->openings++;
    150 	if ((sc_link->flags & SDEV_WAITING) != 0) {
    151 		sc_link->flags &= ~SDEV_WAITING;
    152 		wakeup(sc_link);
    153 	} else {
    154 		if (sc_link->device->start) {
    155 			SC_DEBUG(sc_link, SDEV_DB2,
    156 			    ("calling private start()\n"));
    157 			(*(sc_link->device->start))(sc_link->device_softc);
    158 		}
    159 	}
    160 }
    161 
    162 /*
    163  * Find out from the device what its capacity is.
    164  */
    165 u_long
    166 scsipi_size(sc_link, flags)
    167 	struct scsipi_link *sc_link;
    168 	int flags;
    169 {
    170 	struct scsipi_read_cap_data rdcap;
    171 	struct scsipi_read_capacity scsipi_cmd;
    172 
    173 	/*
    174 	 * make up a scsipi command and ask the scsipi driver to do
    175 	 * it for you.
    176 	 */
    177 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    178 	scsipi_cmd.opcode = READ_CAPACITY;
    179 
    180 	/*
    181 	 * If the command works, interpret the result as a 4 byte
    182 	 * number of blocks
    183 	 */
    184 	if (scsipi_command(sc_link, (struct scsipi_generic *)&scsipi_cmd,
    185 	    sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
    186 	    2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
    187 		sc_link->sc_print_addr(sc_link);
    188 		printf("could not get size\n");
    189 		return (0);
    190 	}
    191 
    192 	return (_4btol(rdcap.addr) + 1);
    193 }
    194 
    195 /*
    196  * Get scsipi driver to send a "are you ready?" command
    197  */
    198 int
    199 scsipi_test_unit_ready(sc_link, flags)
    200 	struct scsipi_link *sc_link;
    201 	int flags;
    202 {
    203 	struct scsipi_test_unit_ready scsipi_cmd;
    204 
    205 	/* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
    206 	if (sc_link->quirks & ADEV_NOTUR)
    207 		return (0);
    208 
    209 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    210 	scsipi_cmd.opcode = TEST_UNIT_READY;
    211 
    212 	return (scsipi_command(sc_link,
    213 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
    214 	    0, 0, 2, 10000, NULL, flags));
    215 }
    216 
    217 /*
    218  * Do a scsipi operation asking a device what it is
    219  * Use the scsipi_cmd routine in the switch table.
    220  * XXX actually this is only used for scsi devices, because I have the feeling
    221  * that some atapi CDROM may not implement it, althouh it marked as mandatory
    222  * in the atapi specs.
    223  */
    224 int
    225 scsipi_inquire(sc_link, inqbuf, flags)
    226 	struct scsipi_link *sc_link;
    227 	struct scsipi_inquiry_data *inqbuf;
    228 	int flags;
    229 {
    230 	struct scsipi_inquiry scsipi_cmd;
    231 
    232 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    233 	scsipi_cmd.opcode = INQUIRY;
    234 	scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
    235 
    236 	return (scsipi_command(sc_link,
    237 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    238 	    (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data),
    239 	    2, 10000, NULL, SCSI_DATA_IN | flags));
    240 }
    241 
    242 /*
    243  * Prevent or allow the user to remove the media
    244  */
    245 int
    246 scsipi_prevent(sc_link, type, flags)
    247 	struct scsipi_link *sc_link;
    248 	int type, flags;
    249 {
    250 	struct scsipi_prevent scsipi_cmd;
    251 
    252 	if (sc_link->quirks & ADEV_NODOORLOCK)
    253 		return (0);
    254 
    255 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    256 	scsipi_cmd.opcode = PREVENT_ALLOW;
    257 	scsipi_cmd.how = type;
    258 	return (scsipi_command(sc_link,
    259 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    260 	    0, 0, 2, 5000, NULL, flags));
    261 }
    262 
    263 /*
    264  * Get scsipi driver to send a "start up" command
    265  */
    266 int
    267 scsipi_start(sc_link, type, flags)
    268 	struct scsipi_link *sc_link;
    269 	int type, flags;
    270 {
    271 	struct scsipi_start_stop scsipi_cmd;
    272 
    273 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    274 	scsipi_cmd.opcode = START_STOP;
    275 	scsipi_cmd.byte2 = 0x00;
    276 	scsipi_cmd.how = type;
    277 	return (scsipi_command(sc_link,
    278 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    279 	    0, 0, 2, type == SSS_START ? 30000 : 10000, NULL, flags));
    280 }
    281 
    282 /*
    283  * This routine is called by the scsipi interrupt when the transfer is
    284  * complete.
    285  */
    286 void
    287 scsipi_done(xs)
    288 	struct scsipi_xfer *xs;
    289 {
    290 	struct scsipi_link *sc_link = xs->sc_link;
    291 	struct buf *bp;
    292 	int error;
    293 
    294 	SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
    295 #ifdef	SCSIDEBUG
    296 	if ((sc_link->flags & SDEV_DB1) != 0)
    297 		show_scsipi_cmd(xs);
    298 #endif /* SCSIDEBUG */
    299 
    300 	/*
    301 	 * If it's a user level request, bypass all usual completion
    302 	 * processing, let the user work it out..  We take
    303 	 * reponsibility for freeing the xs when the user returns.
    304 	 * (and restarting the device's queue).
    305 	 */
    306 	if ((xs->flags & SCSI_USER) != 0) {
    307 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
    308 		scsipi_user_done(xs); /* to take a copy of the sense etc. */
    309 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
    310 
    311 		scsipi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
    312 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
    313 		return;
    314 	}
    315 
    316 	if (!((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)) {
    317 		/*
    318 		 * if it's a normal upper level request, then ask
    319 		 * the upper level code to handle error checking
    320 		 * rather than doing it here at interrupt time
    321 		 */
    322 		wakeup(xs);
    323 		return;
    324 	}
    325 
    326 	/*
    327 	 * Go and handle errors now.
    328 	 * If it returns ERESTART then we should RETRY
    329 	 */
    330 retry:
    331 	error = sc_err1(xs, 1);
    332 	if (error == ERESTART)
    333 		switch (scsipi_command_direct(xs)) {
    334 		case SUCCESSFULLY_QUEUED:
    335 			return;
    336 
    337 		case TRY_AGAIN_LATER:
    338 			xs->error = XS_BUSY;
    339 		case COMPLETE:
    340 			goto retry;
    341 		}
    342 
    343 	bp = xs->bp;
    344 	if (bp) {
    345 		if (error) {
    346 			bp->b_error = error;
    347 			bp->b_flags |= B_ERROR;
    348 			bp->b_resid = bp->b_bcount;
    349 		} else {
    350 			bp->b_error = 0;
    351 			bp->b_resid = xs->resid;
    352 		}
    353 	}
    354 	if (sc_link->device->done) {
    355 		/*
    356 		 * Tell the device the operation is actually complete.
    357 		 * No more will happen with this xfer.  This for
    358 		 * notification of the upper-level driver only; they
    359 		 * won't be returning any meaningful information to us.
    360 		 */
    361 		(*sc_link->device->done)(xs);
    362 	}
    363 	scsipi_free_xs(xs, SCSI_NOSLEEP);
    364 	if (bp)
    365 		biodone(bp);
    366 }
    367 
    368 int
    369 scsipi_execute_xs(xs)
    370 	struct scsipi_xfer *xs;
    371 {
    372 	int error;
    373 	int s;
    374 
    375 	xs->flags &= ~ITSDONE;
    376 	xs->error = XS_NOERROR;
    377 	xs->resid = xs->datalen;
    378 	xs->status = 0;
    379 
    380 retry:
    381 	/*
    382 	 * Do the transfer. If we are polling we will return:
    383 	 * COMPLETE,  Was poll, and scsipi_done has been called
    384 	 * TRY_AGAIN_LATER, Adapter short resources, try again
    385 	 *
    386 	 * if under full steam (interrupts) it will return:
    387 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
    388 	 * TRY_AGAIN_LATER, (as for polling)
    389 	 * After the wakeup, we must still check if it succeeded
    390 	 *
    391 	 * If we have a SCSI_NOSLEEP (typically because we have a buf)
    392 	 * we just return.  All the error proccessing and the buffer
    393 	 * code both expect us to return straight to them, so as soon
    394 	 * as the command is queued, return.
    395 	 */
    396 #ifdef SCSIDEBUG
    397 	if (xs->sc_link->flags & SDEV_DB3) {
    398 		printf("scsipi_exec_cmd: ");
    399 		show_scsipi_xs(xs);
    400 		printf("\n");
    401 	}
    402 #endif
    403 	switch (scsipi_command_direct(xs)) {
    404 	case SUCCESSFULLY_QUEUED:
    405 		if ((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)
    406 			return (EJUSTRETURN);
    407 #ifdef DIAGNOSTIC
    408 		if (xs->flags & SCSI_NOSLEEP)
    409 			panic("scsipi_execute_xs: NOSLEEP and POLL");
    410 #endif
    411 		s = splbio();
    412 		while ((xs->flags & ITSDONE) == 0)
    413 			tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
    414 		splx(s);
    415 	case COMPLETE:		/* Polling command completed ok */
    416 		if (xs->bp)
    417 			return (EJUSTRETURN);
    418 	doit:
    419 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
    420 		if ((error = sc_err1(xs, 0)) != ERESTART)
    421 			return (error);
    422 		goto retry;
    423 
    424 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
    425 		xs->error = XS_BUSY;
    426 		goto doit;
    427 
    428 	default:
    429 		panic("scsipi_execute_xs: invalid return code");
    430 	}
    431 
    432 #ifdef DIAGNOSTIC
    433 	panic("scsipi_execute_xs: impossible");
    434 #endif
    435 	return (EINVAL);
    436 }
    437 
    438 int
    439 sc_err1(xs, async)
    440 	struct scsipi_xfer *xs;
    441 	int async;
    442 {
    443 	int error;
    444 
    445 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
    446 
    447 	/*
    448 	 * If it has a buf, we might be working with
    449 	 * a request from the buffer cache or some other
    450 	 * piece of code that requires us to process
    451 	 * errors at inetrrupt time. We have probably
    452 	 * been called by scsipi_done()
    453 	 */
    454 	switch (xs->error) {
    455 	case XS_NOERROR:	/* nearly always hit this one */
    456 		error = 0;
    457 		break;
    458 
    459 	case XS_SENSE:
    460 		if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
    461 		    ERESTART)
    462 			goto retry;
    463 		SC_DEBUG(xs->sc_link, SDEV_DB3,
    464 		    ("scsipi_interpret_sense returned %d\n", error));
    465 		break;
    466 
    467 	case XS_BUSY:
    468 		if (xs->retries) {
    469 			if ((xs->flags & SCSI_POLL) != 0)
    470 				delay(1000000);
    471 			else if ((xs->flags & SCSI_NOSLEEP) == 0)
    472 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
    473 			else
    474 #if 0
    475 				timeout(scsipi_requeue, xs, hz);
    476 #else
    477 				goto lose;
    478 #endif
    479 		}
    480 	case XS_TIMEOUT:
    481 	retry:
    482 		if (xs->retries--) {
    483 			xs->error = XS_NOERROR;
    484 			xs->flags &= ~ITSDONE;
    485 			return (ERESTART);
    486 		}
    487 	case XS_DRIVER_STUFFUP:
    488 	lose:
    489 		error = EIO;
    490 		break;
    491 
    492 	case XS_SELTIMEOUT:
    493 		/* XXX Disable device? */
    494 		error = EIO;
    495 		break;
    496 
    497 	default:
    498 		(*xs->sc_link->sc_print_addr)(xs->sc_link);
    499 		printf("unknown error category from scsipi driver\n");
    500 		error = EIO;
    501 		break;
    502 	}
    503 
    504 	return (error);
    505 }
    506 
    507 #ifdef	SCSIDEBUG
    508 /*
    509  * Given a scsipi_xfer, dump the request, in all it's glory
    510  */
    511 void
    512 show_scsipi_xs(xs)
    513 	struct scsipi_xfer *xs;
    514 {
    515 
    516 	printf("xs(%p): ", xs);
    517 	printf("flg(0x%x)", xs->flags);
    518 	printf("sc_link(%p)", xs->sc_link);
    519 	printf("retr(0x%x)", xs->retries);
    520 	printf("timo(0x%x)", xs->timeout);
    521 	printf("cmd(%p)", xs->cmd);
    522 	printf("len(0x%x)", xs->cmdlen);
    523 	printf("data(%p)", xs->data);
    524 	printf("len(0x%x)", xs->datalen);
    525 	printf("res(0x%x)", xs->resid);
    526 	printf("err(0x%x)", xs->error);
    527 	printf("bp(%p)", xs->bp);
    528 	show_scsipi_cmd(xs);
    529 }
    530 
    531 void
    532 show_scsipi_cmd(xs)
    533 	struct scsipi_xfer *xs;
    534 {
    535 	u_char *b = (u_char *) xs->cmd;
    536 	int i = 0;
    537 
    538 	(*xs->sc_link->sc_print_addr)(xs->sc_link);
    539 	printf("command: ");
    540 
    541 	if ((xs->flags & SCSI_RESET) == 0) {
    542 		while (i < xs->cmdlen) {
    543 			if (i)
    544 				printf(",");
    545 			printf("0x%x", b[i++]);
    546 		}
    547 		printf("-[%d bytes]\n", xs->datalen);
    548 		if (xs->datalen)
    549 			show_mem(xs->data, min(64, xs->datalen));
    550 	} else
    551 		printf("-RESET-\n");
    552 }
    553 
    554 void
    555 show_mem(address, num)
    556 	u_char *address;
    557 	int num;
    558 {
    559 	int x;
    560 
    561 	printf("------------------------------");
    562 	for (x = 0; x < num; x++) {
    563 		if ((x % 16) == 0)
    564 			printf("\n%03d: ", x);
    565 		printf("%02x ", *address++);
    566 	}
    567 	printf("\n------------------------------\n");
    568 }
    569 #endif /*SCSIDEBUG */
    570 
    571