xref: /linux/drivers/scsi/aha1542.c (revision 3a70c006edbe6071bd78e1e0c8004791cd4b531c)
11da177e4SLinus Torvalds /* $Id: aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $
21da177e4SLinus Torvalds  *  linux/kernel/aha1542.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Tommy Thorn
51da177e4SLinus Torvalds  *  Copyright (C) 1993, 1994, 1995 Eric Youngdale
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Modified by Eric Youngdale
81da177e4SLinus Torvalds  *        Use request_irq and request_dma to help prevent unexpected conflicts
91da177e4SLinus Torvalds  *        Set up on-board DMA controller, such that we do not have to
101da177e4SLinus Torvalds  *        have the bios enabled to use the aha1542.
111da177e4SLinus Torvalds  *  Modified by David Gentzel
121da177e4SLinus Torvalds  *        Don't call request_dma if dma mask is 0 (for BusLogic BT-445S VL-Bus
131da177e4SLinus Torvalds  *        controller).
141da177e4SLinus Torvalds  *  Modified by Matti Aarnio
151da177e4SLinus Torvalds  *        Accept parameters from LILO cmd-line. -- 1-Oct-94
161da177e4SLinus Torvalds  *  Modified by Mike McLagan <mike.mclagan@linux.org>
171da177e4SLinus Torvalds  *        Recognise extended mode on AHA1542CP, different bit than 1542CF
181da177e4SLinus Torvalds  *        1-Jan-97
191da177e4SLinus Torvalds  *  Modified by Bjorn L. Thordarson and Einar Thor Einarsson
201da177e4SLinus Torvalds  *        Recognize that DMA0 is valid DMA channel -- 13-Jul-98
211da177e4SLinus Torvalds  *  Modified by Chris Faulhaber <jedgar@fxp.org>
221da177e4SLinus Torvalds  *        Added module command-line options
231da177e4SLinus Torvalds  *        19-Jul-99
24726a6459SJoe Perches  *  Modified by Adam Fritzler
25a88dc06cSPaul Gortmaker  *        Added proper detection of the AHA-1640 (MCA, now deleted)
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <linux/module.h>
291da177e4SLinus Torvalds #include <linux/interrupt.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/types.h>
321da177e4SLinus Torvalds #include <linux/string.h>
331da177e4SLinus Torvalds #include <linux/ioport.h>
341da177e4SLinus Torvalds #include <linux/delay.h>
351da177e4SLinus Torvalds #include <linux/proc_fs.h>
361da177e4SLinus Torvalds #include <linux/init.h>
371da177e4SLinus Torvalds #include <linux/spinlock.h>
38643a7c43SOndrej Zary #include <linux/isa.h>
39643a7c43SOndrej Zary #include <linux/pnp.h>
401da177e4SLinus Torvalds #include <linux/blkdev.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <asm/dma.h>
441da177e4SLinus Torvalds #include <asm/io.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include "scsi.h"
471da177e4SLinus Torvalds #include <scsi/scsi_host.h>
481da177e4SLinus Torvalds #include "aha1542.h"
491da177e4SLinus Torvalds #include <linux/stat.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #ifdef DEBUG
521da177e4SLinus Torvalds #define DEB(x) x
531da177e4SLinus Torvalds #else
541da177e4SLinus Torvalds #define DEB(x)
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds    static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $";
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /* The adaptec can be configured for quite a number of addresses, but
621da177e4SLinus Torvalds    I generally do not want the card poking around at random.  We allow
631da177e4SLinus Torvalds    two addresses - this allows people to use the Adaptec with a Midi
641da177e4SLinus Torvalds    card, which also used 0x330 -- can be overridden with LILO! */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #define MAXBOARDS 4		/* Increase this and the sizes of the
671da177e4SLinus Torvalds 				   arrays below, if you need more.. */
681da177e4SLinus Torvalds 
69a88dc06cSPaul Gortmaker /* Boards 3,4 slots are reserved for ISAPnP scans */
701da177e4SLinus Torvalds 
71643a7c43SOndrej Zary static unsigned int bases[MAXBOARDS] = {0x330, 0x334, 0, 0};
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* set by aha1542_setup according to the command line; they also may
741da177e4SLinus Torvalds    be marked __initdata, but require zero initializers then */
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static int setup_called[MAXBOARDS];
771da177e4SLinus Torvalds static int setup_buson[MAXBOARDS];
781da177e4SLinus Torvalds static int setup_busoff[MAXBOARDS];
79643a7c43SOndrej Zary static int setup_dmaspeed[MAXBOARDS] = { -1, -1, -1, -1 };
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /*
821da177e4SLinus Torvalds  * LILO/Module params:  aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * Where:  <PORTBASE> is any of the valid AHA addresses:
851da177e4SLinus Torvalds  *                      0x130, 0x134, 0x230, 0x234, 0x330, 0x334
861da177e4SLinus Torvalds  *         <BUSON>  is the time (in microsecs) that AHA spends on the AT-bus
871da177e4SLinus Torvalds  *                  when transferring data.  1542A power-on default is 11us,
881da177e4SLinus Torvalds  *                  valid values are in range: 2..15 (decimal)
891da177e4SLinus Torvalds  *         <BUSOFF> is the time that AHA spends OFF THE BUS after while
901da177e4SLinus Torvalds  *                  it is transferring data (not to monopolize the bus).
911da177e4SLinus Torvalds  *                  Power-on default is 4us, valid range: 1..64 microseconds.
921da177e4SLinus Torvalds  *         <DMASPEED> Default is jumper selected (1542A: on the J1),
931da177e4SLinus Torvalds  *                  but experimenter can alter it with this.
941da177e4SLinus Torvalds  *                  Valid values: 5, 6, 7, 8, 10 (MB/s)
951da177e4SLinus Torvalds  *                  Factory default is 5 MB/s.
961da177e4SLinus Torvalds  */
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #if defined(MODULE)
9990ab5ee9SRusty Russell static bool isapnp = 0;
1001da177e4SLinus Torvalds static int aha1542[] = {0x330, 11, 4, -1};
1011da177e4SLinus Torvalds module_param_array(aha1542, int, NULL, 0);
1021da177e4SLinus Torvalds module_param(isapnp, bool, 0);
1031da177e4SLinus Torvalds #else
1041da177e4SLinus Torvalds static int isapnp = 1;
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #define BIOS_TRANSLATION_1632 0	/* Used by some old 1542A boards */
1081da177e4SLinus Torvalds #define BIOS_TRANSLATION_6432 1	/* Default case these days */
1091da177e4SLinus Torvalds #define BIOS_TRANSLATION_25563 2	/* Big disk case */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds struct aha1542_hostdata {
1121da177e4SLinus Torvalds 	/* This will effectively start both of them at the first mailbox */
1131da177e4SLinus Torvalds 	int bios_translation;	/* Mapping bios uses - for compatibility */
1141da177e4SLinus Torvalds 	int aha1542_last_mbi_used;
1151da177e4SLinus Torvalds 	int aha1542_last_mbo_used;
1161da177e4SLinus Torvalds 	Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
1171da177e4SLinus Torvalds 	struct mailbox mb[2 * AHA1542_MAILBOXES];
1181da177e4SLinus Torvalds 	struct ccb ccb[AHA1542_MAILBOXES];
1191da177e4SLinus Torvalds };
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static DEFINE_SPINLOCK(aha1542_lock);
1221da177e4SLinus Torvalds 
123f1bbef63SOndrej Zary static inline void aha1542_intr_reset(u16 base)
124f1bbef63SOndrej Zary {
125f1bbef63SOndrej Zary 	outb(IRST, CONTROL(base));
126f1bbef63SOndrej Zary }
1271da177e4SLinus Torvalds 
1282093bfa1SOndrej Zary static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
1292093bfa1SOndrej Zary {
1302093bfa1SOndrej Zary 	bool delayed = true;
1312093bfa1SOndrej Zary 
1322093bfa1SOndrej Zary 	if (timeout == 0) {
1332093bfa1SOndrej Zary 		timeout = 3000000;
1342093bfa1SOndrej Zary 		delayed = false;
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds 
1372093bfa1SOndrej Zary 	while (1) {
1382093bfa1SOndrej Zary 		u8 bits = inb(port) & mask;
1392093bfa1SOndrej Zary 		if ((bits & allof) == allof && ((bits & noneof) == 0))
1402093bfa1SOndrej Zary 			break;
1412093bfa1SOndrej Zary 		if (delayed)
1422093bfa1SOndrej Zary 			mdelay(1);
1432093bfa1SOndrej Zary 		if (--timeout == 0)
1442093bfa1SOndrej Zary 			return false;
1452093bfa1SOndrej Zary 	}
1462093bfa1SOndrej Zary 
1472093bfa1SOndrej Zary 	return true;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds /* This is a bit complicated, but we need to make sure that an interrupt
1511da177e4SLinus Torvalds    routine does not send something out while we are in the middle of this.
1521da177e4SLinus Torvalds    Fortunately, it is only at boot time that multi-byte messages
1531da177e4SLinus Torvalds    are ever sent. */
1540c2b6481SOndrej Zary static int aha1542_outb(unsigned int base, u8 cmd)
1551da177e4SLinus Torvalds {
1560c2b6481SOndrej Zary 	unsigned long flags;
1571da177e4SLinus Torvalds 
1580c2b6481SOndrej Zary 	while (1) {
1590c2b6481SOndrej Zary 		if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
1600c2b6481SOndrej Zary 			printk(KERN_ERR "aha1542_outb failed");
1610c2b6481SOndrej Zary 			return 1;
1620c2b6481SOndrej Zary 		}
1631da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
1641da177e4SLinus Torvalds 		if (inb(STATUS(base)) & CDF) {
1651da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1661da177e4SLinus Torvalds 			continue;
1671da177e4SLinus Torvalds 		}
1680c2b6481SOndrej Zary 		outb(cmd, DATA(base));
1691da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
1701da177e4SLinus Torvalds 		return 0;
1711da177e4SLinus Torvalds 	}
1720c2b6481SOndrej Zary }
1730c2b6481SOndrej Zary 
1740c2b6481SOndrej Zary static int aha1542_out(unsigned int base, u8 *cmdp, int len)
1750c2b6481SOndrej Zary {
1760c2b6481SOndrej Zary 	unsigned long flags;
1770c2b6481SOndrej Zary 
1781da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
1791da177e4SLinus Torvalds 	while (len--) {
1800c2b6481SOndrej Zary 		if (!wait_mask(STATUS(base), CDF, 0, CDF, 0)) {
1811da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
1821da177e4SLinus Torvalds 			printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
1831da177e4SLinus Torvalds 			return 1;
1841da177e4SLinus Torvalds 		}
1850c2b6481SOndrej Zary 		outb(*cmdp++, DATA(base));
1860c2b6481SOndrej Zary 	}
1870c2b6481SOndrej Zary 	spin_unlock_irqrestore(&aha1542_lock, flags);
18823e6940aSOndrej Zary 	if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
18923e6940aSOndrej Zary 		return 1;
1900c2b6481SOndrej Zary 
1910c2b6481SOndrej Zary 	return 0;
1920c2b6481SOndrej Zary }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /* Only used at boot time, so we do not need to worry about latency as much
1951da177e4SLinus Torvalds    here */
1961da177e4SLinus Torvalds 
197f8846be3SOndrej Zary static int aha1542_in(unsigned int base, u8 *cmdp, int len, int timeout)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	unsigned long flags;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
2021da177e4SLinus Torvalds 	while (len--) {
203a13b3722SOndrej Zary 		if (!wait_mask(STATUS(base), DF, DF, 0, timeout)) {
2041da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
205f8846be3SOndrej Zary 			if (timeout == 0)
2061da177e4SLinus Torvalds 				printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
2071da177e4SLinus Torvalds 			return 1;
2081da177e4SLinus Torvalds 		}
209a13b3722SOndrej Zary 		*cmdp++ = inb(DATA(base));
210a13b3722SOndrej Zary 	}
211a13b3722SOndrej Zary 	spin_unlock_irqrestore(&aha1542_lock, flags);
212a13b3722SOndrej Zary 	return 0;
213a13b3722SOndrej Zary }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds static int makecode(unsigned hosterr, unsigned scsierr)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	switch (hosterr) {
2181da177e4SLinus Torvalds 	case 0x0:
2191da177e4SLinus Torvalds 	case 0xa:		/* Linked command complete without error and linked normally */
2201da177e4SLinus Torvalds 	case 0xb:		/* Linked command complete without error, interrupt generated */
2211da177e4SLinus Torvalds 		hosterr = 0;
2221da177e4SLinus Torvalds 		break;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	case 0x11:		/* Selection time out-The initiator selection or target
2251da177e4SLinus Torvalds 				   reselection was not complete within the SCSI Time out period */
2261da177e4SLinus Torvalds 		hosterr = DID_TIME_OUT;
2271da177e4SLinus Torvalds 		break;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
2301da177e4SLinus Torvalds 				   than was allocated by the Data Length field or the sum of the
2311da177e4SLinus Torvalds 				   Scatter / Gather Data Length fields. */
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	case 0x15:		/* MBO command was not 00, 01 or 02-The first byte of the CB was
2361da177e4SLinus Torvalds 				   invalid. This usually indicates a software failure. */
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	case 0x16:		/* Invalid CCB Operation Code-The first byte of the CCB was invalid.
2391da177e4SLinus Torvalds 				   This usually indicates a software failure. */
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	case 0x17:		/* Linked CCB does not have the same LUN-A subsequent CCB of a set
2421da177e4SLinus Torvalds 				   of linked CCB's does not specify the same logical unit number as
2431da177e4SLinus Torvalds 				   the first. */
2441da177e4SLinus Torvalds 	case 0x18:		/* Invalid Target Direction received from Host-The direction of a
2451da177e4SLinus Torvalds 				   Target Mode CCB was invalid. */
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	case 0x19:		/* Duplicate CCB Received in Target Mode-More than once CCB was
2481da177e4SLinus Torvalds 				   received to service data transfer between the same target LUN
2491da177e4SLinus Torvalds 				   and initiator SCSI ID in the same direction. */
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	case 0x1a:		/* Invalid CCB or Segment List Parameter-A segment list with a zero
2521da177e4SLinus Torvalds 				   length segment or invalid segment list boundaries was received.
2531da177e4SLinus Torvalds 				   A CCB parameter was invalid. */
2541da177e4SLinus Torvalds 		DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
2551da177e4SLinus Torvalds 		hosterr = DID_ERROR;	/* Couldn't find any better */
2561da177e4SLinus Torvalds 		break;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
2591da177e4SLinus Torvalds 				   phase sequence was requested by the target. The host adapter
2601da177e4SLinus Torvalds 				   will generate a SCSI Reset Condition, notifying the host with
2611da177e4SLinus Torvalds 				   a SCRD interrupt */
2621da177e4SLinus Torvalds 		hosterr = DID_RESET;
2631da177e4SLinus Torvalds 		break;
2641da177e4SLinus Torvalds 	default:
2651da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
2661da177e4SLinus Torvalds 		break;
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 	return scsierr | (hosterr << 16);
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
271643a7c43SOndrej Zary static int aha1542_test_port(int bse, struct Scsi_Host *shpnt)
2721da177e4SLinus Torvalds {
273cb5b570cSOndrej Zary 	u8 inquiry_result[4];
274cb5b570cSOndrej Zary 	u8 *cmdp;
2751da177e4SLinus Torvalds 	int len;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	/* Quick and dirty test for presence of the card. */
2781da177e4SLinus Torvalds 	if (inb(STATUS(bse)) == 0xff)
2791da177e4SLinus Torvalds 		return 0;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	/* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/* In case some other card was probing here, reset interrupts */
2841da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	mdelay(20);		/* Wait a little bit for things to settle down. */
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	/* Expect INIT and IDLE, any of the others are bad */
2912093bfa1SOndrej Zary 	if (!wait_mask(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
292a13b3722SOndrej Zary 		return 0;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	/* Shouldn't have generated any interrupts during reset */
2951da177e4SLinus Torvalds 	if (inb(INTRFLAGS(bse)) & INTRMASK)
296a13b3722SOndrej Zary 		return 0;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	/* Perform a host adapter inquiry instead so we do not need to set
2991da177e4SLinus Torvalds 	   up the mailboxes ahead of time */
3001da177e4SLinus Torvalds 
3010c2b6481SOndrej Zary 	aha1542_outb(bse, CMD_INQUIRY);
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	len = 4;
3041da177e4SLinus Torvalds 	cmdp = &inquiry_result[0];
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	while (len--) {
3072093bfa1SOndrej Zary 		if (!wait_mask(STATUS(bse), DF, DF, 0, 0))
308a13b3722SOndrej Zary 			return 0;
3091da177e4SLinus Torvalds 		*cmdp++ = inb(DATA(bse));
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	/* Reading port should reset DF */
3131da177e4SLinus Torvalds 	if (inb(STATUS(bse)) & DF)
314a13b3722SOndrej Zary 		return 0;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	/* When HACC, command is completed, and we're though testing */
3172093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(bse), HACC, HACC, 0, 0))
318a13b3722SOndrej Zary 		return 0;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	/* Clear interrupts */
3211da177e4SLinus Torvalds 	outb(IRST, CONTROL(bse));
3221da177e4SLinus Torvalds 
323bdebe224SOndrej Zary 	return 1;
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds /* A "high" level interrupt handler */
32787c4d7bcSJeff Garzik static void aha1542_intr_handle(struct Scsi_Host *shost)
3281da177e4SLinus Torvalds {
329e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(shost);
3301da177e4SLinus Torvalds 	void (*my_done) (Scsi_Cmnd *) = NULL;
3311da177e4SLinus Torvalds 	int errstatus, mbi, mbo, mbistatus;
3321da177e4SLinus Torvalds 	int number_serviced;
3331da177e4SLinus Torvalds 	unsigned long flags;
3341da177e4SLinus Torvalds 	Scsi_Cmnd *SCtmp;
3351da177e4SLinus Torvalds 	int flag;
336e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
337e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds #ifdef DEBUG
3401da177e4SLinus Torvalds 	{
3411da177e4SLinus Torvalds 		flag = inb(INTRFLAGS(shost->io_port));
3421da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_intr_handle: ");
3431da177e4SLinus Torvalds 		if (!(flag & ANYINTR))
3441da177e4SLinus Torvalds 			printk("no interrupt?");
3451da177e4SLinus Torvalds 		if (flag & MBIF)
3461da177e4SLinus Torvalds 			printk("MBIF ");
3471da177e4SLinus Torvalds 		if (flag & MBOA)
3481da177e4SLinus Torvalds 			printk("MBOF ");
3491da177e4SLinus Torvalds 		if (flag & HACC)
3501da177e4SLinus Torvalds 			printk("HACC ");
3511da177e4SLinus Torvalds 		if (flag & SCRD)
3521da177e4SLinus Torvalds 			printk("SCRD ");
3531da177e4SLinus Torvalds 		printk("status %02x\n", inb(STATUS(shost->io_port)));
3541da177e4SLinus Torvalds 	};
3551da177e4SLinus Torvalds #endif
3561da177e4SLinus Torvalds 	number_serviced = 0;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	while (1 == 1) {
3591da177e4SLinus Torvalds 		flag = inb(INTRFLAGS(shost->io_port));
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 		/* Check for unusual interrupts.  If any of these happen, we should
3621da177e4SLinus Torvalds 		   probably do something special, but for now just printing a message
3631da177e4SLinus Torvalds 		   is sufficient.  A SCSI reset detected is something that we really
3641da177e4SLinus Torvalds 		   need to deal with in some way. */
3651da177e4SLinus Torvalds 		if (flag & ~MBIF) {
3661da177e4SLinus Torvalds 			if (flag & MBOA)
3671da177e4SLinus Torvalds 				printk("MBOF ");
3681da177e4SLinus Torvalds 			if (flag & HACC)
3691da177e4SLinus Torvalds 				printk("HACC ");
370dfd7c991SOndrej Zary 			if (flag & SCRD)
3711da177e4SLinus Torvalds 				printk("SCRD ");
3721da177e4SLinus Torvalds 		}
3731da177e4SLinus Torvalds 		aha1542_intr_reset(shost->io_port);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		spin_lock_irqsave(&aha1542_lock, flags);
376e98878f7SOndrej Zary 		mbi = aha1542->aha1542_last_mbi_used + 1;
3771da177e4SLinus Torvalds 		if (mbi >= 2 * AHA1542_MAILBOXES)
3781da177e4SLinus Torvalds 			mbi = AHA1542_MAILBOXES;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 		do {
3811da177e4SLinus Torvalds 			if (mb[mbi].status != 0)
3821da177e4SLinus Torvalds 				break;
3831da177e4SLinus Torvalds 			mbi++;
3841da177e4SLinus Torvalds 			if (mbi >= 2 * AHA1542_MAILBOXES)
3851da177e4SLinus Torvalds 				mbi = AHA1542_MAILBOXES;
386e98878f7SOndrej Zary 		} while (mbi != aha1542->aha1542_last_mbi_used);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 		if (mb[mbi].status == 0) {
3891da177e4SLinus Torvalds 			spin_unlock_irqrestore(&aha1542_lock, flags);
3901da177e4SLinus Torvalds 			/* Hmm, no mail.  Must have read it the last time around */
391dfd7c991SOndrej Zary 			if (!number_serviced)
3921da177e4SLinus Torvalds 				printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
3931da177e4SLinus Torvalds 			return;
3941da177e4SLinus Torvalds 		};
3951da177e4SLinus Torvalds 
39610be6250SOndrej Zary 		mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
3971da177e4SLinus Torvalds 		mbistatus = mb[mbi].status;
3981da177e4SLinus Torvalds 		mb[mbi].status = 0;
399e98878f7SOndrej Zary 		aha1542->aha1542_last_mbi_used = mbi;
4001da177e4SLinus Torvalds 		spin_unlock_irqrestore(&aha1542_lock, flags);
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds #ifdef DEBUG
4031da177e4SLinus Torvalds 		{
4041da177e4SLinus Torvalds 			if (ccb[mbo].tarstat | ccb[mbo].hastat)
4051da177e4SLinus Torvalds 				printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
4061da177e4SLinus Torvalds 				       ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
4071da177e4SLinus Torvalds 		};
4081da177e4SLinus Torvalds #endif
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 		if (mbistatus == 3)
4111da177e4SLinus Torvalds 			continue;	/* Aborted command not found */
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds #ifdef DEBUG
4141da177e4SLinus Torvalds 		printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
4151da177e4SLinus Torvalds #endif
4161da177e4SLinus Torvalds 
417e98878f7SOndrej Zary 		SCtmp = aha1542->SCint[mbo];
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 		if (!SCtmp || !SCtmp->scsi_done) {
4201da177e4SLinus Torvalds 			printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
4211da177e4SLinus Torvalds 			printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
4221da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].idlun, mbo);
4231da177e4SLinus Torvalds 			return;
4241da177e4SLinus Torvalds 		}
4251da177e4SLinus Torvalds 		my_done = SCtmp->scsi_done;
4261da177e4SLinus Torvalds 		kfree(SCtmp->host_scribble);
4271da177e4SLinus Torvalds 		SCtmp->host_scribble = NULL;
4281da177e4SLinus Torvalds 		/* Fetch the sense data, and tuck it away, in the required slot.  The
4291da177e4SLinus Torvalds 		   Adaptec automatically fetches it, and there is no guarantee that
4301da177e4SLinus Torvalds 		   we will still have it in the cdb when we come back */
4311da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2)
4321da177e4SLinus Torvalds 			memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
433b80ca4f7SFUJITA Tomonori 			       SCSI_SENSE_BUFFERSIZE);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 		/* is there mail :-) */
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 		/* more error checking left out here */
4391da177e4SLinus Torvalds 		if (mbistatus != 1)
4401da177e4SLinus Torvalds 			/* This is surely wrong, but I don't know what's right */
4411da177e4SLinus Torvalds 			errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
4421da177e4SLinus Torvalds 		else
4431da177e4SLinus Torvalds 			errstatus = 0;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds #ifdef DEBUG
4461da177e4SLinus Torvalds 		if (errstatus)
4471da177e4SLinus Torvalds 			printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
4481da177e4SLinus Torvalds 			       ccb[mbo].hastat, ccb[mbo].tarstat);
4491da177e4SLinus Torvalds #endif
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 		if (ccb[mbo].tarstat == 2) {
4521da177e4SLinus Torvalds #ifdef DEBUG
4531da177e4SLinus Torvalds 			int i;
4541da177e4SLinus Torvalds #endif
4551da177e4SLinus Torvalds 			DEB(printk("aha1542_intr_handle: sense:"));
4561da177e4SLinus Torvalds #ifdef DEBUG
4571da177e4SLinus Torvalds 			for (i = 0; i < 12; i++)
4581da177e4SLinus Torvalds 				printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
4591da177e4SLinus Torvalds 			printk("\n");
4601da177e4SLinus Torvalds #endif
4611da177e4SLinus Torvalds 			/*
4621da177e4SLinus Torvalds 			   DEB(printk("aha1542_intr_handle: buf:"));
4631da177e4SLinus Torvalds 			   for (i = 0; i < bufflen; i++)
4641da177e4SLinus Torvalds 			   printk("%02x ", ((unchar *)buff)[i]);
4651da177e4SLinus Torvalds 			   printk("\n");
4661da177e4SLinus Torvalds 			 */
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 		DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
4691da177e4SLinus Torvalds 		SCtmp->result = errstatus;
470e98878f7SOndrej Zary 		aha1542->SCint[mbo] = NULL;	/* This effectively frees up the mailbox slot, as
4711da177e4SLinus Torvalds 						   far as queuecommand is concerned */
4721da177e4SLinus Torvalds 		my_done(SCtmp);
4731da177e4SLinus Torvalds 		number_serviced++;
4741da177e4SLinus Torvalds 	};
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
47709a44833SOndrej Zary /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
47809a44833SOndrej Zary static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
47909a44833SOndrej Zary {
48009a44833SOndrej Zary 	unsigned long flags;
48109a44833SOndrej Zary 	struct Scsi_Host *shost = dev_id;
48209a44833SOndrej Zary 
48309a44833SOndrej Zary 	spin_lock_irqsave(shost->host_lock, flags);
48409a44833SOndrej Zary 	aha1542_intr_handle(shost);
48509a44833SOndrej Zary 	spin_unlock_irqrestore(shost->host_lock, flags);
48609a44833SOndrej Zary 	return IRQ_HANDLED;
48709a44833SOndrej Zary }
48809a44833SOndrej Zary 
489f281233dSJeff Garzik static int aha1542_queuecommand_lck(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
4901da177e4SLinus Torvalds {
491e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
492cb5b570cSOndrej Zary 	u8 direction;
493cb5b570cSOndrej Zary 	u8 *cmd = (u8 *) SCpnt->cmnd;
494cb5b570cSOndrej Zary 	u8 target = SCpnt->device->id;
495cb5b570cSOndrej Zary 	u8 lun = SCpnt->device->lun;
4961da177e4SLinus Torvalds 	unsigned long flags;
497fc3fdfccSBoaz Harrosh 	int bufflen = scsi_bufflen(SCpnt);
4981da177e4SLinus Torvalds 	int mbo;
499e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
500e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	DEB(int i);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	DEB(if (target > 1) {
5051da177e4SLinus Torvalds 	    SCpnt->result = DID_TIME_OUT << 16;
5061da177e4SLinus Torvalds 	    done(SCpnt); return 0;
5071da177e4SLinus Torvalds 	    }
5081da177e4SLinus Torvalds 	);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	if (*cmd == REQUEST_SENSE) {
5111da177e4SLinus Torvalds 		/* Don't do the command - we have the sense data already */
5121da177e4SLinus Torvalds 		SCpnt->result = 0;
5131da177e4SLinus Torvalds 		done(SCpnt);
5141da177e4SLinus Torvalds 		return 0;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds #ifdef DEBUG
5171da177e4SLinus Torvalds 	if (*cmd == READ_10 || *cmd == WRITE_10)
5181da177e4SLinus Torvalds 		i = xscsi2int(cmd + 2);
5191da177e4SLinus Torvalds 	else if (*cmd == READ_6 || *cmd == WRITE_6)
5201da177e4SLinus Torvalds 		i = scsi2int(cmd + 2);
5211da177e4SLinus Torvalds 	else
5221da177e4SLinus Torvalds 		i = -1;
5231da177e4SLinus Torvalds 	if (done)
5241da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
5251da177e4SLinus Torvalds 	else
5261da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
5271da177e4SLinus Torvalds 	printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
5281da177e4SLinus Torvalds 	for (i = 0; i < SCpnt->cmd_len; i++)
5291da177e4SLinus Torvalds 		printk("%02x ", cmd[i]);
5301da177e4SLinus Torvalds 	printk("\n");
5311da177e4SLinus Torvalds 	if (*cmd == WRITE_10 || *cmd == WRITE_6)
5321da177e4SLinus Torvalds 		return 0;	/* we are still testing, so *don't* write */
5331da177e4SLinus Torvalds #endif
5341da177e4SLinus Torvalds 	/* Use the outgoing mailboxes in a round-robin fashion, because this
5351da177e4SLinus Torvalds 	   is how the host adapter will scan for them */
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
538e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
5391da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
5401da177e4SLinus Torvalds 		mbo = 0;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	do {
543e98878f7SOndrej Zary 		if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
5441da177e4SLinus Torvalds 			break;
5451da177e4SLinus Torvalds 		mbo++;
5461da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
5471da177e4SLinus Torvalds 			mbo = 0;
548e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
5491da177e4SLinus Torvalds 
550e98878f7SOndrej Zary 	if (mb[mbo].status || aha1542->SCint[mbo])
5511da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
5521da177e4SLinus Torvalds 
553e98878f7SOndrej Zary 	aha1542->SCint[mbo] = SCpnt;	/* This will effectively prevent someone else from
5541da177e4SLinus Torvalds 					   screwing with this cdb. */
5551da177e4SLinus Torvalds 
556e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
5571da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds #ifdef DEBUG
5601da177e4SLinus Torvalds 	printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
5611da177e4SLinus Torvalds #endif
5621da177e4SLinus Torvalds 
56310be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	ccb[mbo].cdblen = SCpnt->cmd_len;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	direction = 0;
5701da177e4SLinus Torvalds 	if (*cmd == READ_10 || *cmd == READ_6)
5711da177e4SLinus Torvalds 		direction = 8;
5721da177e4SLinus Torvalds 	else if (*cmd == WRITE_10 || *cmd == WRITE_6)
5731da177e4SLinus Torvalds 		direction = 16;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
5761da177e4SLinus Torvalds 
577fc3fdfccSBoaz Harrosh 	if (bufflen) {
57851cf2249SJens Axboe 		struct scatterlist *sg;
5791da177e4SLinus Torvalds 		struct chain *cptr;
5801da177e4SLinus Torvalds #ifdef DEBUG
5811da177e4SLinus Torvalds 		unsigned char *ptr;
5821da177e4SLinus Torvalds #endif
583fc3fdfccSBoaz Harrosh 		int i, sg_count = scsi_sg_count(SCpnt);
5841da177e4SLinus Torvalds 		ccb[mbo].op = 2;	/* SCSI Initiator Command  w/scatter-gather */
585fc3fdfccSBoaz Harrosh 		SCpnt->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
586fc3fdfccSBoaz Harrosh 		                                         GFP_KERNEL | GFP_DMA);
5871da177e4SLinus Torvalds 		cptr = (struct chain *) SCpnt->host_scribble;
5881da177e4SLinus Torvalds 		if (cptr == NULL) {
5891da177e4SLinus Torvalds 			/* free the claimed mailbox slot */
590e98878f7SOndrej Zary 			aha1542->SCint[mbo] = NULL;
5911da177e4SLinus Torvalds 			return SCSI_MLQUEUE_HOST_BUSY;
5921da177e4SLinus Torvalds 		}
593fc3fdfccSBoaz Harrosh 		scsi_for_each_sg(SCpnt, sg, sg_count, i) {
59410be6250SOndrej Zary 			any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
59510be6250SOndrej Zary 								+ sg->offset);
59651cf2249SJens Axboe 			any2scsi(cptr[i].datalen, sg->length);
5971da177e4SLinus Torvalds 		};
598fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
59910be6250SOndrej Zary 		any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
6001da177e4SLinus Torvalds #ifdef DEBUG
6011da177e4SLinus Torvalds 		printk("cptr %x: ", cptr);
6021da177e4SLinus Torvalds 		ptr = (unsigned char *) cptr;
6031da177e4SLinus Torvalds 		for (i = 0; i < 18; i++)
6041da177e4SLinus Torvalds 			printk("%02x ", ptr[i]);
6051da177e4SLinus Torvalds #endif
6061da177e4SLinus Torvalds 	} else {
6071da177e4SLinus Torvalds 		ccb[mbo].op = 0;	/* SCSI Initiator Command */
6081da177e4SLinus Torvalds 		SCpnt->host_scribble = NULL;
609fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].datalen, 0);
610fc3fdfccSBoaz Harrosh 		any2scsi(ccb[mbo].dataptr, 0);
6111da177e4SLinus Torvalds 	};
6121da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7);	/*SCSI Target Id */
6131da177e4SLinus Torvalds 	ccb[mbo].rsalen = 16;
6141da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
6151da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds #ifdef DEBUG
6181da177e4SLinus Torvalds 	{
6191da177e4SLinus Torvalds 		int i;
6201da177e4SLinus Torvalds 		printk(KERN_DEBUG "aha1542_command: sending.. ");
6211da177e4SLinus Torvalds 		for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
622cb5b570cSOndrej Zary 			printk("%02x ", ((u8 *) &ccb[mbo])[i]);
6231da177e4SLinus Torvalds 	};
6241da177e4SLinus Torvalds #endif
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	if (done) {
627f232d538SOndrej Zary 		DEB(printk("aha1542_queuecommand: now waiting for interrupt "));
6281da177e4SLinus Torvalds 		SCpnt->scsi_done = done;
6291da177e4SLinus Torvalds 		mb[mbo].status = 1;
6300c2b6481SOndrej Zary 		aha1542_outb(SCpnt->device->host->io_port, CMD_START_SCSI);
6311da177e4SLinus Torvalds 	} else
6321da177e4SLinus Torvalds 		printk("aha1542_queuecommand: done can't be NULL\n");
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	return 0;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
637f281233dSJeff Garzik static DEF_SCSI_QCMD(aha1542_queuecommand)
638f281233dSJeff Garzik 
6391da177e4SLinus Torvalds /* Initialize mailboxes */
6401da177e4SLinus Torvalds static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
6411da177e4SLinus Torvalds {
642e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(shpnt);
6431da177e4SLinus Torvalds 	int i;
644e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
645e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
6461da177e4SLinus Torvalds 
647cb5b570cSOndrej Zary 	u8 cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
6501da177e4SLinus Torvalds 		mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
65110be6250SOndrej Zary 		any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
6521da177e4SLinus Torvalds 	};
6531da177e4SLinus Torvalds 	aha1542_intr_reset(bse);	/* reset interrupts, so they don't block */
65410be6250SOndrej Zary 	any2scsi((cmd + 2), isa_virt_to_bus(mb));
65523e6940aSOndrej Zary 	if (aha1542_out(bse, cmd, 5))
6561da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
6571da177e4SLinus Torvalds 	aha1542_intr_reset(bse);
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
660*3a70c006SOndrej Zary static int aha1542_getconfig(int base_io, unsigned int *irq_level, unsigned char *dma_chan, unsigned int *scsi_id)
6611da177e4SLinus Torvalds {
662cb5b570cSOndrej Zary 	u8 inquiry_result[3];
6631da177e4SLinus Torvalds 	int i;
6641da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
6651da177e4SLinus Torvalds 	if (i & DF) {
6661da177e4SLinus Torvalds 		i = inb(DATA(base_io));
6671da177e4SLinus Torvalds 	};
6680c2b6481SOndrej Zary 	aha1542_outb(base_io, CMD_RETCONF);
669f8846be3SOndrej Zary 	aha1542_in(base_io, inquiry_result, 3, 0);
6702093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
6711da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query board settings\n");
6721da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
6731da177e4SLinus Torvalds 	switch (inquiry_result[0]) {
6741da177e4SLinus Torvalds 	case 0x80:
6751da177e4SLinus Torvalds 		*dma_chan = 7;
6761da177e4SLinus Torvalds 		break;
6771da177e4SLinus Torvalds 	case 0x40:
6781da177e4SLinus Torvalds 		*dma_chan = 6;
6791da177e4SLinus Torvalds 		break;
6801da177e4SLinus Torvalds 	case 0x20:
6811da177e4SLinus Torvalds 		*dma_chan = 5;
6821da177e4SLinus Torvalds 		break;
6831da177e4SLinus Torvalds 	case 0x01:
6841da177e4SLinus Torvalds 		*dma_chan = 0;
6851da177e4SLinus Torvalds 		break;
6861da177e4SLinus Torvalds 	case 0:
6871da177e4SLinus Torvalds 		/* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
6881da177e4SLinus Torvalds 		   Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
6891da177e4SLinus Torvalds 		*dma_chan = 0xFF;
6901da177e4SLinus Torvalds 		break;
6911da177e4SLinus Torvalds 	default:
6921da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec DMA priority.  Disabling board\n");
6931da177e4SLinus Torvalds 		return -1;
6941da177e4SLinus Torvalds 	};
6951da177e4SLinus Torvalds 	switch (inquiry_result[1]) {
6961da177e4SLinus Torvalds 	case 0x40:
6971da177e4SLinus Torvalds 		*irq_level = 15;
6981da177e4SLinus Torvalds 		break;
6991da177e4SLinus Torvalds 	case 0x20:
7001da177e4SLinus Torvalds 		*irq_level = 14;
7011da177e4SLinus Torvalds 		break;
7021da177e4SLinus Torvalds 	case 0x8:
7031da177e4SLinus Torvalds 		*irq_level = 12;
7041da177e4SLinus Torvalds 		break;
7051da177e4SLinus Torvalds 	case 0x4:
7061da177e4SLinus Torvalds 		*irq_level = 11;
7071da177e4SLinus Torvalds 		break;
7081da177e4SLinus Torvalds 	case 0x2:
7091da177e4SLinus Torvalds 		*irq_level = 10;
7101da177e4SLinus Torvalds 		break;
7111da177e4SLinus Torvalds 	case 0x1:
7121da177e4SLinus Torvalds 		*irq_level = 9;
7131da177e4SLinus Torvalds 		break;
7141da177e4SLinus Torvalds 	default:
7151da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to determine Adaptec IRQ level.  Disabling board\n");
7161da177e4SLinus Torvalds 		return -1;
7171da177e4SLinus Torvalds 	};
7181da177e4SLinus Torvalds 	*scsi_id = inquiry_result[2] & 7;
7191da177e4SLinus Torvalds 	return 0;
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds /* This function should only be called for 1542C boards - we can detect
7231da177e4SLinus Torvalds    the special firmware settings and unlock the board */
7241da177e4SLinus Torvalds 
725643a7c43SOndrej Zary static int aha1542_mbenable(int base)
7261da177e4SLinus Torvalds {
727cb5b570cSOndrej Zary 	static u8 mbenable_cmd[3];
728cb5b570cSOndrej Zary 	static u8 mbenable_result[2];
7291da177e4SLinus Torvalds 	int retval;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	retval = BIOS_TRANSLATION_6432;
7321da177e4SLinus Torvalds 
7330c2b6481SOndrej Zary 	aha1542_outb(base, CMD_EXTBIOS);
734f8846be3SOndrej Zary 	if (aha1542_in(base, mbenable_result, 2, 100))
7351da177e4SLinus Torvalds 		return retval;
7362093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 100))
7372093bfa1SOndrej Zary 		goto fail;
7381da177e4SLinus Torvalds 	aha1542_intr_reset(base);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
7411da177e4SLinus Torvalds 		mbenable_cmd[0] = CMD_MBENABLE;
7421da177e4SLinus Torvalds 		mbenable_cmd[1] = 0;
7431da177e4SLinus Torvalds 		mbenable_cmd[2] = mbenable_result[1];
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 		if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
7461da177e4SLinus Torvalds 			retval = BIOS_TRANSLATION_25563;
7471da177e4SLinus Torvalds 
74823e6940aSOndrej Zary 		if (aha1542_out(base, mbenable_cmd, 3))
7492093bfa1SOndrej Zary 			goto fail;
7501da177e4SLinus Torvalds 	};
7511da177e4SLinus Torvalds 	while (0) {
7521da177e4SLinus Torvalds fail:
7531da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
7541da177e4SLinus Torvalds 	}
7551da177e4SLinus Torvalds 	aha1542_intr_reset(base);
7561da177e4SLinus Torvalds 	return retval;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
760643a7c43SOndrej Zary static int aha1542_query(int base_io, int *transl)
7611da177e4SLinus Torvalds {
762cb5b570cSOndrej Zary 	u8 inquiry_result[4];
7631da177e4SLinus Torvalds 	int i;
7641da177e4SLinus Torvalds 	i = inb(STATUS(base_io));
7651da177e4SLinus Torvalds 	if (i & DF) {
7661da177e4SLinus Torvalds 		i = inb(DATA(base_io));
7671da177e4SLinus Torvalds 	};
7680c2b6481SOndrej Zary 	aha1542_outb(base_io, CMD_INQUIRY);
769f8846be3SOndrej Zary 	aha1542_in(base_io, inquiry_result, 4, 0);
7702093bfa1SOndrej Zary 	if (!wait_mask(INTRFLAGS(base_io), INTRMASK, HACC, 0, 0))
7711da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542_detect: query card type\n");
7721da177e4SLinus Torvalds 	aha1542_intr_reset(base_io);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	*transl = BIOS_TRANSLATION_6432;	/* Default case */
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	/* For an AHA1740 series board, we ignore the board since there is a
7771da177e4SLinus Torvalds 	   hardware bug which can lead to wrong blocks being returned if the board
7781da177e4SLinus Torvalds 	   is operating in the 1542 emulation mode.  Since there is an extended mode
7791da177e4SLinus Torvalds 	   driver, we simply ignore the board and let the 1740 driver pick it up.
7801da177e4SLinus Torvalds 	 */
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	if (inquiry_result[0] == 0x43) {
7831da177e4SLinus Torvalds 		printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
7841da177e4SLinus Torvalds 		return 1;
7851da177e4SLinus Torvalds 	};
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	/* Always call this - boards that do not support extended bios translation
7881da177e4SLinus Torvalds 	   will ignore the command, and we will set the proper default */
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	*transl = aha1542_mbenable(base_io);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	return 0;
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds #ifndef MODULE
7961da177e4SLinus Torvalds static char *setup_str[MAXBOARDS] __initdata;
7971da177e4SLinus Torvalds static int setup_idx = 0;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds static void __init aha1542_setup(char *str, int *ints)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
8021da177e4SLinus Torvalds 	int setup_portbase;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	if (setup_idx >= MAXBOARDS) {
8051da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
8061da177e4SLinus Torvalds 		printk(KERN_ERR "   Entryline 1: %s\n", setup_str[0]);
8071da177e4SLinus Torvalds 		printk(KERN_ERR "   Entryline 2: %s\n", setup_str[1]);
8081da177e4SLinus Torvalds 		printk(KERN_ERR "   This line:   %s\n", str);
8091da177e4SLinus Torvalds 		return;
8101da177e4SLinus Torvalds 	}
8111da177e4SLinus Torvalds 	if (ints[0] < 1 || ints[0] > 4) {
8121da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: %s\n", str);
8131da177e4SLinus Torvalds 		printk(ahausage);
8141da177e4SLinus Torvalds 		printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 	setup_called[setup_idx] = ints[0];
8171da177e4SLinus Torvalds 	setup_str[setup_idx] = str;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	setup_portbase = ints[0] >= 1 ? ints[1] : 0;	/* Preserve the default value.. */
8201da177e4SLinus Torvalds 	setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
8211da177e4SLinus Torvalds 	setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
8221da177e4SLinus Torvalds 	if (ints[0] >= 4)
8231da177e4SLinus Torvalds 	{
8241da177e4SLinus Torvalds 		int atbt = -1;
8251da177e4SLinus Torvalds 		switch (ints[4]) {
8261da177e4SLinus Torvalds 		case 5:
8271da177e4SLinus Torvalds 			atbt = 0x00;
8281da177e4SLinus Torvalds 			break;
8291da177e4SLinus Torvalds 		case 6:
8301da177e4SLinus Torvalds 			atbt = 0x04;
8311da177e4SLinus Torvalds 			break;
8321da177e4SLinus Torvalds 		case 7:
8331da177e4SLinus Torvalds 			atbt = 0x01;
8341da177e4SLinus Torvalds 			break;
8351da177e4SLinus Torvalds 		case 8:
8361da177e4SLinus Torvalds 			atbt = 0x02;
8371da177e4SLinus Torvalds 			break;
8381da177e4SLinus Torvalds 		case 10:
8391da177e4SLinus Torvalds 			atbt = 0x03;
8401da177e4SLinus Torvalds 			break;
8411da177e4SLinus Torvalds 		default:
8421da177e4SLinus Torvalds 			printk(KERN_ERR "aha1542: %s\n", str);
8431da177e4SLinus Torvalds 			printk(ahausage);
8441da177e4SLinus Torvalds 			printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s.  Using jumper defaults.\n");
8451da177e4SLinus Torvalds 			break;
8461da177e4SLinus Torvalds 		}
8471da177e4SLinus Torvalds 		setup_dmaspeed[setup_idx] = atbt;
8481da177e4SLinus Torvalds 	}
8491da177e4SLinus Torvalds 	if (setup_portbase != 0)
8501da177e4SLinus Torvalds 		bases[setup_idx] = setup_portbase;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	++setup_idx;
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds static int __init do_setup(char *str)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	int ints[5];
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	int count=setup_idx;
8601da177e4SLinus Torvalds 
8616391a113STobias Klauser 	get_options(str, ARRAY_SIZE(ints), ints);
8621da177e4SLinus Torvalds 	aha1542_setup(str,ints);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	return count<setup_idx;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds __setup("aha1542=",do_setup);
8681da177e4SLinus Torvalds #endif
8691da177e4SLinus Torvalds 
870b847fd0dSOndrej Zary /* Set the Bus on/off-times as not to ruin floppy performance */
871b847fd0dSOndrej Zary static void aha1542_set_bus_times(int indx)
872b847fd0dSOndrej Zary {
873b847fd0dSOndrej Zary 	unsigned int base_io = bases[indx];
874b847fd0dSOndrej Zary 	u8 oncmd[] = {CMD_BUSON_TIME, 7};
875b847fd0dSOndrej Zary 	u8 offcmd[] = {CMD_BUSOFF_TIME, 5};
876b847fd0dSOndrej Zary 
877b847fd0dSOndrej Zary 	if (setup_called[indx]) {
878b847fd0dSOndrej Zary 		oncmd[1] = setup_buson[indx];
879b847fd0dSOndrej Zary 		offcmd[1] = setup_busoff[indx];
880b847fd0dSOndrej Zary 	}
881b847fd0dSOndrej Zary 	aha1542_intr_reset(base_io);
88223e6940aSOndrej Zary 	if (aha1542_out(base_io, oncmd, 2))
883b847fd0dSOndrej Zary 		goto fail;
884b847fd0dSOndrej Zary 	aha1542_intr_reset(base_io);
88523e6940aSOndrej Zary 	if (aha1542_out(base_io, offcmd, 2))
886b847fd0dSOndrej Zary 		goto fail;
887b847fd0dSOndrej Zary 	if (setup_dmaspeed[indx] >= 0) {
888b847fd0dSOndrej Zary 		u8 dmacmd[] = {CMD_DMASPEED, 0};
889b847fd0dSOndrej Zary 		dmacmd[1] = setup_dmaspeed[indx];
890b847fd0dSOndrej Zary 		aha1542_intr_reset(base_io);
89123e6940aSOndrej Zary 		if (aha1542_out(base_io, dmacmd, 2))
892b847fd0dSOndrej Zary 			goto fail;
893b847fd0dSOndrej Zary 	}
894b847fd0dSOndrej Zary 	aha1542_intr_reset(base_io);
895b847fd0dSOndrej Zary 	return;
896b847fd0dSOndrej Zary fail:
897b847fd0dSOndrej Zary 	printk(KERN_ERR "setting bus on/off-time failed\n");
898b847fd0dSOndrej Zary 	aha1542_intr_reset(base_io);
899b847fd0dSOndrej Zary }
900b847fd0dSOndrej Zary 
9011da177e4SLinus Torvalds /* return non-zero on detection */
902643a7c43SOndrej Zary static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
9031da177e4SLinus Torvalds {
904*3a70c006SOndrej Zary 	unsigned int base_io = bases[indx];
905*3a70c006SOndrej Zary 	struct Scsi_Host *shpnt;
906e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542;
9071da177e4SLinus Torvalds 
908*3a70c006SOndrej Zary 	if (base_io == 0)
909643a7c43SOndrej Zary 		return NULL;
9101da177e4SLinus Torvalds 
911*3a70c006SOndrej Zary 	if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
912*3a70c006SOndrej Zary 		return NULL;
913*3a70c006SOndrej Zary 
914*3a70c006SOndrej Zary 	shpnt = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
915*3a70c006SOndrej Zary 	if (!shpnt)
916*3a70c006SOndrej Zary 		goto release;
917*3a70c006SOndrej Zary 	aha1542 = shost_priv(shpnt);
918*3a70c006SOndrej Zary 
919*3a70c006SOndrej Zary 	if (!aha1542_test_port(base_io, shpnt))
920*3a70c006SOndrej Zary 		goto unregister;
9211da177e4SLinus Torvalds 
922b847fd0dSOndrej Zary 	aha1542_set_bus_times(indx);
923*3a70c006SOndrej Zary 	if (aha1542_query(base_io, &aha1542->bios_translation))
924*3a70c006SOndrej Zary 		goto unregister;
925*3a70c006SOndrej Zary 	if (aha1542_getconfig(base_io, &shpnt->irq, &shpnt->dma_channel, &shpnt->this_id) == -1)
9261da177e4SLinus Torvalds 		goto unregister;
9271da177e4SLinus Torvalds 
928*3a70c006SOndrej Zary 	printk(KERN_INFO "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d", shpnt->this_id, base_io, shpnt->irq);
929*3a70c006SOndrej Zary 	if (shpnt->dma_channel != 0xFF)
930*3a70c006SOndrej Zary 		printk(", DMA %d", shpnt->dma_channel);
9311da177e4SLinus Torvalds 	printk("\n");
932*3a70c006SOndrej Zary 	if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
933*3a70c006SOndrej Zary 		printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	setup_mailboxes(base_io, shpnt);
9361da177e4SLinus Torvalds 
937*3a70c006SOndrej Zary 	if (request_irq(shpnt->irq, do_aha1542_intr_handle, 0,
93887c4d7bcSJeff Garzik 					"aha1542", shpnt)) {
9391da177e4SLinus Torvalds 		printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
9401da177e4SLinus Torvalds 		goto unregister;
9411da177e4SLinus Torvalds 	}
942*3a70c006SOndrej Zary 	if (shpnt->dma_channel != 0xFF) {
943*3a70c006SOndrej Zary 		if (request_dma(shpnt->dma_channel, "aha1542")) {
9441da177e4SLinus Torvalds 			printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
945*3a70c006SOndrej Zary 			goto free_irq;
9461da177e4SLinus Torvalds 		}
947*3a70c006SOndrej Zary 		if (shpnt->dma_channel == 0 || shpnt->dma_channel >= 5) {
948*3a70c006SOndrej Zary 			set_dma_mode(shpnt->dma_channel, DMA_MODE_CASCADE);
949*3a70c006SOndrej Zary 			enable_dma(shpnt->dma_channel);
9501da177e4SLinus Torvalds 		}
9511da177e4SLinus Torvalds 	}
95287c4d7bcSJeff Garzik 
9531da177e4SLinus Torvalds 	shpnt->unique_id = base_io;
9541da177e4SLinus Torvalds 	shpnt->io_port = base_io;
955*3a70c006SOndrej Zary 	shpnt->n_io_port = AHA1542_REGION_SIZE;
956*3a70c006SOndrej Zary 	aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
957*3a70c006SOndrej Zary 	aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
9581da177e4SLinus Torvalds 
959*3a70c006SOndrej Zary 	if (scsi_add_host(shpnt, pdev))
960*3a70c006SOndrej Zary 		goto free_dma;
9611da177e4SLinus Torvalds 
962643a7c43SOndrej Zary 	scsi_scan_host(shpnt);
9631da177e4SLinus Torvalds 
964643a7c43SOndrej Zary 	return shpnt;
965*3a70c006SOndrej Zary free_dma:
966*3a70c006SOndrej Zary 	if (shpnt->dma_channel != 0xff)
967*3a70c006SOndrej Zary 		free_dma(shpnt->dma_channel);
968*3a70c006SOndrej Zary free_irq:
969*3a70c006SOndrej Zary 	free_irq(shpnt->irq, shpnt);
9701da177e4SLinus Torvalds unregister:
971643a7c43SOndrej Zary 	scsi_host_put(shpnt);
972*3a70c006SOndrej Zary release:
973*3a70c006SOndrej Zary 	release_region(base_io, AHA1542_REGION_SIZE);
9741da177e4SLinus Torvalds 
975643a7c43SOndrej Zary 	return NULL;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds static int aha1542_release(struct Scsi_Host *shost)
9791da177e4SLinus Torvalds {
980643a7c43SOndrej Zary 	scsi_remove_host(shost);
9811da177e4SLinus Torvalds 	if (shost->dma_channel != 0xff)
9821da177e4SLinus Torvalds 		free_dma(shost->dma_channel);
983*3a70c006SOndrej Zary 	if (shost->irq)
984*3a70c006SOndrej Zary 		free_irq(shost->irq, shost);
9851da177e4SLinus Torvalds 	if (shost->io_port && shost->n_io_port)
9861da177e4SLinus Torvalds 		release_region(shost->io_port, shost->n_io_port);
987643a7c43SOndrej Zary 	scsi_host_put(shost);
9881da177e4SLinus Torvalds 	return 0;
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds /*
9931da177e4SLinus Torvalds  * This is a device reset.  This is handled by sending a special command
9941da177e4SLinus Torvalds  * to the device.
9951da177e4SLinus Torvalds  */
9961da177e4SLinus Torvalds static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
9971da177e4SLinus Torvalds {
998e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
9991da177e4SLinus Torvalds 	unsigned long flags;
1000e98878f7SOndrej Zary 	struct mailbox *mb = aha1542->mb;
1001cb5b570cSOndrej Zary 	u8 target = SCpnt->device->id;
1002cb5b570cSOndrej Zary 	u8 lun = SCpnt->device->lun;
10031da177e4SLinus Torvalds 	int mbo;
1004e98878f7SOndrej Zary 	struct ccb *ccb = aha1542->ccb;
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	spin_lock_irqsave(&aha1542_lock, flags);
1007e98878f7SOndrej Zary 	mbo = aha1542->aha1542_last_mbo_used + 1;
10081da177e4SLinus Torvalds 	if (mbo >= AHA1542_MAILBOXES)
10091da177e4SLinus Torvalds 		mbo = 0;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	do {
1012e98878f7SOndrej Zary 		if (mb[mbo].status == 0 && aha1542->SCint[mbo] == NULL)
10131da177e4SLinus Torvalds 			break;
10141da177e4SLinus Torvalds 		mbo++;
10151da177e4SLinus Torvalds 		if (mbo >= AHA1542_MAILBOXES)
10161da177e4SLinus Torvalds 			mbo = 0;
1017e98878f7SOndrej Zary 	} while (mbo != aha1542->aha1542_last_mbo_used);
10181da177e4SLinus Torvalds 
1019e98878f7SOndrej Zary 	if (mb[mbo].status || aha1542->SCint[mbo])
10201da177e4SLinus Torvalds 		panic("Unable to find empty mailbox for aha1542.\n");
10211da177e4SLinus Torvalds 
1022e98878f7SOndrej Zary 	aha1542->SCint[mbo] = SCpnt;	/* This will effectively
10231da177e4SLinus Torvalds 					   prevent someone else from
10241da177e4SLinus Torvalds 					   screwing with this cdb. */
10251da177e4SLinus Torvalds 
1026e98878f7SOndrej Zary 	aha1542->aha1542_last_mbo_used = mbo;
10271da177e4SLinus Torvalds 	spin_unlock_irqrestore(&aha1542_lock, flags);
10281da177e4SLinus Torvalds 
102910be6250SOndrej Zary 	any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo]));	/* This gets trashed for some reason */
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	memset(&ccb[mbo], 0, sizeof(struct ccb));
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	ccb[mbo].op = 0x81;	/* BUS DEVICE RESET */
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	ccb[mbo].idlun = (target & 7) << 5 | (lun & 7);		/*SCSI Target Id */
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
10381da177e4SLinus Torvalds 	ccb[mbo].commlinkid = 0;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	/*
10411da177e4SLinus Torvalds 	 * Now tell the 1542 to flush all pending commands for this
10421da177e4SLinus Torvalds 	 * target
10431da177e4SLinus Torvalds 	 */
10440c2b6481SOndrej Zary 	aha1542_outb(SCpnt->device->host->io_port, CMD_START_SCSI);
10451da177e4SLinus Torvalds 
1046017560fcSJeff Garzik 	scmd_printk(KERN_WARNING, SCpnt,
1047017560fcSJeff Garzik 		"Trying device reset for target\n");
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	return SUCCESS;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
10528537cba8SOndrej Zary static int aha1542_reset(Scsi_Cmnd *SCpnt, u8 reset_cmd)
10531da177e4SLinus Torvalds {
1054e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(SCpnt->device->host);
10551da177e4SLinus Torvalds 	int i;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	/*
10581da177e4SLinus Torvalds 	 * This does a scsi reset for all devices on the bus.
10591da177e4SLinus Torvalds 	 * In principle, we could also reset the 1542 - should
10601da177e4SLinus Torvalds 	 * we do this?  Try this first, and we can add that later
10611da177e4SLinus Torvalds 	 * if it turns out to be useful.
10621da177e4SLinus Torvalds 	 */
10638537cba8SOndrej Zary 	outb(reset_cmd, CONTROL(SCpnt->device->host->io_port));
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	/*
10661da177e4SLinus Torvalds 	 * Wait for the thing to settle down a bit.  Unfortunately
10671da177e4SLinus Torvalds 	 * this is going to basically lock up the machine while we
10681da177e4SLinus Torvalds 	 * wait for this to complete.  To be 100% correct, we need to
10691da177e4SLinus Torvalds 	 * check for timeout, and if we are doing something like this
10701da177e4SLinus Torvalds 	 * we are pretty desperate anyways.
10711da177e4SLinus Torvalds 	 */
10721da177e4SLinus Torvalds 	ssleep(4);
10731da177e4SLinus Torvalds 	spin_lock_irq(SCpnt->device->host->host_lock);
10741da177e4SLinus Torvalds 
10752093bfa1SOndrej Zary 	if (!wait_mask(STATUS(SCpnt->device->host->io_port),
1076a13b3722SOndrej Zary 	     STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
1077a13b3722SOndrej Zary 		spin_unlock_irq(SCpnt->device->host->host_lock);
1078a13b3722SOndrej Zary 		return FAILED;
1079a13b3722SOndrej Zary 	}
10801da177e4SLinus Torvalds 	/*
10811da177e4SLinus Torvalds 	 * We need to do this too before the 1542 can interact with
10828537cba8SOndrej Zary 	 * us again after host reset.
10831da177e4SLinus Torvalds 	 */
10848537cba8SOndrej Zary 	if (reset_cmd & HRST)
10851da177e4SLinus Torvalds 		setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
10861da177e4SLinus Torvalds 	/*
10871da177e4SLinus Torvalds 	 * Now try to pick up the pieces.  For all pending commands,
10881da177e4SLinus Torvalds 	 * free any internal data structures, and basically clear things
10891da177e4SLinus Torvalds 	 * out.  We do not try and restart any commands or anything -
10901da177e4SLinus Torvalds 	 * the strategy handler takes care of that crap.
10911da177e4SLinus Torvalds 	 */
10921da177e4SLinus Torvalds 	printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	for (i = 0; i < AHA1542_MAILBOXES; i++) {
1095e98878f7SOndrej Zary 		if (aha1542->SCint[i] != NULL) {
10961da177e4SLinus Torvalds 			Scsi_Cmnd *SCtmp;
1097e98878f7SOndrej Zary 			SCtmp = aha1542->SCint[i];
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 			if (SCtmp->device->soft_reset) {
11001da177e4SLinus Torvalds 				/*
11011da177e4SLinus Torvalds 				 * If this device implements the soft reset option,
11021da177e4SLinus Torvalds 				 * then it is still holding onto the command, and
11031da177e4SLinus Torvalds 				 * may yet complete it.  In this case, we don't
11041da177e4SLinus Torvalds 				 * flush the data.
11051da177e4SLinus Torvalds 				 */
11061da177e4SLinus Torvalds 				continue;
11071da177e4SLinus Torvalds 			}
11081da177e4SLinus Torvalds 			kfree(SCtmp->host_scribble);
11091da177e4SLinus Torvalds 			SCtmp->host_scribble = NULL;
1110e98878f7SOndrej Zary 			aha1542->SCint[i] = NULL;
1111e98878f7SOndrej Zary 			aha1542->mb[i].status = 0;
11121da177e4SLinus Torvalds 		}
11131da177e4SLinus Torvalds 	}
11141da177e4SLinus Torvalds 
1115df0ae249SJeff Garzik  	spin_unlock_irq(SCpnt->device->host->host_lock);
11161da177e4SLinus Torvalds 	return SUCCESS;
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds 
11198537cba8SOndrej Zary static int aha1542_bus_reset(Scsi_Cmnd *SCpnt)
11208537cba8SOndrej Zary {
11218537cba8SOndrej Zary 	return aha1542_reset(SCpnt, SCRST);
11228537cba8SOndrej Zary }
11238537cba8SOndrej Zary 
11248537cba8SOndrej Zary static int aha1542_host_reset(Scsi_Cmnd *SCpnt)
11258537cba8SOndrej Zary {
11268537cba8SOndrej Zary 	return aha1542_reset(SCpnt, HRST | SCRST);
11278537cba8SOndrej Zary }
11288537cba8SOndrej Zary 
11291da177e4SLinus Torvalds static int aha1542_biosparam(struct scsi_device *sdev,
11301da177e4SLinus Torvalds 		struct block_device *bdev, sector_t capacity, int *ip)
11311da177e4SLinus Torvalds {
1132e98878f7SOndrej Zary 	struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
11331da177e4SLinus Torvalds 	int translation_algorithm;
11341da177e4SLinus Torvalds 	int size = capacity;
11351da177e4SLinus Torvalds 
1136e98878f7SOndrej Zary 	translation_algorithm = aha1542->bios_translation;
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
11391da177e4SLinus Torvalds 		/* Please verify that this is the same as what DOS returns */
11401da177e4SLinus Torvalds 		ip[0] = 255;
11411da177e4SLinus Torvalds 		ip[1] = 63;
11421da177e4SLinus Torvalds 		ip[2] = size / 255 / 63;
11431da177e4SLinus Torvalds 	} else {
11441da177e4SLinus Torvalds 		ip[0] = 64;
11451da177e4SLinus Torvalds 		ip[1] = 32;
11461da177e4SLinus Torvalds 		ip[2] = size >> 11;
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	return 0;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds MODULE_LICENSE("GPL");
11521da177e4SLinus Torvalds 
1153d0be4a7dSChristoph Hellwig static struct scsi_host_template driver_template = {
1154643a7c43SOndrej Zary 	.module			= THIS_MODULE,
11551da177e4SLinus Torvalds 	.proc_name		= "aha1542",
11561da177e4SLinus Torvalds 	.name			= "Adaptec 1542",
11571da177e4SLinus Torvalds 	.queuecommand		= aha1542_queuecommand,
11581da177e4SLinus Torvalds 	.eh_device_reset_handler= aha1542_dev_reset,
11591da177e4SLinus Torvalds 	.eh_bus_reset_handler	= aha1542_bus_reset,
11601da177e4SLinus Torvalds 	.eh_host_reset_handler	= aha1542_host_reset,
11611da177e4SLinus Torvalds 	.bios_param		= aha1542_biosparam,
11621da177e4SLinus Torvalds 	.can_queue		= AHA1542_MAILBOXES,
11631da177e4SLinus Torvalds 	.this_id		= 7,
116410be6250SOndrej Zary 	.sg_tablesize		= 16,
116510be6250SOndrej Zary 	.cmd_per_lun		= 1,
11661da177e4SLinus Torvalds 	.unchecked_isa_dma	= 1,
11671da177e4SLinus Torvalds 	.use_clustering		= ENABLE_CLUSTERING,
11681da177e4SLinus Torvalds };
1169643a7c43SOndrej Zary 
1170643a7c43SOndrej Zary static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
1171643a7c43SOndrej Zary {
1172643a7c43SOndrej Zary 	struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
1173643a7c43SOndrej Zary 
1174643a7c43SOndrej Zary 	if (!sh)
1175643a7c43SOndrej Zary 		return 0;
1176643a7c43SOndrej Zary 
1177643a7c43SOndrej Zary 	dev_set_drvdata(pdev, sh);
1178643a7c43SOndrej Zary 	return 1;
1179643a7c43SOndrej Zary }
1180643a7c43SOndrej Zary 
1181643a7c43SOndrej Zary static int aha1542_isa_remove(struct device *pdev,
1182643a7c43SOndrej Zary 				    unsigned int ndev)
1183643a7c43SOndrej Zary {
1184643a7c43SOndrej Zary 	aha1542_release(dev_get_drvdata(pdev));
1185643a7c43SOndrej Zary 	dev_set_drvdata(pdev, NULL);
1186643a7c43SOndrej Zary 	return 0;
1187643a7c43SOndrej Zary }
1188643a7c43SOndrej Zary 
1189643a7c43SOndrej Zary static struct isa_driver aha1542_isa_driver = {
1190643a7c43SOndrej Zary 	.match		= aha1542_isa_match,
1191643a7c43SOndrej Zary 	.remove		= aha1542_isa_remove,
1192643a7c43SOndrej Zary 	.driver		= {
1193643a7c43SOndrej Zary 		.name	= "aha1542"
1194643a7c43SOndrej Zary 	},
1195643a7c43SOndrej Zary };
1196643a7c43SOndrej Zary static int isa_registered;
1197643a7c43SOndrej Zary 
1198643a7c43SOndrej Zary #ifdef CONFIG_PNP
1199643a7c43SOndrej Zary static struct pnp_device_id aha1542_pnp_ids[] = {
1200643a7c43SOndrej Zary 	{ .id = "ADP1542" },
1201643a7c43SOndrej Zary 	{ .id = "" }
1202643a7c43SOndrej Zary };
1203643a7c43SOndrej Zary MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
1204643a7c43SOndrej Zary 
1205643a7c43SOndrej Zary static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
1206643a7c43SOndrej Zary {
1207643a7c43SOndrej Zary 	int indx;
1208643a7c43SOndrej Zary 	struct Scsi_Host *sh;
1209643a7c43SOndrej Zary 
1210643a7c43SOndrej Zary 	for (indx = 0; indx < ARRAY_SIZE(bases); indx++) {
1211643a7c43SOndrej Zary 		if (bases[indx])
1212643a7c43SOndrej Zary 			continue;
1213643a7c43SOndrej Zary 
1214643a7c43SOndrej Zary 		if (pnp_activate_dev(pdev) < 0)
1215643a7c43SOndrej Zary 			continue;
1216643a7c43SOndrej Zary 
1217643a7c43SOndrej Zary 		bases[indx] = pnp_port_start(pdev, 0);
1218643a7c43SOndrej Zary 
1219643a7c43SOndrej Zary 		/* The card can be queried for its DMA, we have
1220643a7c43SOndrej Zary 		   the DMA set up that is enough */
1221643a7c43SOndrej Zary 
1222643a7c43SOndrej Zary 		printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
1223643a7c43SOndrej Zary 	}
1224643a7c43SOndrej Zary 
1225643a7c43SOndrej Zary 	sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
1226643a7c43SOndrej Zary 	if (!sh)
1227643a7c43SOndrej Zary 		return -ENODEV;
1228643a7c43SOndrej Zary 
1229643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, sh);
1230643a7c43SOndrej Zary 	return 0;
1231643a7c43SOndrej Zary }
1232643a7c43SOndrej Zary 
1233643a7c43SOndrej Zary static void aha1542_pnp_remove(struct pnp_dev *pdev)
1234643a7c43SOndrej Zary {
1235643a7c43SOndrej Zary 	aha1542_release(pnp_get_drvdata(pdev));
1236643a7c43SOndrej Zary 	pnp_set_drvdata(pdev, NULL);
1237643a7c43SOndrej Zary }
1238643a7c43SOndrej Zary 
1239643a7c43SOndrej Zary static struct pnp_driver aha1542_pnp_driver = {
1240643a7c43SOndrej Zary 	.name		= "aha1542",
1241643a7c43SOndrej Zary 	.id_table	= aha1542_pnp_ids,
1242643a7c43SOndrej Zary 	.probe		= aha1542_pnp_probe,
1243643a7c43SOndrej Zary 	.remove		= aha1542_pnp_remove,
1244643a7c43SOndrej Zary };
1245643a7c43SOndrej Zary static int pnp_registered;
1246643a7c43SOndrej Zary #endif /* CONFIG_PNP */
1247643a7c43SOndrej Zary 
1248643a7c43SOndrej Zary static int __init aha1542_init(void)
1249643a7c43SOndrej Zary {
1250643a7c43SOndrej Zary 	int ret = 0;
1251643a7c43SOndrej Zary #ifdef MODULE
1252643a7c43SOndrej Zary 	int atbt = -1;
1253643a7c43SOndrej Zary 
1254643a7c43SOndrej Zary 	bases[0] = aha1542[0];
1255643a7c43SOndrej Zary 	setup_buson[0] = aha1542[1];
1256643a7c43SOndrej Zary 	setup_busoff[0] = aha1542[2];
1257643a7c43SOndrej Zary 
1258643a7c43SOndrej Zary 	switch (aha1542[3]) {
1259643a7c43SOndrej Zary 	case 5:
1260643a7c43SOndrej Zary 		atbt = 0x00;
1261643a7c43SOndrej Zary 		break;
1262643a7c43SOndrej Zary 	case 6:
1263643a7c43SOndrej Zary 		atbt = 0x04;
1264643a7c43SOndrej Zary 		break;
1265643a7c43SOndrej Zary 	case 7:
1266643a7c43SOndrej Zary 		atbt = 0x01;
1267643a7c43SOndrej Zary 		break;
1268643a7c43SOndrej Zary 	case 8:
1269643a7c43SOndrej Zary 		atbt = 0x02;
1270643a7c43SOndrej Zary 		break;
1271643a7c43SOndrej Zary 	case 10:
1272643a7c43SOndrej Zary 		atbt = 0x03;
1273643a7c43SOndrej Zary 		break;
1274643a7c43SOndrej Zary 	};
1275643a7c43SOndrej Zary 	setup_dmaspeed[0] = atbt;
1276643a7c43SOndrej Zary #endif
1277643a7c43SOndrej Zary 
1278643a7c43SOndrej Zary #ifdef CONFIG_PNP
1279643a7c43SOndrej Zary 	if (isapnp) {
1280643a7c43SOndrej Zary 		ret = pnp_register_driver(&aha1542_pnp_driver);
1281643a7c43SOndrej Zary 		if (!ret)
1282643a7c43SOndrej Zary 			pnp_registered = 1;
1283643a7c43SOndrej Zary 	}
1284643a7c43SOndrej Zary #endif
1285643a7c43SOndrej Zary 	ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
1286643a7c43SOndrej Zary 	if (!ret)
1287643a7c43SOndrej Zary 		isa_registered = 1;
1288643a7c43SOndrej Zary 
1289643a7c43SOndrej Zary #ifdef CONFIG_PNP
1290643a7c43SOndrej Zary 	if (pnp_registered)
1291643a7c43SOndrej Zary 		ret = 0;
1292643a7c43SOndrej Zary #endif
1293643a7c43SOndrej Zary 	if (isa_registered)
1294643a7c43SOndrej Zary 		ret = 0;
1295643a7c43SOndrej Zary 
1296643a7c43SOndrej Zary 	return ret;
1297643a7c43SOndrej Zary }
1298643a7c43SOndrej Zary 
1299643a7c43SOndrej Zary static void __exit aha1542_exit(void)
1300643a7c43SOndrej Zary {
1301643a7c43SOndrej Zary #ifdef CONFIG_PNP
1302643a7c43SOndrej Zary 	if (pnp_registered)
1303643a7c43SOndrej Zary 		pnp_unregister_driver(&aha1542_pnp_driver);
1304643a7c43SOndrej Zary #endif
1305643a7c43SOndrej Zary 	if (isa_registered)
1306643a7c43SOndrej Zary 		isa_unregister_driver(&aha1542_isa_driver);
1307643a7c43SOndrej Zary }
1308643a7c43SOndrej Zary 
1309643a7c43SOndrej Zary module_init(aha1542_init);
1310643a7c43SOndrej Zary module_exit(aha1542_exit);
1311