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