xref: /linux/drivers/scsi/aha1542.c (revision 6ddc8cf40a2011af0c1a5dd300cc75d727c3c3a8)
11d084d20SOndrej Zary /*
21d084d20SOndrej 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
61d084d20SOndrej 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/delay.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/spinlock.h>
17643a7c43SOndrej Zary #include <linux/isa.h>
18643a7c43SOndrej Zary #include <linux/pnp.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20954a9fd7SOndrej Zary #include <linux/io.h>
211da177e4SLinus Torvalds #include <asm/dma.h>
22954a9fd7SOndrej Zary #include <scsi/scsi_cmnd.h>
23954a9fd7SOndrej Zary #include <scsi/scsi_device.h>
241da177e4SLinus Torvalds #include <scsi/scsi_host.h>
251da177e4SLinus Torvalds #include "aha1542.h"
261da177e4SLinus Torvalds 
27f71429abSOndrej Zary #define MAXBOARDS 4
281da177e4SLinus Torvalds 
29f71429abSOndrej Zary static bool isapnp = 1;
301da177e4SLinus Torvalds module_param(isapnp, bool, 0);
31f71429abSOndrej Zary MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
32f71429abSOndrej Zary 
33f71429abSOndrej Zary static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
34f71429abSOndrej Zary module_param_array(io, int, NULL, 0);
35f71429abSOndrej Zary MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
36f71429abSOndrej Zary 
37f71429abSOndrej Zary /* time AHA spends on the AT-bus during data transfer */
38f71429abSOndrej Zary static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
39f71429abSOndrej Zary module_param_array(bus_on, int, NULL, 0);
40f71429abSOndrej Zary MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
41f71429abSOndrej Zary 
42f71429abSOndrej Zary /* time AHA spends off the bus (not to monopolize it) during data transfer  */
43f71429abSOndrej Zary static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
44f71429abSOndrej Zary module_param_array(bus_off, int, NULL, 0);
45f71429abSOndrej Zary MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
46f71429abSOndrej Zary 
47f71429abSOndrej Zary /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
48f71429abSOndrej Zary static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
49f71429abSOndrej Zary module_param_array(dma_speed, int, NULL, 0);
50f71429abSOndrej Zary MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #define BIOS_TRANSLATION_6432 1	/* Default case these days */
531da177e4SLinus Torvalds #define BIOS_TRANSLATION_25563 2	/* Big disk case */
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds struct aha1542_hostdata {
561da177e4SLinus Torvalds 	/* This will effectively start both of them at the first mailbox */
571da177e4SLinus Torvalds 	int bios_translation;	/* Mapping bios uses - for compatibility */
581da177e4SLinus Torvalds 	int aha1542_last_mbi_used;
591da177e4SLinus Torvalds 	int aha1542_last_mbo_used;
6055b28f9fSOndrej Zary 	struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES];
611da177e4SLinus Torvalds 	struct mailbox mb[2 * AHA1542_MAILBOXES];
621da177e4SLinus Torvalds 	struct ccb ccb[AHA1542_MAILBOXES];
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds static DEFINE_SPINLOCK(aha1542_lock);
661da177e4SLinus Torvalds 
67f1bbef63SOndrej Zary static inline void aha1542_intr_reset(u16 base)
68f1bbef63SOndrej Zary {
69f1bbef63SOndrej Zary 	outb(IRST, CONTROL(base));
70f1bbef63SOndrej Zary }
711da177e4SLinus Torvalds 
722093bfa1SOndrej Zary static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
732093bfa1SOndrej Zary {
742093bfa1SOndrej Zary 	bool delayed = true;
752093bfa1SOndrej Zary 
762093bfa1SOndrej Zary 	if (timeout == 0) {
772093bfa1SOndrej Zary 		timeout = 3000000;
782093bfa1SOndrej Zary 		delayed = false;
791da177e4SLinus Torvalds 	}
801da177e4SLinus Torvalds 
812093bfa1SOndrej Zary 	while (1) {
822093bfa1SOndrej Zary 		u8 bits = inb(port) & mask;
832093bfa1SOndrej Zary 		if ((bits & allof) == allof && ((bits & noneof) == 0))
842093bfa1SOndrej Zary 			break;
852093bfa1SOndrej Zary 		if (delayed)
862093bfa1SOndrej Zary 			mdelay(1);
872093bfa1SOndrej Zary 		if (--timeout == 0)
882093bfa1SOndrej Zary 			return false;
892093bfa1SOndrej Zary 	}
902093bfa1SOndrej Zary 
912093bfa1SOndrej Zary 	return true;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* This is a bit complicated, but we need to make sure that an interrupt
951da177e4SLinus Torvalds    routine does not send something out while we are in the middle of this.
961da177e4SLinus Torvalds    Fortunately, it is only at boot time that multi-byte messages
971da177e4SLinus Torvalds    are ever sent. */
98cad2fc72SOndrej Zary static int aha1542_outb(unsigned int base, u8 val)
991da177e4SLinus Torvalds {
1000c2b6481SOndrej Zary 	unsigned long flags;
1011da177e4SLinus Torvalds 
1020c2b6481SOndrej Zary 	while (1) {
1032906b3ceSOndrej Zary 		if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
1040c2b6481SOndrej Zary 			return 1;
1051da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
1061da177e4SLinus Torvalds 		if (inb(STATUS(base)) & CDF) {
1071da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1081da177e4SLinus Torvalds 			continue;
1091da177e4SLinus Torvalds 		}
110cad2fc72SOndrej Zary 		outb(val, DATA(base));
1111da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
1121da177e4SLinus Torvalds 		return 0;
1131da177e4SLinus Torvalds 	}
1140c2b6481SOndrej Zary }
1150c2b6481SOndrej Zary 
116cad2fc72SOndrej Zary static int aha1542_out(unsigned int base, u8 *buf, int len)
1170c2b6481SOndrej Zary {
1180c2b6481SOndrej Zary 	unsigned long flags;
1190c2b6481SOndrej Zary 
1201da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
1211da177e4SLinus Torvalds 	while (len--) {
1220c2b6481SOndrej Zary 		if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
1231da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1241da177e4SLinus Torvalds 			return 1;
1251da177e4SLinus Torvalds 		}
126cad2fc72SOndrej Zary 		outb(*buf++, DATA(base));
1270c2b6481SOndrej Zary 	}
1280c2b6481SOndrej Zary 	spin_unlock_irqrestore(&aha1542_lock, flags);
12923e6940aSOndrej Zary 	if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
13023e6940aSOndrej Zary 		return 1;
1310c2b6481SOndrej Zary 
1320c2b6481SOndrej Zary 	return 0;
1330c2b6481SOndrej Zary }
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /* Only used at boot time, so we do not need to worry about latency as much
1361da177e4SLinus Torvalds    here */
1371da177e4SLinus Torvalds 
138cad2fc72SOndrej Zary static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds 	unsigned long flags;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
1431da177e4SLinus Torvalds 	while (len--) {
144a13b3722SOndrej Zary 		if (!wait_mask(STATUS(base), DF, DF, 0, timeout)) {
1451da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1461da177e4SLinus Torvalds 			return 1;
1471da177e4SLinus Torvalds 		}
148cad2fc72SOndrej Zary 		*buf++ = inb(DATA(base));
149a13b3722SOndrej Zary 	}
150a13b3722SOndrej Zary 	spin_unlock_irqrestore(&aha1542_lock, flags);
151a13b3722SOndrej Zary 	return 0;
152a13b3722SOndrej Zary }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds static int makecode(unsigned hosterr, unsigned scsierr)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	switch (hosterr) {
1571da177e4SLinus Torvalds 	case 0x0:
1581da177e4SLinus Torvalds 	case 0xa:		/* Linked command complete without error and linked normally */
1591da177e4SLinus Torvalds 	case 0xb:		/* Linked command complete without error, interrupt generated */
1601da177e4SLinus Torvalds 		hosterr = 0;
1611da177e4SLinus Torvalds 		break;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	case 0x11:		/* Selection time out-The initiator selection or target
1641da177e4SLinus Torvalds 				   reselection was not complete within the SCSI Time out period */
1651da177e4SLinus Torvalds 		hosterr = DID_TIME_OUT;
1661da177e4SLinus Torvalds 		break;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
1691da177e4SLinus Torvalds 				   than was allocated by the Data Length field or the sum of the
1701da177e4SLinus Torvalds 				   Scatter / Gather Data Length fields. */
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	case 0x15:		/* MBO command was not 00, 01 or 02-The first byte of the CB was
1751da177e4SLinus Torvalds 				   invalid. This usually indicates a software failure. */
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	case 0x16:		/* Invalid CCB Operation Code-The first byte of the CCB was invalid.
1781da177e4SLinus Torvalds 				   This usually indicates a software failure. */
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	case 0x17:		/* Linked CCB does not have the same LUN-A subsequent CCB of a set
1811da177e4SLinus Torvalds 				   of linked CCB's does not specify the same logical unit number as
1821da177e4SLinus Torvalds 				   the first. */
1831da177e4SLinus Torvalds 	case 0x18:		/* Invalid Target Direction received from Host-The direction of a
1841da177e4SLinus Torvalds 				   Target Mode CCB was invalid. */
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	case 0x19:		/* Duplicate CCB Received in Target Mode-More than once CCB was
1871da177e4SLinus Torvalds 				   received to service data transfer between the same target LUN
1881da177e4SLinus Torvalds 				   and initiator SCSI ID in the same direction. */
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	case 0x1a:		/* Invalid CCB or Segment List Parameter-A segment list with a zero
1911da177e4SLinus Torvalds 				   length segment or invalid segment list boundaries was received.
1921da177e4SLinus Torvalds 				   A CCB parameter was invalid. */
193fde1fb8aSOndrej Zary #ifdef DEBUG
194fde1fb8aSOndrej Zary 		printk("Aha1542: %x %x\n", hosterr, scsierr);
195fde1fb8aSOndrej Zary #endif
1961da177e4SLinus Torvalds 		hosterr = DID_ERROR;	/* Couldn't find any better */
1971da177e4SLinus Torvalds 		break;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
2001da177e4SLinus Torvalds 				   phase sequence was requested by the target. The host adapter
2011da177e4SLinus Torvalds 				   will generate a SCSI Reset Condition, notifying the host with
2021da177e4SLinus Torvalds 				   a SCRD interrupt */
2031da177e4SLinus Torvalds 		hosterr = DID_RESET;
2041da177e4SLinus Torvalds 		break;
2051da177e4SLinus Torvalds 	default:
2061da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
2071da177e4SLinus Torvalds 		break;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 	return scsierr | (hosterr << 16);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
21268ea9de3SOndrej Zary static int aha1542_test_port(struct Scsi_Host *sh)
2131da177e4SLinus Torvalds {
214cb5b570cSOndrej Zary 	u8 inquiry_result[4];
215cad2fc72SOndrej Zary 	int i;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* Quick and dirty test for presence of the card. */
21868ea9de3SOndrej Zary 	if (inb(STATUS(sh->io_port)) == 0xff)
2191da177e4SLinus Torvalds 		return 0;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	/* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	/* In case some other card was probing here, reset interrupts */
22468ea9de3SOndrej Zary 	aha1542_intr_reset(sh->io_port);	/* reset interrupts, so they don't block */
2251da177e4SLinus Torvalds 
22668ea9de3SOndrej Zary 	outb(SRST | IRST /*|SCRST */ , CONTROL(sh->io_port));
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	mdelay(20);		/* Wait a little bit for things to settle down. */
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	/* Expect INIT and IDLE, any of the others are bad */
23168ea9de3SOndrej Zary 	if (!wait_mask(STATUS(sh->io_port), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
232a13b3722SOndrej Zary 		return 0;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	/* Shouldn't have generated any interrupts during reset */
23568ea9de3SOndrej Zary 	if (inb(INTRFLAGS(sh->io_port)) & INTRMASK)
236a13b3722SOndrej Zary 		return 0;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	/* Perform a host adapter inquiry instead so we do not need to set
2391da177e4SLinus Torvalds 	   up the mailboxes ahead of time */
2401da177e4SLinus Torvalds 
24168ea9de3SOndrej Zary 	aha1542_outb(sh->io_port, CMD_INQUIRY);
2421da177e4SLinus Torvalds 
243cad2fc72SOndrej Zary 	for (i = 0; i < 4; i++) {
24468ea9de3SOndrej Zary 		if (!wait_mask(STATUS(sh->io_port), DF, DF, 0, 0))
245a13b3722SOndrej Zary 			return 0;
24668ea9de3SOndrej Zary 		inquiry_result[i] = inb(DATA(sh->io_port));
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	/* Reading port should reset DF */
25068ea9de3SOndrej Zary 	if (inb(STATUS(sh->io_port)) & DF)
251a13b3722SOndrej Zary 		return 0;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	/* When HACC, command is completed, and we're though testing */
25468ea9de3SOndrej Zary 	if (!wait_mask(INTRFLAGS(sh->io_port), HACC, HACC, 0, 0))
255a13b3722SOndrej Zary 		return 0;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	/* Clear interrupts */
25868ea9de3SOndrej Zary 	outb(IRST, CONTROL(sh->io_port));
2591da177e4SLinus Torvalds 
260bdebe224SOndrej Zary 	return 1;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds /* A "high" level interrupt handler */
264c2532f68SOndrej Zary static void aha1542_intr_handle(struct Scsi_Host *sh)
2651da177e4SLinus Torvalds {
266c2532f68SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sh);
26755b28f9fSOndrej Zary 	void (*my_done)(struct scsi_cmnd *) = NULL;
2681da177e4SLinus Torvalds 	int errstatus, mbi, mbo, mbistatus;
2691da177e4SLinus Torvalds 	int number_serviced;
2701da177e4SLinus Torvalds 	unsigned long flags;
27155b28f9fSOndrej Zary 	struct scsi_cmnd *tmp_cmd;
2721da177e4SLinus Torvalds 	int flag;
273e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
274e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds #ifdef DEBUG
2771da177e4SLinus Torvalds 	{
278c2532f68SOndrej Zary 		flag = inb(INTRFLAGS(sh->io_port));
2792906b3ceSOndrej Zary 		shost_printk(KERN_DEBUG, sh, "aha1542_intr_handle: ");
2801da177e4SLinus Torvalds 		if (!(flag & ANYINTR))
2811da177e4SLinus Torvalds 			printk("no interrupt?");
2821da177e4SLinus Torvalds 		if (flag & MBIF)
2831da177e4SLinus Torvalds 			printk("MBIF ");
2841da177e4SLinus Torvalds 		if (flag & MBOA)
2851da177e4SLinus Torvalds 			printk("MBOF ");
2861da177e4SLinus Torvalds 		if (flag & HACC)
2871da177e4SLinus Torvalds 			printk("HACC ");
2881da177e4SLinus Torvalds 		if (flag & SCRD)
2891da177e4SLinus Torvalds 			printk("SCRD ");
290c2532f68SOndrej Zary 		printk("status %02x\n", inb(STATUS(sh->io_port)));
2911da177e4SLinus Torvalds 	};
2921da177e4SLinus Torvalds #endif
2931da177e4SLinus Torvalds 	number_serviced = 0;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	while (1 == 1) {
296c2532f68SOndrej Zary 		flag = inb(INTRFLAGS(sh->io_port));
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 		/* Check for unusual interrupts.  If any of these happen, we should
2991da177e4SLinus Torvalds 		   probably do something special, but for now just printing a message
3001da177e4SLinus Torvalds 		   is sufficient.  A SCSI reset detected is something that we really
3011da177e4SLinus Torvalds 		   need to deal with in some way. */
3021da177e4SLinus Torvalds 		if (flag & ~MBIF) {
3031da177e4SLinus Torvalds 			if (flag & MBOA)
3041da177e4SLinus Torvalds 				printk("MBOF ");
3051da177e4SLinus Torvalds 			if (flag & HACC)
3061da177e4SLinus Torvalds 				printk("HACC ");
307dfd7c991SOndrej Zary 			if (flag & SCRD)
3081da177e4SLinus Torvalds 				printk("SCRD ");
3091da177e4SLinus Torvalds 		}
310c2532f68SOndrej Zary 		aha1542_intr_reset(sh->io_port);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
313e98878f7SOndrej Zary 		mbi = aha1542->aha1542_last_mbi_used + 1;
3141da177e4SLinus Torvalds 		if (mbi >= 2 * AHA1542_MAILBOXES)
3151da177e4SLinus Torvalds 			mbi = AHA1542_MAILBOXES;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 		do {
3181da177e4SLinus Torvalds 			if (mb[mbi].status != 0)
3191da177e4SLinus Torvalds 				break;
3201da177e4SLinus Torvalds 			mbi++;
3211da177e4SLinus Torvalds 			if (mbi >= 2 * AHA1542_MAILBOXES)
3221da177e4SLinus Torvalds 				mbi = AHA1542_MAILBOXES;
323e98878f7SOndrej Zary 		} while (mbi != aha1542->aha1542_last_mbi_used);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 		if (mb[mbi].status == 0) {
3261da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
3271da177e4SLinus Torvalds 			/* Hmm, no mail.  Must have read it the last time around */
328dfd7c991SOndrej Zary 			if (!number_serviced)
3292906b3ceSOndrej Zary 				shost_printk(KERN_WARNING, sh, "interrupt received, but no mail.\n");
3301da177e4SLinus Torvalds 			return;
3311da177e4SLinus Torvalds 		};
3321da177e4SLinus Torvalds 
33310be6250SOndrej Zary 		mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
3341da177e4SLinus Torvalds 		mbistatus = mb[mbi].status;
3351da177e4SLinus Torvalds 		mb[mbi].status = 0;
336e98878f7SOndrej Zary 		aha1542->aha1542_last_mbi_used = mbi;
3371da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds #ifdef DEBUG
3401da177e4SLinus Torvalds 		if (ccb[mbo].tarstat | ccb[mbo].hastat)
3412906b3ceSOndrej Zary 			shost_printk(KERN_DEBUG, sh, "aha1542_command: returning %x (status %d)\n",
3421da177e4SLinus Torvalds 			       ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
3431da177e4SLinus Torvalds #endif
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		if (mbistatus == 3)
3461da177e4SLinus Torvalds 			continue;	/* Aborted command not found */
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds #ifdef DEBUG
3492906b3ceSOndrej Zary 		shost_printk(KERN_DEBUG, sh, "...done %d %d\n", mbo, mbi);
3501da177e4SLinus Torvalds #endif
3511da177e4SLinus Torvalds 
35255b28f9fSOndrej Zary 		tmp_cmd = aha1542->int_cmds[mbo];
3531da177e4SLinus Torvalds 
35455b28f9fSOndrej Zary 		if (!tmp_cmd || !tmp_cmd->scsi_done) {
3552906b3ceSOndrej Zary 			shost_printk(KERN_WARNING, sh, "Unexpected interrupt\n");
3562906b3ceSOndrej Zary 			shost_printk(KERN_WARNING, sh, "tarstat=%x, hastat=%x idlun=%x ccb#=%d\n", ccb[mbo].tarstat,
3571da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].idlun, mbo);
3581da177e4SLinus Torvalds 			return;
3591da177e4SLinus Torvalds 		}
36055b28f9fSOndrej Zary 		my_done = tmp_cmd->scsi_done;
36155b28f9fSOndrej Zary 		kfree(tmp_cmd->host_scribble);
36255b28f9fSOndrej Zary 		tmp_cmd->host_scribble = NULL;
3631da177e4SLinus Torvalds 		/* Fetch the sense data, and tuck it away, in the required slot.  The
3641da177e4SLinus Torvalds 		   Adaptec automatically fetches it, and there is no guarantee that
3651da177e4SLinus Torvalds 		   we will still have it in the cdb when we come back */
3661da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2)
36755b28f9fSOndrej Zary 			memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
368b80ca4f7SFUJITA Tomonori 			       SCSI_SENSE_BUFFERSIZE);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 		/* is there mail :-) */
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 		/* more error checking left out here */
3741da177e4SLinus Torvalds 		if (mbistatus != 1)
3751da177e4SLinus Torvalds 			/* This is surely wrong, but I don't know what's right */
3761da177e4SLinus Torvalds 			errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
3771da177e4SLinus Torvalds 		else
3781da177e4SLinus Torvalds 			errstatus = 0;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds #ifdef DEBUG
3811da177e4SLinus Torvalds 		if (errstatus)
3822906b3ceSOndrej Zary 			shost_printk(KERN_DEBUG, sh, "(aha1542 error:%x %x %x) ", errstatus,
3831da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].tarstat);
384*6ddc8cf4SOndrej Zary 		if (ccb[mbo].tarstat == 2)
385*6ddc8cf4SOndrej Zary 			print_hex_dump_bytes("sense: ", DUMP_PREFIX_NONE, &ccb[mbo].cdb[ccb[mbo].cdblen], 12);
386fde1fb8aSOndrej Zary 		if (errstatus)
387fde1fb8aSOndrej Zary 			printk("aha1542_intr_handle: returning %6x\n", errstatus);
388fde1fb8aSOndrej Zary #endif
38955b28f9fSOndrej Zary 		tmp_cmd->result = errstatus;
39055b28f9fSOndrej Zary 		aha1542->int_cmds[mbo] = NULL;	/* This effectively frees up the mailbox slot, as
3911da177e4SLinus Torvalds 						   far as queuecommand is concerned */
39255b28f9fSOndrej Zary 		my_done(tmp_cmd);
3931da177e4SLinus Torvalds 		number_serviced++;
3941da177e4SLinus Torvalds 	};
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
39709a44833SOndrej Zary /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
39809a44833SOndrej Zary static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
39909a44833SOndrej Zary {
40009a44833SOndrej Zary 	unsigned long flags;
401c2532f68SOndrej Zary 	struct Scsi_Host *sh = dev_id;
40209a44833SOndrej Zary 
403c2532f68SOndrej Zary 	spin_lock_irqsave(sh->host_lock, flags);
404c2532f68SOndrej Zary 	aha1542_intr_handle(sh);
405c2532f68SOndrej Zary 	spin_unlock_irqrestore(sh->host_lock, flags);
40609a44833SOndrej Zary 	return IRQ_HANDLED;
40709a44833SOndrej Zary }
40809a44833SOndrej Zary 
40955b28f9fSOndrej Zary static int aha1542_queuecommand_lck(struct scsi_cmnd *cmd, void (*done) (struct scsi_cmnd *))
4101da177e4SLinus Torvalds {
4112906b3ceSOndrej Zary 	struct Scsi_Host *sh = cmd->device->host;
4122906b3ceSOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sh);
413cb5b570cSOndrej Zary 	u8 direction;
41455b28f9fSOndrej Zary 	u8 target = cmd->device->id;
41555b28f9fSOndrej Zary 	u8 lun = cmd->device->lun;
4161da177e4SLinus Torvalds 	unsigned long flags;
41755b28f9fSOndrej Zary 	int bufflen = scsi_bufflen(cmd);
4181da177e4SLinus Torvalds 	int mbo;
419e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
420e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
421fde1fb8aSOndrej Zary #ifdef DEBUG
422fde1fb8aSOndrej Zary 	int i;
4231da177e4SLinus Torvalds 
424fde1fb8aSOndrej Zary 	if (target > 1) {
42555b28f9fSOndrej Zary 		cmd->result = DID_TIME_OUT << 16;
426fde1fb8aSOndrej Zary 		done(cmd);
427fde1fb8aSOndrej Zary 		return 0;
4281da177e4SLinus Torvalds 	}
429fde1fb8aSOndrej Zary #endif
43055b28f9fSOndrej Zary 	if (*cmd->cmnd == REQUEST_SENSE) {
4311da177e4SLinus Torvalds 		/* Don't do the command - we have the sense data already */
43255b28f9fSOndrej Zary 		cmd->result = 0;
43355b28f9fSOndrej Zary 		done(cmd);
4341da177e4SLinus Torvalds 		return 0;
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds #ifdef DEBUG
43755b28f9fSOndrej Zary 	if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
43855b28f9fSOndrej Zary 		i = xscsi2int(cmd->cmnd + 2);
43955b28f9fSOndrej Zary 	else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
44055b28f9fSOndrej Zary 		i = scsi2int(cmd->cmnd + 2);
4411da177e4SLinus Torvalds 	else
4421da177e4SLinus Torvalds 		i = -1;
4431da177e4SLinus Torvalds 	if (done)
4442906b3ceSOndrej Zary 		shost_printk(KERN_DEBUG, sh, "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen);
4451da177e4SLinus Torvalds 	else
4462906b3ceSOndrej Zary 		shost_printk(KERN_DEBUG, sh, "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd->cmnd, i, bufflen);
447*6ddc8cf4SOndrej Zary 	print_hex_dump_bytes("command: ", DUMP_PREFIX_NONE, cmd->cmnd, cmd->cmd_len);
44855b28f9fSOndrej Zary 	if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
4491da177e4SLinus Torvalds 		return 0;	/* we are still testing, so *don't* write */
4501da177e4SLinus Torvalds #endif
4511da177e4SLinus Torvalds 	/* Use the outgoing mailboxes in a round-robin fashion, because this
4521da177e4SLinus Torvalds 	   is how the host adapter will scan for them */
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
455e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
4561da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
4571da177e4SLinus Torvalds 		mbo = 0;
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	do {
46055b28f9fSOndrej Zary 		if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
4611da177e4SLinus Torvalds 			break;
4621da177e4SLinus Torvalds 		mbo++;
4631da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
4641da177e4SLinus Torvalds 			mbo = 0;
465e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
4661da177e4SLinus Torvalds 
46755b28f9fSOndrej Zary 	if (mb[mbo].status || aha1542->int_cmds[mbo])
4681da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
4691da177e4SLinus Torvalds 
47055b28f9fSOndrej Zary 	aha1542->int_cmds[mbo] = cmd;	/* This will effectively prevent someone else from
4711da177e4SLinus Torvalds 					   screwing with this cdb. */
4721da177e4SLinus Torvalds 
473e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
4741da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds #ifdef DEBUG
477*6ddc8cf4SOndrej Zary 	shost_printk(KERN_DEBUG, sh, "Sending command (%d %p)...", mbo, done);
4781da177e4SLinus Torvalds #endif
4791da177e4SLinus Torvalds 
48010be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
4831da177e4SLinus Torvalds 
48455b28f9fSOndrej Zary 	ccb[mbo].cdblen = cmd->cmd_len;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	direction = 0;
48755b28f9fSOndrej Zary 	if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
4881da177e4SLinus Torvalds 		direction = 8;
48955b28f9fSOndrej Zary 	else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
4901da177e4SLinus Torvalds 		direction = 16;
4911da177e4SLinus Torvalds 
49255b28f9fSOndrej Zary 	memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
4931da177e4SLinus Torvalds 
494fc3fdfccSBoaz Harrosh 	if (bufflen) {
49551cf2249SJens Axboe 		struct scatterlist *sg;
4961da177e4SLinus Torvalds 		struct chain *cptr;
49755b28f9fSOndrej Zary 		int i, sg_count = scsi_sg_count(cmd);
498*6ddc8cf4SOndrej Zary 
4991da177e4SLinus Torvalds 		ccb[mbo].op = 2;	/* SCSI Initiator Command  w/scatter-gather */
50055b28f9fSOndrej Zary 		cmd->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
501fc3fdfccSBoaz Harrosh 		                                         GFP_KERNEL | GFP_DMA);
50255b28f9fSOndrej Zary 		cptr = (struct chain *) cmd->host_scribble;
5031da177e4SLinus Torvalds 		if (cptr == NULL) {
5041da177e4SLinus Torvalds 			/* free the claimed mailbox slot */
50555b28f9fSOndrej Zary 			aha1542->int_cmds[mbo] = NULL;
5061da177e4SLinus Torvalds 			return SCSI_MLQUEUE_HOST_BUSY;
5071da177e4SLinus Torvalds 		}
50855b28f9fSOndrej Zary 		scsi_for_each_sg(cmd, sg, sg_count, i) {
50910be6250SOndrej Zary 			any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
51010be6250SOndrej Zary 								+ sg->offset);
51151cf2249SJens Axboe 			any2scsi(cptr[i].datalen, sg->length);
5121da177e4SLinus Torvalds 		};
513fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
51410be6250SOndrej Zary 		any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
5151da177e4SLinus Torvalds #ifdef DEBUG
516*6ddc8cf4SOndrej Zary 		shost_printk(KERN_DEBUG, sh, "cptr %p: ", cptr);
517*6ddc8cf4SOndrej Zary 		print_hex_dump_bytes("cptr: ", DUMP_PREFIX_NONE, cptr, 18);
5181da177e4SLinus Torvalds #endif
5191da177e4SLinus Torvalds 	} else {
5201da177e4SLinus Torvalds 		ccb[mbo].op = 0;	/* SCSI Initiator Command */
52155b28f9fSOndrej Zary 		cmd->host_scribble = NULL;
522fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, 0);
523fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].dataptr, 0);
5241da177e4SLinus Torvalds 	};
5251da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);	/*SCSI Target Id */
5261da177e4SLinus Torvalds 	ccb[mbo].rsalen = 16;
5271da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
5281da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds #ifdef DEBUG
531*6ddc8cf4SOndrej Zary 	print_hex_dump_bytes("sending: ", DUMP_PREFIX_NONE, &ccb[mbo], sizeof(ccb[mbo]) - 10);
5321da177e4SLinus Torvalds #endif
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	if (done) {
535fde1fb8aSOndrej Zary #ifdef DEBUG
536fde1fb8aSOndrej Zary 		printk("aha1542_queuecommand: now waiting for interrupt ");
537fde1fb8aSOndrej Zary #endif
53855b28f9fSOndrej Zary 		cmd->scsi_done = done;
5391da177e4SLinus Torvalds 		mb[mbo].status = 1;
54055b28f9fSOndrej Zary 		aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
5411da177e4SLinus Torvalds 	} else
5421da177e4SLinus Torvalds 		printk("aha1542_queuecommand: done can't be NULL\n");
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	return 0;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
547f281233dSJeff Garzik static DEF_SCSI_QCMD(aha1542_queuecommand)
548f281233dSJeff Garzik 
5491da177e4SLinus Torvalds /* Initialize mailboxes */
55068ea9de3SOndrej Zary static void setup_mailboxes(struct Scsi_Host *sh)
5511da177e4SLinus Torvalds {
552c2532f68SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sh);
5531da177e4SLinus Torvalds 	int i;
554e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
555e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
5561da177e4SLinus Torvalds 
557cad2fc72SOndrej Zary 	u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
5601da177e4SLinus Torvalds 		mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
56110be6250SOndrej Zary 		any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
5621da177e4SLinus Torvalds 	};
56368ea9de3SOndrej Zary 	aha1542_intr_reset(sh->io_port);	/* reset interrupts, so they don't block */
564cad2fc72SOndrej Zary 	any2scsi((mb_cmd + 2), isa_virt_to_bus(mb));
56568ea9de3SOndrej Zary 	if (aha1542_out(sh->io_port, mb_cmd, 5))
5662906b3ceSOndrej Zary 		shost_printk(KERN_ERR, sh, "failed setting up mailboxes\n");
56768ea9de3SOndrej Zary 	aha1542_intr_reset(sh->io_port);
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
57068ea9de3SOndrej Zary static int aha1542_getconfig(struct Scsi_Host *sh)
5711da177e4SLinus Torvalds {
572cb5b570cSOndrej Zary 	u8 inquiry_result[3];
5731da177e4SLinus Torvalds 	int i;
57468ea9de3SOndrej Zary 	i = inb(STATUS(sh->io_port));
5751da177e4SLinus Torvalds 	if (i & DF) {
57668ea9de3SOndrej Zary 		i = inb(DATA(sh->io_port));
5771da177e4SLinus Torvalds 	};
57868ea9de3SOndrej Zary 	aha1542_outb(sh->io_port, CMD_RETCONF);
57968ea9de3SOndrej Zary 	aha1542_in(sh->io_port, inquiry_result, 3, 0);
58068ea9de3SOndrej Zary 	if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
5812906b3ceSOndrej Zary 		shost_printk(KERN_ERR, sh, "error querying board settings\n");
58268ea9de3SOndrej Zary 	aha1542_intr_reset(sh->io_port);
5831da177e4SLinus Torvalds 	switch (inquiry_result[0]) {
5841da177e4SLinus Torvalds 	case 0x80:
58568ea9de3SOndrej Zary 		sh->dma_channel = 7;
5861da177e4SLinus Torvalds 		break;
5871da177e4SLinus Torvalds 	case 0x40:
58868ea9de3SOndrej Zary 		sh->dma_channel = 6;
5891da177e4SLinus Torvalds 		break;
5901da177e4SLinus Torvalds 	case 0x20:
59168ea9de3SOndrej Zary 		sh->dma_channel = 5;
5921da177e4SLinus Torvalds 		break;
5931da177e4SLinus Torvalds 	case 0x01:
59468ea9de3SOndrej Zary 		sh->dma_channel = 0;
5951da177e4SLinus Torvalds 		break;
5961da177e4SLinus Torvalds 	case 0:
5971da177e4SLinus Torvalds 		/* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
5981da177e4SLinus Torvalds 		   Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
59968ea9de3SOndrej Zary 		sh->dma_channel = 0xFF;
6001da177e4SLinus Torvalds 		break;
6011da177e4SLinus Torvalds 	default:
6022906b3ceSOndrej Zary 		shost_printk(KERN_ERR, sh, "Unable to determine DMA channel.\n");
6031da177e4SLinus Torvalds 		return -1;
6041da177e4SLinus Torvalds 	};
6051da177e4SLinus Torvalds 	switch (inquiry_result[1]) {
6061da177e4SLinus Torvalds 	case 0x40:
60768ea9de3SOndrej Zary 		sh->irq = 15;
6081da177e4SLinus Torvalds 		break;
6091da177e4SLinus Torvalds 	case 0x20:
61068ea9de3SOndrej Zary 		sh->irq = 14;
6111da177e4SLinus Torvalds 		break;
6121da177e4SLinus Torvalds 	case 0x8:
61368ea9de3SOndrej Zary 		sh->irq = 12;
6141da177e4SLinus Torvalds 		break;
6151da177e4SLinus Torvalds 	case 0x4:
61668ea9de3SOndrej Zary 		sh->irq = 11;
6171da177e4SLinus Torvalds 		break;
6181da177e4SLinus Torvalds 	case 0x2:
61968ea9de3SOndrej Zary 		sh->irq = 10;
6201da177e4SLinus Torvalds 		break;
6211da177e4SLinus Torvalds 	case 0x1:
62268ea9de3SOndrej Zary 		sh->irq = 9;
6231da177e4SLinus Torvalds 		break;
6241da177e4SLinus Torvalds 	default:
6252906b3ceSOndrej Zary 		shost_printk(KERN_ERR, sh, "Unable to determine IRQ level.\n");
6261da177e4SLinus Torvalds 		return -1;
6271da177e4SLinus Torvalds 	};
62868ea9de3SOndrej Zary 	sh->this_id = inquiry_result[2] & 7;
6291da177e4SLinus Torvalds 	return 0;
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds /* This function should only be called for 1542C boards - we can detect
6331da177e4SLinus Torvalds    the special firmware settings and unlock the board */
6341da177e4SLinus Torvalds 
63568ea9de3SOndrej Zary static int aha1542_mbenable(struct Scsi_Host *sh)
6361da177e4SLinus Torvalds {
637cb5b570cSOndrej Zary 	static u8 mbenable_cmd[3];
638cb5b570cSOndrej Zary 	static u8 mbenable_result[2];
6391da177e4SLinus Torvalds 	int retval;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	retval = BIOS_TRANSLATION_6432;
6421da177e4SLinus Torvalds 
64368ea9de3SOndrej Zary 	aha1542_outb(sh->io_port, CMD_EXTBIOS);
64468ea9de3SOndrej Zary 	if (aha1542_in(sh->io_port, mbenable_result, 2, 100))
6451da177e4SLinus Torvalds 		return retval;
64668ea9de3SOndrej Zary 	if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 100))
6472093bfa1SOndrej Zary 		goto fail;
64868ea9de3SOndrej Zary 	aha1542_intr_reset(sh->io_port);
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
6511da177e4SLinus Torvalds 		mbenable_cmd[0] = CMD_MBENABLE;
6521da177e4SLinus Torvalds 		mbenable_cmd[1] = 0;
6531da177e4SLinus Torvalds 		mbenable_cmd[2] = mbenable_result[1];
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 		if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
6561da177e4SLinus Torvalds 			retval = BIOS_TRANSLATION_25563;
6571da177e4SLinus Torvalds 
65868ea9de3SOndrej Zary 		if (aha1542_out(sh->io_port, mbenable_cmd, 3))
6592093bfa1SOndrej Zary 			goto fail;
6601da177e4SLinus Torvalds 	};
6611da177e4SLinus Torvalds 	while (0) {
6621da177e4SLinus Torvalds fail:
6632906b3ceSOndrej Zary 		shost_printk(KERN_ERR, sh, "Mailbox init failed\n");
6641da177e4SLinus Torvalds 	}
66568ea9de3SOndrej Zary 	aha1542_intr_reset(sh->io_port);
6661da177e4SLinus Torvalds 	return retval;
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
67068ea9de3SOndrej Zary static int aha1542_query(struct Scsi_Host *sh)
6711da177e4SLinus Torvalds {
67268ea9de3SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sh);
673cb5b570cSOndrej Zary 	u8 inquiry_result[4];
6741da177e4SLinus Torvalds 	int i;
67568ea9de3SOndrej Zary 	i = inb(STATUS(sh->io_port));
6761da177e4SLinus Torvalds 	if (i & DF) {
67768ea9de3SOndrej Zary 		i = inb(DATA(sh->io_port));
6781da177e4SLinus Torvalds 	};
67968ea9de3SOndrej Zary 	aha1542_outb(sh->io_port, CMD_INQUIRY);
68068ea9de3SOndrej Zary 	aha1542_in(sh->io_port, inquiry_result, 4, 0);
68168ea9de3SOndrej Zary 	if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
6822906b3ceSOndrej Zary 		shost_printk(KERN_ERR, sh, "error querying card type\n");
68368ea9de3SOndrej Zary 	aha1542_intr_reset(sh->io_port);
6841da177e4SLinus Torvalds 
68568ea9de3SOndrej Zary 	aha1542->bios_translation = BIOS_TRANSLATION_6432;	/* Default case */
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	/* For an AHA1740 series board, we ignore the board since there is a
6881da177e4SLinus Torvalds 	   hardware bug which can lead to wrong blocks being returned if the board
6891da177e4SLinus Torvalds 	   is operating in the 1542 emulation mode.  Since there is an extended mode
6901da177e4SLinus Torvalds 	   driver, we simply ignore the board and let the 1740 driver pick it up.
6911da177e4SLinus Torvalds 	 */
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	if (inquiry_result[0] == 0x43) {
6942906b3ceSOndrej Zary 		shost_printk(KERN_INFO, sh, "Emulation mode not supported for AHA-1740 hardware, use aha1740 driver instead.\n");
6951da177e4SLinus Torvalds 		return 1;
6961da177e4SLinus Torvalds 	};
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	/* Always call this - boards that do not support extended bios translation
6991da177e4SLinus Torvalds 	   will ignore the command, and we will set the proper default */
7001da177e4SLinus Torvalds 
70168ea9de3SOndrej Zary 	aha1542->bios_translation = aha1542_mbenable(sh);
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	return 0;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
706f71429abSOndrej Zary static u8 dma_speed_hw(int dma_speed)
7071da177e4SLinus Torvalds {
708f71429abSOndrej Zary 	switch (dma_speed) {
7091da177e4SLinus Torvalds 	case 5:
710f71429abSOndrej Zary 		return 0x00;
7111da177e4SLinus Torvalds 	case 6:
712f71429abSOndrej Zary 		return 0x04;
7131da177e4SLinus Torvalds 	case 7:
714f71429abSOndrej Zary 		return 0x01;
7151da177e4SLinus Torvalds 	case 8:
716f71429abSOndrej Zary 		return 0x02;
7171da177e4SLinus Torvalds 	case 10:
718f71429abSOndrej Zary 		return 0x03;
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 
721f71429abSOndrej Zary 	return 0xff;	/* invalid */
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
724b847fd0dSOndrej Zary /* Set the Bus on/off-times as not to ruin floppy performance */
72537d607bdSOndrej Zary static void aha1542_set_bus_times(struct Scsi_Host *sh, int bus_on, int bus_off, int dma_speed)
726b847fd0dSOndrej Zary {
72737d607bdSOndrej Zary 	if (bus_on > 0) {
72837d607bdSOndrej Zary 		u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on, 2, 15) };
729b847fd0dSOndrej Zary 
73037d607bdSOndrej Zary 		aha1542_intr_reset(sh->io_port);
73137d607bdSOndrej Zary 		if (aha1542_out(sh->io_port, oncmd, 2))
732b847fd0dSOndrej Zary 			goto fail;
733f71429abSOndrej Zary 	}
734f71429abSOndrej Zary 
73537d607bdSOndrej Zary 	if (bus_off > 0) {
73637d607bdSOndrej Zary 		u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off, 1, 64) };
737f71429abSOndrej Zary 
73837d607bdSOndrej Zary 		aha1542_intr_reset(sh->io_port);
73937d607bdSOndrej Zary 		if (aha1542_out(sh->io_port, offcmd, 2))
740b847fd0dSOndrej Zary 			goto fail;
741f71429abSOndrej Zary 	}
742f71429abSOndrej Zary 
74337d607bdSOndrej Zary 	if (dma_speed_hw(dma_speed) != 0xff) {
74437d607bdSOndrej Zary 		u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed) };
745f71429abSOndrej Zary 
74637d607bdSOndrej Zary 		aha1542_intr_reset(sh->io_port);
74737d607bdSOndrej Zary 		if (aha1542_out(sh->io_port, dmacmd, 2))
748b847fd0dSOndrej Zary 			goto fail;
749b847fd0dSOndrej Zary 	}
75037d607bdSOndrej Zary 	aha1542_intr_reset(sh->io_port);
751b847fd0dSOndrej Zary 	return;
752b847fd0dSOndrej Zary fail:
7532906b3ceSOndrej Zary 	shost_printk(KERN_ERR, sh, "setting bus on/off-time failed\n");
75437d607bdSOndrej Zary 	aha1542_intr_reset(sh->io_port);
755b847fd0dSOndrej Zary }
756b847fd0dSOndrej Zary 
7571da177e4SLinus Torvalds /* return non-zero on detection */
758643a7c43SOndrej Zary static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
7591da177e4SLinus Torvalds {
760f71429abSOndrej Zary 	unsigned int base_io = io[indx];
761c2532f68SOndrej Zary 	struct Scsi_Host *sh;
762e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542;
7632906b3ceSOndrej Zary 	char dma_info[] = "no DMA";
7641da177e4SLinus Torvalds 
7653a70c006SOndrej Zary 	if (base_io == 0)
766643a7c43SOndrej Zary 		return NULL;
7671da177e4SLinus Torvalds 
7683a70c006SOndrej Zary 	if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
7693a70c006SOndrej Zary 		return NULL;
7703a70c006SOndrej Zary 
771c2532f68SOndrej Zary 	sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
772c2532f68SOndrej Zary 	if (!sh)
7733a70c006SOndrej Zary 		goto release;
774c2532f68SOndrej Zary 	aha1542 = shost_priv(sh);
7753a70c006SOndrej Zary 
77668ea9de3SOndrej Zary 	sh->unique_id = base_io;
77768ea9de3SOndrej Zary 	sh->io_port = base_io;
77868ea9de3SOndrej Zary 	sh->n_io_port = AHA1542_REGION_SIZE;
77968ea9de3SOndrej Zary 	aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
78068ea9de3SOndrej Zary 	aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
78168ea9de3SOndrej Zary 
78268ea9de3SOndrej Zary 	if (!aha1542_test_port(sh))
7833a70c006SOndrej Zary 		goto unregister;
7841da177e4SLinus Torvalds 
78537d607bdSOndrej Zary 	aha1542_set_bus_times(sh, bus_on[indx], bus_off[indx], dma_speed[indx]);
78668ea9de3SOndrej Zary 	if (aha1542_query(sh))
7873a70c006SOndrej Zary 		goto unregister;
78868ea9de3SOndrej Zary 	if (aha1542_getconfig(sh) == -1)
7891da177e4SLinus Torvalds 		goto unregister;
7901da177e4SLinus Torvalds 
791c2532f68SOndrej Zary 	if (sh->dma_channel != 0xFF)
7922906b3ceSOndrej Zary 		snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
7932906b3ceSOndrej Zary 	shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n",
7942906b3ceSOndrej Zary 				sh->this_id, base_io, sh->irq, dma_info);
7953a70c006SOndrej Zary 	if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
7962906b3ceSOndrej Zary 		shost_printk(KERN_INFO, sh, "Using extended bios translation\n");
7971da177e4SLinus Torvalds 
79868ea9de3SOndrej Zary 	setup_mailboxes(sh);
7991da177e4SLinus Torvalds 
800c2532f68SOndrej Zary 	if (request_irq(sh->irq, do_aha1542_intr_handle, 0,
801c2532f68SOndrej Zary 					"aha1542", sh)) {
8022906b3ceSOndrej Zary 		shost_printk(KERN_ERR, sh, "Unable to allocate IRQ.\n");
8031da177e4SLinus Torvalds 		goto unregister;
8041da177e4SLinus Torvalds 	}
805c2532f68SOndrej Zary 	if (sh->dma_channel != 0xFF) {
806c2532f68SOndrej Zary 		if (request_dma(sh->dma_channel, "aha1542")) {
8072906b3ceSOndrej Zary 			shost_printk(KERN_ERR, sh, "Unable to allocate DMA channel.\n");
8083a70c006SOndrej Zary 			goto free_irq;
8091da177e4SLinus Torvalds 		}
810c2532f68SOndrej Zary 		if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
811c2532f68SOndrej Zary 			set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
812c2532f68SOndrej Zary 			enable_dma(sh->dma_channel);
8131da177e4SLinus Torvalds 		}
8141da177e4SLinus Torvalds 	}
81587c4d7bcSJeff Garzik 
816c2532f68SOndrej Zary 	if (scsi_add_host(sh, pdev))
8173a70c006SOndrej Zary 		goto free_dma;
8181da177e4SLinus Torvalds 
819c2532f68SOndrej Zary 	scsi_scan_host(sh);
8201da177e4SLinus Torvalds 
821c2532f68SOndrej Zary 	return sh;
8223a70c006SOndrej Zary free_dma:
823c2532f68SOndrej Zary 	if (sh->dma_channel != 0xff)
824c2532f68SOndrej Zary 		free_dma(sh->dma_channel);
8253a70c006SOndrej Zary free_irq:
826c2532f68SOndrej Zary 	free_irq(sh->irq, sh);
8271da177e4SLinus Torvalds unregister:
828c2532f68SOndrej Zary 	scsi_host_put(sh);
8293a70c006SOndrej Zary release:
8303a70c006SOndrej Zary 	release_region(base_io, AHA1542_REGION_SIZE);
8311da177e4SLinus Torvalds 
832643a7c43SOndrej Zary 	return NULL;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
835c2532f68SOndrej Zary static int aha1542_release(struct Scsi_Host *sh)
8361da177e4SLinus Torvalds {
837c2532f68SOndrej Zary 	scsi_remove_host(sh);
838c2532f68SOndrej Zary 	if (sh->dma_channel != 0xff)
839c2532f68SOndrej Zary 		free_dma(sh->dma_channel);
840c2532f68SOndrej Zary 	if (sh->irq)
841c2532f68SOndrej Zary 		free_irq(sh->irq, sh);
842c2532f68SOndrej Zary 	if (sh->io_port && sh->n_io_port)
843c2532f68SOndrej Zary 		release_region(sh->io_port, sh->n_io_port);
844c2532f68SOndrej Zary 	scsi_host_put(sh);
8451da177e4SLinus Torvalds 	return 0;
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds /*
8501da177e4SLinus Torvalds  * This is a device reset.  This is handled by sending a special command
8511da177e4SLinus Torvalds  * to the device.
8521da177e4SLinus Torvalds  */
85355b28f9fSOndrej Zary static int aha1542_dev_reset(struct scsi_cmnd *cmd)
8541da177e4SLinus Torvalds {
85555b28f9fSOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
8561da177e4SLinus Torvalds 	unsigned long flags;
857e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
85855b28f9fSOndrej Zary 	u8 target = cmd->device->id;
85955b28f9fSOndrej Zary 	u8 lun = cmd->device->lun;
8601da177e4SLinus Torvalds 	int mbo;
861e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
864e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
8651da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
8661da177e4SLinus Torvalds 		mbo = 0;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	do {
86955b28f9fSOndrej Zary 		if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
8701da177e4SLinus Torvalds 			break;
8711da177e4SLinus Torvalds 		mbo++;
8721da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
8731da177e4SLinus Torvalds 			mbo = 0;
874e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
8751da177e4SLinus Torvalds 
87655b28f9fSOndrej Zary 	if (mb[mbo].status || aha1542->int_cmds[mbo])
8771da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
8781da177e4SLinus Torvalds 
87955b28f9fSOndrej Zary 	aha1542->int_cmds[mbo] = cmd;	/* This will effectively
8801da177e4SLinus Torvalds 					   prevent someone else from
8811da177e4SLinus Torvalds 					   screwing with this cdb. */
8821da177e4SLinus Torvalds 
883e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
8841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
8851da177e4SLinus Torvalds 
88610be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	ccb[mbo].op = 0x81;	/* BUS DEVICE RESET */
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);		/*SCSI Target Id */
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
8951da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	/*
8981da177e4SLinus Torvalds 	 * Now tell the 1542 to flush all pending commands for this
8991da177e4SLinus Torvalds 	 * target
9001da177e4SLinus Torvalds 	 */
90155b28f9fSOndrej Zary 	aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
9021da177e4SLinus Torvalds 
90355b28f9fSOndrej Zary 	scmd_printk(KERN_WARNING, cmd,
904017560fcSJeff Garzik 		"Trying device reset for target\n");
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	return SUCCESS;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
90955b28f9fSOndrej Zary static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
9101da177e4SLinus Torvalds {
91155b28f9fSOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(cmd->device->host);
9121da177e4SLinus Torvalds 	int i;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	/*
9151da177e4SLinus Torvalds 	 * This does a scsi reset for all devices on the bus.
9161da177e4SLinus Torvalds 	 * In principle, we could also reset the 1542 - should
9171da177e4SLinus Torvalds 	 * we do this?  Try this first, and we can add that later
9181da177e4SLinus Torvalds 	 * if it turns out to be useful.
9191da177e4SLinus Torvalds 	 */
92055b28f9fSOndrej Zary 	outb(reset_cmd, CONTROL(cmd->device->host->io_port));
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	/*
9231da177e4SLinus Torvalds 	 * Wait for the thing to settle down a bit.  Unfortunately
9241da177e4SLinus Torvalds 	 * this is going to basically lock up the machine while we
9251da177e4SLinus Torvalds 	 * wait for this to complete.  To be 100% correct, we need to
9261da177e4SLinus Torvalds 	 * check for timeout, and if we are doing something like this
9271da177e4SLinus Torvalds 	 * we are pretty desperate anyways.
9281da177e4SLinus Torvalds 	 */
9291da177e4SLinus Torvalds 	ssleep(4);
93055b28f9fSOndrej Zary 	spin_lock_irq(cmd->device->host->host_lock);
9311da177e4SLinus Torvalds 
93255b28f9fSOndrej Zary 	if (!wait_mask(STATUS(cmd->device->host->io_port),
933a13b3722SOndrej Zary 	     STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
93455b28f9fSOndrej Zary 		spin_unlock_irq(cmd->device->host->host_lock);
935a13b3722SOndrej Zary 		return FAILED;
936a13b3722SOndrej Zary 	}
9371da177e4SLinus Torvalds 	/*
9381da177e4SLinus Torvalds 	 * We need to do this too before the 1542 can interact with
9398537cba8SOndrej Zary 	 * us again after host reset.
9401da177e4SLinus Torvalds 	 */
9418537cba8SOndrej Zary 	if (reset_cmd & HRST)
94268ea9de3SOndrej Zary 		setup_mailboxes(cmd->device->host);
9431da177e4SLinus Torvalds 	/*
9441da177e4SLinus Torvalds 	 * Now try to pick up the pieces.  For all pending commands,
9451da177e4SLinus Torvalds 	 * free any internal data structures, and basically clear things
9461da177e4SLinus Torvalds 	 * out.  We do not try and restart any commands or anything -
9471da177e4SLinus Torvalds 	 * the strategy handler takes care of that crap.
9481da177e4SLinus Torvalds 	 */
9492906b3ceSOndrej Zary 	shost_printk(KERN_WARNING, cmd->device->host, "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
95255b28f9fSOndrej Zary 		if (aha1542->int_cmds[i] != NULL) {
95355b28f9fSOndrej Zary 			struct scsi_cmnd *tmp_cmd;
95455b28f9fSOndrej Zary 			tmp_cmd = aha1542->int_cmds[i];
9551da177e4SLinus Torvalds 
95655b28f9fSOndrej Zary 			if (tmp_cmd->device->soft_reset) {
9571da177e4SLinus Torvalds 				/*
9581da177e4SLinus Torvalds 				 * If this device implements the soft reset option,
9591da177e4SLinus Torvalds 				 * then it is still holding onto the command, and
9601da177e4SLinus Torvalds 				 * may yet complete it.  In this case, we don't
9611da177e4SLinus Torvalds 				 * flush the data.
9621da177e4SLinus Torvalds 				 */
9631da177e4SLinus Torvalds 				continue;
9641da177e4SLinus Torvalds 			}
96555b28f9fSOndrej Zary 			kfree(tmp_cmd->host_scribble);
96655b28f9fSOndrej Zary 			tmp_cmd->host_scribble = NULL;
96755b28f9fSOndrej Zary 			aha1542->int_cmds[i] = NULL;
968e98878f7SOndrej Zary 			aha1542->mb[i].status = 0;
9691da177e4SLinus Torvalds 		}
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds 
97255b28f9fSOndrej Zary 	spin_unlock_irq(cmd->device->host->host_lock);
9731da177e4SLinus Torvalds 	return SUCCESS;
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
97655b28f9fSOndrej Zary static int aha1542_bus_reset(struct scsi_cmnd *cmd)
9778537cba8SOndrej Zary {
97855b28f9fSOndrej Zary 	return aha1542_reset(cmd, SCRST);
9798537cba8SOndrej Zary }
9808537cba8SOndrej Zary 
98155b28f9fSOndrej Zary static int aha1542_host_reset(struct scsi_cmnd *cmd)
9828537cba8SOndrej Zary {
98355b28f9fSOndrej Zary 	return aha1542_reset(cmd, HRST | SCRST);
9848537cba8SOndrej Zary }
9858537cba8SOndrej Zary 
9861da177e4SLinus Torvalds static int aha1542_biosparam(struct scsi_device *sdev,
98717787a09SOndrej Zary 		struct block_device *bdev, sector_t capacity, int geom[])
9881da177e4SLinus Torvalds {
989e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
9901da177e4SLinus Torvalds 
99117787a09SOndrej Zary 	if (capacity >= 0x200000 &&
99217787a09SOndrej Zary 			aha1542->bios_translation == BIOS_TRANSLATION_25563) {
9931da177e4SLinus Torvalds 		/* Please verify that this is the same as what DOS returns */
99417787a09SOndrej Zary 		geom[0] = 255;	/* heads */
99517787a09SOndrej Zary 		geom[1] = 63;	/* sectors */
9961da177e4SLinus Torvalds 	} else {
99717787a09SOndrej Zary 		geom[0] = 64;	/* heads */
99817787a09SOndrej Zary 		geom[1] = 32;	/* sectors */
9991da177e4SLinus Torvalds 	}
100017787a09SOndrej Zary 	geom[2] = sector_div(capacity, geom[0] * geom[1]);	/* cylinders */
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	return 0;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds MODULE_LICENSE("GPL");
10051da177e4SLinus Torvalds 
1006d0be4a7dSChristoph Hellwig static struct scsi_host_template driver_template = {
1007643a7c43SOndrej Zary 	.module			= THIS_MODULE,
10081da177e4SLinus Torvalds 	.proc_name		= "aha1542",
10091da177e4SLinus Torvalds 	.name			= "Adaptec 1542",
10101da177e4SLinus Torvalds 	.queuecommand		= aha1542_queuecommand,
10111da177e4SLinus Torvalds 	.eh_device_reset_handler= aha1542_dev_reset,
10121da177e4SLinus Torvalds 	.eh_bus_reset_handler	= aha1542_bus_reset,
10131da177e4SLinus Torvalds 	.eh_host_reset_handler	= aha1542_host_reset,
10141da177e4SLinus Torvalds 	.bios_param		= aha1542_biosparam,
10151da177e4SLinus Torvalds 	.can_queue		= AHA1542_MAILBOXES,
10161da177e4SLinus Torvalds 	.this_id		= 7,
101710be6250SOndrej Zary 	.sg_tablesize		= 16,
101810be6250SOndrej Zary 	.cmd_per_lun		= 1,
10191da177e4SLinus Torvalds 	.unchecked_isa_dma	= 1,
10201da177e4SLinus Torvalds 	.use_clustering		= ENABLE_CLUSTERING,
10211da177e4SLinus Torvalds };
1022643a7c43SOndrej Zary 
1023643a7c43SOndrej Zary static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1024643a7c43SOndrej Zary {
1025643a7c43SOndrej Zary 	struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1026643a7c43SOndrej Zary 
1027643a7c43SOndrej Zary 	if (!sh)
1028643a7c43SOndrej Zary 		return 0;
1029643a7c43SOndrej Zary 
1030643a7c43SOndrej Zary 	dev_set_drvdata(pdev, sh);
1031643a7c43SOndrej Zary 	return 1;
1032643a7c43SOndrej Zary }
1033643a7c43SOndrej Zary 
1034643a7c43SOndrej Zary static int aha1542_isa_remove(struct device *pdev,
1035643a7c43SOndrej Zary 				    unsigned int ndev)
1036643a7c43SOndrej Zary {
1037643a7c43SOndrej Zary 	aha1542_release(dev_get_drvdata(pdev));
1038643a7c43SOndrej Zary 	dev_set_drvdata(pdev, NULL);
1039643a7c43SOndrej Zary 	return 0;
1040643a7c43SOndrej Zary }
1041643a7c43SOndrej Zary 
1042643a7c43SOndrej Zary static struct isa_driver aha1542_isa_driver = {
1043643a7c43SOndrej Zary 	.match		= aha1542_isa_match,
1044643a7c43SOndrej Zary 	.remove		= aha1542_isa_remove,
1045643a7c43SOndrej Zary 	.driver		= {
1046643a7c43SOndrej Zary 		.name	= "aha1542"
1047643a7c43SOndrej Zary 	},
1048643a7c43SOndrej Zary };
1049643a7c43SOndrej Zary static int isa_registered;
1050643a7c43SOndrej Zary 
1051643a7c43SOndrej Zary #ifdef CONFIG_PNP
1052643a7c43SOndrej Zary static struct pnp_device_id aha1542_pnp_ids[] = {
1053643a7c43SOndrej Zary 	{ .id = "ADP1542" },
1054643a7c43SOndrej Zary 	{ .id = "" }
1055643a7c43SOndrej Zary };
1056643a7c43SOndrej Zary MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1057643a7c43SOndrej Zary 
1058643a7c43SOndrej Zary static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1059643a7c43SOndrej Zary {
1060643a7c43SOndrej Zary 	int indx;
1061643a7c43SOndrej Zary 	struct Scsi_Host *sh;
1062643a7c43SOndrej Zary 
1063f71429abSOndrej Zary 	for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
1064f71429abSOndrej Zary 		if (io[indx])
1065643a7c43SOndrej Zary 			continue;
1066643a7c43SOndrej Zary 
1067643a7c43SOndrej Zary 		if (pnp_activate_dev(pdev) < 0)
1068643a7c43SOndrej Zary 			continue;
1069643a7c43SOndrej Zary 
1070f71429abSOndrej Zary 		io[indx] = pnp_port_start(pdev, 0);
1071643a7c43SOndrej Zary 
1072643a7c43SOndrej Zary 		/* The card can be queried for its DMA, we have
1073643a7c43SOndrej Zary 		   the DMA set up that is enough */
1074643a7c43SOndrej Zary 
10752906b3ceSOndrej Zary 		dev_info(&pdev->dev, "ISAPnP found an AHA1535 at I/O 0x%03X", io[indx]);
1076643a7c43SOndrej Zary 	}
1077643a7c43SOndrej Zary 
1078643a7c43SOndrej Zary 	sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1079643a7c43SOndrej Zary 	if (!sh)
1080643a7c43SOndrej Zary 		return -ENODEV;
1081643a7c43SOndrej Zary 
1082643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, sh);
1083643a7c43SOndrej Zary 	return 0;
1084643a7c43SOndrej Zary }
1085643a7c43SOndrej Zary 
1086643a7c43SOndrej Zary static void aha1542_pnp_remove(struct pnp_dev *pdev)
1087643a7c43SOndrej Zary {
1088643a7c43SOndrej Zary 	aha1542_release(pnp_get_drvdata(pdev));
1089643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, NULL);
1090643a7c43SOndrej Zary }
1091643a7c43SOndrej Zary 
1092643a7c43SOndrej Zary static struct pnp_driver aha1542_pnp_driver = {
1093643a7c43SOndrej Zary 	.name		= "aha1542",
1094643a7c43SOndrej Zary 	.id_table	= aha1542_pnp_ids,
1095643a7c43SOndrej Zary 	.probe		= aha1542_pnp_probe,
1096643a7c43SOndrej Zary 	.remove		= aha1542_pnp_remove,
1097643a7c43SOndrej Zary };
1098643a7c43SOndrej Zary static int pnp_registered;
1099643a7c43SOndrej Zary #endif /* CONFIG_PNP */
1100643a7c43SOndrej Zary 
1101643a7c43SOndrej Zary static int __init aha1542_init(void)
1102643a7c43SOndrej Zary {
1103643a7c43SOndrej Zary 	int ret = 0;
1104643a7c43SOndrej Zary 
1105643a7c43SOndrej Zary #ifdef CONFIG_PNP
1106643a7c43SOndrej Zary 	if (isapnp) {
1107643a7c43SOndrej Zary 		ret = pnp_register_driver(&aha1542_pnp_driver);
1108643a7c43SOndrej Zary 		if (!ret)
1109643a7c43SOndrej Zary 			pnp_registered = 1;
1110643a7c43SOndrej Zary 	}
1111643a7c43SOndrej Zary #endif
1112643a7c43SOndrej Zary 	ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1113643a7c43SOndrej Zary 	if (!ret)
1114643a7c43SOndrej Zary 		isa_registered = 1;
1115643a7c43SOndrej Zary 
1116643a7c43SOndrej Zary #ifdef CONFIG_PNP
1117643a7c43SOndrej Zary 	if (pnp_registered)
1118643a7c43SOndrej Zary 		ret = 0;
1119643a7c43SOndrej Zary #endif
1120643a7c43SOndrej Zary 	if (isa_registered)
1121643a7c43SOndrej Zary 		ret = 0;
1122643a7c43SOndrej Zary 
1123643a7c43SOndrej Zary 	return ret;
1124643a7c43SOndrej Zary }
1125643a7c43SOndrej Zary 
1126643a7c43SOndrej Zary static void __exit aha1542_exit(void)
1127643a7c43SOndrej Zary {
1128643a7c43SOndrej Zary #ifdef CONFIG_PNP
1129643a7c43SOndrej Zary 	if (pnp_registered)
1130643a7c43SOndrej Zary 		pnp_unregister_driver(&aha1542_pnp_driver);
1131643a7c43SOndrej Zary #endif
1132643a7c43SOndrej Zary 	if (isa_registered)
1133643a7c43SOndrej Zary 		isa_unregister_driver(&aha1542_isa_driver);
1134643a7c43SOndrej Zary }
1135643a7c43SOndrej Zary 
1136643a7c43SOndrej Zary module_init(aha1542_init);
1137643a7c43SOndrej Zary module_exit(aha1542_exit);
1138