Home | History | Annotate | Line # | Download | only in scsipi
scsipi_base.c revision 1.6
      1 /*	$NetBSD: scsipi_base.c,v 1.6 1998/07/31 03:00:51 thorpej 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 	 * zero's out the command, as ATAPI may use longer commands
    121 	 * than SCSI
    122 	 */
    123 	bzero(&xs->cmdstore, sizeof(xs->cmdstore));
    124 	xs->flags = INUSE | flags;
    125 	return (xs);
    126 }
    127 
    128 /*
    129  * Given a scsipi_xfer struct, and a device (referenced through sc_link)
    130  * return the struct to the free pool and credit the device with it
    131  * If another process is waiting for an xs, do a wakeup, let it proceed
    132  *
    133  * MUST BE CALLED AT splbio()!!
    134  */
    135 void
    136 scsipi_free_xs(xs, flags)
    137 	struct scsipi_xfer *xs;
    138 	int flags;
    139 {
    140 	struct scsipi_link *sc_link = xs->sc_link;
    141 
    142 	xs->flags &= ~INUSE;
    143 	pool_put(&scsipi_xfer_pool, xs);
    144 
    145 	SC_DEBUG(sc_link, SDEV_DB3, ("scsipi_free_xs\n"));
    146 	/* if was 0 and someone waits, wake them up */
    147 	sc_link->openings++;
    148 	if ((sc_link->flags & SDEV_WAITING) != 0) {
    149 		sc_link->flags &= ~SDEV_WAITING;
    150 		wakeup(sc_link);
    151 	} else {
    152 		if (sc_link->device->start) {
    153 			SC_DEBUG(sc_link, SDEV_DB2,
    154 			    ("calling private start()\n"));
    155 			(*(sc_link->device->start))(sc_link->device_softc);
    156 		}
    157 	}
    158 }
    159 
    160 /*
    161  * Find out from the device what its capacity is.
    162  */
    163 u_long
    164 scsipi_size(sc_link, flags)
    165 	struct scsipi_link *sc_link;
    166 	int flags;
    167 {
    168 	struct scsipi_read_cap_data rdcap;
    169 	struct scsipi_read_capacity scsipi_cmd;
    170 
    171 	/*
    172 	 * make up a scsipi command and ask the scsipi driver to do
    173 	 * it for you.
    174 	 */
    175 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    176 	scsipi_cmd.opcode = READ_CAPACITY;
    177 
    178 	/*
    179 	 * If the command works, interpret the result as a 4 byte
    180 	 * number of blocks
    181 	 */
    182 	if (scsipi_command(sc_link, (struct scsipi_generic *)&scsipi_cmd,
    183 	    sizeof(scsipi_cmd), (u_char *)&rdcap, sizeof(rdcap),
    184 	    2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
    185 		sc_link->sc_print_addr(sc_link);
    186 		printf("could not get size\n");
    187 		return (0);
    188 	}
    189 
    190 	return (_4btol(rdcap.addr) + 1);
    191 }
    192 
    193 /*
    194  * Get scsipi driver to send a "are you ready?" command
    195  */
    196 int
    197 scsipi_test_unit_ready(sc_link, flags)
    198 	struct scsipi_link *sc_link;
    199 	int flags;
    200 {
    201 	struct scsipi_test_unit_ready scsipi_cmd;
    202 
    203 	/* some ATAPI drives don't support TEST_UNIT_READY. Sigh */
    204 	if (sc_link->quirks & ADEV_NOTUR)
    205 		return (0);
    206 
    207 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    208 	scsipi_cmd.opcode = TEST_UNIT_READY;
    209 
    210 	return (scsipi_command(sc_link,
    211 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
    212 	    0, 0, 2, 10000, NULL, flags));
    213 }
    214 
    215 /*
    216  * Do a scsipi operation asking a device what it is
    217  * Use the scsipi_cmd routine in the switch table.
    218  * XXX actually this is only used for scsi devices, because I have the feeling
    219  * that some atapi CDROM may not implement it, althouh it marked as mandatory
    220  * in the atapi specs.
    221  */
    222 int
    223 scsipi_inquire(sc_link, inqbuf, flags)
    224 	struct scsipi_link *sc_link;
    225 	struct scsipi_inquiry_data *inqbuf;
    226 	int flags;
    227 {
    228 	struct scsipi_inquiry scsipi_cmd;
    229 
    230 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    231 	scsipi_cmd.opcode = INQUIRY;
    232 	scsipi_cmd.length = sizeof(struct scsipi_inquiry_data);
    233 
    234 	return (scsipi_command(sc_link,
    235 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    236 	    (u_char *) inqbuf, sizeof(struct scsipi_inquiry_data),
    237 	    2, 10000, NULL, SCSI_DATA_IN | flags));
    238 }
    239 
    240 /*
    241  * Prevent or allow the user to remove the media
    242  */
    243 int
    244 scsipi_prevent(sc_link, type, flags)
    245 	struct scsipi_link *sc_link;
    246 	int type, flags;
    247 {
    248 	struct scsipi_prevent scsipi_cmd;
    249 
    250 	if (sc_link->quirks & ADEV_NODOORLOCK)
    251 		return (0);
    252 
    253 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    254 	scsipi_cmd.opcode = PREVENT_ALLOW;
    255 	scsipi_cmd.how = type;
    256 	return (scsipi_command(sc_link,
    257 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    258 	    0, 0, 2, 5000, NULL, flags));
    259 }
    260 
    261 /*
    262  * Get scsipi driver to send a "start up" command
    263  */
    264 int
    265 scsipi_start(sc_link, type, flags)
    266 	struct scsipi_link *sc_link;
    267 	int type, flags;
    268 {
    269 	struct scsipi_start_stop scsipi_cmd;
    270 
    271 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    272 	scsipi_cmd.opcode = START_STOP;
    273 	scsipi_cmd.byte2 = 0x00;
    274 	scsipi_cmd.how = type;
    275 	return (scsipi_command(sc_link,
    276 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
    277 	    0, 0, 2, type == SSS_START ? 30000 : 10000, NULL, flags));
    278 }
    279 
    280 /*
    281  * This routine is called by the scsipi interrupt when the transfer is
    282  * complete.
    283  */
    284 void
    285 scsipi_done(xs)
    286 	struct scsipi_xfer *xs;
    287 {
    288 	struct scsipi_link *sc_link = xs->sc_link;
    289 	struct buf *bp;
    290 	int error;
    291 
    292 	SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_done\n"));
    293 #ifdef	SCSIDEBUG
    294 	if ((sc_link->flags & SDEV_DB1) != 0)
    295 		show_scsipi_cmd(xs);
    296 #endif /* SCSIDEBUG */
    297 
    298 	/*
    299 	 * If it's a user level request, bypass all usual completion
    300 	 * processing, let the user work it out..  We take
    301 	 * reponsibility for freeing the xs when the user returns.
    302 	 * (and restarting the device's queue).
    303 	 */
    304 	if ((xs->flags & SCSI_USER) != 0) {
    305 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
    306 		scsipi_user_done(xs); /* to take a copy of the sense etc. */
    307 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
    308 
    309 		scsipi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
    310 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
    311 		return;
    312 	}
    313 
    314 	if (!((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)) {
    315 		/*
    316 		 * if it's a normal upper level request, then ask
    317 		 * the upper level code to handle error checking
    318 		 * rather than doing it here at interrupt time
    319 		 */
    320 		wakeup(xs);
    321 		return;
    322 	}
    323 
    324 	/*
    325 	 * Go and handle errors now.
    326 	 * If it returns ERESTART then we should RETRY
    327 	 */
    328 retry:
    329 	error = sc_err1(xs, 1);
    330 	if (error == ERESTART)
    331 		switch (scsipi_command_direct(xs)) {
    332 		case SUCCESSFULLY_QUEUED:
    333 			return;
    334 
    335 		case TRY_AGAIN_LATER:
    336 			xs->error = XS_BUSY;
    337 		case COMPLETE:
    338 			goto retry;
    339 		}
    340 
    341 	bp = xs->bp;
    342 	if (bp) {
    343 		if (error) {
    344 			bp->b_error = error;
    345 			bp->b_flags |= B_ERROR;
    346 			bp->b_resid = bp->b_bcount;
    347 		} else {
    348 			bp->b_error = 0;
    349 			bp->b_resid = xs->resid;
    350 		}
    351 	}
    352 	if (sc_link->device->done) {
    353 		/*
    354 		 * Tell the device the operation is actually complete.
    355 		 * No more will happen with this xfer.  This for
    356 		 * notification of the upper-level driver only; they
    357 		 * won't be returning any meaningful information to us.
    358 		 */
    359 		(*sc_link->device->done)(xs);
    360 	}
    361 	scsipi_free_xs(xs, SCSI_NOSLEEP);
    362 	if (bp)
    363 		biodone(bp);
    364 }
    365 
    366 int
    367 scsipi_execute_xs(xs)
    368 	struct scsipi_xfer *xs;
    369 {
    370 	int error;
    371 	int s;
    372 
    373 	xs->flags &= ~ITSDONE;
    374 	xs->error = XS_NOERROR;
    375 	xs->resid = xs->datalen;
    376 	xs->status = 0;
    377 
    378 retry:
    379 	/*
    380 	 * Do the transfer. If we are polling we will return:
    381 	 * COMPLETE,  Was poll, and scsipi_done has been called
    382 	 * TRY_AGAIN_LATER, Adapter short resources, try again
    383 	 *
    384 	 * if under full steam (interrupts) it will return:
    385 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
    386 	 * TRY_AGAIN_LATER, (as for polling)
    387 	 * After the wakeup, we must still check if it succeeded
    388 	 *
    389 	 * If we have a SCSI_NOSLEEP (typically because we have a buf)
    390 	 * we just return.  All the error proccessing and the buffer
    391 	 * code both expect us to return straight to them, so as soon
    392 	 * as the command is queued, return.
    393 	 */
    394 #ifdef SCSIDEBUG
    395 	if (xs->sc_link->flags & SDEV_DB3) {
    396 		printf("scsipi_exec_cmd: ");
    397 		show_scsipi_xs(xs);
    398 		printf("\n");
    399 	}
    400 #endif
    401 	switch (scsipi_command_direct(xs)) {
    402 	case SUCCESSFULLY_QUEUED:
    403 		if ((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)
    404 			return (EJUSTRETURN);
    405 #ifdef DIAGNOSTIC
    406 		if (xs->flags & SCSI_NOSLEEP)
    407 			panic("scsipi_execute_xs: NOSLEEP and POLL");
    408 #endif
    409 		s = splbio();
    410 		while ((xs->flags & ITSDONE) == 0)
    411 			tsleep(xs, PRIBIO + 1, "scsipi_cmd", 0);
    412 		splx(s);
    413 	case COMPLETE:		/* Polling command completed ok */
    414 		if (xs->bp)
    415 			return (EJUSTRETURN);
    416 	doit:
    417 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
    418 		if ((error = sc_err1(xs, 0)) != ERESTART)
    419 			return (error);
    420 		goto retry;
    421 
    422 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
    423 		xs->error = XS_BUSY;
    424 		goto doit;
    425 
    426 	default:
    427 		panic("scsipi_execute_xs: invalid return code");
    428 	}
    429 
    430 #ifdef DIAGNOSTIC
    431 	panic("scsipi_execute_xs: impossible");
    432 #endif
    433 	return (EINVAL);
    434 }
    435 
    436 int
    437 sc_err1(xs, async)
    438 	struct scsipi_xfer *xs;
    439 	int async;
    440 {
    441 	int error;
    442 
    443 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
    444 
    445 	/*
    446 	 * If it has a buf, we might be working with
    447 	 * a request from the buffer cache or some other
    448 	 * piece of code that requires us to process
    449 	 * errors at inetrrupt time. We have probably
    450 	 * been called by scsipi_done()
    451 	 */
    452 	switch (xs->error) {
    453 	case XS_NOERROR:	/* nearly always hit this one */
    454 		error = 0;
    455 		break;
    456 
    457 	case XS_SENSE:
    458 		if ((error = (*xs->sc_link->scsipi_interpret_sense)(xs)) ==
    459 		    ERESTART)
    460 			goto retry;
    461 		SC_DEBUG(xs->sc_link, SDEV_DB3,
    462 		    ("scsipi_interpret_sense returned %d\n", error));
    463 		break;
    464 
    465 	case XS_BUSY:
    466 		if (xs->retries) {
    467 			if ((xs->flags & SCSI_POLL) != 0)
    468 				delay(1000000);
    469 			else if ((xs->flags & SCSI_NOSLEEP) == 0)
    470 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
    471 			else
    472 #if 0
    473 				timeout(scsipi_requeue, xs, hz);
    474 #else
    475 				goto lose;
    476 #endif
    477 		}
    478 	case XS_TIMEOUT:
    479 	retry:
    480 		if (xs->retries--) {
    481 			xs->error = XS_NOERROR;
    482 			xs->flags &= ~ITSDONE;
    483 			return (ERESTART);
    484 		}
    485 	case XS_DRIVER_STUFFUP:
    486 	lose:
    487 		error = EIO;
    488 		break;
    489 
    490 	case XS_SELTIMEOUT:
    491 		/* XXX Disable device? */
    492 		error = EIO;
    493 		break;
    494 
    495 	default:
    496 		(*xs->sc_link->sc_print_addr)(xs->sc_link);
    497 		printf("unknown error category from scsipi driver\n");
    498 		error = EIO;
    499 		break;
    500 	}
    501 
    502 	return (error);
    503 }
    504 
    505 #ifdef	SCSIDEBUG
    506 /*
    507  * Given a scsipi_xfer, dump the request, in all it's glory
    508  */
    509 void
    510 show_scsipi_xs(xs)
    511 	struct scsipi_xfer *xs;
    512 {
    513 
    514 	printf("xs(%p): ", xs);
    515 	printf("flg(0x%x)", xs->flags);
    516 	printf("sc_link(%p)", xs->sc_link);
    517 	printf("retr(0x%x)", xs->retries);
    518 	printf("timo(0x%x)", xs->timeout);
    519 	printf("cmd(%p)", xs->cmd);
    520 	printf("len(0x%x)", xs->cmdlen);
    521 	printf("data(%p)", xs->data);
    522 	printf("len(0x%x)", xs->datalen);
    523 	printf("res(0x%x)", xs->resid);
    524 	printf("err(0x%x)", xs->error);
    525 	printf("bp(%p)", xs->bp);
    526 	show_scsipi_cmd(xs);
    527 }
    528 
    529 void
    530 show_scsipi_cmd(xs)
    531 	struct scsipi_xfer *xs;
    532 {
    533 	u_char *b = (u_char *) xs->cmd;
    534 	int i = 0;
    535 
    536 	(*xs->sc_link->sc_print_addr)(xs->sc_link);
    537 	printf("command: ");
    538 
    539 	if ((xs->flags & SCSI_RESET) == 0) {
    540 		while (i < xs->cmdlen) {
    541 			if (i)
    542 				printf(",");
    543 			printf("0x%x", b[i++]);
    544 		}
    545 		printf("-[%d bytes]\n", xs->datalen);
    546 		if (xs->datalen)
    547 			show_mem(xs->data, min(64, xs->datalen));
    548 	} else
    549 		printf("-RESET-\n");
    550 }
    551 
    552 void
    553 show_mem(address, num)
    554 	u_char *address;
    555 	int num;
    556 {
    557 	int x;
    558 
    559 	printf("------------------------------");
    560 	for (x = 0; x < num; x++) {
    561 		if ((x % 16) == 0)
    562 			printf("\n%03d: ", x);
    563 		printf("%02x ", *address++);
    564 	}
    565 	printf("\n------------------------------\n");
    566 }
    567 #endif /*SCSIDEBUG */
    568 
    569