Home | History | Annotate | Line # | Download | only in eisa
ahb.c revision 1.32.2.2
      1  1.32.2.2  jdolecek /*	$NetBSD: ahb.c,v 1.32.2.2 2002/06/23 17:46:01 jdolecek Exp $	*/
      2       1.1   mycroft 
      3       1.9   thorpej /*-
      4      1.17   thorpej  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5       1.9   thorpej  * All rights reserved.
      6       1.9   thorpej  *
      7       1.9   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.23   mycroft  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
      9      1.23   mycroft  * Simulation Facility, NASA Ames Research Center.
     10       1.9   thorpej  *
     11       1.9   thorpej  * Redistribution and use in source and binary forms, with or without
     12       1.9   thorpej  * modification, are permitted provided that the following conditions
     13       1.9   thorpej  * are met:
     14       1.9   thorpej  * 1. Redistributions of source code must retain the above copyright
     15       1.9   thorpej  *    notice, this list of conditions and the following disclaimer.
     16       1.9   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.9   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18       1.9   thorpej  *    documentation and/or other materials provided with the distribution.
     19       1.9   thorpej  * 3. All advertising materials mentioning features or use of this software
     20       1.9   thorpej  *    must display the following acknowledgement:
     21       1.9   thorpej  *	This product includes software developed by the NetBSD
     22       1.9   thorpej  *	Foundation, Inc. and its contributors.
     23       1.9   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.9   thorpej  *    contributors may be used to endorse or promote products derived
     25       1.9   thorpej  *    from this software without specific prior written permission.
     26       1.9   thorpej  *
     27       1.9   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.9   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.9   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.9   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.9   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.9   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.9   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.9   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.9   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.9   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.9   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1   mycroft  */
     39       1.1   mycroft 
     40       1.1   mycroft /*
     41       1.1   mycroft  * Originally written by Julian Elischer (julian (at) tfs.com)
     42       1.1   mycroft  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     43       1.1   mycroft  *
     44       1.1   mycroft  * TRW Financial Systems, in accordance with their agreement with Carnegie
     45       1.1   mycroft  * Mellon University, makes this software available to CMU to distribute
     46       1.1   mycroft  * or use in any manner that they see fit as long as this message is kept with
     47       1.1   mycroft  * the software. For this reason TFS also grants any other persons or
     48       1.1   mycroft  * organisations permission to use or modify this software.
     49       1.1   mycroft  *
     50       1.1   mycroft  * TFS supplies this software to be publicly redistributed
     51       1.1   mycroft  * on the understanding that TFS is not responsible for the correct
     52       1.1   mycroft  * functioning of this software in any circumstances.
     53       1.1   mycroft  */
     54       1.1   mycroft 
     55  1.32.2.1   thorpej #include <sys/cdefs.h>
     56  1.32.2.2  jdolecek __KERNEL_RCSID(0, "$NetBSD: ahb.c,v 1.32.2.2 2002/06/23 17:46:01 jdolecek Exp $");
     57  1.32.2.1   thorpej 
     58  1.32.2.1   thorpej #include "opt_ddb.h"
     59  1.32.2.1   thorpej 
     60  1.32.2.1   thorpej #undef	AHBDEBUG
     61  1.32.2.1   thorpej #ifdef DDB
     62  1.32.2.1   thorpej #define	integrate
     63  1.32.2.1   thorpej #else
     64  1.32.2.1   thorpej #define	integrate	static inline
     65  1.32.2.1   thorpej #endif
     66  1.32.2.1   thorpej 
     67       1.1   mycroft #include <sys/param.h>
     68       1.1   mycroft #include <sys/systm.h>
     69       1.1   mycroft #include <sys/kernel.h>
     70       1.1   mycroft #include <sys/errno.h>
     71       1.1   mycroft #include <sys/ioctl.h>
     72       1.1   mycroft #include <sys/device.h>
     73       1.1   mycroft #include <sys/malloc.h>
     74       1.1   mycroft #include <sys/buf.h>
     75       1.1   mycroft #include <sys/proc.h>
     76       1.1   mycroft #include <sys/user.h>
     77       1.1   mycroft 
     78      1.31   thorpej #include <uvm/uvm_extern.h>
     79      1.31   thorpej 
     80       1.1   mycroft #include <machine/bus.h>
     81       1.1   mycroft #include <machine/intr.h>
     82       1.1   mycroft 
     83      1.10    bouyer #include <dev/scsipi/scsi_all.h>
     84      1.10    bouyer #include <dev/scsipi/scsipi_all.h>
     85      1.10    bouyer #include <dev/scsipi/scsiconf.h>
     86       1.1   mycroft 
     87       1.1   mycroft #include <dev/eisa/eisareg.h>
     88       1.1   mycroft #include <dev/eisa/eisavar.h>
     89       1.1   mycroft #include <dev/eisa/eisadevs.h>
     90       1.1   mycroft #include <dev/eisa/ahbreg.h>
     91       1.1   mycroft 
     92       1.1   mycroft #ifndef DDB
     93       1.1   mycroft #define Debugger() panic("should call debugger here (aha1742.c)")
     94       1.1   mycroft #endif /* ! DDB */
     95       1.1   mycroft 
     96       1.1   mycroft #define AHB_ECB_MAX	32	/* store up to 32 ECBs at one time */
     97       1.1   mycroft #define	ECB_HASH_SIZE	32	/* hash table size for phystokv */
     98       1.1   mycroft #define	ECB_HASH_SHIFT	9
     99       1.1   mycroft #define ECB_HASH(x)	((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
    100       1.1   mycroft 
    101       1.9   thorpej #define AHB_MAXXFER	((AHB_NSEG - 1) << PGSHIFT)
    102       1.1   mycroft 
    103       1.1   mycroft struct ahb_softc {
    104       1.1   mycroft 	struct device sc_dev;
    105       1.8   mycroft 
    106       1.6   thorpej 	bus_space_tag_t sc_iot;
    107       1.6   thorpej 	bus_space_handle_t sc_ioh;
    108       1.9   thorpej 	bus_dma_tag_t sc_dmat;
    109       1.1   mycroft 	void *sc_ih;
    110       1.1   mycroft 
    111      1.18   thorpej 	bus_dmamap_t sc_dmamap_ecb;	/* maps the ecbs */
    112      1.18   thorpej 	struct ahb_ecb *sc_ecbs;	/* all our ecbs */
    113      1.18   thorpej 
    114       1.1   mycroft 	struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
    115       1.1   mycroft 	TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
    116       1.1   mycroft 	struct ahb_ecb *sc_immed_ecb;	/* an outstanding immediete command */
    117       1.1   mycroft 	int sc_numecbs;
    118      1.32    bouyer 
    119      1.25   thorpej 	struct scsipi_adapter sc_adapter;
    120      1.32    bouyer 	struct scsipi_channel sc_channel;
    121       1.1   mycroft };
    122       1.1   mycroft 
    123      1.18   thorpej /*
    124      1.18   thorpej  * Offset of an ECB from the beginning of the ECB DMA mapping.
    125      1.18   thorpej  */
    126      1.18   thorpej #define	AHB_ECB_OFF(e)	(((u_long)(e)) - ((u_long)&sc->sc_ecbs[0]))
    127      1.18   thorpej 
    128       1.8   mycroft struct ahb_probe_data {
    129       1.8   mycroft 	int sc_irq;
    130       1.8   mycroft 	int sc_scsi_dev;
    131       1.8   mycroft };
    132       1.8   mycroft 
    133       1.8   mycroft void	ahb_send_mbox __P((struct ahb_softc *, int, struct ahb_ecb *));
    134      1.30   thorpej void	ahb_send_immed __P((struct ahb_softc *, u_int32_t, struct ahb_ecb *));
    135       1.8   mycroft int	ahbintr __P((void *));
    136       1.8   mycroft void	ahb_free_ecb __P((struct ahb_softc *, struct ahb_ecb *));
    137      1.32    bouyer struct	ahb_ecb *ahb_get_ecb __P((struct ahb_softc *));
    138       1.8   mycroft struct	ahb_ecb *ahb_ecb_phys_kv __P((struct ahb_softc *, physaddr));
    139       1.8   mycroft void	ahb_done __P((struct ahb_softc *, struct ahb_ecb *));
    140       1.8   mycroft int	ahb_find __P((bus_space_tag_t, bus_space_handle_t, struct ahb_probe_data *));
    141      1.18   thorpej int	ahb_init __P((struct ahb_softc *));
    142       1.8   mycroft void	ahbminphys __P((struct buf *));
    143      1.32    bouyer void	ahb_scsipi_request __P((struct scsipi_channel *,
    144      1.32    bouyer 	    scsipi_adapter_req_t, void *));
    145      1.10    bouyer int	ahb_poll __P((struct ahb_softc *, struct scsipi_xfer *, int));
    146       1.8   mycroft void	ahb_timeout __P((void *));
    147      1.18   thorpej int	ahb_create_ecbs __P((struct ahb_softc *, struct ahb_ecb *, int));
    148       1.3   thorpej 
    149       1.3   thorpej integrate void ahb_reset_ecb __P((struct ahb_softc *, struct ahb_ecb *));
    150      1.11   thorpej integrate int ahb_init_ecb __P((struct ahb_softc *, struct ahb_ecb *));
    151       1.1   mycroft 
    152       1.9   thorpej int	ahbmatch __P((struct device *, struct cfdata *, void *));
    153       1.1   mycroft void	ahbattach __P((struct device *, struct device *, void *));
    154       1.1   mycroft 
    155       1.1   mycroft struct cfattach ahb_ca = {
    156       1.1   mycroft 	sizeof(struct ahb_softc), ahbmatch, ahbattach
    157       1.1   mycroft };
    158       1.1   mycroft 
    159       1.1   mycroft #define	AHB_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    160       1.9   thorpej 
    161       1.1   mycroft /*
    162       1.1   mycroft  * Check the slots looking for a board we recognise
    163       1.1   mycroft  * If we find one, note it's address (slot) and call
    164       1.1   mycroft  * the actual probe routine to check it out.
    165       1.1   mycroft  */
    166       1.1   mycroft int
    167       1.1   mycroft ahbmatch(parent, match, aux)
    168       1.1   mycroft 	struct device *parent;
    169       1.9   thorpej 	struct cfdata *match;
    170       1.9   thorpej 	void *aux;
    171       1.1   mycroft {
    172       1.1   mycroft 	struct eisa_attach_args *ea = aux;
    173       1.6   thorpej 	bus_space_tag_t iot = ea->ea_iot;
    174       1.6   thorpej 	bus_space_handle_t ioh;
    175       1.1   mycroft 	int rv;
    176       1.1   mycroft 
    177       1.1   mycroft 	/* must match one of our known ID strings */
    178       1.1   mycroft 	if (strcmp(ea->ea_idstring, "ADP0000") &&
    179       1.1   mycroft 	    strcmp(ea->ea_idstring, "ADP0001") &&
    180       1.1   mycroft 	    strcmp(ea->ea_idstring, "ADP0002") &&
    181       1.1   mycroft 	    strcmp(ea->ea_idstring, "ADP0400"))
    182       1.1   mycroft 		return (0);
    183       1.1   mycroft 
    184      1.22   mycroft 	if (bus_space_map(iot,
    185      1.22   mycroft 	    EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET, AHB_EISA_IOSIZE,
    186      1.22   mycroft 	    0, &ioh))
    187       1.1   mycroft 		return (0);
    188       1.1   mycroft 
    189       1.6   thorpej 	rv = !ahb_find(iot, ioh, NULL);
    190       1.1   mycroft 
    191      1.22   mycroft 	bus_space_unmap(iot, ioh, AHB_EISA_IOSIZE);
    192       1.1   mycroft 
    193       1.1   mycroft 	return (rv);
    194       1.1   mycroft }
    195       1.1   mycroft 
    196       1.1   mycroft /*
    197       1.1   mycroft  * Attach all the sub-devices we can find
    198       1.1   mycroft  */
    199       1.1   mycroft void
    200       1.1   mycroft ahbattach(parent, self, aux)
    201       1.1   mycroft 	struct device *parent, *self;
    202       1.1   mycroft 	void *aux;
    203       1.1   mycroft {
    204       1.1   mycroft 	struct eisa_attach_args *ea = aux;
    205       1.1   mycroft 	struct ahb_softc *sc = (void *)self;
    206       1.6   thorpej 	bus_space_tag_t iot = ea->ea_iot;
    207       1.6   thorpej 	bus_space_handle_t ioh;
    208       1.1   mycroft 	eisa_chipset_tag_t ec = ea->ea_ec;
    209       1.1   mycroft 	eisa_intr_handle_t ih;
    210       1.1   mycroft 	const char *model, *intrstr;
    211       1.8   mycroft 	struct ahb_probe_data apd;
    212      1.32    bouyer 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    213      1.32    bouyer 	struct scsipi_channel *chan = &sc->sc_channel;
    214       1.1   mycroft 
    215       1.1   mycroft 	if (!strcmp(ea->ea_idstring, "ADP0000"))
    216       1.1   mycroft 		model = EISA_PRODUCT_ADP0000;
    217       1.1   mycroft 	else if (!strcmp(ea->ea_idstring, "ADP0001"))
    218       1.1   mycroft 		model = EISA_PRODUCT_ADP0001;
    219       1.1   mycroft 	else if (!strcmp(ea->ea_idstring, "ADP0002"))
    220       1.1   mycroft 		model = EISA_PRODUCT_ADP0002;
    221       1.1   mycroft 	else if (!strcmp(ea->ea_idstring, "ADP0400"))
    222       1.1   mycroft 		model = EISA_PRODUCT_ADP0400;
    223       1.1   mycroft 	else
    224       1.1   mycroft 		model = "unknown model!";
    225       1.5  christos 	printf(": %s\n", model);
    226       1.1   mycroft 
    227      1.22   mycroft 	if (bus_space_map(iot,
    228      1.22   mycroft 	    EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET, AHB_EISA_IOSIZE,
    229      1.22   mycroft 	    0, &ioh))
    230       1.1   mycroft 		panic("ahbattach: could not map I/O addresses");
    231       1.1   mycroft 
    232       1.6   thorpej 	sc->sc_iot = iot;
    233       1.1   mycroft 	sc->sc_ioh = ioh;
    234       1.9   thorpej 	sc->sc_dmat = ea->ea_dmat;
    235       1.8   mycroft 	if (ahb_find(iot, ioh, &apd))
    236       1.1   mycroft 		panic("ahbattach: ahb_find failed!");
    237       1.1   mycroft 
    238       1.1   mycroft 	TAILQ_INIT(&sc->sc_free_ecb);
    239      1.32    bouyer 
    240      1.32    bouyer 	/*
    241      1.32    bouyer 	 * Fill in the scsipi_adapter.
    242      1.32    bouyer 	 */
    243      1.32    bouyer 	memset(adapt, 0, sizeof(*adapt));
    244      1.32    bouyer 	adapt->adapt_dev = &sc->sc_dev;
    245      1.32    bouyer 	adapt->adapt_nchannels = 1;
    246      1.32    bouyer 	/* adapt_openings initialized below */
    247      1.32    bouyer 	adapt->adapt_max_periph = 4;		/* XXX arbitrary? */
    248      1.32    bouyer 	adapt->adapt_request = ahb_scsipi_request;
    249      1.32    bouyer 	adapt->adapt_minphys = ahbminphys;
    250      1.32    bouyer 
    251      1.32    bouyer 	/*
    252      1.32    bouyer 	 * Fill in the scsipi_channel.
    253      1.32    bouyer 	 */
    254      1.32    bouyer 	memset(chan, 0, sizeof(*chan));
    255      1.32    bouyer 	chan->chan_adapter = adapt;
    256      1.32    bouyer 	chan->chan_bustype = &scsi_bustype;
    257      1.32    bouyer 	chan->chan_channel = 0;
    258      1.32    bouyer 	chan->chan_ntargets = 8;
    259      1.32    bouyer 	chan->chan_nluns = 8;
    260      1.32    bouyer 	chan->chan_id = apd.sc_scsi_dev;
    261       1.1   mycroft 
    262      1.18   thorpej 	if (ahb_init(sc) != 0) {
    263      1.18   thorpej 		/* Error during initialization! */
    264      1.18   thorpej 		return;
    265      1.18   thorpej 	}
    266      1.18   thorpej 
    267       1.8   mycroft 	if (eisa_intr_map(ec, apd.sc_irq, &ih)) {
    268       1.5  christos 		printf("%s: couldn't map interrupt (%d)\n",
    269       1.8   mycroft 		    sc->sc_dev.dv_xname, apd.sc_irq);
    270       1.1   mycroft 		return;
    271       1.1   mycroft 	}
    272       1.1   mycroft 	intrstr = eisa_intr_string(ec, ih);
    273       1.1   mycroft 	sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
    274       1.1   mycroft 	    ahbintr, sc);
    275       1.1   mycroft 	if (sc->sc_ih == NULL) {
    276       1.5  christos 		printf("%s: couldn't establish interrupt",
    277       1.1   mycroft 		    sc->sc_dev.dv_xname);
    278       1.1   mycroft 		if (intrstr != NULL)
    279       1.5  christos 			printf(" at %s", intrstr);
    280       1.5  christos 		printf("\n");
    281       1.1   mycroft 		return;
    282       1.1   mycroft 	}
    283       1.1   mycroft 	if (intrstr != NULL)
    284       1.5  christos 		printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
    285       1.1   mycroft 		    intrstr);
    286       1.1   mycroft 
    287       1.1   mycroft 	/*
    288       1.1   mycroft 	 * ask the adapter what subunits are present
    289       1.1   mycroft 	 */
    290      1.32    bouyer 	config_found(self, &sc->sc_channel, scsiprint);
    291       1.1   mycroft }
    292       1.1   mycroft 
    293       1.1   mycroft /*
    294       1.1   mycroft  * Function to send a command out through a mailbox
    295       1.1   mycroft  */
    296       1.1   mycroft void
    297       1.1   mycroft ahb_send_mbox(sc, opcode, ecb)
    298       1.1   mycroft 	struct ahb_softc *sc;
    299       1.1   mycroft 	int opcode;
    300       1.1   mycroft 	struct ahb_ecb *ecb;
    301       1.1   mycroft {
    302       1.6   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    303       1.6   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    304       1.1   mycroft 	int wait = 300;	/* 1ms should be enough */
    305       1.1   mycroft 
    306       1.1   mycroft 	while (--wait) {
    307       1.6   thorpej 		if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
    308       1.1   mycroft 		    == (G2STAT_MBOX_EMPTY))
    309       1.1   mycroft 			break;
    310       1.1   mycroft 		delay(10);
    311       1.1   mycroft 	}
    312       1.1   mycroft 	if (!wait) {
    313       1.5  christos 		printf("%s: board not responding\n", sc->sc_dev.dv_xname);
    314       1.1   mycroft 		Debugger();
    315       1.1   mycroft 	}
    316       1.1   mycroft 
    317       1.9   thorpej 	/*
    318       1.9   thorpej 	 * don't know if this will work.
    319       1.9   thorpej 	 * XXX WHAT DOES THIS COMMENT MEAN?!  --thorpej
    320       1.9   thorpej 	 */
    321       1.9   thorpej 	bus_space_write_4(iot, ioh, MBOXOUT0,
    322      1.18   thorpej 	    sc->sc_dmamap_ecb->dm_segs[0].ds_addr + AHB_ECB_OFF(ecb));
    323      1.10    bouyer 	bus_space_write_1(iot, ioh, ATTN, opcode |
    324      1.32    bouyer 		ecb->xs->xs_periph->periph_target);
    325       1.1   mycroft 
    326      1.28   thorpej 	if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
    327      1.29   thorpej 		callout_reset(&ecb->xs->xs_callout,
    328  1.32.2.2  jdolecek 		    mstohz(ecb->timeout), ahb_timeout, ecb);
    329       1.1   mycroft }
    330       1.1   mycroft 
    331       1.1   mycroft /*
    332       1.1   mycroft  * Function to  send an immediate type command to the adapter
    333       1.1   mycroft  */
    334       1.1   mycroft void
    335       1.1   mycroft ahb_send_immed(sc, cmd, ecb)
    336       1.1   mycroft 	struct ahb_softc *sc;
    337      1.30   thorpej 	u_int32_t cmd;
    338       1.1   mycroft 	struct ahb_ecb *ecb;
    339       1.1   mycroft {
    340       1.6   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    341       1.6   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    342       1.1   mycroft 	int wait = 100;	/* 1 ms enough? */
    343       1.1   mycroft 
    344       1.1   mycroft 	while (--wait) {
    345       1.6   thorpej 		if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
    346       1.1   mycroft 		    == (G2STAT_MBOX_EMPTY))
    347       1.1   mycroft 			break;
    348       1.1   mycroft 		delay(10);
    349       1.1   mycroft 	}
    350       1.1   mycroft 	if (!wait) {
    351       1.5  christos 		printf("%s: board not responding\n", sc->sc_dev.dv_xname);
    352       1.1   mycroft 		Debugger();
    353       1.1   mycroft 	}
    354       1.1   mycroft 
    355       1.6   thorpej 	bus_space_write_4(iot, ioh, MBOXOUT0, cmd);	/* don't know this will work */
    356       1.6   thorpej 	bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
    357      1.10    bouyer 	bus_space_write_1(iot, ioh, ATTN, OP_IMMED |
    358      1.32    bouyer 		ecb->xs->xs_periph->periph_target);
    359       1.1   mycroft 
    360      1.28   thorpej 	if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
    361      1.29   thorpej 		callout_reset(&ecb->xs->xs_callout,
    362  1.32.2.2  jdolecek 		    mstohz(ecb->timeout), ahb_timeout, ecb);
    363       1.1   mycroft }
    364       1.1   mycroft 
    365       1.1   mycroft /*
    366       1.1   mycroft  * Catch an interrupt from the adaptor
    367       1.1   mycroft  */
    368       1.1   mycroft int
    369       1.1   mycroft ahbintr(arg)
    370       1.1   mycroft 	void *arg;
    371       1.1   mycroft {
    372       1.1   mycroft 	struct ahb_softc *sc = arg;
    373       1.6   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    374       1.6   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    375       1.1   mycroft 	struct ahb_ecb *ecb;
    376       1.1   mycroft 	u_char ahbstat;
    377      1.30   thorpej 	u_int32_t mboxval;
    378       1.1   mycroft 
    379       1.1   mycroft #ifdef	AHBDEBUG
    380       1.5  christos 	printf("%s: ahbintr ", sc->sc_dev.dv_xname);
    381       1.1   mycroft #endif /* AHBDEBUG */
    382       1.1   mycroft 
    383       1.6   thorpej 	if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
    384       1.1   mycroft 		return 0;
    385       1.1   mycroft 
    386       1.1   mycroft 	for (;;) {
    387       1.1   mycroft 		/*
    388       1.1   mycroft 		 * First get all the information and then
    389       1.1   mycroft 		 * acknowlege the interrupt
    390       1.1   mycroft 		 */
    391       1.6   thorpej 		ahbstat = bus_space_read_1(iot, ioh, G2INTST);
    392       1.6   thorpej 		mboxval = bus_space_read_4(iot, ioh, MBOXIN0);
    393       1.6   thorpej 		bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
    394       1.1   mycroft 
    395       1.1   mycroft #ifdef	AHBDEBUG
    396       1.5  christos 		printf("status = 0x%x ", ahbstat);
    397       1.1   mycroft #endif /* AHBDEBUG */
    398       1.1   mycroft 
    399       1.1   mycroft 		/*
    400       1.1   mycroft 		 * Process the completed operation
    401       1.1   mycroft 		 */
    402       1.1   mycroft 		switch (ahbstat & G2INTST_INT_STAT) {
    403       1.1   mycroft 		case AHB_ECB_OK:
    404       1.1   mycroft 		case AHB_ECB_RECOVERED:
    405       1.1   mycroft 		case AHB_ECB_ERR:
    406       1.1   mycroft 			ecb = ahb_ecb_phys_kv(sc, mboxval);
    407       1.1   mycroft 			if (!ecb) {
    408       1.5  christos 				printf("%s: BAD ECB RETURNED!\n",
    409       1.1   mycroft 				    sc->sc_dev.dv_xname);
    410       1.1   mycroft 				goto next;	/* whatever it was, it'll timeout */
    411       1.1   mycroft 			}
    412       1.1   mycroft 			break;
    413       1.1   mycroft 
    414       1.1   mycroft 		case AHB_IMMED_ERR:
    415       1.1   mycroft 			ecb = sc->sc_immed_ecb;
    416       1.1   mycroft 			sc->sc_immed_ecb = 0;
    417       1.1   mycroft 			ecb->flags |= ECB_IMMED_FAIL;
    418       1.1   mycroft 			break;
    419       1.1   mycroft 
    420       1.1   mycroft 		case AHB_IMMED_OK:
    421       1.1   mycroft 			ecb = sc->sc_immed_ecb;
    422       1.1   mycroft 			sc->sc_immed_ecb = 0;
    423       1.1   mycroft 			break;
    424       1.1   mycroft 
    425       1.1   mycroft 		default:
    426       1.5  christos 			printf("%s: unexpected interrupt %x\n",
    427       1.1   mycroft 			    sc->sc_dev.dv_xname, ahbstat);
    428       1.1   mycroft 			goto next;
    429       1.1   mycroft 		}
    430       1.1   mycroft 
    431      1.29   thorpej 		callout_stop(&ecb->xs->xs_callout);
    432       1.1   mycroft 		ahb_done(sc, ecb);
    433       1.1   mycroft 
    434       1.1   mycroft 	next:
    435       1.6   thorpej 		if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
    436       1.1   mycroft 			return 1;
    437       1.1   mycroft 	}
    438       1.1   mycroft }
    439       1.1   mycroft 
    440       1.1   mycroft integrate void
    441       1.1   mycroft ahb_reset_ecb(sc, ecb)
    442       1.1   mycroft 	struct ahb_softc *sc;
    443       1.1   mycroft 	struct ahb_ecb *ecb;
    444       1.1   mycroft {
    445       1.1   mycroft 
    446       1.1   mycroft 	ecb->flags = 0;
    447       1.1   mycroft }
    448       1.1   mycroft 
    449       1.1   mycroft /*
    450       1.1   mycroft  * A ecb (and hence a mbx-out is put onto the
    451       1.1   mycroft  * free list.
    452       1.1   mycroft  */
    453       1.1   mycroft void
    454       1.1   mycroft ahb_free_ecb(sc, ecb)
    455       1.1   mycroft 	struct ahb_softc *sc;
    456       1.1   mycroft 	struct ahb_ecb *ecb;
    457       1.1   mycroft {
    458       1.1   mycroft 	int s;
    459       1.1   mycroft 
    460       1.1   mycroft 	s = splbio();
    461       1.1   mycroft 	ahb_reset_ecb(sc, ecb);
    462       1.1   mycroft 	TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
    463       1.1   mycroft 	splx(s);
    464       1.1   mycroft }
    465       1.1   mycroft 
    466       1.9   thorpej /*
    467       1.9   thorpej  * Create a set of ecbs and add them to the free list.
    468       1.9   thorpej  */
    469      1.11   thorpej integrate int
    470       1.1   mycroft ahb_init_ecb(sc, ecb)
    471       1.1   mycroft 	struct ahb_softc *sc;
    472       1.1   mycroft 	struct ahb_ecb *ecb;
    473       1.1   mycroft {
    474       1.9   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    475      1.11   thorpej 	int hashnum, error;
    476       1.1   mycroft 
    477       1.9   thorpej 	/*
    478      1.18   thorpej 	 * Create the DMA map for this ECB.
    479       1.9   thorpej 	 */
    480      1.11   thorpej 	error = bus_dmamap_create(dmat, AHB_MAXXFER, AHB_NSEG, AHB_MAXXFER,
    481      1.11   thorpej 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &ecb->dmamap_xfer);
    482      1.11   thorpej 	if (error) {
    483      1.11   thorpej 		printf("%s: can't create ecb dmamap_xfer\n",
    484      1.11   thorpej 		    sc->sc_dev.dv_xname);
    485      1.11   thorpej 		return (error);
    486      1.11   thorpej 	}
    487       1.9   thorpej 
    488       1.9   thorpej 	/*
    489       1.1   mycroft 	 * put in the phystokv hash table
    490       1.1   mycroft 	 * Never gets taken out.
    491       1.1   mycroft 	 */
    492      1.18   thorpej 	ecb->hashkey = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
    493      1.18   thorpej 	    AHB_ECB_OFF(ecb);
    494       1.1   mycroft 	hashnum = ECB_HASH(ecb->hashkey);
    495       1.1   mycroft 	ecb->nexthash = sc->sc_ecbhash[hashnum];
    496       1.1   mycroft 	sc->sc_ecbhash[hashnum] = ecb;
    497       1.1   mycroft 	ahb_reset_ecb(sc, ecb);
    498      1.11   thorpej 	return (0);
    499       1.1   mycroft }
    500       1.1   mycroft 
    501       1.9   thorpej int
    502      1.18   thorpej ahb_create_ecbs(sc, ecbstore, count)
    503       1.9   thorpej 	struct ahb_softc *sc;
    504      1.18   thorpej 	struct ahb_ecb *ecbstore;
    505      1.18   thorpej 	int count;
    506       1.9   thorpej {
    507       1.9   thorpej 	struct ahb_ecb *ecb;
    508      1.18   thorpej 	int i, error;
    509       1.9   thorpej 
    510      1.18   thorpej 	bzero(ecbstore, sizeof(struct ahb_ecb) * count);
    511      1.18   thorpej 	for (i = 0; i < count; i++) {
    512      1.18   thorpej 		ecb = &ecbstore[i];
    513      1.18   thorpej 		if ((error = ahb_init_ecb(sc, ecb)) != 0) {
    514      1.18   thorpej 			printf("%s: unable to initialize ecb, error = %d\n",
    515      1.18   thorpej 			    sc->sc_dev.dv_xname, error);
    516      1.18   thorpej 			goto out;
    517      1.11   thorpej 		}
    518       1.9   thorpej 		TAILQ_INSERT_TAIL(&sc->sc_free_ecb, ecb, chain);
    519       1.9   thorpej 	}
    520      1.18   thorpej  out:
    521      1.18   thorpej 	return (i);
    522       1.9   thorpej }
    523       1.9   thorpej 
    524       1.1   mycroft /*
    525       1.1   mycroft  * Get a free ecb
    526       1.1   mycroft  *
    527       1.1   mycroft  * If there are none, see if we can allocate a new one. If so, put it in the
    528       1.1   mycroft  * hash table too otherwise either return an error or sleep.
    529       1.1   mycroft  */
    530       1.1   mycroft struct ahb_ecb *
    531      1.32    bouyer ahb_get_ecb(sc)
    532       1.1   mycroft 	struct ahb_softc *sc;
    533       1.1   mycroft {
    534       1.1   mycroft 	struct ahb_ecb *ecb;
    535       1.1   mycroft 	int s;
    536       1.1   mycroft 
    537       1.1   mycroft 	s = splbio();
    538      1.32    bouyer 	ecb = TAILQ_FIRST(&sc->sc_free_ecb);
    539      1.32    bouyer 	if (ecb != NULL) {
    540      1.32    bouyer 		TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
    541      1.32    bouyer 		ecb->flags |= ECB_ALLOC;
    542       1.1   mycroft 	}
    543       1.1   mycroft 	splx(s);
    544      1.32    bouyer 	return (ecb);
    545       1.1   mycroft }
    546       1.1   mycroft 
    547       1.1   mycroft /*
    548       1.1   mycroft  * given a physical address, find the ecb that it corresponds to.
    549       1.1   mycroft  */
    550       1.1   mycroft struct ahb_ecb *
    551       1.1   mycroft ahb_ecb_phys_kv(sc, ecb_phys)
    552       1.1   mycroft 	struct ahb_softc *sc;
    553       1.1   mycroft 	physaddr ecb_phys;
    554       1.1   mycroft {
    555       1.1   mycroft 	int hashnum = ECB_HASH(ecb_phys);
    556       1.1   mycroft 	struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
    557       1.1   mycroft 
    558       1.1   mycroft 	while (ecb) {
    559       1.1   mycroft 		if (ecb->hashkey == ecb_phys)
    560       1.1   mycroft 			break;
    561       1.1   mycroft 		ecb = ecb->nexthash;
    562       1.1   mycroft 	}
    563       1.1   mycroft 	return ecb;
    564       1.1   mycroft }
    565       1.1   mycroft 
    566       1.1   mycroft /*
    567       1.1   mycroft  * We have a ecb which has been processed by the adaptor, now we look to see
    568       1.1   mycroft  * how the operation went.
    569       1.1   mycroft  */
    570       1.1   mycroft void
    571       1.1   mycroft ahb_done(sc, ecb)
    572       1.1   mycroft 	struct ahb_softc *sc;
    573       1.1   mycroft 	struct ahb_ecb *ecb;
    574       1.1   mycroft {
    575       1.9   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    576      1.10    bouyer 	struct scsipi_sense_data *s1, *s2;
    577      1.10    bouyer 	struct scsipi_xfer *xs = ecb->xs;
    578       1.1   mycroft 
    579      1.32    bouyer 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("ahb_done\n"));
    580       1.9   thorpej 
    581      1.18   thorpej 	bus_dmamap_sync(dmat, sc->sc_dmamap_ecb,
    582      1.18   thorpej 	    AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
    583      1.18   thorpej 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    584      1.18   thorpej 
    585       1.9   thorpej 	/*
    586       1.9   thorpej 	 * If we were a data transfer, unload the map that described
    587       1.9   thorpej 	 * the data buffer.
    588       1.9   thorpej 	 */
    589       1.9   thorpej 	if (xs->datalen) {
    590      1.17   thorpej 		bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
    591      1.17   thorpej 		    ecb->dmamap_xfer->dm_mapsize,
    592      1.28   thorpej 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    593       1.9   thorpej 		    BUS_DMASYNC_POSTWRITE);
    594       1.9   thorpej 		bus_dmamap_unload(dmat, ecb->dmamap_xfer);
    595       1.9   thorpej 	}
    596       1.9   thorpej 
    597       1.1   mycroft 	/*
    598       1.1   mycroft 	 * Otherwise, put the results of the operation
    599       1.1   mycroft 	 * into the xfer and call whoever started it
    600       1.1   mycroft 	 */
    601       1.1   mycroft 	if ((ecb->flags & ECB_ALLOC) == 0) {
    602       1.5  christos 		printf("%s: exiting ecb not allocated!\n", sc->sc_dev.dv_xname);
    603       1.1   mycroft 		Debugger();
    604       1.1   mycroft 	}
    605       1.1   mycroft 	if (ecb->flags & ECB_IMMED) {
    606       1.1   mycroft 		if (ecb->flags & ECB_IMMED_FAIL)
    607       1.1   mycroft 			xs->error = XS_DRIVER_STUFFUP;
    608       1.1   mycroft 		goto done;
    609       1.1   mycroft 	}
    610       1.1   mycroft 	if (xs->error == XS_NOERROR) {
    611       1.1   mycroft 		if (ecb->ecb_status.host_stat != HS_OK) {
    612       1.1   mycroft 			switch (ecb->ecb_status.host_stat) {
    613       1.1   mycroft 			case HS_TIMED_OUT:	/* No response */
    614       1.1   mycroft 				xs->error = XS_SELTIMEOUT;
    615       1.1   mycroft 				break;
    616       1.1   mycroft 			default:	/* Other scsi protocol messes */
    617       1.5  christos 				printf("%s: host_stat %x\n",
    618       1.1   mycroft 				    sc->sc_dev.dv_xname, ecb->ecb_status.host_stat);
    619       1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    620       1.1   mycroft 			}
    621       1.1   mycroft 		} else if (ecb->ecb_status.target_stat != SCSI_OK) {
    622       1.1   mycroft 			switch (ecb->ecb_status.target_stat) {
    623       1.1   mycroft 			case SCSI_CHECK:
    624       1.1   mycroft 				s1 = &ecb->ecb_sense;
    625      1.10    bouyer 				s2 = &xs->sense.scsi_sense;
    626       1.1   mycroft 				*s2 = *s1;
    627       1.1   mycroft 				xs->error = XS_SENSE;
    628       1.1   mycroft 				break;
    629       1.1   mycroft 			case SCSI_BUSY:
    630       1.1   mycroft 				xs->error = XS_BUSY;
    631       1.1   mycroft 				break;
    632       1.1   mycroft 			default:
    633       1.5  christos 				printf("%s: target_stat %x\n",
    634       1.1   mycroft 				    sc->sc_dev.dv_xname, ecb->ecb_status.target_stat);
    635       1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    636       1.1   mycroft 			}
    637       1.1   mycroft 		} else
    638       1.1   mycroft 			xs->resid = 0;
    639       1.1   mycroft 	}
    640       1.1   mycroft done:
    641       1.1   mycroft 	ahb_free_ecb(sc, ecb);
    642      1.10    bouyer 	scsipi_done(xs);
    643       1.1   mycroft }
    644       1.1   mycroft 
    645       1.1   mycroft /*
    646       1.1   mycroft  * Start the board, ready for normal operation
    647       1.1   mycroft  */
    648       1.1   mycroft int
    649       1.6   thorpej ahb_find(iot, ioh, sc)
    650       1.6   thorpej 	bus_space_tag_t iot;
    651       1.6   thorpej 	bus_space_handle_t ioh;
    652       1.8   mycroft 	struct ahb_probe_data *sc;
    653       1.1   mycroft {
    654       1.1   mycroft 	u_char intdef;
    655       1.1   mycroft 	int i, irq, busid;
    656       1.1   mycroft 	int wait = 1000;	/* 1 sec enough? */
    657       1.1   mycroft 
    658       1.6   thorpej 	bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
    659       1.1   mycroft 
    660       1.1   mycroft #define	NO_NO 1
    661       1.1   mycroft #ifdef NO_NO
    662       1.1   mycroft 	/*
    663       1.1   mycroft 	 * reset board, If it doesn't respond, assume
    664       1.1   mycroft 	 * that it's not there.. good for the probe
    665       1.1   mycroft 	 */
    666       1.6   thorpej 	bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
    667       1.1   mycroft 	delay(1000);
    668       1.6   thorpej 	bus_space_write_1(iot, ioh, G2CNTRL, 0);
    669       1.1   mycroft 	delay(10000);
    670       1.1   mycroft 	while (--wait) {
    671       1.6   thorpej 		if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0)
    672       1.1   mycroft 			break;
    673       1.1   mycroft 		delay(1000);
    674       1.1   mycroft 	}
    675       1.1   mycroft 	if (!wait) {
    676       1.1   mycroft #ifdef	AHBDEBUG
    677       1.5  christos 		printf("ahb_find: No answer from aha1742 board\n");
    678       1.1   mycroft #endif /* AHBDEBUG */
    679       1.1   mycroft 		return ENXIO;
    680       1.1   mycroft 	}
    681       1.6   thorpej 	i = bus_space_read_1(iot, ioh, MBOXIN0);
    682       1.1   mycroft 	if (i) {
    683       1.5  christos 		printf("self test failed, val = 0x%x\n", i);
    684       1.1   mycroft 		return EIO;
    685       1.1   mycroft 	}
    686       1.1   mycroft 
    687       1.1   mycroft 	/* Set it again, just to be sure. */
    688       1.6   thorpej 	bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
    689       1.1   mycroft #endif
    690       1.1   mycroft 
    691       1.6   thorpej 	while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) {
    692       1.5  christos 		printf(".");
    693       1.6   thorpej 		bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
    694       1.1   mycroft 		delay(10000);
    695       1.1   mycroft 	}
    696       1.1   mycroft 
    697       1.6   thorpej 	intdef = bus_space_read_1(iot, ioh, INTDEF);
    698       1.1   mycroft 	switch (intdef & 0x07) {
    699       1.1   mycroft 	case INT9:
    700       1.1   mycroft 		irq = 9;
    701       1.1   mycroft 		break;
    702       1.1   mycroft 	case INT10:
    703       1.1   mycroft 		irq = 10;
    704       1.1   mycroft 		break;
    705       1.1   mycroft 	case INT11:
    706       1.1   mycroft 		irq = 11;
    707       1.1   mycroft 		break;
    708       1.1   mycroft 	case INT12:
    709       1.1   mycroft 		irq = 12;
    710       1.1   mycroft 		break;
    711       1.1   mycroft 	case INT14:
    712       1.1   mycroft 		irq = 14;
    713       1.1   mycroft 		break;
    714       1.1   mycroft 	case INT15:
    715       1.1   mycroft 		irq = 15;
    716       1.1   mycroft 		break;
    717       1.1   mycroft 	default:
    718       1.5  christos 		printf("illegal int setting %x\n", intdef);
    719       1.1   mycroft 		return EIO;
    720       1.1   mycroft 	}
    721       1.1   mycroft 
    722       1.6   thorpej 	bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN));	/* make sure we can interrupt */
    723       1.1   mycroft 
    724       1.1   mycroft 	/* who are we on the scsi bus? */
    725       1.6   thorpej 	busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID);
    726       1.1   mycroft 
    727       1.8   mycroft 	/* if we want to return data, do so now */
    728       1.8   mycroft 	if (sc) {
    729       1.1   mycroft 		sc->sc_irq = irq;
    730       1.1   mycroft 		sc->sc_scsi_dev = busid;
    731       1.1   mycroft 	}
    732       1.1   mycroft 
    733       1.1   mycroft 	/*
    734       1.1   mycroft 	 * Note that we are going and return (to probe)
    735       1.1   mycroft 	 */
    736       1.1   mycroft 	return 0;
    737       1.1   mycroft }
    738       1.1   mycroft 
    739      1.18   thorpej int
    740       1.1   mycroft ahb_init(sc)
    741       1.1   mycroft 	struct ahb_softc *sc;
    742       1.1   mycroft {
    743      1.18   thorpej 	bus_dma_segment_t seg;
    744      1.18   thorpej 	int i, error, rseg;
    745      1.18   thorpej 
    746      1.18   thorpej #define	ECBSIZE		(AHB_ECB_MAX * sizeof(struct ahb_ecb))
    747      1.18   thorpej 
    748      1.18   thorpej 	/*
    749      1.18   thorpej 	 * Allocate the ECBs.
    750      1.18   thorpej 	 */
    751      1.18   thorpej 	if ((error = bus_dmamem_alloc(sc->sc_dmat, ECBSIZE,
    752      1.31   thorpej 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    753      1.18   thorpej 		printf("%s: unable to allocate ecbs, error = %d\n",
    754      1.18   thorpej 		    sc->sc_dev.dv_xname, error);
    755      1.18   thorpej 		return (error);
    756      1.18   thorpej 	}
    757      1.18   thorpej 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    758      1.18   thorpej 	    ECBSIZE, (caddr_t *)&sc->sc_ecbs,
    759      1.18   thorpej 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    760      1.18   thorpej 		printf("%s: unable to map ecbs, error = %d\n",
    761      1.18   thorpej 		    sc->sc_dev.dv_xname, error);
    762      1.18   thorpej 		return (error);
    763      1.18   thorpej 	}
    764      1.18   thorpej 
    765      1.18   thorpej 	/*
    766      1.18   thorpej 	 * Create and load the DMA map used for the ecbs.
    767      1.18   thorpej 	 */
    768      1.18   thorpej 	if ((error = bus_dmamap_create(sc->sc_dmat, ECBSIZE,
    769      1.18   thorpej 	    1, ECBSIZE, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_ecb)) != 0) {
    770      1.18   thorpej 		printf("%s: unable to create ecb DMA map, error = %d\n",
    771      1.18   thorpej 		    sc->sc_dev.dv_xname, error);
    772      1.18   thorpej 		return (error);
    773      1.18   thorpej 	}
    774      1.18   thorpej 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ecb,
    775      1.18   thorpej 	    sc->sc_ecbs, ECBSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
    776      1.18   thorpej 		printf("%s: unable to load ecb DMA map, error = %d\n",
    777      1.18   thorpej 		    sc->sc_dev.dv_xname, error);
    778      1.18   thorpej 		return (error);
    779      1.18   thorpej 	}
    780       1.1   mycroft 
    781      1.18   thorpej #undef ECBSIZE
    782      1.18   thorpej 
    783      1.18   thorpej 	/*
    784      1.18   thorpej 	 * Initialize the ecbs.
    785      1.18   thorpej 	 */
    786      1.18   thorpej 	i = ahb_create_ecbs(sc, sc->sc_ecbs, AHB_ECB_MAX);
    787      1.18   thorpej 	if (i == 0) {
    788      1.18   thorpej 		printf("%s: unable to create ecbs\n",
    789      1.18   thorpej 		    sc->sc_dev.dv_xname);
    790      1.18   thorpej 		return (ENOMEM);
    791      1.18   thorpej 	} else if (i != AHB_ECB_MAX) {
    792      1.18   thorpej 		printf("%s: WARNING: only %d of %d ecbs created\n",
    793      1.18   thorpej 		    sc->sc_dev.dv_xname, i, AHB_ECB_MAX);
    794      1.18   thorpej 	}
    795      1.18   thorpej 
    796      1.32    bouyer 	sc->sc_adapter.adapt_openings = i;
    797      1.32    bouyer 
    798      1.18   thorpej 	return (0);
    799       1.1   mycroft }
    800       1.1   mycroft 
    801       1.1   mycroft void
    802       1.1   mycroft ahbminphys(bp)
    803       1.1   mycroft 	struct buf *bp;
    804       1.1   mycroft {
    805       1.1   mycroft 
    806       1.9   thorpej 	if (bp->b_bcount > AHB_MAXXFER)
    807       1.9   thorpej 		bp->b_bcount = AHB_MAXXFER;
    808       1.1   mycroft 	minphys(bp);
    809       1.1   mycroft }
    810       1.1   mycroft 
    811       1.1   mycroft /*
    812       1.1   mycroft  * start a scsi operation given the command and the data address.  Also needs
    813       1.1   mycroft  * the unit, target and lu.
    814       1.1   mycroft  */
    815      1.32    bouyer void
    816      1.32    bouyer ahb_scsipi_request(chan, req, arg)
    817      1.32    bouyer 	struct scsipi_channel *chan;
    818      1.32    bouyer 	scsipi_adapter_req_t req;
    819      1.32    bouyer 	void *arg;
    820      1.32    bouyer {
    821      1.10    bouyer 	struct scsipi_xfer *xs;
    822      1.32    bouyer 	struct scsipi_periph *periph;
    823      1.32    bouyer 	struct ahb_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    824       1.9   thorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
    825       1.1   mycroft 	struct ahb_ecb *ecb;
    826       1.9   thorpej 	int error, seg, flags, s;
    827      1.12   thorpej 
    828      1.32    bouyer 	switch (req) {
    829      1.32    bouyer 	case ADAPTER_REQ_RUN_XFER:
    830      1.32    bouyer 		xs = arg;
    831      1.32    bouyer 		periph = xs->xs_periph;
    832      1.32    bouyer 		flags = xs->xs_control;
    833      1.32    bouyer 
    834      1.32    bouyer 		SC_DEBUG(periph, SCSIPI_DB2, ("ahb_scsipi_request\n"));
    835      1.32    bouyer 
    836      1.32    bouyer 		/* Get an ECB to use. */
    837      1.32    bouyer 		ecb = ahb_get_ecb(sc);
    838      1.32    bouyer #ifdef DIAGNOSTIC
    839      1.12   thorpej 		/*
    840      1.32    bouyer 		 * This should never happen as we track the resources
    841      1.32    bouyer 		 * in the mid-layer.
    842      1.12   thorpej 		 */
    843      1.32    bouyer 		if (ecb == NULL) {
    844      1.32    bouyer 			scsipi_printaddr(periph);
    845      1.32    bouyer 			printf("unable to allocate ecb\n");
    846      1.32    bouyer 			panic("ahb_scsipi_request");
    847      1.12   thorpej 		}
    848      1.32    bouyer #endif
    849      1.32    bouyer 
    850      1.32    bouyer 		ecb->xs = xs;
    851      1.32    bouyer 		ecb->timeout = xs->timeout;
    852      1.12   thorpej 
    853      1.12   thorpej 		/*
    854      1.32    bouyer 		 * If it's a reset, we need to do an 'immediate'
    855      1.32    bouyer 		 * command, and store its ecb for later
    856      1.32    bouyer 		 * if there is already an immediate waiting,
    857      1.32    bouyer 		 * then WE must wait
    858      1.12   thorpej 		 */
    859      1.32    bouyer 		if (flags & XS_CTL_RESET) {
    860      1.32    bouyer 			ecb->flags |= ECB_IMMED;
    861      1.32    bouyer 			if (sc->sc_immed_ecb) {
    862      1.32    bouyer 				ahb_free_ecb(sc, ecb);
    863      1.32    bouyer 				xs->error = XS_BUSY;
    864      1.32    bouyer 				scsipi_done(xs);
    865      1.32    bouyer 				return;
    866      1.32    bouyer 			}
    867      1.32    bouyer 			sc->sc_immed_ecb = ecb;
    868      1.12   thorpej 
    869      1.32    bouyer 			s = splbio();
    870      1.32    bouyer 			ahb_send_immed(sc, AHB_TARG_RESET, ecb);
    871      1.12   thorpej 			splx(s);
    872      1.32    bouyer 
    873      1.32    bouyer 			if ((flags & XS_CTL_POLL) == 0)
    874      1.32    bouyer 				return;
    875      1.32    bouyer 
    876      1.32    bouyer 			/*
    877      1.32    bouyer 			 * If we can't use interrupts, poll on completion
    878      1.32    bouyer 			 */
    879      1.32    bouyer 			if (ahb_poll(sc, xs, ecb->timeout))
    880      1.32    bouyer 				ahb_timeout(ecb);
    881      1.32    bouyer 			return;
    882      1.12   thorpej 		}
    883      1.12   thorpej 
    884      1.12   thorpej 		/*
    885      1.32    bouyer 		 * Put all the arguments for the xfer in the ecb
    886      1.12   thorpej 		 */
    887      1.32    bouyer 		ecb->opcode = ECB_SCSI_OP;
    888      1.32    bouyer 		ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
    889      1.32    bouyer 		ecb->opt2 = periph->periph_lun | ECB_NRB;
    890      1.32    bouyer 		bcopy(xs->cmd, &ecb->scsi_cmd,
    891      1.32    bouyer 		    ecb->scsi_cmd_length = xs->cmdlen);
    892      1.32    bouyer 		ecb->sense_ptr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
    893      1.32    bouyer 		    AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_sense);
    894      1.32    bouyer 		ecb->req_sense_length = sizeof(ecb->ecb_sense);
    895      1.32    bouyer 		ecb->status = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
    896      1.32    bouyer 		    AHB_ECB_OFF(ecb) + offsetof(struct ahb_ecb, ecb_status);
    897      1.32    bouyer 		ecb->ecb_status.host_stat = 0x00;
    898      1.32    bouyer 		ecb->ecb_status.target_stat = 0x00;
    899      1.32    bouyer 
    900      1.32    bouyer 		if (xs->datalen) {
    901      1.32    bouyer 			/*
    902      1.32    bouyer 			 * Map the DMA transfer.
    903      1.32    bouyer 			 */
    904      1.32    bouyer #ifdef TFS
    905      1.32    bouyer 			if (flags & XS_CTL_DATA_UIO) {
    906      1.32    bouyer 				error = bus_dmamap_load_uio(sc->sc_dmat,
    907      1.32    bouyer 				    ecb->dmamap_xfer, (struct uio *)xs->data,
    908      1.32    bouyer 				    BUS_DMA_NOWAIT);
    909      1.32    bouyer 			} else
    910      1.32    bouyer #endif /* TFS */
    911      1.32    bouyer 			{
    912      1.32    bouyer 				error = bus_dmamap_load(sc->sc_dmat,
    913      1.32    bouyer 				    ecb->dmamap_xfer, xs->data, xs->datalen,
    914      1.32    bouyer 				    NULL, BUS_DMA_NOWAIT);
    915      1.32    bouyer 			}
    916      1.32    bouyer 
    917      1.32    bouyer 			switch (error) {
    918      1.32    bouyer 			case 0:
    919      1.32    bouyer 				break;
    920      1.32    bouyer 
    921      1.32    bouyer 			case ENOMEM:
    922      1.32    bouyer 			case EAGAIN:
    923      1.32    bouyer 				xs->error = XS_RESOURCE_SHORTAGE;
    924      1.32    bouyer 				goto out_bad;
    925      1.12   thorpej 
    926      1.32    bouyer 			default:
    927      1.32    bouyer 				xs->error = XS_DRIVER_STUFFUP;
    928      1.32    bouyer 				printf("%s: error %d loading DMA map\n",
    929      1.32    bouyer 				    sc->sc_dev.dv_xname, error);
    930      1.32    bouyer  out_bad:
    931      1.32    bouyer 				ahb_free_ecb(sc, ecb);
    932      1.32    bouyer 				scsipi_done(xs);
    933      1.32    bouyer 				return;
    934      1.32    bouyer 			}
    935      1.12   thorpej 
    936      1.32    bouyer 			bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
    937      1.32    bouyer 			    ecb->dmamap_xfer->dm_mapsize,
    938      1.32    bouyer 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
    939      1.32    bouyer 			    BUS_DMASYNC_PREWRITE);
    940      1.32    bouyer 
    941      1.32    bouyer 			/*
    942      1.32    bouyer 			 * Load the hardware scatter/gather map with the
    943      1.32    bouyer 			 * contents of the DMA map.
    944      1.32    bouyer 			 */
    945      1.32    bouyer 			for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) {
    946      1.32    bouyer 				ecb->ahb_dma[seg].seg_addr =
    947      1.32    bouyer 				    ecb->dmamap_xfer->dm_segs[seg].ds_addr;
    948      1.32    bouyer 				ecb->ahb_dma[seg].seg_len =
    949      1.32    bouyer 				    ecb->dmamap_xfer->dm_segs[seg].ds_len;
    950      1.32    bouyer 			}
    951       1.1   mycroft 
    952      1.32    bouyer 			ecb->data_addr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
    953      1.32    bouyer 			    AHB_ECB_OFF(ecb) +
    954      1.32    bouyer 			    offsetof(struct ahb_ecb, ahb_dma);
    955      1.32    bouyer 			ecb->data_length = ecb->dmamap_xfer->dm_nsegs *
    956      1.32    bouyer 			    sizeof(struct ahb_dma_seg);
    957      1.32    bouyer 			ecb->opt1 |= ECB_S_G;
    958      1.32    bouyer 		} else {	/* No data xfer, use non S/G values */
    959      1.32    bouyer 			ecb->data_addr = (physaddr)0;
    960      1.32    bouyer 			ecb->data_length = 0;
    961      1.32    bouyer 		}
    962      1.32    bouyer 		ecb->link_addr = (physaddr)0;
    963      1.32    bouyer 
    964      1.32    bouyer 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ecb,
    965      1.32    bouyer 		    AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
    966      1.32    bouyer 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    967       1.1   mycroft 
    968       1.1   mycroft 		s = splbio();
    969      1.32    bouyer 		ahb_send_mbox(sc, OP_START_ECB, ecb);
    970       1.1   mycroft 		splx(s);
    971       1.1   mycroft 
    972      1.28   thorpej 		if ((flags & XS_CTL_POLL) == 0)
    973      1.32    bouyer 			return;
    974       1.1   mycroft 
    975       1.1   mycroft 		/*
    976       1.1   mycroft 		 * If we can't use interrupts, poll on completion
    977       1.1   mycroft 		 */
    978      1.32    bouyer 		if (ahb_poll(sc, xs, ecb->timeout)) {
    979       1.1   mycroft 			ahb_timeout(ecb);
    980      1.32    bouyer 			if (ahb_poll(sc, xs, ecb->timeout))
    981      1.32    bouyer 				ahb_timeout(ecb);
    982       1.1   mycroft 		}
    983      1.32    bouyer 		return;
    984       1.9   thorpej 
    985      1.32    bouyer 	case ADAPTER_REQ_GROW_RESOURCES:
    986      1.32    bouyer 		/* XXX Not supported. */
    987      1.32    bouyer 		return;
    988       1.9   thorpej 
    989      1.32    bouyer 	case ADAPTER_REQ_SET_XFER_MODE:
    990      1.32    bouyer 		/* XXX How do we do this? */
    991      1.32    bouyer 		return;
    992       1.1   mycroft 	}
    993       1.1   mycroft }
    994       1.1   mycroft 
    995       1.1   mycroft /*
    996       1.1   mycroft  * Function to poll for command completion when in poll mode
    997       1.1   mycroft  */
    998       1.1   mycroft int
    999       1.1   mycroft ahb_poll(sc, xs, count)
   1000       1.1   mycroft 	struct ahb_softc *sc;
   1001      1.10    bouyer 	struct scsipi_xfer *xs;
   1002       1.1   mycroft 	int count;
   1003       1.1   mycroft {				/* in msec  */
   1004       1.6   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1005       1.6   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1006       1.1   mycroft 
   1007       1.1   mycroft 	while (count) {
   1008       1.1   mycroft 		/*
   1009       1.1   mycroft 		 * If we had interrupts enabled, would we
   1010       1.1   mycroft 		 * have got an interrupt?
   1011       1.1   mycroft 		 */
   1012       1.6   thorpej 		if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
   1013       1.1   mycroft 			ahbintr(sc);
   1014      1.28   thorpej 		if (xs->xs_status & XS_STS_DONE)
   1015       1.1   mycroft 			return 0;
   1016       1.1   mycroft 		delay(1000);
   1017       1.1   mycroft 		count--;
   1018       1.1   mycroft 	}
   1019       1.1   mycroft 	return 1;
   1020       1.1   mycroft }
   1021       1.1   mycroft 
   1022       1.1   mycroft void
   1023       1.1   mycroft ahb_timeout(arg)
   1024       1.1   mycroft 	void *arg;
   1025       1.1   mycroft {
   1026       1.1   mycroft 	struct ahb_ecb *ecb = arg;
   1027      1.10    bouyer 	struct scsipi_xfer *xs = ecb->xs;
   1028      1.32    bouyer 	struct scsipi_periph *periph = xs->xs_periph;
   1029      1.32    bouyer 	struct ahb_softc *sc =
   1030      1.32    bouyer 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
   1031       1.1   mycroft 	int s;
   1032       1.1   mycroft 
   1033      1.32    bouyer 	scsipi_printaddr(periph);
   1034       1.5  christos 	printf("timed out");
   1035       1.1   mycroft 
   1036       1.1   mycroft 	s = splbio();
   1037       1.1   mycroft 
   1038       1.1   mycroft 	if (ecb->flags & ECB_IMMED) {
   1039       1.5  christos 		printf("\n");
   1040       1.1   mycroft 		ecb->flags |= ECB_IMMED_FAIL;
   1041       1.1   mycroft 		/* XXX Must reset! */
   1042       1.1   mycroft 	} else
   1043       1.1   mycroft 
   1044       1.1   mycroft 	/*
   1045       1.1   mycroft 	 * If it has been through before, then
   1046       1.1   mycroft 	 * a previous abort has failed, don't
   1047       1.1   mycroft 	 * try abort again
   1048       1.1   mycroft 	 */
   1049       1.1   mycroft 	if (ecb->flags & ECB_ABORT) {
   1050       1.1   mycroft 		/* abort timed out */
   1051       1.5  christos 		printf(" AGAIN\n");
   1052       1.1   mycroft 		/* XXX Must reset! */
   1053       1.1   mycroft 	} else {
   1054       1.1   mycroft 		/* abort the operation that has timed out */
   1055       1.5  christos 		printf("\n");
   1056       1.1   mycroft 		ecb->xs->error = XS_TIMEOUT;
   1057       1.1   mycroft 		ecb->timeout = AHB_ABORT_TIMEOUT;
   1058       1.1   mycroft 		ecb->flags |= ECB_ABORT;
   1059       1.1   mycroft 		ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
   1060       1.1   mycroft 	}
   1061       1.1   mycroft 
   1062       1.1   mycroft 	splx(s);
   1063       1.1   mycroft }
   1064