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