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