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