xref: /linux/drivers/scsi/aha1542.c (revision b80ca4f7ee36c26d300c5a8f429e73372d153379)
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
241da177e4SLinus Torvalds  *  Modified by Adam Fritzler <mid@auk.cx>
251da177e4SLinus Torvalds  *        Added proper detection of the AHA-1640 (MCA version of AHA-1540)
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>
381da177e4SLinus Torvalds #include <linux/isapnp.h>
391da177e4SLinus Torvalds #include <linux/blkdev.h>
401da177e4SLinus Torvalds #include <linux/mca.h>
411da177e4SLinus Torvalds #include <linux/mca-legacy.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <asm/dma.h>
441da177e4SLinus Torvalds #include <asm/system.h>
451da177e4SLinus Torvalds #include <asm/io.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include "scsi.h"
481da177e4SLinus Torvalds #include <scsi/scsi_host.h>
491da177e4SLinus Torvalds #include "aha1542.h"
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #define SCSI_BUF_PA(address)	isa_virt_to_bus(address)
5245711f1aSJens Axboe #define SCSI_SG_PA(sgent)	(isa_page_to_bus(sg_page((sgent))) + (sgent)->offset)
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static void BAD_SG_DMA(Scsi_Cmnd * SCpnt,
5551cf2249SJens Axboe 		       struct scatterlist *sgp,
561da177e4SLinus Torvalds 		       int nseg,
571da177e4SLinus Torvalds 		       int badseg)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	printk(KERN_CRIT "sgpnt[%d:%d] page %p/0x%llx length %u\n",
6045711f1aSJens Axboe 	       badseg, nseg, sg_virt(sgp),
6151cf2249SJens Axboe 	       (unsigned long long)SCSI_SG_PA(sgp),
6251cf2249SJens Axboe 	       sgp->length);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	/*
651da177e4SLinus Torvalds 	 * Not safe to continue.
661da177e4SLinus Torvalds 	 */
671da177e4SLinus Torvalds 	panic("Buffer at physical address > 16Mb used for aha1542");
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #include<linux/stat.h>
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #ifdef DEBUG
731da177e4SLinus Torvalds #define DEB(x) x
741da177e4SLinus Torvalds #else
751da177e4SLinus Torvalds #define DEB(x)
761da177e4SLinus Torvalds #endif
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /*
791da177e4SLinus 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 $";
801da177e4SLinus Torvalds  */
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /* The adaptec can be configured for quite a number of addresses, but
831da177e4SLinus Torvalds    I generally do not want the card poking around at random.  We allow
841da177e4SLinus Torvalds    two addresses - this allows people to use the Adaptec with a Midi
851da177e4SLinus Torvalds    card, which also used 0x330 -- can be overridden with LILO! */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #define MAXBOARDS 4		/* Increase this and the sizes of the
881da177e4SLinus Torvalds 				   arrays below, if you need more.. */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* Boards 3,4 slots are reserved for ISAPnP/MCA scans */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds static unsigned int bases[MAXBOARDS] __initdata = {0x330, 0x334, 0, 0};
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* set by aha1542_setup according to the command line; they also may
951da177e4SLinus Torvalds    be marked __initdata, but require zero initializers then */
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds static int setup_called[MAXBOARDS];
981da177e4SLinus Torvalds static int setup_buson[MAXBOARDS];
991da177e4SLinus Torvalds static int setup_busoff[MAXBOARDS];
1001da177e4SLinus Torvalds static int setup_dmaspeed[MAXBOARDS] __initdata = { -1, -1, -1, -1 };
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /*
1031da177e4SLinus Torvalds  * LILO/Module params:  aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
1041da177e4SLinus Torvalds  *
1051da177e4SLinus Torvalds  * Where:  <PORTBASE> is any of the valid AHA addresses:
1061da177e4SLinus Torvalds  *                      0x130, 0x134, 0x230, 0x234, 0x330, 0x334
1071da177e4SLinus Torvalds  *         <BUSON>  is the time (in microsecs) that AHA spends on the AT-bus
1081da177e4SLinus Torvalds  *                  when transferring data.  1542A power-on default is 11us,
1091da177e4SLinus Torvalds  *                  valid values are in range: 2..15 (decimal)
1101da177e4SLinus Torvalds  *         <BUSOFF> is the time that AHA spends OFF THE BUS after while
1111da177e4SLinus Torvalds  *                  it is transferring data (not to monopolize the bus).
1121da177e4SLinus Torvalds  *                  Power-on default is 4us, valid range: 1..64 microseconds.
1131da177e4SLinus Torvalds  *         <DMASPEED> Default is jumper selected (1542A: on the J1),
1141da177e4SLinus Torvalds  *                  but experimenter can alter it with this.
1151da177e4SLinus Torvalds  *                  Valid values: 5, 6, 7, 8, 10 (MB/s)
1161da177e4SLinus Torvalds  *                  Factory default is 5 MB/s.
1171da177e4SLinus Torvalds  */
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds #if defined(MODULE)
1201da177e4SLinus Torvalds static int isapnp = 0;
1211da177e4SLinus Torvalds static int aha1542[] = {0x330, 11, 4, -1};
1221da177e4SLinus Torvalds module_param_array(aha1542, int, NULL, 0);
1231da177e4SLinus Torvalds module_param(isapnp, bool, 0);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds static struct isapnp_device_id id_table[] __initdata = {
1261da177e4SLinus Torvalds 	{
1271da177e4SLinus Torvalds 		ISAPNP_ANY_ID, ISAPNP_ANY_ID,
1281da177e4SLinus Torvalds 		ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1542),
1291da177e4SLinus Torvalds 		0
1301da177e4SLinus Torvalds 	},
1311da177e4SLinus Torvalds 	{0}
1321da177e4SLinus Torvalds };
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds MODULE_DEVICE_TABLE(isapnp, id_table);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #else
1371da177e4SLinus Torvalds static int isapnp = 1;
1381da177e4SLinus Torvalds #endif
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds #define BIOS_TRANSLATION_1632 0	/* Used by some old 1542A boards */
1411da177e4SLinus Torvalds #define BIOS_TRANSLATION_6432 1	/* Default case these days */
1421da177e4SLinus Torvalds #define BIOS_TRANSLATION_25563 2	/* Big disk case */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds struct aha1542_hostdata {
1451da177e4SLinus Torvalds 	/* This will effectively start both of them at the first mailbox */
1461da177e4SLinus Torvalds 	int bios_translation;	/* Mapping bios uses - for compatibility */
1471da177e4SLinus Torvalds 	int aha1542_last_mbi_used;
1481da177e4SLinus Torvalds 	int aha1542_last_mbo_used;
1491da177e4SLinus Torvalds 	Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
1501da177e4SLinus Torvalds 	struct mailbox mb[2 * AHA1542_MAILBOXES];
1511da177e4SLinus Torvalds 	struct ccb ccb[AHA1542_MAILBOXES];
1521da177e4SLinus Torvalds };
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds #define HOSTDATA(host) ((struct aha1542_hostdata *) &host->hostdata)
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds static struct Scsi_Host *aha_host[7];	/* One for each IRQ level (9-15) */
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static DEFINE_SPINLOCK(aha1542_lock);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds #define WAITnexttimeout 3000000
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds static void setup_mailboxes(int base_io, struct Scsi_Host *shpnt);
1651da177e4SLinus Torvalds static int aha1542_restart(struct Scsi_Host *shost);
1667d12e780SDavid Howells static void aha1542_intr_handle(struct Scsi_Host *shost, void *dev_id);
1677d12e780SDavid Howells static irqreturn_t do_aha1542_intr_handle(int irq, void *dev_id);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds #define aha1542_intr_reset(base)  outb(IRST, CONTROL(base))
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds #define WAIT(port, mask, allof, noneof)					\
1721da177e4SLinus Torvalds  { register int WAITbits;						\
1731da177e4SLinus Torvalds    register int WAITtimeout = WAITnexttimeout;				\
1741da177e4SLinus Torvalds    while (1) {								\
1751da177e4SLinus Torvalds      WAITbits = inb(port) & (mask);					\
1761da177e4SLinus Torvalds      if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
1771da177e4SLinus Torvalds        break;                                                         	\
1781da177e4SLinus Torvalds      if (--WAITtimeout == 0) goto fail;					\
1791da177e4SLinus Torvalds    }									\
1801da177e4SLinus Torvalds  }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /* Similar to WAIT, except we use the udelay call to regulate the
1831da177e4SLinus Torvalds    amount of time we wait.  */
1841da177e4SLinus Torvalds #define WAITd(port, mask, allof, noneof, timeout)			\
1851da177e4SLinus Torvalds  { register int WAITbits;						\
1861da177e4SLinus Torvalds    register int WAITtimeout = timeout;					\
1871da177e4SLinus Torvalds    while (1) {								\
1881da177e4SLinus Torvalds      WAITbits = inb(port) & (mask);					\
1891da177e4SLinus Torvalds      if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
1901da177e4SLinus Torvalds        break;                                                         	\
1911da177e4SLinus Torvalds      mdelay(1);							\
1921da177e4SLinus Torvalds      if (--WAITtimeout == 0) goto fail;					\
1931da177e4SLinus Torvalds    }									\
1941da177e4SLinus Torvalds  }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static void aha1542_stat(void)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds /*	int s = inb(STATUS), i = inb(INTRFLAGS);
1991da177e4SLinus Torvalds 	printk("status=%x intrflags=%x\n", s, i, WAITnexttimeout-WAITtimeout); */
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds /* This is a bit complicated, but we need to make sure that an interrupt
2031da177e4SLinus Torvalds    routine does not send something out while we are in the middle of this.
2041da177e4SLinus Torvalds    Fortunately, it is only at boot time that multi-byte messages
2051da177e4SLinus Torvalds    are ever sent. */
2061da177e4SLinus Torvalds static int aha1542_out(unsigned int base, unchar * cmdp, int len)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	unsigned long flags = 0;
2091da177e4SLinus Torvalds 	int got_lock;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (len == 1) {
2121da177e4SLinus Torvalds 		got_lock = 0;
2131da177e4SLinus Torvalds 		while (1 == 1) {
2141da177e4SLinus Torvalds 			WAIT(STATUS(base), CDF, 0, CDF);
2151da177e4SLinus Torvalds 			spin_lock_irqsave(&aha1542_lock, flags);
2161da177e4SLinus Torvalds 			if (inb(STATUS(base)) & CDF) {
2171da177e4SLinus Torvalds 				spin_unlock_irqrestore(&aha1542_lock, flags);
2181da177e4SLinus Torvalds 				continue;
2191da177e4SLinus Torvalds 			}
2201da177e4SLinus Torvalds 			outb(*cmdp, DATA(base));
2211da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
2221da177e4SLinus Torvalds 			return 0;
2231da177e4SLinus Torvalds 		}
2241da177e4SLinus Torvalds 	} else {
2251da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
2261da177e4SLinus Torvalds 		got_lock = 1;
2271da177e4SLinus Torvalds 		while (len--) {
2281da177e4SLinus Torvalds 			WAIT(STATUS(base), CDF, 0, CDF);
2291da177e4SLinus Torvalds 			outb(*cmdp++, DATA(base));
2301da177e4SLinus Torvalds 		}
2311da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 	return 0;
2341da177e4SLinus Torvalds fail:
2351da177e4SLinus Torvalds 	if (got_lock)
2361da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
2371da177e4SLinus Torvalds 	printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
2381da177e4SLinus Torvalds 	aha1542_stat();
2391da177e4SLinus Torvalds 	return 1;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds /* Only used at boot time, so we do not need to worry about latency as much
2431da177e4SLinus Torvalds    here */
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static int __init aha1542_in(unsigned int base, unchar * cmdp, int len)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	unsigned long flags;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
2501da177e4SLinus Torvalds 	while (len--) {
2511da177e4SLinus Torvalds 		WAIT(STATUS(base), DF, DF, 0);
2521da177e4SLinus Torvalds 		*cmdp++ = inb(DATA(base));
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
2551da177e4SLinus Torvalds 	return 0;
2561da177e4SLinus Torvalds fail:
2571da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
2581da177e4SLinus Torvalds 	printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
2591da177e4SLinus Torvalds 	aha1542_stat();
2601da177e4SLinus Torvalds 	return 1;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds /* Similar to aha1542_in, except that we wait a very short period of time.
2641da177e4SLinus Torvalds    We use this if we know the board is alive and awake, but we are not sure
2651da177e4SLinus Torvalds    if the board will respond to the command we are about to send or not */
2661da177e4SLinus Torvalds static int __init aha1542_in1(unsigned int base, unchar * cmdp, int len)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	unsigned long flags;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
2711da177e4SLinus Torvalds 	while (len--) {
2721da177e4SLinus Torvalds 		WAITd(STATUS(base), DF, DF, 0, 100);
2731da177e4SLinus Torvalds 		*cmdp++ = inb(DATA(base));
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
2761da177e4SLinus Torvalds 	return 0;
2771da177e4SLinus Torvalds fail:
2781da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
2791da177e4SLinus Torvalds 	return 1;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds static int makecode(unsigned hosterr, unsigned scsierr)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	switch (hosterr) {
2851da177e4SLinus Torvalds 	case 0x0:
2861da177e4SLinus Torvalds 	case 0xa:		/* Linked command complete without error and linked normally */
2871da177e4SLinus Torvalds 	case 0xb:		/* Linked command complete without error, interrupt generated */
2881da177e4SLinus Torvalds 		hosterr = 0;
2891da177e4SLinus Torvalds 		break;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	case 0x11:		/* Selection time out-The initiator selection or target
2921da177e4SLinus Torvalds 				   reselection was not complete within the SCSI Time out period */
2931da177e4SLinus Torvalds 		hosterr = DID_TIME_OUT;
2941da177e4SLinus Torvalds 		break;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
2971da177e4SLinus Torvalds 				   than was allocated by the Data Length field or the sum of the
2981da177e4SLinus Torvalds 				   Scatter / Gather Data Length fields. */
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	case 0x15:		/* MBO command was not 00, 01 or 02-The first byte of the CB was
3031da177e4SLinus Torvalds 				   invalid. This usually indicates a software failure. */
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	case 0x16:		/* Invalid CCB Operation Code-The first byte of the CCB was invalid.
3061da177e4SLinus Torvalds 				   This usually indicates a software failure. */
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	case 0x17:		/* Linked CCB does not have the same LUN-A subsequent CCB of a set
3091da177e4SLinus Torvalds 				   of linked CCB's does not specify the same logical unit number as
3101da177e4SLinus Torvalds 				   the first. */
3111da177e4SLinus Torvalds 	case 0x18:		/* Invalid Target Direction received from Host-The direction of a
3121da177e4SLinus Torvalds 				   Target Mode CCB was invalid. */
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	case 0x19:		/* Duplicate CCB Received in Target Mode-More than once CCB was
3151da177e4SLinus Torvalds 				   received to service data transfer between the same target LUN
3161da177e4SLinus Torvalds 				   and initiator SCSI ID in the same direction. */
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	case 0x1a:		/* Invalid CCB or Segment List Parameter-A segment list with a zero
3191da177e4SLinus Torvalds 				   length segment or invalid segment list boundaries was received.
3201da177e4SLinus Torvalds 				   A CCB parameter was invalid. */
3211da177e4SLinus Torvalds 		DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
3221da177e4SLinus Torvalds 		hosterr = DID_ERROR;	/* Couldn't find any better */
3231da177e4SLinus Torvalds 		break;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
3261da177e4SLinus Torvalds 				   phase sequence was requested by the target. The host adapter
3271da177e4SLinus Torvalds 				   will generate a SCSI Reset Condition, notifying the host with
3281da177e4SLinus Torvalds 				   a SCRD interrupt */
3291da177e4SLinus Torvalds 		hosterr = DID_RESET;
3301da177e4SLinus Torvalds 		break;
3311da177e4SLinus Torvalds 	default:
3321da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
3331da177e4SLinus Torvalds 		break;
3341da177e4SLinus Torvalds 	}
3351da177e4SLinus Torvalds 	return scsierr | (hosterr << 16);
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds static int __init aha1542_test_port(int bse, struct Scsi_Host *shpnt)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	unchar inquiry_cmd[] = {CMD_INQUIRY};
3411da177e4SLinus Torvalds 	unchar inquiry_result[4];
3421da177e4SLinus Torvalds 	unchar *cmdp;
3431da177e4SLinus Torvalds 	int len;
3441da177e4SLinus Torvalds 	volatile int debug = 0;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	/* Quick and dirty test for presence of the card. */
3471da177e4SLinus Torvalds 	if (inb(STATUS(bse)) == 0xff)
3481da177e4SLinus Torvalds 		return 0;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	/* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	/*  DEB(printk("aha1542_test_port called \n")); */
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	/* In case some other card was probing here, reset interrupts */
3551da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	mdelay(20);		/* Wait a little bit for things to settle down. */
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	debug = 1;
3621da177e4SLinus Torvalds 	/* Expect INIT and IDLE, any of the others are bad */
3631da177e4SLinus Torvalds 	WAIT(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	debug = 2;
3661da177e4SLinus Torvalds 	/* Shouldn't have generated any interrupts during reset */
3671da177e4SLinus Torvalds 	if (inb(INTRFLAGS(bse)) & INTRMASK)
3681da177e4SLinus Torvalds 		goto fail;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	/* Perform a host adapter inquiry instead so we do not need to set
3721da177e4SLinus Torvalds 	   up the mailboxes ahead of time */
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	aha1542_out(bse, inquiry_cmd, 1);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	debug = 3;
3771da177e4SLinus Torvalds 	len = 4;
3781da177e4SLinus Torvalds 	cmdp = &inquiry_result[0];
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	while (len--) {
3811da177e4SLinus Torvalds 		WAIT(STATUS(bse), DF, DF, 0);
3821da177e4SLinus Torvalds 		*cmdp++ = inb(DATA(bse));
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	debug = 8;
3861da177e4SLinus Torvalds 	/* Reading port should reset DF */
3871da177e4SLinus Torvalds 	if (inb(STATUS(bse)) & DF)
3881da177e4SLinus Torvalds 		goto fail;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	debug = 9;
3911da177e4SLinus Torvalds 	/* When HACC, command is completed, and we're though testing */
3921da177e4SLinus Torvalds 	WAIT(INTRFLAGS(bse), HACC, HACC, 0);
3931da177e4SLinus Torvalds 	/* now initialize adapter */
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	debug = 10;
3961da177e4SLinus Torvalds 	/* Clear interrupts */
3971da177e4SLinus Torvalds 	outb(IRST, CONTROL(bse));
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	debug = 11;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	return debug;		/* 1 = ok */
4021da177e4SLinus Torvalds fail:
4031da177e4SLinus Torvalds 	return 0;		/* 0 = not ok */
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
4077d12e780SDavid Howells static irqreturn_t do_aha1542_intr_handle(int irq, void *dev_id)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	unsigned long flags;
4101da177e4SLinus Torvalds 	struct Scsi_Host *shost;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	shost = aha_host[irq - 9];
4131da177e4SLinus Torvalds 	if (!shost)
4141da177e4SLinus Torvalds 		panic("Splunge!");
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
4177d12e780SDavid Howells 	aha1542_intr_handle(shost, dev_id);
4181da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
4191da177e4SLinus Torvalds 	return IRQ_HANDLED;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds /* A "high" level interrupt handler */
4237d12e780SDavid Howells static void aha1542_intr_handle(struct Scsi_Host *shost, void *dev_id)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	void (*my_done) (Scsi_Cmnd *) = NULL;
4261da177e4SLinus Torvalds 	int errstatus, mbi, mbo, mbistatus;
4271da177e4SLinus Torvalds 	int number_serviced;
4281da177e4SLinus Torvalds 	unsigned long flags;
4291da177e4SLinus Torvalds 	Scsi_Cmnd *SCtmp;
4301da177e4SLinus Torvalds 	int flag;
4311da177e4SLinus Torvalds 	int needs_restart;
4321da177e4SLinus Torvalds 	struct mailbox *mb;
4331da177e4SLinus Torvalds 	struct ccb *ccb;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	mb = HOSTDATA(shost)->mb;
4361da177e4SLinus Torvalds 	ccb = HOSTDATA(shost)->ccb;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds #ifdef DEBUG
4391da177e4SLinus Torvalds 	{
4401da177e4SLinus Torvalds 		flag = inb(INTRFLAGS(shost->io_port));
4411da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_intr_handle: ");
4421da177e4SLinus Torvalds 		if (!(flag & ANYINTR))
4431da177e4SLinus Torvalds 			printk("no interrupt?");
4441da177e4SLinus Torvalds 		if (flag & MBIF)
4451da177e4SLinus Torvalds 			printk("MBIF ");
4461da177e4SLinus Torvalds 		if (flag & MBOA)
4471da177e4SLinus Torvalds 			printk("MBOF ");
4481da177e4SLinus Torvalds 		if (flag & HACC)
4491da177e4SLinus Torvalds 			printk("HACC ");
4501da177e4SLinus Torvalds 		if (flag & SCRD)
4511da177e4SLinus Torvalds 			printk("SCRD ");
4521da177e4SLinus Torvalds 		printk("status %02x\n", inb(STATUS(shost->io_port)));
4531da177e4SLinus Torvalds 	};
4541da177e4SLinus Torvalds #endif
4551da177e4SLinus Torvalds 	number_serviced = 0;
4561da177e4SLinus Torvalds 	needs_restart = 0;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	while (1 == 1) {
4591da177e4SLinus Torvalds 		flag = inb(INTRFLAGS(shost->io_port));
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 		/* Check for unusual interrupts.  If any of these happen, we should
4621da177e4SLinus Torvalds 		   probably do something special, but for now just printing a message
4631da177e4SLinus Torvalds 		   is sufficient.  A SCSI reset detected is something that we really
4641da177e4SLinus Torvalds 		   need to deal with in some way. */
4651da177e4SLinus Torvalds 		if (flag & ~MBIF) {
4661da177e4SLinus Torvalds 			if (flag & MBOA)
4671da177e4SLinus Torvalds 				printk("MBOF ");
4681da177e4SLinus Torvalds 			if (flag & HACC)
4691da177e4SLinus Torvalds 				printk("HACC ");
4701da177e4SLinus Torvalds 			if (flag & SCRD) {
4711da177e4SLinus Torvalds 				needs_restart = 1;
4721da177e4SLinus Torvalds 				printk("SCRD ");
4731da177e4SLinus Torvalds 			}
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 		aha1542_intr_reset(shost->io_port);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
4781da177e4SLinus Torvalds 		mbi = HOSTDATA(shost)->aha1542_last_mbi_used + 1;
4791da177e4SLinus Torvalds 		if (mbi >= 2 * AHA1542_MAILBOXES)
4801da177e4SLinus Torvalds 			mbi = AHA1542_MAILBOXES;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 		do {
4831da177e4SLinus Torvalds 			if (mb[mbi].status != 0)
4841da177e4SLinus Torvalds 				break;
4851da177e4SLinus Torvalds 			mbi++;
4861da177e4SLinus Torvalds 			if (mbi >= 2 * AHA1542_MAILBOXES)
4871da177e4SLinus Torvalds 				mbi = AHA1542_MAILBOXES;
4881da177e4SLinus Torvalds 		} while (mbi != HOSTDATA(shost)->aha1542_last_mbi_used);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 		if (mb[mbi].status == 0) {
4911da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
4921da177e4SLinus Torvalds 			/* Hmm, no mail.  Must have read it the last time around */
4931da177e4SLinus Torvalds 			if (!number_serviced && !needs_restart)
4941da177e4SLinus Torvalds 				printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
4951da177e4SLinus Torvalds 			/* We detected a reset.  Restart all pending commands for
4961da177e4SLinus Torvalds 			   devices that use the hard reset option */
4971da177e4SLinus Torvalds 			if (needs_restart)
4981da177e4SLinus Torvalds 				aha1542_restart(shost);
4991da177e4SLinus Torvalds 			return;
5001da177e4SLinus Torvalds 		};
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 		mbo = (scsi2int(mb[mbi].ccbptr) - (SCSI_BUF_PA(&ccb[0]))) / sizeof(struct ccb);
5031da177e4SLinus Torvalds 		mbistatus = mb[mbi].status;
5041da177e4SLinus Torvalds 		mb[mbi].status = 0;
5051da177e4SLinus Torvalds 		HOSTDATA(shost)->aha1542_last_mbi_used = mbi;
5061da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds #ifdef DEBUG
5091da177e4SLinus Torvalds 		{
5101da177e4SLinus Torvalds 			if (ccb[mbo].tarstat | ccb[mbo].hastat)
5111da177e4SLinus Torvalds 				printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
5121da177e4SLinus Torvalds 				       ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
5131da177e4SLinus Torvalds 		};
5141da177e4SLinus Torvalds #endif
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 		if (mbistatus == 3)
5171da177e4SLinus Torvalds 			continue;	/* Aborted command not found */
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds #ifdef DEBUG
5201da177e4SLinus Torvalds 		printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
5211da177e4SLinus Torvalds #endif
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 		SCtmp = HOSTDATA(shost)->SCint[mbo];
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 		if (!SCtmp || !SCtmp->scsi_done) {
5261da177e4SLinus Torvalds 			printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
5271da177e4SLinus Torvalds 			printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
5281da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].idlun, mbo);
5291da177e4SLinus Torvalds 			return;
5301da177e4SLinus Torvalds 		}
5311da177e4SLinus Torvalds 		my_done = SCtmp->scsi_done;
5321da177e4SLinus Torvalds 		kfree(SCtmp->host_scribble);
5331da177e4SLinus Torvalds 		SCtmp->host_scribble = NULL;
5341da177e4SLinus Torvalds 		/* Fetch the sense data, and tuck it away, in the required slot.  The
5351da177e4SLinus Torvalds 		   Adaptec automatically fetches it, and there is no guarantee that
5361da177e4SLinus Torvalds 		   we will still have it in the cdb when we come back */
5371da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2)
5381da177e4SLinus Torvalds 			memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
539*b80ca4f7SFUJITA Tomonori 			       SCSI_SENSE_BUFFERSIZE);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 		/* is there mail :-) */
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 		/* more error checking left out here */
5451da177e4SLinus Torvalds 		if (mbistatus != 1)
5461da177e4SLinus Torvalds 			/* This is surely wrong, but I don't know what's right */
5471da177e4SLinus Torvalds 			errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
5481da177e4SLinus Torvalds 		else
5491da177e4SLinus Torvalds 			errstatus = 0;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds #ifdef DEBUG
5521da177e4SLinus Torvalds 		if (errstatus)
5531da177e4SLinus Torvalds 			printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
5541da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].tarstat);
5551da177e4SLinus Torvalds #endif
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2) {
5581da177e4SLinus Torvalds #ifdef DEBUG
5591da177e4SLinus Torvalds 			int i;
5601da177e4SLinus Torvalds #endif
5611da177e4SLinus Torvalds 			DEB(printk("aha1542_intr_handle: sense:"));
5621da177e4SLinus Torvalds #ifdef DEBUG
5631da177e4SLinus Torvalds 			for (i = 0; i < 12; i++)
5641da177e4SLinus Torvalds 				printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
5651da177e4SLinus Torvalds 			printk("\n");
5661da177e4SLinus Torvalds #endif
5671da177e4SLinus Torvalds 			/*
5681da177e4SLinus Torvalds 			   DEB(printk("aha1542_intr_handle: buf:"));
5691da177e4SLinus Torvalds 			   for (i = 0; i < bufflen; i++)
5701da177e4SLinus Torvalds 			   printk("%02x ", ((unchar *)buff)[i]);
5711da177e4SLinus Torvalds 			   printk("\n");
5721da177e4SLinus Torvalds 			 */
5731da177e4SLinus Torvalds 		}
5741da177e4SLinus Torvalds 		DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
5751da177e4SLinus Torvalds 		SCtmp->result = errstatus;
5761da177e4SLinus Torvalds 		HOSTDATA(shost)->SCint[mbo] = NULL;	/* This effectively frees up the mailbox slot, as
5771da177e4SLinus Torvalds 							   far as queuecommand is concerned */
5781da177e4SLinus Torvalds 		my_done(SCtmp);
5791da177e4SLinus Torvalds 		number_serviced++;
5801da177e4SLinus Torvalds 	};
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds static int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
5841da177e4SLinus Torvalds {
5851da177e4SLinus Torvalds 	unchar ahacmd = CMD_START_SCSI;
5861da177e4SLinus Torvalds 	unchar direction;
5871da177e4SLinus Torvalds 	unchar *cmd = (unchar *) SCpnt->cmnd;
5881da177e4SLinus Torvalds 	unchar target = SCpnt->device->id;
5891da177e4SLinus Torvalds 	unchar lun = SCpnt->device->lun;
5901da177e4SLinus Torvalds 	unsigned long flags;
591fc3fdfccSBoaz Harrosh 	int bufflen = scsi_bufflen(SCpnt);
5921da177e4SLinus Torvalds 	int mbo;
5931da177e4SLinus Torvalds 	struct mailbox *mb;
5941da177e4SLinus Torvalds 	struct ccb *ccb;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	DEB(int i);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	mb = HOSTDATA(SCpnt->device->host)->mb;
5991da177e4SLinus Torvalds 	ccb = HOSTDATA(SCpnt->device->host)->ccb;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	DEB(if (target > 1) {
6021da177e4SLinus Torvalds 	    SCpnt->result = DID_TIME_OUT << 16;
6031da177e4SLinus Torvalds 	    done(SCpnt); return 0;
6041da177e4SLinus Torvalds 	    }
6051da177e4SLinus Torvalds 	);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	if (*cmd == REQUEST_SENSE) {
6081da177e4SLinus Torvalds 		/* Don't do the command - we have the sense data already */
6091da177e4SLinus Torvalds #if 0
6101da177e4SLinus Torvalds 		/* scsi_request_sense() provides a buffer of size 256,
6111da177e4SLinus Torvalds 		   so there is no reason to expect equality */
612*b80ca4f7SFUJITA Tomonori 		if (bufflen != SCSI_SENSE_BUFFERSIZE)
6131da177e4SLinus Torvalds 			printk(KERN_CRIT "aha1542: Wrong buffer length supplied "
6141da177e4SLinus Torvalds 			       "for request sense (%d)\n", bufflen);
6151da177e4SLinus Torvalds #endif
6161da177e4SLinus Torvalds 		SCpnt->result = 0;
6171da177e4SLinus Torvalds 		done(SCpnt);
6181da177e4SLinus Torvalds 		return 0;
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds #ifdef DEBUG
6211da177e4SLinus Torvalds 	if (*cmd == READ_10 || *cmd == WRITE_10)
6221da177e4SLinus Torvalds 		i = xscsi2int(cmd + 2);
6231da177e4SLinus Torvalds 	else if (*cmd == READ_6 || *cmd == WRITE_6)
6241da177e4SLinus Torvalds 		i = scsi2int(cmd + 2);
6251da177e4SLinus Torvalds 	else
6261da177e4SLinus Torvalds 		i = -1;
6271da177e4SLinus Torvalds 	if (done)
6281da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
6291da177e4SLinus Torvalds 	else
6301da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
6311da177e4SLinus Torvalds 	aha1542_stat();
6321da177e4SLinus Torvalds 	printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
6331da177e4SLinus Torvalds 	for (i = 0; i < SCpnt->cmd_len; i++)
6341da177e4SLinus Torvalds 		printk("%02x ", cmd[i]);
6351da177e4SLinus Torvalds 	printk("\n");
6361da177e4SLinus Torvalds 	if (*cmd == WRITE_10 || *cmd == WRITE_6)
6371da177e4SLinus Torvalds 		return 0;	/* we are still testing, so *don't* write */
6381da177e4SLinus Torvalds #endif
6391da177e4SLinus Torvalds 	/* Use the outgoing mailboxes in a round-robin fashion, because this
6401da177e4SLinus Torvalds 	   is how the host adapter will scan for them */
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
6431da177e4SLinus Torvalds 	mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
6441da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
6451da177e4SLinus Torvalds 		mbo = 0;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	do {
6481da177e4SLinus Torvalds 		if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
6491da177e4SLinus Torvalds 			break;
6501da177e4SLinus Torvalds 		mbo++;
6511da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
6521da177e4SLinus Torvalds 			mbo = 0;
6531da177e4SLinus Torvalds 	} while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
6561da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt;	/* This will effectively prevent someone else from
6591da177e4SLinus Torvalds 							   screwing with this cdb. */
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
6621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds #ifdef DEBUG
6651da177e4SLinus Torvalds 	printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
6661da177e4SLinus Torvalds #endif
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo]));	/* This gets trashed for some reason */
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	ccb[mbo].cdblen = SCpnt->cmd_len;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	direction = 0;
6751da177e4SLinus Torvalds 	if (*cmd == READ_10 || *cmd == READ_6)
6761da177e4SLinus Torvalds 		direction = 8;
6771da177e4SLinus Torvalds 	else if (*cmd == WRITE_10 || *cmd == WRITE_6)
6781da177e4SLinus Torvalds 		direction = 16;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
6811da177e4SLinus Torvalds 
682fc3fdfccSBoaz Harrosh 	if (bufflen) {
68351cf2249SJens Axboe 		struct scatterlist *sg;
6841da177e4SLinus Torvalds 		struct chain *cptr;
6851da177e4SLinus Torvalds #ifdef DEBUG
6861da177e4SLinus Torvalds 		unsigned char *ptr;
6871da177e4SLinus Torvalds #endif
688fc3fdfccSBoaz Harrosh 		int i, sg_count = scsi_sg_count(SCpnt);
6891da177e4SLinus Torvalds 		ccb[mbo].op = 2;	/* SCSI Initiator Command  w/scatter-gather */
690fc3fdfccSBoaz Harrosh 		SCpnt->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
691fc3fdfccSBoaz Harrosh 		                                         GFP_KERNEL | GFP_DMA);
6921da177e4SLinus Torvalds 		cptr = (struct chain *) SCpnt->host_scribble;
6931da177e4SLinus Torvalds 		if (cptr == NULL) {
6941da177e4SLinus Torvalds 			/* free the claimed mailbox slot */
6951da177e4SLinus Torvalds 			HOSTDATA(SCpnt->device->host)->SCint[mbo] = NULL;
6961da177e4SLinus Torvalds 			return SCSI_MLQUEUE_HOST_BUSY;
6971da177e4SLinus Torvalds 		}
698fc3fdfccSBoaz Harrosh 		scsi_for_each_sg(SCpnt, sg, sg_count, i) {
69951cf2249SJens Axboe 			any2scsi(cptr[i].dataptr, SCSI_SG_PA(sg));
70051cf2249SJens Axboe 			if (SCSI_SG_PA(sg) + sg->length - 1 > ISA_DMA_THRESHOLD)
701fc3fdfccSBoaz Harrosh 				BAD_SG_DMA(SCpnt, scsi_sglist(SCpnt), sg_count, i);
70251cf2249SJens Axboe 			any2scsi(cptr[i].datalen, sg->length);
7031da177e4SLinus Torvalds 		};
704fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
7051da177e4SLinus Torvalds 		any2scsi(ccb[mbo].dataptr, SCSI_BUF_PA(cptr));
7061da177e4SLinus Torvalds #ifdef DEBUG
7071da177e4SLinus Torvalds 		printk("cptr %x: ", cptr);
7081da177e4SLinus Torvalds 		ptr = (unsigned char *) cptr;
7091da177e4SLinus Torvalds 		for (i = 0; i < 18; i++)
7101da177e4SLinus Torvalds 			printk("%02x ", ptr[i]);
7111da177e4SLinus Torvalds #endif
7121da177e4SLinus Torvalds 	} else {
7131da177e4SLinus Torvalds 		ccb[mbo].op = 0;	/* SCSI Initiator Command */
7141da177e4SLinus Torvalds 		SCpnt->host_scribble = NULL;
715fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, 0);
716fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].dataptr, 0);
7171da177e4SLinus Torvalds 	};
7181da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);	/*SCSI Target Id */
7191da177e4SLinus Torvalds 	ccb[mbo].rsalen = 16;
7201da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
7211da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds #ifdef DEBUG
7241da177e4SLinus Torvalds 	{
7251da177e4SLinus Torvalds 		int i;
7261da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_command: sending.. ");
7271da177e4SLinus Torvalds 		for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
7281da177e4SLinus Torvalds 			printk("%02x ", ((unchar *) & ccb[mbo])[i]);
7291da177e4SLinus Torvalds 	};
7301da177e4SLinus Torvalds #endif
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	if (done) {
7331da177e4SLinus Torvalds 		DEB(printk("aha1542_queuecommand: now waiting for interrupt ");
7341da177e4SLinus Torvalds 		    aha1542_stat());
7351da177e4SLinus Torvalds 		SCpnt->scsi_done = done;
7361da177e4SLinus Torvalds 		mb[mbo].status = 1;
7371da177e4SLinus Torvalds 		aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);	/* start scsi command */
7381da177e4SLinus Torvalds 		DEB(aha1542_stat());
7391da177e4SLinus Torvalds 	} else
7401da177e4SLinus Torvalds 		printk("aha1542_queuecommand: done can't be NULL\n");
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	return 0;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds /* Initialize mailboxes */
7461da177e4SLinus Torvalds static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
7471da177e4SLinus Torvalds {
7481da177e4SLinus Torvalds 	int i;
7491da177e4SLinus Torvalds 	struct mailbox *mb;
7501da177e4SLinus Torvalds 	struct ccb *ccb;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	unchar cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	mb = HOSTDATA(shpnt)->mb;
7551da177e4SLinus Torvalds 	ccb = HOSTDATA(shpnt)->ccb;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
7581da177e4SLinus Torvalds 		mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
7591da177e4SLinus Torvalds 		any2scsi(mb[i].ccbptr, SCSI_BUF_PA(&ccb[i]));
7601da177e4SLinus Torvalds 	};
7611da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
7621da177e4SLinus Torvalds 	any2scsi((cmd + 2), SCSI_BUF_PA(mb));
7631da177e4SLinus Torvalds 	aha1542_out(bse, cmd, 5);
7641da177e4SLinus Torvalds 	WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
7651da177e4SLinus Torvalds 	while (0) {
7661da177e4SLinus Torvalds fail:
7671da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 	aha1542_intr_reset(bse);
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds static int __init aha1542_getconfig(int base_io, unsigned char *irq_level, unsigned char *dma_chan, unsigned char *scsi_id)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	unchar inquiry_cmd[] = {CMD_RETCONF};
7751da177e4SLinus Torvalds 	unchar inquiry_result[3];
7761da177e4SLinus Torvalds 	int i;
7771da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
7781da177e4SLinus Torvalds 	if (i & DF) {
7791da177e4SLinus Torvalds 		i = inb(DATA(base_io));
7801da177e4SLinus Torvalds 	};
7811da177e4SLinus Torvalds 	aha1542_out(base_io, inquiry_cmd, 1);
7821da177e4SLinus Torvalds 	aha1542_in(base_io, inquiry_result, 3);
7831da177e4SLinus Torvalds 	WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
7841da177e4SLinus Torvalds 	while (0) {
7851da177e4SLinus Torvalds fail:
7861da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query board settings\n");
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
7891da177e4SLinus Torvalds 	switch (inquiry_result[0]) {
7901da177e4SLinus Torvalds 	case 0x80:
7911da177e4SLinus Torvalds 		*dma_chan = 7;
7921da177e4SLinus Torvalds 		break;
7931da177e4SLinus Torvalds 	case 0x40:
7941da177e4SLinus Torvalds 		*dma_chan = 6;
7951da177e4SLinus Torvalds 		break;
7961da177e4SLinus Torvalds 	case 0x20:
7971da177e4SLinus Torvalds 		*dma_chan = 5;
7981da177e4SLinus Torvalds 		break;
7991da177e4SLinus Torvalds 	case 0x01:
8001da177e4SLinus Torvalds 		*dma_chan = 0;
8011da177e4SLinus Torvalds 		break;
8021da177e4SLinus Torvalds 	case 0:
8031da177e4SLinus Torvalds 		/* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
8041da177e4SLinus Torvalds 		   Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
8051da177e4SLinus Torvalds 		*dma_chan = 0xFF;
8061da177e4SLinus Torvalds 		break;
8071da177e4SLinus Torvalds 	default:
8081da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec DMA priority.  Disabling board\n");
8091da177e4SLinus Torvalds 		return -1;
8101da177e4SLinus Torvalds 	};
8111da177e4SLinus Torvalds 	switch (inquiry_result[1]) {
8121da177e4SLinus Torvalds 	case 0x40:
8131da177e4SLinus Torvalds 		*irq_level = 15;
8141da177e4SLinus Torvalds 		break;
8151da177e4SLinus Torvalds 	case 0x20:
8161da177e4SLinus Torvalds 		*irq_level = 14;
8171da177e4SLinus Torvalds 		break;
8181da177e4SLinus Torvalds 	case 0x8:
8191da177e4SLinus Torvalds 		*irq_level = 12;
8201da177e4SLinus Torvalds 		break;
8211da177e4SLinus Torvalds 	case 0x4:
8221da177e4SLinus Torvalds 		*irq_level = 11;
8231da177e4SLinus Torvalds 		break;
8241da177e4SLinus Torvalds 	case 0x2:
8251da177e4SLinus Torvalds 		*irq_level = 10;
8261da177e4SLinus Torvalds 		break;
8271da177e4SLinus Torvalds 	case 0x1:
8281da177e4SLinus Torvalds 		*irq_level = 9;
8291da177e4SLinus Torvalds 		break;
8301da177e4SLinus Torvalds 	default:
8311da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec IRQ level.  Disabling board\n");
8321da177e4SLinus Torvalds 		return -1;
8331da177e4SLinus Torvalds 	};
8341da177e4SLinus Torvalds 	*scsi_id = inquiry_result[2] & 7;
8351da177e4SLinus Torvalds 	return 0;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds /* This function should only be called for 1542C boards - we can detect
8391da177e4SLinus Torvalds    the special firmware settings and unlock the board */
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds static int __init aha1542_mbenable(int base)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	static unchar mbenable_cmd[3];
8441da177e4SLinus Torvalds 	static unchar mbenable_result[2];
8451da177e4SLinus Torvalds 	int retval;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	retval = BIOS_TRANSLATION_6432;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	mbenable_cmd[0] = CMD_EXTBIOS;
8501da177e4SLinus Torvalds 	aha1542_out(base, mbenable_cmd, 1);
8511da177e4SLinus Torvalds 	if (aha1542_in1(base, mbenable_result, 2))
8521da177e4SLinus Torvalds 		return retval;
8531da177e4SLinus Torvalds 	WAITd(INTRFLAGS(base), INTRMASK, HACC, 0, 100);
8541da177e4SLinus Torvalds 	aha1542_intr_reset(base);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
8571da177e4SLinus Torvalds 		mbenable_cmd[0] = CMD_MBENABLE;
8581da177e4SLinus Torvalds 		mbenable_cmd[1] = 0;
8591da177e4SLinus Torvalds 		mbenable_cmd[2] = mbenable_result[1];
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
8621da177e4SLinus Torvalds 			retval = BIOS_TRANSLATION_25563;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 		aha1542_out(base, mbenable_cmd, 3);
8651da177e4SLinus Torvalds 		WAIT(INTRFLAGS(base), INTRMASK, HACC, 0);
8661da177e4SLinus Torvalds 	};
8671da177e4SLinus Torvalds 	while (0) {
8681da177e4SLinus Torvalds fail:
8691da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds 	aha1542_intr_reset(base);
8721da177e4SLinus Torvalds 	return retval;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
8761da177e4SLinus Torvalds static int __init aha1542_query(int base_io, int *transl)
8771da177e4SLinus Torvalds {
8781da177e4SLinus Torvalds 	unchar inquiry_cmd[] = {CMD_INQUIRY};
8791da177e4SLinus Torvalds 	unchar inquiry_result[4];
8801da177e4SLinus Torvalds 	int i;
8811da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
8821da177e4SLinus Torvalds 	if (i & DF) {
8831da177e4SLinus Torvalds 		i = inb(DATA(base_io));
8841da177e4SLinus Torvalds 	};
8851da177e4SLinus Torvalds 	aha1542_out(base_io, inquiry_cmd, 1);
8861da177e4SLinus Torvalds 	aha1542_in(base_io, inquiry_result, 4);
8871da177e4SLinus Torvalds 	WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
8881da177e4SLinus Torvalds 	while (0) {
8891da177e4SLinus Torvalds fail:
8901da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query card type\n");
8911da177e4SLinus Torvalds 	}
8921da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	*transl = BIOS_TRANSLATION_6432;	/* Default case */
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	/* For an AHA1740 series board, we ignore the board since there is a
8971da177e4SLinus Torvalds 	   hardware bug which can lead to wrong blocks being returned if the board
8981da177e4SLinus Torvalds 	   is operating in the 1542 emulation mode.  Since there is an extended mode
8991da177e4SLinus Torvalds 	   driver, we simply ignore the board and let the 1740 driver pick it up.
9001da177e4SLinus Torvalds 	 */
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	if (inquiry_result[0] == 0x43) {
9031da177e4SLinus Torvalds 		printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
9041da177e4SLinus Torvalds 		return 1;
9051da177e4SLinus Torvalds 	};
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/* Always call this - boards that do not support extended bios translation
9081da177e4SLinus Torvalds 	   will ignore the command, and we will set the proper default */
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	*transl = aha1542_mbenable(base_io);
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	return 0;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds #ifndef MODULE
9161da177e4SLinus Torvalds static char *setup_str[MAXBOARDS] __initdata;
9171da177e4SLinus Torvalds static int setup_idx = 0;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds static void __init aha1542_setup(char *str, int *ints)
9201da177e4SLinus Torvalds {
9211da177e4SLinus Torvalds 	const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
9221da177e4SLinus Torvalds 	int setup_portbase;
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	if (setup_idx >= MAXBOARDS) {
9251da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
9261da177e4SLinus Torvalds 		printk(KERN_ERR "   Entryline 1: %s\n", setup_str[0]);
9271da177e4SLinus Torvalds 		printk(KERN_ERR "   Entryline 2: %s\n", setup_str[1]);
9281da177e4SLinus Torvalds 		printk(KERN_ERR "   This line:   %s\n", str);
9291da177e4SLinus Torvalds 		return;
9301da177e4SLinus Torvalds 	}
9311da177e4SLinus Torvalds 	if (ints[0] < 1 || ints[0] > 4) {
9321da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: %s\n", str);
9331da177e4SLinus Torvalds 		printk(ahausage);
9341da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 	setup_called[setup_idx] = ints[0];
9371da177e4SLinus Torvalds 	setup_str[setup_idx] = str;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	setup_portbase = ints[0] >= 1 ? ints[1] : 0;	/* Preserve the default value.. */
9401da177e4SLinus Torvalds 	setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
9411da177e4SLinus Torvalds 	setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
9421da177e4SLinus Torvalds 	if (ints[0] >= 4)
9431da177e4SLinus Torvalds 	{
9441da177e4SLinus Torvalds 		int atbt = -1;
9451da177e4SLinus Torvalds 		switch (ints[4]) {
9461da177e4SLinus Torvalds 		case 5:
9471da177e4SLinus Torvalds 			atbt = 0x00;
9481da177e4SLinus Torvalds 			break;
9491da177e4SLinus Torvalds 		case 6:
9501da177e4SLinus Torvalds 			atbt = 0x04;
9511da177e4SLinus Torvalds 			break;
9521da177e4SLinus Torvalds 		case 7:
9531da177e4SLinus Torvalds 			atbt = 0x01;
9541da177e4SLinus Torvalds 			break;
9551da177e4SLinus Torvalds 		case 8:
9561da177e4SLinus Torvalds 			atbt = 0x02;
9571da177e4SLinus Torvalds 			break;
9581da177e4SLinus Torvalds 		case 10:
9591da177e4SLinus Torvalds 			atbt = 0x03;
9601da177e4SLinus Torvalds 			break;
9611da177e4SLinus Torvalds 		default:
9621da177e4SLinus Torvalds 			printk(KERN_ERR "aha1542: %s\n", str);
9631da177e4SLinus Torvalds 			printk(ahausage);
9641da177e4SLinus Torvalds 			printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s.  Using jumper defaults.\n");
9651da177e4SLinus Torvalds 			break;
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 		setup_dmaspeed[setup_idx] = atbt;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 	if (setup_portbase != 0)
9701da177e4SLinus Torvalds 		bases[setup_idx] = setup_portbase;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	++setup_idx;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds static int __init do_setup(char *str)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds 	int ints[5];
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	int count=setup_idx;
9801da177e4SLinus Torvalds 
9816391a113STobias Klauser 	get_options(str, ARRAY_SIZE(ints), ints);
9821da177e4SLinus Torvalds 	aha1542_setup(str,ints);
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	return count<setup_idx;
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds __setup("aha1542=",do_setup);
9881da177e4SLinus Torvalds #endif
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds /* return non-zero on detection */
991d0be4a7dSChristoph Hellwig static int __init aha1542_detect(struct scsi_host_template * tpnt)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	unsigned char dma_chan;
9941da177e4SLinus Torvalds 	unsigned char irq_level;
9951da177e4SLinus Torvalds 	unsigned char scsi_id;
9961da177e4SLinus Torvalds 	unsigned long flags;
9971da177e4SLinus Torvalds 	unsigned int base_io;
9981da177e4SLinus Torvalds 	int trans;
9991da177e4SLinus Torvalds 	struct Scsi_Host *shpnt = NULL;
10001da177e4SLinus Torvalds 	int count = 0;
10011da177e4SLinus Torvalds 	int indx;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	DEB(printk("aha1542_detect: \n"));
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	tpnt->proc_name = "aha1542";
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds #ifdef MODULE
10081da177e4SLinus Torvalds 	bases[0] = aha1542[0];
10091da177e4SLinus Torvalds 	setup_buson[0] = aha1542[1];
10101da177e4SLinus Torvalds 	setup_busoff[0] = aha1542[2];
10111da177e4SLinus Torvalds 	{
10121da177e4SLinus Torvalds 		int atbt = -1;
10131da177e4SLinus Torvalds 		switch (aha1542[3]) {
10141da177e4SLinus Torvalds 		case 5:
10151da177e4SLinus Torvalds 			atbt = 0x00;
10161da177e4SLinus Torvalds 			break;
10171da177e4SLinus Torvalds 		case 6:
10181da177e4SLinus Torvalds 			atbt = 0x04;
10191da177e4SLinus Torvalds 			break;
10201da177e4SLinus Torvalds 		case 7:
10211da177e4SLinus Torvalds 			atbt = 0x01;
10221da177e4SLinus Torvalds 			break;
10231da177e4SLinus Torvalds 		case 8:
10241da177e4SLinus Torvalds 			atbt = 0x02;
10251da177e4SLinus Torvalds 			break;
10261da177e4SLinus Torvalds 		case 10:
10271da177e4SLinus Torvalds 			atbt = 0x03;
10281da177e4SLinus Torvalds 			break;
10291da177e4SLinus Torvalds 		};
10301da177e4SLinus Torvalds 		setup_dmaspeed[0] = atbt;
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds #endif
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	/*
10351da177e4SLinus Torvalds 	 *	Find MicroChannel cards (AHA1640)
10361da177e4SLinus Torvalds 	 */
10371da177e4SLinus Torvalds #ifdef CONFIG_MCA_LEGACY
10381da177e4SLinus Torvalds 	if(MCA_bus) {
10391da177e4SLinus Torvalds 		int slot = 0;
10401da177e4SLinus Torvalds 		int pos = 0;
10411da177e4SLinus Torvalds 
10426391a113STobias Klauser 		for (indx = 0; (slot != MCA_NOTFOUND) && (indx < ARRAY_SIZE(bases)); indx++) {
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 			if (bases[indx])
10451da177e4SLinus Torvalds 				continue;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 			/* Detect only AHA-1640 cards -- MCA ID 0F1F */
10481da177e4SLinus Torvalds 			slot = mca_find_unused_adapter(0x0f1f, slot);
10491da177e4SLinus Torvalds 			if (slot == MCA_NOTFOUND)
10501da177e4SLinus Torvalds 				break;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 			/* Found one */
10531da177e4SLinus Torvalds 			pos = mca_read_stored_pos(slot, 3);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 			/* Decode address */
10561da177e4SLinus Torvalds 			if (pos & 0x80) {
10571da177e4SLinus Torvalds 				if (pos & 0x02) {
10581da177e4SLinus Torvalds 					if (pos & 0x01)
10591da177e4SLinus Torvalds 						bases[indx] = 0x334;
10601da177e4SLinus Torvalds 					else
10611da177e4SLinus Torvalds 						bases[indx] = 0x234;
10621da177e4SLinus Torvalds 				} else {
10631da177e4SLinus Torvalds 					if (pos & 0x01)
10641da177e4SLinus Torvalds 						bases[indx] = 0x134;
10651da177e4SLinus Torvalds 				}
10661da177e4SLinus Torvalds 			} else {
10671da177e4SLinus Torvalds 				if (pos & 0x02) {
10681da177e4SLinus Torvalds 					if (pos & 0x01)
10691da177e4SLinus Torvalds 						bases[indx] = 0x330;
10701da177e4SLinus Torvalds 					else
10711da177e4SLinus Torvalds 						bases[indx] = 0x230;
10721da177e4SLinus Torvalds 				} else {
10731da177e4SLinus Torvalds 					if (pos & 0x01)
10741da177e4SLinus Torvalds 						bases[indx] = 0x130;
10751da177e4SLinus Torvalds 				}
10761da177e4SLinus Torvalds 			}
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 			/* No need to decode IRQ and Arb level -- those are
10791da177e4SLinus Torvalds 			 * read off the card later.
10801da177e4SLinus Torvalds 			 */
10811da177e4SLinus Torvalds 			printk(KERN_INFO "Found an AHA-1640 in MCA slot %d, I/O 0x%04x\n", slot, bases[indx]);
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 			mca_set_adapter_name(slot, "Adapter AHA-1640");
10841da177e4SLinus Torvalds 			mca_set_adapter_procfn(slot, NULL, NULL);
10851da177e4SLinus Torvalds 			mca_mark_as_used(slot);
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 			/* Go on */
10881da177e4SLinus Torvalds 			slot++;
10891da177e4SLinus Torvalds 		}
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	}
10921da177e4SLinus Torvalds #endif
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	/*
10951da177e4SLinus Torvalds 	 *	Hunt for ISA Plug'n'Pray Adaptecs (AHA1535)
10961da177e4SLinus Torvalds 	 */
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	if(isapnp)
10991da177e4SLinus Torvalds 	{
11001da177e4SLinus Torvalds 		struct pnp_dev *pdev = NULL;
11016391a113STobias Klauser 		for(indx = 0; indx < ARRAY_SIZE(bases); indx++) {
11021da177e4SLinus Torvalds 			if(bases[indx])
11031da177e4SLinus Torvalds 				continue;
11041da177e4SLinus Torvalds 			pdev = pnp_find_dev(NULL, ISAPNP_VENDOR('A', 'D', 'P'),
11051da177e4SLinus Torvalds 				ISAPNP_FUNCTION(0x1542), pdev);
11061da177e4SLinus Torvalds 			if(pdev==NULL)
11071da177e4SLinus Torvalds 				break;
11081da177e4SLinus Torvalds 			/*
11091da177e4SLinus Torvalds 			 *	Activate the PnP card
11101da177e4SLinus Torvalds 			 */
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 			if(pnp_device_attach(pdev)<0)
11131da177e4SLinus Torvalds 				continue;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 			if(pnp_activate_dev(pdev)<0) {
11161da177e4SLinus Torvalds 				pnp_device_detach(pdev);
11171da177e4SLinus Torvalds 				continue;
11181da177e4SLinus Torvalds 			}
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 			if(!pnp_port_valid(pdev, 0)) {
11211da177e4SLinus Torvalds 				pnp_device_detach(pdev);
11221da177e4SLinus Torvalds 				continue;
11231da177e4SLinus Torvalds 			}
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 			bases[indx] = pnp_port_start(pdev, 0);
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 			/* The card can be queried for its DMA, we have
11281da177e4SLinus Torvalds 			   the DMA set up that is enough */
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 			printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
11311da177e4SLinus Torvalds 		}
11321da177e4SLinus Torvalds 	}
11336391a113STobias Klauser 	for (indx = 0; indx < ARRAY_SIZE(bases); indx++)
11341da177e4SLinus Torvalds 		if (bases[indx] != 0 && request_region(bases[indx], 4, "aha1542")) {
11351da177e4SLinus Torvalds 			shpnt = scsi_register(tpnt,
11361da177e4SLinus Torvalds 					sizeof(struct aha1542_hostdata));
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 			if(shpnt==NULL) {
11391da177e4SLinus Torvalds 				release_region(bases[indx], 4);
11401da177e4SLinus Torvalds 				continue;
11411da177e4SLinus Torvalds 			}
11421da177e4SLinus Torvalds 			/* For now we do this - until kmalloc is more intelligent
11431da177e4SLinus Torvalds 			   we are resigned to stupid hacks like this */
11441da177e4SLinus Torvalds 			if (SCSI_BUF_PA(shpnt) >= ISA_DMA_THRESHOLD) {
11451da177e4SLinus Torvalds 				printk(KERN_ERR "Invalid address for shpnt with 1542.\n");
11461da177e4SLinus Torvalds 				goto unregister;
11471da177e4SLinus Torvalds 			}
11481da177e4SLinus Torvalds 			if (!aha1542_test_port(bases[indx], shpnt))
11491da177e4SLinus Torvalds 				goto unregister;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 			base_io = bases[indx];
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 			/* Set the Bus on/off-times as not to ruin floppy performance */
11551da177e4SLinus Torvalds 			{
11561da177e4SLinus Torvalds 				unchar oncmd[] = {CMD_BUSON_TIME, 7};
11571da177e4SLinus Torvalds 				unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 				if (setup_called[indx]) {
11601da177e4SLinus Torvalds 					oncmd[1] = setup_buson[indx];
11611da177e4SLinus Torvalds 					offcmd[1] = setup_busoff[indx];
11621da177e4SLinus Torvalds 				}
11631da177e4SLinus Torvalds 				aha1542_intr_reset(base_io);
11641da177e4SLinus Torvalds 				aha1542_out(base_io, oncmd, 2);
11651da177e4SLinus Torvalds 				WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
11661da177e4SLinus Torvalds 				aha1542_intr_reset(base_io);
11671da177e4SLinus Torvalds 				aha1542_out(base_io, offcmd, 2);
11681da177e4SLinus Torvalds 				WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
11691da177e4SLinus Torvalds 				if (setup_dmaspeed[indx] >= 0) {
11701da177e4SLinus Torvalds 					unchar dmacmd[] = {CMD_DMASPEED, 0};
11711da177e4SLinus Torvalds 					dmacmd[1] = setup_dmaspeed[indx];
11721da177e4SLinus Torvalds 					aha1542_intr_reset(base_io);
11731da177e4SLinus Torvalds 					aha1542_out(base_io, dmacmd, 2);
11741da177e4SLinus Torvalds 					WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
11751da177e4SLinus Torvalds 				}
11761da177e4SLinus Torvalds 				while (0) {
11771da177e4SLinus Torvalds fail:
11781da177e4SLinus Torvalds 					printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
11791da177e4SLinus Torvalds 				}
11801da177e4SLinus Torvalds 				aha1542_intr_reset(base_io);
11811da177e4SLinus Torvalds 			}
11821da177e4SLinus Torvalds 			if (aha1542_query(base_io, &trans))
11831da177e4SLinus Torvalds 				goto unregister;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 			if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
11861da177e4SLinus Torvalds 				goto unregister;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 			printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
11891da177e4SLinus Torvalds 			if (dma_chan != 0xFF)
11901da177e4SLinus Torvalds 				printk(", DMA priority %d", dma_chan);
11911da177e4SLinus Torvalds 			printk("\n");
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 			DEB(aha1542_stat());
11941da177e4SLinus Torvalds 			setup_mailboxes(base_io, shpnt);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 			DEB(aha1542_stat());
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 			DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
11991da177e4SLinus Torvalds 			spin_lock_irqsave(&aha1542_lock, flags);
12001da177e4SLinus Torvalds 			if (request_irq(irq_level, do_aha1542_intr_handle, 0, "aha1542", NULL)) {
12011da177e4SLinus Torvalds 				printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
12021da177e4SLinus Torvalds 				spin_unlock_irqrestore(&aha1542_lock, flags);
12031da177e4SLinus Torvalds 				goto unregister;
12041da177e4SLinus Torvalds 			}
12051da177e4SLinus Torvalds 			if (dma_chan != 0xFF) {
12061da177e4SLinus Torvalds 				if (request_dma(dma_chan, "aha1542")) {
12071da177e4SLinus Torvalds 					printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
12081da177e4SLinus Torvalds 					free_irq(irq_level, NULL);
12091da177e4SLinus Torvalds 					spin_unlock_irqrestore(&aha1542_lock, flags);
12101da177e4SLinus Torvalds 					goto unregister;
12111da177e4SLinus Torvalds 				}
12121da177e4SLinus Torvalds 				if (dma_chan == 0 || dma_chan >= 5) {
12131da177e4SLinus Torvalds 					set_dma_mode(dma_chan, DMA_MODE_CASCADE);
12141da177e4SLinus Torvalds 					enable_dma(dma_chan);
12151da177e4SLinus Torvalds 				}
12161da177e4SLinus Torvalds 			}
12171da177e4SLinus Torvalds 			aha_host[irq_level - 9] = shpnt;
12181da177e4SLinus Torvalds 			shpnt->this_id = scsi_id;
12191da177e4SLinus Torvalds 			shpnt->unique_id = base_io;
12201da177e4SLinus Torvalds 			shpnt->io_port = base_io;
12211da177e4SLinus Torvalds 			shpnt->n_io_port = 4;	/* Number of bytes of I/O space used */
12221da177e4SLinus Torvalds 			shpnt->dma_channel = dma_chan;
12231da177e4SLinus Torvalds 			shpnt->irq = irq_level;
12241da177e4SLinus Torvalds 			HOSTDATA(shpnt)->bios_translation = trans;
12251da177e4SLinus Torvalds 			if (trans == BIOS_TRANSLATION_25563)
12261da177e4SLinus Torvalds 				printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
12271da177e4SLinus Torvalds 			HOSTDATA(shpnt)->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
12281da177e4SLinus Torvalds 			HOSTDATA(shpnt)->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
12291da177e4SLinus Torvalds 			memset(HOSTDATA(shpnt)->SCint, 0, sizeof(HOSTDATA(shpnt)->SCint));
12301da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
12311da177e4SLinus Torvalds #if 0
12321da177e4SLinus Torvalds 			DEB(printk(" *** READ CAPACITY ***\n"));
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 			{
12351da177e4SLinus Torvalds 				unchar buf[8];
12361da177e4SLinus Torvalds 				static unchar cmd[] = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
12371da177e4SLinus Torvalds 				int i;
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 				for (i = 0; i < sizeof(buf); ++i)
12401da177e4SLinus Torvalds 					buf[i] = 0x87;
12411da177e4SLinus Torvalds 				for (i = 0; i < 2; ++i)
12421da177e4SLinus Torvalds 					if (!aha1542_command(i, cmd, buf, sizeof(buf))) {
12431da177e4SLinus Torvalds 						printk(KERN_DEBUG "aha_detect: LU %d sector_size %d device_size %d\n",
12441da177e4SLinus Torvalds 						       i, xscsi2int(buf + 4), xscsi2int(buf));
12451da177e4SLinus Torvalds 					}
12461da177e4SLinus Torvalds 			}
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 			DEB(printk(" *** NOW RUNNING MY OWN TEST *** \n"));
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 			for (i = 0; i < 4; ++i) {
12511da177e4SLinus Torvalds 				unsigned char cmd[10];
12521da177e4SLinus Torvalds 				static buffer[512];
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 				cmd[0] = READ_10;
12551da177e4SLinus Torvalds 				cmd[1] = 0;
12561da177e4SLinus Torvalds 				xany2scsi(cmd + 2, i);
12571da177e4SLinus Torvalds 				cmd[6] = 0;
12581da177e4SLinus Torvalds 				cmd[7] = 0;
12591da177e4SLinus Torvalds 				cmd[8] = 1;
12601da177e4SLinus Torvalds 				cmd[9] = 0;
12611da177e4SLinus Torvalds 				aha1542_command(0, cmd, buffer, 512);
12621da177e4SLinus Torvalds 			}
12631da177e4SLinus Torvalds #endif
12641da177e4SLinus Torvalds 			count++;
12651da177e4SLinus Torvalds 			continue;
12661da177e4SLinus Torvalds unregister:
12671da177e4SLinus Torvalds 			release_region(bases[indx], 4);
12681da177e4SLinus Torvalds 			scsi_unregister(shpnt);
12691da177e4SLinus Torvalds 			continue;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 		};
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	return count;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds static int aha1542_release(struct Scsi_Host *shost)
12771da177e4SLinus Torvalds {
12781da177e4SLinus Torvalds 	if (shost->irq)
12791da177e4SLinus Torvalds 		free_irq(shost->irq, NULL);
12801da177e4SLinus Torvalds 	if (shost->dma_channel != 0xff)
12811da177e4SLinus Torvalds 		free_dma(shost->dma_channel);
12821da177e4SLinus Torvalds 	if (shost->io_port && shost->n_io_port)
12831da177e4SLinus Torvalds 		release_region(shost->io_port, shost->n_io_port);
12841da177e4SLinus Torvalds 	scsi_unregister(shost);
12851da177e4SLinus Torvalds 	return 0;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds static int aha1542_restart(struct Scsi_Host *shost)
12891da177e4SLinus Torvalds {
12901da177e4SLinus Torvalds 	int i;
12911da177e4SLinus Torvalds 	int count = 0;
12921da177e4SLinus Torvalds #if 0
12931da177e4SLinus Torvalds 	unchar ahacmd = CMD_START_SCSI;
12941da177e4SLinus Torvalds #endif
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++)
12971da177e4SLinus Torvalds 		if (HOSTDATA(shost)->SCint[i] &&
12981da177e4SLinus Torvalds 		    !(HOSTDATA(shost)->SCint[i]->device->soft_reset)) {
12991da177e4SLinus Torvalds #if 0
13001da177e4SLinus Torvalds 			HOSTDATA(shost)->mb[i].status = 1;	/* Indicate ready to restart... */
13011da177e4SLinus Torvalds #endif
13021da177e4SLinus Torvalds 			count++;
13031da177e4SLinus Torvalds 		}
13041da177e4SLinus Torvalds 	printk(KERN_DEBUG "Potential to restart %d stalled commands...\n", count);
13051da177e4SLinus Torvalds #if 0
13061da177e4SLinus Torvalds 	/* start scsi command */
13071da177e4SLinus Torvalds 	if (count)
13081da177e4SLinus Torvalds 		aha1542_out(shost->io_port, &ahacmd, 1);
13091da177e4SLinus Torvalds #endif
13101da177e4SLinus Torvalds 	return 0;
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds /*
13141da177e4SLinus Torvalds  * This is a device reset.  This is handled by sending a special command
13151da177e4SLinus Torvalds  * to the device.
13161da177e4SLinus Torvalds  */
13171da177e4SLinus Torvalds static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	unsigned long flags;
13201da177e4SLinus Torvalds 	struct mailbox *mb;
13211da177e4SLinus Torvalds 	unchar target = SCpnt->device->id;
13221da177e4SLinus Torvalds 	unchar lun = SCpnt->device->lun;
13231da177e4SLinus Torvalds 	int mbo;
13241da177e4SLinus Torvalds 	struct ccb *ccb;
13251da177e4SLinus Torvalds 	unchar ahacmd = CMD_START_SCSI;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	ccb = HOSTDATA(SCpnt->device->host)->ccb;
13281da177e4SLinus Torvalds 	mb = HOSTDATA(SCpnt->device->host)->mb;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
13311da177e4SLinus Torvalds 	mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
13321da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
13331da177e4SLinus Torvalds 		mbo = 0;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	do {
13361da177e4SLinus Torvalds 		if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
13371da177e4SLinus Torvalds 			break;
13381da177e4SLinus Torvalds 		mbo++;
13391da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
13401da177e4SLinus Torvalds 			mbo = 0;
13411da177e4SLinus Torvalds 	} while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
13441da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt;	/* This will effectively
13471da177e4SLinus Torvalds 							   prevent someone else from
13481da177e4SLinus Torvalds 							   screwing with this cdb. */
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
13511da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo]));	/* This gets trashed for some reason */
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	ccb[mbo].op = 0x81;	/* BUS DEVICE RESET */
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);		/*SCSI Target Id */
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
13621da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	/*
13651da177e4SLinus Torvalds 	 * Now tell the 1542 to flush all pending commands for this
13661da177e4SLinus Torvalds 	 * target
13671da177e4SLinus Torvalds 	 */
13681da177e4SLinus Torvalds 	aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);
13691da177e4SLinus Torvalds 
1370017560fcSJeff Garzik 	scmd_printk(KERN_WARNING, SCpnt,
1371017560fcSJeff Garzik 		"Trying device reset for target\n");
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	return SUCCESS;
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds #ifdef ERIC_neverdef
13771da177e4SLinus Torvalds 	/*
13781da177e4SLinus Torvalds 	 * With the 1542 we apparently never get an interrupt to
13791da177e4SLinus Torvalds 	 * acknowledge a device reset being sent.  Then again, Leonard
13801da177e4SLinus Torvalds 	 * says we are doing this wrong in the first place...
13811da177e4SLinus Torvalds 	 *
13821da177e4SLinus Torvalds 	 * Take a wait and see attitude.  If we get spurious interrupts,
13831da177e4SLinus Torvalds 	 * then the device reset is doing something sane and useful, and
13841da177e4SLinus Torvalds 	 * we will wait for the interrupt to post completion.
13851da177e4SLinus Torvalds 	 */
13861da177e4SLinus Torvalds 	printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	/*
13891da177e4SLinus Torvalds 	 * Free the command block for all commands running on this
13901da177e4SLinus Torvalds 	 * target...
13911da177e4SLinus Torvalds 	 */
13921da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
13931da177e4SLinus Torvalds 		if (HOSTDATA(SCpnt->host)->SCint[i] &&
13941da177e4SLinus Torvalds 		    HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
13951da177e4SLinus Torvalds 			Scsi_Cmnd *SCtmp;
13961da177e4SLinus Torvalds 			SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
13971da177e4SLinus Torvalds 			kfree(SCtmp->host_scribble);
13981da177e4SLinus Torvalds 			SCtmp->host_scribble = NULL;
13991da177e4SLinus Torvalds 			HOSTDATA(SCpnt->host)->SCint[i] = NULL;
14001da177e4SLinus Torvalds 			HOSTDATA(SCpnt->host)->mb[i].status = 0;
14011da177e4SLinus Torvalds 		}
14021da177e4SLinus Torvalds 	}
14031da177e4SLinus Torvalds 	return SUCCESS;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	return FAILED;
14061da177e4SLinus Torvalds #endif				/* ERIC_neverdef */
14071da177e4SLinus Torvalds }
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds static int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
14101da177e4SLinus Torvalds {
14111da177e4SLinus Torvalds 	int i;
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 	/*
14141da177e4SLinus Torvalds 	 * This does a scsi reset for all devices on the bus.
14151da177e4SLinus Torvalds 	 * In principle, we could also reset the 1542 - should
14161da177e4SLinus Torvalds 	 * we do this?  Try this first, and we can add that later
14171da177e4SLinus Torvalds 	 * if it turns out to be useful.
14181da177e4SLinus Torvalds 	 */
14191da177e4SLinus Torvalds 	outb(SCRST, CONTROL(SCpnt->device->host->io_port));
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	/*
14221da177e4SLinus Torvalds 	 * Wait for the thing to settle down a bit.  Unfortunately
14231da177e4SLinus Torvalds 	 * this is going to basically lock up the machine while we
14241da177e4SLinus Torvalds 	 * wait for this to complete.  To be 100% correct, we need to
14251da177e4SLinus Torvalds 	 * check for timeout, and if we are doing something like this
14261da177e4SLinus Torvalds 	 * we are pretty desperate anyways.
14271da177e4SLinus Torvalds 	 */
14281da177e4SLinus Torvalds 	ssleep(4);
142968b3aa7cSJeff Garzik  
14301da177e4SLinus Torvalds 	spin_lock_irq(SCpnt->device->host->host_lock);
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	WAIT(STATUS(SCpnt->device->host->io_port),
14331da177e4SLinus Torvalds 	     STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	/*
14361da177e4SLinus Torvalds 	 * Now try to pick up the pieces.  For all pending commands,
14371da177e4SLinus Torvalds 	 * free any internal data structures, and basically clear things
14381da177e4SLinus Torvalds 	 * out.  We do not try and restart any commands or anything -
14391da177e4SLinus Torvalds 	 * the strategy handler takes care of that crap.
14401da177e4SLinus Torvalds 	 */
14411da177e4SLinus Torvalds 	printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
14441da177e4SLinus Torvalds 		if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
14451da177e4SLinus Torvalds 			Scsi_Cmnd *SCtmp;
14461da177e4SLinus Torvalds 			SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 			if (SCtmp->device->soft_reset) {
14501da177e4SLinus Torvalds 				/*
14511da177e4SLinus Torvalds 				 * If this device implements the soft reset option,
14521da177e4SLinus Torvalds 				 * then it is still holding onto the command, and
14531da177e4SLinus Torvalds 				 * may yet complete it.  In this case, we don't
14541da177e4SLinus Torvalds 				 * flush the data.
14551da177e4SLinus Torvalds 				 */
14561da177e4SLinus Torvalds 				continue;
14571da177e4SLinus Torvalds 			}
14581da177e4SLinus Torvalds 			kfree(SCtmp->host_scribble);
14591da177e4SLinus Torvalds 			SCtmp->host_scribble = NULL;
14601da177e4SLinus Torvalds 			HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
14611da177e4SLinus Torvalds 			HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
14621da177e4SLinus Torvalds 		}
14631da177e4SLinus Torvalds 	}
14641da177e4SLinus Torvalds 
146568b3aa7cSJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
14661da177e4SLinus Torvalds 	return SUCCESS;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds fail:
146968b3aa7cSJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
14701da177e4SLinus Torvalds 	return FAILED;
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds static int aha1542_host_reset(Scsi_Cmnd * SCpnt)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	int i;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	/*
14781da177e4SLinus Torvalds 	 * This does a scsi reset for all devices on the bus.
14791da177e4SLinus Torvalds 	 * In principle, we could also reset the 1542 - should
14801da177e4SLinus Torvalds 	 * we do this?  Try this first, and we can add that later
14811da177e4SLinus Torvalds 	 * if it turns out to be useful.
14821da177e4SLinus Torvalds 	 */
14831da177e4SLinus Torvalds 	outb(HRST | SCRST, CONTROL(SCpnt->device->host->io_port));
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	/*
14861da177e4SLinus Torvalds 	 * Wait for the thing to settle down a bit.  Unfortunately
14871da177e4SLinus Torvalds 	 * this is going to basically lock up the machine while we
14881da177e4SLinus Torvalds 	 * wait for this to complete.  To be 100% correct, we need to
14891da177e4SLinus Torvalds 	 * check for timeout, and if we are doing something like this
14901da177e4SLinus Torvalds 	 * we are pretty desperate anyways.
14911da177e4SLinus Torvalds 	 */
14921da177e4SLinus Torvalds 	ssleep(4);
14931da177e4SLinus Torvalds 	spin_lock_irq(SCpnt->device->host->host_lock);
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	WAIT(STATUS(SCpnt->device->host->io_port),
14961da177e4SLinus Torvalds 	     STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	/*
14991da177e4SLinus Torvalds 	 * We need to do this too before the 1542 can interact with
15001da177e4SLinus Torvalds 	 * us again.
15011da177e4SLinus Torvalds 	 */
15021da177e4SLinus Torvalds 	setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds 	/*
15051da177e4SLinus Torvalds 	 * Now try to pick up the pieces.  For all pending commands,
15061da177e4SLinus Torvalds 	 * free any internal data structures, and basically clear things
15071da177e4SLinus Torvalds 	 * out.  We do not try and restart any commands or anything -
15081da177e4SLinus Torvalds 	 * the strategy handler takes care of that crap.
15091da177e4SLinus Torvalds 	 */
15101da177e4SLinus Torvalds 	printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
15131da177e4SLinus Torvalds 		if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
15141da177e4SLinus Torvalds 			Scsi_Cmnd *SCtmp;
15151da177e4SLinus Torvalds 			SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 			if (SCtmp->device->soft_reset) {
15181da177e4SLinus Torvalds 				/*
15191da177e4SLinus Torvalds 				 * If this device implements the soft reset option,
15201da177e4SLinus Torvalds 				 * then it is still holding onto the command, and
15211da177e4SLinus Torvalds 				 * may yet complete it.  In this case, we don't
15221da177e4SLinus Torvalds 				 * flush the data.
15231da177e4SLinus Torvalds 				 */
15241da177e4SLinus Torvalds 				continue;
15251da177e4SLinus Torvalds 			}
15261da177e4SLinus Torvalds 			kfree(SCtmp->host_scribble);
15271da177e4SLinus Torvalds 			SCtmp->host_scribble = NULL;
15281da177e4SLinus Torvalds 			HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
15291da177e4SLinus Torvalds 			HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
15301da177e4SLinus Torvalds 		}
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 
1533df0ae249SJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
15341da177e4SLinus Torvalds 	return SUCCESS;
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds fail:
1537df0ae249SJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
15381da177e4SLinus Torvalds 	return FAILED;
15391da177e4SLinus Torvalds }
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds #if 0
15421da177e4SLinus Torvalds /*
15431da177e4SLinus Torvalds  * These are the old error handling routines.  They are only temporarily
15441da177e4SLinus Torvalds  * here while we play with the new error handling code.
15451da177e4SLinus Torvalds  */
15461da177e4SLinus Torvalds static int aha1542_old_abort(Scsi_Cmnd * SCpnt)
15471da177e4SLinus Torvalds {
15481da177e4SLinus Torvalds #if 0
15491da177e4SLinus Torvalds 	unchar ahacmd = CMD_START_SCSI;
15501da177e4SLinus Torvalds 	unsigned long flags;
15511da177e4SLinus Torvalds 	struct mailbox *mb;
15521da177e4SLinus Torvalds 	int mbi, mbo, i;
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 	printk(KERN_DEBUG "In aha1542_abort: %x %x\n",
15551da177e4SLinus Torvalds 	       inb(STATUS(SCpnt->host->io_port)),
15561da177e4SLinus Torvalds 	       inb(INTRFLAGS(SCpnt->host->io_port)));
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
15591da177e4SLinus Torvalds 	mb = HOSTDATA(SCpnt->host)->mb;
15601da177e4SLinus Torvalds 	mbi = HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
15611da177e4SLinus Torvalds 	if (mbi >= 2 * AHA1542_MAILBOXES)
15621da177e4SLinus Torvalds 		mbi = AHA1542_MAILBOXES;
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	do {
15651da177e4SLinus Torvalds 		if (mb[mbi].status != 0)
15661da177e4SLinus Torvalds 			break;
15671da177e4SLinus Torvalds 		mbi++;
15681da177e4SLinus Torvalds 		if (mbi >= 2 * AHA1542_MAILBOXES)
15691da177e4SLinus Torvalds 			mbi = AHA1542_MAILBOXES;
15701da177e4SLinus Torvalds 	} while (mbi != HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
15711da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	if (mb[mbi].status) {
15741da177e4SLinus Torvalds 		printk(KERN_ERR "Lost interrupt discovered on irq %d - attempting to recover\n",
15751da177e4SLinus Torvalds 		       SCpnt->host->irq);
15761da177e4SLinus Torvalds 		aha1542_intr_handle(SCpnt->host, NULL);
15771da177e4SLinus Torvalds 		return 0;
15781da177e4SLinus Torvalds 	}
15791da177e4SLinus Torvalds 	/* OK, no lost interrupt.  Try looking to see how many pending commands
15801da177e4SLinus Torvalds 	   we think we have. */
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++)
15831da177e4SLinus Torvalds 		if (HOSTDATA(SCpnt->host)->SCint[i]) {
15841da177e4SLinus Torvalds 			if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
15851da177e4SLinus Torvalds 				printk(KERN_ERR "Timed out command pending for %s\n",
15861da177e4SLinus Torvalds 				       SCpnt->request->rq_disk ?
15871da177e4SLinus Torvalds 				       SCpnt->request->rq_disk->disk_name : "?"
15881da177e4SLinus Torvalds 				       );
15891da177e4SLinus Torvalds 				if (HOSTDATA(SCpnt->host)->mb[i].status) {
15901da177e4SLinus Torvalds 					printk(KERN_ERR "OGMB still full - restarting\n");
15911da177e4SLinus Torvalds 					aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
15921da177e4SLinus Torvalds 				};
15931da177e4SLinus Torvalds 			} else
15941da177e4SLinus Torvalds 				printk(KERN_ERR "Other pending command %s\n",
15951da177e4SLinus Torvalds 				       SCpnt->request->rq_disk ?
15961da177e4SLinus Torvalds 				       SCpnt->request->rq_disk->disk_name : "?"
15971da177e4SLinus Torvalds 				       );
15981da177e4SLinus Torvalds 		}
15991da177e4SLinus Torvalds #endif
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	DEB(printk("aha1542_abort\n"));
16021da177e4SLinus Torvalds #if 0
16031da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
16041da177e4SLinus Torvalds 	for (mbo = 0; mbo < AHA1542_MAILBOXES; mbo++) {
16051da177e4SLinus Torvalds 		if (SCpnt == HOSTDATA(SCpnt->host)->SCint[mbo]) {
16061da177e4SLinus Torvalds 			mb[mbo].status = 2;	/* Abort command */
16071da177e4SLinus Torvalds 			aha1542_out(SCpnt->host->io_port, &ahacmd, 1);	/* start scsi command */
16081da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
16091da177e4SLinus Torvalds 			break;
16101da177e4SLinus Torvalds 		}
16111da177e4SLinus Torvalds 	}
16121da177e4SLinus Torvalds 	if (AHA1542_MAILBOXES == mbo)
16131da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
16141da177e4SLinus Torvalds #endif
16151da177e4SLinus Torvalds 	return SCSI_ABORT_SNOOZE;
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds /* We do not implement a reset function here, but the upper level code
16191da177e4SLinus Torvalds    assumes that it will get some kind of response for the command in
16201da177e4SLinus Torvalds    SCpnt.  We must oblige, or the command will hang the scsi system.
16211da177e4SLinus Torvalds    For a first go, we assume that the 1542 notifies us with all of the
16221da177e4SLinus Torvalds    pending commands (it does implement soft reset, after all). */
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds static int aha1542_old_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
16251da177e4SLinus Torvalds {
16261da177e4SLinus Torvalds 	unchar ahacmd = CMD_START_SCSI;
16271da177e4SLinus Torvalds 	int i;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	/*
16301da177e4SLinus Torvalds 	 * See if a bus reset was suggested.
16311da177e4SLinus Torvalds 	 */
16321da177e4SLinus Torvalds 	if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
16331da177e4SLinus Torvalds 		/*
16341da177e4SLinus Torvalds 		 * This does a scsi reset for all devices on the bus.
16351da177e4SLinus Torvalds 		 * In principle, we could also reset the 1542 - should
16361da177e4SLinus Torvalds 		 * we do this?  Try this first, and we can add that later
16371da177e4SLinus Torvalds 		 * if it turns out to be useful.
16381da177e4SLinus Torvalds 		 */
16391da177e4SLinus Torvalds 		outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 		/*
16421da177e4SLinus Torvalds 		 * Wait for the thing to settle down a bit.  Unfortunately
16431da177e4SLinus Torvalds 		 * this is going to basically lock up the machine while we
16441da177e4SLinus Torvalds 		 * wait for this to complete.  To be 100% correct, we need to
16451da177e4SLinus Torvalds 		 * check for timeout, and if we are doing something like this
16461da177e4SLinus Torvalds 		 * we are pretty desperate anyways.
16471da177e4SLinus Torvalds 		 */
16481da177e4SLinus Torvalds 		WAIT(STATUS(SCpnt->host->io_port),
16491da177e4SLinus Torvalds 		STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 		/*
16521da177e4SLinus Torvalds 		 * We need to do this too before the 1542 can interact with
16531da177e4SLinus Torvalds 		 * us again.
16541da177e4SLinus Torvalds 		 */
16551da177e4SLinus Torvalds 		setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 		/*
16581da177e4SLinus Torvalds 		 * Now try to pick up the pieces.  Restart all commands
16591da177e4SLinus Torvalds 		 * that are currently active on the bus, and reset all of
16601da177e4SLinus Torvalds 		 * the datastructures.  We have some time to kill while
16611da177e4SLinus Torvalds 		 * things settle down, so print a nice message.
16621da177e4SLinus Torvalds 		 */
16631da177e4SLinus Torvalds 		printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 		for (i = 0; i < AHA1542_MAILBOXES; i++)
16661da177e4SLinus Torvalds 			if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
16671da177e4SLinus Torvalds 				Scsi_Cmnd *SCtmp;
16681da177e4SLinus Torvalds 				SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
16691da177e4SLinus Torvalds 				SCtmp->result = DID_RESET << 16;
16701da177e4SLinus Torvalds 				kfree(SCtmp->host_scribble);
16711da177e4SLinus Torvalds 				SCtmp->host_scribble = NULL;
16721da177e4SLinus Torvalds 				printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
16731da177e4SLinus Torvalds 				SCtmp->scsi_done(SCpnt);
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 				HOSTDATA(SCpnt->host)->SCint[i] = NULL;
16761da177e4SLinus Torvalds 				HOSTDATA(SCpnt->host)->mb[i].status = 0;
16771da177e4SLinus Torvalds 			}
16781da177e4SLinus Torvalds 		/*
16791da177e4SLinus Torvalds 		 * Now tell the mid-level code what we did here.  Since
16801da177e4SLinus Torvalds 		 * we have restarted all of the outstanding commands,
16811da177e4SLinus Torvalds 		 * then report SUCCESS.
16821da177e4SLinus Torvalds 		 */
16831da177e4SLinus Torvalds 		return (SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET);
16841da177e4SLinus Torvalds fail:
16851da177e4SLinus Torvalds 		printk(KERN_CRIT "aha1542.c: Unable to perform hard reset.\n");
16861da177e4SLinus Torvalds 		printk(KERN_CRIT "Power cycle machine to reset\n");
16871da177e4SLinus Torvalds 		return (SCSI_RESET_ERROR | SCSI_RESET_BUS_RESET);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 	} else {
16911da177e4SLinus Torvalds 		/* This does a selective reset of just the one device */
16921da177e4SLinus Torvalds 		/* First locate the ccb for this command */
16931da177e4SLinus Torvalds 		for (i = 0; i < AHA1542_MAILBOXES; i++)
16941da177e4SLinus Torvalds 			if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
16951da177e4SLinus Torvalds 				HOSTDATA(SCpnt->host)->ccb[i].op = 0x81;	/* BUS DEVICE RESET */
16961da177e4SLinus Torvalds 				/* Now tell the 1542 to flush all pending commands for this target */
16971da177e4SLinus Torvalds 				aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 				/* Here is the tricky part.  What to do next.  Do we get an interrupt
17001da177e4SLinus Torvalds 				   for the commands that we aborted with the specified target, or
17011da177e4SLinus Torvalds 				   do we generate this on our own?  Try it without first and see
17021da177e4SLinus Torvalds 				   what happens */
17031da177e4SLinus Torvalds 				printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 				/* If the first does not work, then try the second.  I think the
17061da177e4SLinus Torvalds 				   first option is more likely to be correct. Free the command
17071da177e4SLinus Torvalds 				   block for all commands running on this target... */
17081da177e4SLinus Torvalds 				for (i = 0; i < AHA1542_MAILBOXES; i++)
17091da177e4SLinus Torvalds 					if (HOSTDATA(SCpnt->host)->SCint[i] &&
17101da177e4SLinus Torvalds 					    HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
17111da177e4SLinus Torvalds 						Scsi_Cmnd *SCtmp;
17121da177e4SLinus Torvalds 						SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
17131da177e4SLinus Torvalds 						SCtmp->result = DID_RESET << 16;
17141da177e4SLinus Torvalds 						kfree(SCtmp->host_scribble);
17151da177e4SLinus Torvalds 						SCtmp->host_scribble = NULL;
17161da177e4SLinus Torvalds 						printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
17171da177e4SLinus Torvalds 						SCtmp->scsi_done(SCpnt);
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 						HOSTDATA(SCpnt->host)->SCint[i] = NULL;
17201da177e4SLinus Torvalds 						HOSTDATA(SCpnt->host)->mb[i].status = 0;
17211da177e4SLinus Torvalds 					}
17221da177e4SLinus Torvalds 				return SCSI_RESET_SUCCESS;
17231da177e4SLinus Torvalds 			}
17241da177e4SLinus Torvalds 	}
17251da177e4SLinus Torvalds 	/* No active command at this time, so this means that each time we got
17261da177e4SLinus Torvalds 	   some kind of response the last time through.  Tell the mid-level code
17271da177e4SLinus Torvalds 	   to request sense information in order to decide what to do next. */
17281da177e4SLinus Torvalds 	return SCSI_RESET_PUNT;
17291da177e4SLinus Torvalds }
17301da177e4SLinus Torvalds #endif    /* end of big comment block around old_abort + old_reset */
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds static int aha1542_biosparam(struct scsi_device *sdev,
17331da177e4SLinus Torvalds 		struct block_device *bdev, sector_t capacity, int *ip)
17341da177e4SLinus Torvalds {
17351da177e4SLinus Torvalds 	int translation_algorithm;
17361da177e4SLinus Torvalds 	int size = capacity;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	translation_algorithm = HOSTDATA(sdev->host)->bios_translation;
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds 	if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
17411da177e4SLinus Torvalds 		/* Please verify that this is the same as what DOS returns */
17421da177e4SLinus Torvalds 		ip[0] = 255;
17431da177e4SLinus Torvalds 		ip[1] = 63;
17441da177e4SLinus Torvalds 		ip[2] = size / 255 / 63;
17451da177e4SLinus Torvalds 	} else {
17461da177e4SLinus Torvalds 		ip[0] = 64;
17471da177e4SLinus Torvalds 		ip[1] = 32;
17481da177e4SLinus Torvalds 		ip[2] = size >> 11;
17491da177e4SLinus Torvalds 	}
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	return 0;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds MODULE_LICENSE("GPL");
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 
1756d0be4a7dSChristoph Hellwig static struct scsi_host_template driver_template = {
17571da177e4SLinus Torvalds 	.proc_name		= "aha1542",
17581da177e4SLinus Torvalds 	.name			= "Adaptec 1542",
17591da177e4SLinus Torvalds 	.detect			= aha1542_detect,
17601da177e4SLinus Torvalds 	.release		= aha1542_release,
17611da177e4SLinus Torvalds 	.queuecommand		= aha1542_queuecommand,
17621da177e4SLinus Torvalds 	.eh_device_reset_handler= aha1542_dev_reset,
17631da177e4SLinus Torvalds 	.eh_bus_reset_handler	= aha1542_bus_reset,
17641da177e4SLinus Torvalds 	.eh_host_reset_handler	= aha1542_host_reset,
17651da177e4SLinus Torvalds 	.bios_param		= aha1542_biosparam,
17661da177e4SLinus Torvalds 	.can_queue		= AHA1542_MAILBOXES,
17671da177e4SLinus Torvalds 	.this_id		= 7,
17681da177e4SLinus Torvalds 	.sg_tablesize		= AHA1542_SCATTER,
17691da177e4SLinus Torvalds 	.cmd_per_lun		= AHA1542_CMDLUN,
17701da177e4SLinus Torvalds 	.unchecked_isa_dma	= 1,
17711da177e4SLinus Torvalds 	.use_clustering		= ENABLE_CLUSTERING,
17721da177e4SLinus Torvalds };
17731da177e4SLinus Torvalds #include "scsi_module.c"
1774