xref: /openbmc/linux/drivers/scsi/arm/fas216.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/drivers/acorn/scsi/fas216.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1997-2003 Russell King
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
81da177e4SLinus Torvalds  * other sources, including:
91da177e4SLinus Torvalds  *   the AMD Am53CF94 data sheet
101da177e4SLinus Torvalds  *   the AMD Am53C94 data sheet
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * This is a generic driver.  To use it, have a look at cumana_2.c.  You
131da177e4SLinus Torvalds  * should define your own structure that overlays FAS216_Info, eg:
141da177e4SLinus Torvalds  * struct my_host_data {
151da177e4SLinus Torvalds  *    FAS216_Info info;
161da177e4SLinus Torvalds  *    ... my host specific data ...
171da177e4SLinus Torvalds  * };
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Changelog:
201da177e4SLinus Torvalds  *  30-08-1997	RMK	Created
211da177e4SLinus Torvalds  *  14-09-1997	RMK	Started disconnect support
221da177e4SLinus Torvalds  *  08-02-1998	RMK	Corrected real DMA support
231da177e4SLinus Torvalds  *  15-02-1998	RMK	Started sync xfer support
241da177e4SLinus Torvalds  *  06-04-1998	RMK	Tightened conditions for printing incomplete
251da177e4SLinus Torvalds  *			transfers
261da177e4SLinus Torvalds  *  02-05-1998	RMK	Added extra checks in fas216_reset
271da177e4SLinus Torvalds  *  24-05-1998	RMK	Fixed synchronous transfers with period >= 200ns
281da177e4SLinus Torvalds  *  27-06-1998	RMK	Changed asm/delay.h to linux/delay.h
291da177e4SLinus Torvalds  *  26-08-1998	RMK	Improved message support wrt MESSAGE_REJECT
301da177e4SLinus Torvalds  *  02-04-2000	RMK	Converted to use the new error handling, and
311da177e4SLinus Torvalds  *			automatically request sense data upon check
321da177e4SLinus Torvalds  *			condition status from targets.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds #include <linux/module.h>
351da177e4SLinus Torvalds #include <linux/blkdev.h>
361da177e4SLinus Torvalds #include <linux/kernel.h>
371da177e4SLinus Torvalds #include <linux/string.h>
381da177e4SLinus Torvalds #include <linux/ioport.h>
391da177e4SLinus Torvalds #include <linux/proc_fs.h>
401da177e4SLinus Torvalds #include <linux/delay.h>
411da177e4SLinus Torvalds #include <linux/bitops.h>
421da177e4SLinus Torvalds #include <linux/init.h>
431da177e4SLinus Torvalds #include <linux/interrupt.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <asm/dma.h>
461da177e4SLinus Torvalds #include <asm/io.h>
471da177e4SLinus Torvalds #include <asm/irq.h>
481da177e4SLinus Torvalds #include <asm/ecard.h>
491da177e4SLinus Torvalds 
5053555fb7SBart Van Assche #include <scsi/scsi.h>
5153555fb7SBart Van Assche #include <scsi/scsi_cmnd.h>
52db9dff36S #include <scsi/scsi_dbg.h>
5353555fb7SBart Van Assche #include <scsi/scsi_device.h>
5453555fb7SBart Van Assche #include <scsi/scsi_eh.h>
551da177e4SLinus Torvalds #include <scsi/scsi_host.h>
5653555fb7SBart Van Assche #include <scsi/scsi_tcq.h>
571da177e4SLinus Torvalds #include "fas216.h"
588c97e2f3SBart Van Assche #include "arm_scsi.h"
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /* NOTE: SCSI2 Synchronous transfers *require* DMA according to
611da177e4SLinus Torvalds  *  the data sheet.  This restriction is crazy, especially when
621da177e4SLinus Torvalds  *  you only want to send 16 bytes!  What were the guys who
631da177e4SLinus Torvalds  *  designed this chip on at that time?  Did they read the SCSI2
641da177e4SLinus Torvalds  *  spec at all?  The following sections are taken from the SCSI2
651da177e4SLinus Torvalds  *  standard (s2r10) concerning this:
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  * > IMPLEMENTORS NOTES:
681da177e4SLinus Torvalds  * >   (1)  Re-negotiation at every selection is not recommended, since a
691da177e4SLinus Torvalds  * >   significant performance impact is likely.
701da177e4SLinus Torvalds  *
711da177e4SLinus Torvalds  * >  The implied synchronous agreement shall remain in effect until a BUS DEVICE
721da177e4SLinus Torvalds  * >  RESET message is received, until a hard reset condition occurs, or until one
731da177e4SLinus Torvalds  * >  of the two SCSI devices elects to modify the agreement.  The default data
741da177e4SLinus Torvalds  * >  transfer mode is asynchronous data transfer mode.  The default data transfer
751da177e4SLinus Torvalds  * >  mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
761da177e4SLinus Torvalds  * >  reset condition.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  *  In total, this means that once you have elected to use synchronous
791da177e4SLinus Torvalds  *  transfers, you must always use DMA.
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  *  I was thinking that this was a good chip until I found this restriction ;(
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds #define SCSI2_SYNC
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 
101dc0d79b5SArnd Bergmann #ifndef MODULE
fas216_log_setup(char * str)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);
142dc0d79b5SArnd Bergmann #endif
1431da177e4SLinus Torvalds 
fas216_readb(FAS216_Info * info,unsigned int reg)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 
fas216_writeb(FAS216_Info * info,unsigned int reg,unsigned int val)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 
fas216_dumpstate(FAS216_Info * info)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 
print_SCp(struct scsi_pointer * SCp,const char * prefix,const char * suffix)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
fas216_dumpinfo(FAS216_Info * info)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 
__fas216_checkmagic(FAS216_Info * info,const char * func)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 
fas216_bus_phase(int stat)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 
fas216_drv_phase(FAS216_Info * info)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 
fas216_target(FAS216_Info * info)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
fas216_do_log(FAS216_Info * info,char target,char * fmt,va_list ap)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 
fas216_log_command(FAS216_Info * info,int level,struct scsi_cmnd * SCpnt,char * fmt,...)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
fas216_log_target(FAS216_Info * info,int level,int target,char * fmt,...)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 
fas216_log(FAS216_Info * info,int level,char * fmt,...)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 
add_debug_list(int stat,int ssr,int isr,int ph)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 
fas216_cmd(FAS216_Info * info,unsigned int command)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 
print_debug_list(void)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
fas216_get_last_msg(FAS216_Info * info,int pos)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  */
fas216_syncperiod(FAS216_Info * info,int ns)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  */
fas216_set_sync(FAS216_Info * info,int target)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  */
fas216_handlesync(FAS216_Info * info,char * msg)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;
609df561f66SGustavo A. R. Silva 		fallthrough;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	case async:
6121da177e4SLinus Torvalds 		dev->period = info->ifcfg.asyncperiod / 4;
6131da177e4SLinus Torvalds 		dev->sof    = 0;
6141da177e4SLinus Torvalds 		dev->stp    = info->scsi.async_stp;
6151da177e4SLinus Torvalds 		fas216_set_sync(info, info->SCpnt->device->id);
6161da177e4SLinus Torvalds 		break;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	case none:
6191da177e4SLinus Torvalds 		break;
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds /**
6241da177e4SLinus Torvalds  * fas216_updateptrs - update data pointers after transfer suspended/paused
6251da177e4SLinus Torvalds  * @info: interface's local pointer to update
6261da177e4SLinus Torvalds  * @bytes_transferred: number of bytes transferred
6271da177e4SLinus Torvalds  *
6281da177e4SLinus Torvalds  * Update data pointers after transfer suspended/paused
6291da177e4SLinus Torvalds  */
fas216_updateptrs(FAS216_Info * info,int bytes_transferred)6301da177e4SLinus Torvalds static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
6311da177e4SLinus Torvalds {
6320a04137eSChristoph Hellwig 	struct scsi_pointer *SCp = &info->scsi.SCp;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	fas216_checkmagic(info);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	BUG_ON(bytes_transferred < 0);
6371da177e4SLinus Torvalds 
6386b4df7eeSRussell King 	SCp->phase -= bytes_transferred;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	while (bytes_transferred != 0) {
6411da177e4SLinus Torvalds 		if (SCp->this_residual > bytes_transferred)
6421da177e4SLinus Torvalds 			break;
6431da177e4SLinus Torvalds 		/*
6441da177e4SLinus Torvalds 		 * We have used up this buffer.  Move on to the
6451da177e4SLinus Torvalds 		 * next buffer.
6461da177e4SLinus Torvalds 		 */
6471da177e4SLinus Torvalds 		bytes_transferred -= SCp->this_residual;
6481da177e4SLinus Torvalds 		if (!next_SCp(SCp) && bytes_transferred) {
6491da177e4SLinus Torvalds 			printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
6501da177e4SLinus Torvalds 				info->host->host_no, '0' + info->SCpnt->device->id);
6511da177e4SLinus Torvalds 			return;
6521da177e4SLinus Torvalds 		}
6531da177e4SLinus Torvalds 	}
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	SCp->this_residual -= bytes_transferred;
6561da177e4SLinus Torvalds 	if (SCp->this_residual)
6571da177e4SLinus Torvalds 		SCp->ptr += bytes_transferred;
6581da177e4SLinus Torvalds 	else
6591da177e4SLinus Torvalds 		SCp->ptr = NULL;
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds /**
6631da177e4SLinus Torvalds  * fas216_pio - transfer data off of/on to card using programmed IO
6641da177e4SLinus Torvalds  * @info: interface to transfer data to/from
6651da177e4SLinus Torvalds  * @direction: direction to transfer data (DMA_OUT/DMA_IN)
6661da177e4SLinus Torvalds  *
6671da177e4SLinus Torvalds  * Transfer data off of/on to card using programmed IO.
6681da177e4SLinus Torvalds  * Notes: this is incredibly slow.
6691da177e4SLinus Torvalds  */
fas216_pio(FAS216_Info * info,fasdmadir_t direction)6701da177e4SLinus Torvalds static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
6711da177e4SLinus Torvalds {
6720a04137eSChristoph Hellwig 	struct scsi_pointer *SCp = &info->scsi.SCp;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	fas216_checkmagic(info);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	if (direction == DMA_OUT)
6771da177e4SLinus Torvalds 		fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
6781da177e4SLinus Torvalds 	else
6791da177e4SLinus Torvalds 		put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	if (SCp->this_residual == 0)
6821da177e4SLinus Torvalds 		next_SCp(SCp);
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
fas216_set_stc(FAS216_Info * info,unsigned int length)6851da177e4SLinus Torvalds static void fas216_set_stc(FAS216_Info *info, unsigned int length)
6861da177e4SLinus Torvalds {
6871da177e4SLinus Torvalds 	fas216_writeb(info, REG_STCL, length);
6881da177e4SLinus Torvalds 	fas216_writeb(info, REG_STCM, length >> 8);
6891da177e4SLinus Torvalds 	fas216_writeb(info, REG_STCH, length >> 16);
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
fas216_get_ctc(FAS216_Info * info)6921da177e4SLinus Torvalds static unsigned int fas216_get_ctc(FAS216_Info *info)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	return fas216_readb(info, REG_CTCL) +
6951da177e4SLinus Torvalds 	       (fas216_readb(info, REG_CTCM) << 8) +
6961da177e4SLinus Torvalds 	       (fas216_readb(info, REG_CTCH) << 16);
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds /**
7001da177e4SLinus Torvalds  * fas216_cleanuptransfer - clean up after a transfer has completed.
7011da177e4SLinus Torvalds  * @info: interface to clean up
7021da177e4SLinus Torvalds  *
7031da177e4SLinus Torvalds  * Update the data pointers according to the number of bytes transferred
7041da177e4SLinus Torvalds  * on the SCSI bus.
7051da177e4SLinus Torvalds  */
fas216_cleanuptransfer(FAS216_Info * info)7061da177e4SLinus Torvalds static void fas216_cleanuptransfer(FAS216_Info *info)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	unsigned long total, residual, fifo;
7091da177e4SLinus Torvalds 	fasdmatype_t dmatype = info->dma.transfer_type;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	info->dma.transfer_type = fasdma_none;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	/*
7141da177e4SLinus Torvalds 	 * PIO transfers do not need to be cleaned up.
7151da177e4SLinus Torvalds 	 */
7161da177e4SLinus Torvalds 	if (dmatype == fasdma_pio || dmatype == fasdma_none)
7171da177e4SLinus Torvalds 		return;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	if (dmatype == fasdma_real_all)
7206b4df7eeSRussell King 		total = info->scsi.SCp.phase;
7211da177e4SLinus Torvalds 	else
7221da177e4SLinus Torvalds 		total = info->scsi.SCp.this_residual;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	residual = fas216_get_ctc(info);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	fas216_log(info, LOG_BUFFER, "cleaning up from previous "
7291da177e4SLinus Torvalds 		   "transfer: length 0x%06x, residual 0x%x, fifo %d",
7301da177e4SLinus Torvalds 		   total, residual, fifo);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	/*
7331da177e4SLinus Torvalds 	 * If we were performing Data-Out, the transfer counter
7341da177e4SLinus Torvalds 	 * counts down each time a byte is transferred by the
7351da177e4SLinus Torvalds 	 * host to the FIFO.  This means we must include the
7361da177e4SLinus Torvalds 	 * bytes left in the FIFO from the transfer counter.
7371da177e4SLinus Torvalds 	 */
7381da177e4SLinus Torvalds 	if (info->scsi.phase == PHASE_DATAOUT)
7391da177e4SLinus Torvalds 		residual += fifo;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	fas216_updateptrs(info, total - residual);
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds /**
7451da177e4SLinus Torvalds  * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
7461da177e4SLinus Torvalds  * @info: interface from which device disconnected from
7471da177e4SLinus Torvalds  *
7481da177e4SLinus Torvalds  * Start a DMA/PIO transfer off of/on to card
7491da177e4SLinus Torvalds  */
fas216_transfer(FAS216_Info * info)7501da177e4SLinus Torvalds static void fas216_transfer(FAS216_Info *info)
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds 	fasdmadir_t direction;
7531da177e4SLinus Torvalds 	fasdmatype_t dmatype;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	fas216_log(info, LOG_BUFFER,
7561da177e4SLinus Torvalds 		   "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
7571da177e4SLinus Torvalds 		   info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
7586b4df7eeSRussell King 		   info->scsi.SCp.phase);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	if (!info->scsi.SCp.ptr) {
7611da177e4SLinus Torvalds 		fas216_log(info, LOG_ERROR, "null buffer passed to "
7621da177e4SLinus Torvalds 			   "fas216_starttransfer");
7631da177e4SLinus Torvalds 		print_SCp(&info->scsi.SCp, "SCp: ", "\n");
764*dc417545SBart Van Assche 		print_SCp(arm_scsi_pointer(info->SCpnt), "Cmnd SCp: ", "\n");
7651da177e4SLinus Torvalds 		return;
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	/*
7691da177e4SLinus Torvalds 	 * If we have a synchronous transfer agreement in effect, we must
7701da177e4SLinus Torvalds 	 * use DMA mode.  If we are using asynchronous transfers, we may
7711da177e4SLinus Torvalds 	 * use DMA mode or PIO mode.
7721da177e4SLinus Torvalds 	 */
7731da177e4SLinus Torvalds 	if (info->device[info->SCpnt->device->id].sof)
7741da177e4SLinus Torvalds 		dmatype = fasdma_real_all;
7751da177e4SLinus Torvalds 	else
7761da177e4SLinus Torvalds 		dmatype = fasdma_pio;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	if (info->scsi.phase == PHASE_DATAOUT)
7791da177e4SLinus Torvalds 		direction = DMA_OUT;
7801da177e4SLinus Torvalds 	else
7811da177e4SLinus Torvalds 		direction = DMA_IN;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	if (info->dma.setup)
7841da177e4SLinus Torvalds 		dmatype = info->dma.setup(info->host, &info->scsi.SCp,
7851da177e4SLinus Torvalds 					  direction, dmatype);
7861da177e4SLinus Torvalds 	info->dma.transfer_type = dmatype;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	if (dmatype == fasdma_real_all)
7896b4df7eeSRussell King 		fas216_set_stc(info, info->scsi.SCp.phase);
7901da177e4SLinus Torvalds 	else
7911da177e4SLinus Torvalds 		fas216_set_stc(info, info->scsi.SCp.this_residual);
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	switch (dmatype) {
7941da177e4SLinus Torvalds 	case fasdma_pio:
7951da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER, "PIO transfer");
7961da177e4SLinus Torvalds 		fas216_writeb(info, REG_SOF, 0);
7971da177e4SLinus Torvalds 		fas216_writeb(info, REG_STP, info->scsi.async_stp);
7981da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO);
7991da177e4SLinus Torvalds 		fas216_pio(info, direction);
8001da177e4SLinus Torvalds 		break;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	case fasdma_pseudo:
8031da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER, "pseudo transfer");
8041da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
8051da177e4SLinus Torvalds 		info->dma.pseudo(info->host, &info->scsi.SCp,
8061da177e4SLinus Torvalds 				 direction, info->SCpnt->transfersize);
8071da177e4SLinus Torvalds 		break;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	case fasdma_real_block:
8101da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER, "block dma transfer");
8111da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
8121da177e4SLinus Torvalds 		break;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	case fasdma_real_all:
8151da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER, "total dma transfer");
8161da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
8171da177e4SLinus Torvalds 		break;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	default:
8201da177e4SLinus Torvalds 		fas216_log(info, LOG_BUFFER | LOG_ERROR,
8211da177e4SLinus Torvalds 			   "invalid FAS216 DMA type");
8221da177e4SLinus Torvalds 		break;
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds /**
8271da177e4SLinus Torvalds  * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
8281da177e4SLinus Torvalds  * @info: interface from which device disconnected from
8291da177e4SLinus Torvalds  *
8301da177e4SLinus Torvalds  * Called when we switch away from DATA IN or DATA OUT phases.
8311da177e4SLinus Torvalds  */
fas216_stoptransfer(FAS216_Info * info)8321da177e4SLinus Torvalds static void fas216_stoptransfer(FAS216_Info *info)
8331da177e4SLinus Torvalds {
8341da177e4SLinus Torvalds 	fas216_checkmagic(info);
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	if (info->dma.transfer_type == fasdma_real_all ||
8371da177e4SLinus Torvalds 	    info->dma.transfer_type == fasdma_real_block)
8381da177e4SLinus Torvalds 		info->dma.stop(info->host, &info->scsi.SCp);
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	fas216_cleanuptransfer(info);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	if (info->scsi.phase == PHASE_DATAIN) {
8431da177e4SLinus Torvalds 		unsigned int fifo;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 		/*
8461da177e4SLinus Torvalds 		 * If we were performing Data-In, then the FIFO counter
8471da177e4SLinus Torvalds 		 * contains the number of bytes not transferred via DMA
8481da177e4SLinus Torvalds 		 * from the on-board FIFO.  Read them manually.
8491da177e4SLinus Torvalds 		 */
8501da177e4SLinus Torvalds 		fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
8511da177e4SLinus Torvalds 		while (fifo && info->scsi.SCp.ptr) {
8521da177e4SLinus Torvalds 			*info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
8531da177e4SLinus Torvalds 			fas216_updateptrs(info, 1);
8541da177e4SLinus Torvalds 			fifo--;
8551da177e4SLinus Torvalds 		}
8561da177e4SLinus Torvalds 	} else {
8571da177e4SLinus Torvalds 		/*
8581da177e4SLinus Torvalds 		 * After a Data-Out phase, there may be unsent
8591da177e4SLinus Torvalds 		 * bytes left in the FIFO.  Flush them out.
8601da177e4SLinus Torvalds 		 */
8611da177e4SLinus Torvalds 		fas216_cmd(info, CMD_FLUSHFIFO);
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
fas216_aborttransfer(FAS216_Info * info)8651da177e4SLinus Torvalds static void fas216_aborttransfer(FAS216_Info *info)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	fas216_checkmagic(info);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	if (info->dma.transfer_type == fasdma_real_all ||
8701da177e4SLinus Torvalds 	    info->dma.transfer_type == fasdma_real_block)
8711da177e4SLinus Torvalds 		info->dma.stop(info->host, &info->scsi.SCp);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	info->dma.transfer_type = fasdma_none;
8741da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds static void fas216_kick(FAS216_Info *info);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds /**
8801da177e4SLinus Torvalds  * fas216_disconnected_intr - handle device disconnection
8811da177e4SLinus Torvalds  * @info: interface from which device disconnected from
8821da177e4SLinus Torvalds  *
8831da177e4SLinus Torvalds  * Handle device disconnection
8841da177e4SLinus Torvalds  */
fas216_disconnect_intr(FAS216_Info * info)8851da177e4SLinus Torvalds static void fas216_disconnect_intr(FAS216_Info *info)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	unsigned long flags;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	fas216_checkmagic(info);
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
8921da177e4SLinus Torvalds 		   info->scsi.phase);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	switch (info->scsi.phase) {
8971da177e4SLinus Torvalds 	case PHASE_SELECTION:			/* while selecting - no target		*/
8981da177e4SLinus Torvalds 	case PHASE_SELSTEPS:
8991da177e4SLinus Torvalds 		fas216_done(info, DID_NO_CONNECT);
9001da177e4SLinus Torvalds 		break;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	case PHASE_MSGIN_DISCONNECT:		/* message in - disconnecting		*/
9031da177e4SLinus Torvalds 		info->scsi.disconnectable = 1;
9041da177e4SLinus Torvalds 		info->scsi.phase = PHASE_IDLE;
9051da177e4SLinus Torvalds 		info->stats.disconnects += 1;
9061da177e4SLinus Torvalds 		spin_lock_irqsave(&info->host_lock, flags);
9071da177e4SLinus Torvalds 		if (info->scsi.phase == PHASE_IDLE)
9081da177e4SLinus Torvalds 			fas216_kick(info);
9091da177e4SLinus Torvalds 		spin_unlock_irqrestore(&info->host_lock, flags);
9101da177e4SLinus Torvalds 		break;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	case PHASE_DONE:			/* at end of command - complete		*/
9131da177e4SLinus Torvalds 		fas216_done(info, DID_OK);
9141da177e4SLinus Torvalds 		break;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	case PHASE_MSGOUT:			/* message out - possible ABORT message	*/
9171da177e4SLinus Torvalds 		if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
9181da177e4SLinus Torvalds 			info->scsi.aborting = 0;
9191da177e4SLinus Torvalds 			fas216_done(info, DID_ABORT);
9201da177e4SLinus Torvalds 			break;
9211da177e4SLinus Torvalds 		}
922df561f66SGustavo A. R. Silva 		fallthrough;
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	default:				/* huh?					*/
9251da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
9261da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info), fas216_drv_phase(info));
9271da177e4SLinus Torvalds 		print_debug_list();
9281da177e4SLinus Torvalds 		fas216_stoptransfer(info);
9291da177e4SLinus Torvalds 		fas216_done(info, DID_ERROR);
9301da177e4SLinus Torvalds 		break;
9311da177e4SLinus Torvalds 	}
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds /**
9351da177e4SLinus Torvalds  * fas216_reselected_intr - start reconnection of a device
9361da177e4SLinus Torvalds  * @info: interface which was reselected
9371da177e4SLinus Torvalds  *
9381da177e4SLinus Torvalds  * Start reconnection of a device
9391da177e4SLinus Torvalds  */
9401da177e4SLinus Torvalds static void
fas216_reselected_intr(FAS216_Info * info)9411da177e4SLinus Torvalds fas216_reselected_intr(FAS216_Info *info)
9421da177e4SLinus Torvalds {
9431da177e4SLinus Torvalds 	unsigned int cfis, i;
9441da177e4SLinus Torvalds 	unsigned char msg[4];
9451da177e4SLinus Torvalds 	unsigned char target, lun, tag;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	fas216_checkmagic(info);
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	WARN_ON(info->scsi.phase == PHASE_SELECTION ||
9501da177e4SLinus Torvalds 		info->scsi.phase == PHASE_SELSTEPS);
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	cfis = fas216_readb(info, REG_CFIS);
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
9551da177e4SLinus Torvalds 		   info->scsi.phase, cfis);
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	cfis &= CFIS_CF;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	if (cfis < 2 || cfis > 4) {
9601da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
9611da177e4SLinus Torvalds 			info->host->host_no);
9621da177e4SLinus Torvalds 		goto bad_message;
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	for (i = 0; i < cfis; i++)
9661da177e4SLinus Torvalds 		msg[i] = fas216_readb(info, REG_FF);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	if (!(msg[0] & (1 << info->host->this_id)) ||
9691da177e4SLinus Torvalds 	    !(msg[1] & 0x80))
9701da177e4SLinus Torvalds 		goto initiator_error;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	target = msg[0] & ~(1 << info->host->this_id);
9731da177e4SLinus Torvalds 	target = ffs(target) - 1;
9741da177e4SLinus Torvalds 	lun = msg[1] & 7;
9751da177e4SLinus Torvalds 	tag = 0;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	if (cfis >= 3) {
9781da177e4SLinus Torvalds 		if (msg[2] != SIMPLE_QUEUE_TAG)
9791da177e4SLinus Torvalds 			goto initiator_error;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 		tag = msg[3];
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/* set up for synchronous transfers */
9851da177e4SLinus Torvalds 	fas216_writeb(info, REG_SDID, target);
9861da177e4SLinus Torvalds 	fas216_set_sync(info, target);
9871da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
9901da177e4SLinus Torvalds 		   target, lun, tag);
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	if (info->scsi.disconnectable && info->SCpnt) {
9931da177e4SLinus Torvalds 		info->scsi.disconnectable = 0;
9941da177e4SLinus Torvalds 		if (info->SCpnt->device->id  == target &&
9951da177e4SLinus Torvalds 		    info->SCpnt->device->lun == lun &&
996bc41fcbfSHannes Reinecke 		    scsi_cmd_to_rq(info->SCpnt)->tag == tag) {
9971da177e4SLinus Torvalds 			fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
9981da177e4SLinus Torvalds 		} else {
9991da177e4SLinus Torvalds 			queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
10001da177e4SLinus Torvalds 			fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
10011da177e4SLinus Torvalds 			info->SCpnt = NULL;
10021da177e4SLinus Torvalds 		}
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 	if (!info->SCpnt) {
10051da177e4SLinus Torvalds 		info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
10061da177e4SLinus Torvalds 					target, lun, tag);
10071da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT, "had to get command");
10081da177e4SLinus Torvalds 	}
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	if (info->SCpnt) {
10111da177e4SLinus Torvalds 		/*
10121da177e4SLinus Torvalds 		 * Restore data pointer from SAVED data pointer
10131da177e4SLinus Torvalds 		 */
1014*dc417545SBart Van Assche 		info->scsi.SCp = *arm_scsi_pointer(info->SCpnt);
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
10171da177e4SLinus Torvalds 			info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
10181da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGIN;
10191da177e4SLinus Torvalds 	} else {
10201da177e4SLinus Torvalds 		/*
10211da177e4SLinus Torvalds 		 * Our command structure not found - abort the
10221da177e4SLinus Torvalds 		 * command on the target.  Since we have no
10231da177e4SLinus Torvalds 		 * record of this command, we can't send
10241da177e4SLinus Torvalds 		 * an INITIATOR DETECTED ERROR message.
10251da177e4SLinus Torvalds 		 */
10261da177e4SLinus Torvalds 		fas216_cmd(info, CMD_SETATN);
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds #if 0
10291da177e4SLinus Torvalds 		if (tag)
10301da177e4SLinus Torvalds 			msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
10311da177e4SLinus Torvalds 		else
10321da177e4SLinus Torvalds #endif
10331da177e4SLinus Torvalds 			msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
10341da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGOUT_EXPECT;
10351da177e4SLinus Torvalds 		info->scsi.aborting = 1;
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	fas216_cmd(info, CMD_MSGACCEPTED);
10391da177e4SLinus Torvalds 	return;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds  initiator_error:
10421da177e4SLinus Torvalds 	printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
10431da177e4SLinus Torvalds 		info->host->host_no);
10441da177e4SLinus Torvalds 	for (i = 0; i < cfis; i++)
10451da177e4SLinus Torvalds 		printk(" %02x", msg[i]);
10461da177e4SLinus Torvalds 	printk("\n");
10471da177e4SLinus Torvalds  bad_message:
10481da177e4SLinus Torvalds 	fas216_cmd(info, CMD_SETATN);
10491da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
10501da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
10511da177e4SLinus Torvalds 	info->scsi.phase = PHASE_MSGOUT_EXPECT;
10521da177e4SLinus Torvalds 	fas216_cmd(info, CMD_MSGACCEPTED);
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
fas216_parse_message(FAS216_Info * info,unsigned char * message,int msglen)10551da177e4SLinus Torvalds static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
10561da177e4SLinus Torvalds {
1057*dc417545SBart Van Assche 	struct scsi_pointer *scsi_pointer;
10581da177e4SLinus Torvalds 	int i;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	switch (message[0]) {
10611da177e4SLinus Torvalds 	case COMMAND_COMPLETE:
10621da177e4SLinus Torvalds 		if (msglen != 1)
10631da177e4SLinus Torvalds 			goto unrecognised;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.%c: command complete with no "
10661da177e4SLinus Torvalds 			"status in MESSAGE_IN?\n",
10671da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info));
10681da177e4SLinus Torvalds 		break;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	case SAVE_POINTERS:
10711da177e4SLinus Torvalds 		if (msglen != 1)
10721da177e4SLinus Torvalds 			goto unrecognised;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 		/*
10751da177e4SLinus Torvalds 		 * Save current data pointer to SAVED data pointer
10761da177e4SLinus Torvalds 		 * SCSI II standard says that we must not acknowledge
10771da177e4SLinus Torvalds 		 * this until we have really saved pointers.
10781da177e4SLinus Torvalds 		 * NOTE: we DO NOT save the command nor status pointers
10791da177e4SLinus Torvalds 		 * as required by the SCSI II standard.  These always
10801da177e4SLinus Torvalds 		 * point to the start of their respective areas.
10811da177e4SLinus Torvalds 		 */
1082*dc417545SBart Van Assche 		scsi_pointer = arm_scsi_pointer(info->SCpnt);
1083*dc417545SBart Van Assche 		*scsi_pointer = info->scsi.SCp;
1084*dc417545SBart Van Assche 		scsi_pointer->sent_command = 0;
10851da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
10861da177e4SLinus Torvalds 			"save data pointers: [%p, %X]",
10871da177e4SLinus Torvalds 			info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
10881da177e4SLinus Torvalds 		break;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	case RESTORE_POINTERS:
10911da177e4SLinus Torvalds 		if (msglen != 1)
10921da177e4SLinus Torvalds 			goto unrecognised;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 		/*
10951da177e4SLinus Torvalds 		 * Restore current data pointer from SAVED data pointer
10961da177e4SLinus Torvalds 		 */
1097*dc417545SBart Van Assche 		info->scsi.SCp = *arm_scsi_pointer(info->SCpnt);
10981da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
10991da177e4SLinus Torvalds 			"restore data pointers: [%p, 0x%x]",
11001da177e4SLinus Torvalds 			info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
11011da177e4SLinus Torvalds 		break;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	case DISCONNECT:
11041da177e4SLinus Torvalds 		if (msglen != 1)
11051da177e4SLinus Torvalds 			goto unrecognised;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGIN_DISCONNECT;
11081da177e4SLinus Torvalds 		break;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	case MESSAGE_REJECT:
11111da177e4SLinus Torvalds 		if (msglen != 1)
11121da177e4SLinus Torvalds 			goto unrecognised;
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 		switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
11151da177e4SLinus Torvalds 		case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
11161da177e4SLinus Torvalds 			fas216_handlesync(info, message);
11171da177e4SLinus Torvalds 			break;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		default:
11201da177e4SLinus Torvalds 			fas216_log(info, 0, "reject, last message 0x%04x",
11211da177e4SLinus Torvalds 				fas216_get_last_msg(info, info->scsi.msgin_fifo));
11221da177e4SLinus Torvalds 		}
11231da177e4SLinus Torvalds 		break;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	case NOP:
11261da177e4SLinus Torvalds 		break;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	case EXTENDED_MESSAGE:
11291da177e4SLinus Torvalds 		if (msglen < 3)
11301da177e4SLinus Torvalds 			goto unrecognised;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 		switch (message[2]) {
11331da177e4SLinus Torvalds 		case EXTENDED_SDTR:	/* Sync transfer negotiation request/reply */
11341da177e4SLinus Torvalds 			fas216_handlesync(info, message);
11351da177e4SLinus Torvalds 			break;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 		default:
11381da177e4SLinus Torvalds 			goto unrecognised;
11391da177e4SLinus Torvalds 		}
11401da177e4SLinus Torvalds 		break;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	default:
11431da177e4SLinus Torvalds 		goto unrecognised;
11441da177e4SLinus Torvalds 	}
11451da177e4SLinus Torvalds 	return;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds unrecognised:
11481da177e4SLinus Torvalds 	fas216_log(info, 0, "unrecognised message, rejecting");
11491da177e4SLinus Torvalds 	printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
11501da177e4SLinus Torvalds 	for (i = 0; i < msglen; i++)
11511da177e4SLinus Torvalds 		printk("%s%02X", i & 31 ? " " : "\n  ", message[i]);
11521da177e4SLinus Torvalds 	printk("\n");
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	/*
11551da177e4SLinus Torvalds 	 * Something strange seems to be happening here -
11561da177e4SLinus Torvalds 	 * I can't use SETATN since the chip gives me an
11571da177e4SLinus Torvalds 	 * invalid command interrupt when I do.  Weird.
11581da177e4SLinus Torvalds 	 */
11591da177e4SLinus Torvalds fas216_cmd(info, CMD_NOP);
11601da177e4SLinus Torvalds fas216_dumpstate(info);
11611da177e4SLinus Torvalds 	fas216_cmd(info, CMD_SETATN);
11621da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
11631da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
11641da177e4SLinus Torvalds 	info->scsi.phase = PHASE_MSGOUT_EXPECT;
11651da177e4SLinus Torvalds fas216_dumpstate(info);
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
fas216_wait_cmd(FAS216_Info * info,int cmd)11681da177e4SLinus Torvalds static int fas216_wait_cmd(FAS216_Info *info, int cmd)
11691da177e4SLinus Torvalds {
11701da177e4SLinus Torvalds 	int tout;
11711da177e4SLinus Torvalds 	int stat;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	fas216_cmd(info, cmd);
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	for (tout = 1000; tout; tout -= 1) {
11761da177e4SLinus Torvalds 		stat = fas216_readb(info, REG_STAT);
11771da177e4SLinus Torvalds 		if (stat & (STAT_INT|STAT_PARITYERROR))
11781da177e4SLinus Torvalds 			break;
11791da177e4SLinus Torvalds 		udelay(1);
11801da177e4SLinus Torvalds 	}
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	return stat;
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds 
fas216_get_msg_byte(FAS216_Info * info)11851da177e4SLinus Torvalds static int fas216_get_msg_byte(FAS216_Info *info)
11861da177e4SLinus Torvalds {
11871da177e4SLinus Torvalds 	unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	if ((stat & STAT_INT) == 0)
11901da177e4SLinus Torvalds 		goto timedout;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	if ((stat & STAT_BUSMASK) != STAT_MESGIN)
11931da177e4SLinus Torvalds 		goto unexpected_phase_change;
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	fas216_readb(info, REG_INST);
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	if ((stat & STAT_INT) == 0)
12001da177e4SLinus Torvalds 		goto timedout;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	if (stat & STAT_PARITYERROR)
12031da177e4SLinus Torvalds 		goto parity_error;
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	if ((stat & STAT_BUSMASK) != STAT_MESGIN)
12061da177e4SLinus Torvalds 		goto unexpected_phase_change;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	fas216_readb(info, REG_INST);
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	return fas216_readb(info, REG_FF);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds timedout:
12131da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
12141da177e4SLinus Torvalds 	return -1;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds unexpected_phase_change:
12171da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
12181da177e4SLinus Torvalds 	return -2;
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds parity_error:
12211da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "parity error during message in phase");
12221da177e4SLinus Torvalds 	return -3;
12231da177e4SLinus Torvalds }
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds /**
12261da177e4SLinus Torvalds  * fas216_message - handle a function done interrupt from FAS216 chip
12271da177e4SLinus Torvalds  * @info: interface which caused function done interrupt
12281da177e4SLinus Torvalds  *
12291da177e4SLinus Torvalds  * Handle a function done interrupt from FAS216 chip
12301da177e4SLinus Torvalds  */
fas216_message(FAS216_Info * info)12311da177e4SLinus Torvalds static void fas216_message(FAS216_Info *info)
12321da177e4SLinus Torvalds {
12331da177e4SLinus Torvalds 	unsigned char *message = info->scsi.message;
12341da177e4SLinus Torvalds 	unsigned int msglen = 1;
12351da177e4SLinus Torvalds 	int msgbyte = 0;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	fas216_checkmagic(info);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	message[0] = fas216_readb(info, REG_FF);
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	if (message[0] == EXTENDED_MESSAGE) {
12421da177e4SLinus Torvalds 		msgbyte = fas216_get_msg_byte(info);
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 		if (msgbyte >= 0) {
12451da177e4SLinus Torvalds 			message[1] = msgbyte;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 			for (msglen = 2; msglen < message[1] + 2; msglen++) {
12481da177e4SLinus Torvalds 				msgbyte = fas216_get_msg_byte(info);
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 				if (msgbyte >= 0)
12511da177e4SLinus Torvalds 					message[msglen] = msgbyte;
12521da177e4SLinus Torvalds 				else
12531da177e4SLinus Torvalds 					break;
12541da177e4SLinus Torvalds 			}
12551da177e4SLinus Torvalds 		}
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	if (msgbyte == -3)
12591da177e4SLinus Torvalds 		goto parity_error;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds #ifdef DEBUG_MESSAGES
12621da177e4SLinus Torvalds 	{
12631da177e4SLinus Torvalds 		int i;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 		printk("scsi%d.%c: message in: ",
12661da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info));
12671da177e4SLinus Torvalds 		for (i = 0; i < msglen; i++)
12681da177e4SLinus Torvalds 			printk("%02X ", message[i]);
12691da177e4SLinus Torvalds 		printk("\n");
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds #endif
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	fas216_parse_message(info, message, msglen);
12741da177e4SLinus Torvalds 	fas216_cmd(info, CMD_MSGACCEPTED);
12751da177e4SLinus Torvalds 	return;
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds parity_error:
12781da177e4SLinus Torvalds 	fas216_cmd(info, CMD_SETATN);
12791da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
12801da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
12811da177e4SLinus Torvalds 	info->scsi.phase = PHASE_MSGOUT_EXPECT;
12821da177e4SLinus Torvalds 	fas216_cmd(info, CMD_MSGACCEPTED);
12831da177e4SLinus Torvalds 	return;
12841da177e4SLinus Torvalds }
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds /**
12871da177e4SLinus Torvalds  * fas216_send_command - send command after all message bytes have been sent
12881da177e4SLinus Torvalds  * @info: interface which caused bus service
12891da177e4SLinus Torvalds  *
12901da177e4SLinus Torvalds  * Send a command to a target after all message bytes have been sent
12911da177e4SLinus Torvalds  */
fas216_send_command(FAS216_Info * info)12921da177e4SLinus Torvalds static void fas216_send_command(FAS216_Info *info)
12931da177e4SLinus Torvalds {
12941da177e4SLinus Torvalds 	int i;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	fas216_checkmagic(info);
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
12991da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	/* load command */
13021da177e4SLinus Torvalds 	for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
13031da177e4SLinus Torvalds 		fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	fas216_cmd(info, CMD_TRANSFERINFO);
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	info->scsi.phase = PHASE_COMMAND;
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds /**
13111da177e4SLinus Torvalds  * fas216_send_messageout - handle bus service to send a message
13121da177e4SLinus Torvalds  * @info: interface which caused bus service
13131da177e4SLinus Torvalds  *
13141da177e4SLinus Torvalds  * Handle bus service to send a message.
13151da177e4SLinus Torvalds  * Note: We do not allow the device to change the data direction!
13161da177e4SLinus Torvalds  */
fas216_send_messageout(FAS216_Info * info,int start)13171da177e4SLinus Torvalds static void fas216_send_messageout(FAS216_Info *info, int start)
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	fas216_checkmagic(info);
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	if (tot_msglen) {
13261da177e4SLinus Torvalds 		struct message *msg;
13271da177e4SLinus Torvalds 		int msgnr = 0;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 		while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
13301da177e4SLinus Torvalds 			int i;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 			for (i = start; i < msg->length; i++)
13331da177e4SLinus Torvalds 				fas216_writeb(info, REG_FF, msg->msg[i]);
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 			msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
13361da177e4SLinus Torvalds 			start = 0;
13371da177e4SLinus Torvalds 		}
13381da177e4SLinus Torvalds 	} else
13391da177e4SLinus Torvalds 		fas216_writeb(info, REG_FF, NOP);
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	fas216_cmd(info, CMD_TRANSFERINFO);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	info->scsi.phase = PHASE_MSGOUT;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds /**
13471da177e4SLinus Torvalds  * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
13481da177e4SLinus Torvalds  * @info: interface which caused bus service interrupt
13491da177e4SLinus Torvalds  * @stat: Status register contents
13501da177e4SLinus Torvalds  * @is: SCSI Status register contents
13511da177e4SLinus Torvalds  *
13521da177e4SLinus Torvalds  * Handle a bus service interrupt from FAS216 chip
13531da177e4SLinus Torvalds  */
fas216_busservice_intr(FAS216_Info * info,unsigned int stat,unsigned int is)13541da177e4SLinus Torvalds static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
13551da177e4SLinus Torvalds {
13561da177e4SLinus Torvalds 	fas216_checkmagic(info);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	fas216_log(info, LOG_BUSSERVICE,
13591da177e4SLinus Torvalds 		   "bus service: stat=%02x is=%02x phase=%02x",
13601da177e4SLinus Torvalds 		   stat, is, info->scsi.phase);
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	switch (info->scsi.phase) {
13631da177e4SLinus Torvalds 	case PHASE_SELECTION:
13641da177e4SLinus Torvalds 		if ((is & IS_BITS) != IS_MSGBYTESENT)
13651da177e4SLinus Torvalds 			goto bad_is;
13661da177e4SLinus Torvalds 		break;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	case PHASE_SELSTEPS:
13691da177e4SLinus Torvalds 		switch (is & IS_BITS) {
13701da177e4SLinus Torvalds 		case IS_SELARB:
13711da177e4SLinus Torvalds 		case IS_MSGBYTESENT:
13721da177e4SLinus Torvalds 			goto bad_is;
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 		case IS_NOTCOMMAND:
13751da177e4SLinus Torvalds 		case IS_EARLYPHASE:
13761da177e4SLinus Torvalds 			if ((stat & STAT_BUSMASK) == STAT_MESGIN)
13771da177e4SLinus Torvalds 				break;
13781da177e4SLinus Torvalds 			goto bad_is;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 		case IS_COMPLETE:
13811da177e4SLinus Torvalds 			break;
13821da177e4SLinus Torvalds 		}
1383cb163627SGustavo A. R. Silva 		break;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	default:
13861da177e4SLinus Torvalds 		break;
13871da177e4SLinus Torvalds 	}
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP);
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds #define STATE(st,ph) ((ph) << 3 | (st))
13921da177e4SLinus Torvalds 	/* This table describes the legal SCSI state transitions,
13931da177e4SLinus Torvalds 	 * as described by the SCSI II spec.
13941da177e4SLinus Torvalds 	 */
13951da177e4SLinus Torvalds 	switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
13961da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In      */
13971da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_MSGOUT):  /* Message Out  -> Data In      */
13981da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command      -> Data In      */
13991da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_MSGIN):   /* Message In   -> Data In      */
14001da177e4SLinus Torvalds 		info->scsi.phase = PHASE_DATAIN;
14011da177e4SLinus Torvalds 		fas216_transfer(info);
14021da177e4SLinus Torvalds 		return;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	case STATE(STAT_DATAIN, PHASE_DATAIN):  /* Data In      -> Data In      */
14051da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out     -> Data Out     */
14061da177e4SLinus Torvalds 		fas216_cleanuptransfer(info);
14071da177e4SLinus Torvalds 		fas216_transfer(info);
14081da177e4SLinus Torvalds 		return;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out     */
14111da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out  -> Data Out     */
14121da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command      -> Data Out     */
14131da177e4SLinus Torvalds 	case STATE(STAT_DATAOUT, PHASE_MSGIN):  /* Message In   -> Data Out     */
14141da177e4SLinus Torvalds 		fas216_cmd(info, CMD_FLUSHFIFO);
14151da177e4SLinus Torvalds 		info->scsi.phase = PHASE_DATAOUT;
14161da177e4SLinus Torvalds 		fas216_transfer(info);
14171da177e4SLinus Torvalds 		return;
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out     -> Status       */
14201da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_DATAIN):  /* Data In      -> Status       */
14211da177e4SLinus Torvalds 		fas216_stoptransfer(info);
1422df561f66SGustavo A. R. Silva 		fallthrough;
1423fccf01b6SGustavo A. R. Silva 
14241da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status       */
14251da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_MSGOUT):  /* Message Out  -> Status       */
14261da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_COMMAND): /* Command      -> Status       */
14271da177e4SLinus Torvalds 	case STATE(STAT_STATUS, PHASE_MSGIN):   /* Message In   -> Status       */
14281da177e4SLinus Torvalds 		fas216_cmd(info, CMD_INITCMDCOMPLETE);
14291da177e4SLinus Torvalds 		info->scsi.phase = PHASE_STATUS;
14301da177e4SLinus Torvalds 		return;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out     -> Message In   */
14331da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_DATAIN):  /* Data In      -> Message In   */
14341da177e4SLinus Torvalds 		fas216_stoptransfer(info);
1435df561f66SGustavo A. R. Silva 		fallthrough;
1436fccf01b6SGustavo A. R. Silva 
14371da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_COMMAND):	/* Command	-> Message In	*/
14381da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In   */
14391da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_MSGOUT):  /* Message Out  -> Message In   */
14401da177e4SLinus Torvalds 		info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
14411da177e4SLinus Torvalds 		fas216_cmd(info, CMD_FLUSHFIFO);
14421da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO);
14431da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGIN;
14441da177e4SLinus Torvalds 		return;
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	case STATE(STAT_MESGIN, PHASE_MSGIN):
14471da177e4SLinus Torvalds 		info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
14481da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO);
14491da177e4SLinus Torvalds 		return;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out  -> Command      */
14521da177e4SLinus Torvalds 	case STATE(STAT_COMMAND, PHASE_MSGIN):  /* Message In   -> Command      */
14531da177e4SLinus Torvalds 		fas216_send_command(info);
14541da177e4SLinus Torvalds 		info->scsi.phase = PHASE_COMMAND;
14551da177e4SLinus Torvalds 		return;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	/*
14591da177e4SLinus Torvalds 	 * Selection    -> Message Out
14601da177e4SLinus Torvalds 	 */
14611da177e4SLinus Torvalds 	case STATE(STAT_MESGOUT, PHASE_SELECTION):
14621da177e4SLinus Torvalds 		fas216_send_messageout(info, 1);
14631da177e4SLinus Torvalds 		return;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	/*
14661da177e4SLinus Torvalds 	 * Message Out  -> Message Out
14671da177e4SLinus Torvalds 	 */
14681da177e4SLinus Torvalds 	case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
14691da177e4SLinus Torvalds 	case STATE(STAT_MESGOUT, PHASE_MSGOUT):
14701da177e4SLinus Torvalds 		/*
14711da177e4SLinus Torvalds 		 * If we get another message out phase, this usually
14721da177e4SLinus Torvalds 		 * means some parity error occurred.  Resend complete
14731da177e4SLinus Torvalds 		 * set of messages.  If we have more than one byte to
14741da177e4SLinus Torvalds 		 * send, we need to assert ATN again.
14751da177e4SLinus Torvalds 		 */
14761da177e4SLinus Torvalds 		if (info->device[info->SCpnt->device->id].parity_check) {
14771da177e4SLinus Torvalds 			/*
14781da177e4SLinus Torvalds 			 * We were testing... good, the device
14791da177e4SLinus Torvalds 			 * supports parity checking.
14801da177e4SLinus Torvalds 			 */
14811da177e4SLinus Torvalds 			info->device[info->SCpnt->device->id].parity_check = 0;
14821da177e4SLinus Torvalds 			info->device[info->SCpnt->device->id].parity_enabled = 1;
14831da177e4SLinus Torvalds 			fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
14841da177e4SLinus Torvalds 		}
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 		if (msgqueue_msglength(&info->scsi.msgs) > 1)
14871da177e4SLinus Torvalds 			fas216_cmd(info, CMD_SETATN);
148886cfe4adSWei Ming Chen 		fallthrough;
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	/*
14911da177e4SLinus Torvalds 	 * Any          -> Message Out
14921da177e4SLinus Torvalds 	 */
14931da177e4SLinus Torvalds 	case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
14941da177e4SLinus Torvalds 		fas216_send_messageout(info, 0);
14951da177e4SLinus Torvalds 		return;
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	/* Error recovery rules.
14981da177e4SLinus Torvalds 	 *   These either attempt to abort or retry the operation.
14991da177e4SLinus Torvalds 	 * TODO: we need more of these
15001da177e4SLinus Torvalds 	 */
15011da177e4SLinus Torvalds 	case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command      -> Command      */
15021da177e4SLinus Torvalds 		/* error - we've sent out all the command bytes
15031da177e4SLinus Torvalds 		 * we have.
15041da177e4SLinus Torvalds 		 * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
15051da177e4SLinus Torvalds 		 * to include the command bytes sent for this to work
15061da177e4SLinus Torvalds 		 * correctly.
15071da177e4SLinus Torvalds 		 */
15081da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.%c: "
15091da177e4SLinus Torvalds 			"target trying to receive more command bytes\n",
15101da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info));
15111da177e4SLinus Torvalds 		fas216_cmd(info, CMD_SETATN);
15121da177e4SLinus Torvalds 		fas216_set_stc(info, 15);
15131da177e4SLinus Torvalds 		fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
15141da177e4SLinus Torvalds 		msgqueue_flush(&info->scsi.msgs);
15151da177e4SLinus Torvalds 		msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
15161da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGOUT_EXPECT;
15171da177e4SLinus Torvalds 		return;
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
15211da177e4SLinus Torvalds 		printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
15221da177e4SLinus Torvalds 			info->host->host_no, fas216_target(info),
15231da177e4SLinus Torvalds 			fas216_bus_phase(stat));
15241da177e4SLinus Torvalds 		msgqueue_flush(&info->scsi.msgs);
15251da177e4SLinus Torvalds 		fas216_cmd(info, CMD_SETATN);
15261da177e4SLinus Torvalds 		msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
15271da177e4SLinus Torvalds 		info->scsi.phase = PHASE_MSGOUT_EXPECT;
15281da177e4SLinus Torvalds 		info->scsi.aborting = 1;
15291da177e4SLinus Torvalds 		fas216_cmd(info, CMD_TRANSFERINFO);
15301da177e4SLinus Torvalds 		return;
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 	printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
15331da177e4SLinus Torvalds 		info->host->host_no, fas216_target(info),
15341da177e4SLinus Torvalds 		fas216_bus_phase(stat),
15351da177e4SLinus Torvalds 		fas216_drv_phase(info));
15361da177e4SLinus Torvalds 	print_debug_list();
15371da177e4SLinus Torvalds 	return;
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds bad_is:
15401da177e4SLinus Torvalds 	fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
15411da177e4SLinus Torvalds 	fas216_dumpstate(info);
15421da177e4SLinus Torvalds 	print_debug_list();
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	fas216_done(info, DID_ERROR);
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds /**
15481da177e4SLinus Torvalds  * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
15491da177e4SLinus Torvalds  * @info: interface which caused function done interrupt
15501da177e4SLinus Torvalds  * @stat: Status register contents
15511da177e4SLinus Torvalds  * @is: SCSI Status register contents
15521da177e4SLinus Torvalds  *
15531da177e4SLinus Torvalds  * Handle a function done interrupt from FAS216 chip
15541da177e4SLinus Torvalds  */
fas216_funcdone_intr(FAS216_Info * info,unsigned int stat,unsigned int is)15551da177e4SLinus Torvalds static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	fas216_checkmagic(info);
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	fas216_log(info, LOG_FUNCTIONDONE,
15621da177e4SLinus Torvalds 		   "function done: stat=%02x is=%02x phase=%02x",
15631da177e4SLinus Torvalds 		   stat, is, info->scsi.phase);
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	switch (info->scsi.phase) {
15661da177e4SLinus Torvalds 	case PHASE_STATUS:			/* status phase - read status and msg	*/
15671da177e4SLinus Torvalds 		if (fifo_len != 2) {
15681da177e4SLinus Torvalds 			fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
15691da177e4SLinus Torvalds 		}
15701da177e4SLinus Torvalds 		/*
15711da177e4SLinus Torvalds 		 * Read status then message byte.
15721da177e4SLinus Torvalds 		 */
15731da177e4SLinus Torvalds 		info->scsi.SCp.Status = fas216_readb(info, REG_FF);
15741da177e4SLinus Torvalds 		info->scsi.SCp.Message = fas216_readb(info, REG_FF);
15751da177e4SLinus Torvalds 		info->scsi.phase = PHASE_DONE;
15761da177e4SLinus Torvalds 		fas216_cmd(info, CMD_MSGACCEPTED);
15771da177e4SLinus Torvalds 		break;
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	case PHASE_IDLE:
15801da177e4SLinus Torvalds 	case PHASE_SELECTION:
15811da177e4SLinus Torvalds 	case PHASE_SELSTEPS:
15821da177e4SLinus Torvalds 		break;
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	case PHASE_MSGIN:			/* message in phase			*/
15851da177e4SLinus Torvalds 		if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
15861da177e4SLinus Torvalds 			info->scsi.msgin_fifo = fifo_len;
15871da177e4SLinus Torvalds 			fas216_message(info);
15881da177e4SLinus Torvalds 			break;
15891da177e4SLinus Torvalds 		}
1590df561f66SGustavo A. R. Silva 		fallthrough;
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 	default:
15931da177e4SLinus Torvalds 		fas216_log(info, 0, "internal phase %s for function done?"
15941da177e4SLinus Torvalds 			"  What do I do with this?",
15951da177e4SLinus Torvalds 			fas216_target(info), fas216_drv_phase(info));
15961da177e4SLinus Torvalds 	}
15971da177e4SLinus Torvalds }
15981da177e4SLinus Torvalds 
fas216_bus_reset(FAS216_Info * info)15991da177e4SLinus Torvalds static void fas216_bus_reset(FAS216_Info *info)
16001da177e4SLinus Torvalds {
16011da177e4SLinus Torvalds 	neg_t sync_state;
16021da177e4SLinus Torvalds 	int i;
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	sync_state = neg_invalid;
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds #ifdef SCSI2_SYNC
16091da177e4SLinus Torvalds 	if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
16101da177e4SLinus Torvalds 		sync_state = neg_wait;
16111da177e4SLinus Torvalds #endif
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	info->scsi.phase = PHASE_IDLE;
16141da177e4SLinus Torvalds 	info->SCpnt = NULL; /* bug! */
16151da177e4SLinus Torvalds 	memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
16181da177e4SLinus Torvalds 		info->device[i].disconnect_ok	= info->ifcfg.disconnect_ok;
16191da177e4SLinus Torvalds 		info->device[i].sync_state	= sync_state;
16201da177e4SLinus Torvalds 		info->device[i].period		= info->ifcfg.asyncperiod / 4;
16211da177e4SLinus Torvalds 		info->device[i].stp		= info->scsi.async_stp;
16221da177e4SLinus Torvalds 		info->device[i].sof		= 0;
16231da177e4SLinus Torvalds 		info->device[i].wide_xfer	= 0;
16241da177e4SLinus Torvalds 	}
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	info->rst_bus_status = 1;
16271da177e4SLinus Torvalds 	wake_up(&info->eh_wait);
16281da177e4SLinus Torvalds }
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds /**
16311da177e4SLinus Torvalds  * fas216_intr - handle interrupts to progress a command
16321da177e4SLinus Torvalds  * @info: interface to service
16331da177e4SLinus Torvalds  *
16341da177e4SLinus Torvalds  * Handle interrupts from the interface to progress a command
16351da177e4SLinus Torvalds  */
fas216_intr(FAS216_Info * info)16361da177e4SLinus Torvalds irqreturn_t fas216_intr(FAS216_Info *info)
16371da177e4SLinus Torvalds {
16381da177e4SLinus Torvalds 	unsigned char inst, is, stat;
16391da177e4SLinus Torvalds 	int handled = IRQ_NONE;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	fas216_checkmagic(info);
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	stat = fas216_readb(info, REG_STAT);
16441da177e4SLinus Torvalds 	is = fas216_readb(info, REG_IS);
16451da177e4SLinus Torvalds 	inst = fas216_readb(info, REG_INST);
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	add_debug_list(stat, is, inst, info->scsi.phase);
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 	if (stat & STAT_INT) {
16501da177e4SLinus Torvalds 		if (inst & INST_BUSRESET) {
16511da177e4SLinus Torvalds 			fas216_log(info, 0, "bus reset detected");
16521da177e4SLinus Torvalds 			fas216_bus_reset(info);
16531da177e4SLinus Torvalds 			scsi_report_bus_reset(info->host, 0);
16541da177e4SLinus Torvalds 		} else if (inst & INST_ILLEGALCMD) {
16551da177e4SLinus Torvalds 			fas216_log(info, LOG_ERROR, "illegal command given\n");
16561da177e4SLinus Torvalds 			fas216_dumpstate(info);
16571da177e4SLinus Torvalds 			print_debug_list();
16581da177e4SLinus Torvalds 		} else if (inst & INST_DISCONNECT)
16591da177e4SLinus Torvalds 			fas216_disconnect_intr(info);
16601da177e4SLinus Torvalds 		else if (inst & INST_RESELECTED)	/* reselected			*/
16611da177e4SLinus Torvalds 			fas216_reselected_intr(info);
16621da177e4SLinus Torvalds 		else if (inst & INST_BUSSERVICE)	/* bus service request		*/
16631da177e4SLinus Torvalds 			fas216_busservice_intr(info, stat, is);
16641da177e4SLinus Torvalds 		else if (inst & INST_FUNCDONE)		/* function done		*/
16651da177e4SLinus Torvalds 			fas216_funcdone_intr(info, stat, is);
16661da177e4SLinus Torvalds 		else
16671da177e4SLinus Torvalds 		    	fas216_log(info, 0, "unknown interrupt received:"
16681da177e4SLinus Torvalds 				" phase %s inst %02X is %02X stat %02X",
16691da177e4SLinus Torvalds 				fas216_drv_phase(info), inst, is, stat);
16701da177e4SLinus Torvalds 		handled = IRQ_HANDLED;
16711da177e4SLinus Torvalds 	}
16721da177e4SLinus Torvalds 	return handled;
16731da177e4SLinus Torvalds }
16741da177e4SLinus Torvalds 
__fas216_start_command(FAS216_Info * info,struct scsi_cmnd * SCpnt)1675ee0ca6baSHenne static void __fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
16761da177e4SLinus Torvalds {
16771da177e4SLinus Torvalds 	int tot_msglen;
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	/* following what the ESP driver says */
16801da177e4SLinus Torvalds 	fas216_set_stc(info, 0);
16811da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	/* flush FIFO */
16841da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	/* load bus-id and timeout */
16871da177e4SLinus Torvalds 	fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
16881da177e4SLinus Torvalds 	fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds 	/* synchronous transfers */
16911da177e4SLinus Torvalds 	fas216_set_sync(info, SCpnt->device->id);
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 	tot_msglen = msgqueue_msglength(&info->scsi.msgs);
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds #ifdef DEBUG_MESSAGES
16961da177e4SLinus Torvalds 	{
16971da177e4SLinus Torvalds 		struct message *msg;
16981da177e4SLinus Torvalds 		int msgnr = 0, i;
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 		printk("scsi%d.%c: message out: ",
17011da177e4SLinus Torvalds 			info->host->host_no, '0' + SCpnt->device->id);
17021da177e4SLinus Torvalds 		while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
17031da177e4SLinus Torvalds 			printk("{ ");
17041da177e4SLinus Torvalds 			for (i = 0; i < msg->length; i++)
17051da177e4SLinus Torvalds 				printk("%02x ", msg->msg[i]);
17061da177e4SLinus Torvalds 			printk("} ");
17071da177e4SLinus Torvalds 		}
17081da177e4SLinus Torvalds 		printk("\n");
17091da177e4SLinus Torvalds 	}
17101da177e4SLinus Torvalds #endif
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	if (tot_msglen == 1 || tot_msglen == 3) {
17131da177e4SLinus Torvalds 		/*
17141da177e4SLinus Torvalds 		 * We have an easy message length to send...
17151da177e4SLinus Torvalds 		 */
17161da177e4SLinus Torvalds 		struct message *msg;
17171da177e4SLinus Torvalds 		int msgnr = 0, i;
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 		info->scsi.phase = PHASE_SELSTEPS;
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 		/* load message bytes */
17221da177e4SLinus Torvalds 		while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
17231da177e4SLinus Torvalds 			for (i = 0; i < msg->length; i++)
17241da177e4SLinus Torvalds 				fas216_writeb(info, REG_FF, msg->msg[i]);
17251da177e4SLinus Torvalds 			msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
17261da177e4SLinus Torvalds 		}
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 		/* load command */
17291da177e4SLinus Torvalds 		for (i = 0; i < SCpnt->cmd_len; i++)
17301da177e4SLinus Torvalds 			fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 		if (tot_msglen == 1)
17331da177e4SLinus Torvalds 			fas216_cmd(info, CMD_SELECTATN);
17341da177e4SLinus Torvalds 		else
17351da177e4SLinus Torvalds 			fas216_cmd(info, CMD_SELECTATN3);
17361da177e4SLinus Torvalds 	} else {
17371da177e4SLinus Torvalds 		/*
17381da177e4SLinus Torvalds 		 * We have an unusual number of message bytes to send.
17391da177e4SLinus Torvalds 		 *  Load first byte into fifo, and issue SELECT with ATN and
17401da177e4SLinus Torvalds 		 *  stop steps.
17411da177e4SLinus Torvalds 		 */
17421da177e4SLinus Torvalds 		struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 		fas216_writeb(info, REG_FF, msg->msg[0]);
17451da177e4SLinus Torvalds 		msg->fifo = 1;
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 		fas216_cmd(info, CMD_SELECTATNSTOP);
17481da177e4SLinus Torvalds 	}
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds /*
17521da177e4SLinus Torvalds  * Decide whether we need to perform a parity test on this device.
17531da177e4SLinus Torvalds  * Can also be used to force parity error conditions during initial
17541da177e4SLinus Torvalds  * information transfer phase (message out) for test purposes.
17551da177e4SLinus Torvalds  */
parity_test(FAS216_Info * info,int target)17561da177e4SLinus Torvalds static int parity_test(FAS216_Info *info, int target)
17571da177e4SLinus Torvalds {
17581da177e4SLinus Torvalds #if 0
17591da177e4SLinus Torvalds 	if (target == 3) {
17601da177e4SLinus Torvalds 		info->device[target].parity_check = 0;
17611da177e4SLinus Torvalds 		return 1;
17621da177e4SLinus Torvalds 	}
17631da177e4SLinus Torvalds #endif
17641da177e4SLinus Torvalds 	return info->device[target].parity_check;
17651da177e4SLinus Torvalds }
17661da177e4SLinus Torvalds 
fas216_start_command(FAS216_Info * info,struct scsi_cmnd * SCpnt)1767ee0ca6baSHenne static void fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
17681da177e4SLinus Torvalds {
17691da177e4SLinus Torvalds 	int disconnect_ok;
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	/*
17721da177e4SLinus Torvalds 	 * claim host busy
17731da177e4SLinus Torvalds 	 */
17741da177e4SLinus Torvalds 	info->scsi.phase = PHASE_SELECTION;
1775*dc417545SBart Van Assche 	info->scsi.SCp = *arm_scsi_pointer(SCpnt);
17761da177e4SLinus Torvalds 	info->SCpnt = SCpnt;
17771da177e4SLinus Torvalds 	info->dma.transfer_type = fasdma_none;
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	if (parity_test(info, SCpnt->device->id))
17801da177e4SLinus Torvalds 		fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
17811da177e4SLinus Torvalds 	else
17821da177e4SLinus Torvalds 		fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	/*
17851da177e4SLinus Torvalds 	 * Don't allow request sense commands to disconnect.
17861da177e4SLinus Torvalds 	 */
17871da177e4SLinus Torvalds 	disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
17881da177e4SLinus Torvalds 			info->device[SCpnt->device->id].disconnect_ok;
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds 	/*
17911da177e4SLinus Torvalds 	 * build outgoing message bytes
17921da177e4SLinus Torvalds 	 */
17931da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
17941da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->device->lun));
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds 	/*
17971da177e4SLinus Torvalds 	 * add tag message if required
17981da177e4SLinus Torvalds 	 */
1799bc41fcbfSHannes Reinecke 	if (SCpnt->device->simple_tags)
1800bc41fcbfSHannes Reinecke 		msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG,
1801bc41fcbfSHannes Reinecke 				scsi_cmd_to_rq(SCpnt)->tag);
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	do {
18041da177e4SLinus Torvalds #ifdef SCSI2_SYNC
18051da177e4SLinus Torvalds 		if ((info->device[SCpnt->device->id].sync_state == neg_wait ||
18061da177e4SLinus Torvalds 		     info->device[SCpnt->device->id].sync_state == neg_complete) &&
18071da177e4SLinus Torvalds 		    (SCpnt->cmnd[0] == REQUEST_SENSE ||
18081da177e4SLinus Torvalds 		     SCpnt->cmnd[0] == INQUIRY)) {
18091da177e4SLinus Torvalds 			info->device[SCpnt->device->id].sync_state = neg_inprogress;
18101da177e4SLinus Torvalds 			msgqueue_addmsg(&info->scsi.msgs, 5,
18111da177e4SLinus Torvalds 					EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
18121da177e4SLinus Torvalds 					1000 / info->ifcfg.clockrate,
18131da177e4SLinus Torvalds 					info->ifcfg.sync_max_depth);
18141da177e4SLinus Torvalds 			break;
18151da177e4SLinus Torvalds 		}
18161da177e4SLinus Torvalds #endif
18171da177e4SLinus Torvalds 	} while (0);
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	__fas216_start_command(info, SCpnt);
18201da177e4SLinus Torvalds }
18211da177e4SLinus Torvalds 
fas216_allocate_tag(FAS216_Info * info,struct scsi_cmnd * SCpnt)1822ee0ca6baSHenne static void fas216_allocate_tag(FAS216_Info *info, struct scsi_cmnd *SCpnt)
18231da177e4SLinus Torvalds {
18249cb78c16SHannes Reinecke 	set_bit(SCpnt->device->id * 8 +
18259cb78c16SHannes Reinecke 		(u8)(SCpnt->device->lun & 0x7), info->busyluns);
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	info->stats.removes += 1;
18281da177e4SLinus Torvalds 	switch (SCpnt->cmnd[0]) {
18291da177e4SLinus Torvalds 	case WRITE_6:
18301da177e4SLinus Torvalds 	case WRITE_10:
18311da177e4SLinus Torvalds 	case WRITE_12:
18321da177e4SLinus Torvalds 		info->stats.writes += 1;
18331da177e4SLinus Torvalds 		break;
18341da177e4SLinus Torvalds 	case READ_6:
18351da177e4SLinus Torvalds 	case READ_10:
18361da177e4SLinus Torvalds 	case READ_12:
18371da177e4SLinus Torvalds 		info->stats.reads += 1;
18381da177e4SLinus Torvalds 		break;
18391da177e4SLinus Torvalds 	default:
18401da177e4SLinus Torvalds 		info->stats.miscs += 1;
18411da177e4SLinus Torvalds 		break;
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
fas216_do_bus_device_reset(FAS216_Info * info,struct scsi_cmnd * SCpnt)1845ee0ca6baSHenne static void fas216_do_bus_device_reset(FAS216_Info *info,
1846ee0ca6baSHenne 				       struct scsi_cmnd *SCpnt)
18471da177e4SLinus Torvalds {
18481da177e4SLinus Torvalds 	struct message *msg;
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	/*
18511da177e4SLinus Torvalds 	 * claim host busy
18521da177e4SLinus Torvalds 	 */
18531da177e4SLinus Torvalds 	info->scsi.phase = PHASE_SELECTION;
1854*dc417545SBart Van Assche 	info->scsi.SCp = *arm_scsi_pointer(SCpnt);
18551da177e4SLinus Torvalds 	info->SCpnt = SCpnt;
18561da177e4SLinus Torvalds 	info->dma.transfer_type = fasdma_none;
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "sending bus device reset");
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 	msgqueue_flush(&info->scsi.msgs);
18611da177e4SLinus Torvalds 	msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	/* following what the ESP driver says */
18641da177e4SLinus Torvalds 	fas216_set_stc(info, 0);
18651da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	/* flush FIFO */
18681da177e4SLinus Torvalds 	fas216_cmd(info, CMD_FLUSHFIFO);
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	/* load bus-id and timeout */
18711da177e4SLinus Torvalds 	fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
18721da177e4SLinus Torvalds 	fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	/* synchronous transfers */
18751da177e4SLinus Torvalds 	fas216_set_sync(info, SCpnt->device->id);
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 	msg = msgqueue_getmsg(&info->scsi.msgs, 0);
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	fas216_writeb(info, REG_FF, BUS_DEVICE_RESET);
18801da177e4SLinus Torvalds 	msg->fifo = 1;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	fas216_cmd(info, CMD_SELECTATNSTOP);
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds /**
18861da177e4SLinus Torvalds  * fas216_kick - kick a command to the interface
18871da177e4SLinus Torvalds  * @info: our host interface to kick
18881da177e4SLinus Torvalds  *
18891da177e4SLinus Torvalds  * Kick a command to the interface, interface should be idle.
18901da177e4SLinus Torvalds  * Notes: Interrupts are always disabled!
18911da177e4SLinus Torvalds  */
fas216_kick(FAS216_Info * info)18921da177e4SLinus Torvalds static void fas216_kick(FAS216_Info *info)
18931da177e4SLinus Torvalds {
1894ee0ca6baSHenne 	struct scsi_cmnd *SCpnt = NULL;
18951da177e4SLinus Torvalds #define TYPE_OTHER	0
18961da177e4SLinus Torvalds #define TYPE_RESET	1
18971da177e4SLinus Torvalds #define TYPE_QUEUE	2
18981da177e4SLinus Torvalds 	int where_from = TYPE_OTHER;
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	fas216_checkmagic(info);
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	/*
19031da177e4SLinus Torvalds 	 * Obtain the next command to process.
19041da177e4SLinus Torvalds 	 */
19051da177e4SLinus Torvalds 	do {
19061da177e4SLinus Torvalds 		if (info->rstSCpnt) {
19071da177e4SLinus Torvalds 			SCpnt = info->rstSCpnt;
19081da177e4SLinus Torvalds 			/* don't remove it */
19091da177e4SLinus Torvalds 			where_from = TYPE_RESET;
19101da177e4SLinus Torvalds 			break;
19111da177e4SLinus Torvalds 		}
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 		if (info->reqSCpnt) {
19141da177e4SLinus Torvalds 			SCpnt = info->reqSCpnt;
19151da177e4SLinus Torvalds 			info->reqSCpnt = NULL;
19161da177e4SLinus Torvalds 			break;
19171da177e4SLinus Torvalds 		}
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 		if (info->origSCpnt) {
19201da177e4SLinus Torvalds 			SCpnt = info->origSCpnt;
19211da177e4SLinus Torvalds 			info->origSCpnt = NULL;
19221da177e4SLinus Torvalds 			break;
19231da177e4SLinus Torvalds 		}
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 		/* retrieve next command */
19261da177e4SLinus Torvalds 		if (!SCpnt) {
19271da177e4SLinus Torvalds 			SCpnt = queue_remove_exclude(&info->queues.issue,
19281da177e4SLinus Torvalds 						     info->busyluns);
19291da177e4SLinus Torvalds 			where_from = TYPE_QUEUE;
19301da177e4SLinus Torvalds 			break;
19311da177e4SLinus Torvalds 		}
19321da177e4SLinus Torvalds 	} while (0);
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 	if (!SCpnt) {
19351da177e4SLinus Torvalds 		/*
19361da177e4SLinus Torvalds 		 * no command pending, so enable reselection.
19371da177e4SLinus Torvalds 		 */
19381da177e4SLinus Torvalds 		fas216_cmd(info, CMD_ENABLESEL);
19391da177e4SLinus Torvalds 		return;
19401da177e4SLinus Torvalds 	}
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds 	/*
19431da177e4SLinus Torvalds 	 * We're going to start a command, so disable reselection
19441da177e4SLinus Torvalds 	 */
19451da177e4SLinus Torvalds 	fas216_cmd(info, CMD_DISABLESEL);
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	if (info->scsi.disconnectable && info->SCpnt) {
19481da177e4SLinus Torvalds 		fas216_log(info, LOG_CONNECT,
19491da177e4SLinus Torvalds 			"moved command for %d to disconnected queue",
19501da177e4SLinus Torvalds 			info->SCpnt->device->id);
19511da177e4SLinus Torvalds 		queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
19521da177e4SLinus Torvalds 		info->scsi.disconnectable = 0;
19531da177e4SLinus Torvalds 		info->SCpnt = NULL;
19541da177e4SLinus Torvalds 	}
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	fas216_log_command(info, LOG_CONNECT | LOG_MESSAGES, SCpnt,
19571da177e4SLinus Torvalds 			   "starting");
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	switch (where_from) {
19601da177e4SLinus Torvalds 	case TYPE_QUEUE:
19611da177e4SLinus Torvalds 		fas216_allocate_tag(info, SCpnt);
1962df561f66SGustavo A. R. Silva 		fallthrough;
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  */
fas216_devicereset_done(FAS216_Info * info,struct scsi_cmnd * SCpnt,unsigned int result)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  */
fas216_rq_sns_done(FAS216_Info * info,struct scsi_cmnd * SCpnt,unsigned int result)2001ee0ca6baSHenne static void fas216_rq_sns_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
2002ee0ca6baSHenne 			       unsigned int result)
20031da177e4SLinus Torvalds {
2004*dc417545SBart Van Assche 	struct scsi_pointer *scsi_pointer = arm_scsi_pointer(SCpnt);
2005*dc417545SBart Van Assche 
20061da177e4SLinus Torvalds 	fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
20071da177e4SLinus Torvalds 		   "request sense complete, result=0x%04x%02x%02x",
2008*dc417545SBart Van Assche 		   result, scsi_pointer->Message, scsi_pointer->Status);
20091da177e4SLinus Torvalds 
2010*dc417545SBart Van Assche 	if (result != DID_OK || scsi_pointer->Status != SAM_STAT_GOOD)
20111da177e4SLinus Torvalds 		/*
20121da177e4SLinus Torvalds 		 * Something went wrong.  Make sure that we don't
20131da177e4SLinus Torvalds 		 * have valid data in the sense buffer that could
20141da177e4SLinus Torvalds 		 * confuse the higher levels.
20151da177e4SLinus Torvalds 		 */
201696d5eaa9SArnd Bergmann 		memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
20171da177e4SLinus Torvalds //printk("scsi%d.%c: sense buffer: ", info->host->host_no, '0' + SCpnt->device->id);
20181da177e4SLinus Torvalds //{ int i; for (i = 0; i < 32; i++) printk("%02x ", SCpnt->sense_buffer[i]); printk("\n"); }
20191da177e4SLinus Torvalds 	/*
20201da177e4SLinus Torvalds 	 * Note that we don't set SCpnt->result, since that should
20211da177e4SLinus Torvalds 	 * reflect the status of the command that we were asked by
20221da177e4SLinus Torvalds 	 * the upper layers to process.  This would have been set
20231da177e4SLinus Torvalds 	 * correctly by fas216_std_done.
20241da177e4SLinus Torvalds 	 */
2025ccf9ea91SBoaz Harrosh 	scsi_eh_restore_cmnd(SCpnt, &info->ses);
2026696fec18SBart Van Assche 	fas216_cmd_priv(SCpnt)->scsi_done(SCpnt);
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds /**
20301da177e4SLinus Torvalds  * fas216_std_done - finish processing of standard command
20311da177e4SLinus Torvalds  * @info: interface that completed
20321da177e4SLinus Torvalds  * @SCpnt: command that completed
20331da177e4SLinus Torvalds  * @result: driver byte of result
20341da177e4SLinus Torvalds  *
20351da177e4SLinus Torvalds  * Finish processing of standard command
20361da177e4SLinus Torvalds  */
20371da177e4SLinus Torvalds static void
fas216_std_done(FAS216_Info * info,struct scsi_cmnd * SCpnt,unsigned int result)2038ee0ca6baSHenne fas216_std_done(FAS216_Info *info, struct scsi_cmnd *SCpnt, unsigned int result)
20391da177e4SLinus Torvalds {
2040*dc417545SBart Van Assche 	struct scsi_pointer *scsi_pointer = arm_scsi_pointer(SCpnt);
2041*dc417545SBart Van Assche 
20421da177e4SLinus Torvalds 	info->stats.fins += 1;
20431da177e4SLinus Torvalds 
20447f33f56bSHannes Reinecke 	set_host_byte(SCpnt, result);
20457f33f56bSHannes Reinecke 	if (result == DID_OK)
20467f33f56bSHannes Reinecke 		scsi_msg_to_host_byte(SCpnt, info->scsi.SCp.Message);
20477f33f56bSHannes Reinecke 	set_status_byte(SCpnt, info->scsi.SCp.Status);
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 	fas216_log_command(info, LOG_CONNECT, SCpnt,
20501da177e4SLinus Torvalds 		"command complete, result=0x%08x", SCpnt->result);
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	/*
20531da177e4SLinus Torvalds 	 * If the driver detected an error, we're all done.
20541da177e4SLinus Torvalds 	 */
20557f33f56bSHannes Reinecke 	if (get_host_byte(SCpnt) != DID_OK)
20561da177e4SLinus Torvalds 		goto done;
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	/*
20591da177e4SLinus Torvalds 	 * If the command returned CHECK_CONDITION or COMMAND_TERMINATED
20601da177e4SLinus Torvalds 	 * status, request the sense information.
20611da177e4SLinus Torvalds 	 */
2062df130314SHannes Reinecke 	if (get_status_byte(SCpnt) == SAM_STAT_CHECK_CONDITION ||
2063df130314SHannes Reinecke 	    get_status_byte(SCpnt) == SAM_STAT_COMMAND_TERMINATED)
20641da177e4SLinus Torvalds 		goto request_sense;
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	/*
20671da177e4SLinus Torvalds 	 * If the command did not complete with GOOD status,
20681da177e4SLinus Torvalds 	 * we are all done here.
20691da177e4SLinus Torvalds 	 */
2070df130314SHannes Reinecke 	if (get_status_byte(SCpnt) != SAM_STAT_GOOD)
20711da177e4SLinus Torvalds 		goto done;
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 	/*
20741da177e4SLinus Torvalds 	 * We have successfully completed a command.  Make sure that
20751da177e4SLinus Torvalds 	 * we do not have any buffers left to transfer.  The world
20761da177e4SLinus Torvalds 	 * is not perfect, and we seem to occasionally hit this.
20771da177e4SLinus Torvalds 	 * It can be indicative of a buggy driver, target or the upper
20781da177e4SLinus Torvalds 	 * levels of the SCSI code.
20791da177e4SLinus Torvalds 	 */
20801da177e4SLinus Torvalds 	if (info->scsi.SCp.ptr) {
20811da177e4SLinus Torvalds 		switch (SCpnt->cmnd[0]) {
20821da177e4SLinus Torvalds 		case INQUIRY:
20831da177e4SLinus Torvalds 		case START_STOP:
20841da177e4SLinus Torvalds 		case MODE_SENSE:
20851da177e4SLinus Torvalds 			break;
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 		default:
2088e04ca907SHannes Reinecke 			scmd_printk(KERN_ERR, SCpnt,
2089e04ca907SHannes Reinecke 				    "incomplete data transfer detected: res=%08X ptr=%p len=%X\n",
20901da177e4SLinus Torvalds 				    SCpnt->result, info->scsi.SCp.ptr,
20911da177e4SLinus Torvalds 				    info->scsi.SCp.this_residual);
2092e04ca907SHannes Reinecke 			scsi_print_command(SCpnt);
2093279c43b3SHannes Reinecke 			set_host_byte(SCpnt, DID_ERROR);
20941da177e4SLinus Torvalds 			goto request_sense;
20951da177e4SLinus Torvalds 		}
20961da177e4SLinus Torvalds 	}
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds done:
2099696fec18SBart Van Assche 	if (fas216_cmd_priv(SCpnt)->scsi_done) {
2100696fec18SBart Van Assche 		fas216_cmd_priv(SCpnt)->scsi_done(SCpnt);
21011da177e4SLinus Torvalds 		return;
21021da177e4SLinus Torvalds 	}
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds 	panic("scsi%d.H: null scsi_done function in fas216_done",
21051da177e4SLinus Torvalds 		info->host->host_no);
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds request_sense:
21091da177e4SLinus Torvalds 	if (SCpnt->cmnd[0] == REQUEST_SENSE)
21101da177e4SLinus Torvalds 		goto done;
21111da177e4SLinus Torvalds 
2112ccf9ea91SBoaz Harrosh 	scsi_eh_prep_cmnd(SCpnt, &info->ses, NULL, 0, ~0);
21131da177e4SLinus Torvalds 	fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
21141da177e4SLinus Torvalds 			  "requesting sense");
2115ccf9ea91SBoaz Harrosh 	init_SCp(SCpnt);
2116*dc417545SBart Van Assche 	scsi_pointer->Message = 0;
2117*dc417545SBart Van Assche 	scsi_pointer->Status = 0;
21181da177e4SLinus Torvalds 	SCpnt->host_scribble = (void *)fas216_rq_sns_done;
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds 	/*
21211da177e4SLinus Torvalds 	 * Place this command into the high priority "request
21221da177e4SLinus Torvalds 	 * sense" slot.  This will be the very next command
21231da177e4SLinus Torvalds 	 * executed, unless a target connects to us.
21241da177e4SLinus Torvalds 	 */
21251da177e4SLinus Torvalds 	if (info->reqSCpnt)
212625985edcSLucas De Marchi 		printk(KERN_WARNING "scsi%d.%c: losing request command\n",
21271da177e4SLinus Torvalds 			info->host->host_no, '0' + SCpnt->device->id);
21281da177e4SLinus Torvalds 	info->reqSCpnt = SCpnt;
21291da177e4SLinus Torvalds }
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds /**
21321da177e4SLinus Torvalds  * fas216_done - complete processing for current command
21331da177e4SLinus Torvalds  * @info: interface that completed
21341da177e4SLinus Torvalds  * @result: driver byte of result
21351da177e4SLinus Torvalds  *
21361da177e4SLinus Torvalds  * Complete processing for current command
21371da177e4SLinus Torvalds  */
fas216_done(FAS216_Info * info,unsigned int result)21381da177e4SLinus Torvalds static void fas216_done(FAS216_Info *info, unsigned int result)
21391da177e4SLinus Torvalds {
2140ee0ca6baSHenne 	void (*fn)(FAS216_Info *, struct scsi_cmnd *, unsigned int);
2141ee0ca6baSHenne 	struct scsi_cmnd *SCpnt;
21421da177e4SLinus Torvalds 	unsigned long flags;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	fas216_checkmagic(info);
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	if (!info->SCpnt)
21471da177e4SLinus Torvalds 		goto no_command;
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	SCpnt = info->SCpnt;
21501da177e4SLinus Torvalds 	info->SCpnt = NULL;
21511da177e4SLinus Torvalds     	info->scsi.phase = PHASE_IDLE;
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds 	if (info->scsi.aborting) {
21541da177e4SLinus Torvalds 		fas216_log(info, 0, "uncaught abort - returning DID_ABORT");
21551da177e4SLinus Torvalds 		result = DID_ABORT;
21561da177e4SLinus Torvalds 		info->scsi.aborting = 0;
21571da177e4SLinus Torvalds 	}
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	/*
21601da177e4SLinus Torvalds 	 * Sanity check the completion - if we have zero bytes left
21611da177e4SLinus Torvalds 	 * to transfer, we should not have a valid pointer.
21621da177e4SLinus Torvalds 	 */
21631da177e4SLinus Torvalds 	if (info->scsi.SCp.ptr && info->scsi.SCp.this_residual == 0) {
2164e04ca907SHannes Reinecke 		scmd_printk(KERN_INFO, SCpnt,
2165e04ca907SHannes Reinecke 			    "zero bytes left to transfer, but buffer pointer still valid: ptr=%p len=%08x\n",
21661da177e4SLinus Torvalds 			    info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
21671da177e4SLinus Torvalds 		info->scsi.SCp.ptr = NULL;
2168e04ca907SHannes Reinecke 		scsi_print_command(SCpnt);
21691da177e4SLinus Torvalds 	}
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	/*
21721da177e4SLinus Torvalds 	 * Clear down this command as completed.  If we need to request
21731da177e4SLinus Torvalds 	 * the sense information, fas216_kick will re-assert the busy
21741da177e4SLinus Torvalds 	 * status.
21751da177e4SLinus Torvalds 	 */
21761da177e4SLinus Torvalds 	info->device[SCpnt->device->id].parity_check = 0;
21779cb78c16SHannes Reinecke 	clear_bit(SCpnt->device->id * 8 +
21789cb78c16SHannes Reinecke 		  (u8)(SCpnt->device->lun & 0x7), info->busyluns);
21791da177e4SLinus Torvalds 
2180ee0ca6baSHenne 	fn = (void (*)(FAS216_Info *, struct scsi_cmnd *, unsigned int))SCpnt->host_scribble;
21811da177e4SLinus Torvalds 	fn(info, SCpnt, result);
21821da177e4SLinus Torvalds 
218341569e37SRussell King 	if (info->scsi.irq) {
21841da177e4SLinus Torvalds 		spin_lock_irqsave(&info->host_lock, flags);
21851da177e4SLinus Torvalds 		if (info->scsi.phase == PHASE_IDLE)
21861da177e4SLinus Torvalds 			fas216_kick(info);
21871da177e4SLinus Torvalds 		spin_unlock_irqrestore(&info->host_lock, flags);
21881da177e4SLinus Torvalds 	}
21891da177e4SLinus Torvalds 	return;
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds no_command:
21921da177e4SLinus Torvalds 	panic("scsi%d.H: null command in fas216_done",
21931da177e4SLinus Torvalds 		info->host->host_no);
21941da177e4SLinus Torvalds }
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds /**
21970feb3429SBart Van Assche  * fas216_queue_command_internal - queue a command for the adapter to process
21981da177e4SLinus Torvalds  * @SCpnt: Command to queue
21991da177e4SLinus Torvalds  * @done: done function to call once command is complete
22001da177e4SLinus Torvalds  *
22011da177e4SLinus Torvalds  * Queue a command for adapter to process.
22021da177e4SLinus Torvalds  * Returns: 0 on success, else error.
22031da177e4SLinus Torvalds  * Notes: io_request_lock is held, interrupts are disabled.
22041da177e4SLinus Torvalds  */
fas216_queue_command_internal(struct scsi_cmnd * SCpnt,void (* done)(struct scsi_cmnd *))22050feb3429SBart Van Assche static int fas216_queue_command_internal(struct scsi_cmnd *SCpnt,
2206ee0ca6baSHenne 					 void (*done)(struct scsi_cmnd *))
22071da177e4SLinus Torvalds {
22081da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
22091da177e4SLinus Torvalds 	int result;
22101da177e4SLinus Torvalds 
22111da177e4SLinus Torvalds 	fas216_checkmagic(info);
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	fas216_log_command(info, LOG_CONNECT, SCpnt,
22141da177e4SLinus Torvalds 			   "received command (%p)", SCpnt);
22151da177e4SLinus Torvalds 
2216696fec18SBart Van Assche 	fas216_cmd_priv(SCpnt)->scsi_done = done;
22171da177e4SLinus Torvalds 	SCpnt->host_scribble = (void *)fas216_std_done;
22181da177e4SLinus Torvalds 	SCpnt->result = 0;
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 	init_SCp(SCpnt);
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	info->stats.queues += 1;
22231da177e4SLinus Torvalds 
22241da177e4SLinus Torvalds 	spin_lock(&info->host_lock);
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	/*
22271da177e4SLinus Torvalds 	 * Add command into execute queue and let it complete under
22281da177e4SLinus Torvalds 	 * whatever scheme we're using.
22291da177e4SLinus Torvalds 	 */
22301da177e4SLinus Torvalds 	result = !queue_add_cmd_ordered(&info->queues.issue, SCpnt);
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	/*
22331da177e4SLinus Torvalds 	 * If we successfully added the command,
22341da177e4SLinus Torvalds 	 * kick the interface to get it moving.
22351da177e4SLinus Torvalds 	 */
22361da177e4SLinus Torvalds 	if (result == 0 && info->scsi.phase == PHASE_IDLE)
22371da177e4SLinus Torvalds 		fas216_kick(info);
22381da177e4SLinus Torvalds 	spin_unlock(&info->host_lock);
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	fas216_log_target(info, LOG_CONNECT, -1, "queue %s",
22411da177e4SLinus Torvalds 		result ? "failure" : "success");
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 	return result;
22441da177e4SLinus Torvalds }
22451da177e4SLinus Torvalds 
fas216_queue_command_lck(struct scsi_cmnd * SCpnt)2246af049dfdSBart Van Assche static int fas216_queue_command_lck(struct scsi_cmnd *SCpnt)
22470feb3429SBart Van Assche {
2248af049dfdSBart Van Assche 	return fas216_queue_command_internal(SCpnt, scsi_done);
22490feb3429SBart Van Assche }
22500feb3429SBart Van Assche 
DEF_SCSI_QCMD(fas216_queue_command)2251f281233dSJeff Garzik DEF_SCSI_QCMD(fas216_queue_command)
2252f281233dSJeff Garzik 
22531da177e4SLinus Torvalds /**
22541da177e4SLinus Torvalds  * fas216_internal_done - trigger restart of a waiting thread in fas216_noqueue_command
22551da177e4SLinus Torvalds  * @SCpnt: Command to wake
22561da177e4SLinus Torvalds  *
22571da177e4SLinus Torvalds  * Trigger restart of a waiting thread in fas216_command
22581da177e4SLinus Torvalds  */
2259ee0ca6baSHenne static void fas216_internal_done(struct scsi_cmnd *SCpnt)
22601da177e4SLinus Torvalds {
22611da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds 	fas216_checkmagic(info);
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	info->internal_done = 1;
22661da177e4SLinus Torvalds }
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds /**
22691da177e4SLinus Torvalds  * fas216_noqueue_command - process a command for the adapter.
22701da177e4SLinus Torvalds  * @SCpnt: Command to queue
22711da177e4SLinus Torvalds  *
22721da177e4SLinus Torvalds  * Queue a command for adapter to process.
22731da177e4SLinus Torvalds  * Returns: scsi result code.
22741da177e4SLinus Torvalds  * Notes: io_request_lock is held, interrupts are disabled.
22751da177e4SLinus Torvalds  */
fas216_noqueue_command_lck(struct scsi_cmnd * SCpnt)2276af049dfdSBart Van Assche static int fas216_noqueue_command_lck(struct scsi_cmnd *SCpnt)
22771da177e4SLinus Torvalds {
22781da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 	fas216_checkmagic(info);
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 	/*
22831da177e4SLinus Torvalds 	 * We should only be using this if we don't have an interrupt.
22841da177e4SLinus Torvalds 	 * Provide some "incentive" to use the queueing code.
22851da177e4SLinus Torvalds 	 */
228641569e37SRussell King 	BUG_ON(info->scsi.irq);
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	info->internal_done = 0;
22890feb3429SBart Van Assche 	fas216_queue_command_internal(SCpnt, fas216_internal_done);
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds 	/*
22921da177e4SLinus Torvalds 	 * This wastes time, since we can't return until the command is
22931da177e4SLinus Torvalds 	 * complete. We can't sleep either since we may get re-entered!
22941da177e4SLinus Torvalds 	 * However, we must re-enable interrupts, or else we'll be
22951da177e4SLinus Torvalds 	 * waiting forever.
22961da177e4SLinus Torvalds 	 */
22971da177e4SLinus Torvalds 	spin_unlock_irq(info->host->host_lock);
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 	while (!info->internal_done) {
23001da177e4SLinus Torvalds 		/*
23011da177e4SLinus Torvalds 		 * If we don't have an IRQ, then we must poll the card for
23021da177e4SLinus Torvalds 		 * it's interrupt, and use that to call this driver's
23031da177e4SLinus Torvalds 		 * interrupt routine.  That way, we keep the command
230425985edcSLucas De Marchi 		 * progressing.  Maybe we can add some intelligence here
23051da177e4SLinus Torvalds 		 * and go to sleep if we know that the device is going
23061da177e4SLinus Torvalds 		 * to be some time (eg, disconnected).
23071da177e4SLinus Torvalds 		 */
23081da177e4SLinus Torvalds 		if (fas216_readb(info, REG_STAT) & STAT_INT) {
23091da177e4SLinus Torvalds 			spin_lock_irq(info->host->host_lock);
23101da177e4SLinus Torvalds 			fas216_intr(info);
23111da177e4SLinus Torvalds 			spin_unlock_irq(info->host->host_lock);
23121da177e4SLinus Torvalds 		}
23131da177e4SLinus Torvalds 	}
23141da177e4SLinus Torvalds 
23151da177e4SLinus Torvalds 	spin_lock_irq(info->host->host_lock);
23161da177e4SLinus Torvalds 
2317af049dfdSBart Van Assche 	scsi_done(SCpnt);
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds 	return 0;
23201da177e4SLinus Torvalds }
23211da177e4SLinus Torvalds 
DEF_SCSI_QCMD(fas216_noqueue_command)2322f281233dSJeff Garzik DEF_SCSI_QCMD(fas216_noqueue_command)
2323f281233dSJeff Garzik 
23241da177e4SLinus Torvalds /*
23251da177e4SLinus Torvalds  * Error handler timeout function.  Indicate that we timed out,
23261da177e4SLinus Torvalds  * and wake up any error handler process so it can continue.
23271da177e4SLinus Torvalds  */
2328e99e88a9SKees Cook static void fas216_eh_timer(struct timer_list *t)
23291da177e4SLinus Torvalds {
2330e99e88a9SKees Cook 	FAS216_Info *info = from_timer(info, t, eh_timer);
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "error handling timed out\n");
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	del_timer(&info->eh_timer);
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	if (info->rst_bus_status == 0)
23371da177e4SLinus Torvalds 		info->rst_bus_status = -1;
23381da177e4SLinus Torvalds 	if (info->rst_dev_status == 0)
23391da177e4SLinus Torvalds 		info->rst_dev_status = -1;
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	wake_up(&info->eh_wait);
23421da177e4SLinus Torvalds }
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds enum res_find {
23451da177e4SLinus Torvalds 	res_failed,		/* not found			*/
23461da177e4SLinus Torvalds 	res_success,		/* command on issue queue	*/
23471da177e4SLinus Torvalds 	res_hw_abort		/* command on disconnected dev	*/
23481da177e4SLinus Torvalds };
23491da177e4SLinus Torvalds 
23501da177e4SLinus Torvalds /**
23511da177e4SLinus Torvalds  * fas216_do_abort - decide how to abort a command
23521da177e4SLinus Torvalds  * @SCpnt: command to abort
23531da177e4SLinus Torvalds  *
23541da177e4SLinus Torvalds  * Decide how to abort a command.
23551da177e4SLinus Torvalds  * Returns: abort status
23561da177e4SLinus Torvalds  */
fas216_find_command(FAS216_Info * info,struct scsi_cmnd * SCpnt)2357ee0ca6baSHenne static enum res_find fas216_find_command(FAS216_Info *info,
2358ee0ca6baSHenne 					 struct scsi_cmnd *SCpnt)
23591da177e4SLinus Torvalds {
23601da177e4SLinus Torvalds 	enum res_find res = res_failed;
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	if (queue_remove_cmd(&info->queues.issue, SCpnt)) {
23631da177e4SLinus Torvalds 		/*
23641da177e4SLinus Torvalds 		 * The command was on the issue queue, and has not been
23651da177e4SLinus Torvalds 		 * issued yet.  We can remove the command from the queue,
23661da177e4SLinus Torvalds 		 * and acknowledge the abort.  Neither the device nor the
23671da177e4SLinus Torvalds 		 * interface know about the command.
23681da177e4SLinus Torvalds 		 */
23691da177e4SLinus Torvalds 		printk("on issue queue ");
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 		res = res_success;
23721da177e4SLinus Torvalds 	} else if (queue_remove_cmd(&info->queues.disconnected, SCpnt)) {
23731da177e4SLinus Torvalds 		/*
23741da177e4SLinus Torvalds 		 * The command was on the disconnected queue.  We must
23751da177e4SLinus Torvalds 		 * reconnect with the device if possible, and send it
23761da177e4SLinus Torvalds 		 * an abort message.
23771da177e4SLinus Torvalds 		 */
23781da177e4SLinus Torvalds 		printk("on disconnected queue ");
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 		res = res_hw_abort;
23811da177e4SLinus Torvalds 	} else if (info->SCpnt == SCpnt) {
23821da177e4SLinus Torvalds 		printk("executing ");
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 		switch (info->scsi.phase) {
23851da177e4SLinus Torvalds 		/*
23861da177e4SLinus Torvalds 		 * If the interface is idle, and the command is 'disconnectable',
23871da177e4SLinus Torvalds 		 * then it is the same as on the disconnected queue.
23881da177e4SLinus Torvalds 		 */
23891da177e4SLinus Torvalds 		case PHASE_IDLE:
23901da177e4SLinus Torvalds 			if (info->scsi.disconnectable) {
23911da177e4SLinus Torvalds 				info->scsi.disconnectable = 0;
23921da177e4SLinus Torvalds 				info->SCpnt = NULL;
23931da177e4SLinus Torvalds 				res = res_hw_abort;
23941da177e4SLinus Torvalds 			}
23951da177e4SLinus Torvalds 			break;
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds 		default:
23981da177e4SLinus Torvalds 			break;
23991da177e4SLinus Torvalds 		}
24001da177e4SLinus Torvalds 	} else if (info->origSCpnt == SCpnt) {
24011da177e4SLinus Torvalds 		/*
24021da177e4SLinus Torvalds 		 * The command will be executed next, but a command
24031da177e4SLinus Torvalds 		 * is currently using the interface.  This is similar to
24041da177e4SLinus Torvalds 		 * being on the issue queue, except the busylun bit has
24051da177e4SLinus Torvalds 		 * been set.
24061da177e4SLinus Torvalds 		 */
24071da177e4SLinus Torvalds 		info->origSCpnt = NULL;
24089cb78c16SHannes Reinecke 		clear_bit(SCpnt->device->id * 8 +
24099cb78c16SHannes Reinecke 			  (u8)(SCpnt->device->lun & 0x7), info->busyluns);
24101da177e4SLinus Torvalds 		printk("waiting for execution ");
24111da177e4SLinus Torvalds 		res = res_success;
24121da177e4SLinus Torvalds 	} else
24131da177e4SLinus Torvalds 		printk("unknown ");
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 	return res;
24161da177e4SLinus Torvalds }
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds /**
24191da177e4SLinus Torvalds  * fas216_eh_abort - abort this command
24201da177e4SLinus Torvalds  * @SCpnt: command to abort
24211da177e4SLinus Torvalds  *
24221da177e4SLinus Torvalds  * Abort this command.
24231da177e4SLinus Torvalds  * Returns: FAILED if unable to abort
24241da177e4SLinus Torvalds  * Notes: io_request_lock is taken, and irqs are disabled
24251da177e4SLinus Torvalds  */
fas216_eh_abort(struct scsi_cmnd * SCpnt)2426ee0ca6baSHenne int fas216_eh_abort(struct scsi_cmnd *SCpnt)
24271da177e4SLinus Torvalds {
24281da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
24291da177e4SLinus Torvalds 	int result = FAILED;
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 	fas216_checkmagic(info);
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	info->stats.aborts += 1;
24341da177e4SLinus Torvalds 
24351fa6b5fbSHannes Reinecke 	scmd_printk(KERN_WARNING, SCpnt, "abort command\n");
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 	print_debug_list();
24381da177e4SLinus Torvalds 	fas216_dumpstate(info);
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds 	switch (fas216_find_command(info, SCpnt)) {
24411da177e4SLinus Torvalds 	/*
24421da177e4SLinus Torvalds 	 * We found the command, and cleared it out.  Either
24431da177e4SLinus Torvalds 	 * the command is still known to be executing on the
24441da177e4SLinus Torvalds 	 * target, or the busylun bit is not set.
24451da177e4SLinus Torvalds 	 */
24461da177e4SLinus Torvalds 	case res_success:
24471fa6b5fbSHannes Reinecke 		scmd_printk(KERN_WARNING, SCpnt, "abort %p success\n", SCpnt);
24481da177e4SLinus Torvalds 		result = SUCCESS;
24491da177e4SLinus Torvalds 		break;
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	/*
24521da177e4SLinus Torvalds 	 * We need to reconnect to the target and send it an
24531da177e4SLinus Torvalds 	 * ABORT or ABORT_TAG message.  We can only do this
24541da177e4SLinus Torvalds 	 * if the bus is free.
24551da177e4SLinus Torvalds 	 */
24561da177e4SLinus Torvalds 	case res_hw_abort:
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds 	/*
24591da177e4SLinus Torvalds 	 * We are unable to abort the command for some reason.
24601da177e4SLinus Torvalds 	 */
24611da177e4SLinus Torvalds 	default:
24621da177e4SLinus Torvalds 	case res_failed:
24631fa6b5fbSHannes Reinecke 		scmd_printk(KERN_WARNING, SCpnt, "abort %p failed\n", SCpnt);
24641da177e4SLinus Torvalds 		break;
24651da177e4SLinus Torvalds 	}
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds 	return result;
24681da177e4SLinus Torvalds }
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds /**
24711da177e4SLinus Torvalds  * fas216_eh_device_reset - Reset the device associated with this command
24721da177e4SLinus Torvalds  * @SCpnt: command specifing device to reset
24731da177e4SLinus Torvalds  *
24741da177e4SLinus Torvalds  * Reset the device associated with this command.
24751da177e4SLinus Torvalds  * Returns: FAILED if unable to reset.
24761da177e4SLinus Torvalds  * Notes: We won't be re-entered, so we'll only have one device
24771da177e4SLinus Torvalds  * reset on the go at one time.
24781da177e4SLinus Torvalds  */
fas216_eh_device_reset(struct scsi_cmnd * SCpnt)2479ee0ca6baSHenne int fas216_eh_device_reset(struct scsi_cmnd *SCpnt)
24801da177e4SLinus Torvalds {
24811da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
24821da177e4SLinus Torvalds 	unsigned long flags;
24831da177e4SLinus Torvalds 	int i, res = FAILED, target = SCpnt->device->id;
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "device reset for target %d", target);
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds 	spin_lock_irqsave(&info->host_lock, flags);
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds 	do {
24901da177e4SLinus Torvalds 		/*
24911da177e4SLinus Torvalds 		 * If we are currently connected to a device, and
24921da177e4SLinus Torvalds 		 * it is the device we want to reset, there is
24931da177e4SLinus Torvalds 		 * nothing we can do here.  Chances are it is stuck,
24941da177e4SLinus Torvalds 		 * and we need a bus reset.
24951da177e4SLinus Torvalds 		 */
24961da177e4SLinus Torvalds 		if (info->SCpnt && !info->scsi.disconnectable &&
24971da177e4SLinus Torvalds 		    info->SCpnt->device->id == SCpnt->device->id)
24981da177e4SLinus Torvalds 			break;
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds 		/*
25011da177e4SLinus Torvalds 		 * We're going to be resetting this device.  Remove
25021da177e4SLinus Torvalds 		 * all pending commands from the driver.  By doing
25031da177e4SLinus Torvalds 		 * so, we guarantee that we won't touch the command
25041da177e4SLinus Torvalds 		 * structures except to process the reset request.
25051da177e4SLinus Torvalds 		 */
25061da177e4SLinus Torvalds 		queue_remove_all_target(&info->queues.issue, target);
25071da177e4SLinus Torvalds 		queue_remove_all_target(&info->queues.disconnected, target);
25081da177e4SLinus Torvalds 		if (info->origSCpnt && info->origSCpnt->device->id == target)
25091da177e4SLinus Torvalds 			info->origSCpnt = NULL;
25101da177e4SLinus Torvalds 		if (info->reqSCpnt && info->reqSCpnt->device->id == target)
25111da177e4SLinus Torvalds 			info->reqSCpnt = NULL;
25121da177e4SLinus Torvalds 		for (i = 0; i < 8; i++)
25131da177e4SLinus Torvalds 			clear_bit(target * 8 + i, info->busyluns);
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds 		/*
25161da177e4SLinus Torvalds 		 * Hijack this SCSI command structure to send
25171da177e4SLinus Torvalds 		 * a bus device reset message to this device.
25181da177e4SLinus Torvalds 		 */
25191da177e4SLinus Torvalds 		SCpnt->host_scribble = (void *)fas216_devicereset_done;
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 		info->rst_dev_status = 0;
25221da177e4SLinus Torvalds 		info->rstSCpnt = SCpnt;
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds 		if (info->scsi.phase == PHASE_IDLE)
25251da177e4SLinus Torvalds 			fas216_kick(info);
25261da177e4SLinus Torvalds 
2527b857df1aSGuennadi Liakhovetski 		mod_timer(&info->eh_timer, jiffies + 30 * HZ);
25281da177e4SLinus Torvalds 		spin_unlock_irqrestore(&info->host_lock, flags);
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 		/*
25311da177e4SLinus Torvalds 		 * Wait up to 30 seconds for the reset to complete.
25321da177e4SLinus Torvalds 		 */
25331da177e4SLinus Torvalds 		wait_event(info->eh_wait, info->rst_dev_status);
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 		del_timer_sync(&info->eh_timer);
25361da177e4SLinus Torvalds 		spin_lock_irqsave(&info->host_lock, flags);
25371da177e4SLinus Torvalds 		info->rstSCpnt = NULL;
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 		if (info->rst_dev_status == 1)
25401da177e4SLinus Torvalds 			res = SUCCESS;
25411da177e4SLinus Torvalds 	} while (0);
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds 	SCpnt->host_scribble = NULL;
25441da177e4SLinus Torvalds 	spin_unlock_irqrestore(&info->host_lock, flags);
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "device reset complete: %s\n",
25471da177e4SLinus Torvalds 		   res == SUCCESS ? "success" : "failed");
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	return res;
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds /**
25531da177e4SLinus Torvalds  * fas216_eh_bus_reset - Reset the bus associated with the command
25541da177e4SLinus Torvalds  * @SCpnt: command specifing bus to reset
25551da177e4SLinus Torvalds  *
25561da177e4SLinus Torvalds  * Reset the bus associated with the command.
25571da177e4SLinus Torvalds  * Returns: FAILED if unable to reset.
25581da177e4SLinus Torvalds  * Notes: Further commands are blocked.
25591da177e4SLinus Torvalds  */
fas216_eh_bus_reset(struct scsi_cmnd * SCpnt)2560ee0ca6baSHenne int fas216_eh_bus_reset(struct scsi_cmnd *SCpnt)
25611da177e4SLinus Torvalds {
25621da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
25631da177e4SLinus Torvalds 	unsigned long flags;
2564f64a181dSChristoph Hellwig 	struct scsi_device *SDpnt;
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	fas216_checkmagic(info);
25671da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "resetting bus");
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	info->stats.bus_resets += 1;
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds 	spin_lock_irqsave(&info->host_lock, flags);
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds 	/*
25741da177e4SLinus Torvalds 	 * Stop all activity on this interface.
25751da177e4SLinus Torvalds 	 */
25761da177e4SLinus Torvalds 	fas216_aborttransfer(info);
25771da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds 	/*
25801da177e4SLinus Torvalds 	 * Clear any pending interrupts.
25811da177e4SLinus Torvalds 	 */
25821da177e4SLinus Torvalds 	while (fas216_readb(info, REG_STAT) & STAT_INT)
25831da177e4SLinus Torvalds 		fas216_readb(info, REG_INST);
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds 	info->rst_bus_status = 0;
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	/*
25881da177e4SLinus Torvalds 	 * For each attached hard-reset device, clear out
25891da177e4SLinus Torvalds 	 * all command structures.  Leave the running
25901da177e4SLinus Torvalds 	 * command in place.
25911da177e4SLinus Torvalds 	 */
25921da177e4SLinus Torvalds 	shost_for_each_device(SDpnt, info->host) {
25931da177e4SLinus Torvalds 		int i;
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds 		if (SDpnt->soft_reset)
25961da177e4SLinus Torvalds 			continue;
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 		queue_remove_all_target(&info->queues.issue, SDpnt->id);
25991da177e4SLinus Torvalds 		queue_remove_all_target(&info->queues.disconnected, SDpnt->id);
26001da177e4SLinus Torvalds 		if (info->origSCpnt && info->origSCpnt->device->id == SDpnt->id)
26011da177e4SLinus Torvalds 			info->origSCpnt = NULL;
26021da177e4SLinus Torvalds 		if (info->reqSCpnt && info->reqSCpnt->device->id == SDpnt->id)
26031da177e4SLinus Torvalds 			info->reqSCpnt = NULL;
26041da177e4SLinus Torvalds 		info->SCpnt = NULL;
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 		for (i = 0; i < 8; i++)
26071da177e4SLinus Torvalds 			clear_bit(SDpnt->id * 8 + i, info->busyluns);
26081da177e4SLinus Torvalds 	}
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 	info->scsi.phase = PHASE_IDLE;
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds 	/*
26131da177e4SLinus Torvalds 	 * Reset the SCSI bus.  Device cleanup happens in
26141da177e4SLinus Torvalds 	 * the interrupt handler.
26151da177e4SLinus Torvalds 	 */
26161da177e4SLinus Torvalds 	fas216_cmd(info, CMD_RESETSCSI);
26171da177e4SLinus Torvalds 
26181da177e4SLinus Torvalds 	mod_timer(&info->eh_timer, jiffies + HZ);
26191da177e4SLinus Torvalds 	spin_unlock_irqrestore(&info->host_lock, flags);
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds 	/*
26221da177e4SLinus Torvalds 	 * Wait one second for the interrupt.
26231da177e4SLinus Torvalds 	 */
26241da177e4SLinus Torvalds 	wait_event(info->eh_wait, info->rst_bus_status);
26251da177e4SLinus Torvalds 	del_timer_sync(&info->eh_timer);
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds 	fas216_log(info, LOG_ERROR, "bus reset complete: %s\n",
26281da177e4SLinus Torvalds 		   info->rst_bus_status == 1 ? "success" : "failed");
26291da177e4SLinus Torvalds 
26301da177e4SLinus Torvalds 	return info->rst_bus_status == 1 ? SUCCESS : FAILED;
26311da177e4SLinus Torvalds }
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds /**
26341da177e4SLinus Torvalds  * fas216_init_chip - Initialise FAS216 state after reset
26351da177e4SLinus Torvalds  * @info: state structure for interface
26361da177e4SLinus Torvalds  *
26371da177e4SLinus Torvalds  * Initialise FAS216 state after reset
26381da177e4SLinus Torvalds  */
fas216_init_chip(FAS216_Info * info)26391da177e4SLinus Torvalds static void fas216_init_chip(FAS216_Info *info)
26401da177e4SLinus Torvalds {
26411da177e4SLinus Torvalds 	unsigned int clock = ((info->ifcfg.clockrate - 1) / 5 + 1) & 7;
26421da177e4SLinus Torvalds 	fas216_writeb(info, REG_CLKF, clock);
26431da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
26441da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL2, info->scsi.cfg[1]);
26451da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
26461da177e4SLinus Torvalds 	fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
26471da177e4SLinus Torvalds 	fas216_writeb(info, REG_SOF, 0);
26481da177e4SLinus Torvalds 	fas216_writeb(info, REG_STP, info->scsi.async_stp);
26491da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
26501da177e4SLinus Torvalds }
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds /**
26531da177e4SLinus Torvalds  * fas216_eh_host_reset - Reset the host associated with this command
26541da177e4SLinus Torvalds  * @SCpnt: command specifing host to reset
26551da177e4SLinus Torvalds  *
26561da177e4SLinus Torvalds  * Reset the host associated with this command.
26571da177e4SLinus Torvalds  * Returns: FAILED if unable to reset.
26581da177e4SLinus Torvalds  * Notes: io_request_lock is taken, and irqs are disabled
26591da177e4SLinus Torvalds  */
fas216_eh_host_reset(struct scsi_cmnd * SCpnt)2660ee0ca6baSHenne int fas216_eh_host_reset(struct scsi_cmnd *SCpnt)
26611da177e4SLinus Torvalds {
26621da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
26631da177e4SLinus Torvalds 
2664df0ae249SJeff Garzik  	spin_lock_irq(info->host->host_lock);
2665df0ae249SJeff Garzik  
26661da177e4SLinus Torvalds 	fas216_checkmagic(info);
26671da177e4SLinus Torvalds 
2668e04ca907SHannes Reinecke 	fas216_log(info, LOG_ERROR, "resetting host");
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds 	/*
26711da177e4SLinus Torvalds 	 * Reset the SCSI chip.
26721da177e4SLinus Torvalds 	 */
26731da177e4SLinus Torvalds 	fas216_cmd(info, CMD_RESETCHIP);
26741da177e4SLinus Torvalds 
26751da177e4SLinus Torvalds 	/*
26761da177e4SLinus Torvalds 	 * Ugly ugly ugly!
26771da177e4SLinus Torvalds 	 * We need to release the host_lock and enable
26781da177e4SLinus Torvalds 	 * IRQs if we sleep, but we must relock and disable
26791da177e4SLinus Torvalds 	 * IRQs after the sleep.
26801da177e4SLinus Torvalds 	 */
26811da177e4SLinus Torvalds 	spin_unlock_irq(info->host->host_lock);
26821da177e4SLinus Torvalds 	msleep(50 * 1000/100);
26831da177e4SLinus Torvalds 	spin_lock_irq(info->host->host_lock);
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds 	/*
26861da177e4SLinus Torvalds 	 * Release the SCSI reset.
26871da177e4SLinus Torvalds 	 */
26881da177e4SLinus Torvalds 	fas216_cmd(info, CMD_NOP);
26891da177e4SLinus Torvalds 
26901da177e4SLinus Torvalds 	fas216_init_chip(info);
26911da177e4SLinus Torvalds 
2692df0ae249SJeff Garzik  	spin_unlock_irq(info->host->host_lock);
26931da177e4SLinus Torvalds 	return SUCCESS;
26941da177e4SLinus Torvalds }
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds #define TYPE_UNKNOWN	0
26971da177e4SLinus Torvalds #define TYPE_NCR53C90	1
26981da177e4SLinus Torvalds #define TYPE_NCR53C90A	2
26991da177e4SLinus Torvalds #define TYPE_NCR53C9x	3
27001da177e4SLinus Torvalds #define TYPE_Am53CF94	4
27011da177e4SLinus Torvalds #define TYPE_EmFAS216	5
27021da177e4SLinus Torvalds #define TYPE_QLFAS216	6
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds static char *chip_types[] = {
27051da177e4SLinus Torvalds 	"unknown",
27061da177e4SLinus Torvalds 	"NS NCR53C90",
27071da177e4SLinus Torvalds 	"NS NCR53C90A",
27081da177e4SLinus Torvalds 	"NS NCR53C9x",
27091da177e4SLinus Torvalds 	"AMD Am53CF94",
27101da177e4SLinus Torvalds 	"Emulex FAS216",
27111da177e4SLinus Torvalds 	"QLogic FAS216"
27121da177e4SLinus Torvalds };
27131da177e4SLinus Torvalds 
fas216_detect_type(FAS216_Info * info)27141da177e4SLinus Torvalds static int fas216_detect_type(FAS216_Info *info)
27151da177e4SLinus Torvalds {
27161da177e4SLinus Torvalds 	int family, rev;
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds 	/*
27191da177e4SLinus Torvalds 	 * Reset the chip.
27201da177e4SLinus Torvalds 	 */
27211da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
27221da177e4SLinus Torvalds 	udelay(50);
27231da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_NOP);
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds 	/*
27261da177e4SLinus Torvalds 	 * Check to see if control reg 2 is present.
27271da177e4SLinus Torvalds 	 */
27281da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 0);
27291da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL2, CNTL2_S2FE);
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 	/*
27321da177e4SLinus Torvalds 	 * If we are unable to read back control reg 2
27331da177e4SLinus Torvalds 	 * correctly, it is not present, and we have a
27341da177e4SLinus Torvalds 	 * NCR53C90.
27351da177e4SLinus Torvalds 	 */
27361da177e4SLinus Torvalds 	if ((fas216_readb(info, REG_CNTL2) & (~0xe0)) != CNTL2_S2FE)
27371da177e4SLinus Torvalds 		return TYPE_NCR53C90;
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds 	/*
27401da177e4SLinus Torvalds 	 * Now, check control register 3
27411da177e4SLinus Torvalds 	 */
27421da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL2, 0);
27431da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 0);
27441da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 5);
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 	/*
27471da177e4SLinus Torvalds 	 * If we are unable to read the register back
27481da177e4SLinus Torvalds 	 * correctly, we have a NCR53C90A
27491da177e4SLinus Torvalds 	 */
27501da177e4SLinus Torvalds 	if (fas216_readb(info, REG_CNTL3) != 5)
27511da177e4SLinus Torvalds 		return TYPE_NCR53C90A;
27521da177e4SLinus Torvalds 
27531da177e4SLinus Torvalds 	/*
27541da177e4SLinus Torvalds 	 * Now read the ID from the chip.
27551da177e4SLinus Torvalds 	 */
27561da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 0);
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, CNTL3_ADIDCHK);
27591da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL3, 0);
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
27621da177e4SLinus Torvalds 	udelay(50);
27631da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_WITHDMA | CMD_NOP);
27641da177e4SLinus Torvalds 
27651da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL2, CNTL2_ENF);
27661da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
27671da177e4SLinus Torvalds 	udelay(50);
27681da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_NOP);
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds 	rev     = fas216_readb(info, REG_ID);
27711da177e4SLinus Torvalds 	family  = rev >> 3;
27721da177e4SLinus Torvalds 	rev    &= 7;
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	switch (family) {
27751da177e4SLinus Torvalds 	case 0x01:
27761da177e4SLinus Torvalds 		if (rev == 4)
27771da177e4SLinus Torvalds 			return TYPE_Am53CF94;
27781da177e4SLinus Torvalds 		break;
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds 	case 0x02:
27811da177e4SLinus Torvalds 		switch (rev) {
27821da177e4SLinus Torvalds 		case 2:
27831da177e4SLinus Torvalds 			return TYPE_EmFAS216;
27841da177e4SLinus Torvalds 		case 3:
27851da177e4SLinus Torvalds 			return TYPE_QLFAS216;
27861da177e4SLinus Torvalds 		}
27871da177e4SLinus Torvalds 		break;
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 	default:
27901da177e4SLinus Torvalds 		break;
27911da177e4SLinus Torvalds 	}
27921da177e4SLinus Torvalds 	printk("family %x rev %x\n", family, rev);
27931da177e4SLinus Torvalds 	return TYPE_NCR53C9x;
27941da177e4SLinus Torvalds }
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds /**
27971da177e4SLinus Torvalds  * fas216_reset_state - Initialise driver internal state
27981da177e4SLinus Torvalds  * @info: state to initialise
27991da177e4SLinus Torvalds  *
28001da177e4SLinus Torvalds  * Initialise driver internal state
28011da177e4SLinus Torvalds  */
fas216_reset_state(FAS216_Info * info)28021da177e4SLinus Torvalds static void fas216_reset_state(FAS216_Info *info)
28031da177e4SLinus Torvalds {
28041da177e4SLinus Torvalds 	int i;
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds 	fas216_checkmagic(info);
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	fas216_bus_reset(info);
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 	/*
28111da177e4SLinus Torvalds 	 * Clear out all stale info in our state structure
28121da177e4SLinus Torvalds 	 */
28131da177e4SLinus Torvalds 	memset(info->busyluns, 0, sizeof(info->busyluns));
28141da177e4SLinus Torvalds 	info->scsi.disconnectable = 0;
28151da177e4SLinus Torvalds 	info->scsi.aborting = 0;
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
28181da177e4SLinus Torvalds 		info->device[i].parity_enabled	= 0;
28191da177e4SLinus Torvalds 		info->device[i].parity_check	= 1;
28201da177e4SLinus Torvalds 	}
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds 	/*
28231da177e4SLinus Torvalds 	 * Drain all commands on disconnected queue
28241da177e4SLinus Torvalds 	 */
28251da177e4SLinus Torvalds 	while (queue_remove(&info->queues.disconnected) != NULL);
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds 	/*
28281da177e4SLinus Torvalds 	 * Remove executing commands.
28291da177e4SLinus Torvalds 	 */
28301da177e4SLinus Torvalds 	info->SCpnt     = NULL;
28311da177e4SLinus Torvalds 	info->reqSCpnt  = NULL;
28321da177e4SLinus Torvalds 	info->rstSCpnt  = NULL;
28331da177e4SLinus Torvalds 	info->origSCpnt = NULL;
28341da177e4SLinus Torvalds }
28351da177e4SLinus Torvalds 
28361da177e4SLinus Torvalds /**
28371da177e4SLinus Torvalds  * fas216_init - initialise FAS/NCR/AMD SCSI structures.
28381da177e4SLinus Torvalds  * @host: a driver-specific filled-out structure
28391da177e4SLinus Torvalds  *
28401da177e4SLinus Torvalds  * Initialise FAS/NCR/AMD SCSI structures.
28411da177e4SLinus Torvalds  * Returns: 0 on success
28421da177e4SLinus Torvalds  */
fas216_init(struct Scsi_Host * host)28431da177e4SLinus Torvalds int fas216_init(struct Scsi_Host *host)
28441da177e4SLinus Torvalds {
28451da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)host->hostdata;
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds 	info->magic_start    = MAGIC;
28481da177e4SLinus Torvalds 	info->magic_end      = MAGIC;
28491da177e4SLinus Torvalds 	info->host           = host;
28501da177e4SLinus Torvalds 	info->scsi.cfg[0]    = host->this_id | CNTL1_PERE;
28511da177e4SLinus Torvalds 	info->scsi.cfg[1]    = CNTL2_ENF | CNTL2_S2FE;
28521da177e4SLinus Torvalds 	info->scsi.cfg[2]    = info->ifcfg.cntl3 |
28531da177e4SLinus Torvalds 			       CNTL3_ADIDCHK | CNTL3_QTAG | CNTL3_G2CB | CNTL3_LBTM;
28541da177e4SLinus Torvalds 	info->scsi.async_stp = fas216_syncperiod(info, info->ifcfg.asyncperiod);
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds 	info->rst_dev_status = -1;
28571da177e4SLinus Torvalds 	info->rst_bus_status = -1;
28581da177e4SLinus Torvalds 	init_waitqueue_head(&info->eh_wait);
2859e99e88a9SKees Cook 	timer_setup(&info->eh_timer, fas216_eh_timer, 0);
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds 	spin_lock_init(&info->host_lock);
28621da177e4SLinus Torvalds 
28631da177e4SLinus Torvalds 	memset(&info->stats, 0, sizeof(info->stats));
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds 	msgqueue_initialise(&info->scsi.msgs);
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds 	if (!queue_initialise(&info->queues.issue))
28681da177e4SLinus Torvalds 		return -ENOMEM;
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 	if (!queue_initialise(&info->queues.disconnected)) {
28711da177e4SLinus Torvalds 		queue_free(&info->queues.issue);
28721da177e4SLinus Torvalds 		return -ENOMEM;
28731da177e4SLinus Torvalds 	}
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 	return 0;
28761da177e4SLinus Torvalds }
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds /**
28791da177e4SLinus Torvalds  * fas216_add - initialise FAS/NCR/AMD SCSI ic.
28801da177e4SLinus Torvalds  * @host: a driver-specific filled-out structure
28811da177e4SLinus Torvalds  * @dev: parent device
28821da177e4SLinus Torvalds  *
28831da177e4SLinus Torvalds  * Initialise FAS/NCR/AMD SCSI ic.
28841da177e4SLinus Torvalds  * Returns: 0 on success
28851da177e4SLinus Torvalds  */
fas216_add(struct Scsi_Host * host,struct device * dev)28861da177e4SLinus Torvalds int fas216_add(struct Scsi_Host *host, struct device *dev)
28871da177e4SLinus Torvalds {
28881da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)host->hostdata;
28891da177e4SLinus Torvalds 	int type, ret;
28901da177e4SLinus Torvalds 
28911da177e4SLinus Torvalds 	if (info->ifcfg.clockrate <= 10 || info->ifcfg.clockrate > 40) {
28921da177e4SLinus Torvalds 		printk(KERN_CRIT "fas216: invalid clock rate %u MHz\n",
28931da177e4SLinus Torvalds 			info->ifcfg.clockrate);
28941da177e4SLinus Torvalds 		return -EINVAL;
28951da177e4SLinus Torvalds 	}
28961da177e4SLinus Torvalds 
28971da177e4SLinus Torvalds 	fas216_reset_state(info);
28981da177e4SLinus Torvalds 	type = fas216_detect_type(info);
28991da177e4SLinus Torvalds 	info->scsi.type = chip_types[type];
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	udelay(300);
29021da177e4SLinus Torvalds 
29031da177e4SLinus Torvalds 	/*
29041da177e4SLinus Torvalds 	 * Initialise the chip correctly.
29051da177e4SLinus Torvalds 	 */
29061da177e4SLinus Torvalds 	fas216_init_chip(info);
29071da177e4SLinus Torvalds 
29081da177e4SLinus Torvalds 	/*
29091da177e4SLinus Torvalds 	 * Reset the SCSI bus.  We don't want to see
29101da177e4SLinus Torvalds 	 * the resulting reset interrupt, so mask it
29111da177e4SLinus Torvalds 	 * out.
29121da177e4SLinus Torvalds 	 */
29131da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_DISR);
29141da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETSCSI);
29151da177e4SLinus Torvalds 
29161da177e4SLinus Torvalds 	/*
29171da177e4SLinus Torvalds 	 * scsi standard says wait 250ms
29181da177e4SLinus Torvalds 	 */
29191da177e4SLinus Torvalds 	spin_unlock_irq(info->host->host_lock);
29201da177e4SLinus Torvalds 	msleep(100*1000/100);
29211da177e4SLinus Torvalds 	spin_lock_irq(info->host->host_lock);
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
29241da177e4SLinus Torvalds 	fas216_readb(info, REG_INST);
29251da177e4SLinus Torvalds 
29261da177e4SLinus Torvalds 	fas216_checkmagic(info);
29271da177e4SLinus Torvalds 
29281da177e4SLinus Torvalds 	ret = scsi_add_host(host, dev);
29291da177e4SLinus Torvalds 	if (ret)
29301da177e4SLinus Torvalds 		fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
29311da177e4SLinus Torvalds 	else
29321da177e4SLinus Torvalds 		scsi_scan_host(host);
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds 	return ret;
29351da177e4SLinus Torvalds }
29361da177e4SLinus Torvalds 
fas216_remove(struct Scsi_Host * host)29371da177e4SLinus Torvalds void fas216_remove(struct Scsi_Host *host)
29381da177e4SLinus Torvalds {
29391da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)host->hostdata;
29401da177e4SLinus Torvalds 
29411da177e4SLinus Torvalds 	fas216_checkmagic(info);
29421da177e4SLinus Torvalds 	scsi_remove_host(host);
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds 	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
29451da177e4SLinus Torvalds 	scsi_host_put(host);
29461da177e4SLinus Torvalds }
29471da177e4SLinus Torvalds 
29481da177e4SLinus Torvalds /**
29491da177e4SLinus Torvalds  * fas216_release - release all resources for FAS/NCR/AMD SCSI ic.
29501da177e4SLinus Torvalds  * @host: a driver-specific filled-out structure
29511da177e4SLinus Torvalds  *
29521da177e4SLinus Torvalds  * release all resources and put everything to bed for FAS/NCR/AMD SCSI ic.
29531da177e4SLinus Torvalds  */
fas216_release(struct Scsi_Host * host)29541da177e4SLinus Torvalds void fas216_release(struct Scsi_Host *host)
29551da177e4SLinus Torvalds {
29561da177e4SLinus Torvalds 	FAS216_Info *info = (FAS216_Info *)host->hostdata;
29571da177e4SLinus Torvalds 
29581da177e4SLinus Torvalds 	queue_free(&info->queues.disconnected);
29591da177e4SLinus Torvalds 	queue_free(&info->queues.issue);
29601da177e4SLinus Torvalds }
29611da177e4SLinus Torvalds 
fas216_print_host(FAS216_Info * info,struct seq_file * m)29629d4e5c54SAl Viro void fas216_print_host(FAS216_Info *info, struct seq_file *m)
29631da177e4SLinus Torvalds {
29649d4e5c54SAl Viro 	seq_printf(m,
29651da177e4SLinus Torvalds 			"\n"
29661da177e4SLinus Torvalds 			"Chip    : %s\n"
29671da177e4SLinus Torvalds 			" Address: 0x%p\n"
29681da177e4SLinus Torvalds 			" IRQ    : %d\n"
29691da177e4SLinus Torvalds 			" DMA    : %d\n",
29701da177e4SLinus Torvalds 			info->scsi.type, info->scsi.io_base,
29711da177e4SLinus Torvalds 			info->scsi.irq, info->scsi.dma);
29721da177e4SLinus Torvalds }
29731da177e4SLinus Torvalds 
fas216_print_stats(FAS216_Info * info,struct seq_file * m)29749d4e5c54SAl Viro void fas216_print_stats(FAS216_Info *info, struct seq_file *m)
29751da177e4SLinus Torvalds {
29769d4e5c54SAl Viro 	seq_printf(m, "\n"
29771da177e4SLinus Torvalds 			"Command Statistics:\n"
29781da177e4SLinus Torvalds 			" Queued     : %u\n"
29791da177e4SLinus Torvalds 			" Issued     : %u\n"
29801da177e4SLinus Torvalds 			" Completed  : %u\n"
29811da177e4SLinus Torvalds 			" Reads      : %u\n"
29821da177e4SLinus Torvalds 			" Writes     : %u\n"
29831da177e4SLinus Torvalds 			" Others     : %u\n"
29841da177e4SLinus Torvalds 			" Disconnects: %u\n"
29851da177e4SLinus Torvalds 			" Aborts     : %u\n"
29861da177e4SLinus Torvalds 			" Bus resets : %u\n"
29871da177e4SLinus Torvalds 			" Host resets: %u\n",
29881da177e4SLinus Torvalds 			info->stats.queues,	 info->stats.removes,
29891da177e4SLinus Torvalds 			info->stats.fins,	 info->stats.reads,
29901da177e4SLinus Torvalds 			info->stats.writes,	 info->stats.miscs,
29911da177e4SLinus Torvalds 			info->stats.disconnects, info->stats.aborts,
29921da177e4SLinus Torvalds 			info->stats.bus_resets,	 info->stats.host_resets);
29931da177e4SLinus Torvalds }
29941da177e4SLinus Torvalds 
fas216_print_devices(FAS216_Info * info,struct seq_file * m)29959d4e5c54SAl Viro void fas216_print_devices(FAS216_Info *info, struct seq_file *m)
29961da177e4SLinus Torvalds {
29971da177e4SLinus Torvalds 	struct fas216_device *dev;
2998f64a181dSChristoph Hellwig 	struct scsi_device *scd;
29991da177e4SLinus Torvalds 
300091c40f24SRasmus Villemoes 	seq_puts(m, "Device/Lun TaggedQ       Parity   Sync\n");
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 	shost_for_each_device(scd, info->host) {
30031da177e4SLinus Torvalds 		dev = &info->device[scd->id];
30049cb78c16SHannes Reinecke 		seq_printf(m, "     %d/%llu   ", scd->id, scd->lun);
30051da177e4SLinus Torvalds 		if (scd->tagged_supported)
3006bc41fcbfSHannes Reinecke 			seq_printf(m, "%3sabled ",
3007bc41fcbfSHannes Reinecke 				     scd->simple_tags ? "en" : "dis");
30081da177e4SLinus Torvalds 		else
300991c40f24SRasmus Villemoes 			seq_puts(m, "unsupported   ");
30101da177e4SLinus Torvalds 
30119d4e5c54SAl Viro 		seq_printf(m, "%3sabled ", dev->parity_enabled ? "en" : "dis");
30121da177e4SLinus Torvalds 
30131da177e4SLinus Torvalds 		if (dev->sof)
30149d4e5c54SAl Viro 			seq_printf(m, "offset %d, %d ns\n",
30151da177e4SLinus Torvalds 				     dev->sof, dev->period * 4);
30161da177e4SLinus Torvalds 		else
301791c40f24SRasmus Villemoes 			seq_puts(m, "async\n");
30181da177e4SLinus Torvalds 	}
30191da177e4SLinus Torvalds }
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_init);
30221da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_add);
30231da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_queue_command);
30241da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_noqueue_command);
30251da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_intr);
30261da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_remove);
30271da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_release);
30281da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_eh_abort);
30291da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_eh_device_reset);
30301da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_eh_bus_reset);
30311da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_eh_host_reset);
30321da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_print_host);
30331da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_print_stats);
30341da177e4SLinus Torvalds EXPORT_SYMBOL(fas216_print_devices);
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds MODULE_AUTHOR("Russell King");
30371da177e4SLinus Torvalds MODULE_DESCRIPTION("Generic FAS216/NCR53C9x driver core");
30381da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3039