*BSD News Article 5517


Return to BSD News archive

Newsgroups: comp.unix.bsd
Path: sserve!manuel!munnari.oz.au!spool.mu.edu!uwm.edu!caen!uakari.primate.wisc.edu!ames!agate!tfs.com!tfs.com!julian
From: julian@tfs.com (Julian Elischer)
Subject: new new scsi release beta2 part 4 of 5
Message-ID: <1992Sep23.222227.17120@tfs.com>
Organization: TRW Financial Systems
Date: Wed, 23 Sep 1992 22:22:27 GMT
Lines: 1819

# 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/isa/aha1542.c
#	/dev/MAKEDEV
#
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: adaptec.c,v $
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/ioctl.h>
X#include <sys/buf.h>
X#include <sys/proc.h>
X#include <sys/user.h>
X#if defined(OSF)
X#include <i386/AT386/atbus.h>
X#else /* defined(OSF) */
X#ifndef	MACH
X/*#include <i386/include/pio.h>*/
X#include <i386/isa/isa_device.h>
X#define isa_dev isa_device
X#define dev_unit id_unit
X#define dev_addr id_iobase
X#else	/*not mach */
X#include <i386at/atbus.h>
X#include <i386/pio.h>
X#endif	MACH
X#endif /* defined(OSF) */
X
X#if	defined(OSF) || defined(MACH)
X#include <i386/ipl.h>
X#include <i386at/scsi.h>
X#include <i386at/scsiconf.h>
X#else	defined(OSF) || defined(MACH)
X#include <scsi/scsi_all.h>
X#include <scsi/scsiconf.h>
X#endif	defined(OSF) || defined(MACH)
X
X#if defined(OSF)
X#include <sys/table.h>
X#include <i386/handler.h>
X#include <i386/dispatcher.h>
X#endif /* defined(OSF) */
X
X#ifdef	__386BSD__
X#define Debugger() panic("should call debugger here (adaptec.c)")
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#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 *soonest = (struct  aha_ccb *)0;
Xstruct	aha_ccb	*latest = (struct  aha_ccb *)0;
Xlong int	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};
X#endif
X#ifdef	__386BSD__
Xstruct	isa_driver	ahadriver = { ahaprobe, ahaattach, "aha",};
X#endif	__386BSD__
Xint			(*ahaintrs[])() = {ahaintr, 0};
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();
X
Xstruct	scsi_switch	aha_switch =
X{
X	aha_scsi_cmd,
X	ahaminphys
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* 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				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		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#if !defined(OSF)
X#ifdef	MACH
X	bp->b_flags |= B_NPAGES;		/* can support scat/gather */
X#endif	MACH
X/*	if(bp->b_bcount > (16 * PAGESIZ)) bp->b_bcount = (16 * PAGESIZ);*/
X/*	aha seems to explode with 17 segs (64k may require 17 segs) */
X/* 	so use a max of 16 segs */
X	if(bp->b_bcount > (15 * PAGESIZ)) bp->b_bcount = (15 * PAGESIZ);
X#else /* !defined(OSF) */
X 	if(bp->b_bcount > 4096) bp->b_bcount = 4096;
X#endif /* !defined(OSF) */
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		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}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 test_string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz!@";
X
Xint aha_bus_speed_check(unit,speed)
Xint	unit,speed;
X{
X	int	numspeeds = sizeof(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,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,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(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(bus_speeds[speed].nsecs);
X	}
X}
X
X
X/*
X *              +----------+     +----------+     +----------+
X * soonest----->|    later |---->|     later|---->|     later|--->0
X *              | [Delta]  |     | [Delta]  |     | [Delta]  |
X *       0<-----|sooner    |<----|sooner    |<----|sooner    |<----latest
X *              +----------+     +----------+     +----------+
X *
X *     furtherest = sum(Delta[1..n])
X */
Xadd_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 = latest) /* yes, an assign */
X	{
X		timeprev = 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			furtherest = time;
X			latest = ccb;
X		}
X		ccb->sooner = prev;
X		prev->later = ccb;
X	}
X	else
X	{
X		if( ccb->later = soonest) /* yes, an assign*/
X		{
X			ccb->later->sooner = ccb;
X			ccb->later->delta -= time;
X		}
X		else
X		{
X			furtherest = time;
X			latest = ccb;
X		}
X		ccb->delta = time;
X		ccb->sooner = (struct aha_ccb *)0;
X		soonest = ccb;
X	}
X	splx(s);
X}
X
Xremove_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		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		latest = ccb->sooner;
X		furtherest -= ccb->delta;
X	}
X	ccb->sooner = ccb->later = (struct aha_ccb *)0;
X	splx(s);
X}
X
X
Xextern int sd_debug;
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 = 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			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				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			furtherest -= ONETICK;
X			break;
X		}
X	}
X	splx(s);
X	timeout(aha_timeout,arg,SLEEPTIME);
X}
END-of-i386/isa/aha1542.c
echo x - /dev/MAKEDEV
sed 's/^X//' >/dev/MAKEDEV << 'END-of-/dev/MAKEDEV'
X#!/bin/sh -
X#
X# Copyright (c) 1990 The Regents of the University of California.
X# All rights reserved.
X#
X# Written and contributed by W. Jolitz 12/90
X#
X# Redistribution and use in source and binary forms are permitted provided
X# that: (1) source distributions retain this entire copyright notice and
X# comment, and (2) distributions including binaries display the following
X# acknowledgement:  ``This product includes software developed by the
X# University of California, Berkeley and its contributors'' in the
X# documentation or other materials provided with the distribution and in
X# all advertising materials mentioning features or use of this software.
X# Neither the name of the University nor the names of its contributors may
X# be used to endorse or promote products derived from this software without
X# specific prior written permission.
X# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
X# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
X# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
X#
X#	@(#)MAKEDEV	5.2 (Berkeley) 6/22/90
X#
X# Device "make" file.  Valid arguments:
X#	std	standard devices
X#	local	configuration specific devices
X#
X# Tapes:
X#	wt*	QIC-interfaced (e.g. not SCSI) 3M cartridge tape
X#	st*	"NEW type scsi tapes"
X#
X# Disks:
X#	wd*	"winchester" disk drives (ST506,IDE,ESDI,RLL,...)
X#	fd*	"floppy" disk drives (3 1/2", 5 1/4")
X#	as*	"SCSI" disk/tape/CDROM drives
X#	sd*	"NEW type scsi disks"
X#
X# Terminal ports:
X#	com*	standard PC COM ports
X#
X# Pseudo terminals:
X#	pty*	set of 16 master and slave pseudo terminals
X#
X# Printers:
X#
X# Call units:
X#
X# Special purpose devices:
X#	flog*	kernel logging device
X#
X
XPATH=/sbin:/bin/:/usr/bin:/usr/sbin:
Xumask 77
Xfor i
Xdo
Xcase $i in
X
Xstd)
X	rm -f console drum mem kmdem null tty klog stdin stdout stderr
X	mknod console		c 0 0
X	mknod drum		c 4 0	; chmod 640 drum ; chgrp kmem drum
X	mknod kmem		c 2 1	; chmod 640 kmem ; chgrp kmem kmem
X	mknod mem		c 2 0	; chmod 640 mem ; chgrp kmem mem
X	mknod null		c 2 2	; chmod 666 null
X	mknod tty		c 1 0	; chmod 666 tty
X	mknod klog		c 7 0	; chmod 600 klog
X	mknod stdin		c 53 0	; chmod 666 stdin
X	mknod stdout		c 53 1	; chmod 666 stdout
X	mknod stderr		c 53 2	; chmod 666 stderr
X	rm -f fd/*
X	mkdir fd > /dev/null 2>&1
X	(cd fd && eval `echo "" | awk ' BEGIN { \
X		for (i = 0; i < 64; i++) \
X	 		printf("mknod %d c 53 %d;", i, i)}'`)
X	chown -R bin.bin fd
X	chmod 555 fd
X	chmod 666 fd/*
X	;;
X
Xwt*)
X	umask 2
X	mknod wt0 b 3 0
X	mknod rwt0 c 10 0
X	umask 77
X	;;
X
Xst*)
X	umask 2 ; unit=`expr $i : '..\(.*\)'`
X	case $i in
X	st*) name=st;  chr=14;;
X	esac
X	rm -f r$name$unit nr$name$unit er$name$unit enr$name$unit 
X	case $unit in
X	0|1|2|3|4|5|6)
X		mknod r${name}${unit}	c $chr `expr $unit '*' 16 + 0`
X		mknod nr${name}${unit}	c $chr `expr $unit '*' 16 + 1`
X		mknod er${name}${unit}	c $chr `expr $unit '*' 16 + 2`
X		mknod enr${name}${unit}	c $chr `expr $unit '*' 16 + 3`
X		chgrp operator r${name}${unit} nr${name}${unit} \
X			er${name}${unit} enr${name}${unit} 
X		chmod 640 r${name}${unit} nr${name}${unit} \
X			er${name}${unit} enr${name}${unit} 
X		;;
X	*)
X		echo bad unit for tape in: $i
X		;;
X	esac
X	umask 77
X	;;
X
Xcd*)
X	umask 2 ; unit=`expr $i : '..\(.*\)'`
X	case $i in
X	cd*) name=cd; blk=6; chr=15;;
X	esac
X	rm -f $name$unit? r$name$unit?
X	case $unit in
X	0|1|2|3|4|5|6)
X		mknod ${name}${unit}a	b $blk `expr $unit '*' 8 + 0`
X		mknod ${name}${unit}d	b $blk `expr $unit '*' 8 + 3`
X		mknod r${name}${unit}a	c $chr `expr $unit '*' 8 + 0`
X		mknod r${name}${unit}d	c $chr `expr $unit '*' 8 + 3`
X		chgrp operator ${name}${unit}[a-h] r${name}${unit}[a-h]
X		chmod 640 ${name}${unit}[a-h] r${name}${unit}[a-h]
X		;;
X	*)
X		echo bad unit for disk in: $i
X		;;
X	esac
X	umask 77
X	;;
Xfd*|wd*|as*|sd*)
X	umask 2 ; unit=`expr $i : '..\(.*\)'`
X	case $i in
X	fd*) name=fd; blk=2; chr=9;;
X	wd*) name=wd; blk=0; chr=3;;
X	as*) name=as; blk=4; chr=13;;
X	sd*) name=sd; blk=4; chr=13;;
X	esac
X	rm -f $name$unit? r$name$unit?
X	case $unit in
X	0|1|2|3|4|5|6)
X		mknod ${name}${unit}a	b $blk `expr $unit '*' 8 + 0`
X		mknod ${name}${unit}b	b $blk `expr $unit '*' 8 + 1`
X		mknod ${name}${unit}c	b $blk `expr $unit '*' 8 + 2`
X		mknod ${name}${unit}d	b $blk `expr $unit '*' 8 + 3`
X		mknod ${name}${unit}e	b $blk `expr $unit '*' 8 + 4`
X		mknod ${name}${unit}f	b $blk `expr $unit '*' 8 + 5`
X		mknod ${name}${unit}g	b $blk `expr $unit '*' 8 + 6`
X		mknod ${name}${unit}h	b $blk `expr $unit '*' 8 + 7`
X		mknod r${name}${unit}a	c $chr `expr $unit '*' 8 + 0`
X		mknod r${name}${unit}b	c $chr `expr $unit '*' 8 + 1`
X		mknod r${name}${unit}c	c $chr `expr $unit '*' 8 + 2`
X		mknod r${name}${unit}d	c $chr `expr $unit '*' 8 + 3`
X		mknod r${name}${unit}e	c $chr `expr $unit '*' 8 + 4`
X		mknod r${name}${unit}f	c $chr `expr $unit '*' 8 + 5`
X		mknod r${name}${unit}g	c $chr `expr $unit '*' 8 + 6`
X		mknod r${name}${unit}h	c $chr `expr $unit '*' 8 + 7`
X		chgrp operator ${name}${unit}[a-h] r${name}${unit}[a-h]
X		chmod 640 ${name}${unit}[a-h] r${name}${unit}[a-h]
X		;;
X	*)
X		echo bad unit for disk in: $i
X		;;
X	esac
X	umask 77
X	;;
X
Xcom*)
X	unit=`expr $i : 'com\(.*\)'`
X	rm -f com0$unit
X	mknod com0$unit c 8 $unit
X	;;
X
Xpty*)
X	class=`expr $i : 'pty\(.*\)'`
X	case $class in
X	0) offset=0 name=p;;
X	1) offset=16 name=q;;
X	2) offset=32 name=r;;
X	3) offset=48 name=s;;
X# Note that telnetd, rlogind, and xterm (at least) only look at p-s.
X	4) offset=64 name=t;;
X	*) echo bad unit for pty in: $i;;
X	esac
X	case $class in
X	0|1|2|3|4)
X		umask 0
X		eval `echo $offset $name | awk ' { b=$1; n=$2 } END {
X			for (i = 0; i < 16; i++)
X				printf("mknod tty%s%x c 5 %d; \
X					mknod pty%s%x c 6 %d; ", \
X					n, i, b+i, n, i, b+i); }'`
X		umask 77
X		if [ $class = 1 ]; then
X			mv ttyqf ttyv0; mv ptyqf ptyv0
X		fi
X		;;
X	esac
X	;;
X
Xlocal)
X	umask 0
X	sh MAKEDEV.local
X	;;
X
Xesac
Xdone
END-of-/dev/MAKEDEV
exit