xref: /netbsd-src/sys/arch/arc/dti/btl.c (revision 8ff6f65dafc58186614d149001c905dec4c934a2)
1*8ff6f65dSthorpej /*	$NetBSD: btl.c,v 1.31 2023/12/20 06:36:02 thorpej Exp $	*/
2e802a7dfSsoda /*	NetBSD: bt.c,v 1.10 1996/05/12 23:51:54 mycroft Exp 	*/
35009ff6dSsoda 
45009ff6dSsoda #undef BTDIAG
55009ff6dSsoda #define integrate
65009ff6dSsoda 
7564df9b6Ssoda #define notyet /* XXX - #undef this, if this driver does actually work */
8564df9b6Ssoda 
95009ff6dSsoda /*
105009ff6dSsoda  * Copyright (c) 1994, 1996 Charles M. Hannum.  All rights reserved.
115009ff6dSsoda  *
125009ff6dSsoda  * Redistribution and use in source and binary forms, with or without
135009ff6dSsoda  * modification, are permitted provided that the following conditions
145009ff6dSsoda  * are met:
155009ff6dSsoda  * 1. Redistributions of source code must retain the above copyright
165009ff6dSsoda  *    notice, this list of conditions and the following disclaimer.
175009ff6dSsoda  * 2. Redistributions in binary form must reproduce the above copyright
185009ff6dSsoda  *    notice, this list of conditions and the following disclaimer in the
195009ff6dSsoda  *    documentation and/or other materials provided with the distribution.
205009ff6dSsoda  * 3. All advertising materials mentioning features or use of this software
215009ff6dSsoda  *    must display the following acknowledgement:
225009ff6dSsoda  *	This product includes software developed by Charles M. Hannum.
235009ff6dSsoda  * 4. The name of the author may not be used to endorse or promote products
245009ff6dSsoda  *    derived from this software without specific prior written permission.
255009ff6dSsoda  *
265009ff6dSsoda  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
275009ff6dSsoda  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
285009ff6dSsoda  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
295009ff6dSsoda  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
305009ff6dSsoda  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
315009ff6dSsoda  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
325009ff6dSsoda  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
335009ff6dSsoda  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
345009ff6dSsoda  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
355009ff6dSsoda  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
365009ff6dSsoda  */
375009ff6dSsoda 
385009ff6dSsoda /*
395009ff6dSsoda  * Originally written by Julian Elischer (julian@tfs.com)
405009ff6dSsoda  * for TRW Financial Systems for use under the MACH(2.5) operating system.
415009ff6dSsoda  *
425009ff6dSsoda  * TRW Financial Systems, in accordance with their agreement with Carnegie
435009ff6dSsoda  * Mellon University, makes this software available to CMU to distribute
445009ff6dSsoda  * or use in any manner that they see fit as long as this message is kept with
455009ff6dSsoda  * the software. For this reason TFS also grants any other persons or
465009ff6dSsoda  * organisations permission to use or modify this software.
475009ff6dSsoda  *
485009ff6dSsoda  * TFS supplies this software to be publicly redistributed
495009ff6dSsoda  * on the understanding that TFS is not responsible for the correct
505009ff6dSsoda  * functioning of this software in any circumstances.
515009ff6dSsoda  */
525009ff6dSsoda 
53a4183603Slukem #include <sys/cdefs.h>
54*8ff6f65dSthorpej __KERNEL_RCSID(0, "$NetBSD: btl.c,v 1.31 2023/12/20 06:36:02 thorpej Exp $");
55a4183603Slukem 
565009ff6dSsoda #include <sys/types.h>
575009ff6dSsoda #include <sys/param.h>
585009ff6dSsoda #include <sys/systm.h>
595009ff6dSsoda #include <sys/kernel.h>
605009ff6dSsoda #include <sys/errno.h>
615009ff6dSsoda #include <sys/ioctl.h>
625009ff6dSsoda #include <sys/device.h>
635009ff6dSsoda #include <sys/buf.h>
645009ff6dSsoda #include <sys/proc.h>
655009ff6dSsoda 
665009ff6dSsoda #include <machine/intr.h>
675009ff6dSsoda #include <machine/pio.h>
685009ff6dSsoda 
695009ff6dSsoda #include <arc/dti/desktech.h>
705009ff6dSsoda 
71564df9b6Ssoda #include <dev/scsipi/scsi_all.h>
72564df9b6Ssoda #include <dev/scsipi/scsipi_all.h>
73564df9b6Ssoda #include <dev/scsipi/scsiconf.h>
745009ff6dSsoda 
755009ff6dSsoda #include <dev/isa/isavar.h>
765009ff6dSsoda #include <arc/dti/btlreg.h>
77ceabf327Ssoda #include <arc/dti/btlvar.h>
785009ff6dSsoda 
795009ff6dSsoda #ifndef DDB
805009ff6dSsoda #define Debugger() panic("should call debugger here (bt742a.c)")
815009ff6dSsoda #endif /* ! DDB */
825009ff6dSsoda 
835009ff6dSsoda /*
845009ff6dSsoda  * Mail box defs  etc.
855009ff6dSsoda  * these could be bigger but we need the bt_softc to fit on a single page..
865009ff6dSsoda  */
875009ff6dSsoda #define BT_MBX_SIZE	32	/* mail box size  (MAX 255 MBxs) */
885009ff6dSsoda 				/* don't need that many really */
895009ff6dSsoda #define BT_CCB_MAX	32	/* store up to 32 CCBs at one time */
905009ff6dSsoda #define	CCB_HASH_SIZE	32	/* hash table size for phystokv */
915009ff6dSsoda #define	CCB_HASH_SHIFT	9
925009ff6dSsoda #define CCB_HASH(x)	((((long)(x))>>CCB_HASH_SHIFT) & (CCB_HASH_SIZE - 1))
935009ff6dSsoda 
945009ff6dSsoda #define bt_nextmbx(wmb, mbx, mbio) \
955009ff6dSsoda 	if ((wmb) == &(mbx)->mbio[BT_MBX_SIZE - 1])	\
965009ff6dSsoda 		(wmb) = &(mbx)->mbio[0];		\
975009ff6dSsoda 	else						\
985009ff6dSsoda 		(wmb)++;
995009ff6dSsoda 
1005009ff6dSsoda struct bt_mbx {
1015009ff6dSsoda 	struct bt_mbx_out mbo[BT_MBX_SIZE];
1025009ff6dSsoda 	struct bt_mbx_in mbi[BT_MBX_SIZE];
1035009ff6dSsoda 	struct bt_mbx_out *cmbo;	/* Collection Mail Box out */
1045009ff6dSsoda 	struct bt_mbx_out *tmbo;	/* Target Mail Box out */
1055009ff6dSsoda 	struct bt_mbx_in *tmbi;		/* Target Mail Box in */
1065009ff6dSsoda };
1075009ff6dSsoda 
108ceabf327Ssoda #define KVTOPHYS(x)	(*btl_conf->bc_kvtophys)((int)(x))
109ceabf327Ssoda #define PHYSTOKV(x)	(*btl_conf->bc_phystokv)((int)(x))
1105009ff6dSsoda 
1115009ff6dSsoda struct bt_softc {
1120f31d9deStsutsui 	device_t sc_dev;
1135009ff6dSsoda 	void *sc_ih;
1145009ff6dSsoda 
1155009ff6dSsoda 	int sc_iobase;
1165009ff6dSsoda 	int sc_irq, sc_drq;
1175009ff6dSsoda 
1185009ff6dSsoda 	char sc_model[7],
1195009ff6dSsoda 	     sc_firmware[6];
1205009ff6dSsoda 
1215009ff6dSsoda 	struct bt_mbx *sc_mbx;		/* all our mailboxes */
1225009ff6dSsoda #define	wmbx	(sc->sc_mbx)
1235009ff6dSsoda 	struct bt_ccb *sc_ccbhash[CCB_HASH_SIZE];
1245009ff6dSsoda 	TAILQ_HEAD(, bt_ccb) sc_free_ccb, sc_waiting_ccb;
1255009ff6dSsoda 	TAILQ_HEAD(, bt_buf) sc_free_buf;
1265009ff6dSsoda 	int sc_numccbs, sc_mbofull;
1275009ff6dSsoda 	int sc_numbufs;
1285009ff6dSsoda 	int sc_scsi_dev;		/* adapters scsi id */
129564df9b6Ssoda 	struct scsipi_link sc_link;	/* prototype for devs */
130564df9b6Ssoda 	struct scsipi_adapter sc_adapter;
1315009ff6dSsoda };
1325009ff6dSsoda 
1335009ff6dSsoda #ifdef BTDEBUG
1345009ff6dSsoda int     bt_debug = 0;
1355009ff6dSsoda #endif /* BTDEBUG */
1365009ff6dSsoda 
1377fe2a5a0Stsutsui int bt_cmd(int, struct bt_softc *, int, u_char *, int, u_char *);
1387fe2a5a0Stsutsui integrate void bt_finish_ccbs(struct bt_softc *);
1397fe2a5a0Stsutsui int btintr(void *);
1407fe2a5a0Stsutsui integrate void bt_reset_ccb(struct bt_softc *, struct bt_ccb *);
1417fe2a5a0Stsutsui void bt_free_ccb(struct bt_softc *, struct bt_ccb *);
1427fe2a5a0Stsutsui integrate void bt_init_ccb(struct bt_softc *, struct bt_ccb *);
1437fe2a5a0Stsutsui struct bt_ccb *bt_get_ccb(struct bt_softc *, int);
1447fe2a5a0Stsutsui struct bt_ccb *bt_ccb_phys_kv(struct bt_softc *, u_long);
1457fe2a5a0Stsutsui void bt_queue_ccb(struct bt_softc *, struct bt_ccb *);
1467fe2a5a0Stsutsui void bt_collect_mbo(struct bt_softc *);
1477fe2a5a0Stsutsui void bt_start_ccbs(struct bt_softc *);
1487fe2a5a0Stsutsui void bt_done(struct bt_softc *, struct bt_ccb *);
1497fe2a5a0Stsutsui int bt_find(struct isa_attach_args *, struct bt_softc *);
1507fe2a5a0Stsutsui void bt_init(struct bt_softc *);
1517fe2a5a0Stsutsui void bt_inquire_setup_information(struct bt_softc *);
1527fe2a5a0Stsutsui void btminphys(struct buf *);
1537fe2a5a0Stsutsui int bt_scsi_cmd(struct scsipi_xfer *);
1547fe2a5a0Stsutsui int bt_poll(struct bt_softc *, struct scsipi_xfer *, int);
1557fe2a5a0Stsutsui void bt_timeout(void *arg);
1567fe2a5a0Stsutsui void bt_free_buf(struct bt_softc *, struct bt_buf *);
1577fe2a5a0Stsutsui struct bt_buf * bt_get_buf(struct bt_softc *, int);
1585009ff6dSsoda 
1595009ff6dSsoda /* the below structure is so we have a default dev struct for out link struct */
160564df9b6Ssoda struct scsipi_device bt_dev = {
1615009ff6dSsoda 	NULL,			/* Use default error handler */
1625009ff6dSsoda 	NULL,			/* have a queue, served by this */
1635009ff6dSsoda 	NULL,			/* have no async handler */
1645009ff6dSsoda 	NULL,			/* Use default 'done' routine */
1655009ff6dSsoda };
1665009ff6dSsoda 
1670f31d9deStsutsui static int	btprobe(device_t, cfdata_t, void *);
1680f31d9deStsutsui static void	btattach(device_t, device_t, void *);
1695009ff6dSsoda 
1700f31d9deStsutsui CFATTACH_DECL_NEW(btl, sizeof(struct bt_softc),
171c5e91d44Sthorpej     btprobe, btattach, NULL, NULL);
1725009ff6dSsoda 
1735009ff6dSsoda #define BT_RESET_TIMEOUT	2000	/* time to wait for reset (mSec) */
1745009ff6dSsoda #define	BT_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
1755009ff6dSsoda 
176ceabf327Ssoda struct btl_config *btl_conf = NULL;
177ceabf327Ssoda 
1785009ff6dSsoda /*
1795009ff6dSsoda  * bt_cmd(iobase, sc, icnt, ibuf, ocnt, obuf)
1805009ff6dSsoda  *
1815009ff6dSsoda  * Activate Adapter command
1825009ff6dSsoda  *    icnt:   number of args (outbound bytes including opcode)
1835009ff6dSsoda  *    ibuf:   argument buffer
1845009ff6dSsoda  *    ocnt:   number of expected returned bytes
1855009ff6dSsoda  *    obuf:   result buffer
1865009ff6dSsoda  *    wait:   number of seconds to wait for response
1875009ff6dSsoda  *
1885009ff6dSsoda  * Performs an adapter command through the ports.  Not to be confused with a
1891ffa7b76Swiz  * scsi command, which is read in via the DMA; one of the adapter commands
1905009ff6dSsoda  * tells it to read in a scsi command.
1915009ff6dSsoda  */
1925009ff6dSsoda int
bt_cmd(int iobase,struct bt_softc * sc,int icnt,int ocnt,u_char * ibuf,u_char * obuf)1937fe2a5a0Stsutsui bt_cmd(int iobase, struct bt_softc *sc, int icnt, int ocnt, u_char *ibuf,
1947fe2a5a0Stsutsui     u_char *obuf)
1955009ff6dSsoda {
1965009ff6dSsoda 	const char *name;
1978e19dfb2Stsutsui 	int i;
1985009ff6dSsoda 	int wait;
1995009ff6dSsoda 	u_char sts;
2005009ff6dSsoda 	u_char opcode = ibuf[0];
2015009ff6dSsoda 
2025009ff6dSsoda 	if (sc != NULL)
2030f31d9deStsutsui 		name = device_xname(sc->sc_dev);
2045009ff6dSsoda 	else
2055009ff6dSsoda 		name = "(bt probe)";
2065009ff6dSsoda 
2075009ff6dSsoda 	/*
2085009ff6dSsoda 	 * Calculate a reasonable timeout for the command.
2095009ff6dSsoda 	 */
2105009ff6dSsoda 	switch (opcode) {
2115009ff6dSsoda 	case BT_INQUIRE_DEVICES:
2125009ff6dSsoda 		wait = 15 * 20000;
2135009ff6dSsoda 		break;
2145009ff6dSsoda 	default:
2155009ff6dSsoda 		wait = 1 * 20000;
2165009ff6dSsoda 		break;
2175009ff6dSsoda 	}
2185009ff6dSsoda 
2195009ff6dSsoda 	/*
2205009ff6dSsoda 	 * Wait for the adapter to go idle, unless it's one of
2215009ff6dSsoda 	 * the commands which don't need this
2225009ff6dSsoda 	 */
2235009ff6dSsoda 	if (opcode != BT_MBO_INTR_EN) {
2245009ff6dSsoda 		for (i = 20000; i; i--) {	/* 1 sec? */
2255009ff6dSsoda 			sts = isa_inb(iobase + BT_STAT_PORT);
2265009ff6dSsoda 			if (sts & BT_STAT_IDLE)
2275009ff6dSsoda 				break;
2285009ff6dSsoda 			delay(50);
2295009ff6dSsoda 		}
2305009ff6dSsoda 		if (!i) {
2315009ff6dSsoda 			printf("%s: bt_cmd, host not idle(0x%x)\n",
2325009ff6dSsoda 			    name, sts);
2335009ff6dSsoda 			return ENXIO;
2345009ff6dSsoda 		}
2355009ff6dSsoda 	}
2365009ff6dSsoda 	/*
2375009ff6dSsoda 	 * Now that it is idle, if we expect output, preflush the
2385009ff6dSsoda 	 * queue feeding to us.
2395009ff6dSsoda 	 */
2405009ff6dSsoda 	if (ocnt) {
2415009ff6dSsoda 		while ((isa_inb(iobase + BT_STAT_PORT)) & BT_STAT_DF)
2425009ff6dSsoda 			isa_inb(iobase + BT_DATA_PORT);
2435009ff6dSsoda 	}
2445009ff6dSsoda 	/*
2455009ff6dSsoda 	 * Output the command and the number of arguments given
2465009ff6dSsoda 	 * for each byte, first check the port is empty.
2475009ff6dSsoda 	 */
2485009ff6dSsoda 	while (icnt--) {
2495009ff6dSsoda 		for (i = wait; i; i--) {
2505009ff6dSsoda 			sts = isa_inb(iobase + BT_STAT_PORT);
2515009ff6dSsoda 			if (!(sts & BT_STAT_CDF))
2525009ff6dSsoda 				break;
2535009ff6dSsoda 			delay(50);
2545009ff6dSsoda 		}
2555009ff6dSsoda 		if (!i) {
2565009ff6dSsoda 			if (opcode != BT_INQUIRE_REVISION &&
2575009ff6dSsoda 			    opcode != BT_INQUIRE_REVISION_3)
2585009ff6dSsoda 				printf("%s: bt_cmd, cmd/data port full\n", name);
2595009ff6dSsoda 			isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_SRST);
2605009ff6dSsoda 			return ENXIO;
2615009ff6dSsoda 		}
2625009ff6dSsoda 		isa_outb(iobase + BT_CMD_PORT, *ibuf++);
2635009ff6dSsoda 	}
2645009ff6dSsoda 	/*
2655009ff6dSsoda 	 * If we expect input, loop that many times, each time,
2665009ff6dSsoda 	 * looking for the data register to have valid data
2675009ff6dSsoda 	 */
2685009ff6dSsoda 	while (ocnt--) {
2695009ff6dSsoda 		for (i = wait; i; i--) {
2705009ff6dSsoda 			sts = isa_inb(iobase + BT_STAT_PORT);
2715009ff6dSsoda 			if (sts & BT_STAT_DF)
2725009ff6dSsoda 				break;
2735009ff6dSsoda 			delay(50);
2745009ff6dSsoda 		}
2755009ff6dSsoda 		if (!i) {
2765009ff6dSsoda 			if (opcode != BT_INQUIRE_REVISION &&
2775009ff6dSsoda 			    opcode != BT_INQUIRE_REVISION_3)
2785009ff6dSsoda 				printf("%s: bt_cmd, cmd/data port empty %d\n",
2795009ff6dSsoda 				    name, ocnt);
2805009ff6dSsoda 			isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_SRST);
2815009ff6dSsoda 			return ENXIO;
2825009ff6dSsoda 		}
2835009ff6dSsoda 		*obuf++ = isa_inb(iobase + BT_DATA_PORT);
2845009ff6dSsoda 	}
2855009ff6dSsoda 	/*
2865009ff6dSsoda 	 * Wait for the board to report a finished instruction.
2875009ff6dSsoda 	 * We may get an extra interrupt for the HACC signal, but this is
2885009ff6dSsoda 	 * unimportant.
2895009ff6dSsoda 	 */
2905009ff6dSsoda 	if (opcode != BT_MBO_INTR_EN) {
2915009ff6dSsoda 		for (i = 20000; i; i--) {	/* 1 sec? */
2925009ff6dSsoda 			sts = isa_inb(iobase + BT_INTR_PORT);
2935009ff6dSsoda 			/* XXX Need to save this in the interrupt handler? */
2945009ff6dSsoda 			if (sts & BT_INTR_HACC)
2955009ff6dSsoda 				break;
2965009ff6dSsoda 			delay(50);
2975009ff6dSsoda 		}
2985009ff6dSsoda 		if (!i) {
2995009ff6dSsoda 			printf("%s: bt_cmd, host not finished(0x%x)\n",
3005009ff6dSsoda 			    name, sts);
3015009ff6dSsoda 			return ENXIO;
3025009ff6dSsoda 		}
3035009ff6dSsoda 	}
3045009ff6dSsoda 	isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_IRST);
3055009ff6dSsoda 	return 0;
3065009ff6dSsoda }
3075009ff6dSsoda 
3085009ff6dSsoda /*
3095009ff6dSsoda  * Check if the device can be found at the port given
3105009ff6dSsoda  * and if so, set it up ready for further work
3115009ff6dSsoda  * as an argument, takes the isa_device structure from
3125009ff6dSsoda  * autoconf.c
3135009ff6dSsoda  */
3140f31d9deStsutsui static int
btprobe(device_t parent,cfdata_t cf,void * aux)3150f31d9deStsutsui btprobe(device_t parent, cfdata_t cf, void *aux)
3165009ff6dSsoda {
3178e19dfb2Stsutsui 	struct isa_attach_args *ia = aux;
3185009ff6dSsoda 
3195009ff6dSsoda #ifdef NEWCONFIG
3205009ff6dSsoda 	if (ia->ia_iobase == IOBASEUNK)
3215009ff6dSsoda 		return 0;
3225009ff6dSsoda #endif
3235009ff6dSsoda 
324ceabf327Ssoda 	if (btl_conf == NULL)
325ceabf327Ssoda 		return (0);
326ceabf327Ssoda 
3275009ff6dSsoda 	/* See if there is a unit at this location. */
3285009ff6dSsoda 	if (bt_find(ia, NULL) != 0)
3295009ff6dSsoda 		return 0;
3305009ff6dSsoda 
3315009ff6dSsoda 	ia->ia_msize = 0;
3325009ff6dSsoda 	ia->ia_iosize = 4;
3335009ff6dSsoda 	/* IRQ and DRQ set by bt_find(). */
3345009ff6dSsoda 	return 1;
3355009ff6dSsoda }
3365009ff6dSsoda 
3375009ff6dSsoda /*
3385009ff6dSsoda  * Attach all the sub-devices we can find
3395009ff6dSsoda  */
3400f31d9deStsutsui static void
btattach(device_t parent,device_t self,void * aux)3410f31d9deStsutsui btattach(device_t parent, device_t self, void *aux)
3425009ff6dSsoda {
3435009ff6dSsoda 	struct isa_attach_args *ia = aux;
3440f31d9deStsutsui 	struct bt_softc *sc = device_private(self);
3455009ff6dSsoda 	struct bt_ccb *ccb;
3465009ff6dSsoda 	struct bt_buf *buf;
3475009ff6dSsoda 	u_int bouncearea;
3485009ff6dSsoda 	u_int bouncebase;
3495009ff6dSsoda 	u_int bouncesize;
3505009ff6dSsoda 
3510f31d9deStsutsui 	sc->sc_dev = self;
3520f31d9deStsutsui 
3535009ff6dSsoda 	if (bt_find(ia, sc) != 0)
354cbab9cadSchs 		panic("btattach: bt_find of %s failed", device_xname(self));
3555009ff6dSsoda 	sc->sc_iobase = ia->ia_iobase;
3565009ff6dSsoda 
3575009ff6dSsoda 	/*
3585009ff6dSsoda 	 * create mbox area
3595009ff6dSsoda 	 */
360ceabf327Ssoda 	(*btl_conf->bc_bouncemem)(&bouncebase, &bouncesize);
3615009ff6dSsoda 	bouncearea = bouncebase + sizeof(struct bt_mbx);
3625009ff6dSsoda 	sc->sc_mbx = (struct bt_mbx *)bouncebase;
3635009ff6dSsoda 
3645009ff6dSsoda 	bt_inquire_setup_information(sc);
3655009ff6dSsoda 	bt_init(sc);
3665009ff6dSsoda 	TAILQ_INIT(&sc->sc_free_ccb);
3675009ff6dSsoda 	TAILQ_INIT(&sc->sc_free_buf);
3685009ff6dSsoda 	TAILQ_INIT(&sc->sc_waiting_ccb);
3695009ff6dSsoda 
3705009ff6dSsoda 	/*
3715009ff6dSsoda 	 * fill up with ccb's
3725009ff6dSsoda 	 */
3735009ff6dSsoda 	while (sc->sc_numccbs < BT_CCB_MAX) {
3745009ff6dSsoda 		ccb = (struct bt_ccb *)bouncearea;
3755009ff6dSsoda 		bouncearea +=  sizeof(struct bt_ccb);
3765009ff6dSsoda 		bt_init_ccb(sc, ccb);
3775009ff6dSsoda 		TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
3785009ff6dSsoda 		sc->sc_numccbs++;
3795009ff6dSsoda 	}
3805009ff6dSsoda 	/*
3815009ff6dSsoda 	 * fill up with bufs's
3825009ff6dSsoda 	 */
3835009ff6dSsoda 	while ((bouncearea + sizeof(struct bt_buf)) < bouncebase + bouncesize) {
3845009ff6dSsoda 		buf = (struct bt_buf *)bouncearea;
3855009ff6dSsoda 		bouncearea +=  sizeof(struct bt_buf);
3865009ff6dSsoda 		TAILQ_INSERT_HEAD(&sc->sc_free_buf, buf, chain);
3875009ff6dSsoda 		sc->sc_numbufs++;
3885009ff6dSsoda 	}
3895009ff6dSsoda 	/*
390564df9b6Ssoda 	 * Fill in the adapter.
3915009ff6dSsoda 	 */
392564df9b6Ssoda 	sc->sc_adapter.scsipi_cmd = bt_scsi_cmd;
393564df9b6Ssoda 	sc->sc_adapter.scsipi_minphys = btminphys;
394564df9b6Ssoda 	/*
395564df9b6Ssoda 	 * fill in the prototype scsipi_link.
396564df9b6Ssoda 	 */
397564df9b6Ssoda 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
3985009ff6dSsoda 	sc->sc_link.adapter_softc = sc;
399564df9b6Ssoda 	sc->sc_link.scsipi_scsi.adapter_target = sc->sc_scsi_dev;
400564df9b6Ssoda 	sc->sc_link.adapter = &sc->sc_adapter;
4015009ff6dSsoda 	sc->sc_link.device = &bt_dev;
4025009ff6dSsoda 	sc->sc_link.openings = 1;
403564df9b6Ssoda 	sc->sc_link.scsipi_scsi.max_target = 7;
404564df9b6Ssoda 	sc->sc_link.scsipi_scsi.max_lun = 7;
405564df9b6Ssoda 	sc->sc_link.type = BUS_SCSI;
4065009ff6dSsoda 
4075009ff6dSsoda 	sc->sc_ih = isa_intr_establish(ia->ia_ic, sc->sc_irq, IST_EDGE,
408564df9b6Ssoda 	    IPL_BIO, btintr, sc);
4095009ff6dSsoda 
4105009ff6dSsoda 	/*
4115009ff6dSsoda 	 * ask the adapter what subunits are present
4125009ff6dSsoda 	 */
413c7fb772bSthorpej 	config_found(self, &sc->sc_link, scsiprint, CFARGS_NONE);
4145009ff6dSsoda }
4155009ff6dSsoda 
4165009ff6dSsoda integrate void
bt_finish_ccbs(struct bt_softc * sc)4177fe2a5a0Stsutsui bt_finish_ccbs(struct bt_softc *sc)
4185009ff6dSsoda {
4195009ff6dSsoda 	struct bt_mbx_in *wmbi;
4205009ff6dSsoda 	struct bt_ccb *ccb;
4215009ff6dSsoda 	int i;
4225009ff6dSsoda 
4235009ff6dSsoda 	wmbi = wmbx->tmbi;
4245009ff6dSsoda 
4255009ff6dSsoda 	if (wmbi->stat == BT_MBI_FREE) {
4265009ff6dSsoda 		for (i = 0; i < BT_MBX_SIZE; i++) {
4275009ff6dSsoda 			if (wmbi->stat != BT_MBI_FREE) {
4285009ff6dSsoda 				printf("%s: mbi not in round-robin order\n",
4290f31d9deStsutsui 				    device_xname(sc->sc_dev));
4305009ff6dSsoda 				goto AGAIN;
4315009ff6dSsoda 			}
4325009ff6dSsoda 			bt_nextmbx(wmbi, wmbx, mbi);
4335009ff6dSsoda 		}
4345009ff6dSsoda #ifdef BTDIAGnot
4355009ff6dSsoda 		printf("%s: mbi interrupt with no full mailboxes\n",
4360f31d9deStsutsui 		    device_xname(sc->sc_dev));
4375009ff6dSsoda #endif
4385009ff6dSsoda 		return;
4395009ff6dSsoda 	}
4405009ff6dSsoda 
4415009ff6dSsoda AGAIN:
4425009ff6dSsoda 	do {
4435009ff6dSsoda 		ccb = bt_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
4445009ff6dSsoda 		if (!ccb) {
4455009ff6dSsoda 			printf("%s: bad mbi ccb pointer; skipping\n",
4460f31d9deStsutsui 			    device_xname(sc->sc_dev));
4475009ff6dSsoda 			goto next;
4485009ff6dSsoda 		}
4495009ff6dSsoda 
4505009ff6dSsoda #ifdef BTDEBUG
4515009ff6dSsoda 		if (bt_debug) {
4525009ff6dSsoda 			u_char *cp = (u_char *) &ccb->scsi_cmd;
4535009ff6dSsoda 			printf("op=%x %x %x %x %x %x\n",
4545009ff6dSsoda 			    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
4555009ff6dSsoda 			printf("stat %x for mbi addr = 0x%08x, ",
4565009ff6dSsoda 			    wmbi->stat, wmbi);
4575009ff6dSsoda 			printf("ccb addr = 0x%x\n", ccb);
4585009ff6dSsoda 		}
4595009ff6dSsoda #endif /* BTDEBUG */
4605009ff6dSsoda 
4615009ff6dSsoda 		switch (wmbi->stat) {
4625009ff6dSsoda 		case BT_MBI_OK:
4635009ff6dSsoda 		case BT_MBI_ERROR:
4645009ff6dSsoda 			if ((ccb->flags & CCB_ABORT) != 0) {
4655009ff6dSsoda 				/*
4665009ff6dSsoda 				 * If we already started an abort, wait for it
4675009ff6dSsoda 				 * to complete before clearing the CCB.  We
4685009ff6dSsoda 				 * could instead just clear CCB_SENDING, but
4695009ff6dSsoda 				 * what if the mailbox was already received?
4705009ff6dSsoda 				 * The worst that happens here is that we clear
4715009ff6dSsoda 				 * the CCB a bit later than we need to.  BFD.
4725009ff6dSsoda 				 */
4735009ff6dSsoda 				goto next;
4745009ff6dSsoda 			}
4755009ff6dSsoda 			break;
4765009ff6dSsoda 
4775009ff6dSsoda 		case BT_MBI_ABORT:
4785009ff6dSsoda 		case BT_MBI_UNKNOWN:
4795009ff6dSsoda 			/*
4805009ff6dSsoda 			 * Even if the CCB wasn't found, we clear it anyway.
4811e378c4cSwiz 			 * See preceding comment.
4825009ff6dSsoda 			 */
4835009ff6dSsoda 			break;
4845009ff6dSsoda 
4855009ff6dSsoda 		default:
4865009ff6dSsoda 			printf("%s: bad mbi status %02x; skipping\n",
4870f31d9deStsutsui 			    device_xname(sc->sc_dev), wmbi->stat);
4885009ff6dSsoda 			goto next;
4895009ff6dSsoda 		}
4905009ff6dSsoda 
491b667a5a3Sthorpej 		callout_stop(&ccb->xs->xs_callout);
4925009ff6dSsoda 		bt_done(sc, ccb);
4935009ff6dSsoda 
4945009ff6dSsoda 	next:
4955009ff6dSsoda 		wmbi->stat = BT_MBI_FREE;
4965009ff6dSsoda 		bt_nextmbx(wmbi, wmbx, mbi);
4975009ff6dSsoda 	} while (wmbi->stat != BT_MBI_FREE);
4985009ff6dSsoda 
4995009ff6dSsoda 	wmbx->tmbi = wmbi;
5005009ff6dSsoda }
5015009ff6dSsoda 
5025009ff6dSsoda /*
5035009ff6dSsoda  * Catch an interrupt from the adaptor
5045009ff6dSsoda  */
5055009ff6dSsoda int
btintr(void * arg)5067fe2a5a0Stsutsui btintr(void *arg)
5075009ff6dSsoda {
5085009ff6dSsoda 	struct bt_softc *sc = arg;
5095009ff6dSsoda 	int iobase = sc->sc_iobase;
5105009ff6dSsoda 	u_char sts;
5115009ff6dSsoda 
5125009ff6dSsoda #ifdef BTDEBUG
5130f31d9deStsutsui 	printf("%s: btintr ", device_xname(sc->sc_dev));
5145009ff6dSsoda #endif /* BTDEBUG */
5155009ff6dSsoda 
5165009ff6dSsoda 	/*
51721cc7f1bSmaya 	 * First acknowledge the interrupt, Then if it's not telling about
5185009ff6dSsoda 	 * a completed operation just return.
5195009ff6dSsoda 	 */
5205009ff6dSsoda 	sts = isa_inb(iobase + BT_INTR_PORT);
5215009ff6dSsoda 	if ((sts & BT_INTR_ANYINTR) == 0)
5225009ff6dSsoda 		return 0;
5235009ff6dSsoda 	isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_IRST);
5245009ff6dSsoda 
5255009ff6dSsoda #ifdef BTDIAG
5265009ff6dSsoda 	/* Make sure we clear CCB_SENDING before finishing a CCB. */
5275009ff6dSsoda 	bt_collect_mbo(sc);
5285009ff6dSsoda #endif
5295009ff6dSsoda 
5305009ff6dSsoda 	/* Mail box out empty? */
5315009ff6dSsoda 	if (sts & BT_INTR_MBOA) {
5325009ff6dSsoda 		struct bt_toggle toggle;
5335009ff6dSsoda 
5345009ff6dSsoda 		toggle.cmd.opcode = BT_MBO_INTR_EN;
5355009ff6dSsoda 		toggle.cmd.enable = 0;
5365009ff6dSsoda 		bt_cmd(iobase, sc, sizeof(toggle.cmd), (u_char *)&toggle.cmd, 0,
5375009ff6dSsoda 		    (u_char *)0);
5385009ff6dSsoda 		bt_start_ccbs(sc);
5395009ff6dSsoda 	}
5405009ff6dSsoda 
5415009ff6dSsoda 	/* Mail box in full? */
5425009ff6dSsoda 	if (sts & BT_INTR_MBIF)
5435009ff6dSsoda 		bt_finish_ccbs(sc);
5445009ff6dSsoda 
5455009ff6dSsoda 	return 1;
5465009ff6dSsoda }
5475009ff6dSsoda 
5485009ff6dSsoda integrate void
bt_reset_ccb(struct bt_softc * sc,struct bt_ccb * ccb)5497fe2a5a0Stsutsui bt_reset_ccb(struct bt_softc *sc, struct bt_ccb *ccb)
5505009ff6dSsoda {
5515009ff6dSsoda 
5525009ff6dSsoda 	ccb->flags = 0;
5535009ff6dSsoda }
5545009ff6dSsoda 
5555009ff6dSsoda /*
5565009ff6dSsoda  * A ccb is put onto the free list.
5575009ff6dSsoda  */
5585009ff6dSsoda void
bt_free_ccb(struct bt_softc * sc,struct bt_ccb * ccb)5597fe2a5a0Stsutsui bt_free_ccb(struct bt_softc *sc, struct bt_ccb *ccb)
5605009ff6dSsoda {
5615009ff6dSsoda 	int s;
5625009ff6dSsoda 
5635009ff6dSsoda 	s = splbio();
5645009ff6dSsoda 
5655009ff6dSsoda 	bt_reset_ccb(sc, ccb);
5665009ff6dSsoda 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
5675009ff6dSsoda 
5685009ff6dSsoda 	/*
5695009ff6dSsoda 	 * If there were none, wake anybody waiting for one to come free,
5705009ff6dSsoda 	 * starting with queued entries.
5715009ff6dSsoda 	 */
5725009ff6dSsoda 	if (ccb->chain.tqe_next == 0)
5735009ff6dSsoda 		wakeup(&sc->sc_free_ccb);
5745009ff6dSsoda 
5755009ff6dSsoda 	splx(s);
5765009ff6dSsoda }
5775009ff6dSsoda 
5785009ff6dSsoda /*
5795009ff6dSsoda  * A buf is put onto the free list.
5805009ff6dSsoda  */
5815009ff6dSsoda void
bt_free_buf(struct bt_softc * sc,struct bt_buf * buf)5827fe2a5a0Stsutsui bt_free_buf(struct bt_softc *sc, struct bt_buf *buf)
5835009ff6dSsoda {
5845009ff6dSsoda 	int s;
5855009ff6dSsoda 
5865009ff6dSsoda 	s = splbio();
5875009ff6dSsoda 
5885009ff6dSsoda 	TAILQ_INSERT_HEAD(&sc->sc_free_buf, buf, chain);
5895009ff6dSsoda 	sc->sc_numbufs++;
5905009ff6dSsoda 
5915009ff6dSsoda 	/*
5925009ff6dSsoda 	 * If there were none, wake anybody waiting for one to come free,
5935009ff6dSsoda 	 * starting with queued entries.
5945009ff6dSsoda 	 */
5955009ff6dSsoda 	if (buf->chain.tqe_next == 0)
5965009ff6dSsoda 		wakeup(&sc->sc_free_buf);
5975009ff6dSsoda 
5985009ff6dSsoda 	splx(s);
5995009ff6dSsoda }
6005009ff6dSsoda 
6015009ff6dSsoda integrate void
bt_init_ccb(struct bt_softc * sc,struct bt_ccb * ccb)6027fe2a5a0Stsutsui bt_init_ccb(struct bt_softc *sc, struct bt_ccb *ccb)
6035009ff6dSsoda {
6045009ff6dSsoda 	int hashnum;
6055009ff6dSsoda 
60666f3c84bStsutsui 	memset(ccb, 0, sizeof(struct bt_ccb));
6075009ff6dSsoda 	/*
6085009ff6dSsoda 	 * put in the phystokv hash table
6095009ff6dSsoda 	 * Never gets taken out.
6105009ff6dSsoda 	 */
6115009ff6dSsoda 	ccb->hashkey = KVTOPHYS(ccb);
6125009ff6dSsoda 	hashnum = CCB_HASH(ccb->hashkey);
6135009ff6dSsoda 	ccb->nexthash = sc->sc_ccbhash[hashnum];
6145009ff6dSsoda 	sc->sc_ccbhash[hashnum] = ccb;
6155009ff6dSsoda 	bt_reset_ccb(sc, ccb);
6165009ff6dSsoda }
6175009ff6dSsoda 
6185009ff6dSsoda /*
6195009ff6dSsoda  * Get a free ccb
6205009ff6dSsoda  *
6215009ff6dSsoda  * If there are none, either return an error or sleep.
6225009ff6dSsoda  */
6235009ff6dSsoda struct bt_ccb *
bt_get_ccb(struct bt_softc * sc,int nosleep)6247fe2a5a0Stsutsui bt_get_ccb(struct bt_softc *sc, int nosleep)
6255009ff6dSsoda {
6265009ff6dSsoda 	struct bt_ccb *ccb;
6275009ff6dSsoda 	int s;
6285009ff6dSsoda 
6295009ff6dSsoda 	s = splbio();
6305009ff6dSsoda 
6315009ff6dSsoda 	/*
6325009ff6dSsoda 	 * If we can and have to, sleep waiting for one to come free.
6335009ff6dSsoda 	 */
6345009ff6dSsoda 	for (;;) {
6355009ff6dSsoda 		ccb = sc->sc_free_ccb.tqh_first;
6365009ff6dSsoda 		if (ccb) {
6375009ff6dSsoda 			TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
6385009ff6dSsoda 			break;
6395009ff6dSsoda 		}
640564df9b6Ssoda 		if (nosleep)
6415009ff6dSsoda 			goto out;
6425009ff6dSsoda 		tsleep(&sc->sc_free_ccb, PRIBIO, "btccb", 0);
6435009ff6dSsoda 	}
6445009ff6dSsoda 
6455009ff6dSsoda 	ccb->flags |= CCB_ALLOC;
6465009ff6dSsoda 
6475009ff6dSsoda out:
6485009ff6dSsoda 	splx(s);
6495009ff6dSsoda 	return ccb;
6505009ff6dSsoda }
6515009ff6dSsoda 
6525009ff6dSsoda /*
6535009ff6dSsoda  * Get a free buf
6545009ff6dSsoda  *
6555009ff6dSsoda  * If there are none, either return an error or sleep.
6565009ff6dSsoda  */
6575009ff6dSsoda struct bt_buf *
bt_get_buf(struct bt_softc * sc,int nosleep)6587fe2a5a0Stsutsui bt_get_buf(struct bt_softc *sc, int nosleep)
6595009ff6dSsoda {
6605009ff6dSsoda 	struct bt_buf *buf;
6615009ff6dSsoda 	int s;
6625009ff6dSsoda 
6635009ff6dSsoda 	s = splbio();
6645009ff6dSsoda 
6655009ff6dSsoda 	/*
6665009ff6dSsoda 	 * If we can and have to, sleep waiting for one to come free.
6675009ff6dSsoda 	 */
6685009ff6dSsoda 	for (;;) {
6695009ff6dSsoda 		buf = sc->sc_free_buf.tqh_first;
6705009ff6dSsoda 		if (buf) {
6715009ff6dSsoda 			TAILQ_REMOVE(&sc->sc_free_buf, buf, chain);
6725009ff6dSsoda 			sc->sc_numbufs--;
6735009ff6dSsoda 			break;
6745009ff6dSsoda 		}
675564df9b6Ssoda 		if (nosleep)
6765009ff6dSsoda 			goto out;
6775009ff6dSsoda 		tsleep(&sc->sc_free_buf, PRIBIO, "btbuf", 0);
6785009ff6dSsoda 	}
6795009ff6dSsoda 
6805009ff6dSsoda out:
6815009ff6dSsoda 	splx(s);
6825009ff6dSsoda 	return buf;
6835009ff6dSsoda }
6845009ff6dSsoda 
6855009ff6dSsoda /*
6865009ff6dSsoda  * Given a physical address, find the ccb that it corresponds to.
6875009ff6dSsoda  */
6885009ff6dSsoda struct bt_ccb *
bt_ccb_phys_kv(struct bt_softc * sc,u_long ccb_phys)6897fe2a5a0Stsutsui bt_ccb_phys_kv(struct bt_softc *sc, u_long ccb_phys)
6905009ff6dSsoda {
6915009ff6dSsoda 	int hashnum = CCB_HASH(ccb_phys);
6925009ff6dSsoda 	struct bt_ccb *ccb = sc->sc_ccbhash[hashnum];
6935009ff6dSsoda 
6945009ff6dSsoda 	while (ccb) {
6955009ff6dSsoda 		if (ccb->hashkey == ccb_phys)
6965009ff6dSsoda 			break;
6975009ff6dSsoda 		ccb = ccb->nexthash;
6985009ff6dSsoda 	}
6995009ff6dSsoda 	return ccb;
7005009ff6dSsoda }
7015009ff6dSsoda 
7025009ff6dSsoda /*
7035009ff6dSsoda  * Queue a CCB to be sent to the controller, and send it if possible.
7045009ff6dSsoda  */
7055009ff6dSsoda void
bt_queue_ccb(struct bt_softc * sc,struct bt_ccb * ccb)7067fe2a5a0Stsutsui bt_queue_ccb(struct bt_softc *sc, struct bt_ccb *ccb)
7075009ff6dSsoda {
7085009ff6dSsoda 
7095009ff6dSsoda 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
7105009ff6dSsoda 	bt_start_ccbs(sc);
7115009ff6dSsoda }
7125009ff6dSsoda 
7135009ff6dSsoda /*
7145009ff6dSsoda  * Garbage collect mailboxes that are no longer in use.
7155009ff6dSsoda  */
7165009ff6dSsoda void
bt_collect_mbo(struct bt_softc * sc)7177fe2a5a0Stsutsui bt_collect_mbo(struct bt_softc *sc)
7185009ff6dSsoda {
7195009ff6dSsoda 	struct bt_mbx_out *wmbo;	/* Mail Box Out pointer */
7205009ff6dSsoda 
7215009ff6dSsoda 	wmbo = wmbx->cmbo;
7225009ff6dSsoda 
7235009ff6dSsoda 	while (sc->sc_mbofull > 0) {
7245009ff6dSsoda 		if (wmbo->cmd != BT_MBO_FREE)
7255009ff6dSsoda 			break;
7265009ff6dSsoda 
7275009ff6dSsoda #ifdef BTDIAG
7285009ff6dSsoda 		ccb = bt_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
7295009ff6dSsoda 		ccb->flags &= ~CCB_SENDING;
7305009ff6dSsoda #endif
7315009ff6dSsoda 
7325009ff6dSsoda 		--sc->sc_mbofull;
7335009ff6dSsoda 		bt_nextmbx(wmbo, wmbx, mbo);
7345009ff6dSsoda 	}
7355009ff6dSsoda 
7365009ff6dSsoda 	wmbx->cmbo = wmbo;
7375009ff6dSsoda }
7385009ff6dSsoda 
7395009ff6dSsoda /*
7405009ff6dSsoda  * Send as many CCBs as we have empty mailboxes for.
7415009ff6dSsoda  */
7425009ff6dSsoda void
bt_start_ccbs(struct bt_softc * sc)7437fe2a5a0Stsutsui bt_start_ccbs(struct bt_softc *sc)
7445009ff6dSsoda {
7455009ff6dSsoda 	int iobase = sc->sc_iobase;
7465009ff6dSsoda 	struct bt_mbx_out *wmbo;	/* Mail Box Out pointer */
7475009ff6dSsoda 	struct bt_ccb *ccb;
7485009ff6dSsoda 
7495009ff6dSsoda 	wmbo = wmbx->tmbo;
7505009ff6dSsoda 
7515009ff6dSsoda 	while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
7525009ff6dSsoda 		if (sc->sc_mbofull >= BT_MBX_SIZE) {
7535009ff6dSsoda 			bt_collect_mbo(sc);
7545009ff6dSsoda 			if (sc->sc_mbofull >= BT_MBX_SIZE) {
7555009ff6dSsoda 				struct bt_toggle toggle;
7565009ff6dSsoda 
7575009ff6dSsoda 				toggle.cmd.opcode = BT_MBO_INTR_EN;
7585009ff6dSsoda 				toggle.cmd.enable = 1;
7595009ff6dSsoda 				bt_cmd(iobase, sc, sizeof(toggle.cmd),
7605009ff6dSsoda 				    (u_char *)&toggle.cmd, 0, (u_char *)0);
7615009ff6dSsoda 				break;
7625009ff6dSsoda 			}
7635009ff6dSsoda 		}
7645009ff6dSsoda 
7655009ff6dSsoda 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
7665009ff6dSsoda #ifdef BTDIAG
7675009ff6dSsoda 		ccb->flags |= CCB_SENDING;
7685009ff6dSsoda #endif
7695009ff6dSsoda 
7705009ff6dSsoda 		/* Link ccb to mbo. */
7715009ff6dSsoda 		ltophys(KVTOPHYS(ccb), wmbo->ccb_addr);
7725009ff6dSsoda 		if (ccb->flags & CCB_ABORT)
7735009ff6dSsoda 			wmbo->cmd = BT_MBO_ABORT;
7745009ff6dSsoda 		else
7755009ff6dSsoda 			wmbo->cmd = BT_MBO_START;
7765009ff6dSsoda 
7775009ff6dSsoda 		/* Tell the card to poll immediately. */
7785009ff6dSsoda 		isa_outb(iobase + BT_CMD_PORT, BT_START_SCSI);
7795009ff6dSsoda 
780564df9b6Ssoda 		if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
781b667a5a3Sthorpej 			callout_reset(&ccb->xs->xs_callout,
782e5727031Sbouyer 			    mstohz(ccb->timeout), bt_timeout, ccb);
7835009ff6dSsoda 
7845009ff6dSsoda 		++sc->sc_mbofull;
7855009ff6dSsoda 		bt_nextmbx(wmbo, wmbx, mbo);
7865009ff6dSsoda 	}
7875009ff6dSsoda 
7885009ff6dSsoda 	wmbx->tmbo = wmbo;
7895009ff6dSsoda }
7905009ff6dSsoda 
7915009ff6dSsoda /*
7925009ff6dSsoda  * We have a ccb which has been processed by the
7935009ff6dSsoda  * adaptor, now we look to see how the operation
7945009ff6dSsoda  * went. Wake up the owner if waiting
7955009ff6dSsoda  */
7965009ff6dSsoda void
bt_done(struct bt_softc * sc,struct bt_ccb * ccb)7977fe2a5a0Stsutsui bt_done(struct bt_softc *sc, struct bt_ccb *ccb)
7985009ff6dSsoda {
799df9803ceSthorpej 	struct scsi_sense_data *s1, *s2;
800564df9b6Ssoda 	struct scsipi_xfer *xs = ccb->xs;
8015009ff6dSsoda 
8025009ff6dSsoda 	u_long thiskv, thisbounce;
8035009ff6dSsoda 	int bytes_this_page, datalen;
8045009ff6dSsoda 	struct bt_scat_gath *sg;
8055009ff6dSsoda 	int seg;
8065009ff6dSsoda 
8075009ff6dSsoda 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("bt_done\n"));
8085009ff6dSsoda 	/*
8095009ff6dSsoda 	 * Otherwise, put the results of the operation
8105009ff6dSsoda 	 * into the xfer and call whoever started it
8115009ff6dSsoda 	 */
8125009ff6dSsoda #ifdef BTDIAG
8135009ff6dSsoda 	if (ccb->flags & CCB_SENDING) {
8140f31d9deStsutsui 		printf("%s: exiting ccb still in transit!\n",
8150f31d9deStsutsui 		    device_xname(sc->sc_dev));
8165009ff6dSsoda 		Debugger();
8175009ff6dSsoda 		return;
8185009ff6dSsoda 	}
8195009ff6dSsoda #endif
8205009ff6dSsoda 	if ((ccb->flags & CCB_ALLOC) == 0) {
8210f31d9deStsutsui 		printf("%s: exiting ccb not allocated!\n",
8220f31d9deStsutsui 		    device_xname(sc->sc_dev));
8235009ff6dSsoda 		Debugger();
8245009ff6dSsoda 		return;
8255009ff6dSsoda 	}
8265009ff6dSsoda 	if (xs->error == XS_NOERROR) {
8275009ff6dSsoda 		if (ccb->host_stat != BT_OK) {
8285009ff6dSsoda 			switch (ccb->host_stat) {
8295009ff6dSsoda 			case BT_SEL_TIMEOUT:	/* No response */
8305009ff6dSsoda 				xs->error = XS_SELTIMEOUT;
8315009ff6dSsoda 				break;
8325009ff6dSsoda 			default:	/* Other scsi protocol messes */
8335009ff6dSsoda 				printf("%s: host_stat %x\n",
8340f31d9deStsutsui 				    device_xname(sc->sc_dev), ccb->host_stat);
8355009ff6dSsoda 				xs->error = XS_DRIVER_STUFFUP;
8365009ff6dSsoda 				break;
8375009ff6dSsoda 			}
8385009ff6dSsoda 		} else if (ccb->target_stat != SCSI_OK) {
8395009ff6dSsoda 			switch (ccb->target_stat) {
8405009ff6dSsoda 			case SCSI_CHECK:
8415009ff6dSsoda 				s1 = &ccb->scsi_sense;
842564df9b6Ssoda 				s2 = &xs->sense.scsi_sense;
8435009ff6dSsoda 				*s2 = *s1;
8445009ff6dSsoda 				xs->error = XS_SENSE;
8455009ff6dSsoda 				break;
8465009ff6dSsoda 			case SCSI_BUSY:
8475009ff6dSsoda 				xs->error = XS_BUSY;
8485009ff6dSsoda 				break;
8495009ff6dSsoda 			default:
8505009ff6dSsoda 				printf("%s: target_stat %x\n",
8510f31d9deStsutsui 				    device_xname(sc->sc_dev), ccb->target_stat);
8525009ff6dSsoda 				xs->error = XS_DRIVER_STUFFUP;
8535009ff6dSsoda 				break;
8545009ff6dSsoda 			}
8555009ff6dSsoda 		} else
8565009ff6dSsoda 			xs->resid = 0;
8575009ff6dSsoda 	}
8585009ff6dSsoda 
8595009ff6dSsoda 	if((datalen = xs->datalen) != 0) {
8605009ff6dSsoda 		thiskv = (int)xs->data;
8615009ff6dSsoda 		sg = ccb->scat_gath;
8625009ff6dSsoda 		seg = phystol(ccb->data_length) / sizeof(struct bt_scat_gath);
8635009ff6dSsoda 
8645009ff6dSsoda 		while (seg) {
8655009ff6dSsoda 			thisbounce = PHYSTOKV(phystol(sg->seg_addr));
8665009ff6dSsoda 			bytes_this_page = phystol(sg->seg_len);
867564df9b6Ssoda 			if(xs->xs_control & XS_CTL_DATA_IN) {
868e2cb8590Scegger 				memcpy((void *)thiskv, (void *)thisbounce, bytes_this_page);
8695009ff6dSsoda 			}
8705009ff6dSsoda 			bt_free_buf(sc, (struct bt_buf *)thisbounce);
8715009ff6dSsoda 			thiskv += bytes_this_page;
8725009ff6dSsoda 			datalen -= bytes_this_page;
8735009ff6dSsoda 
8745009ff6dSsoda 			sg++;
8755009ff6dSsoda 			seg--;
8765009ff6dSsoda 		}
8775009ff6dSsoda 	}
8785009ff6dSsoda 
8795009ff6dSsoda 	bt_free_ccb(sc, ccb);
880564df9b6Ssoda 	xs->xs_status |= XS_STS_DONE;
881564df9b6Ssoda 	scsipi_done(xs);
8825009ff6dSsoda }
8835009ff6dSsoda 
8845009ff6dSsoda /*
885f0a7346dSsnj  * Find the board and find its irq/drq
8865009ff6dSsoda  */
8875009ff6dSsoda int
bt_find(struct isa_attach_args * ia,struct bt_softc * sc)8884c2c307cStsutsui bt_find(struct isa_attach_args *ia, struct bt_softc *sc)
8895009ff6dSsoda {
8905009ff6dSsoda 	int iobase = ia->ia_iobase;
8915009ff6dSsoda 	int i;
8925009ff6dSsoda 	u_char sts;
8935009ff6dSsoda 	struct bt_extended_inquire inquire;
8945009ff6dSsoda 	struct bt_config config;
8955009ff6dSsoda 	int irq, drq;
8965009ff6dSsoda 
897564df9b6Ssoda #ifndef notyet
898564df9b6Ssoda 	/* Check something is at the ports we need to access */
899564df9b6Ssoda 	sts = isa_inb(iobase + BHA_STAT_PORT);
900564df9b6Ssoda 	if (sts == 0xFF)
901564df9b6Ssoda 		return (0);
902564df9b6Ssoda #endif
903564df9b6Ssoda 
9045009ff6dSsoda 	/*
9055009ff6dSsoda 	 * reset board, If it doesn't respond, assume
9065009ff6dSsoda 	 * that it's not there.. good for the probe
9075009ff6dSsoda 	 */
9085009ff6dSsoda 
9095009ff6dSsoda 	isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_HRST | BT_CTRL_SRST);
9105009ff6dSsoda 
9115009ff6dSsoda 	delay(100);
9125009ff6dSsoda 	for (i = BT_RESET_TIMEOUT; i; i--) {
9135009ff6dSsoda 		sts = isa_inb(iobase + BT_STAT_PORT);
9145009ff6dSsoda 		if (sts == (BT_STAT_IDLE | BT_STAT_INIT))
9155009ff6dSsoda 			break;
9165009ff6dSsoda 		delay(1000);
9175009ff6dSsoda 	}
9185009ff6dSsoda 	if (!i) {
9195009ff6dSsoda #ifdef BTDEBUG
9205009ff6dSsoda 		if (bt_debug)
9215009ff6dSsoda 			printf("bt_find: No answer from buslogic board\n");
9225009ff6dSsoda #endif /* BTDEBUG */
9235009ff6dSsoda 		return 1;
9245009ff6dSsoda 	}
9255009ff6dSsoda 
926564df9b6Ssoda #ifndef notyet
927564df9b6Ssoda 	/*
928564df9b6Ssoda 	 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
929564df9b6Ssoda 	 * interface. The native bha interface is not compatible with
930564df9b6Ssoda 	 * an aha. 1542. We need to ensure that we never match an
931564df9b6Ssoda 	 * Adaptec 1542. We must also avoid sending Adaptec-compatible
932564df9b6Ssoda 	 * commands to a real bha, lest it go into 1542 emulation mode.
933564df9b6Ssoda 	 * (On an indirect bus like ISA, we should always probe for BusLogic
934564df9b6Ssoda 	 * interfaces before Adaptec interfaces).
935564df9b6Ssoda 	 */
936564df9b6Ssoda 
937564df9b6Ssoda 	/*
938564df9b6Ssoda 	 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
939564df9b6Ssoda 	 * for an extended-geometry register.  The 1542[AB] don't have one.
940564df9b6Ssoda 	 */
941564df9b6Ssoda 	sts = isa_inb(iobase +  BT_EXTGEOM_PORT);
942564df9b6Ssoda 	if (sts == 0xFF)
943564df9b6Ssoda 		return (0);
944564df9b6Ssoda #endif /* notyet */
945564df9b6Ssoda 
9465009ff6dSsoda 	/*
9475009ff6dSsoda 	 * Check that we actually know how to use this board.
9485009ff6dSsoda 	 */
9495009ff6dSsoda 	delay(1000);
95066f3c84bStsutsui 	memset(&inquire, 0, sizeof inquire);
9515009ff6dSsoda 	inquire.cmd.opcode = BT_INQUIRE_EXTENDED;
9525009ff6dSsoda 	inquire.cmd.len = sizeof(inquire.reply);
953564df9b6Ssoda 	i = bt_cmd(iobase, sc, sizeof(inquire.cmd), (u_char *)&inquire.cmd,
9545009ff6dSsoda 	    sizeof(inquire.reply), (u_char *)&inquire.reply);
955564df9b6Ssoda 
956564df9b6Ssoda #ifndef notyet
957564df9b6Ssoda 	/*
958564df9b6Ssoda 	 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
959564df9b6Ssoda 	 * have the extended-geometry register and also respond to
960564df9b6Ssoda 	 * BHA_INQUIRE_EXTENDED.  Make sure we never match such cards,
961564df9b6Ssoda 	 * by checking the size of the reply is what a BusLogic card returns.
962564df9b6Ssoda 	 */
963564df9b6Ssoda 	if (i) { /* XXX - this doesn't really check the size. ??? see bha.c */
964564df9b6Ssoda #ifdef BTDEBUG
965564df9b6Ssoda 		printf("bt_find: board returned %d instead of %d to %s\n",
966564df9b6Ssoda 		       i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
967564df9b6Ssoda #endif
968564df9b6Ssoda 		return (0);
969564df9b6Ssoda 	}
970564df9b6Ssoda 
971564df9b6Ssoda 	/* OK, we know we've found a buslogic adaptor. */
972564df9b6Ssoda #endif /* notyet */
973564df9b6Ssoda 
9745009ff6dSsoda 	switch (inquire.reply.bus_type) {
9755009ff6dSsoda 	case BT_BUS_TYPE_24BIT:
9765009ff6dSsoda 	case BT_BUS_TYPE_32BIT:
9775009ff6dSsoda 		break;
9785009ff6dSsoda 	case BT_BUS_TYPE_MCA:
9795009ff6dSsoda 		/* We don't grok MicroChannel (yet). */
9805009ff6dSsoda 		return 1;
9815009ff6dSsoda 	default:
9825009ff6dSsoda 		printf("bt_find: illegal bus type %c\n", inquire.reply.bus_type);
9835009ff6dSsoda 		return 1;
9845009ff6dSsoda 	}
9855009ff6dSsoda 
9865009ff6dSsoda 	/*
9871ffa7b76Swiz 	 * Assume we have a board at this stage setup DMA channel from
9885009ff6dSsoda 	 * jumpers and save int level
9895009ff6dSsoda 	 */
9905009ff6dSsoda 	delay(1000);
9915009ff6dSsoda 	config.cmd.opcode = BT_INQUIRE_CONFIG;
9925009ff6dSsoda 	bt_cmd(iobase, sc, sizeof(config.cmd), (u_char *)&config.cmd,
9935009ff6dSsoda 	    sizeof(config.reply), (u_char *)&config.reply);
9945009ff6dSsoda 	switch (config.reply.chan) {
9955009ff6dSsoda 	case EISADMA:
9965009ff6dSsoda 		drq = DRQUNK;
9975009ff6dSsoda 		break;
9985009ff6dSsoda 	case CHAN0:
9995009ff6dSsoda 		drq = 0;
10005009ff6dSsoda 		break;
10015009ff6dSsoda 	case CHAN5:
10025009ff6dSsoda 		drq = 5;
10035009ff6dSsoda 		break;
10045009ff6dSsoda 	case CHAN6:
10055009ff6dSsoda 		drq = 6;
10065009ff6dSsoda 		break;
10075009ff6dSsoda 	case CHAN7:
10085009ff6dSsoda 		drq = 7;
10095009ff6dSsoda 		break;
10105009ff6dSsoda 	default:
10115009ff6dSsoda 		printf("bt_find: illegal drq setting %x\n", config.reply.chan);
10125009ff6dSsoda 		return 1;
10135009ff6dSsoda 	}
10145009ff6dSsoda 
10155009ff6dSsoda 	switch (config.reply.intr) {
10165009ff6dSsoda 	case INT9:
10175009ff6dSsoda 		irq = 9;
10185009ff6dSsoda 		break;
10195009ff6dSsoda 	case INT10:
10205009ff6dSsoda 		irq = 10;
10215009ff6dSsoda 		break;
10225009ff6dSsoda 	case INT11:
10235009ff6dSsoda 		irq = 11;
10245009ff6dSsoda 		break;
10255009ff6dSsoda 	case INT12:
10265009ff6dSsoda 		irq = 12;
10275009ff6dSsoda 		break;
10285009ff6dSsoda 	case INT14:
10295009ff6dSsoda 		irq = 14;
10305009ff6dSsoda 		break;
10315009ff6dSsoda 	case INT15:
10325009ff6dSsoda 		irq = 15;
10335009ff6dSsoda 		break;
10345009ff6dSsoda 	default:
10355009ff6dSsoda 		printf("bt_find: illegal irq setting %x\n", config.reply.intr);
10365009ff6dSsoda 		return 1;
10375009ff6dSsoda 	}
10385009ff6dSsoda 
10395009ff6dSsoda 	if (sc != NULL) {
10405009ff6dSsoda 		/* who are we on the scsi bus? */
10415009ff6dSsoda 		sc->sc_scsi_dev = config.reply.scsi_dev;
10425009ff6dSsoda 
10435009ff6dSsoda 		sc->sc_iobase = iobase;
10445009ff6dSsoda 		sc->sc_irq = irq;
10455009ff6dSsoda 		sc->sc_drq = drq;
10465009ff6dSsoda 	} else {
10475009ff6dSsoda 		if (ia->ia_irq == IRQUNK)
10485009ff6dSsoda 			ia->ia_irq = irq;
10495009ff6dSsoda 		else if (ia->ia_irq != irq)
10505009ff6dSsoda 			return 1;
10515009ff6dSsoda 		if (ia->ia_drq == DRQUNK)
10525009ff6dSsoda 			ia->ia_drq = drq;
10535009ff6dSsoda 		else if (ia->ia_drq != drq)
10545009ff6dSsoda 			return 1;
10555009ff6dSsoda 	}
10565009ff6dSsoda 
10575009ff6dSsoda 	return 0;
10585009ff6dSsoda }
10595009ff6dSsoda 
10605009ff6dSsoda /*
10615009ff6dSsoda  * Start the board, ready for normal operation
10625009ff6dSsoda  */
10635009ff6dSsoda void
bt_init(struct bt_softc * sc)10647fe2a5a0Stsutsui bt_init(struct bt_softc *sc)
10655009ff6dSsoda {
10665009ff6dSsoda 	int iobase = sc->sc_iobase;
10675009ff6dSsoda 	struct bt_devices devices;
10685009ff6dSsoda 	struct bt_setup setup;
10695009ff6dSsoda 	struct bt_mailbox mailbox;
10705009ff6dSsoda 	struct bt_period period;
10715009ff6dSsoda 	int i;
10725009ff6dSsoda 
10735009ff6dSsoda 	/* Enable round-robin scheme - appeared at firmware rev. 3.31. */
10745009ff6dSsoda 	if (strcmp(sc->sc_firmware, "3.31") >= 0) {
10755009ff6dSsoda 		struct bt_toggle toggle;
10765009ff6dSsoda 
10775009ff6dSsoda 		toggle.cmd.opcode = BT_ROUND_ROBIN;
10785009ff6dSsoda 		toggle.cmd.enable = 1;
10795009ff6dSsoda 		bt_cmd(iobase, sc, sizeof(toggle.cmd), (u_char *)&toggle.cmd,
10805009ff6dSsoda 		    0, (u_char *)0);
10815009ff6dSsoda 	}
10825009ff6dSsoda 
10835009ff6dSsoda 	/* Inquire Installed Devices (to force synchronous negotiation). */
10845009ff6dSsoda 	devices.cmd.opcode = BT_INQUIRE_DEVICES;
10855009ff6dSsoda 	bt_cmd(iobase, sc, sizeof(devices.cmd), (u_char *)&devices.cmd,
10865009ff6dSsoda 	    sizeof(devices.reply), (u_char *)&devices.reply);
10875009ff6dSsoda 
10885009ff6dSsoda 	/* Obtain setup information from. */
10895009ff6dSsoda 	setup.cmd.opcode = BT_INQUIRE_SETUP;
10905009ff6dSsoda 	setup.cmd.len = sizeof(setup.reply);
10915009ff6dSsoda 	bt_cmd(iobase, sc, sizeof(setup.cmd), (u_char *)&setup.cmd,
10925009ff6dSsoda 	    sizeof(setup.reply), (u_char *)&setup.reply);
10935009ff6dSsoda 
10945009ff6dSsoda 	printf("%s: %s, %s\n",
10950f31d9deStsutsui 	    device_xname(sc->sc_dev),
10965009ff6dSsoda 	    setup.reply.sync_neg ? "sync" : "async",
10975009ff6dSsoda 	    setup.reply.parity ? "parity" : "no parity");
10985009ff6dSsoda 
10995009ff6dSsoda 	for (i = 0; i < 8; i++)
11005009ff6dSsoda 		period.reply.period[i] = setup.reply.sync[i].period * 5 + 20;
11015009ff6dSsoda 
11025009ff6dSsoda 	if (sc->sc_firmware[0] >= '3') {
11035009ff6dSsoda 		period.cmd.opcode = BT_INQUIRE_PERIOD;
11045009ff6dSsoda 		period.cmd.len = sizeof(period.reply);
11055009ff6dSsoda 		bt_cmd(iobase, sc, sizeof(period.cmd), (u_char *)&period.cmd,
11065009ff6dSsoda 		    sizeof(period.reply), (u_char *)&period.reply);
11075009ff6dSsoda 	}
11085009ff6dSsoda 
11095009ff6dSsoda 	for (i = 0; i < 8; i++) {
11105009ff6dSsoda 		if (!setup.reply.sync[i].valid ||
11115009ff6dSsoda 		    (!setup.reply.sync[i].offset && !setup.reply.sync[i].period))
11125009ff6dSsoda 			continue;
11135009ff6dSsoda 		printf("%s targ %d: sync, offset %d, period %dnsec\n",
11140f31d9deStsutsui 		    device_xname(sc->sc_dev), i,
11155009ff6dSsoda 		    setup.reply.sync[i].offset, period.reply.period[i] * 10);
11165009ff6dSsoda 	}
11175009ff6dSsoda 
11185009ff6dSsoda 	/*
11195009ff6dSsoda 	 * Set up initial mail box for round-robin operation.
11205009ff6dSsoda 	 */
11215009ff6dSsoda 	for (i = 0; i < BT_MBX_SIZE; i++) {
11225009ff6dSsoda 		wmbx->mbo[i].cmd = BT_MBO_FREE;
11235009ff6dSsoda 		wmbx->mbi[i].stat = BT_MBI_FREE;
11245009ff6dSsoda 	}
11255009ff6dSsoda 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
11265009ff6dSsoda 	wmbx->tmbi = &wmbx->mbi[0];
11275009ff6dSsoda 	sc->sc_mbofull = 0;
11285009ff6dSsoda 
11295009ff6dSsoda 	/* Initialize mail box. */
11305009ff6dSsoda 	mailbox.cmd.opcode = BT_MBX_INIT_EXTENDED;
11315009ff6dSsoda 	mailbox.cmd.nmbx = BT_MBX_SIZE;
11325009ff6dSsoda 	ltophys(KVTOPHYS(wmbx), mailbox.cmd.addr);
11335009ff6dSsoda 	bt_cmd(iobase, sc, sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
11345009ff6dSsoda 	    0, (u_char *)0);
11355009ff6dSsoda }
11365009ff6dSsoda 
11375009ff6dSsoda void
bt_inquire_setup_information(struct bt_softc * sc)11387fe2a5a0Stsutsui bt_inquire_setup_information(struct bt_softc *sc)
11395009ff6dSsoda {
11405009ff6dSsoda 	int iobase = sc->sc_iobase;
11415009ff6dSsoda 	struct bt_model model;
11425009ff6dSsoda 	struct bt_revision revision;
11435009ff6dSsoda 	struct bt_digit digit;
11445009ff6dSsoda 	char *p;
11455009ff6dSsoda 
11465009ff6dSsoda 	/*
11475009ff6dSsoda 	 * Get the firmware revision.
11485009ff6dSsoda 	 */
11495009ff6dSsoda 	p = sc->sc_firmware;
11505009ff6dSsoda 	revision.cmd.opcode = BT_INQUIRE_REVISION;
11515009ff6dSsoda 	bt_cmd(iobase, sc, sizeof(revision.cmd), (u_char *)&revision.cmd,
11525009ff6dSsoda 	    sizeof(revision.reply), (u_char *)&revision.reply);
11535009ff6dSsoda 	*p++ = revision.reply.firm_revision;
11545009ff6dSsoda 	*p++ = '.';
11555009ff6dSsoda 	*p++ = revision.reply.firm_version;
11565009ff6dSsoda 	digit.cmd.opcode = BT_INQUIRE_REVISION_3;
11575009ff6dSsoda 	bt_cmd(iobase, sc, sizeof(digit.cmd), (u_char *)&digit.cmd,
11585009ff6dSsoda 	    sizeof(digit.reply), (u_char *)&digit.reply);
11595009ff6dSsoda 	*p++ = digit.reply.digit;
11605009ff6dSsoda 	if (revision.reply.firm_revision >= '3' ||
11615009ff6dSsoda 	    (revision.reply.firm_revision == '3' && revision.reply.firm_version >= '3')) {
11625009ff6dSsoda 		digit.cmd.opcode = BT_INQUIRE_REVISION_4;
11635009ff6dSsoda 		bt_cmd(iobase, sc, sizeof(digit.cmd), (u_char *)&digit.cmd,
11645009ff6dSsoda 		    sizeof(digit.reply), (u_char *)&digit.reply);
11655009ff6dSsoda 		*p++ = digit.reply.digit;
11665009ff6dSsoda 	}
11675009ff6dSsoda 	while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
11685009ff6dSsoda 		p--;
11695009ff6dSsoda 	*p = '\0';
11705009ff6dSsoda 
11715009ff6dSsoda 	/*
11725009ff6dSsoda 	 * Get the model number.
11735009ff6dSsoda 	 */
11745009ff6dSsoda 	if (revision.reply.firm_revision >= '3') {
11755009ff6dSsoda 		p = sc->sc_model;
11765009ff6dSsoda 		model.cmd.opcode = BT_INQUIRE_MODEL;
11775009ff6dSsoda 		model.cmd.len = sizeof(model.reply);
11785009ff6dSsoda 		bt_cmd(iobase, sc, sizeof(model.cmd), (u_char *)&model.cmd,
11795009ff6dSsoda 		    sizeof(model.reply), (u_char *)&model.reply);
11805009ff6dSsoda 		*p++ = model.reply.id[0];
11815009ff6dSsoda 		*p++ = model.reply.id[1];
11825009ff6dSsoda 		*p++ = model.reply.id[2];
11835009ff6dSsoda 		*p++ = model.reply.id[3];
11845009ff6dSsoda 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
11855009ff6dSsoda 			p--;
11865009ff6dSsoda 		*p++ = model.reply.version[0];
11875009ff6dSsoda 		*p++ = model.reply.version[1];
11885009ff6dSsoda 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
11895009ff6dSsoda 			p--;
11905009ff6dSsoda 		*p = '\0';
11915009ff6dSsoda 	} else
11925009ff6dSsoda 		strcpy(sc->sc_model, "542B");
11935009ff6dSsoda 
11945009ff6dSsoda 	printf(": model BT-%s, firmware %s\n", sc->sc_model, sc->sc_firmware);
11955009ff6dSsoda }
11965009ff6dSsoda 
11975009ff6dSsoda void
btminphys(struct buf * bp)11987fe2a5a0Stsutsui btminphys(struct buf *bp)
11995009ff6dSsoda {
12005009ff6dSsoda 
12015009ff6dSsoda 	if (bp->b_bcount > ((BT_NSEG - 1) << PGSHIFT))
12025009ff6dSsoda 		bp->b_bcount = ((BT_NSEG - 1) << PGSHIFT);
12035009ff6dSsoda 	minphys(bp);
12045009ff6dSsoda }
12055009ff6dSsoda 
12065009ff6dSsoda /*
12075009ff6dSsoda  * start a scsi operation given the command and the data address.  Also needs
12085009ff6dSsoda  * the unit, target and lu.
12095009ff6dSsoda  */
12105009ff6dSsoda int
bt_scsi_cmd(struct scsipi_xfer * xs)12117fe2a5a0Stsutsui bt_scsi_cmd(struct scsipi_xfer *xs)
12125009ff6dSsoda {
1213564df9b6Ssoda 	struct scsipi_link *sc_link = xs->sc_link;
12145009ff6dSsoda 	struct bt_softc *sc = sc_link->adapter_softc;
12155009ff6dSsoda 	struct bt_ccb *ccb;
12165009ff6dSsoda 	struct bt_scat_gath *sg;
12175009ff6dSsoda 	int seg;		/* scatter gather seg being worked on */
12185009ff6dSsoda 	u_long thiskv, thisbounce;
1219564df9b6Ssoda 	int bytes_this_page, datalen, control;
12205009ff6dSsoda 	int s;
12215009ff6dSsoda 
12225009ff6dSsoda 	SC_DEBUG(sc_link, SDEV_DB2, ("bt_scsi_cmd\n"));
12235009ff6dSsoda 	/*
12245009ff6dSsoda 	 * get a ccb to use. If the transfer
12255009ff6dSsoda 	 * is from a buf (possibly from interrupt time)
12265009ff6dSsoda 	 * then we can't allow it to sleep
12275009ff6dSsoda 	 */
1228564df9b6Ssoda 	control = xs->xs_control;
1229564df9b6Ssoda 	if ((ccb = bt_get_ccb(sc, control & XS_CTL_NOSLEEP)) == NULL) {
12305009ff6dSsoda 		xs->error = XS_DRIVER_STUFFUP;
12315009ff6dSsoda 		return TRY_AGAIN_LATER;
12325009ff6dSsoda 	}
12335009ff6dSsoda 	ccb->xs = xs;
12345009ff6dSsoda 	ccb->timeout = xs->timeout;
12355009ff6dSsoda 
12365009ff6dSsoda 	/*
12375009ff6dSsoda 	 * Put all the arguments for the xfer in the ccb
12385009ff6dSsoda 	 */
1239564df9b6Ssoda 	if (control & XS_CTL_RESET) {
12405009ff6dSsoda 		ccb->opcode = BT_RESET_CCB;
12415009ff6dSsoda 		ccb->scsi_cmd_length = 0;
12425009ff6dSsoda 	} else {
12435009ff6dSsoda 		/* can't use S/G if zero length */
1244d41e8f00Sthorpej 		if (xs->cmdlen > sizeof(ccb->scsi_cmd)) {
1245d41e8f00Sthorpej 			printf("%s: cmdlen %d too large for CCB\n",
12460f31d9deStsutsui 			    device_xname(sc->sc_dev), xs->cmdlen);
1247d41e8f00Sthorpej 			xs->error = XS_DRIVER_STUFFUP;
1248d41e8f00Sthorpej 			bt_free_ccb(sc, ccb);
1249d41e8f00Sthorpej 			return COMPLETE;
1250d41e8f00Sthorpej 		}
12515009ff6dSsoda 		ccb->opcode = (xs->datalen ? BT_INIT_SCAT_GATH_CCB
12525009ff6dSsoda 					   : BT_INITIATOR_CCB);
1253e2cb8590Scegger 		memcpy(&ccb->scsi_cmd, xs->cmd,
12545009ff6dSsoda 		    ccb->scsi_cmd_length = xs->cmdlen);
12555009ff6dSsoda 	}
12565009ff6dSsoda 
12575009ff6dSsoda 	if (xs->datalen) {
12585009ff6dSsoda 		sg = ccb->scat_gath;
12595009ff6dSsoda 		seg = 0;
12605009ff6dSsoda 		/*
12615009ff6dSsoda 		 * Set up the scatter-gather block.
12625009ff6dSsoda 		 */
12635009ff6dSsoda 		SC_DEBUG(sc_link, SDEV_DB4,
12645009ff6dSsoda 		    ("%d @0x%x:- ", xs->datalen, xs->data));
12655009ff6dSsoda 
12665009ff6dSsoda 		datalen = xs->datalen;
12675009ff6dSsoda 		thiskv = (int)xs->data;
12685009ff6dSsoda 
12695009ff6dSsoda 		while (datalen && seg < BT_NSEG) {
12705009ff6dSsoda 
12715009ff6dSsoda 			/* put in the base address of a buf */
1272564df9b6Ssoda 			thisbounce = (u_long)
1273564df9b6Ssoda 				bt_get_buf(sc, control & XS_CTL_NOSLEEP);
12745009ff6dSsoda 			if(thisbounce == 0)
12755009ff6dSsoda 				break;
12765009ff6dSsoda 			ltophys(KVTOPHYS(thisbounce), sg->seg_addr);
1277d1579b2dSriastradh 			bytes_this_page = uimin(sizeof(struct bt_buf), datalen);
1278564df9b6Ssoda 			if (control & XS_CTL_DATA_OUT) {
1279e2cb8590Scegger 				memcpy((void *)thisbounce, (void *)thiskv, bytes_this_page);
12805009ff6dSsoda 			}
12815009ff6dSsoda 			thiskv += bytes_this_page;
12825009ff6dSsoda 			datalen -= bytes_this_page;
12835009ff6dSsoda 
12845009ff6dSsoda 			ltophys(bytes_this_page, sg->seg_len);
12855009ff6dSsoda 			sg++;
12865009ff6dSsoda 			seg++;
12875009ff6dSsoda 		}
12885009ff6dSsoda 		SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
12895009ff6dSsoda 		if (datalen) {
12905009ff6dSsoda 			printf("%s: bt_scsi_cmd, out of bufs %d of %d left.\n",
12910f31d9deStsutsui 			    device_xname(sc->sc_dev), datalen, xs->datalen);
12925009ff6dSsoda 			goto badbuf;
12935009ff6dSsoda 		}
12945009ff6dSsoda 		ltophys(KVTOPHYS(ccb->scat_gath), ccb->data_addr);
12955009ff6dSsoda 		ltophys(seg * sizeof(struct bt_scat_gath), ccb->data_length);
12965009ff6dSsoda 	} else {		/* No data xfer, use non S/G values */
12975009ff6dSsoda 		ltophys(0, ccb->data_addr);
12985009ff6dSsoda 		ltophys(0, ccb->data_length);
12995009ff6dSsoda 	}
13005009ff6dSsoda 
13015009ff6dSsoda 	ccb->data_out = 0;
13025009ff6dSsoda 	ccb->data_in = 0;
1303564df9b6Ssoda 	ccb->target = sc_link->scsipi_scsi.target;
1304564df9b6Ssoda 	ccb->lun = sc_link->scsipi_scsi.lun;
13055009ff6dSsoda 	ltophys(KVTOPHYS(&ccb->scsi_sense), ccb->sense_ptr);
13065009ff6dSsoda 	ccb->req_sense_length = sizeof(ccb->scsi_sense);
13075009ff6dSsoda 	ccb->host_stat = 0x00;
13085009ff6dSsoda 	ccb->target_stat = 0x00;
13095009ff6dSsoda 	ccb->link_id = 0;
13105009ff6dSsoda 	ltophys(0, ccb->link_addr);
13115009ff6dSsoda 
13125009ff6dSsoda 	s = splbio();
13135009ff6dSsoda 	bt_queue_ccb(sc, ccb);
13145009ff6dSsoda 	splx(s);
13155009ff6dSsoda 
13165009ff6dSsoda 	/*
13175009ff6dSsoda 	 * Usually return SUCCESSFULLY QUEUED
13185009ff6dSsoda 	 */
13195009ff6dSsoda 	SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
1320564df9b6Ssoda 	if ((control & XS_CTL_POLL) == 0)
13215009ff6dSsoda 		return SUCCESSFULLY_QUEUED;
13225009ff6dSsoda 
13235009ff6dSsoda 	/*
13245009ff6dSsoda 	 * If we can't use interrupts, poll on completion
13255009ff6dSsoda 	 */
13265009ff6dSsoda 	if (bt_poll(sc, xs, ccb->timeout)) {
13275009ff6dSsoda 		bt_timeout(ccb);
13285009ff6dSsoda 		if (bt_poll(sc, xs, ccb->timeout))
13295009ff6dSsoda 			bt_timeout(ccb);
13305009ff6dSsoda 	}
13315009ff6dSsoda 	return COMPLETE;
13325009ff6dSsoda 
13335009ff6dSsoda badbuf:
13345009ff6dSsoda 	sg = ccb->scat_gath;
13355009ff6dSsoda 	while (seg) {
13365009ff6dSsoda 		thisbounce = PHYSTOKV(phystol(sg->seg_addr));
13375009ff6dSsoda 		bt_free_buf(sc, (struct bt_buf *)thisbounce);
13385009ff6dSsoda 		sg++;
13395009ff6dSsoda 		seg--;
13405009ff6dSsoda 	}
13415009ff6dSsoda 	xs->error = XS_DRIVER_STUFFUP;
13425009ff6dSsoda 	bt_free_ccb(sc, ccb);
13435009ff6dSsoda 	return TRY_AGAIN_LATER;
13445009ff6dSsoda }
13455009ff6dSsoda 
13465009ff6dSsoda /*
13475009ff6dSsoda  * Poll a particular unit, looking for a particular xs
13485009ff6dSsoda  */
13495009ff6dSsoda int
bt_poll(struct bt_softc * sc,struct scsipi_xfer * xs,int count)13507fe2a5a0Stsutsui bt_poll(struct bt_softc *sc, struct scsipi_xfer *xs, int count)
13515009ff6dSsoda {
13525009ff6dSsoda 	int iobase = sc->sc_iobase;
13535009ff6dSsoda 
13545009ff6dSsoda 	/* timeouts are in msec, so we loop in 1000 usec cycles */
13555009ff6dSsoda 	while (count) {
13565009ff6dSsoda 		/*
13575009ff6dSsoda 		 * If we had interrupts enabled, would we
13585009ff6dSsoda 		 * have got an interrupt?
13595009ff6dSsoda 		 */
13605009ff6dSsoda 		if (isa_inb(iobase + BT_INTR_PORT) & BT_INTR_ANYINTR)
13615009ff6dSsoda 			btintr(sc);
1362564df9b6Ssoda 		if (xs->xs_status & XS_STS_DONE)
13635009ff6dSsoda 			return 0;
13645009ff6dSsoda 		delay(1000);	/* only happens in boot so ok */
13655009ff6dSsoda 		count--;
13665009ff6dSsoda 	}
13675009ff6dSsoda 	return 1;
13685009ff6dSsoda }
13695009ff6dSsoda 
13705009ff6dSsoda void
bt_timeout(void * arg)13717fe2a5a0Stsutsui bt_timeout(void *arg)
13725009ff6dSsoda {
13735009ff6dSsoda 	struct bt_ccb *ccb = arg;
1374564df9b6Ssoda 	struct scsipi_xfer *xs = ccb->xs;
1375564df9b6Ssoda 	struct scsipi_link *sc_link = xs->sc_link;
13765009ff6dSsoda 	struct bt_softc *sc = sc_link->adapter_softc;
13775009ff6dSsoda 	int s;
13785009ff6dSsoda 
1379564df9b6Ssoda 	scsi_print_addr(sc_link);
13805009ff6dSsoda 	printf("timed out");
13815009ff6dSsoda 
13825009ff6dSsoda 	s = splbio();
13835009ff6dSsoda 
13845009ff6dSsoda #ifdef BTDIAG
13855009ff6dSsoda 	/*
13865009ff6dSsoda 	 * If the ccb's mbx is not free, then the board has gone Far East?
13875009ff6dSsoda 	 */
13885009ff6dSsoda 	bt_collect_mbo(sc);
13895009ff6dSsoda 	if (ccb->flags & CCB_SENDING) {
13900f31d9deStsutsui 		printf("%s: not taking commands!\n", device_xname(sc->sc_dev));
13915009ff6dSsoda 		Debugger();
13925009ff6dSsoda 	}
13935009ff6dSsoda #endif
13945009ff6dSsoda 
13955009ff6dSsoda 	/*
13965009ff6dSsoda 	 * If it has been through before, then
13975009ff6dSsoda 	 * a previous abort has failed, don't
13985009ff6dSsoda 	 * try abort again
13995009ff6dSsoda 	 */
14005009ff6dSsoda 	if (ccb->flags & CCB_ABORT) {
14015009ff6dSsoda 		/* abort timed out */
14025009ff6dSsoda 		printf(" AGAIN\n");
14035009ff6dSsoda 		/* XXX Must reset! */
14045009ff6dSsoda 	} else {
14055009ff6dSsoda 		/* abort the operation that has timed out */
14065009ff6dSsoda 		printf("\n");
14075009ff6dSsoda 		ccb->xs->error = XS_TIMEOUT;
14085009ff6dSsoda 		ccb->timeout = BT_ABORT_TIMEOUT;
14095009ff6dSsoda 		ccb->flags |= CCB_ABORT;
14105009ff6dSsoda 		bt_queue_ccb(sc, ccb);
14115009ff6dSsoda 	}
14125009ff6dSsoda 
14135009ff6dSsoda 	splx(s);
14145009ff6dSsoda }
1415