xref: /netbsd-src/sys/dev/eisa/ahb.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: ahb.c,v 1.70 2021/08/07 16:19:10 thorpej Exp $	*/
20c0b33c7Smycroft 
3fbc0df0aSthorpej /*-
48abe76d2Sthorpej  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5fbc0df0aSthorpej  * All rights reserved.
6fbc0df0aSthorpej  *
7fbc0df0aSthorpej  * This code is derived from software contributed to The NetBSD Foundation
8a1ac6881Smycroft  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9a1ac6881Smycroft  * Simulation Facility, NASA Ames Research Center.
10fbc0df0aSthorpej  *
11fbc0df0aSthorpej  * Redistribution and use in source and binary forms, with or without
12fbc0df0aSthorpej  * modification, are permitted provided that the following conditions
13fbc0df0aSthorpej  * are met:
14fbc0df0aSthorpej  * 1. Redistributions of source code must retain the above copyright
15fbc0df0aSthorpej  *    notice, this list of conditions and the following disclaimer.
16fbc0df0aSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
17fbc0df0aSthorpej  *    notice, this list of conditions and the following disclaimer in the
18fbc0df0aSthorpej  *    documentation and/or other materials provided with the distribution.
19fbc0df0aSthorpej  *
20fbc0df0aSthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21fbc0df0aSthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22fbc0df0aSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23fbc0df0aSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24fbc0df0aSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25fbc0df0aSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26fbc0df0aSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27fbc0df0aSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28fbc0df0aSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29fbc0df0aSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30fbc0df0aSthorpej  * POSSIBILITY OF SUCH DAMAGE.
31fbc0df0aSthorpej  */
32fbc0df0aSthorpej 
330c0b33c7Smycroft /*
340c0b33c7Smycroft  * Originally written by Julian Elischer (julian@tfs.com)
350c0b33c7Smycroft  * for TRW Financial Systems for use under the MACH(2.5) operating system.
360c0b33c7Smycroft  *
370c0b33c7Smycroft  * TRW Financial Systems, in accordance with their agreement with Carnegie
380c0b33c7Smycroft  * Mellon University, makes this software available to CMU to distribute
390c0b33c7Smycroft  * or use in any manner that they see fit as long as this message is kept with
400c0b33c7Smycroft  * the software. For this reason TFS also grants any other persons or
410c0b33c7Smycroft  * organisations permission to use or modify this software.
420c0b33c7Smycroft  *
430c0b33c7Smycroft  * TFS supplies this software to be publicly redistributed
440c0b33c7Smycroft  * on the understanding that TFS is not responsible for the correct
450c0b33c7Smycroft  * functioning of this software in any circumstances.
460c0b33c7Smycroft  */
470c0b33c7Smycroft 
48b84f53efSlukem #include <sys/cdefs.h>
49*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: ahb.c,v 1.70 2021/08/07 16:19:10 thorpej Exp $");
50b84f53efSlukem 
51b84f53efSlukem #include "opt_ddb.h"
52b84f53efSlukem 
53b84f53efSlukem #undef	AHBDEBUG
54b84f53efSlukem 
550c0b33c7Smycroft #include <sys/param.h>
560c0b33c7Smycroft #include <sys/systm.h>
570c0b33c7Smycroft #include <sys/kernel.h>
580c0b33c7Smycroft #include <sys/errno.h>
590c0b33c7Smycroft #include <sys/ioctl.h>
600c0b33c7Smycroft #include <sys/device.h>
610c0b33c7Smycroft #include <sys/buf.h>
620c0b33c7Smycroft #include <sys/proc.h>
630c0b33c7Smycroft 
64a2a38285Sad #include <sys/bus.h>
65a2a38285Sad #include <sys/intr.h>
660c0b33c7Smycroft 
676f3bab1fSbouyer #include <dev/scsipi/scsi_all.h>
686f3bab1fSbouyer #include <dev/scsipi/scsipi_all.h>
696f3bab1fSbouyer #include <dev/scsipi/scsiconf.h>
700c0b33c7Smycroft 
710c0b33c7Smycroft #include <dev/eisa/eisareg.h>
720c0b33c7Smycroft #include <dev/eisa/eisavar.h>
730c0b33c7Smycroft #include <dev/eisa/eisadevs.h>
740c0b33c7Smycroft #include <dev/eisa/ahbreg.h>
750c0b33c7Smycroft 
760c0b33c7Smycroft #ifndef DDB
770c0b33c7Smycroft #define Debugger() panic("should call debugger here (aha1742.c)")
780c0b33c7Smycroft #endif /* ! DDB */
790c0b33c7Smycroft 
800c0b33c7Smycroft #define AHB_ECB_MAX	32	/* store up to 32 ECBs at one time */
810c0b33c7Smycroft #define	ECB_HASH_SIZE	32	/* hash table size for phystokv */
820c0b33c7Smycroft #define	ECB_HASH_SHIFT	9
830c0b33c7Smycroft #define ECB_HASH(x)	((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
840c0b33c7Smycroft 
85fbc0df0aSthorpej #define AHB_MAXXFER	((AHB_NSEG - 1) << PGSHIFT)
860c0b33c7Smycroft 
870c0b33c7Smycroft struct ahb_softc {
88cbab9cadSchs 	device_t sc_dev;
890c0b33c7Smycroft 
90080350dcSmycroft 	bus_space_tag_t sc_iot;
9116c4c5afSthorpej 	bus_space_handle_t sc_ioh;
92fbc0df0aSthorpej 	bus_dma_tag_t sc_dmat;
930c0b33c7Smycroft 	void *sc_ih;
940c0b33c7Smycroft 
954399060eSthorpej 	bus_dmamap_t sc_dmamap_ecb;	/* maps the ecbs */
964399060eSthorpej 	struct ahb_ecb *sc_ecbs;	/* all our ecbs */
974399060eSthorpej 
980c0b33c7Smycroft 	struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
990c0b33c7Smycroft 	TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
1000c0b33c7Smycroft 	struct ahb_ecb *sc_immed_ecb;	/* an outstanding immediete command */
1010c0b33c7Smycroft 	int sc_numecbs;
10203a0c2d9Sthorpej 
103937a7a3eSbouyer 	struct scsipi_adapter sc_adapter;
104937a7a3eSbouyer 	struct scsipi_channel sc_channel;
1050c0b33c7Smycroft };
1060c0b33c7Smycroft 
1074399060eSthorpej /*
1084399060eSthorpej  * Offset of an ECB from the beginning of the ECB DMA mapping.
1094399060eSthorpej  */
110e145d99dSthorpej #define	AHB_ECB_OFF(e)	(((uintptr_t)(e)) - ((uintptr_t)&sc->sc_ecbs[0]))
1114399060eSthorpej 
112080350dcSmycroft struct ahb_probe_data {
113080350dcSmycroft 	int sc_irq;
1140165ccc0Sthorpej 	int sc_ist;
115080350dcSmycroft 	int sc_scsi_dev;
116080350dcSmycroft };
117080350dcSmycroft 
11841e5f04cSthorpej static void	ahb_send_mbox(struct ahb_softc *, int, struct ahb_ecb *);
11941e5f04cSthorpej static void	ahb_send_immed(struct ahb_softc *, u_int32_t, struct ahb_ecb *);
12041e5f04cSthorpej static int	ahbintr(void *);
12141e5f04cSthorpej static void	ahb_free_ecb(struct ahb_softc *, struct ahb_ecb *);
12241e5f04cSthorpej static struct	ahb_ecb *ahb_get_ecb(struct ahb_softc *);
1230039f264Sthorpej static struct	ahb_ecb *ahb_ecb_lookup(struct ahb_softc *, uint32_t);
12441e5f04cSthorpej static void	ahb_done(struct ahb_softc *, struct ahb_ecb *);
12541e5f04cSthorpej static int	ahb_find(bus_space_tag_t, bus_space_handle_t,
12641e5f04cSthorpej 		    struct ahb_probe_data *);
12741e5f04cSthorpej static int	ahb_init(struct ahb_softc *);
12841e5f04cSthorpej static void	ahbminphys(struct buf *);
12941e5f04cSthorpej static void	ahb_scsipi_request(struct scsipi_channel *,
13041e5f04cSthorpej 		    scsipi_adapter_req_t, void *);
13141e5f04cSthorpej static int	ahb_poll(struct ahb_softc *, struct scsipi_xfer *, int);
13241e5f04cSthorpej static void	ahb_timeout(void *);
13341e5f04cSthorpej static int	ahb_create_ecbs(struct ahb_softc *, struct ahb_ecb *, int);
1340c0b33c7Smycroft 
13541e5f04cSthorpej static int	ahb_init_ecb(struct ahb_softc *, struct ahb_ecb *);
136c9960663Sthorpej 
137529e91fcScegger static int	ahbmatch(device_t, cfdata_t, void *);
138529e91fcScegger static void	ahbattach(device_t, device_t, void *);
1390c0b33c7Smycroft 
140cbab9cadSchs CFATTACH_DECL_NEW(ahb, sizeof(struct ahb_softc),
141c9b3657cSthorpej     ahbmatch, ahbattach, NULL, NULL);
1420c0b33c7Smycroft 
1430c0b33c7Smycroft #define	AHB_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
1440c0b33c7Smycroft 
1455ce3f1d1Sthorpej static const struct device_compatible_entry compat_data[] = {
1465ce3f1d1Sthorpej 	{ .compat = "ADP0000",	.data = EISA_PRODUCT_ADP0000 },
1475ce3f1d1Sthorpej 	{ .compat = "ADP0001",	.data = EISA_PRODUCT_ADP0001 },
1485ce3f1d1Sthorpej 	{ .compat = "ADP0002",	.data = EISA_PRODUCT_ADP0002 },
1495ce3f1d1Sthorpej 	{ .compat = "ADP0400",	.data = EISA_PRODUCT_ADP0400 },
1505ce3f1d1Sthorpej 	DEVICE_COMPAT_EOL
1515ce3f1d1Sthorpej };
1525ce3f1d1Sthorpej 
1530c0b33c7Smycroft /*
1540c0b33c7Smycroft  * Check the slots looking for a board we recognise
155f0a7346dSsnj  * If we find one, note its address (slot) and call
1560c0b33c7Smycroft  * the actual probe routine to check it out.
1570c0b33c7Smycroft  */
15841e5f04cSthorpej static int
ahbmatch(device_t parent,cfdata_t match,void * aux)159d21ffc75Smsaitoh ahbmatch(device_t parent, cfdata_t match, void *aux)
1600c0b33c7Smycroft {
1610c0b33c7Smycroft 	struct eisa_attach_args *ea = aux;
16216c4c5afSthorpej 	bus_space_tag_t iot = ea->ea_iot;
16316c4c5afSthorpej 	bus_space_handle_t ioh;
1640c0b33c7Smycroft 	int rv;
1650c0b33c7Smycroft 
1665ce3f1d1Sthorpej 	if (!eisa_compatible_match(ea, compat_data))
1670c0b33c7Smycroft 		return (0);
1680c0b33c7Smycroft 
169dd769acbSmycroft 	if (bus_space_map(iot,
17071fbb921Smsaitoh 	    EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET,
17171fbb921Smsaitoh 	    AHB_EISA_IOSIZE, 0, &ioh))
1720c0b33c7Smycroft 		return (0);
1730c0b33c7Smycroft 
17416c4c5afSthorpej 	rv = !ahb_find(iot, ioh, NULL);
1750c0b33c7Smycroft 
176dd769acbSmycroft 	bus_space_unmap(iot, ioh, AHB_EISA_IOSIZE);
1770c0b33c7Smycroft 
1780c0b33c7Smycroft 	return (rv);
1790c0b33c7Smycroft }
1800c0b33c7Smycroft 
1810c0b33c7Smycroft /*
1820c0b33c7Smycroft  * Attach all the sub-devices we can find
1830c0b33c7Smycroft  */
18441e5f04cSthorpej static void
ahbattach(device_t parent,device_t self,void * aux)185529e91fcScegger ahbattach(device_t parent, device_t self, void *aux)
1860c0b33c7Smycroft {
1870c0b33c7Smycroft 	struct eisa_attach_args *ea = aux;
18892c7bba3Sthorpej 	struct ahb_softc *sc = device_private(self);
1895ce3f1d1Sthorpej 	const struct device_compatible_entry *dce;
19016c4c5afSthorpej 	bus_space_tag_t iot = ea->ea_iot;
19116c4c5afSthorpej 	bus_space_handle_t ioh;
1920c0b33c7Smycroft 	eisa_chipset_tag_t ec = ea->ea_ec;
1930c0b33c7Smycroft 	eisa_intr_handle_t ih;
1945ce3f1d1Sthorpej 	const char *intrstr;
195080350dcSmycroft 	struct ahb_probe_data apd;
196937a7a3eSbouyer 	struct scsipi_adapter *adapt = &sc->sc_adapter;
197937a7a3eSbouyer 	struct scsipi_channel *chan = &sc->sc_channel;
198e58a356cSchristos 	char intrbuf[EISA_INTRSTR_LEN];
1990c0b33c7Smycroft 
200cbab9cadSchs 	sc->sc_dev = self;
201cbab9cadSchs 
2025ce3f1d1Sthorpej 	dce = eisa_compatible_lookup(ea, compat_data);
2035ce3f1d1Sthorpej 	KASSERT(dce != NULL);
2045ce3f1d1Sthorpej 
205d21ffc75Smsaitoh 	aprint_naive("\n");
2065ce3f1d1Sthorpej 	aprint_normal(": %s\n", (const char *)dce->data);
2070c0b33c7Smycroft 
208dd769acbSmycroft 	if (bus_space_map(iot,
20971fbb921Smsaitoh 	    EISA_SLOT_ADDR(ea->ea_slot) + AHB_EISA_SLOT_OFFSET,
21071fbb921Smsaitoh 	    AHB_EISA_IOSIZE, 0, &ioh))
2110c0b33c7Smycroft 		panic("ahbattach: could not map I/O addresses");
2120c0b33c7Smycroft 
21316c4c5afSthorpej 	sc->sc_iot = iot;
2140c0b33c7Smycroft 	sc->sc_ioh = ioh;
215fbc0df0aSthorpej 	sc->sc_dmat = ea->ea_dmat;
216080350dcSmycroft 	if (ahb_find(iot, ioh, &apd))
2170c0b33c7Smycroft 		panic("ahbattach: ahb_find failed!");
2180c0b33c7Smycroft 
2190c0b33c7Smycroft 	TAILQ_INIT(&sc->sc_free_ecb);
220937a7a3eSbouyer 
221937a7a3eSbouyer 	/*
222937a7a3eSbouyer 	 * Fill in the scsipi_adapter.
223937a7a3eSbouyer 	 */
224937a7a3eSbouyer 	memset(adapt, 0, sizeof(*adapt));
225cbab9cadSchs 	adapt->adapt_dev = sc->sc_dev;
226937a7a3eSbouyer 	adapt->adapt_nchannels = 1;
227937a7a3eSbouyer 	/* adapt_openings initialized below */
228937a7a3eSbouyer 	adapt->adapt_max_periph = 4;		/* XXX arbitrary? */
229937a7a3eSbouyer 	adapt->adapt_request = ahb_scsipi_request;
230937a7a3eSbouyer 	adapt->adapt_minphys = ahbminphys;
231937a7a3eSbouyer 
232937a7a3eSbouyer 	/*
233937a7a3eSbouyer 	 * Fill in the scsipi_channel.
234937a7a3eSbouyer 	 */
235937a7a3eSbouyer 	memset(chan, 0, sizeof(*chan));
236937a7a3eSbouyer 	chan->chan_adapter = adapt;
237937a7a3eSbouyer 	chan->chan_bustype = &scsi_bustype;
238937a7a3eSbouyer 	chan->chan_channel = 0;
239937a7a3eSbouyer 	chan->chan_ntargets = 8;
240937a7a3eSbouyer 	chan->chan_nluns = 8;
241937a7a3eSbouyer 	chan->chan_id = apd.sc_scsi_dev;
2420c0b33c7Smycroft 
2434399060eSthorpej 	if (ahb_init(sc) != 0) {
2444399060eSthorpej 		/* Error during initialization! */
2454399060eSthorpej 		return;
2464399060eSthorpej 	}
2474399060eSthorpej 
248080350dcSmycroft 	if (eisa_intr_map(ec, apd.sc_irq, &ih)) {
249cbab9cadSchs 		aprint_error_dev(sc->sc_dev, "couldn't map interrupt (%d)\n",
25058956204Scegger 		    apd.sc_irq);
2510c0b33c7Smycroft 		return;
2520c0b33c7Smycroft 	}
253e58a356cSchristos 	intrstr = eisa_intr_string(ec, ih, intrbuf, sizeof(intrbuf));
2540165ccc0Sthorpej 	sc->sc_ih = eisa_intr_establish(ec, ih, apd.sc_ist, IPL_BIO,
2550c0b33c7Smycroft 	    ahbintr, sc);
2560c0b33c7Smycroft 	if (sc->sc_ih == NULL) {
257cbab9cadSchs 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
2580c0b33c7Smycroft 		if (intrstr != NULL)
25974341862Snjoly 			aprint_error(" at %s", intrstr);
26074341862Snjoly 		aprint_error("\n");
2610c0b33c7Smycroft 		return;
2620c0b33c7Smycroft 	}
2630165ccc0Sthorpej 	if (intrstr != NULL) {
2640165ccc0Sthorpej 		aprint_normal_dev(sc->sc_dev,
2650165ccc0Sthorpej 		    "interrupting at %s (%s trigger)\n", intrstr,
2660165ccc0Sthorpej 		    apd.sc_ist == IST_EDGE ? "edge" : "level");
2670165ccc0Sthorpej 	}
2680c0b33c7Smycroft 
2690c0b33c7Smycroft 	/*
2700c0b33c7Smycroft 	 * ask the adapter what subunits are present
2710c0b33c7Smycroft 	 */
272*c7fb772bSthorpej 	config_found(self, &sc->sc_channel, scsiprint, CFARGS_NONE);
2730c0b33c7Smycroft }
2740c0b33c7Smycroft 
2750c0b33c7Smycroft /*
2760c0b33c7Smycroft  * Function to send a command out through a mailbox
2770c0b33c7Smycroft  */
27841e5f04cSthorpej static void
ahb_send_mbox(struct ahb_softc * sc,int opcode,struct ahb_ecb * ecb)27941e5f04cSthorpej ahb_send_mbox(struct ahb_softc *sc, int opcode, struct ahb_ecb *ecb)
2800c0b33c7Smycroft {
28116c4c5afSthorpej 	bus_space_tag_t iot = sc->sc_iot;
28216c4c5afSthorpej 	bus_space_handle_t ioh = sc->sc_ioh;
2830c0b33c7Smycroft 	int wait = 300;	/* 1ms should be enough */
2840c0b33c7Smycroft 
2850c0b33c7Smycroft 	while (--wait) {
28616c4c5afSthorpej 		if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
2870c0b33c7Smycroft 		    == (G2STAT_MBOX_EMPTY))
2880c0b33c7Smycroft 			break;
2890c0b33c7Smycroft 		delay(10);
2900c0b33c7Smycroft 	}
2910c0b33c7Smycroft 	if (!wait) {
292cbab9cadSchs 		printf("%s: board not responding\n", device_xname(sc->sc_dev));
2930c0b33c7Smycroft 		Debugger();
2940c0b33c7Smycroft 	}
2950c0b33c7Smycroft 
296e145d99dSthorpej 	bus_space_write_4(iot, ioh, MBOXOUT0, ecb->ecb_dma_addr);
2976f3bab1fSbouyer 	bus_space_write_1(iot, ioh, ATTN, opcode |
298937a7a3eSbouyer 		ecb->xs->xs_periph->periph_target);
2990c0b33c7Smycroft 
30063b8530bSthorpej 	if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
301fc96443dSthorpej 		callout_reset(&ecb->xs->xs_callout,
302e5727031Sbouyer 		    mstohz(ecb->timeout), ahb_timeout, ecb);
3030c0b33c7Smycroft }
3040c0b33c7Smycroft 
3050c0b33c7Smycroft /*
3060c0b33c7Smycroft  * Function to send an immediate type command to the adapter
3070c0b33c7Smycroft  */
30841e5f04cSthorpej static void
ahb_send_immed(struct ahb_softc * sc,u_int32_t cmd,struct ahb_ecb * ecb)30941e5f04cSthorpej ahb_send_immed(struct ahb_softc *sc, u_int32_t cmd, struct ahb_ecb *ecb)
3100c0b33c7Smycroft {
31116c4c5afSthorpej 	bus_space_tag_t iot = sc->sc_iot;
31216c4c5afSthorpej 	bus_space_handle_t ioh = sc->sc_ioh;
3130c0b33c7Smycroft 	int wait = 100;	/* 1 ms enough? */
3140c0b33c7Smycroft 
3150c0b33c7Smycroft 	while (--wait) {
31616c4c5afSthorpej 		if ((bus_space_read_1(iot, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
3170c0b33c7Smycroft 		    == (G2STAT_MBOX_EMPTY))
3180c0b33c7Smycroft 			break;
3190c0b33c7Smycroft 		delay(10);
3200c0b33c7Smycroft 	}
3210c0b33c7Smycroft 	if (!wait) {
322cbab9cadSchs 		printf("%s: board not responding\n", device_xname(sc->sc_dev));
3230c0b33c7Smycroft 		Debugger();
3240c0b33c7Smycroft 	}
3250c0b33c7Smycroft 
3260039f264Sthorpej 	bus_space_write_4(iot, ioh, MBOXOUT0, cmd);
32716c4c5afSthorpej 	bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
3286f3bab1fSbouyer 	bus_space_write_1(iot, ioh, ATTN, OP_IMMED |
329937a7a3eSbouyer 		ecb->xs->xs_periph->periph_target);
3300c0b33c7Smycroft 
33163b8530bSthorpej 	if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
332fc96443dSthorpej 		callout_reset(&ecb->xs->xs_callout,
333e5727031Sbouyer 		    mstohz(ecb->timeout), ahb_timeout, ecb);
3340c0b33c7Smycroft }
3350c0b33c7Smycroft 
3360c0b33c7Smycroft /*
3370c0b33c7Smycroft  * Catch an interrupt from the adaptor
3380c0b33c7Smycroft  */
33941e5f04cSthorpej static int
ahbintr(void * arg)34041e5f04cSthorpej ahbintr(void *arg)
3410c0b33c7Smycroft {
3420c0b33c7Smycroft 	struct ahb_softc *sc = arg;
34316c4c5afSthorpej 	bus_space_tag_t iot = sc->sc_iot;
34416c4c5afSthorpej 	bus_space_handle_t ioh = sc->sc_ioh;
3450c0b33c7Smycroft 	struct ahb_ecb *ecb;
3460c0b33c7Smycroft 	u_char ahbstat;
347b2a5b02fSthorpej 	u_int32_t mboxval;
3480c0b33c7Smycroft 
3490c0b33c7Smycroft #ifdef	AHBDEBUG
350cbab9cadSchs 	printf("%s: ahbintr ", device_xname(sc->sc_dev));
3510c0b33c7Smycroft #endif /* AHBDEBUG */
3520c0b33c7Smycroft 
35316c4c5afSthorpej 	if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
3540c0b33c7Smycroft 		return 0;
3550c0b33c7Smycroft 
3560c0b33c7Smycroft 	for (;;) {
3570c0b33c7Smycroft 		/*
3580c0b33c7Smycroft 		 * First get all the information and then
359c0177219Swiz 		 * acknowledge the interrupt
3600c0b33c7Smycroft 		 */
36116c4c5afSthorpej 		ahbstat = bus_space_read_1(iot, ioh, G2INTST);
36216c4c5afSthorpej 		mboxval = bus_space_read_4(iot, ioh, MBOXIN0);
36316c4c5afSthorpej 		bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
3640c0b33c7Smycroft 
3650c0b33c7Smycroft #ifdef	AHBDEBUG
36686373f8cSchristos 		printf("status = 0x%x ", ahbstat);
3670c0b33c7Smycroft #endif /* AHBDEBUG */
3680c0b33c7Smycroft 
3690c0b33c7Smycroft 		/*
3700c0b33c7Smycroft 		 * Process the completed operation
3710c0b33c7Smycroft 		 */
3720c0b33c7Smycroft 		switch (ahbstat & G2INTST_INT_STAT) {
3730c0b33c7Smycroft 		case AHB_ECB_OK:
3740c0b33c7Smycroft 		case AHB_ECB_RECOVERED:
3750c0b33c7Smycroft 		case AHB_ECB_ERR:
3760039f264Sthorpej 			ecb = ahb_ecb_lookup(sc, mboxval);
3770c0b33c7Smycroft 			if (!ecb) {
37871fbb921Smsaitoh 				aprint_error_dev(sc->sc_dev,
37971fbb921Smsaitoh 				    "BAD ECB RETURNED!\n");
3800c0b33c7Smycroft 				goto next; /* whatever it was, it'll timeout */
3810c0b33c7Smycroft 			}
3820c0b33c7Smycroft 			break;
3830c0b33c7Smycroft 
3840c0b33c7Smycroft 		case AHB_IMMED_ERR:
3850c0b33c7Smycroft 			ecb = sc->sc_immed_ecb;
3860c0b33c7Smycroft 			sc->sc_immed_ecb = 0;
3870c0b33c7Smycroft 			ecb->flags |= ECB_IMMED_FAIL;
3880c0b33c7Smycroft 			break;
3890c0b33c7Smycroft 
3900c0b33c7Smycroft 		case AHB_IMMED_OK:
3910c0b33c7Smycroft 			ecb = sc->sc_immed_ecb;
3920c0b33c7Smycroft 			sc->sc_immed_ecb = 0;
3930c0b33c7Smycroft 			break;
3940c0b33c7Smycroft 
3950c0b33c7Smycroft 		default:
39671fbb921Smsaitoh 			aprint_error_dev(sc->sc_dev,
39771fbb921Smsaitoh 			    "unexpected interrupt %x\n", ahbstat);
3980c0b33c7Smycroft 			goto next;
3990c0b33c7Smycroft 		}
4000c0b33c7Smycroft 
401fc96443dSthorpej 		callout_stop(&ecb->xs->xs_callout);
4020c0b33c7Smycroft 		ahb_done(sc, ecb);
4030c0b33c7Smycroft 
4040c0b33c7Smycroft 	next:
40516c4c5afSthorpej 		if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
4060c0b33c7Smycroft 			return 1;
4070c0b33c7Smycroft 	}
4080c0b33c7Smycroft }
4090c0b33c7Smycroft 
41093124077Sperry static inline void
ahb_reset_ecb(struct ahb_softc * sc,struct ahb_ecb * ecb)411168cd830Schristos ahb_reset_ecb(struct ahb_softc *sc, struct ahb_ecb *ecb)
4120c0b33c7Smycroft {
4130c0b33c7Smycroft 
4140c0b33c7Smycroft 	ecb->flags = 0;
4150c0b33c7Smycroft }
4160c0b33c7Smycroft 
4170c0b33c7Smycroft /*
4180c0b33c7Smycroft  * A ecb (and hence a mbx-out is put onto the
4190c0b33c7Smycroft  * free list.
4200c0b33c7Smycroft  */
42141e5f04cSthorpej static void
ahb_free_ecb(struct ahb_softc * sc,struct ahb_ecb * ecb)42241e5f04cSthorpej ahb_free_ecb(struct ahb_softc *sc, struct ahb_ecb *ecb)
4230c0b33c7Smycroft {
4240c0b33c7Smycroft 	int s;
4250c0b33c7Smycroft 
4260c0b33c7Smycroft 	s = splbio();
4270c0b33c7Smycroft 	ahb_reset_ecb(sc, ecb);
4280c0b33c7Smycroft 	TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
4290c0b33c7Smycroft 	splx(s);
4300c0b33c7Smycroft }
4310c0b33c7Smycroft 
432fbc0df0aSthorpej /*
433fbc0df0aSthorpej  * Create a set of ecbs and add them to the free list.
434fbc0df0aSthorpej  */
43541e5f04cSthorpej static int
ahb_init_ecb(struct ahb_softc * sc,struct ahb_ecb * ecb)43641e5f04cSthorpej ahb_init_ecb(struct ahb_softc *sc, struct ahb_ecb *ecb)
4370c0b33c7Smycroft {
438fbc0df0aSthorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
4398001991eSthorpej 	int hashnum, error;
4400c0b33c7Smycroft 
441fbc0df0aSthorpej 	/*
4424399060eSthorpej 	 * Create the DMA map for this ECB.
443fbc0df0aSthorpej 	 */
4448001991eSthorpej 	error = bus_dmamap_create(dmat, AHB_MAXXFER, AHB_NSEG, AHB_MAXXFER,
4458001991eSthorpej 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &ecb->dmamap_xfer);
4468001991eSthorpej 	if (error) {
447cbab9cadSchs 		aprint_error_dev(sc->sc_dev, "can't create ecb dmamap_xfer\n");
4488001991eSthorpej 		return (error);
4498001991eSthorpej 	}
450fbc0df0aSthorpej 
451e145d99dSthorpej 	ecb->ecb_dma_addr = sc->sc_dmamap_ecb->dm_segs[0].ds_addr +
452e145d99dSthorpej 	    AHB_ECB_OFF(ecb);
453e145d99dSthorpej 
454fbc0df0aSthorpej 	/*
4550c0b33c7Smycroft 	 * put in the phystokv hash table
4560c0b33c7Smycroft 	 * Never gets taken out.
4570c0b33c7Smycroft 	 */
458e145d99dSthorpej 	hashnum = ECB_HASH(ecb->ecb_dma_addr);
4590c0b33c7Smycroft 	ecb->nexthash = sc->sc_ecbhash[hashnum];
4600c0b33c7Smycroft 	sc->sc_ecbhash[hashnum] = ecb;
4610c0b33c7Smycroft 	ahb_reset_ecb(sc, ecb);
4628001991eSthorpej 	return (0);
4630c0b33c7Smycroft }
4640c0b33c7Smycroft 
46541e5f04cSthorpej static int
ahb_create_ecbs(struct ahb_softc * sc,struct ahb_ecb * ecbstore,int count)46641e5f04cSthorpej ahb_create_ecbs(struct ahb_softc *sc, struct ahb_ecb *ecbstore, int count)
467fbc0df0aSthorpej {
468fbc0df0aSthorpej 	struct ahb_ecb *ecb;
4694399060eSthorpej 	int i, error;
470fbc0df0aSthorpej 
471c363a9cbScegger 	memset(ecbstore, 0, sizeof(struct ahb_ecb) * count);
4724399060eSthorpej 	for (i = 0; i < count; i++) {
4734399060eSthorpej 		ecb = &ecbstore[i];
4744399060eSthorpej 		if ((error = ahb_init_ecb(sc, ecb)) != 0) {
47571fbb921Smsaitoh 			aprint_error_dev(sc->sc_dev,
47671fbb921Smsaitoh 			    "unable to initialize ecb, error = %d\n", error);
4774399060eSthorpej 			goto out;
4788001991eSthorpej 		}
479fbc0df0aSthorpej 		TAILQ_INSERT_TAIL(&sc->sc_free_ecb, ecb, chain);
480fbc0df0aSthorpej 	}
4814399060eSthorpej  out:
4824399060eSthorpej 	return (i);
483fbc0df0aSthorpej }
484fbc0df0aSthorpej 
4850c0b33c7Smycroft /*
4860c0b33c7Smycroft  * Get a free ecb
4870c0b33c7Smycroft  *
4880c0b33c7Smycroft  * If there are none, see if we can allocate a new one. If so, put it in the
4890c0b33c7Smycroft  * hash table too otherwise either return an error or sleep.
4900c0b33c7Smycroft  */
49141e5f04cSthorpej static struct ahb_ecb *
ahb_get_ecb(struct ahb_softc * sc)49241e5f04cSthorpej ahb_get_ecb(struct ahb_softc *sc)
4930c0b33c7Smycroft {
4940c0b33c7Smycroft 	struct ahb_ecb *ecb;
4950c0b33c7Smycroft 	int s;
4960c0b33c7Smycroft 
4970c0b33c7Smycroft 	s = splbio();
498937a7a3eSbouyer 	ecb = TAILQ_FIRST(&sc->sc_free_ecb);
499937a7a3eSbouyer 	if (ecb != NULL) {
5000c0b33c7Smycroft 		TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
5010c0b33c7Smycroft 		ecb->flags |= ECB_ALLOC;
502937a7a3eSbouyer 	}
5030c0b33c7Smycroft 	splx(s);
504937a7a3eSbouyer 	return (ecb);
5050c0b33c7Smycroft }
5060c0b33c7Smycroft 
5070c0b33c7Smycroft /*
5080039f264Sthorpej  * Lookup and return the ECB that has the specified DMA address.
5090c0b33c7Smycroft  */
51041e5f04cSthorpej static struct ahb_ecb *
ahb_ecb_lookup(struct ahb_softc * sc,uint32_t ecb_phys)5110039f264Sthorpej ahb_ecb_lookup(struct ahb_softc *sc, uint32_t ecb_phys)
5120c0b33c7Smycroft {
5130c0b33c7Smycroft 	int hashnum = ECB_HASH(ecb_phys);
5140c0b33c7Smycroft 	struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
5150c0b33c7Smycroft 
5160c0b33c7Smycroft 	while (ecb) {
517e145d99dSthorpej 		if (ecb->ecb_dma_addr == ecb_phys)
5180c0b33c7Smycroft 			break;
5190c0b33c7Smycroft 		ecb = ecb->nexthash;
5200c0b33c7Smycroft 	}
5210c0b33c7Smycroft 	return ecb;
5220c0b33c7Smycroft }
5230c0b33c7Smycroft 
5240c0b33c7Smycroft /*
5250c0b33c7Smycroft  * We have a ecb which has been processed by the adaptor, now we look to see
5260c0b33c7Smycroft  * how the operation went.
5270c0b33c7Smycroft  */
52841e5f04cSthorpej static void
ahb_done(struct ahb_softc * sc,struct ahb_ecb * ecb)52941e5f04cSthorpej ahb_done(struct ahb_softc *sc, struct ahb_ecb *ecb)
5300c0b33c7Smycroft {
531fbc0df0aSthorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
532df9803ceSthorpej 	struct scsi_sense_data *s1, *s2;
5336f3bab1fSbouyer 	struct scsipi_xfer *xs = ecb->xs;
5340c0b33c7Smycroft 
535937a7a3eSbouyer 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("ahb_done\n"));
536fbc0df0aSthorpej 
5374399060eSthorpej 	bus_dmamap_sync(dmat, sc->sc_dmamap_ecb,
5384399060eSthorpej 	    AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
5394399060eSthorpej 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
5404399060eSthorpej 
541fbc0df0aSthorpej 	/*
542fbc0df0aSthorpej 	 * If we were a data transfer, unload the map that described
543fbc0df0aSthorpej 	 * the data buffer.
544fbc0df0aSthorpej 	 */
545fbc0df0aSthorpej 	if (xs->datalen) {
5468abe76d2Sthorpej 		bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
5478abe76d2Sthorpej 		    ecb->dmamap_xfer->dm_mapsize,
54863b8530bSthorpej 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
549fbc0df0aSthorpej 		    BUS_DMASYNC_POSTWRITE);
550fbc0df0aSthorpej 		bus_dmamap_unload(dmat, ecb->dmamap_xfer);
551fbc0df0aSthorpej 	}
552fbc0df0aSthorpej 
5530c0b33c7Smycroft 	/*
5540c0b33c7Smycroft 	 * Otherwise, put the results of the operation
5550c0b33c7Smycroft 	 * into the xfer and call whoever started it
5560c0b33c7Smycroft 	 */
5570c0b33c7Smycroft 	if ((ecb->flags & ECB_ALLOC) == 0) {
558cbab9cadSchs 		aprint_error_dev(sc->sc_dev, "exiting ecb not allocated!\n");
5590c0b33c7Smycroft 		Debugger();
5600c0b33c7Smycroft 	}
5610c0b33c7Smycroft 	if (ecb->flags & ECB_IMMED) {
5620c0b33c7Smycroft 		if (ecb->flags & ECB_IMMED_FAIL)
5630c0b33c7Smycroft 			xs->error = XS_DRIVER_STUFFUP;
5640c0b33c7Smycroft 		goto done;
5650c0b33c7Smycroft 	}
5660c0b33c7Smycroft 	if (xs->error == XS_NOERROR) {
5670c0b33c7Smycroft 		if (ecb->ecb_status.host_stat != HS_OK) {
5680c0b33c7Smycroft 			switch (ecb->ecb_status.host_stat) {
5690c0b33c7Smycroft 			case HS_TIMED_OUT:	/* No response */
5700c0b33c7Smycroft 				xs->error = XS_SELTIMEOUT;
5710c0b33c7Smycroft 				break;
5720c0b33c7Smycroft 			default:	/* Other scsi protocol messes */
57386373f8cSchristos 				printf("%s: host_stat %x\n",
574d21ffc75Smsaitoh 				    device_xname(sc->sc_dev),
575d21ffc75Smsaitoh 				    ecb->ecb_status.host_stat);
5760c0b33c7Smycroft 				xs->error = XS_DRIVER_STUFFUP;
5770c0b33c7Smycroft 			}
5780c0b33c7Smycroft 		} else if (ecb->ecb_status.target_stat != SCSI_OK) {
5790c0b33c7Smycroft 			switch (ecb->ecb_status.target_stat) {
5800c0b33c7Smycroft 			case SCSI_CHECK:
5810c0b33c7Smycroft 				s1 = &ecb->ecb_sense;
5826f3bab1fSbouyer 				s2 = &xs->sense.scsi_sense;
5830c0b33c7Smycroft 				*s2 = *s1;
5840c0b33c7Smycroft 				xs->error = XS_SENSE;
5850c0b33c7Smycroft 				break;
5860c0b33c7Smycroft 			case SCSI_BUSY:
5870c0b33c7Smycroft 				xs->error = XS_BUSY;
5880c0b33c7Smycroft 				break;
5890c0b33c7Smycroft 			default:
59086373f8cSchristos 				printf("%s: target_stat %x\n",
591d21ffc75Smsaitoh 				    device_xname(sc->sc_dev),
592d21ffc75Smsaitoh 				    ecb->ecb_status.target_stat);
5930c0b33c7Smycroft 				xs->error = XS_DRIVER_STUFFUP;
5940c0b33c7Smycroft 			}
5950c0b33c7Smycroft 		} else
5960c0b33c7Smycroft 			xs->resid = 0;
5970c0b33c7Smycroft 	}
5980c0b33c7Smycroft done:
5990c0b33c7Smycroft 	ahb_free_ecb(sc, ecb);
6006f3bab1fSbouyer 	scsipi_done(xs);
6010c0b33c7Smycroft }
6020c0b33c7Smycroft 
6030c0b33c7Smycroft /*
6040c0b33c7Smycroft  * Start the board, ready for normal operation
6050c0b33c7Smycroft  */
60641e5f04cSthorpej static int
ahb_find(bus_space_tag_t iot,bus_space_handle_t ioh,struct ahb_probe_data * sc)60771fbb921Smsaitoh ahb_find(bus_space_tag_t iot, bus_space_handle_t ioh,
60871fbb921Smsaitoh     struct ahb_probe_data *sc)
6090c0b33c7Smycroft {
6100c0b33c7Smycroft 	u_char intdef;
6110165ccc0Sthorpej 	int i, irq, ist, busid;
6120c0b33c7Smycroft 	int wait = 1000;	/* 1 sec enough? */
6130c0b33c7Smycroft 
61416c4c5afSthorpej 	bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
6150c0b33c7Smycroft 
6160c0b33c7Smycroft #define	NO_NO 1
6170c0b33c7Smycroft #ifdef NO_NO
6180c0b33c7Smycroft 	/*
6190c0b33c7Smycroft 	 * reset board, If it doesn't respond, assume
6200c0b33c7Smycroft 	 * that it's not there.. good for the probe
6210c0b33c7Smycroft 	 */
62216c4c5afSthorpej 	bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
6230c0b33c7Smycroft 	delay(1000);
62416c4c5afSthorpej 	bus_space_write_1(iot, ioh, G2CNTRL, 0);
6250c0b33c7Smycroft 	delay(10000);
6260c0b33c7Smycroft 	while (--wait) {
62716c4c5afSthorpej 		if ((bus_space_read_1(iot, ioh, G2STAT) & G2STAT_BUSY) == 0)
6280c0b33c7Smycroft 			break;
6290c0b33c7Smycroft 		delay(1000);
6300c0b33c7Smycroft 	}
6310c0b33c7Smycroft 	if (!wait) {
6320c0b33c7Smycroft #ifdef	AHBDEBUG
63386373f8cSchristos 		printf("ahb_find: No answer from aha1742 board\n");
6340c0b33c7Smycroft #endif /* AHBDEBUG */
6350c0b33c7Smycroft 		return ENXIO;
6360c0b33c7Smycroft 	}
63716c4c5afSthorpej 	i = bus_space_read_1(iot, ioh, MBOXIN0);
6380c0b33c7Smycroft 	if (i) {
63986373f8cSchristos 		printf("self test failed, val = 0x%x\n", i);
6400c0b33c7Smycroft 		return EIO;
6410c0b33c7Smycroft 	}
6420c0b33c7Smycroft 
6430c0b33c7Smycroft 	/* Set it again, just to be sure. */
64416c4c5afSthorpej 	bus_space_write_1(iot, ioh, PORTADDR, PORTADDR_ENHANCED);
6450c0b33c7Smycroft #endif
6460c0b33c7Smycroft 
64716c4c5afSthorpej 	while (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND) {
64886373f8cSchristos 		printf(".");
64916c4c5afSthorpej 		bus_space_write_1(iot, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
6500c0b33c7Smycroft 		delay(10000);
6510c0b33c7Smycroft 	}
6520c0b33c7Smycroft 
65316c4c5afSthorpej 	intdef = bus_space_read_1(iot, ioh, INTDEF);
6540c0b33c7Smycroft 	switch (intdef & 0x07) {
6550c0b33c7Smycroft 	case INT9:
6560c0b33c7Smycroft 		irq = 9;
6570c0b33c7Smycroft 		break;
6580c0b33c7Smycroft 	case INT10:
6590c0b33c7Smycroft 		irq = 10;
6600c0b33c7Smycroft 		break;
6610c0b33c7Smycroft 	case INT11:
6620c0b33c7Smycroft 		irq = 11;
6630c0b33c7Smycroft 		break;
6640c0b33c7Smycroft 	case INT12:
6650c0b33c7Smycroft 		irq = 12;
6660c0b33c7Smycroft 		break;
6670c0b33c7Smycroft 	case INT14:
6680c0b33c7Smycroft 		irq = 14;
6690c0b33c7Smycroft 		break;
6700c0b33c7Smycroft 	case INT15:
6710c0b33c7Smycroft 		irq = 15;
6720c0b33c7Smycroft 		break;
6730c0b33c7Smycroft 	default:
67486373f8cSchristos 		printf("illegal int setting %x\n", intdef);
6750c0b33c7Smycroft 		return EIO;
6760c0b33c7Smycroft 	}
6770c0b33c7Smycroft 
6780165ccc0Sthorpej 	/*
6790165ccc0Sthorpej 	 * On EISA, edge triggered interrupts are signalled by the rising
6800165ccc0Sthorpej 	 * edge of the interrupt signal, while level tiggered interrupts
6810165ccc0Sthorpej 	 * are signalled so long as the interrupt signal is driven low.
6820165ccc0Sthorpej 	 *
6830165ccc0Sthorpej 	 * So, if the controller is configured for active-high interrupts,
6840165ccc0Sthorpej 	 * that is "edge trigger" in our parlance, while active-low would
6850165ccc0Sthorpej 	 * be "level trigger".
6860165ccc0Sthorpej 	 */
6870165ccc0Sthorpej 	if (intdef & INTHIGH) {
6880165ccc0Sthorpej 		ist = IST_EDGE;
6890165ccc0Sthorpej 	} else {
6900165ccc0Sthorpej 		ist = IST_LEVEL;
6910165ccc0Sthorpej 	}
6920165ccc0Sthorpej 
69316c4c5afSthorpej 	bus_space_write_1(iot, ioh, INTDEF, (intdef | INTEN));	/* make sure we can interrupt */
6940c0b33c7Smycroft 
6950c0b33c7Smycroft 	/* who are we on the scsi bus? */
69616c4c5afSthorpej 	busid = (bus_space_read_1(iot, ioh, SCSIDEF) & HSCSIID);
6970c0b33c7Smycroft 
698080350dcSmycroft 	/* if we want to return data, do so now */
699080350dcSmycroft 	if (sc) {
7000c0b33c7Smycroft 		sc->sc_irq = irq;
7010165ccc0Sthorpej 		sc->sc_ist = ist;
7020c0b33c7Smycroft 		sc->sc_scsi_dev = busid;
7030c0b33c7Smycroft 	}
7040c0b33c7Smycroft 
7050c0b33c7Smycroft 	/*
7060c0b33c7Smycroft 	 * Note that we are going and return (to probe)
7070c0b33c7Smycroft 	 */
7080c0b33c7Smycroft 	return 0;
7090c0b33c7Smycroft }
7100c0b33c7Smycroft 
71141e5f04cSthorpej static int
ahb_init(struct ahb_softc * sc)71241e5f04cSthorpej ahb_init(struct ahb_softc *sc)
7130c0b33c7Smycroft {
7144399060eSthorpej 	bus_dma_segment_t seg;
7154399060eSthorpej 	int i, error, rseg;
7160c0b33c7Smycroft 
7174399060eSthorpej #define	ECBSIZE		(AHB_ECB_MAX * sizeof(struct ahb_ecb))
7184399060eSthorpej 
7194399060eSthorpej 	/*
7204399060eSthorpej 	 * Allocate the ECBs.
7214399060eSthorpej 	 */
7224399060eSthorpej 	if ((error = bus_dmamem_alloc(sc->sc_dmat, ECBSIZE,
723275539f0Sthorpej 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
724d21ffc75Smsaitoh 		aprint_error_dev(sc->sc_dev,
725d21ffc75Smsaitoh 		    "unable to allocate ecbs, error = %d\n", error);
7264399060eSthorpej 		return (error);
7274399060eSthorpej 	}
7284399060eSthorpej 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
72953524e44Schristos 	    ECBSIZE, (void **)&sc->sc_ecbs,
7304399060eSthorpej 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
73171fbb921Smsaitoh 		aprint_error_dev(sc->sc_dev,
73271fbb921Smsaitoh 		    "unable to map ecbs, error = %d\n", error);
7334399060eSthorpej 		return (error);
7344399060eSthorpej 	}
7354399060eSthorpej 
7364399060eSthorpej 	/*
7374399060eSthorpej 	 * Create and load the DMA map used for the ecbs.
7384399060eSthorpej 	 */
7394399060eSthorpej 	if ((error = bus_dmamap_create(sc->sc_dmat, ECBSIZE,
7404399060eSthorpej 	    1, ECBSIZE, 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_ecb)) != 0) {
74171fbb921Smsaitoh 		aprint_error_dev(sc->sc_dev,
74271fbb921Smsaitoh 		    "unable to create ecb DMA map, error = %d\n", error);
7434399060eSthorpej 		return (error);
7444399060eSthorpej 	}
7454399060eSthorpej 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_ecb,
7464399060eSthorpej 	    sc->sc_ecbs, ECBSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
74771fbb921Smsaitoh 		aprint_error_dev(sc->sc_dev,
74871fbb921Smsaitoh 		    "unable to load ecb DMA map, error = %d\n", error);
7494399060eSthorpej 		return (error);
7504399060eSthorpej 	}
7514399060eSthorpej 
7524399060eSthorpej #undef ECBSIZE
7534399060eSthorpej 
7544399060eSthorpej 	/*
7554399060eSthorpej 	 * Initialize the ecbs.
7564399060eSthorpej 	 */
7574399060eSthorpej 	i = ahb_create_ecbs(sc, sc->sc_ecbs, AHB_ECB_MAX);
7584399060eSthorpej 	if (i == 0) {
759cbab9cadSchs 		aprint_error_dev(sc->sc_dev, "unable to create ecbs\n");
7604399060eSthorpej 		return (ENOMEM);
7614399060eSthorpej 	} else if (i != AHB_ECB_MAX) {
7624399060eSthorpej 		printf("%s: WARNING: only %d of %d ecbs created\n",
763cbab9cadSchs 		    device_xname(sc->sc_dev), i, AHB_ECB_MAX);
7644399060eSthorpej 	}
7654399060eSthorpej 
766937a7a3eSbouyer 	sc->sc_adapter.adapt_openings = i;
767937a7a3eSbouyer 
7684399060eSthorpej 	return (0);
7690c0b33c7Smycroft }
7700c0b33c7Smycroft 
77141e5f04cSthorpej static void
ahbminphys(struct buf * bp)77241e5f04cSthorpej ahbminphys(struct buf *bp)
7730c0b33c7Smycroft {
7740c0b33c7Smycroft 
775fbc0df0aSthorpej 	if (bp->b_bcount > AHB_MAXXFER)
776fbc0df0aSthorpej 		bp->b_bcount = AHB_MAXXFER;
7770c0b33c7Smycroft 	minphys(bp);
7780c0b33c7Smycroft }
7790c0b33c7Smycroft 
7800c0b33c7Smycroft /*
7810c0b33c7Smycroft  * start a scsi operation given the command and the data address.  Also needs
7820c0b33c7Smycroft  * the unit, target and lu.
7830c0b33c7Smycroft  */
78441e5f04cSthorpej static void
ahb_scsipi_request(struct scsipi_channel * chan,scsipi_adapter_req_t req,void * arg)78541e5f04cSthorpej ahb_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
78641e5f04cSthorpej     void *arg)
7870c0b33c7Smycroft {
788937a7a3eSbouyer 	struct scsipi_xfer *xs;
789937a7a3eSbouyer 	struct scsipi_periph *periph;
790cbab9cadSchs 	struct ahb_softc *sc = device_private(chan->chan_adapter->adapt_dev);
791fbc0df0aSthorpej 	bus_dma_tag_t dmat = sc->sc_dmat;
7920c0b33c7Smycroft 	struct ahb_ecb *ecb;
793fbc0df0aSthorpej 	int error, seg, flags, s;
7940c0b33c7Smycroft 
795937a7a3eSbouyer 	switch (req) {
796937a7a3eSbouyer 	case ADAPTER_REQ_RUN_XFER:
797937a7a3eSbouyer 		xs = arg;
798937a7a3eSbouyer 		periph = xs->xs_periph;
79963b8530bSthorpej 		flags = xs->xs_control;
80003a0c2d9Sthorpej 
801937a7a3eSbouyer 		SC_DEBUG(periph, SCSIPI_DB2, ("ahb_scsipi_request\n"));
80203a0c2d9Sthorpej 
803937a7a3eSbouyer 		/* Get an ECB to use. */
804937a7a3eSbouyer 		ecb = ahb_get_ecb(sc);
805937a7a3eSbouyer #ifdef DIAGNOSTIC
806937a7a3eSbouyer 		/*
807937a7a3eSbouyer 		 * This should never happen as we track the resources
808937a7a3eSbouyer 		 * in the mid-layer.
809937a7a3eSbouyer 		 */
810937a7a3eSbouyer 		if (ecb == NULL) {
811937a7a3eSbouyer 			scsipi_printaddr(periph);
812937a7a3eSbouyer 			printf("unable to allocate ecb\n");
813937a7a3eSbouyer 			panic("ahb_scsipi_request");
814937a7a3eSbouyer 		}
815937a7a3eSbouyer #endif
81603a0c2d9Sthorpej 
8170c0b33c7Smycroft 		ecb->xs = xs;
8180c0b33c7Smycroft 		ecb->timeout = xs->timeout;
8190c0b33c7Smycroft 
8200c0b33c7Smycroft 		/*
8210c0b33c7Smycroft 		 * If it's a reset, we need to do an 'immediate'
8220c0b33c7Smycroft 		 * command, and store its ecb for later
8230c0b33c7Smycroft 		 * if there is already an immediate waiting,
8240c0b33c7Smycroft 		 * then WE must wait
8250c0b33c7Smycroft 		 */
82663b8530bSthorpej 		if (flags & XS_CTL_RESET) {
8270c0b33c7Smycroft 			ecb->flags |= ECB_IMMED;
828937a7a3eSbouyer 			if (sc->sc_immed_ecb) {
829937a7a3eSbouyer 				ahb_free_ecb(sc, ecb);
830937a7a3eSbouyer 				xs->error = XS_BUSY;
831937a7a3eSbouyer 				scsipi_done(xs);
832937a7a3eSbouyer 				return;
833937a7a3eSbouyer 			}
8340c0b33c7Smycroft 			sc->sc_immed_ecb = ecb;
8350c0b33c7Smycroft 
8360c0b33c7Smycroft 			s = splbio();
8370c0b33c7Smycroft 			ahb_send_immed(sc, AHB_TARG_RESET, ecb);
8380c0b33c7Smycroft 			splx(s);
8390c0b33c7Smycroft 
84063b8530bSthorpej 			if ((flags & XS_CTL_POLL) == 0)
841937a7a3eSbouyer 				return;
8420c0b33c7Smycroft 
8430c0b33c7Smycroft 			/*
8440c0b33c7Smycroft 			 * If we can't use interrupts, poll on completion
8450c0b33c7Smycroft 			 */
8460c0b33c7Smycroft 			if (ahb_poll(sc, xs, ecb->timeout))
8470c0b33c7Smycroft 				ahb_timeout(ecb);
848937a7a3eSbouyer 			return;
8490c0b33c7Smycroft 		}
8500c0b33c7Smycroft 
8510c0b33c7Smycroft 		/*
8520c0b33c7Smycroft 		 * Put all the arguments for the xfer in the ecb
8530c0b33c7Smycroft 		 */
854d41e8f00Sthorpej 		if (xs->cmdlen > sizeof(ecb->scsi_cmd)) {
85571fbb921Smsaitoh 			aprint_error_dev(sc->sc_dev,
85671fbb921Smsaitoh 			    "cmdlen %d too large for ECB\n", xs->cmdlen);
857d41e8f00Sthorpej 			xs->error = XS_DRIVER_STUFFUP;
858d41e8f00Sthorpej 			goto out_bad;
859d41e8f00Sthorpej 		}
8600c0b33c7Smycroft 		ecb->opcode = ECB_SCSI_OP;
8610c0b33c7Smycroft 		ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
862937a7a3eSbouyer 		ecb->opt2 = periph->periph_lun | ECB_NRB;
863e2cb8590Scegger 		memcpy(&ecb->scsi_cmd, xs->cmd,
864937a7a3eSbouyer 		    ecb->scsi_cmd_length = xs->cmdlen);
865e145d99dSthorpej 		ecb->sense_ptr = ecb->ecb_dma_addr +
866e145d99dSthorpej 		    offsetof(struct ahb_ecb, ecb_sense);
8670c0b33c7Smycroft 		ecb->req_sense_length = sizeof(ecb->ecb_sense);
868e145d99dSthorpej 		ecb->status = ecb->ecb_dma_addr +
869e145d99dSthorpej 		    offsetof(struct ahb_ecb, ecb_status);
8700c0b33c7Smycroft 		ecb->ecb_status.host_stat = 0x00;
8710c0b33c7Smycroft 		ecb->ecb_status.target_stat = 0x00;
8720c0b33c7Smycroft 
8730c0b33c7Smycroft 		if (xs->datalen) {
874fbc0df0aSthorpej 			/*
875fbc0df0aSthorpej 			 * Map the DMA transfer.
876fbc0df0aSthorpej 			 */
8770c0b33c7Smycroft #ifdef TFS
87863b8530bSthorpej 			if (flags & XS_CTL_DATA_UIO) {
879fbc0df0aSthorpej 				error = bus_dmamap_load_uio(sc->sc_dmat,
880fbc0df0aSthorpej 				    ecb->dmamap_xfer, (struct uio *)xs->data,
881937a7a3eSbouyer 				    BUS_DMA_NOWAIT);
882fbc0df0aSthorpej 			} else
8830c0b33c7Smycroft #endif /* TFS */
8840c0b33c7Smycroft 			{
885fbc0df0aSthorpej 				error = bus_dmamap_load(sc->sc_dmat,
886937a7a3eSbouyer 				    ecb->dmamap_xfer, xs->data, xs->datalen,
887937a7a3eSbouyer 				    NULL, BUS_DMA_NOWAIT);
8880c0b33c7Smycroft 			}
889fbc0df0aSthorpej 
890937a7a3eSbouyer 			switch (error) {
891937a7a3eSbouyer 			case 0:
892937a7a3eSbouyer 				break;
893937a7a3eSbouyer 
894937a7a3eSbouyer 			case ENOMEM:
895937a7a3eSbouyer 			case EAGAIN:
896937a7a3eSbouyer 				xs->error = XS_RESOURCE_SHORTAGE;
897937a7a3eSbouyer 				goto out_bad;
898937a7a3eSbouyer 
899937a7a3eSbouyer 			default:
900937a7a3eSbouyer 				xs->error = XS_DRIVER_STUFFUP;
90171fbb921Smsaitoh 				aprint_error_dev(sc->sc_dev,
90271fbb921Smsaitoh 				    "error %d loading DMA map\n", error);
903937a7a3eSbouyer  out_bad:
904937a7a3eSbouyer 				ahb_free_ecb(sc, ecb);
905937a7a3eSbouyer 				scsipi_done(xs);
906937a7a3eSbouyer 				return;
9070c0b33c7Smycroft 			}
908fbc0df0aSthorpej 
9098abe76d2Sthorpej 			bus_dmamap_sync(dmat, ecb->dmamap_xfer, 0,
9108abe76d2Sthorpej 			    ecb->dmamap_xfer->dm_mapsize,
91163b8530bSthorpej 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
912fbc0df0aSthorpej 			    BUS_DMASYNC_PREWRITE);
913fbc0df0aSthorpej 
914fbc0df0aSthorpej 			/*
915fbc0df0aSthorpej 			 * Load the hardware scatter/gather map with the
916fbc0df0aSthorpej 			 * contents of the DMA map.
917fbc0df0aSthorpej 			 */
918fbc0df0aSthorpej 			for (seg = 0; seg < ecb->dmamap_xfer->dm_nsegs; seg++) {
919fbc0df0aSthorpej 				ecb->ahb_dma[seg].seg_addr =
920fbc0df0aSthorpej 				    ecb->dmamap_xfer->dm_segs[seg].ds_addr;
921fbc0df0aSthorpej 				ecb->ahb_dma[seg].seg_len =
922fbc0df0aSthorpej 				    ecb->dmamap_xfer->dm_segs[seg].ds_len;
923fbc0df0aSthorpej 			}
924fbc0df0aSthorpej 
925e145d99dSthorpej 			ecb->data_addr = ecb->ecb_dma_addr +
926937a7a3eSbouyer 			    offsetof(struct ahb_ecb, ahb_dma);
927fbc0df0aSthorpej 			ecb->data_length = ecb->dmamap_xfer->dm_nsegs *
928fbc0df0aSthorpej 			    sizeof(struct ahb_dma_seg);
9290c0b33c7Smycroft 			ecb->opt1 |= ECB_S_G;
9300c0b33c7Smycroft 		} else {	/* No data xfer, use non S/G values */
931e145d99dSthorpej 			ecb->data_addr = 0;
9320c0b33c7Smycroft 			ecb->data_length = 0;
9330c0b33c7Smycroft 		}
934e145d99dSthorpej 		ecb->link_addr = 0;
9350c0b33c7Smycroft 
9364399060eSthorpej 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_ecb,
9374399060eSthorpej 		    AHB_ECB_OFF(ecb), sizeof(struct ahb_ecb),
9384399060eSthorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
9394399060eSthorpej 
9400c0b33c7Smycroft 		s = splbio();
9410c0b33c7Smycroft 		ahb_send_mbox(sc, OP_START_ECB, ecb);
9420c0b33c7Smycroft 		splx(s);
9430c0b33c7Smycroft 
94463b8530bSthorpej 		if ((flags & XS_CTL_POLL) == 0)
945937a7a3eSbouyer 			return;
9460c0b33c7Smycroft 
9470c0b33c7Smycroft 		/*
9480c0b33c7Smycroft 		 * If we can't use interrupts, poll on completion
9490c0b33c7Smycroft 		 */
9500c0b33c7Smycroft 		if (ahb_poll(sc, xs, ecb->timeout)) {
9510c0b33c7Smycroft 			ahb_timeout(ecb);
9520c0b33c7Smycroft 			if (ahb_poll(sc, xs, ecb->timeout))
9530c0b33c7Smycroft 				ahb_timeout(ecb);
9540c0b33c7Smycroft 		}
955937a7a3eSbouyer 		return;
9560c0b33c7Smycroft 
957937a7a3eSbouyer 	case ADAPTER_REQ_GROW_RESOURCES:
958937a7a3eSbouyer 		/* XXX Not supported. */
959937a7a3eSbouyer 		return;
960937a7a3eSbouyer 
961937a7a3eSbouyer 	case ADAPTER_REQ_SET_XFER_MODE:
962937a7a3eSbouyer 		/* XXX How do we do this? */
963937a7a3eSbouyer 		return;
964937a7a3eSbouyer 	}
9650c0b33c7Smycroft }
9660c0b33c7Smycroft 
9670c0b33c7Smycroft /*
9680c0b33c7Smycroft  * Function to poll for command completion when in poll mode
9690c0b33c7Smycroft  */
97041e5f04cSthorpej static int
ahb_poll(struct ahb_softc * sc,struct scsipi_xfer * xs,int count)97141e5f04cSthorpej ahb_poll(struct ahb_softc *sc, struct scsipi_xfer *xs, int count)
9720c0b33c7Smycroft {				/* in msec  */
97316c4c5afSthorpej 	bus_space_tag_t iot = sc->sc_iot;
97416c4c5afSthorpej 	bus_space_handle_t ioh = sc->sc_ioh;
9750c0b33c7Smycroft 
9760c0b33c7Smycroft 	while (count) {
9770c0b33c7Smycroft 		/*
9780c0b33c7Smycroft 		 * If we had interrupts enabled, would we
9790c0b33c7Smycroft 		 * have got an interrupt?
9800c0b33c7Smycroft 		 */
98116c4c5afSthorpej 		if (bus_space_read_1(iot, ioh, G2STAT) & G2STAT_INT_PEND)
9820c0b33c7Smycroft 			ahbintr(sc);
98363b8530bSthorpej 		if (xs->xs_status & XS_STS_DONE)
9840c0b33c7Smycroft 			return 0;
9850c0b33c7Smycroft 		delay(1000);
9860c0b33c7Smycroft 		count--;
9870c0b33c7Smycroft 	}
9880c0b33c7Smycroft 	return 1;
9890c0b33c7Smycroft }
9900c0b33c7Smycroft 
99141e5f04cSthorpej static void
ahb_timeout(void * arg)99241e5f04cSthorpej ahb_timeout(void *arg)
9930c0b33c7Smycroft {
9940c0b33c7Smycroft 	struct ahb_ecb *ecb = arg;
9956f3bab1fSbouyer 	struct scsipi_xfer *xs = ecb->xs;
996937a7a3eSbouyer 	struct scsipi_periph *periph = xs->xs_periph;
997937a7a3eSbouyer 	struct ahb_softc *sc =
998cbab9cadSchs 	    device_private(periph->periph_channel->chan_adapter->adapt_dev);
9990c0b33c7Smycroft 	int s;
10000c0b33c7Smycroft 
1001937a7a3eSbouyer 	scsipi_printaddr(periph);
100286373f8cSchristos 	printf("timed out");
10030c0b33c7Smycroft 
10040c0b33c7Smycroft 	s = splbio();
10050c0b33c7Smycroft 
10060c0b33c7Smycroft 	if (ecb->flags & ECB_IMMED) {
100786373f8cSchristos 		printf("\n");
10080c0b33c7Smycroft 		ecb->flags |= ECB_IMMED_FAIL;
10090c0b33c7Smycroft 		/* XXX Must reset! */
10100c0b33c7Smycroft 	} else
10110c0b33c7Smycroft 
10120c0b33c7Smycroft 	/*
10130c0b33c7Smycroft 	 * If it has been through before, then
10140c0b33c7Smycroft 	 * a previous abort has failed, don't
10150c0b33c7Smycroft 	 * try abort again
10160c0b33c7Smycroft 	 */
10170c0b33c7Smycroft 	if (ecb->flags & ECB_ABORT) {
10180c0b33c7Smycroft 		/* abort timed out */
101986373f8cSchristos 		printf(" AGAIN\n");
10200c0b33c7Smycroft 		/* XXX Must reset! */
10210c0b33c7Smycroft 	} else {
10220c0b33c7Smycroft 		/* abort the operation that has timed out */
102386373f8cSchristos 		printf("\n");
10240c0b33c7Smycroft 		ecb->xs->error = XS_TIMEOUT;
10250c0b33c7Smycroft 		ecb->timeout = AHB_ABORT_TIMEOUT;
10260c0b33c7Smycroft 		ecb->flags |= ECB_ABORT;
10270c0b33c7Smycroft 		ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
10280c0b33c7Smycroft 	}
10290c0b33c7Smycroft 
10300c0b33c7Smycroft 	splx(s);
10310c0b33c7Smycroft }
1032