aboutsummaryrefslogtreecommitdiff
path: root/drivers/scsi/arm/fas216.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/arm/fas216.c')
-rw-r--r--drivers/scsi/arm/fas216.c3043
1 files changed, 3043 insertions, 0 deletions
diff --git a/drivers/scsi/arm/fas216.c b/drivers/scsi/arm/fas216.c
new file mode 100644
index 00000000000..5411e850c83
--- /dev/null
+++ b/drivers/scsi/arm/fas216.c
@@ -0,0 +1,3043 @@
+/*
+ * linux/drivers/acorn/scsi/fas216.c
+ *
+ * Copyright (C) 1997-2003 Russell King
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
+ * other sources, including:
+ * the AMD Am53CF94 data sheet
+ * the AMD Am53C94 data sheet
+ *
+ * This is a generic driver. To use it, have a look at cumana_2.c. You
+ * should define your own structure that overlays FAS216_Info, eg:
+ * struct my_host_data {
+ * FAS216_Info info;
+ * ... my host specific data ...
+ * };
+ *
+ * Changelog:
+ * 30-08-1997 RMK Created
+ * 14-09-1997 RMK Started disconnect support
+ * 08-02-1998 RMK Corrected real DMA support
+ * 15-02-1998 RMK Started sync xfer support
+ * 06-04-1998 RMK Tightened conditions for printing incomplete
+ * transfers
+ * 02-05-1998 RMK Added extra checks in fas216_reset
+ * 24-05-1998 RMK Fixed synchronous transfers with period >= 200ns
+ * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
+ * 26-08-1998 RMK Improved message support wrt MESSAGE_REJECT
+ * 02-04-2000 RMK Converted to use the new error handling, and
+ * automatically request sense data upon check
+ * condition status from targets.
+ */
+#include <linux/module.h>
+#include <linux/blkdev.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/ioport.h>
+#include <linux/sched.h>
+#include <linux/proc_fs.h>
+#include <linux/delay.h>
+#include <linux/bitops.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+
+#include <asm/dma.h>
+#include <asm/io.h>
+#include <asm/irq.h>
+#include <asm/ecard.h>
+
+#include "../scsi.h"
+#include <scsi/scsi_host.h>
+#include "fas216.h"
+#include "scsi.h"
+
+/* NOTE: SCSI2 Synchronous transfers *require* DMA according to
+ * the data sheet. This restriction is crazy, especially when
+ * you only want to send 16 bytes! What were the guys who
+ * designed this chip on at that time? Did they read the SCSI2
+ * spec at all? The following sections are taken from the SCSI2
+ * standard (s2r10) concerning this:
+ *
+ * > IMPLEMENTORS NOTES:
+ * > (1) Re-negotiation at every selection is not recommended, since a
+ * > significant performance impact is likely.
+ *
+ * > The implied synchronous agreement shall remain in effect until a BUS DEVICE
+ * > RESET message is received, until a hard reset condition occurs, or until one
+ * > of the two SCSI devices elects to modify the agreement. The default data
+ * > transfer mode is asynchronous data transfer mode. The default data transfer
+ * > mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
+ * > reset condition.
+ *
+ * In total, this means that once you have elected to use synchronous
+ * transfers, you must always use DMA.
+ *
+ * I was thinking that this was a good chip until I found this restriction ;(
+ */
+#define SCSI2_SYNC
+#undef SCSI2_TAG
+
+#undef DEBUG_CONNECT
+#undef DEBUG_MESSAGES
+
+#undef CHECK_STRUCTURE
+
+#define LOG_CONNECT (1 << 0)
+#define LOG_BUSSERVICE (1 << 1)
+#define LOG_FUNCTIONDONE (1 << 2)
+#define LOG_MESSAGES (1 << 3)
+#define LOG_BUFFER (1 << 4)
+#define LOG_ERROR (1 << 8)
+
+static int level_mask = LOG_ERROR;
+
+module_param(level_mask, int, 0644);
+
+static int __init fas216_log_setup(char *str)
+{
+ char *s;
+
+ level_mask = 0;
+
+ while ((s = strsep(&str, ",")) != NULL) {
+ switch (s[0]) {
+ case 'a':
+ if (strcmp(s, "all") == 0)
+ level_mask |= -1;
+ break;
+ case 'b':
+ if (strncmp(s, "bus", 3) == 0)
+ level_mask |= LOG_BUSSERVICE;
+ if (strncmp(s, "buf", 3) == 0)
+ level_mask |= LOG_BUFFER;
+ break;
+ case 'c':
+ level_mask |= LOG_CONNECT;
+ break;
+ case 'e':
+ level_mask |= LOG_ERROR;
+ break;
+ case 'm':
+ level_mask |= LOG_MESSAGES;
+ break;
+ case 'n':
+ if (strcmp(s, "none") == 0)
+ level_mask = 0;
+ break;
+ case 's':
+ level_mask |= LOG_FUNCTIONDONE;
+ break;
+ }
+ }
+ return 1;
+}
+
+__setup("fas216_logging=", fas216_log_setup);
+
+static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
+{
+ unsigned int off = reg << info->scsi.io_shift;
+ return readb(info->scsi.io_base + off);
+}
+
+static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
+{
+ unsigned int off = reg << info->scsi.io_shift;
+ writeb(val, info->scsi.io_base + off);
+}
+
+static void fas216_dumpstate(FAS216_Info *info)
+{
+ unsigned char is, stat, inst;
+
+ is = fas216_readb(info, REG_IS);
+ stat = fas216_readb(info, REG_STAT);
+ inst = fas216_readb(info, REG_INST);
+
+ printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
+ " INST=%02X IS=%02X CFIS=%02X",
+ fas216_readb(info, REG_CTCL),
+ fas216_readb(info, REG_CTCM),
+ fas216_readb(info, REG_CMD), stat, inst, is,
+ fas216_readb(info, REG_CFIS));
+ printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
+ fas216_readb(info, REG_CNTL1),
+ fas216_readb(info, REG_CNTL2),
+ fas216_readb(info, REG_CNTL3),
+ fas216_readb(info, REG_CTCH));
+}
+
+static void print_SCp(Scsi_Pointer *SCp, const char *prefix, const char *suffix)
+{
+ printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
+ prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
+ SCp->buffers_residual, suffix);
+}
+
+static void fas216_dumpinfo(FAS216_Info *info)
+{
+ static int used = 0;
+ int i;
+
+ if (used++)
+ return;
+
+ printk("FAS216_Info=\n");
+ printk(" { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
+ info->magic_start, info->host, info->SCpnt,
+ info->origSCpnt);
+ printk(" scsi={ io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
+ info->scsi.io_shift, info->scsi.irq,
+ info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
+ info->scsi.cfg[3]);
+ printk(" type=%p phase=%X\n",
+ info->scsi.type, info->scsi.phase);
+ print_SCp(&info->scsi.SCp, " SCp={ ", " }\n");
+ printk(" msgs async_stp=%X disconnectable=%d aborting=%d }\n",
+ info->scsi.async_stp,
+ info->scsi.disconnectable, info->scsi.aborting);
+ printk(" stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
+ " disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
+ info->stats.queues, info->stats.removes, info->stats.fins,
+ info->stats.reads, info->stats.writes, info->stats.miscs,
+ info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
+ info->stats.host_resets);
+ printk(" ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
+ info->ifcfg.clockrate, info->ifcfg.select_timeout,
+ info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
+ for (i = 0; i < 8; i++) {
+ printk(" busyluns[%d]=%08lx dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
+ i, info->busyluns[i], i,
+ info->device[i].disconnect_ok, info->device[i].stp,
+ info->device[i].sof, info->device[i].sync_state);
+ }
+ printk(" dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
+ info->dma.transfer_type, info->dma.setup,
+ info->dma.pseudo, info->dma.stop);
+ printk(" internal_done=%X magic_end=%lX }\n",
+ info->internal_done, info->magic_end);
+}
+
+#ifdef CHECK_STRUCTURE
+static void __fas216_checkmagic(FAS216_Info *info, const char *func)
+{
+ int corruption = 0;
+ if (info->magic_start != MAGIC) {
+ printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
+ corruption++;
+ }
+ if (info->magic_end != MAGIC) {
+ printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
+ corruption++;
+ }
+ if (corruption) {
+ fas216_dumpinfo(info);
+ panic("scsi memory space corrupted in %s", func);
+ }
+}
+#define fas216_checkmagic(info) __fas216_checkmagic((info), __FUNCTION__)
+#else
+#define fas216_checkmagic(info)
+#endif
+
+static const char *fas216_bus_phase(int stat)
+{
+ static const char *phases[] = {
+ "DATA OUT", "DATA IN",
+ "COMMAND", "STATUS",
+ "MISC OUT", "MISC IN",
+ "MESG OUT", "MESG IN"
+ };
+
+ return phases[stat & STAT_BUSMASK];
+}
+
+static const char *fas216_drv_phase(FAS216_Info *info)
+{
+ static const char *phases[] = {
+ [PHASE_IDLE] = "idle",
+ [PHASE_SELECTION] = "selection",
+ [PHASE_COMMAND] = "command",
+ [PHASE_DATAOUT] = "data out",
+ [PHASE_DATAIN] = "data in",
+ [PHASE_MSGIN] = "message in",
+ [PHASE_MSGIN_DISCONNECT]= "disconnect",
+ [PHASE_MSGOUT_EXPECT] = "expect message out",
+ [PHASE_MSGOUT] = "message out",
+ [PHASE_STATUS] = "status",
+ [PHASE_DONE] = "done",
+ };
+
+ if (info->scsi.phase < ARRAY_SIZE(phases) &&
+ phases[info->scsi.phase])
+ return phases[info->scsi.phase];
+ return "???";
+}
+
+static char fas216_target(FAS216_Info *info)
+{
+ if (info->SCpnt)
+ return '0' + info->SCpnt->device->id;
+ else
+ return 'H';
+}
+
+static void
+fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
+{
+ static char buf[1024];
+
+ vsnprintf(buf, sizeof(buf), fmt, ap);
+ printk("scsi%d.%c: %s", info->host->host_no, target, buf);
+}
+
+static void
+fas216_log_command(FAS216_Info *info, int level, Scsi_Cmnd *SCpnt, char *fmt, ...)
+{
+ va_list args;
+
+ if (level != 0 && !(level & level_mask))
+ return;
+
+ va_start(args, fmt);
+ fas216_do_log(info, '0' + SCpnt->device->id, fmt, args);
+ va_end(args);
+
+ printk(" CDB: ");
+ print_command(SCpnt->cmnd);
+}
+
+static void
+fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
+{
+ va_list args;
+
+ if (level != 0 && !(level & level_mask))
+ return;
+
+ if (target < 0)
+ target = 'H';
+ else
+ target += '0';
+
+ va_start(args, fmt);
+ fas216_do_log(info, target, fmt, args);
+ va_end(args);
+
+ printk("\n");
+}
+
+static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
+{
+ va_list args;
+
+ if (level != 0 && !(level & level_mask))
+ return;
+
+ va_start(args, fmt);
+ fas216_do_log(info, fas216_target(info), fmt, args);
+ va_end(args);
+
+ printk("\n");
+}
+
+#define PH_SIZE 32
+
+static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
+static int ph_ptr;
+
+static void add_debug_list(int stat, int ssr, int isr, int ph)
+{
+ ph_list[ph_ptr].stat = stat;
+ ph_list[ph_ptr].ssr = ssr;
+ ph_list[ph_ptr].isr = isr;
+ ph_list[ph_ptr].ph = ph;
+
+ ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
+}
+
+static struct { int command; void *from; } cmd_list[8];
+static int cmd_ptr;
+
+static void fas216_cmd(FAS216_Info *info, unsigned int command)
+{
+ cmd_list[cmd_ptr].command = command;
+ cmd_list[cmd_ptr].from = __builtin_return_address(0);
+
+ cmd_ptr = (cmd_ptr + 1) & 7;
+
+ fas216_writeb(info, REG_CMD, command);
+}
+
+static void print_debug_list(void)
+{
+ int i;
+
+ i = ph_ptr;
+
+ printk(KERN_ERR "SCSI IRQ trail\n");
+ do {
+ printk(" %02x:%02x:%02x:%1x",
+ ph_list[i].stat, ph_list[i].ssr,
+ ph_list[i].isr, ph_list[i].ph);
+ i = (i + 1) & (PH_SIZE - 1);
+ if (((i ^ ph_ptr) & 7) == 0)
+ printk("\n");
+ } while (i != ph_ptr);
+ if ((i ^ ph_ptr) & 7)
+ printk("\n");
+
+ i = cmd_ptr;
+ printk(KERN_ERR "FAS216 commands: ");
+ do {
+ printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
+ i = (i + 1) & 7;
+ } while (i != cmd_ptr);
+ printk("\n");
+}
+
+static void fas216_done(FAS216_Info *info, unsigned int result);
+
+/**
+ * fas216_get_last_msg - retrive last message from the list
+ * @info: interface to search
+ * @pos: current fifo position
+ *
+ * Retrieve a last message from the list, using position in fifo.
+ */
+static inline unsigned short
+fas216_get_last_msg(FAS216_Info *info, int pos)
+{
+ unsigned short packed_msg = NOP;
+ struct message *msg;
+ int msgnr = 0;
+
+ while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
+ if (pos >= msg->fifo)
+ break;
+ }
+
+ if (msg) {
+ if (msg->msg[0] == EXTENDED_MESSAGE)
+ packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
+ else
+ packed_msg = msg->msg[0];
+ }
+
+ fas216_log(info, LOG_MESSAGES,
+ "Message: %04x found at position %02x\n", packed_msg, pos);
+
+ return packed_msg;
+}
+
+/**
+ * fas216_syncperiod - calculate STP register value
+ * @info: state structure for interface connected to device
+ * @ns: period in ns (between subsequent bytes)
+ *
+ * Calculate value to be loaded into the STP register for a given period
+ * in ns. Returns a value suitable for REG_STP.
+ */
+static int fas216_syncperiod(FAS216_Info *info, int ns)
+{
+ int value = (info->ifcfg.clockrate * ns) / 1000;
+
+ fas216_checkmagic(info);
+
+ if (value < 4)
+ value = 4;
+ else if (value > 35)
+ value = 35;
+
+ return value & 31;
+}
+
+/**
+ * fas216_set_sync - setup FAS216 chip for specified transfer period.
+ * @info: state structure for interface connected to device
+ * @target: target
+ *
+ * Correctly setup FAS216 chip for specified transfer period.
+ * Notes : we need to switch the chip out of FASTSCSI mode if we have
+ * a transfer period >= 200ns - otherwise the chip will violate
+ * the SCSI timings.
+ */
+static void fas216_set_sync(FAS216_Info *info, int target)
+{
+ unsigned int cntl3;
+
+ fas216_writeb(info, REG_SOF, info->device[target].sof);
+ fas216_writeb(info, REG_STP, info->device[target].stp);
+
+ cntl3 = info->scsi.cfg[2];
+ if (info->device[target].period >= (200 / 4))
+ cntl3 = cntl3 & ~CNTL3_FASTSCSI;
+
+ fas216_writeb(info, REG_CNTL3, cntl3);
+}
+
+/* Synchronous transfer support
+ *
+ * Note: The SCSI II r10 spec says (5.6.12):
+ *
+ * (2) Due to historical problems with early host adapters that could
+ * not accept an SDTR message, some targets may not initiate synchronous
+ * negotiation after a power cycle as required by this standard. Host
+ * adapters that support synchronous mode may avoid the ensuing failure
+ * modes when the target is independently power cycled by initiating a
+ * synchronous negotiation on each REQUEST SENSE and INQUIRY command.
+ * This approach increases the SCSI bus overhead and is not recommended
+ * for new implementations. The correct method is to respond to an
+ * SDTR message with a MESSAGE REJECT message if the either the
+ * initiator or target devices does not support synchronous transfers
+ * or does not want to negotiate for synchronous transfers at the time.
+ * Using the correct method assures compatibility with wide data
+ * transfers and future enhancements.
+ *
+ * We will always initiate a synchronous transfer negotiation request on
+ * every INQUIRY or REQUEST SENSE message, unless the target itself has
+ * at some point performed a synchronous transfer negotiation request, or
+ * we have synchronous transfers disabled for this device.
+ */
+
+/**
+ * fas216_handlesync - Handle a synchronous transfer message
+ * @info: state structure for interface
+ * @msg: message from target
+ *
+ * Handle a synchronous transfer message from the target
+ */
+static void fas216_handlesync(FAS216_Info *info, char *msg)
+{
+ struct fas216_device *dev = &info->device[info->SCpnt->device->id];
+ enum { sync, async, none, reject } res = none;
+
+#ifdef SCSI2_SYNC
+ switch (msg[0]) {
+ case MESSAGE_REJECT:
+ /* Synchronous transfer request failed.
+ * Note: SCSI II r10:
+ *
+ * SCSI devices that are capable of synchronous
+ * data transfers shall not respond to an SDTR
+ * message with a MESSAGE REJECT message.
+ *
+ * Hence, if we get this condition, we disable
+ * negotiation for this device.
+ */
+ if (dev->sync_state == neg_inprogress) {
+ dev->sync_state = neg_invalid;
+ res = async;
+ }
+ break;
+
+ case EXTENDED_MESSAGE:
+ switch (dev->sync_state) {
+ /* We don't accept synchronous transfer requests.
+ * Respond with a MESSAGE_REJECT to prevent a
+ * synchronous transfer agreement from being reached.
+ */
+ case neg_invalid:
+ res = reject;
+ break;
+
+ /* We were not negotiating a synchronous transfer,
+ * but the device sent us a negotiation request.
+ * Honour the request by sending back a SDTR
+ * message containing our capability, limited by
+ * the targets capability.
+ */
+ default:
+ fas216_cmd(info, CMD_SETATN);
+ if (msg[4] > info->ifcfg.sync_max_depth)
+ msg[4] = info->ifcfg.sync_max_depth;
+ if (msg[3] < 1000 / info->ifcfg.clockrate)
+ msg[3] = 1000 / info->ifcfg.clockrate;
+
+ msgqueue_flush(&info->scsi.msgs);
+ msgqueue_addmsg(&info->scsi.msgs, 5,
+ EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
+ msg[3], msg[4]);
+ info->scsi.phase = PHASE_MSGOUT_EXPECT;
+
+ /* This is wrong. The agreement is not in effect
+ * until this message is accepted by the device
+ */
+ dev->sync_state = neg_targcomplete;
+ res = sync;
+ break;
+
+ /* We initiated the synchronous transfer negotiation,
+ * and have successfully received a response from the
+ * target. The synchronous transfer agreement has been
+ * reached. Note: if the values returned are out of our
+ * bounds, we must reject the message.
+ */
+ case neg_inprogress:
+ res = reject;
+ if (msg[4] <= info->ifcfg.sync_max_depth &&
+ msg[3] >= 1000 / info->ifcfg.clockrate) {
+ dev->sync_state = neg_complete;
+ res = sync;
+ }
+ break;
+ }
+ }
+#else
+ res = reject;
+#endif
+
+ switch (res) {
+ case sync:
+ dev->period = msg[3];
+ dev->sof = msg[4];
+ dev->stp = fas216_syncperiod(info, msg[3] * 4);
+ fas216_set_sync(info, info->SCpnt->device->id);
+ break;
+
+ case reject:
+ fas216_cmd(info, CMD_SETATN);
+ msgqueue_flush(&info->scsi.msgs);
+ msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
+ info->scsi.phase = PHASE_MSGOUT_EXPECT;
+
+ case async:
+ dev->period = info->ifcfg.asyncperiod / 4;
+ dev->sof = 0;
+ dev->stp = info->scsi.async_stp;
+ fas216_set_sync(info, info->SCpnt->device->id);
+ break;
+
+ case none:
+ break;
+ }
+}
+
+/**
+ * fas216_updateptrs - update data pointers after transfer suspended/paused
+ * @info: interface's local pointer to update
+ * @bytes_transferred: number of bytes transferred
+ *
+ * Update data pointers after transfer suspended/paused
+ */
+static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
+{
+ Scsi_Pointer *SCp = &info->scsi.SCp;
+
+ fas216_checkmagic(info);
+
+ BUG_ON(bytes_transferred < 0);
+
+ info->SCpnt->request_bufflen -= bytes_transferred;
+
+ while (bytes_transferred != 0) {
+ if (SCp->this_residual > bytes_transferred)
+ break;
+ /*
+ * We have used up this buffer. Move on to the
+ * next buffer.
+ */
+ bytes_transferred -= SCp->this_residual;
+ if (!next_SCp(SCp) && bytes_transferred) {
+ printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
+ info->host->host_no, '0' + info->SCpnt->device->id);
+ return;
+ }
+ }
+
+ SCp->this_residual -= bytes_transferred;
+ if (SCp->this_residual)
+ SCp->ptr += bytes_transferred;
+ else
+ SCp->ptr = NULL;
+}
+
+/**
+ * fas216_pio - transfer data off of/on to card using programmed IO
+ * @info: interface to transfer data to/from
+ * @direction: direction to transfer data (DMA_OUT/DMA_IN)
+ *
+ * Transfer data off of/on to card using programmed IO.
+ * Notes: this is incredibly slow.
+ */
+static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
+{
+ Scsi_Pointer *SCp = &info->scsi.SCp;
+
+ fas216_checkmagic(info);
+
+ if (direction == DMA_OUT)
+ fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
+ else
+ put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
+
+ if (SCp->this_residual == 0)
+ next_SCp(SCp);
+}
+
+static void fas216_set_stc(FAS216_Info *info, unsigned int length)
+{
+ fas216_writeb(info, REG_STCL, length);
+ fas216_writeb(info, REG_STCM, length >> 8);
+ fas216_writeb(info, REG_STCH, length >> 16);
+}
+
+static unsigned int fas216_get_ctc(FAS216_Info *info)
+{
+ return fas216_readb(info, REG_CTCL) +
+ (fas216_readb(info, REG_CTCM) << 8) +
+ (fas216_readb(info, REG_CTCH) << 16);
+}
+
+/**
+ * fas216_cleanuptransfer - clean up after a transfer has completed.
+ * @info: interface to clean up
+ *
+ * Update the data pointers according to the number of bytes transferred
+ * on the SCSI bus.
+ */
+static void fas216_cleanuptransfer(FAS216_Info *info)
+{
+ unsigned long total, residual, fifo;
+ fasdmatype_t dmatype = info->dma.transfer_type;
+
+ info->dma.transfer_type = fasdma_none;
+
+ /*
+ * PIO transfers do not need to be cleaned up.
+ */
+ if (dmatype == fasdma_pio || dmatype == fasdma_none)
+ return;
+
+ if (dmatype == fasdma_real_all)
+ total = info->SCpnt->request_bufflen;
+ else
+ total = info->scsi.SCp.this_residual;
+
+ residual = fas216_get_ctc(info);
+
+ fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
+
+ fas216_log(info, LOG_BUFFER, "cleaning up from previous "
+ "transfer: length 0x%06x, residual 0x%x, fifo %d",
+ total, residual, fifo);
+
+ /*
+ * If we were performing Data-Out, the transfer counter
+ * counts down each time a byte is transferred by the
+ * host to the FIFO. This means we must include the
+ * bytes left in the FIFO from the transfer counter.
+ */
+ if (info->scsi.phase == PHASE_DATAOUT)
+ residual += fifo;
+
+ fas216_updateptrs(info, total - residual);
+}
+
+/**
+ * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
+ * @info: interface from which device disconnected from
+ *
+ * Start a DMA/PIO transfer off of/on to card
+ */
+static void fas216_transfer(FAS216_Info *info)
+{
+ fasdmadir_t direction;
+ fasdmatype_t dmatype;
+
+ fas216_log(info, LOG_BUFFER,
+ "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
+ info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
+ info->SCpnt->request_bufflen);
+
+ if (!info->scsi.SCp.ptr) {
+ fas216_log(info, LOG_ERROR, "null buffer passed to "
+ "fas216_starttransfer");
+ print_SCp(&info->scsi.SCp, "SCp: ", "\n");
+ print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
+ return;
+ }
+
+ /*
+ * If we have a synchronous transfer agreement in effect, we must
+ * use DMA mode. If we are using asynchronous transfers, we may
+ * use DMA mode or PIO mode.
+ */
+ if (info->device[info->SCpnt->device->id].sof)
+ dmatype = fasdma_real_all;
+ else
+ dmatype = fasdma_pio;
+
+ if (info->scsi.phase == PHASE_DATAOUT)
+ direction = DMA_OUT;
+ else
+ direction = DMA_IN;
+
+ if (info->dma.setup)
+ dmatype = info->dma.setup(info->host, &info->scsi.SCp,
+ direction, dmatype);
+ info->dma.transfer_type = dmatype;
+
+ if (dmatype == fasdma_real_all)
+ fas216_set_stc(info, info->SCpnt->request_bufflen);
+ else
+ fas216_set_stc(info, info->scsi.SCp.this_residual);
+
+ switch (dmatype) {
+ case fasdma_pio:
+ fas216_log(info, LOG_BUFFER, "PIO transfer");
+ fas216_writeb(info, REG_SOF, 0);
+ fas216_writeb(info, REG_STP, info->scsi.async_stp);
+ fas216_cmd(info, CMD_TRANSFERINFO);
+ fas216_pio(info, direction);
+ break;
+
+ case fasdma_pseudo:
+ fas216_log(info, LOG_BUFFER, "pseudo transfer");
+ fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
+ info->dma.pseudo(info->host, &info->scsi.SCp,
+ direction, info->SCpnt->transfersize);
+ break;
+
+ case fasdma_real_block:
+ fas216_log(info, LOG_BUFFER, "block dma transfer");
+ fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
+ break;
+
+ case fasdma_real_all:
+ fas216_log(info, LOG_BUFFER, "total dma transfer");
+ fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
+ break;
+
+ default:
+ fas216_log(info, LOG_BUFFER | LOG_ERROR,
+ "invalid FAS216 DMA type");
+ break;
+ }
+}
+
+/**
+ * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
+ * @info: interface from which device disconnected from
+ *
+ * Called when we switch away from DATA IN or DATA OUT phases.
+ */
+static void fas216_stoptransfer(FAS216_Info *info)
+{
+ fas216_checkmagic(info);
+
+ if (info->dma.transfer_type == fasdma_real_all ||
+ info->dma.transfer_type == fasdma_real_block)
+ info->dma.stop(info->host, &info->scsi.SCp);
+
+ fas216_cleanuptransfer(info);
+
+ if (info->scsi.phase == PHASE_DATAIN) {
+ unsigned int fifo;
+
+ /*
+ * If we were performing Data-In, then the FIFO counter
+ * contains the number of bytes not transferred via DMA
+ * from the on-board FIFO. Read them manually.
+ */
+ fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
+ while (fifo && info->scsi.SCp.ptr) {
+ *info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
+ fas216_updateptrs(info, 1);
+ fifo--;
+ }
+ } else {
+ /*
+ * After a Data-Out phase, there may be unsent
+ * bytes left in the FIFO. Flush them out.
+ */
+ fas216_cmd(info, CMD_FLUSHFIFO);
+ }
+}
+
+static void fas216_aborttransfer(FAS216_Info *info)
+{
+ fas216_checkmagic(info);
+
+ if (info->dma.transfer_type == fasdma_real_all ||
+ info->dma.transfer_type == fasdma_real_block)
+ info->dma.stop(info->host, &info->scsi.SCp);
+
+ info->dma.transfer_type = fasdma_none;
+ fas216_cmd(info, CMD_FLUSHFIFO);
+}
+
+static void fas216_kick(FAS216_Info *info);
+
+/**
+ * fas216_disconnected_intr - handle device disconnection
+ * @info: interface from which device disconnected from
+ *
+ * Handle device disconnection
+ */
+static void fas216_disconnect_intr(FAS216_Info *info)
+{
+ unsigned long flags;
+
+ fas216_checkmagic(info);
+
+ fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
+ info->scsi.phase);
+
+ msgqueue_flush(&info->scsi.msgs);
+
+ switch (info->scsi.phase) {
+ case PHASE_SELECTION: /* while selecting - no target */
+ case PHASE_SELSTEPS:
+ fas216_done(info, DID_NO_CONNECT);
+ break;
+
+ case PHASE_MSGIN_DISCONNECT: /* message in - disconnecting */
+ info->scsi.disconnectable = 1;
+ info->scsi.phase = PHASE_IDLE;
+ info->stats.disconnects += 1;
+ spin_lock_irqsave(&info->host_lock, flags);
+ if (info->scsi.phase == PHASE_IDLE)
+ fas216_kick(info);
+ spin_unlock_irqrestore(&info->host_lock, flags);
+ break;
+
+ case PHASE_DONE: /* at end of command - complete */
+ fas216_done(info, DID_OK);
+ break;
+
+ case PHASE_MSGOUT: /* message out - possible ABORT message */
+ if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
+ info->scsi.aborting = 0;
+ fas216_done(info, DID_ABORT);
+ break;
+ }
+
+ default: /* huh? */
+ printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
+ info->host->host_no, fas216_target(info), fas216_drv_phase(info));
+ print_debug_list();
+ fas216_stoptransfer(info);
+ fas216_done(info, DID_ERROR);
+ break;
+ }
+}
+
+/**
+ * fas216_reselected_intr - start reconnection of a device
+ * @info: interface which was reselected
+ *
+ * Start reconnection of a device
+ */
+static void
+fas216_reselected_intr(FAS216_Info *info)
+{
+ unsigned int cfis, i;
+ unsigned char msg[4];
+ unsigned char target, lun, tag;
+
+ fas216_checkmagic(info);
+
+ WARN_ON(info->scsi.phase == PHASE_SELECTION ||
+ info->scsi.phase == PHASE_SELSTEPS);
+
+ cfis = fas216_readb(info, REG_CFIS);
+
+ fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
+ info->scsi.phase, cfis);
+
+ cfis &= CFIS_CF;
+
+ if (cfis < 2 || cfis > 4) {
+ printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
+ info->host->host_no);
+ goto bad_message;
+ }
+
+ for (i = 0; i < cfis; i++)
+ msg[i] = fas216_readb(info, REG_FF);
+
+ if (!(msg[0] & (1 << info->host->this_id)) ||
+ !(msg[1] & 0x80))
+ goto initiator_error;
+
+ target = msg[0] & ~(1 << info->host->this_id);
+ target = ffs(target) - 1;
+ lun = msg[1] & 7;
+ tag = 0;
+
+ if (cfis >= 3) {
+ if (msg[2] != SIMPLE_QUEUE_TAG)
+ goto initiator_error;
+
+ tag = msg[3];
+ }
+
+ /* set up for synchronous transfers */
+ fas216_writeb(info, REG_SDID, target);
+ fas216_set_sync(info, target);
+ msgqueue_flush(&info->scsi.msgs);
+
+ fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
+ target, lun, tag);
+
+ if (info->scsi.disconnectable && info->SCpnt) {
+ info->scsi.disconnectable = 0;
+ if (info->SCpnt->device->id == target &&
+ info->SCpnt->device->lun == lun &&
+ info->SCpnt->tag == tag) {
+ fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
+ } else {
+ queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
+ fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
+ info->SCpnt = NULL;
+ }
+ }
+ if (!info->SCpnt) {
+ info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
+ target, lun, tag);
+ fas216_log(info, LOG_CONNECT, "had to get command");
+ }
+
+ if (info->SCpnt) {
+ /*
+ * Restore data pointer from SAVED data pointer
+ */
+ info->scsi.SCp = info->SCpnt->SCp;
+
+ fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
+ info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
+ info->scsi.phase = PHASE_MSGIN;
+ } else {
+ /*
+ * Our command structure not found - abort the
+ * command on the target. Since we have no
+ * record of this command, we can't send
+ * an INITIATOR DETECTED ERROR message.
+ */
+ fas216_cmd(info, CMD_SETATN);
+
+#if 0
+ if (tag)
+ msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
+ else
+#endif
+ msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
+ info->scsi.phase = PHASE_MSGOUT_EXPECT;
+ info->scsi.aborting = 1;
+ }
+
+ fas216_cmd(info, CMD_MSGACCEPTED);
+ return;
+
+ initiator_error:
+ printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
+ info->host->host_no);
+ for (i = 0; i < cfis; i++)
+ printk(" %02x", msg[i]);
+ printk("\n");
+ bad_message:
+ fas216_cmd(info, CMD_SETATN);
+ msgqueue_flush(&info->scsi.msgs);
+ msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
+ info->scsi.phase = PHASE_MSGOUT_EXPECT;
+ fas216_cmd(info, CMD_MSGACCEPTED);
+}
+
+static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
+{
+ int i;
+
+ switch (message[0]) {
+ case COMMAND_COMPLETE:
+ if (msglen != 1)
+ goto unrecognised;
+
+ printk(KERN_ERR "scsi%d.%c: command complete with no "
+ "status in MESSAGE_IN?\n",
+ info->host->host_no, fas216_target(info));
+ break;
+
+ case SAVE_POINTERS:
+ if (msglen != 1)
+ goto unrecognised;
+
+ /*
+ * Save current data pointer to SAVED data pointer
+ * SCSI II standard says that we must not acknowledge
+ * this until we have really saved pointers.
+ * NOTE: we DO NOT save the command nor status pointers
+ * as required by the SCSI II standard. These always
+ * point to the start of their respective areas.
+ */
+ info->SCpnt->SCp = info->scsi.SCp;
+ info->SCpnt->SCp.sent_command = 0;
+ fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
+ "save data pointers: [%p, %X]",
+ info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
+ break;
+
+ case RESTORE_POINTERS:
+ if (msglen != 1)
+ goto unrecognised;
+
+ /*
+ * Restore current data pointer from SAVED data pointer
+ */
+ info->scsi.SCp = info->SCpnt->SCp;
+ fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
+ "restore data pointers: [%p, 0x%x]",
+ info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
+ break;
+
+ case DISCONNECT:
+ if (msglen != 1)
+ goto unrecognised;
+
+ info->scsi.phase = PHASE_MSGIN_DISCONNECT;
+ break;
+
+ case MESSAGE_REJECT:
+ if (msglen != 1)
+ goto unrecognised;
+
+ switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
+ case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
+ fas216_handlesync(info, message);
+ break;
+
+ default:
+ fas216_log(info, 0, "reject, last message 0x%04x",
+ fas216_get_last_msg(info, info->scsi.msgin_fifo));
+ }
+ break;
+
+ case NOP:
+ break;
+
+ case EXTENDED_MESSAGE:
+ if (msglen < 3)
+ goto unrecognised;
+
+ switch (message[2]) {
+ case EXTENDED_SDTR: /* Sync transfer negotiation request/reply */
+ fas216_handlesync(info, message);
+ break;
+
+ default:
+ goto unrecognised;
+ }
+ break;
+
+ default:
+ goto unrecognised;
+ }
+ return;
+
+unrecognised:
+ fas216_log(info, 0, "unrecognised message, rejecting");
+ printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
+ for (i = 0; i < msglen; i++)
+ printk("%s%02X", i & 31 ? " " : "\n ", message[i]);
+ printk("\n");
+
+ /*
+ * Something strange seems to be happening here -
+ * I can't use SETATN since the chip gives me an
+ * invalid command interrupt when I do. Weird.
+ */
+fas216_cmd(info, CMD_NOP);
+fas216_dumpstate(info);
+ fas216_cmd(info, CMD_SETATN);
+ msgqueue_flush(&info->scsi.msgs);
+ msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
+ info->scsi.phase = PHASE_MSGOUT_EXPECT;
+fas216_dumpstate(info);
+}
+
+static int fas216_wait_cmd(FAS216_Info *info, int cmd)
+{
+ int tout;
+ int stat;
+
+ fas216_cmd(info, cmd);
+
+ for (tout = 1000; tout; tout -= 1) {
+ stat = fas216_readb(info, REG_STAT);
+ if (stat & (STAT_INT|STAT_PARITYERROR))
+ break;
+ udelay(1);
+ }
+
+ return stat;
+}
+