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