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