xref: /linux/drivers/scsi/aha1542.c (revision f1bbef63442f36f5a0fc91f73da104aa8a39a873)
11da177e4SLinus Torvalds /* $Id: aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $
21da177e4SLinus Torvalds  *  linux/kernel/aha1542.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Tommy Thorn
51da177e4SLinus Torvalds  *  Copyright (C) 1993, 1994, 1995 Eric Youngdale
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Modified by Eric Youngdale
81da177e4SLinus Torvalds  *        Use request_irq and request_dma to help prevent unexpected conflicts
91da177e4SLinus Torvalds  *        Set up on-board DMA controller, such that we do not have to
101da177e4SLinus Torvalds  *        have the bios enabled to use the aha1542.
111da177e4SLinus Torvalds  *  Modified by David Gentzel
121da177e4SLinus Torvalds  *        Don't call request_dma if dma mask is 0 (for BusLogic BT-445S VL-Bus
131da177e4SLinus Torvalds  *        controller).
141da177e4SLinus Torvalds  *  Modified by Matti Aarnio
151da177e4SLinus Torvalds  *        Accept parameters from LILO cmd-line. -- 1-Oct-94
161da177e4SLinus Torvalds  *  Modified by Mike McLagan <mike.mclagan@linux.org>
171da177e4SLinus Torvalds  *        Recognise extended mode on AHA1542CP, different bit than 1542CF
181da177e4SLinus Torvalds  *        1-Jan-97
191da177e4SLinus Torvalds  *  Modified by Bjorn L. Thordarson and Einar Thor Einarsson
201da177e4SLinus Torvalds  *        Recognize that DMA0 is valid DMA channel -- 13-Jul-98
211da177e4SLinus Torvalds  *  Modified by Chris Faulhaber <jedgar@fxp.org>
221da177e4SLinus Torvalds  *        Added module command-line options
231da177e4SLinus Torvalds  *        19-Jul-99
24726a6459SJoe Perches  *  Modified by Adam Fritzler
25a88dc06cSPaul Gortmaker  *        Added proper detection of the AHA-1640 (MCA, now deleted)
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <linux/module.h>
291da177e4SLinus Torvalds #include <linux/interrupt.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/types.h>
321da177e4SLinus Torvalds #include <linux/string.h>
331da177e4SLinus Torvalds #include <linux/ioport.h>
341da177e4SLinus Torvalds #include <linux/delay.h>
351da177e4SLinus Torvalds #include <linux/proc_fs.h>
361da177e4SLinus Torvalds #include <linux/init.h>
371da177e4SLinus Torvalds #include <linux/spinlock.h>
38643a7c43SOndrej Zary #include <linux/isa.h>
39643a7c43SOndrej Zary #include <linux/pnp.h>
401da177e4SLinus Torvalds #include <linux/blkdev.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <asm/dma.h>
441da177e4SLinus Torvalds #include <asm/io.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include "scsi.h"
471da177e4SLinus Torvalds #include <scsi/scsi_host.h>
481da177e4SLinus Torvalds #include "aha1542.h"
491da177e4SLinus Torvalds #include <linux/stat.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #ifdef DEBUG
521da177e4SLinus Torvalds #define DEB(x) x
531da177e4SLinus Torvalds #else
541da177e4SLinus Torvalds #define DEB(x)
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds    static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $";
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /* The adaptec can be configured for quite a number of addresses, but
621da177e4SLinus Torvalds    I generally do not want the card poking around at random.  We allow
631da177e4SLinus Torvalds    two addresses - this allows people to use the Adaptec with a Midi
641da177e4SLinus Torvalds    card, which also used 0x330 -- can be overridden with LILO! */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #define MAXBOARDS 4		/* Increase this and the sizes of the
671da177e4SLinus Torvalds 				   arrays below, if you need more.. */
681da177e4SLinus Torvalds 
69a88dc06cSPaul Gortmaker /* Boards 3,4 slots are reserved for ISAPnP scans */
701da177e4SLinus Torvalds 
71643a7c43SOndrej Zary static unsigned int bases[MAXBOARDS] = {0x330, 0x334, 0, 0};
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* set by aha1542_setup according to the command line; they also may
741da177e4SLinus Torvalds    be marked __initdata, but require zero initializers then */
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static int setup_called[MAXBOARDS];
771da177e4SLinus Torvalds static int setup_buson[MAXBOARDS];
781da177e4SLinus Torvalds static int setup_busoff[MAXBOARDS];
79643a7c43SOndrej Zary static int setup_dmaspeed[MAXBOARDS] = { -1, -1, -1, -1 };
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /*
821da177e4SLinus Torvalds  * LILO/Module params:  aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * Where:  <PORTBASE> is any of the valid AHA addresses:
851da177e4SLinus Torvalds  *                      0x130, 0x134, 0x230, 0x234, 0x330, 0x334
861da177e4SLinus Torvalds  *         <BUSON>  is the time (in microsecs) that AHA spends on the AT-bus
871da177e4SLinus Torvalds  *                  when transferring data.  1542A power-on default is 11us,
881da177e4SLinus Torvalds  *                  valid values are in range: 2..15 (decimal)
891da177e4SLinus Torvalds  *         <BUSOFF> is the time that AHA spends OFF THE BUS after while
901da177e4SLinus Torvalds  *                  it is transferring data (not to monopolize the bus).
911da177e4SLinus Torvalds  *                  Power-on default is 4us, valid range: 1..64 microseconds.
921da177e4SLinus Torvalds  *         <DMASPEED> Default is jumper selected (1542A: on the J1),
931da177e4SLinus Torvalds  *                  but experimenter can alter it with this.
941da177e4SLinus Torvalds  *                  Valid values: 5, 6, 7, 8, 10 (MB/s)
951da177e4SLinus Torvalds  *                  Factory default is 5 MB/s.
961da177e4SLinus Torvalds  */
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #if defined(MODULE)
9990ab5ee9SRusty Russell static bool isapnp = 0;
1001da177e4SLinus Torvalds static int aha1542[] = {0x330, 11, 4, -1};
1011da177e4SLinus Torvalds module_param_array(aha1542, int, NULL, 0);
1021da177e4SLinus Torvalds module_param(isapnp, bool, 0);
1031da177e4SLinus Torvalds #else
1041da177e4SLinus Torvalds static int isapnp = 1;
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #define BIOS_TRANSLATION_1632 0	/* Used by some old 1542A boards */
1081da177e4SLinus Torvalds #define BIOS_TRANSLATION_6432 1	/* Default case these days */
1091da177e4SLinus Torvalds #define BIOS_TRANSLATION_25563 2	/* Big disk case */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds struct aha1542_hostdata {
1121da177e4SLinus Torvalds 	/* This will effectively start both of them at the first mailbox */
1131da177e4SLinus Torvalds 	int bios_translation;	/* Mapping bios uses - for compatibility */
1141da177e4SLinus Torvalds 	int aha1542_last_mbi_used;
1151da177e4SLinus Torvalds 	int aha1542_last_mbo_used;
1161da177e4SLinus Torvalds 	Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
1171da177e4SLinus Torvalds 	struct mailbox mb[2 * AHA1542_MAILBOXES];
1181da177e4SLinus Torvalds 	struct ccb ccb[AHA1542_MAILBOXES];
1191da177e4SLinus Torvalds };
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static DEFINE_SPINLOCK(aha1542_lock);
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds #define WAITnexttimeout 3000000
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static void setup_mailboxes(int base_io, struct Scsi_Host *shpnt);
1281da177e4SLinus Torvalds static int aha1542_restart(struct Scsi_Host *shost);
12987c4d7bcSJeff Garzik static void aha1542_intr_handle(struct Scsi_Host *shost);
1301da177e4SLinus Torvalds 
131*f1bbef63SOndrej Zary static inline void aha1542_intr_reset(u16 base)
132*f1bbef63SOndrej Zary {
133*f1bbef63SOndrej Zary 	outb(IRST, CONTROL(base));
134*f1bbef63SOndrej Zary }
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #define WAIT(port, mask, allof, noneof)					\
1371da177e4SLinus Torvalds  { register int WAITbits;						\
1381da177e4SLinus Torvalds    register int WAITtimeout = WAITnexttimeout;				\
1391da177e4SLinus Torvalds    while (1) {								\
1401da177e4SLinus Torvalds      WAITbits = inb(port) & (mask);					\
1411da177e4SLinus Torvalds      if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
1421da177e4SLinus Torvalds        break;                                                         	\
1431da177e4SLinus Torvalds      if (--WAITtimeout == 0) goto fail;					\
1441da177e4SLinus Torvalds    }									\
1451da177e4SLinus Torvalds  }
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /* Similar to WAIT, except we use the udelay call to regulate the
1481da177e4SLinus Torvalds    amount of time we wait.  */
1491da177e4SLinus Torvalds #define WAITd(port, mask, allof, noneof, timeout)			\
1501da177e4SLinus Torvalds  { register int WAITbits;						\
1511da177e4SLinus Torvalds    register int WAITtimeout = timeout;					\
1521da177e4SLinus Torvalds    while (1) {								\
1531da177e4SLinus Torvalds      WAITbits = inb(port) & (mask);					\
1541da177e4SLinus Torvalds      if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
1551da177e4SLinus Torvalds        break;                                                         	\
1561da177e4SLinus Torvalds      mdelay(1);							\
1571da177e4SLinus Torvalds      if (--WAITtimeout == 0) goto fail;					\
1581da177e4SLinus Torvalds    }									\
1591da177e4SLinus Torvalds  }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds static void aha1542_stat(void)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds /*	int s = inb(STATUS), i = inb(INTRFLAGS);
1641da177e4SLinus Torvalds 	printk("status=%x intrflags=%x\n", s, i, WAITnexttimeout-WAITtimeout); */
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /* This is a bit complicated, but we need to make sure that an interrupt
1681da177e4SLinus Torvalds    routine does not send something out while we are in the middle of this.
1691da177e4SLinus Torvalds    Fortunately, it is only at boot time that multi-byte messages
1701da177e4SLinus Torvalds    are ever sent. */
1711da177e4SLinus Torvalds static int aha1542_out(unsigned int base, unchar * cmdp, int len)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	unsigned long flags = 0;
1741da177e4SLinus Torvalds 	int got_lock;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	if (len == 1) {
1771da177e4SLinus Torvalds 		got_lock = 0;
1781da177e4SLinus Torvalds 		while (1 == 1) {
1791da177e4SLinus Torvalds 			WAIT(STATUS(base), CDF, 0, CDF);
1801da177e4SLinus Torvalds 			spin_lock_irqsave(&aha1542_lock, flags);
1811da177e4SLinus Torvalds 			if (inb(STATUS(base)) & CDF) {
1821da177e4SLinus Torvalds 				spin_unlock_irqrestore(&aha1542_lock, flags);
1831da177e4SLinus Torvalds 				continue;
1841da177e4SLinus Torvalds 			}
1851da177e4SLinus Torvalds 			outb(*cmdp, DATA(base));
1861da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1871da177e4SLinus Torvalds 			return 0;
1881da177e4SLinus Torvalds 		}
1891da177e4SLinus Torvalds 	} else {
1901da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
1911da177e4SLinus Torvalds 		got_lock = 1;
1921da177e4SLinus Torvalds 		while (len--) {
1931da177e4SLinus Torvalds 			WAIT(STATUS(base), CDF, 0, CDF);
1941da177e4SLinus Torvalds 			outb(*cmdp++, DATA(base));
1951da177e4SLinus Torvalds 		}
1961da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
1971da177e4SLinus Torvalds 	}
1981da177e4SLinus Torvalds 	return 0;
1991da177e4SLinus Torvalds fail:
2001da177e4SLinus Torvalds 	if (got_lock)
2011da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
2021da177e4SLinus Torvalds 	printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
2031da177e4SLinus Torvalds 	aha1542_stat();
2041da177e4SLinus Torvalds 	return 1;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /* Only used at boot time, so we do not need to worry about latency as much
2081da177e4SLinus Torvalds    here */
2091da177e4SLinus Torvalds 
210643a7c43SOndrej Zary static int aha1542_in(unsigned int base, unchar *cmdp, int len)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	unsigned long flags;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
2151da177e4SLinus Torvalds 	while (len--) {
2161da177e4SLinus Torvalds 		WAIT(STATUS(base), DF, DF, 0);
2171da177e4SLinus Torvalds 		*cmdp++ = inb(DATA(base));
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
2201da177e4SLinus Torvalds 	return 0;
2211da177e4SLinus Torvalds fail:
2221da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
2231da177e4SLinus Torvalds 	printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
2241da177e4SLinus Torvalds 	aha1542_stat();
2251da177e4SLinus Torvalds 	return 1;
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds /* Similar to aha1542_in, except that we wait a very short period of time.
2291da177e4SLinus Torvalds    We use this if we know the board is alive and awake, but we are not sure
2301da177e4SLinus Torvalds    if the board will respond to the command we are about to send or not */
231643a7c43SOndrej Zary static int aha1542_in1(unsigned int base, unchar *cmdp, int len)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	unsigned long flags;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
2361da177e4SLinus Torvalds 	while (len--) {
2371da177e4SLinus Torvalds 		WAITd(STATUS(base), DF, DF, 0, 100);
2381da177e4SLinus Torvalds 		*cmdp++ = inb(DATA(base));
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
2411da177e4SLinus Torvalds 	return 0;
2421da177e4SLinus Torvalds fail:
2431da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
2441da177e4SLinus Torvalds 	return 1;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static int makecode(unsigned hosterr, unsigned scsierr)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	switch (hosterr) {
2501da177e4SLinus Torvalds 	case 0x0:
2511da177e4SLinus Torvalds 	case 0xa:		/* Linked command complete without error and linked normally */
2521da177e4SLinus Torvalds 	case 0xb:		/* Linked command complete without error, interrupt generated */
2531da177e4SLinus Torvalds 		hosterr = 0;
2541da177e4SLinus Torvalds 		break;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	case 0x11:		/* Selection time out-The initiator selection or target
2571da177e4SLinus Torvalds 				   reselection was not complete within the SCSI Time out period */
2581da177e4SLinus Torvalds 		hosterr = DID_TIME_OUT;
2591da177e4SLinus Torvalds 		break;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
2621da177e4SLinus Torvalds 				   than was allocated by the Data Length field or the sum of the
2631da177e4SLinus Torvalds 				   Scatter / Gather Data Length fields. */
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	case 0x15:		/* MBO command was not 00, 01 or 02-The first byte of the CB was
2681da177e4SLinus Torvalds 				   invalid. This usually indicates a software failure. */
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	case 0x16:		/* Invalid CCB Operation Code-The first byte of the CCB was invalid.
2711da177e4SLinus Torvalds 				   This usually indicates a software failure. */
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	case 0x17:		/* Linked CCB does not have the same LUN-A subsequent CCB of a set
2741da177e4SLinus Torvalds 				   of linked CCB's does not specify the same logical unit number as
2751da177e4SLinus Torvalds 				   the first. */
2761da177e4SLinus Torvalds 	case 0x18:		/* Invalid Target Direction received from Host-The direction of a
2771da177e4SLinus Torvalds 				   Target Mode CCB was invalid. */
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	case 0x19:		/* Duplicate CCB Received in Target Mode-More than once CCB was
2801da177e4SLinus Torvalds 				   received to service data transfer between the same target LUN
2811da177e4SLinus Torvalds 				   and initiator SCSI ID in the same direction. */
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	case 0x1a:		/* Invalid CCB or Segment List Parameter-A segment list with a zero
2841da177e4SLinus Torvalds 				   length segment or invalid segment list boundaries was received.
2851da177e4SLinus Torvalds 				   A CCB parameter was invalid. */
2861da177e4SLinus Torvalds 		DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
2871da177e4SLinus Torvalds 		hosterr = DID_ERROR;	/* Couldn't find any better */
2881da177e4SLinus Torvalds 		break;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
2911da177e4SLinus Torvalds 				   phase sequence was requested by the target. The host adapter
2921da177e4SLinus Torvalds 				   will generate a SCSI Reset Condition, notifying the host with
2931da177e4SLinus Torvalds 				   a SCRD interrupt */
2941da177e4SLinus Torvalds 		hosterr = DID_RESET;
2951da177e4SLinus Torvalds 		break;
2961da177e4SLinus Torvalds 	default:
2971da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
2981da177e4SLinus Torvalds 		break;
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 	return scsierr | (hosterr << 16);
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
303643a7c43SOndrej Zary static int aha1542_test_port(int bse, struct Scsi_Host *shpnt)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	unchar inquiry_cmd[] = {CMD_INQUIRY};
3061da177e4SLinus Torvalds 	unchar inquiry_result[4];
3071da177e4SLinus Torvalds 	unchar *cmdp;
3081da177e4SLinus Torvalds 	int len;
3091da177e4SLinus Torvalds 	volatile int debug = 0;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/* Quick and dirty test for presence of the card. */
3121da177e4SLinus Torvalds 	if (inb(STATUS(bse)) == 0xff)
3131da177e4SLinus Torvalds 		return 0;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	/* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	/*  DEB(printk("aha1542_test_port called \n")); */
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	/* In case some other card was probing here, reset interrupts */
3201da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	mdelay(20);		/* Wait a little bit for things to settle down. */
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	debug = 1;
3271da177e4SLinus Torvalds 	/* Expect INIT and IDLE, any of the others are bad */
3281da177e4SLinus Torvalds 	WAIT(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	debug = 2;
3311da177e4SLinus Torvalds 	/* Shouldn't have generated any interrupts during reset */
3321da177e4SLinus Torvalds 	if (inb(INTRFLAGS(bse)) & INTRMASK)
3331da177e4SLinus Torvalds 		goto fail;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	/* Perform a host adapter inquiry instead so we do not need to set
3371da177e4SLinus Torvalds 	   up the mailboxes ahead of time */
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	aha1542_out(bse, inquiry_cmd, 1);
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	debug = 3;
3421da177e4SLinus Torvalds 	len = 4;
3431da177e4SLinus Torvalds 	cmdp = &inquiry_result[0];
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	while (len--) {
3461da177e4SLinus Torvalds 		WAIT(STATUS(bse), DF, DF, 0);
3471da177e4SLinus Torvalds 		*cmdp++ = inb(DATA(bse));
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	debug = 8;
3511da177e4SLinus Torvalds 	/* Reading port should reset DF */
3521da177e4SLinus Torvalds 	if (inb(STATUS(bse)) & DF)
3531da177e4SLinus Torvalds 		goto fail;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	debug = 9;
3561da177e4SLinus Torvalds 	/* When HACC, command is completed, and we're though testing */
3571da177e4SLinus Torvalds 	WAIT(INTRFLAGS(bse), HACC, HACC, 0);
3581da177e4SLinus Torvalds 	/* now initialize adapter */
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	debug = 10;
3611da177e4SLinus Torvalds 	/* Clear interrupts */
3621da177e4SLinus Torvalds 	outb(IRST, CONTROL(bse));
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	debug = 11;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	return debug;		/* 1 = ok */
3671da177e4SLinus Torvalds fail:
3681da177e4SLinus Torvalds 	return 0;		/* 0 = not ok */
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
37287c4d7bcSJeff Garzik static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds 	unsigned long flags;
37587c4d7bcSJeff Garzik 	struct Scsi_Host *shost = dev_id;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
37887c4d7bcSJeff Garzik 	aha1542_intr_handle(shost);
3791da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
3801da177e4SLinus Torvalds 	return IRQ_HANDLED;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /* A "high" level interrupt handler */
38487c4d7bcSJeff Garzik static void aha1542_intr_handle(struct Scsi_Host *shost)
3851da177e4SLinus Torvalds {
386e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(shost);
3871da177e4SLinus Torvalds 	void (*my_done) (Scsi_Cmnd *) = NULL;
3881da177e4SLinus Torvalds 	int errstatus, mbi, mbo, mbistatus;
3891da177e4SLinus Torvalds 	int number_serviced;
3901da177e4SLinus Torvalds 	unsigned long flags;
3911da177e4SLinus Torvalds 	Scsi_Cmnd *SCtmp;
3921da177e4SLinus Torvalds 	int flag;
3931da177e4SLinus Torvalds 	int needs_restart;
394e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
395e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds #ifdef DEBUG
3981da177e4SLinus Torvalds 	{
3991da177e4SLinus Torvalds 		flag = inb(INTRFLAGS(shost->io_port));
4001da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_intr_handle: ");
4011da177e4SLinus Torvalds 		if (!(flag & ANYINTR))
4021da177e4SLinus Torvalds 			printk("no interrupt?");
4031da177e4SLinus Torvalds 		if (flag & MBIF)
4041da177e4SLinus Torvalds 			printk("MBIF ");
4051da177e4SLinus Torvalds 		if (flag & MBOA)
4061da177e4SLinus Torvalds 			printk("MBOF ");
4071da177e4SLinus Torvalds 		if (flag & HACC)
4081da177e4SLinus Torvalds 			printk("HACC ");
4091da177e4SLinus Torvalds 		if (flag & SCRD)
4101da177e4SLinus Torvalds 			printk("SCRD ");
4111da177e4SLinus Torvalds 		printk("status %02x\n", inb(STATUS(shost->io_port)));
4121da177e4SLinus Torvalds 	};
4131da177e4SLinus Torvalds #endif
4141da177e4SLinus Torvalds 	number_serviced = 0;
4151da177e4SLinus Torvalds 	needs_restart = 0;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	while (1 == 1) {
4181da177e4SLinus Torvalds 		flag = inb(INTRFLAGS(shost->io_port));
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 		/* Check for unusual interrupts.  If any of these happen, we should
4211da177e4SLinus Torvalds 		   probably do something special, but for now just printing a message
4221da177e4SLinus Torvalds 		   is sufficient.  A SCSI reset detected is something that we really
4231da177e4SLinus Torvalds 		   need to deal with in some way. */
4241da177e4SLinus Torvalds 		if (flag & ~MBIF) {
4251da177e4SLinus Torvalds 			if (flag & MBOA)
4261da177e4SLinus Torvalds 				printk("MBOF ");
4271da177e4SLinus Torvalds 			if (flag & HACC)
4281da177e4SLinus Torvalds 				printk("HACC ");
4291da177e4SLinus Torvalds 			if (flag & SCRD) {
4301da177e4SLinus Torvalds 				needs_restart = 1;
4311da177e4SLinus Torvalds 				printk("SCRD ");
4321da177e4SLinus Torvalds 			}
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 		aha1542_intr_reset(shost->io_port);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
437e98878f7SOndrej Zary 		mbi = aha1542->aha1542_last_mbi_used + 1;
4381da177e4SLinus Torvalds 		if (mbi >= 2 * AHA1542_MAILBOXES)
4391da177e4SLinus Torvalds 			mbi = AHA1542_MAILBOXES;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 		do {
4421da177e4SLinus Torvalds 			if (mb[mbi].status != 0)
4431da177e4SLinus Torvalds 				break;
4441da177e4SLinus Torvalds 			mbi++;
4451da177e4SLinus Torvalds 			if (mbi >= 2 * AHA1542_MAILBOXES)
4461da177e4SLinus Torvalds 				mbi = AHA1542_MAILBOXES;
447e98878f7SOndrej Zary 		} while (mbi != aha1542->aha1542_last_mbi_used);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 		if (mb[mbi].status == 0) {
4501da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
4511da177e4SLinus Torvalds 			/* Hmm, no mail.  Must have read it the last time around */
4521da177e4SLinus Torvalds 			if (!number_serviced && !needs_restart)
4531da177e4SLinus Torvalds 				printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
4541da177e4SLinus Torvalds 			/* We detected a reset.  Restart all pending commands for
4551da177e4SLinus Torvalds 			   devices that use the hard reset option */
4561da177e4SLinus Torvalds 			if (needs_restart)
4571da177e4SLinus Torvalds 				aha1542_restart(shost);
4581da177e4SLinus Torvalds 			return;
4591da177e4SLinus Torvalds 		};
4601da177e4SLinus Torvalds 
46110be6250SOndrej Zary 		mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
4621da177e4SLinus Torvalds 		mbistatus = mb[mbi].status;
4631da177e4SLinus Torvalds 		mb[mbi].status = 0;
464e98878f7SOndrej Zary 		aha1542->aha1542_last_mbi_used = mbi;
4651da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds #ifdef DEBUG
4681da177e4SLinus Torvalds 		{
4691da177e4SLinus Torvalds 			if (ccb[mbo].tarstat | ccb[mbo].hastat)
4701da177e4SLinus Torvalds 				printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
4711da177e4SLinus Torvalds 				       ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
4721da177e4SLinus Torvalds 		};
4731da177e4SLinus Torvalds #endif
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 		if (mbistatus == 3)
4761da177e4SLinus Torvalds 			continue;	/* Aborted command not found */
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds #ifdef DEBUG
4791da177e4SLinus Torvalds 		printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
4801da177e4SLinus Torvalds #endif
4811da177e4SLinus Torvalds 
482e98878f7SOndrej Zary 		SCtmp = aha1542->SCint[mbo];
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 		if (!SCtmp || !SCtmp->scsi_done) {
4851da177e4SLinus Torvalds 			printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
4861da177e4SLinus Torvalds 			printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
4871da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].idlun, mbo);
4881da177e4SLinus Torvalds 			return;
4891da177e4SLinus Torvalds 		}
4901da177e4SLinus Torvalds 		my_done = SCtmp->scsi_done;
4911da177e4SLinus Torvalds 		kfree(SCtmp->host_scribble);
4921da177e4SLinus Torvalds 		SCtmp->host_scribble = NULL;
4931da177e4SLinus Torvalds 		/* Fetch the sense data, and tuck it away, in the required slot.  The
4941da177e4SLinus Torvalds 		   Adaptec automatically fetches it, and there is no guarantee that
4951da177e4SLinus Torvalds 		   we will still have it in the cdb when we come back */
4961da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2)
4971da177e4SLinus Torvalds 			memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
498b80ca4f7SFUJITA Tomonori 			       SCSI_SENSE_BUFFERSIZE);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 		/* is there mail :-) */
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 		/* more error checking left out here */
5041da177e4SLinus Torvalds 		if (mbistatus != 1)
5051da177e4SLinus Torvalds 			/* This is surely wrong, but I don't know what's right */
5061da177e4SLinus Torvalds 			errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
5071da177e4SLinus Torvalds 		else
5081da177e4SLinus Torvalds 			errstatus = 0;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds #ifdef DEBUG
5111da177e4SLinus Torvalds 		if (errstatus)
5121da177e4SLinus Torvalds 			printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
5131da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].tarstat);
5141da177e4SLinus Torvalds #endif
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2) {
5171da177e4SLinus Torvalds #ifdef DEBUG
5181da177e4SLinus Torvalds 			int i;
5191da177e4SLinus Torvalds #endif
5201da177e4SLinus Torvalds 			DEB(printk("aha1542_intr_handle: sense:"));
5211da177e4SLinus Torvalds #ifdef DEBUG
5221da177e4SLinus Torvalds 			for (i = 0; i < 12; i++)
5231da177e4SLinus Torvalds 				printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
5241da177e4SLinus Torvalds 			printk("\n");
5251da177e4SLinus Torvalds #endif
5261da177e4SLinus Torvalds 			/*
5271da177e4SLinus Torvalds 			   DEB(printk("aha1542_intr_handle: buf:"));
5281da177e4SLinus Torvalds 			   for (i = 0; i < bufflen; i++)
5291da177e4SLinus Torvalds 			   printk("%02x ", ((unchar *)buff)[i]);
5301da177e4SLinus Torvalds 			   printk("\n");
5311da177e4SLinus Torvalds 			 */
5321da177e4SLinus Torvalds 		}
5331da177e4SLinus Torvalds 		DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
5341da177e4SLinus Torvalds 		SCtmp->result = errstatus;
535e98878f7SOndrej Zary 		aha1542->SCint[mbo] = NULL;	/* This effectively frees up the mailbox slot, as
5361da177e4SLinus Torvalds 						   far as queuecommand is concerned */
5371da177e4SLinus Torvalds 		my_done(SCtmp);
5381da177e4SLinus Torvalds 		number_serviced++;
5391da177e4SLinus Torvalds 	};
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
542f281233dSJeff Garzik static int aha1542_queuecommand_lck(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
5431da177e4SLinus Torvalds {
544e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
5451da177e4SLinus Torvalds 	unchar ahacmd = CMD_START_SCSI;
5461da177e4SLinus Torvalds 	unchar direction;
5471da177e4SLinus Torvalds 	unchar *cmd = (unchar *) SCpnt->cmnd;
5481da177e4SLinus Torvalds 	unchar target = SCpnt->device->id;
5491da177e4SLinus Torvalds 	unchar lun = SCpnt->device->lun;
5501da177e4SLinus Torvalds 	unsigned long flags;
551fc3fdfccSBoaz Harrosh 	int bufflen = scsi_bufflen(SCpnt);
5521da177e4SLinus Torvalds 	int mbo;
553e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
554e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	DEB(int i);
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	DEB(if (target > 1) {
5591da177e4SLinus Torvalds 	    SCpnt->result = DID_TIME_OUT << 16;
5601da177e4SLinus Torvalds 	    done(SCpnt); return 0;
5611da177e4SLinus Torvalds 	    }
5621da177e4SLinus Torvalds 	);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	if (*cmd == REQUEST_SENSE) {
5651da177e4SLinus Torvalds 		/* Don't do the command - we have the sense data already */
5661da177e4SLinus Torvalds 		SCpnt->result = 0;
5671da177e4SLinus Torvalds 		done(SCpnt);
5681da177e4SLinus Torvalds 		return 0;
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds #ifdef DEBUG
5711da177e4SLinus Torvalds 	if (*cmd == READ_10 || *cmd == WRITE_10)
5721da177e4SLinus Torvalds 		i = xscsi2int(cmd + 2);
5731da177e4SLinus Torvalds 	else if (*cmd == READ_6 || *cmd == WRITE_6)
5741da177e4SLinus Torvalds 		i = scsi2int(cmd + 2);
5751da177e4SLinus Torvalds 	else
5761da177e4SLinus Torvalds 		i = -1;
5771da177e4SLinus Torvalds 	if (done)
5781da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
5791da177e4SLinus Torvalds 	else
5801da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
5811da177e4SLinus Torvalds 	aha1542_stat();
5821da177e4SLinus Torvalds 	printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
5831da177e4SLinus Torvalds 	for (i = 0; i < SCpnt->cmd_len; i++)
5841da177e4SLinus Torvalds 		printk("%02x ", cmd[i]);
5851da177e4SLinus Torvalds 	printk("\n");
5861da177e4SLinus Torvalds 	if (*cmd == WRITE_10 || *cmd == WRITE_6)
5871da177e4SLinus Torvalds 		return 0;	/* we are still testing, so *don't* write */
5881da177e4SLinus Torvalds #endif
5891da177e4SLinus Torvalds 	/* Use the outgoing mailboxes in a round-robin fashion, because this
5901da177e4SLinus Torvalds 	   is how the host adapter will scan for them */
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
593e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
5941da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
5951da177e4SLinus Torvalds 		mbo = 0;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	do {
598e98878f7SOndrej Zary 		if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
5991da177e4SLinus Torvalds 			break;
6001da177e4SLinus Torvalds 		mbo++;
6011da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
6021da177e4SLinus Torvalds 			mbo = 0;
603e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
6041da177e4SLinus Torvalds 
605e98878f7SOndrej Zary 	if (mb[mbo].status || aha1542->SCint[mbo])
6061da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
6071da177e4SLinus Torvalds 
608e98878f7SOndrej Zary 	aha1542->SCint[mbo] = SCpnt;	/* This will effectively prevent someone else from
6091da177e4SLinus Torvalds 					   screwing with this cdb. */
6101da177e4SLinus Torvalds 
611e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
6121da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds #ifdef DEBUG
6151da177e4SLinus Torvalds 	printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
6161da177e4SLinus Torvalds #endif
6171da177e4SLinus Torvalds 
61810be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	ccb[mbo].cdblen = SCpnt->cmd_len;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	direction = 0;
6251da177e4SLinus Torvalds 	if (*cmd == READ_10 || *cmd == READ_6)
6261da177e4SLinus Torvalds 		direction = 8;
6271da177e4SLinus Torvalds 	else if (*cmd == WRITE_10 || *cmd == WRITE_6)
6281da177e4SLinus Torvalds 		direction = 16;
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
6311da177e4SLinus Torvalds 
632fc3fdfccSBoaz Harrosh 	if (bufflen) {
63351cf2249SJens Axboe 		struct scatterlist *sg;
6341da177e4SLinus Torvalds 		struct chain *cptr;
6351da177e4SLinus Torvalds #ifdef DEBUG
6361da177e4SLinus Torvalds 		unsigned char *ptr;
6371da177e4SLinus Torvalds #endif
638fc3fdfccSBoaz Harrosh 		int i, sg_count = scsi_sg_count(SCpnt);
6391da177e4SLinus Torvalds 		ccb[mbo].op = 2;	/* SCSI Initiator Command  w/scatter-gather */
640fc3fdfccSBoaz Harrosh 		SCpnt->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
641fc3fdfccSBoaz Harrosh 		                                         GFP_KERNEL | GFP_DMA);
6421da177e4SLinus Torvalds 		cptr = (struct chain *) SCpnt->host_scribble;
6431da177e4SLinus Torvalds 		if (cptr == NULL) {
6441da177e4SLinus Torvalds 			/* free the claimed mailbox slot */
645e98878f7SOndrej Zary 			aha1542->SCint[mbo] = NULL;
6461da177e4SLinus Torvalds 			return SCSI_MLQUEUE_HOST_BUSY;
6471da177e4SLinus Torvalds 		}
648fc3fdfccSBoaz Harrosh 		scsi_for_each_sg(SCpnt, sg, sg_count, i) {
64910be6250SOndrej Zary 			any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
65010be6250SOndrej Zary 								+ sg->offset);
65151cf2249SJens Axboe 			any2scsi(cptr[i].datalen, sg->length);
6521da177e4SLinus Torvalds 		};
653fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
65410be6250SOndrej Zary 		any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
6551da177e4SLinus Torvalds #ifdef DEBUG
6561da177e4SLinus Torvalds 		printk("cptr %x: ", cptr);
6571da177e4SLinus Torvalds 		ptr = (unsigned char *) cptr;
6581da177e4SLinus Torvalds 		for (i = 0; i < 18; i++)
6591da177e4SLinus Torvalds 			printk("%02x ", ptr[i]);
6601da177e4SLinus Torvalds #endif
6611da177e4SLinus Torvalds 	} else {
6621da177e4SLinus Torvalds 		ccb[mbo].op = 0;	/* SCSI Initiator Command */
6631da177e4SLinus Torvalds 		SCpnt->host_scribble = NULL;
664fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, 0);
665fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].dataptr, 0);
6661da177e4SLinus Torvalds 	};
6671da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);	/*SCSI Target Id */
6681da177e4SLinus Torvalds 	ccb[mbo].rsalen = 16;
6691da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
6701da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds #ifdef DEBUG
6731da177e4SLinus Torvalds 	{
6741da177e4SLinus Torvalds 		int i;
6751da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_command: sending.. ");
6761da177e4SLinus Torvalds 		for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
6771da177e4SLinus Torvalds 			printk("%02x ", ((unchar *) & ccb[mbo])[i]);
6781da177e4SLinus Torvalds 	};
6791da177e4SLinus Torvalds #endif
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	if (done) {
6821da177e4SLinus Torvalds 		DEB(printk("aha1542_queuecommand: now waiting for interrupt ");
6831da177e4SLinus Torvalds 		    aha1542_stat());
6841da177e4SLinus Torvalds 		SCpnt->scsi_done = done;
6851da177e4SLinus Torvalds 		mb[mbo].status = 1;
6861da177e4SLinus Torvalds 		aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);	/* start scsi command */
6871da177e4SLinus Torvalds 		DEB(aha1542_stat());
6881da177e4SLinus Torvalds 	} else
6891da177e4SLinus Torvalds 		printk("aha1542_queuecommand: done can't be NULL\n");
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	return 0;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
694f281233dSJeff Garzik static DEF_SCSI_QCMD(aha1542_queuecommand)
695f281233dSJeff Garzik 
6961da177e4SLinus Torvalds /* Initialize mailboxes */
6971da177e4SLinus Torvalds static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
6981da177e4SLinus Torvalds {
699e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(shpnt);
7001da177e4SLinus Torvalds 	int i;
701e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
702e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	unchar cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
7071da177e4SLinus Torvalds 		mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
70810be6250SOndrej Zary 		any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
7091da177e4SLinus Torvalds 	};
7101da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
71110be6250SOndrej Zary 	any2scsi((cmd + 2), isa_virt_to_bus(mb));
7121da177e4SLinus Torvalds 	aha1542_out(bse, cmd, 5);
7131da177e4SLinus Torvalds 	WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
7141da177e4SLinus Torvalds 	while (0) {
7151da177e4SLinus Torvalds fail:
7161da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 	aha1542_intr_reset(bse);
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
721643a7c43SOndrej Zary static int aha1542_getconfig(int base_io, unsigned char *irq_level, unsigned char *dma_chan, unsigned char *scsi_id)
7221da177e4SLinus Torvalds {
7231da177e4SLinus Torvalds 	unchar inquiry_cmd[] = {CMD_RETCONF};
7241da177e4SLinus Torvalds 	unchar inquiry_result[3];
7251da177e4SLinus Torvalds 	int i;
7261da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
7271da177e4SLinus Torvalds 	if (i & DF) {
7281da177e4SLinus Torvalds 		i = inb(DATA(base_io));
7291da177e4SLinus Torvalds 	};
7301da177e4SLinus Torvalds 	aha1542_out(base_io, inquiry_cmd, 1);
7311da177e4SLinus Torvalds 	aha1542_in(base_io, inquiry_result, 3);
7321da177e4SLinus Torvalds 	WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
7331da177e4SLinus Torvalds 	while (0) {
7341da177e4SLinus Torvalds fail:
7351da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query board settings\n");
7361da177e4SLinus Torvalds 	}
7371da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
7381da177e4SLinus Torvalds 	switch (inquiry_result[0]) {
7391da177e4SLinus Torvalds 	case 0x80:
7401da177e4SLinus Torvalds 		*dma_chan = 7;
7411da177e4SLinus Torvalds 		break;
7421da177e4SLinus Torvalds 	case 0x40:
7431da177e4SLinus Torvalds 		*dma_chan = 6;
7441da177e4SLinus Torvalds 		break;
7451da177e4SLinus Torvalds 	case 0x20:
7461da177e4SLinus Torvalds 		*dma_chan = 5;
7471da177e4SLinus Torvalds 		break;
7481da177e4SLinus Torvalds 	case 0x01:
7491da177e4SLinus Torvalds 		*dma_chan = 0;
7501da177e4SLinus Torvalds 		break;
7511da177e4SLinus Torvalds 	case 0:
7521da177e4SLinus Torvalds 		/* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
7531da177e4SLinus Torvalds 		   Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
7541da177e4SLinus Torvalds 		*dma_chan = 0xFF;
7551da177e4SLinus Torvalds 		break;
7561da177e4SLinus Torvalds 	default:
7571da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec DMA priority.  Disabling board\n");
7581da177e4SLinus Torvalds 		return -1;
7591da177e4SLinus Torvalds 	};
7601da177e4SLinus Torvalds 	switch (inquiry_result[1]) {
7611da177e4SLinus Torvalds 	case 0x40:
7621da177e4SLinus Torvalds 		*irq_level = 15;
7631da177e4SLinus Torvalds 		break;
7641da177e4SLinus Torvalds 	case 0x20:
7651da177e4SLinus Torvalds 		*irq_level = 14;
7661da177e4SLinus Torvalds 		break;
7671da177e4SLinus Torvalds 	case 0x8:
7681da177e4SLinus Torvalds 		*irq_level = 12;
7691da177e4SLinus Torvalds 		break;
7701da177e4SLinus Torvalds 	case 0x4:
7711da177e4SLinus Torvalds 		*irq_level = 11;
7721da177e4SLinus Torvalds 		break;
7731da177e4SLinus Torvalds 	case 0x2:
7741da177e4SLinus Torvalds 		*irq_level = 10;
7751da177e4SLinus Torvalds 		break;
7761da177e4SLinus Torvalds 	case 0x1:
7771da177e4SLinus Torvalds 		*irq_level = 9;
7781da177e4SLinus Torvalds 		break;
7791da177e4SLinus Torvalds 	default:
7801da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec IRQ level.  Disabling board\n");
7811da177e4SLinus Torvalds 		return -1;
7821da177e4SLinus Torvalds 	};
7831da177e4SLinus Torvalds 	*scsi_id = inquiry_result[2] & 7;
7841da177e4SLinus Torvalds 	return 0;
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds /* This function should only be called for 1542C boards - we can detect
7881da177e4SLinus Torvalds    the special firmware settings and unlock the board */
7891da177e4SLinus Torvalds 
790643a7c43SOndrej Zary static int aha1542_mbenable(int base)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	static unchar mbenable_cmd[3];
7931da177e4SLinus Torvalds 	static unchar mbenable_result[2];
7941da177e4SLinus Torvalds 	int retval;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	retval = BIOS_TRANSLATION_6432;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	mbenable_cmd[0] = CMD_EXTBIOS;
7991da177e4SLinus Torvalds 	aha1542_out(base, mbenable_cmd, 1);
8001da177e4SLinus Torvalds 	if (aha1542_in1(base, mbenable_result, 2))
8011da177e4SLinus Torvalds 		return retval;
8021da177e4SLinus Torvalds 	WAITd(INTRFLAGS(base), INTRMASK, HACC, 0, 100);
8031da177e4SLinus Torvalds 	aha1542_intr_reset(base);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
8061da177e4SLinus Torvalds 		mbenable_cmd[0] = CMD_MBENABLE;
8071da177e4SLinus Torvalds 		mbenable_cmd[1] = 0;
8081da177e4SLinus Torvalds 		mbenable_cmd[2] = mbenable_result[1];
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 		if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
8111da177e4SLinus Torvalds 			retval = BIOS_TRANSLATION_25563;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		aha1542_out(base, mbenable_cmd, 3);
8141da177e4SLinus Torvalds 		WAIT(INTRFLAGS(base), INTRMASK, HACC, 0);
8151da177e4SLinus Torvalds 	};
8161da177e4SLinus Torvalds 	while (0) {
8171da177e4SLinus Torvalds fail:
8181da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 	aha1542_intr_reset(base);
8211da177e4SLinus Torvalds 	return retval;
8221da177e4SLinus Torvalds }
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
825643a7c43SOndrej Zary static int aha1542_query(int base_io, int *transl)
8261da177e4SLinus Torvalds {
8271da177e4SLinus Torvalds 	unchar inquiry_cmd[] = {CMD_INQUIRY};
8281da177e4SLinus Torvalds 	unchar inquiry_result[4];
8291da177e4SLinus Torvalds 	int i;
8301da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
8311da177e4SLinus Torvalds 	if (i & DF) {
8321da177e4SLinus Torvalds 		i = inb(DATA(base_io));
8331da177e4SLinus Torvalds 	};
8341da177e4SLinus Torvalds 	aha1542_out(base_io, inquiry_cmd, 1);
8351da177e4SLinus Torvalds 	aha1542_in(base_io, inquiry_result, 4);
8361da177e4SLinus Torvalds 	WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
8371da177e4SLinus Torvalds 	while (0) {
8381da177e4SLinus Torvalds fail:
8391da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query card type\n");
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	*transl = BIOS_TRANSLATION_6432;	/* Default case */
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	/* For an AHA1740 series board, we ignore the board since there is a
8461da177e4SLinus Torvalds 	   hardware bug which can lead to wrong blocks being returned if the board
8471da177e4SLinus Torvalds 	   is operating in the 1542 emulation mode.  Since there is an extended mode
8481da177e4SLinus Torvalds 	   driver, we simply ignore the board and let the 1740 driver pick it up.
8491da177e4SLinus Torvalds 	 */
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	if (inquiry_result[0] == 0x43) {
8521da177e4SLinus Torvalds 		printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
8531da177e4SLinus Torvalds 		return 1;
8541da177e4SLinus Torvalds 	};
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	/* Always call this - boards that do not support extended bios translation
8571da177e4SLinus Torvalds 	   will ignore the command, and we will set the proper default */
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	*transl = aha1542_mbenable(base_io);
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	return 0;
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds #ifndef MODULE
8651da177e4SLinus Torvalds static char *setup_str[MAXBOARDS] __initdata;
8661da177e4SLinus Torvalds static int setup_idx = 0;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds static void __init aha1542_setup(char *str, int *ints)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
8711da177e4SLinus Torvalds 	int setup_portbase;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	if (setup_idx >= MAXBOARDS) {
8741da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
8751da177e4SLinus Torvalds 		printk(KERN_ERR "   Entryline 1: %s\n", setup_str[0]);
8761da177e4SLinus Torvalds 		printk(KERN_ERR "   Entryline 2: %s\n", setup_str[1]);
8771da177e4SLinus Torvalds 		printk(KERN_ERR "   This line:   %s\n", str);
8781da177e4SLinus Torvalds 		return;
8791da177e4SLinus Torvalds 	}
8801da177e4SLinus Torvalds 	if (ints[0] < 1 || ints[0] > 4) {
8811da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: %s\n", str);
8821da177e4SLinus Torvalds 		printk(ahausage);
8831da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 	setup_called[setup_idx] = ints[0];
8861da177e4SLinus Torvalds 	setup_str[setup_idx] = str;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	setup_portbase = ints[0] >= 1 ? ints[1] : 0;	/* Preserve the default value.. */
8891da177e4SLinus Torvalds 	setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
8901da177e4SLinus Torvalds 	setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
8911da177e4SLinus Torvalds 	if (ints[0] >= 4)
8921da177e4SLinus Torvalds 	{
8931da177e4SLinus Torvalds 		int atbt = -1;
8941da177e4SLinus Torvalds 		switch (ints[4]) {
8951da177e4SLinus Torvalds 		case 5:
8961da177e4SLinus Torvalds 			atbt = 0x00;
8971da177e4SLinus Torvalds 			break;
8981da177e4SLinus Torvalds 		case 6:
8991da177e4SLinus Torvalds 			atbt = 0x04;
9001da177e4SLinus Torvalds 			break;
9011da177e4SLinus Torvalds 		case 7:
9021da177e4SLinus Torvalds 			atbt = 0x01;
9031da177e4SLinus Torvalds 			break;
9041da177e4SLinus Torvalds 		case 8:
9051da177e4SLinus Torvalds 			atbt = 0x02;
9061da177e4SLinus Torvalds 			break;
9071da177e4SLinus Torvalds 		case 10:
9081da177e4SLinus Torvalds 			atbt = 0x03;
9091da177e4SLinus Torvalds 			break;
9101da177e4SLinus Torvalds 		default:
9111da177e4SLinus Torvalds 			printk(KERN_ERR "aha1542: %s\n", str);
9121da177e4SLinus Torvalds 			printk(ahausage);
9131da177e4SLinus Torvalds 			printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s.  Using jumper defaults.\n");
9141da177e4SLinus Torvalds 			break;
9151da177e4SLinus Torvalds 		}
9161da177e4SLinus Torvalds 		setup_dmaspeed[setup_idx] = atbt;
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 	if (setup_portbase != 0)
9191da177e4SLinus Torvalds 		bases[setup_idx] = setup_portbase;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	++setup_idx;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds static int __init do_setup(char *str)
9251da177e4SLinus Torvalds {
9261da177e4SLinus Torvalds 	int ints[5];
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	int count=setup_idx;
9291da177e4SLinus Torvalds 
9306391a113STobias Klauser 	get_options(str, ARRAY_SIZE(ints), ints);
9311da177e4SLinus Torvalds 	aha1542_setup(str,ints);
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	return count<setup_idx;
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds __setup("aha1542=",do_setup);
9371da177e4SLinus Torvalds #endif
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds /* return non-zero on detection */
940643a7c43SOndrej Zary static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	unsigned char dma_chan;
9431da177e4SLinus Torvalds 	unsigned char irq_level;
9441da177e4SLinus Torvalds 	unsigned char scsi_id;
9451da177e4SLinus Torvalds 	unsigned long flags;
9461da177e4SLinus Torvalds 	unsigned int base_io;
9471da177e4SLinus Torvalds 	int trans;
9481da177e4SLinus Torvalds 	struct Scsi_Host *shpnt = NULL;
949e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	DEB(printk("aha1542_detect: \n"));
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	tpnt->proc_name = "aha1542";
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 		if (bases[indx] != 0 && request_region(bases[indx], 4, "aha1542")) {
956643a7c43SOndrej Zary 			shpnt = scsi_host_alloc(tpnt,
9571da177e4SLinus Torvalds 					sizeof(struct aha1542_hostdata));
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 			if(shpnt==NULL) {
9601da177e4SLinus Torvalds 				release_region(bases[indx], 4);
961643a7c43SOndrej Zary 				return NULL;
9621da177e4SLinus Torvalds 			}
963e98878f7SOndrej Zary 			aha1542 = shost_priv(shpnt);
9641da177e4SLinus Torvalds 			if (!aha1542_test_port(bases[indx], shpnt))
9651da177e4SLinus Torvalds 				goto unregister;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 			base_io = bases[indx];
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 			/* Set the Bus on/off-times as not to ruin floppy performance */
9701da177e4SLinus Torvalds 			{
9711da177e4SLinus Torvalds 				unchar oncmd[] = {CMD_BUSON_TIME, 7};
9721da177e4SLinus Torvalds 				unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 				if (setup_called[indx]) {
9751da177e4SLinus Torvalds 					oncmd[1] = setup_buson[indx];
9761da177e4SLinus Torvalds 					offcmd[1] = setup_busoff[indx];
9771da177e4SLinus Torvalds 				}
9781da177e4SLinus Torvalds 				aha1542_intr_reset(base_io);
9791da177e4SLinus Torvalds 				aha1542_out(base_io, oncmd, 2);
9801da177e4SLinus Torvalds 				WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
9811da177e4SLinus Torvalds 				aha1542_intr_reset(base_io);
9821da177e4SLinus Torvalds 				aha1542_out(base_io, offcmd, 2);
9831da177e4SLinus Torvalds 				WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
9841da177e4SLinus Torvalds 				if (setup_dmaspeed[indx] >= 0) {
9851da177e4SLinus Torvalds 					unchar dmacmd[] = {CMD_DMASPEED, 0};
9861da177e4SLinus Torvalds 					dmacmd[1] = setup_dmaspeed[indx];
9871da177e4SLinus Torvalds 					aha1542_intr_reset(base_io);
9881da177e4SLinus Torvalds 					aha1542_out(base_io, dmacmd, 2);
9891da177e4SLinus Torvalds 					WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
9901da177e4SLinus Torvalds 				}
9911da177e4SLinus Torvalds 				while (0) {
9921da177e4SLinus Torvalds fail:
9931da177e4SLinus Torvalds 					printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
9941da177e4SLinus Torvalds 				}
9951da177e4SLinus Torvalds 				aha1542_intr_reset(base_io);
9961da177e4SLinus Torvalds 			}
9971da177e4SLinus Torvalds 			if (aha1542_query(base_io, &trans))
9981da177e4SLinus Torvalds 				goto unregister;
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 			if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
10011da177e4SLinus Torvalds 				goto unregister;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 			printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
10041da177e4SLinus Torvalds 			if (dma_chan != 0xFF)
10051da177e4SLinus Torvalds 				printk(", DMA priority %d", dma_chan);
10061da177e4SLinus Torvalds 			printk("\n");
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 			DEB(aha1542_stat());
10091da177e4SLinus Torvalds 			setup_mailboxes(base_io, shpnt);
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 			DEB(aha1542_stat());
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 			DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
10141da177e4SLinus Torvalds 			spin_lock_irqsave(&aha1542_lock, flags);
101587c4d7bcSJeff Garzik 			if (request_irq(irq_level, do_aha1542_intr_handle, 0,
101687c4d7bcSJeff Garzik 					"aha1542", shpnt)) {
10171da177e4SLinus Torvalds 				printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
10181da177e4SLinus Torvalds 				spin_unlock_irqrestore(&aha1542_lock, flags);
10191da177e4SLinus Torvalds 				goto unregister;
10201da177e4SLinus Torvalds 			}
10211da177e4SLinus Torvalds 			if (dma_chan != 0xFF) {
10221da177e4SLinus Torvalds 				if (request_dma(dma_chan, "aha1542")) {
10231da177e4SLinus Torvalds 					printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
102487c4d7bcSJeff Garzik 					free_irq(irq_level, shpnt);
10251da177e4SLinus Torvalds 					spin_unlock_irqrestore(&aha1542_lock, flags);
10261da177e4SLinus Torvalds 					goto unregister;
10271da177e4SLinus Torvalds 				}
10281da177e4SLinus Torvalds 				if (dma_chan == 0 || dma_chan >= 5) {
10291da177e4SLinus Torvalds 					set_dma_mode(dma_chan, DMA_MODE_CASCADE);
10301da177e4SLinus Torvalds 					enable_dma(dma_chan);
10311da177e4SLinus Torvalds 				}
10321da177e4SLinus Torvalds 			}
103387c4d7bcSJeff Garzik 
10341da177e4SLinus Torvalds 			shpnt->this_id = scsi_id;
10351da177e4SLinus Torvalds 			shpnt->unique_id = base_io;
10361da177e4SLinus Torvalds 			shpnt->io_port = base_io;
10371da177e4SLinus Torvalds 			shpnt->n_io_port = 4;	/* Number of bytes of I/O space used */
10381da177e4SLinus Torvalds 			shpnt->dma_channel = dma_chan;
10391da177e4SLinus Torvalds 			shpnt->irq = irq_level;
1040e98878f7SOndrej Zary 			aha1542->bios_translation = trans;
10411da177e4SLinus Torvalds 			if (trans == BIOS_TRANSLATION_25563)
10421da177e4SLinus Torvalds 				printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
1043e98878f7SOndrej Zary 			aha1542->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
1044e98878f7SOndrej Zary 			aha1542->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
1045e98878f7SOndrej Zary 			memset(aha1542->SCint, 0, sizeof(aha1542->SCint));
10461da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
10471da177e4SLinus Torvalds 
1048643a7c43SOndrej Zary 			if (scsi_add_host(shpnt, pdev)) {
1049643a7c43SOndrej Zary 				if (shpnt->dma_channel != 0xff)
1050643a7c43SOndrej Zary 					free_dma(shpnt->dma_channel);
1051643a7c43SOndrej Zary 				free_irq(irq_level, shpnt);
1052643a7c43SOndrej Zary 				goto unregister;
10531da177e4SLinus Torvalds 			}
10541da177e4SLinus Torvalds 
1055643a7c43SOndrej Zary 			scsi_scan_host(shpnt);
10561da177e4SLinus Torvalds 
1057643a7c43SOndrej Zary 			return shpnt;
10581da177e4SLinus Torvalds unregister:
10591da177e4SLinus Torvalds 			release_region(bases[indx], 4);
1060643a7c43SOndrej Zary 			scsi_host_put(shpnt);
1061643a7c43SOndrej Zary 			return NULL;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 		};
10641da177e4SLinus Torvalds 
1065643a7c43SOndrej Zary 	return NULL;
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds static int aha1542_release(struct Scsi_Host *shost)
10691da177e4SLinus Torvalds {
1070643a7c43SOndrej Zary 	scsi_remove_host(shost);
10711da177e4SLinus Torvalds 	if (shost->irq)
107287c4d7bcSJeff Garzik 		free_irq(shost->irq, shost);
10731da177e4SLinus Torvalds 	if (shost->dma_channel != 0xff)
10741da177e4SLinus Torvalds 		free_dma(shost->dma_channel);
10751da177e4SLinus Torvalds 	if (shost->io_port && shost->n_io_port)
10761da177e4SLinus Torvalds 		release_region(shost->io_port, shost->n_io_port);
1077643a7c43SOndrej Zary 	scsi_host_put(shost);
10781da177e4SLinus Torvalds 	return 0;
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds static int aha1542_restart(struct Scsi_Host *shost)
10821da177e4SLinus Torvalds {
1083e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(shost);
10841da177e4SLinus Torvalds 	int i;
10851da177e4SLinus Torvalds 	int count = 0;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++)
1088e98878f7SOndrej Zary 		if (aha1542->SCint[i] &&
1089e98878f7SOndrej Zary 		    !(aha1542->SCint[i]->device->soft_reset)) {
10901da177e4SLinus Torvalds 			count++;
10911da177e4SLinus Torvalds 		}
10921da177e4SLinus Torvalds 	printk(KERN_DEBUG "Potential to restart %d stalled commands...\n", count);
10936f82fbf3SOndrej Zary 
10941da177e4SLinus Torvalds 	return 0;
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds /*
10981da177e4SLinus Torvalds  * This is a device reset.  This is handled by sending a special command
10991da177e4SLinus Torvalds  * to the device.
11001da177e4SLinus Torvalds  */
11011da177e4SLinus Torvalds static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
11021da177e4SLinus Torvalds {
1103e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
11041da177e4SLinus Torvalds 	unsigned long flags;
1105e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
11061da177e4SLinus Torvalds 	unchar target = SCpnt->device->id;
11071da177e4SLinus Torvalds 	unchar lun = SCpnt->device->lun;
11081da177e4SLinus Torvalds 	int mbo;
1109e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
11101da177e4SLinus Torvalds 	unchar ahacmd = CMD_START_SCSI;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
1113e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
11141da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
11151da177e4SLinus Torvalds 		mbo = 0;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	do {
1118e98878f7SOndrej Zary 		if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
11191da177e4SLinus Torvalds 			break;
11201da177e4SLinus Torvalds 		mbo++;
11211da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
11221da177e4SLinus Torvalds 			mbo = 0;
1123e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
11241da177e4SLinus Torvalds 
1125e98878f7SOndrej Zary 	if (mb[mbo].status || aha1542->SCint[mbo])
11261da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
11271da177e4SLinus Torvalds 
1128e98878f7SOndrej Zary 	aha1542->SCint[mbo] = SCpnt;	/* This will effectively
11291da177e4SLinus Torvalds 					   prevent someone else from
11301da177e4SLinus Torvalds 					   screwing with this cdb. */
11311da177e4SLinus Torvalds 
1132e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
11331da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
11341da177e4SLinus Torvalds 
113510be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	ccb[mbo].op = 0x81;	/* BUS DEVICE RESET */
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);		/*SCSI Target Id */
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
11441da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	/*
11471da177e4SLinus Torvalds 	 * Now tell the 1542 to flush all pending commands for this
11481da177e4SLinus Torvalds 	 * target
11491da177e4SLinus Torvalds 	 */
11501da177e4SLinus Torvalds 	aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);
11511da177e4SLinus Torvalds 
1152017560fcSJeff Garzik 	scmd_printk(KERN_WARNING, SCpnt,
1153017560fcSJeff Garzik 		"Trying device reset for target\n");
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	return SUCCESS;
11561da177e4SLinus Torvalds }
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds static int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
11591da177e4SLinus Torvalds {
1160e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
11611da177e4SLinus Torvalds 	int i;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	/*
11641da177e4SLinus Torvalds 	 * This does a scsi reset for all devices on the bus.
11651da177e4SLinus Torvalds 	 * In principle, we could also reset the 1542 - should
11661da177e4SLinus Torvalds 	 * we do this?  Try this first, and we can add that later
11671da177e4SLinus Torvalds 	 * if it turns out to be useful.
11681da177e4SLinus Torvalds 	 */
11691da177e4SLinus Torvalds 	outb(SCRST, CONTROL(SCpnt->device->host->io_port));
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	/*
11721da177e4SLinus Torvalds 	 * Wait for the thing to settle down a bit.  Unfortunately
11731da177e4SLinus Torvalds 	 * this is going to basically lock up the machine while we
11741da177e4SLinus Torvalds 	 * wait for this to complete.  To be 100% correct, we need to
11751da177e4SLinus Torvalds 	 * check for timeout, and if we are doing something like this
11761da177e4SLinus Torvalds 	 * we are pretty desperate anyways.
11771da177e4SLinus Torvalds 	 */
11781da177e4SLinus Torvalds 	ssleep(4);
117968b3aa7cSJeff Garzik  
11801da177e4SLinus Torvalds 	spin_lock_irq(SCpnt->device->host->host_lock);
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	WAIT(STATUS(SCpnt->device->host->io_port),
11831da177e4SLinus Torvalds 	     STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	/*
11861da177e4SLinus Torvalds 	 * Now try to pick up the pieces.  For all pending commands,
11871da177e4SLinus Torvalds 	 * free any internal data structures, and basically clear things
11881da177e4SLinus Torvalds 	 * out.  We do not try and restart any commands or anything -
11891da177e4SLinus Torvalds 	 * the strategy handler takes care of that crap.
11901da177e4SLinus Torvalds 	 */
11911da177e4SLinus Torvalds 	printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
1194e98878f7SOndrej Zary 		if (aha1542->SCint[i] != NULL) {
11951da177e4SLinus Torvalds 			Scsi_Cmnd *SCtmp;
1196e98878f7SOndrej Zary 			SCtmp = aha1542->SCint[i];
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 			if (SCtmp->device->soft_reset) {
12001da177e4SLinus Torvalds 				/*
12011da177e4SLinus Torvalds 				 * If this device implements the soft reset option,
12021da177e4SLinus Torvalds 				 * then it is still holding onto the command, and
12031da177e4SLinus Torvalds 				 * may yet complete it.  In this case, we don't
12041da177e4SLinus Torvalds 				 * flush the data.
12051da177e4SLinus Torvalds 				 */
12061da177e4SLinus Torvalds 				continue;
12071da177e4SLinus Torvalds 			}
12081da177e4SLinus Torvalds 			kfree(SCtmp->host_scribble);
12091da177e4SLinus Torvalds 			SCtmp->host_scribble = NULL;
1210e98878f7SOndrej Zary 			aha1542->SCint[i] = NULL;
1211e98878f7SOndrej Zary 			aha1542->mb[i].status = 0;
12121da177e4SLinus Torvalds 		}
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds 
121568b3aa7cSJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
12161da177e4SLinus Torvalds 	return SUCCESS;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds fail:
121968b3aa7cSJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
12201da177e4SLinus Torvalds 	return FAILED;
12211da177e4SLinus Torvalds }
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds static int aha1542_host_reset(Scsi_Cmnd * SCpnt)
12241da177e4SLinus Torvalds {
1225e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
12261da177e4SLinus Torvalds 	int i;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	/*
12291da177e4SLinus Torvalds 	 * This does a scsi reset for all devices on the bus.
12301da177e4SLinus Torvalds 	 * In principle, we could also reset the 1542 - should
12311da177e4SLinus Torvalds 	 * we do this?  Try this first, and we can add that later
12321da177e4SLinus Torvalds 	 * if it turns out to be useful.
12331da177e4SLinus Torvalds 	 */
12341da177e4SLinus Torvalds 	outb(HRST | SCRST, CONTROL(SCpnt->device->host->io_port));
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	/*
12371da177e4SLinus Torvalds 	 * Wait for the thing to settle down a bit.  Unfortunately
12381da177e4SLinus Torvalds 	 * this is going to basically lock up the machine while we
12391da177e4SLinus Torvalds 	 * wait for this to complete.  To be 100% correct, we need to
12401da177e4SLinus Torvalds 	 * check for timeout, and if we are doing something like this
12411da177e4SLinus Torvalds 	 * we are pretty desperate anyways.
12421da177e4SLinus Torvalds 	 */
12431da177e4SLinus Torvalds 	ssleep(4);
12441da177e4SLinus Torvalds 	spin_lock_irq(SCpnt->device->host->host_lock);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	WAIT(STATUS(SCpnt->device->host->io_port),
12471da177e4SLinus Torvalds 	     STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	/*
12501da177e4SLinus Torvalds 	 * We need to do this too before the 1542 can interact with
12511da177e4SLinus Torvalds 	 * us again.
12521da177e4SLinus Torvalds 	 */
12531da177e4SLinus Torvalds 	setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	/*
12561da177e4SLinus Torvalds 	 * Now try to pick up the pieces.  For all pending commands,
12571da177e4SLinus Torvalds 	 * free any internal data structures, and basically clear things
12581da177e4SLinus Torvalds 	 * out.  We do not try and restart any commands or anything -
12591da177e4SLinus Torvalds 	 * the strategy handler takes care of that crap.
12601da177e4SLinus Torvalds 	 */
12611da177e4SLinus Torvalds 	printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
1264e98878f7SOndrej Zary 		if (aha1542->SCint[i] != NULL) {
12651da177e4SLinus Torvalds 			Scsi_Cmnd *SCtmp;
1266e98878f7SOndrej Zary 			SCtmp = aha1542->SCint[i];
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 			if (SCtmp->device->soft_reset) {
12691da177e4SLinus Torvalds 				/*
12701da177e4SLinus Torvalds 				 * If this device implements the soft reset option,
12711da177e4SLinus Torvalds 				 * then it is still holding onto the command, and
12721da177e4SLinus Torvalds 				 * may yet complete it.  In this case, we don't
12731da177e4SLinus Torvalds 				 * flush the data.
12741da177e4SLinus Torvalds 				 */
12751da177e4SLinus Torvalds 				continue;
12761da177e4SLinus Torvalds 			}
12771da177e4SLinus Torvalds 			kfree(SCtmp->host_scribble);
12781da177e4SLinus Torvalds 			SCtmp->host_scribble = NULL;
1279e98878f7SOndrej Zary 			aha1542->SCint[i] = NULL;
1280e98878f7SOndrej Zary 			aha1542->mb[i].status = 0;
12811da177e4SLinus Torvalds 		}
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds 
1284df0ae249SJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
12851da177e4SLinus Torvalds 	return SUCCESS;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds fail:
1288df0ae249SJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
12891da177e4SLinus Torvalds 	return FAILED;
12901da177e4SLinus Torvalds }
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds static int aha1542_biosparam(struct scsi_device *sdev,
12931da177e4SLinus Torvalds 		struct block_device *bdev, sector_t capacity, int *ip)
12941da177e4SLinus Torvalds {
1295e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
12961da177e4SLinus Torvalds 	int translation_algorithm;
12971da177e4SLinus Torvalds 	int size = capacity;
12981da177e4SLinus Torvalds 
1299e98878f7SOndrej Zary 	translation_algorithm = aha1542->bios_translation;
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
13021da177e4SLinus Torvalds 		/* Please verify that this is the same as what DOS returns */
13031da177e4SLinus Torvalds 		ip[0] = 255;
13041da177e4SLinus Torvalds 		ip[1] = 63;
13051da177e4SLinus Torvalds 		ip[2] = size / 255 / 63;
13061da177e4SLinus Torvalds 	} else {
13071da177e4SLinus Torvalds 		ip[0] = 64;
13081da177e4SLinus Torvalds 		ip[1] = 32;
13091da177e4SLinus Torvalds 		ip[2] = size >> 11;
13101da177e4SLinus Torvalds 	}
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	return 0;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds MODULE_LICENSE("GPL");
13151da177e4SLinus Torvalds 
1316d0be4a7dSChristoph Hellwig static struct scsi_host_template driver_template = {
1317643a7c43SOndrej Zary 	.module			= THIS_MODULE,
13181da177e4SLinus Torvalds 	.proc_name		= "aha1542",
13191da177e4SLinus Torvalds 	.name			= "Adaptec 1542",
13201da177e4SLinus Torvalds 	.queuecommand		= aha1542_queuecommand,
13211da177e4SLinus Torvalds 	.eh_device_reset_handler= aha1542_dev_reset,
13221da177e4SLinus Torvalds 	.eh_bus_reset_handler	= aha1542_bus_reset,
13231da177e4SLinus Torvalds 	.eh_host_reset_handler	= aha1542_host_reset,
13241da177e4SLinus Torvalds 	.bios_param		= aha1542_biosparam,
13251da177e4SLinus Torvalds 	.can_queue		= AHA1542_MAILBOXES,
13261da177e4SLinus Torvalds 	.this_id		= 7,
132710be6250SOndrej Zary 	.sg_tablesize		= 16,
132810be6250SOndrej Zary 	.cmd_per_lun		= 1,
13291da177e4SLinus Torvalds 	.unchecked_isa_dma	= 1,
13301da177e4SLinus Torvalds 	.use_clustering		= ENABLE_CLUSTERING,
13311da177e4SLinus Torvalds };
1332643a7c43SOndrej Zary 
1333643a7c43SOndrej Zary static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1334643a7c43SOndrej Zary {
1335643a7c43SOndrej Zary 	struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1336643a7c43SOndrej Zary 
1337643a7c43SOndrej Zary 	if (!sh)
1338643a7c43SOndrej Zary 		return 0;
1339643a7c43SOndrej Zary 
1340643a7c43SOndrej Zary 	dev_set_drvdata(pdev, sh);
1341643a7c43SOndrej Zary 	return 1;
1342643a7c43SOndrej Zary }
1343643a7c43SOndrej Zary 
1344643a7c43SOndrej Zary static int aha1542_isa_remove(struct device *pdev,
1345643a7c43SOndrej Zary 				    unsigned int ndev)
1346643a7c43SOndrej Zary {
1347643a7c43SOndrej Zary 	aha1542_release(dev_get_drvdata(pdev));
1348643a7c43SOndrej Zary 	dev_set_drvdata(pdev, NULL);
1349643a7c43SOndrej Zary 	return 0;
1350643a7c43SOndrej Zary }
1351643a7c43SOndrej Zary 
1352643a7c43SOndrej Zary static struct isa_driver aha1542_isa_driver = {
1353643a7c43SOndrej Zary 	.match		= aha1542_isa_match,
1354643a7c43SOndrej Zary 	.remove		= aha1542_isa_remove,
1355643a7c43SOndrej Zary 	.driver		= {
1356643a7c43SOndrej Zary 		.name	= "aha1542"
1357643a7c43SOndrej Zary 	},
1358643a7c43SOndrej Zary };
1359643a7c43SOndrej Zary static int isa_registered;
1360643a7c43SOndrej Zary 
1361643a7c43SOndrej Zary #ifdef CONFIG_PNP
1362643a7c43SOndrej Zary static struct pnp_device_id aha1542_pnp_ids[] = {
1363643a7c43SOndrej Zary 	{ .id = "ADP1542" },
1364643a7c43SOndrej Zary 	{ .id = "" }
1365643a7c43SOndrej Zary };
1366643a7c43SOndrej Zary MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1367643a7c43SOndrej Zary 
1368643a7c43SOndrej Zary static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1369643a7c43SOndrej Zary {
1370643a7c43SOndrej Zary 	int indx;
1371643a7c43SOndrej Zary 	struct Scsi_Host *sh;
1372643a7c43SOndrej Zary 
1373643a7c43SOndrej Zary 	for (indx = 0; indx < ARRAY_SIZE(bases); indx++) {
1374643a7c43SOndrej Zary 		if (bases[indx])
1375643a7c43SOndrej Zary 			continue;
1376643a7c43SOndrej Zary 
1377643a7c43SOndrej Zary 		if (pnp_activate_dev(pdev) < 0)
1378643a7c43SOndrej Zary 			continue;
1379643a7c43SOndrej Zary 
1380643a7c43SOndrej Zary 		bases[indx] = pnp_port_start(pdev, 0);
1381643a7c43SOndrej Zary 
1382643a7c43SOndrej Zary 		/* The card can be queried for its DMA, we have
1383643a7c43SOndrej Zary 		   the DMA set up that is enough */
1384643a7c43SOndrej Zary 
1385643a7c43SOndrej Zary 		printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
1386643a7c43SOndrej Zary 	}
1387643a7c43SOndrej Zary 
1388643a7c43SOndrej Zary 	sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1389643a7c43SOndrej Zary 	if (!sh)
1390643a7c43SOndrej Zary 		return -ENODEV;
1391643a7c43SOndrej Zary 
1392643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, sh);
1393643a7c43SOndrej Zary 	return 0;
1394643a7c43SOndrej Zary }
1395643a7c43SOndrej Zary 
1396643a7c43SOndrej Zary static void aha1542_pnp_remove(struct pnp_dev *pdev)
1397643a7c43SOndrej Zary {
1398643a7c43SOndrej Zary 	aha1542_release(pnp_get_drvdata(pdev));
1399643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, NULL);
1400643a7c43SOndrej Zary }
1401643a7c43SOndrej Zary 
1402643a7c43SOndrej Zary static struct pnp_driver aha1542_pnp_driver = {
1403643a7c43SOndrej Zary 	.name		= "aha1542",
1404643a7c43SOndrej Zary 	.id_table	= aha1542_pnp_ids,
1405643a7c43SOndrej Zary 	.probe		= aha1542_pnp_probe,
1406643a7c43SOndrej Zary 	.remove		= aha1542_pnp_remove,
1407643a7c43SOndrej Zary };
1408643a7c43SOndrej Zary static int pnp_registered;
1409643a7c43SOndrej Zary #endif /* CONFIG_PNP */
1410643a7c43SOndrej Zary 
1411643a7c43SOndrej Zary static int __init aha1542_init(void)
1412643a7c43SOndrej Zary {
1413643a7c43SOndrej Zary 	int ret = 0;
1414643a7c43SOndrej Zary #ifdef MODULE
1415643a7c43SOndrej Zary 	int atbt = -1;
1416643a7c43SOndrej Zary 
1417643a7c43SOndrej Zary 	bases[0] = aha1542[0];
1418643a7c43SOndrej Zary 	setup_buson[0] = aha1542[1];
1419643a7c43SOndrej Zary 	setup_busoff[0] = aha1542[2];
1420643a7c43SOndrej Zary 
1421643a7c43SOndrej Zary 	switch (aha1542[3]) {
1422643a7c43SOndrej Zary 	case 5:
1423643a7c43SOndrej Zary 		atbt = 0x00;
1424643a7c43SOndrej Zary 		break;
1425643a7c43SOndrej Zary 	case 6:
1426643a7c43SOndrej Zary 		atbt = 0x04;
1427643a7c43SOndrej Zary 		break;
1428643a7c43SOndrej Zary 	case 7:
1429643a7c43SOndrej Zary 		atbt = 0x01;
1430643a7c43SOndrej Zary 		break;
1431643a7c43SOndrej Zary 	case 8:
1432643a7c43SOndrej Zary 		atbt = 0x02;
1433643a7c43SOndrej Zary 		break;
1434643a7c43SOndrej Zary 	case 10:
1435643a7c43SOndrej Zary 		atbt = 0x03;
1436643a7c43SOndrej Zary 		break;
1437643a7c43SOndrej Zary 	};
1438643a7c43SOndrej Zary 	setup_dmaspeed[0] = atbt;
1439643a7c43SOndrej Zary #endif
1440643a7c43SOndrej Zary 
1441643a7c43SOndrej Zary #ifdef CONFIG_PNP
1442643a7c43SOndrej Zary 	if (isapnp) {
1443643a7c43SOndrej Zary 		ret = pnp_register_driver(&aha1542_pnp_driver);
1444643a7c43SOndrej Zary 		if (!ret)
1445643a7c43SOndrej Zary 			pnp_registered = 1;
1446643a7c43SOndrej Zary 	}
1447643a7c43SOndrej Zary #endif
1448643a7c43SOndrej Zary 	ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1449643a7c43SOndrej Zary 	if (!ret)
1450643a7c43SOndrej Zary 		isa_registered = 1;
1451643a7c43SOndrej Zary 
1452643a7c43SOndrej Zary #ifdef CONFIG_PNP
1453643a7c43SOndrej Zary 	if (pnp_registered)
1454643a7c43SOndrej Zary 		ret = 0;
1455643a7c43SOndrej Zary #endif
1456643a7c43SOndrej Zary 	if (isa_registered)
1457643a7c43SOndrej Zary 		ret = 0;
1458643a7c43SOndrej Zary 
1459643a7c43SOndrej Zary 	return ret;
1460643a7c43SOndrej Zary }
1461643a7c43SOndrej Zary 
1462643a7c43SOndrej Zary static void __exit aha1542_exit(void)
1463643a7c43SOndrej Zary {
1464643a7c43SOndrej Zary #ifdef CONFIG_PNP
1465643a7c43SOndrej Zary 	if (pnp_registered)
1466643a7c43SOndrej Zary 		pnp_unregister_driver(&aha1542_pnp_driver);
1467643a7c43SOndrej Zary #endif
1468643a7c43SOndrej Zary 	if (isa_registered)
1469643a7c43SOndrej Zary 		isa_unregister_driver(&aha1542_isa_driver);
1470643a7c43SOndrej Zary }
1471643a7c43SOndrej Zary 
1472643a7c43SOndrej Zary module_init(aha1542_init);
1473643a7c43SOndrej Zary module_exit(aha1542_exit);
1474