Home | History | Annotate | Line # | Download | only in ic
uha.c revision 1.12
      1 /*	$NetBSD: uha.c,v 1.12 1998/01/12 09:23:36 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) 1997 The NetBSD Foundation, Inc.
     12  * All rights reserved.
     13  *
     14  * This code is derived from software contributed to The NetBSD Foundation
     15  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     16  * NASA Ames Research Center.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  * 3. All advertising materials mentioning features or use of this software
     27  *    must display the following acknowledgement:
     28  *	This product includes software developed by the NetBSD
     29  *	Foundation, Inc. and its contributors.
     30  * 4. Neither the name of The NetBSD Foundation nor the names of its
     31  *    contributors may be used to endorse or promote products derived
     32  *    from this software without specific prior written permission.
     33  *
     34  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     35  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     36  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     37  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     39  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     40  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     41  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     43  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     44  * POSSIBILITY OF SUCH DAMAGE.
     45  */
     46 
     47 /*
     48  * Copyright (c) 1994, 1996, 1997 Charles M. Hannum.  All rights reserved.
     49  *
     50  * Redistribution and use in source and binary forms, with or without
     51  * modification, are permitted provided that the following conditions
     52  * are met:
     53  * 1. Redistributions of source code must retain the above copyright
     54  *    notice, this list of conditions and the following disclaimer.
     55  * 2. Redistributions in binary form must reproduce the above copyright
     56  *    notice, this list of conditions and the following disclaimer in the
     57  *    documentation and/or other materials provided with the distribution.
     58  * 3. All advertising materials mentioning features or use of this software
     59  *    must display the following acknowledgement:
     60  *	This product includes software developed by Charles M. Hannum.
     61  * 4. The name of the author may not be used to endorse or promote products
     62  *    derived from this software without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     65  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     66  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     67  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     68  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     69  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     70  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     71  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     72  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     73  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     74  */
     75 
     76 /*
     77  * Ported for use with the UltraStor 14f by Gary Close (gclose (at) wvnvms.wvnet.edu)
     78  * Slight fixes to timeouts to run with the 34F
     79  * Thanks to Julian Elischer for advice and help with this port.
     80  *
     81  * Originally written by Julian Elischer (julian (at) tfs.com)
     82  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     83  *
     84  * TRW Financial Systems, in accordance with their agreement with Carnegie
     85  * Mellon University, makes this software available to CMU to distribute
     86  * or use in any manner that they see fit as long as this message is kept with
     87  * the software. For this reason TFS also grants any other persons or
     88  * organisations permission to use or modify this software.
     89  *
     90  * TFS supplies this software to be publicly redistributed
     91  * on the understanding that TFS is not responsible for the correct
     92  * functioning of this software in any circumstances.
     93  *
     94  * commenced: Sun Sep 27 18:14:01 PDT 1992
     95  * slight mod to make work with 34F as well: Wed Jun  2 18:05:48 WST 1993
     96  */
     97 
     98 #include <sys/types.h>
     99 #include <sys/param.h>
    100 #include <sys/systm.h>
    101 #include <sys/kernel.h>
    102 #include <sys/errno.h>
    103 #include <sys/ioctl.h>
    104 #include <sys/device.h>
    105 #include <sys/malloc.h>
    106 #include <sys/buf.h>
    107 #include <sys/proc.h>
    108 #include <sys/user.h>
    109 
    110 #include <machine/bus.h>
    111 #include <machine/intr.h>
    112 
    113 #include <dev/scsipi/scsi_all.h>
    114 #include <dev/scsipi/scsipi_all.h>
    115 #include <dev/scsipi/scsiconf.h>
    116 
    117 #include <dev/ic/uhareg.h>
    118 #include <dev/ic/uhavar.h>
    119 
    120 #ifndef	DDB
    121 #define Debugger() panic("should call debugger here (uha.c)")
    122 #endif /* ! DDB */
    123 
    124 #define	UHA_MAXXFER	((UHA_NSEG - 1) << PGSHIFT)
    125 
    126 integrate void uha_reset_mscp __P((struct uha_softc *, struct uha_mscp *));
    127 void uha_free_mscp __P((struct uha_softc *, struct uha_mscp *));
    128 integrate int uha_init_mscp __P((struct uha_softc *, struct uha_mscp *));
    129 struct uha_mscp *uha_get_mscp __P((struct uha_softc *, int));
    130 void uhaminphys __P((struct buf *));
    131 int uha_scsi_cmd __P((struct scsipi_xfer *));
    132 int uha_create_mscps __P((struct uha_softc *, void *, size_t));
    133 void uha_enqueue __P((struct uha_softc *, struct scsipi_xfer *, int));
    134 struct scsipi_xfer *uha_dequeue __P((struct uha_softc *));
    135 
    136 struct scsipi_adapter uha_switch = {
    137 	uha_scsi_cmd,
    138 	uhaminphys,
    139 	0,
    140 	0,
    141 };
    142 
    143 /* the below structure is so we have a default dev struct for out link struct */
    144 struct scsipi_device uha_dev = {
    145 	NULL,			/* Use default error handler */
    146 	NULL,			/* have a queue, served by this */
    147 	NULL,			/* have no async handler */
    148 	NULL,			/* Use default 'done' routine */
    149 };
    150 
    151 #define	UHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    152 
    153 /* XXX Should put this in a better place. */
    154 #define	offsetof(type, member)	((size_t)(&((type *)0)->member))
    155 
    156 /*
    157  * Insert a scsipi_xfer into the software queue.  We overload xs->free_list
    158  * to avoid having to allocate additional resources (since we're used
    159  * only during resource shortages anyhow.
    160  */
    161 void
    162 uha_enqueue(sc, xs, infront)
    163 	struct uha_softc *sc;
    164 	struct scsipi_xfer *xs;
    165 	int infront;
    166 {
    167 
    168 	if (infront || sc->sc_queue.lh_first == NULL) {
    169 		if (sc->sc_queue.lh_first == NULL)
    170 			sc->sc_queuelast = xs;
    171 		LIST_INSERT_HEAD(&sc->sc_queue, xs, free_list);
    172 		return;
    173 	}
    174 
    175 	LIST_INSERT_AFTER(sc->sc_queuelast, xs, free_list);
    176 	sc->sc_queuelast = xs;
    177 }
    178 
    179 /*
    180  * Pull a scsipi_xfer off the front of the software queue.
    181  */
    182 struct scsipi_xfer *
    183 uha_dequeue(sc)
    184 	struct uha_softc *sc;
    185 {
    186 	struct scsipi_xfer *xs;
    187 
    188 	xs = sc->sc_queue.lh_first;
    189 	LIST_REMOVE(xs, free_list);
    190 
    191 	if (sc->sc_queue.lh_first == NULL)
    192 		sc->sc_queuelast = NULL;
    193 
    194 	return (xs);
    195 }
    196 
    197 /*
    198  * Attach all the sub-devices we can find
    199  */
    200 void
    201 uha_attach(sc, upd)
    202 	struct uha_softc *sc;
    203 	struct uha_probe_data *upd;
    204 {
    205 
    206 	TAILQ_INIT(&sc->sc_free_mscp);
    207 	LIST_INIT(&sc->sc_queue);
    208 
    209 	(sc->init)(sc);
    210 
    211 	/*
    212 	 * fill in the prototype scsipi_link.
    213 	 */
    214 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    215 	sc->sc_link.adapter_softc = sc;
    216 	sc->sc_link.scsipi_scsi.adapter_target = upd->sc_scsi_dev;
    217 	sc->sc_link.adapter = &uha_switch;
    218 	sc->sc_link.device = &uha_dev;
    219 	sc->sc_link.openings = 2;
    220 	sc->sc_link.scsipi_scsi.max_target = 7;
    221 	sc->sc_link.type = BUS_SCSI;
    222 
    223 	/*
    224 	 * ask the adapter what subunits are present
    225 	 */
    226 	config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    227 }
    228 
    229 integrate void
    230 uha_reset_mscp(sc, mscp)
    231 	struct uha_softc *sc;
    232 	struct uha_mscp *mscp;
    233 {
    234 
    235 	mscp->flags = 0;
    236 }
    237 
    238 /*
    239  * A mscp (and hence a mbx-out) is put onto the free list.
    240  */
    241 void
    242 uha_free_mscp(sc, mscp)
    243 	struct uha_softc *sc;
    244 	struct uha_mscp *mscp;
    245 {
    246 	int s;
    247 
    248 	s = splbio();
    249 
    250 	uha_reset_mscp(sc, mscp);
    251 	TAILQ_INSERT_HEAD(&sc->sc_free_mscp, mscp, chain);
    252 
    253 	/*
    254 	 * If there were none, wake anybody waiting for one to come free,
    255 	 * starting with queued entries.
    256 	 */
    257 	if (mscp->chain.tqe_next == 0)
    258 		wakeup(&sc->sc_free_mscp);
    259 
    260 	splx(s);
    261 }
    262 
    263 integrate int
    264 uha_init_mscp(sc, mscp)
    265 	struct uha_softc *sc;
    266 	struct uha_mscp *mscp;
    267 {
    268 	bus_dma_tag_t dmat = sc->sc_dmat;
    269 	int hashnum, error;
    270 
    271 	/*
    272 	 * XXX Should we put a DIAGNOSTIC check for multiple
    273 	 * XXX MSCP inits here?
    274 	 */
    275 
    276 	bzero(mscp, sizeof(struct uha_mscp));
    277 
    278 	/*
    279 	 * Create the DMA maps for this MSCP.
    280 	 */
    281 	error = bus_dmamap_create(dmat, sizeof(struct uha_mscp), 1,
    282 	    sizeof(struct uha_mscp), 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
    283 	    &mscp->dmamap_self);
    284 	if (error) {
    285 		printf("%s: can't create mscp dmamap_self\n",
    286 		    sc->sc_dev.dv_xname);
    287 		return (error);
    288 	}
    289 
    290 	error = bus_dmamap_create(dmat, UHA_MAXXFER, UHA_NSEG, UHA_MAXXFER,
    291 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
    292 	    &mscp->dmamap_xfer);
    293 	if (error) {
    294 		printf("%s: can't create mscp dmamap_xfer\n",
    295 		    sc->sc_dev.dv_xname);
    296 		return (error);
    297 	}
    298 
    299 	/*
    300 	 * Load the permanent DMA maps.
    301 	 */
    302 	error = bus_dmamap_load(dmat, mscp->dmamap_self, mscp,
    303 	    sizeof(struct uha_mscp), NULL, BUS_DMA_NOWAIT);
    304 	if (error) {
    305 		printf("%s: can't load mscp dmamap_self\n",
    306 		    sc->sc_dev.dv_xname);
    307 		bus_dmamap_destroy(dmat, mscp->dmamap_self);
    308 		bus_dmamap_destroy(dmat, mscp->dmamap_xfer);
    309 		return (error);
    310 	}
    311 
    312 	/*
    313 	 * put in the phystokv hash table
    314 	 * Never gets taken out.
    315 	 */
    316 	mscp->hashkey = mscp->dmamap_self->dm_segs[0].ds_addr;
    317 	hashnum = MSCP_HASH(mscp->hashkey);
    318 	mscp->nexthash = sc->sc_mscphash[hashnum];
    319 	sc->sc_mscphash[hashnum] = mscp;
    320 	uha_reset_mscp(sc, mscp);
    321 	return (0);
    322 }
    323 
    324 /*
    325  * Create a set of MSCPs and add them to the free list.
    326  */
    327 int
    328 uha_create_mscps(sc, mem, size)
    329 	struct uha_softc *sc;
    330 	void *mem;
    331 	size_t size;
    332 {
    333 	bus_dma_segment_t seg;
    334 	struct uha_mscp *mscp;
    335 	int rseg, error;
    336 
    337 	if (sc->sc_nummscps >= UHA_MSCP_MAX)
    338 		return (0);
    339 
    340 	if ((mscp = mem) != NULL)
    341 		goto have_mem;
    342 
    343 	size = NBPG;
    344 	error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &seg, 1, &rseg,
    345 	    BUS_DMA_NOWAIT);
    346 	if (error) {
    347 		printf("%s: can't allocate memory for mscps\n",
    348 		    sc->sc_dev.dv_xname);
    349 		return (error);
    350 	}
    351 
    352 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    353 	    (caddr_t *)&mscp, BUS_DMA_NOWAIT|BUS_DMAMEM_NOSYNC);
    354 	if (error) {
    355 		printf("%s: can't map memory for mscps\n",
    356 		    sc->sc_dev.dv_xname);
    357 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    358 		return (error);
    359 	}
    360 
    361  have_mem:
    362 	bzero(mscp, size);
    363 	while (size > sizeof(struct uha_mscp) &&
    364 	    sc->sc_nummscps < UHA_MSCP_MAX) {
    365 		error = uha_init_mscp(sc, mscp);
    366 		if (error) {
    367 			printf("%s: can't initialize mscp\n",
    368 			    sc->sc_dev.dv_xname);
    369 			return (error);
    370 		}
    371 		TAILQ_INSERT_TAIL(&sc->sc_free_mscp, mscp, chain);
    372 		(caddr_t)mscp += ALIGN(sizeof(struct uha_mscp));
    373 		size -= ALIGN(sizeof(struct uha_mscp));
    374 		sc->sc_nummscps++;
    375 	}
    376 
    377 	return (0);
    378 }
    379 
    380 /*
    381  * Get a free mscp
    382  *
    383  * If there are none, see if we can allocate a new one.  If so, put it in the
    384  * hash table too otherwise either return an error or sleep.
    385  */
    386 struct uha_mscp *
    387 uha_get_mscp(sc, flags)
    388 	struct uha_softc *sc;
    389 	int flags;
    390 {
    391 	struct uha_mscp *mscp;
    392 	int s;
    393 
    394 	s = splbio();
    395 
    396 	/*
    397 	 * If we can and have to, sleep waiting for one to come free
    398 	 * but only if we can't allocate a new one
    399 	 */
    400 	for (;;) {
    401 		mscp = sc->sc_free_mscp.tqh_first;
    402 		if (mscp) {
    403 			TAILQ_REMOVE(&sc->sc_free_mscp, mscp, chain);
    404 			break;
    405 		}
    406 		if (sc->sc_nummscps < UHA_MSCP_MAX) {
    407 			/*
    408 			 * uha_create_mscps() might have managed to create
    409 			 * one before it failed.  If so, don't abort,
    410 			 * just grab it and continue to hobble along.
    411 			 */
    412 			if (uha_create_mscps(sc, NULL, 0) &&
    413 			    sc->sc_free_mscp.tqh_first == NULL) {
    414 				printf("%s: can't allocate mscps\n",
    415 				    sc->sc_dev.dv_xname);
    416 				goto out;
    417 			}
    418 			continue;
    419 		}
    420 		if ((flags & SCSI_NOSLEEP) != 0)
    421 			goto out;
    422 		tsleep(&sc->sc_free_mscp, PRIBIO, "uhamsc", 0);
    423 	}
    424 
    425 	mscp->flags |= MSCP_ALLOC;
    426 
    427 out:
    428 	splx(s);
    429 	return (mscp);
    430 }
    431 
    432 /*
    433  * given a physical address, find the mscp that it corresponds to.
    434  */
    435 struct uha_mscp *
    436 uha_mscp_phys_kv(sc, mscp_phys)
    437 	struct uha_softc *sc;
    438 	u_long mscp_phys;
    439 {
    440 	int hashnum = MSCP_HASH(mscp_phys);
    441 	struct uha_mscp *mscp = sc->sc_mscphash[hashnum];
    442 
    443 	while (mscp) {
    444 		if (mscp->hashkey == mscp_phys)
    445 			break;
    446 		mscp = mscp->nexthash;
    447 	}
    448 	return (mscp);
    449 }
    450 
    451 /*
    452  * We have a mscp which has been processed by the adaptor, now we look to see
    453  * how the operation went.
    454  */
    455 void
    456 uha_done(sc, mscp)
    457 	struct uha_softc *sc;
    458 	struct uha_mscp *mscp;
    459 {
    460 	bus_dma_tag_t dmat = sc->sc_dmat;
    461 	struct scsipi_sense_data *s1, *s2;
    462 	struct scsipi_xfer *xs = mscp->xs;
    463 
    464 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("uha_done\n"));
    465 
    466 	/*
    467 	 * If we were a data transfer, unload the map that described
    468 	 * the data buffer.
    469 	 */
    470 	if (xs->datalen) {
    471 		bus_dmamap_sync(dmat, mscp->dmamap_xfer,
    472 		    (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    473 		    BUS_DMASYNC_POSTWRITE);
    474 		bus_dmamap_unload(dmat, mscp->dmamap_xfer);
    475 	}
    476 
    477 	/*
    478 	 * Otherwise, put the results of the operation
    479 	 * into the xfer and call whoever started it
    480 	 */
    481 	if ((mscp->flags & MSCP_ALLOC) == 0) {
    482 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
    483 		Debugger();
    484 		return;
    485 	}
    486 	if (xs->error == XS_NOERROR) {
    487 		if (mscp->host_stat != UHA_NO_ERR) {
    488 			switch (mscp->host_stat) {
    489 			case UHA_SBUS_TIMEOUT:		/* No response */
    490 				xs->error = XS_SELTIMEOUT;
    491 				break;
    492 			default:	/* Other scsi protocol messes */
    493 				printf("%s: host_stat %x\n",
    494 				    sc->sc_dev.dv_xname, mscp->host_stat);
    495 				xs->error = XS_DRIVER_STUFFUP;
    496 			}
    497 		} else if (mscp->target_stat != SCSI_OK) {
    498 			switch (mscp->target_stat) {
    499 			case SCSI_CHECK:
    500 				s1 = &mscp->mscp_sense;
    501 				s2 = &xs->sense.scsi_sense;
    502 				*s2 = *s1;
    503 				xs->error = XS_SENSE;
    504 				break;
    505 			case SCSI_BUSY:
    506 				xs->error = XS_BUSY;
    507 				break;
    508 			default:
    509 				printf("%s: target_stat %x\n",
    510 				    sc->sc_dev.dv_xname, mscp->target_stat);
    511 				xs->error = XS_DRIVER_STUFFUP;
    512 			}
    513 		} else
    514 			xs->resid = 0;
    515 	}
    516 	uha_free_mscp(sc, mscp);
    517 	xs->flags |= ITSDONE;
    518 	scsipi_done(xs);
    519 
    520 	/*
    521 	 * If there are queue entries in the software queue, try to
    522 	 * run the first one.  We should be more or less guaranteed
    523 	 * to succeed, since we just freed an MSCP.
    524 	 *
    525 	 * NOTE: uha_scsi_cmd() relies on our calling it with
    526 	 * the first entry in the queue.
    527 	 */
    528 	if ((xs = sc->sc_queue.lh_first) != NULL)
    529 		(void) uha_scsi_cmd(xs);
    530 }
    531 
    532 void
    533 uhaminphys(bp)
    534 	struct buf *bp;
    535 {
    536 
    537 	if (bp->b_bcount > UHA_MAXXFER)
    538 		bp->b_bcount = UHA_MAXXFER;
    539 	minphys(bp);
    540 }
    541 
    542 /*
    543  * start a scsi operation given the command and the data address.  Also
    544  * needs the unit, target and lu.
    545  */
    546 int
    547 uha_scsi_cmd(xs)
    548 	struct scsipi_xfer *xs;
    549 {
    550 	struct scsipi_link *sc_link = xs->sc_link;
    551 	struct uha_softc *sc = sc_link->adapter_softc;
    552 	bus_dma_tag_t dmat = sc->sc_dmat;
    553 	struct uha_mscp *mscp;
    554 	struct uha_dma_seg *sg;
    555 	int error, seg, flags, s;
    556 	int fromqueue = 0, dontqueue = 0;
    557 
    558 	SC_DEBUG(sc_link, SDEV_DB2, ("uha_scsi_cmd\n"));
    559 
    560 	s = splbio();		/* protect the queue */
    561 
    562 	/*
    563 	 * If we're running the queue from bha_done(), we've been
    564 	 * called with the first queue entry as our argument.
    565 	 */
    566 	if (xs == sc->sc_queue.lh_first) {
    567 		xs = uha_dequeue(sc);
    568 		fromqueue = 1;
    569 		goto get_mscp;
    570 	}
    571 
    572 	/* Polled requests can't be queued for later. */
    573 	dontqueue = xs->flags & SCSI_POLL;
    574 
    575 	/*
    576 	 * If there are jobs in the queue, run them first.
    577 	 */
    578 	if (sc->sc_queue.lh_first != NULL) {
    579 		/*
    580 		 * If we can't queue, we have to abort, since
    581 		 * we have to preserve order.
    582 		 */
    583 		if (dontqueue) {
    584 			splx(s);
    585 			xs->error = XS_DRIVER_STUFFUP;
    586 			return (TRY_AGAIN_LATER);
    587 		}
    588 
    589 		/*
    590 		 * Swap with the first queue entry.
    591 		 */
    592 		uha_enqueue(sc, xs, 0);
    593 		xs = uha_dequeue(sc);
    594 		fromqueue = 1;
    595 	}
    596 
    597  get_mscp:
    598 	/*
    599 	 * get a mscp (mbox-out) to use. If the transfer
    600 	 * is from a buf (possibly from interrupt time)
    601 	 * then we can't allow it to sleep
    602 	 */
    603 	flags = xs->flags;
    604 	if ((mscp = uha_get_mscp(sc, flags)) == NULL) {
    605 		/*
    606 		 * If we can't queue, we lose.
    607 		 */
    608 		if (dontqueue) {
    609 			splx(s);
    610 			xs->error = XS_DRIVER_STUFFUP;
    611 			return (TRY_AGAIN_LATER);
    612 		}
    613 
    614 		/*
    615 		 * Stuff ourselves into the queue, in front
    616 		 * if we came off in the first place.
    617 		 */
    618 		uha_enqueue(sc, xs, fromqueue);
    619 		splx(s);
    620 		return (SUCCESSFULLY_QUEUED);
    621 	}
    622 
    623 	splx(s);		/* done playing with the queue */
    624 
    625 	mscp->xs = xs;
    626 	mscp->timeout = xs->timeout;
    627 
    628 	/*
    629 	 * Put all the arguments for the xfer in the mscp
    630 	 */
    631 	if (flags & SCSI_RESET) {
    632 		mscp->opcode = UHA_SDR;
    633 		mscp->ca = 0x01;
    634 	} else {
    635 		mscp->opcode = UHA_TSP;
    636 		/* XXX Not for tapes. */
    637 		mscp->ca = 0x01;
    638 		bcopy(xs->cmd, &mscp->scsi_cmd, mscp->scsi_cmd_length);
    639 	}
    640 	mscp->xdir = UHA_SDET;
    641 	mscp->dcn = 0x00;
    642 	mscp->chan = 0x00;
    643 	mscp->target = sc_link->scsipi_scsi.target;
    644 	mscp->lun = sc_link->scsipi_scsi.lun;
    645 	mscp->scsi_cmd_length = xs->cmdlen;
    646 	mscp->sense_ptr = mscp->dmamap_self->dm_segs[0].ds_addr +
    647 	    offsetof(struct uha_mscp, mscp_sense);
    648 	mscp->req_sense_length = sizeof(mscp->mscp_sense);
    649 	mscp->host_stat = 0x00;
    650 	mscp->target_stat = 0x00;
    651 
    652 	if (xs->datalen) {
    653 		sg = mscp->uha_dma;
    654 		seg = 0;
    655 #ifdef	TFS
    656 		if (flags & SCSI_DATA_UIO) {
    657 			error = bus_dmamap_load_uio(dmat,
    658 			    mscp->dmamap_xfer, (struct uio *)xs->data,
    659 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
    660 			    BUS_DMA_WAITOK);
    661 		} else
    662 #endif /*TFS */
    663 		{
    664 			error = bus_dmamap_load(dmat,
    665 			    mscp->dmamap_xfer, xs->data, xs->datalen, NULL,
    666 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
    667 			    BUS_DMA_WAITOK);
    668 		}
    669 
    670 		if (error) {
    671 			if (error == EFBIG) {
    672 				printf("%s: uha_scsi_cmd, more than %d"
    673 				    " dma segments\n",
    674 				    sc->sc_dev.dv_xname, UHA_NSEG);
    675 			} else {
    676 				printf("%s: uha_scsi_cmd, error %d loading"
    677 				    " dma map\n",
    678 				    sc->sc_dev.dv_xname, error);
    679 			}
    680 			goto bad;
    681 		}
    682 
    683 		bus_dmamap_sync(dmat, mscp->dmamap_xfer,
    684 		    (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
    685 		    BUS_DMASYNC_PREWRITE);
    686 
    687 		/*
    688 		 * Load the hardware scatter/gather map with the
    689 		 * contents of the DMA map.
    690 		 */
    691 		for (seg = 0; seg < mscp->dmamap_xfer->dm_nsegs; seg++) {
    692 			mscp->uha_dma[seg].seg_addr =
    693 			    mscp->dmamap_xfer->dm_segs[seg].ds_addr;
    694 			mscp->uha_dma[seg].seg_len =
    695 			    mscp->dmamap_xfer->dm_segs[seg].ds_len;
    696 		}
    697 
    698 		mscp->data_addr = mscp->dmamap_self->dm_segs[0].ds_addr +
    699 		    offsetof(struct uha_mscp, uha_dma);
    700 		mscp->data_length = xs->datalen;
    701 		mscp->sgth = 0x01;
    702 		mscp->sg_num = seg;
    703 	} else {		/* No data xfer, use non S/G values */
    704 		mscp->data_addr = (physaddr)0;
    705 		mscp->data_length = 0;
    706 		mscp->sgth = 0x00;
    707 		mscp->sg_num = 0;
    708 	}
    709 	mscp->link_id = 0;
    710 	mscp->link_addr = (physaddr)0;
    711 
    712 	s = splbio();
    713 	(sc->start_mbox)(sc, mscp);
    714 	splx(s);
    715 
    716 	/*
    717 	 * Usually return SUCCESSFULLY QUEUED
    718 	 */
    719 	if ((flags & SCSI_POLL) == 0)
    720 		return (SUCCESSFULLY_QUEUED);
    721 
    722 	/*
    723 	 * If we can't use interrupts, poll on completion
    724 	 */
    725 	if ((sc->poll)(sc, xs, mscp->timeout)) {
    726 		uha_timeout(mscp);
    727 		if ((sc->poll)(sc, xs, mscp->timeout))
    728 			uha_timeout(mscp);
    729 	}
    730 	return (COMPLETE);
    731 
    732 bad:
    733 	xs->error = XS_DRIVER_STUFFUP;
    734 	uha_free_mscp(sc, mscp);
    735 	return (COMPLETE);
    736 }
    737 
    738 void
    739 uha_timeout(arg)
    740 	void *arg;
    741 {
    742 	struct uha_mscp *mscp = arg;
    743 	struct scsipi_xfer *xs = mscp->xs;
    744 	struct scsipi_link *sc_link = xs->sc_link;
    745 	struct uha_softc *sc = sc_link->adapter_softc;
    746 	int s;
    747 
    748 	scsi_print_addr(sc_link);
    749 	printf("timed out");
    750 
    751 	s = splbio();
    752 
    753 	if (mscp->flags & MSCP_ABORT) {
    754 		/* abort timed out */
    755 		printf(" AGAIN\n");
    756 		/* XXX Must reset! */
    757 	} else {
    758 		/* abort the operation that has timed out */
    759 		printf("\n");
    760 		mscp->xs->error = XS_TIMEOUT;
    761 		mscp->timeout = UHA_ABORT_TIMEOUT;
    762 		mscp->flags |= MSCP_ABORT;
    763 		(sc->start_mbox)(sc, mscp);
    764 	}
    765 
    766 	splx(s);
    767 }
    768