Home | History | Annotate | Line # | Download | only in isa
wds.c revision 1.73
      1 /*	$NetBSD: wds.c,v 1.73 2009/05/12 09:10:16 cegger Exp $	*/
      2 
      3 /*
      4  * XXX
      5  * aborts
      6  * resets
      7  */
      8 
      9 /*-
     10  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     11  * All rights reserved.
     12  *
     13  * This code is derived from software contributed to The NetBSD Foundation
     14  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     15  * NASA Ames Research Center.
     16  *
     17  * Redistribution and use in source and binary forms, with or without
     18  * modification, are permitted provided that the following conditions
     19  * are met:
     20  * 1. Redistributions of source code must retain the above copyright
     21  *    notice, this list of conditions and the following disclaimer.
     22  * 2. Redistributions in binary form must reproduce the above copyright
     23  *    notice, this list of conditions and the following disclaimer in the
     24  *    documentation and/or other materials provided with the distribution.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1994, 1995 Julian Highfield.  All rights reserved.
     41  * Portions copyright (c) 1994, 1996, 1997
     42  *	Charles M. Hannum.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Julian Highfield.
     55  * 4. The name of the author may not be used to endorse or promote products
     56  *    derived from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68  */
     69 
     70 /*
     71  * This driver is for the WD7000 family of SCSI controllers:
     72  *   the WD7000-ASC, a bus-mastering DMA controller,
     73  *   the WD7000-FASST2, an -ASC with new firmware and scatter-gather,
     74  *   and the WD7000-ASE, which was custom manufactured for Apollo
     75  *      workstations and seems to include an -ASC as well as floppy
     76  *      and ESDI interfaces.
     77  *
     78  * Loosely based on Theo Deraadt's unfinished attempt.
     79  */
     80 
     81 #include <sys/cdefs.h>
     82 __KERNEL_RCSID(0, "$NetBSD: wds.c,v 1.73 2009/05/12 09:10:16 cegger Exp $");
     83 
     84 #include "opt_ddb.h"
     85 
     86 #undef WDSDIAG
     87 #ifdef DDB
     88 #define	integrate
     89 #else
     90 #define	integrate	static inline
     91 #endif
     92 
     93 #include <sys/param.h>
     94 #include <sys/systm.h>
     95 #include <sys/kernel.h>
     96 #include <sys/errno.h>
     97 #include <sys/ioctl.h>
     98 #include <sys/device.h>
     99 #include <sys/malloc.h>
    100 #include <sys/buf.h>
    101 #include <sys/proc.h>
    102 #include <sys/user.h>
    103 
    104 #include <uvm/uvm_extern.h>
    105 
    106 #include <sys/bus.h>
    107 #include <sys/intr.h>
    108 
    109 #include <dev/scsipi/scsi_all.h>
    110 #include <dev/scsipi/scsipi_all.h>
    111 #include <dev/scsipi/scsiconf.h>
    112 
    113 #include <dev/isa/isavar.h>
    114 #include <dev/isa/isadmavar.h>
    115 
    116 #include <dev/isa/wdsreg.h>
    117 
    118 #define	WDS_ISA_IOSIZE	8
    119 
    120 #ifndef DDB
    121 #define Debugger() panic("should call debugger here (wds.c)")
    122 #endif /* ! DDB */
    123 
    124 #define	WDS_MAXXFER	((WDS_NSEG - 1) << PGSHIFT)
    125 
    126 #define WDS_MBX_SIZE	16
    127 
    128 #define WDS_SCB_MAX	32
    129 #define	SCB_HASH_SIZE	32	/* hash table size for phystokv */
    130 #define	SCB_HASH_SHIFT	9
    131 #define	SCB_HASH(x)	((((long)(x))>>SCB_HASH_SHIFT) & (SCB_HASH_SIZE - 1))
    132 
    133 #define	wds_nextmbx(wmb, mbx, mbio) \
    134 	if ((wmb) == &(mbx)->mbio[WDS_MBX_SIZE - 1])	\
    135 		(wmb) = &(mbx)->mbio[0];		\
    136 	else						\
    137 		(wmb)++;
    138 
    139 struct wds_mbx {
    140 	struct wds_mbx_out mbo[WDS_MBX_SIZE];
    141 	struct wds_mbx_in mbi[WDS_MBX_SIZE];
    142 	struct wds_mbx_out *cmbo;	/* Collection Mail Box out */
    143 	struct wds_mbx_out *tmbo;	/* Target Mail Box out */
    144 	struct wds_mbx_in *tmbi;	/* Target Mail Box in */
    145 };
    146 
    147 struct wds_softc {
    148 	struct device sc_dev;
    149 
    150 	bus_space_tag_t sc_iot;
    151 	bus_space_handle_t sc_ioh;
    152 	bus_dma_tag_t sc_dmat;
    153 	bus_dmamap_t sc_dmamap_mbox;	/* maps the mailbox */
    154 	void *sc_ih;
    155 
    156 	struct wds_mbx *sc_mbx;
    157 #define	wmbx	(sc->sc_mbx)
    158 	struct wds_scb *sc_scbhash[SCB_HASH_SIZE];
    159 	TAILQ_HEAD(, wds_scb) sc_free_scb, sc_waiting_scb;
    160 	int sc_numscbs, sc_mbofull;
    161 
    162 	struct scsipi_adapter sc_adapter;
    163 	struct scsipi_channel sc_channel;
    164 
    165 	int sc_revision;
    166 	int sc_maxsegs;
    167 };
    168 
    169 struct wds_probe_data {
    170 #ifdef notyet
    171 	int sc_irq, sc_drq;
    172 #endif
    173 	int sc_scsi_dev;
    174 };
    175 
    176 integrate void
    177 	wds_wait(bus_space_tag_t, bus_space_handle_t, int, int, int);
    178 int     wds_cmd(bus_space_tag_t, bus_space_handle_t, u_char *, int);
    179 integrate void wds_finish_scbs(struct wds_softc *);
    180 int     wdsintr(void *);
    181 integrate void wds_reset_scb(struct wds_softc *, struct wds_scb *);
    182 void    wds_free_scb(struct wds_softc *, struct wds_scb *);
    183 integrate int wds_init_scb(struct wds_softc *, struct wds_scb *);
    184 struct	wds_scb *wds_get_scb(struct wds_softc *);
    185 struct	wds_scb *wds_scb_phys_kv(struct wds_softc *, u_long);
    186 void	wds_queue_scb(struct wds_softc *, struct wds_scb *);
    187 void	wds_collect_mbo(struct wds_softc *);
    188 void	wds_start_scbs(struct wds_softc *);
    189 void    wds_done(struct wds_softc *, struct wds_scb *, u_char);
    190 int	wds_find(bus_space_tag_t, bus_space_handle_t, struct wds_probe_data *);
    191 void	wds_attach(struct wds_softc *, struct wds_probe_data *);
    192 void	wds_init(struct wds_softc *, int);
    193 void	wds_inquire_setup_information(struct wds_softc *);
    194 void    wdsminphys(struct buf *);
    195 void	wds_scsipi_request(struct scsipi_channel *,
    196 	    scsipi_adapter_req_t, void *);
    197 int	wds_poll(struct wds_softc *, struct scsipi_xfer *, int);
    198 int	wds_ipoll(struct wds_softc *, struct wds_scb *, int);
    199 void	wds_timeout(void *);
    200 int	wds_create_scbs(struct wds_softc *, void *, size_t);
    201 
    202 int	wdsprobe(device_t, cfdata_t, void *);
    203 void	wdsattach(device_t, device_t, void *);
    204 
    205 CFATTACH_DECL(wds, sizeof(struct wds_softc),
    206     wdsprobe, wdsattach, NULL, NULL);
    207 
    208 #ifdef WDSDEBUG
    209 int wds_debug = 0;
    210 #endif
    211 
    212 #define	WDS_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    213 
    214 integrate void
    215 wds_wait(bus_space_tag_t iot, bus_space_handle_t ioh, int port, int mask, int val)
    216 {
    217 
    218 	while ((bus_space_read_1(iot, ioh, port) & mask) != val)
    219 		;
    220 }
    221 
    222 /*
    223  * Write a command to the board's I/O ports.
    224  */
    225 int
    226 wds_cmd(bus_space_tag_t iot, bus_space_handle_t ioh, u_char *ibuf, int icnt)
    227 {
    228 	u_char c;
    229 
    230 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
    231 
    232 	while (icnt--) {
    233 		bus_space_write_1(iot, ioh, WDS_CMD, *ibuf++);
    234 		wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
    235 		c = bus_space_read_1(iot, ioh, WDS_STAT);
    236 		if (c & WDSS_REJ)
    237 			return 1;
    238 	}
    239 
    240 	return 0;
    241 }
    242 
    243 /*
    244  * Check for the presence of a WD7000 SCSI controller.
    245  */
    246 int
    247 wdsprobe(device_t parent, cfdata_t match, void *aux)
    248 {
    249 	struct isa_attach_args *ia = aux;
    250 	bus_space_tag_t iot = ia->ia_iot;
    251 	bus_space_handle_t ioh;
    252 	struct wds_probe_data wpd;
    253 	int rv;
    254 
    255 	if (ia->ia_nio < 1)
    256 		return (0);
    257 	if (ia->ia_nirq < 1)
    258 		return (0);
    259 	if (ia->ia_ndrq < 1)
    260 		return (0);
    261 
    262 	if (ISA_DIRECT_CONFIG(ia))
    263 		return (0);
    264 
    265 	/* Disallow wildcarded i/o address. */
    266 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    267 		return (0);
    268 
    269 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, WDS_ISA_IOSIZE, 0, &ioh))
    270 		return (0);
    271 
    272 	rv = wds_find(iot, ioh, &wpd);
    273 
    274 	bus_space_unmap(iot, ioh, WDS_ISA_IOSIZE);
    275 
    276 	if (rv) {
    277 #ifdef notyet
    278 		if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
    279 		    ia->ia_irq[0].ir_irq != wpd.sc_irq)
    280 			return (0);
    281 		if (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ &&
    282 		    ia->ia_drq[0].ir_drq != wpd.sc_drq)
    283 			return (0);
    284 
    285 		ia->ia_nirq = 1;
    286 		ia->ia_irq[0].ir_irq = wpd.sc_irq;
    287 
    288 		ia->ia_ndrq = 1;
    289 		ia->ia_drq[0].ir_drq = wpd.sc_drq;
    290 #else
    291 		if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
    292 			return (0);
    293 		if (ia->ia_drq[0].ir_drq == ISA_UNKNOWN_DRQ)
    294 			return (0);
    295 
    296 		ia->ia_nirq = 1;
    297 		ia->ia_ndrq = 1;
    298 #endif
    299 		ia->ia_nio = 1;
    300 		ia->ia_io[0].ir_size = WDS_ISA_IOSIZE;
    301 
    302 		ia->ia_niomem = 0;
    303 	}
    304 	return (rv);
    305 }
    306 
    307 /*
    308  * Attach all available units.
    309  */
    310 void
    311 wdsattach(device_t parent, device_t self, void *aux)
    312 {
    313 	struct isa_attach_args *ia = aux;
    314 	struct wds_softc *sc = (void *)self;
    315 	bus_space_tag_t iot = ia->ia_iot;
    316 	bus_space_handle_t ioh;
    317 	struct wds_probe_data wpd;
    318 	isa_chipset_tag_t ic = ia->ia_ic;
    319 	int error;
    320 
    321 	printf("\n");
    322 
    323 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, WDS_ISA_IOSIZE, 0, &ioh)) {
    324 		aprint_error_dev(&sc->sc_dev, "can't map i/o space\n");
    325 		return;
    326 	}
    327 
    328 	sc->sc_iot = iot;
    329 	sc->sc_ioh = ioh;
    330 	sc->sc_dmat = ia->ia_dmat;
    331 	if (!wds_find(iot, ioh, &wpd)) {
    332 		aprint_error_dev(&sc->sc_dev, "wds_find failed\n");
    333 		return;
    334 	}
    335 
    336 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
    337 #ifdef notyet
    338 	if (wpd.sc_drq != -1) {
    339 		if ((error = isa_dmacascade(ic, wpd.sc_drq)) != 0) {
    340 			aprint_error_dev(&sc->sc_dev, "unable to cascade DRQ, error = %d\n", error);
    341 			return;
    342 		}
    343 	}
    344 
    345 	sc->sc_ih = isa_intr_establish(ic, wpd.sc_irq, IST_EDGE, IPL_BIO,
    346 	    wdsintr, sc);
    347 #else
    348 	if ((error = isa_dmacascade(ic, ia->ia_drq[0].ir_drq)) != 0) {
    349 		aprint_error_dev(&sc->sc_dev, "unable to cascade DRQ, error = %d\n", error);
    350 		return;
    351 	}
    352 
    353 	sc->sc_ih = isa_intr_establish(ic, ia->ia_irq[0].ir_irq, IST_EDGE,
    354 	    IPL_BIO, wdsintr, sc);
    355 #endif
    356 	if (sc->sc_ih == NULL) {
    357 		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt\n");
    358 		return;
    359 	}
    360 
    361 	wds_attach(sc, &wpd);
    362 }
    363 
    364 void
    365 wds_attach(struct wds_softc *sc, struct wds_probe_data *wpd)
    366 {
    367 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    368 	struct scsipi_channel *chan = &sc->sc_channel;
    369 
    370 	TAILQ_INIT(&sc->sc_free_scb);
    371 	TAILQ_INIT(&sc->sc_waiting_scb);
    372 
    373 	/*
    374 	 * Fill in the scsipi_adapter.
    375 	 */
    376 	memset(adapt, 0, sizeof(*adapt));
    377 	adapt->adapt_dev = &sc->sc_dev;
    378 	adapt->adapt_nchannels = 1;
    379 	/* adapt_openings initialized below */
    380 	adapt->adapt_max_periph = 1;
    381 	adapt->adapt_request = wds_scsipi_request;
    382 	adapt->adapt_minphys = minphys;
    383 
    384 	/*
    385 	 * Fill in the scsipi_channel.
    386 	 */
    387 	memset(chan, 0, sizeof(*chan));
    388 	chan->chan_adapter = adapt;
    389 	chan->chan_bustype = &scsi_bustype;
    390 	chan->chan_channel = 0;
    391 	chan->chan_ntargets = 8;
    392 	chan->chan_nluns = 8;
    393 	chan->chan_id = wpd->sc_scsi_dev;
    394 
    395 	wds_init(sc, 0);
    396 	wds_inquire_setup_information(sc);
    397 
    398 	/* XXX add support for GROW */
    399 	adapt->adapt_openings = sc->sc_numscbs;
    400 
    401 	/*
    402 	 * ask the adapter what subunits are present
    403 	 */
    404 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    405 }
    406 
    407 integrate void
    408 wds_finish_scbs(struct wds_softc *sc)
    409 {
    410 	struct wds_mbx_in *wmbi;
    411 	struct wds_scb *scb;
    412 	int i;
    413 
    414 	wmbi = wmbx->tmbi;
    415 
    416 	if (wmbi->stat == WDS_MBI_FREE) {
    417 		for (i = 0; i < WDS_MBX_SIZE; i++) {
    418 			if (wmbi->stat != WDS_MBI_FREE) {
    419 				printf("%s: mbi not in round-robin order\n",
    420 				    device_xname(&sc->sc_dev));
    421 				goto AGAIN;
    422 			}
    423 			wds_nextmbx(wmbi, wmbx, mbi);
    424 		}
    425 #ifdef WDSDIAGnot
    426 		printf("%s: mbi interrupt with no full mailboxes\n",
    427 		    device_xname(&sc->sc_dev));
    428 #endif
    429 		return;
    430 	}
    431 
    432 AGAIN:
    433 	do {
    434 		scb = wds_scb_phys_kv(sc, phystol(wmbi->scb_addr));
    435 		if (!scb) {
    436 			printf("%s: bad mbi scb pointer; skipping\n",
    437 			    device_xname(&sc->sc_dev));
    438 			goto next;
    439 		}
    440 
    441 #ifdef WDSDEBUG
    442 		if (wds_debug) {
    443 			u_char *cp = scb->cmd.xx;
    444 			printf("op=%x %x %x %x %x %x\n",
    445 			    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
    446 			printf("stat %x for mbi addr = %p, ",
    447 			    wmbi->stat, wmbi);
    448 			printf("scb addr = %p\n", scb);
    449 		}
    450 #endif /* WDSDEBUG */
    451 
    452 		callout_stop(&scb->xs->xs_callout);
    453 		wds_done(sc, scb, wmbi->stat);
    454 
    455 	next:
    456 		wmbi->stat = WDS_MBI_FREE;
    457 		wds_nextmbx(wmbi, wmbx, mbi);
    458 	} while (wmbi->stat != WDS_MBI_FREE);
    459 
    460 	wmbx->tmbi = wmbi;
    461 }
    462 
    463 /*
    464  * Process an interrupt.
    465  */
    466 int
    467 wdsintr(void *arg)
    468 {
    469 	struct wds_softc *sc = arg;
    470 	bus_space_tag_t iot = sc->sc_iot;
    471 	bus_space_handle_t ioh = sc->sc_ioh;
    472 	u_char c;
    473 
    474 	/* Was it really an interrupt from the board? */
    475 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ) == 0)
    476 		return 0;
    477 
    478 	/* Get the interrupt status byte. */
    479 	c = bus_space_read_1(iot, ioh, WDS_IRQSTAT) & WDSI_MASK;
    480 
    481 	/* Acknowledge (which resets) the interrupt. */
    482 	bus_space_write_1(iot, ioh, WDS_IRQACK, 0x00);
    483 
    484 	switch (c) {
    485 	case WDSI_MSVC:
    486 		wds_finish_scbs(sc);
    487 		break;
    488 
    489 	case WDSI_MFREE:
    490 		wds_start_scbs(sc);
    491 		break;
    492 
    493 	default:
    494 		aprint_error_dev(&sc->sc_dev, "unrecognized interrupt type %02x", c);
    495 		break;
    496 	}
    497 
    498 	return 1;
    499 }
    500 
    501 integrate void
    502 wds_reset_scb(struct wds_softc *sc, struct wds_scb *scb)
    503 {
    504 
    505 	scb->flags = 0;
    506 }
    507 
    508 /*
    509  * Free the command structure, the outgoing mailbox and the data buffer.
    510  */
    511 void
    512 wds_free_scb(struct wds_softc *sc, struct wds_scb *scb)
    513 {
    514 	int s;
    515 
    516 	s = splbio();
    517 	wds_reset_scb(sc, scb);
    518 	TAILQ_INSERT_HEAD(&sc->sc_free_scb, scb, chain);
    519 	splx(s);
    520 }
    521 
    522 integrate int
    523 wds_init_scb(struct wds_softc *sc, struct wds_scb *scb)
    524 {
    525 	bus_dma_tag_t dmat = sc->sc_dmat;
    526 	int hashnum, error;
    527 
    528 	/*
    529 	 * XXX Should we put a DIAGNOSTIC check for multiple
    530 	 * XXX SCB inits here?
    531 	 */
    532 
    533 	memset(scb, 0, sizeof(struct wds_scb));
    534 
    535 	/*
    536 	 * Create DMA maps for this SCB.
    537 	 */
    538 	error = bus_dmamap_create(dmat, sizeof(struct wds_scb), 1,
    539 	    sizeof(struct wds_scb), 0, BUS_DMA_NOWAIT, &scb->dmamap_self);
    540 	if (error) {
    541 		aprint_error_dev(&sc->sc_dev, "can't create scb dmamap_self\n");
    542 		return (error);
    543 	}
    544 
    545 	error = bus_dmamap_create(dmat, WDS_MAXXFER, WDS_NSEG, WDS_MAXXFER,
    546 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &scb->dmamap_xfer);
    547 	if (error) {
    548 		aprint_error_dev(&sc->sc_dev, "can't create scb dmamap_xfer\n");
    549 		bus_dmamap_destroy(dmat, scb->dmamap_self);
    550 		return (error);
    551 	}
    552 
    553 	/*
    554 	 * Load the permanent DMA maps.
    555 	 */
    556 	error = bus_dmamap_load(dmat, scb->dmamap_self, scb,
    557 	    sizeof(struct wds_scb), NULL, BUS_DMA_NOWAIT);
    558 	if (error) {
    559 		aprint_error_dev(&sc->sc_dev, "can't load scb dmamap_self\n");
    560 		bus_dmamap_destroy(dmat, scb->dmamap_self);
    561 		bus_dmamap_destroy(dmat, scb->dmamap_xfer);
    562 		return (error);
    563 	}
    564 
    565 	/*
    566 	 * put in the phystokv hash table
    567 	 * Never gets taken out.
    568 	 */
    569 	scb->hashkey = scb->dmamap_self->dm_segs[0].ds_addr;
    570 	hashnum = SCB_HASH(scb->hashkey);
    571 	scb->nexthash = sc->sc_scbhash[hashnum];
    572 	sc->sc_scbhash[hashnum] = scb;
    573 	wds_reset_scb(sc, scb);
    574 	return (0);
    575 }
    576 
    577 /*
    578  * Create a set of scbs and add them to the free list.
    579  */
    580 int
    581 wds_create_scbs(struct wds_softc *sc, void *mem, size_t size)
    582 {
    583 	bus_dma_segment_t seg;
    584 	struct wds_scb *scb;
    585 	int rseg, error;
    586 
    587 	if (sc->sc_numscbs >= WDS_SCB_MAX)
    588 		return (0);
    589 
    590 	if ((scb = mem) != NULL)
    591 		goto have_mem;
    592 
    593 	size = PAGE_SIZE;
    594 	error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg,
    595 	    1, &rseg, BUS_DMA_NOWAIT);
    596 	if (error) {
    597 		aprint_error_dev(&sc->sc_dev, "can't allocate memory for scbs\n");
    598 		return (error);
    599 	}
    600 
    601 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    602 	    (void *)&scb, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    603 	if (error) {
    604 		aprint_error_dev(&sc->sc_dev, "can't map memory for scbs\n");
    605 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    606 		return (error);
    607 	}
    608 
    609  have_mem:
    610 	memset(scb, 0, size);
    611 	while (size > sizeof(struct wds_scb) && sc->sc_numscbs < WDS_SCB_MAX) {
    612 		error = wds_init_scb(sc, scb);
    613 		if (error) {
    614 			aprint_error_dev(&sc->sc_dev, "can't initialize scb\n");
    615 			return (error);
    616 		}
    617 		TAILQ_INSERT_TAIL(&sc->sc_free_scb, scb, chain);
    618 		scb = (struct wds_scb *)((char *)scb +
    619 			ALIGN(sizeof(struct wds_scb)));
    620 		size -= ALIGN(sizeof(struct wds_scb));
    621 		sc->sc_numscbs++;
    622 	}
    623 
    624 	return (0);
    625 }
    626 
    627 /*
    628  * Get a free scb
    629  *
    630  * If there are none, see if we can allocate a new one.  If so, put it in
    631  * the hash table too otherwise either return an error or sleep.
    632  */
    633 struct wds_scb *
    634 wds_get_scb(struct wds_softc *sc)
    635 {
    636 	struct wds_scb *scb;
    637 	int s;
    638 
    639 	s = splbio();
    640 	scb = TAILQ_FIRST(&sc->sc_free_scb);
    641 	if (scb != NULL) {
    642 		TAILQ_REMOVE(&sc->sc_free_scb, scb, chain);
    643 		scb->flags |= SCB_ALLOC;
    644 	}
    645 	splx(s);
    646 	return (scb);
    647 }
    648 
    649 struct wds_scb *
    650 wds_scb_phys_kv(struct wds_softc *sc, u_long scb_phys)
    651 {
    652 	int hashnum = SCB_HASH(scb_phys);
    653 	struct wds_scb *scb = sc->sc_scbhash[hashnum];
    654 
    655 	while (scb) {
    656 		if (scb->hashkey == scb_phys)
    657 			break;
    658 		/* XXX Check to see if it matches the sense command block. */
    659 		if (scb->hashkey == (scb_phys - sizeof(struct wds_cmd)))
    660 			break;
    661 		scb = scb->nexthash;
    662 	}
    663 	return (scb);
    664 }
    665 
    666 /*
    667  * Queue a SCB to be sent to the controller, and send it if possible.
    668  */
    669 void
    670 wds_queue_scb(struct wds_softc *sc, struct wds_scb *scb)
    671 {
    672 
    673 	TAILQ_INSERT_TAIL(&sc->sc_waiting_scb, scb, chain);
    674 	wds_start_scbs(sc);
    675 }
    676 
    677 /*
    678  * Garbage collect mailboxes that are no longer in use.
    679  */
    680 void
    681 wds_collect_mbo(struct wds_softc *sc)
    682 {
    683 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
    684 #ifdef WDSDIAG
    685 	struct wds_scb *scb;
    686 #endif
    687 
    688 	wmbo = wmbx->cmbo;
    689 
    690 	while (sc->sc_mbofull > 0) {
    691 		if (wmbo->cmd != WDS_MBO_FREE)
    692 			break;
    693 
    694 #ifdef WDSDIAG
    695 		scb = wds_scb_phys_kv(sc, phystol(wmbo->scb_addr));
    696 		scb->flags &= ~SCB_SENDING;
    697 #endif
    698 
    699 		--sc->sc_mbofull;
    700 		wds_nextmbx(wmbo, wmbx, mbo);
    701 	}
    702 
    703 	wmbx->cmbo = wmbo;
    704 }
    705 
    706 /*
    707  * Send as many SCBs as we have empty mailboxes for.
    708  */
    709 void
    710 wds_start_scbs(struct wds_softc *sc)
    711 {
    712 	bus_space_tag_t iot = sc->sc_iot;
    713 	bus_space_handle_t ioh = sc->sc_ioh;
    714 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
    715 	struct wds_scb *scb;
    716 	u_char c;
    717 
    718 	wmbo = wmbx->tmbo;
    719 
    720 	while ((scb = sc->sc_waiting_scb.tqh_first) != NULL) {
    721 		if (sc->sc_mbofull >= WDS_MBX_SIZE) {
    722 			wds_collect_mbo(sc);
    723 			if (sc->sc_mbofull >= WDS_MBX_SIZE) {
    724 				c = WDSC_IRQMFREE;
    725 				wds_cmd(iot, ioh, &c, sizeof c);
    726 				break;
    727 			}
    728 		}
    729 
    730 		TAILQ_REMOVE(&sc->sc_waiting_scb, scb, chain);
    731 #ifdef WDSDIAG
    732 		scb->flags |= SCB_SENDING;
    733 #endif
    734 
    735 		/* Link scb to mbo. */
    736 		ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
    737 		    offsetof(struct wds_scb, cmd), wmbo->scb_addr);
    738 		/* XXX What about aborts? */
    739 		wmbo->cmd = WDS_MBO_START;
    740 
    741 		/* Tell the card to poll immediately. */
    742 		c = WDSC_MSTART(wmbo - wmbx->mbo);
    743 		wds_cmd(sc->sc_iot, sc->sc_ioh, &c, sizeof c);
    744 
    745 		if ((scb->flags & SCB_POLLED) == 0)
    746 			callout_reset(&scb->xs->xs_callout,
    747 			    mstohz(scb->timeout), wds_timeout, scb);
    748 
    749 		++sc->sc_mbofull;
    750 		wds_nextmbx(wmbo, wmbx, mbo);
    751 	}
    752 
    753 	wmbx->tmbo = wmbo;
    754 }
    755 
    756 /*
    757  * Process the result of a SCSI command.
    758  */
    759 void
    760 wds_done(struct wds_softc *sc, struct wds_scb *scb, u_char stat)
    761 {
    762 	bus_dma_tag_t dmat = sc->sc_dmat;
    763 	struct scsipi_xfer *xs = scb->xs;
    764 
    765 	/* XXXXX */
    766 
    767 	/* Don't release the SCB if it was an internal command. */
    768 	if (xs == 0) {
    769 		scb->flags |= SCB_DONE;
    770 		return;
    771 	}
    772 
    773 	/*
    774 	 * If we were a data transfer, unload the map that described
    775 	 * the data buffer.
    776 	 */
    777 	if (xs->datalen) {
    778 		bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
    779 		    scb->dmamap_xfer->dm_mapsize,
    780 		    (xs->xs_control & XS_CTL_DATA_IN) ?
    781 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    782 		bus_dmamap_unload(dmat, scb->dmamap_xfer);
    783 	}
    784 	if (xs->error == XS_NOERROR) {
    785 		/* If all went well, or an error is acceptable. */
    786 		if (stat == WDS_MBI_OK) {
    787 			/* OK, set the result */
    788 			xs->resid = 0;
    789 		} else {
    790 			/* Check the mailbox status. */
    791 			switch (stat) {
    792 			case WDS_MBI_OKERR:
    793 				/*
    794 				 * SCSI error recorded in scb,
    795 				 * counts as WDS_MBI_OK
    796 				 */
    797 				switch (scb->cmd.venderr) {
    798 				case 0x00:
    799 					aprint_error_dev(&sc->sc_dev, "Is this "
    800 					    "an error?\n");
    801 					/* Experiment. */
    802 					xs->error = XS_DRIVER_STUFFUP;
    803 					break;
    804 				case 0x01:
    805 #if 0
    806 					aprint_error_dev(&sc->sc_dev, "OK, see SCSI "
    807 					    "error field.\n");
    808 #endif
    809 					if (scb->cmd.stat == SCSI_CHECK ||
    810 					    scb->cmd.stat == SCSI_BUSY) {
    811 						xs->status = scb->cmd.stat;
    812 						xs->error = XS_BUSY;
    813 					}
    814 					break;
    815 				case 0x40:
    816 #if 0
    817 					printf("%s: DMA underrun!\n",
    818 					    device_xname(&sc->sc_dev));
    819 #endif
    820 					/*
    821 					 * Hits this if the target
    822 					 * returns fewer that datalen
    823 					 * bytes (eg my CD-ROM, which
    824 					 * returns a short version
    825 					 * string, or if DMA is
    826 					 * turned off etc.
    827 					 */
    828 					xs->resid = 0;
    829 					break;
    830 				default:
    831 					printf("%s: VENDOR ERROR "
    832 					    "%02x, scsi %02x\n",
    833 					    device_xname(&sc->sc_dev),
    834 					    scb->cmd.venderr,
    835 					    scb->cmd.stat);
    836 					/* Experiment. */
    837 					xs->error = XS_DRIVER_STUFFUP;
    838 					break;
    839 				}
    840 					break;
    841 			case WDS_MBI_ETIME:
    842 				/*
    843 				 * The documentation isn't clear on
    844 				 * what conditions might generate this,
    845 				 * but selection timeouts are the only
    846 				 * one I can think of.
    847 				 */
    848 				xs->error = XS_SELTIMEOUT;
    849 				break;
    850 			case WDS_MBI_ERESET:
    851 			case WDS_MBI_ETARCMD:
    852 			case WDS_MBI_ERESEL:
    853 			case WDS_MBI_ESEL:
    854 			case WDS_MBI_EABORT:
    855 			case WDS_MBI_ESRESET:
    856 			case WDS_MBI_EHRESET:
    857 				xs->error = XS_DRIVER_STUFFUP;
    858 				break;
    859 			}
    860 		}
    861 	} /* XS_NOERROR */
    862 
    863 	wds_free_scb(sc, scb);
    864 	scsipi_done(xs);
    865 }
    866 
    867 int
    868 wds_find(bus_space_tag_t iot, bus_space_handle_t ioh, struct wds_probe_data *sc)
    869 {
    870 	int i;
    871 
    872 	/* XXXXX */
    873 
    874 	/*
    875 	 * Sending a command causes the CMDRDY bit to clear.
    876  	 */
    877 	for (i = 5; i; i--) {
    878 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
    879 			break;
    880 		delay(100);
    881 	}
    882 	if (!i)
    883 		return 0;
    884 
    885 	bus_space_write_1(iot, ioh, WDS_CMD, WDSC_NOOP);
    886 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
    887 		return 0;
    888 
    889 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_SCSIRESET|WDSH_ASCRESET);
    890 	delay(10000);
    891 	bus_space_write_1(iot, ioh, WDS_HCR, 0x00);
    892 	delay(500000);
    893 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
    894 	if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 1)
    895 		if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 7)
    896 			return 0;
    897 
    898 	for (i = 2000; i; i--) {
    899 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
    900 			break;
    901 		delay(100);
    902 	}
    903 	if (!i)
    904 		return 0;
    905 
    906 	if (sc) {
    907 #ifdef notyet
    908 		sc->sc_irq = ...;
    909 		sc->sc_drq = ...;
    910 #endif
    911 		/* XXX Can we do this better? */
    912 		sc->sc_scsi_dev = 7;
    913 	}
    914 
    915 	return 1;
    916 }
    917 
    918 /*
    919  * Initialise the board and driver.
    920  */
    921 void
    922 wds_init(struct wds_softc *sc, int isreset)
    923 {
    924 	bus_space_tag_t iot = sc->sc_iot;
    925 	bus_space_handle_t ioh = sc->sc_ioh;
    926 	bus_dma_segment_t seg;
    927 	struct wds_setup init;
    928 	u_char c;
    929 	int i, rseg;
    930 
    931 	if (isreset)
    932 		goto doinit;
    933 
    934 	/*
    935 	 * Allocate the mailbox.
    936 	 */
    937 	if (bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, &seg, 1,
    938 	    &rseg, BUS_DMA_NOWAIT) ||
    939 	    bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
    940 	    (void **)&wmbx, BUS_DMA_NOWAIT|BUS_DMA_COHERENT))
    941 		panic("wds_init: can't create or map mailbox");
    942 
    943 	/*
    944 	 * Since DMA memory allocation is always rounded up to a
    945 	 * page size, create some scbs from the leftovers.
    946 	 */
    947 	if (wds_create_scbs(sc, ((char *)wmbx) +
    948 	    ALIGN(sizeof(struct wds_mbx)),
    949 	    PAGE_SIZE - ALIGN(sizeof(struct wds_mbx))))
    950 		panic("wds_init: can't create scbs");
    951 
    952 	/*
    953 	 * Create and load the mailbox DMA map.
    954 	 */
    955 	if (bus_dmamap_create(sc->sc_dmat, sizeof(struct wds_mbx), 1,
    956 	    sizeof(struct wds_mbx), 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_mbox) ||
    957 	    bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox, wmbx,
    958 	    sizeof(struct wds_mbx), NULL, BUS_DMA_NOWAIT))
    959 		panic("wds_ionit: can't create or load mailbox DMA map");
    960 
    961  doinit:
    962 	/*
    963 	 * Set up initial mail box for round-robin operation.
    964 	 */
    965 	for (i = 0; i < WDS_MBX_SIZE; i++) {
    966 		wmbx->mbo[i].cmd = WDS_MBO_FREE;
    967 		wmbx->mbi[i].stat = WDS_MBI_FREE;
    968 	}
    969 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
    970 	wmbx->tmbi = &wmbx->mbi[0];
    971 	sc->sc_mbofull = 0;
    972 
    973 	init.opcode = WDSC_INIT;
    974 	init.scsi_id = sc->sc_channel.chan_id;
    975 	init.buson_t = 48;
    976 	init.busoff_t = 24;
    977 	init.xx = 0;
    978 	ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, init.mbaddr);
    979 	init.nomb = init.nimb = WDS_MBX_SIZE;
    980 	wds_cmd(iot, ioh, (u_char *)&init, sizeof init);
    981 
    982 	wds_wait(iot, ioh, WDS_STAT, WDSS_INIT, WDSS_INIT);
    983 
    984 	c = WDSC_DISUNSOL;
    985 	wds_cmd(iot, ioh, &c, sizeof c);
    986 }
    987 
    988 /*
    989  * Read the board's firmware revision information.
    990  */
    991 void
    992 wds_inquire_setup_information(struct wds_softc *sc)
    993 {
    994 	bus_space_tag_t iot = sc->sc_iot;
    995 	bus_space_handle_t ioh = sc->sc_ioh;
    996 	struct wds_scb *scb;
    997 	u_char *j;
    998 	int s;
    999 
   1000 	sc->sc_maxsegs = 1;
   1001 
   1002 	scb = wds_get_scb(sc);
   1003 	if (scb == 0)
   1004 		panic("wds_inquire_setup_information: no scb available");
   1005 
   1006 	scb->xs = NULL;
   1007 	scb->timeout = 40;
   1008 
   1009 	memset(&scb->cmd, 0, sizeof scb->cmd);
   1010 	scb->cmd.write = 0x80;
   1011 	scb->cmd.opcode = WDSX_GETFIRMREV;
   1012 
   1013 	/* Will poll card, await result. */
   1014 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
   1015 	scb->flags |= SCB_POLLED;
   1016 
   1017 	s = splbio();
   1018 	wds_queue_scb(sc, scb);
   1019 	splx(s);
   1020 
   1021 	if (wds_ipoll(sc, scb, scb->timeout))
   1022 		goto out;
   1023 
   1024 	/* Print the version number. */
   1025 	printf("%s: version %x.%02x ", device_xname(&sc->sc_dev),
   1026 	    scb->cmd.targ, scb->cmd.scb[0]);
   1027 	sc->sc_revision = (scb->cmd.targ << 8) | scb->cmd.scb[0];
   1028 	/* Print out the version string. */
   1029 	j = 2 + &(scb->cmd.targ);
   1030 	while ((*j >= 32) && (*j < 128)) {
   1031 		printf("%c", *j);
   1032 		j++;
   1033 	}
   1034 
   1035 	/*
   1036 	 * Determine if we can use scatter/gather.
   1037 	 */
   1038 	if (sc->sc_revision >= 0x800)
   1039 		sc->sc_maxsegs = WDS_NSEG;
   1040 
   1041 out:
   1042 	printf("\n");
   1043 
   1044 	/*
   1045 	 * Free up the resources used by this scb.
   1046 	 */
   1047 	wds_free_scb(sc, scb);
   1048 }
   1049 
   1050 void
   1051 wdsminphys(struct buf *bp)
   1052 {
   1053 
   1054 	if (bp->b_bcount > WDS_MAXXFER)
   1055 		bp->b_bcount = WDS_MAXXFER;
   1056 	minphys(bp);
   1057 }
   1058 
   1059 /*
   1060  * Send a SCSI command.
   1061  */
   1062 void
   1063 wds_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
   1064 {
   1065 	struct scsipi_xfer *xs;
   1066 	struct scsipi_periph *periph;
   1067 	struct wds_softc *sc = (void *)chan->chan_adapter->adapt_dev;
   1068 	bus_dma_tag_t dmat = sc->sc_dmat;
   1069 	struct wds_scb *scb;
   1070 	int error, seg, flags, s;
   1071 
   1072 	switch (req) {
   1073 	case ADAPTER_REQ_RUN_XFER:
   1074 		xs = arg;
   1075 		periph = xs->xs_periph;
   1076 
   1077 		if (xs->xs_control & XS_CTL_RESET) {
   1078 			/* XXX Fix me! */
   1079 			printf("%s: reset!\n", device_xname(&sc->sc_dev));
   1080 			wds_init(sc, 1);
   1081 			scsipi_done(xs);
   1082 			return;
   1083 		}
   1084 
   1085 		if (xs->xs_control & XS_CTL_DATA_UIO) {
   1086 			/* XXX Fix me! */
   1087 			/*
   1088 			 * Let's not worry about UIO. There isn't any code
   1089 			 * for the non-SG boards anyway!
   1090 			 */
   1091 			aprint_error_dev(&sc->sc_dev, "UIO is untested and disabled!\n");
   1092 			xs->error = XS_DRIVER_STUFFUP;
   1093 			scsipi_done(xs);
   1094 			return;
   1095 		}
   1096 
   1097 		flags = xs->xs_control;
   1098 
   1099 		/* Get an SCB to use. */
   1100 		scb = wds_get_scb(sc);
   1101 #ifdef DIAGNOSTIC
   1102 		/*
   1103 		 * This should never happen as we track the resources
   1104 		 * in the mid-layer.
   1105 		 */
   1106 		if (scb == NULL) {
   1107 			scsipi_printaddr(periph);
   1108 			printf("unable to allocate scb\n");
   1109 			panic("wds_scsipi_request");
   1110 		}
   1111 #endif
   1112 
   1113 		scb->xs = xs;
   1114 		scb->timeout = xs->timeout;
   1115 
   1116 		/* Zero out the command structure. */
   1117 		if (xs->cmdlen > sizeof(scb->cmd.scb)) {
   1118 			aprint_error_dev(&sc->sc_dev, "cmdlen %d too large for SCB\n",
   1119 			    xs->cmdlen);
   1120 			xs->error = XS_DRIVER_STUFFUP;
   1121 			goto out_bad;
   1122 		}
   1123 		memset(&scb->cmd, 0, sizeof scb->cmd);
   1124 		memcpy(&scb->cmd.scb, xs->cmd, xs->cmdlen);
   1125 
   1126 		/* Set up some of the command fields. */
   1127 		scb->cmd.targ = (periph->periph_target << 5) |
   1128 		    periph->periph_lun;
   1129 
   1130 		/*
   1131 		 * NOTE: cmd.write may be OK as 0x40 (disable direction
   1132 		 * checking) on boards other than the WD-7000V-ASE. Need
   1133 		 * this for the ASE:
   1134  		 */
   1135 		scb->cmd.write = (xs->xs_control & XS_CTL_DATA_IN) ?
   1136 		    0x80 : 0x00;
   1137 
   1138 		if (xs->datalen) {
   1139 			seg = 0;
   1140 #ifdef TFS
   1141 			if (flags & XS_CTL_DATA_UIO) {
   1142 				error = bus_dmamap_load_uio(dmat,
   1143 				    scb->dmamap_xfer, (struct uio *)xs->data,
   1144 				    BUS_DMA_NOWAIT |
   1145 				    ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
   1146 				     BUS_DMA_WRITE));
   1147 			} else
   1148 #endif /* TFS */
   1149 			{
   1150 				error = bus_dmamap_load(dmat,
   1151 				    scb->dmamap_xfer, xs->data, xs->datalen,
   1152 				    NULL, BUS_DMA_NOWAIT |
   1153 				    ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
   1154 				     BUS_DMA_WRITE));
   1155 			}
   1156 
   1157 			switch (error) {
   1158 			case 0:
   1159 				break;
   1160 
   1161 			case ENOMEM:
   1162 			case EAGAIN:
   1163 				xs->error = XS_RESOURCE_SHORTAGE;
   1164 				goto out_bad;
   1165 
   1166 			default:
   1167 				xs->error = XS_DRIVER_STUFFUP;
   1168 				aprint_error_dev(&sc->sc_dev, "error %d loading DMA map\n", error);
   1169  out_bad:
   1170 				wds_free_scb(sc, scb);
   1171 				scsipi_done(xs);
   1172 				return;
   1173 			}
   1174 
   1175 			bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
   1176 			    scb->dmamap_xfer->dm_mapsize,
   1177 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
   1178 			    BUS_DMASYNC_PREWRITE);
   1179 
   1180 			if (sc->sc_maxsegs > 1) {
   1181 				/*
   1182 				 * Load the hardware scatter/gather map with the
   1183 				 * contents of the DMA map.
   1184 				 */
   1185 				for (seg = 0;
   1186 				     seg < scb->dmamap_xfer->dm_nsegs; seg++) {
   1187 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_addr,
   1188 					    scb->scat_gath[seg].seg_addr);
   1189 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_len,
   1190 					    scb->scat_gath[seg].seg_len);
   1191 				}
   1192 
   1193 				/*
   1194 				 * Set up for scatter/gather transfer.
   1195 				 */
   1196 				scb->cmd.opcode = WDSX_SCSISG;
   1197 				ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
   1198 				    offsetof(struct wds_scb, scat_gath),
   1199 				    scb->cmd.data);
   1200 				ltophys(scb->dmamap_self->dm_nsegs *
   1201 				    sizeof(struct wds_scat_gath), scb->cmd.len);
   1202 			} else {
   1203 				/*
   1204 				 * This board is an ASC or an ASE, and the
   1205 				 * transfer has been mapped contig for us.
   1206 				 */
   1207 				scb->cmd.opcode = WDSX_SCSICMD;
   1208 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_addr,
   1209 				    scb->cmd.data);
   1210 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_len,
   1211 				    scb->cmd.len);
   1212 			}
   1213 		} else {
   1214 			scb->cmd.opcode = WDSX_SCSICMD;
   1215 			ltophys(0, scb->cmd.data);
   1216 			ltophys(0, scb->cmd.len);
   1217 		}
   1218 
   1219 		scb->cmd.stat = 0x00;
   1220 		scb->cmd.venderr = 0x00;
   1221 		ltophys(0, scb->cmd.link);
   1222 
   1223 		/* XXX Do we really want to do this? */
   1224 		if (flags & XS_CTL_POLL) {
   1225 			/* Will poll card, await result. */
   1226 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1227 			    WDS_HCR, WDSH_DRQEN);
   1228 			scb->flags |= SCB_POLLED;
   1229 		} else {
   1230 			/*
   1231 			 * Will send command, let interrupt routine
   1232 			 * handle result.
   1233 			 */
   1234 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR,
   1235 			    WDSH_IRQEN | WDSH_DRQEN);
   1236 		}
   1237 
   1238 		s = splbio();
   1239 		wds_queue_scb(sc, scb);
   1240 		splx(s);
   1241 
   1242 		if ((flags & XS_CTL_POLL) == 0)
   1243 			return;
   1244 
   1245 		if (wds_poll(sc, xs, scb->timeout)) {
   1246 			wds_timeout(scb);
   1247 			if (wds_poll(sc, xs, scb->timeout))
   1248 				wds_timeout(scb);
   1249 		}
   1250 		return;
   1251 
   1252 	case ADAPTER_REQ_GROW_RESOURCES:
   1253 		/* XXX Not supported. */
   1254 		return;
   1255 
   1256 	case ADAPTER_REQ_SET_XFER_MODE:
   1257 		/* XXX How do we do this? */
   1258 		return;
   1259 	}
   1260 }
   1261 
   1262 /*
   1263  * Poll a particular unit, looking for a particular scb
   1264  */
   1265 int
   1266 wds_poll(struct wds_softc *sc, struct scsipi_xfer *xs, int count)
   1267 {
   1268 	bus_space_tag_t iot = sc->sc_iot;
   1269 	bus_space_handle_t ioh = sc->sc_ioh;
   1270 
   1271 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1272 	while (count) {
   1273 		/*
   1274 		 * If we had interrupts enabled, would we
   1275 		 * have got an interrupt?
   1276 		 */
   1277 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
   1278 			wdsintr(sc);
   1279 		if (xs->xs_status & XS_STS_DONE)
   1280 			return 0;
   1281 		delay(1000);	/* only happens in boot so ok */
   1282 		count--;
   1283 	}
   1284 	return 1;
   1285 }
   1286 
   1287 /*
   1288  * Poll a particular unit, looking for a particular scb
   1289  */
   1290 int
   1291 wds_ipoll(struct wds_softc *sc, struct wds_scb *scb, int count)
   1292 {
   1293 	bus_space_tag_t iot = sc->sc_iot;
   1294 	bus_space_handle_t ioh = sc->sc_ioh;
   1295 
   1296 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1297 	while (count) {
   1298 		/*
   1299 		 * If we had interrupts enabled, would we
   1300 		 * have got an interrupt?
   1301 		 */
   1302 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
   1303 			wdsintr(sc);
   1304 		if (scb->flags & SCB_DONE)
   1305 			return 0;
   1306 		delay(1000);	/* only happens in boot so ok */
   1307 		count--;
   1308 	}
   1309 	return 1;
   1310 }
   1311 
   1312 void
   1313 wds_timeout(void *arg)
   1314 {
   1315 	struct wds_scb *scb = arg;
   1316 	struct scsipi_xfer *xs = scb->xs;
   1317 	struct scsipi_periph *periph = xs->xs_periph;
   1318 	struct wds_softc *sc =
   1319 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
   1320 	int s;
   1321 
   1322 	scsipi_printaddr(periph);
   1323 	printf("timed out");
   1324 
   1325 	s = splbio();
   1326 
   1327 #ifdef WDSDIAG
   1328 	/*
   1329 	 * If The scb's mbx is not free, then the board has gone south?
   1330 	 */
   1331 	wds_collect_mbo(sc);
   1332 	if (scb->flags & SCB_SENDING) {
   1333 		aprint_error_dev(&sc->sc_dev, "not taking commands!\n");
   1334 		Debugger();
   1335 	}
   1336 #endif
   1337 
   1338 	/*
   1339 	 * If it has been through before, then
   1340 	 * a previous abort has failed, don't
   1341 	 * try abort again
   1342 	 */
   1343 	if (scb->flags & SCB_ABORT) {
   1344 		/* abort timed out */
   1345 		printf(" AGAIN\n");
   1346 		/* XXX Must reset! */
   1347 	} else {
   1348 		/* abort the operation that has timed out */
   1349 		printf("\n");
   1350 		scb->xs->error = XS_TIMEOUT;
   1351 		scb->timeout = WDS_ABORT_TIMEOUT;
   1352 		scb->flags |= SCB_ABORT;
   1353 		wds_queue_scb(sc, scb);
   1354 	}
   1355 
   1356 	splx(s);
   1357 }
   1358