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