xref: /openbmc/linux/drivers/scsi/arm/fas216.c (revision dc0d79b5c9489da52ada177bfc4bf4e38b8e556f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/acorn/scsi/fas216.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1997-2003 Russell King
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
81da177e4SLinus Torvalds  * published by the Free Software Foundation.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
111da177e4SLinus Torvalds  * other sources, including:
121da177e4SLinus Torvalds  *   the AMD Am53CF94 data sheet
131da177e4SLinus Torvalds  *   the AMD Am53C94 data sheet
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * This is a generic driver.  To use it, have a look at cumana_2.c.  You
161da177e4SLinus Torvalds  * should define your own structure that overlays FAS216_Info, eg:
171da177e4SLinus Torvalds  * struct my_host_data {
181da177e4SLinus Torvalds  *    FAS216_Info info;
191da177e4SLinus Torvalds  *    ... my host specific data ...
201da177e4SLinus Torvalds  * };
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * Changelog:
231da177e4SLinus Torvalds  *  30-08-1997	RMK	Created
241da177e4SLinus Torvalds  *  14-09-1997	RMK	Started disconnect support
251da177e4SLinus Torvalds  *  08-02-1998	RMK	Corrected real DMA support
261da177e4SLinus Torvalds  *  15-02-1998	RMK	Started sync xfer support
271da177e4SLinus Torvalds  *  06-04-1998	RMK	Tightened conditions for printing incomplete
281da177e4SLinus Torvalds  *			transfers
291da177e4SLinus Torvalds  *  02-05-1998	RMK	Added extra checks in fas216_reset
301da177e4SLinus Torvalds  *  24-05-1998	RMK	Fixed synchronous transfers with period >= 200ns
311da177e4SLinus Torvalds  *  27-06-1998	RMK	Changed asm/delay.h to linux/delay.h
321da177e4SLinus Torvalds  *  26-08-1998	RMK	Improved message support wrt MESSAGE_REJECT
331da177e4SLinus Torvalds  *  02-04-2000	RMK	Converted to use the new error handling, and
341da177e4SLinus Torvalds  *			automatically request sense data upon check
351da177e4SLinus Torvalds  *			condition status from targets.
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds #include <linux/module.h>
381da177e4SLinus Torvalds #include <linux/blkdev.h>
391da177e4SLinus Torvalds #include <linux/kernel.h>
401da177e4SLinus Torvalds #include <linux/string.h>
411da177e4SLinus Torvalds #include <linux/ioport.h>
421da177e4SLinus Torvalds #include <linux/proc_fs.h>
431da177e4SLinus Torvalds #include <linux/delay.h>
441da177e4SLinus Torvalds #include <linux/bitops.h>
451da177e4SLinus Torvalds #include <linux/init.h>
461da177e4SLinus Torvalds #include <linux/interrupt.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #include <asm/dma.h>
491da177e4SLinus Torvalds #include <asm/io.h>
501da177e4SLinus Torvalds #include <asm/irq.h>
511da177e4SLinus Torvalds #include <asm/ecard.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include "../scsi.h"
54db9dff36S #include <scsi/scsi_dbg.h>
551da177e4SLinus Torvalds #include <scsi/scsi_host.h>
561da177e4SLinus Torvalds #include "fas216.h"
571da177e4SLinus Torvalds #include "scsi.h"
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* NOTE: SCSI2 Synchronous transfers *require* DMA according to
601da177e4SLinus Torvalds  *  the data sheet.  This restriction is crazy, especially when
611da177e4SLinus Torvalds  *  you only want to send 16 bytes!  What were the guys who
621da177e4SLinus Torvalds  *  designed this chip on at that time?  Did they read the SCSI2
631da177e4SLinus Torvalds  *  spec at all?  The following sections are taken from the SCSI2
641da177e4SLinus Torvalds  *  standard (s2r10) concerning this:
651da177e4SLinus Torvalds  *
661da177e4SLinus Torvalds  * > IMPLEMENTORS NOTES:
671da177e4SLinus Torvalds  * >   (1)  Re-negotiation at every selection is not recommended, since a
681da177e4SLinus Torvalds  * >   significant performance impact is likely.
691da177e4SLinus Torvalds  *
701da177e4SLinus Torvalds  * >  The implied synchronous agreement shall remain in effect until a BUS DEVICE
711da177e4SLinus Torvalds  * >  RESET message is received, until a hard reset condition occurs, or until one
721da177e4SLinus Torvalds  * >  of the two SCSI devices elects to modify the agreement.  The default data
731da177e4SLinus Torvalds  * >  transfer mode is asynchronous data transfer mode.  The default data transfer
741da177e4SLinus Torvalds  * >  mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
751da177e4SLinus Torvalds  * >  reset condition.
761da177e4SLinus Torvalds  *
771da177e4SLinus Torvalds  *  In total, this means that once you have elected to use synchronous
781da177e4SLinus Torvalds  *  transfers, you must always use DMA.
791da177e4SLinus Torvalds  *
801da177e4SLinus Torvalds  *  I was thinking that this was a good chip until I found this restriction ;(
811da177e4SLinus Torvalds  */
821da177e4SLinus Torvalds #define SCSI2_SYNC
831da177e4SLinus Torvalds #undef  SCSI2_TAG
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #undef DEBUG_CONNECT
861da177e4SLinus Torvalds #undef DEBUG_MESSAGES
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #undef CHECK_STRUCTURE
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #define LOG_CONNECT		(1 << 0)
911da177e4SLinus Torvalds #define LOG_BUSSERVICE		(1 << 1)
921da177e4SLinus Torvalds #define LOG_FUNCTIONDONE	(1 << 2)
931da177e4SLinus Torvalds #define LOG_MESSAGES		(1 << 3)
941da177e4SLinus Torvalds #define LOG_BUFFER		(1 << 4)
951da177e4SLinus Torvalds #define LOG_ERROR		(1 << 8)
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds static int level_mask = LOG_ERROR;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds module_param(level_mask, int, 0644);
1001da177e4SLinus Torvalds 
101*dc0d79b5SArnd Bergmann #ifndef MODULE
1021da177e4SLinus Torvalds static int __init fas216_log_setup(char *str)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	char *s;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	level_mask = 0;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	while ((s = strsep(&str, ",")) != NULL) {
1091da177e4SLinus Torvalds 		switch (s[0]) {
1101da177e4SLinus Torvalds 		case 'a':
1111da177e4SLinus Torvalds 			if (strcmp(s, "all") == 0)
1121da177e4SLinus Torvalds 				level_mask |= -1;
1131da177e4SLinus Torvalds 			break;
1141da177e4SLinus Torvalds 		case 'b':
1151da177e4SLinus Torvalds 			if (strncmp(s, "bus", 3) == 0)
1161da177e4SLinus Torvalds 				level_mask |= LOG_BUSSERVICE;
1171da177e4SLinus Torvalds 			if (strncmp(s, "buf", 3) == 0)
1181da177e4SLinus Torvalds 				level_mask |= LOG_BUFFER;
1191da177e4SLinus Torvalds 			break;
1201da177e4SLinus Torvalds 		case 'c':
1211da177e4SLinus Torvalds 			level_mask |= LOG_CONNECT;
1221da177e4SLinus Torvalds 			break;
1231da177e4SLinus Torvalds 		case 'e':
1241da177e4SLinus Torvalds 			level_mask |= LOG_ERROR;
1251da177e4SLinus Torvalds 			break;
1261da177e4SLinus Torvalds 		case 'm':
1271da177e4SLinus Torvalds 			level_mask |= LOG_MESSAGES;
1281da177e4SLinus Torvalds 			break;
1291da177e4SLinus Torvalds 		case 'n':
1301da177e4SLinus Torvalds 			if (strcmp(s, "none") == 0)
1311da177e4SLinus Torvalds 				level_mask = 0;
1321da177e4SLinus Torvalds 			break;
1331da177e4SLinus Torvalds 		case 's':
1341da177e4SLinus Torvalds 			level_mask |= LOG_FUNCTIONDONE;
1351da177e4SLinus Torvalds 			break;
1361da177e4SLinus Torvalds 		}
1371da177e4SLinus Torvalds 	}
1381da177e4SLinus Torvalds 	return 1;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds __setup("fas216_logging=", fas216_log_setup);
142*dc0d79b5SArnd Bergmann #endif
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	unsigned int off = reg << info->scsi.io_shift;
1471da177e4SLinus Torvalds 	return readb(info->scsi.io_base + off);
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	unsigned int off = reg << info->scsi.io_shift;
1531da177e4SLinus Torvalds 	writeb(val, info->scsi.io_base + off);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds static void fas216_dumpstate(FAS216_Info *info)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	unsigned char is, stat, inst;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	is   = fas216_readb(info, REG_IS);
1611da177e4SLinus Torvalds 	stat = fas216_readb(info, REG_STAT);
1621da177e4SLinus Torvalds 	inst = fas216_readb(info, REG_INST);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
1651da177e4SLinus Torvalds 	       " INST=%02X IS=%02X CFIS=%02X",
1661da177e4SLinus Torvalds 		fas216_readb(info, REG_CTCL),
1671da177e4SLinus Torvalds 		fas216_readb(info, REG_CTCM),
1681da177e4SLinus Torvalds 		fas216_readb(info, REG_CMD),  stat, inst, is,
1691da177e4SLinus Torvalds 		fas216_readb(info, REG_CFIS));
1701da177e4SLinus Torvalds 	printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
1711da177e4SLinus Torvalds 		fas216_readb(info, REG_CNTL1),
1721da177e4SLinus Torvalds 		fas216_readb(info, REG_CNTL2),
1731da177e4SLinus Torvalds 		fas216_readb(info, REG_CNTL3),
1741da177e4SLinus Torvalds 		fas216_readb(info, REG_CTCH));
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1770a04137eSChristoph Hellwig static void print_SCp(struct scsi_pointer *SCp, const char *prefix, const char *suffix)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
1801da177e4SLinus Torvalds 		prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
1811da177e4SLinus Torvalds 		SCp->buffers_residual, suffix);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
18448968177SArnd Bergmann #ifdef CHECK_STRUCTURE
1851da177e4SLinus Torvalds static void fas216_dumpinfo(FAS216_Info *info)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	static int used = 0;
1881da177e4SLinus Torvalds 	int i;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	if (used++)
1911da177e4SLinus Torvalds 		return;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	printk("FAS216_Info=\n");
1941da177e4SLinus Torvalds 	printk("  { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
1951da177e4SLinus Torvalds 		info->magic_start, info->host, info->SCpnt,
1961da177e4SLinus Torvalds 		info->origSCpnt);
1971da177e4SLinus Torvalds 	printk("    scsi={ io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
1981da177e4SLinus Torvalds 		info->scsi.io_shift, info->scsi.irq,
1991da177e4SLinus Torvalds 		info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
2001da177e4SLinus Torvalds 		info->scsi.cfg[3]);
2011da177e4SLinus Torvalds 	printk("           type=%p phase=%X\n",
2021da177e4SLinus Torvalds 		info->scsi.type, info->scsi.phase);
2031da177e4SLinus Torvalds 	print_SCp(&info->scsi.SCp, "           SCp={ ", " }\n");
2041da177e4SLinus Torvalds 	printk("      msgs async_stp=%X disconnectable=%d aborting=%d }\n",
2051da177e4SLinus Torvalds 		info->scsi.async_stp,
2061da177e4SLinus Torvalds 		info->scsi.disconnectable, info->scsi.aborting);
2071da177e4SLinus Torvalds 	printk("    stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
2081da177e4SLinus Torvalds 	       "            disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
2091da177e4SLinus Torvalds 		info->stats.queues, info->stats.removes, info->stats.fins,
2101da177e4SLinus Torvalds 		info->stats.reads, info->stats.writes, info->stats.miscs,
2111da177e4SLinus Torvalds 		info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
2121da177e4SLinus Torvalds 		info->stats.host_resets);
2131da177e4SLinus Torvalds 	printk("    ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
2141da177e4SLinus Torvalds 		info->ifcfg.clockrate, info->ifcfg.select_timeout,
2151da177e4SLinus Torvalds 		info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
2161da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
2171da177e4SLinus Torvalds 		printk("    busyluns[%d]=%08lx dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
2181da177e4SLinus Torvalds 			i, info->busyluns[i], i,
2191da177e4SLinus Torvalds 			info->device[i].disconnect_ok, info->device[i].stp,
2201da177e4SLinus Torvalds 			info->device[i].sof, info->device[i].sync_state);
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 	printk("    dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
2231da177e4SLinus Torvalds 		info->dma.transfer_type, info->dma.setup,
2241da177e4SLinus Torvalds 		info->dma.pseudo, info->dma.stop);
2251da177e4SLinus Torvalds 	printk("    internal_done=%X magic_end=%lX }\n",
2261da177e4SLinus Torvalds 		info->internal_done, info->magic_end);
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds static void __fas216_checkmagic(FAS216_Info *info, const char *func)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	int corruption = 0;
2321da177e4SLinus Torvalds 	if (info->magic_start != MAGIC) {
2331da177e4SLinus Torvalds 		printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
2341da177e4SLinus Torvalds 		corruption++;
2351da177e4SLinus Torvalds 	}
2361da177e4SLinus Torvalds 	if (info->magic_end != MAGIC) {
2371da177e4SLinus Torvalds 		printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
2381da177e4SLinus Torvalds 		corruption++;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	if (corruption) {
2411da177e4SLinus Torvalds 		fas216_dumpinfo(info);
2421da177e4SLinus Torvalds 		panic("scsi memory space corrupted in %s", func);
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds }
245cadbd4a5SHarvey Harrison #define fas216_checkmagic(info) __fas216_checkmagic((info), __func__)
2461da177e4SLinus Torvalds #else
2471da177e4SLinus Torvalds #define fas216_checkmagic(info)
2481da177e4SLinus Torvalds #endif
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds static const char *fas216_bus_phase(int stat)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	static const char *phases[] = {
2531da177e4SLinus Torvalds 		"DATA OUT", "DATA IN",
2541da177e4SLinus Torvalds 		"COMMAND", "STATUS",
2551da177e4SLinus Torvalds 		"MISC OUT", "MISC IN",
2561da177e4SLinus Torvalds 		"MESG OUT", "MESG IN"
2571da177e4SLinus Torvalds 	};
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	return phases[stat & STAT_BUSMASK];
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static const char *fas216_drv_phase(FAS216_Info *info)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	static const char *phases[] = {
2651da177e4SLinus Torvalds 		[PHASE_IDLE]		= "idle",
2661da177e4SLinus Torvalds 		[PHASE_SELECTION]	= "selection",
2671da177e4SLinus Torvalds 		[PHASE_COMMAND]		= "command",
2681da177e4SLinus Torvalds 		[PHASE_DATAOUT]		= "data out",
2691da177e4SLinus Torvalds 		[PHASE_DATAIN]		= "data in",
2701da177e4SLinus Torvalds 		[PHASE_MSGIN]		= "message in",
2711da177e4SLinus Torvalds 		[PHASE_MSGIN_DISCONNECT]= "disconnect",
2721da177e4SLinus Torvalds 		[PHASE_MSGOUT_EXPECT]	= "expect message out",
2731da177e4SLinus Torvalds 		[PHASE_MSGOUT]		= "message out",
2741da177e4SLinus Torvalds 		[PHASE_STATUS]		= "status",
2751da177e4SLinus Torvalds 		[PHASE_DONE]		= "done",
2761da177e4SLinus Torvalds 	};
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	if (info->scsi.phase < ARRAY_SIZE(phases) &&
2791da177e4SLinus Torvalds 	    phases[info->scsi.phase])
2801da177e4SLinus Torvalds 		return phases[info->scsi.phase];
2811da177e4SLinus Torvalds 	return "???";
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds static char fas216_target(FAS216_Info *info)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	if (info->SCpnt)
2871da177e4SLinus Torvalds 		return '0' + info->SCpnt->device->id;
2881da177e4SLinus Torvalds 	else
2891da177e4SLinus Torvalds 		return 'H';
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds static void
2931da177e4SLinus Torvalds fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	static char buf[1024];
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	vsnprintf(buf, sizeof(buf), fmt, ap);
2981da177e4SLinus Torvalds 	printk("scsi%d.%c: %s", info->host->host_no, target, buf);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
301ee0ca6baSHenne static void fas216_log_command(FAS216_Info *info, int level,
302ee0ca6baSHenne 			       struct scsi_cmnd *SCpnt, char *fmt, ...)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	va_list args;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	if (level != 0 && !(level & level_mask))
3071da177e4SLinus Torvalds 		return;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	va_start(args, fmt);
3101da177e4SLinus Torvalds 	fas216_do_log(info, '0' + SCpnt->device->id, fmt, args);
3111da177e4SLinus Torvalds 	va_end(args);
3121da177e4SLinus Torvalds 
313e04ca907SHannes Reinecke 	scsi_print_command(SCpnt);
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static void
3171da177e4SLinus Torvalds fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	va_list args;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	if (level != 0 && !(level & level_mask))
3221da177e4SLinus Torvalds 		return;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	if (target < 0)
3251da177e4SLinus Torvalds 		target = 'H';
3261da177e4SLinus Torvalds 	else
3271da177e4SLinus Torvalds 		target += '0';
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	va_start(args, fmt);
3301da177e4SLinus Torvalds 	fas216_do_log(info, target, fmt, args);
3311da177e4SLinus Torvalds 	va_end(args);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	printk("\n");
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	va_list args;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	if (level != 0 && !(level & level_mask))
3411da177e4SLinus Torvalds 		return;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	va_start(args, fmt);
3441da177e4SLinus Torvalds 	fas216_do_log(info, fas216_target(info), fmt, args);
3451da177e4SLinus Torvalds 	va_end(args);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	printk("\n");
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds #define PH_SIZE	32
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
3531da177e4SLinus Torvalds static int ph_ptr;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds static void add_debug_list(int stat, int ssr, int isr, int ph)
3561da177e4SLinus Torvalds {
3571da177e4SLinus Torvalds 	ph_list[ph_ptr].stat = stat;
3581da177e4SLinus Torvalds 	ph_list[ph_ptr].ssr = ssr;
3591da177e4SLinus Torvalds 	ph_list[ph_ptr].isr = isr;
3601da177e4SLinus Torvalds 	ph_list[ph_ptr].ph = ph;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds static struct { int command; void *from; } cmd_list[8];
3661da177e4SLinus Torvalds static int cmd_ptr;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds static void fas216_cmd(FAS216_Info *info, unsigned int command)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	cmd_list[cmd_ptr].command = command;
3711da177e4SLinus Torvalds 	cmd_list[cmd_ptr].from = __builtin_return_address(0);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	cmd_ptr = (cmd_ptr + 1) & 7;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, command);
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds static void print_debug_list(void)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	int i;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	i = ph_ptr;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	printk(KERN_ERR "SCSI IRQ trail\n");
3851da177e4SLinus Torvalds 	do {
3861da177e4SLinus Torvalds 		printk(" %02x:%02x:%02x:%1x",
3871da177e4SLinus Torvalds 			ph_list[i].stat, ph_list[i].ssr,
3881da177e4SLinus Torvalds 			ph_list[i].isr, ph_list[i].ph);
3891da177e4SLinus Torvalds 		i = (i + 1) & (PH_SIZE - 1);
3901da177e4SLinus Torvalds 		if (((i ^ ph_ptr) & 7) == 0)
3911da177e4SLinus Torvalds 			printk("\n");
3921da177e4SLinus Torvalds 	} while (i != ph_ptr);
3931da177e4SLinus Torvalds 	if ((i ^ ph_ptr) & 7)
3941da177e4SLinus Torvalds 		printk("\n");
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	i = cmd_ptr;
3971da177e4SLinus Torvalds 	printk(KERN_ERR "FAS216 commands: ");
3981da177e4SLinus Torvalds 	do {
3991da177e4SLinus Torvalds 		printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
4001da177e4SLinus Torvalds 		i = (i + 1) & 7;
4011da177e4SLinus Torvalds 	} while (i != cmd_ptr);
4021da177e4SLinus Torvalds 	printk("\n");
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds static void fas216_done(FAS216_Info *info, unsigned int result);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds /**
4081da177e4SLinus Torvalds  * fas216_get_last_msg - retrive last message from the list
4091da177e4SLinus Torvalds  * @info: interface to search
4101da177e4SLinus Torvalds  * @pos: current fifo position
4111da177e4SLinus Torvalds  *
4121da177e4SLinus Torvalds  * Retrieve a last message from the list, using position in fifo.
4131da177e4SLinus Torvalds  */
4141da177e4SLinus Torvalds static inline unsigned short
4151da177e4SLinus Torvalds fas216_get_last_msg(FAS216_Info *info, int pos)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	unsigned short packed_msg = NOP;
4181da177e4SLinus Torvalds 	struct message *msg;
4191da177e4SLinus Torvalds 	int msgnr = 0;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
4221da177e4SLinus Torvalds 		if (pos >= msg->fifo)
4231da177e4SLinus Torvalds 			break;
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	if (msg) {
4271da177e4SLinus Torvalds 		if (msg->msg[0] == EXTENDED_MESSAGE)
4281da177e4SLinus Torvalds 			packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
4291da177e4SLinus Torvalds 		else
4301da177e4SLinus Torvalds 			packed_msg = msg->msg[0];
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	fas216_log(info, LOG_MESSAGES,
4341da177e4SLinus Torvalds 		"Message: %04x found at position %02x\n", packed_msg, pos);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	return packed_msg;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds /**
4401da177e4SLinus Torvalds  * fas216_syncperiod - calculate STP register value
4411da177e4SLinus Torvalds  * @info: state structure for interface connected to device
4421da177e4SLinus Torvalds  * @ns: period in ns (between subsequent bytes)
4431da177e4SLinus Torvalds  *
4441da177e4SLinus Torvalds  * Calculate value to be loaded into the STP register for a given period
4451da177e4SLinus Torvalds  * in ns. Returns a value suitable for REG_STP.
4461da177e4SLinus Torvalds  */
4471da177e4SLinus Torvalds static int fas216_syncperiod(FAS216_Info *info, int ns)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	int value = (info->ifcfg.clockrate * ns) / 1000;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	fas216_checkmagic(info);
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	if (value < 4)
4541da177e4SLinus Torvalds 		value = 4;
4551da177e4SLinus Torvalds 	else if (value > 35)
4561da177e4SLinus Torvalds 		value = 35;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	return value & 31;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds /**
4621da177e4SLinus Torvalds  * fas216_set_sync - setup FAS216 chip for specified transfer period.
4631da177e4SLinus Torvalds  * @info: state structure for interface connected to device
4641da177e4SLinus Torvalds  * @target: target
4651da177e4SLinus Torvalds  *
4661da177e4SLinus Torvalds  * Correctly setup FAS216 chip for specified transfer period.
4671da177e4SLinus Torvalds  * Notes   : we need to switch the chip out of FASTSCSI mode if we have
4681da177e4SLinus Torvalds  *           a transfer period >= 200ns - otherwise the chip will violate
4691da177e4SLinus Torvalds  *           the SCSI timings.
4701da177e4SLinus Torvalds  */
4711da177e4SLinus Torvalds static void fas216_set_sync(FAS216_Info *info, int target)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	unsigned int cntl3;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	fas216_writeb(info, REG_SOF, info->device[target].sof);
4761da177e4SLinus Torvalds 	fas216_writeb(info, REG_STP, info->device[target].stp);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	cntl3 = info->scsi.cfg[2];
4791da177e4SLinus Torvalds 	if (info->device[target].period >= (200 / 4))
4801da177e4SLinus Torvalds 		cntl3 = cntl3 & ~CNTL3_FASTSCSI;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, cntl3);
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /* Synchronous transfer support
4861da177e4SLinus Torvalds  *
4871da177e4SLinus Torvalds  * Note: The SCSI II r10 spec says (5.6.12):
4881da177e4SLinus Torvalds  *
4891da177e4SLinus Torvalds  *  (2)  Due to historical problems with early host adapters that could
4901da177e4SLinus Torvalds  *  not accept an SDTR message, some targets may not initiate synchronous
4911da177e4SLinus Torvalds  *  negotiation after a power cycle as required by this standard.  Host
4921da177e4SLinus Torvalds  *  adapters that support synchronous mode may avoid the ensuing failure
4931da177e4SLinus Torvalds  *  modes when the target is independently power cycled by initiating a
4941da177e4SLinus Torvalds  *  synchronous negotiation on each REQUEST SENSE and INQUIRY command.
4951da177e4SLinus Torvalds  *  This approach increases the SCSI bus overhead and is not recommended
4961da177e4SLinus Torvalds  *  for new implementations.  The correct method is to respond to an
4971da177e4SLinus Torvalds  *  SDTR message with a MESSAGE REJECT message if the either the
4981da177e4SLinus Torvalds  *  initiator or target devices does not support synchronous transfers
4991da177e4SLinus Torvalds  *  or does not want to negotiate for synchronous transfers at the time.
5001da177e4SLinus Torvalds  *  Using the correct method assures compatibility with wide data
5011da177e4SLinus Torvalds  *  transfers and future enhancements.
5021da177e4SLinus Torvalds  *
5031da177e4SLinus Torvalds  * We will always initiate a synchronous transfer negotiation request on
5041da177e4SLinus Torvalds  * every INQUIRY or REQUEST SENSE message, unless the target itself has
5051da177e4SLinus Torvalds  * at some point performed a synchronous transfer negotiation request, or
5061da177e4SLinus Torvalds  * we have synchronous transfers disabled for this device.
5071da177e4SLinus Torvalds  */
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds /**
5101da177e4SLinus Torvalds  * fas216_handlesync - Handle a synchronous transfer message
5111da177e4SLinus Torvalds  * @info: state structure for interface
5121da177e4SLinus Torvalds  * @msg: message from target
5131da177e4SLinus Torvalds  *
5141da177e4SLinus Torvalds  * Handle a synchronous transfer message from the target
5151da177e4SLinus Torvalds  */
5161da177e4SLinus Torvalds static void fas216_handlesync(FAS216_Info *info, char *msg)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	struct fas216_device *dev = &info->device[info->SCpnt->device->id];
5191da177e4SLinus Torvalds 	enum { sync, async, none, reject } res = none;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds #ifdef SCSI2_SYNC
5221da177e4SLinus Torvalds 	switch (msg[0]) {
5231da177e4SLinus Torvalds 	case MESSAGE_REJECT:
5241da177e4SLinus Torvalds 		/* Synchronous transfer request failed.
5251da177e4SLinus Torvalds 		 * Note: SCSI II r10:
5261da177e4SLinus Torvalds 		 *
5271da177e4SLinus Torvalds 		 *  SCSI devices that are capable of synchronous
5281da177e4SLinus Torvalds 		 *  data transfers shall not respond to an SDTR
5291da177e4SLinus Torvalds 		 *  message with a MESSAGE REJECT message.
5301da177e4SLinus Torvalds 		 *
5311da177e4SLinus Torvalds 		 * Hence, if we get this condition, we disable
5321da177e4SLinus Torvalds 		 * negotiation for this device.
5331da177e4SLinus Torvalds 		 */
5341da177e4SLinus Torvalds 		if (dev->sync_state == neg_inprogress) {
5351da177e4SLinus Torvalds 			dev->sync_state = neg_invalid;
5361da177e4SLinus Torvalds 			res = async;
5371da177e4SLinus Torvalds 		}
5381da177e4SLinus Torvalds 		break;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	case EXTENDED_MESSAGE:
5411da177e4SLinus Torvalds 		switch (dev->sync_state) {
5421da177e4SLinus Torvalds 		/* We don't accept synchronous transfer requests.
5431da177e4SLinus Torvalds 		 * Respond with a MESSAGE_REJECT to prevent a
5441da177e4SLinus Torvalds 		 * synchronous transfer agreement from being reached.
5451da177e4SLinus Torvalds 		 */
5461da177e4SLinus Torvalds 		case neg_invalid:
5471da177e4SLinus Torvalds 			res = reject;
5481da177e4SLinus Torvalds 			break;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 		/* We were not negotiating a synchronous transfer,
5511da177e4SLinus Torvalds 		 * but the device sent us a negotiation request.
5521da177e4SLinus Torvalds 		 * Honour the request by sending back a SDTR
5531da177e4SLinus Torvalds 		 * message containing our capability, limited by
5541da177e4SLinus Torvalds 		 * the targets capability.
5551da177e4SLinus Torvalds 		 */
5561da177e4SLinus Torvalds 		default:
5571da177e4SLinus Torvalds 			fas216_cmd(info, CMD_SETATN);
5581da177e4SLinus Torvalds 			if (msg[4] > info->ifcfg.sync_max_depth)
5591da177e4SLinus Torvalds 				msg[4] = info->ifcfg.sync_max_depth;
5601da177e4SLinus Torvalds 			if (msg[3] < 1000 / info->ifcfg.clockrate)
5611da177e4SLinus Torvalds 				msg[3] = 1000 / info->ifcfg.clockrate;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 			msgqueue_flush(&info->scsi.msgs);
5641da177e4SLinus Torvalds 			msgqueue_addmsg(&info->scsi.msgs, 5,
5651da177e4SLinus Torvalds 					EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
5661da177e4SLinus Torvalds 					msg[3], msg[4]);
5671da177e4SLinus Torvalds 			info->scsi.phase = PHASE_MSGOUT_EXPECT;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 			/* This is wrong.  The agreement is not in effect
5701da177e4SLinus Torvalds 			 * until this message is accepted by the device
5711da177e4SLinus Torvalds 			 */
5721da177e4SLinus Torvalds 			dev->sync_state = neg_targcomplete;
5731da177e4SLinus Torvalds 			res = sync;
5741da177e4SLinus Torvalds 			break;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 		/* We initiated the synchronous transfer negotiation,
5771da177e4SLinus Torvalds 		 * and have successfully received a response from the
5781da177e4SLinus Torvalds 		 * target.  The synchronous transfer agreement has been
5791da177e4SLinus Torvalds 		 * reached.  Note: if the values returned are out of our
5801da177e4SLinus Torvalds 		 * bounds, we must reject the message.
5811da177e4SLinus Torvalds 		 */
5821da177e4SLinus Torvalds 		case neg_inprogress:
5831da177e4SLinus Torvalds 			res = reject;
5841da177e4SLinus Torvalds 			if (msg[4] <= info->ifcfg.sync_max_depth &&
5851da177e4SLinus Torvalds 			    msg[3] >= 1000 / info->ifcfg.clockrate) {
5861da177e4SLinus Torvalds 				dev->sync_state = neg_complete;
5871da177e4SLinus Torvalds 				res = sync;
5881da177e4SLinus Torvalds 			}
5891da177e4SLinus Torvalds 			break;
5901da177e4SLinus Torvalds 		}
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds #else
5931da177e4SLinus Torvalds 	res = reject;
5941da177e4SLinus Torvalds #endif
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	switch (res) {
5971da177e4SLinus Torvalds 	case sync:
5981da177e4SLinus Torvalds 		dev->period = msg[3];
5991da177e4SLinus Torvalds 		dev->sof    = msg[4];
6001da177e4SLinus Torvalds 		dev->stp    = fas216_syncperiod(info, msg[3] * 4);
6011da177e4SLinus Torvalds 		fas216_set_sync(info, info->SCpnt->device->id);
6021da177e4SLinus Torvalds 		break;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	case reject:
6051da177e4SLinus Torvalds 		fas216_cmd(info, CMD_SETATN);
6061da177e4SLinus Torvalds 		msgqueue_flush(&info->scsi.msgs);
6071da177e4SLinus Torvalds 		msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
6081da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGOUT_EXPECT;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	case async:
6111da177e4SLinus Torvalds 		dev->period = info->ifcfg.asyncperiod / 4;
6121da177e4SLinus Torvalds 		dev->sof    = 0;
6131da177e4SLinus Torvalds 		dev->stp    = info->scsi.async_stp;
6141da177e4SLinus Torvalds 		fas216_set_sync(info, info->SCpnt->device->id);
6151da177e4SLinus Torvalds 		break;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	case none:
6181da177e4SLinus Torvalds 		break;
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds /**
6231da177e4SLinus Torvalds  * fas216_updateptrs - update data pointers after transfer suspended/paused
6241da177e4SLinus Torvalds  * @info: interface's local pointer to update
6251da177e4SLinus Torvalds  * @bytes_transferred: number of bytes transferred
6261da177e4SLinus Torvalds  *
6271da177e4SLinus Torvalds  * Update data pointers after transfer suspended/paused
6281da177e4SLinus Torvalds  */
6291da177e4SLinus Torvalds static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
6301da177e4SLinus Torvalds {
6310a04137eSChristoph Hellwig 	struct scsi_pointer *SCp = &info->scsi.SCp;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	fas216_checkmagic(info);
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	BUG_ON(bytes_transferred < 0);
6361da177e4SLinus Torvalds 
6376b4df7eeSRussell King 	SCp->phase -= bytes_transferred;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	while (bytes_transferred != 0) {
6401da177e4SLinus Torvalds 		if (SCp->this_residual > bytes_transferred)
6411da177e4SLinus Torvalds 			break;
6421da177e4SLinus Torvalds 		/*
6431da177e4SLinus Torvalds 		 * We have used up this buffer.  Move on to the
6441da177e4SLinus Torvalds 		 * next buffer.
6451da177e4SLinus Torvalds 		 */
6461da177e4SLinus Torvalds 		bytes_transferred -= SCp->this_residual;
6471da177e4SLinus Torvalds 		if (!next_SCp(SCp) && bytes_transferred) {
6481da177e4SLinus Torvalds 			printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
6491da177e4SLinus Torvalds 				info->host->host_no, '0' + info->SCpnt->device->id);
6501da177e4SLinus Torvalds 			return;
6511da177e4SLinus Torvalds 		}
6521da177e4SLinus Torvalds 	}
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	SCp->this_residual -= bytes_transferred;
6551da177e4SLinus Torvalds 	if (SCp->this_residual)
6561da177e4SLinus Torvalds 		SCp->ptr += bytes_transferred;
6571da177e4SLinus Torvalds 	else
6581da177e4SLinus Torvalds 		SCp->ptr = NULL;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /**
6621da177e4SLinus Torvalds  * fas216_pio - transfer data off of/on to card using programmed IO
6631da177e4SLinus Torvalds  * @info: interface to transfer data to/from
6641da177e4SLinus Torvalds  * @direction: direction to transfer data (DMA_OUT/DMA_IN)
6651da177e4SLinus Torvalds  *
6661da177e4SLinus Torvalds  * Transfer data off of/on to card using programmed IO.
6671da177e4SLinus Torvalds  * Notes: this is incredibly slow.
6681da177e4SLinus Torvalds  */
6691da177e4SLinus Torvalds static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
6701da177e4SLinus Torvalds {
6710a04137eSChristoph Hellwig 	struct scsi_pointer *SCp = &info->scsi.SCp;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	fas216_checkmagic(info);
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	if (direction == DMA_OUT)
6761da177e4SLinus Torvalds 		fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
6771da177e4SLinus Torvalds 	else
6781da177e4SLinus Torvalds 		put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	if (SCp->this_residual == 0)
6811da177e4SLinus Torvalds 		next_SCp(SCp);
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds static void fas216_set_stc(FAS216_Info *info, unsigned int length)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds 	fas216_writeb(info, REG_STCL, length);
6871da177e4SLinus Torvalds 	fas216_writeb(info, REG_STCM, length >> 8);
6881da177e4SLinus Torvalds 	fas216_writeb(info, REG_STCH, length >> 16);
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds static unsigned int fas216_get_ctc(FAS216_Info *info)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	return fas216_readb(info, REG_CTCL) +
6941da177e4SLinus Torvalds 	       (fas216_readb(info, REG_CTCM) << 8) +
6951da177e4SLinus Torvalds 	       (fas216_readb(info, REG_CTCH) << 16);
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds /**
6991da177e4SLinus Torvalds  * fas216_cleanuptransfer - clean up after a transfer has completed.
7001da177e4SLinus Torvalds  * @info: interface to clean up
7011da177e4SLinus Torvalds  *
7021da177e4SLinus Torvalds  * Update the data pointers according to the number of bytes transferred
7031da177e4SLinus Torvalds  * on the SCSI bus.
7041da177e4SLinus Torvalds  */
7051da177e4SLinus Torvalds static void fas216_cleanuptransfer(FAS216_Info *info)
7061da177e4SLinus Torvalds {
7071da177e4SLinus Torvalds 	unsigned long total, residual, fifo;
7081da177e4SLinus Torvalds 	fasdmatype_t dmatype = info->dma.transfer_type;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	info->dma.transfer_type = fasdma_none;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	/*
7131da177e4SLinus Torvalds 	 * PIO transfers do not need to be cleaned up.
7141da177e4SLinus Torvalds 	 */
7151da177e4SLinus Torvalds 	if (dmatype == fasdma_pio || dmatype == fasdma_none)
7161da177e4SLinus Torvalds 		return;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	if (dmatype == fasdma_real_all)
7196b4df7eeSRussell King 		total = info->scsi.SCp.phase;
7201da177e4SLinus Torvalds 	else
7211da177e4SLinus Torvalds 		total = info->scsi.SCp.this_residual;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	residual = fas216_get_ctc(info);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	fas216_log(info, LOG_BUFFER, "cleaning up from previous "
7281da177e4SLinus Torvalds 		   "transfer: length 0x%06x, residual 0x%x, fifo %d",
7291da177e4SLinus Torvalds 		   total, residual, fifo);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	/*
7321da177e4SLinus Torvalds 	 * If we were performing Data-Out, the transfer counter
7331da177e4SLinus Torvalds 	 * counts down each time a byte is transferred by the
7341da177e4SLinus Torvalds 	 * host to the FIFO.  This means we must include the
7351da177e4SLinus Torvalds 	 * bytes left in the FIFO from the transfer counter.
7361da177e4SLinus Torvalds 	 */
7371da177e4SLinus Torvalds 	if (info->scsi.phase == PHASE_DATAOUT)
7381da177e4SLinus Torvalds 		residual += fifo;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	fas216_updateptrs(info, total - residual);
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds /**
7441da177e4SLinus Torvalds  * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
7451da177e4SLinus Torvalds  * @info: interface from which device disconnected from
7461da177e4SLinus Torvalds  *
7471da177e4SLinus Torvalds  * Start a DMA/PIO transfer off of/on to card
7481da177e4SLinus Torvalds  */
7491da177e4SLinus Torvalds static void fas216_transfer(FAS216_Info *info)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	fasdmadir_t direction;
7521da177e4SLinus Torvalds 	fasdmatype_t dmatype;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	fas216_log(info, LOG_BUFFER,
7551da177e4SLinus Torvalds 		   "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
7561da177e4SLinus Torvalds 		   info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
7576b4df7eeSRussell King 		   info->scsi.SCp.phase);
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	if (!info->scsi.SCp.ptr) {
7601da177e4SLinus Torvalds 		fas216_log(info, LOG_ERROR, "null buffer passed to "
7611da177e4SLinus Torvalds 			   "fas216_starttransfer");
7621da177e4SLinus Torvalds 		print_SCp(&info->scsi.SCp, "SCp: ", "\n");
7631da177e4SLinus Torvalds 		print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
7641da177e4SLinus Torvalds 		return;
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	/*
7681da177e4SLinus Torvalds 	 * If we have a synchronous transfer agreement in effect, we must
7691da177e4SLinus Torvalds 	 * use DMA mode.  If we are using asynchronous transfers, we may
7701da177e4SLinus Torvalds 	 * use DMA mode or PIO mode.
7711da177e4SLinus Torvalds 	 */
7721da177e4SLinus Torvalds 	if (info->device[info->SCpnt->device->id].sof)
7731da177e4SLinus Torvalds 		dmatype = fasdma_real_all;
7741da177e4SLinus Torvalds 	else
7751da177e4SLinus Torvalds 		dmatype = fasdma_pio;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	if (info->scsi.phase == PHASE_DATAOUT)
7781da177e4SLinus Torvalds 		direction = DMA_OUT;
7791da177e4SLinus Torvalds 	else
7801da177e4SLinus Torvalds 		direction = DMA_IN;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	if (info->dma.setup)
7831da177e4SLinus Torvalds 		dmatype = info->dma.setup(info->host, &info->scsi.SCp,
7841da177e4SLinus Torvalds 					  direction, dmatype);
7851da177e4SLinus Torvalds 	info->dma.transfer_type = dmatype;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	if (dmatype == fasdma_real_all)
7886b4df7eeSRussell King 		fas216_set_stc(info, info->scsi.SCp.phase);
7891da177e4SLinus Torvalds 	else
7901da177e4SLinus Torvalds 		fas216_set_stc(info, info->scsi.SCp.this_residual);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	switch (dmatype) {
7931da177e4SLinus Torvalds 	case fasdma_pio:
7941da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER, "PIO transfer");
7951da177e4SLinus Torvalds 		fas216_writeb(info, REG_SOF, 0);
7961da177e4SLinus Torvalds 		fas216_writeb(info, REG_STP, info->scsi.async_stp);
7971da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO);
7981da177e4SLinus Torvalds 		fas216_pio(info, direction);
7991da177e4SLinus Torvalds 		break;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	case fasdma_pseudo:
8021da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER, "pseudo transfer");
8031da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
8041da177e4SLinus Torvalds 		info->dma.pseudo(info->host, &info->scsi.SCp,
8051da177e4SLinus Torvalds 				 direction, info->SCpnt->transfersize);
8061da177e4SLinus Torvalds 		break;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	case fasdma_real_block:
8091da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER, "block dma transfer");
8101da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
8111da177e4SLinus Torvalds 		break;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	case fasdma_real_all:
8141da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER, "total dma transfer");
8151da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
8161da177e4SLinus Torvalds 		break;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	default:
8191da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER | LOG_ERROR,
8201da177e4SLinus Torvalds 			   "invalid FAS216 DMA type");
8211da177e4SLinus Torvalds 		break;
8221da177e4SLinus Torvalds 	}
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds /**
8261da177e4SLinus Torvalds  * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
8271da177e4SLinus Torvalds  * @info: interface from which device disconnected from
8281da177e4SLinus Torvalds  *
8291da177e4SLinus Torvalds  * Called when we switch away from DATA IN or DATA OUT phases.
8301da177e4SLinus Torvalds  */
8311da177e4SLinus Torvalds static void fas216_stoptransfer(FAS216_Info *info)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	fas216_checkmagic(info);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	if (info->dma.transfer_type == fasdma_real_all ||
8361da177e4SLinus Torvalds 	    info->dma.transfer_type == fasdma_real_block)
8371da177e4SLinus Torvalds 		info->dma.stop(info->host, &info->scsi.SCp);
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	fas216_cleanuptransfer(info);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	if (info->scsi.phase == PHASE_DATAIN) {
8421da177e4SLinus Torvalds 		unsigned int fifo;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 		/*
8451da177e4SLinus Torvalds 		 * If we were performing Data-In, then the FIFO counter
8461da177e4SLinus Torvalds 		 * contains the number of bytes not transferred via DMA
8471da177e4SLinus Torvalds 		 * from the on-board FIFO.  Read them manually.
8481da177e4SLinus Torvalds 		 */
8491da177e4SLinus Torvalds 		fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
8501da177e4SLinus Torvalds 		while (fifo && info->scsi.SCp.ptr) {
8511da177e4SLinus Torvalds 			*info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
8521da177e4SLinus Torvalds 			fas216_updateptrs(info, 1);
8531da177e4SLinus Torvalds 			fifo--;
8541da177e4SLinus Torvalds 		}
8551da177e4SLinus Torvalds 	} else {
8561da177e4SLinus Torvalds 		/*
8571da177e4SLinus Torvalds 		 * After a Data-Out phase, there may be unsent
8581da177e4SLinus Torvalds 		 * bytes left in the FIFO.  Flush them out.
8591da177e4SLinus Torvalds 		 */
8601da177e4SLinus Torvalds 		fas216_cmd(info, CMD_FLUSHFIFO);
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds static void fas216_aborttransfer(FAS216_Info *info)
8651da177e4SLinus Torvalds {
8661da177e4SLinus Torvalds 	fas216_checkmagic(info);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	if (info->dma.transfer_type == fasdma_real_all ||
8691da177e4SLinus Torvalds 	    info->dma.transfer_type == fasdma_real_block)
8701da177e4SLinus Torvalds 		info->dma.stop(info->host, &info->scsi.SCp);
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	info->dma.transfer_type = fasdma_none;
8731da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds static void fas216_kick(FAS216_Info *info);
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds /**
8791da177e4SLinus Torvalds  * fas216_disconnected_intr - handle device disconnection
8801da177e4SLinus Torvalds  * @info: interface from which device disconnected from
8811da177e4SLinus Torvalds  *
8821da177e4SLinus Torvalds  * Handle device disconnection
8831da177e4SLinus Torvalds  */
8841da177e4SLinus Torvalds static void fas216_disconnect_intr(FAS216_Info *info)
8851da177e4SLinus Torvalds {
8861da177e4SLinus Torvalds 	unsigned long flags;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	fas216_checkmagic(info);
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
8911da177e4SLinus Torvalds 		   info->scsi.phase);
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	switch (info->scsi.phase) {
8961da177e4SLinus Torvalds 	case PHASE_SELECTION:			/* while selecting - no target		*/
8971da177e4SLinus Torvalds 	case PHASE_SELSTEPS:
8981da177e4SLinus Torvalds 		fas216_done(info, DID_NO_CONNECT);
8991da177e4SLinus Torvalds 		break;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	case PHASE_MSGIN_DISCONNECT:		/* message in - disconnecting		*/
9021da177e4SLinus Torvalds 		info->scsi.disconnectable = 1;
9031da177e4SLinus Torvalds 		info->scsi.phase = PHASE_IDLE;
9041da177e4SLinus Torvalds 		info->stats.disconnects += 1;
9051da177e4SLinus Torvalds 		spin_lock_irqsave(&info->host_lock, flags);
9061da177e4SLinus Torvalds 		if (info->scsi.phase == PHASE_IDLE)
9071da177e4SLinus Torvalds 			fas216_kick(info);
9081da177e4SLinus Torvalds 		spin_unlock_irqrestore(&info->host_lock, flags);
9091da177e4SLinus Torvalds 		break;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	case PHASE_DONE:			/* at end of command - complete		*/
9121da177e4SLinus Torvalds 		fas216_done(info, DID_OK);
9131da177e4SLinus Torvalds 		break;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	case PHASE_MSGOUT:			/* message out - possible ABORT message	*/
9161da177e4SLinus Torvalds 		if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
9171da177e4SLinus Torvalds 			info->scsi.aborting = 0;
9181da177e4SLinus Torvalds 			fas216_done(info, DID_ABORT);
9191da177e4SLinus Torvalds 			break;
9201da177e4SLinus Torvalds 		}
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	default:				/* huh?					*/
9231da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
9241da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info), fas216_drv_phase(info));
9251da177e4SLinus Torvalds 		print_debug_list();
9261da177e4SLinus Torvalds 		fas216_stoptransfer(info);
9271da177e4SLinus Torvalds 		fas216_done(info, DID_ERROR);
9281da177e4SLinus Torvalds 		break;
9291da177e4SLinus Torvalds 	}
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds /**
9331da177e4SLinus Torvalds  * fas216_reselected_intr - start reconnection of a device
9341da177e4SLinus Torvalds  * @info: interface which was reselected
9351da177e4SLinus Torvalds  *
9361da177e4SLinus Torvalds  * Start reconnection of a device
9371da177e4SLinus Torvalds  */
9381da177e4SLinus Torvalds static void
9391da177e4SLinus Torvalds fas216_reselected_intr(FAS216_Info *info)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	unsigned int cfis, i;
9421da177e4SLinus Torvalds 	unsigned char msg[4];
9431da177e4SLinus Torvalds 	unsigned char target, lun, tag;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	fas216_checkmagic(info);
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	WARN_ON(info->scsi.phase == PHASE_SELECTION ||
9481da177e4SLinus Torvalds 		info->scsi.phase == PHASE_SELSTEPS);
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	cfis = fas216_readb(info, REG_CFIS);
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
9531da177e4SLinus Torvalds 		   info->scsi.phase, cfis);
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	cfis &= CFIS_CF;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	if (cfis < 2 || cfis > 4) {
9581da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
9591da177e4SLinus Torvalds 			info->host->host_no);
9601da177e4SLinus Torvalds 		goto bad_message;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	for (i = 0; i < cfis; i++)
9641da177e4SLinus Torvalds 		msg[i] = fas216_readb(info, REG_FF);
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	if (!(msg[0] & (1 << info->host->this_id)) ||
9671da177e4SLinus Torvalds 	    !(msg[1] & 0x80))
9681da177e4SLinus Torvalds 		goto initiator_error;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	target = msg[0] & ~(1 << info->host->this_id);
9711da177e4SLinus Torvalds 	target = ffs(target) - 1;
9721da177e4SLinus Torvalds 	lun = msg[1] & 7;
9731da177e4SLinus Torvalds 	tag = 0;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	if (cfis >= 3) {
9761da177e4SLinus Torvalds 		if (msg[2] != SIMPLE_QUEUE_TAG)
9771da177e4SLinus Torvalds 			goto initiator_error;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 		tag = msg[3];
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	/* set up for synchronous transfers */
9831da177e4SLinus Torvalds 	fas216_writeb(info, REG_SDID, target);
9841da177e4SLinus Torvalds 	fas216_set_sync(info, target);
9851da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
9881da177e4SLinus Torvalds 		   target, lun, tag);
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	if (info->scsi.disconnectable && info->SCpnt) {
9911da177e4SLinus Torvalds 		info->scsi.disconnectable = 0;
9921da177e4SLinus Torvalds 		if (info->SCpnt->device->id  == target &&
9931da177e4SLinus Torvalds 		    info->SCpnt->device->lun == lun &&
9941da177e4SLinus Torvalds 		    info->SCpnt->tag         == tag) {
9951da177e4SLinus Torvalds 			fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
9961da177e4SLinus Torvalds 		} else {
9971da177e4SLinus Torvalds 			queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
9981da177e4SLinus Torvalds 			fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
9991da177e4SLinus Torvalds 			info->SCpnt = NULL;
10001da177e4SLinus Torvalds 		}
10011da177e4SLinus Torvalds 	}
10021da177e4SLinus Torvalds 	if (!info->SCpnt) {
10031da177e4SLinus Torvalds 		info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
10041da177e4SLinus Torvalds 					target, lun, tag);
10051da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT, "had to get command");
10061da177e4SLinus Torvalds 	}
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	if (info->SCpnt) {
10091da177e4SLinus Torvalds 		/*
10101da177e4SLinus Torvalds 		 * Restore data pointer from SAVED data pointer
10111da177e4SLinus Torvalds 		 */
10121da177e4SLinus Torvalds 		info->scsi.SCp = info->SCpnt->SCp;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
10151da177e4SLinus Torvalds 			info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
10161da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGIN;
10171da177e4SLinus Torvalds 	} else {
10181da177e4SLinus Torvalds 		/*
10191da177e4SLinus Torvalds 		 * Our command structure not found - abort the
10201da177e4SLinus Torvalds 		 * command on the target.  Since we have no
10211da177e4SLinus Torvalds 		 * record of this command, we can't send
10221da177e4SLinus Torvalds 		 * an INITIATOR DETECTED ERROR message.
10231da177e4SLinus Torvalds 		 */
10241da177e4SLinus Torvalds 		fas216_cmd(info, CMD_SETATN);
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds #if 0
10271da177e4SLinus Torvalds 		if (tag)
10281da177e4SLinus Torvalds 			msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
10291da177e4SLinus Torvalds 		else
10301da177e4SLinus Torvalds #endif
10311da177e4SLinus Torvalds 			msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
10321da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGOUT_EXPECT;
10331da177e4SLinus Torvalds 		info->scsi.aborting = 1;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	fas216_cmd(info, CMD_MSGACCEPTED);
10371da177e4SLinus Torvalds 	return;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds  initiator_error:
10401da177e4SLinus Torvalds 	printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
10411da177e4SLinus Torvalds 		info->host->host_no);
10421da177e4SLinus Torvalds 	for (i = 0; i < cfis; i++)
10431da177e4SLinus Torvalds 		printk(" %02x", msg[i]);
10441da177e4SLinus Torvalds 	printk("\n");
10451da177e4SLinus Torvalds  bad_message:
10461da177e4SLinus Torvalds 	fas216_cmd(info, CMD_SETATN);
10471da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
10481da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
10491da177e4SLinus Torvalds 	info->scsi.phase = PHASE_MSGOUT_EXPECT;
10501da177e4SLinus Torvalds 	fas216_cmd(info, CMD_MSGACCEPTED);
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
10541da177e4SLinus Torvalds {
10551da177e4SLinus Torvalds 	int i;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	switch (message[0]) {
10581da177e4SLinus Torvalds 	case COMMAND_COMPLETE:
10591da177e4SLinus Torvalds 		if (msglen != 1)
10601da177e4SLinus Torvalds 			goto unrecognised;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.%c: command complete with no "
10631da177e4SLinus Torvalds 			"status in MESSAGE_IN?\n",
10641da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info));
10651da177e4SLinus Torvalds 		break;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	case SAVE_POINTERS:
10681da177e4SLinus Torvalds 		if (msglen != 1)
10691da177e4SLinus Torvalds 			goto unrecognised;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 		/*
10721da177e4SLinus Torvalds 		 * Save current data pointer to SAVED data pointer
10731da177e4SLinus Torvalds 		 * SCSI II standard says that we must not acknowledge
10741da177e4SLinus Torvalds 		 * this until we have really saved pointers.
10751da177e4SLinus Torvalds 		 * NOTE: we DO NOT save the command nor status pointers
10761da177e4SLinus Torvalds 		 * as required by the SCSI II standard.  These always
10771da177e4SLinus Torvalds 		 * point to the start of their respective areas.
10781da177e4SLinus Torvalds 		 */
10791da177e4SLinus Torvalds 		info->SCpnt->SCp = info->scsi.SCp;
10801da177e4SLinus Torvalds 		info->SCpnt->SCp.sent_command = 0;
10811da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
10821da177e4SLinus Torvalds 			"save data pointers: [%p, %X]",
10831da177e4SLinus Torvalds 			info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
10841da177e4SLinus Torvalds 		break;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	case RESTORE_POINTERS:
10871da177e4SLinus Torvalds 		if (msglen != 1)
10881da177e4SLinus Torvalds 			goto unrecognised;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 		/*
10911da177e4SLinus Torvalds 		 * Restore current data pointer from SAVED data pointer
10921da177e4SLinus Torvalds 		 */
10931da177e4SLinus Torvalds 		info->scsi.SCp = info->SCpnt->SCp;
10941da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
10951da177e4SLinus Torvalds 			"restore data pointers: [%p, 0x%x]",
10961da177e4SLinus Torvalds 			info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
10971da177e4SLinus Torvalds 		break;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	case DISCONNECT:
11001da177e4SLinus Torvalds 		if (msglen != 1)
11011da177e4SLinus Torvalds 			goto unrecognised;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGIN_DISCONNECT;
11041da177e4SLinus Torvalds 		break;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	case MESSAGE_REJECT:
11071da177e4SLinus Torvalds 		if (msglen != 1)
11081da177e4SLinus Torvalds 			goto unrecognised;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 		switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
11111da177e4SLinus Torvalds 		case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
11121da177e4SLinus Torvalds 			fas216_handlesync(info, message);
11131da177e4SLinus Torvalds 			break;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 		default:
11161da177e4SLinus Torvalds 			fas216_log(info, 0, "reject, last message 0x%04x",
11171da177e4SLinus Torvalds 				fas216_get_last_msg(info, info->scsi.msgin_fifo));
11181da177e4SLinus Torvalds 		}
11191da177e4SLinus Torvalds 		break;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	case NOP:
11221da177e4SLinus Torvalds 		break;
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	case EXTENDED_MESSAGE:
11251da177e4SLinus Torvalds 		if (msglen < 3)
11261da177e4SLinus Torvalds 			goto unrecognised;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 		switch (message[2]) {
11291da177e4SLinus Torvalds 		case EXTENDED_SDTR:	/* Sync transfer negotiation request/reply */
11301da177e4SLinus Torvalds 			fas216_handlesync(info, message);
11311da177e4SLinus Torvalds 			break;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 		default:
11341da177e4SLinus Torvalds 			goto unrecognised;
11351da177e4SLinus Torvalds 		}
11361da177e4SLinus Torvalds 		break;
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	default:
11391da177e4SLinus Torvalds 		goto unrecognised;
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 	return;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds unrecognised:
11441da177e4SLinus Torvalds 	fas216_log(info, 0, "unrecognised message, rejecting");
11451da177e4SLinus Torvalds 	printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
11461da177e4SLinus Torvalds 	for (i = 0; i < msglen; i++)
11471da177e4SLinus Torvalds 		printk("%s%02X", i & 31 ? " " : "\n  ", message[i]);
11481da177e4SLinus Torvalds 	printk("\n");
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	/*
11511da177e4SLinus Torvalds 	 * Something strange seems to be happening here -
11521da177e4SLinus Torvalds 	 * I can't use SETATN since the chip gives me an
11531da177e4SLinus Torvalds 	 * invalid command interrupt when I do.  Weird.
11541da177e4SLinus Torvalds 	 */
11551da177e4SLinus Torvalds fas216_cmd(info, CMD_NOP);
11561da177e4SLinus Torvalds fas216_dumpstate(info);
11571da177e4SLinus Torvalds 	fas216_cmd(info, CMD_SETATN);
11581da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
11591da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
11601da177e4SLinus Torvalds 	info->scsi.phase = PHASE_MSGOUT_EXPECT;
11611da177e4SLinus Torvalds fas216_dumpstate(info);
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds static int fas216_wait_cmd(FAS216_Info *info, int cmd)
11651da177e4SLinus Torvalds {
11661da177e4SLinus Torvalds 	int tout;
11671da177e4SLinus Torvalds 	int stat;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	fas216_cmd(info, cmd);
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	for (tout = 1000; tout; tout -= 1) {
11721da177e4SLinus Torvalds 		stat = fas216_readb(info, REG_STAT);
11731da177e4SLinus Torvalds 		if (stat & (STAT_INT|STAT_PARITYERROR))
11741da177e4SLinus Torvalds 			break;
11751da177e4SLinus Torvalds 		udelay(1);
11761da177e4SLinus Torvalds 	}
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	return stat;
11791da177e4SLinus Torvalds }
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds static int fas216_get_msg_byte(FAS216_Info *info)
11821da177e4SLinus Torvalds {
11831da177e4SLinus Torvalds 	unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	if ((stat & STAT_INT) == 0)
11861da177e4SLinus Torvalds 		goto timedout;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	if ((stat & STAT_BUSMASK) != STAT_MESGIN)
11891da177e4SLinus Torvalds 		goto unexpected_phase_change;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	fas216_readb(info, REG_INST);
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	if ((stat & STAT_INT) == 0)
11961da177e4SLinus Torvalds 		goto timedout;
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	if (stat & STAT_PARITYERROR)
11991da177e4SLinus Torvalds 		goto parity_error;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	if ((stat & STAT_BUSMASK) != STAT_MESGIN)
12021da177e4SLinus Torvalds 		goto unexpected_phase_change;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	fas216_readb(info, REG_INST);
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	return fas216_readb(info, REG_FF);
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds timedout:
12091da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
12101da177e4SLinus Torvalds 	return -1;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds unexpected_phase_change:
12131da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
12141da177e4SLinus Torvalds 	return -2;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds parity_error:
12171da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "parity error during message in phase");
12181da177e4SLinus Torvalds 	return -3;
12191da177e4SLinus Torvalds }
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds /**
12221da177e4SLinus Torvalds  * fas216_message - handle a function done interrupt from FAS216 chip
12231da177e4SLinus Torvalds  * @info: interface which caused function done interrupt
12241da177e4SLinus Torvalds  *
12251da177e4SLinus Torvalds  * Handle a function done interrupt from FAS216 chip
12261da177e4SLinus Torvalds  */
12271da177e4SLinus Torvalds static void fas216_message(FAS216_Info *info)
12281da177e4SLinus Torvalds {
12291da177e4SLinus Torvalds 	unsigned char *message = info->scsi.message;
12301da177e4SLinus Torvalds 	unsigned int msglen = 1;
12311da177e4SLinus Torvalds 	int msgbyte = 0;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	fas216_checkmagic(info);
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	message[0] = fas216_readb(info, REG_FF);
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	if (message[0] == EXTENDED_MESSAGE) {
12381da177e4SLinus Torvalds 		msgbyte = fas216_get_msg_byte(info);
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 		if (msgbyte >= 0) {
12411da177e4SLinus Torvalds 			message[1] = msgbyte;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 			for (msglen = 2; msglen < message[1] + 2; msglen++) {
12441da177e4SLinus Torvalds 				msgbyte = fas216_get_msg_byte(info);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 				if (msgbyte >= 0)
12471da177e4SLinus Torvalds 					message[msglen] = msgbyte;
12481da177e4SLinus Torvalds 				else
12491da177e4SLinus Torvalds 					break;
12501da177e4SLinus Torvalds 			}
12511da177e4SLinus Torvalds 		}
12521da177e4SLinus Torvalds 	}
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	if (msgbyte == -3)
12551da177e4SLinus Torvalds 		goto parity_error;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds #ifdef DEBUG_MESSAGES
12581da177e4SLinus Torvalds 	{
12591da177e4SLinus Torvalds 		int i;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 		printk("scsi%d.%c: message in: ",
12621da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info));
12631da177e4SLinus Torvalds 		for (i = 0; i < msglen; i++)
12641da177e4SLinus Torvalds 			printk("%02X ", message[i]);
12651da177e4SLinus Torvalds 		printk("\n");
12661da177e4SLinus Torvalds 	}
12671da177e4SLinus Torvalds #endif
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	fas216_parse_message(info, message, msglen);
12701da177e4SLinus Torvalds 	fas216_cmd(info, CMD_MSGACCEPTED);
12711da177e4SLinus Torvalds 	return;
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds parity_error:
12741da177e4SLinus Torvalds 	fas216_cmd(info, CMD_SETATN);
12751da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
12761da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
12771da177e4SLinus Torvalds 	info->scsi.phase = PHASE_MSGOUT_EXPECT;
12781da177e4SLinus Torvalds 	fas216_cmd(info, CMD_MSGACCEPTED);
12791da177e4SLinus Torvalds 	return;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds /**
12831da177e4SLinus Torvalds  * fas216_send_command - send command after all message bytes have been sent
12841da177e4SLinus Torvalds  * @info: interface which caused bus service
12851da177e4SLinus Torvalds  *
12861da177e4SLinus Torvalds  * Send a command to a target after all message bytes have been sent
12871da177e4SLinus Torvalds  */
12881da177e4SLinus Torvalds static void fas216_send_command(FAS216_Info *info)
12891da177e4SLinus Torvalds {
12901da177e4SLinus Torvalds 	int i;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	fas216_checkmagic(info);
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
12951da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	/* load command */
12981da177e4SLinus Torvalds 	for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
12991da177e4SLinus Torvalds 		fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	fas216_cmd(info, CMD_TRANSFERINFO);
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	info->scsi.phase = PHASE_COMMAND;
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds /**
13071da177e4SLinus Torvalds  * fas216_send_messageout - handle bus service to send a message
13081da177e4SLinus Torvalds  * @info: interface which caused bus service
13091da177e4SLinus Torvalds  *
13101da177e4SLinus Torvalds  * Handle bus service to send a message.
13111da177e4SLinus Torvalds  * Note: We do not allow the device to change the data direction!
13121da177e4SLinus Torvalds  */
13131da177e4SLinus Torvalds static void fas216_send_messageout(FAS216_Info *info, int start)
13141da177e4SLinus Torvalds {
13151da177e4SLinus Torvalds 	unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	fas216_checkmagic(info);
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	if (tot_msglen) {
13221da177e4SLinus Torvalds 		struct message *msg;
13231da177e4SLinus Torvalds 		int msgnr = 0;
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 		while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
13261da177e4SLinus Torvalds 			int i;
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 			for (i = start; i < msg->length; i++)
13291da177e4SLinus Torvalds 				fas216_writeb(info, REG_FF, msg->msg[i]);
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 			msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
13321da177e4SLinus Torvalds 			start = 0;
13331da177e4SLinus Torvalds 		}
13341da177e4SLinus Torvalds 	} else
13351da177e4SLinus Torvalds 		fas216_writeb(info, REG_FF, NOP);
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	fas216_cmd(info, CMD_TRANSFERINFO);
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	info->scsi.phase = PHASE_MSGOUT;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds /**
13431da177e4SLinus Torvalds  * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
13441da177e4SLinus Torvalds  * @info: interface which caused bus service interrupt
13451da177e4SLinus Torvalds  * @stat: Status register contents
13461da177e4SLinus Torvalds  * @is: SCSI Status register contents
13471da177e4SLinus Torvalds  *
13481da177e4SLinus Torvalds  * Handle a bus service interrupt from FAS216 chip
13491da177e4SLinus Torvalds  */
13501da177e4SLinus Torvalds static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
13511da177e4SLinus Torvalds {
13521da177e4SLinus Torvalds 	fas216_checkmagic(info);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	fas216_log(info, LOG_BUSSERVICE,
13551da177e4SLinus Torvalds 		   "bus service: stat=%02x is=%02x phase=%02x",
13561da177e4SLinus Torvalds 		   stat, is, info->scsi.phase);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	switch (info->scsi.phase) {
13591da177e4SLinus Torvalds 	case PHASE_SELECTION:
13601da177e4SLinus Torvalds 		if ((is & IS_BITS) != IS_MSGBYTESENT)
13611da177e4SLinus Torvalds 			goto bad_is;
13621da177e4SLinus Torvalds 		break;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	case PHASE_SELSTEPS:
13651da177e4SLinus Torvalds 		switch (is & IS_BITS) {
13661da177e4SLinus Torvalds 		case IS_SELARB:
13671da177e4SLinus Torvalds 		case IS_MSGBYTESENT:
13681da177e4SLinus Torvalds 			goto bad_is;
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 		case IS_NOTCOMMAND:
13711da177e4SLinus Torvalds 		case IS_EARLYPHASE:
13721da177e4SLinus Torvalds 			if ((stat & STAT_BUSMASK) == STAT_MESGIN)
13731da177e4SLinus Torvalds 				break;
13741da177e4SLinus Torvalds 			goto bad_is;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 		case IS_COMPLETE:
13771da177e4SLinus Torvalds 			break;
13781da177e4SLinus Torvalds 		}
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	default:
13811da177e4SLinus Torvalds 		break;
13821da177e4SLinus Torvalds 	}
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP);
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds #define STATE(st,ph) ((ph) << 3 | (st))
13871da177e4SLinus Torvalds 	/* This table describes the legal SCSI state transitions,
13881da177e4SLinus Torvalds 	 * as described by the SCSI II spec.
13891da177e4SLinus Torvalds 	 */
13901da177e4SLinus Torvalds 	switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
13911da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In      */
13921da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_MSGOUT):  /* Message Out  -> Data In      */
13931da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command      -> Data In      */
13941da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_MSGIN):   /* Message In   -> Data In      */
13951da177e4SLinus Torvalds 		info->scsi.phase = PHASE_DATAIN;
13961da177e4SLinus Torvalds 		fas216_transfer(info);
13971da177e4SLinus Torvalds 		return;
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_DATAIN):  /* Data In      -> Data In      */
14001da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out     -> Data Out     */
14011da177e4SLinus Torvalds 		fas216_cleanuptransfer(info);
14021da177e4SLinus Torvalds 		fas216_transfer(info);
14031da177e4SLinus Torvalds 		return;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out     */
14061da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out  -> Data Out     */
14071da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command      -> Data Out     */
14081da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_MSGIN):  /* Message In   -> Data Out     */
14091da177e4SLinus Torvalds 		fas216_cmd(info, CMD_FLUSHFIFO);
14101da177e4SLinus Torvalds 		info->scsi.phase = PHASE_DATAOUT;
14111da177e4SLinus Torvalds 		fas216_transfer(info);
14121da177e4SLinus Torvalds 		return;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out     -> Status       */
14151da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_DATAIN):  /* Data In      -> Status       */
14161da177e4SLinus Torvalds 		fas216_stoptransfer(info);
14171da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status       */
14181da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_MSGOUT):  /* Message Out  -> Status       */
14191da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_COMMAND): /* Command      -> Status       */
14201da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_MSGIN):   /* Message In   -> Status       */
14211da177e4SLinus Torvalds 		fas216_cmd(info, CMD_INITCMDCOMPLETE);
14221da177e4SLinus Torvalds 		info->scsi.phase = PHASE_STATUS;
14231da177e4SLinus Torvalds 		return;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out     -> Message In   */
14261da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_DATAIN):  /* Data In      -> Message In   */
14271da177e4SLinus Torvalds 		fas216_stoptransfer(info);
14281da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_COMMAND):	/* Command	-> Message In	*/
14291da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In   */
14301da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_MSGOUT):  /* Message Out  -> Message In   */
14311da177e4SLinus Torvalds 		info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
14321da177e4SLinus Torvalds 		fas216_cmd(info, CMD_FLUSHFIFO);
14331da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO);
14341da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGIN;
14351da177e4SLinus Torvalds 		return;
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_MSGIN):
14381da177e4SLinus Torvalds 		info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
14391da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO);
14401da177e4SLinus Torvalds 		return;
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out  -> Command      */
14431da177e4SLinus Torvalds 	case STATE(STAT_COMMAND, PHASE_MSGIN):  /* Message In   -> Command      */
14441da177e4SLinus Torvalds 		fas216_send_command(info);
14451da177e4SLinus Torvalds 		info->scsi.phase = PHASE_COMMAND;
14461da177e4SLinus Torvalds 		return;
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	/*
14501da177e4SLinus Torvalds 	 * Selection    -> Message Out
14511da177e4SLinus Torvalds 	 */
14521da177e4SLinus Torvalds 	case STATE(STAT_MESGOUT, PHASE_SELECTION):
14531da177e4SLinus Torvalds 		fas216_send_messageout(info, 1);
14541da177e4SLinus Torvalds 		return;
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 	/*
14571da177e4SLinus Torvalds 	 * Message Out  -> Message Out
14581da177e4SLinus Torvalds 	 */
14591da177e4SLinus Torvalds 	case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
14601da177e4SLinus Torvalds 	case STATE(STAT_MESGOUT, PHASE_MSGOUT):
14611da177e4SLinus Torvalds 		/*
14621da177e4SLinus Torvalds 		 * If we get another message out phase, this usually
14631da177e4SLinus Torvalds 		 * means some parity error occurred.  Resend complete
14641da177e4SLinus Torvalds 		 * set of messages.  If we have more than one byte to
14651da177e4SLinus Torvalds 		 * send, we need to assert ATN again.
14661da177e4SLinus Torvalds 		 */
14671da177e4SLinus Torvalds 		if (info->device[info->SCpnt->device->id].parity_check) {
14681da177e4SLinus Torvalds 			/*
14691da177e4SLinus Torvalds 			 * We were testing... good, the device
14701da177e4SLinus Torvalds 			 * supports parity checking.
14711da177e4SLinus Torvalds 			 */
14721da177e4SLinus Torvalds 			info->device[info->SCpnt->device->id].parity_check = 0;
14731da177e4SLinus Torvalds 			info->device[info->SCpnt->device->id].parity_enabled = 1;
14741da177e4SLinus Torvalds 			fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
14751da177e4SLinus Torvalds 		}
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 		if (msgqueue_msglength(&info->scsi.msgs) > 1)
14781da177e4SLinus Torvalds 			fas216_cmd(info, CMD_SETATN);
14791da177e4SLinus Torvalds 		/*FALLTHROUGH*/
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	/*
14821da177e4SLinus Torvalds 	 * Any          -> Message Out
14831da177e4SLinus Torvalds 	 */
14841da177e4SLinus Torvalds 	case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
14851da177e4SLinus Torvalds 		fas216_send_messageout(info, 0);
14861da177e4SLinus Torvalds 		return;
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 	/* Error recovery rules.
14891da177e4SLinus Torvalds 	 *   These either attempt to abort or retry the operation.
14901da177e4SLinus Torvalds 	 * TODO: we need more of these
14911da177e4SLinus Torvalds 	 */
14921da177e4SLinus Torvalds 	case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command      -> Command      */
14931da177e4SLinus Torvalds 		/* error - we've sent out all the command bytes
14941da177e4SLinus Torvalds 		 * we have.
14951da177e4SLinus Torvalds 		 * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
14961da177e4SLinus Torvalds 		 * to include the command bytes sent for this to work
14971da177e4SLinus Torvalds 		 * correctly.
14981da177e4SLinus Torvalds 		 */
14991da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.%c: "
15001da177e4SLinus Torvalds 			"target trying to receive more command bytes\n",
15011da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info));
15021da177e4SLinus Torvalds 		fas216_cmd(info, CMD_SETATN);
15031da177e4SLinus Torvalds 		fas216_set_stc(info, 15);
15041da177e4SLinus Torvalds 		fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
15051da177e4SLinus Torvalds 		msgqueue_flush(&info->scsi.msgs);
15061da177e4SLinus Torvalds 		msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
15071da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGOUT_EXPECT;
15081da177e4SLinus Torvalds 		return;
15091da177e4SLinus Torvalds 	}
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
15121da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
15131da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info),
15141da177e4SLinus Torvalds 			fas216_bus_phase(stat));
15151da177e4SLinus Torvalds 		msgqueue_flush(&info->scsi.msgs);
15161da177e4SLinus Torvalds 		fas216_cmd(info, CMD_SETATN);
15171da177e4SLinus Torvalds 		msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
15181da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGOUT_EXPECT;
15191da177e4SLinus Torvalds 		info->scsi.aborting = 1;
15201da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO);
15211da177e4SLinus Torvalds 		return;
15221da177e4SLinus Torvalds 	}
15231da177e4SLinus Torvalds 	printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
15241da177e4SLinus Torvalds 		info->host->host_no, fas216_target(info),
15251da177e4SLinus Torvalds 		fas216_bus_phase(stat),
15261da177e4SLinus Torvalds 		fas216_drv_phase(info));
15271da177e4SLinus Torvalds 	print_debug_list();
15281da177e4SLinus Torvalds 	return;
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds bad_is:
15311da177e4SLinus Torvalds 	fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
15321da177e4SLinus Torvalds 	fas216_dumpstate(info);
15331da177e4SLinus Torvalds 	print_debug_list();
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	fas216_done(info, DID_ERROR);
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds /**
15391da177e4SLinus Torvalds  * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
15401da177e4SLinus Torvalds  * @info: interface which caused function done interrupt
15411da177e4SLinus Torvalds  * @stat: Status register contents
15421da177e4SLinus Torvalds  * @is: SCSI Status register contents
15431da177e4SLinus Torvalds  *
15441da177e4SLinus Torvalds  * Handle a function done interrupt from FAS216 chip
15451da177e4SLinus Torvalds  */
15461da177e4SLinus Torvalds static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
15471da177e4SLinus Torvalds {
15481da177e4SLinus Torvalds 	unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	fas216_checkmagic(info);
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	fas216_log(info, LOG_FUNCTIONDONE,
15531da177e4SLinus Torvalds 		   "function done: stat=%02x is=%02x phase=%02x",
15541da177e4SLinus Torvalds 		   stat, is, info->scsi.phase);
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	switch (info->scsi.phase) {
15571da177e4SLinus Torvalds 	case PHASE_STATUS:			/* status phase - read status and msg	*/
15581da177e4SLinus Torvalds 		if (fifo_len != 2) {
15591da177e4SLinus Torvalds 			fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
15601da177e4SLinus Torvalds 		}
15611da177e4SLinus Torvalds 		/*
15621da177e4SLinus Torvalds 		 * Read status then message byte.
15631da177e4SLinus Torvalds 		 */
15641da177e4SLinus Torvalds 		info->scsi.SCp.Status = fas216_readb(info, REG_FF);
15651da177e4SLinus Torvalds 		info->scsi.SCp.Message = fas216_readb(info, REG_FF);
15661da177e4SLinus Torvalds 		info->scsi.phase = PHASE_DONE;
15671da177e4SLinus Torvalds 		fas216_cmd(info, CMD_MSGACCEPTED);
15681da177e4SLinus Torvalds 		break;
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	case PHASE_IDLE:
15711da177e4SLinus Torvalds 	case PHASE_SELECTION:
15721da177e4SLinus Torvalds 	case PHASE_SELSTEPS:
15731da177e4SLinus Torvalds 		break;
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	case PHASE_MSGIN:			/* message in phase			*/
15761da177e4SLinus Torvalds 		if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
15771da177e4SLinus Torvalds 			info->scsi.msgin_fifo = fifo_len;
15781da177e4SLinus Torvalds 			fas216_message(info);
15791da177e4SLinus Torvalds 			break;
15801da177e4SLinus Torvalds 		}
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	default:
15831da177e4SLinus Torvalds 		fas216_log(info, 0, "internal phase %s for function done?"
15841da177e4SLinus Torvalds 			"  What do I do with this?",
15851da177e4SLinus Torvalds 			fas216_target(info), fas216_drv_phase(info));
15861da177e4SLinus Torvalds 	}
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds static void fas216_bus_reset(FAS216_Info *info)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	neg_t sync_state;
15921da177e4SLinus Torvalds 	int i;
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	sync_state = neg_invalid;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds #ifdef SCSI2_SYNC
15991da177e4SLinus Torvalds 	if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
16001da177e4SLinus Torvalds 		sync_state = neg_wait;
16011da177e4SLinus Torvalds #endif
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	info->scsi.phase = PHASE_IDLE;
16041da177e4SLinus Torvalds 	info->SCpnt = NULL; /* bug! */
16051da177e4SLinus Torvalds 	memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
16081da177e4SLinus Torvalds 		info->device[i].disconnect_ok	= info->ifcfg.disconnect_ok;
16091da177e4SLinus Torvalds 		info->device[i].sync_state	= sync_state;
16101da177e4SLinus Torvalds 		info->device[i].period		= info->ifcfg.asyncperiod / 4;
16111da177e4SLinus Torvalds 		info->device[i].stp		= info->scsi.async_stp;
16121da177e4SLinus Torvalds 		info->device[i].sof		= 0;
16131da177e4SLinus Torvalds 		info->device[i].wide_xfer	= 0;
16141da177e4SLinus Torvalds 	}
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	info->rst_bus_status = 1;
16171da177e4SLinus Torvalds 	wake_up(&info->eh_wait);
16181da177e4SLinus Torvalds }
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds /**
16211da177e4SLinus Torvalds  * fas216_intr - handle interrupts to progress a command
16221da177e4SLinus Torvalds  * @info: interface to service
16231da177e4SLinus Torvalds  *
16241da177e4SLinus Torvalds  * Handle interrupts from the interface to progress a command
16251da177e4SLinus Torvalds  */
16261da177e4SLinus Torvalds irqreturn_t fas216_intr(FAS216_Info *info)
16271da177e4SLinus Torvalds {
16281da177e4SLinus Torvalds 	unsigned char inst, is, stat;
16291da177e4SLinus Torvalds 	int handled = IRQ_NONE;
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	fas216_checkmagic(info);
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	stat = fas216_readb(info, REG_STAT);
16341da177e4SLinus Torvalds 	is = fas216_readb(info, REG_IS);
16351da177e4SLinus Torvalds 	inst = fas216_readb(info, REG_INST);
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	add_debug_list(stat, is, inst, info->scsi.phase);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	if (stat & STAT_INT) {
16401da177e4SLinus Torvalds 		if (inst & INST_BUSRESET) {
16411da177e4SLinus Torvalds 			fas216_log(info, 0, "bus reset detected");
16421da177e4SLinus Torvalds 			fas216_bus_reset(info);
16431da177e4SLinus Torvalds 			scsi_report_bus_reset(info->host, 0);
16441da177e4SLinus Torvalds 		} else if (inst & INST_ILLEGALCMD) {
16451da177e4SLinus Torvalds 			fas216_log(info, LOG_ERROR, "illegal command given\n");
16461da177e4SLinus Torvalds 			fas216_dumpstate(info);
16471da177e4SLinus Torvalds 			print_debug_list();
16481da177e4SLinus Torvalds 		} else if (inst & INST_DISCONNECT)
16491da177e4SLinus Torvalds 			fas216_disconnect_intr(info);
16501da177e4SLinus Torvalds 		else if (inst & INST_RESELECTED)	/* reselected			*/
16511da177e4SLinus Torvalds 			fas216_reselected_intr(info);
16521da177e4SLinus Torvalds 		else if (inst & INST_BUSSERVICE)	/* bus service request		*/
16531da177e4SLinus Torvalds 			fas216_busservice_intr(info, stat, is);
16541da177e4SLinus Torvalds 		else if (inst & INST_FUNCDONE)		/* function done		*/
16551da177e4SLinus Torvalds 			fas216_funcdone_intr(info, stat, is);
16561da177e4SLinus Torvalds 		else
16571da177e4SLinus Torvalds 		    	fas216_log(info, 0, "unknown interrupt received:"
16581da177e4SLinus Torvalds 				" phase %s inst %02X is %02X stat %02X",
16591da177e4SLinus Torvalds 				fas216_drv_phase(info), inst, is, stat);
16601da177e4SLinus Torvalds 		handled = IRQ_HANDLED;
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds 	return handled;
16631da177e4SLinus Torvalds }
16641da177e4SLinus Torvalds 
1665ee0ca6baSHenne static void __fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
16661da177e4SLinus Torvalds {
16671da177e4SLinus Torvalds 	int tot_msglen;
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 	/* following what the ESP driver says */
16701da177e4SLinus Torvalds 	fas216_set_stc(info, 0);
16711da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	/* flush FIFO */
16741da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	/* load bus-id and timeout */
16771da177e4SLinus Torvalds 	fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
16781da177e4SLinus Torvalds 	fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 	/* synchronous transfers */
16811da177e4SLinus Torvalds 	fas216_set_sync(info, SCpnt->device->id);
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	tot_msglen = msgqueue_msglength(&info->scsi.msgs);
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds #ifdef DEBUG_MESSAGES
16861da177e4SLinus Torvalds 	{
16871da177e4SLinus Torvalds 		struct message *msg;
16881da177e4SLinus Torvalds 		int msgnr = 0, i;
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 		printk("scsi%d.%c: message out: ",
16911da177e4SLinus Torvalds 			info->host->host_no, '0' + SCpnt->device->id);
16921da177e4SLinus Torvalds 		while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
16931da177e4SLinus Torvalds 			printk("{ ");
16941da177e4SLinus Torvalds 			for (i = 0; i < msg->length; i++)
16951da177e4SLinus Torvalds 				printk("%02x ", msg->msg[i]);
16961da177e4SLinus Torvalds 			printk("} ");
16971da177e4SLinus Torvalds 		}
16981da177e4SLinus Torvalds 		printk("\n");
16991da177e4SLinus Torvalds 	}
17001da177e4SLinus Torvalds #endif
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	if (tot_msglen == 1 || tot_msglen == 3) {
17031da177e4SLinus Torvalds 		/*
17041da177e4SLinus Torvalds 		 * We have an easy message length to send...
17051da177e4SLinus Torvalds 		 */
17061da177e4SLinus Torvalds 		struct message *msg;
17071da177e4SLinus Torvalds 		int msgnr = 0, i;
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 		info->scsi.phase = PHASE_SELSTEPS;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 		/* load message bytes */
17121da177e4SLinus Torvalds 		while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
17131da177e4SLinus Torvalds 			for (i = 0; i < msg->length; i++)
17141da177e4SLinus Torvalds 				fas216_writeb(info, REG_FF, msg->msg[i]);
17151da177e4SLinus Torvalds 			msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
17161da177e4SLinus Torvalds 		}
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 		/* load command */
17191da177e4SLinus Torvalds 		for (i = 0; i < SCpnt->cmd_len; i++)
17201da177e4SLinus Torvalds 			fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 		if (tot_msglen == 1)
17231da177e4SLinus Torvalds 			fas216_cmd(info, CMD_SELECTATN);
17241da177e4SLinus Torvalds 		else
17251da177e4SLinus Torvalds 			fas216_cmd(info, CMD_SELECTATN3);
17261da177e4SLinus Torvalds 	} else {
17271da177e4SLinus Torvalds 		/*
17281da177e4SLinus Torvalds 		 * We have an unusual number of message bytes to send.
17291da177e4SLinus Torvalds 		 *  Load first byte into fifo, and issue SELECT with ATN and
17301da177e4SLinus Torvalds 		 *  stop steps.
17311da177e4SLinus Torvalds 		 */
17321da177e4SLinus Torvalds 		struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 		fas216_writeb(info, REG_FF, msg->msg[0]);
17351da177e4SLinus Torvalds 		msg->fifo = 1;
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 		fas216_cmd(info, CMD_SELECTATNSTOP);
17381da177e4SLinus Torvalds 	}
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds /*
17421da177e4SLinus Torvalds  * Decide whether we need to perform a parity test on this device.
17431da177e4SLinus Torvalds  * Can also be used to force parity error conditions during initial
17441da177e4SLinus Torvalds  * information transfer phase (message out) for test purposes.
17451da177e4SLinus Torvalds  */
17461da177e4SLinus Torvalds static int parity_test(FAS216_Info *info, int target)
17471da177e4SLinus Torvalds {
17481da177e4SLinus Torvalds #if 0
17491da177e4SLinus Torvalds 	if (target == 3) {
17501da177e4SLinus Torvalds 		info->device[target].parity_check = 0;
17511da177e4SLinus Torvalds 		return 1;
17521da177e4SLinus Torvalds 	}
17531da177e4SLinus Torvalds #endif
17541da177e4SLinus Torvalds 	return info->device[target].parity_check;
17551da177e4SLinus Torvalds }
17561da177e4SLinus Torvalds 
1757ee0ca6baSHenne static void fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
17581da177e4SLinus Torvalds {
17591da177e4SLinus Torvalds 	int disconnect_ok;
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	/*
17621da177e4SLinus Torvalds 	 * claim host busy
17631da177e4SLinus Torvalds 	 */
17641da177e4SLinus Torvalds 	info->scsi.phase = PHASE_SELECTION;
17651da177e4SLinus Torvalds 	info->scsi.SCp = SCpnt->SCp;
17661da177e4SLinus Torvalds 	info->SCpnt = SCpnt;
17671da177e4SLinus Torvalds 	info->dma.transfer_type = fasdma_none;
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	if (parity_test(info, SCpnt->device->id))
17701da177e4SLinus Torvalds 		fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
17711da177e4SLinus Torvalds 	else
17721da177e4SLinus Torvalds 		fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	/*
17751da177e4SLinus Torvalds 	 * Don't allow request sense commands to disconnect.
17761da177e4SLinus Torvalds 	 */
17771da177e4SLinus Torvalds 	disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
17781da177e4SLinus Torvalds 			info->device[SCpnt->device->id].disconnect_ok;
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	/*
17811da177e4SLinus Torvalds 	 * build outgoing message bytes
17821da177e4SLinus Torvalds 	 */
17831da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
17841da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->device->lun));
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 	/*
17871da177e4SLinus Torvalds 	 * add tag message if required
17881da177e4SLinus Torvalds 	 */
17891da177e4SLinus Torvalds 	if (SCpnt->tag)
17901da177e4SLinus Torvalds 		msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	do {
17931da177e4SLinus Torvalds #ifdef SCSI2_SYNC
17941da177e4SLinus Torvalds 		if ((info->device[SCpnt->device->id].sync_state == neg_wait ||
17951da177e4SLinus Torvalds 		     info->device[SCpnt->device->id].sync_state == neg_complete) &&
17961da177e4SLinus Torvalds 		    (SCpnt->cmnd[0] == REQUEST_SENSE ||
17971da177e4SLinus Torvalds 		     SCpnt->cmnd[0] == INQUIRY)) {
17981da177e4SLinus Torvalds 			info->device[SCpnt->device->id].sync_state = neg_inprogress;
17991da177e4SLinus Torvalds 			msgqueue_addmsg(&info->scsi.msgs, 5,
18001da177e4SLinus Torvalds 					EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
18011da177e4SLinus Torvalds 					1000 / info->ifcfg.clockrate,
18021da177e4SLinus Torvalds 					info->ifcfg.sync_max_depth);
18031da177e4SLinus Torvalds 			break;
18041da177e4SLinus Torvalds 		}
18051da177e4SLinus Torvalds #endif
18061da177e4SLinus Torvalds 	} while (0);
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	__fas216_start_command(info, SCpnt);
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds 
1811ee0ca6baSHenne static void fas216_allocate_tag(FAS216_Info *info, struct scsi_cmnd *SCpnt)
18121da177e4SLinus Torvalds {
18131da177e4SLinus Torvalds #ifdef SCSI2_TAG
18141da177e4SLinus Torvalds 	/*
18151da177e4SLinus Torvalds 	 * tagged queuing - allocate a new tag to this command
18161da177e4SLinus Torvalds 	 */
18171da177e4SLinus Torvalds 	if (SCpnt->device->simple_tags && SCpnt->cmnd[0] != REQUEST_SENSE &&
18181da177e4SLinus Torvalds 	    SCpnt->cmnd[0] != INQUIRY) {
18191da177e4SLinus Torvalds 	    SCpnt->device->current_tag += 1;
18201da177e4SLinus Torvalds 		if (SCpnt->device->current_tag == 0)
18211da177e4SLinus Torvalds 		    SCpnt->device->current_tag = 1;
18221da177e4SLinus Torvalds 			SCpnt->tag = SCpnt->device->current_tag;
18231da177e4SLinus Torvalds 	} else
18241da177e4SLinus Torvalds #endif
18259cb78c16SHannes Reinecke 		set_bit(SCpnt->device->id * 8 +
18269cb78c16SHannes Reinecke 			(u8)(SCpnt->device->lun & 0x7), info->busyluns);
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	info->stats.removes += 1;
18291da177e4SLinus Torvalds 	switch (SCpnt->cmnd[0]) {
18301da177e4SLinus Torvalds 	case WRITE_6:
18311da177e4SLinus Torvalds 	case WRITE_10:
18321da177e4SLinus Torvalds 	case WRITE_12:
18331da177e4SLinus Torvalds 		info->stats.writes += 1;
18341da177e4SLinus Torvalds 		break;
18351da177e4SLinus Torvalds 	case READ_6:
18361da177e4SLinus Torvalds 	case READ_10:
18371da177e4SLinus Torvalds 	case READ_12:
18381da177e4SLinus Torvalds 		info->stats.reads += 1;
18391da177e4SLinus Torvalds 		break;
18401da177e4SLinus Torvalds 	default:
18411da177e4SLinus Torvalds 		info->stats.miscs += 1;
18421da177e4SLinus Torvalds 		break;
18431da177e4SLinus Torvalds 	}
18441da177e4SLinus Torvalds }
18451da177e4SLinus Torvalds 
1846ee0ca6baSHenne static void fas216_do_bus_device_reset(FAS216_Info *info,
1847ee0ca6baSHenne 				       struct scsi_cmnd *SCpnt)
18481da177e4SLinus Torvalds {
18491da177e4SLinus Torvalds 	struct message *msg;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	/*
18521da177e4SLinus Torvalds 	 * claim host busy
18531da177e4SLinus Torvalds 	 */
18541da177e4SLinus Torvalds 	info->scsi.phase = PHASE_SELECTION;
18551da177e4SLinus Torvalds 	info->scsi.SCp = SCpnt->SCp;
18561da177e4SLinus Torvalds 	info->SCpnt = SCpnt;
18571da177e4SLinus Torvalds 	info->dma.transfer_type = fasdma_none;
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "sending bus device reset");
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
18621da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	/* following what the ESP driver says */
18651da177e4SLinus Torvalds 	fas216_set_stc(info, 0);
18661da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	/* flush FIFO */
18691da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	/* load bus-id and timeout */
18721da177e4SLinus Torvalds 	fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
18731da177e4SLinus Torvalds 	fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	/* synchronous transfers */
18761da177e4SLinus Torvalds 	fas216_set_sync(info, SCpnt->device->id);
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds 	msg = msgqueue_getmsg(&info->scsi.msgs, 0);
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	fas216_writeb(info, REG_FF, BUS_DEVICE_RESET);
18811da177e4SLinus Torvalds 	msg->fifo = 1;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	fas216_cmd(info, CMD_SELECTATNSTOP);
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds /**
18871da177e4SLinus Torvalds  * fas216_kick - kick a command to the interface
18881da177e4SLinus Torvalds  * @info: our host interface to kick
18891da177e4SLinus Torvalds  *
18901da177e4SLinus Torvalds  * Kick a command to the interface, interface should be idle.
18911da177e4SLinus Torvalds  * Notes: Interrupts are always disabled!
18921da177e4SLinus Torvalds  */
18931da177e4SLinus Torvalds static void fas216_kick(FAS216_Info *info)
18941da177e4SLinus Torvalds {
1895ee0ca6baSHenne 	struct scsi_cmnd *SCpnt = NULL;
18961da177e4SLinus Torvalds #define TYPE_OTHER	0
18971da177e4SLinus Torvalds #define TYPE_RESET	1
18981da177e4SLinus Torvalds #define TYPE_QUEUE	2
18991da177e4SLinus Torvalds 	int where_from = TYPE_OTHER;
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds 	fas216_checkmagic(info);
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	/*
19041da177e4SLinus Torvalds 	 * Obtain the next command to process.
19051da177e4SLinus Torvalds 	 */
19061da177e4SLinus Torvalds 	do {
19071da177e4SLinus Torvalds 		if (info->rstSCpnt) {
19081da177e4SLinus Torvalds 			SCpnt = info->rstSCpnt;
19091da177e4SLinus Torvalds 			/* don't remove it */
19101da177e4SLinus Torvalds 			where_from = TYPE_RESET;
19111da177e4SLinus Torvalds 			break;
19121da177e4SLinus Torvalds 		}
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 		if (info->reqSCpnt) {
19151da177e4SLinus Torvalds 			SCpnt = info->reqSCpnt;
19161da177e4SLinus Torvalds 			info->reqSCpnt = NULL;
19171da177e4SLinus Torvalds 			break;
19181da177e4SLinus Torvalds 		}
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 		if (info->origSCpnt) {
19211da177e4SLinus Torvalds 			SCpnt = info->origSCpnt;
19221da177e4SLinus Torvalds 			info->origSCpnt = NULL;
19231da177e4SLinus Torvalds 			break;
19241da177e4SLinus Torvalds 		}
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 		/* retrieve next command */
19271da177e4SLinus Torvalds 		if (!SCpnt) {
19281da177e4SLinus Torvalds 			SCpnt = queue_remove_exclude(&info->queues.issue,
19291da177e4SLinus Torvalds 						     info->busyluns);
19301da177e4SLinus Torvalds 			where_from = TYPE_QUEUE;
19311da177e4SLinus Torvalds 			break;
19321da177e4SLinus Torvalds 		}
19331da177e4SLinus Torvalds 	} while (0);
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	if (!SCpnt) {
19361da177e4SLinus Torvalds 		/*
19371da177e4SLinus Torvalds 		 * no command pending, so enable reselection.
19381da177e4SLinus Torvalds 		 */
19391da177e4SLinus Torvalds 		fas216_cmd(info, CMD_ENABLESEL);
19401da177e4SLinus Torvalds 		return;
19411da177e4SLinus Torvalds 	}
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	/*
19441da177e4SLinus Torvalds 	 * We're going to start a command, so disable reselection
19451da177e4SLinus Torvalds 	 */
19461da177e4SLinus Torvalds 	fas216_cmd(info, CMD_DISABLESEL);
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	if (info->scsi.disconnectable && info->SCpnt) {
19491da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT,
19501da177e4SLinus Torvalds 			"moved command for %d to disconnected queue",
19511da177e4SLinus Torvalds 			info->SCpnt->device->id);
19521da177e4SLinus Torvalds 		queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
19531da177e4SLinus Torvalds 		info->scsi.disconnectable = 0;
19541da177e4SLinus Torvalds 		info->SCpnt = NULL;
19551da177e4SLinus Torvalds 	}
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds 	fas216_log_command(info, LOG_CONNECT | LOG_MESSAGES, SCpnt,
19581da177e4SLinus Torvalds 			   "starting");
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	switch (where_from) {
19611da177e4SLinus Torvalds 	case TYPE_QUEUE:
19621da177e4SLinus Torvalds 		fas216_allocate_tag(info, SCpnt);
19631da177e4SLinus Torvalds 	case TYPE_OTHER:
19641da177e4SLinus Torvalds 		fas216_start_command(info, SCpnt);
19651da177e4SLinus Torvalds 		break;
19661da177e4SLinus Torvalds 	case TYPE_RESET:
19671da177e4SLinus Torvalds 		fas216_do_bus_device_reset(info, SCpnt);
19681da177e4SLinus Torvalds 		break;
19691da177e4SLinus Torvalds 	}
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	fas216_log(info, LOG_CONNECT, "select: data pointers [%p, %X]",
19721da177e4SLinus Torvalds 		info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	/*
19751da177e4SLinus Torvalds 	 * should now get either DISCONNECT or
19761da177e4SLinus Torvalds 	 * (FUNCTION DONE with BUS SERVICE) interrupt
19771da177e4SLinus Torvalds 	 */
19781da177e4SLinus Torvalds }
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds /*
19811da177e4SLinus Torvalds  * Clean up from issuing a BUS DEVICE RESET message to a device.
19821da177e4SLinus Torvalds  */
1983ee0ca6baSHenne static void fas216_devicereset_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
1984ee0ca6baSHenne 				    unsigned int result)
19851da177e4SLinus Torvalds {
19861da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "fas216 device reset complete");
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	info->rstSCpnt = NULL;
19891da177e4SLinus Torvalds 	info->rst_dev_status = 1;
19901da177e4SLinus Torvalds 	wake_up(&info->eh_wait);
19911da177e4SLinus Torvalds }
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds /**
19941da177e4SLinus Torvalds  * fas216_rq_sns_done - Finish processing automatic request sense command
19951da177e4SLinus Torvalds  * @info: interface that completed
19961da177e4SLinus Torvalds  * @SCpnt: command that completed
19971da177e4SLinus Torvalds  * @result: driver byte of result
19981da177e4SLinus Torvalds  *
19991da177e4SLinus Torvalds  * Finish processing automatic request sense command
20001da177e4SLinus Torvalds  */
2001ee0ca6baSHenne static void fas216_rq_sns_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
2002ee0ca6baSHenne 			       unsigned int result)
20031da177e4SLinus Torvalds {
20041da177e4SLinus Torvalds 	fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
20051da177e4SLinus Torvalds 		   "request sense complete, result=0x%04x%02x%02x",
20061da177e4SLinus Torvalds 		   result, SCpnt->SCp.Message, SCpnt->SCp.Status);
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds 	if (result != DID_OK || SCpnt->SCp.Status != GOOD)
20091da177e4SLinus Torvalds 		/*
20101da177e4SLinus Torvalds 		 * Something went wrong.  Make sure that we don't
20111da177e4SLinus Torvalds 		 * have valid data in the sense buffer that could
20121da177e4SLinus Torvalds 		 * confuse the higher levels.
20131da177e4SLinus Torvalds 		 */
20141da177e4SLinus Torvalds 		memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
20151da177e4SLinus Torvalds //printk("scsi%d.%c: sense buffer: ", info->host->host_no, '0' + SCpnt->device->id);
20161da177e4SLinus Torvalds //{ int i; for (i = 0; i < 32; i++) printk("%02x ", SCpnt->sense_buffer[i]); printk("\n"); }
20171da177e4SLinus Torvalds 	/*
20181da177e4SLinus Torvalds 	 * Note that we don't set SCpnt->result, since that should
20191da177e4SLinus Torvalds 	 * reflect the status of the command that we were asked by
20201da177e4SLinus Torvalds 	 * the upper layers to process.  This would have been set
20211da177e4SLinus Torvalds 	 * correctly by fas216_std_done.
20221da177e4SLinus Torvalds 	 */
2023ccf9ea91SBoaz Harrosh 	scsi_eh_restore_cmnd(SCpnt, &info->ses);
20241da177e4SLinus Torvalds 	SCpnt->scsi_done(SCpnt);
20251da177e4SLinus Torvalds }
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds /**
20281da177e4SLinus Torvalds  * fas216_std_done - finish processing of standard command
20291da177e4SLinus Torvalds  * @info: interface that completed
20301da177e4SLinus Torvalds  * @SCpnt: command that completed
20311da177e4SLinus Torvalds  * @result: driver byte of result
20321da177e4SLinus Torvalds  *
20331da177e4SLinus Torvalds  * Finish processing of standard command
20341da177e4SLinus Torvalds  */
20351da177e4SLinus Torvalds static void
2036ee0ca6baSHenne fas216_std_done(FAS216_Info *info, struct scsi_cmnd *SCpnt, unsigned int result)
20371da177e4SLinus Torvalds {
20381da177e4SLinus Torvalds 	info->stats.fins += 1;
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	SCpnt->result = result << 16 | info->scsi.SCp.Message << 8 |
20411da177e4SLinus Torvalds 			info->scsi.SCp.Status;
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	fas216_log_command(info, LOG_CONNECT, SCpnt,
20441da177e4SLinus Torvalds 		"command complete, result=0x%08x", SCpnt->result);
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 	/*
20471da177e4SLinus Torvalds 	 * If the driver detected an error, we're all done.
20481da177e4SLinus Torvalds 	 */
20491da177e4SLinus Torvalds 	if (host_byte(SCpnt->result) != DID_OK ||
20501da177e4SLinus Torvalds 	    msg_byte(SCpnt->result) != COMMAND_COMPLETE)
20511da177e4SLinus Torvalds 		goto done;
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	/*
20541da177e4SLinus Torvalds 	 * If the command returned CHECK_CONDITION or COMMAND_TERMINATED
20551da177e4SLinus Torvalds 	 * status, request the sense information.
20561da177e4SLinus Torvalds 	 */
20571da177e4SLinus Torvalds 	if (status_byte(SCpnt->result) == CHECK_CONDITION ||
20581da177e4SLinus Torvalds 	    status_byte(SCpnt->result) == COMMAND_TERMINATED)
20591da177e4SLinus Torvalds 		goto request_sense;
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	/*
20621da177e4SLinus Torvalds 	 * If the command did not complete with GOOD status,
20631da177e4SLinus Torvalds 	 * we are all done here.
20641da177e4SLinus Torvalds 	 */
20651da177e4SLinus Torvalds 	if (status_byte(SCpnt->result) != GOOD)
20661da177e4SLinus Torvalds 		goto done;
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 	/*
20691da177e4SLinus Torvalds 	 * We have successfully completed a command.  Make sure that
20701da177e4SLinus Torvalds 	 * we do not have any buffers left to transfer.  The world
20711da177e4SLinus Torvalds 	 * is not perfect, and we seem to occasionally hit this.
20721da177e4SLinus Torvalds 	 * It can be indicative of a buggy driver, target or the upper
20731da177e4SLinus Torvalds 	 * levels of the SCSI code.
20741da177e4SLinus Torvalds 	 */
20751da177e4SLinus Torvalds 	if (info->scsi.SCp.ptr) {
20761da177e4SLinus Torvalds 		switch (SCpnt->cmnd[0]) {
20771da177e4SLinus Torvalds 		case INQUIRY:
20781da177e4SLinus Torvalds 		case START_STOP:
20791da177e4SLinus Torvalds 		case MODE_SENSE:
20801da177e4SLinus Torvalds 			break;
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 		default:
2083e04ca907SHannes Reinecke 			scmd_printk(KERN_ERR, SCpnt,
2084e04ca907SHannes Reinecke 				    "incomplete data transfer detected: res=%08X ptr=%p len=%X\n",
20851da177e4SLinus Torvalds 				    SCpnt->result, info->scsi.SCp.ptr,
20861da177e4SLinus Torvalds 				    info->scsi.SCp.this_residual);
2087e04ca907SHannes Reinecke 			scsi_print_command(SCpnt);
2088279c43b3SHannes Reinecke 			set_host_byte(SCpnt, DID_ERROR);
20891da177e4SLinus Torvalds 			goto request_sense;
20901da177e4SLinus Torvalds 		}
20911da177e4SLinus Torvalds 	}
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds done:
20941da177e4SLinus Torvalds 	if (SCpnt->scsi_done) {
20951da177e4SLinus Torvalds 		SCpnt->scsi_done(SCpnt);
20961da177e4SLinus Torvalds 		return;
20971da177e4SLinus Torvalds 	}
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	panic("scsi%d.H: null scsi_done function in fas216_done",
21001da177e4SLinus Torvalds 		info->host->host_no);
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds request_sense:
21041da177e4SLinus Torvalds 	if (SCpnt->cmnd[0] == REQUEST_SENSE)
21051da177e4SLinus Torvalds 		goto done;
21061da177e4SLinus Torvalds 
2107ccf9ea91SBoaz Harrosh 	scsi_eh_prep_cmnd(SCpnt, &info->ses, NULL, 0, ~0);
21081da177e4SLinus Torvalds 	fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
21091da177e4SLinus Torvalds 			  "requesting sense");
2110ccf9ea91SBoaz Harrosh 	init_SCp(SCpnt);
21111da177e4SLinus Torvalds 	SCpnt->SCp.Message = 0;
21121da177e4SLinus Torvalds 	SCpnt->SCp.Status = 0;
21131da177e4SLinus Torvalds 	SCpnt->tag = 0;
21141da177e4SLinus Torvalds 	SCpnt->host_scribble = (void *)fas216_rq_sns_done;
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	/*
21171da177e4SLinus Torvalds 	 * Place this command into the high priority "request
21181da177e4SLinus Torvalds 	 * sense" slot.  This will be the very next command
21191da177e4SLinus Torvalds 	 * executed, unless a target connects to us.
21201da177e4SLinus Torvalds 	 */
21211da177e4SLinus Torvalds 	if (info->reqSCpnt)
212225985edcSLucas De Marchi 		printk(KERN_WARNING "scsi%d.%c: losing request command\n",
21231da177e4SLinus Torvalds 			info->host->host_no, '0' + SCpnt->device->id);
21241da177e4SLinus Torvalds 	info->reqSCpnt = SCpnt;
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds /**
21281da177e4SLinus Torvalds  * fas216_done - complete processing for current command
21291da177e4SLinus Torvalds  * @info: interface that completed
21301da177e4SLinus Torvalds  * @result: driver byte of result
21311da177e4SLinus Torvalds  *
21321da177e4SLinus Torvalds  * Complete processing for current command
21331da177e4SLinus Torvalds  */
21341da177e4SLinus Torvalds static void fas216_done(FAS216_Info *info, unsigned int result)
21351da177e4SLinus Torvalds {
2136ee0ca6baSHenne 	void (*fn)(FAS216_Info *, struct scsi_cmnd *, unsigned int);
2137ee0ca6baSHenne 	struct scsi_cmnd *SCpnt;
21381da177e4SLinus Torvalds 	unsigned long flags;
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	fas216_checkmagic(info);
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds 	if (!info->SCpnt)
21431da177e4SLinus Torvalds 		goto no_command;
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 	SCpnt = info->SCpnt;
21461da177e4SLinus Torvalds 	info->SCpnt = NULL;
21471da177e4SLinus Torvalds     	info->scsi.phase = PHASE_IDLE;
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	if (info->scsi.aborting) {
21501da177e4SLinus Torvalds 		fas216_log(info, 0, "uncaught abort - returning DID_ABORT");
21511da177e4SLinus Torvalds 		result = DID_ABORT;
21521da177e4SLinus Torvalds 		info->scsi.aborting = 0;
21531da177e4SLinus Torvalds 	}
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	/*
21561da177e4SLinus Torvalds 	 * Sanity check the completion - if we have zero bytes left
21571da177e4SLinus Torvalds 	 * to transfer, we should not have a valid pointer.
21581da177e4SLinus Torvalds 	 */
21591da177e4SLinus Torvalds 	if (info->scsi.SCp.ptr && info->scsi.SCp.this_residual == 0) {
2160e04ca907SHannes Reinecke 		scmd_printk(KERN_INFO, SCpnt,
2161e04ca907SHannes Reinecke 			    "zero bytes left to transfer, but buffer pointer still valid: ptr=%p len=%08x\n",
21621da177e4SLinus Torvalds 			    info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
21631da177e4SLinus Torvalds 		info->scsi.SCp.ptr = NULL;
2164e04ca907SHannes Reinecke 		scsi_print_command(SCpnt);
21651da177e4SLinus Torvalds 	}
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	/*
21681da177e4SLinus Torvalds 	 * Clear down this command as completed.  If we need to request
21691da177e4SLinus Torvalds 	 * the sense information, fas216_kick will re-assert the busy
21701da177e4SLinus Torvalds 	 * status.
21711da177e4SLinus Torvalds 	 */
21721da177e4SLinus Torvalds 	info->device[SCpnt->device->id].parity_check = 0;
21739cb78c16SHannes Reinecke 	clear_bit(SCpnt->device->id * 8 +
21749cb78c16SHannes Reinecke 		  (u8)(SCpnt->device->lun & 0x7), info->busyluns);
21751da177e4SLinus Torvalds 
2176ee0ca6baSHenne 	fn = (void (*)(FAS216_Info *, struct scsi_cmnd *, unsigned int))SCpnt->host_scribble;
21771da177e4SLinus Torvalds 	fn(info, SCpnt, result);
21781da177e4SLinus Torvalds 
217941569e37SRussell King 	if (info->scsi.irq) {
21801da177e4SLinus Torvalds 		spin_lock_irqsave(&info->host_lock, flags);
21811da177e4SLinus Torvalds 		if (info->scsi.phase == PHASE_IDLE)
21821da177e4SLinus Torvalds 			fas216_kick(info);
21831da177e4SLinus Torvalds 		spin_unlock_irqrestore(&info->host_lock, flags);
21841da177e4SLinus Torvalds 	}
21851da177e4SLinus Torvalds 	return;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds no_command:
21881da177e4SLinus Torvalds 	panic("scsi%d.H: null command in fas216_done",
21891da177e4SLinus Torvalds 		info->host->host_no);
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds /**
21931da177e4SLinus Torvalds  * fas216_queue_command - queue a command for adapter to process.
21941da177e4SLinus Torvalds  * @SCpnt: Command to queue
21951da177e4SLinus Torvalds  * @done: done function to call once command is complete
21961da177e4SLinus Torvalds  *
21971da177e4SLinus Torvalds  * Queue a command for adapter to process.
21981da177e4SLinus Torvalds  * Returns: 0 on success, else error.
21991da177e4SLinus Torvalds  * Notes: io_request_lock is held, interrupts are disabled.
22001da177e4SLinus Torvalds  */
2201f281233dSJeff Garzik static int fas216_queue_command_lck(struct scsi_cmnd *SCpnt,
2202ee0ca6baSHenne 			 void (*done)(struct scsi_cmnd *))
22031da177e4SLinus Torvalds {
22041da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
22051da177e4SLinus Torvalds 	int result;
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 	fas216_checkmagic(info);
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	fas216_log_command(info, LOG_CONNECT, SCpnt,
22101da177e4SLinus Torvalds 			   "received command (%p)", SCpnt);
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	SCpnt->scsi_done = done;
22131da177e4SLinus Torvalds 	SCpnt->host_scribble = (void *)fas216_std_done;
22141da177e4SLinus Torvalds 	SCpnt->result = 0;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	init_SCp(SCpnt);
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	info->stats.queues += 1;
22191da177e4SLinus Torvalds 	SCpnt->tag = 0;
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	spin_lock(&info->host_lock);
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	/*
22241da177e4SLinus Torvalds 	 * Add command into execute queue and let it complete under
22251da177e4SLinus Torvalds 	 * whatever scheme we're using.
22261da177e4SLinus Torvalds 	 */
22271da177e4SLinus Torvalds 	result = !queue_add_cmd_ordered(&info->queues.issue, SCpnt);
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	/*
22301da177e4SLinus Torvalds 	 * If we successfully added the command,
22311da177e4SLinus Torvalds 	 * kick the interface to get it moving.
22321da177e4SLinus Torvalds 	 */
22331da177e4SLinus Torvalds 	if (result == 0 && info->scsi.phase == PHASE_IDLE)
22341da177e4SLinus Torvalds 		fas216_kick(info);
22351da177e4SLinus Torvalds 	spin_unlock(&info->host_lock);
22361da177e4SLinus Torvalds 
22371da177e4SLinus Torvalds 	fas216_log_target(info, LOG_CONNECT, -1, "queue %s",
22381da177e4SLinus Torvalds 		result ? "failure" : "success");
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	return result;
22411da177e4SLinus Torvalds }
22421da177e4SLinus Torvalds 
2243f281233dSJeff Garzik DEF_SCSI_QCMD(fas216_queue_command)
2244f281233dSJeff Garzik 
22451da177e4SLinus Torvalds /**
22461da177e4SLinus Torvalds  * fas216_internal_done - trigger restart of a waiting thread in fas216_noqueue_command
22471da177e4SLinus Torvalds  * @SCpnt: Command to wake
22481da177e4SLinus Torvalds  *
22491da177e4SLinus Torvalds  * Trigger restart of a waiting thread in fas216_command
22501da177e4SLinus Torvalds  */
2251ee0ca6baSHenne static void fas216_internal_done(struct scsi_cmnd *SCpnt)
22521da177e4SLinus Torvalds {
22531da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	fas216_checkmagic(info);
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	info->internal_done = 1;
22581da177e4SLinus Torvalds }
22591da177e4SLinus Torvalds 
22601da177e4SLinus Torvalds /**
22611da177e4SLinus Torvalds  * fas216_noqueue_command - process a command for the adapter.
22621da177e4SLinus Torvalds  * @SCpnt: Command to queue
22631da177e4SLinus Torvalds  *
22641da177e4SLinus Torvalds  * Queue a command for adapter to process.
22651da177e4SLinus Torvalds  * Returns: scsi result code.
22661da177e4SLinus Torvalds  * Notes: io_request_lock is held, interrupts are disabled.
22671da177e4SLinus Torvalds  */
2268f281233dSJeff Garzik static int fas216_noqueue_command_lck(struct scsi_cmnd *SCpnt,
2269ee0ca6baSHenne 			   void (*done)(struct scsi_cmnd *))
22701da177e4SLinus Torvalds {
22711da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds 	fas216_checkmagic(info);
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	/*
22761da177e4SLinus Torvalds 	 * We should only be using this if we don't have an interrupt.
22771da177e4SLinus Torvalds 	 * Provide some "incentive" to use the queueing code.
22781da177e4SLinus Torvalds 	 */
227941569e37SRussell King 	BUG_ON(info->scsi.irq);
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds 	info->internal_done = 0;
2282f281233dSJeff Garzik 	fas216_queue_command_lck(SCpnt, fas216_internal_done);
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds 	/*
22851da177e4SLinus Torvalds 	 * This wastes time, since we can't return until the command is
22861da177e4SLinus Torvalds 	 * complete. We can't sleep either since we may get re-entered!
22871da177e4SLinus Torvalds 	 * However, we must re-enable interrupts, or else we'll be
22881da177e4SLinus Torvalds 	 * waiting forever.
22891da177e4SLinus Torvalds 	 */
22901da177e4SLinus Torvalds 	spin_unlock_irq(info->host->host_lock);
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds 	while (!info->internal_done) {
22931da177e4SLinus Torvalds 		/*
22941da177e4SLinus Torvalds 		 * If we don't have an IRQ, then we must poll the card for
22951da177e4SLinus Torvalds 		 * it's interrupt, and use that to call this driver's
22961da177e4SLinus Torvalds 		 * interrupt routine.  That way, we keep the command
229725985edcSLucas De Marchi 		 * progressing.  Maybe we can add some intelligence here
22981da177e4SLinus Torvalds 		 * and go to sleep if we know that the device is going
22991da177e4SLinus Torvalds 		 * to be some time (eg, disconnected).
23001da177e4SLinus Torvalds 		 */
23011da177e4SLinus Torvalds 		if (fas216_readb(info, REG_STAT) & STAT_INT) {
23021da177e4SLinus Torvalds 			spin_lock_irq(info->host->host_lock);
23031da177e4SLinus Torvalds 			fas216_intr(info);
23041da177e4SLinus Torvalds 			spin_unlock_irq(info->host->host_lock);
23051da177e4SLinus Torvalds 		}
23061da177e4SLinus Torvalds 	}
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds 	spin_lock_irq(info->host->host_lock);
23091da177e4SLinus Torvalds 
23101da177e4SLinus Torvalds 	done(SCpnt);
23111da177e4SLinus Torvalds 
23121da177e4SLinus Torvalds 	return 0;
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
2315f281233dSJeff Garzik DEF_SCSI_QCMD(fas216_noqueue_command)
2316f281233dSJeff Garzik 
23171da177e4SLinus Torvalds /*
23181da177e4SLinus Torvalds  * Error handler timeout function.  Indicate that we timed out,
23191da177e4SLinus Torvalds  * and wake up any error handler process so it can continue.
23201da177e4SLinus Torvalds  */
23211da177e4SLinus Torvalds static void fas216_eh_timer(unsigned long data)
23221da177e4SLinus Torvalds {
23231da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)data;
23241da177e4SLinus Torvalds 
23251da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "error handling timed out\n");
23261da177e4SLinus Torvalds 
23271da177e4SLinus Torvalds 	del_timer(&info->eh_timer);
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 	if (info->rst_bus_status == 0)
23301da177e4SLinus Torvalds 		info->rst_bus_status = -1;
23311da177e4SLinus Torvalds 	if (info->rst_dev_status == 0)
23321da177e4SLinus Torvalds 		info->rst_dev_status = -1;
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	wake_up(&info->eh_wait);
23351da177e4SLinus Torvalds }
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds enum res_find {
23381da177e4SLinus Torvalds 	res_failed,		/* not found			*/
23391da177e4SLinus Torvalds 	res_success,		/* command on issue queue	*/
23401da177e4SLinus Torvalds 	res_hw_abort		/* command on disconnected dev	*/
23411da177e4SLinus Torvalds };
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds /**
23441da177e4SLinus Torvalds  * fas216_do_abort - decide how to abort a command
23451da177e4SLinus Torvalds  * @SCpnt: command to abort
23461da177e4SLinus Torvalds  *
23471da177e4SLinus Torvalds  * Decide how to abort a command.
23481da177e4SLinus Torvalds  * Returns: abort status
23491da177e4SLinus Torvalds  */
2350ee0ca6baSHenne static enum res_find fas216_find_command(FAS216_Info *info,
2351ee0ca6baSHenne 					 struct scsi_cmnd *SCpnt)
23521da177e4SLinus Torvalds {
23531da177e4SLinus Torvalds 	enum res_find res = res_failed;
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	if (queue_remove_cmd(&info->queues.issue, SCpnt)) {
23561da177e4SLinus Torvalds 		/*
23571da177e4SLinus Torvalds 		 * The command was on the issue queue, and has not been
23581da177e4SLinus Torvalds 		 * issued yet.  We can remove the command from the queue,
23591da177e4SLinus Torvalds 		 * and acknowledge the abort.  Neither the device nor the
23601da177e4SLinus Torvalds 		 * interface know about the command.
23611da177e4SLinus Torvalds 		 */
23621da177e4SLinus Torvalds 		printk("on issue queue ");
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 		res = res_success;
23651da177e4SLinus Torvalds 	} else if (queue_remove_cmd(&info->queues.disconnected, SCpnt)) {
23661da177e4SLinus Torvalds 		/*
23671da177e4SLinus Torvalds 		 * The command was on the disconnected queue.  We must
23681da177e4SLinus Torvalds 		 * reconnect with the device if possible, and send it
23691da177e4SLinus Torvalds 		 * an abort message.
23701da177e4SLinus Torvalds 		 */
23711da177e4SLinus Torvalds 		printk("on disconnected queue ");
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds 		res = res_hw_abort;
23741da177e4SLinus Torvalds 	} else if (info->SCpnt == SCpnt) {
23751da177e4SLinus Torvalds 		printk("executing ");
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds 		switch (info->scsi.phase) {
23781da177e4SLinus Torvalds 		/*
23791da177e4SLinus Torvalds 		 * If the interface is idle, and the command is 'disconnectable',
23801da177e4SLinus Torvalds 		 * then it is the same as on the disconnected queue.
23811da177e4SLinus Torvalds 		 */
23821da177e4SLinus Torvalds 		case PHASE_IDLE:
23831da177e4SLinus Torvalds 			if (info->scsi.disconnectable) {
23841da177e4SLinus Torvalds 				info->scsi.disconnectable = 0;
23851da177e4SLinus Torvalds 				info->SCpnt = NULL;
23861da177e4SLinus Torvalds 				res = res_hw_abort;
23871da177e4SLinus Torvalds 			}
23881da177e4SLinus Torvalds 			break;
23891da177e4SLinus Torvalds 
23901da177e4SLinus Torvalds 		default:
23911da177e4SLinus Torvalds 			break;
23921da177e4SLinus Torvalds 		}
23931da177e4SLinus Torvalds 	} else if (info->origSCpnt == SCpnt) {
23941da177e4SLinus Torvalds 		/*
23951da177e4SLinus Torvalds 		 * The command will be executed next, but a command
23961da177e4SLinus Torvalds 		 * is currently using the interface.  This is similar to
23971da177e4SLinus Torvalds 		 * being on the issue queue, except the busylun bit has
23981da177e4SLinus Torvalds 		 * been set.
23991da177e4SLinus Torvalds 		 */
24001da177e4SLinus Torvalds 		info->origSCpnt = NULL;
24019cb78c16SHannes Reinecke 		clear_bit(SCpnt->device->id * 8 +
24029cb78c16SHannes Reinecke 			  (u8)(SCpnt->device->lun & 0x7), info->busyluns);
24031da177e4SLinus Torvalds 		printk("waiting for execution ");
24041da177e4SLinus Torvalds 		res = res_success;
24051da177e4SLinus Torvalds 	} else
24061da177e4SLinus Torvalds 		printk("unknown ");
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	return res;
24091da177e4SLinus Torvalds }
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds /**
24121da177e4SLinus Torvalds  * fas216_eh_abort - abort this command
24131da177e4SLinus Torvalds  * @SCpnt: command to abort
24141da177e4SLinus Torvalds  *
24151da177e4SLinus Torvalds  * Abort this command.
24161da177e4SLinus Torvalds  * Returns: FAILED if unable to abort
24171da177e4SLinus Torvalds  * Notes: io_request_lock is taken, and irqs are disabled
24181da177e4SLinus Torvalds  */
2419ee0ca6baSHenne int fas216_eh_abort(struct scsi_cmnd *SCpnt)
24201da177e4SLinus Torvalds {
24211da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
24221da177e4SLinus Torvalds 	int result = FAILED;
24231da177e4SLinus Torvalds 
24241da177e4SLinus Torvalds 	fas216_checkmagic(info);
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 	info->stats.aborts += 1;
24271da177e4SLinus Torvalds 
24281fa6b5fbSHannes Reinecke 	scmd_printk(KERN_WARNING, SCpnt, "abort command\n");
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	print_debug_list();
24311da177e4SLinus Torvalds 	fas216_dumpstate(info);
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	switch (fas216_find_command(info, SCpnt)) {
24341da177e4SLinus Torvalds 	/*
24351da177e4SLinus Torvalds 	 * We found the command, and cleared it out.  Either
24361da177e4SLinus Torvalds 	 * the command is still known to be executing on the
24371da177e4SLinus Torvalds 	 * target, or the busylun bit is not set.
24381da177e4SLinus Torvalds 	 */
24391da177e4SLinus Torvalds 	case res_success:
24401fa6b5fbSHannes Reinecke 		scmd_printk(KERN_WARNING, SCpnt, "abort %p success\n", SCpnt);
24411da177e4SLinus Torvalds 		result = SUCCESS;
24421da177e4SLinus Torvalds 		break;
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 	/*
24451da177e4SLinus Torvalds 	 * We need to reconnect to the target and send it an
24461da177e4SLinus Torvalds 	 * ABORT or ABORT_TAG message.  We can only do this
24471da177e4SLinus Torvalds 	 * if the bus is free.
24481da177e4SLinus Torvalds 	 */
24491da177e4SLinus Torvalds 	case res_hw_abort:
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	/*
24521da177e4SLinus Torvalds 	 * We are unable to abort the command for some reason.
24531da177e4SLinus Torvalds 	 */
24541da177e4SLinus Torvalds 	default:
24551da177e4SLinus Torvalds 	case res_failed:
24561fa6b5fbSHannes Reinecke 		scmd_printk(KERN_WARNING, SCpnt, "abort %p failed\n", SCpnt);
24571da177e4SLinus Torvalds 		break;
24581da177e4SLinus Torvalds 	}
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	return result;
24611da177e4SLinus Torvalds }
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds /**
24641da177e4SLinus Torvalds  * fas216_eh_device_reset - Reset the device associated with this command
24651da177e4SLinus Torvalds  * @SCpnt: command specifing device to reset
24661da177e4SLinus Torvalds  *
24671da177e4SLinus Torvalds  * Reset the device associated with this command.
24681da177e4SLinus Torvalds  * Returns: FAILED if unable to reset.
24691da177e4SLinus Torvalds  * Notes: We won't be re-entered, so we'll only have one device
24701da177e4SLinus Torvalds  * reset on the go at one time.
24711da177e4SLinus Torvalds  */
2472ee0ca6baSHenne int fas216_eh_device_reset(struct scsi_cmnd *SCpnt)
24731da177e4SLinus Torvalds {
24741da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
24751da177e4SLinus Torvalds 	unsigned long flags;
24761da177e4SLinus Torvalds 	int i, res = FAILED, target = SCpnt->device->id;
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "device reset for target %d", target);
24791da177e4SLinus Torvalds 
24801da177e4SLinus Torvalds 	spin_lock_irqsave(&info->host_lock, flags);
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	do {
24831da177e4SLinus Torvalds 		/*
24841da177e4SLinus Torvalds 		 * If we are currently connected to a device, and
24851da177e4SLinus Torvalds 		 * it is the device we want to reset, there is
24861da177e4SLinus Torvalds 		 * nothing we can do here.  Chances are it is stuck,
24871da177e4SLinus Torvalds 		 * and we need a bus reset.
24881da177e4SLinus Torvalds 		 */
24891da177e4SLinus Torvalds 		if (info->SCpnt && !info->scsi.disconnectable &&
24901da177e4SLinus Torvalds 		    info->SCpnt->device->id == SCpnt->device->id)
24911da177e4SLinus Torvalds 			break;
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 		/*
24941da177e4SLinus Torvalds 		 * We're going to be resetting this device.  Remove
24951da177e4SLinus Torvalds 		 * all pending commands from the driver.  By doing
24961da177e4SLinus Torvalds 		 * so, we guarantee that we won't touch the command
24971da177e4SLinus Torvalds 		 * structures except to process the reset request.
24981da177e4SLinus Torvalds 		 */
24991da177e4SLinus Torvalds 		queue_remove_all_target(&info->queues.issue, target);
25001da177e4SLinus Torvalds 		queue_remove_all_target(&info->queues.disconnected, target);
25011da177e4SLinus Torvalds 		if (info->origSCpnt && info->origSCpnt->device->id == target)
25021da177e4SLinus Torvalds 			info->origSCpnt = NULL;
25031da177e4SLinus Torvalds 		if (info->reqSCpnt && info->reqSCpnt->device->id == target)
25041da177e4SLinus Torvalds 			info->reqSCpnt = NULL;
25051da177e4SLinus Torvalds 		for (i = 0; i < 8; i++)
25061da177e4SLinus Torvalds 			clear_bit(target * 8 + i, info->busyluns);
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds 		/*
25091da177e4SLinus Torvalds 		 * Hijack this SCSI command structure to send
25101da177e4SLinus Torvalds 		 * a bus device reset message to this device.
25111da177e4SLinus Torvalds 		 */
25121da177e4SLinus Torvalds 		SCpnt->host_scribble = (void *)fas216_devicereset_done;
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds 		info->rst_dev_status = 0;
25151da177e4SLinus Torvalds 		info->rstSCpnt = SCpnt;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 		if (info->scsi.phase == PHASE_IDLE)
25181da177e4SLinus Torvalds 			fas216_kick(info);
25191da177e4SLinus Torvalds 
2520b857df1aSGuennadi Liakhovetski 		mod_timer(&info->eh_timer, jiffies + 30 * HZ);
25211da177e4SLinus Torvalds 		spin_unlock_irqrestore(&info->host_lock, flags);
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 		/*
25241da177e4SLinus Torvalds 		 * Wait up to 30 seconds for the reset to complete.
25251da177e4SLinus Torvalds 		 */
25261da177e4SLinus Torvalds 		wait_event(info->eh_wait, info->rst_dev_status);
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 		del_timer_sync(&info->eh_timer);
25291da177e4SLinus Torvalds 		spin_lock_irqsave(&info->host_lock, flags);
25301da177e4SLinus Torvalds 		info->rstSCpnt = NULL;
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 		if (info->rst_dev_status == 1)
25331da177e4SLinus Torvalds 			res = SUCCESS;
25341da177e4SLinus Torvalds 	} while (0);
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds 	SCpnt->host_scribble = NULL;
25371da177e4SLinus Torvalds 	spin_unlock_irqrestore(&info->host_lock, flags);
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "device reset complete: %s\n",
25401da177e4SLinus Torvalds 		   res == SUCCESS ? "success" : "failed");
25411da177e4SLinus Torvalds 
25421da177e4SLinus Torvalds 	return res;
25431da177e4SLinus Torvalds }
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds /**
25461da177e4SLinus Torvalds  * fas216_eh_bus_reset - Reset the bus associated with the command
25471da177e4SLinus Torvalds  * @SCpnt: command specifing bus to reset
25481da177e4SLinus Torvalds  *
25491da177e4SLinus Torvalds  * Reset the bus associated with the command.
25501da177e4SLinus Torvalds  * Returns: FAILED if unable to reset.
25511da177e4SLinus Torvalds  * Notes: Further commands are blocked.
25521da177e4SLinus Torvalds  */
2553ee0ca6baSHenne int fas216_eh_bus_reset(struct scsi_cmnd *SCpnt)
25541da177e4SLinus Torvalds {
25551da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
25561da177e4SLinus Torvalds 	unsigned long flags;
2557f64a181dSChristoph Hellwig 	struct scsi_device *SDpnt;
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	fas216_checkmagic(info);
25601da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "resetting bus");
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds 	info->stats.bus_resets += 1;
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 	spin_lock_irqsave(&info->host_lock, flags);
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	/*
25671da177e4SLinus Torvalds 	 * Stop all activity on this interface.
25681da177e4SLinus Torvalds 	 */
25691da177e4SLinus Torvalds 	fas216_aborttransfer(info);
25701da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 	/*
25731da177e4SLinus Torvalds 	 * Clear any pending interrupts.
25741da177e4SLinus Torvalds 	 */
25751da177e4SLinus Torvalds 	while (fas216_readb(info, REG_STAT) & STAT_INT)
25761da177e4SLinus Torvalds 		fas216_readb(info, REG_INST);
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds 	info->rst_bus_status = 0;
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	/*
25811da177e4SLinus Torvalds 	 * For each attached hard-reset device, clear out
25821da177e4SLinus Torvalds 	 * all command structures.  Leave the running
25831da177e4SLinus Torvalds 	 * command in place.
25841da177e4SLinus Torvalds 	 */
25851da177e4SLinus Torvalds 	shost_for_each_device(SDpnt, info->host) {
25861da177e4SLinus Torvalds 		int i;
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 		if (SDpnt->soft_reset)
25891da177e4SLinus Torvalds 			continue;
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 		queue_remove_all_target(&info->queues.issue, SDpnt->id);
25921da177e4SLinus Torvalds 		queue_remove_all_target(&info->queues.disconnected, SDpnt->id);
25931da177e4SLinus Torvalds 		if (info->origSCpnt && info->origSCpnt->device->id == SDpnt->id)
25941da177e4SLinus Torvalds 			info->origSCpnt = NULL;
25951da177e4SLinus Torvalds 		if (info->reqSCpnt && info->reqSCpnt->device->id == SDpnt->id)
25961da177e4SLinus Torvalds 			info->reqSCpnt = NULL;
25971da177e4SLinus Torvalds 		info->SCpnt = NULL;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 		for (i = 0; i < 8; i++)
26001da177e4SLinus Torvalds 			clear_bit(SDpnt->id * 8 + i, info->busyluns);
26011da177e4SLinus Torvalds 	}
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 	info->scsi.phase = PHASE_IDLE;
26041da177e4SLinus Torvalds 
26051da177e4SLinus Torvalds 	/*
26061da177e4SLinus Torvalds 	 * Reset the SCSI bus.  Device cleanup happens in
26071da177e4SLinus Torvalds 	 * the interrupt handler.
26081da177e4SLinus Torvalds 	 */
26091da177e4SLinus Torvalds 	fas216_cmd(info, CMD_RESETSCSI);
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds 	mod_timer(&info->eh_timer, jiffies + HZ);
26121da177e4SLinus Torvalds 	spin_unlock_irqrestore(&info->host_lock, flags);
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	/*
26151da177e4SLinus Torvalds 	 * Wait one second for the interrupt.
26161da177e4SLinus Torvalds 	 */
26171da177e4SLinus Torvalds 	wait_event(info->eh_wait, info->rst_bus_status);
26181da177e4SLinus Torvalds 	del_timer_sync(&info->eh_timer);
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "bus reset complete: %s\n",
26211da177e4SLinus Torvalds 		   info->rst_bus_status == 1 ? "success" : "failed");
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds 	return info->rst_bus_status == 1 ? SUCCESS : FAILED;
26241da177e4SLinus Torvalds }
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds /**
26271da177e4SLinus Torvalds  * fas216_init_chip - Initialise FAS216 state after reset
26281da177e4SLinus Torvalds  * @info: state structure for interface
26291da177e4SLinus Torvalds  *
26301da177e4SLinus Torvalds  * Initialise FAS216 state after reset
26311da177e4SLinus Torvalds  */
26321da177e4SLinus Torvalds static void fas216_init_chip(FAS216_Info *info)
26331da177e4SLinus Torvalds {
26341da177e4SLinus Torvalds 	unsigned int clock = ((info->ifcfg.clockrate - 1) / 5 + 1) & 7;
26351da177e4SLinus Torvalds 	fas216_writeb(info, REG_CLKF, clock);
26361da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
26371da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL2, info->scsi.cfg[1]);
26381da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
26391da177e4SLinus Torvalds 	fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
26401da177e4SLinus Torvalds 	fas216_writeb(info, REG_SOF, 0);
26411da177e4SLinus Torvalds 	fas216_writeb(info, REG_STP, info->scsi.async_stp);
26421da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
26431da177e4SLinus Torvalds }
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds /**
26461da177e4SLinus Torvalds  * fas216_eh_host_reset - Reset the host associated with this command
26471da177e4SLinus Torvalds  * @SCpnt: command specifing host to reset
26481da177e4SLinus Torvalds  *
26491da177e4SLinus Torvalds  * Reset the host associated with this command.
26501da177e4SLinus Torvalds  * Returns: FAILED if unable to reset.
26511da177e4SLinus Torvalds  * Notes: io_request_lock is taken, and irqs are disabled
26521da177e4SLinus Torvalds  */
2653ee0ca6baSHenne int fas216_eh_host_reset(struct scsi_cmnd *SCpnt)
26541da177e4SLinus Torvalds {
26551da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
26561da177e4SLinus Torvalds 
2657df0ae249SJeff Garzik  	spin_lock_irq(info->host->host_lock);
2658df0ae249SJeff Garzik  
26591da177e4SLinus Torvalds 	fas216_checkmagic(info);
26601da177e4SLinus Torvalds 
2661e04ca907SHannes Reinecke 	fas216_log(info, LOG_ERROR, "resetting host");
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	/*
26641da177e4SLinus Torvalds 	 * Reset the SCSI chip.
26651da177e4SLinus Torvalds 	 */
26661da177e4SLinus Torvalds 	fas216_cmd(info, CMD_RESETCHIP);
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds 	/*
26691da177e4SLinus Torvalds 	 * Ugly ugly ugly!
26701da177e4SLinus Torvalds 	 * We need to release the host_lock and enable
26711da177e4SLinus Torvalds 	 * IRQs if we sleep, but we must relock and disable
26721da177e4SLinus Torvalds 	 * IRQs after the sleep.
26731da177e4SLinus Torvalds 	 */
26741da177e4SLinus Torvalds 	spin_unlock_irq(info->host->host_lock);
26751da177e4SLinus Torvalds 	msleep(50 * 1000/100);
26761da177e4SLinus Torvalds 	spin_lock_irq(info->host->host_lock);
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds 	/*
26791da177e4SLinus Torvalds 	 * Release the SCSI reset.
26801da177e4SLinus Torvalds 	 */
26811da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP);
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds 	fas216_init_chip(info);
26841da177e4SLinus Torvalds 
2685df0ae249SJeff Garzik  	spin_unlock_irq(info->host->host_lock);
26861da177e4SLinus Torvalds 	return SUCCESS;
26871da177e4SLinus Torvalds }
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds #define TYPE_UNKNOWN	0
26901da177e4SLinus Torvalds #define TYPE_NCR53C90	1
26911da177e4SLinus Torvalds #define TYPE_NCR53C90A	2
26921da177e4SLinus Torvalds #define TYPE_NCR53C9x	3
26931da177e4SLinus Torvalds #define TYPE_Am53CF94	4
26941da177e4SLinus Torvalds #define TYPE_EmFAS216	5
26951da177e4SLinus Torvalds #define TYPE_QLFAS216	6
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds static char *chip_types[] = {
26981da177e4SLinus Torvalds 	"unknown",
26991da177e4SLinus Torvalds 	"NS NCR53C90",
27001da177e4SLinus Torvalds 	"NS NCR53C90A",
27011da177e4SLinus Torvalds 	"NS NCR53C9x",
27021da177e4SLinus Torvalds 	"AMD Am53CF94",
27031da177e4SLinus Torvalds 	"Emulex FAS216",
27041da177e4SLinus Torvalds 	"QLogic FAS216"
27051da177e4SLinus Torvalds };
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds static int fas216_detect_type(FAS216_Info *info)
27081da177e4SLinus Torvalds {
27091da177e4SLinus Torvalds 	int family, rev;
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds 	/*
27121da177e4SLinus Torvalds 	 * Reset the chip.
27131da177e4SLinus Torvalds 	 */
27141da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
27151da177e4SLinus Torvalds 	udelay(50);
27161da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_NOP);
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds 	/*
27191da177e4SLinus Torvalds 	 * Check to see if control reg 2 is present.
27201da177e4SLinus Torvalds 	 */
27211da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 0);
27221da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL2, CNTL2_S2FE);
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds 	/*
27251da177e4SLinus Torvalds 	 * If we are unable to read back control reg 2
27261da177e4SLinus Torvalds 	 * correctly, it is not present, and we have a
27271da177e4SLinus Torvalds 	 * NCR53C90.
27281da177e4SLinus Torvalds 	 */
27291da177e4SLinus Torvalds 	if ((fas216_readb(info, REG_CNTL2) & (~0xe0)) != CNTL2_S2FE)
27301da177e4SLinus Torvalds 		return TYPE_NCR53C90;
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds 	/*
27331da177e4SLinus Torvalds 	 * Now, check control register 3
27341da177e4SLinus Torvalds 	 */
27351da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL2, 0);
27361da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 0);
27371da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 5);
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds 	/*
27401da177e4SLinus Torvalds 	 * If we are unable to read the register back
27411da177e4SLinus Torvalds 	 * correctly, we have a NCR53C90A
27421da177e4SLinus Torvalds 	 */
27431da177e4SLinus Torvalds 	if (fas216_readb(info, REG_CNTL3) != 5)
27441da177e4SLinus Torvalds 		return TYPE_NCR53C90A;
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 	/*
27471da177e4SLinus Torvalds 	 * Now read the ID from the chip.
27481da177e4SLinus Torvalds 	 */
27491da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 0);
27501da177e4SLinus Torvalds 
27511da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, CNTL3_ADIDCHK);
27521da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 0);
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
27551da177e4SLinus Torvalds 	udelay(50);
27561da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_WITHDMA | CMD_NOP);
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL2, CNTL2_ENF);
27591da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
27601da177e4SLinus Torvalds 	udelay(50);
27611da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_NOP);
27621da177e4SLinus Torvalds 
27631da177e4SLinus Torvalds 	rev     = fas216_readb(info, REG_ID);
27641da177e4SLinus Torvalds 	family  = rev >> 3;
27651da177e4SLinus Torvalds 	rev    &= 7;
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds 	switch (family) {
27681da177e4SLinus Torvalds 	case 0x01:
27691da177e4SLinus Torvalds 		if (rev == 4)
27701da177e4SLinus Torvalds 			return TYPE_Am53CF94;
27711da177e4SLinus Torvalds 		break;
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds 	case 0x02:
27741da177e4SLinus Torvalds 		switch (rev) {
27751da177e4SLinus Torvalds 		case 2:
27761da177e4SLinus Torvalds 			return TYPE_EmFAS216;
27771da177e4SLinus Torvalds 		case 3:
27781da177e4SLinus Torvalds 			return TYPE_QLFAS216;
27791da177e4SLinus Torvalds 		}
27801da177e4SLinus Torvalds 		break;
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds 	default:
27831da177e4SLinus Torvalds 		break;
27841da177e4SLinus Torvalds 	}
27851da177e4SLinus Torvalds 	printk("family %x rev %x\n", family, rev);
27861da177e4SLinus Torvalds 	return TYPE_NCR53C9x;
27871da177e4SLinus Torvalds }
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds /**
27901da177e4SLinus Torvalds  * fas216_reset_state - Initialise driver internal state
27911da177e4SLinus Torvalds  * @info: state to initialise
27921da177e4SLinus Torvalds  *
27931da177e4SLinus Torvalds  * Initialise driver internal state
27941da177e4SLinus Torvalds  */
27951da177e4SLinus Torvalds static void fas216_reset_state(FAS216_Info *info)
27961da177e4SLinus Torvalds {
27971da177e4SLinus Torvalds 	int i;
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds 	fas216_checkmagic(info);
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds 	fas216_bus_reset(info);
28021da177e4SLinus Torvalds 
28031da177e4SLinus Torvalds 	/*
28041da177e4SLinus Torvalds 	 * Clear out all stale info in our state structure
28051da177e4SLinus Torvalds 	 */
28061da177e4SLinus Torvalds 	memset(info->busyluns, 0, sizeof(info->busyluns));
28071da177e4SLinus Torvalds 	info->scsi.disconnectable = 0;
28081da177e4SLinus Torvalds 	info->scsi.aborting = 0;
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
28111da177e4SLinus Torvalds 		info->device[i].parity_enabled	= 0;
28121da177e4SLinus Torvalds 		info->device[i].parity_check	= 1;
28131da177e4SLinus Torvalds 	}
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds 	/*
28161da177e4SLinus Torvalds 	 * Drain all commands on disconnected queue
28171da177e4SLinus Torvalds 	 */
28181da177e4SLinus Torvalds 	while (queue_remove(&info->queues.disconnected) != NULL);
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds 	/*
28211da177e4SLinus Torvalds 	 * Remove executing commands.
28221da177e4SLinus Torvalds 	 */
28231da177e4SLinus Torvalds 	info->SCpnt     = NULL;
28241da177e4SLinus Torvalds 	info->reqSCpnt  = NULL;
28251da177e4SLinus Torvalds 	info->rstSCpnt  = NULL;
28261da177e4SLinus Torvalds 	info->origSCpnt = NULL;
28271da177e4SLinus Torvalds }
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds /**
28301da177e4SLinus Torvalds  * fas216_init - initialise FAS/NCR/AMD SCSI structures.
28311da177e4SLinus Torvalds  * @host: a driver-specific filled-out structure
28321da177e4SLinus Torvalds  *
28331da177e4SLinus Torvalds  * Initialise FAS/NCR/AMD SCSI structures.
28341da177e4SLinus Torvalds  * Returns: 0 on success
28351da177e4SLinus Torvalds  */
28361da177e4SLinus Torvalds int fas216_init(struct Scsi_Host *host)
28371da177e4SLinus Torvalds {
28381da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)host->hostdata;
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds 	info->magic_start    = MAGIC;
28411da177e4SLinus Torvalds 	info->magic_end      = MAGIC;
28421da177e4SLinus Torvalds 	info->host           = host;
28431da177e4SLinus Torvalds 	info->scsi.cfg[0]    = host->this_id | CNTL1_PERE;
28441da177e4SLinus Torvalds 	info->scsi.cfg[1]    = CNTL2_ENF | CNTL2_S2FE;
28451da177e4SLinus Torvalds 	info->scsi.cfg[2]    = info->ifcfg.cntl3 |
28461da177e4SLinus Torvalds 			       CNTL3_ADIDCHK | CNTL3_QTAG | CNTL3_G2CB | CNTL3_LBTM;
28471da177e4SLinus Torvalds 	info->scsi.async_stp = fas216_syncperiod(info, info->ifcfg.asyncperiod);
28481da177e4SLinus Torvalds 
28491da177e4SLinus Torvalds 	info->rst_dev_status = -1;
28501da177e4SLinus Torvalds 	info->rst_bus_status = -1;
28511da177e4SLinus Torvalds 	init_waitqueue_head(&info->eh_wait);
28521da177e4SLinus Torvalds 	init_timer(&info->eh_timer);
28531da177e4SLinus Torvalds 	info->eh_timer.data  = (unsigned long)info;
28541da177e4SLinus Torvalds 	info->eh_timer.function = fas216_eh_timer;
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds 	spin_lock_init(&info->host_lock);
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds 	memset(&info->stats, 0, sizeof(info->stats));
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 	msgqueue_initialise(&info->scsi.msgs);
28611da177e4SLinus Torvalds 
28621da177e4SLinus Torvalds 	if (!queue_initialise(&info->queues.issue))
28631da177e4SLinus Torvalds 		return -ENOMEM;
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds 	if (!queue_initialise(&info->queues.disconnected)) {
28661da177e4SLinus Torvalds 		queue_free(&info->queues.issue);
28671da177e4SLinus Torvalds 		return -ENOMEM;
28681da177e4SLinus Torvalds 	}
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 	return 0;
28711da177e4SLinus Torvalds }
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds /**
28741da177e4SLinus Torvalds  * fas216_add - initialise FAS/NCR/AMD SCSI ic.
28751da177e4SLinus Torvalds  * @host: a driver-specific filled-out structure
28761da177e4SLinus Torvalds  * @dev: parent device
28771da177e4SLinus Torvalds  *
28781da177e4SLinus Torvalds  * Initialise FAS/NCR/AMD SCSI ic.
28791da177e4SLinus Torvalds  * Returns: 0 on success
28801da177e4SLinus Torvalds  */
28811da177e4SLinus Torvalds int fas216_add(struct Scsi_Host *host, struct device *dev)
28821da177e4SLinus Torvalds {
28831da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)host->hostdata;
28841da177e4SLinus Torvalds 	int type, ret;
28851da177e4SLinus Torvalds 
28861da177e4SLinus Torvalds 	if (info->ifcfg.clockrate <= 10 || info->ifcfg.clockrate > 40) {
28871da177e4SLinus Torvalds 		printk(KERN_CRIT "fas216: invalid clock rate %u MHz\n",
28881da177e4SLinus Torvalds 			info->ifcfg.clockrate);
28891da177e4SLinus Torvalds 		return -EINVAL;
28901da177e4SLinus Torvalds 	}
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds 	fas216_reset_state(info);
28931da177e4SLinus Torvalds 	type = fas216_detect_type(info);
28941da177e4SLinus Torvalds 	info->scsi.type = chip_types[type];
28951da177e4SLinus Torvalds 
28961da177e4SLinus Torvalds 	udelay(300);
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds 	/*
28991da177e4SLinus Torvalds 	 * Initialise the chip correctly.
29001da177e4SLinus Torvalds 	 */
29011da177e4SLinus Torvalds 	fas216_init_chip(info);
29021da177e4SLinus Torvalds 
29031da177e4SLinus Torvalds 	/*
29041da177e4SLinus Torvalds 	 * Reset the SCSI bus.  We don't want to see
29051da177e4SLinus Torvalds 	 * the resulting reset interrupt, so mask it
29061da177e4SLinus Torvalds 	 * out.
29071da177e4SLinus Torvalds 	 */
29081da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_DISR);
29091da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETSCSI);
29101da177e4SLinus Torvalds 
29111da177e4SLinus Torvalds 	/*
29121da177e4SLinus Torvalds 	 * scsi standard says wait 250ms
29131da177e4SLinus Torvalds 	 */
29141da177e4SLinus Torvalds 	spin_unlock_irq(info->host->host_lock);
29151da177e4SLinus Torvalds 	msleep(100*1000/100);
29161da177e4SLinus Torvalds 	spin_lock_irq(info->host->host_lock);
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
29191da177e4SLinus Torvalds 	fas216_readb(info, REG_INST);
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds 	fas216_checkmagic(info);
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 	ret = scsi_add_host(host, dev);
29241da177e4SLinus Torvalds 	if (ret)
29251da177e4SLinus Torvalds 		fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
29261da177e4SLinus Torvalds 	else
29271da177e4SLinus Torvalds 		scsi_scan_host(host);
29281da177e4SLinus Torvalds 
29291da177e4SLinus Torvalds 	return ret;
29301da177e4SLinus Torvalds }
29311da177e4SLinus Torvalds 
29321da177e4SLinus Torvalds void fas216_remove(struct Scsi_Host *host)
29331da177e4SLinus Torvalds {
29341da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)host->hostdata;
29351da177e4SLinus Torvalds 
29361da177e4SLinus Torvalds 	fas216_checkmagic(info);
29371da177e4SLinus Torvalds 	scsi_remove_host(host);
29381da177e4SLinus Torvalds 
29391da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
29401da177e4SLinus Torvalds 	scsi_host_put(host);
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds /**
29441da177e4SLinus Torvalds  * fas216_release - release all resources for FAS/NCR/AMD SCSI ic.
29451da177e4SLinus Torvalds  * @host: a driver-specific filled-out structure
29461da177e4SLinus Torvalds  *
29471da177e4SLinus Torvalds  * release all resources and put everything to bed for FAS/NCR/AMD SCSI ic.
29481da177e4SLinus Torvalds  */
29491da177e4SLinus Torvalds void fas216_release(struct Scsi_Host *host)
29501da177e4SLinus Torvalds {
29511da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)host->hostdata;
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds 	queue_free(&info->queues.disconnected);
29541da177e4SLinus Torvalds 	queue_free(&info->queues.issue);
29551da177e4SLinus Torvalds }
29561da177e4SLinus Torvalds 
29579d4e5c54SAl Viro void fas216_print_host(FAS216_Info *info, struct seq_file *m)
29581da177e4SLinus Torvalds {
29599d4e5c54SAl Viro 	seq_printf(m,
29601da177e4SLinus Torvalds 			"\n"
29611da177e4SLinus Torvalds 			"Chip    : %s\n"
29621da177e4SLinus Torvalds 			" Address: 0x%p\n"
29631da177e4SLinus Torvalds 			" IRQ    : %d\n"
29641da177e4SLinus Torvalds 			" DMA    : %d\n",
29651da177e4SLinus Torvalds 			info->scsi.type, info->scsi.io_base,
29661da177e4SLinus Torvalds 			info->scsi.irq, info->scsi.dma);
29671da177e4SLinus Torvalds }
29681da177e4SLinus Torvalds 
29699d4e5c54SAl Viro void fas216_print_stats(FAS216_Info *info, struct seq_file *m)
29701da177e4SLinus Torvalds {
29719d4e5c54SAl Viro 	seq_printf(m, "\n"
29721da177e4SLinus Torvalds 			"Command Statistics:\n"
29731da177e4SLinus Torvalds 			" Queued     : %u\n"
29741da177e4SLinus Torvalds 			" Issued     : %u\n"
29751da177e4SLinus Torvalds 			" Completed  : %u\n"
29761da177e4SLinus Torvalds 			" Reads      : %u\n"
29771da177e4SLinus Torvalds 			" Writes     : %u\n"
29781da177e4SLinus Torvalds 			" Others     : %u\n"
29791da177e4SLinus Torvalds 			" Disconnects: %u\n"
29801da177e4SLinus Torvalds 			" Aborts     : %u\n"
29811da177e4SLinus Torvalds 			" Bus resets : %u\n"
29821da177e4SLinus Torvalds 			" Host resets: %u\n",
29831da177e4SLinus Torvalds 			info->stats.queues,	 info->stats.removes,
29841da177e4SLinus Torvalds 			info->stats.fins,	 info->stats.reads,
29851da177e4SLinus Torvalds 			info->stats.writes,	 info->stats.miscs,
29861da177e4SLinus Torvalds 			info->stats.disconnects, info->stats.aborts,
29871da177e4SLinus Torvalds 			info->stats.bus_resets,	 info->stats.host_resets);
29881da177e4SLinus Torvalds }
29891da177e4SLinus Torvalds 
29909d4e5c54SAl Viro void fas216_print_devices(FAS216_Info *info, struct seq_file *m)
29911da177e4SLinus Torvalds {
29921da177e4SLinus Torvalds 	struct fas216_device *dev;
2993f64a181dSChristoph Hellwig 	struct scsi_device *scd;
29941da177e4SLinus Torvalds 
299591c40f24SRasmus Villemoes 	seq_puts(m, "Device/Lun TaggedQ       Parity   Sync\n");
29961da177e4SLinus Torvalds 
29971da177e4SLinus Torvalds 	shost_for_each_device(scd, info->host) {
29981da177e4SLinus Torvalds 		dev = &info->device[scd->id];
29999cb78c16SHannes Reinecke 		seq_printf(m, "     %d/%llu   ", scd->id, scd->lun);
30001da177e4SLinus Torvalds 		if (scd->tagged_supported)
30019d4e5c54SAl Viro 			seq_printf(m, "%3sabled(%3d) ",
30021da177e4SLinus Torvalds 				     scd->simple_tags ? "en" : "dis",
30031da177e4SLinus Torvalds 				     scd->current_tag);
30041da177e4SLinus Torvalds 		else
300591c40f24SRasmus Villemoes 			seq_puts(m, "unsupported   ");
30061da177e4SLinus Torvalds 
30079d4e5c54SAl Viro 		seq_printf(m, "%3sabled ", dev->parity_enabled ? "en" : "dis");
30081da177e4SLinus Torvalds 
30091da177e4SLinus Torvalds 		if (dev->sof)
30109d4e5c54SAl Viro 			seq_printf(m, "offset %d, %d ns\n",
30111da177e4SLinus Torvalds 				     dev->sof, dev->period * 4);
30121da177e4SLinus Torvalds 		else
301391c40f24SRasmus Villemoes 			seq_puts(m, "async\n");
30141da177e4SLinus Torvalds 	}
30151da177e4SLinus Torvalds }
30161da177e4SLinus Torvalds 
30171da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_init);
30181da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_add);
30191da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_queue_command);
30201da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_noqueue_command);
30211da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_intr);
30221da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_remove);
30231da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_release);
30241da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_eh_abort);
30251da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_eh_device_reset);
30261da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_eh_bus_reset);
30271da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_eh_host_reset);
30281da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_print_host);
30291da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_print_stats);
30301da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_print_devices);
30311da177e4SLinus Torvalds 
30321da177e4SLinus Torvalds MODULE_AUTHOR("Russell King");
30331da177e4SLinus Torvalds MODULE_DESCRIPTION("Generic FAS216/NCR53C9x driver core");
30341da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3035