Home | History | Annotate | Line # | Download | only in ic
uha.c revision 1.6
      1 /*	$NetBSD: uha.c,v 1.6 1996/12/10 21:28:02 thorpej Exp $	*/
      2 
      3 #undef UHADEBUG
      4 #ifdef DDB
      5 #define	integrate
      6 #else
      7 #define	integrate	static inline
      8 #endif
      9 
     10 /*
     11  * Copyright (c) 1994, 1996 Charles M. Hannum.  All rights reserved.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by Charles M. Hannum.
     24  * 4. The name of the author may not be used to endorse or promote products
     25  *    derived from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Ported for use with the UltraStor 14f by Gary Close (gclose (at) wvnvms.wvnet.edu)
     41  * Slight fixes to timeouts to run with the 34F
     42  * Thanks to Julian Elischer for advice and help with this port.
     43  *
     44  * Originally written by Julian Elischer (julian (at) tfs.com)
     45  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     46  *
     47  * TRW Financial Systems, in accordance with their agreement with Carnegie
     48  * Mellon University, makes this software available to CMU to distribute
     49  * or use in any manner that they see fit as long as this message is kept with
     50  * the software. For this reason TFS also grants any other persons or
     51  * organisations permission to use or modify this software.
     52  *
     53  * TFS supplies this software to be publicly redistributed
     54  * on the understanding that TFS is not responsible for the correct
     55  * functioning of this software in any circumstances.
     56  *
     57  * commenced: Sun Sep 27 18:14:01 PDT 1992
     58  * slight mod to make work with 34F as well: Wed Jun  2 18:05:48 WST 1993
     59  */
     60 
     61 #include <sys/types.h>
     62 #include <sys/param.h>
     63 #include <sys/systm.h>
     64 #include <sys/kernel.h>
     65 #include <sys/errno.h>
     66 #include <sys/ioctl.h>
     67 #include <sys/device.h>
     68 #include <sys/malloc.h>
     69 #include <sys/buf.h>
     70 #include <sys/proc.h>
     71 #include <sys/user.h>
     72 
     73 #include <machine/bus.h>
     74 #include <machine/intr.h>
     75 
     76 #include <scsi/scsi_all.h>
     77 #include <scsi/scsiconf.h>
     78 
     79 #include <dev/ic/uhareg.h>
     80 #include <dev/ic/uhavar.h>
     81 
     82 #ifndef	DDB
     83 #define Debugger() panic("should call debugger here (uha.c)")
     84 #endif /* ! DDB */
     85 
     86 #define KVTOPHYS(x)	vtophys(x)
     87 
     88 integrate void uha_reset_mscp __P((struct uha_softc *, struct uha_mscp *));
     89 void uha_free_mscp __P((struct uha_softc *, struct uha_mscp *));
     90 integrate void uha_init_mscp __P((struct uha_softc *, struct uha_mscp *));
     91 struct uha_mscp *uha_get_mscp __P((struct uha_softc *, int));
     92 void uhaminphys __P((struct buf *));
     93 int uha_scsi_cmd __P((struct scsi_xfer *));
     94 
     95 struct scsi_adapter uha_switch = {
     96 	uha_scsi_cmd,
     97 	uhaminphys,
     98 	0,
     99 	0,
    100 };
    101 
    102 /* the below structure is so we have a default dev struct for out link struct */
    103 struct scsi_device uha_dev = {
    104 	NULL,			/* Use default error handler */
    105 	NULL,			/* have a queue, served by this */
    106 	NULL,			/* have no async handler */
    107 	NULL,			/* Use default 'done' routine */
    108 };
    109 
    110 struct cfdriver uha_cd = {
    111 	NULL, "uha", DV_DULL
    112 };
    113 
    114 #define	UHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    115 
    116 /*
    117  * Attach all the sub-devices we can find
    118  */
    119 void
    120 uha_attach(sc)
    121 	struct uha_softc *sc;
    122 {
    123 
    124 	(sc->init)(sc);
    125 	TAILQ_INIT(&sc->sc_free_mscp);
    126 
    127 	/*
    128 	 * fill in the prototype scsi_link.
    129 	 */
    130 	sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
    131 	sc->sc_link.adapter_softc = sc;
    132 	sc->sc_link.adapter_target = sc->sc_scsi_dev;
    133 	sc->sc_link.adapter = &uha_switch;
    134 	sc->sc_link.device = &uha_dev;
    135 	sc->sc_link.openings = 2;
    136 	sc->sc_link.max_target = 7;
    137 
    138 	/*
    139 	 * ask the adapter what subunits are present
    140 	 */
    141 	config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    142 }
    143 
    144 integrate void
    145 uha_reset_mscp(sc, mscp)
    146 	struct uha_softc *sc;
    147 	struct uha_mscp *mscp;
    148 {
    149 
    150 	mscp->flags = 0;
    151 }
    152 
    153 /*
    154  * A mscp (and hence a mbx-out) is put onto the free list.
    155  */
    156 void
    157 uha_free_mscp(sc, mscp)
    158 	struct uha_softc *sc;
    159 	struct uha_mscp *mscp;
    160 {
    161 	int s;
    162 
    163 	s = splbio();
    164 
    165 	uha_reset_mscp(sc, mscp);
    166 	TAILQ_INSERT_HEAD(&sc->sc_free_mscp, mscp, chain);
    167 
    168 	/*
    169 	 * If there were none, wake anybody waiting for one to come free,
    170 	 * starting with queued entries.
    171 	 */
    172 	if (mscp->chain.tqe_next == 0)
    173 		wakeup(&sc->sc_free_mscp);
    174 
    175 	splx(s);
    176 }
    177 
    178 integrate void
    179 uha_init_mscp(sc, mscp)
    180 	struct uha_softc *sc;
    181 	struct uha_mscp *mscp;
    182 {
    183 	int hashnum;
    184 
    185 	bzero(mscp, sizeof(struct uha_mscp));
    186 	/*
    187 	 * put in the phystokv hash table
    188 	 * Never gets taken out.
    189 	 */
    190 	mscp->hashkey = KVTOPHYS(mscp);
    191 	hashnum = MSCP_HASH(mscp->hashkey);
    192 	mscp->nexthash = sc->sc_mscphash[hashnum];
    193 	sc->sc_mscphash[hashnum] = mscp;
    194 	uha_reset_mscp(sc, mscp);
    195 }
    196 
    197 /*
    198  * Get a free mscp
    199  *
    200  * If there are none, see if we can allocate a new one.  If so, put it in the
    201  * hash table too otherwise either return an error or sleep.
    202  */
    203 struct uha_mscp *
    204 uha_get_mscp(sc, flags)
    205 	struct uha_softc *sc;
    206 	int flags;
    207 {
    208 	struct uha_mscp *mscp;
    209 	int s;
    210 
    211 	s = splbio();
    212 
    213 	/*
    214 	 * If we can and have to, sleep waiting for one to come free
    215 	 * but only if we can't allocate a new one
    216 	 */
    217 	for (;;) {
    218 		mscp = sc->sc_free_mscp.tqh_first;
    219 		if (mscp) {
    220 			TAILQ_REMOVE(&sc->sc_free_mscp, mscp, chain);
    221 			break;
    222 		}
    223 		if (sc->sc_nummscps < UHA_MSCP_MAX) {
    224 			mscp = (struct uha_mscp *) malloc(sizeof(struct uha_mscp),
    225 			    M_TEMP, M_NOWAIT);
    226 			if (!mscp) {
    227 				printf("%s: can't malloc mscp\n",
    228 				    sc->sc_dev.dv_xname);
    229 				goto out;
    230 			}
    231 			uha_init_mscp(sc, mscp);
    232 			sc->sc_nummscps++;
    233 			break;
    234 		}
    235 		if ((flags & SCSI_NOSLEEP) != 0)
    236 			goto out;
    237 		tsleep(&sc->sc_free_mscp, PRIBIO, "uhamsc", 0);
    238 	}
    239 
    240 	mscp->flags |= MSCP_ALLOC;
    241 
    242 out:
    243 	splx(s);
    244 	return (mscp);
    245 }
    246 
    247 /*
    248  * given a physical address, find the mscp that it corresponds to.
    249  */
    250 struct uha_mscp *
    251 uha_mscp_phys_kv(sc, mscp_phys)
    252 	struct uha_softc *sc;
    253 	u_long mscp_phys;
    254 {
    255 	int hashnum = MSCP_HASH(mscp_phys);
    256 	struct uha_mscp *mscp = sc->sc_mscphash[hashnum];
    257 
    258 	while (mscp) {
    259 		if (mscp->hashkey == mscp_phys)
    260 			break;
    261 		mscp = mscp->nexthash;
    262 	}
    263 	return (mscp);
    264 }
    265 
    266 /*
    267  * We have a mscp which has been processed by the adaptor, now we look to see
    268  * how the operation went.
    269  */
    270 void
    271 uha_done(sc, mscp)
    272 	struct uha_softc *sc;
    273 	struct uha_mscp *mscp;
    274 {
    275 	struct scsi_sense_data *s1, *s2;
    276 	struct scsi_xfer *xs = mscp->xs;
    277 
    278 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("uha_done\n"));
    279 	/*
    280 	 * Otherwise, put the results of the operation
    281 	 * into the xfer and call whoever started it
    282 	 */
    283 	if ((mscp->flags & MSCP_ALLOC) == 0) {
    284 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
    285 		Debugger();
    286 		return;
    287 	}
    288 	if (xs->error == XS_NOERROR) {
    289 		if (mscp->host_stat != UHA_NO_ERR) {
    290 			switch (mscp->host_stat) {
    291 			case UHA_SBUS_TIMEOUT:		/* No response */
    292 				xs->error = XS_SELTIMEOUT;
    293 				break;
    294 			default:	/* Other scsi protocol messes */
    295 				printf("%s: host_stat %x\n",
    296 				    sc->sc_dev.dv_xname, mscp->host_stat);
    297 				xs->error = XS_DRIVER_STUFFUP;
    298 			}
    299 		} else if (mscp->target_stat != SCSI_OK) {
    300 			switch (mscp->target_stat) {
    301 			case SCSI_CHECK:
    302 				s1 = &mscp->mscp_sense;
    303 				s2 = &xs->sense;
    304 				*s2 = *s1;
    305 				xs->error = XS_SENSE;
    306 				break;
    307 			case SCSI_BUSY:
    308 				xs->error = XS_BUSY;
    309 				break;
    310 			default:
    311 				printf("%s: target_stat %x\n",
    312 				    sc->sc_dev.dv_xname, mscp->target_stat);
    313 				xs->error = XS_DRIVER_STUFFUP;
    314 			}
    315 		} else
    316 			xs->resid = 0;
    317 	}
    318 	uha_free_mscp(sc, mscp);
    319 	xs->flags |= ITSDONE;
    320 	scsi_done(xs);
    321 }
    322 
    323 void
    324 uhaminphys(bp)
    325 	struct buf *bp;
    326 {
    327 
    328 	if (bp->b_bcount > ((UHA_NSEG - 1) << PGSHIFT))
    329 		bp->b_bcount = ((UHA_NSEG - 1) << PGSHIFT);
    330 	minphys(bp);
    331 }
    332 
    333 /*
    334  * start a scsi operation given the command and the data address.  Also
    335  * needs the unit, target and lu.
    336  */
    337 int
    338 uha_scsi_cmd(xs)
    339 	struct scsi_xfer *xs;
    340 {
    341 	struct scsi_link *sc_link = xs->sc_link;
    342 	struct uha_softc *sc = sc_link->adapter_softc;
    343 	struct uha_mscp *mscp;
    344 	struct uha_dma_seg *sg;
    345 	int seg;		/* scatter gather seg being worked on */
    346 	u_long thiskv, thisphys, nextphys;
    347 	int bytes_this_seg, bytes_this_page, datalen, flags;
    348 	int s;
    349 
    350 	SC_DEBUG(sc_link, SDEV_DB2, ("uha_scsi_cmd\n"));
    351 	/*
    352 	 * get a mscp (mbox-out) to use. If the transfer
    353 	 * is from a buf (possibly from interrupt time)
    354 	 * then we can't allow it to sleep
    355 	 */
    356 	flags = xs->flags;
    357 	if ((mscp = uha_get_mscp(sc, flags)) == NULL) {
    358 		xs->error = XS_DRIVER_STUFFUP;
    359 		return (TRY_AGAIN_LATER);
    360 	}
    361 	mscp->xs = xs;
    362 	mscp->timeout = xs->timeout;
    363 
    364 	/*
    365 	 * Put all the arguments for the xfer in the mscp
    366 	 */
    367 	if (flags & SCSI_RESET) {
    368 		mscp->opcode = UHA_SDR;
    369 		mscp->ca = 0x01;
    370 	} else {
    371 		mscp->opcode = UHA_TSP;
    372 		/* XXX Not for tapes. */
    373 		mscp->ca = 0x01;
    374 		bcopy(xs->cmd, &mscp->scsi_cmd, mscp->scsi_cmd_length);
    375 	}
    376 	mscp->xdir = UHA_SDET;
    377 	mscp->dcn = 0x00;
    378 	mscp->chan = 0x00;
    379 	mscp->target = sc_link->target;
    380 	mscp->lun = sc_link->lun;
    381 	mscp->scsi_cmd_length = xs->cmdlen;
    382 	mscp->sense_ptr = KVTOPHYS(&mscp->mscp_sense);
    383 	mscp->req_sense_length = sizeof(mscp->mscp_sense);
    384 	mscp->host_stat = 0x00;
    385 	mscp->target_stat = 0x00;
    386 
    387 	if (xs->datalen) {
    388 		sg = mscp->uha_dma;
    389 		seg = 0;
    390 #ifdef	TFS
    391 		if (flags & SCSI_DATA_UIO) {
    392 			struct iovec *iovp;
    393 			iovp = ((struct uio *) xs->data)->uio_iov;
    394 			datalen = ((struct uio *) xs->data)->uio_iovcnt;
    395 			xs->datalen = 0;
    396 			while (datalen && seg < UHA_NSEG) {
    397 				sg->seg_addr = (physaddr)iovp->iov_base;
    398 				sg->seg_len = iovp->iov_len;
    399 				xs->datalen += iovp->iov_len;
    400 				SC_DEBUGN(sc_link, SDEV_DB4, ("(0x%x@0x%x)",
    401 				    iovp->iov_len, iovp->iov_base));
    402 				sg++;
    403 				iovp++;
    404 				seg++;
    405 				datalen--;
    406 			}
    407 		} else
    408 #endif /*TFS */
    409 		{
    410 			/*
    411 			 * Set up the scatter gather block
    412 			 */
    413 			SC_DEBUG(sc_link, SDEV_DB4,
    414 			    ("%d @0x%x:- ", xs->datalen, xs->data));
    415 			datalen = xs->datalen;
    416 			thiskv = (int) xs->data;
    417 			thisphys = KVTOPHYS(thiskv);
    418 
    419 			while (datalen && seg < UHA_NSEG) {
    420 				bytes_this_seg = 0;
    421 
    422 				/* put in the base address */
    423 				sg->seg_addr = thisphys;
    424 
    425 				SC_DEBUGN(sc_link, SDEV_DB4, ("0x%x", thisphys));
    426 
    427 				/* do it at least once */
    428 				nextphys = thisphys;
    429 				while (datalen && thisphys == nextphys) {
    430 					/*
    431 					 * This page is contiguous (physically)
    432 					 * with the the last, just extend the
    433 					 * length
    434 					 */
    435 					/* how far to the end of the page */
    436 					nextphys = (thisphys & ~PGOFSET) + NBPG;
    437 					bytes_this_page = nextphys - thisphys;
    438 					/**** or the data ****/
    439 					bytes_this_page = min(bytes_this_page,
    440 							      datalen);
    441 					bytes_this_seg += bytes_this_page;
    442 					datalen -= bytes_this_page;
    443 
    444 					/* get more ready for the next page */
    445 					thiskv = (thiskv & ~PGOFSET) + NBPG;
    446 					if (datalen)
    447 						thisphys = KVTOPHYS(thiskv);
    448 				}
    449 				/*
    450 				 * next page isn't contiguous, finish the seg
    451 				 */
    452 				SC_DEBUGN(sc_link, SDEV_DB4,
    453 				    ("(0x%x)", bytes_this_seg));
    454 				sg->seg_len = bytes_this_seg;
    455 				sg++;
    456 				seg++;
    457 			}
    458 		}
    459 		/* end of iov/kv decision */
    460 		SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
    461 		if (datalen) {
    462 			/*
    463 			 * there's still data, must have run out of segs!
    464 			 */
    465 			printf("%s: uha_scsi_cmd, more than %d dma segs\n",
    466 			    sc->sc_dev.dv_xname, UHA_NSEG);
    467 			goto bad;
    468 		}
    469 		mscp->data_addr = KVTOPHYS(mscp->uha_dma);
    470 		mscp->data_length = xs->datalen;
    471 		mscp->sgth = 0x01;
    472 		mscp->sg_num = seg;
    473 	} else {		/* No data xfer, use non S/G values */
    474 		mscp->data_addr = (physaddr)0;
    475 		mscp->data_length = 0;
    476 		mscp->sgth = 0x00;
    477 		mscp->sg_num = 0;
    478 	}
    479 	mscp->link_id = 0;
    480 	mscp->link_addr = (physaddr)0;
    481 
    482 	s = splbio();
    483 	(sc->start_mbox)(sc, mscp);
    484 	splx(s);
    485 
    486 	/*
    487 	 * Usually return SUCCESSFULLY QUEUED
    488 	 */
    489 	if ((flags & SCSI_POLL) == 0)
    490 		return (SUCCESSFULLY_QUEUED);
    491 
    492 	/*
    493 	 * If we can't use interrupts, poll on completion
    494 	 */
    495 	if ((sc->poll)(sc, xs, mscp->timeout)) {
    496 		uha_timeout(mscp);
    497 		if ((sc->poll)(sc, xs, mscp->timeout))
    498 			uha_timeout(mscp);
    499 	}
    500 	return (COMPLETE);
    501 
    502 bad:
    503 	xs->error = XS_DRIVER_STUFFUP;
    504 	uha_free_mscp(sc, mscp);
    505 	return (COMPLETE);
    506 }
    507 
    508 void
    509 uha_timeout(arg)
    510 	void *arg;
    511 {
    512 	struct uha_mscp *mscp = arg;
    513 	struct scsi_xfer *xs = mscp->xs;
    514 	struct scsi_link *sc_link = xs->sc_link;
    515 	struct uha_softc *sc = sc_link->adapter_softc;
    516 	int s;
    517 
    518 	sc_print_addr(sc_link);
    519 	printf("timed out");
    520 
    521 	s = splbio();
    522 
    523 	if (mscp->flags & MSCP_ABORT) {
    524 		/* abort timed out */
    525 		printf(" AGAIN\n");
    526 		/* XXX Must reset! */
    527 	} else {
    528 		/* abort the operation that has timed out */
    529 		printf("\n");
    530 		mscp->xs->error = XS_TIMEOUT;
    531 		mscp->timeout = UHA_ABORT_TIMEOUT;
    532 		mscp->flags |= MSCP_ABORT;
    533 		(sc->start_mbox)(sc, mscp);
    534 	}
    535 
    536 	splx(s);
    537 }
    538