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