Home | History | Annotate | Line # | Download | only in isa
wds.c revision 1.52
      1 /*	$NetBSD: wds.c,v 1.52 2002/10/02 03:10:50 thorpej 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.52 2002/10/02 03:10:50 thorpej 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 __P((bus_space_tag_t, bus_space_handle_t, int, int, int));
    185 int     wds_cmd __P((bus_space_tag_t, bus_space_handle_t, u_char *, int));
    186 integrate void wds_finish_scbs __P((struct wds_softc *));
    187 int     wdsintr __P((void *));
    188 integrate void wds_reset_scb __P((struct wds_softc *, struct wds_scb *));
    189 void    wds_free_scb __P((struct wds_softc *, struct wds_scb *));
    190 integrate int wds_init_scb __P((struct wds_softc *, struct wds_scb *));
    191 struct	wds_scb *wds_get_scb __P((struct wds_softc *));
    192 struct	wds_scb *wds_scb_phys_kv __P((struct wds_softc *, u_long));
    193 void	wds_queue_scb __P((struct wds_softc *, struct wds_scb *));
    194 void	wds_collect_mbo __P((struct wds_softc *));
    195 void	wds_start_scbs __P((struct wds_softc *));
    196 void    wds_done __P((struct wds_softc *, struct wds_scb *, u_char));
    197 int	wds_find __P((bus_space_tag_t, bus_space_handle_t, struct wds_probe_data *));
    198 void	wds_attach __P((struct wds_softc *, struct wds_probe_data *));
    199 void	wds_init __P((struct wds_softc *, int));
    200 void	wds_inquire_setup_information __P((struct wds_softc *));
    201 void    wdsminphys __P((struct buf *));
    202 void	wds_scsipi_request __P((struct scsipi_channel *,
    203 	    scsipi_adapter_req_t, void *));
    204 int	wds_poll __P((struct wds_softc *, struct scsipi_xfer *, int));
    205 int	wds_ipoll __P((struct wds_softc *, struct wds_scb *, int));
    206 void	wds_timeout __P((void *));
    207 int	wds_create_scbs __P((struct wds_softc *, void *, size_t));
    208 
    209 int	wdsprobe __P((struct device *, struct cfdata *, void *));
    210 void	wdsattach __P((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 == ISACF_PORT_DEFAULT)
    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 != ISACF_IRQ_DEFAULT &&
    293 		    ia->ia_irq[0].ir_irq != wpd.sc_irq)
    294 			return (0);
    295 		if (ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT &&
    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 == ISACF_IRQ_DEFAULT)
    306 			return (0);
    307 		if (ia->ia_drq[0].ir_drq == ISACF_DRQ_DEFAULT)
    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 	    (caddr_t *)&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 		(caddr_t)scb += ALIGN(sizeof(struct wds_scb));
    658 		size -= ALIGN(sizeof(struct wds_scb));
    659 		sc->sc_numscbs++;
    660 	}
    661 
    662 	return (0);
    663 }
    664 
    665 /*
    666  * Get a free scb
    667  *
    668  * If there are none, see if we can allocate a new one.  If so, put it in
    669  * the hash table too otherwise either return an error or sleep.
    670  */
    671 struct wds_scb *
    672 wds_get_scb(sc)
    673 	struct wds_softc *sc;
    674 {
    675 	struct wds_scb *scb;
    676 	int s;
    677 
    678 	s = splbio();
    679 	scb = TAILQ_FIRST(&sc->sc_free_scb);
    680 	if (scb != NULL) {
    681 		TAILQ_REMOVE(&sc->sc_free_scb, scb, chain);
    682 		scb->flags |= SCB_ALLOC;
    683 	}
    684 	splx(s);
    685 	return (scb);
    686 }
    687 
    688 struct wds_scb *
    689 wds_scb_phys_kv(sc, scb_phys)
    690 	struct wds_softc *sc;
    691 	u_long scb_phys;
    692 {
    693 	int hashnum = SCB_HASH(scb_phys);
    694 	struct wds_scb *scb = sc->sc_scbhash[hashnum];
    695 
    696 	while (scb) {
    697 		if (scb->hashkey == scb_phys)
    698 			break;
    699 		/* XXX Check to see if it matches the sense command block. */
    700 		if (scb->hashkey == (scb_phys - sizeof(struct wds_cmd)))
    701 			break;
    702 		scb = scb->nexthash;
    703 	}
    704 	return (scb);
    705 }
    706 
    707 /*
    708  * Queue a SCB to be sent to the controller, and send it if possible.
    709  */
    710 void
    711 wds_queue_scb(sc, scb)
    712 	struct wds_softc *sc;
    713 	struct wds_scb *scb;
    714 {
    715 
    716 	TAILQ_INSERT_TAIL(&sc->sc_waiting_scb, scb, chain);
    717 	wds_start_scbs(sc);
    718 }
    719 
    720 /*
    721  * Garbage collect mailboxes that are no longer in use.
    722  */
    723 void
    724 wds_collect_mbo(sc)
    725 	struct wds_softc *sc;
    726 {
    727 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
    728 #ifdef WDSDIAG
    729 	struct wds_scb *scb;
    730 #endif
    731 
    732 	wmbo = wmbx->cmbo;
    733 
    734 	while (sc->sc_mbofull > 0) {
    735 		if (wmbo->cmd != WDS_MBO_FREE)
    736 			break;
    737 
    738 #ifdef WDSDIAG
    739 		scb = wds_scb_phys_kv(sc, phystol(wmbo->scb_addr));
    740 		scb->flags &= ~SCB_SENDING;
    741 #endif
    742 
    743 		--sc->sc_mbofull;
    744 		wds_nextmbx(wmbo, wmbx, mbo);
    745 	}
    746 
    747 	wmbx->cmbo = wmbo;
    748 }
    749 
    750 /*
    751  * Send as many SCBs as we have empty mailboxes for.
    752  */
    753 void
    754 wds_start_scbs(sc)
    755 	struct wds_softc *sc;
    756 {
    757 	bus_space_tag_t iot = sc->sc_iot;
    758 	bus_space_handle_t ioh = sc->sc_ioh;
    759 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
    760 	struct wds_scb *scb;
    761 	u_char c;
    762 
    763 	wmbo = wmbx->tmbo;
    764 
    765 	while ((scb = sc->sc_waiting_scb.tqh_first) != NULL) {
    766 		if (sc->sc_mbofull >= WDS_MBX_SIZE) {
    767 			wds_collect_mbo(sc);
    768 			if (sc->sc_mbofull >= WDS_MBX_SIZE) {
    769 				c = WDSC_IRQMFREE;
    770 				wds_cmd(iot, ioh, &c, sizeof c);
    771 				break;
    772 			}
    773 		}
    774 
    775 		TAILQ_REMOVE(&sc->sc_waiting_scb, scb, chain);
    776 #ifdef WDSDIAG
    777 		scb->flags |= SCB_SENDING;
    778 #endif
    779 
    780 		/* Link scb to mbo. */
    781 		ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
    782 		    offsetof(struct wds_scb, cmd), wmbo->scb_addr);
    783 		/* XXX What about aborts? */
    784 		wmbo->cmd = WDS_MBO_START;
    785 
    786 		/* Tell the card to poll immediately. */
    787 		c = WDSC_MSTART(wmbo - wmbx->mbo);
    788 		wds_cmd(sc->sc_iot, sc->sc_ioh, &c, sizeof c);
    789 
    790 		if ((scb->flags & SCB_POLLED) == 0)
    791 			callout_reset(&scb->xs->xs_callout,
    792 			    mstohz(scb->timeout), wds_timeout, scb);
    793 
    794 		++sc->sc_mbofull;
    795 		wds_nextmbx(wmbo, wmbx, mbo);
    796 	}
    797 
    798 	wmbx->tmbo = wmbo;
    799 }
    800 
    801 /*
    802  * Process the result of a SCSI command.
    803  */
    804 void
    805 wds_done(sc, scb, stat)
    806 	struct wds_softc *sc;
    807 	struct wds_scb *scb;
    808 	u_char stat;
    809 {
    810 	bus_dma_tag_t dmat = sc->sc_dmat;
    811 	struct scsipi_xfer *xs = scb->xs;
    812 
    813 	/* XXXXX */
    814 
    815 	/* Don't release the SCB if it was an internal command. */
    816 	if (xs == 0) {
    817 		scb->flags |= SCB_DONE;
    818 		return;
    819 	}
    820 
    821 	/*
    822 	 * If we were a data transfer, unload the map that described
    823 	 * the data buffer.
    824 	 */
    825 	if (xs->datalen) {
    826 		bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
    827 		    scb->dmamap_xfer->dm_mapsize,
    828 		    (xs->xs_control & XS_CTL_DATA_IN) ?
    829 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    830 		bus_dmamap_unload(dmat, scb->dmamap_xfer);
    831 	}
    832 	if (xs->error == XS_NOERROR) {
    833 		/* If all went well, or an error is acceptable. */
    834 		if (stat == WDS_MBI_OK) {
    835 			/* OK, set the result */
    836 			xs->resid = 0;
    837 		} else {
    838 			/* Check the mailbox status. */
    839 			switch (stat) {
    840 			case WDS_MBI_OKERR:
    841 				/*
    842 				 * SCSI error recorded in scb,
    843 				 * counts as WDS_MBI_OK
    844 				 */
    845 				switch (scb->cmd.venderr) {
    846 				case 0x00:
    847 					printf("%s: Is this "
    848 					    "an error?\n",
    849 					    sc->sc_dev.dv_xname);
    850 					/* Experiment. */
    851 					xs->error = XS_DRIVER_STUFFUP;
    852 					break;
    853 				case 0x01:
    854 #if 0
    855 					printf("%s: OK, see SCSI "
    856 					    "error field.\n",
    857 					    sc->sc_dev.dv_xname);
    858 #endif
    859 					if (scb->cmd.stat == SCSI_CHECK ||
    860 					    scb->cmd.stat == SCSI_BUSY) {
    861 						xs->status = scb->cmd.stat;
    862 						xs->error = XS_BUSY;
    863 					}
    864 					break;
    865 				case 0x40:
    866 #if 0
    867 					printf("%s: DMA underrun!\n",
    868 					    sc->sc_dev.dv_xname);
    869 #endif
    870 					/*
    871 					 * Hits this if the target
    872 					 * returns fewer that datalen
    873 					 * bytes (eg my CD-ROM, which
    874 					 * returns a short version
    875 					 * string, or if DMA is
    876 					 * turned off etc.
    877 					 */
    878 					xs->resid = 0;
    879 					break;
    880 				default:
    881 					printf("%s: VENDOR ERROR "
    882 					    "%02x, scsi %02x\n",
    883 					    sc->sc_dev.dv_xname,
    884 					    scb->cmd.venderr,
    885 					    scb->cmd.stat);
    886 					/* Experiment. */
    887 					xs->error = XS_DRIVER_STUFFUP;
    888 					break;
    889 				}
    890 					break;
    891 			case WDS_MBI_ETIME:
    892 				/*
    893 				 * The documentation isn't clear on
    894 				 * what conditions might generate this,
    895 				 * but selection timeouts are the only
    896 				 * one I can think of.
    897 				 */
    898 				xs->error = XS_SELTIMEOUT;
    899 				break;
    900 			case WDS_MBI_ERESET:
    901 			case WDS_MBI_ETARCMD:
    902 			case WDS_MBI_ERESEL:
    903 			case WDS_MBI_ESEL:
    904 			case WDS_MBI_EABORT:
    905 			case WDS_MBI_ESRESET:
    906 			case WDS_MBI_EHRESET:
    907 				xs->error = XS_DRIVER_STUFFUP;
    908 				break;
    909 			}
    910 		}
    911 	} /* XS_NOERROR */
    912 
    913 	wds_free_scb(sc, scb);
    914 	scsipi_done(xs);
    915 }
    916 
    917 int
    918 wds_find(iot, ioh, sc)
    919 	bus_space_tag_t iot;
    920 	bus_space_handle_t ioh;
    921 	struct wds_probe_data *sc;
    922 {
    923 	int i;
    924 
    925 	/* XXXXX */
    926 
    927 	/*
    928 	 * Sending a command causes the CMDRDY bit to clear.
    929  	 */
    930 	for (i = 5; i; i--) {
    931 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
    932 			break;
    933 		delay(100);
    934 	}
    935 	if (!i)
    936 		return 0;
    937 
    938 	bus_space_write_1(iot, ioh, WDS_CMD, WDSC_NOOP);
    939 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
    940 		return 0;
    941 
    942 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_SCSIRESET|WDSH_ASCRESET);
    943 	delay(10000);
    944 	bus_space_write_1(iot, ioh, WDS_HCR, 0x00);
    945 	delay(500000);
    946 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
    947 	if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 1)
    948 		if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 7)
    949 			return 0;
    950 
    951 	for (i = 2000; i; i--) {
    952 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
    953 			break;
    954 		delay(100);
    955 	}
    956 	if (!i)
    957 		return 0;
    958 
    959 	if (sc) {
    960 #ifdef notyet
    961 		sc->sc_irq = ...;
    962 		sc->sc_drq = ...;
    963 #endif
    964 		/* XXX Can we do this better? */
    965 		sc->sc_scsi_dev = 7;
    966 	}
    967 
    968 	return 1;
    969 }
    970 
    971 /*
    972  * Initialise the board and driver.
    973  */
    974 void
    975 wds_init(sc, isreset)
    976 	struct wds_softc *sc;
    977 	int isreset;
    978 {
    979 	bus_space_tag_t iot = sc->sc_iot;
    980 	bus_space_handle_t ioh = sc->sc_ioh;
    981 	bus_dma_segment_t seg;
    982 	struct wds_setup init;
    983 	u_char c;
    984 	int i, rseg;
    985 
    986 	if (isreset)
    987 		goto doinit;
    988 
    989 	/*
    990 	 * Allocate the mailbox.
    991 	 */
    992 	if (bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, &seg, 1,
    993 	    &rseg, BUS_DMA_NOWAIT) ||
    994 	    bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
    995 	    (caddr_t *)&wmbx, BUS_DMA_NOWAIT|BUS_DMA_COHERENT))
    996 		panic("wds_init: can't create or map mailbox");
    997 
    998 	/*
    999 	 * Since DMA memory allocation is always rounded up to a
   1000 	 * page size, create some scbs from the leftovers.
   1001 	 */
   1002 	if (wds_create_scbs(sc, ((caddr_t)wmbx) +
   1003 	    ALIGN(sizeof(struct wds_mbx)),
   1004 	    PAGE_SIZE - ALIGN(sizeof(struct wds_mbx))))
   1005 		panic("wds_init: can't create scbs");
   1006 
   1007 	/*
   1008 	 * Create and load the mailbox DMA map.
   1009 	 */
   1010 	if (bus_dmamap_create(sc->sc_dmat, sizeof(struct wds_mbx), 1,
   1011 	    sizeof(struct wds_mbx), 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_mbox) ||
   1012 	    bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox, wmbx,
   1013 	    sizeof(struct wds_mbx), NULL, BUS_DMA_NOWAIT))
   1014 		panic("wds_ionit: can't craete or load mailbox dma map");
   1015 
   1016  doinit:
   1017 	/*
   1018 	 * Set up initial mail box for round-robin operation.
   1019 	 */
   1020 	for (i = 0; i < WDS_MBX_SIZE; i++) {
   1021 		wmbx->mbo[i].cmd = WDS_MBO_FREE;
   1022 		wmbx->mbi[i].stat = WDS_MBI_FREE;
   1023 	}
   1024 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
   1025 	wmbx->tmbi = &wmbx->mbi[0];
   1026 	sc->sc_mbofull = 0;
   1027 
   1028 	init.opcode = WDSC_INIT;
   1029 	init.scsi_id = sc->sc_channel.chan_id;
   1030 	init.buson_t = 48;
   1031 	init.busoff_t = 24;
   1032 	init.xx = 0;
   1033 	ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, init.mbaddr);
   1034 	init.nomb = init.nimb = WDS_MBX_SIZE;
   1035 	wds_cmd(iot, ioh, (u_char *)&init, sizeof init);
   1036 
   1037 	wds_wait(iot, ioh, WDS_STAT, WDSS_INIT, WDSS_INIT);
   1038 
   1039 	c = WDSC_DISUNSOL;
   1040 	wds_cmd(iot, ioh, &c, sizeof c);
   1041 }
   1042 
   1043 /*
   1044  * Read the board's firmware revision information.
   1045  */
   1046 void
   1047 wds_inquire_setup_information(sc)
   1048 	struct wds_softc *sc;
   1049 {
   1050 	bus_space_tag_t iot = sc->sc_iot;
   1051 	bus_space_handle_t ioh = sc->sc_ioh;
   1052 	struct wds_scb *scb;
   1053 	u_char *j;
   1054 	int s;
   1055 
   1056 	sc->sc_maxsegs = 1;
   1057 
   1058 	scb = wds_get_scb(sc);
   1059 	if (scb == 0)
   1060 		panic("wds_inquire_setup_information: no scb available");
   1061 
   1062 	scb->xs = NULL;
   1063 	scb->timeout = 40;
   1064 
   1065 	memset(&scb->cmd, 0, sizeof scb->cmd);
   1066 	scb->cmd.write = 0x80;
   1067 	scb->cmd.opcode = WDSX_GETFIRMREV;
   1068 
   1069 	/* Will poll card, await result. */
   1070 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
   1071 	scb->flags |= SCB_POLLED;
   1072 
   1073 	s = splbio();
   1074 	wds_queue_scb(sc, scb);
   1075 	splx(s);
   1076 
   1077 	if (wds_ipoll(sc, scb, scb->timeout))
   1078 		goto out;
   1079 
   1080 	/* Print the version number. */
   1081 	printf("%s: version %x.%02x ", sc->sc_dev.dv_xname,
   1082 	    scb->cmd.targ, scb->cmd.scb.opcode);
   1083 	sc->sc_revision = (scb->cmd.targ << 8) | scb->cmd.scb.opcode;
   1084 	/* Print out the version string. */
   1085 	j = 2 + &(scb->cmd.targ);
   1086 	while ((*j >= 32) && (*j < 128)) {
   1087 		printf("%c", *j);
   1088 		j++;
   1089 	}
   1090 
   1091 	/*
   1092 	 * Determine if we can use scatter/gather.
   1093 	 */
   1094 	if (sc->sc_revision >= 0x800)
   1095 		sc->sc_maxsegs = WDS_NSEG;
   1096 
   1097 out:
   1098 	printf("\n");
   1099 
   1100 	/*
   1101 	 * Free up the resources used by this scb.
   1102 	 */
   1103 	wds_free_scb(sc, scb);
   1104 }
   1105 
   1106 void
   1107 wdsminphys(bp)
   1108 	struct buf *bp;
   1109 {
   1110 
   1111 	if (bp->b_bcount > WDS_MAXXFER)
   1112 		bp->b_bcount = WDS_MAXXFER;
   1113 	minphys(bp);
   1114 }
   1115 
   1116 /*
   1117  * Send a SCSI command.
   1118  */
   1119 void
   1120 wds_scsipi_request(chan, req, arg)
   1121 	struct scsipi_channel *chan;
   1122 	scsipi_adapter_req_t req;
   1123 	void *arg;
   1124 {
   1125 	struct scsipi_xfer *xs;
   1126 	struct scsipi_periph *periph;
   1127 	struct wds_softc *sc = (void *)chan->chan_adapter->adapt_dev;
   1128 	bus_dma_tag_t dmat = sc->sc_dmat;
   1129 	struct wds_scb *scb;
   1130 	struct wds_scat_gath *sg;
   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 		memset(&scb->cmd, 0, sizeof scb->cmd);
   1180 		memcpy(&scb->cmd.scb, xs->cmd,
   1181 		    xs->cmdlen < 12 ? xs->cmdlen : 12);
   1182 
   1183 		/* Set up some of the command fields. */
   1184 		scb->cmd.targ = (periph->periph_target << 5) |
   1185 		    periph->periph_lun;
   1186 
   1187 		/*
   1188 		 * NOTE: cmd.write may be OK as 0x40 (disable direction
   1189 		 * checking) on boards other than the WD-7000V-ASE. Need
   1190 		 * this for the ASE:
   1191  		 */
   1192 		scb->cmd.write = (xs->xs_control & XS_CTL_DATA_IN) ?
   1193 		    0x80 : 0x00;
   1194 
   1195 		if (xs->datalen) {
   1196 			sg = scb->scat_gath;
   1197 			seg = 0;
   1198 #ifdef TFS
   1199 			if (flags & XS_CTL_DATA_UIO) {
   1200 				error = bus_dmamap_load_uio(dmat,
   1201 				    scb->dmamap_xfer, (struct uio *)xs->data,
   1202 				    BUS_DMA_NOWAIT |
   1203 				    ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
   1204 				     BUS_DMA_WRITE));
   1205 			} else
   1206 #endif /* TFS */
   1207 			{
   1208 				error = bus_dmamap_load(dmat,
   1209 				    scb->dmamap_xfer, xs->data, xs->datalen,
   1210 				    NULL, BUS_DMA_NOWAIT |
   1211 				    ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
   1212 				     BUS_DMA_WRITE));
   1213 			}
   1214 
   1215 			switch (error) {
   1216 			case 0:
   1217 				break;
   1218 
   1219 			case ENOMEM:
   1220 			case EAGAIN:
   1221 				xs->error = XS_RESOURCE_SHORTAGE;
   1222 				goto out_bad;
   1223 
   1224 			default:
   1225 				xs->error = XS_DRIVER_STUFFUP;
   1226 				printf("%s: error %d loading DMA map\n",
   1227 				    sc->sc_dev.dv_xname, error);
   1228  out_bad:
   1229 				wds_free_scb(sc, scb);
   1230 				scsipi_done(xs);
   1231 				return;
   1232 			}
   1233 
   1234 			bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
   1235 			    scb->dmamap_xfer->dm_mapsize,
   1236 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
   1237 			    BUS_DMASYNC_PREWRITE);
   1238 
   1239 			if (sc->sc_maxsegs > 1) {
   1240 				/*
   1241 				 * Load the hardware scatter/gather map with the
   1242 				 * contents of the DMA map.
   1243 				 */
   1244 				for (seg = 0;
   1245 				     seg < scb->dmamap_xfer->dm_nsegs; seg++) {
   1246 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_addr,
   1247 					    scb->scat_gath[seg].seg_addr);
   1248 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_len,
   1249 					    scb->scat_gath[seg].seg_len);
   1250 				}
   1251 
   1252 				/*
   1253 				 * Set up for scatter/gather transfer.
   1254 				 */
   1255 				scb->cmd.opcode = WDSX_SCSISG;
   1256 				ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
   1257 				    offsetof(struct wds_scb, scat_gath),
   1258 				    scb->cmd.data);
   1259 				ltophys(scb->dmamap_self->dm_nsegs *
   1260 				    sizeof(struct wds_scat_gath), scb->cmd.len);
   1261 			} else {
   1262 				/*
   1263 				 * This board is an ASC or an ASE, and the
   1264 				 * transfer has been mapped contig for us.
   1265 				 */
   1266 				scb->cmd.opcode = WDSX_SCSICMD;
   1267 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_addr,
   1268 				    scb->cmd.data);
   1269 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_len,
   1270 				    scb->cmd.len);
   1271 			}
   1272 		} else {
   1273 			scb->cmd.opcode = WDSX_SCSICMD;
   1274 			ltophys(0, scb->cmd.data);
   1275 			ltophys(0, scb->cmd.len);
   1276 		}
   1277 
   1278 		scb->cmd.stat = 0x00;
   1279 		scb->cmd.venderr = 0x00;
   1280 		ltophys(0, scb->cmd.link);
   1281 
   1282 		/* XXX Do we really want to do this? */
   1283 		if (flags & XS_CTL_POLL) {
   1284 			/* Will poll card, await result. */
   1285 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1286 			    WDS_HCR, WDSH_DRQEN);
   1287 			scb->flags |= SCB_POLLED;
   1288 		} else {
   1289 			/*
   1290 			 * Will send command, let interrupt routine
   1291 			 * handle result.
   1292 			 */
   1293 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR,
   1294 			    WDSH_IRQEN | WDSH_DRQEN);
   1295 		}
   1296 
   1297 		s = splbio();
   1298 		wds_queue_scb(sc, scb);
   1299 		splx(s);
   1300 
   1301 		if ((flags & XS_CTL_POLL) == 0)
   1302 			return;
   1303 
   1304 		if (wds_poll(sc, xs, scb->timeout)) {
   1305 			wds_timeout(scb);
   1306 			if (wds_poll(sc, xs, scb->timeout))
   1307 				wds_timeout(scb);
   1308 		}
   1309 		return;
   1310 
   1311 	case ADAPTER_REQ_GROW_RESOURCES:
   1312 		/* XXX Not supported. */
   1313 		return;
   1314 
   1315 	case ADAPTER_REQ_SET_XFER_MODE:
   1316 		/* XXX How do we do this? */
   1317 		return;
   1318 	}
   1319 }
   1320 
   1321 /*
   1322  * Poll a particular unit, looking for a particular scb
   1323  */
   1324 int
   1325 wds_poll(sc, xs, count)
   1326 	struct wds_softc *sc;
   1327 	struct scsipi_xfer *xs;
   1328 	int count;
   1329 {
   1330 	bus_space_tag_t iot = sc->sc_iot;
   1331 	bus_space_handle_t ioh = sc->sc_ioh;
   1332 
   1333 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1334 	while (count) {
   1335 		/*
   1336 		 * If we had interrupts enabled, would we
   1337 		 * have got an interrupt?
   1338 		 */
   1339 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
   1340 			wdsintr(sc);
   1341 		if (xs->xs_status & XS_STS_DONE)
   1342 			return 0;
   1343 		delay(1000);	/* only happens in boot so ok */
   1344 		count--;
   1345 	}
   1346 	return 1;
   1347 }
   1348 
   1349 /*
   1350  * Poll a particular unit, looking for a particular scb
   1351  */
   1352 int
   1353 wds_ipoll(sc, scb, count)
   1354 	struct wds_softc *sc;
   1355 	struct wds_scb *scb;
   1356 	int count;
   1357 {
   1358 	bus_space_tag_t iot = sc->sc_iot;
   1359 	bus_space_handle_t ioh = sc->sc_ioh;
   1360 
   1361 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1362 	while (count) {
   1363 		/*
   1364 		 * If we had interrupts enabled, would we
   1365 		 * have got an interrupt?
   1366 		 */
   1367 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
   1368 			wdsintr(sc);
   1369 		if (scb->flags & SCB_DONE)
   1370 			return 0;
   1371 		delay(1000);	/* only happens in boot so ok */
   1372 		count--;
   1373 	}
   1374 	return 1;
   1375 }
   1376 
   1377 void
   1378 wds_timeout(arg)
   1379 	void *arg;
   1380 {
   1381 	struct wds_scb *scb = arg;
   1382 	struct scsipi_xfer *xs = scb->xs;
   1383 	struct scsipi_periph *periph = xs->xs_periph;
   1384 	struct wds_softc *sc =
   1385 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
   1386 	int s;
   1387 
   1388 	scsipi_printaddr(periph);
   1389 	printf("timed out");
   1390 
   1391 	s = splbio();
   1392 
   1393 #ifdef WDSDIAG
   1394 	/*
   1395 	 * If The scb's mbx is not free, then the board has gone south?
   1396 	 */
   1397 	wds_collect_mbo(sc);
   1398 	if (scb->flags & SCB_SENDING) {
   1399 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
   1400 		Debugger();
   1401 	}
   1402 #endif
   1403 
   1404 	/*
   1405 	 * If it has been through before, then
   1406 	 * a previous abort has failed, don't
   1407 	 * try abort again
   1408 	 */
   1409 	if (scb->flags & SCB_ABORT) {
   1410 		/* abort timed out */
   1411 		printf(" AGAIN\n");
   1412 		/* XXX Must reset! */
   1413 	} else {
   1414 		/* abort the operation that has timed out */
   1415 		printf("\n");
   1416 		scb->xs->error = XS_TIMEOUT;
   1417 		scb->timeout = WDS_ABORT_TIMEOUT;
   1418 		scb->flags |= SCB_ABORT;
   1419 		wds_queue_scb(sc, scb);
   1420 	}
   1421 
   1422 	splx(s);
   1423 }
   1424