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