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