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