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