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