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