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