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