OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [scsi/] [fas216_mod.c] - Diff between revs 1765 and 1782

Only display areas with differences | Details | Blame | View Log

Rev 1765 Rev 1782
/*
/*
 * linux/arch/arm/drivers/scsi/fas216.c
 * linux/arch/arm/drivers/scsi/fas216.c
 *
 *
 * Copyright (C) 1997 Russell King
 * Copyright (C) 1997 Russell King
 *
 *
 * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
 * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
 * other sources, including:
 * other sources, including:
 *   the AMD Am53CF94 data sheet
 *   the AMD Am53CF94 data sheet
 *
 *
 * This is a generic driver.  To use it, have a look at cumana_2.c.  You
 * 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:
 * should define your own structure that overlays FAS216_Info, eg:
 * struct my_host_data {
 * struct my_host_data {
 *    FAS216_Info info;
 *    FAS216_Info info;
 *    ... my host specific data ...
 *    ... my host specific data ...
 * };
 * };
 *
 *
 * Changelog:
 * Changelog:
 *  30-08-1997  RMK     Created
 *  30-08-1997  RMK     Created
 *  14-09-1997  RMK     Started disconnect support
 *  14-09-1997  RMK     Started disconnect support
 *  08-02-1998  RMK     Corrected real DMA support
 *  08-02-1998  RMK     Corrected real DMA support
 *  15-02-1998  RMK     Started sync xfer support
 *  15-02-1998  RMK     Started sync xfer support
 *  06-04-1998  RMK     Tightened conditions for printing incomplete
 *  06-04-1998  RMK     Tightened conditions for printing incomplete
 *                      transfers
 *                      transfers
 *  02-05-1998  RMK     Added extra checks in fas216_reset
 *  02-05-1998  RMK     Added extra checks in fas216_reset
 *  24-05-1998  RMK     Fixed synchronous transfers with period >= 200ns
 *  24-05-1998  RMK     Fixed synchronous transfers with period >= 200ns
 *  26-08-1998  RMK     Improved message support wrt MESSAGE_REJECT
 *  26-08-1998  RMK     Improved message support wrt MESSAGE_REJECT
 *
 *
 * Todo:
 * Todo:
 *  - allow individual devices to enable sync xfers.
 *  - allow individual devices to enable sync xfers.
 */
 */
 
 
#define __NO_VERSION__
#define __NO_VERSION__
 
 
#include <linux/module.h>
#include <linux/module.h>
#include <linux/blk.h>
#include <linux/blk.h>
#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#include <linux/proc_fs.h>
#include <linux/unistd.h>
#include <linux/unistd.h>
#include <linux/stat.h>
#include <linux/stat.h>
 
 
#include <asm/delay.h>
#include <asm/delay.h>
#include <asm/dma.h>
#include <asm/dma.h>
#include <asm/io.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/irq.h>
#include <asm/ecard.h>
#include <asm/ecard.h>
 
 
#define FAS216_C
#define FAS216_C
 
 
#include "scsi.h"
#include "scsi.h"
#include "hosts.h"
#include "hosts.h"
#include "fas216.h"
#include "fas216.h"
 
 
#define VER_MAJOR       0
#define VER_MAJOR       0
#define VER_MINOR       0
#define VER_MINOR       0
#define VER_PATCH       5
#define VER_PATCH       5
 
 
#define SCSI2_TAG
#define SCSI2_TAG
 
 
/* NOTE: SCSI2 Synchronous transfers *require* DMA according to
/* NOTE: SCSI2 Synchronous transfers *require* DMA according to
 *  the data sheet.  This restriction is crazy, especially when
 *  the data sheet.  This restriction is crazy, especially when
 *  you only want to send 16 bytes!  What were the guys who
 *  you only want to send 16 bytes!  What were the guys who
 *  designed this chip on at that time?  Did they read the SCSI2
 *  designed this chip on at that time?  Did they read the SCSI2
 *  spec at all?  The following sections are taken from the SCSI2
 *  spec at all?  The following sections are taken from the SCSI2
 *  standard (s2r10) concerning this:
 *  standard (s2r10) concerning this:
 *
 *
 * > IMPLEMENTORS NOTES:
 * > IMPLEMENTORS NOTES:
 * >   (1)  Re-negotiation at every selection is not recommended, since a
 * >   (1)  Re-negotiation at every selection is not recommended, since a
 * >   significant performance impact is likely.
 * >   significant performance impact is likely.
 *
 *
 * >  The implied synchronous agreement shall remain in effect until a BUS DEVICE
 * >  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
 * >  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
 * >  of the two SCSI devices elects to modify the agreement.  The default data
 * >  transfer mode is asynchronous data transfer mode.  The default data transfer
 * >  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
 * >  mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
 * >  reset condition.
 * >  reset condition.
 *
 *
 *  In total, this means that once you have elected to use synchronous
 *  In total, this means that once you have elected to use synchronous
 *  transfers, you must always use DMA.
 *  transfers, you must always use DMA.
 *
 *
 *  I was thinking that this was a good chip until I found this restriction ;(
 *  I was thinking that this was a good chip until I found this restriction ;(
 */
 */
#define SCSI2_SYNC
#define SCSI2_SYNC
 
 
#define SCSI2_WIDE
#define SCSI2_WIDE
 
 
#undef DEBUG_CONNECT
#undef DEBUG_CONNECT
#undef DEBUG_BUSSERVICE
#undef DEBUG_BUSSERVICE
#undef DEBUG_FUNCTIONDONE
#undef DEBUG_FUNCTIONDONE
#undef DEBUG_MESSAGES
#undef DEBUG_MESSAGES
 
 
#undef CHECK_STRUCTURE
#undef CHECK_STRUCTURE
 
 
static struct { int stat, ssr, isr, ph; } list[8];
static struct { int stat, ssr, isr, ph; } list[8];
static int ptr;
static int ptr;
 
 
static void fas216_dumpstate(FAS216_Info *info)
static void fas216_dumpstate(FAS216_Info *info)
{
{
        printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
        printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
               " INST=%02X IS=%02X CFIS=%02X",
               " INST=%02X IS=%02X CFIS=%02X",
                inb(REG_CTCL(info)), inb(REG_CTCM(info)),
                inb(REG_CTCL(info)), inb(REG_CTCM(info)),
                inb(REG_CMD(info)),  inb(REG_STAT(info)),
                inb(REG_CMD(info)),  inb(REG_STAT(info)),
                inb(REG_INST(info)), inb(REG_IS(info)),
                inb(REG_INST(info)), inb(REG_IS(info)),
                inb(REG_CFIS(info)));
                inb(REG_CFIS(info)));
        printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
        printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
                inb(REG_CNTL1(info)), inb(REG_CNTL2(info)),
                inb(REG_CNTL1(info)), inb(REG_CNTL2(info)),
                inb(REG_CNTL3(info)), inb(REG_CTCH(info)));
                inb(REG_CNTL3(info)), inb(REG_CTCH(info)));
}
}
 
 
static void fas216_dumpinfo(FAS216_Info *info)
static void fas216_dumpinfo(FAS216_Info *info)
{
{
        static int used = 0;
        static int used = 0;
        int i;
        int i;
 
 
        if (used++)
        if (used++)
                return;
                return;
 
 
        printk("FAS216_Info=\n");
        printk("FAS216_Info=\n");
        printk("  { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
        printk("  { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
                info->magic_start, info->host, info->SCpnt,
                info->magic_start, info->host, info->SCpnt,
                info->origSCpnt);
                info->origSCpnt);
        printk("    scsi={ io_port=%X io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
        printk("    scsi={ io_port=%X io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
                info->scsi.io_port, info->scsi.io_shift, info->scsi.irq,
                info->scsi.io_port, info->scsi.io_shift, info->scsi.irq,
                info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
                info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
                info->scsi.cfg[3]);
                info->scsi.cfg[3]);
        printk("           type=%p phase=%X reconnected={ target=%d lun=%d tag=%d }\n",
        printk("           type=%p phase=%X reconnected={ target=%d lun=%d tag=%d }\n",
                info->scsi.type, info->scsi.phase,
                info->scsi.type, info->scsi.phase,
                info->scsi.reconnected.target,
                info->scsi.reconnected.target,
                info->scsi.reconnected.lun, info->scsi.reconnected.tag);
                info->scsi.reconnected.lun, info->scsi.reconnected.tag);
        printk("           SCp={ ptr=%p this_residual=%X buffer=%p buffers_residual=%X }\n",
        printk("           SCp={ ptr=%p this_residual=%X buffer=%p buffers_residual=%X }\n",
                info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
                info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
                info->scsi.SCp.buffer, info->scsi.SCp.buffers_residual);
                info->scsi.SCp.buffer, info->scsi.SCp.buffers_residual);
        printk("      msgs async_stp=%X disconnectable=%d aborting=%d }\n",
        printk("      msgs async_stp=%X disconnectable=%d aborting=%d }\n",
                info->scsi.async_stp,
                info->scsi.async_stp,
                info->scsi.disconnectable, info->scsi.aborting);
                info->scsi.disconnectable, info->scsi.aborting);
        printk("    stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
        printk("    stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
               "            disconnects=%X aborts=%X resets=%X }\n",
               "            disconnects=%X aborts=%X resets=%X }\n",
                info->stats.queues, info->stats.removes, info->stats.fins,
                info->stats.queues, info->stats.removes, info->stats.fins,
                info->stats.reads, info->stats.writes, info->stats.miscs,
                info->stats.reads, info->stats.writes, info->stats.miscs,
                info->stats.disconnects, info->stats.aborts, info->stats.resets);
                info->stats.disconnects, info->stats.aborts, info->stats.resets);
        printk("    ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
        printk("    ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
                info->ifcfg.clockrate, info->ifcfg.select_timeout,
                info->ifcfg.clockrate, info->ifcfg.select_timeout,
                info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
                info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
        for (i = 0; i < 8; i++) {
        for (i = 0; i < 8; i++) {
                printk("    busyluns[%d]=%X dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
                printk("    busyluns[%d]=%X dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
                        i, info->busyluns[i], i,
                        i, info->busyluns[i], i,
                        info->device[i].disconnect_ok, info->device[i].stp,
                        info->device[i].disconnect_ok, info->device[i].stp,
                        info->device[i].sof, info->device[i].sync_state);
                        info->device[i].sof, info->device[i].sync_state);
        }
        }
        printk("    dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
        printk("    dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
                info->dma.transfer_type, info->dma.setup,
                info->dma.transfer_type, info->dma.setup,
                info->dma.pseudo, info->dma.stop);
                info->dma.pseudo, info->dma.stop);
        printk("    internal_done=%X magic_end=%lX }\n",
        printk("    internal_done=%X magic_end=%lX }\n",
                info->internal_done, info->magic_end);
                info->internal_done, info->magic_end);
}
}
 
 
#ifdef CHECK_STRUCTURE
#ifdef CHECK_STRUCTURE
static void fas216_checkmagic(FAS216_Info *info, const char *func)
static void fas216_checkmagic(FAS216_Info *info, const char *func)
{
{
        int corruption = 0;
        int corruption = 0;
        if (info->magic_start != MAGIC) {
        if (info->magic_start != MAGIC) {
                printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
                printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
                corruption++;
                corruption++;
        }
        }
        if (info->magic_end != MAGIC) {
        if (info->magic_end != MAGIC) {
                printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
                printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
                corruption++;
                corruption++;
        }
        }
        if (corruption) {
        if (corruption) {
                fas216_dumpinfo(info);
                fas216_dumpinfo(info);
                panic("scsi memory space corrupted in %s", func);
                panic("scsi memory space corrupted in %s", func);
        }
        }
}
}
#else
#else
#define fas216_checkmagic(info,func)
#define fas216_checkmagic(info,func)
#endif
#endif
 
 
static const char *fas216_bus_phase(int stat)
static const char *fas216_bus_phase(int stat)
{
{
        static const char *phases[] = {
        static const char *phases[] = {
                "DATA OUT", "DATA IN",
                "DATA OUT", "DATA IN",
                "COMMAND", "STATUS",
                "COMMAND", "STATUS",
                "MISC OUT", "MISC IN",
                "MISC OUT", "MISC IN",
                "MESG OUT", "MESG IN"
                "MESG OUT", "MESG IN"
        };
        };
 
 
        return phases[stat & STAT_BUSMASK];
        return phases[stat & STAT_BUSMASK];
}
}
 
 
static const char *fas216_drv_phase(FAS216_Info *info)
static const char *fas216_drv_phase(FAS216_Info *info)
{
{
        switch (info->scsi.phase) {
        switch (info->scsi.phase) {
        case PHASE_IDLE:                return "idle";
        case PHASE_IDLE:                return "idle";
        case PHASE_SELECTION:           return "selection";
        case PHASE_SELECTION:           return "selection";
        case PHASE_COMMAND:             return "command";
        case PHASE_COMMAND:             return "command";
        case PHASE_RECONNECTED:         return "reconnected";
        case PHASE_RECONNECTED:         return "reconnected";
        case PHASE_DATAOUT:             return "data out";
        case PHASE_DATAOUT:             return "data out";
        case PHASE_DATAIN:              return "data in";
        case PHASE_DATAIN:              return "data in";
        case PHASE_MSGIN:               return "message in";
        case PHASE_MSGIN:               return "message in";
        case PHASE_MSGIN_DISCONNECT:    return "disconnect";
        case PHASE_MSGIN_DISCONNECT:    return "disconnect";
        case PHASE_MSGOUT_EXPECT:       return "expect message out";
        case PHASE_MSGOUT_EXPECT:       return "expect message out";
        case PHASE_MSGOUT:              return "message out";
        case PHASE_MSGOUT:              return "message out";
        case PHASE_STATUS:              return "status";
        case PHASE_STATUS:              return "status";
        case PHASE_DONE:                return "done";
        case PHASE_DONE:                return "done";
        default:                        return "???";
        default:                        return "???";
        }
        }
}
}
 
 
static char fas216_target(FAS216_Info *info)
static char fas216_target(FAS216_Info *info)
{
{
        if (info->SCpnt)
        if (info->SCpnt)
                return '0' + info->SCpnt->target;
                return '0' + info->SCpnt->target;
        else
        else
                return 'H';
                return 'H';
}
}
 
 
static void add_debug_list(int stat, int ssr, int isr, int ph)
static void add_debug_list(int stat, int ssr, int isr, int ph)
{
{
        list[ptr].stat = stat;
        list[ptr].stat = stat;
        list[ptr].ssr = ssr;
        list[ptr].ssr = ssr;
        list[ptr].isr = isr;
        list[ptr].isr = isr;
        list[ptr].ph = ph;
        list[ptr].ph = ph;
 
 
        ptr = (ptr + 1) & 7;
        ptr = (ptr + 1) & 7;
}
}
 
 
static void print_debug_list(void)
static void print_debug_list(void)
{
{
        int i;
        int i;
 
 
        i = ptr;
        i = ptr;
 
 
        printk(KERN_ERR "SCSI IRQ trail: ");
        printk(KERN_ERR "SCSI IRQ trail: ");
        do {
        do {
                printk("%02X:%02X:%02X:%1X ",
                printk("%02X:%02X:%02X:%1X ",
                        list[i].stat, list[i].ssr,
                        list[i].stat, list[i].ssr,
                        list[i].isr, list[i].ph);
                        list[i].isr, list[i].ph);
                i = (i + 1) & 7;
                i = (i + 1) & 7;
        } while (i != ptr);
        } while (i != ptr);
        printk("\n");
        printk("\n");
}
}
 
 
static void fas216_done(FAS216_Info *info, unsigned int result);
static void fas216_done(FAS216_Info *info, unsigned int result);
 
 
/* Function: int fas216_clockrate(unsigned int clock)
/* Function: int fas216_clockrate(unsigned int clock)
 * Purpose : calculate correct value to be written into clock conversion
 * Purpose : calculate correct value to be written into clock conversion
 *           factor register.
 *           factor register.
 * Params  : clock - clock speed in MHz
 * Params  : clock - clock speed in MHz
 * Returns : CLKF_ value
 * Returns : CLKF_ value
 */
 */
static int fas216_clockrate(int clock)
static int fas216_clockrate(int clock)
{
{
        if (clock <= 10 || clock > 40) {
        if (clock <= 10 || clock > 40) {
                printk(KERN_CRIT
                printk(KERN_CRIT
                       "fas216: invalid clock rate: check your driver!\n");
                       "fas216: invalid clock rate: check your driver!\n");
                clock = -1;
                clock = -1;
        } else
        } else
                clock = ((clock - 1) / 5 + 1) & 7;
                clock = ((clock - 1) / 5 + 1) & 7;
 
 
        return clock;
        return clock;
}
}
 
 
/* Function: unsigned short fas216_get_last_msg(FAS216_Info *info, int pos)
/* Function: unsigned short fas216_get_last_msg(FAS216_Info *info, int pos)
 * Purpose : retrieve a last message from the list, using position in fifo
 * Purpose : retrieve a last message from the list, using position in fifo
 * Params  : info - interface to search
 * Params  : info - interface to search
 *         : pos  - current fifo position
 *         : pos  - current fifo position
 */
 */
static inline unsigned short
static inline unsigned short
fas216_get_last_msg(FAS216_Info *info, int pos)
fas216_get_last_msg(FAS216_Info *info, int pos)
{
{
        unsigned short packed_msg = NOP;
        unsigned short packed_msg = NOP;
        struct message *msg;
        struct message *msg;
        int msgnr = 0;
        int msgnr = 0;
 
 
        while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
        while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
                if (pos >= msg->fifo)
                if (pos >= msg->fifo)
                        break;
                        break;
        }
        }
 
 
        if (msg) {
        if (msg) {
                if (msg->msg[0] == EXTENDED_MESSAGE)
                if (msg->msg[0] == EXTENDED_MESSAGE)
                        packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
                        packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
                else
                else
                        packed_msg = msg->msg[0];
                        packed_msg = msg->msg[0];
        }
        }
 
 
#ifdef DEBUG_MESSAGES
#ifdef DEBUG_MESSAGES
        printk("Message: %04X found at position %02X\n",
        printk("Message: %04X found at position %02X\n",
                packed_msg, pos);
                packed_msg, pos);
#endif
#endif
        return packed_msg;
        return packed_msg;
}
}
 
 
/* Function: int fas216_syncperiod(FAS216_Info *info, int ns)
/* Function: int fas216_syncperiod(FAS216_Info *info, int ns)
 * Purpose : Calculate value to be loaded into the STP register
 * Purpose : Calculate value to be loaded into the STP register
 *           for a given period in ns
 *           for a given period in ns
 * Params  : info - state structure for interface connected to device
 * Params  : info - state structure for interface connected to device
 *         : ns   - period in ns (between subsequent bytes)
 *         : ns   - period in ns (between subsequent bytes)
 * Returns : Value suitable for REG_STP
 * Returns : Value suitable for REG_STP
 */
 */
static int
static int
fas216_syncperiod(FAS216_Info *info, int ns)
fas216_syncperiod(FAS216_Info *info, int ns)
{
{
        int value = (info->ifcfg.clockrate * ns) / 1000;
        int value = (info->ifcfg.clockrate * ns) / 1000;
 
 
        fas216_checkmagic(info, "fas216_syncperiod");
        fas216_checkmagic(info, "fas216_syncperiod");
 
 
        if (value < 4)
        if (value < 4)
                value = 4;
                value = 4;
        else if (value > 35)
        else if (value > 35)
                value = 35;
                value = 35;
 
 
        return value & 31;
        return value & 31;
}
}
 
 
/* Function: void fas216_set_sync(FAS216_Info *info, int target)
/* Function: void fas216_set_sync(FAS216_Info *info, int target)
 * Purpose : Correctly setup FAS216 chip for specified transfer period.
 * Purpose : Correctly setup FAS216 chip for specified transfer period.
 * Params  : info   - state structure for interface
 * Params  : info   - state structure for interface
 *         : target - target
 *         : target - target
 * Notes   : we need to switch the chip out of FASTSCSI mode if we have
 * Notes   : we need to switch the chip out of FASTSCSI mode if we have
 *           a transfer period >= 200ns - otherwise the chip will violate
 *           a transfer period >= 200ns - otherwise the chip will violate
 *           the SCSI timings.
 *           the SCSI timings.
 */
 */
static void
static void
fas216_set_sync(FAS216_Info *info, int target)
fas216_set_sync(FAS216_Info *info, int target)
{
{
        outb(info->device[target].sof, REG_SOF(info));
        outb(info->device[target].sof, REG_SOF(info));
        outb(info->device[target].stp, REG_STP(info));
        outb(info->device[target].stp, REG_STP(info));
        if (info->device[target].period >= (200 / 4))
        if (info->device[target].period >= (200 / 4))
                outb(info->scsi.cfg[2] & ~CNTL3_FASTSCSI, REG_CNTL3(info));
                outb(info->scsi.cfg[2] & ~CNTL3_FASTSCSI, REG_CNTL3(info));
        else
        else
                outb(info->scsi.cfg[2], REG_CNTL3(info));
                outb(info->scsi.cfg[2], REG_CNTL3(info));
}
}
 
 
/* Synchronous transfer support
/* Synchronous transfer support
 *
 *
 * Note: The SCSI II r10 spec says (5.6.12):
 * Note: The SCSI II r10 spec says (5.6.12):
 *
 *
 *  (2)  Due to historical problems with early host adapters that could
 *  (2)  Due to historical problems with early host adapters that could
 *  not accept an SDTR message, some targets may not initiate synchronous
 *  not accept an SDTR message, some targets may not initiate synchronous
 *  negotiation after a power cycle as required by this standard.  Host
 *  negotiation after a power cycle as required by this standard.  Host
 *  adapters that support synchronous mode may avoid the ensuing failure
 *  adapters that support synchronous mode may avoid the ensuing failure
 *  modes when the target is independently power cycled by initiating a
 *  modes when the target is independently power cycled by initiating a
 *  synchronous negotiation on each REQUEST SENSE and INQUIRY command.
 *  synchronous negotiation on each REQUEST SENSE and INQUIRY command.
 *  This approach increases the SCSI bus overhead and is not recommended
 *  This approach increases the SCSI bus overhead and is not recommended
 *  for new implementations.  The correct method is to respond to an
 *  for new implementations.  The correct method is to respond to an
 *  SDTR message with a MESSAGE REJECT message if the either the
 *  SDTR message with a MESSAGE REJECT message if the either the
 *  initiator or target devices does not support synchronous transfers
 *  initiator or target devices does not support synchronous transfers
 *  or does not want to negotiate for synchronous transfers at the time.
 *  or does not want to negotiate for synchronous transfers at the time.
 *  Using the correct method assures compatibility with wide data
 *  Using the correct method assures compatibility with wide data
 *  transfers and future enhancements.
 *  transfers and future enhancements.
 *
 *
 * We will always initiate a synchronous transfer negociation request on
 * We will always initiate a synchronous transfer negociation request on
 * every INQUIRY or REQUEST SENSE message, unless the target itself has
 * every INQUIRY or REQUEST SENSE message, unless the target itself has
 * at some point performed a synchronous transfer negociation request, or
 * at some point performed a synchronous transfer negociation request, or
 * we have synchronous transfers disabled for this device.
 * we have synchronous transfers disabled for this device.
 */
 */
 
 
/* Function: void fas216_handlesync(FAS216_Info *info, char *msg)
/* Function: void fas216_handlesync(FAS216_Info *info, char *msg)
 * Purpose : Handle a synchronous transfer message from the target
 * Purpose : Handle a synchronous transfer message from the target
 * Params  : info - state structure for interface
 * Params  : info - state structure for interface
 *         : msg  - message from target
 *         : msg  - message from target
 */
 */
static void
static void
fas216_handlesync(FAS216_Info *info, char *msg)
fas216_handlesync(FAS216_Info *info, char *msg)
{
{
        struct fas216_device *dev = &info->device[info->SCpnt->target];
        struct fas216_device *dev = &info->device[info->SCpnt->target];
        enum { sync, async, none, reject } res = none;
        enum { sync, async, none, reject } res = none;
 
 
#ifdef SCSI2_SYNC
#ifdef SCSI2_SYNC
        switch (msg[0]) {
        switch (msg[0]) {
        case MESSAGE_REJECT:
        case MESSAGE_REJECT:
                /* Synchronous transfer request failed.
                /* Synchronous transfer request failed.
                 * Note: SCSI II r10:
                 * Note: SCSI II r10:
                 *
                 *
                 *  SCSI devices that are capable of synchronous
                 *  SCSI devices that are capable of synchronous
                 *  data transfers shall not respond to an SDTR
                 *  data transfers shall not respond to an SDTR
                 *  message with a MESSAGE REJECT message.
                 *  message with a MESSAGE REJECT message.
                 *
                 *
                 * Hence, if we get this condition, we disable
                 * Hence, if we get this condition, we disable
                 * negociation for this device.
                 * negociation for this device.
                 */
                 */
                if (dev->sync_state == neg_inprogress) {
                if (dev->sync_state == neg_inprogress) {
                        dev->sync_state = neg_invalid;
                        dev->sync_state = neg_invalid;
                        res = async;
                        res = async;
                }
                }
                break;
                break;
 
 
        case EXTENDED_MESSAGE:
        case EXTENDED_MESSAGE:
                switch (dev->sync_state) {
                switch (dev->sync_state) {
                /* We don't accept synchronous transfer requests.
                /* We don't accept synchronous transfer requests.
                 * Respond with a MESSAGE_REJECT to prevent a
                 * Respond with a MESSAGE_REJECT to prevent a
                 * synchronous transfer agreement from being reached.
                 * synchronous transfer agreement from being reached.
                 */
                 */
                case neg_invalid:
                case neg_invalid:
                        res = reject;
                        res = reject;
                        break;
                        break;
 
 
                /* We were not negociating a synchronous transfer,
                /* We were not negociating a synchronous transfer,
                 * but the device sent us a negociation request.
                 * but the device sent us a negociation request.
                 * Honour the request by sending back a SDTR
                 * Honour the request by sending back a SDTR
                 * message containing our capability, limited by
                 * message containing our capability, limited by
                 * the targets capability.
                 * the targets capability.
                 */
                 */
                default:
                default:
                        outb(CMD_SETATN, REG_CMD(info));
                        outb(CMD_SETATN, REG_CMD(info));
                        if (msg[4] > info->ifcfg.sync_max_depth)
                        if (msg[4] > info->ifcfg.sync_max_depth)
                                msg[4] = info->ifcfg.sync_max_depth;
                                msg[4] = info->ifcfg.sync_max_depth;
                        if (msg[3] < 1000 / info->ifcfg.clockrate)
                        if (msg[3] < 1000 / info->ifcfg.clockrate)
                                msg[3] = 1000 / info->ifcfg.clockrate;
                                msg[3] = 1000 / info->ifcfg.clockrate;
 
 
                        msgqueue_flush(&info->scsi.msgs);
                        msgqueue_flush(&info->scsi.msgs);
                        msgqueue_addmsg(&info->scsi.msgs, 5,
                        msgqueue_addmsg(&info->scsi.msgs, 5,
                                        EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
                                        EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
                                        msg[3], msg[4]);
                                        msg[3], msg[4]);
                        info->scsi.phase = PHASE_MSGOUT_EXPECT;
                        info->scsi.phase = PHASE_MSGOUT_EXPECT;
 
 
                        /* This is wrong.  The agreement is not in effect
                        /* This is wrong.  The agreement is not in effect
                         * until this message is accepted by the device
                         * until this message is accepted by the device
                         */
                         */
                        dev->sync_state = neg_targcomplete;
                        dev->sync_state = neg_targcomplete;
                        res = sync;
                        res = sync;
                        break;
                        break;
 
 
                /* We initiated the synchronous transfer negociation,
                /* We initiated the synchronous transfer negociation,
                 * and have successfully received a response from the
                 * and have successfully received a response from the
                 * target.  The synchronous transfer agreement has been
                 * target.  The synchronous transfer agreement has been
                 * reached.  Note: if the values returned are out of our
                 * reached.  Note: if the values returned are out of our
                 * bounds, we must reject the message.
                 * bounds, we must reject the message.
                 */
                 */
                case neg_inprogress:
                case neg_inprogress:
                        res = reject;
                        res = reject;
                        if (msg[4] <= info->ifcfg.sync_max_depth &&
                        if (msg[4] <= info->ifcfg.sync_max_depth &&
                            msg[3] >= 1000 / info->ifcfg.clockrate) {
                            msg[3] >= 1000 / info->ifcfg.clockrate) {
                                dev->sync_state = neg_complete;
                                dev->sync_state = neg_complete;
                                res = sync;
                                res = sync;
                        }
                        }
                        break;
                        break;
                }
                }
        }
        }
#else
#else
        res = reject;
        res = reject;
#endif
#endif
 
 
        switch (res) {
        switch (res) {
        case sync:
        case sync:
                dev->period = msg[3];
                dev->period = msg[3];
                dev->sof    = msg[4];
                dev->sof    = msg[4];
                dev->stp    = fas216_syncperiod(info, msg[3] * 4);
                dev->stp    = fas216_syncperiod(info, msg[3] * 4);
                fas216_set_sync(info, info->SCpnt->target);
                fas216_set_sync(info, info->SCpnt->target);
                break;
                break;
 
 
        case reject:
        case reject:
                outb(CMD_SETATN, REG_CMD(info));
                outb(CMD_SETATN, REG_CMD(info));
                msgqueue_flush(&info->scsi.msgs);
                msgqueue_flush(&info->scsi.msgs);
                msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
                msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
 
 
        case async:
        case async:
                dev->period = info->ifcfg.asyncperiod / 4;
                dev->period = info->ifcfg.asyncperiod / 4;
                dev->sof    = 0;
                dev->sof    = 0;
                dev->stp    = info->scsi.async_stp;
                dev->stp    = info->scsi.async_stp;
                fas216_set_sync(info, info->SCpnt->target);
                fas216_set_sync(info, info->SCpnt->target);
                break;
                break;
 
 
        case none:
        case none:
                break;
                break;
        }
        }
}
}
 
 
/* Function: void fas216_handlewide(FAS216_Info *info, char *msg)
/* Function: void fas216_handlewide(FAS216_Info *info, char *msg)
 * Purpose : Handle a wide transfer message from the target
 * Purpose : Handle a wide transfer message from the target
 * Params  : info - state structure for interface
 * Params  : info - state structure for interface
 *         : msg  - message from target
 *         : msg  - message from target
 */
 */
static void
static void
fas216_handlewide(FAS216_Info *info, char *msg)
fas216_handlewide(FAS216_Info *info, char *msg)
{
{
        struct fas216_device *dev = &info->device[info->SCpnt->target];
        struct fas216_device *dev = &info->device[info->SCpnt->target];
        enum { wide, bit8, none, reject } res = none;
        enum { wide, bit8, none, reject } res = none;
 
 
#ifdef SCSI2_WIDE
#ifdef SCSI2_WIDE
        switch (msg[0]) {
        switch (msg[0]) {
        case MESSAGE_REJECT:
        case MESSAGE_REJECT:
                /* Wide transfer request failed.
                /* Wide transfer request failed.
                 * Note: SCSI II r10:
                 * Note: SCSI II r10:
                 *
                 *
                 *  SCSI devices that are capable of wide
                 *  SCSI devices that are capable of wide
                 *  data transfers shall not respond to a
                 *  data transfers shall not respond to a
                 *  WDTR message with a MESSAGE REJECT message.
                 *  WDTR message with a MESSAGE REJECT message.
                 *
                 *
                 * Hence, if we get this condition, we never
                 * Hence, if we get this condition, we never
                 * reattempt negociation for this device.
                 * reattempt negociation for this device.
                 */
                 */
                if (dev->wide_state == neg_inprogress) {
                if (dev->wide_state == neg_inprogress) {
                        dev->wide_state = neg_invalid;
                        dev->wide_state = neg_invalid;
                        res = bit8;
                        res = bit8;
                }
                }
                break;
                break;
 
 
        case EXTENDED_MESSAGE:
        case EXTENDED_MESSAGE:
                switch (dev->wide_state) {
                switch (dev->wide_state) {
                /* We don't accept wide data transfer requests.
                /* We don't accept wide data transfer requests.
                 * Respond with a MESSAGE REJECT to prevent a
                 * Respond with a MESSAGE REJECT to prevent a
                 * wide data transfer agreement from being reached.
                 * wide data transfer agreement from being reached.
                 */
                 */
                case neg_invalid:
                case neg_invalid:
                        res = reject;
                        res = reject;
                        break;
                        break;
 
 
                /* We were not negociating a wide data transfer,
                /* We were not negociating a wide data transfer,
                 * but the device sent is a negociation request.
                 * but the device sent is a negociation request.
                 * Honour the request by sending back a WDTR
                 * Honour the request by sending back a WDTR
                 * message containing our capability, limited by
                 * message containing our capability, limited by
                 * the targets capability.
                 * the targets capability.
                 */
                 */
                default:
                default:
                        outb(CMD_SETATN, REG_CMD(info));
                        outb(CMD_SETATN, REG_CMD(info));
                        if (msg[3] > info->ifcfg.wide_max_size)
                        if (msg[3] > info->ifcfg.wide_max_size)
                                msg[3] = info->ifcfg.wide_max_size;
                                msg[3] = info->ifcfg.wide_max_size;
 
 
                        msgqueue_flush(&info->scsi.msgs);
                        msgqueue_flush(&info->scsi.msgs);
                        msgqueue_addmsg(&info->scsi.msgs, 4,
                        msgqueue_addmsg(&info->scsi.msgs, 4,
                                        EXTENDED_MESSAGE, 2, EXTENDED_WDTR,
                                        EXTENDED_MESSAGE, 2, EXTENDED_WDTR,
                                        msg[3]);
                                        msg[3]);
                        info->scsi.phase = PHASE_MSGOUT_EXPECT;
                        info->scsi.phase = PHASE_MSGOUT_EXPECT;
                        res = wide;
                        res = wide;
                        break;
                        break;
 
 
                /* We initiated the wide data transfer negociation,
                /* We initiated the wide data transfer negociation,
                 * and have successfully received a response from the
                 * and have successfully received a response from the
                 * target.  The synchronous transfer agreement has been
                 * target.  The synchronous transfer agreement has been
                 * reached.  Note: if the values returned are out of our
                 * reached.  Note: if the values returned are out of our
                 * bounds, we must reject the message.
                 * bounds, we must reject the message.
                 */
                 */
                case neg_inprogress:
                case neg_inprogress:
                        res = reject;
                        res = reject;
                        if (msg[3] <= info->ifcfg.wide_max_size) {
                        if (msg[3] <= info->ifcfg.wide_max_size) {
                                dev->wide_state = neg_complete;
                                dev->wide_state = neg_complete;
                                res = wide;
                                res = wide;
                        }
                        }
                        break;
                        break;
                }
                }
        }
        }
#else
#else
        res = reject;
        res = reject;
#endif
#endif
 
 
        switch (res) {
        switch (res) {
        case wide:
        case wide:
                dev->wide_xfer = msg[3];
                dev->wide_xfer = msg[3];
                break;
                break;
 
 
        case reject:
        case reject:
                outb(CMD_SETATN, REG_CMD(info));
                outb(CMD_SETATN, REG_CMD(info));
                msgqueue_flush(&info->scsi.msgs);
                msgqueue_flush(&info->scsi.msgs);
                msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
                msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
 
 
        case bit8:
        case bit8:
                dev->wide_xfer = 0;
                dev->wide_xfer = 0;
                break;
                break;
 
 
        case none:
        case none:
                break;
                break;
        }
        }
}
}
 
 
/* Function: void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
/* Function: void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
 * Purpose : update data pointers after transfer suspended/paused
 * Purpose : update data pointers after transfer suspended/paused
 * Params  : info              - interface's local pointer to update
 * Params  : info              - interface's local pointer to update
 *           bytes_transferred - number of bytes transferred
 *           bytes_transferred - number of bytes transferred
 */
 */
static void
static void
fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
{
{
        unsigned char *ptr;
        unsigned char *ptr;
        unsigned int residual;
        unsigned int residual;
 
 
        fas216_checkmagic(info, "fas216_updateptrs");
        fas216_checkmagic(info, "fas216_updateptrs");
 
 
        ptr = info->scsi.SCp.ptr;
        ptr = info->scsi.SCp.ptr;
        residual = info->scsi.SCp.this_residual;
        residual = info->scsi.SCp.this_residual;
 
 
        info->SCpnt->request_bufflen -= bytes_transferred;
        info->SCpnt->request_bufflen -= bytes_transferred;
 
 
        while (residual <= bytes_transferred && bytes_transferred) {
        while (residual <= bytes_transferred && bytes_transferred) {
                /* We have used up this buffer */
                /* We have used up this buffer */
                bytes_transferred -= residual;
                bytes_transferred -= residual;
                if (info->scsi.SCp.buffers_residual) {
                if (info->scsi.SCp.buffers_residual) {
                        info->scsi.SCp.buffer++;
                        info->scsi.SCp.buffer++;
                        info->scsi.SCp.buffers_residual--;
                        info->scsi.SCp.buffers_residual--;
                        ptr = (unsigned char *)info->scsi.SCp.buffer->address;
                        ptr = (unsigned char *)info->scsi.SCp.buffer->address;
                        residual = info->scsi.SCp.buffer->length;
                        residual = info->scsi.SCp.buffer->length;
                } else {
                } else {
                        ptr = NULL;
                        ptr = NULL;
                        residual = 0;
                        residual = 0;
                }
                }
        }
        }
 
 
        residual -= bytes_transferred;
        residual -= bytes_transferred;
        ptr += bytes_transferred;
        ptr += bytes_transferred;
 
 
        if (residual == 0)
        if (residual == 0)
                ptr = NULL;
                ptr = NULL;
 
 
        info->scsi.SCp.ptr = ptr;
        info->scsi.SCp.ptr = ptr;
        info->scsi.SCp.this_residual = residual;
        info->scsi.SCp.this_residual = residual;
}
}
 
 
/* Function: void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
/* Function: void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
 * Purpose : transfer data off of/on to card using programmed IO
 * Purpose : transfer data off of/on to card using programmed IO
 * Params  : info      - interface to transfer data to/from
 * Params  : info      - interface to transfer data to/from
 *           direction - direction to transfer data (DMA_OUT/DMA_IN)
 *           direction - direction to transfer data (DMA_OUT/DMA_IN)
 * Notes   : this is incredibly slow
 * Notes   : this is incredibly slow
 */
 */
static void
static void
fas216_pio(FAS216_Info *info, fasdmadir_t direction)
fas216_pio(FAS216_Info *info, fasdmadir_t direction)
{
{
        unsigned int residual;
        unsigned int residual;
        char *ptr;
        char *ptr;
        int correction;
        int correction;
 
 
        fas216_checkmagic(info, "fas216_pio");
        fas216_checkmagic(info, "fas216_pio");
 
 
        residual = info->scsi.SCp.this_residual;
        residual = info->scsi.SCp.this_residual;
        ptr = info->scsi.SCp.ptr;
        ptr = info->scsi.SCp.ptr;
 
 
        if (direction == DMA_OUT) {
        if (direction == DMA_OUT) {
//              while (residual > 0) {
//              while (residual > 0) {
//                      if ((inb(REG_CFIS(info)) & CFIS_CF) < 8) {
//                      if ((inb(REG_CFIS(info)) & CFIS_CF) < 8) {
                                outb(*ptr++, REG_FF(info));
                                outb(*ptr++, REG_FF(info));
                                residual -= 1;
                                residual -= 1;
//                      }
//                      }
//                      if (inb(REG_STAT(info)) & STAT_INT)
//                      if (inb(REG_STAT(info)) & STAT_INT)
//                              break;
//                              break;
//              }
//              }
//              correction = inb(REG_CFIS(info)) & CFIS_CF;
//              correction = inb(REG_CFIS(info)) & CFIS_CF;
correction = 0;
correction = 0;
        } else {
        } else {
//              while (residual > 0) {
//              while (residual > 0) {
//                      if ((inb(REG_CFIS(info)) & CFIS_CF) != 0) {
//                      if ((inb(REG_CFIS(info)) & CFIS_CF) != 0) {
                                *ptr++ = inb(REG_FF(info));
                                *ptr++ = inb(REG_FF(info));
                                residual -= 1;
                                residual -= 1;
//                      }
//                      }
//                      if (inb(REG_STAT(info)) & STAT_INT)
//                      if (inb(REG_STAT(info)) & STAT_INT)
//                              break;
//                              break;
//              }
//              }
                correction = 0;
                correction = 0;
        }
        }
 
 
        ptr -= correction;
        ptr -= correction;
        residual += correction;
        residual += correction;
 
 
        if (residual == 0) {
        if (residual == 0) {
                if (info->scsi.SCp.buffers_residual) {
                if (info->scsi.SCp.buffers_residual) {
                        info->scsi.SCp.buffer++;
                        info->scsi.SCp.buffer++;
                        info->scsi.SCp.buffers_residual--;
                        info->scsi.SCp.buffers_residual--;
                        ptr = (unsigned char *)info->scsi.SCp.buffer->address;
                        ptr = (unsigned char *)info->scsi.SCp.buffer->address;
                        residual = info->scsi.SCp.buffer->length;
                        residual = info->scsi.SCp.buffer->length;
                } else {
                } else {
                        ptr = NULL;
                        ptr = NULL;
                        residual = 0;
                        residual = 0;
                }
                }
        }
        }
 
 
        info->scsi.SCp.ptr = ptr;
        info->scsi.SCp.ptr = ptr;
        info->scsi.SCp.this_residual = residual;
        info->scsi.SCp.this_residual = residual;
}
}
 
 
/* Function: void fas216_starttransfer(FAS216_Info *info,
/* Function: void fas216_starttransfer(FAS216_Info *info,
 *                                     fasdmadir_t direction)
 *                                     fasdmadir_t direction)
 * Purpose : Start a DMA/PIO transfer off of/on to card
 * Purpose : Start a DMA/PIO transfer off of/on to card
 * Params  : info      - interface from which device disconnected from
 * Params  : info      - interface from which device disconnected from
 *           direction - transfer direction (DMA_OUT/DMA_IN)
 *           direction - transfer direction (DMA_OUT/DMA_IN)
 */
 */
static void
static void
fas216_starttransfer(FAS216_Info *info, fasdmadir_t direction, int flush_fifo)
fas216_starttransfer(FAS216_Info *info, fasdmadir_t direction, int flush_fifo)
{
{
        fasdmatype_t dmatype;
        fasdmatype_t dmatype;
 
 
        fas216_checkmagic(info, "fas216_starttransfer");
        fas216_checkmagic(info, "fas216_starttransfer");
 
 
        info->scsi.phase = (direction == DMA_OUT) ?
        info->scsi.phase = (direction == DMA_OUT) ?
                                PHASE_DATAOUT : PHASE_DATAIN;
                                PHASE_DATAOUT : PHASE_DATAIN;
 
 
        if (info->dma.transfer_type != fasdma_none &&
        if (info->dma.transfer_type != fasdma_none &&
            info->dma.transfer_type != fasdma_pio) {
            info->dma.transfer_type != fasdma_pio) {
                unsigned long total, residual;
                unsigned long total, residual;
 
 
                if (info->dma.transfer_type == fasdma_real_all)
                if (info->dma.transfer_type == fasdma_real_all)
                        total = info->SCpnt->request_bufflen;
                        total = info->SCpnt->request_bufflen;
                else
                else
                        total = info->scsi.SCp.this_residual;
                        total = info->scsi.SCp.this_residual;
 
 
                residual = (inb(REG_CFIS(info)) & CFIS_CF) +
                residual = (inb(REG_CFIS(info)) & CFIS_CF) +
                            inb(REG_CTCL(info)) +
                            inb(REG_CTCL(info)) +
                            (inb(REG_CTCM(info)) << 8) +
                            (inb(REG_CTCM(info)) << 8) +
                            (inb(REG_CTCH(info)) << 16);
                            (inb(REG_CTCH(info)) << 16);
                fas216_updateptrs(info, total - residual);
                fas216_updateptrs(info, total - residual);
        }
        }
        info->dma.transfer_type = fasdma_none;
        info->dma.transfer_type = fasdma_none;
 
 
        if (!info->scsi.SCp.ptr) {
        if (!info->scsi.SCp.ptr) {
                printk("scsi%d.%c: null buffer passed to "
                printk("scsi%d.%c: null buffer passed to "
                        "fas216_starttransfer\n", info->host->host_no,
                        "fas216_starttransfer\n", info->host->host_no,
                        fas216_target(info));
                        fas216_target(info));
                return;
                return;
        }
        }
 
 
        /* flush FIFO */
        /* flush FIFO */
        if (flush_fifo)
        if (flush_fifo)
                outb(CMD_FLUSHFIFO, REG_CMD(info));
                outb(CMD_FLUSHFIFO, REG_CMD(info));
 
 
        /*
        /*
         * Default to PIO mode or DMA mode if we have a synchronous
         * Default to PIO mode or DMA mode if we have a synchronous
         * transfer agreement.
         * transfer agreement.
         */
         */
        if (info->device[info->SCpnt->target].sof && info->dma.setup)
        if (info->device[info->SCpnt->target].sof && info->dma.setup)
                dmatype = fasdma_real_all;
                dmatype = fasdma_real_all;
        else
        else
                dmatype = fasdma_pio;
                dmatype = fasdma_pio;
 
 
        if (info->dma.setup)
        if (info->dma.setup)
                dmatype = info->dma.setup(info->host, &info->scsi.SCp,
                dmatype = info->dma.setup(info->host, &info->scsi.SCp,
                                          direction, dmatype);
                                          direction, dmatype);
        info->dma.transfer_type = dmatype;
        info->dma.transfer_type = dmatype;
 
 
        switch (dmatype) {
        switch (dmatype) {
        case fasdma_pio:
        case fasdma_pio:
                outb(0, REG_SOF(info));
                outb(0, REG_SOF(info));
                outb(info->scsi.async_stp, REG_STP(info));
                outb(info->scsi.async_stp, REG_STP(info));
                outb(info->scsi.SCp.this_residual, REG_STCL(info));
                outb(info->scsi.SCp.this_residual, REG_STCL(info));
                outb(info->scsi.SCp.this_residual >> 8, REG_STCM(info));
                outb(info->scsi.SCp.this_residual >> 8, REG_STCM(info));
                outb(info->scsi.SCp.this_residual >> 16, REG_STCH(info));
                outb(info->scsi.SCp.this_residual >> 16, REG_STCH(info));
                outb(CMD_NOP | CMD_WITHDMA, REG_CMD(info));
                outb(CMD_NOP | CMD_WITHDMA, REG_CMD(info));
                outb(CMD_TRANSFERINFO, REG_CMD(info));
                outb(CMD_TRANSFERINFO, REG_CMD(info));
                fas216_pio(info, direction);
                fas216_pio(info, direction);
                break;
                break;
 
 
        case fasdma_pseudo:
        case fasdma_pseudo:
                outb(info->scsi.SCp.this_residual, REG_STCL(info));
                outb(info->scsi.SCp.this_residual, REG_STCL(info));
                outb(info->scsi.SCp.this_residual >> 8, REG_STCM(info));
                outb(info->scsi.SCp.this_residual >> 8, REG_STCM(info));
                outb(info->scsi.SCp.this_residual >> 16, REG_STCH(info));
                outb(info->scsi.SCp.this_residual >> 16, REG_STCH(info));
                outb(CMD_TRANSFERINFO | CMD_WITHDMA, REG_CMD(info));
                outb(CMD_TRANSFERINFO | CMD_WITHDMA, REG_CMD(info));
                info->dma.pseudo(info->host, &info->scsi.SCp,
                info->dma.pseudo(info->host, &info->scsi.SCp,
                                 direction, info->SCpnt->transfersize);
                                 direction, info->SCpnt->transfersize);
                break;
                break;
 
 
        case fasdma_real_block:
        case fasdma_real_block:
                outb(info->scsi.SCp.this_residual, REG_STCL(info));
                outb(info->scsi.SCp.this_residual, REG_STCL(info));
                outb(info->scsi.SCp.this_residual >> 8, REG_STCM(info));
                outb(info->scsi.SCp.this_residual >> 8, REG_STCM(info));
                outb(info->scsi.SCp.this_residual >> 16, REG_STCH(info));
                outb(info->scsi.SCp.this_residual >> 16, REG_STCH(info));
                outb(CMD_TRANSFERINFO | CMD_WITHDMA, REG_CMD(info));
                outb(CMD_TRANSFERINFO | CMD_WITHDMA, REG_CMD(info));
                break;
                break;
 
 
        case fasdma_real_all:
        case fasdma_real_all:
                outb(info->SCpnt->request_bufflen, REG_STCL(info));
                outb(info->SCpnt->request_bufflen, REG_STCL(info));
                outb(info->SCpnt->request_bufflen >> 8, REG_STCM(info));
                outb(info->SCpnt->request_bufflen >> 8, REG_STCM(info));
                outb(info->SCpnt->request_bufflen >> 16, REG_STCH(info));
                outb(info->SCpnt->request_bufflen >> 16, REG_STCH(info));
                outb(CMD_TRANSFERINFO | CMD_WITHDMA, REG_CMD(info));
                outb(CMD_TRANSFERINFO | CMD_WITHDMA, REG_CMD(info));
                break;
                break;
 
 
        default:
        default:
                printk(KERN_ERR "scsi%d.%d: invalid FAS216 DMA type\n",
                printk(KERN_ERR "scsi%d.%d: invalid FAS216 DMA type\n",
                       info->host->host_no, fas216_target(info));
                       info->host->host_no, fas216_target(info));
                break;
                break;
        }
        }
}
}
 
 
/* Function: void fas216_stoptransfer(FAS216_Info *info)
/* Function: void fas216_stoptransfer(FAS216_Info *info)
 * Purpose : Stop a DMA transfer onto / off of the card
 * Purpose : Stop a DMA transfer onto / off of the card
 * Params  : info      - interface from which device disconnected from
 * Params  : info      - interface from which device disconnected from
 */
 */
static void
static void
fas216_stoptransfer(FAS216_Info *info)
fas216_stoptransfer(FAS216_Info *info)
{
{
        fas216_checkmagic(info, "fas216_stoptransfer");
        fas216_checkmagic(info, "fas216_stoptransfer");
 
 
        if (info->dma.transfer_type != fasdma_none &&
        if (info->dma.transfer_type != fasdma_none &&
            info->dma.transfer_type != fasdma_pio) {
            info->dma.transfer_type != fasdma_pio) {
                unsigned long total, residual;
                unsigned long total, residual;
 
 
                if ((info->dma.transfer_type == fasdma_real_all ||
                if ((info->dma.transfer_type == fasdma_real_all ||
                     info->dma.transfer_type == fasdma_real_block) &&
                     info->dma.transfer_type == fasdma_real_block) &&
                    info->dma.stop)
                    info->dma.stop)
                        info->dma.stop(info->host, &info->scsi.SCp);
                        info->dma.stop(info->host, &info->scsi.SCp);
 
 
                if (info->dma.transfer_type == fasdma_real_all)
                if (info->dma.transfer_type == fasdma_real_all)
                        total = info->SCpnt->request_bufflen;
                        total = info->SCpnt->request_bufflen;
                else
                else
                        total = info->scsi.SCp.this_residual;
                        total = info->scsi.SCp.this_residual;
 
 
                residual = (inb(REG_CFIS(info)) & CFIS_CF) +
                residual = (inb(REG_CFIS(info)) & CFIS_CF) +
                            inb(REG_CTCL(info)) +
                            inb(REG_CTCL(info)) +
                            (inb(REG_CTCM(info)) << 8) +
                            (inb(REG_CTCM(info)) << 8) +
                            (inb(REG_CTCH(info)) << 16);
                            (inb(REG_CTCH(info)) << 16);
if (residual > total) {
if (residual > total) {
        printk("stopxfer: total=%ld, residual=%ld\n", total, residual);
        printk("stopxfer: total=%ld, residual=%ld\n", total, residual);
        fas216_dumpstate(info);
        fas216_dumpstate(info);
        fas216_dumpinfo(info);
        fas216_dumpinfo(info);
        residual = total;
        residual = total;
}
}
                fas216_updateptrs(info, total - residual);
                fas216_updateptrs(info, total - residual);
                info->dma.transfer_type = fasdma_none;
                info->dma.transfer_type = fasdma_none;
        }
        }
        if (info->scsi.phase == PHASE_DATAOUT)
        if (info->scsi.phase == PHASE_DATAOUT)
                outb(CMD_FLUSHFIFO, REG_CMD(info));
                outb(CMD_FLUSHFIFO, REG_CMD(info));
}
}
 
 
/* Function: void fas216_disconnected_intr(FAS216_Info *info)
/* Function: void fas216_disconnected_intr(FAS216_Info *info)
 * Purpose : handle device disconnection
 * Purpose : handle device disconnection
 * Params  : info - interface from which device disconnected from
 * Params  : info - interface from which device disconnected from
 */
 */
static void
static void
fas216_disconnect_intr(FAS216_Info *info)
fas216_disconnect_intr(FAS216_Info *info)
{
{
        fas216_checkmagic(info, "fas216_disconnected_intr");
        fas216_checkmagic(info, "fas216_disconnected_intr");
 
 
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
        printk("scsi%d.%c: disconnect phase=%02X\n", info->host->host_no,
        printk("scsi%d.%c: disconnect phase=%02X\n", info->host->host_no,
                fas216_target(info), info->scsi.phase);
                fas216_target(info), info->scsi.phase);
#endif
#endif
        msgqueue_flush(&info->scsi.msgs);
        msgqueue_flush(&info->scsi.msgs);
 
 
        switch (info->scsi.phase) {
        switch (info->scsi.phase) {
        case PHASE_SELECTION:                   /* while selecting - no target          */
        case PHASE_SELECTION:                   /* while selecting - no target          */
        case PHASE_SELSTEPS:
        case PHASE_SELSTEPS:
                fas216_done(info, DID_NO_CONNECT);
                fas216_done(info, DID_NO_CONNECT);
                break;
                break;
 
 
        case PHASE_MSGIN_DISCONNECT:                    /* message in - disconnecting           */
        case PHASE_MSGIN_DISCONNECT:                    /* message in - disconnecting           */
                outb(CMD_ENABLESEL, REG_CMD(info));
                outb(CMD_ENABLESEL, REG_CMD(info));
                info->scsi.disconnectable = 1;
                info->scsi.disconnectable = 1;
                info->scsi.reconnected.tag = 0;
                info->scsi.reconnected.tag = 0;
                info->scsi.phase = PHASE_IDLE;
                info->scsi.phase = PHASE_IDLE;
                info->stats.disconnects += 1;
                info->stats.disconnects += 1;
                break;
                break;
 
 
        case PHASE_DONE:                        /* at end of command - complete         */
        case PHASE_DONE:                        /* at end of command - complete         */
                fas216_done(info, DID_OK);
                fas216_done(info, DID_OK);
                break;
                break;
 
 
        case PHASE_MSGOUT:                      /* message out - possible ABORT message */
        case PHASE_MSGOUT:                      /* message out - possible ABORT message */
                if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
                if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
                        info->scsi.aborting = 0;
                        info->scsi.aborting = 0;
                        fas216_done(info, DID_ABORT);
                        fas216_done(info, DID_ABORT);
                        break;
                        break;
                }
                }
 
 
        default:                                /* huh?                                 */
        default:                                /* huh?                                 */
                printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
                printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
                        info->host->host_no, fas216_target(info), fas216_drv_phase(info));
                        info->host->host_no, fas216_target(info), fas216_drv_phase(info));
                print_debug_list();
                print_debug_list();
                fas216_stoptransfer(info);
                fas216_stoptransfer(info);
                fas216_done(info, DID_ERROR);
                fas216_done(info, DID_ERROR);
                break;
                break;
        }
        }
}
}
 
 
/* Function: void fas216_reselected_intr(FAS216_Info *info)
/* Function: void fas216_reselected_intr(FAS216_Info *info)
 * Purpose : Start reconnection of a device
 * Purpose : Start reconnection of a device
 * Params  : info - interface which was reselected
 * Params  : info - interface which was reselected
 */
 */
static void
static void
fas216_reselected_intr(FAS216_Info *info)
fas216_reselected_intr(FAS216_Info *info)
{
{
        unsigned char target, identify_msg, ok;
        unsigned char target, identify_msg, ok;
 
 
        fas216_checkmagic(info, "fas216_reselected_intr");
        fas216_checkmagic(info, "fas216_reselected_intr");
 
 
        if ((info->scsi.phase == PHASE_SELECTION ||
        if ((info->scsi.phase == PHASE_SELECTION ||
             info->scsi.phase == PHASE_SELSTEPS) && info->SCpnt) {
             info->scsi.phase == PHASE_SELSTEPS) && info->SCpnt) {
                Scsi_Cmnd *SCpnt = info->SCpnt;
                Scsi_Cmnd *SCpnt = info->SCpnt;
 
 
                info->origSCpnt = SCpnt;
                info->origSCpnt = SCpnt;
                info->SCpnt = NULL;
                info->SCpnt = NULL;
 
 
                if (info->device[SCpnt->target].wide_state == neg_inprogress)
                if (info->device[SCpnt->target].wide_state == neg_inprogress)
                        info->device[SCpnt->target].wide_state = neg_wait;
                        info->device[SCpnt->target].wide_state = neg_wait;
                if (info->device[SCpnt->target].sync_state == neg_inprogress)
                if (info->device[SCpnt->target].sync_state == neg_inprogress)
                        info->device[SCpnt->target].sync_state = neg_wait;
                        info->device[SCpnt->target].sync_state = neg_wait;
        }
        }
 
 
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
        printk("scsi%d.%c: reconnect phase=%02X\n", info->host->host_no,
        printk("scsi%d.%c: reconnect phase=%02X\n", info->host->host_no,
                fas216_target(info), info->scsi.phase);
                fas216_target(info), info->scsi.phase);
#endif
#endif
 
 
        if ((inb(REG_CFIS(info)) & CFIS_CF) != 2) {
        if ((inb(REG_CFIS(info)) & CFIS_CF) != 2) {
                printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
                printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
                        info->host->host_no);
                        info->host->host_no);
                outb(CMD_SETATN, REG_CMD(info));
                outb(CMD_SETATN, REG_CMD(info));
                outb(CMD_MSGACCEPTED, REG_CMD(info));
                outb(CMD_MSGACCEPTED, REG_CMD(info));
                msgqueue_flush(&info->scsi.msgs);
                msgqueue_flush(&info->scsi.msgs);
                msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
                msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
                return;
                return;
        }
        }
 
 
        target = inb(REG_FF(info));
        target = inb(REG_FF(info));
        identify_msg = inb(REG_FF(info));
        identify_msg = inb(REG_FF(info));
 
 
        ok = 1;
        ok = 1;
        if (!(target & (1 << info->host->this_id))) {
        if (!(target & (1 << info->host->this_id))) {
                printk(KERN_ERR "scsi%d.H: invalid host id on reselect\n", info->host->host_no);
                printk(KERN_ERR "scsi%d.H: invalid host id on reselect\n", info->host->host_no);
                ok = 0;
                ok = 0;
        }
        }
 
 
        if (!(identify_msg & 0x80)) {
        if (!(identify_msg & 0x80)) {
                printk(KERN_ERR "scsi%d.H: no IDENTIFY message on reselect, got msg %02X\n",
                printk(KERN_ERR "scsi%d.H: no IDENTIFY message on reselect, got msg %02X\n",
                        info->host->host_no, identify_msg);
                        info->host->host_no, identify_msg);
                ok = 0;
                ok = 0;
        }
        }
 
 
        if (!ok) {
        if (!ok) {
                /*
                /*
                 * Something went wrong - send an initiator error to
                 * Something went wrong - send an initiator error to
                 * the target.
                 * the target.
                 */
                 */
                outb(CMD_SETATN, REG_CMD(info));
                outb(CMD_SETATN, REG_CMD(info));
                outb(CMD_MSGACCEPTED, REG_CMD(info));
                outb(CMD_MSGACCEPTED, REG_CMD(info));
                msgqueue_flush(&info->scsi.msgs);
                msgqueue_flush(&info->scsi.msgs);
                msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
                msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
                return;
                return;
        }
        }
 
 
        target &= ~(1 << info->host->this_id);
        target &= ~(1 << info->host->this_id);
        switch (target) {
        switch (target) {
        case   1:  target = 0; break;
        case   1:  target = 0; break;
        case   2:  target = 1; break;
        case   2:  target = 1; break;
        case   4:  target = 2; break;
        case   4:  target = 2; break;
        case   8:  target = 3; break;
        case   8:  target = 3; break;
        case  16:  target = 4; break;
        case  16:  target = 4; break;
        case  32:  target = 5; break;
        case  32:  target = 5; break;
        case  64:  target = 6; break;
        case  64:  target = 6; break;
        case 128:  target = 7; break;
        case 128:  target = 7; break;
        default:   target = info->host->this_id; break;
        default:   target = info->host->this_id; break;
        }
        }
 
 
        identify_msg &= 7;
        identify_msg &= 7;
        info->scsi.reconnected.target = target;
        info->scsi.reconnected.target = target;
        info->scsi.reconnected.lun    = identify_msg;
        info->scsi.reconnected.lun    = identify_msg;
        info->scsi.reconnected.tag    = 0;
        info->scsi.reconnected.tag    = 0;
 
 
        ok = 0;
        ok = 0;
        if (info->scsi.disconnectable && info->SCpnt &&
        if (info->scsi.disconnectable && info->SCpnt &&
            info->SCpnt->target == target && info->SCpnt->lun == identify_msg)
            info->SCpnt->target == target && info->SCpnt->lun == identify_msg)
                ok = 1;
                ok = 1;
 
 
        if (!ok && queue_probetgtlun(&info->queues.disconnected, target, identify_msg))
        if (!ok && queue_probetgtlun(&info->queues.disconnected, target, identify_msg))
                ok = 1;
                ok = 1;
 
 
        msgqueue_flush(&info->scsi.msgs);
        msgqueue_flush(&info->scsi.msgs);
        if (ok) {
        if (ok) {
                info->scsi.phase = PHASE_RECONNECTED;
                info->scsi.phase = PHASE_RECONNECTED;
                outb(target, REG_SDID(info));
                outb(target, REG_SDID(info));
        } else {
        } else {
                /*
                /*
                 * Our command structure not found - abort the
                 * Our command structure not found - abort the
                 * command on the target.  Since we have no
                 * command on the target.  Since we have no
                 * record of this command, we can't send
                 * record of this command, we can't send
                 * an INITIATOR DETECTED ERROR message.
                 * an INITIATOR DETECTED ERROR message.
                 */
                 */
                outb(CMD_SETATN, REG_CMD(info));
                outb(CMD_SETATN, REG_CMD(info));
                msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
                msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
        }
        }
        outb(CMD_MSGACCEPTED, REG_CMD(info));
        outb(CMD_MSGACCEPTED, REG_CMD(info));
}
}
 
 
/* Function: void fas216_finish_reconnect(FAS216_Info *info)
/* Function: void fas216_finish_reconnect(FAS216_Info *info)
 * Purpose : finish reconnection sequence for device
 * Purpose : finish reconnection sequence for device
 * Params  : info - interface which caused function done interrupt
 * Params  : info - interface which caused function done interrupt
 */
 */
static void
static void
fas216_finish_reconnect(FAS216_Info *info)
fas216_finish_reconnect(FAS216_Info *info)
{
{
        fas216_checkmagic(info, "fas216_reconnect");
        fas216_checkmagic(info, "fas216_reconnect");
 
 
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
        printk("Connected: %1X %1X %02X, reconnected: %1X %1X %02X\n",
        printk("Connected: %1X %1X %02X, reconnected: %1X %1X %02X\n",
                info->SCpnt->target, info->SCpnt->lun, info->SCpnt->tag,
                info->SCpnt->target, info->SCpnt->lun, info->SCpnt->tag,
                info->scsi.reconnected.target, info->scsi.reconnected.lun,
                info->scsi.reconnected.target, info->scsi.reconnected.lun,
                info->scsi.reconnected.tag);
                info->scsi.reconnected.tag);
#endif
#endif
 
 
        if (info->scsi.disconnectable && info->SCpnt) {
        if (info->scsi.disconnectable && info->SCpnt) {
                info->scsi.disconnectable = 0;
                info->scsi.disconnectable = 0;
                if (info->SCpnt->target == info->scsi.reconnected.target &&
                if (info->SCpnt->target == info->scsi.reconnected.target &&
                    info->SCpnt->lun    == info->scsi.reconnected.lun &&
                    info->SCpnt->lun    == info->scsi.reconnected.lun &&
                    info->SCpnt->tag    == info->scsi.reconnected.tag) {
                    info->SCpnt->tag    == info->scsi.reconnected.tag) {
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
                        printk("scsi%d.%c: reconnected",
                        printk("scsi%d.%c: reconnected",
                                info->host->host_no, fas216_target(info));
                                info->host->host_no, fas216_target(info));
#endif
#endif
                } else {
                } else {
                        queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
                        queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
                        printk("scsi%d.%c: had to move command to disconnected queue\n",
                        printk("scsi%d.%c: had to move command to disconnected queue\n",
                                info->host->host_no, fas216_target(info));
                                info->host->host_no, fas216_target(info));
#endif
#endif
                        info->SCpnt = NULL;
                        info->SCpnt = NULL;
                }
                }
        }
        }
        if (!info->SCpnt) {
        if (!info->SCpnt) {
                info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
                info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
                                        info->scsi.reconnected.target,
                                        info->scsi.reconnected.target,
                                        info->scsi.reconnected.lun,
                                        info->scsi.reconnected.lun,
                                        info->scsi.reconnected.tag);
                                        info->scsi.reconnected.tag);
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
                printk("scsi%d.%c: had to get command",
                printk("scsi%d.%c: had to get command",
                        info->host->host_no, fas216_target(info));
                        info->host->host_no, fas216_target(info));
#endif
#endif
        }
        }
        if (!info->SCpnt) {
        if (!info->SCpnt) {
                outb(CMD_SETATN, REG_CMD(info));
                outb(CMD_SETATN, REG_CMD(info));
                msgqueue_flush(&info->scsi.msgs);
                msgqueue_flush(&info->scsi.msgs);
#if 0
#if 0
                if (info->scsi.reconnected.tag)
                if (info->scsi.reconnected.tag)
                        msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, info->scsi.reconnected.tag);
                        msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, info->scsi.reconnected.tag);
                else
                else
#endif
#endif
                        msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
                        msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
                info->scsi.phase = PHASE_MSGOUT_EXPECT;
                info->scsi.aborting = 1;
                info->scsi.aborting = 1;
        } else {
        } else {
                /*
                /*
                 * Restore data pointer from SAVED data pointer
                 * Restore data pointer from SAVED data pointer
                 */
                 */
                info->scsi.SCp = info->SCpnt->SCp;
                info->scsi.SCp = info->SCpnt->SCp;
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
                printk(", data pointers: [%p, %X]",
                printk(", data pointers: [%p, %X]",
                        info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
                        info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
#endif
#endif
        }
        }
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
        printk("\n");
        printk("\n");
#endif
#endif
}
}
 
 
static unsigned char fas216_get_msg_byte(FAS216_Info *info)
static unsigned char fas216_get_msg_byte(FAS216_Info *info)
{
{
        int tout;
        int tout;
 
 
        outb(CMD_MSGACCEPTED, REG_CMD(info));
        outb(CMD_MSGACCEPTED, REG_CMD(info));
        for (tout = 1000000; tout; tout --)
        for (tout = 1000000; tout; tout --)
                if (inb(REG_STAT(info)) & STAT_INT)
                if (inb(REG_STAT(info)) & STAT_INT)
                        break;
                        break;
 
 
        inb(REG_INST(info));
        inb(REG_INST(info));
 
 
        outb(CMD_TRANSFERINFO, REG_CMD(info));
        outb(CMD_TRANSFERINFO, REG_CMD(info));
 
 
        for (tout = 1000000; tout; tout --)
        for (tout = 1000000; tout; tout --)
                if (inb(REG_STAT(info)) & STAT_INT)
                if (inb(REG_STAT(info)) & STAT_INT)
                        break;
                        break;
 
 
        inb(REG_INST(info));
        inb(REG_INST(info));
 
 
        return inb(REG_FF(info));
        return inb(REG_FF(info));
}
}
 
 
/* Function: void fas216_message(FAS216_Info *info)
/* Function: void fas216_message(FAS216_Info *info)
 * Purpose : handle a function done interrupt from FAS216 chip
 * Purpose : handle a function done interrupt from FAS216 chip
 * Params  : info - interface which caused function done interrupt
 * Params  : info - interface which caused function done interrupt
 */
 */
static void fas216_message(FAS216_Info *info)
static void fas216_message(FAS216_Info *info)
{
{
        unsigned char message[16];
        unsigned char message[16];
        unsigned int msglen = 1;
        unsigned int msglen = 1;
 
 
        fas216_checkmagic(info, "fas216_message");
        fas216_checkmagic(info, "fas216_message");
 
 
        message[0] = inb(REG_FF(info));
        message[0] = inb(REG_FF(info));
 
 
        if (message[0] == EXTENDED_MESSAGE) {
        if (message[0] == EXTENDED_MESSAGE) {
                message[1] = fas216_get_msg_byte(info);
                message[1] = fas216_get_msg_byte(info);
 
 
                for (msglen = 2; msglen < message[1] + 2; msglen++)
                for (msglen = 2; msglen < message[1] + 2; msglen++)
                        message[msglen] = fas216_get_msg_byte(info);
                        message[msglen] = fas216_get_msg_byte(info);
        }
        }
 
 
#ifdef DEBUG_MESSAGES
#ifdef DEBUG_MESSAGES
        {
        {
                int i;
                int i;
 
 
                printk("scsi%d.%c: message in: ",
                printk("scsi%d.%c: message in: ",
                        info->host->host_no, fas216_target(info));
                        info->host->host_no, fas216_target(info));
                for (i = 0; i < msglen; i++)
                for (i = 0; i < msglen; i++)
                        printk("%02X ", message[i]);
                        printk("%02X ", message[i]);
                printk("\n");
                printk("\n");
        }
        }
#endif
#endif
 
 
        if (info->scsi.phase == PHASE_RECONNECTED) {
        if (info->scsi.phase == PHASE_RECONNECTED) {
                if (message[0] == SIMPLE_QUEUE_TAG)
                if (message[0] == SIMPLE_QUEUE_TAG)
                        info->scsi.reconnected.tag = message[1];
                        info->scsi.reconnected.tag = message[1];
                fas216_finish_reconnect(info);
                fas216_finish_reconnect(info);
                info->scsi.phase = PHASE_MSGIN;
                info->scsi.phase = PHASE_MSGIN;
        }
        }
 
 
        switch (message[0]) {
        switch (message[0]) {
        case COMMAND_COMPLETE:
        case COMMAND_COMPLETE:
                printk(KERN_ERR "scsi%d.%c: command complete with no "
                printk(KERN_ERR "scsi%d.%c: command complete with no "
                        "status in MESSAGE_IN?\n",
                        "status in MESSAGE_IN?\n",
                        info->host->host_no, fas216_target(info));
                        info->host->host_no, fas216_target(info));
                break;
                break;
 
 
        case SAVE_POINTERS:
        case SAVE_POINTERS:
                /*
                /*
                 * Save current data pointer to SAVED data pointer
                 * Save current data pointer to SAVED data pointer
                 * SCSI II standard says that we must not acknowledge
                 * SCSI II standard says that we must not acknowledge
                 * this until we have really saved pointers.
                 * this until we have really saved pointers.
                 * NOTE: we DO NOT save the command nor status pointers
                 * NOTE: we DO NOT save the command nor status pointers
                 * as required by the SCSI II standard.  These always
                 * as required by the SCSI II standard.  These always
                 * point to the start of their respective areas.
                 * point to the start of their respective areas.
                 */
                 */
                info->SCpnt->SCp = info->scsi.SCp;
                info->SCpnt->SCp = info->scsi.SCp;
                info->SCpnt->SCp.sent_command = 0;
                info->SCpnt->SCp.sent_command = 0;
#if defined (DEBUG_MESSAGES) || defined (DEBUG_CONNECT)
#if defined (DEBUG_MESSAGES) || defined (DEBUG_CONNECT)
                printk("scsi%d.%c: save data pointers: [%p, %X]\n",
                printk("scsi%d.%c: save data pointers: [%p, %X]\n",
                        info->host->host_no, fas216_target(info),
                        info->host->host_no, fas216_target(info),
                        info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
                        info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
#endif
#endif
                break;
                break;
 
 
        case RESTORE_POINTERS:
        case RESTORE_POINTERS:
                /*
                /*
                 * Restore current data pointer from SAVED data pointer
                 * Restore current data pointer from SAVED data pointer
                 */
                 */
                info->scsi.SCp = info->SCpnt->SCp;
                info->scsi.SCp = info->SCpnt->SCp;
#if defined (DEBUG_MESSAGES) || defined (DEBUG_CONNECT)
#if defined (DEBUG_MESSAGES) || defined (DEBUG_CONNECT)
                printk("scsi%d.%c: restore data pointers: [%p, %X]\n",
                printk("scsi%d.%c: restore data pointers: [%p, %X]\n",
                        info->host->host_no, fas216_target(info),
                        info->host->host_no, fas216_target(info),
                        info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
                        info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
#endif
#endif
                break;
                break;
 
 
        case DISCONNECT:
        case DISCONNECT:
                info->scsi.phase = PHASE_MSGIN_DISCONNECT;
                info->scsi.phase = PHASE_MSGIN_DISCONNECT;
                break;
                break;
 
 
        case MESSAGE_REJECT:
        case MESSAGE_REJECT:
printk("scsi%d.%c: message reject at %02X (%X)\n", info->host->host_no, fas216_target(info), info->scsi.msgin_fifo,
printk("scsi%d.%c: message reject at %02X (%X)\n", info->host->host_no, fas216_target(info), info->scsi.msgin_fifo,
        fas216_get_last_msg(info, info->scsi.msgin_fifo));
        fas216_get_last_msg(info, info->scsi.msgin_fifo));
                switch(fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
                switch(fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
                case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
                case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
                        fas216_handlesync(info, message);
                        fas216_handlesync(info, message);
                        break;
                        break;
 
 
                case EXTENDED_MESSAGE | EXTENDED_WDTR << 8:
                case EXTENDED_MESSAGE | EXTENDED_WDTR << 8:
                        fas216_handlewide(info, message);
                        fas216_handlewide(info, message);
                        break;
                        break;
 
 
                default:
                default:
                        printk("scsi%d.%c: reject, last message %04X\n",
                        printk("scsi%d.%c: reject, last message %04X\n",
                                info->host->host_no, fas216_target(info),
                                info->host->host_no, fas216_target(info),
                                fas216_get_last_msg(info, info->scsi.msgin_fifo));
                                fas216_get_last_msg(info, info->scsi.msgin_fifo));
                }
                }
                break;
                break;
 
 
        case NOP:
        case NOP:
                break;
                break;
 
 
        case SIMPLE_QUEUE_TAG:
        case SIMPLE_QUEUE_TAG:
                /* handled above */
                /* handled above */
                printk("scsi%d.%c: reconnect queue tag %02X\n",
                printk("scsi%d.%c: reconnect queue tag %02X\n",
                        info->host->host_no, fas216_target(info),
                        info->host->host_no, fas216_target(info),
                        message[1]);
                        message[1]);
                break;
                break;
 
 
        case EXTENDED_MESSAGE:
        case EXTENDED_MESSAGE:
                switch (message[2]) {
                switch (message[2]) {
                case EXTENDED_SDTR:     /* Sync transfer negociation request/reply */
                case EXTENDED_SDTR:     /* Sync transfer negociation request/reply */
                        fas216_handlesync(info, message);
                        fas216_handlesync(info, message);
                        break;
                        break;
 
 
                case EXTENDED_WDTR:     /* Wide transfer negociation request/reply */
                case EXTENDED_WDTR:     /* Wide transfer negociation request/reply */
                        fas216_handlewide(info, message);
                        fas216_handlewide(info, message);
                        break;
                        break;
 
 
                default:
                default:
                        printk("scsi%d.%c: unrecognised extended message %02X, rejecting\n",
                        printk("scsi%d.%c: unrecognised extended message %02X, rejecting\n",
                                info->host->host_no, fas216_target(info),
                                info->host->host_no, fas216_target(info),
                                message[2]);
                                message[2]);
                        goto reject_message;
                        goto reject_message;
                }
                }
                break;
                break;
 
 
        default:
        default:
                printk("scsi%d.%c: unrecognised message %02X, rejecting\n",
                printk("scsi%d.%c: unrecognised message %02X, rejecting\n",
                        info->host->host_no, fas216_target(info),
                        info->host->host_no, fas216_target(info),
                        message[0]);
                        message[0]);
                goto reject_message;
                goto reject_message;
        }
        }
        outb(CMD_MSGACCEPTED, REG_CMD(info));
        outb(CMD_MSGACCEPTED, REG_CMD(info));
        return;
        return;
 
 
reject_message:
reject_message:
        outb(CMD_SETATN, REG_CMD(info));
        outb(CMD_SETATN, REG_CMD(info));
        outb(CMD_MSGACCEPTED, REG_CMD(info));
        outb(CMD_MSGACCEPTED, REG_CMD(info));
        msgqueue_flush(&info->scsi.msgs);
        msgqueue_flush(&info->scsi.msgs);
        msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
        msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
        info->scsi.phase = PHASE_MSGOUT_EXPECT;
        info->scsi.phase = PHASE_MSGOUT_EXPECT;
}
}
 
 
/* Function: void fas216_send_command(FAS216_Info *info)
/* Function: void fas216_send_command(FAS216_Info *info)
 * Purpose : send a command to a target after all message bytes have been sent
 * Purpose : send a command to a target after all message bytes have been sent
 * Params  : info - interface which caused bus service
 * Params  : info - interface which caused bus service
 */
 */
static void fas216_send_command(FAS216_Info *info)
static void fas216_send_command(FAS216_Info *info)
{
{
        int i;
        int i;
 
 
        fas216_checkmagic(info, "fas216_send_command");
        fas216_checkmagic(info, "fas216_send_command");
 
 
        outb(CMD_NOP|CMD_WITHDMA, REG_CMD(info));
        outb(CMD_NOP|CMD_WITHDMA, REG_CMD(info));
        outb(CMD_FLUSHFIFO, REG_CMD(info));
        outb(CMD_FLUSHFIFO, REG_CMD(info));
 
 
        /* load command */
        /* load command */
        for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
        for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
                outb(info->SCpnt->cmnd[i], REG_FF(info));
                outb(info->SCpnt->cmnd[i], REG_FF(info));
 
 
        outb(CMD_TRANSFERINFO, REG_CMD(info));
        outb(CMD_TRANSFERINFO, REG_CMD(info));
 
 
        info->scsi.phase = PHASE_COMMAND;
        info->scsi.phase = PHASE_COMMAND;
}
}
 
 
/* Function: void fas216_send_messageout(FAS216_Info *info)
/* Function: void fas216_send_messageout(FAS216_Info *info)
 * Purpose : handle bus service to send a message
 * Purpose : handle bus service to send a message
 * Params  : info - interface which caused bus service
 * Params  : info - interface which caused bus service
 * Note    : We do not allow the device to change the data direction!
 * Note    : We do not allow the device to change the data direction!
 */
 */
static void fas216_send_messageout(FAS216_Info *info, int start)
static void fas216_send_messageout(FAS216_Info *info, int start)
{
{
        unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
        unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
 
 
        fas216_checkmagic(info, "fas216_send_messageout");
        fas216_checkmagic(info, "fas216_send_messageout");
 
 
        outb(CMD_FLUSHFIFO, REG_CMD(info));
        outb(CMD_FLUSHFIFO, REG_CMD(info));
 
 
        if (tot_msglen) {
        if (tot_msglen) {
                struct message *msg;
                struct message *msg;
                int msgnr = 0;
                int msgnr = 0;
 
 
                while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
                while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
                        int i;
                        int i;
 
 
                        for (i = start; i < msg->length; i++)
                        for (i = start; i < msg->length; i++)
                                outb(msg->msg[i], REG_FF(info));
                                outb(msg->msg[i], REG_FF(info));
 
 
                        msg->fifo = tot_msglen - (inb(REG_CFIS(info)) & CFIS_CF);
                        msg->fifo = tot_msglen - (inb(REG_CFIS(info)) & CFIS_CF);
                        start = 0;
                        start = 0;
                }
                }
        } else
        } else
                outb(NOP, REG_FF(info));
                outb(NOP, REG_FF(info));
 
 
        outb(CMD_TRANSFERINFO, REG_CMD(info));
        outb(CMD_TRANSFERINFO, REG_CMD(info));
 
 
        info->scsi.phase = PHASE_MSGOUT;
        info->scsi.phase = PHASE_MSGOUT;
}
}
 
 
/* Function: void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int ssr)
/* Function: void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int ssr)
 * Purpose : handle a bus service interrupt from FAS216 chip
 * Purpose : handle a bus service interrupt from FAS216 chip
 * Params  : info - interface which caused bus service interrupt
 * Params  : info - interface which caused bus service interrupt
 *           stat - Status register contents
 *           stat - Status register contents
 *           ssr  - SCSI Status register contents
 *           ssr  - SCSI Status register contents
 */
 */
static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int ssr)
static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int ssr)
{
{
        fas216_checkmagic(info, "fas216_busservice_intr");
        fas216_checkmagic(info, "fas216_busservice_intr");
 
 
#ifdef DEBUG_BUSSERVICE
#ifdef DEBUG_BUSSERVICE
        printk("scsi%d.%c: bus service: stat=%02X ssr=%02X phase=%02X\n",
        printk("scsi%d.%c: bus service: stat=%02X ssr=%02X phase=%02X\n",
                info->host->host_no, fas216_target(info), stat, ssr, info->scsi.phase);
                info->host->host_no, fas216_target(info), stat, ssr, info->scsi.phase);
#endif
#endif
        switch (ssr & IS_BITS) {
        switch (ssr & IS_BITS) {
        case IS_MSGBYTESENT:            /* select with ATN and stop steps completed     */
        case IS_MSGBYTESENT:            /* select with ATN and stop steps completed     */
        case IS_COMPLETE:                       /* last action completed                */
        case IS_COMPLETE:                       /* last action completed                */
                outb(CMD_NOP, REG_CMD(info));
                outb(CMD_NOP, REG_CMD(info));
 
 
#define STATE(st,ph) ((ph) << 3 | (st))
#define STATE(st,ph) ((ph) << 3 | (st))
                /* This table describes the legal SCSI state transitions,
                /* This table describes the legal SCSI state transitions,
                 * as described by the SCSI II spec.
                 * as described by the SCSI II spec.
                 */
                 */
                switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
                switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
                                                        /* Reselmsgin   -> Data In      */
                                                        /* Reselmsgin   -> Data In      */
                case STATE(STAT_DATAIN, PHASE_RECONNECTED):
                case STATE(STAT_DATAIN, PHASE_RECONNECTED):
                        fas216_finish_reconnect(info);
                        fas216_finish_reconnect(info);
                case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In      */
                case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In      */
                case STATE(STAT_DATAIN, PHASE_DATAIN):  /* Data In      -> Data In      */
                case STATE(STAT_DATAIN, PHASE_DATAIN):  /* Data In      -> Data In      */
                case STATE(STAT_DATAIN, PHASE_MSGOUT):  /* Message Out  -> Data In      */
                case STATE(STAT_DATAIN, PHASE_MSGOUT):  /* Message Out  -> Data In      */
                case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command      -> Data In      */
                case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command      -> Data In      */
                case STATE(STAT_DATAIN, PHASE_MSGIN):   /* Message In   -> Data In      */
                case STATE(STAT_DATAIN, PHASE_MSGIN):   /* Message In   -> Data In      */
                        fas216_starttransfer(info, DMA_IN, 0);
                        fas216_starttransfer(info, DMA_IN, 0);
                        return;
                        return;
 
 
                case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out     -> Data Out     */
                case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out     -> Data Out     */
                        fas216_starttransfer(info, DMA_OUT, 0);
                        fas216_starttransfer(info, DMA_OUT, 0);
                        return;
                        return;
 
 
                                                        /* Reselmsgin   -> Data Out     */
                                                        /* Reselmsgin   -> Data Out     */
                case STATE(STAT_DATAOUT, PHASE_RECONNECTED):
                case STATE(STAT_DATAOUT, PHASE_RECONNECTED):
                        fas216_finish_reconnect(info);
                        fas216_finish_reconnect(info);
                case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out     */
                case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out     */
                case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out  -> Data Out     */
                case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out  -> Data Out     */
                case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command      -> Data Out     */
                case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command      -> Data Out     */
                case STATE(STAT_DATAOUT, PHASE_MSGIN):  /* Message In   -> Data Out     */
                case STATE(STAT_DATAOUT, PHASE_MSGIN):  /* Message In   -> Data Out     */
                        fas216_starttransfer(info, DMA_OUT, 1);
                        fas216_starttransfer(info, DMA_OUT, 1);
                        return;
                        return;
 
 
                                                        /* Reselmsgin   -> Status       */
                                                        /* Reselmsgin   -> Status       */
                case STATE(STAT_STATUS, PHASE_RECONNECTED):
                case STATE(STAT_STATUS, PHASE_RECONNECTED):
                        fas216_finish_reconnect(info);
                        fas216_finish_reconnect(info);
                        goto status;
                        goto status;
                case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out     -> Status       */
                case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out     -> Status       */
                case STATE(STAT_STATUS, PHASE_DATAIN):  /* Data In      -> Status       */
                case STATE(STAT_STATUS, PHASE_DATAIN):  /* Data In      -> Status       */
                        fas216_stoptransfer(info);
                        fas216_stoptransfer(info);
                case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status       */
                case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status       */
                case STATE(STAT_STATUS, PHASE_MSGOUT):  /* Message Out  -> Status       */
                case STATE(STAT_STATUS, PHASE_MSGOUT):  /* Message Out  -> Status       */
                case STATE(STAT_STATUS, PHASE_COMMAND): /* Command      -> Status       */
                case STATE(STAT_STATUS, PHASE_COMMAND): /* Command      -> Status       */
                case STATE(STAT_STATUS, PHASE_MSGIN):   /* Message In   -> Status       */
                case STATE(STAT_STATUS, PHASE_MSGIN):   /* Message In   -> Status       */
                status:
                status:
                        outb(CMD_INITCMDCOMPLETE, REG_CMD(info));
                        outb(CMD_INITCMDCOMPLETE, REG_CMD(info));
                        info->scsi.phase = PHASE_STATUS;
                        info->scsi.phase = PHASE_STATUS;
                        return;
                        return;
 
 
                case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out     -> Message In   */
                case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out     -> Message In   */
                case STATE(STAT_MESGIN, PHASE_DATAIN):  /* Data In      -> Message In   */
                case STATE(STAT_MESGIN, PHASE_DATAIN):  /* Data In      -> Message In   */
                        fas216_stoptransfer(info);
                        fas216_stoptransfer(info);
                case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In   */
                case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In   */
                case STATE(STAT_MESGIN, PHASE_MSGOUT):  /* Message Out  -> Message In   */
                case STATE(STAT_MESGIN, PHASE_MSGOUT):  /* Message Out  -> Message In   */
                        info->scsi.msgin_fifo = inb(REG_CFIS(info)) & CFIS_CF;
                        info->scsi.msgin_fifo = inb(REG_CFIS(info)) & CFIS_CF;
                        outb(CMD_TRANSFERINFO, REG_CMD(info));
                        outb(CMD_TRANSFERINFO, REG_CMD(info));
                        info->scsi.phase = PHASE_MSGIN;
                        info->scsi.phase = PHASE_MSGIN;
                        return;
                        return;
 
 
                                                        /* Reselmsgin   -> Message In   */
                                                        /* Reselmsgin   -> Message In   */
                case STATE(STAT_MESGIN, PHASE_RECONNECTED):
                case STATE(STAT_MESGIN, PHASE_RECONNECTED):
                case STATE(STAT_MESGIN, PHASE_MSGIN):
                case STATE(STAT_MESGIN, PHASE_MSGIN):
                        info->scsi.msgin_fifo = inb(REG_CFIS(info)) & CFIS_CF;
                        info->scsi.msgin_fifo = inb(REG_CFIS(info)) & CFIS_CF;
                        outb(CMD_TRANSFERINFO, REG_CMD(info));
                        outb(CMD_TRANSFERINFO, REG_CMD(info));
                        return;
                        return;
 
 
                                                        /* Reselmsgin   -> Command      */
                                                        /* Reselmsgin   -> Command      */
                case STATE(STAT_COMMAND, PHASE_RECONNECTED):
                case STATE(STAT_COMMAND, PHASE_RECONNECTED):
                        fas216_finish_reconnect(info);
                        fas216_finish_reconnect(info);
                case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out  -> Command      */
                case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out  -> Command      */
                case STATE(STAT_COMMAND, PHASE_MSGIN):  /* Message In   -> Command      */
                case STATE(STAT_COMMAND, PHASE_MSGIN):  /* Message In   -> Command      */
                        fas216_send_command(info);
                        fas216_send_command(info);
                        info->scsi.phase = PHASE_COMMAND;
                        info->scsi.phase = PHASE_COMMAND;
                        return;
                        return;
                                                        /* Selection    -> Message Out  */
                                                        /* Selection    -> Message Out  */
                case STATE(STAT_MESGOUT, PHASE_SELECTION):
                case STATE(STAT_MESGOUT, PHASE_SELECTION):
                        fas216_send_messageout(info, 1);
                        fas216_send_messageout(info, 1);
                        return;
                        return;
                                                        /* Any          -> Message Out  */
                                                        /* Any          -> Message Out  */
                case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
                case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
                        fas216_send_messageout(info, 0);
                        fas216_send_messageout(info, 0);
                        return;
                        return;
 
 
                /* Error recovery rules.
                /* Error recovery rules.
                 *   These either attempt to abort or retry the operation.
                 *   These either attempt to abort or retry the operation.
                 * TODO: we need more of these
                 * TODO: we need more of these
                 */
                 */
                case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command      -> Command      */
                case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command      -> Command      */
                        /* error - we've sent out all the command bytes
                        /* error - we've sent out all the command bytes
                         * we have.
                         * we have.
                         * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
                         * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
                         * to include the command bytes sent for this to work
                         * to include the command bytes sent for this to work
                         * correctly.
                         * correctly.
                         */
                         */
                        printk(KERN_ERR "scsi%d.%c: "
                        printk(KERN_ERR "scsi%d.%c: "
                                "target trying to receive more command bytes\n",
                                "target trying to receive more command bytes\n",
                                info->host->host_no, fas216_target(info));
                                info->host->host_no, fas216_target(info));
                        outb(CMD_SETATN, REG_CMD(info));
                        outb(CMD_SETATN, REG_CMD(info));
                        outb(15, REG_STCL(info));
                        outb(15, REG_STCL(info));
                        outb(0, REG_STCM(info));
                        outb(0, REG_STCM(info));
                        outb(0, REG_STCH(info));
                        outb(0, REG_STCH(info));
                        outb(CMD_PADBYTES | CMD_WITHDMA, REG_CMD(info));
                        outb(CMD_PADBYTES | CMD_WITHDMA, REG_CMD(info));
                        msgqueue_flush(&info->scsi.msgs);
                        msgqueue_flush(&info->scsi.msgs);
                        msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
                        msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
                        info->scsi.phase = PHASE_MSGOUT_EXPECT;
                        info->scsi.phase = PHASE_MSGOUT_EXPECT;
                        return;
                        return;
 
 
                                                        /* Selection    -> Message Out  */
                                                        /* Selection    -> Message Out  */
                case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
                case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
                case STATE(STAT_MESGOUT, PHASE_MSGOUT): /* Message Out  -> Message Out  */
                case STATE(STAT_MESGOUT, PHASE_MSGOUT): /* Message Out  -> Message Out  */
                        /* If we get another message out phase, this
                        /* If we get another message out phase, this
                         * usually means some parity error occurred.
                         * usually means some parity error occurred.
                         * Resend complete set of messages.  If we have
                         * Resend complete set of messages.  If we have
                         * more than 1 byte to send, we need to assert
                         * more than 1 byte to send, we need to assert
                         * ATN again.
                         * ATN again.
                         */
                         */
                        if (msgqueue_msglength(&info->scsi.msgs) > 1)
                        if (msgqueue_msglength(&info->scsi.msgs) > 1)
                                outb(CMD_SETATN, REG_CMD(info));
                                outb(CMD_SETATN, REG_CMD(info));
 
 
                        fas216_send_messageout(info, 0);
                        fas216_send_messageout(info, 0);
                        return;
                        return;
                }
                }
 
 
                if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
                if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
                        printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
                        printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
                                info->host->host_no, fas216_target(info),
                                info->host->host_no, fas216_target(info),
                                fas216_bus_phase(stat));
                                fas216_bus_phase(stat));
                        msgqueue_flush(&info->scsi.msgs);
                        msgqueue_flush(&info->scsi.msgs);
                        outb(CMD_SETATN, REG_CMD(info));
                        outb(CMD_SETATN, REG_CMD(info));
                        msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
                        msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
                        info->scsi.phase = PHASE_MSGOUT_EXPECT;
                        info->scsi.phase = PHASE_MSGOUT_EXPECT;
                        info->scsi.aborting = 1;
                        info->scsi.aborting = 1;
                        outb(CMD_TRANSFERINFO, REG_CMD(info));
                        outb(CMD_TRANSFERINFO, REG_CMD(info));
                        return;
                        return;
                }
                }
                printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
                printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
                        info->host->host_no, fas216_target(info),
                        info->host->host_no, fas216_target(info),
                        fas216_bus_phase(stat),
                        fas216_bus_phase(stat),
                        fas216_drv_phase(info));
                        fas216_drv_phase(info));
                return;
                return;
 
 
        default:
        default:
                printk("scsi%d.%c: bus service at step %d?\n",
                printk("scsi%d.%c: bus service at step %d?\n",
                        info->host->host_no, fas216_target(info),
                        info->host->host_no, fas216_target(info),
                        ssr & IS_BITS);
                        ssr & IS_BITS);
        }
        }
}
}
 
 
/* Function: void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int ssr)
/* Function: void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int ssr)
 * Purpose : handle a function done interrupt from FAS216 chip
 * Purpose : handle a function done interrupt from FAS216 chip
 * Params  : info - interface which caused function done interrupt
 * Params  : info - interface which caused function done interrupt
 *           stat - Status register contents
 *           stat - Status register contents
 *           ssr  - SCSI Status register contents
 *           ssr  - SCSI Status register contents
 */
 */
static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int ssr)
static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int ssr)
{
{
        int status, message;
        int status, message;
 
 
        fas216_checkmagic(info, "fas216_funcdone_intr");
        fas216_checkmagic(info, "fas216_funcdone_intr");
 
 
#ifdef DEBUG_FUNCTIONDONE
#ifdef DEBUG_FUNCTIONDONE
        printk("scsi%d.%c: function done: stat=%X ssr=%X phase=%02X\n",
        printk("scsi%d.%c: function done: stat=%X ssr=%X phase=%02X\n",
                info->host->host_no, fas216_target(info), stat, ssr, info->scsi.phase);
                info->host->host_no, fas216_target(info), stat, ssr, info->scsi.phase);
#endif
#endif
        switch (info->scsi.phase) {
        switch (info->scsi.phase) {
        case PHASE_STATUS:                      /* status phase - read status and msg   */
        case PHASE_STATUS:                      /* status phase - read status and msg   */
                status = inb(REG_FF(info));
                status = inb(REG_FF(info));
                message = inb(REG_FF(info));
                message = inb(REG_FF(info));
                info->scsi.SCp.Message = message;
                info->scsi.SCp.Message = message;
                info->scsi.SCp.Status = status;
                info->scsi.SCp.Status = status;
                info->scsi.phase = PHASE_DONE;
                info->scsi.phase = PHASE_DONE;
                outb(CMD_MSGACCEPTED, REG_CMD(info));
                outb(CMD_MSGACCEPTED, REG_CMD(info));
                break;
                break;
 
 
        case PHASE_IDLE:                        /* reselected?                          */
        case PHASE_IDLE:                        /* reselected?                          */
        case PHASE_MSGIN:                       /* message in phase                     */
        case PHASE_MSGIN:                       /* message in phase                     */
        case PHASE_RECONNECTED:                 /* reconnected command                  */
        case PHASE_RECONNECTED:                 /* reconnected command                  */
                if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
                if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
                        info->scsi.msgin_fifo = inb(REG_CFIS(info)) & CFIS_CF;
                        info->scsi.msgin_fifo = inb(REG_CFIS(info)) & CFIS_CF;
                        fas216_message(info);
                        fas216_message(info);
                        break;
                        break;
                }
                }
 
 
        default:
        default:
                printk("scsi%d.%c: internal phase %s for function done?"
                printk("scsi%d.%c: internal phase %s for function done?"
                        "  What do I do with this?\n",
                        "  What do I do with this?\n",
                        info->host->host_no, fas216_target(info),
                        info->host->host_no, fas216_target(info),
                        fas216_drv_phase(info));
                        fas216_drv_phase(info));
        }
        }
}
}
 
 
/* Function: void fas216_intr(struct Scsi_Host *instance)
/* Function: void fas216_intr(struct Scsi_Host *instance)
 * Purpose : handle interrupts from the interface to progress a command
 * Purpose : handle interrupts from the interface to progress a command
 * Params  : instance - interface to service
 * Params  : instance - interface to service
 */
 */
void fas216_intr(struct Scsi_Host *instance)
void fas216_intr(struct Scsi_Host *instance)
{
{
        FAS216_Info *info = (FAS216_Info *)instance->hostdata;
        FAS216_Info *info = (FAS216_Info *)instance->hostdata;
        unsigned char isr, ssr, stat;
        unsigned char isr, ssr, stat;
 
 
        fas216_checkmagic(info, "fas216_intr");
        fas216_checkmagic(info, "fas216_intr");
 
 
        stat = inb(REG_STAT(info));
        stat = inb(REG_STAT(info));
        ssr = inb(REG_IS(info));
        ssr = inb(REG_IS(info));
        isr = inb(REG_INST(info));
        isr = inb(REG_INST(info));
 
 
        add_debug_list(stat, ssr, isr, info->scsi.phase);
        add_debug_list(stat, ssr, isr, info->scsi.phase);
 
 
        if (stat & STAT_INT) {
        if (stat & STAT_INT) {
                if (isr & INST_BUSRESET)
                if (isr & INST_BUSRESET)
                        printk("scsi%d.H: bus reset detected\n", instance->host_no);
                        printk("scsi%d.H: bus reset detected\n", instance->host_no);
                else if (isr & INST_ILLEGALCMD) {
                else if (isr & INST_ILLEGALCMD) {
                        printk(KERN_CRIT "scsi%d.H: illegal command given\n", instance->host_no);
                        printk(KERN_CRIT "scsi%d.H: illegal command given\n", instance->host_no);
                        fas216_dumpstate(info);
                        fas216_dumpstate(info);
                } else if (isr & INST_DISCONNECT)
                } else if (isr & INST_DISCONNECT)
                        fas216_disconnect_intr(info);
                        fas216_disconnect_intr(info);
                else if (isr & INST_RESELECTED)         /* reselected                   */
                else if (isr & INST_RESELECTED)         /* reselected                   */
                        fas216_reselected_intr(info);
                        fas216_reselected_intr(info);
                else if (isr & INST_BUSSERVICE)         /* bus service request          */
                else if (isr & INST_BUSSERVICE)         /* bus service request          */
                        fas216_busservice_intr(info, stat, ssr);
                        fas216_busservice_intr(info, stat, ssr);
                else if (isr & INST_FUNCDONE)           /* function done                */
                else if (isr & INST_FUNCDONE)           /* function done                */
                        fas216_funcdone_intr(info, stat, ssr);
                        fas216_funcdone_intr(info, stat, ssr);
                else
                else
                        printk("scsi%d.%c: unknown interrupt received:"
                        printk("scsi%d.%c: unknown interrupt received:"
                                " phase %s isr %02X ssr %02X stat %02X\n",
                                " phase %s isr %02X ssr %02X stat %02X\n",
                                instance->host_no, fas216_target(info),
                                instance->host_no, fas216_target(info),
                                fas216_drv_phase(info), isr, ssr, stat);
                                fas216_drv_phase(info), isr, ssr, stat);
        }
        }
}
}
 
 
/* Function: void fas216_kick(FAS216_Info *info)
/* Function: void fas216_kick(FAS216_Info *info)
 * Purpose : kick a command to the interface - interface should be idle
 * Purpose : kick a command to the interface - interface should be idle
 * Params  : info - our host interface to kick
 * Params  : info - our host interface to kick
 * Notes   : Interrupts are always disabled!
 * Notes   : Interrupts are always disabled!
 */
 */
static void fas216_kick(FAS216_Info *info)
static void fas216_kick(FAS216_Info *info)
{
{
        Scsi_Cmnd *SCpnt;
        Scsi_Cmnd *SCpnt;
        int tot_msglen, from_queue = 0;
        int tot_msglen, from_queue = 0;
 
 
        fas216_checkmagic(info, "fas216_kick");
        fas216_checkmagic(info, "fas216_kick");
 
 
        if (info->origSCpnt) {
        if (info->origSCpnt) {
                SCpnt = info->origSCpnt;
                SCpnt = info->origSCpnt;
                info->origSCpnt = NULL;
                info->origSCpnt = NULL;
        } else
        } else
                SCpnt = NULL;
                SCpnt = NULL;
 
 
        /* retrieve next command */
        /* retrieve next command */
        if (!SCpnt) {
        if (!SCpnt) {
                SCpnt = queue_remove_exclude(&info->queues.issue, info->busyluns);
                SCpnt = queue_remove_exclude(&info->queues.issue, info->busyluns);
                from_queue = 1;
                from_queue = 1;
        }
        }
 
 
        if (!SCpnt) /* no command pending - just exit */
        if (!SCpnt) /* no command pending - just exit */
                return;
                return;
 
 
        if (info->scsi.disconnectable && info->SCpnt) {
        if (info->scsi.disconnectable && info->SCpnt) {
                queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
                queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
                info->scsi.disconnectable = 0;
                info->scsi.disconnectable = 0;
                info->SCpnt = NULL;
                info->SCpnt = NULL;
                printk("scsi%d.%c: moved command to disconnected queue\n",
                printk("scsi%d.%c: moved command to disconnected queue\n",
                        info->host->host_no, fas216_target(info));
                        info->host->host_no, fas216_target(info));
        }
        }
 
 
        /*
        /*
         * tagged queuing - allocate a new tag to this command
         * tagged queuing - allocate a new tag to this command
         */
         */
        if (SCpnt->device->tagged_queue && SCpnt->cmnd[0] != REQUEST_SENSE) {
        if (SCpnt->device->tagged_queue && SCpnt->cmnd[0] != REQUEST_SENSE) {
                SCpnt->device->current_tag += 1;
                SCpnt->device->current_tag += 1;
                if (SCpnt->device->current_tag == 0)
                if (SCpnt->device->current_tag == 0)
                    SCpnt->device->current_tag = 1;
                    SCpnt->device->current_tag = 1;
                SCpnt->tag = SCpnt->device->current_tag;
                SCpnt->tag = SCpnt->device->current_tag;
        }
        }
 
 
        /*
        /*
         * claim host busy
         * claim host busy
         */
         */
        info->scsi.phase = PHASE_SELECTION;
        info->scsi.phase = PHASE_SELECTION;
        info->SCpnt = SCpnt;
        info->SCpnt = SCpnt;
        info->scsi.SCp = SCpnt->SCp;
        info->scsi.SCp = SCpnt->SCp;
        info->dma.transfer_type = fasdma_none;
        info->dma.transfer_type = fasdma_none;
 
 
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
        printk("scsi%d.%c: starting cmd %02X",
        printk("scsi%d.%c: starting cmd %02X",
                info->host->host_no, '0' + SCpnt->target,
                info->host->host_no, '0' + SCpnt->target,
                SCpnt->cmnd[0]);
                SCpnt->cmnd[0]);
#endif
#endif
 
 
        if (from_queue) {
        if (from_queue) {
#ifdef SCSI2_TAG
#ifdef SCSI2_TAG
                if (SCpnt->device->tagged_queue && SCpnt->cmnd[0] != REQUEST_SENSE &&
                if (SCpnt->device->tagged_queue && SCpnt->cmnd[0] != REQUEST_SENSE &&
                    SCpnt->cmnd[0] != INQUIRY) {
                    SCpnt->cmnd[0] != INQUIRY) {
                    SCpnt->device->current_tag += 1;
                    SCpnt->device->current_tag += 1;
                        if (SCpnt->device->current_tag == 0)
                        if (SCpnt->device->current_tag == 0)
                            SCpnt->device->current_tag = 1;
                            SCpnt->device->current_tag = 1;
                                SCpnt->tag = SCpnt->device->current_tag;
                                SCpnt->tag = SCpnt->device->current_tag;
                } else
                } else
#endif
#endif
                        set_bit(SCpnt->target * 8 + SCpnt->lun, info->busyluns);
                        set_bit(SCpnt->target * 8 + SCpnt->lun, info->busyluns);
 
 
                info->stats.removes += 1;
                info->stats.removes += 1;
                switch (SCpnt->cmnd[0]) {
                switch (SCpnt->cmnd[0]) {
                case WRITE_6:
                case WRITE_6:
                case WRITE_10:
                case WRITE_10:
                case WRITE_12:
                case WRITE_12:
                        info->stats.writes += 1;
                        info->stats.writes += 1;
                        break;
                        break;
                case READ_6:
                case READ_6:
                case READ_10:
                case READ_10:
                case READ_12:
                case READ_12:
                        info->stats.reads += 1;
                        info->stats.reads += 1;
                        break;
                        break;
                default:
                default:
                        info->stats.miscs += 1;
                        info->stats.miscs += 1;
                        break;
                        break;
                }
                }
        }
        }
 
 
        /* build outgoing message bytes */
        /* build outgoing message bytes */
        msgqueue_flush(&info->scsi.msgs);
        msgqueue_flush(&info->scsi.msgs);
 
 
        if (info->device[SCpnt->target].disconnect_ok)
        if (info->device[SCpnt->target].disconnect_ok)
                msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(1, SCpnt->lun));
                msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(1, SCpnt->lun));
        else
        else
                msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(0, SCpnt->lun));
                msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(0, SCpnt->lun));
 
 
        /* add tag message if required */
        /* add tag message if required */
        if (SCpnt->tag)
        if (SCpnt->tag)
                msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
                msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
 
 
#ifdef SCSI2_WIDE
#ifdef SCSI2_WIDE
        if (info->device[SCpnt->target].wide_state == neg_wait) {
        if (info->device[SCpnt->target].wide_state == neg_wait) {
                info->device[SCpnt->target].wide_state = neg_inprogress;
                info->device[SCpnt->target].wide_state = neg_inprogress;
                msgqueue_addmsg(&info->scsi.msgs, 4,
                msgqueue_addmsg(&info->scsi.msgs, 4,
                                EXTENDED_MESSAGE, 2, EXTENDED_WDTR,
                                EXTENDED_MESSAGE, 2, EXTENDED_WDTR,
                                info->ifcfg.wide_max_size);
                                info->ifcfg.wide_max_size);
        }
        }
#ifdef SCSI2_SYNC
#ifdef SCSI2_SYNC
        else
        else
#endif
#endif
#endif
#endif
#ifdef SCSI2_SYNC
#ifdef SCSI2_SYNC
        if ((info->device[SCpnt->target].sync_state == neg_wait ||
        if ((info->device[SCpnt->target].sync_state == neg_wait ||
             info->device[SCpnt->target].sync_state == neg_complete) &&
             info->device[SCpnt->target].sync_state == neg_complete) &&
            (SCpnt->cmnd[0] == REQUEST_SENSE ||
            (SCpnt->cmnd[0] == REQUEST_SENSE ||
             SCpnt->cmnd[0] == INQUIRY)) {
             SCpnt->cmnd[0] == INQUIRY)) {
                info->device[SCpnt->target].sync_state = neg_inprogress;
                info->device[SCpnt->target].sync_state = neg_inprogress;
                msgqueue_addmsg(&info->scsi.msgs, 5,
                msgqueue_addmsg(&info->scsi.msgs, 5,
                                EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
                                EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
                                1000 / info->ifcfg.clockrate,
                                1000 / info->ifcfg.clockrate,
                                info->ifcfg.sync_max_depth);
                                info->ifcfg.sync_max_depth);
        }
        }
#endif
#endif
 
 
        /* following what the ESP driver says */
        /* following what the ESP driver says */
        outb(0, REG_STCL(info));
        outb(0, REG_STCL(info));
        outb(0, REG_STCM(info));
        outb(0, REG_STCM(info));
        outb(0, REG_STCH(info));
        outb(0, REG_STCH(info));
        outb(CMD_NOP | CMD_WITHDMA, REG_CMD(info));
        outb(CMD_NOP | CMD_WITHDMA, REG_CMD(info));
 
 
        /* flush FIFO */
        /* flush FIFO */
        outb(CMD_FLUSHFIFO, REG_CMD(info));
        outb(CMD_FLUSHFIFO, REG_CMD(info));
 
 
        /* load bus-id and timeout */
        /* load bus-id and timeout */
        outb(BUSID(SCpnt->target), REG_SDID(info));
        outb(BUSID(SCpnt->target), REG_SDID(info));
        outb(info->ifcfg.select_timeout, REG_STIM(info));
        outb(info->ifcfg.select_timeout, REG_STIM(info));
 
 
        /* synchronous transfers */
        /* synchronous transfers */
        fas216_set_sync(info, SCpnt->target);
        fas216_set_sync(info, SCpnt->target);
 
 
        tot_msglen = msgqueue_msglength(&info->scsi.msgs);
        tot_msglen = msgqueue_msglength(&info->scsi.msgs);
 
 
        if (tot_msglen == 1 || tot_msglen == 3) {
        if (tot_msglen == 1 || tot_msglen == 3) {
                /*
                /*
                 * We have an easy message length to send...
                 * We have an easy message length to send...
                 */
                 */
                struct message *msg;
                struct message *msg;
                int msgnr = 0, i;
                int msgnr = 0, i;
 
 
                info->scsi.phase = PHASE_SELSTEPS;
                info->scsi.phase = PHASE_SELSTEPS;
 
 
                /* load message bytes */
                /* load message bytes */
                while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
                while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
                        for (i = 0; i < msg->length; i++)
                        for (i = 0; i < msg->length; i++)
                                outb(msg->msg[i], REG_FF(info));
                                outb(msg->msg[i], REG_FF(info));
                        msg->fifo = tot_msglen - (inb(REG_CFIS(info)) & CFIS_CF);
                        msg->fifo = tot_msglen - (inb(REG_CFIS(info)) & CFIS_CF);
                }
                }
 
 
                /* load command */
                /* load command */
                for (i = 0; i < SCpnt->cmd_len; i++)
                for (i = 0; i < SCpnt->cmd_len; i++)
                        outb(SCpnt->cmnd[i], REG_FF(info));
                        outb(SCpnt->cmnd[i], REG_FF(info));
 
 
                if (tot_msglen == 1)
                if (tot_msglen == 1)
                        outb(CMD_SELECTATN, REG_CMD(info));
                        outb(CMD_SELECTATN, REG_CMD(info));
                else
                else
                        outb(CMD_SELECTATN3, REG_CMD(info));
                        outb(CMD_SELECTATN3, REG_CMD(info));
        } else {
        } else {
                /*
                /*
                 * We have an unusual number of message bytes to send.
                 * We have an unusual number of message bytes to send.
                 *  Load first byte into fifo, and issue SELECT with ATN and
                 *  Load first byte into fifo, and issue SELECT with ATN and
                 *  stop steps.
                 *  stop steps.
                 */
                 */
                struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
                struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
 
 
                outb(msg->msg[0], REG_FF(info));
                outb(msg->msg[0], REG_FF(info));
                msg->fifo = 1;
                msg->fifo = 1;
 
 
                outb(CMD_SELECTATNSTOP, REG_CMD(info));
                outb(CMD_SELECTATNSTOP, REG_CMD(info));
        }
        }
 
 
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
        printk(", data pointers [%p, %X]\n",
        printk(", data pointers [%p, %X]\n",
                info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
                info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
#endif
#endif
        /* should now get either DISCONNECT or (FUNCTION DONE with BUS SERVICE) intr */
        /* should now get either DISCONNECT or (FUNCTION DONE with BUS SERVICE) intr */
}
}
 
 
/* Function: void fas216_done(FAS216_Info *info, unsigned int result)
/* Function: void fas216_done(FAS216_Info *info, unsigned int result)
 * Purpose : complete processing for command
 * Purpose : complete processing for command
 * Params  : info   - interface that completed
 * Params  : info   - interface that completed
 *           result - driver byte of result
 *           result - driver byte of result
 */
 */
static void fas216_done(FAS216_Info *info, unsigned int result)
static void fas216_done(FAS216_Info *info, unsigned int result)
{
{
        Scsi_Cmnd *SCpnt;
        Scsi_Cmnd *SCpnt;
 
 
        fas216_checkmagic(info, "fas216_done");
        fas216_checkmagic(info, "fas216_done");
 
 
        SCpnt = info->SCpnt;
        SCpnt = info->SCpnt;
 
 
        if (info->scsi.aborting) {
        if (info->scsi.aborting) {
                printk("scsi%d.%c: uncaught abort - returning DID_ABORT\n",
                printk("scsi%d.%c: uncaught abort - returning DID_ABORT\n",
                        info->host->host_no, fas216_target(info));
                        info->host->host_no, fas216_target(info));
                result = DID_ABORT;
                result = DID_ABORT;
                info->scsi.aborting = 0;
                info->scsi.aborting = 0;
        }
        }
 
 
        info->stats.fins += 1;
        info->stats.fins += 1;
 
 
        if (SCpnt) {
        if (SCpnt) {
                info->scsi.phase = PHASE_IDLE;
                info->scsi.phase = PHASE_IDLE;
                info->SCpnt = NULL;
                info->SCpnt = NULL;
 
 
                SCpnt->result = result << 16 | info->scsi.SCp.Message << 8 |
                SCpnt->result = result << 16 | info->scsi.SCp.Message << 8 |
                                info->scsi.SCp.Status;
                                info->scsi.SCp.Status;
 
 
                /*
                /*
                 * In theory, this should not happen, but just in case it does.
                 * In theory, this should not happen, but just in case it does.
                 */
                 */
                if (info->scsi.SCp.ptr &&
                if (info->scsi.SCp.ptr &&
                    info->scsi.SCp.this_residual &&
                    info->scsi.SCp.this_residual &&
                    result == DID_OK) {
                    result == DID_OK) {
                        switch (SCpnt->cmnd[0]) {
                        switch (SCpnt->cmnd[0]) {
                        case INQUIRY:
                        case INQUIRY:
                        case START_STOP:
                        case START_STOP:
                        case READ_CAPACITY:
                        case READ_CAPACITY:
                        case TEST_UNIT_READY:
                        case TEST_UNIT_READY:
                        case MODE_SENSE:
                        case MODE_SENSE:
                                break;
                                break;
 
 
                        default:
                        default:
                                switch (status_byte(SCpnt->result)) {
                                switch (status_byte(SCpnt->result)) {
                                case CHECK_CONDITION:
                                case CHECK_CONDITION:
                                case COMMAND_TERMINATED:
                                case COMMAND_TERMINATED:
                                case BUSY:
                                case BUSY:
                                case QUEUE_FULL:
                                case QUEUE_FULL:
                                case RESERVATION_CONFLICT:
                                case RESERVATION_CONFLICT:
                                        break;
                                        break;
 
 
                                default:
                                default:
                                        printk(KERN_ERR "scsi%d.H: incomplete data transfer "
                                        printk(KERN_ERR "scsi%d.H: incomplete data transfer "
                                                "detected: res=%08X ptr=%p len=%X command=",
                                                "detected: res=%08X ptr=%p len=%X command=",
                                                info->host->host_no, SCpnt->result,
                                                info->host->host_no, SCpnt->result,
                                                info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
                                                info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
                                        print_command(SCpnt->cmnd);
                                        print_command(SCpnt->cmnd);
                                }
                                }
                        }
                        }
                }
                }
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
                printk("scsi%d.%c: scsi command (%p) complete, result=%08X\n",
                printk("scsi%d.%c: scsi command (%p) complete, result=%08X\n",
                        info->host->host_no, fas216_target(info),
                        info->host->host_no, fas216_target(info),
                        SCpnt, SCpnt->result);
                        SCpnt, SCpnt->result);
#endif
#endif
 
 
                if (!SCpnt->scsi_done)
                if (!SCpnt->scsi_done)
                        panic("scsi%d.H: null scsi_done function in "
                        panic("scsi%d.H: null scsi_done function in "
                                "fas216_done", info->host->host_no);
                                "fas216_done", info->host->host_no);
 
 
                clear_bit(SCpnt->target * 8 + SCpnt->lun, info->busyluns);
                clear_bit(SCpnt->target * 8 + SCpnt->lun, info->busyluns);
 
 
                SCpnt->scsi_done(SCpnt);
                SCpnt->scsi_done(SCpnt);
        } else
        } else
                panic("scsi%d.H: null command in fas216_done", info->host->host_no);
                panic("scsi%d.H: null command in fas216_done", info->host->host_no);
 
 
        if (info->scsi.irq != NO_IRQ)
        if (info->scsi.irq != NO_IRQ)
                fas216_kick(info);
                fas216_kick(info);
}
}
 
 
/* Function: int fas216_queue_command(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
/* Function: int fas216_queue_command(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
 * Purpose : queue a command for adapter to process.
 * Purpose : queue a command for adapter to process.
 * Params  : SCpnt - Command to queue
 * Params  : SCpnt - Command to queue
 *           done  - done function to call once command is complete
 *           done  - done function to call once command is complete
 * Returns : 0 - success, else error
 * Returns : 0 - success, else error
 */
 */
int fas216_queue_command(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
int fas216_queue_command(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
{
{
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
 
 
        fas216_checkmagic(info, "fas216_queue_command");
        fas216_checkmagic(info, "fas216_queue_command");
 
 
#ifdef DEBUG_CONNECT
#ifdef DEBUG_CONNECT
        printk("scsi%d.%c: received queuable command (%p) %02X\n",
        printk("scsi%d.%c: received queuable command (%p) %02X\n",
                SCpnt->host->host_no, '0' + SCpnt->target,
                SCpnt->host->host_no, '0' + SCpnt->target,
                SCpnt, SCpnt->cmnd[0]);
                SCpnt, SCpnt->cmnd[0]);
#endif
#endif
 
 
        SCpnt->scsi_done = done;
        SCpnt->scsi_done = done;
        SCpnt->host_scribble = NULL;
        SCpnt->host_scribble = NULL;
        SCpnt->result = 0;
        SCpnt->result = 0;
        SCpnt->SCp.Message = 0;
        SCpnt->SCp.Message = 0;
        SCpnt->SCp.Status = 0;
        SCpnt->SCp.Status = 0;
 
 
        if (SCpnt->use_sg) {
        if (SCpnt->use_sg) {
                unsigned long len = 0;
                unsigned long len = 0;
                int buf;
                int buf;
 
 
                SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->buffer;
                SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->buffer;
                SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
                SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
                SCpnt->SCp.ptr = (char *) SCpnt->SCp.buffer->address;
                SCpnt->SCp.ptr = (char *) SCpnt->SCp.buffer->address;
                SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
                SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
                /*
                /*
                 * Calculate correct buffer length
                 * Calculate correct buffer length
                 */
                 */
                for (buf = 0; buf <= SCpnt->SCp.buffers_residual; buf++)
                for (buf = 0; buf <= SCpnt->SCp.buffers_residual; buf++)
                        len += SCpnt->SCp.buffer[buf].length;
                        len += SCpnt->SCp.buffer[buf].length;
                SCpnt->request_bufflen = len;
                SCpnt->request_bufflen = len;
        } else {
        } else {
                SCpnt->SCp.buffer = NULL;
                SCpnt->SCp.buffer = NULL;
                SCpnt->SCp.buffers_residual = 0;
                SCpnt->SCp.buffers_residual = 0;
                SCpnt->SCp.ptr = (unsigned char *)SCpnt->request_buffer;
                SCpnt->SCp.ptr = (unsigned char *)SCpnt->request_buffer;
                SCpnt->SCp.this_residual = SCpnt->request_bufflen;
                SCpnt->SCp.this_residual = SCpnt->request_bufflen;
        }
        }
 
 
        info->stats.queues += 1;
        info->stats.queues += 1;
        SCpnt->tag = 0;
        SCpnt->tag = 0;
 
 
        if (info->scsi.irq != NO_IRQ) {
        if (info->scsi.irq != NO_IRQ) {
                unsigned long flags;
                unsigned long flags;
 
 
                /* add command into execute queue and let it complete under
                /* add command into execute queue and let it complete under
                 * the drivers interrupts.
                 * the drivers interrupts.
                 */
                 */
                if (!queue_add_cmd_ordered(&info->queues.issue, SCpnt)) {
                if (!queue_add_cmd_ordered(&info->queues.issue, SCpnt)) {
                        SCpnt->result = DID_ERROR << 16;
                        SCpnt->result = DID_ERROR << 16;
                        done(SCpnt);
                        done(SCpnt);
                }
                }
                save_flags_cli(flags);
                save_flags_cli(flags);
                if (!info->SCpnt || info->scsi.disconnectable)
                if (!info->SCpnt || info->scsi.disconnectable)
                        fas216_kick(info);
                        fas216_kick(info);
                restore_flags(flags);
                restore_flags(flags);
        } else {
        } else {
                /* no interrupts to rely on - we'll have to handle the
                /* no interrupts to rely on - we'll have to handle the
                 * command ourselves.  For now, we give up.
                 * command ourselves.  For now, we give up.
                 */
                 */
                SCpnt->result = DID_ERROR << 16;
                SCpnt->result = DID_ERROR << 16;
                done(SCpnt);
                done(SCpnt);
        }
        }
        return 0;
        return 0;
}
}
 
 
/* Function: void fas216_internal_done(Scsi_Cmnd *SCpnt)
/* Function: void fas216_internal_done(Scsi_Cmnd *SCpnt)
 * Purpose : trigger restart of a waiting thread in fas216_command
 * Purpose : trigger restart of a waiting thread in fas216_command
 * Params  : SCpnt - Command to wake
 * Params  : SCpnt - Command to wake
 */
 */
static void fas216_internal_done(Scsi_Cmnd *SCpnt)
static void fas216_internal_done(Scsi_Cmnd *SCpnt)
{
{
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
 
 
        fas216_checkmagic(info, "fas216_internal_done");
        fas216_checkmagic(info, "fas216_internal_done");
 
 
        info->internal_done = 1;
        info->internal_done = 1;
}
}
 
 
/* Function: int fas216_command(Scsi_Cmnd *SCpnt)
/* Function: int fas216_command(Scsi_Cmnd *SCpnt)
 * Purpose : queue a command for adapter to process.
 * Purpose : queue a command for adapter to process.
 * Params  : SCpnt - Command to queue
 * Params  : SCpnt - Command to queue
 * Returns : scsi result code
 * Returns : scsi result code
 */
 */
int fas216_command(Scsi_Cmnd *SCpnt)
int fas216_command(Scsi_Cmnd *SCpnt)
{
{
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
        unsigned long flags;
        unsigned long flags;
 
 
        fas216_checkmagic(info, "fas216_command");
        fas216_checkmagic(info, "fas216_command");
 
 
        info->internal_done = 0;
        info->internal_done = 0;
        fas216_queue_command(SCpnt, fas216_internal_done);
        fas216_queue_command(SCpnt, fas216_internal_done);
 
 
        /*
        /*
         * This wastes time, since we can't return until the command is
         * This wastes time, since we can't return until the command is
         * complete. We can't seep either since we may get re-entered!
         * complete. We can't seep either since we may get re-entered!
         * However, we must re-enable interrupts, or else we'll be
         * However, we must re-enable interrupts, or else we'll be
         * waiting forever.
         * waiting forever.
         */
         */
        save_flags(flags);
        save_flags(flags);
        sti();
        sti();
 
 
        while (!info->internal_done)
        while (!info->internal_done)
                barrier();
                barrier();
 
 
        restore_flags(flags);
        restore_flags(flags);
 
 
        return SCpnt->result;
        return SCpnt->result;
}
}
 
 
/* Prototype: void fas216_reportstatus(Scsi_Cmnd **SCpntp1,
/* Prototype: void fas216_reportstatus(Scsi_Cmnd **SCpntp1,
 *                                     Scsi_Cmnd **SCpntp2, int result)
 *                                     Scsi_Cmnd **SCpntp2, int result)
 * Purpose  : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
 * Purpose  : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
 * Params   : SCpntp1 - pointer to command to return
 * Params   : SCpntp1 - pointer to command to return
 *            SCpntp2 - pointer to command to check
 *            SCpntp2 - pointer to command to check
 *            result  - result to pass back to mid-level done function
 *            result  - result to pass back to mid-level done function
 * Returns  : *SCpntp2 = NULL if *SCpntp1 is the same command
 * Returns  : *SCpntp2 = NULL if *SCpntp1 is the same command
 *            structure as *SCpntp2.
 *            structure as *SCpntp2.
 */
 */
static void fas216_reportstatus(Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2,
static void fas216_reportstatus(Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2,
                                int result)
                                int result)
{
{
        Scsi_Cmnd *SCpnt = *SCpntp1;
        Scsi_Cmnd *SCpnt = *SCpntp1;
 
 
        if (SCpnt) {
        if (SCpnt) {
                *SCpntp1 = NULL;
                *SCpntp1 = NULL;
 
 
                SCpnt->result = result;
                SCpnt->result = result;
                SCpnt->scsi_done(SCpnt);
                SCpnt->scsi_done(SCpnt);
        }
        }
 
 
        if (SCpnt == *SCpntp2)
        if (SCpnt == *SCpntp2)
                *SCpntp2 = NULL;
                *SCpntp2 = NULL;
}
}
 
 
/* Function: int fas216_abort(Scsi_Cmnd *SCpnt)
/* Function: int fas216_abort(Scsi_Cmnd *SCpnt)
 * Purpose : abort a command if something horrible happens.
 * Purpose : abort a command if something horrible happens.
 * Params  : SCpnt - Command that is believed to be causing a problem.
 * Params  : SCpnt - Command that is believed to be causing a problem.
 * Returns : one of SCSI_ABORT_ macros.
 * Returns : one of SCSI_ABORT_ macros.
 */
 */
int fas216_abort(Scsi_Cmnd *SCpnt)
int fas216_abort(Scsi_Cmnd *SCpnt)
{
{
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
        int result = SCSI_ABORT_SNOOZE;
        int result = SCSI_ABORT_SNOOZE;
 
 
        fas216_checkmagic(info, "fas216_abort");
        fas216_checkmagic(info, "fas216_abort");
 
 
        info->stats.aborts += 1;
        info->stats.aborts += 1;
 
 
        print_debug_list();
        print_debug_list();
        fas216_dumpstate(info);
        fas216_dumpstate(info);
        fas216_dumpinfo(info);
        fas216_dumpinfo(info);
        printk(KERN_WARNING "scsi%d: fas216_abort: ", info->host->host_no);
        printk(KERN_WARNING "scsi%d: fas216_abort: ", info->host->host_no);
 
 
        do {
        do {
                /* If command is waiting in the issue queue, then we can
                /* If command is waiting in the issue queue, then we can
                 * simply remove the command and return abort status
                 * simply remove the command and return abort status
                 */
                 */
                if (queue_removecmd(&info->queues.issue, SCpnt)) {
                if (queue_removecmd(&info->queues.issue, SCpnt)) {
                        SCpnt->result = DID_ABORT << 16;
                        SCpnt->result = DID_ABORT << 16;
                        SCpnt->scsi_done(SCpnt);
                        SCpnt->scsi_done(SCpnt);
                        printk("command on issue queue");
                        printk("command on issue queue");
                        result = SCSI_ABORT_SUCCESS;
                        result = SCSI_ABORT_SUCCESS;
                        break;
                        break;
                }
                }
 
 
                /* If the command is on the disconencted queue, we need to
                /* If the command is on the disconencted queue, we need to
                 * reconnect to the device
                 * reconnect to the device
                 */
                 */
                if (queue_cmdonqueue(&info->queues.disconnected, SCpnt))
                if (queue_cmdonqueue(&info->queues.disconnected, SCpnt))
                        printk("command on disconnected queue");
                        printk("command on disconnected queue");
 
 
                /* If the command is connected, we need to flag that the
                /* If the command is connected, we need to flag that the
                 * command needs to be aborted
                 * command needs to be aborted
                 */
                 */
                if (info->SCpnt == SCpnt)
                if (info->SCpnt == SCpnt)
                        printk("command executing");
                        printk("command executing");
 
 
                /* If the command is pending for execution, then again
                /* If the command is pending for execution, then again
                 * this is simple - we remove it and report abort status
                 * this is simple - we remove it and report abort status
                 */
                 */
                if (info->origSCpnt == SCpnt) {
                if (info->origSCpnt == SCpnt) {
                        info->origSCpnt = NULL;
                        info->origSCpnt = NULL;
                        SCpnt->result = DID_ABORT << 16;
                        SCpnt->result = DID_ABORT << 16;
                        SCpnt->scsi_done(SCpnt);
                        SCpnt->scsi_done(SCpnt);
                        printk("command waiting for execution");
                        printk("command waiting for execution");
                        result = SCSI_ABORT_SUCCESS;
                        result = SCSI_ABORT_SUCCESS;
                        break;
                        break;
                }
                }
        } while (0);
        } while (0);
 
 
        printk("\n");
        printk("\n");
 
 
        return result;
        return result;
}
}
 
 
/* Function: void fas216_reset_state(FAS216_Info *info)
/* Function: void fas216_reset_state(FAS216_Info *info)
 * Purpose : Initialise driver internal state
 * Purpose : Initialise driver internal state
 * Params  : info - state to initialise
 * Params  : info - state to initialise
 */
 */
static void fas216_reset_state(FAS216_Info *info)
static void fas216_reset_state(FAS216_Info *info)
{
{
        neg_t sync_state, wide_state;
        neg_t sync_state, wide_state;
        int i;
        int i;
 
 
        fas216_checkmagic(info, "fas216_reset_state");
        fas216_checkmagic(info, "fas216_reset_state");
 
 
        /*
        /*
         * Clear out all stale info in our state structure
         * Clear out all stale info in our state structure
         */
         */
        memset(info->busyluns, 0, sizeof(info->busyluns));
        memset(info->busyluns, 0, sizeof(info->busyluns));
        msgqueue_flush(&info->scsi.msgs);
        msgqueue_flush(&info->scsi.msgs);
        info->scsi.reconnected.target = 0;
        info->scsi.reconnected.target = 0;
        info->scsi.reconnected.lun = 0;
        info->scsi.reconnected.lun = 0;
        info->scsi.reconnected.tag = 0;
        info->scsi.reconnected.tag = 0;
        info->scsi.disconnectable = 0;
        info->scsi.disconnectable = 0;
        info->scsi.aborting = 0;
        info->scsi.aborting = 0;
        info->scsi.phase = PHASE_IDLE;
        info->scsi.phase = PHASE_IDLE;
        info->scsi.async_stp =
        info->scsi.async_stp =
                        fas216_syncperiod(info, info->ifcfg.asyncperiod);
                        fas216_syncperiod(info, info->ifcfg.asyncperiod);
 
 
        if (info->ifcfg.wide_max_size == 0)
        if (info->ifcfg.wide_max_size == 0)
                wide_state = neg_invalid;
                wide_state = neg_invalid;
        else
        else
#ifdef SCSI2_WIDE
#ifdef SCSI2_WIDE
                wide_state = neg_wait;
                wide_state = neg_wait;
#else
#else
                wide_state = neg_invalid;
                wide_state = neg_invalid;
#endif
#endif
 
 
        if (info->host->dma_channel == NO_DMA || !info->dma.setup)
        if (info->host->dma_channel == NO_DMA || !info->dma.setup)
                sync_state = neg_invalid;
                sync_state = neg_invalid;
        else
        else
#ifdef SCSI2_SYNC
#ifdef SCSI2_SYNC
                sync_state = neg_wait;
                sync_state = neg_wait;
#else
#else
                sync_state = neg_invalid;
                sync_state = neg_invalid;
#endif
#endif
 
 
        for (i = 0; i < 8; i++) {
        for (i = 0; i < 8; i++) {
                info->device[i].disconnect_ok   = info->ifcfg.disconnect_ok;
                info->device[i].disconnect_ok   = info->ifcfg.disconnect_ok;
                info->device[i].sync_state      = sync_state;
                info->device[i].sync_state      = sync_state;
                info->device[i].wide_state      = wide_state;
                info->device[i].wide_state      = wide_state;
                info->device[i].period          = info->ifcfg.asyncperiod / 4;
                info->device[i].period          = info->ifcfg.asyncperiod / 4;
                info->device[i].stp             = info->scsi.async_stp;
                info->device[i].stp             = info->scsi.async_stp;
                info->device[i].sof             = 0;
                info->device[i].sof             = 0;
                info->device[i].wide_xfer       = 0;
                info->device[i].wide_xfer       = 0;
        }
        }
}
}
 
 
/* Function: void fas216_init_chip(FAS216_Info *info)
/* Function: void fas216_init_chip(FAS216_Info *info)
 * Purpose : Initialise FAS216 state after reset
 * Purpose : Initialise FAS216 state after reset
 * Params  : info - state structure for interface
 * Params  : info - state structure for interface
 */
 */
static void fas216_init_chip(FAS216_Info *info)
static void fas216_init_chip(FAS216_Info *info)
{
{
        fas216_checkmagic(info, "fas216_init_chip");
        fas216_checkmagic(info, "fas216_init_chip");
 
 
        outb(fas216_clockrate(info->ifcfg.clockrate), REG_CLKF(info));
        outb(fas216_clockrate(info->ifcfg.clockrate), REG_CLKF(info));
        outb(info->scsi.cfg[0], REG_CNTL1(info));
        outb(info->scsi.cfg[0], REG_CNTL1(info));
        outb(info->scsi.cfg[1], REG_CNTL2(info));
        outb(info->scsi.cfg[1], REG_CNTL2(info));
        outb(info->scsi.cfg[2], REG_CNTL3(info));
        outb(info->scsi.cfg[2], REG_CNTL3(info));
        outb(info->ifcfg.select_timeout, REG_STIM(info));
        outb(info->ifcfg.select_timeout, REG_STIM(info));
        outb(0, REG_SOF(info));
        outb(0, REG_SOF(info));
        outb(info->scsi.async_stp, REG_STP(info));
        outb(info->scsi.async_stp, REG_STP(info));
        outb(info->scsi.cfg[0], REG_CNTL1(info));
        outb(info->scsi.cfg[0], REG_CNTL1(info));
}
}
 
 
/* Function: int fas216_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
/* Function: int fas216_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
 * Purpose : resets the adapter if something horrible happens.
 * Purpose : resets the adapter if something horrible happens.
 * Params  : SCpnt - Command that is believed to be causing a problem.
 * Params  : SCpnt - Command that is believed to be causing a problem.
 *           reset_flags - flags indicating reset type that is believed
 *           reset_flags - flags indicating reset type that is believed
 *           to be required.
 *           to be required.
 * Returns : one of SCSI_RESET_ macros, or'd with the SCSI_RESET_*_RESET
 * Returns : one of SCSI_RESET_ macros, or'd with the SCSI_RESET_*_RESET
 *           macros.
 *           macros.
 */
 */
int fas216_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
int fas216_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
{
{
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
        FAS216_Info *info = (FAS216_Info *)SCpnt->host->hostdata;
        Scsi_Cmnd *SCptr;
        Scsi_Cmnd *SCptr;
        int result = 0;
        int result = 0;
 
 
        fas216_checkmagic(info, "fas216_reset");
        fas216_checkmagic(info, "fas216_reset");
 
 
        /*
        /*
         * Validate that command is actually on one of our queues if we're doing
         * Validate that command is actually on one of our queues if we're doing
         * an asynchronous reset
         * an asynchronous reset
         */
         */
        if (reset_flags & SCSI_RESET_ASYNCHRONOUS &&
        if (reset_flags & SCSI_RESET_ASYNCHRONOUS &&
            SCpnt &&
            SCpnt &&
            info->SCpnt != SCpnt &&
            info->SCpnt != SCpnt &&
            info->origSCpnt != SCpnt &&
            info->origSCpnt != SCpnt &&
            !queue_cmdonqueue(&info->queues.disconnected, SCpnt) &&
            !queue_cmdonqueue(&info->queues.disconnected, SCpnt) &&
            !queue_cmdonqueue(&info->queues.issue, SCpnt)) {
            !queue_cmdonqueue(&info->queues.issue, SCpnt)) {
                printk("scsi%d: fas216_reset: asynchronous reset for unknown command\n",
                printk("scsi%d: fas216_reset: asynchronous reset for unknown command\n",
                       info->host->host_no);
                       info->host->host_no);
                return SCSI_RESET_NOT_RUNNING;
                return SCSI_RESET_NOT_RUNNING;
        }
        }
 
 
        info->stats.resets += 1;
        info->stats.resets += 1;
 
 
        print_debug_list();
        print_debug_list();
        printk(KERN_WARNING "scsi%d: fas216_reset: ", info->host->host_no);
        printk(KERN_WARNING "scsi%d: fas216_reset: ", info->host->host_no);
        if (SCpnt)
        if (SCpnt)
                printk(" for target %d ", SCpnt->target);
                printk(" for target %d ", SCpnt->target);
 
 
        outb(info->scsi.cfg[3], REG_CNTL3(info));
        outb(info->scsi.cfg[3], REG_CNTL3(info));
 
 
        fas216_stoptransfer(info);
        fas216_stoptransfer(info);
 
 
        switch (reset_flags & (SCSI_RESET_SUGGEST_BUS_RESET | SCSI_RESET_SUGGEST_HOST_RESET)) {
        switch (reset_flags & (SCSI_RESET_SUGGEST_BUS_RESET | SCSI_RESET_SUGGEST_HOST_RESET)) {
        case SCSI_RESET_SUGGEST_BUS_RESET:
        case SCSI_RESET_SUGGEST_BUS_RESET:
                outb(CMD_RESETSCSI, REG_CMD(info));
                outb(CMD_RESETSCSI, REG_CMD(info));
                outb(CMD_NOP, REG_CMD(info));
                outb(CMD_NOP, REG_CMD(info));
                result |= SCSI_RESET_BUS_RESET;
                result |= SCSI_RESET_BUS_RESET;
                break;
                break;
 
 
        case SCSI_RESET_SUGGEST_HOST_RESET:
        case SCSI_RESET_SUGGEST_HOST_RESET:
                outb(CMD_RESETCHIP, REG_CMD(info));
                outb(CMD_RESETCHIP, REG_CMD(info));
                outb(CMD_NOP, REG_CMD(info));
                outb(CMD_NOP, REG_CMD(info));
                result |= SCSI_RESET_HOST_RESET;
                result |= SCSI_RESET_HOST_RESET;
                break;
                break;
 
 
        default:
        default:
                outb(CMD_RESETCHIP, REG_CMD(info));
                outb(CMD_RESETCHIP, REG_CMD(info));
                outb(CMD_NOP, REG_CMD(info));
                outb(CMD_NOP, REG_CMD(info));
                outb(CMD_RESETSCSI, REG_CMD(info));
                outb(CMD_RESETSCSI, REG_CMD(info));
                result |= SCSI_RESET_HOST_RESET | SCSI_RESET_BUS_RESET;
                result |= SCSI_RESET_HOST_RESET | SCSI_RESET_BUS_RESET;
                break;
                break;
        }
        }
 
 
        udelay(300);
        udelay(300);
        fas216_reset_state(info);
        fas216_reset_state(info);
        fas216_init_chip(info);
        fas216_init_chip(info);
 
 
        /*
        /*
         * Signal all commands in progress have been reset
         * Signal all commands in progress have been reset
         */
         */
        fas216_reportstatus(&info->SCpnt, &SCpnt, DID_RESET << 16);
        fas216_reportstatus(&info->SCpnt, &SCpnt, DID_RESET << 16);
 
 
        while ((SCptr = queue_remove(&info->queues.disconnected)) != NULL)
        while ((SCptr = queue_remove(&info->queues.disconnected)) != NULL)
                fas216_reportstatus(&SCptr, &SCpnt, DID_RESET << 16);
                fas216_reportstatus(&SCptr, &SCpnt, DID_RESET << 16);
 
 
        if (SCpnt) {
        if (SCpnt) {
                /*
                /*
                 * Command not found on disconnected queue, nor currently
                 * Command not found on disconnected queue, nor currently
                 * executing command - check pending commands
                 * executing command - check pending commands
                 */
                 */
                if (info->origSCpnt == SCpnt)
                if (info->origSCpnt == SCpnt)
                        info->origSCpnt = NULL;
                        info->origSCpnt = NULL;
 
 
                queue_removecmd(&info->queues.issue, SCpnt);
                queue_removecmd(&info->queues.issue, SCpnt);
 
 
                SCpnt->result = DID_RESET << 16;
                SCpnt->result = DID_RESET << 16;
                SCpnt->scsi_done(SCpnt);
                SCpnt->scsi_done(SCpnt);
        }
        }
 
 
        printk("\n");
        printk("\n");
 
 
        return result | SCSI_RESET_SUCCESS;
        return result | SCSI_RESET_SUCCESS;
}
}
 
 
/* Function: int fas216_init(struct Scsi_Host *instance)
/* Function: int fas216_init(struct Scsi_Host *instance)
 * Purpose : initialise FAS/NCR/AMD SCSI ic.
 * Purpose : initialise FAS/NCR/AMD SCSI ic.
 * Params  : instance - a driver-specific filled-out structure
 * Params  : instance - a driver-specific filled-out structure
 * Returns : 0 on success
 * Returns : 0 on success
 */
 */
int fas216_init(struct Scsi_Host *instance)
int fas216_init(struct Scsi_Host *instance)
{
{
        FAS216_Info *info = (FAS216_Info *)instance->hostdata;
        FAS216_Info *info = (FAS216_Info *)instance->hostdata;
        unsigned long flags;
        unsigned long flags;
        int target_jiffies;
        int target_jiffies;
 
 
        info->magic_start = MAGIC;
        info->magic_start = MAGIC;
        info->magic_end = MAGIC;
        info->magic_end = MAGIC;
 
 
        info->host = instance;
        info->host = instance;
        info->scsi.cfg[0] = instance->this_id;
        info->scsi.cfg[0] = instance->this_id;
        info->scsi.cfg[1] = CNTL2_ENF | CNTL2_S2FE;
        info->scsi.cfg[1] = CNTL2_ENF | CNTL2_S2FE;
        info->scsi.cfg[2] = info->ifcfg.cntl3 | CNTL3_ADIDCHK | CNTL3_G2CB;
        info->scsi.cfg[2] = info->ifcfg.cntl3 | CNTL3_ADIDCHK | CNTL3_G2CB;
        info->scsi.type = "unknown";
        info->scsi.type = "unknown";
        info->SCpnt = NULL;
        info->SCpnt = NULL;
        fas216_reset_state(info);
        fas216_reset_state(info);
 
 
        memset(&info->stats, 0, sizeof(info->stats));
        memset(&info->stats, 0, sizeof(info->stats));
 
 
        msgqueue_initialise(&info->scsi.msgs);
        msgqueue_initialise(&info->scsi.msgs);
 
 
        if (!queue_initialise(&info->queues.issue))
        if (!queue_initialise(&info->queues.issue))
                return 1;
                return 1;
 
 
        if (!queue_initialise(&info->queues.disconnected)) {
        if (!queue_initialise(&info->queues.disconnected)) {
                queue_free(&info->queues.issue);
                queue_free(&info->queues.issue);
                return 1;
                return 1;
        }
        }
 
 
        outb(0, REG_CNTL3(info));
        outb(0, REG_CNTL3(info));
        outb(CNTL2_S2FE, REG_CNTL2(info));
        outb(CNTL2_S2FE, REG_CNTL2(info));
 
 
        if ((inb(REG_CNTL2(info)) & (~0xe0)) != CNTL2_S2FE) {
        if ((inb(REG_CNTL2(info)) & (~0xe0)) != CNTL2_S2FE) {
                info->scsi.type = "NCR53C90";
                info->scsi.type = "NCR53C90";
        } else {
        } else {
                outb(0, REG_CNTL2(info));
                outb(0, REG_CNTL2(info));
                outb(0, REG_CNTL3(info));
                outb(0, REG_CNTL3(info));
                outb(5, REG_CNTL3(info));
                outb(5, REG_CNTL3(info));
                if (inb(REG_CNTL3(info)) != 5) {
                if (inb(REG_CNTL3(info)) != 5) {
                        info->scsi.type = "NCR53C90A";
                        info->scsi.type = "NCR53C90A";
                } else {
                } else {
                        outb(0, REG_CNTL3(info));
                        outb(0, REG_CNTL3(info));
                        info->scsi.type = "NCR53C9x";
                        info->scsi.type = "NCR53C9x";
                }
                }
        }
        }
 
 
 
 
        outb(CNTL3_ADIDCHK, REG_CNTL3(info));
        outb(CNTL3_ADIDCHK, REG_CNTL3(info));
        outb(0, REG_CNTL3(info));
        outb(0, REG_CNTL3(info));
 
 
        outb(CMD_RESETCHIP, REG_CMD(info));
        outb(CMD_RESETCHIP, REG_CMD(info));
        outb(CMD_WITHDMA | CMD_NOP, REG_CMD(info));
        outb(CMD_WITHDMA | CMD_NOP, REG_CMD(info));
        outb(CNTL2_ENF, REG_CNTL2(info));
        outb(CNTL2_ENF, REG_CNTL2(info));
        outb(CMD_RESETCHIP, REG_CMD(info));
        outb(CMD_RESETCHIP, REG_CMD(info));
        switch (inb(REG1_ID(info))) {
        switch (inb(REG1_ID(info))) {
        case 12:
        case 12:
                info->scsi.type = "Am53CF94";
                info->scsi.type = "Am53CF94";
                break;
                break;
        default:
        default:
                break;
                break;
        }
        }
 
 
        udelay(300);
        udelay(300);
        /* now for the real initialisation */
        /* now for the real initialisation */
        fas216_init_chip(info);
        fas216_init_chip(info);
 
 
        outb(info->scsi.cfg[0] | CNTL1_DISR, REG_CNTL1(info));
        outb(info->scsi.cfg[0] | CNTL1_DISR, REG_CNTL1(info));
        outb(CMD_RESETSCSI, REG_CMD(info));
        outb(CMD_RESETSCSI, REG_CMD(info));
 
 
        /* scsi standard says 250ms */
        /* scsi standard says 250ms */
        target_jiffies = jiffies + (25 * HZ) / 100;
        target_jiffies = jiffies + (25 * HZ) / 100;
        save_flags(flags);
        save_flags(flags);
        sti();
        sti();
 
 
        while (jiffies < target_jiffies) barrier();
        while (jiffies < target_jiffies) barrier();
 
 
        restore_flags(flags);
        restore_flags(flags);
 
 
        outb(info->scsi.cfg[0], REG_CNTL1(info));
        outb(info->scsi.cfg[0], REG_CNTL1(info));
        inb(REG_INST(info));
        inb(REG_INST(info));
 
 
        /* now for the real initialisation */
        /* now for the real initialisation */
        fas216_init_chip(info);
        fas216_init_chip(info);
 
 
        fas216_checkmagic(info, "fas216_init");
        fas216_checkmagic(info, "fas216_init");
 
 
        return 0;
        return 0;
}
}
 
 
/* Function: int fas216_release(struct Scsi_Host *instance)
/* Function: int fas216_release(struct Scsi_Host *instance)
 * Purpose : release all resources and put everything to bed for
 * Purpose : release all resources and put everything to bed for
 *           FAS/NCR/AMD SCSI ic.
 *           FAS/NCR/AMD SCSI ic.
 * Params  : instance - a driver-specific filled-out structure
 * Params  : instance - a driver-specific filled-out structure
 * Returns : 0 on success
 * Returns : 0 on success
 */
 */
int fas216_release(struct Scsi_Host *instance)
int fas216_release(struct Scsi_Host *instance)
{
{
        FAS216_Info *info = (FAS216_Info *)instance->hostdata;
        FAS216_Info *info = (FAS216_Info *)instance->hostdata;
 
 
        fas216_checkmagic(info, "fas216_release");
        fas216_checkmagic(info, "fas216_release");
 
 
        outb(CMD_RESETCHIP, REG_CMD(info));
        outb(CMD_RESETCHIP, REG_CMD(info));
        queue_free(&info->queues.disconnected);
        queue_free(&info->queues.disconnected);
        queue_free(&info->queues.issue);
        queue_free(&info->queues.issue);
 
 
        return 0;
        return 0;
}
}
 
 
int fas216_print_stats(FAS216_Info *info, char *buffer)
int fas216_print_stats(FAS216_Info *info, char *buffer)
{
{
        return sprintf(buffer,
        return sprintf(buffer,
                        "Queued commands: %-10u   Issued commands: %-10u\n"
                        "Queued commands: %-10u   Issued commands: %-10u\n"
                        "Done commands  : %-10u   Reads          : %-10u\n"
                        "Done commands  : %-10u   Reads          : %-10u\n"
                        "Writes         : %-10u   Others         : %-10u\n"
                        "Writes         : %-10u   Others         : %-10u\n"
                        "Disconnects    : %-10u   Aborts         : %-10u\n"
                        "Disconnects    : %-10u   Aborts         : %-10u\n"
                        "Resets         : %-10u\n",
                        "Resets         : %-10u\n",
                        info->stats.queues,      info->stats.removes,
                        info->stats.queues,      info->stats.removes,
                        info->stats.fins,        info->stats.reads,
                        info->stats.fins,        info->stats.reads,
                        info->stats.writes,      info->stats.miscs,
                        info->stats.writes,      info->stats.miscs,
                        info->stats.disconnects, info->stats.aborts,
                        info->stats.disconnects, info->stats.aborts,
                        info->stats.resets);
                        info->stats.resets);
}
}
 
 
int fas216_print_device(FAS216_Info *info, Scsi_Device *scd, char *buffer)
int fas216_print_device(FAS216_Info *info, Scsi_Device *scd, char *buffer)
{
{
        struct fas216_device *dev = &info->device[scd->id];
        struct fas216_device *dev = &info->device[scd->id];
        int len = 0;
        int len = 0;
        char *p;
        char *p;
 
 
        proc_print_scsidevice(scd, buffer, &len, 0);
        proc_print_scsidevice(scd, buffer, &len, 0);
        p = buffer + len;
        p = buffer + len;
 
 
        p += sprintf(p, "  Extensions: ");
        p += sprintf(p, "  Extensions: ");
 
 
        if (scd->tagged_supported)
        if (scd->tagged_supported)
                p += sprintf(p, "TAG %sabled [%d] ",
                p += sprintf(p, "TAG %sabled [%d] ",
                             scd->tagged_queue ? "en" : "dis",
                             scd->tagged_queue ? "en" : "dis",
                             scd->current_tag);
                             scd->current_tag);
 
 
        p += sprintf(p, "\n  Transfers : %d-bit ",
        p += sprintf(p, "\n  Transfers : %d-bit ",
                     8 << dev->wide_xfer);
                     8 << dev->wide_xfer);
 
 
        if (dev->sof)
        if (dev->sof)
                p += sprintf(p, "sync offset %d, %d ns\n",
                p += sprintf(p, "sync offset %d, %d ns\n",
                                dev->sof, dev->period * 4);
                                dev->sof, dev->period * 4);
        else
        else
                p += sprintf(p, "async\n");
                p += sprintf(p, "async\n");
 
 
        return p - buffer;
        return p - buffer;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.