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