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