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