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