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