From: julian@tfs.com (Julian Elischer)
Subject: New scsi system beta3 (part 8 of       10)
Organization: TRW Financial Systems
Date: Sat, 3 Oct 1992 04:02:33 GMT

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	i386/conf/SCSITEST
#	i386/isa/aha1542.c
#
echo x - i386/conf/SCSITEST
sed 's/^X//' >i386/conf/SCSITEST << 'END-of-i386/conf/SCSITEST'
X#
X# SCSITEST -- Generic ISA machine -- scsi test kernel
X#
Xmachine		"i386"
Xcpu		"i386"
Xident		SCSITEST
Xtimezone	8 dst
Xmaxusers	10
Xoptions		INET,ISOFS,NFS
Xoptions		"COMPAT_43"
Xoptions		"TCP_COMPAT_42"
X
Xconfig		"386bsd"	root on wd0 swap on wd0 and sd0
X
Xcontroller	isa0
Xcontroller	wd0	at isa? port "IO_WD1" bio irq 14 vector wdintr
Xdisk		wd0	at wd0 drive 0
Xdisk		wd0	at wd0 drive 1
X
Xcontroller	fd0	at isa? port "IO_FD1" bio irq 6 drq 2 vector fdintr
Xdisk		fd0	at fd0 drive 0
Xdisk		fd1	at fd0 drive 1
X
Xdevice		pc0	at isa? port "IO_KBD" tty irq 1 vector pcrint
Xdevice		npx0	at isa? port "IO_NPX" irq 13 vector npxintr
Xdevice		com1	at isa? port "IO_COM1" tty irq 4 vector comintr
Xdevice		com2	at isa? port "IO_COM2" tty irq 3 vector comintr
X
Xcontroller	aha0	at isa? port "IO_AHA0" bio irq 11 drq 5 vector ahaintr
Xcontroller	aha1	at isa? port "IO_AHA1" bio irq 12 drq 7 vector ahaintr
X#controller	bt0	at isa? port "IO_BT0" bio irq 12  vector btintr
Xcontroller	scbus0
X
Xdevice		sd0
Xdevice		sd1
Xdevice		sd2
Xdevice		sd3
X
Xdevice		st0
Xdevice		st1
Xdevice		st2
Xdevice		st3
X
Xdevice		cd0
Xdevice		cd1
X
Xdevice		we0 at isa? port 0x280 net irq 2 iomem 0xd0000 iosiz 8192 vector weintr
X
X
Xpseudo-device	loop
Xpseudo-device	ether
Xpseudo-device	sl	2
Xpseudo-device	log
Xpseudo-device	ddb
Xpseudo-device	pty	4
X
Xpseudo-device	swappager
Xpseudo-device	vnodepager
Xpseudo-device	devpager
END-of-i386/conf/SCSITEST
echo x - i386/isa/aha1542.c
sed 's/^X//' >i386/isa/aha1542.c << 'END-of-i386/isa/aha1542.c'
X/*
X * (Mostly) Written by Julian Elischer (julian@tfs.com)
X * for TRW Financial Systems for use under the MACH(2.5) operating system.
X *
X * TRW Financial Systems, in accordance with their agreement with Carnegie
X * Mellon University, makes this software available to CMU to distribute
X * or use in any manner that they see fit as long as this message is kept with
X * the software. For this reason TFS also grants any other persons or
X * organisations permission to use or modify this software.
X *
X * TFS supplies this software to be publicly redistributed
X * on the understanding that TFS is not responsible for the correct
X * functioning of this software in any circumstances.
X *
X */
X
X/*
X * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
X */
X
X/*
X * HISTORY
X * $Log: aha1542.c,v $
X * Revision 1.6  1992/08/24  21:01:58  jason
X * many changes and bugfixes for osf1
X *
X * Revision 1.5  1992/07/31  01:22:03  julian
X * support improved scsi.h layout
X *
X * Revision 1.4  1992/07/25  03:11:26  julian
X * check each request fro sane flags.
X *
X * Revision 1.3  1992/07/24  00:52:45  julian
X * improved timeout handling.
X * added support for two arguments to the sd_done (or equiv) call so that
X * they can pre-queue several arguments.
X * slightly clean up error handling
X *
X * Revision 1.2  1992/07/17  22:03:54  julian
X * upgraded the timeout code.
X * added support for UIO-based i/o (as used for pmem operations)
X *
X * Revision 1.1  1992/05/27  00:51:12  balsup
X * machkern/cor merge
X */
X
X/*
X * a FEW lines in this driver come from a MACH adaptec-disk driver
X * so the copyright below is included:
X *
X * Copyright 1990 by Open Software Foundation,
X * Grenoble, FRANCE
X *
X * 		All Rights Reserved
X * 
X *   Permission to use, copy, modify, and distribute this software and
X * its documentation for any purpose and without fee is hereby granted,
X * provided that the above copyright notice appears in all copies and
X * that both the copyright notice and this permission notice appear in
X * supporting documentation, and that the name of OSF or Open Software
X * Foundation not be used in advertising or publicity pertaining to
X * distribution of the software without specific, written prior
X * permission.
X * 
X *   OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
X * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
X * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
X * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
X * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
X * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
X * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X */
X
X
X#include <sys/types.h>
X#include <aha.h>
X
X#include <sys/param.h>
X#include <sys/systm.h>
X#include <sys/errno.h>
X#include <sys/ioctl.h>
X#include <sys/buf.h>
X#include <sys/proc.h>
X#include <sys/user.h>
X
X#ifdef  MACH    /* EITHER CMU OR OSF */
X#include <i386/ipl.h>
X#include <i386at/scsi.h>
X#include <i386at/scsiconf.h>
X
X#ifdef  OSF     /* OSF ONLY */
X#include <sys/table.h>
X#include <i386/handler.h>
X#include <i386/dispatcher.h>
X#include <i386/AT386/atbus.h>
X
X#else   OSF     /* CMU ONLY */
X#include <i386at/atbus.h>
X#include <i386/pio.h>
X#endif  OSF
X#endif  MACH    /* end of MACH specific */
X
X#ifdef  __386BSD__      /* 386BSD specific */
X#define isa_dev isa_device
X#define dev_unit id_unit
X#define dev_addr id_iobase
X
X#include <i386/isa/isa_device.h>
X#include <scsi/scsi_all.h>
X#include <scsi/scsiconf.h>
X#endif  __386BSD__
X
X
X#ifdef	__386BSD__
X#include "ddb.h"
X#if	NDDB > 0
Xint	Debugger();
X#else	NDDB
X#define Debugger() panic("should call debugger here (adaptec.c)")
X#endif	NDDB
X#endif	__386BSD__
Xextern int delaycount;  /* from clock setup code */
X
X/************************** board definitions *******************************/
X/*
X * I/O Port Interface
X */
X
X#define	AHA_BASE		aha_base[unit]
X#define	AHA_CTRL_STAT_PORT	(AHA_BASE + 0x0)	/* control & status */
X#define	AHA_CMD_DATA_PORT	(AHA_BASE + 0x1)	/* cmds and datas */
X#define	AHA_INTR_PORT		(AHA_BASE + 0x2)	/* Intr. stat */
X
X/*
X * AHA_CTRL_STAT bits (write)
X */
X
X#define AHA_HRST		0x80	/* Hardware reset */
X#define AHA_SRST		0x40	/* Software reset */
X#define AHA_IRST		0x20	/* Interrupt reset */
X#define AHA_SCRST		0x10	/* SCSI bus reset */
X
X/*
X * AHA_CTRL_STAT bits (read)
X */
X
X#define AHA_STST		0x80	/* Self test in Progress */
X#define AHA_DIAGF		0x40	/* Diagnostic Failure */
X#define AHA_INIT		0x20	/* Mbx Init required */
X#define AHA_IDLE		0x10	/* Host Adapter Idle */
X#define AHA_CDF			0x08	/* cmd/data out port full */
X#define AHA_DF			0x04	/* Data in port full */
X#define AHA_INVDCMD		0x01	/* Invalid command */
X
X/*
X * AHA_CMD_DATA bits (write)
X */
X
X#define	AHA_NOP			0x00	/* No operation */
X#define AHA_MBX_INIT		0x01	/* Mbx initialization */
X#define AHA_START_SCSI		0x02	/* start scsi command */
X#define AHA_START_BIOS		0x03	/* start bios command */
X#define AHA_INQUIRE		0x04	/* Adapter Inquiry */
X#define AHA_MBO_INTR_EN		0x05	/* Enable MBO available interrupt */
X#define AHA_SEL_TIMEOUT_SET	0x06	/* set selection time-out */
X#define AHA_BUS_ON_TIME_SET	0x07	/* set bus-on time */
X#define AHA_BUS_OFF_TIME_SET	0x08	/* set bus-off time */
X#define AHA_SPEED_SET		0x09	/* set transfer speed */
X#define AHA_DEV_GET		0x0a	/* return installed devices */
X#define AHA_CONF_GET		0x0b	/* return configuration data */
X#define AHA_TARGET_EN		0x0c	/* enable target mode */
X#define AHA_SETUP_GET		0x0d	/* return setup data */
X#define AHA_WRITE_CH2		0x1a	/* write channel 2 buffer */
X#define AHA_READ_CH2		0x1b	/* read channel 2 buffer */
X#define AHA_WRITE_FIFO		0x1c	/* write fifo buffer */
X#define AHA_READ_FIFO		0x1d	/* read fifo buffer */
X#define AHA_ECHO		0x1e	/* Echo command data */
X
Xstruct aha_cmd_buf {
X	 u_char byte[16];	
X};
X
X/*
X * AHA_INTR_PORT bits (read)
X */
X
X#define AHA_ANY_INTR		0x80	/* Any interrupt */
X#define AHA_SCRD		0x08	/* SCSI reset detected */
X#define AHA_HACC		0x04	/* Command complete */
X#define AHA_MBOA		0x02	/* MBX out empty */
X#define AHA_MBIF		0x01	/* MBX in full */
X
X/*
X * Mail box defs 
X */
X
X#define AHA_MBX_SIZE		16	/* mail box size */
X
Xstruct aha_mbx {
X	struct aha_mbx_out {
X		unsigned char cmd;
X		unsigned char ccb_addr[3];
X	} mbo [AHA_MBX_SIZE];
X	struct aha_mbx_in{
X		unsigned char stat;
X		unsigned char ccb_addr[3];
X	} mbi[AHA_MBX_SIZE];
X};
X
X/*
X * mbo.cmd values
X */
X
X#define AHA_MBO_FREE	0x0	/* MBO entry is free */
X#define AHA_MBO_START	0x1	/* MBO activate entry */
X#define AHA_MBO_ABORT	0x2	/* MBO abort entry */
X
X#define AHA_MBI_FREE	0x0	/* MBI entry is free */
X#define AHA_MBI_OK	0x1	/* completed without error */
X#define AHA_MBI_ABORT	0x2	/* aborted ccb */
X#define AHA_MBI_UNKNOWN	0x3	/* Tried to abort invalid CCB */
X#define AHA_MBI_ERROR	0x4	/* Completed with error */
X
Xextern struct aha_mbx aha_mbx[];
X
X/* FOR OLD VERSIONS OF THE !%$@ this may have to be 16 (yuk) */
X#define	AHA_NSEG	17	/* Number of scatter gather segments <= 16 */
X				/* allow 64 K i/o (min) */
X
Xstruct aha_ccb {
X	unsigned char	opcode;
X	unsigned char	lun:3;
X	unsigned char	data_in:1;		/* must be 0 */
X	unsigned char	data_out:1;		/* must be 0 */
X	unsigned char	target:3;
X	unsigned char	scsi_cmd_length;
X	unsigned char	req_sense_length;
X	unsigned char	data_length[3];
X	unsigned char	data_addr[3];
X	unsigned char	link_addr[3];
X	unsigned char	link_id;
X	unsigned char	host_stat;
X	unsigned char	target_stat;
X	unsigned char	reserved[2];
X	struct	scsi_generic	scsi_cmd;
X	struct	scsi_sense_data	scsi_sense;
X	struct	aha_scat_gath {
X		unsigned char seg_len[3];
X		unsigned char seg_addr[3];
X	} scat_gath[AHA_NSEG];
X	struct	aha_ccb	*next;
X	struct	scsi_xfer	*xfer;		/* the scsi_xfer for this cmd */
X	struct	aha_mbx_out	*mbx;		/* pointer to mail box */
X	long int	delta;	/* difference from previous*/
X	struct	aha_ccb	*later,*sooner;
X	int	flags;
X#define CCB_FREE        0
X#define CCB_ACTIVE      1
X#define CCB_ABORTED     2
X
X};
X
Xstruct	aha_ccb *aha_soonest = (struct  aha_ccb *)0;
Xstruct	aha_ccb	*aha_latest = (struct  aha_ccb *)0;
Xlong int	aha_furtherest = 0;	/* longest time in the timeout queue */
X
X/*
X * opcode fields
X */
X
X#define AHA_INITIATOR_CCB	0x00	/* SCSI Initiator CCB */
X#define AHA_TARGET_CCB		0x01	/* SCSI Target CCB */
X#define AHA_INIT_SCAT_GATH_CCB	0x02	/* SCSI Initiator with scattter gather*/
X#define AHA_RESET_CCB		0x81	/* SCSI Bus reset */
X
X
X/*
X * aha_ccb.host_stat values
X */
X
X#define AHA_OK		0x00	/* cmd ok */
X#define AHA_LINK_OK	0x0a	/* Link cmd ok */
X#define AHA_LINK_IT	0x0b	/* Link cmd ok + int */
X#define AHA_SEL_TIMEOUT	0x11	/* Selection time out */
X#define AHA_OVER_UNDER	0x12	/* Data over/under run */
X#define AHA_BUS_FREE	0x13	/* Bus dropped at unexpected time */
X#define AHA_INV_BUS	0x14	/* Invalid bus phase/sequence */
X#define AHA_BAD_MBO	0x15	/* Incorrect MBO cmd */
X#define AHA_BAD_CCB	0x16	/* Incorrect ccb opcode */
X#define AHA_BAD_LINK	0x17	/* Not same values of LUN for links */
X#define AHA_INV_TARGET	0x18	/* Invalid target direction */
X#define AHA_CCB_DUP	0x19	/* Duplicate CCB received */
X#define AHA_INV_CCB	0x1a	/* Invalid CCB or segment list */
X#define AHA_ABORTED      42
X
X
X
X
Xstruct aha_setup
X{
X	u_char	sync_neg:1;
X	u_char	parity:1;
X	u_char	:6;
X	u_char	speed;
X	u_char	bus_on;
X	u_char	bus_off;
X	u_char	num_mbx;
X	u_char	mbx[3];
X	struct
X	{
X		u_char	offset:4;
X		u_char	period:3;
X		u_char	valid:1;
X	}sync[8];
X	u_char	disc_sts;
X};
X
Xstruct	aha_config
X{
X	u_char	chan;
X	u_char	intr;
X	u_char	scsi_dev:3;
X	u_char	:5;
X};
X
X#define INT9	0x01
X#define INT10	0x02
X#define INT11	0x04
X#define INT12	0x08
X#define INT14	0x20
X#define INT15	0x40
X
X#define CHAN0	0x01
X#define CHAN5	0x20
X#define CHAN6	0x40
X#define CHAN7	0x80
X
X
X/*********************************** end of board definitions***************/
X
X
X#ifdef	MACH
X#define PHYSTOKV(x)	phystokv(x)
X#define KVTOPHYS(x)	kvtophys(x)
X#else	MACH
X#ifdef	__386BSD__
X#define PHYSTOKV(x)	(x | 0xFE000000)
X#define KVTOPHYS(x)	vtophys(x)
X#else	__386BSD__
X#endif	__386BSD__
X#endif	MACH
X#define	AHA_DMA_PAGES	AHA_NSEG
X
X#define PAGESIZ 	4096
X#define INVALIDATE_CACHE {asm volatile( ".byte	0x0F ;.byte 0x08" ); }
X
X
Xu_char			aha_scratch_buf[256];
X#ifdef	MACH
Xcaddr_t			aha_base[NAHA];		/* base port for each board */
X#else
Xshort			aha_base[NAHA];		/* base port for each board */
X#endif
Xstruct	aha_mbx		aha_mbx[NAHA];
Xstruct	aha_ccb		*aha_ccb_free[NAHA];
Xstruct	aha_ccb		aha_ccb[NAHA][AHA_MBX_SIZE];
Xstruct	scsi_xfer	aha_scsi_xfer[NAHA];
Xstruct	isa_dev		*ahainfo[NAHA];
Xstruct	aha_ccb		*aha_get_ccb();
Xint			aha_int[NAHA];
Xint			aha_dma[NAHA];
Xint			aha_scsi_dev[NAHA];
Xint			aha_initialized[NAHA];
X#ifdef	OSF
Xint			aha_attached[NAHA];
X#endif	OSF
Xint			aha_debug = 0;
X
Xint ahaprobe(), ahaattach(), ahaintr();
X#ifdef	MACH
Xstruct	isa_driver	ahadriver = { ahaprobe, 0, ahaattach, "aha", 0, 0, 0};
Xint			(*ahaintrs[])() = {ahaintr, 0};
X#endif
X#ifdef	__386BSD__
Xstruct	isa_driver	ahadriver = { ahaprobe, ahaattach, "aha",};
X#endif	__386BSD__
Xstatic int		ahaunit = 0;
X
X
X#define aha_abortmbx(mbx) \
X	(mbx)->cmd = AHA_MBO_ABORT; \
X	outb(AHA_CMD_DATA_PORT, AHA_START_SCSI);
X#define aha_startmbx(mbx) \
X	(mbx)->cmd = AHA_MBO_START; \
X	outb(AHA_CMD_DATA_PORT, AHA_START_SCSI);
X
X
X
Xint	aha_scsi_cmd();
Xint	aha_timeout();
Xvoid	ahaminphys();
Xlong int	aha_adapter_info();
X
Xstruct	scsi_switch	aha_switch =
X{
X	aha_scsi_cmd,
X	ahaminphys,
X	0,
X	0,
X	aha_adapter_info,
X	0,0,0
X};	
X#define AHA_CMD_TIMEOUT_FUDGE	200	/* multiplied to get Secs	*/
X#define AHA_RESET_TIMEOUT	1000000 /* time to wait for reset	*/
X#define AHA_SCSI_TIMEOUT_FUDGE	20	/* divided by for mSecs		*/
X
X
X/***********************************************************************\
X* aha_cmd(unit,icnt, ocnt,wait, retval, opcode, args)			*
X* Activate Adapter command						*
X*	icnt:	number of args (outbound bytes written after opcode)	*
X*	ocnt:	number of expected returned bytes			*
X*	wait:	number of seconds to wait for response			*
X*	retval:	buffer where to place returned bytes			*
X*	opcode:	opcode AHA_NOP, AHA_MBX_INIT, AHA_START_SCSI ...	*
X*	args:	parameters						*
X*									*
X* Performs an adapter command through the ports. Not to be confused	*
X*	with a scsi command, which is read in via the dma		*
X* One of the adapter commands tells it to read in a scsi command	*
X\***********************************************************************/
X
X
Xaha_cmd(unit,icnt, ocnt, wait,retval, opcode, args)
X
Xu_char *retval;
Xunsigned opcode;
Xu_char args;
X{
X	unsigned *ic = &opcode;
X	u_char oc;
X	register i;
X	int	sts;
X
X	/*******************************************************\
X	* multiply the wait argument by a big constant		*
X	* zero defaults to 1					*
X	\*******************************************************/
X	if(!wait) 
X		wait = AHA_CMD_TIMEOUT_FUDGE * delaycount; 
X	else
X		wait *= AHA_CMD_TIMEOUT_FUDGE * delaycount; 
X	/*******************************************************\
X	* Wait for the adapter to go idle, unless it's one of	*
X	* the commands which don't need this			*
X	\*******************************************************/
X	if (opcode != AHA_MBX_INIT && opcode != AHA_START_SCSI)
X	{
X		i = AHA_CMD_TIMEOUT_FUDGE * delaycount; /* 1 sec?*/
X		while (--i)
X		{
X			sts = inb(AHA_CTRL_STAT_PORT);
X			if (sts & AHA_IDLE)
X			{
X				break;
X			}
X		}
X		if (!i)
X		{
X			printf("aha_cmd: aha1542 host not idle(0x%x)\n",sts);
X			return(ENXIO);
X		}
X	}
X	/*******************************************************\
X	* Now that it is idle, if we expect output, preflush the*
X	* queue feeding to us.					*
X	\*******************************************************/
X	if (ocnt)
X	{
X		while((inb(AHA_CTRL_STAT_PORT)) & AHA_DF)
X			inb(AHA_CMD_DATA_PORT);
X	}
X			
X	/*******************************************************\
X	* Output the command and the number of arguments given	*
X	* for each byte, first check the port is empty.		*
X	\*******************************************************/
X	icnt++;		/* include the command */
X	while (icnt--)
X	{
X		sts = inb(AHA_CTRL_STAT_PORT);
X		for (i=0; i< wait; i++)
X		{
X			sts = inb(AHA_CTRL_STAT_PORT);
X			if (!(sts & AHA_CDF))
X				break;
X		}
X		if (i >=  wait)
X		{
X			printf("aha_cmd: aha1542 cmd/data port full\n");
X			outb(AHA_CTRL_STAT_PORT, AHA_SRST); 
X			return(ENXIO);
X		}
X		outb(AHA_CMD_DATA_PORT, (u_char)(*ic++));
X	}
X	/*******************************************************\
X	* If we expect input, loop that many times, each time,	*
X	* looking for the data register to have valid data	*
X	\*******************************************************/
X	while (ocnt--)
X	{
X		sts = inb(AHA_CTRL_STAT_PORT);
X		for (i=0; i< wait; i++)
X		{
X			sts = inb(AHA_CTRL_STAT_PORT);
X			if (sts  & AHA_DF)
X				break;
X		}
X		if (i >=  wait)
X		{
X			printf("aha_cmd: aha1542 cmd/data port empty %d\n",ocnt);
X			return(ENXIO);
X		}
X		oc = inb(AHA_CMD_DATA_PORT);
X		if (retval)
X			*retval++ = oc;
X	}
X	/*******************************************************\
X	* Wait for the board to report a finised instruction	*
X	\*******************************************************/
X	i=AHA_CMD_TIMEOUT_FUDGE * delaycount;	/* 1 sec? */
X	while (--i)
X	{
X		sts = inb(AHA_INTR_PORT);
X		if (sts & AHA_HACC)
X		{
X			break;
X		}
X	}
X	if (!i)
X	{
X		printf("aha_cmd: aha1542 host not finished(0x%x)\n",sts);
X		return(ENXIO);
X	}
X	outb(AHA_CTRL_STAT_PORT, AHA_IRST);
X	return(0);
X}
X
X/*******************************************************\
X* Check if the device can be found at the port given	*
X* and if so, set it up ready for further work		*
X* as an argument, takes the isa_dev structure from	*
X* autoconf.c						*
X\*******************************************************/
Xahaprobe(dev)
Xstruct isa_dev *dev;
X{
X 	int	unit = ahaunit;
X#if defined(OSF)
X 	static ihandler_t aha_handler[NAHA];
X 	static ihandler_id_t *aha_handler_id[NAHA];
X 	register ihandler_t *chp = &aha_handler[unit];;
X#endif /* defined(OSF) */
X
X  	/***********************************************\
X	/***********************************************\
X	* find unit and check we have that many defined	*
X	\***********************************************/
X	dev->dev_unit = unit;
X	aha_base[unit] = dev->dev_addr;
X	if(unit >= NAHA) 
X	{
X		printf("aha: unit number (%d) too high\n",unit);
X		return(0);
X	}
X	/***********************************************\
X	* Try initialise a unit at this location	*
X	* sets up dma and bus speed, loads aha_int[unit]*
X	\***********************************************/
X	if (aha_init(unit) != 0)
X	{
X		return(0);
X	}
X
X	/***********************************************\
X	* If it's there, put in it's interrupt vectors	*
X	\***********************************************/
X#if !defined(OSF)
X#if	defined MACH
X	iunit[aha_int[unit]] =unit;
X	ivect[aha_int[unit]] = ahaintr;
X	intpri[aha_int[unit]] = dev->dev_spl;
X	form_pic_mask();
X	/*take_dev_irq(dev);*/
X#else
X#ifdef	__386BSD__
X	dev->id_irq = (1 << aha_int[unit]);
X	dev->id_drq = aha_dma[unit];
X#endif	__386BSD__
X#endif
X#else /* !defined(OSF) */
X 
X 	chp->ih_level = dev->dev_pic;
X 	chp->ih_handler = dev->dev_intr[0];
X 	chp->ih_resolver = i386_resolver;
X 	chp->ih_rdev = dev;
X 	chp->ih_stats.intr_type = INTR_DEVICE;
X 	chp->ih_stats.intr_cnt = 0;
X 	chp->ih_hparam[0].intparam = unit;
X 	if ((aha_handler_id[unit] = handler_add(chp)) != NULL)
X 		handler_enable(aha_handler_id[unit]);
X 	else
X 		panic("Unable to add aha interrupt handler");
X#endif /* !defined(OSF) */
X#ifdef	__386BSD__
X	printf("\n  **");
X#else	__386BSD__
X	printf("port=%x spl=%d\n",
X	   dev->dev_addr, dev->dev_spl);
X#endif	__386BSD__
X	ahaunit ++;
X	return(1);
X}
X
X/***********************************************\
X* Attach all the sub-devices we can find	*
X\***********************************************/
Xahaattach(dev)
Xstruct	isa_dev	*dev;
X{
X	int	unit = dev->dev_unit;
X
X#ifdef	__386BSD__
X	printf(" probing for scsi devices**\n");
X#endif	__386BSD__
X	/***********************************************\
X	* ask the adapter what subunits are present	*
X	\***********************************************/
X	scsi_attachdevs( unit, aha_scsi_dev[unit], &aha_switch);
X#if defined(OSF)
X	aha_attached[unit]=1;
X#endif /* defined(OSF) */
X	if(!unit) /* only one for all boards */
X	{
X		aha_timeout(0);
X	}
X#ifdef	__386BSD__
X	printf("aha%d",unit);
X#endif	__386BSD__
X	return;
X
X}
X
X
X/***********************************************\
X* Return some information to the caller about	*
X* the adapter and it's capabilities		*
X\***********************************************/
Xlong int aha_adapter_info(unit)
Xint	unit;
X{
X	return(2);	/* 2 outstanding requests at a time per device */
X}
X
X/***********************************************\
X* Catch an interrupt from the adaptor		*
X\***********************************************/
Xahaintr(unit)
X{
X	struct aha_ccb *ccb;
X	unsigned char stat;
X	register i;
X
X	if(scsi_debug & PRINTROUTINES)
X		printf("ahaintr ");
X	/***********************************************\
X	* First acknowlege the interrupt, Then if it's	*
X	* not telling about a completed operation	*
X	* just return. 					*
X	\***********************************************/
X	stat = inb(AHA_INTR_PORT);
X	outb(AHA_CTRL_STAT_PORT, AHA_IRST);
X	if(scsi_debug & TRACEINTERRUPTS)
X		printf("int ");
X	if (! (stat & AHA_MBIF))
X		return(1);
X	if(scsi_debug & TRACEINTERRUPTS)
X		printf("b ");
X#if defined(OSF)
X	if (!aha_attached[unit])
X	{
X		return(1);
X	}
X#endif /* defined(OSF) */
X	/***********************************************\
X	* If it IS then process the competed operation	*
X	\***********************************************/
X	for (i = 0; i < AHA_MBX_SIZE; i++)
X	{
X		if (aha_mbx[unit].mbi[i].stat != AHA_MBI_FREE) 
X		{
X			ccb = (struct aha_ccb *)PHYSTOKV(
X				(_3btol(aha_mbx[unit].mbi[i].ccb_addr)));
X
X			if((stat =  aha_mbx[unit].mbi[i].stat) != AHA_MBI_OK)
X			{
X				switch(stat)
X				{
X				case	AHA_MBI_ABORT:
X					if(aha_debug)
X					    printf("abort");
X					ccb->host_stat = AHA_ABORTED;
X					break;
X
X				case	AHA_MBI_UNKNOWN:
X					ccb = (struct aha_ccb *)0;
X					if(aha_debug)
X					     printf("unknown ccb for abort ");
X					/* may have missed it */
X					/* no such ccb known for abort */
X
X				case	AHA_MBI_ERROR:
X					break;
X
X				default:
X					panic("Impossible mbxi status");
X
X				}
X				if( aha_debug && ccb )
X				{
X					u_char	*cp;
X					cp = (u_char *)(&(ccb->scsi_cmd));
X					printf("op=%x %x %x %x %x %x\n", 
X						cp[0], cp[1], cp[2],
X						cp[3], cp[4], cp[5]);
X					printf("stat %x for mbi[%d]\n"
X						, aha_mbx[unit].mbi[i].stat, i);
X					printf("addr = 0x%x\n", ccb);
X				}
X			}
X			if(ccb)
X			{
X				aha_remove_timeout(ccb);
X				aha_done(unit,ccb);
X			}
X			aha_mbx[unit].mbi[i].stat = AHA_MBI_FREE;
X		}
X	}
X	return(1);
X}
X
X/***********************************************\
X* A ccb (and hence a mbx-out is put onto the 	*
X* free list.					*
X\***********************************************/
Xaha_free_ccb(unit,ccb, flags)
Xstruct aha_ccb *ccb;
X{
X	unsigned int opri;
X	
X	if(scsi_debug & PRINTROUTINES)
X		printf("ccb%d(0x%x)> ",unit,flags);
X	if (!(flags & SCSI_NOMASK)) 
X	  	opri = splbio();
X
X	ccb->next = aha_ccb_free[unit];
X	aha_ccb_free[unit] = ccb;
X	ccb->flags = CCB_FREE;
X	if(ccb->sooner || ccb->later)
X	{
X		printf("yikes, still in timeout queue\n");
X		aha_remove_timeout(ccb);
X	}
X	/***********************************************\
X	* If there were none, wake abybody waiting for	*
X	* one to come free, starting with queued entries*
X	\***********************************************/
X	if (!ccb->next) {
X		wakeup(&aha_ccb_free[unit]);
X	}
X	if (!(flags & SCSI_NOMASK)) 
X		splx(opri);
X}
X
X/***********************************************\
X* Get a free ccb (and hence mbox-out entry)	*
X\***********************************************/
Xstruct aha_ccb *
Xaha_get_ccb(unit,flags)
X{
X	unsigned opri;
X	struct aha_ccb *rc;
X
X	if(scsi_debug & PRINTROUTINES)
X		printf("<ccb%d(0x%x) ",unit,flags);
X	if (!(flags & SCSI_NOMASK)) 
X	  	opri = splbio();
X	/***********************************************\
X	* If we can and have to, sleep waiting for one	*
X	* to come free					*
X	\***********************************************/
X	while ((!(rc = aha_ccb_free[unit])) && (!(flags & SCSI_NOSLEEP)))
X	{
X		sleep(&aha_ccb_free[unit], PRIBIO);
X	}
X	if (rc) 
X	{
X		aha_ccb_free[unit] = aha_ccb_free[unit]->next;
X		rc->flags = CCB_ACTIVE;
X	}
X	if (!(flags & SCSI_NOMASK)) 
X		splx(opri);
X	return(rc);
X}
X		
X
X/***********************************************\
X* We have a ccb which has been processed by the	*
X* adaptor, now we look to see how the operation	*
X* went. Wake up the owner if waiting		*
X\***********************************************/
Xaha_done(unit,ccb)
Xstruct aha_ccb *ccb;
X{
X	struct	scsi_sense_data *s1,*s2;
X	struct	scsi_xfer *xs = ccb->xfer;
X
X	if(scsi_debug & PRINTROUTINES )
X		printf("aha_done ");
X	/***********************************************\
X	* Otherwise, put the results of the operation	*
X	* into the xfer and call whoever started it	*
X	\***********************************************/
X	if(!(xs->flags & INUSE))
X	{
X		printf("exiting but not in use! ");
X		Debugger();
X	}
X	if (  	(	ccb->host_stat != AHA_OK 
X			|| ccb->target_stat != SCSI_OK)
X	      && (!(xs->flags & SCSI_ERR_OK)))
X	{
X		s1 = (struct scsi_sense_data *)(((char *)(&ccb->scsi_cmd)) 
X				+ ccb->scsi_cmd_length);
X		s2 = &(xs->sense);
X
X		if(ccb->host_stat)
X		{
X			switch(ccb->host_stat)
X			{
X			case	AHA_ABORTED:
X			case	AHA_SEL_TIMEOUT:	/* No response */
X				xs->error = XS_TIMEOUT;
X				break;
X			default:	/* Other scsi protocol messes */
X				xs->error = XS_DRIVER_STUFFUP;
X				if (aha_debug > 1)
X				{
X					printf("host_stat%x\n",
X						ccb->host_stat);
X				}
X			}
X
X		}
X		else
X		{
X			switch(ccb->target_stat)
X			{
X			case 0x02:
X				/* structure copy!!!!!*/
X				*s2=*s1;
X				xs->error = XS_SENSE;
X				break;
X			case 0x08:
X				xs->error = XS_BUSY;
X				break;
X			default:
X				if (aha_debug > 1)
X				{
X					printf("target_stat%x\n",
X						ccb->target_stat);
X				}
X				xs->error = XS_DRIVER_STUFFUP;
X			}
X		}
X	}
X	else		/* All went correctly  OR errors expected */
X	{
X		xs->resid = 0;
X	}
X	xs->flags |= ITSDONE;
X	aha_free_ccb(unit,ccb, xs->flags);
X	if(xs->when_done)
X		(*(xs->when_done))(xs->done_arg,xs->done_arg2);
X}
X
X
X/***********************************************\
X* Start the board, ready for normal operation	*
X\***********************************************/
Xaha_init(unit)
Xint	unit;
X{
X	unsigned char ad[3];
X	volatile int i,sts;
X	struct	aha_config conf;
X
X	/***********************************************\
X	* reset board, If it doesn't respond, assume 	*
X	* that it's not there.. good for the probe	*
X	\***********************************************/
X
X	outb(AHA_CTRL_STAT_PORT, AHA_HRST|AHA_SRST);
X
X	for (i=0; i < AHA_RESET_TIMEOUT; i++)
X	{
X		sts = inb(AHA_CTRL_STAT_PORT) ;
X		if ( sts == (AHA_IDLE | AHA_INIT))
X			break;
X	}
X	if (i >= AHA_RESET_TIMEOUT)
X	{
X		if (aha_debug)
X			printf("aha_init: No answer from adaptec board\n");
X		return(ENXIO);
X	}
X
X	/***********************************************\
X	* Assume we have a board at this stage		*
X	* setup dma channel from jumpers and save int	*
X	* level						*
X	\***********************************************/
X#ifdef	__386BSD__
X	printf("aha%d reading board settings, ",unit);
X#define	PRNT(x)
X#else	__386BSD__
X	printf("aha%d:",unit);
X#define	PRNT(x) printf(x)
X#endif	__386BSD__
X	aha_cmd(unit,0, sizeof(conf), 0 ,&conf, AHA_CONF_GET);
X	switch(conf.chan)
X	{
X	case	CHAN0:
X		outb(0x0b, 0x0c);
X		outb(0x0a, 0x00);
X		aha_dma[unit] = 0;
X		PRNT("dma=0 ");
X		break;
X	case	CHAN5:
X		outb(0xd6, 0xc1);
X		outb(0xd4, 0x01);
X		aha_dma[unit] = 5;
X		PRNT("dma=5 ");
X		break;
X	case	CHAN6:
X		outb(0xd6, 0xc2);
X		outb(0xd4, 0x02);
X		aha_dma[unit] = 6;
X		PRNT("dma=6 ");
X		break;
X	case	CHAN7:
X		outb(0xd6, 0xc3);
X		outb(0xd4, 0x03);
X		aha_dma[unit] = 7;
X		PRNT("dma=7 ");
X		break;
X	default:
X		printf("illegal dma jumper setting\n");
X		return(EIO);
X	}
X	switch(conf.intr)
X	{
X	case	INT9:
X		aha_int[unit] = 9;
X		PRNT("int=9 ");
X		break;
X	case	INT10:
X		aha_int[unit] = 10;
X		PRNT("int=10 ");
X		break;
X	case	INT11:
X		aha_int[unit] = 11;
X		PRNT("int=11 ");
X		break;
X	case	INT12:
X		aha_int[unit] = 12;
X		PRNT("int=12 ");
X		break;
X	case	INT14:
X		aha_int[unit] = 14;
X		PRNT("int=14 ");
X		break;
X	case	INT15:
X		aha_int[unit] = 15;
X		PRNT("int=15 ");
X		break;
X	default:
X		printf("illegal int jumper setting\n");
X		return(EIO);
X	}
X	/* who are we on the scsi bus */
X	aha_scsi_dev[unit] = conf.scsi_dev;
X
X
X	/***********************************************\
X	* Initialize memory transfer speed		*
X	\***********************************************/
X	if(!(aha_set_bus_speed(unit)))
X	{
X		return(EIO);
X	}
X	
X
X	/***********************************************\
X	* Initialize mail box 				*
X	\***********************************************/
X
X	lto3b(KVTOPHYS(&aha_mbx[unit]), ad);
X
X	aha_cmd(unit,4, 0, 0, 0, AHA_MBX_INIT,
X			AHA_MBX_SIZE,
X			ad[0],
X			ad[1],
X			ad[2]);
X
X
X	/***********************************************\
X	* link the ccb's with the mbox-out entries and	*
X	* into a free-list				*
X	\***********************************************/
X	for (i=0; i < AHA_MBX_SIZE; i++) {
X		aha_ccb[unit][i].next = aha_ccb_free[unit];
X		aha_ccb_free[unit] = &aha_ccb[unit][i];
X		aha_ccb_free[unit]->flags = CCB_FREE;
X		aha_ccb_free[unit]->mbx = &aha_mbx[unit].mbo[i];
X		lto3b(KVTOPHYS(aha_ccb_free[unit]), aha_mbx[unit].mbo[i].ccb_addr);
X	}
X
X	/***********************************************\
X	* Note that we are going and return (to probe)	*
X	\***********************************************/
X	aha_initialized[unit]++;
X	return(0);
X}
X
X
X
X
X
Xvoid ahaminphys(bp)
Xstruct	buf *bp;
X{
X#ifdef	MACH
X#if !defined(OSF)
X	bp->b_flags |= B_NPAGES;		/* can support scat/gather */
X#endif /* !defined(OSF) */
X#endif	MACH
X/*	aha seems to explode with 17 segs (64k may require 17 segs) */
X/* 	on old boards so use a max of 16 segs if you have problems here*/
X	if(bp->b_bcount > ((AHA_NSEG - 1) * PAGESIZ))
X	{
X		bp->b_bcount = ((AHA_NSEG - 1) * PAGESIZ);
X	}
X}
X	
X/***********************************************\
X* start a scsi operation given the command and	*
X* the data address. Also needs the unit, target	*
X* and lu					*
X\***********************************************/
Xint	aha_scsi_cmd(xs)
Xstruct scsi_xfer *xs;
X{
X	struct	scsi_sense_data *s1,*s2;
X	struct aha_ccb *ccb;
X	struct aha_scat_gath *sg;
X	int	seg;	/* scatter gather seg being worked on */
X	int i	= 0;
X	int rc	=  0;
X	int	thiskv;
X	int	thisphys,nextphys;
X	int	unit =xs->adapter;
X	int	bytes_this_seg,bytes_this_page,datalen,flags;
X	struct	iovec	*iovp;
X	int	s;
X
X	if(scsi_debug & PRINTROUTINES)
X		printf("aha_scsi_cmd ");
X	/***********************************************\
X	* get a ccb (mbox-out) to use. If the transfer	*
X	* is from a buf (possibly from interrupt time)	*
X	* then we can't allow it to sleep		*
X	\***********************************************/
X	flags = xs->flags;
X	if(!(flags & INUSE))
X	{
X		printf("not in use!");
X		Debugger();
X		xs->flags |= INUSE;
X	}
X	if(flags & ITSDONE)
X	{
X		printf("Already done! check device retry code ");
X		Debugger();
X		xs->flags &= ~ITSDONE;
X	}
X	if(xs->bp) flags |= (SCSI_NOSLEEP); /* just to be sure */
X	if (!(ccb = aha_get_ccb(unit,flags)))
X	{
X		xs->error = XS_DRIVER_STUFFUP;
X		return(TRY_AGAIN_LATER);
X	}
X
X	if (ccb->mbx->cmd != AHA_MBO_FREE)
X		printf("MBO not free\n");
X
X	/***********************************************\
X	* Put all the arguments for the xfer in the ccb	*
X	\***********************************************/
X	ccb->xfer		=	xs;
X	if(flags & SCSI_RESET)
X	{
X		ccb->opcode	=	AHA_RESET_CCB;
X	}
X	else
X	{
X		/* can't use S/G if zero length */
X		ccb->opcode	=	(xs->datalen?
X						AHA_INIT_SCAT_GATH_CCB
X						:AHA_INITIATOR_CCB);
X	}
X	ccb->target		=	xs->targ;;
X	ccb->data_out		=	0;
X	ccb->data_in		=	0;
X	ccb->lun		=	xs->lu;
X	ccb->scsi_cmd_length	=	xs->cmdlen;
X	ccb->req_sense_length	=	sizeof(ccb->scsi_sense);
X
X	if((xs->datalen) && (!(flags & SCSI_RESET)))
X	{ /* can use S/G only if not zero length */
X		lto3b(KVTOPHYS(ccb->scat_gath), ccb->data_addr );
X		sg		=	ccb->scat_gath ;
X		seg 		=	0;
X		if(flags & SCSI_DATA_UIO)
X		{
X			iovp = ((struct uio *)xs->data)->uio_iov;
X			datalen = ((struct uio *)xs->data)->uio_iovcnt;
X			while ((datalen) && (seg < AHA_NSEG))
X			{
X				lto3b(iovp->iov_base,&(sg->seg_addr));
X				lto3b(iovp->iov_len,&(sg->seg_len));
X				if(scsi_debug & SHOWSCATGATH)
X					printf("(0x%x@0x%x)"
X							,iovp->iov_len
X							,iovp->iov_base);
X				sg++;
X				iovp++;
X				seg++;
X				datalen--;
X			}
X		}
X		else
X		{
X			/***********************************************\
X			* Set up the scatter gather block		*
X			\***********************************************/
X		
X			if(scsi_debug & SHOWSCATGATH)
X				printf("%d @0x%x:- ",xs->datalen,xs->data);
X			datalen		=	xs->datalen;
X			thiskv		=	(int)xs->data;
X			thisphys	=	KVTOPHYS(thiskv);
X		
X			while ((datalen) && (seg < AHA_NSEG))
X			{
X				bytes_this_seg	= 0;
X	
X				/* put in the base address */
X				lto3b(thisphys,&(sg->seg_addr));
X		
X				if(scsi_debug & SHOWSCATGATH)
X					printf("0x%x",thisphys);
X	
X				/* do it at least once */
X				nextphys = thisphys;	
X				while ((datalen) && (thisphys == nextphys))
X				/***************************************\
X				* This page is contiguous (physically)	*
X				* with the the last, just extend the	*
X				* length				*
X				\***************************************/
X				{
X					/** how far to the end of the page ***/
X					nextphys = (thisphys & (~(PAGESIZ - 1)))
X								+ PAGESIZ;
X					bytes_this_page	= nextphys - thisphys;
X					/**** or the data ****/
X					bytes_this_page	= min(bytes_this_page
X								,datalen);
X					bytes_this_seg	+= bytes_this_page;
X					datalen		-= bytes_this_page;
X		
X					/**** get more ready for the next page ****/
X					thiskv	= (thiskv & (~(PAGESIZ - 1)))
X								+ PAGESIZ;
X					if(datalen)
X						thisphys = KVTOPHYS(thiskv);
X				}
X				/***************************************\
X				* next page isn't contiguous, finish the seg*
X				\***************************************/
X				if(scsi_debug & SHOWSCATGATH)
X					printf("(0x%x)",bytes_this_seg);
X				lto3b(bytes_this_seg,&(sg->seg_len));	
X				sg++;
X				seg++;
X			}
X		}
X		lto3b(seg * sizeof(struct aha_scat_gath),ccb->data_length);
X		if(scsi_debug & SHOWSCATGATH)
X			printf("\n");
X		if (datalen)
X		{ /* there's still data, must have run out of segs! */
X			printf("aha_scsi_cmd%d: more than %d DMA segs\n",
X				unit,AHA_NSEG);
X			xs->error = XS_DRIVER_STUFFUP;
X			aha_free_ccb(unit,ccb,flags);
X			return(HAD_ERROR);
X		}
X
X	}
X	else
X	{	/* No data xfer, use non S/G values */
X		lto3b(0, ccb->data_addr );
X		lto3b(0,ccb->data_length);
X	}
X	lto3b(0, ccb->link_addr );
X	/***********************************************\
X	* Put the scsi command in the ccb and start it	*
X	\***********************************************/
X	if(!(flags & SCSI_RESET))
X		bcopy(xs->cmd, &ccb->scsi_cmd, ccb->scsi_cmd_length);
X	if(scsi_debug & SHOWCOMMANDS)
X	{
X		u_char	*b = (u_char *)&ccb->scsi_cmd;
X		if(!(flags & SCSI_RESET))
X		{
X			int i = 0;
X			printf("aha%d:%d:%d-"
X				,unit
X				,ccb->target
X				,ccb->lun );
X				while(i < ccb->scsi_cmd_length )
X				{
X					if(i) printf(",");
X					 printf("%x",b[i++]);
X				}
X		}
X		else
X		{
X			printf("aha%d:%d:%d-RESET- " 
X				,unit 
X				,ccb->target
X				,ccb->lun
X			);
X		}
X	}
X	if (!(flags & SCSI_NOMASK))
X	{
X		s= splbio(); /* stop instant timeouts */
X		aha_add_timeout(ccb,xs->timeout);
X		aha_startmbx(ccb->mbx);
X		/***********************************************\
X		* Usually return SUCCESSFULLY QUEUED		*
X		\***********************************************/
X		splx(s);
X		if(scsi_debug & TRACEINTERRUPTS)
X			printf("sent ");
X		return(SUCCESSFULLY_QUEUED);
X	}
X	aha_startmbx(ccb->mbx);
X	if(scsi_debug & TRACEINTERRUPTS)
X		printf("cmd_sent, waiting ");
X	/***********************************************\
X	* If we can't use interrupts, poll on completion*
X	\***********************************************/
X	{
X		int done = 0;
X		int count = delaycount * xs->timeout / AHA_SCSI_TIMEOUT_FUDGE;
X		while((!done) && count)
X		{
X			i=0;
X			while ( (!done) && i<AHA_MBX_SIZE)
X			{
X				if ((aha_mbx[unit].mbi[i].stat != AHA_MBI_FREE )
X				   && (PHYSTOKV(_3btol(aha_mbx[unit].mbi[i].ccb_addr)
X					== (int)ccb)))
X				{
X					aha_mbx[unit].mbi[i].stat = AHA_MBI_FREE;
X					aha_done(unit,ccb);
X					done++;
X				}
X				i++;
X			}
X			count--;
X		}
X		if (!count)
X		{
X			if (!(xs->flags & SCSI_SILENT))
X				printf("cmd fail\n");
X			aha_abortmbx(ccb->mbx);
X			count = delaycount * 2000 / AHA_SCSI_TIMEOUT_FUDGE;
X			while((!done) && count)
X			{
X				i=0;
X				while ( (!done) && i<AHA_MBX_SIZE)
X				{
X					if ((aha_mbx[unit].mbi[i].stat != AHA_MBI_FREE )
X				   	&& (PHYSTOKV(_3btol(aha_mbx[unit].mbi[i].ccb_addr)
X						== (int)ccb)))
X					{
X						aha_mbx[unit].mbi[i].stat = AHA_MBI_FREE;
X						aha_done(unit,ccb);
X						done++;
X					}
X					i++;
X				}
X				count--;
X			}
X			if(!count)
X			{
X				printf("abort failed in wait\n");
X				ccb->mbx->cmd = AHA_MBO_FREE;
X			}
X			aha_free_ccb(unit,ccb,flags);
X			ahaintr(unit);
X			xs->error = XS_DRIVER_STUFFUP;
X			return(HAD_ERROR);
X		}
X		ahaintr(unit);
X		if(xs->error) return(HAD_ERROR);
X		return(COMPLETE);
X
X	} 
X}
X/***************************************************************\
X* try each speed in turn, when we find one that works, use	*
X* the NEXT one for a safety margin, unless that doesn't exist	*
X* or doesn't work. returns the nSEC value of the time used	*
X* or 0 if it could get a working speed ( or the NEXT speed 	*
X* failed)							*
X\***************************************************************/
X
Xint	aha_set_bus_speed(unit)
Xint	unit;
X{
X	int	speed;
X	int	retval,retval2;
X
X#ifdef	EISA
X	speed = 0; /* start at the fastest */
X#else	EISA
X	speed = 1; /* 100 ns can crash some ISA busses (!?!) */
X#endif	EISA
X	while (1)
X	{
X		retval = aha_bus_speed_check(unit,speed);
X		if(retval == HAD_ERROR) 
X		{
X			printf("no working bus speed!!!\n");
X			return(0);
X		}
X		if(retval == 0)
X		{
X			speed++;
X		}
X		else	/* Go one slower to be safe */
X		{	/* unless eisa at 100 ns.. trust it */
X			if(speed != 0)
X			{
X				speed++;
X			}
X			printf("%d nSEC ok, use ",retval);
X			retval2 = aha_bus_speed_check(unit,speed);
X			if(retval2 == HAD_ERROR) /* retval is slowest already */
X			{
X				printf("marginal ");
X				retval2 = retval;
X			}
X			if(retval2)
X			{
X				printf("%d nSEC ",retval2);
X				return(retval2);
X			}
X			else
X			{
X				printf(".. slower failed, abort.\n",retval);
X				return(0);
X			}
X
X		}
X	}
X}
X
X/***************************************************************\
X* Set the DMA speed to the Nth speed and try an xfer. If it	*
X* fails return 0, if it succeeds return the nSec value selected	*
X* If there is no such speed return HAD_ERROR.			*
X\***************************************************************/
Xstatic	struct bus_speed
X{
X	char	arg;
X	int	nsecs;
X}aha_bus_speeds[] =
X{
X	{0x88,100},
X	{0x99,150},
X	{0xaa,200},
X	{0xbb,250},
X	{0xcc,300},
X	{0xdd,350},
X	{0xee,400},
X	{0xff,450}
X};
Xstatic	char aha_test_string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz!@";
X
Xint aha_bus_speed_check(unit,speed)
Xint	unit,speed;
X{
X	int	numspeeds = sizeof(aha_bus_speeds)/sizeof(struct bus_speed);
X	u_char	ad[3];
X
X	/*******************************************************\
X	* Check we have such an entry				*
X	\*******************************************************/
X	if(speed >= numspeeds) return(HAD_ERROR);	/* illegal speed */
X	
X	/*******************************************************\
X	* Set the dma-speed					*
X	\*******************************************************/
X	aha_cmd(unit,1, 0, 0, 0, AHA_SPEED_SET,aha_bus_speeds[speed].arg);
X
X	/*******************************************************\
X	* put the test data into the buffer and calculate	*
X	* it's address. Read it onto the board			*
X	\*******************************************************/
X	strcpy(aha_scratch_buf,aha_test_string);
X	lto3b(KVTOPHYS(aha_scratch_buf),ad);
X
X	aha_cmd(unit,3, 0, 0, 0, AHA_WRITE_FIFO, ad[0], ad[1], ad[2]);
X
X	/*******************************************************\
X	* clear the buffer then copy the contents back from the	*
X	* board.						*
X	\*******************************************************/
X	bzero(aha_scratch_buf,54);	/* 54 bytes transfered by test */
X
X	aha_cmd(unit,3, 0, 0, 0, AHA_READ_FIFO, ad[0], ad[1], ad[2]);
X
X	/*******************************************************\
X	* Compare the original data and the final data and	*
X	* return the correct value depending upon the result	*
X	\*******************************************************/
X	if(strcmp(aha_test_string,aha_scratch_buf))
X	{	/* copy failed.. assume too fast */
X		return(0);
X	}
X	else
X	{	/* copy succeded assume speed ok */
X		return(aha_bus_speeds[speed].nsecs);
X	}
X}
X
X
X/*
X *                +----------+    +----------+    +----------+
X * aha_soonest--->|    later |--->|     later|--->|     later|-->0
X *                | [Delta]  |    | [Delta]  |    | [Delta]  |
X *           0<---|sooner    |<---|sooner    |<---|sooner    |<---aha_latest
X *                +----------+    +----------+    +----------+
X *
X *     aha_furtherest = sum(Delta[1..n])
X */
Xaha_add_timeout(ccb,time)
Xstruct	aha_ccb	*ccb;
Xint	time;
X{
X	int	timeprev;
X	struct aha_ccb *prev;
X	int	s = splbio();
X
X	if(prev = aha_latest) /* yes, an assign */
X	{
X		timeprev = aha_furtherest;
X	}
X	else
X	{
X		timeprev = 0;
X	}
X	while(prev && (timeprev > time)) 
X	{
X		timeprev -= prev->delta;
X		prev = prev->sooner;
X	}
X	if(prev)
X	{
X		ccb->delta = time - timeprev;
X		if( ccb->later = prev->later) /* yes an assign */
X		{
X			ccb->later->sooner = ccb;
X			ccb->later->delta -= ccb->delta;
X		}
X		else
X		{
X			aha_furtherest = time;
X			aha_latest = ccb;
X		}
X		ccb->sooner = prev;
X		prev->later = ccb;
X	}
X	else
X	{
X		if( ccb->later = aha_soonest) /* yes, an assign*/
X		{
X			ccb->later->sooner = ccb;
X			ccb->later->delta -= time;
X		}
X		else
X		{
X			aha_furtherest = time;
X			aha_latest = ccb;
X		}
X		ccb->delta = time;
X		ccb->sooner = (struct aha_ccb *)0;
X		aha_soonest = ccb;
X	}
X	splx(s);
X}
X
Xaha_remove_timeout(ccb)
Xstruct	aha_ccb	*ccb;
X{
X	int	s = splbio();
X
X	if(ccb->sooner)
X	{
X		ccb->sooner->later = ccb->later;
X	}
X	else
X	{
X		aha_soonest = ccb->later;
X	}
X	if(ccb->later)
X	{
X		ccb->later->sooner = ccb->sooner;
X		ccb->later->delta += ccb->delta;
X	}
X	else
X	{
X		aha_latest = ccb->sooner;
X		aha_furtherest -= ccb->delta;
X	}
X	ccb->sooner = ccb->later = (struct aha_ccb *)0;
X	splx(s);
X}
X
X
Xextern int 	hz;
X#define ONETICK 500 /* milliseconds */
X#define SLEEPTIME ((hz * 1000) / ONETICK)
Xaha_timeout(arg)
Xint	arg;
X{
X	struct  aha_ccb  *ccb;
X	int	unit;
X	int	s	= splbio();
X
X	while( ccb = aha_soonest )
X	{
X		if(ccb->delta <= ONETICK)
X		/***********************************************\
X		* It has timed out, we need to do some work	*
X		\***********************************************/
X		{
X			unit = ccb->xfer->adapter;
X			printf("aha%d: device %d timed out ",unit
X				,ccb->xfer->targ);
X
X			/***************************************\
X			* Unlink it from the queue		*
X			\***************************************/
X			aha_remove_timeout(ccb);
X
X			/***************************************\
X			* If The ccb's mbx is not free, then	*
X			* the board has gone south		*
X			\***************************************/
X			if(ccb->mbx->cmd != AHA_MBO_FREE)
X			{
X				printf("aha%d not taking commands!\n"
X							,unit);
X				Debugger();
X			}
X			/***************************************\
X			* If it has been through before, then	*
X			* a previous abort has failed, don't	*
X			* try abort again			*
X			\***************************************/
X			if(ccb->flags == CCB_ABORTED) /* abort timed out */
X			{
X				printf(" AGAIN\n");
X				ccb->xfer->retries = 0;	/* I MEAN IT ! */
X				ccb->host_stat = AHA_ABORTED;
X				aha_done(unit,ccb);
X			}
X			else	/* abort the operation that has timed out */
X			{
X				printf("\n");
X				aha_abortmbx(ccb->mbx);
X						/* 2 secs for the abort */
X				aha_add_timeout(ccb,2000 + ONETICK);
X				ccb->flags = CCB_ABORTED;
X			}
X		}
X		else
X		/***********************************************\
X		* It has not timed out, adjust and leave	*
X		\***********************************************/
X		{
X			ccb->delta -= ONETICK;
X			aha_furtherest -= ONETICK;
X			break;
X		}
X	}
X	splx(s);
X	timeout(aha_timeout,arg,SLEEPTIME);
X}
END-of-i386/isa/aha1542.c
exit

Newsgroups: comp.unix.bsd