Home | History | Annotate | Line # | Download | only in ic
uha.c revision 1.11
      1 /*	$NetBSD: uha.c,v 1.11 1997/11/04 05:58:29 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 struct cfdriver uha_cd = {
    152 	NULL, "uha", DV_DULL
    153 };
    154 
    155 #define	UHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    156 
    157 /* XXX Should put this in a better place. */
    158 #define	offsetof(type, member)	((size_t)(&((type *)0)->member))
    159 
    160 /*
    161  * Insert a scsipi_xfer into the software queue.  We overload xs->free_list
    162  * to avoid having to allocate additional resources (since we're used
    163  * only during resource shortages anyhow.
    164  */
    165 void
    166 uha_enqueue(sc, xs, infront)
    167 	struct uha_softc *sc;
    168 	struct scsipi_xfer *xs;
    169 	int infront;
    170 {
    171 
    172 	if (infront || sc->sc_queue.lh_first == NULL) {
    173 		if (sc->sc_queue.lh_first == NULL)
    174 			sc->sc_queuelast = xs;
    175 		LIST_INSERT_HEAD(&sc->sc_queue, xs, free_list);
    176 		return;
    177 	}
    178 
    179 	LIST_INSERT_AFTER(sc->sc_queuelast, xs, free_list);
    180 	sc->sc_queuelast = xs;
    181 }
    182 
    183 /*
    184  * Pull a scsipi_xfer off the front of the software queue.
    185  */
    186 struct scsipi_xfer *
    187 uha_dequeue(sc)
    188 	struct uha_softc *sc;
    189 {
    190 	struct scsipi_xfer *xs;
    191 
    192 	xs = sc->sc_queue.lh_first;
    193 	LIST_REMOVE(xs, free_list);
    194 
    195 	if (sc->sc_queue.lh_first == NULL)
    196 		sc->sc_queuelast = NULL;
    197 
    198 	return (xs);
    199 }
    200 
    201 /*
    202  * Attach all the sub-devices we can find
    203  */
    204 void
    205 uha_attach(sc, upd)
    206 	struct uha_softc *sc;
    207 	struct uha_probe_data *upd;
    208 {
    209 
    210 	TAILQ_INIT(&sc->sc_free_mscp);
    211 	LIST_INIT(&sc->sc_queue);
    212 
    213 	(sc->init)(sc);
    214 
    215 	/*
    216 	 * fill in the prototype scsipi_link.
    217 	 */
    218 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    219 	sc->sc_link.adapter_softc = sc;
    220 	sc->sc_link.scsipi_scsi.adapter_target = upd->sc_scsi_dev;
    221 	sc->sc_link.adapter = &uha_switch;
    222 	sc->sc_link.device = &uha_dev;
    223 	sc->sc_link.openings = 2;
    224 	sc->sc_link.scsipi_scsi.max_target = 7;
    225 	sc->sc_link.type = BUS_SCSI;
    226 
    227 	/*
    228 	 * ask the adapter what subunits are present
    229 	 */
    230 	config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    231 }
    232 
    233 integrate void
    234 uha_reset_mscp(sc, mscp)
    235 	struct uha_softc *sc;
    236 	struct uha_mscp *mscp;
    237 {
    238 
    239 	mscp->flags = 0;
    240 }
    241 
    242 /*
    243  * A mscp (and hence a mbx-out) is put onto the free list.
    244  */
    245 void
    246 uha_free_mscp(sc, mscp)
    247 	struct uha_softc *sc;
    248 	struct uha_mscp *mscp;
    249 {
    250 	int s;
    251 
    252 	s = splbio();
    253 
    254 	uha_reset_mscp(sc, mscp);
    255 	TAILQ_INSERT_HEAD(&sc->sc_free_mscp, mscp, chain);
    256 
    257 	/*
    258 	 * If there were none, wake anybody waiting for one to come free,
    259 	 * starting with queued entries.
    260 	 */
    261 	if (mscp->chain.tqe_next == 0)
    262 		wakeup(&sc->sc_free_mscp);
    263 
    264 	splx(s);
    265 }
    266 
    267 integrate int
    268 uha_init_mscp(sc, mscp)
    269 	struct uha_softc *sc;
    270 	struct uha_mscp *mscp;
    271 {
    272 	bus_dma_tag_t dmat = sc->sc_dmat;
    273 	int hashnum, error;
    274 
    275 	/*
    276 	 * XXX Should we put a DIAGNOSTIC check for multiple
    277 	 * XXX MSCP inits here?
    278 	 */
    279 
    280 	bzero(mscp, sizeof(struct uha_mscp));
    281 
    282 	/*
    283 	 * Create the DMA maps for this MSCP.
    284 	 */
    285 	error = bus_dmamap_create(dmat, sizeof(struct uha_mscp), 1,
    286 	    sizeof(struct uha_mscp), 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
    287 	    &mscp->dmamap_self);
    288 	if (error) {
    289 		printf("%s: can't create mscp dmamap_self\n",
    290 		    sc->sc_dev.dv_xname);
    291 		return (error);
    292 	}
    293 
    294 	error = bus_dmamap_create(dmat, UHA_MAXXFER, UHA_NSEG, UHA_MAXXFER,
    295 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
    296 	    &mscp->dmamap_xfer);
    297 	if (error) {
    298 		printf("%s: can't create mscp dmamap_xfer\n",
    299 		    sc->sc_dev.dv_xname);
    300 		return (error);
    301 	}
    302 
    303 	/*
    304 	 * Load the permanent DMA maps.
    305 	 */
    306 	error = bus_dmamap_load(dmat, mscp->dmamap_self, mscp,
    307 	    sizeof(struct uha_mscp), NULL, BUS_DMA_NOWAIT);
    308 	if (error) {
    309 		printf("%s: can't load mscp dmamap_self\n",
    310 		    sc->sc_dev.dv_xname);
    311 		bus_dmamap_destroy(dmat, mscp->dmamap_self);
    312 		bus_dmamap_destroy(dmat, mscp->dmamap_xfer);
    313 		return (error);
    314 	}
    315 
    316 	/*
    317 	 * put in the phystokv hash table
    318 	 * Never gets taken out.
    319 	 */
    320 	mscp->hashkey = mscp->dmamap_self->dm_segs[0].ds_addr;
    321 	hashnum = MSCP_HASH(mscp->hashkey);
    322 	mscp->nexthash = sc->sc_mscphash[hashnum];
    323 	sc->sc_mscphash[hashnum] = mscp;
    324 	uha_reset_mscp(sc, mscp);
    325 	return (0);
    326 }
    327 
    328 /*
    329  * Create a set of MSCPs and add them to the free list.
    330  */
    331 int
    332 uha_create_mscps(sc, mem, size)
    333 	struct uha_softc *sc;
    334 	void *mem;
    335 	size_t size;
    336 {
    337 	bus_dma_segment_t seg;
    338 	struct uha_mscp *mscp;
    339 	int rseg, error;
    340 
    341 	if (sc->sc_nummscps >= UHA_MSCP_MAX)
    342 		return (0);
    343 
    344 	if ((mscp = mem) != NULL)
    345 		goto have_mem;
    346 
    347 	size = NBPG;
    348 	error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &seg, 1, &rseg,
    349 	    BUS_DMA_NOWAIT);
    350 	if (error) {
    351 		printf("%s: can't allocate memory for mscps\n",
    352 		    sc->sc_dev.dv_xname);
    353 		return (error);
    354 	}
    355 
    356 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    357 	    (caddr_t *)&mscp, BUS_DMA_NOWAIT|BUS_DMAMEM_NOSYNC);
    358 	if (error) {
    359 		printf("%s: can't map memory for mscps\n",
    360 		    sc->sc_dev.dv_xname);
    361 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    362 		return (error);
    363 	}
    364 
    365  have_mem:
    366 	bzero(mscp, size);
    367 	while (size > sizeof(struct uha_mscp) &&
    368 	    sc->sc_nummscps < UHA_MSCP_MAX) {
    369 		error = uha_init_mscp(sc, mscp);
    370 		if (error) {
    371 			printf("%s: can't initialize mscp\n",
    372 			    sc->sc_dev.dv_xname);
    373 			return (error);
    374 		}
    375 		TAILQ_INSERT_TAIL(&sc->sc_free_mscp, mscp, chain);
    376 		(caddr_t)mscp += ALIGN(sizeof(struct uha_mscp));
    377 		size -= ALIGN(sizeof(struct uha_mscp));
    378 		sc->sc_nummscps++;
    379 	}
    380 
    381 	return (0);
    382 }
    383 
    384 /*
    385  * Get a free mscp
    386  *
    387  * If there are none, see if we can allocate a new one.  If so, put it in the
    388  * hash table too otherwise either return an error or sleep.
    389  */
    390 struct uha_mscp *
    391 uha_get_mscp(sc, flags)
    392 	struct uha_softc *sc;
    393 	int flags;
    394 {
    395 	struct uha_mscp *mscp;
    396 	int s;
    397 
    398 	s = splbio();
    399 
    400 	/*
    401 	 * If we can and have to, sleep waiting for one to come free
    402 	 * but only if we can't allocate a new one
    403 	 */
    404 	for (;;) {
    405 		mscp = sc->sc_free_mscp.tqh_first;
    406 		if (mscp) {
    407 			TAILQ_REMOVE(&sc->sc_free_mscp, mscp, chain);
    408 			break;
    409 		}
    410 		if (sc->sc_nummscps < UHA_MSCP_MAX) {
    411 			/*
    412 			 * uha_create_mscps() might have managed to create
    413 			 * one before it failed.  If so, don't abort,
    414 			 * just grab it and continue to hobble along.
    415 			 */
    416 			if (uha_create_mscps(sc, NULL, 0) &&
    417 			    sc->sc_free_mscp.tqh_first == NULL) {
    418 				printf("%s: can't allocate mscps\n",
    419 				    sc->sc_dev.dv_xname);
    420 				goto out;
    421 			}
    422 			continue;
    423 		}
    424 		if ((flags & SCSI_NOSLEEP) != 0)
    425 			goto out;
    426 		tsleep(&sc->sc_free_mscp, PRIBIO, "uhamsc", 0);
    427 	}
    428 
    429 	mscp->flags |= MSCP_ALLOC;
    430 
    431 out:
    432 	splx(s);
    433 	return (mscp);
    434 }
    435 
    436 /*
    437  * given a physical address, find the mscp that it corresponds to.
    438  */
    439 struct uha_mscp *
    440 uha_mscp_phys_kv(sc, mscp_phys)
    441 	struct uha_softc *sc;
    442 	u_long mscp_phys;
    443 {
    444 	int hashnum = MSCP_HASH(mscp_phys);
    445 	struct uha_mscp *mscp = sc->sc_mscphash[hashnum];
    446 
    447 	while (mscp) {
    448 		if (mscp->hashkey == mscp_phys)
    449 			break;
    450 		mscp = mscp->nexthash;
    451 	}
    452 	return (mscp);
    453 }
    454 
    455 /*
    456  * We have a mscp which has been processed by the adaptor, now we look to see
    457  * how the operation went.
    458  */
    459 void
    460 uha_done(sc, mscp)
    461 	struct uha_softc *sc;
    462 	struct uha_mscp *mscp;
    463 {
    464 	bus_dma_tag_t dmat = sc->sc_dmat;
    465 	struct scsipi_sense_data *s1, *s2;
    466 	struct scsipi_xfer *xs = mscp->xs;
    467 
    468 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("uha_done\n"));
    469 
    470 	/*
    471 	 * If we were a data transfer, unload the map that described
    472 	 * the data buffer.
    473 	 */
    474 	if (xs->datalen) {
    475 		bus_dmamap_sync(dmat, mscp->dmamap_xfer,
    476 		    (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    477 		    BUS_DMASYNC_POSTWRITE);
    478 		bus_dmamap_unload(dmat, mscp->dmamap_xfer);
    479 	}
    480 
    481 	/*
    482 	 * Otherwise, put the results of the operation
    483 	 * into the xfer and call whoever started it
    484 	 */
    485 	if ((mscp->flags & MSCP_ALLOC) == 0) {
    486 		printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
    487 		Debugger();
    488 		return;
    489 	}
    490 	if (xs->error == XS_NOERROR) {
    491 		if (mscp->host_stat != UHA_NO_ERR) {
    492 			switch (mscp->host_stat) {
    493 			case UHA_SBUS_TIMEOUT:		/* No response */
    494 				xs->error = XS_SELTIMEOUT;
    495 				break;
    496 			default:	/* Other scsi protocol messes */
    497 				printf("%s: host_stat %x\n",
    498 				    sc->sc_dev.dv_xname, mscp->host_stat);
    499 				xs->error = XS_DRIVER_STUFFUP;
    500 			}
    501 		} else if (mscp->target_stat != SCSI_OK) {
    502 			switch (mscp->target_stat) {
    503 			case SCSI_CHECK:
    504 				s1 = &mscp->mscp_sense;
    505 				s2 = &xs->sense.scsi_sense;
    506 				*s2 = *s1;
    507 				xs->error = XS_SENSE;
    508 				break;
    509 			case SCSI_BUSY:
    510 				xs->error = XS_BUSY;
    511 				break;
    512 			default:
    513 				printf("%s: target_stat %x\n",
    514 				    sc->sc_dev.dv_xname, mscp->target_stat);
    515 				xs->error = XS_DRIVER_STUFFUP;
    516 			}
    517 		} else
    518 			xs->resid = 0;
    519 	}
    520 	uha_free_mscp(sc, mscp);
    521 	xs->flags |= ITSDONE;
    522 	scsipi_done(xs);
    523 
    524 	/*
    525 	 * If there are queue entries in the software queue, try to
    526 	 * run the first one.  We should be more or less guaranteed
    527 	 * to succeed, since we just freed an MSCP.
    528 	 *
    529 	 * NOTE: uha_scsi_cmd() relies on our calling it with
    530 	 * the first entry in the queue.
    531 	 */
    532 	if ((xs = sc->sc_queue.lh_first) != NULL)
    533 		(void) uha_scsi_cmd(xs);
    534 }
    535 
    536 void
    537 uhaminphys(bp)
    538 	struct buf *bp;
    539 {
    540 
    541 	if (bp->b_bcount > UHA_MAXXFER)
    542 		bp->b_bcount = UHA_MAXXFER;
    543 	minphys(bp);
    544 }
    545 
    546 /*
    547  * start a scsi operation given the command and the data address.  Also
    548  * needs the unit, target and lu.
    549  */
    550 int
    551 uha_scsi_cmd(xs)
    552 	struct scsipi_xfer *xs;
    553 {
    554 	struct scsipi_link *sc_link = xs->sc_link;
    555 	struct uha_softc *sc = sc_link->adapter_softc;
    556 	bus_dma_tag_t dmat = sc->sc_dmat;
    557 	struct uha_mscp *mscp;
    558 	struct uha_dma_seg *sg;
    559 	int error, seg, flags, s;
    560 	int fromqueue = 0, dontqueue = 0;
    561 
    562 	SC_DEBUG(sc_link, SDEV_DB2, ("uha_scsi_cmd\n"));
    563 
    564 	s = splbio();		/* protect the queue */
    565 
    566 	/*
    567 	 * If we're running the queue from bha_done(), we've been
    568 	 * called with the first queue entry as our argument.
    569 	 */
    570 	if (xs == sc->sc_queue.lh_first) {
    571 		xs = uha_dequeue(sc);
    572 		fromqueue = 1;
    573 		goto get_mscp;
    574 	}
    575 
    576 	/* Polled requests can't be queued for later. */
    577 	dontqueue = xs->flags & SCSI_POLL;
    578 
    579 	/*
    580 	 * If there are jobs in the queue, run them first.
    581 	 */
    582 	if (sc->sc_queue.lh_first != NULL) {
    583 		/*
    584 		 * If we can't queue, we have to abort, since
    585 		 * we have to preserve order.
    586 		 */
    587 		if (dontqueue) {
    588 			splx(s);
    589 			xs->error = XS_DRIVER_STUFFUP;
    590 			return (TRY_AGAIN_LATER);
    591 		}
    592 
    593 		/*
    594 		 * Swap with the first queue entry.
    595 		 */
    596 		uha_enqueue(sc, xs, 0);
    597 		xs = uha_dequeue(sc);
    598 		fromqueue = 1;
    599 	}
    600 
    601  get_mscp:
    602 	/*
    603 	 * get a mscp (mbox-out) to use. If the transfer
    604 	 * is from a buf (possibly from interrupt time)
    605 	 * then we can't allow it to sleep
    606 	 */
    607 	flags = xs->flags;
    608 	if ((mscp = uha_get_mscp(sc, flags)) == NULL) {
    609 		/*
    610 		 * If we can't queue, we lose.
    611 		 */
    612 		if (dontqueue) {
    613 			splx(s);
    614 			xs->error = XS_DRIVER_STUFFUP;
    615 			return (TRY_AGAIN_LATER);
    616 		}
    617 
    618 		/*
    619 		 * Stuff ourselves into the queue, in front
    620 		 * if we came off in the first place.
    621 		 */
    622 		uha_enqueue(sc, xs, fromqueue);
    623 		splx(s);
    624 		return (SUCCESSFULLY_QUEUED);
    625 	}
    626 
    627 	splx(s);		/* done playing with the queue */
    628 
    629 	mscp->xs = xs;
    630 	mscp->timeout = xs->timeout;
    631 
    632 	/*
    633 	 * Put all the arguments for the xfer in the mscp
    634 	 */
    635 	if (flags & SCSI_RESET) {
    636 		mscp->opcode = UHA_SDR;
    637 		mscp->ca = 0x01;
    638 	} else {
    639 		mscp->opcode = UHA_TSP;
    640 		/* XXX Not for tapes. */
    641 		mscp->ca = 0x01;
    642 		bcopy(xs->cmd, &mscp->scsi_cmd, mscp->scsi_cmd_length);
    643 	}
    644 	mscp->xdir = UHA_SDET;
    645 	mscp->dcn = 0x00;
    646 	mscp->chan = 0x00;
    647 	mscp->target = sc_link->scsipi_scsi.target;
    648 	mscp->lun = sc_link->scsipi_scsi.lun;
    649 	mscp->scsi_cmd_length = xs->cmdlen;
    650 	mscp->sense_ptr = mscp->dmamap_self->dm_segs[0].ds_addr +
    651 	    offsetof(struct uha_mscp, mscp_sense);
    652 	mscp->req_sense_length = sizeof(mscp->mscp_sense);
    653 	mscp->host_stat = 0x00;
    654 	mscp->target_stat = 0x00;
    655 
    656 	if (xs->datalen) {
    657 		sg = mscp->uha_dma;
    658 		seg = 0;
    659 #ifdef	TFS
    660 		if (flags & SCSI_DATA_UIO) {
    661 			error = bus_dmamap_load_uio(dmat,
    662 			    mscp->dmamap_xfer, (struct uio *)xs->data,
    663 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
    664 			    BUS_DMA_WAITOK);
    665 		} else
    666 #endif /*TFS */
    667 		{
    668 			error = bus_dmamap_load(dmat,
    669 			    mscp->dmamap_xfer, xs->data, xs->datalen, NULL,
    670 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
    671 			    BUS_DMA_WAITOK);
    672 		}
    673 
    674 		if (error) {
    675 			if (error == EFBIG) {
    676 				printf("%s: uha_scsi_cmd, more than %d"
    677 				    " dma segments\n",
    678 				    sc->sc_dev.dv_xname, UHA_NSEG);
    679 			} else {
    680 				printf("%s: uha_scsi_cmd, error %d loading"
    681 				    " dma map\n",
    682 				    sc->sc_dev.dv_xname, error);
    683 			}
    684 			goto bad;
    685 		}
    686 
    687 		bus_dmamap_sync(dmat, mscp->dmamap_xfer,
    688 		    (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
    689 		    BUS_DMASYNC_PREWRITE);
    690 
    691 		/*
    692 		 * Load the hardware scatter/gather map with the
    693 		 * contents of the DMA map.
    694 		 */
    695 		for (seg = 0; seg < mscp->dmamap_xfer->dm_nsegs; seg++) {
    696 			mscp->uha_dma[seg].seg_addr =
    697 			    mscp->dmamap_xfer->dm_segs[seg].ds_addr;
    698 			mscp->uha_dma[seg].seg_len =
    699 			    mscp->dmamap_xfer->dm_segs[seg].ds_len;
    700 		}
    701 
    702 		mscp->data_addr = mscp->dmamap_self->dm_segs[0].ds_addr +
    703 		    offsetof(struct uha_mscp, uha_dma);
    704 		mscp->data_length = xs->datalen;
    705 		mscp->sgth = 0x01;
    706 		mscp->sg_num = seg;
    707 	} else {		/* No data xfer, use non S/G values */
    708 		mscp->data_addr = (physaddr)0;
    709 		mscp->data_length = 0;
    710 		mscp->sgth = 0x00;
    711 		mscp->sg_num = 0;
    712 	}
    713 	mscp->link_id = 0;
    714 	mscp->link_addr = (physaddr)0;
    715 
    716 	s = splbio();
    717 	(sc->start_mbox)(sc, mscp);
    718 	splx(s);
    719 
    720 	/*
    721 	 * Usually return SUCCESSFULLY QUEUED
    722 	 */
    723 	if ((flags & SCSI_POLL) == 0)
    724 		return (SUCCESSFULLY_QUEUED);
    725 
    726 	/*
    727 	 * If we can't use interrupts, poll on completion
    728 	 */
    729 	if ((sc->poll)(sc, xs, mscp->timeout)) {
    730 		uha_timeout(mscp);
    731 		if ((sc->poll)(sc, xs, mscp->timeout))
    732 			uha_timeout(mscp);
    733 	}
    734 	return (COMPLETE);
    735 
    736 bad:
    737 	xs->error = XS_DRIVER_STUFFUP;
    738 	uha_free_mscp(sc, mscp);
    739 	return (COMPLETE);
    740 }
    741 
    742 void
    743 uha_timeout(arg)
    744 	void *arg;
    745 {
    746 	struct uha_mscp *mscp = arg;
    747 	struct scsipi_xfer *xs = mscp->xs;
    748 	struct scsipi_link *sc_link = xs->sc_link;
    749 	struct uha_softc *sc = sc_link->adapter_softc;
    750 	int s;
    751 
    752 	scsi_print_addr(sc_link);
    753 	printf("timed out");
    754 
    755 	s = splbio();
    756 
    757 	if (mscp->flags & MSCP_ABORT) {
    758 		/* abort timed out */
    759 		printf(" AGAIN\n");
    760 		/* XXX Must reset! */
    761 	} else {
    762 		/* abort the operation that has timed out */
    763 		printf("\n");
    764 		mscp->xs->error = XS_TIMEOUT;
    765 		mscp->timeout = UHA_ABORT_TIMEOUT;
    766 		mscp->flags |= MSCP_ABORT;
    767 		(sc->start_mbox)(sc, mscp);
    768 	}
    769 
    770 	splx(s);
    771 }
    772