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