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