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