xref: /linux/drivers/scsi/aha1542.c (revision 1d084d200d2a57219ec2c912c700ddff09f0676c)
1*1d084d20SOndrej Zary /*
2*1d084d20SOndrej Zary  *  Driver for Adaptec AHA-1542 SCSI host adapters
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Tommy Thorn
51da177e4SLinus Torvalds  *  Copyright (C) 1993, 1994, 1995 Eric Youngdale
6*1d084d20SOndrej Zary  *  Copyright (C) 2015 Ondrej Zary
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/interrupt.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/types.h>
131da177e4SLinus Torvalds #include <linux/string.h>
141da177e4SLinus Torvalds #include <linux/ioport.h>
151da177e4SLinus Torvalds #include <linux/delay.h>
161da177e4SLinus Torvalds #include <linux/proc_fs.h>
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/spinlock.h>
19643a7c43SOndrej Zary #include <linux/isa.h>
20643a7c43SOndrej Zary #include <linux/pnp.h>
211da177e4SLinus Torvalds #include <linux/blkdev.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/dma.h>
251da177e4SLinus Torvalds #include <asm/io.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "scsi.h"
281da177e4SLinus Torvalds #include <scsi/scsi_host.h>
291da177e4SLinus Torvalds #include "aha1542.h"
301da177e4SLinus Torvalds #include <linux/stat.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #ifdef DEBUG
331da177e4SLinus Torvalds #define DEB(x) x
341da177e4SLinus Torvalds #else
351da177e4SLinus Torvalds #define DEB(x)
361da177e4SLinus Torvalds #endif
37f71429abSOndrej Zary #define MAXBOARDS 4
381da177e4SLinus Torvalds 
39f71429abSOndrej Zary static bool isapnp = 1;
401da177e4SLinus Torvalds module_param(isapnp, bool, 0);
41f71429abSOndrej Zary MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
42f71429abSOndrej Zary 
43f71429abSOndrej Zary static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
44f71429abSOndrej Zary module_param_array(io, int, NULL, 0);
45f71429abSOndrej Zary MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
46f71429abSOndrej Zary 
47f71429abSOndrej Zary /* time AHA spends on the AT-bus during data transfer */
48f71429abSOndrej Zary static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
49f71429abSOndrej Zary module_param_array(bus_on, int, NULL, 0);
50f71429abSOndrej Zary MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
51f71429abSOndrej Zary 
52f71429abSOndrej Zary /* time AHA spends off the bus (not to monopolize it) during data transfer  */
53f71429abSOndrej Zary static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
54f71429abSOndrej Zary module_param_array(bus_off, int, NULL, 0);
55f71429abSOndrej Zary MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
56f71429abSOndrej Zary 
57f71429abSOndrej Zary /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
58f71429abSOndrej Zary static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
59f71429abSOndrej Zary module_param_array(dma_speed, int, NULL, 0);
60f71429abSOndrej Zary MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #define BIOS_TRANSLATION_6432 1	/* Default case these days */
631da177e4SLinus Torvalds #define BIOS_TRANSLATION_25563 2	/* Big disk case */
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds struct aha1542_hostdata {
661da177e4SLinus Torvalds 	/* This will effectively start both of them at the first mailbox */
671da177e4SLinus Torvalds 	int bios_translation;	/* Mapping bios uses - for compatibility */
681da177e4SLinus Torvalds 	int aha1542_last_mbi_used;
691da177e4SLinus Torvalds 	int aha1542_last_mbo_used;
7055b28f9fSOndrej Zary 	struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES];
711da177e4SLinus Torvalds 	struct mailbox mb[2 * AHA1542_MAILBOXES];
721da177e4SLinus Torvalds 	struct ccb ccb[AHA1542_MAILBOXES];
731da177e4SLinus Torvalds };
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static DEFINE_SPINLOCK(aha1542_lock);
761da177e4SLinus Torvalds 
77f1bbef63SOndrej Zary static inline void aha1542_intr_reset(u16 base)
78f1bbef63SOndrej Zary {
79f1bbef63SOndrej Zary 	outb(IRST, CONTROL(base));
80f1bbef63SOndrej Zary }
811da177e4SLinus Torvalds 
822093bfa1SOndrej Zary static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
832093bfa1SOndrej Zary {
842093bfa1SOndrej Zary 	bool delayed = true;
852093bfa1SOndrej Zary 
862093bfa1SOndrej Zary 	if (timeout == 0) {
872093bfa1SOndrej Zary 		timeout = 3000000;
882093bfa1SOndrej Zary 		delayed = false;
891da177e4SLinus Torvalds 	}
901da177e4SLinus Torvalds 
912093bfa1SOndrej Zary 	while (1) {
922093bfa1SOndrej Zary 		u8 bits = inb(port) & mask;
932093bfa1SOndrej Zary 		if ((bits & allof) == allof && ((bits & noneof) == 0))
942093bfa1SOndrej Zary 			break;
952093bfa1SOndrej Zary 		if (delayed)
962093bfa1SOndrej Zary 			mdelay(1);
972093bfa1SOndrej Zary 		if (--timeout == 0)
982093bfa1SOndrej Zary 			return false;
992093bfa1SOndrej Zary 	}
1002093bfa1SOndrej Zary 
1012093bfa1SOndrej Zary 	return true;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /* This is a bit complicated, but we need to make sure that an interrupt
1051da177e4SLinus Torvalds    routine does not send something out while we are in the middle of this.
1061da177e4SLinus Torvalds    Fortunately, it is only at boot time that multi-byte messages
1071da177e4SLinus Torvalds    are ever sent. */
108cad2fc72SOndrej Zary static int aha1542_outb(unsigned int base, u8 val)
1091da177e4SLinus Torvalds {
1100c2b6481SOndrej Zary 	unsigned long flags;
1111da177e4SLinus Torvalds 
1120c2b6481SOndrej Zary 	while (1) {
1130c2b6481SOndrej Zary 		if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
1140c2b6481SOndrej Zary 			printk(KERN_ERR "aha1542_outb failed");
1150c2b6481SOndrej Zary 			return 1;
1160c2b6481SOndrej Zary 		}
1171da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
1181da177e4SLinus Torvalds 		if (inb(STATUS(base)) & CDF) {
1191da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1201da177e4SLinus Torvalds 			continue;
1211da177e4SLinus Torvalds 		}
122cad2fc72SOndrej Zary 		outb(val, DATA(base));
1231da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
1241da177e4SLinus Torvalds 		return 0;
1251da177e4SLinus Torvalds 	}
1260c2b6481SOndrej Zary }
1270c2b6481SOndrej Zary 
128cad2fc72SOndrej Zary static int aha1542_out(unsigned int base, u8 *buf, int len)
1290c2b6481SOndrej Zary {
1300c2b6481SOndrej Zary 	unsigned long flags;
1310c2b6481SOndrej Zary 
1321da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
1331da177e4SLinus Torvalds 	while (len--) {
1340c2b6481SOndrej Zary 		if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
1351da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1361da177e4SLinus Torvalds 			printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
1371da177e4SLinus Torvalds 			return 1;
1381da177e4SLinus Torvalds 		}
139cad2fc72SOndrej Zary 		outb(*buf++, DATA(base));
1400c2b6481SOndrej Zary 	}
1410c2b6481SOndrej Zary 	spin_unlock_irqrestore(&aha1542_lock, flags);
14223e6940aSOndrej Zary 	if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
14323e6940aSOndrej Zary 		return 1;
1440c2b6481SOndrej Zary 
1450c2b6481SOndrej Zary 	return 0;
1460c2b6481SOndrej Zary }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds /* Only used at boot time, so we do not need to worry about latency as much
1491da177e4SLinus Torvalds    here */
1501da177e4SLinus Torvalds 
151cad2fc72SOndrej Zary static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	unsigned long flags;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
1561da177e4SLinus Torvalds 	while (len--) {
157a13b3722SOndrej Zary 		if (!wait_mask(STATUS(base), DF, DF, 0, timeout)) {
1581da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
159f8846be3SOndrej Zary 			if (timeout == 0)
1601da177e4SLinus Torvalds 				printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
1611da177e4SLinus Torvalds 			return 1;
1621da177e4SLinus Torvalds 		}
163cad2fc72SOndrej Zary 		*buf++ = inb(DATA(base));
164a13b3722SOndrej Zary 	}
165a13b3722SOndrej Zary 	spin_unlock_irqrestore(&aha1542_lock, flags);
166a13b3722SOndrej Zary 	return 0;
167a13b3722SOndrej Zary }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds static int makecode(unsigned hosterr, unsigned scsierr)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	switch (hosterr) {
1721da177e4SLinus Torvalds 	case 0x0:
1731da177e4SLinus Torvalds 	case 0xa:		/* Linked command complete without error and linked normally */
1741da177e4SLinus Torvalds 	case 0xb:		/* Linked command complete without error, interrupt generated */
1751da177e4SLinus Torvalds 		hosterr = 0;
1761da177e4SLinus Torvalds 		break;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	case 0x11:		/* Selection time out-The initiator selection or target
1791da177e4SLinus Torvalds 				   reselection was not complete within the SCSI Time out period */
1801da177e4SLinus Torvalds 		hosterr = DID_TIME_OUT;
1811da177e4SLinus Torvalds 		break;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
1841da177e4SLinus Torvalds 				   than was allocated by the Data Length field or the sum of the
1851da177e4SLinus Torvalds 				   Scatter / Gather Data Length fields. */
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	case 0x15:		/* MBO command was not 00, 01 or 02-The first byte of the CB was
1901da177e4SLinus Torvalds 				   invalid. This usually indicates a software failure. */
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	case 0x16:		/* Invalid CCB Operation Code-The first byte of the CCB was invalid.
1931da177e4SLinus Torvalds 				   This usually indicates a software failure. */
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	case 0x17:		/* Linked CCB does not have the same LUN-A subsequent CCB of a set
1961da177e4SLinus Torvalds 				   of linked CCB's does not specify the same logical unit number as
1971da177e4SLinus Torvalds 				   the first. */
1981da177e4SLinus Torvalds 	case 0x18:		/* Invalid Target Direction received from Host-The direction of a
1991da177e4SLinus Torvalds 				   Target Mode CCB was invalid. */
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	case 0x19:		/* Duplicate CCB Received in Target Mode-More than once CCB was
2021da177e4SLinus Torvalds 				   received to service data transfer between the same target LUN
2031da177e4SLinus Torvalds 				   and initiator SCSI ID in the same direction. */
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	case 0x1a:		/* Invalid CCB or Segment List Parameter-A segment list with a zero
2061da177e4SLinus Torvalds 				   length segment or invalid segment list boundaries was received.
2071da177e4SLinus Torvalds 				   A CCB parameter was invalid. */
2081da177e4SLinus Torvalds 		DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
2091da177e4SLinus Torvalds 		hosterr = DID_ERROR;	/* Couldn't find any better */
2101da177e4SLinus Torvalds 		break;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
2131da177e4SLinus Torvalds 				   phase sequence was requested by the target. The host adapter
2141da177e4SLinus Torvalds 				   will generate a SCSI Reset Condition, notifying the host with
2151da177e4SLinus Torvalds 				   a SCRD interrupt */
2161da177e4SLinus Torvalds 		hosterr = DID_RESET;
2171da177e4SLinus Torvalds 		break;
2181da177e4SLinus Torvalds 	default:
2191da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
2201da177e4SLinus Torvalds 		break;
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 	return scsierr | (hosterr << 16);
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
225c2532f68SOndrej Zary static int aha1542_test_port(int bse, struct Scsi_Host *sh)
2261da177e4SLinus Torvalds {
227cb5b570cSOndrej Zary 	u8 inquiry_result[4];
228cad2fc72SOndrej Zary 	int i;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	/* Quick and dirty test for presence of the card. */
2311da177e4SLinus Torvalds 	if (inb(STATUS(bse)) == 0xff)
2321da177e4SLinus Torvalds 		return 0;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	/* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/* In case some other card was probing here, reset interrupts */
2371da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	mdelay(20);		/* Wait a little bit for things to settle down. */
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/* Expect INIT and IDLE, any of the others are bad */
2442093bfa1SOndrej Zary 	if (!wait_mask(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
245a13b3722SOndrej Zary 		return 0;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/* Shouldn't have generated any interrupts during reset */
2481da177e4SLinus Torvalds 	if (inb(INTRFLAGS(bse)) & INTRMASK)
249a13b3722SOndrej Zary 		return 0;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	/* Perform a host adapter inquiry instead so we do not need to set
2521da177e4SLinus Torvalds 	   up the mailboxes ahead of time */
2531da177e4SLinus Torvalds 
2540c2b6481SOndrej Zary 	aha1542_outb(bse, CMD_INQUIRY);
2551da177e4SLinus Torvalds 
256cad2fc72SOndrej Zary 	for (i = 0; i < 4; i++) {
2572093bfa1SOndrej Zary 		if (!wait_mask(STATUS(bse), DF, DF, 0, 0))
258a13b3722SOndrej Zary 			return 0;
259cad2fc72SOndrej Zary 		inquiry_result[i] = inb(DATA(bse));
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	/* Reading port should reset DF */
2631da177e4SLinus Torvalds 	if (inb(STATUS(bse)) & DF)
264a13b3722SOndrej Zary 		return 0;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	/* When HACC, command is completed, and we're though testing */
2672093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(bse), HACC, HACC, 0, 0))
268a13b3722SOndrej Zary 		return 0;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	/* Clear interrupts */
2711da177e4SLinus Torvalds 	outb(IRST, CONTROL(bse));
2721da177e4SLinus Torvalds 
273bdebe224SOndrej Zary 	return 1;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds /* A "high" level interrupt handler */
277c2532f68SOndrej Zary static void aha1542_intr_handle(struct Scsi_Host *sh)
2781da177e4SLinus Torvalds {
279c2532f68SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sh);
28055b28f9fSOndrej Zary 	void (*my_done)(struct scsi_cmnd *) = NULL;
2811da177e4SLinus Torvalds 	int errstatus, mbi, mbo, mbistatus;
2821da177e4SLinus Torvalds 	int number_serviced;
2831da177e4SLinus Torvalds 	unsigned long flags;
28455b28f9fSOndrej Zary 	struct scsi_cmnd *tmp_cmd;
2851da177e4SLinus Torvalds 	int flag;
286e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
287e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds #ifdef DEBUG
2901da177e4SLinus Torvalds 	{
291c2532f68SOndrej Zary 		flag = inb(INTRFLAGS(sh->io_port));
2921da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_intr_handle: ");
2931da177e4SLinus Torvalds 		if (!(flag & ANYINTR))
2941da177e4SLinus Torvalds 			printk("no interrupt?");
2951da177e4SLinus Torvalds 		if (flag & MBIF)
2961da177e4SLinus Torvalds 			printk("MBIF ");
2971da177e4SLinus Torvalds 		if (flag & MBOA)
2981da177e4SLinus Torvalds 			printk("MBOF ");
2991da177e4SLinus Torvalds 		if (flag & HACC)
3001da177e4SLinus Torvalds 			printk("HACC ");
3011da177e4SLinus Torvalds 		if (flag & SCRD)
3021da177e4SLinus Torvalds 			printk("SCRD ");
303c2532f68SOndrej Zary 		printk("status %02x\n", inb(STATUS(sh->io_port)));
3041da177e4SLinus Torvalds 	};
3051da177e4SLinus Torvalds #endif
3061da177e4SLinus Torvalds 	number_serviced = 0;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	while (1 == 1) {
309c2532f68SOndrej Zary 		flag = inb(INTRFLAGS(sh->io_port));
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 		/* Check for unusual interrupts.  If any of these happen, we should
3121da177e4SLinus Torvalds 		   probably do something special, but for now just printing a message
3131da177e4SLinus Torvalds 		   is sufficient.  A SCSI reset detected is something that we really
3141da177e4SLinus Torvalds 		   need to deal with in some way. */
3151da177e4SLinus Torvalds 		if (flag & ~MBIF) {
3161da177e4SLinus Torvalds 			if (flag & MBOA)
3171da177e4SLinus Torvalds 				printk("MBOF ");
3181da177e4SLinus Torvalds 			if (flag & HACC)
3191da177e4SLinus Torvalds 				printk("HACC ");
320dfd7c991SOndrej Zary 			if (flag & SCRD)
3211da177e4SLinus Torvalds 				printk("SCRD ");
3221da177e4SLinus Torvalds 		}
323c2532f68SOndrej Zary 		aha1542_intr_reset(sh->io_port);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
326e98878f7SOndrej Zary 		mbi = aha1542->aha1542_last_mbi_used + 1;
3271da177e4SLinus Torvalds 		if (mbi >= 2 * AHA1542_MAILBOXES)
3281da177e4SLinus Torvalds 			mbi = AHA1542_MAILBOXES;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 		do {
3311da177e4SLinus Torvalds 			if (mb[mbi].status != 0)
3321da177e4SLinus Torvalds 				break;
3331da177e4SLinus Torvalds 			mbi++;
3341da177e4SLinus Torvalds 			if (mbi >= 2 * AHA1542_MAILBOXES)
3351da177e4SLinus Torvalds 				mbi = AHA1542_MAILBOXES;
336e98878f7SOndrej Zary 		} while (mbi != aha1542->aha1542_last_mbi_used);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 		if (mb[mbi].status == 0) {
3391da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
3401da177e4SLinus Torvalds 			/* Hmm, no mail.  Must have read it the last time around */
341dfd7c991SOndrej Zary 			if (!number_serviced)
3421da177e4SLinus Torvalds 				printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
3431da177e4SLinus Torvalds 			return;
3441da177e4SLinus Torvalds 		};
3451da177e4SLinus Torvalds 
34610be6250SOndrej Zary 		mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
3471da177e4SLinus Torvalds 		mbistatus = mb[mbi].status;
3481da177e4SLinus Torvalds 		mb[mbi].status = 0;
349e98878f7SOndrej Zary 		aha1542->aha1542_last_mbi_used = mbi;
3501da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds #ifdef DEBUG
3531da177e4SLinus Torvalds 		{
3541da177e4SLinus Torvalds 			if (ccb[mbo].tarstat | ccb[mbo].hastat)
3551da177e4SLinus Torvalds 				printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
3561da177e4SLinus Torvalds 				       ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
3571da177e4SLinus Torvalds 		};
3581da177e4SLinus Torvalds #endif
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 		if (mbistatus == 3)
3611da177e4SLinus Torvalds 			continue;	/* Aborted command not found */
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds #ifdef DEBUG
3641da177e4SLinus Torvalds 		printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
3651da177e4SLinus Torvalds #endif
3661da177e4SLinus Torvalds 
36755b28f9fSOndrej Zary 		tmp_cmd = aha1542->int_cmds[mbo];
3681da177e4SLinus Torvalds 
36955b28f9fSOndrej Zary 		if (!tmp_cmd || !tmp_cmd->scsi_done) {
3701da177e4SLinus Torvalds 			printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
3711da177e4SLinus Torvalds 			printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
3721da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].idlun, mbo);
3731da177e4SLinus Torvalds 			return;
3741da177e4SLinus Torvalds 		}
37555b28f9fSOndrej Zary 		my_done = tmp_cmd->scsi_done;
37655b28f9fSOndrej Zary 		kfree(tmp_cmd->host_scribble);
37755b28f9fSOndrej Zary 		tmp_cmd->host_scribble = NULL;
3781da177e4SLinus Torvalds 		/* Fetch the sense data, and tuck it away, in the required slot.  The
3791da177e4SLinus Torvalds 		   Adaptec automatically fetches it, and there is no guarantee that
3801da177e4SLinus Torvalds 		   we will still have it in the cdb when we come back */
3811da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2)
38255b28f9fSOndrej Zary 			memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
383b80ca4f7SFUJITA Tomonori 			       SCSI_SENSE_BUFFERSIZE);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		/* is there mail :-) */
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 		/* more error checking left out here */
3891da177e4SLinus Torvalds 		if (mbistatus != 1)
3901da177e4SLinus Torvalds 			/* This is surely wrong, but I don't know what's right */
3911da177e4SLinus Torvalds 			errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
3921da177e4SLinus Torvalds 		else
3931da177e4SLinus Torvalds 			errstatus = 0;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds #ifdef DEBUG
3961da177e4SLinus Torvalds 		if (errstatus)
3971da177e4SLinus Torvalds 			printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
3981da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].tarstat);
3991da177e4SLinus Torvalds #endif
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2) {
4021da177e4SLinus Torvalds #ifdef DEBUG
4031da177e4SLinus Torvalds 			int i;
4041da177e4SLinus Torvalds #endif
4051da177e4SLinus Torvalds 			DEB(printk("aha1542_intr_handle: sense:"));
4061da177e4SLinus Torvalds #ifdef DEBUG
4071da177e4SLinus Torvalds 			for (i = 0; i < 12; i++)
4081da177e4SLinus Torvalds 				printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
4091da177e4SLinus Torvalds 			printk("\n");
4101da177e4SLinus Torvalds #endif
4111da177e4SLinus Torvalds 			/*
4121da177e4SLinus Torvalds 			   DEB(printk("aha1542_intr_handle: buf:"));
4131da177e4SLinus Torvalds 			   for (i = 0; i < bufflen; i++)
4141da177e4SLinus Torvalds 			   printk("%02x ", ((unchar *)buff)[i]);
4151da177e4SLinus Torvalds 			   printk("\n");
4161da177e4SLinus Torvalds 			 */
4171da177e4SLinus Torvalds 		}
4181da177e4SLinus Torvalds 		DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
41955b28f9fSOndrej Zary 		tmp_cmd->result = errstatus;
42055b28f9fSOndrej Zary 		aha1542->int_cmds[mbo] = NULL;	/* This effectively frees up the mailbox slot, as
4211da177e4SLinus Torvalds 						   far as queuecommand is concerned */
42255b28f9fSOndrej Zary 		my_done(tmp_cmd);
4231da177e4SLinus Torvalds 		number_serviced++;
4241da177e4SLinus Torvalds 	};
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
42709a44833SOndrej Zary /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
42809a44833SOndrej Zary static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
42909a44833SOndrej Zary {
43009a44833SOndrej Zary 	unsigned long flags;
431c2532f68SOndrej Zary 	struct Scsi_Host *sh = dev_id;
43209a44833SOndrej Zary 
433c2532f68SOndrej Zary 	spin_lock_irqsave(sh->host_lock, flags);
434c2532f68SOndrej Zary 	aha1542_intr_handle(sh);
435c2532f68SOndrej Zary 	spin_unlock_irqrestore(sh->host_lock, flags);
43609a44833SOndrej Zary 	return IRQ_HANDLED;
43709a44833SOndrej Zary }
43809a44833SOndrej Zary 
43955b28f9fSOndrej Zary static int aha1542_queuecommand_lck(struct scsi_cmnd *cmd, void (*done) (struct scsi_cmnd *))
4401da177e4SLinus Torvalds {
44155b28f9fSOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
442cb5b570cSOndrej Zary 	u8 direction;
44355b28f9fSOndrej Zary 	u8 target = cmd->device->id;
44455b28f9fSOndrej Zary 	u8 lun = cmd->device->lun;
4451da177e4SLinus Torvalds 	unsigned long flags;
44655b28f9fSOndrej Zary 	int bufflen = scsi_bufflen(cmd);
4471da177e4SLinus Torvalds 	int mbo;
448e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
449e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	DEB(int i);
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	DEB(if (target > 1) {
45455b28f9fSOndrej Zary 	    cmd->result = DID_TIME_OUT << 16;
45555b28f9fSOndrej Zary 	    done(cmd); return 0;
4561da177e4SLinus Torvalds 	    }
4571da177e4SLinus Torvalds 	);
4581da177e4SLinus Torvalds 
45955b28f9fSOndrej Zary 	if (*cmd->cmnd == REQUEST_SENSE) {
4601da177e4SLinus Torvalds 		/* Don't do the command - we have the sense data already */
46155b28f9fSOndrej Zary 		cmd->result = 0;
46255b28f9fSOndrej Zary 		done(cmd);
4631da177e4SLinus Torvalds 		return 0;
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds #ifdef DEBUG
46655b28f9fSOndrej Zary 	if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
46755b28f9fSOndrej Zary 		i = xscsi2int(cmd->cmnd + 2);
46855b28f9fSOndrej Zary 	else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
46955b28f9fSOndrej Zary 		i = scsi2int(cmd->cmnd + 2);
4701da177e4SLinus Torvalds 	else
4711da177e4SLinus Torvalds 		i = -1;
4721da177e4SLinus Torvalds 	if (done)
47355b28f9fSOndrej Zary 		printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen);
4741da177e4SLinus Torvalds 	else
47555b28f9fSOndrej Zary 		printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen);
4761da177e4SLinus Torvalds 	printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
47755b28f9fSOndrej Zary 	for (i = 0; i < cmd->cmd_len; i++)
47855b28f9fSOndrej Zary 		printk("%02x ", cmd->cmnd[i]);
4791da177e4SLinus Torvalds 	printk("\n");
48055b28f9fSOndrej Zary 	if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
4811da177e4SLinus Torvalds 		return 0;	/* we are still testing, so *don't* write */
4821da177e4SLinus Torvalds #endif
4831da177e4SLinus Torvalds 	/* Use the outgoing mailboxes in a round-robin fashion, because this
4841da177e4SLinus Torvalds 	   is how the host adapter will scan for them */
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
487e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
4881da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
4891da177e4SLinus Torvalds 		mbo = 0;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	do {
49255b28f9fSOndrej Zary 		if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
4931da177e4SLinus Torvalds 			break;
4941da177e4SLinus Torvalds 		mbo++;
4951da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
4961da177e4SLinus Torvalds 			mbo = 0;
497e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
4981da177e4SLinus Torvalds 
49955b28f9fSOndrej Zary 	if (mb[mbo].status || aha1542->int_cmds[mbo])
5001da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
5011da177e4SLinus Torvalds 
50255b28f9fSOndrej Zary 	aha1542->int_cmds[mbo] = cmd;	/* This will effectively prevent someone else from
5031da177e4SLinus Torvalds 					   screwing with this cdb. */
5041da177e4SLinus Torvalds 
505e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
5061da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds #ifdef DEBUG
5091da177e4SLinus Torvalds 	printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
5101da177e4SLinus Torvalds #endif
5111da177e4SLinus Torvalds 
51210be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
5151da177e4SLinus Torvalds 
51655b28f9fSOndrej Zary 	ccb[mbo].cdblen = cmd->cmd_len;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	direction = 0;
51955b28f9fSOndrej Zary 	if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
5201da177e4SLinus Torvalds 		direction = 8;
52155b28f9fSOndrej Zary 	else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
5221da177e4SLinus Torvalds 		direction = 16;
5231da177e4SLinus Torvalds 
52455b28f9fSOndrej Zary 	memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
5251da177e4SLinus Torvalds 
526fc3fdfccSBoaz Harrosh 	if (bufflen) {
52751cf2249SJens Axboe 		struct scatterlist *sg;
5281da177e4SLinus Torvalds 		struct chain *cptr;
5291da177e4SLinus Torvalds #ifdef DEBUG
5301da177e4SLinus Torvalds 		unsigned char *ptr;
5311da177e4SLinus Torvalds #endif
53255b28f9fSOndrej Zary 		int i, sg_count = scsi_sg_count(cmd);
5331da177e4SLinus Torvalds 		ccb[mbo].op = 2;	/* SCSI Initiator Command  w/scatter-gather */
53455b28f9fSOndrej Zary 		cmd->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
535fc3fdfccSBoaz Harrosh 		                                         GFP_KERNEL | GFP_DMA);
53655b28f9fSOndrej Zary 		cptr = (struct chain *) cmd->host_scribble;
5371da177e4SLinus Torvalds 		if (cptr == NULL) {
5381da177e4SLinus Torvalds 			/* free the claimed mailbox slot */
53955b28f9fSOndrej Zary 			aha1542->int_cmds[mbo] = NULL;
5401da177e4SLinus Torvalds 			return SCSI_MLQUEUE_HOST_BUSY;
5411da177e4SLinus Torvalds 		}
54255b28f9fSOndrej Zary 		scsi_for_each_sg(cmd, sg, sg_count, i) {
54310be6250SOndrej Zary 			any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
54410be6250SOndrej Zary 								+ sg->offset);
54551cf2249SJens Axboe 			any2scsi(cptr[i].datalen, sg->length);
5461da177e4SLinus Torvalds 		};
547fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
54810be6250SOndrej Zary 		any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
5491da177e4SLinus Torvalds #ifdef DEBUG
5501da177e4SLinus Torvalds 		printk("cptr %x: ", cptr);
5511da177e4SLinus Torvalds 		ptr = (unsigned char *) cptr;
5521da177e4SLinus Torvalds 		for (i = 0; i < 18; i++)
5531da177e4SLinus Torvalds 			printk("%02x ", ptr[i]);
5541da177e4SLinus Torvalds #endif
5551da177e4SLinus Torvalds 	} else {
5561da177e4SLinus Torvalds 		ccb[mbo].op = 0;	/* SCSI Initiator Command */
55755b28f9fSOndrej Zary 		cmd->host_scribble = NULL;
558fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, 0);
559fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].dataptr, 0);
5601da177e4SLinus Torvalds 	};
5611da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);	/*SCSI Target Id */
5621da177e4SLinus Torvalds 	ccb[mbo].rsalen = 16;
5631da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
5641da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds #ifdef DEBUG
5671da177e4SLinus Torvalds 	{
5681da177e4SLinus Torvalds 		int i;
5691da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_command: sending.. ");
5701da177e4SLinus Torvalds 		for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
571cb5b570cSOndrej Zary 			printk("%02x ", ((u8 *) &ccb[mbo])[i]);
5721da177e4SLinus Torvalds 	};
5731da177e4SLinus Torvalds #endif
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	if (done) {
576f232d538SOndrej Zary 		DEB(printk("aha1542_queuecommand: now waiting for interrupt "));
57755b28f9fSOndrej Zary 		cmd->scsi_done = done;
5781da177e4SLinus Torvalds 		mb[mbo].status = 1;
57955b28f9fSOndrej Zary 		aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
5801da177e4SLinus Torvalds 	} else
5811da177e4SLinus Torvalds 		printk("aha1542_queuecommand: done can't be NULL\n");
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	return 0;
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
586f281233dSJeff Garzik static DEF_SCSI_QCMD(aha1542_queuecommand)
587f281233dSJeff Garzik 
5881da177e4SLinus Torvalds /* Initialize mailboxes */
589c2532f68SOndrej Zary static void setup_mailboxes(int bse, struct Scsi_Host *sh)
5901da177e4SLinus Torvalds {
591c2532f68SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sh);
5921da177e4SLinus Torvalds 	int i;
593e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
594e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
5951da177e4SLinus Torvalds 
596cad2fc72SOndrej Zary 	u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
5991da177e4SLinus Torvalds 		mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
60010be6250SOndrej Zary 		any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
6011da177e4SLinus Torvalds 	};
6021da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
603cad2fc72SOndrej Zary 	any2scsi((mb_cmd + 2), isa_virt_to_bus(mb));
604cad2fc72SOndrej Zary 	if (aha1542_out(bse, mb_cmd, 5))
6051da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
6061da177e4SLinus Torvalds 	aha1542_intr_reset(bse);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6093a70c006SOndrej Zary static int aha1542_getconfig(int base_io, unsigned int *irq_level, unsigned char *dma_chan, unsigned int *scsi_id)
6101da177e4SLinus Torvalds {
611cb5b570cSOndrej Zary 	u8 inquiry_result[3];
6121da177e4SLinus Torvalds 	int i;
6131da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
6141da177e4SLinus Torvalds 	if (i & DF) {
6151da177e4SLinus Torvalds 		i = inb(DATA(base_io));
6161da177e4SLinus Torvalds 	};
6170c2b6481SOndrej Zary 	aha1542_outb(base_io, CMD_RETCONF);
618f8846be3SOndrej Zary 	aha1542_in(base_io, inquiry_result, 3, 0);
6192093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
6201da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query board settings\n");
6211da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
6221da177e4SLinus Torvalds 	switch (inquiry_result[0]) {
6231da177e4SLinus Torvalds 	case 0x80:
6241da177e4SLinus Torvalds 		*dma_chan = 7;
6251da177e4SLinus Torvalds 		break;
6261da177e4SLinus Torvalds 	case 0x40:
6271da177e4SLinus Torvalds 		*dma_chan = 6;
6281da177e4SLinus Torvalds 		break;
6291da177e4SLinus Torvalds 	case 0x20:
6301da177e4SLinus Torvalds 		*dma_chan = 5;
6311da177e4SLinus Torvalds 		break;
6321da177e4SLinus Torvalds 	case 0x01:
6331da177e4SLinus Torvalds 		*dma_chan = 0;
6341da177e4SLinus Torvalds 		break;
6351da177e4SLinus Torvalds 	case 0:
6361da177e4SLinus Torvalds 		/* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
6371da177e4SLinus Torvalds 		   Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
6381da177e4SLinus Torvalds 		*dma_chan = 0xFF;
6391da177e4SLinus Torvalds 		break;
6401da177e4SLinus Torvalds 	default:
6411da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec DMA priority.  Disabling board\n");
6421da177e4SLinus Torvalds 		return -1;
6431da177e4SLinus Torvalds 	};
6441da177e4SLinus Torvalds 	switch (inquiry_result[1]) {
6451da177e4SLinus Torvalds 	case 0x40:
6461da177e4SLinus Torvalds 		*irq_level = 15;
6471da177e4SLinus Torvalds 		break;
6481da177e4SLinus Torvalds 	case 0x20:
6491da177e4SLinus Torvalds 		*irq_level = 14;
6501da177e4SLinus Torvalds 		break;
6511da177e4SLinus Torvalds 	case 0x8:
6521da177e4SLinus Torvalds 		*irq_level = 12;
6531da177e4SLinus Torvalds 		break;
6541da177e4SLinus Torvalds 	case 0x4:
6551da177e4SLinus Torvalds 		*irq_level = 11;
6561da177e4SLinus Torvalds 		break;
6571da177e4SLinus Torvalds 	case 0x2:
6581da177e4SLinus Torvalds 		*irq_level = 10;
6591da177e4SLinus Torvalds 		break;
6601da177e4SLinus Torvalds 	case 0x1:
6611da177e4SLinus Torvalds 		*irq_level = 9;
6621da177e4SLinus Torvalds 		break;
6631da177e4SLinus Torvalds 	default:
6641da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec IRQ level.  Disabling board\n");
6651da177e4SLinus Torvalds 		return -1;
6661da177e4SLinus Torvalds 	};
6671da177e4SLinus Torvalds 	*scsi_id = inquiry_result[2] & 7;
6681da177e4SLinus Torvalds 	return 0;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds /* This function should only be called for 1542C boards - we can detect
6721da177e4SLinus Torvalds    the special firmware settings and unlock the board */
6731da177e4SLinus Torvalds 
674643a7c43SOndrej Zary static int aha1542_mbenable(int base)
6751da177e4SLinus Torvalds {
676cb5b570cSOndrej Zary 	static u8 mbenable_cmd[3];
677cb5b570cSOndrej Zary 	static u8 mbenable_result[2];
6781da177e4SLinus Torvalds 	int retval;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	retval = BIOS_TRANSLATION_6432;
6811da177e4SLinus Torvalds 
6820c2b6481SOndrej Zary 	aha1542_outb(base, CMD_EXTBIOS);
683f8846be3SOndrej Zary 	if (aha1542_in(base, mbenable_result, 2, 100))
6841da177e4SLinus Torvalds 		return retval;
6852093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 100))
6862093bfa1SOndrej Zary 		goto fail;
6871da177e4SLinus Torvalds 	aha1542_intr_reset(base);
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
6901da177e4SLinus Torvalds 		mbenable_cmd[0] = CMD_MBENABLE;
6911da177e4SLinus Torvalds 		mbenable_cmd[1] = 0;
6921da177e4SLinus Torvalds 		mbenable_cmd[2] = mbenable_result[1];
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 		if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
6951da177e4SLinus Torvalds 			retval = BIOS_TRANSLATION_25563;
6961da177e4SLinus Torvalds 
69723e6940aSOndrej Zary 		if (aha1542_out(base, mbenable_cmd, 3))
6982093bfa1SOndrej Zary 			goto fail;
6991da177e4SLinus Torvalds 	};
7001da177e4SLinus Torvalds 	while (0) {
7011da177e4SLinus Torvalds fail:
7021da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 	aha1542_intr_reset(base);
7051da177e4SLinus Torvalds 	return retval;
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
709643a7c43SOndrej Zary static int aha1542_query(int base_io, int *transl)
7101da177e4SLinus Torvalds {
711cb5b570cSOndrej Zary 	u8 inquiry_result[4];
7121da177e4SLinus Torvalds 	int i;
7131da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
7141da177e4SLinus Torvalds 	if (i & DF) {
7151da177e4SLinus Torvalds 		i = inb(DATA(base_io));
7161da177e4SLinus Torvalds 	};
7170c2b6481SOndrej Zary 	aha1542_outb(base_io, CMD_INQUIRY);
718f8846be3SOndrej Zary 	aha1542_in(base_io, inquiry_result, 4, 0);
7192093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
7201da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query card type\n");
7211da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	*transl = BIOS_TRANSLATION_6432;	/* Default case */
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	/* For an AHA1740 series board, we ignore the board since there is a
7261da177e4SLinus Torvalds 	   hardware bug which can lead to wrong blocks being returned if the board
7271da177e4SLinus Torvalds 	   is operating in the 1542 emulation mode.  Since there is an extended mode
7281da177e4SLinus Torvalds 	   driver, we simply ignore the board and let the 1740 driver pick it up.
7291da177e4SLinus Torvalds 	 */
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	if (inquiry_result[0] == 0x43) {
7321da177e4SLinus Torvalds 		printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
7331da177e4SLinus Torvalds 		return 1;
7341da177e4SLinus Torvalds 	};
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	/* Always call this - boards that do not support extended bios translation
7371da177e4SLinus Torvalds 	   will ignore the command, and we will set the proper default */
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	*transl = aha1542_mbenable(base_io);
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	return 0;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744f71429abSOndrej Zary static u8 dma_speed_hw(int dma_speed)
7451da177e4SLinus Torvalds {
746f71429abSOndrej Zary 	switch (dma_speed) {
7471da177e4SLinus Torvalds 	case 5:
748f71429abSOndrej Zary 		return 0x00;
7491da177e4SLinus Torvalds 	case 6:
750f71429abSOndrej Zary 		return 0x04;
7511da177e4SLinus Torvalds 	case 7:
752f71429abSOndrej Zary 		return 0x01;
7531da177e4SLinus Torvalds 	case 8:
754f71429abSOndrej Zary 		return 0x02;
7551da177e4SLinus Torvalds 	case 10:
756f71429abSOndrej Zary 		return 0x03;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
759f71429abSOndrej Zary 	return 0xff;	/* invalid */
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
762b847fd0dSOndrej Zary /* Set the Bus on/off-times as not to ruin floppy performance */
763b847fd0dSOndrej Zary static void aha1542_set_bus_times(int indx)
764b847fd0dSOndrej Zary {
765f71429abSOndrej Zary 	unsigned int base_io = io[indx];
766b847fd0dSOndrej Zary 
767f71429abSOndrej Zary 	if (bus_on[indx] > 0) {
768f71429abSOndrej Zary 		u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on[indx], 2, 15) };
769f71429abSOndrej Zary 
770b847fd0dSOndrej Zary 		aha1542_intr_reset(base_io);
77123e6940aSOndrej Zary 		if (aha1542_out(base_io, oncmd, 2))
772b847fd0dSOndrej Zary 			goto fail;
773f71429abSOndrej Zary 	}
774f71429abSOndrej Zary 
775f71429abSOndrej Zary 	if (bus_off[indx] > 0) {
776f71429abSOndrej Zary 		u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off[indx], 1, 64) };
777f71429abSOndrej Zary 
778b847fd0dSOndrej Zary 		aha1542_intr_reset(base_io);
77923e6940aSOndrej Zary 		if (aha1542_out(base_io, offcmd, 2))
780b847fd0dSOndrej Zary 			goto fail;
781f71429abSOndrej Zary 	}
782f71429abSOndrej Zary 
783f71429abSOndrej Zary 	if (dma_speed_hw(dma_speed[indx]) != 0xff) {
784f71429abSOndrej Zary 		u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed[indx]) };
785f71429abSOndrej Zary 
786b847fd0dSOndrej Zary 		aha1542_intr_reset(base_io);
78723e6940aSOndrej Zary 		if (aha1542_out(base_io, dmacmd, 2))
788b847fd0dSOndrej Zary 			goto fail;
789b847fd0dSOndrej Zary 	}
790b847fd0dSOndrej Zary 	aha1542_intr_reset(base_io);
791b847fd0dSOndrej Zary 	return;
792b847fd0dSOndrej Zary fail:
793b847fd0dSOndrej Zary 	printk(KERN_ERR "setting bus on/off-time failed\n");
794b847fd0dSOndrej Zary 	aha1542_intr_reset(base_io);
795b847fd0dSOndrej Zary }
796b847fd0dSOndrej Zary 
7971da177e4SLinus Torvalds /* return non-zero on detection */
798643a7c43SOndrej Zary static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
7991da177e4SLinus Torvalds {
800f71429abSOndrej Zary 	unsigned int base_io = io[indx];
801c2532f68SOndrej Zary 	struct Scsi_Host *sh;
802e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542;
8031da177e4SLinus Torvalds 
8043a70c006SOndrej Zary 	if (base_io == 0)
805643a7c43SOndrej Zary 		return NULL;
8061da177e4SLinus Torvalds 
8073a70c006SOndrej Zary 	if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
8083a70c006SOndrej Zary 		return NULL;
8093a70c006SOndrej Zary 
810c2532f68SOndrej Zary 	sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
811c2532f68SOndrej Zary 	if (!sh)
8123a70c006SOndrej Zary 		goto release;
813c2532f68SOndrej Zary 	aha1542 = shost_priv(sh);
8143a70c006SOndrej Zary 
815c2532f68SOndrej Zary 	if (!aha1542_test_port(base_io, sh))
8163a70c006SOndrej Zary 		goto unregister;
8171da177e4SLinus Torvalds 
818b847fd0dSOndrej Zary 	aha1542_set_bus_times(indx);
8193a70c006SOndrej Zary 	if (aha1542_query(base_io, &aha1542->bios_translation))
8203a70c006SOndrej Zary 		goto unregister;
821c2532f68SOndrej Zary 	if (aha1542_getconfig(base_io, &sh->irq, &sh->dma_channel, &sh->this_id) == -1)
8221da177e4SLinus Torvalds 		goto unregister;
8231da177e4SLinus Torvalds 
824c2532f68SOndrej Zary 	printk(KERN_INFO "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d", sh->this_id, base_io, sh->irq);
825c2532f68SOndrej Zary 	if (sh->dma_channel != 0xFF)
826c2532f68SOndrej Zary 		printk(", DMA %d", sh->dma_channel);
8271da177e4SLinus Torvalds 	printk("\n");
8283a70c006SOndrej Zary 	if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
8293a70c006SOndrej Zary 		printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
8301da177e4SLinus Torvalds 
831c2532f68SOndrej Zary 	setup_mailboxes(base_io, sh);
8321da177e4SLinus Torvalds 
833c2532f68SOndrej Zary 	if (request_irq(sh->irq, do_aha1542_intr_handle, 0,
834c2532f68SOndrej Zary 					"aha1542", sh)) {
8351da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
8361da177e4SLinus Torvalds 		goto unregister;
8371da177e4SLinus Torvalds 	}
838c2532f68SOndrej Zary 	if (sh->dma_channel != 0xFF) {
839c2532f68SOndrej Zary 		if (request_dma(sh->dma_channel, "aha1542")) {
8401da177e4SLinus Torvalds 			printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
8413a70c006SOndrej Zary 			goto free_irq;
8421da177e4SLinus Torvalds 		}
843c2532f68SOndrej Zary 		if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
844c2532f68SOndrej Zary 			set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
845c2532f68SOndrej Zary 			enable_dma(sh->dma_channel);
8461da177e4SLinus Torvalds 		}
8471da177e4SLinus Torvalds 	}
84887c4d7bcSJeff Garzik 
849c2532f68SOndrej Zary 	sh->unique_id = base_io;
850c2532f68SOndrej Zary 	sh->io_port = base_io;
851c2532f68SOndrej Zary 	sh->n_io_port = AHA1542_REGION_SIZE;
8523a70c006SOndrej Zary 	aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
8533a70c006SOndrej Zary 	aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
8541da177e4SLinus Torvalds 
855c2532f68SOndrej Zary 	if (scsi_add_host(sh, pdev))
8563a70c006SOndrej Zary 		goto free_dma;
8571da177e4SLinus Torvalds 
858c2532f68SOndrej Zary 	scsi_scan_host(sh);
8591da177e4SLinus Torvalds 
860c2532f68SOndrej Zary 	return sh;
8613a70c006SOndrej Zary free_dma:
862c2532f68SOndrej Zary 	if (sh->dma_channel != 0xff)
863c2532f68SOndrej Zary 		free_dma(sh->dma_channel);
8643a70c006SOndrej Zary free_irq:
865c2532f68SOndrej Zary 	free_irq(sh->irq, sh);
8661da177e4SLinus Torvalds unregister:
867c2532f68SOndrej Zary 	scsi_host_put(sh);
8683a70c006SOndrej Zary release:
8693a70c006SOndrej Zary 	release_region(base_io, AHA1542_REGION_SIZE);
8701da177e4SLinus Torvalds 
871643a7c43SOndrej Zary 	return NULL;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
874c2532f68SOndrej Zary static int aha1542_release(struct Scsi_Host *sh)
8751da177e4SLinus Torvalds {
876c2532f68SOndrej Zary 	scsi_remove_host(sh);
877c2532f68SOndrej Zary 	if (sh->dma_channel != 0xff)
878c2532f68SOndrej Zary 		free_dma(sh->dma_channel);
879c2532f68SOndrej Zary 	if (sh->irq)
880c2532f68SOndrej Zary 		free_irq(sh->irq, sh);
881c2532f68SOndrej Zary 	if (sh->io_port && sh->n_io_port)
882c2532f68SOndrej Zary 		release_region(sh->io_port, sh->n_io_port);
883c2532f68SOndrej Zary 	scsi_host_put(sh);
8841da177e4SLinus Torvalds 	return 0;
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds /*
8891da177e4SLinus Torvalds  * This is a device reset.  This is handled by sending a special command
8901da177e4SLinus Torvalds  * to the device.
8911da177e4SLinus Torvalds  */
89255b28f9fSOndrej Zary static int aha1542_dev_reset(struct scsi_cmnd *cmd)
8931da177e4SLinus Torvalds {
89455b28f9fSOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
8951da177e4SLinus Torvalds 	unsigned long flags;
896e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
89755b28f9fSOndrej Zary 	u8 target = cmd->device->id;
89855b28f9fSOndrej Zary 	u8 lun = cmd->device->lun;
8991da177e4SLinus Torvalds 	int mbo;
900e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
903e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
9041da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
9051da177e4SLinus Torvalds 		mbo = 0;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	do {
90855b28f9fSOndrej Zary 		if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
9091da177e4SLinus Torvalds 			break;
9101da177e4SLinus Torvalds 		mbo++;
9111da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
9121da177e4SLinus Torvalds 			mbo = 0;
913e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
9141da177e4SLinus Torvalds 
91555b28f9fSOndrej Zary 	if (mb[mbo].status || aha1542->int_cmds[mbo])
9161da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
9171da177e4SLinus Torvalds 
91855b28f9fSOndrej Zary 	aha1542->int_cmds[mbo] = cmd;	/* This will effectively
9191da177e4SLinus Torvalds 					   prevent someone else from
9201da177e4SLinus Torvalds 					   screwing with this cdb. */
9211da177e4SLinus Torvalds 
922e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
9231da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
9241da177e4SLinus Torvalds 
92510be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	ccb[mbo].op = 0x81;	/* BUS DEVICE RESET */
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);		/*SCSI Target Id */
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
9341da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	/*
9371da177e4SLinus Torvalds 	 * Now tell the 1542 to flush all pending commands for this
9381da177e4SLinus Torvalds 	 * target
9391da177e4SLinus Torvalds 	 */
94055b28f9fSOndrej Zary 	aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
9411da177e4SLinus Torvalds 
94255b28f9fSOndrej Zary 	scmd_printk(KERN_WARNING, cmd,
943017560fcSJeff Garzik 		"Trying device reset for target\n");
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	return SUCCESS;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
94855b28f9fSOndrej Zary static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
9491da177e4SLinus Torvalds {
95055b28f9fSOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
9511da177e4SLinus Torvalds 	int i;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	/*
9541da177e4SLinus Torvalds 	 * This does a scsi reset for all devices on the bus.
9551da177e4SLinus Torvalds 	 * In principle, we could also reset the 1542 - should
9561da177e4SLinus Torvalds 	 * we do this?  Try this first, and we can add that later
9571da177e4SLinus Torvalds 	 * if it turns out to be useful.
9581da177e4SLinus Torvalds 	 */
95955b28f9fSOndrej Zary 	outb(reset_cmd, CONTROL(cmd->device->host->io_port));
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	/*
9621da177e4SLinus Torvalds 	 * Wait for the thing to settle down a bit.  Unfortunately
9631da177e4SLinus Torvalds 	 * this is going to basically lock up the machine while we
9641da177e4SLinus Torvalds 	 * wait for this to complete.  To be 100% correct, we need to
9651da177e4SLinus Torvalds 	 * check for timeout, and if we are doing something like this
9661da177e4SLinus Torvalds 	 * we are pretty desperate anyways.
9671da177e4SLinus Torvalds 	 */
9681da177e4SLinus Torvalds 	ssleep(4);
96955b28f9fSOndrej Zary 	spin_lock_irq(cmd->device->host->host_lock);
9701da177e4SLinus Torvalds 
97155b28f9fSOndrej Zary 	if (!wait_mask(STATUS(cmd->device->host->io_port),
972a13b3722SOndrej Zary 	     STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
97355b28f9fSOndrej Zary 		spin_unlock_irq(cmd->device->host->host_lock);
974a13b3722SOndrej Zary 		return FAILED;
975a13b3722SOndrej Zary 	}
9761da177e4SLinus Torvalds 	/*
9771da177e4SLinus Torvalds 	 * We need to do this too before the 1542 can interact with
9788537cba8SOndrej Zary 	 * us again after host reset.
9791da177e4SLinus Torvalds 	 */
9808537cba8SOndrej Zary 	if (reset_cmd & HRST)
98155b28f9fSOndrej Zary 		setup_mailboxes(cmd->device->host->io_port, cmd->device->host);
9821da177e4SLinus Torvalds 	/*
9831da177e4SLinus Torvalds 	 * Now try to pick up the pieces.  For all pending commands,
9841da177e4SLinus Torvalds 	 * free any internal data structures, and basically clear things
9851da177e4SLinus Torvalds 	 * out.  We do not try and restart any commands or anything -
9861da177e4SLinus Torvalds 	 * the strategy handler takes care of that crap.
9871da177e4SLinus Torvalds 	 */
98855b28f9fSOndrej Zary 	printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
99155b28f9fSOndrej Zary 		if (aha1542->int_cmds[i] != NULL) {
99255b28f9fSOndrej Zary 			struct scsi_cmnd *tmp_cmd;
99355b28f9fSOndrej Zary 			tmp_cmd = aha1542->int_cmds[i];
9941da177e4SLinus Torvalds 
99555b28f9fSOndrej Zary 			if (tmp_cmd->device->soft_reset) {
9961da177e4SLinus Torvalds 				/*
9971da177e4SLinus Torvalds 				 * If this device implements the soft reset option,
9981da177e4SLinus Torvalds 				 * then it is still holding onto the command, and
9991da177e4SLinus Torvalds 				 * may yet complete it.  In this case, we don't
10001da177e4SLinus Torvalds 				 * flush the data.
10011da177e4SLinus Torvalds 				 */
10021da177e4SLinus Torvalds 				continue;
10031da177e4SLinus Torvalds 			}
100455b28f9fSOndrej Zary 			kfree(tmp_cmd->host_scribble);
100555b28f9fSOndrej Zary 			tmp_cmd->host_scribble = NULL;
100655b28f9fSOndrej Zary 			aha1542->int_cmds[i] = NULL;
1007e98878f7SOndrej Zary 			aha1542->mb[i].status = 0;
10081da177e4SLinus Torvalds 		}
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 
101155b28f9fSOndrej Zary 	spin_unlock_irq(cmd->device->host->host_lock);
10121da177e4SLinus Torvalds 	return SUCCESS;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
101555b28f9fSOndrej Zary static int aha1542_bus_reset(struct scsi_cmnd *cmd)
10168537cba8SOndrej Zary {
101755b28f9fSOndrej Zary 	return aha1542_reset(cmd, SCRST);
10188537cba8SOndrej Zary }
10198537cba8SOndrej Zary 
102055b28f9fSOndrej Zary static int aha1542_host_reset(struct scsi_cmnd *cmd)
10218537cba8SOndrej Zary {
102255b28f9fSOndrej Zary 	return aha1542_reset(cmd, HRST | SCRST);
10238537cba8SOndrej Zary }
10248537cba8SOndrej Zary 
10251da177e4SLinus Torvalds static int aha1542_biosparam(struct scsi_device *sdev,
102617787a09SOndrej Zary 		struct block_device *bdev, sector_t capacity, int geom[])
10271da177e4SLinus Torvalds {
1028e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
10291da177e4SLinus Torvalds 
103017787a09SOndrej Zary 	if (capacity >= 0x200000 &&
103117787a09SOndrej Zary 			aha1542->bios_translation == BIOS_TRANSLATION_25563) {
10321da177e4SLinus Torvalds 		/* Please verify that this is the same as what DOS returns */
103317787a09SOndrej Zary 		geom[0] = 255;	/* heads */
103417787a09SOndrej Zary 		geom[1] = 63;	/* sectors */
10351da177e4SLinus Torvalds 	} else {
103617787a09SOndrej Zary 		geom[0] = 64;	/* heads */
103717787a09SOndrej Zary 		geom[1] = 32;	/* sectors */
10381da177e4SLinus Torvalds 	}
103917787a09SOndrej Zary 	geom[2] = sector_div(capacity, geom[0] * geom[1]);	/* cylinders */
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	return 0;
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds MODULE_LICENSE("GPL");
10441da177e4SLinus Torvalds 
1045d0be4a7dSChristoph Hellwig static struct scsi_host_template driver_template = {
1046643a7c43SOndrej Zary 	.module			= THIS_MODULE,
10471da177e4SLinus Torvalds 	.proc_name		= "aha1542",
10481da177e4SLinus Torvalds 	.name			= "Adaptec 1542",
10491da177e4SLinus Torvalds 	.queuecommand		= aha1542_queuecommand,
10501da177e4SLinus Torvalds 	.eh_device_reset_handler= aha1542_dev_reset,
10511da177e4SLinus Torvalds 	.eh_bus_reset_handler	= aha1542_bus_reset,
10521da177e4SLinus Torvalds 	.eh_host_reset_handler	= aha1542_host_reset,
10531da177e4SLinus Torvalds 	.bios_param		= aha1542_biosparam,
10541da177e4SLinus Torvalds 	.can_queue		= AHA1542_MAILBOXES,
10551da177e4SLinus Torvalds 	.this_id		= 7,
105610be6250SOndrej Zary 	.sg_tablesize		= 16,
105710be6250SOndrej Zary 	.cmd_per_lun		= 1,
10581da177e4SLinus Torvalds 	.unchecked_isa_dma	= 1,
10591da177e4SLinus Torvalds 	.use_clustering		= ENABLE_CLUSTERING,
10601da177e4SLinus Torvalds };
1061643a7c43SOndrej Zary 
1062643a7c43SOndrej Zary static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1063643a7c43SOndrej Zary {
1064643a7c43SOndrej Zary 	struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1065643a7c43SOndrej Zary 
1066643a7c43SOndrej Zary 	if (!sh)
1067643a7c43SOndrej Zary 		return 0;
1068643a7c43SOndrej Zary 
1069643a7c43SOndrej Zary 	dev_set_drvdata(pdev, sh);
1070643a7c43SOndrej Zary 	return 1;
1071643a7c43SOndrej Zary }
1072643a7c43SOndrej Zary 
1073643a7c43SOndrej Zary static int aha1542_isa_remove(struct device *pdev,
1074643a7c43SOndrej Zary 				    unsigned int ndev)
1075643a7c43SOndrej Zary {
1076643a7c43SOndrej Zary 	aha1542_release(dev_get_drvdata(pdev));
1077643a7c43SOndrej Zary 	dev_set_drvdata(pdev, NULL);
1078643a7c43SOndrej Zary 	return 0;
1079643a7c43SOndrej Zary }
1080643a7c43SOndrej Zary 
1081643a7c43SOndrej Zary static struct isa_driver aha1542_isa_driver = {
1082643a7c43SOndrej Zary 	.match		= aha1542_isa_match,
1083643a7c43SOndrej Zary 	.remove		= aha1542_isa_remove,
1084643a7c43SOndrej Zary 	.driver		= {
1085643a7c43SOndrej Zary 		.name	= "aha1542"
1086643a7c43SOndrej Zary 	},
1087643a7c43SOndrej Zary };
1088643a7c43SOndrej Zary static int isa_registered;
1089643a7c43SOndrej Zary 
1090643a7c43SOndrej Zary #ifdef CONFIG_PNP
1091643a7c43SOndrej Zary static struct pnp_device_id aha1542_pnp_ids[] = {
1092643a7c43SOndrej Zary 	{ .id = "ADP1542" },
1093643a7c43SOndrej Zary 	{ .id = "" }
1094643a7c43SOndrej Zary };
1095643a7c43SOndrej Zary MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1096643a7c43SOndrej Zary 
1097643a7c43SOndrej Zary static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1098643a7c43SOndrej Zary {
1099643a7c43SOndrej Zary 	int indx;
1100643a7c43SOndrej Zary 	struct Scsi_Host *sh;
1101643a7c43SOndrej Zary 
1102f71429abSOndrej Zary 	for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1103f71429abSOndrej Zary 		if (io[indx])
1104643a7c43SOndrej Zary 			continue;
1105643a7c43SOndrej Zary 
1106643a7c43SOndrej Zary 		if (pnp_activate_dev(pdev) < 0)
1107643a7c43SOndrej Zary 			continue;
1108643a7c43SOndrej Zary 
1109f71429abSOndrej Zary 		io[indx] = pnp_port_start(pdev, 0);
1110643a7c43SOndrej Zary 
1111643a7c43SOndrej Zary 		/* The card can be queried for its DMA, we have
1112643a7c43SOndrej Zary 		   the DMA set up that is enough */
1113643a7c43SOndrej Zary 
1114f71429abSOndrej Zary 		printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", io[indx]);
1115643a7c43SOndrej Zary 	}
1116643a7c43SOndrej Zary 
1117643a7c43SOndrej Zary 	sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1118643a7c43SOndrej Zary 	if (!sh)
1119643a7c43SOndrej Zary 		return -ENODEV;
1120643a7c43SOndrej Zary 
1121643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, sh);
1122643a7c43SOndrej Zary 	return 0;
1123643a7c43SOndrej Zary }
1124643a7c43SOndrej Zary 
1125643a7c43SOndrej Zary static void aha1542_pnp_remove(struct pnp_dev *pdev)
1126643a7c43SOndrej Zary {
1127643a7c43SOndrej Zary 	aha1542_release(pnp_get_drvdata(pdev));
1128643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, NULL);
1129643a7c43SOndrej Zary }
1130643a7c43SOndrej Zary 
1131643a7c43SOndrej Zary static struct pnp_driver aha1542_pnp_driver = {
1132643a7c43SOndrej Zary 	.name		= "aha1542",
1133643a7c43SOndrej Zary 	.id_table	= aha1542_pnp_ids,
1134643a7c43SOndrej Zary 	.probe		= aha1542_pnp_probe,
1135643a7c43SOndrej Zary 	.remove		= aha1542_pnp_remove,
1136643a7c43SOndrej Zary };
1137643a7c43SOndrej Zary static int pnp_registered;
1138643a7c43SOndrej Zary #endif /* CONFIG_PNP */
1139643a7c43SOndrej Zary 
1140643a7c43SOndrej Zary static int __init aha1542_init(void)
1141643a7c43SOndrej Zary {
1142643a7c43SOndrej Zary 	int ret = 0;
1143643a7c43SOndrej Zary 
1144643a7c43SOndrej Zary #ifdef CONFIG_PNP
1145643a7c43SOndrej Zary 	if (isapnp) {
1146643a7c43SOndrej Zary 		ret = pnp_register_driver(&aha1542_pnp_driver);
1147643a7c43SOndrej Zary 		if (!ret)
1148643a7c43SOndrej Zary 			pnp_registered = 1;
1149643a7c43SOndrej Zary 	}
1150643a7c43SOndrej Zary #endif
1151643a7c43SOndrej Zary 	ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1152643a7c43SOndrej Zary 	if (!ret)
1153643a7c43SOndrej Zary 		isa_registered = 1;
1154643a7c43SOndrej Zary 
1155643a7c43SOndrej Zary #ifdef CONFIG_PNP
1156643a7c43SOndrej Zary 	if (pnp_registered)
1157643a7c43SOndrej Zary 		ret = 0;
1158643a7c43SOndrej Zary #endif
1159643a7c43SOndrej Zary 	if (isa_registered)
1160643a7c43SOndrej Zary 		ret = 0;
1161643a7c43SOndrej Zary 
1162643a7c43SOndrej Zary 	return ret;
1163643a7c43SOndrej Zary }
1164643a7c43SOndrej Zary 
1165643a7c43SOndrej Zary static void __exit aha1542_exit(void)
1166643a7c43SOndrej Zary {
1167643a7c43SOndrej Zary #ifdef CONFIG_PNP
1168643a7c43SOndrej Zary 	if (pnp_registered)
1169643a7c43SOndrej Zary 		pnp_unregister_driver(&aha1542_pnp_driver);
1170643a7c43SOndrej Zary #endif
1171643a7c43SOndrej Zary 	if (isa_registered)
1172643a7c43SOndrej Zary 		isa_unregister_driver(&aha1542_isa_driver);
1173643a7c43SOndrej Zary }
1174643a7c43SOndrej Zary 
1175643a7c43SOndrej Zary module_init(aha1542_init);
1176643a7c43SOndrej Zary module_exit(aha1542_exit);
1177