xref: /linux/drivers/scsi/aha1542.c (revision 17787a09fdfcb07ccc361758737af0dfa1ea4214)
11da177e4SLinus Torvalds /* $Id: aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $
21da177e4SLinus Torvalds  *  linux/kernel/aha1542.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Tommy Thorn
51da177e4SLinus Torvalds  *  Copyright (C) 1993, 1994, 1995 Eric Youngdale
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Modified by Eric Youngdale
81da177e4SLinus Torvalds  *        Use request_irq and request_dma to help prevent unexpected conflicts
91da177e4SLinus Torvalds  *        Set up on-board DMA controller, such that we do not have to
101da177e4SLinus Torvalds  *        have the bios enabled to use the aha1542.
111da177e4SLinus Torvalds  *  Modified by David Gentzel
121da177e4SLinus Torvalds  *        Don't call request_dma if dma mask is 0 (for BusLogic BT-445S VL-Bus
131da177e4SLinus Torvalds  *        controller).
141da177e4SLinus Torvalds  *  Modified by Matti Aarnio
151da177e4SLinus Torvalds  *        Accept parameters from LILO cmd-line. -- 1-Oct-94
161da177e4SLinus Torvalds  *  Modified by Mike McLagan <mike.mclagan@linux.org>
171da177e4SLinus Torvalds  *        Recognise extended mode on AHA1542CP, different bit than 1542CF
181da177e4SLinus Torvalds  *        1-Jan-97
191da177e4SLinus Torvalds  *  Modified by Bjorn L. Thordarson and Einar Thor Einarsson
201da177e4SLinus Torvalds  *        Recognize that DMA0 is valid DMA channel -- 13-Jul-98
211da177e4SLinus Torvalds  *  Modified by Chris Faulhaber <jedgar@fxp.org>
221da177e4SLinus Torvalds  *        Added module command-line options
231da177e4SLinus Torvalds  *        19-Jul-99
24726a6459SJoe Perches  *  Modified by Adam Fritzler
25a88dc06cSPaul Gortmaker  *        Added proper detection of the AHA-1640 (MCA, now deleted)
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <linux/module.h>
291da177e4SLinus Torvalds #include <linux/interrupt.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/types.h>
321da177e4SLinus Torvalds #include <linux/string.h>
331da177e4SLinus Torvalds #include <linux/ioport.h>
341da177e4SLinus Torvalds #include <linux/delay.h>
351da177e4SLinus Torvalds #include <linux/proc_fs.h>
361da177e4SLinus Torvalds #include <linux/init.h>
371da177e4SLinus Torvalds #include <linux/spinlock.h>
38643a7c43SOndrej Zary #include <linux/isa.h>
39643a7c43SOndrej Zary #include <linux/pnp.h>
401da177e4SLinus Torvalds #include <linux/blkdev.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <asm/dma.h>
441da177e4SLinus Torvalds #include <asm/io.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include "scsi.h"
471da177e4SLinus Torvalds #include <scsi/scsi_host.h>
481da177e4SLinus Torvalds #include "aha1542.h"
491da177e4SLinus Torvalds #include <linux/stat.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #ifdef DEBUG
521da177e4SLinus Torvalds #define DEB(x) x
531da177e4SLinus Torvalds #else
541da177e4SLinus Torvalds #define DEB(x)
551da177e4SLinus Torvalds #endif
56f71429abSOndrej Zary #define MAXBOARDS 4
571da177e4SLinus Torvalds 
58f71429abSOndrej Zary static bool isapnp = 1;
591da177e4SLinus Torvalds module_param(isapnp, bool, 0);
60f71429abSOndrej Zary MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
61f71429abSOndrej Zary 
62f71429abSOndrej Zary static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
63f71429abSOndrej Zary module_param_array(io, int, NULL, 0);
64f71429abSOndrej Zary MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
65f71429abSOndrej Zary 
66f71429abSOndrej Zary /* time AHA spends on the AT-bus during data transfer */
67f71429abSOndrej Zary static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
68f71429abSOndrej Zary module_param_array(bus_on, int, NULL, 0);
69f71429abSOndrej Zary MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
70f71429abSOndrej Zary 
71f71429abSOndrej Zary /* time AHA spends off the bus (not to monopolize it) during data transfer  */
72f71429abSOndrej Zary static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
73f71429abSOndrej Zary module_param_array(bus_off, int, NULL, 0);
74f71429abSOndrej Zary MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
75f71429abSOndrej Zary 
76f71429abSOndrej Zary /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
77f71429abSOndrej Zary static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
78f71429abSOndrej Zary module_param_array(dma_speed, int, NULL, 0);
79f71429abSOndrej Zary MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #define BIOS_TRANSLATION_6432 1	/* Default case these days */
821da177e4SLinus Torvalds #define BIOS_TRANSLATION_25563 2	/* Big disk case */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds struct aha1542_hostdata {
851da177e4SLinus Torvalds 	/* This will effectively start both of them at the first mailbox */
861da177e4SLinus Torvalds 	int bios_translation;	/* Mapping bios uses - for compatibility */
871da177e4SLinus Torvalds 	int aha1542_last_mbi_used;
881da177e4SLinus Torvalds 	int aha1542_last_mbo_used;
891da177e4SLinus Torvalds 	Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
901da177e4SLinus Torvalds 	struct mailbox mb[2 * AHA1542_MAILBOXES];
911da177e4SLinus Torvalds 	struct ccb ccb[AHA1542_MAILBOXES];
921da177e4SLinus Torvalds };
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds static DEFINE_SPINLOCK(aha1542_lock);
951da177e4SLinus Torvalds 
96f1bbef63SOndrej Zary static inline void aha1542_intr_reset(u16 base)
97f1bbef63SOndrej Zary {
98f1bbef63SOndrej Zary 	outb(IRST, CONTROL(base));
99f1bbef63SOndrej Zary }
1001da177e4SLinus Torvalds 
1012093bfa1SOndrej Zary static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
1022093bfa1SOndrej Zary {
1032093bfa1SOndrej Zary 	bool delayed = true;
1042093bfa1SOndrej Zary 
1052093bfa1SOndrej Zary 	if (timeout == 0) {
1062093bfa1SOndrej Zary 		timeout = 3000000;
1072093bfa1SOndrej Zary 		delayed = false;
1081da177e4SLinus Torvalds 	}
1091da177e4SLinus Torvalds 
1102093bfa1SOndrej Zary 	while (1) {
1112093bfa1SOndrej Zary 		u8 bits = inb(port) & mask;
1122093bfa1SOndrej Zary 		if ((bits & allof) == allof && ((bits & noneof) == 0))
1132093bfa1SOndrej Zary 			break;
1142093bfa1SOndrej Zary 		if (delayed)
1152093bfa1SOndrej Zary 			mdelay(1);
1162093bfa1SOndrej Zary 		if (--timeout == 0)
1172093bfa1SOndrej Zary 			return false;
1182093bfa1SOndrej Zary 	}
1192093bfa1SOndrej Zary 
1202093bfa1SOndrej Zary 	return true;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /* This is a bit complicated, but we need to make sure that an interrupt
1241da177e4SLinus Torvalds    routine does not send something out while we are in the middle of this.
1251da177e4SLinus Torvalds    Fortunately, it is only at boot time that multi-byte messages
1261da177e4SLinus Torvalds    are ever sent. */
1270c2b6481SOndrej Zary static int aha1542_outb(unsigned int base, u8 cmd)
1281da177e4SLinus Torvalds {
1290c2b6481SOndrej Zary 	unsigned long flags;
1301da177e4SLinus Torvalds 
1310c2b6481SOndrej Zary 	while (1) {
1320c2b6481SOndrej Zary 		if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
1330c2b6481SOndrej Zary 			printk(KERN_ERR "aha1542_outb failed");
1340c2b6481SOndrej Zary 			return 1;
1350c2b6481SOndrej Zary 		}
1361da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
1371da177e4SLinus Torvalds 		if (inb(STATUS(base)) & CDF) {
1381da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1391da177e4SLinus Torvalds 			continue;
1401da177e4SLinus Torvalds 		}
1410c2b6481SOndrej Zary 		outb(cmd, DATA(base));
1421da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
1431da177e4SLinus Torvalds 		return 0;
1441da177e4SLinus Torvalds 	}
1450c2b6481SOndrej Zary }
1460c2b6481SOndrej Zary 
1470c2b6481SOndrej Zary static int aha1542_out(unsigned int base, u8 *cmdp, int len)
1480c2b6481SOndrej Zary {
1490c2b6481SOndrej Zary 	unsigned long flags;
1500c2b6481SOndrej Zary 
1511da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
1521da177e4SLinus Torvalds 	while (len--) {
1530c2b6481SOndrej Zary 		if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
1541da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1551da177e4SLinus Torvalds 			printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
1561da177e4SLinus Torvalds 			return 1;
1571da177e4SLinus Torvalds 		}
1580c2b6481SOndrej Zary 		outb(*cmdp++, DATA(base));
1590c2b6481SOndrej Zary 	}
1600c2b6481SOndrej Zary 	spin_unlock_irqrestore(&aha1542_lock, flags);
16123e6940aSOndrej Zary 	if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
16223e6940aSOndrej Zary 		return 1;
1630c2b6481SOndrej Zary 
1640c2b6481SOndrej Zary 	return 0;
1650c2b6481SOndrej Zary }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /* Only used at boot time, so we do not need to worry about latency as much
1681da177e4SLinus Torvalds    here */
1691da177e4SLinus Torvalds 
170f8846be3SOndrej Zary static int aha1542_in(unsigned int base, u8 *cmdp, int len, int timeout)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	unsigned long flags;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
1751da177e4SLinus Torvalds 	while (len--) {
176a13b3722SOndrej Zary 		if (!wait_mask(STATUS(base), DF, DF, 0, timeout)) {
1771da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
178f8846be3SOndrej Zary 			if (timeout == 0)
1791da177e4SLinus Torvalds 				printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
1801da177e4SLinus Torvalds 			return 1;
1811da177e4SLinus Torvalds 		}
182a13b3722SOndrej Zary 		*cmdp++ = inb(DATA(base));
183a13b3722SOndrej Zary 	}
184a13b3722SOndrej Zary 	spin_unlock_irqrestore(&aha1542_lock, flags);
185a13b3722SOndrej Zary 	return 0;
186a13b3722SOndrej Zary }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds static int makecode(unsigned hosterr, unsigned scsierr)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	switch (hosterr) {
1911da177e4SLinus Torvalds 	case 0x0:
1921da177e4SLinus Torvalds 	case 0xa:		/* Linked command complete without error and linked normally */
1931da177e4SLinus Torvalds 	case 0xb:		/* Linked command complete without error, interrupt generated */
1941da177e4SLinus Torvalds 		hosterr = 0;
1951da177e4SLinus Torvalds 		break;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	case 0x11:		/* Selection time out-The initiator selection or target
1981da177e4SLinus Torvalds 				   reselection was not complete within the SCSI Time out period */
1991da177e4SLinus Torvalds 		hosterr = DID_TIME_OUT;
2001da177e4SLinus Torvalds 		break;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
2031da177e4SLinus Torvalds 				   than was allocated by the Data Length field or the sum of the
2041da177e4SLinus Torvalds 				   Scatter / Gather Data Length fields. */
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	case 0x15:		/* MBO command was not 00, 01 or 02-The first byte of the CB was
2091da177e4SLinus Torvalds 				   invalid. This usually indicates a software failure. */
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	case 0x16:		/* Invalid CCB Operation Code-The first byte of the CCB was invalid.
2121da177e4SLinus Torvalds 				   This usually indicates a software failure. */
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	case 0x17:		/* Linked CCB does not have the same LUN-A subsequent CCB of a set
2151da177e4SLinus Torvalds 				   of linked CCB's does not specify the same logical unit number as
2161da177e4SLinus Torvalds 				   the first. */
2171da177e4SLinus Torvalds 	case 0x18:		/* Invalid Target Direction received from Host-The direction of a
2181da177e4SLinus Torvalds 				   Target Mode CCB was invalid. */
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	case 0x19:		/* Duplicate CCB Received in Target Mode-More than once CCB was
2211da177e4SLinus Torvalds 				   received to service data transfer between the same target LUN
2221da177e4SLinus Torvalds 				   and initiator SCSI ID in the same direction. */
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	case 0x1a:		/* Invalid CCB or Segment List Parameter-A segment list with a zero
2251da177e4SLinus Torvalds 				   length segment or invalid segment list boundaries was received.
2261da177e4SLinus Torvalds 				   A CCB parameter was invalid. */
2271da177e4SLinus Torvalds 		DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
2281da177e4SLinus Torvalds 		hosterr = DID_ERROR;	/* Couldn't find any better */
2291da177e4SLinus Torvalds 		break;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
2321da177e4SLinus Torvalds 				   phase sequence was requested by the target. The host adapter
2331da177e4SLinus Torvalds 				   will generate a SCSI Reset Condition, notifying the host with
2341da177e4SLinus Torvalds 				   a SCRD interrupt */
2351da177e4SLinus Torvalds 		hosterr = DID_RESET;
2361da177e4SLinus Torvalds 		break;
2371da177e4SLinus Torvalds 	default:
2381da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
2391da177e4SLinus Torvalds 		break;
2401da177e4SLinus Torvalds 	}
2411da177e4SLinus Torvalds 	return scsierr | (hosterr << 16);
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
244643a7c43SOndrej Zary static int aha1542_test_port(int bse, struct Scsi_Host *shpnt)
2451da177e4SLinus Torvalds {
246cb5b570cSOndrej Zary 	u8 inquiry_result[4];
247cb5b570cSOndrej Zary 	u8 *cmdp;
2481da177e4SLinus Torvalds 	int len;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	/* Quick and dirty test for presence of the card. */
2511da177e4SLinus Torvalds 	if (inb(STATUS(bse)) == 0xff)
2521da177e4SLinus Torvalds 		return 0;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	/* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/* In case some other card was probing here, reset interrupts */
2571da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	mdelay(20);		/* Wait a little bit for things to settle down. */
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	/* Expect INIT and IDLE, any of the others are bad */
2642093bfa1SOndrej Zary 	if (!wait_mask(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
265a13b3722SOndrej Zary 		return 0;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	/* Shouldn't have generated any interrupts during reset */
2681da177e4SLinus Torvalds 	if (inb(INTRFLAGS(bse)) & INTRMASK)
269a13b3722SOndrej Zary 		return 0;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	/* Perform a host adapter inquiry instead so we do not need to set
2721da177e4SLinus Torvalds 	   up the mailboxes ahead of time */
2731da177e4SLinus Torvalds 
2740c2b6481SOndrej Zary 	aha1542_outb(bse, CMD_INQUIRY);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	len = 4;
2771da177e4SLinus Torvalds 	cmdp = &inquiry_result[0];
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	while (len--) {
2802093bfa1SOndrej Zary 		if (!wait_mask(STATUS(bse), DF, DF, 0, 0))
281a13b3722SOndrej Zary 			return 0;
2821da177e4SLinus Torvalds 		*cmdp++ = inb(DATA(bse));
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* Reading port should reset DF */
2861da177e4SLinus Torvalds 	if (inb(STATUS(bse)) & DF)
287a13b3722SOndrej Zary 		return 0;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	/* When HACC, command is completed, and we're though testing */
2902093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(bse), HACC, HACC, 0, 0))
291a13b3722SOndrej Zary 		return 0;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	/* Clear interrupts */
2941da177e4SLinus Torvalds 	outb(IRST, CONTROL(bse));
2951da177e4SLinus Torvalds 
296bdebe224SOndrej Zary 	return 1;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /* A "high" level interrupt handler */
30087c4d7bcSJeff Garzik static void aha1542_intr_handle(struct Scsi_Host *shost)
3011da177e4SLinus Torvalds {
302e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(shost);
3031da177e4SLinus Torvalds 	void (*my_done) (Scsi_Cmnd *) = NULL;
3041da177e4SLinus Torvalds 	int errstatus, mbi, mbo, mbistatus;
3051da177e4SLinus Torvalds 	int number_serviced;
3061da177e4SLinus Torvalds 	unsigned long flags;
3071da177e4SLinus Torvalds 	Scsi_Cmnd *SCtmp;
3081da177e4SLinus Torvalds 	int flag;
309e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
310e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds #ifdef DEBUG
3131da177e4SLinus Torvalds 	{
3141da177e4SLinus Torvalds 		flag = inb(INTRFLAGS(shost->io_port));
3151da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_intr_handle: ");
3161da177e4SLinus Torvalds 		if (!(flag & ANYINTR))
3171da177e4SLinus Torvalds 			printk("no interrupt?");
3181da177e4SLinus Torvalds 		if (flag & MBIF)
3191da177e4SLinus Torvalds 			printk("MBIF ");
3201da177e4SLinus Torvalds 		if (flag & MBOA)
3211da177e4SLinus Torvalds 			printk("MBOF ");
3221da177e4SLinus Torvalds 		if (flag & HACC)
3231da177e4SLinus Torvalds 			printk("HACC ");
3241da177e4SLinus Torvalds 		if (flag & SCRD)
3251da177e4SLinus Torvalds 			printk("SCRD ");
3261da177e4SLinus Torvalds 		printk("status %02x\n", inb(STATUS(shost->io_port)));
3271da177e4SLinus Torvalds 	};
3281da177e4SLinus Torvalds #endif
3291da177e4SLinus Torvalds 	number_serviced = 0;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	while (1 == 1) {
3321da177e4SLinus Torvalds 		flag = inb(INTRFLAGS(shost->io_port));
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		/* Check for unusual interrupts.  If any of these happen, we should
3351da177e4SLinus Torvalds 		   probably do something special, but for now just printing a message
3361da177e4SLinus Torvalds 		   is sufficient.  A SCSI reset detected is something that we really
3371da177e4SLinus Torvalds 		   need to deal with in some way. */
3381da177e4SLinus Torvalds 		if (flag & ~MBIF) {
3391da177e4SLinus Torvalds 			if (flag & MBOA)
3401da177e4SLinus Torvalds 				printk("MBOF ");
3411da177e4SLinus Torvalds 			if (flag & HACC)
3421da177e4SLinus Torvalds 				printk("HACC ");
343dfd7c991SOndrej Zary 			if (flag & SCRD)
3441da177e4SLinus Torvalds 				printk("SCRD ");
3451da177e4SLinus Torvalds 		}
3461da177e4SLinus Torvalds 		aha1542_intr_reset(shost->io_port);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
349e98878f7SOndrej Zary 		mbi = aha1542->aha1542_last_mbi_used + 1;
3501da177e4SLinus Torvalds 		if (mbi >= 2 * AHA1542_MAILBOXES)
3511da177e4SLinus Torvalds 			mbi = AHA1542_MAILBOXES;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 		do {
3541da177e4SLinus Torvalds 			if (mb[mbi].status != 0)
3551da177e4SLinus Torvalds 				break;
3561da177e4SLinus Torvalds 			mbi++;
3571da177e4SLinus Torvalds 			if (mbi >= 2 * AHA1542_MAILBOXES)
3581da177e4SLinus Torvalds 				mbi = AHA1542_MAILBOXES;
359e98878f7SOndrej Zary 		} while (mbi != aha1542->aha1542_last_mbi_used);
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 		if (mb[mbi].status == 0) {
3621da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
3631da177e4SLinus Torvalds 			/* Hmm, no mail.  Must have read it the last time around */
364dfd7c991SOndrej Zary 			if (!number_serviced)
3651da177e4SLinus Torvalds 				printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
3661da177e4SLinus Torvalds 			return;
3671da177e4SLinus Torvalds 		};
3681da177e4SLinus Torvalds 
36910be6250SOndrej Zary 		mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
3701da177e4SLinus Torvalds 		mbistatus = mb[mbi].status;
3711da177e4SLinus Torvalds 		mb[mbi].status = 0;
372e98878f7SOndrej Zary 		aha1542->aha1542_last_mbi_used = mbi;
3731da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds #ifdef DEBUG
3761da177e4SLinus Torvalds 		{
3771da177e4SLinus Torvalds 			if (ccb[mbo].tarstat | ccb[mbo].hastat)
3781da177e4SLinus Torvalds 				printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
3791da177e4SLinus Torvalds 				       ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
3801da177e4SLinus Torvalds 		};
3811da177e4SLinus Torvalds #endif
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 		if (mbistatus == 3)
3841da177e4SLinus Torvalds 			continue;	/* Aborted command not found */
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds #ifdef DEBUG
3871da177e4SLinus Torvalds 		printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
3881da177e4SLinus Torvalds #endif
3891da177e4SLinus Torvalds 
390e98878f7SOndrej Zary 		SCtmp = aha1542->SCint[mbo];
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 		if (!SCtmp || !SCtmp->scsi_done) {
3931da177e4SLinus Torvalds 			printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
3941da177e4SLinus Torvalds 			printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
3951da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].idlun, mbo);
3961da177e4SLinus Torvalds 			return;
3971da177e4SLinus Torvalds 		}
3981da177e4SLinus Torvalds 		my_done = SCtmp->scsi_done;
3991da177e4SLinus Torvalds 		kfree(SCtmp->host_scribble);
4001da177e4SLinus Torvalds 		SCtmp->host_scribble = NULL;
4011da177e4SLinus Torvalds 		/* Fetch the sense data, and tuck it away, in the required slot.  The
4021da177e4SLinus Torvalds 		   Adaptec automatically fetches it, and there is no guarantee that
4031da177e4SLinus Torvalds 		   we will still have it in the cdb when we come back */
4041da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2)
4051da177e4SLinus Torvalds 			memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
406b80ca4f7SFUJITA Tomonori 			       SCSI_SENSE_BUFFERSIZE);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		/* is there mail :-) */
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 		/* more error checking left out here */
4121da177e4SLinus Torvalds 		if (mbistatus != 1)
4131da177e4SLinus Torvalds 			/* This is surely wrong, but I don't know what's right */
4141da177e4SLinus Torvalds 			errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
4151da177e4SLinus Torvalds 		else
4161da177e4SLinus Torvalds 			errstatus = 0;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds #ifdef DEBUG
4191da177e4SLinus Torvalds 		if (errstatus)
4201da177e4SLinus Torvalds 			printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
4211da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].tarstat);
4221da177e4SLinus Torvalds #endif
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2) {
4251da177e4SLinus Torvalds #ifdef DEBUG
4261da177e4SLinus Torvalds 			int i;
4271da177e4SLinus Torvalds #endif
4281da177e4SLinus Torvalds 			DEB(printk("aha1542_intr_handle: sense:"));
4291da177e4SLinus Torvalds #ifdef DEBUG
4301da177e4SLinus Torvalds 			for (i = 0; i < 12; i++)
4311da177e4SLinus Torvalds 				printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
4321da177e4SLinus Torvalds 			printk("\n");
4331da177e4SLinus Torvalds #endif
4341da177e4SLinus Torvalds 			/*
4351da177e4SLinus Torvalds 			   DEB(printk("aha1542_intr_handle: buf:"));
4361da177e4SLinus Torvalds 			   for (i = 0; i < bufflen; i++)
4371da177e4SLinus Torvalds 			   printk("%02x ", ((unchar *)buff)[i]);
4381da177e4SLinus Torvalds 			   printk("\n");
4391da177e4SLinus Torvalds 			 */
4401da177e4SLinus Torvalds 		}
4411da177e4SLinus Torvalds 		DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
4421da177e4SLinus Torvalds 		SCtmp->result = errstatus;
443e98878f7SOndrej Zary 		aha1542->SCint[mbo] = NULL;	/* This effectively frees up the mailbox slot, as
4441da177e4SLinus Torvalds 						   far as queuecommand is concerned */
4451da177e4SLinus Torvalds 		my_done(SCtmp);
4461da177e4SLinus Torvalds 		number_serviced++;
4471da177e4SLinus Torvalds 	};
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
45009a44833SOndrej Zary /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
45109a44833SOndrej Zary static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
45209a44833SOndrej Zary {
45309a44833SOndrej Zary 	unsigned long flags;
45409a44833SOndrej Zary 	struct Scsi_Host *shost = dev_id;
45509a44833SOndrej Zary 
45609a44833SOndrej Zary 	spin_lock_irqsave(shost->host_lock, flags);
45709a44833SOndrej Zary 	aha1542_intr_handle(shost);
45809a44833SOndrej Zary 	spin_unlock_irqrestore(shost->host_lock, flags);
45909a44833SOndrej Zary 	return IRQ_HANDLED;
46009a44833SOndrej Zary }
46109a44833SOndrej Zary 
462f281233dSJeff Garzik static int aha1542_queuecommand_lck(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
4631da177e4SLinus Torvalds {
464e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
465cb5b570cSOndrej Zary 	u8 direction;
466cb5b570cSOndrej Zary 	u8 *cmd = (u8 *) SCpnt->cmnd;
467cb5b570cSOndrej Zary 	u8 target = SCpnt->device->id;
468cb5b570cSOndrej Zary 	u8 lun = SCpnt->device->lun;
4691da177e4SLinus Torvalds 	unsigned long flags;
470fc3fdfccSBoaz Harrosh 	int bufflen = scsi_bufflen(SCpnt);
4711da177e4SLinus Torvalds 	int mbo;
472e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
473e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	DEB(int i);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	DEB(if (target > 1) {
4781da177e4SLinus Torvalds 	    SCpnt->result = DID_TIME_OUT << 16;
4791da177e4SLinus Torvalds 	    done(SCpnt); return 0;
4801da177e4SLinus Torvalds 	    }
4811da177e4SLinus Torvalds 	);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (*cmd == REQUEST_SENSE) {
4841da177e4SLinus Torvalds 		/* Don't do the command - we have the sense data already */
4851da177e4SLinus Torvalds 		SCpnt->result = 0;
4861da177e4SLinus Torvalds 		done(SCpnt);
4871da177e4SLinus Torvalds 		return 0;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds #ifdef DEBUG
4901da177e4SLinus Torvalds 	if (*cmd == READ_10 || *cmd == WRITE_10)
4911da177e4SLinus Torvalds 		i = xscsi2int(cmd + 2);
4921da177e4SLinus Torvalds 	else if (*cmd == READ_6 || *cmd == WRITE_6)
4931da177e4SLinus Torvalds 		i = scsi2int(cmd + 2);
4941da177e4SLinus Torvalds 	else
4951da177e4SLinus Torvalds 		i = -1;
4961da177e4SLinus Torvalds 	if (done)
4971da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
4981da177e4SLinus Torvalds 	else
4991da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
5001da177e4SLinus Torvalds 	printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
5011da177e4SLinus Torvalds 	for (i = 0; i < SCpnt->cmd_len; i++)
5021da177e4SLinus Torvalds 		printk("%02x ", cmd[i]);
5031da177e4SLinus Torvalds 	printk("\n");
5041da177e4SLinus Torvalds 	if (*cmd == WRITE_10 || *cmd == WRITE_6)
5051da177e4SLinus Torvalds 		return 0;	/* we are still testing, so *don't* write */
5061da177e4SLinus Torvalds #endif
5071da177e4SLinus Torvalds 	/* Use the outgoing mailboxes in a round-robin fashion, because this
5081da177e4SLinus Torvalds 	   is how the host adapter will scan for them */
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
511e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
5121da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
5131da177e4SLinus Torvalds 		mbo = 0;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	do {
516e98878f7SOndrej Zary 		if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
5171da177e4SLinus Torvalds 			break;
5181da177e4SLinus Torvalds 		mbo++;
5191da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
5201da177e4SLinus Torvalds 			mbo = 0;
521e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
5221da177e4SLinus Torvalds 
523e98878f7SOndrej Zary 	if (mb[mbo].status || aha1542->SCint[mbo])
5241da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
5251da177e4SLinus Torvalds 
526e98878f7SOndrej Zary 	aha1542->SCint[mbo] = SCpnt;	/* This will effectively prevent someone else from
5271da177e4SLinus Torvalds 					   screwing with this cdb. */
5281da177e4SLinus Torvalds 
529e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
5301da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds #ifdef DEBUG
5331da177e4SLinus Torvalds 	printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
5341da177e4SLinus Torvalds #endif
5351da177e4SLinus Torvalds 
53610be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	ccb[mbo].cdblen = SCpnt->cmd_len;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	direction = 0;
5431da177e4SLinus Torvalds 	if (*cmd == READ_10 || *cmd == READ_6)
5441da177e4SLinus Torvalds 		direction = 8;
5451da177e4SLinus Torvalds 	else if (*cmd == WRITE_10 || *cmd == WRITE_6)
5461da177e4SLinus Torvalds 		direction = 16;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
5491da177e4SLinus Torvalds 
550fc3fdfccSBoaz Harrosh 	if (bufflen) {
55151cf2249SJens Axboe 		struct scatterlist *sg;
5521da177e4SLinus Torvalds 		struct chain *cptr;
5531da177e4SLinus Torvalds #ifdef DEBUG
5541da177e4SLinus Torvalds 		unsigned char *ptr;
5551da177e4SLinus Torvalds #endif
556fc3fdfccSBoaz Harrosh 		int i, sg_count = scsi_sg_count(SCpnt);
5571da177e4SLinus Torvalds 		ccb[mbo].op = 2;	/* SCSI Initiator Command  w/scatter-gather */
558fc3fdfccSBoaz Harrosh 		SCpnt->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
559fc3fdfccSBoaz Harrosh 		                                         GFP_KERNEL | GFP_DMA);
5601da177e4SLinus Torvalds 		cptr = (struct chain *) SCpnt->host_scribble;
5611da177e4SLinus Torvalds 		if (cptr == NULL) {
5621da177e4SLinus Torvalds 			/* free the claimed mailbox slot */
563e98878f7SOndrej Zary 			aha1542->SCint[mbo] = NULL;
5641da177e4SLinus Torvalds 			return SCSI_MLQUEUE_HOST_BUSY;
5651da177e4SLinus Torvalds 		}
566fc3fdfccSBoaz Harrosh 		scsi_for_each_sg(SCpnt, sg, sg_count, i) {
56710be6250SOndrej Zary 			any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
56810be6250SOndrej Zary 								+ sg->offset);
56951cf2249SJens Axboe 			any2scsi(cptr[i].datalen, sg->length);
5701da177e4SLinus Torvalds 		};
571fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
57210be6250SOndrej Zary 		any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
5731da177e4SLinus Torvalds #ifdef DEBUG
5741da177e4SLinus Torvalds 		printk("cptr %x: ", cptr);
5751da177e4SLinus Torvalds 		ptr = (unsigned char *) cptr;
5761da177e4SLinus Torvalds 		for (i = 0; i < 18; i++)
5771da177e4SLinus Torvalds 			printk("%02x ", ptr[i]);
5781da177e4SLinus Torvalds #endif
5791da177e4SLinus Torvalds 	} else {
5801da177e4SLinus Torvalds 		ccb[mbo].op = 0;	/* SCSI Initiator Command */
5811da177e4SLinus Torvalds 		SCpnt->host_scribble = NULL;
582fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, 0);
583fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].dataptr, 0);
5841da177e4SLinus Torvalds 	};
5851da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);	/*SCSI Target Id */
5861da177e4SLinus Torvalds 	ccb[mbo].rsalen = 16;
5871da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
5881da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds #ifdef DEBUG
5911da177e4SLinus Torvalds 	{
5921da177e4SLinus Torvalds 		int i;
5931da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_command: sending.. ");
5941da177e4SLinus Torvalds 		for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
595cb5b570cSOndrej Zary 			printk("%02x ", ((u8 *) &ccb[mbo])[i]);
5961da177e4SLinus Torvalds 	};
5971da177e4SLinus Torvalds #endif
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	if (done) {
600f232d538SOndrej Zary 		DEB(printk("aha1542_queuecommand: now waiting for interrupt "));
6011da177e4SLinus Torvalds 		SCpnt->scsi_done = done;
6021da177e4SLinus Torvalds 		mb[mbo].status = 1;
6030c2b6481SOndrej Zary 		aha1542_outb(SCpnt->device->host->io_port, CMD_START_SCSI);
6041da177e4SLinus Torvalds 	} else
6051da177e4SLinus Torvalds 		printk("aha1542_queuecommand: done can't be NULL\n");
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	return 0;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
610f281233dSJeff Garzik static DEF_SCSI_QCMD(aha1542_queuecommand)
611f281233dSJeff Garzik 
6121da177e4SLinus Torvalds /* Initialize mailboxes */
6131da177e4SLinus Torvalds static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
6141da177e4SLinus Torvalds {
615e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(shpnt);
6161da177e4SLinus Torvalds 	int i;
617e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
618e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
6191da177e4SLinus Torvalds 
620cb5b570cSOndrej Zary 	u8 cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
6231da177e4SLinus Torvalds 		mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
62410be6250SOndrej Zary 		any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
6251da177e4SLinus Torvalds 	};
6261da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
62710be6250SOndrej Zary 	any2scsi((cmd + 2), isa_virt_to_bus(mb));
62823e6940aSOndrej Zary 	if (aha1542_out(bse, cmd, 5))
6291da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
6301da177e4SLinus Torvalds 	aha1542_intr_reset(bse);
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6333a70c006SOndrej Zary static int aha1542_getconfig(int base_io, unsigned int *irq_level, unsigned char *dma_chan, unsigned int *scsi_id)
6341da177e4SLinus Torvalds {
635cb5b570cSOndrej Zary 	u8 inquiry_result[3];
6361da177e4SLinus Torvalds 	int i;
6371da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
6381da177e4SLinus Torvalds 	if (i & DF) {
6391da177e4SLinus Torvalds 		i = inb(DATA(base_io));
6401da177e4SLinus Torvalds 	};
6410c2b6481SOndrej Zary 	aha1542_outb(base_io, CMD_RETCONF);
642f8846be3SOndrej Zary 	aha1542_in(base_io, inquiry_result, 3, 0);
6432093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
6441da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query board settings\n");
6451da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
6461da177e4SLinus Torvalds 	switch (inquiry_result[0]) {
6471da177e4SLinus Torvalds 	case 0x80:
6481da177e4SLinus Torvalds 		*dma_chan = 7;
6491da177e4SLinus Torvalds 		break;
6501da177e4SLinus Torvalds 	case 0x40:
6511da177e4SLinus Torvalds 		*dma_chan = 6;
6521da177e4SLinus Torvalds 		break;
6531da177e4SLinus Torvalds 	case 0x20:
6541da177e4SLinus Torvalds 		*dma_chan = 5;
6551da177e4SLinus Torvalds 		break;
6561da177e4SLinus Torvalds 	case 0x01:
6571da177e4SLinus Torvalds 		*dma_chan = 0;
6581da177e4SLinus Torvalds 		break;
6591da177e4SLinus Torvalds 	case 0:
6601da177e4SLinus Torvalds 		/* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
6611da177e4SLinus Torvalds 		   Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
6621da177e4SLinus Torvalds 		*dma_chan = 0xFF;
6631da177e4SLinus Torvalds 		break;
6641da177e4SLinus Torvalds 	default:
6651da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec DMA priority.  Disabling board\n");
6661da177e4SLinus Torvalds 		return -1;
6671da177e4SLinus Torvalds 	};
6681da177e4SLinus Torvalds 	switch (inquiry_result[1]) {
6691da177e4SLinus Torvalds 	case 0x40:
6701da177e4SLinus Torvalds 		*irq_level = 15;
6711da177e4SLinus Torvalds 		break;
6721da177e4SLinus Torvalds 	case 0x20:
6731da177e4SLinus Torvalds 		*irq_level = 14;
6741da177e4SLinus Torvalds 		break;
6751da177e4SLinus Torvalds 	case 0x8:
6761da177e4SLinus Torvalds 		*irq_level = 12;
6771da177e4SLinus Torvalds 		break;
6781da177e4SLinus Torvalds 	case 0x4:
6791da177e4SLinus Torvalds 		*irq_level = 11;
6801da177e4SLinus Torvalds 		break;
6811da177e4SLinus Torvalds 	case 0x2:
6821da177e4SLinus Torvalds 		*irq_level = 10;
6831da177e4SLinus Torvalds 		break;
6841da177e4SLinus Torvalds 	case 0x1:
6851da177e4SLinus Torvalds 		*irq_level = 9;
6861da177e4SLinus Torvalds 		break;
6871da177e4SLinus Torvalds 	default:
6881da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec IRQ level.  Disabling board\n");
6891da177e4SLinus Torvalds 		return -1;
6901da177e4SLinus Torvalds 	};
6911da177e4SLinus Torvalds 	*scsi_id = inquiry_result[2] & 7;
6921da177e4SLinus Torvalds 	return 0;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds /* This function should only be called for 1542C boards - we can detect
6961da177e4SLinus Torvalds    the special firmware settings and unlock the board */
6971da177e4SLinus Torvalds 
698643a7c43SOndrej Zary static int aha1542_mbenable(int base)
6991da177e4SLinus Torvalds {
700cb5b570cSOndrej Zary 	static u8 mbenable_cmd[3];
701cb5b570cSOndrej Zary 	static u8 mbenable_result[2];
7021da177e4SLinus Torvalds 	int retval;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	retval = BIOS_TRANSLATION_6432;
7051da177e4SLinus Torvalds 
7060c2b6481SOndrej Zary 	aha1542_outb(base, CMD_EXTBIOS);
707f8846be3SOndrej Zary 	if (aha1542_in(base, mbenable_result, 2, 100))
7081da177e4SLinus Torvalds 		return retval;
7092093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 100))
7102093bfa1SOndrej Zary 		goto fail;
7111da177e4SLinus Torvalds 	aha1542_intr_reset(base);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
7141da177e4SLinus Torvalds 		mbenable_cmd[0] = CMD_MBENABLE;
7151da177e4SLinus Torvalds 		mbenable_cmd[1] = 0;
7161da177e4SLinus Torvalds 		mbenable_cmd[2] = mbenable_result[1];
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 		if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
7191da177e4SLinus Torvalds 			retval = BIOS_TRANSLATION_25563;
7201da177e4SLinus Torvalds 
72123e6940aSOndrej Zary 		if (aha1542_out(base, mbenable_cmd, 3))
7222093bfa1SOndrej Zary 			goto fail;
7231da177e4SLinus Torvalds 	};
7241da177e4SLinus Torvalds 	while (0) {
7251da177e4SLinus Torvalds fail:
7261da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 	aha1542_intr_reset(base);
7291da177e4SLinus Torvalds 	return retval;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
733643a7c43SOndrej Zary static int aha1542_query(int base_io, int *transl)
7341da177e4SLinus Torvalds {
735cb5b570cSOndrej Zary 	u8 inquiry_result[4];
7361da177e4SLinus Torvalds 	int i;
7371da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
7381da177e4SLinus Torvalds 	if (i & DF) {
7391da177e4SLinus Torvalds 		i = inb(DATA(base_io));
7401da177e4SLinus Torvalds 	};
7410c2b6481SOndrej Zary 	aha1542_outb(base_io, CMD_INQUIRY);
742f8846be3SOndrej Zary 	aha1542_in(base_io, inquiry_result, 4, 0);
7432093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
7441da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query card type\n");
7451da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	*transl = BIOS_TRANSLATION_6432;	/* Default case */
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	/* For an AHA1740 series board, we ignore the board since there is a
7501da177e4SLinus Torvalds 	   hardware bug which can lead to wrong blocks being returned if the board
7511da177e4SLinus Torvalds 	   is operating in the 1542 emulation mode.  Since there is an extended mode
7521da177e4SLinus Torvalds 	   driver, we simply ignore the board and let the 1740 driver pick it up.
7531da177e4SLinus Torvalds 	 */
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	if (inquiry_result[0] == 0x43) {
7561da177e4SLinus Torvalds 		printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
7571da177e4SLinus Torvalds 		return 1;
7581da177e4SLinus Torvalds 	};
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	/* Always call this - boards that do not support extended bios translation
7611da177e4SLinus Torvalds 	   will ignore the command, and we will set the proper default */
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	*transl = aha1542_mbenable(base_io);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	return 0;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
768f71429abSOndrej Zary static u8 dma_speed_hw(int dma_speed)
7691da177e4SLinus Torvalds {
770f71429abSOndrej Zary 	switch (dma_speed) {
7711da177e4SLinus Torvalds 	case 5:
772f71429abSOndrej Zary 		return 0x00;
7731da177e4SLinus Torvalds 	case 6:
774f71429abSOndrej Zary 		return 0x04;
7751da177e4SLinus Torvalds 	case 7:
776f71429abSOndrej Zary 		return 0x01;
7771da177e4SLinus Torvalds 	case 8:
778f71429abSOndrej Zary 		return 0x02;
7791da177e4SLinus Torvalds 	case 10:
780f71429abSOndrej Zary 		return 0x03;
7811da177e4SLinus Torvalds 	}
7821da177e4SLinus Torvalds 
783f71429abSOndrej Zary 	return 0xff;	/* invalid */
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
786b847fd0dSOndrej Zary /* Set the Bus on/off-times as not to ruin floppy performance */
787b847fd0dSOndrej Zary static void aha1542_set_bus_times(int indx)
788b847fd0dSOndrej Zary {
789f71429abSOndrej Zary 	unsigned int base_io = io[indx];
790b847fd0dSOndrej Zary 
791f71429abSOndrej Zary 	if (bus_on[indx] > 0) {
792f71429abSOndrej Zary 		u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on[indx], 2, 15) };
793f71429abSOndrej Zary 
794b847fd0dSOndrej Zary 		aha1542_intr_reset(base_io);
79523e6940aSOndrej Zary 		if (aha1542_out(base_io, oncmd, 2))
796b847fd0dSOndrej Zary 			goto fail;
797f71429abSOndrej Zary 	}
798f71429abSOndrej Zary 
799f71429abSOndrej Zary 	if (bus_off[indx] > 0) {
800f71429abSOndrej Zary 		u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off[indx], 1, 64) };
801f71429abSOndrej Zary 
802b847fd0dSOndrej Zary 		aha1542_intr_reset(base_io);
80323e6940aSOndrej Zary 		if (aha1542_out(base_io, offcmd, 2))
804b847fd0dSOndrej Zary 			goto fail;
805f71429abSOndrej Zary 	}
806f71429abSOndrej Zary 
807f71429abSOndrej Zary 	if (dma_speed_hw(dma_speed[indx]) != 0xff) {
808f71429abSOndrej Zary 		u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed[indx]) };
809f71429abSOndrej Zary 
810b847fd0dSOndrej Zary 		aha1542_intr_reset(base_io);
81123e6940aSOndrej Zary 		if (aha1542_out(base_io, dmacmd, 2))
812b847fd0dSOndrej Zary 			goto fail;
813b847fd0dSOndrej Zary 	}
814b847fd0dSOndrej Zary 	aha1542_intr_reset(base_io);
815b847fd0dSOndrej Zary 	return;
816b847fd0dSOndrej Zary fail:
817b847fd0dSOndrej Zary 	printk(KERN_ERR "setting bus on/off-time failed\n");
818b847fd0dSOndrej Zary 	aha1542_intr_reset(base_io);
819b847fd0dSOndrej Zary }
820b847fd0dSOndrej Zary 
8211da177e4SLinus Torvalds /* return non-zero on detection */
822643a7c43SOndrej Zary static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
8231da177e4SLinus Torvalds {
824f71429abSOndrej Zary 	unsigned int base_io = io[indx];
8253a70c006SOndrej Zary 	struct Scsi_Host *shpnt;
826e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542;
8271da177e4SLinus Torvalds 
8283a70c006SOndrej Zary 	if (base_io == 0)
829643a7c43SOndrej Zary 		return NULL;
8301da177e4SLinus Torvalds 
8313a70c006SOndrej Zary 	if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
8323a70c006SOndrej Zary 		return NULL;
8333a70c006SOndrej Zary 
8343a70c006SOndrej Zary 	shpnt = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
8353a70c006SOndrej Zary 	if (!shpnt)
8363a70c006SOndrej Zary 		goto release;
8373a70c006SOndrej Zary 	aha1542 = shost_priv(shpnt);
8383a70c006SOndrej Zary 
8393a70c006SOndrej Zary 	if (!aha1542_test_port(base_io, shpnt))
8403a70c006SOndrej Zary 		goto unregister;
8411da177e4SLinus Torvalds 
842b847fd0dSOndrej Zary 	aha1542_set_bus_times(indx);
8433a70c006SOndrej Zary 	if (aha1542_query(base_io, &aha1542->bios_translation))
8443a70c006SOndrej Zary 		goto unregister;
8453a70c006SOndrej Zary 	if (aha1542_getconfig(base_io, &shpnt->irq, &shpnt->dma_channel, &shpnt->this_id) == -1)
8461da177e4SLinus Torvalds 		goto unregister;
8471da177e4SLinus Torvalds 
8483a70c006SOndrej Zary 	printk(KERN_INFO "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d", shpnt->this_id, base_io, shpnt->irq);
8493a70c006SOndrej Zary 	if (shpnt->dma_channel != 0xFF)
8503a70c006SOndrej Zary 		printk(", DMA %d", shpnt->dma_channel);
8511da177e4SLinus Torvalds 	printk("\n");
8523a70c006SOndrej Zary 	if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
8533a70c006SOndrej Zary 		printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	setup_mailboxes(base_io, shpnt);
8561da177e4SLinus Torvalds 
8573a70c006SOndrej Zary 	if (request_irq(shpnt->irq, do_aha1542_intr_handle, 0,
85887c4d7bcSJeff Garzik 					"aha1542", shpnt)) {
8591da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
8601da177e4SLinus Torvalds 		goto unregister;
8611da177e4SLinus Torvalds 	}
8623a70c006SOndrej Zary 	if (shpnt->dma_channel != 0xFF) {
8633a70c006SOndrej Zary 		if (request_dma(shpnt->dma_channel, "aha1542")) {
8641da177e4SLinus Torvalds 			printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
8653a70c006SOndrej Zary 			goto free_irq;
8661da177e4SLinus Torvalds 		}
8673a70c006SOndrej Zary 		if (shpnt->dma_channel == 0 || shpnt->dma_channel >= 5) {
8683a70c006SOndrej Zary 			set_dma_mode(shpnt->dma_channel, DMA_MODE_CASCADE);
8693a70c006SOndrej Zary 			enable_dma(shpnt->dma_channel);
8701da177e4SLinus Torvalds 		}
8711da177e4SLinus Torvalds 	}
87287c4d7bcSJeff Garzik 
8731da177e4SLinus Torvalds 	shpnt->unique_id = base_io;
8741da177e4SLinus Torvalds 	shpnt->io_port = base_io;
8753a70c006SOndrej Zary 	shpnt->n_io_port = AHA1542_REGION_SIZE;
8763a70c006SOndrej Zary 	aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
8773a70c006SOndrej Zary 	aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
8781da177e4SLinus Torvalds 
8793a70c006SOndrej Zary 	if (scsi_add_host(shpnt, pdev))
8803a70c006SOndrej Zary 		goto free_dma;
8811da177e4SLinus Torvalds 
882643a7c43SOndrej Zary 	scsi_scan_host(shpnt);
8831da177e4SLinus Torvalds 
884643a7c43SOndrej Zary 	return shpnt;
8853a70c006SOndrej Zary free_dma:
8863a70c006SOndrej Zary 	if (shpnt->dma_channel != 0xff)
8873a70c006SOndrej Zary 		free_dma(shpnt->dma_channel);
8883a70c006SOndrej Zary free_irq:
8893a70c006SOndrej Zary 	free_irq(shpnt->irq, shpnt);
8901da177e4SLinus Torvalds unregister:
891643a7c43SOndrej Zary 	scsi_host_put(shpnt);
8923a70c006SOndrej Zary release:
8933a70c006SOndrej Zary 	release_region(base_io, AHA1542_REGION_SIZE);
8941da177e4SLinus Torvalds 
895643a7c43SOndrej Zary 	return NULL;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds static int aha1542_release(struct Scsi_Host *shost)
8991da177e4SLinus Torvalds {
900643a7c43SOndrej Zary 	scsi_remove_host(shost);
9011da177e4SLinus Torvalds 	if (shost->dma_channel != 0xff)
9021da177e4SLinus Torvalds 		free_dma(shost->dma_channel);
9033a70c006SOndrej Zary 	if (shost->irq)
9043a70c006SOndrej Zary 		free_irq(shost->irq, shost);
9051da177e4SLinus Torvalds 	if (shost->io_port && shost->n_io_port)
9061da177e4SLinus Torvalds 		release_region(shost->io_port, shost->n_io_port);
907643a7c43SOndrej Zary 	scsi_host_put(shost);
9081da177e4SLinus Torvalds 	return 0;
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds /*
9131da177e4SLinus Torvalds  * This is a device reset.  This is handled by sending a special command
9141da177e4SLinus Torvalds  * to the device.
9151da177e4SLinus Torvalds  */
9161da177e4SLinus Torvalds static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
9171da177e4SLinus Torvalds {
918e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
9191da177e4SLinus Torvalds 	unsigned long flags;
920e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
921cb5b570cSOndrej Zary 	u8 target = SCpnt->device->id;
922cb5b570cSOndrej Zary 	u8 lun = SCpnt->device->lun;
9231da177e4SLinus Torvalds 	int mbo;
924e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
927e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
9281da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
9291da177e4SLinus Torvalds 		mbo = 0;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	do {
932e98878f7SOndrej Zary 		if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
9331da177e4SLinus Torvalds 			break;
9341da177e4SLinus Torvalds 		mbo++;
9351da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
9361da177e4SLinus Torvalds 			mbo = 0;
937e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
9381da177e4SLinus Torvalds 
939e98878f7SOndrej Zary 	if (mb[mbo].status || aha1542->SCint[mbo])
9401da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
9411da177e4SLinus Torvalds 
942e98878f7SOndrej Zary 	aha1542->SCint[mbo] = SCpnt;	/* This will effectively
9431da177e4SLinus Torvalds 					   prevent someone else from
9441da177e4SLinus Torvalds 					   screwing with this cdb. */
9451da177e4SLinus Torvalds 
946e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
9471da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
9481da177e4SLinus Torvalds 
94910be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	ccb[mbo].op = 0x81;	/* BUS DEVICE RESET */
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);		/*SCSI Target Id */
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
9581da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	/*
9611da177e4SLinus Torvalds 	 * Now tell the 1542 to flush all pending commands for this
9621da177e4SLinus Torvalds 	 * target
9631da177e4SLinus Torvalds 	 */
9640c2b6481SOndrej Zary 	aha1542_outb(SCpnt->device->host->io_port, CMD_START_SCSI);
9651da177e4SLinus Torvalds 
966017560fcSJeff Garzik 	scmd_printk(KERN_WARNING, SCpnt,
967017560fcSJeff Garzik 		"Trying device reset for target\n");
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	return SUCCESS;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
9728537cba8SOndrej Zary static int aha1542_reset(Scsi_Cmnd *SCpnt, u8 reset_cmd)
9731da177e4SLinus Torvalds {
974e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
9751da177e4SLinus Torvalds 	int i;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	/*
9781da177e4SLinus Torvalds 	 * This does a scsi reset for all devices on the bus.
9791da177e4SLinus Torvalds 	 * In principle, we could also reset the 1542 - should
9801da177e4SLinus Torvalds 	 * we do this?  Try this first, and we can add that later
9811da177e4SLinus Torvalds 	 * if it turns out to be useful.
9821da177e4SLinus Torvalds 	 */
9838537cba8SOndrej Zary 	outb(reset_cmd, CONTROL(SCpnt->device->host->io_port));
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	/*
9861da177e4SLinus Torvalds 	 * Wait for the thing to settle down a bit.  Unfortunately
9871da177e4SLinus Torvalds 	 * this is going to basically lock up the machine while we
9881da177e4SLinus Torvalds 	 * wait for this to complete.  To be 100% correct, we need to
9891da177e4SLinus Torvalds 	 * check for timeout, and if we are doing something like this
9901da177e4SLinus Torvalds 	 * we are pretty desperate anyways.
9911da177e4SLinus Torvalds 	 */
9921da177e4SLinus Torvalds 	ssleep(4);
9931da177e4SLinus Torvalds 	spin_lock_irq(SCpnt->device->host->host_lock);
9941da177e4SLinus Torvalds 
9952093bfa1SOndrej Zary 	if (!wait_mask(STATUS(SCpnt->device->host->io_port),
996a13b3722SOndrej Zary 	     STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
997a13b3722SOndrej Zary 		spin_unlock_irq(SCpnt->device->host->host_lock);
998a13b3722SOndrej Zary 		return FAILED;
999a13b3722SOndrej Zary 	}
10001da177e4SLinus Torvalds 	/*
10011da177e4SLinus Torvalds 	 * We need to do this too before the 1542 can interact with
10028537cba8SOndrej Zary 	 * us again after host reset.
10031da177e4SLinus Torvalds 	 */
10048537cba8SOndrej Zary 	if (reset_cmd & HRST)
10051da177e4SLinus Torvalds 		setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
10061da177e4SLinus Torvalds 	/*
10071da177e4SLinus Torvalds 	 * Now try to pick up the pieces.  For all pending commands,
10081da177e4SLinus Torvalds 	 * free any internal data structures, and basically clear things
10091da177e4SLinus Torvalds 	 * out.  We do not try and restart any commands or anything -
10101da177e4SLinus Torvalds 	 * the strategy handler takes care of that crap.
10111da177e4SLinus Torvalds 	 */
10121da177e4SLinus Torvalds 	printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
1015e98878f7SOndrej Zary 		if (aha1542->SCint[i] != NULL) {
10161da177e4SLinus Torvalds 			Scsi_Cmnd *SCtmp;
1017e98878f7SOndrej Zary 			SCtmp = aha1542->SCint[i];
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 			if (SCtmp->device->soft_reset) {
10201da177e4SLinus Torvalds 				/*
10211da177e4SLinus Torvalds 				 * If this device implements the soft reset option,
10221da177e4SLinus Torvalds 				 * then it is still holding onto the command, and
10231da177e4SLinus Torvalds 				 * may yet complete it.  In this case, we don't
10241da177e4SLinus Torvalds 				 * flush the data.
10251da177e4SLinus Torvalds 				 */
10261da177e4SLinus Torvalds 				continue;
10271da177e4SLinus Torvalds 			}
10281da177e4SLinus Torvalds 			kfree(SCtmp->host_scribble);
10291da177e4SLinus Torvalds 			SCtmp->host_scribble = NULL;
1030e98878f7SOndrej Zary 			aha1542->SCint[i] = NULL;
1031e98878f7SOndrej Zary 			aha1542->mb[i].status = 0;
10321da177e4SLinus Torvalds 		}
10331da177e4SLinus Torvalds 	}
10341da177e4SLinus Torvalds 
1035df0ae249SJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
10361da177e4SLinus Torvalds 	return SUCCESS;
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
10398537cba8SOndrej Zary static int aha1542_bus_reset(Scsi_Cmnd *SCpnt)
10408537cba8SOndrej Zary {
10418537cba8SOndrej Zary 	return aha1542_reset(SCpnt, SCRST);
10428537cba8SOndrej Zary }
10438537cba8SOndrej Zary 
10448537cba8SOndrej Zary static int aha1542_host_reset(Scsi_Cmnd *SCpnt)
10458537cba8SOndrej Zary {
10468537cba8SOndrej Zary 	return aha1542_reset(SCpnt, HRST | SCRST);
10478537cba8SOndrej Zary }
10488537cba8SOndrej Zary 
10491da177e4SLinus Torvalds static int aha1542_biosparam(struct scsi_device *sdev,
1050*17787a09SOndrej Zary 		struct block_device *bdev, sector_t capacity, int geom[])
10511da177e4SLinus Torvalds {
1052e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
10531da177e4SLinus Torvalds 
1054*17787a09SOndrej Zary 	if (capacity >= 0x200000 &&
1055*17787a09SOndrej Zary 			aha1542->bios_translation == BIOS_TRANSLATION_25563) {
10561da177e4SLinus Torvalds 		/* Please verify that this is the same as what DOS returns */
1057*17787a09SOndrej Zary 		geom[0] = 255;	/* heads */
1058*17787a09SOndrej Zary 		geom[1] = 63;	/* sectors */
10591da177e4SLinus Torvalds 	} else {
1060*17787a09SOndrej Zary 		geom[0] = 64;	/* heads */
1061*17787a09SOndrej Zary 		geom[1] = 32;	/* sectors */
10621da177e4SLinus Torvalds 	}
1063*17787a09SOndrej Zary 	geom[2] = sector_div(capacity, geom[0] * geom[1]);	/* cylinders */
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	return 0;
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds MODULE_LICENSE("GPL");
10681da177e4SLinus Torvalds 
1069d0be4a7dSChristoph Hellwig static struct scsi_host_template driver_template = {
1070643a7c43SOndrej Zary 	.module			= THIS_MODULE,
10711da177e4SLinus Torvalds 	.proc_name		= "aha1542",
10721da177e4SLinus Torvalds 	.name			= "Adaptec 1542",
10731da177e4SLinus Torvalds 	.queuecommand		= aha1542_queuecommand,
10741da177e4SLinus Torvalds 	.eh_device_reset_handler= aha1542_dev_reset,
10751da177e4SLinus Torvalds 	.eh_bus_reset_handler	= aha1542_bus_reset,
10761da177e4SLinus Torvalds 	.eh_host_reset_handler	= aha1542_host_reset,
10771da177e4SLinus Torvalds 	.bios_param		= aha1542_biosparam,
10781da177e4SLinus Torvalds 	.can_queue		= AHA1542_MAILBOXES,
10791da177e4SLinus Torvalds 	.this_id		= 7,
108010be6250SOndrej Zary 	.sg_tablesize		= 16,
108110be6250SOndrej Zary 	.cmd_per_lun		= 1,
10821da177e4SLinus Torvalds 	.unchecked_isa_dma	= 1,
10831da177e4SLinus Torvalds 	.use_clustering		= ENABLE_CLUSTERING,
10841da177e4SLinus Torvalds };
1085643a7c43SOndrej Zary 
1086643a7c43SOndrej Zary static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1087643a7c43SOndrej Zary {
1088643a7c43SOndrej Zary 	struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1089643a7c43SOndrej Zary 
1090643a7c43SOndrej Zary 	if (!sh)
1091643a7c43SOndrej Zary 		return 0;
1092643a7c43SOndrej Zary 
1093643a7c43SOndrej Zary 	dev_set_drvdata(pdev, sh);
1094643a7c43SOndrej Zary 	return 1;
1095643a7c43SOndrej Zary }
1096643a7c43SOndrej Zary 
1097643a7c43SOndrej Zary static int aha1542_isa_remove(struct device *pdev,
1098643a7c43SOndrej Zary 				    unsigned int ndev)
1099643a7c43SOndrej Zary {
1100643a7c43SOndrej Zary 	aha1542_release(dev_get_drvdata(pdev));
1101643a7c43SOndrej Zary 	dev_set_drvdata(pdev, NULL);
1102643a7c43SOndrej Zary 	return 0;
1103643a7c43SOndrej Zary }
1104643a7c43SOndrej Zary 
1105643a7c43SOndrej Zary static struct isa_driver aha1542_isa_driver = {
1106643a7c43SOndrej Zary 	.match		= aha1542_isa_match,
1107643a7c43SOndrej Zary 	.remove		= aha1542_isa_remove,
1108643a7c43SOndrej Zary 	.driver		= {
1109643a7c43SOndrej Zary 		.name	= "aha1542"
1110643a7c43SOndrej Zary 	},
1111643a7c43SOndrej Zary };
1112643a7c43SOndrej Zary static int isa_registered;
1113643a7c43SOndrej Zary 
1114643a7c43SOndrej Zary #ifdef CONFIG_PNP
1115643a7c43SOndrej Zary static struct pnp_device_id aha1542_pnp_ids[] = {
1116643a7c43SOndrej Zary 	{ .id = "ADP1542" },
1117643a7c43SOndrej Zary 	{ .id = "" }
1118643a7c43SOndrej Zary };
1119643a7c43SOndrej Zary MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1120643a7c43SOndrej Zary 
1121643a7c43SOndrej Zary static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1122643a7c43SOndrej Zary {
1123643a7c43SOndrej Zary 	int indx;
1124643a7c43SOndrej Zary 	struct Scsi_Host *sh;
1125643a7c43SOndrej Zary 
1126f71429abSOndrej Zary 	for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1127f71429abSOndrej Zary 		if (io[indx])
1128643a7c43SOndrej Zary 			continue;
1129643a7c43SOndrej Zary 
1130643a7c43SOndrej Zary 		if (pnp_activate_dev(pdev) < 0)
1131643a7c43SOndrej Zary 			continue;
1132643a7c43SOndrej Zary 
1133f71429abSOndrej Zary 		io[indx] = pnp_port_start(pdev, 0);
1134643a7c43SOndrej Zary 
1135643a7c43SOndrej Zary 		/* The card can be queried for its DMA, we have
1136643a7c43SOndrej Zary 		   the DMA set up that is enough */
1137643a7c43SOndrej Zary 
1138f71429abSOndrej Zary 		printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", io[indx]);
1139643a7c43SOndrej Zary 	}
1140643a7c43SOndrej Zary 
1141643a7c43SOndrej Zary 	sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1142643a7c43SOndrej Zary 	if (!sh)
1143643a7c43SOndrej Zary 		return -ENODEV;
1144643a7c43SOndrej Zary 
1145643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, sh);
1146643a7c43SOndrej Zary 	return 0;
1147643a7c43SOndrej Zary }
1148643a7c43SOndrej Zary 
1149643a7c43SOndrej Zary static void aha1542_pnp_remove(struct pnp_dev *pdev)
1150643a7c43SOndrej Zary {
1151643a7c43SOndrej Zary 	aha1542_release(pnp_get_drvdata(pdev));
1152643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, NULL);
1153643a7c43SOndrej Zary }
1154643a7c43SOndrej Zary 
1155643a7c43SOndrej Zary static struct pnp_driver aha1542_pnp_driver = {
1156643a7c43SOndrej Zary 	.name		= "aha1542",
1157643a7c43SOndrej Zary 	.id_table	= aha1542_pnp_ids,
1158643a7c43SOndrej Zary 	.probe		= aha1542_pnp_probe,
1159643a7c43SOndrej Zary 	.remove		= aha1542_pnp_remove,
1160643a7c43SOndrej Zary };
1161643a7c43SOndrej Zary static int pnp_registered;
1162643a7c43SOndrej Zary #endif /* CONFIG_PNP */
1163643a7c43SOndrej Zary 
1164643a7c43SOndrej Zary static int __init aha1542_init(void)
1165643a7c43SOndrej Zary {
1166643a7c43SOndrej Zary 	int ret = 0;
1167643a7c43SOndrej Zary 
1168643a7c43SOndrej Zary #ifdef CONFIG_PNP
1169643a7c43SOndrej Zary 	if (isapnp) {
1170643a7c43SOndrej Zary 		ret = pnp_register_driver(&aha1542_pnp_driver);
1171643a7c43SOndrej Zary 		if (!ret)
1172643a7c43SOndrej Zary 			pnp_registered = 1;
1173643a7c43SOndrej Zary 	}
1174643a7c43SOndrej Zary #endif
1175643a7c43SOndrej Zary 	ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1176643a7c43SOndrej Zary 	if (!ret)
1177643a7c43SOndrej Zary 		isa_registered = 1;
1178643a7c43SOndrej Zary 
1179643a7c43SOndrej Zary #ifdef CONFIG_PNP
1180643a7c43SOndrej Zary 	if (pnp_registered)
1181643a7c43SOndrej Zary 		ret = 0;
1182643a7c43SOndrej Zary #endif
1183643a7c43SOndrej Zary 	if (isa_registered)
1184643a7c43SOndrej Zary 		ret = 0;
1185643a7c43SOndrej Zary 
1186643a7c43SOndrej Zary 	return ret;
1187643a7c43SOndrej Zary }
1188643a7c43SOndrej Zary 
1189643a7c43SOndrej Zary static void __exit aha1542_exit(void)
1190643a7c43SOndrej Zary {
1191643a7c43SOndrej Zary #ifdef CONFIG_PNP
1192643a7c43SOndrej Zary 	if (pnp_registered)
1193643a7c43SOndrej Zary 		pnp_unregister_driver(&aha1542_pnp_driver);
1194643a7c43SOndrej Zary #endif
1195643a7c43SOndrej Zary 	if (isa_registered)
1196643a7c43SOndrej Zary 		isa_unregister_driver(&aha1542_isa_driver);
1197643a7c43SOndrej Zary }
1198643a7c43SOndrej Zary 
1199643a7c43SOndrej Zary module_init(aha1542_init);
1200643a7c43SOndrej Zary module_exit(aha1542_exit);
1201