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