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