4  * Device Driver for Tekram DC395(U/UW/F), DC315(U)
 
   5  * PCI SCSI Bus Master Host Adapter
 
   6  * (SCSI chip set used Tekram ASIC TRM-S1040)
 
   9  *  C.L. Huang <ching@tekram.com.tw>
 
  10  *  Erich Chen <erich@tekram.com.tw>
 
  11  *  (C) Copyright 1995-1999 Tekram Technology Co., Ltd.
 
  13  *  Kurt Garloff <garloff@suse.de>
 
  14  *  (C) 1999-2000 Kurt Garloff
 
  16  *  Oliver Neukum <oliver@neukum.name>
 
  17  *  Ali Akcaagac <aliakc@web.de>
 
  18  *  Jamie Lenehan <lenehan@twibble.org>
 
  23  *************************************************************************
 
  25  * Redistribution and use in source and binary forms, with or without
 
  26  * modification, are permitted provided that the following conditions
 
  28  * 1. Redistributions of source code must retain the above copyright
 
  29  *    notice, this list of conditions and the following disclaimer.
 
  30  * 2. Redistributions in binary form must reproduce the above copyright
 
  31  *    notice, this list of conditions and the following disclaimer in the
 
  32  *    documentation and/or other materials provided with the distribution.
 
  33  * 3. The name of the author may not be used to endorse or promote products
 
  34  *    derived from this software without specific prior written permission.
 
  36  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 
  37  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
  38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
  39  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 
  40  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
  41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
  42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
  43  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
  44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 
  45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
  47  ************************************************************************
 
  49 #include <linux/module.h>
 
  50 #include <linux/moduleparam.h>
 
  51 #include <linux/delay.h>
 
  52 #include <linux/ctype.h>
 
  53 #include <linux/blkdev.h>
 
  54 #include <linux/interrupt.h>
 
  55 #include <linux/init.h>
 
  56 #include <linux/spinlock.h>
 
  57 #include <linux/pci.h>
 
  58 #include <linux/list.h>
 
  59 #include <linux/vmalloc.h>
 
  62 #include <scsi/scsi.h>
 
  63 #include <scsi/scsicam.h>       /* needed for scsicam_bios_param */
 
  64 #include <scsi/scsi_cmnd.h>
 
  65 #include <scsi/scsi_device.h>
 
  66 #include <scsi/scsi_host.h>
 
  70 #define DC395X_NAME     "dc395x"
 
  71 #define DC395X_BANNER   "Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
 
  72 #define DC395X_VERSION  "v2.05, 2004/03/08"
 
  74 /*---------------------------------------------------------------------------
 
  76  ---------------------------------------------------------------------------*/
 
  78  * Set to disable parts of the driver
 
  80 /*#define DC395x_NO_DISCONNECT*/
 
  81 /*#define DC395x_NO_TAGQ*/
 
  82 /*#define DC395x_NO_SYNC*/
 
  83 /*#define DC395x_NO_WIDE*/
 
  85 /*---------------------------------------------------------------------------
 
  87  ---------------------------------------------------------------------------*/
 
  89  * Types of debugging that can be enabled and disabled
 
  95 #define DBG_FIFO        0x0040
 
  96 #define DBG_PIO         0x0080
 
 100  * Set set of things to output debugging for.
 
 101  * Undefine to remove all debugging
 
 103 /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
 
 104 /*#define  DEBUG_MASK   DBG_0*/
 
 108  * Output a kernel mesage at the specified level and append the
 
 109  * driver name and a ": " to the start of the message
 
 111 #define dprintkl(level, format, arg...)  \
 
 112     printk(level DC395X_NAME ": " format , ## arg)
 
 117  * print a debug message - this is formated with KERN_DEBUG, then the
 
 118  * driver name followed by a ": " and then the message is output. 
 
 119  * This also checks that the specified debug level is enabled before
 
 120  * outputing the message
 
 122 #define dprintkdbg(type, format, arg...) \
 
 124                 if ((type) & (DEBUG_MASK)) \
 
 125                         dprintkl(KERN_DEBUG , format , ## arg); \
 
 129  * Check if the specified type of debugging is enabled
 
 131 #define debug_enabled(type)     ((DEBUG_MASK) & (type))
 
 135  * No debugging. Do nothing
 
 137 #define dprintkdbg(type, format, arg...) \
 
 139 #define debug_enabled(type)     (0)
 
 144 #ifndef PCI_VENDOR_ID_TEKRAM
 
 145 #define PCI_VENDOR_ID_TEKRAM                    0x1DE1  /* Vendor ID    */
 
 147 #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
 
 148 #define PCI_DEVICE_ID_TEKRAM_TRMS1040           0x0391  /* Device ID    */
 
 152 #define DC395x_LOCK_IO(dev,flags)               spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
 
 153 #define DC395x_UNLOCK_IO(dev,flags)             spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
 
 155 #define DC395x_read8(acb,address)               (u8)(inb(acb->io_port_base + (address)))
 
 156 #define DC395x_read16(acb,address)              (u16)(inw(acb->io_port_base + (address)))
 
 157 #define DC395x_read32(acb,address)              (u32)(inl(acb->io_port_base + (address)))
 
 158 #define DC395x_write8(acb,address,value)        outb((value), acb->io_port_base + (address))
 
 159 #define DC395x_write16(acb,address,value)       outw((value), acb->io_port_base + (address))
 
 160 #define DC395x_write32(acb,address,value)       outl((value), acb->io_port_base + (address))
 
 163 #define RES_TARGET              0x000000FF      /* Target State */
 
 164 #define RES_TARGET_LNX  STATUS_MASK     /* Only official ... */
 
 165 #define RES_ENDMSG              0x0000FF00      /* End Message */
 
 166 #define RES_DID                 0x00FF0000      /* DID_ codes */
 
 167 #define RES_DRV                 0xFF000000      /* DRIVER_ codes */
 
 169 #define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt))
 
 170 #define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1)
 
 172 #define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); }
 
 173 #define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; }
 
 174 #define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; }
 
 175 #define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; }
 
 176 #define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; }
 
 181  * srb->segement_x is the hw sg list. It is always allocated as a
 
 182  * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
 
 183  * cross a page boundy.
 
 185 #define SEGMENTX_LEN    (sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
 
 189         u32 address;            /* bus! address */
 
 193 /* The SEEPROM structure for TRM_S1040 */
 
 195         u8 cfg0;                /* Target configuration byte 0  */
 
 196         u8 period;              /* Target period                */
 
 197         u8 cfg2;                /* Target configuration byte 2  */
 
 198         u8 cfg3;                /* Target configuration byte 3  */
 
 202         u8 sub_vendor_id[2];    /* 0,1  Sub Vendor ID   */
 
 203         u8 sub_sys_id[2];       /* 2,3  Sub System ID   */
 
 204         u8 sub_class;           /* 4    Sub Class       */
 
 205         u8 vendor_id[2];        /* 5,6  Vendor ID       */
 
 206         u8 device_id[2];        /* 7,8  Device ID       */
 
 207         u8 reserved;            /* 9    Reserved        */
 
 208         struct NVRamTarget target[DC395x_MAX_SCSI_ID];
 
 215         u8 scsi_id;             /* 74 Host Adapter SCSI ID      */
 
 216         u8 channel_cfg;         /* 75 Channel configuration     */
 
 217         u8 delay_time;          /* 76 Power on delay time       */
 
 218         u8 max_tag;             /* 77 Maximum tags              */
 
 219         u8 reserved0;           /* 78  */
 
 220         u8 boot_target;         /* 79  */
 
 221         u8 boot_lun;            /* 80  */
 
 222         u8 reserved1;           /* 81  */
 
 223         u16 reserved2[22];      /* 82,..125 */
 
 224         u16 cksum;              /* 126,127 */
 
 228         struct list_head list;          /* next/prev ptrs for srb lists */
 
 229         struct DeviceCtlBlk *dcb;
 
 230         struct scsi_cmnd *cmd;
 
 232         struct SGentry *segment_x;      /* Linear array of hw sg entries (up to 64 entries) */
 
 233         u32 sg_bus_addr;                /* Bus address of sg list (ie, of segment_x) */
 
 235         u8 sg_count;                    /* No of HW sg entries for this request */
 
 236         u8 sg_index;                    /* Index of HW sg entry for this request */
 
 237         u32 total_xfer_length;          /* Total number of bytes remaining to be transfered */
 
 238         unsigned char *virt_addr;       /* Virtual address of current transfer position */
 
 241          * The sense buffer handling function, request_sense, uses
 
 242          * the first hw sg entry (segment_x[0]) and the transfer
 
 243          * length (total_xfer_length). While doing this it stores the
 
 244          * original values into the last sg hw list
 
 245          * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
 
 246          * total_xfer_length in xferred. These values are restored in
 
 247          * pci_unmap_srb_sense. This is the only place xferred is used.
 
 249         u32 xferred;                    /* Saved copy of total_xfer_length */
 
 269 struct DeviceCtlBlk {
 
 270         struct list_head list;          /* next/prev ptrs for the dcb list */
 
 271         struct AdapterCtlBlk *acb;
 
 272         struct list_head srb_going_list;        /* head of going srb list */
 
 273         struct list_head srb_waiting_list;      /* head of waiting srb list */
 
 275         struct ScsiReqBlk *active_srb;
 
 280         u8 target_id;           /* SCSI Target ID  (SCSI Only) */
 
 281         u8 target_lun;          /* SCSI Log.  Unit (SCSI Only) */
 
 285         u8 inquiry7;            /* To store Inquiry flags */
 
 286         u8 sync_mode;           /* 0:async mode */
 
 287         u8 min_nego_period;     /* for nego. */
 
 288         u8 sync_period;         /* for reg.  */
 
 290         u8 sync_offset;         /* for reg. and nego.(low nibble) */
 
 296 struct AdapterCtlBlk {
 
 297         struct Scsi_Host *scsi_host;
 
 299         unsigned long io_port_base;
 
 300         unsigned long io_port_len;
 
 302         struct list_head dcb_list;              /* head of going dcb list */
 
 303         struct DeviceCtlBlk *dcb_run_robin;
 
 304         struct DeviceCtlBlk *active_dcb;
 
 306         struct list_head srb_free_list;         /* head of free srb list */
 
 307         struct ScsiReqBlk *tmp_srb;
 
 308         struct timer_list waiting_timer;
 
 309         struct timer_list selto_timer;
 
 315         unsigned int irq_level;
 
 325         u8 dcb_map[DC395x_MAX_SCSI_ID];
 
 326         struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
 
 332         struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
 
 333         struct ScsiReqBlk srb;
 
 335         struct NvRamType eeprom;        /* eeprom settings for this adapter */
 
 339 /*---------------------------------------------------------------------------
 
 341  ---------------------------------------------------------------------------*/
 
 342 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 344 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 346 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 348 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 350 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 352 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 354 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 356 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 358 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 360 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 362 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 364 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 366 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
 368 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 
 
 370 static void set_basic_config(struct AdapterCtlBlk *acb);
 
 371 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
 
 372                 struct ScsiReqBlk *srb);
 
 373 static void reset_scsi_bus(struct AdapterCtlBlk *acb);
 
 374 static void data_io_transfer(struct AdapterCtlBlk *acb,
 
 375                 struct ScsiReqBlk *srb, u16 io_dir);
 
 376 static void disconnect(struct AdapterCtlBlk *acb);
 
 377 static void reselect(struct AdapterCtlBlk *acb);
 
 378 static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 
 379                 struct ScsiReqBlk *srb);
 
 380 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
 
 381                 struct ScsiReqBlk *srb);
 
 382 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
 
 383                 struct ScsiReqBlk *srb);
 
 384 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
 
 385                 struct scsi_cmnd *cmd, u8 force);
 
 386 static void scsi_reset_detect(struct AdapterCtlBlk *acb);
 
 387 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
 
 388 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
 
 389                 struct ScsiReqBlk *srb);
 
 390 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 
 391                 struct ScsiReqBlk *srb);
 
 392 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 
 393                 struct ScsiReqBlk *srb);
 
 394 static void set_xfer_rate(struct AdapterCtlBlk *acb,
 
 395                 struct DeviceCtlBlk *dcb);
 
 396 static void waiting_timeout(unsigned long ptr);
 
 399 /*---------------------------------------------------------------------------
 
 401  ---------------------------------------------------------------------------*/
 
 402 static u16 current_sync_offset = 0;
 
 404 static void *dc395x_scsi_phase0[] = {
 
 405         data_out_phase0,/* phase:0 */
 
 406         data_in_phase0, /* phase:1 */
 
 407         command_phase0, /* phase:2 */
 
 408         status_phase0,  /* phase:3 */
 
 409         nop0,           /* phase:4 PH_BUS_FREE .. initial phase */
 
 410         nop0,           /* phase:5 PH_BUS_FREE .. initial phase */
 
 411         msgout_phase0,  /* phase:6 */
 
 412         msgin_phase0,   /* phase:7 */
 
 415 static void *dc395x_scsi_phase1[] = {
 
 416         data_out_phase1,/* phase:0 */
 
 417         data_in_phase1, /* phase:1 */
 
 418         command_phase1, /* phase:2 */
 
 419         status_phase1,  /* phase:3 */
 
 420         nop1,           /* phase:4 PH_BUS_FREE .. initial phase */
 
 421         nop1,           /* phase:5 PH_BUS_FREE .. initial phase */
 
 422         msgout_phase1,  /* phase:6 */
 
 423         msgin_phase1,   /* phase:7 */
 
 427  *Fast20:       000      50ns, 20.0 MHz
 
 429  *              010     100ns, 10.0 MHz
 
 436  *Fast40(LVDS): 000      25ns, 40.0 MHz
 
 439  *              011     100ns, 10.0 MHz
 
 445 /*static u8     clock_period[] = {12,19,25,31,37,44,50,62};*/
 
 447 /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
 
 448 static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
 
 449 static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
 
 452 /*---------------------------------------------------------------------------
 
 454   ---------------------------------------------------------------------------*/
 
 456  * Module/boot parameters currently effect *all* instances of the
 
 457  * card in the system.
 
 461  * Command line parameters are stored in a structure below.
 
 462  * These are the index's into the structure for the various
 
 463  * command line options.
 
 465 #define CFG_ADAPTER_ID          0
 
 466 #define CFG_MAX_SPEED           1
 
 467 #define CFG_DEV_MODE            2
 
 468 #define CFG_ADAPTER_MODE        3
 
 470 #define CFG_RESET_DELAY         5
 
 472 #define CFG_NUM                 6       /* number of configuration items */
 
 476  * Value used to indicate that a command line override
 
 477  * hasn't been used to modify the value.
 
 479 #define CFG_PARAM_UNSET -1
 
 483  * Hold command line parameters.
 
 485 struct ParameterData {
 
 486         int value;              /* value of this setting */
 
 487         int min;                /* minimum value */
 
 488         int max;                /* maximum value */
 
 489         int def;                /* default value */
 
 490         int safe;               /* safe value */
 
 492 static struct ParameterData __devinitdata cfg_data[] = {
 
 511                 NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
 
 512                         NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
 
 514                 NTC_DO_PARITY_CHK | NTC_DO_SEND_START
 
 520 #ifdef CONFIG_SCSI_MULTI_LUN
 
 523                 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
 
 524                         /*| NAC_ACTIVE_NEG*/,
 
 525                 NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
 
 531                 3,      /* 16 tags (??) */
 
 545  * Safe settings. If set to zero the the BIOS/default values with
 
 546  * command line overrides will be used. If set to 1 then safe and
 
 547  * slow settings will be used.
 
 549 static int use_safe_settings = 0;
 
 550 module_param_named(safe, use_safe_settings, bool, 0);
 
 551 MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
 
 554 module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
 
 555 MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
 
 557 module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
 
 558 MODULE_PARM_DESC(max_speed, "Maximum bus speed. Default 1 (0-7) Speeds: 0=20, 1=13.3, 2=10, 3=8, 4=6.7, 5=5.8, 6=5, 7=4 Mhz");
 
 560 module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
 
 561 MODULE_PARM_DESC(dev_mode, "Device mode.");
 
 563 module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
 
 564 MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
 
 566 module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
 
 567 MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
 
 569 module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
 
 570 MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
 
 574  * set_safe_settings - if the use_safe_settings option is set then
 
 575  * set all values to the safe and slow values.
 
 577 static void __devinit set_safe_settings(void)
 
 579         if (use_safe_settings)
 
 583                 dprintkl(KERN_INFO, "Using safe settings.\n");
 
 584                 for (i = 0; i < CFG_NUM; i++)
 
 586                         cfg_data[i].value = cfg_data[i].safe;
 
 593  * fix_settings - reset any boot parameters which are out of range
 
 594  * back to the default values.
 
 596 static void __devinit fix_settings(void)
 
 601                 "setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
 
 602                 "AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
 
 603                 cfg_data[CFG_ADAPTER_ID].value,
 
 604                 cfg_data[CFG_MAX_SPEED].value,
 
 605                 cfg_data[CFG_DEV_MODE].value,
 
 606                 cfg_data[CFG_ADAPTER_MODE].value,
 
 607                 cfg_data[CFG_TAGS].value,
 
 608                 cfg_data[CFG_RESET_DELAY].value);
 
 609         for (i = 0; i < CFG_NUM; i++)
 
 611                 if (cfg_data[i].value < cfg_data[i].min
 
 612                     || cfg_data[i].value > cfg_data[i].max)
 
 613                         cfg_data[i].value = cfg_data[i].def;
 
 620  * Mapping from the eeprom delay index value (index into this array)
 
 621  * to the the number of actual seconds that the delay should be for.
 
 623 static char __devinitdata eeprom_index_to_delay_map[] = 
 
 624         { 1, 3, 5, 10, 16, 30, 60, 120 };
 
 628  * eeprom_index_to_delay - Take the eeprom delay setting and convert it
 
 629  * into a number of seconds.
 
 631  * @eeprom: The eeprom structure in which we find the delay index to map.
 
 633 static void __devinit eeprom_index_to_delay(struct NvRamType *eeprom)
 
 635         eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
 
 640  * delay_to_eeprom_index - Take a delay in seconds and return the
 
 641  * closest eeprom index which will delay for at least that amount of
 
 644  * @delay: The delay, in seconds, to find the eeprom index for.
 
 646 static int __devinit delay_to_eeprom_index(int delay)
 
 649         while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
 
 656  * eeprom_override - Override the eeprom settings, in the provided
 
 657  * eeprom structure, with values that have been set on the command
 
 660  * @eeprom: The eeprom data to override with command line options.
 
 662 static void __devinit eeprom_override(struct NvRamType *eeprom)
 
 666         /* Adapter Settings */
 
 667         if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
 
 668                 eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
 
 670         if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
 
 671                 eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
 
 673         if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
 
 674                 eeprom->delay_time = delay_to_eeprom_index(
 
 675                                         cfg_data[CFG_RESET_DELAY].value);
 
 677         if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
 
 678                 eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
 
 680         /* Device Settings */
 
 681         for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
 
 682                 if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
 
 683                         eeprom->target[id].cfg0 =
 
 684                                 (u8)cfg_data[CFG_DEV_MODE].value;
 
 686                 if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
 
 687                         eeprom->target[id].period =
 
 688                                 (u8)cfg_data[CFG_MAX_SPEED].value;
 
 694 /*---------------------------------------------------------------------------
 
 695  ---------------------------------------------------------------------------*/
 
 697 static unsigned int list_size(struct list_head *head)
 
 699         unsigned int count = 0;
 
 700         struct list_head *pos;
 
 701         list_for_each(pos, head)
 
 707 static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
 
 708                 struct DeviceCtlBlk *pos)
 
 711         struct DeviceCtlBlk* next = NULL;
 
 712         struct DeviceCtlBlk* i;
 
 714         if (list_empty(head))
 
 717         /* find supplied dcb and then select the next one */
 
 718         list_for_each_entry(i, head, list)
 
 722                 } else if (i == pos) {
 
 725         /* if no next one take the head one (ie, wraparound) */
 
 727                 list_for_each_entry(i, head, list) {
 
 736 static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
 
 738         if (srb->tag_number < 255) {
 
 739                 dcb->tag_mask &= ~(1 << srb->tag_number);       /* free tag mask */
 
 740                 srb->tag_number = 255;
 
 745 /* Find cmd in SRB list */
 
 746 static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
 
 747                 struct list_head *head)
 
 749         struct ScsiReqBlk *i;
 
 750         list_for_each_entry(i, head, list)
 
 757 static struct ScsiReqBlk *srb_get_free(struct AdapterCtlBlk *acb)
 
 759         struct list_head *head = &acb->srb_free_list;
 
 760         struct ScsiReqBlk *srb = NULL;
 
 762         if (!list_empty(head)) {
 
 763                 srb = list_entry(head->next, struct ScsiReqBlk, list);
 
 764                 list_del(head->next);
 
 765                 dprintkdbg(DBG_0, "srb_get_free: srb=%p\n", srb);
 
 771 static void srb_free_insert(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
 
 773         dprintkdbg(DBG_0, "srb_free_insert: srb=%p\n", srb);
 
 774         list_add_tail(&srb->list, &acb->srb_free_list);
 
 778 static void srb_waiting_insert(struct DeviceCtlBlk *dcb,
 
 779                 struct ScsiReqBlk *srb)
 
 781         dprintkdbg(DBG_0, "srb_waiting_insert: (pid#%li) <%02i-%i> srb=%p\n",
 
 782                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
 
 783         list_add(&srb->list, &dcb->srb_waiting_list);
 
 787 static void srb_waiting_append(struct DeviceCtlBlk *dcb,
 
 788                 struct ScsiReqBlk *srb)
 
 790         dprintkdbg(DBG_0, "srb_waiting_append: (pid#%li) <%02i-%i> srb=%p\n",
 
 791                  srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
 
 792         list_add_tail(&srb->list, &dcb->srb_waiting_list);
 
 796 static void srb_going_append(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
 
 798         dprintkdbg(DBG_0, "srb_going_append: (pid#%li) <%02i-%i> srb=%p\n",
 
 799                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
 
 800         list_add_tail(&srb->list, &dcb->srb_going_list);
 
 804 static void srb_going_remove(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
 
 806         struct ScsiReqBlk *i;
 
 807         struct ScsiReqBlk *tmp;
 
 808         dprintkdbg(DBG_0, "srb_going_remove: (pid#%li) <%02i-%i> srb=%p\n",
 
 809                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
 
 811         list_for_each_entry_safe(i, tmp, &dcb->srb_going_list, list)
 
 813                         list_del(&srb->list);
 
 819 static void srb_waiting_remove(struct DeviceCtlBlk *dcb,
 
 820                 struct ScsiReqBlk *srb)
 
 822         struct ScsiReqBlk *i;
 
 823         struct ScsiReqBlk *tmp;
 
 824         dprintkdbg(DBG_0, "srb_waiting_remove: (pid#%li) <%02i-%i> srb=%p\n",
 
 825                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
 
 827         list_for_each_entry_safe(i, tmp, &dcb->srb_waiting_list, list)
 
 829                         list_del(&srb->list);
 
 835 static void srb_going_to_waiting_move(struct DeviceCtlBlk *dcb,
 
 836                 struct ScsiReqBlk *srb)
 
 839                 "srb_going_to_waiting_move: (pid#%li) <%02i-%i> srb=%p\n",
 
 840                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
 
 841         list_move(&srb->list, &dcb->srb_waiting_list);
 
 845 static void srb_waiting_to_going_move(struct DeviceCtlBlk *dcb,
 
 846                 struct ScsiReqBlk *srb)
 
 849                 "srb_waiting_to_going_move: (pid#%li) <%02i-%i> srb=%p\n",
 
 850                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
 
 851         list_move(&srb->list, &dcb->srb_going_list);
 
 855 /* Sets the timer to wake us up */
 
 856 static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
 
 858         if (timer_pending(&acb->waiting_timer))
 
 860         init_timer(&acb->waiting_timer);
 
 861         acb->waiting_timer.function = waiting_timeout;
 
 862         acb->waiting_timer.data = (unsigned long) acb;
 
 863         if (time_before(jiffies + to, acb->scsi_host->last_reset - HZ / 2))
 
 864                 acb->waiting_timer.expires =
 
 865                     acb->scsi_host->last_reset - HZ / 2 + 1;
 
 867                 acb->waiting_timer.expires = jiffies + to + 1;
 
 868         add_timer(&acb->waiting_timer);
 
 872 /* Send the next command from the waiting list to the bus */
 
 873 static void waiting_process_next(struct AdapterCtlBlk *acb)
 
 875         struct DeviceCtlBlk *start = NULL;
 
 876         struct DeviceCtlBlk *pos;
 
 877         struct DeviceCtlBlk *dcb;
 
 878         struct ScsiReqBlk *srb;
 
 879         struct list_head *dcb_list_head = &acb->dcb_list;
 
 882             || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
 
 885         if (timer_pending(&acb->waiting_timer))
 
 886                 del_timer(&acb->waiting_timer);
 
 888         if (list_empty(dcb_list_head))
 
 892          * Find the starting dcb. Need to find it again in the list
 
 893          * since the list may have changed since we set the ptr to it
 
 895         list_for_each_entry(dcb, dcb_list_head, list)
 
 896                 if (dcb == acb->dcb_run_robin) {
 
 901                 /* This can happen! */
 
 902                 start = list_entry(dcb_list_head->next, typeof(*start), list);
 
 903                 acb->dcb_run_robin = start;
 
 908          * Loop over the dcb, but we start somewhere (potentially) in
 
 909          * the middle of the loop so we need to manully do this.
 
 913                 struct list_head *waiting_list_head = &pos->srb_waiting_list;
 
 915                 /* Make sure, the next another device gets scheduled ... */
 
 916                 acb->dcb_run_robin = dcb_get_next(dcb_list_head,
 
 919                 if (list_empty(waiting_list_head) ||
 
 920                     pos->max_command <= list_size(&pos->srb_going_list)) {
 
 921                         /* move to next dcb */
 
 922                         pos = dcb_get_next(dcb_list_head, pos);
 
 924                         srb = list_entry(waiting_list_head->next,
 
 925                                          struct ScsiReqBlk, list);
 
 927                         /* Try to send to the bus */
 
 928                         if (!start_scsi(acb, pos, srb))
 
 929                                 srb_waiting_to_going_move(pos, srb);
 
 931                                 waiting_set_timer(acb, HZ/50);
 
 934         } while (pos != start);
 
 938 /* Wake up waiting queue */
 
 939 static void waiting_timeout(unsigned long ptr)
 
 942         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
 
 944                 "waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
 
 945         DC395x_LOCK_IO(acb->scsi_host, flags);
 
 946         waiting_process_next(acb);
 
 947         DC395x_UNLOCK_IO(acb->scsi_host, flags);
 
 951 /* Get the DCB for a given ID/LUN combination */
 
 952 static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
 
 954         return acb->children[id][lun];
 
 958 /* Send SCSI Request Block (srb) to adapter (acb) */
 
 959 static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
 
 961         struct DeviceCtlBlk *dcb = srb->dcb;
 
 963         if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
 
 965             (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
 
 966                 srb_waiting_append(dcb, srb);
 
 967                 waiting_process_next(acb);
 
 971         if (!start_scsi(acb, dcb, srb))
 
 972                 srb_going_append(dcb, srb);
 
 974                 srb_waiting_insert(dcb, srb);
 
 975                 waiting_set_timer(acb, HZ / 50);
 
 979 static inline void pio_trigger(void)
 
 981         static int feedback_requested;
 
 983         if (!feedback_requested) {
 
 984                 feedback_requested = 1;
 
 985                 printk(KERN_WARNING "%s: Please, contact <linux-scsi@vger.kernel.org> "
 
 986                        "to help improve support for your system.\n", __FILE__);
 
 990 /* Prepare SRB for being sent to Device DCB w/ command *cmd */
 
 991 static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
 
 992                 struct ScsiReqBlk *srb)
 
 994         enum dma_data_direction dir = cmd->sc_data_direction;
 
 995         dprintkdbg(DBG_0, "build_srb: (pid#%li) <%02i-%i>\n",
 
 996                 cmd->pid, dcb->target_id, dcb->target_lun);
 
1001         srb->total_xfer_length = 0;
 
1002         srb->sg_bus_addr = 0;
 
1003         srb->virt_addr = NULL;
 
1005         srb->adapter_status = 0;
 
1006         srb->target_status = 0;
 
1011         srb->retry_count = 0;
 
1012         srb->tag_number = TAG_NONE;
 
1013         srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
 
1014         srb->end_message = 0;
 
1016         if (dir == PCI_DMA_NONE || !cmd->request_buffer) {
 
1018                         "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
 
1019                         cmd->bufflen, cmd->request_buffer,
 
1020                         cmd->use_sg, srb->segment_x[0].address);
 
1021         } else if (cmd->use_sg) {
 
1023                 u32 reqlen = cmd->request_bufflen;
 
1024                 struct scatterlist *sl = (struct scatterlist *)
 
1025                                          cmd->request_buffer;
 
1026                 struct SGentry *sgp = srb->segment_x;
 
1027                 srb->sg_count = pci_map_sg(dcb->acb->dev, sl, cmd->use_sg,
 
1030                         "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
 
1031                         reqlen, cmd->request_buffer, cmd->use_sg,
 
1034                 srb->virt_addr = page_address(sl->page);
 
1035                 for (i = 0; i < srb->sg_count; i++) {
 
1036                         u32 busaddr = (u32)sg_dma_address(&sl[i]);
 
1037                         u32 seglen = (u32)sl[i].length;
 
1038                         sgp[i].address = busaddr;
 
1039                         sgp[i].length = seglen;
 
1040                         srb->total_xfer_length += seglen;
 
1042                 sgp += srb->sg_count - 1;
 
1045                  * adjust last page if too big as it is allocated
 
1046                  * on even page boundaries
 
1048                 if (srb->total_xfer_length > reqlen) {
 
1049                         sgp->length -= (srb->total_xfer_length - reqlen);
 
1050                         srb->total_xfer_length = reqlen;
 
1053                 /* Fixup for WIDE padding - make sure length is even */
 
1054                 if (dcb->sync_period & WIDE_SYNC &&
 
1055                     srb->total_xfer_length % 2) {
 
1056                         srb->total_xfer_length++;
 
1060                 srb->sg_bus_addr = pci_map_single(dcb->acb->dev,
 
1065                 dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
 
1066                         srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
 
1068                 srb->total_xfer_length = cmd->request_bufflen;
 
1070                 srb->segment_x[0].address =
 
1071                         pci_map_single(dcb->acb->dev, cmd->request_buffer,
 
1072                                        srb->total_xfer_length, dir);
 
1074                 /* Fixup for WIDE padding - make sure length is even */
 
1075                 if (dcb->sync_period & WIDE_SYNC && srb->total_xfer_length % 2)
 
1076                         srb->total_xfer_length++;
 
1078                 srb->segment_x[0].length = srb->total_xfer_length;
 
1079                 srb->virt_addr = cmd->request_buffer;
 
1081                         "build_srb: [1] len=%d buf=%p use_sg=%d map=%08x\n",
 
1082                         srb->total_xfer_length, cmd->request_buffer,
 
1083                         cmd->use_sg, srb->segment_x[0].address);
 
1089  * dc395x_queue_command - queue scsi command passed from the mid
 
1090  * layer, invoke 'done' on completion
 
1092  * @cmd: pointer to scsi command object
 
1093  * @done: function pointer to be invoked on completion
 
1095  * Returns 1 if the adapter (host) is busy, else returns 0. One
 
1096  * reason for an adapter to be busy is that the number
 
1097  * of outstanding queued commands is already equal to
 
1098  * struct Scsi_Host::can_queue .
 
1100  * Required: if struct Scsi_Host::can_queue is ever non-zero
 
1101  *           then this function is required.
 
1103  * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
 
1104  *        and is expected to be held on return.
 
1107 static int dc395x_queue_command(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
 
1109         struct DeviceCtlBlk *dcb;
 
1110         struct ScsiReqBlk *srb;
 
1111         struct AdapterCtlBlk *acb =
 
1112             (struct AdapterCtlBlk *)cmd->device->host->hostdata;
 
1113         dprintkdbg(DBG_0, "queue_command: (pid#%li) <%02i-%i> cmnd=0x%02x\n",
 
1114                 cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
 
1116         /* Assume BAD_TARGET; will be cleared later */
 
1117         cmd->result = DID_BAD_TARGET << 16;
 
1119         /* ignore invalid targets */
 
1120         if (cmd->device->id >= acb->scsi_host->max_id ||
 
1121             cmd->device->lun >= acb->scsi_host->max_lun ||
 
1122             cmd->device->lun >31) {
 
1126         /* does the specified lun on the specified device exist */
 
1127         if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
 
1128                 dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
 
1129                         cmd->device->id, cmd->device->lun);
 
1133         /* do we have a DCB for the device */
 
1134         dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
 
1136                 /* should never happen */
 
1137                 dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
 
1138                         cmd->device->id, cmd->device->lun);
 
1142         /* set callback and clear result in the command */
 
1143         cmd->scsi_done = done;
 
1146         srb = srb_get_free(acb);
 
1150                  * Return 1 since we are unable to queue this command at this
 
1153                 dprintkdbg(DBG_0, "queue_command: No free srb's\n");
 
1157         build_srb(cmd, dcb, srb);
 
1159         if (!list_empty(&dcb->srb_waiting_list)) {
 
1160                 /* append to waiting queue */
 
1161                 srb_waiting_append(dcb, srb);
 
1162                 waiting_process_next(acb);
 
1164                 /* process immediately */
 
1167         dprintkdbg(DBG_1, "queue_command: (pid#%li) done\n", cmd->pid);
 
1172          * Complete the command immediatey, and then return 0 to
 
1173          * indicate that we have handled the command. This is usually
 
1174          * done when the commad is for things like non existent
 
1183  * Return the disk geometry for the given SCSI device.
 
1185 static int dc395x_bios_param(struct scsi_device *sdev,
 
1186                 struct block_device *bdev, sector_t capacity, int *info)
 
1188 #ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP
 
1189         int heads, sectors, cylinders;
 
1190         struct AdapterCtlBlk *acb;
 
1191         int size = capacity;
 
1193         dprintkdbg(DBG_0, "dc395x_bios_param..............\n");
 
1194         acb = (struct AdapterCtlBlk *)sdev->host->hostdata;
 
1197         cylinders = size / (heads * sectors);
 
1199         if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) {
 
1202                 cylinders = size / (heads * sectors);
 
1206         geom[2] = cylinders;
 
1209         return scsicam_bios_param(bdev, capacity, info);
 
1214 static void dump_register_info(struct AdapterCtlBlk *acb,
 
1215                 struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
 
1218         struct pci_dev *dev = acb->dev;
 
1219         pci_read_config_word(dev, PCI_STATUS, &pstat);
 
1221                 dcb = acb->active_dcb;
 
1223                 srb = dcb->active_srb;
 
1226                         dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
 
1229                         dprintkl(KERN_INFO, "dump: srb=%p cmd=%p (pid#%li) "
 
1230                                  "cmnd=0x%02x <%02i-%i>\n",
 
1231                                 srb, srb->cmd, srb->cmd->pid,
 
1232                                 srb->cmd->cmnd[0], srb->cmd->device->id,
 
1233                                 srb->cmd->device->lun);
 
1234                 printk("  sglist=%p cnt=%i idx=%i len=%i\n",
 
1235                        srb->segment_x, srb->sg_count, srb->sg_index,
 
1236                        srb->total_xfer_length);
 
1237                 printk("  state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
 
1238                        srb->state, srb->status, srb->scsi_phase,
 
1239                        (acb->active_dcb) ? "" : "not");
 
1241         dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
 
1242                 "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
 
1243                 "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
 
1244                 "config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
 
1245                 DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
 
1246                 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
 
1247                 DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
 
1248                 DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
 
1249                 DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
 
1250                 DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
 
1251                 DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
 
1252                 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
 
1253                 DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
 
1254                 DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
 
1255                 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
 
1256                 DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
 
1257                 DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
 
1258         dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
 
1259                 "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
 
1260                 "ctctr=0x%08x addr=0x%08x:0x%08x}\n",
 
1261                 DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
 
1262                 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
 
1263                 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
 
1264                 DC395x_read8(acb, TRM_S1040_DMA_STATUS),
 
1265                 DC395x_read8(acb, TRM_S1040_DMA_INTEN),
 
1266                 DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
 
1267                 DC395x_read32(acb, TRM_S1040_DMA_XCNT),
 
1268                 DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
 
1269                 DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
 
1270                 DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
 
1271         dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
 
1272                 "pci{status=0x%04x}\n",
 
1273                 DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
 
1274                 DC395x_read8(acb, TRM_S1040_GEN_STATUS),
 
1275                 DC395x_read8(acb, TRM_S1040_GEN_TIMER),
 
1280 static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
 
1282 #if debug_enabled(DBG_FIFO)
 
1283         u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
 
1284         u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
 
1285         if (!(fifocnt & 0x40))
 
1286                 dprintkdbg(DBG_FIFO,
 
1287                         "clear_fifo: (%i bytes) on phase %02x in %s\n",
 
1288                         fifocnt & 0x3f, lines, txt);
 
1290         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
 
1294 static void reset_dev_param(struct AdapterCtlBlk *acb)
 
1296         struct DeviceCtlBlk *dcb;
 
1297         struct NvRamType *eeprom = &acb->eeprom;
 
1298         dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
 
1300         list_for_each_entry(dcb, &acb->dcb_list, list) {
 
1303                 dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
 
1304                 dcb->sync_period = 0;
 
1305                 dcb->sync_offset = 0;
 
1307                 dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
 
1308                 period_index = eeprom->target[dcb->target_id].period & 0x07;
 
1309                 dcb->min_nego_period = clock_period[period_index];
 
1310                 if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
 
1311                     || !(acb->config & HCC_WIDE_CARD))
 
1312                         dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
 
1318  * perform a hard reset on the SCSI bus
 
1319  * @cmd - some command for this host (for fetching hooks)
 
1320  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
 
1322 static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
 
1324         struct AdapterCtlBlk *acb =
 
1325                 (struct AdapterCtlBlk *)cmd->device->host->hostdata;
 
1327                 "eh_bus_reset: (pid#%li) target=<%02i-%i> cmd=%p\n",
 
1328                 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
 
1330         if (timer_pending(&acb->waiting_timer))
 
1331                 del_timer(&acb->waiting_timer);
 
1336         DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
 
1337         DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
 
1338         DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
 
1339         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
 
1341         reset_scsi_bus(acb);
 
1344         /* We may be in serious trouble. Wait some seconds */
 
1345         acb->scsi_host->last_reset =
 
1346             jiffies + 3 * HZ / 2 +
 
1347             HZ * acb->eeprom.delay_time;
 
1350          * re-enable interrupt      
 
1352         /* Clear SCSI FIFO          */
 
1353         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
 
1354         clear_fifo(acb, "eh_bus_reset");
 
1355         /* Delete pending IRQ */
 
1356         DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
 
1357         set_basic_config(acb);
 
1359         reset_dev_param(acb);
 
1360         doing_srb_done(acb, DID_RESET, cmd, 0);
 
1361         acb->active_dcb = NULL;
 
1362         acb->acb_flag = 0;      /* RESET_DETECT, RESET_DONE ,RESET_DEV */
 
1363         waiting_process_next(acb);
 
1368 static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
 
1372         spin_lock_irq(cmd->device->host->host_lock);
 
1373         rc = __dc395x_eh_bus_reset(cmd);
 
1374         spin_unlock_irq(cmd->device->host->host_lock);
 
1380  * abort an errant SCSI command
 
1381  * @cmd - command to be aborted
 
1382  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
 
1384 static int dc395x_eh_abort(struct scsi_cmnd *cmd)
 
1387          * Look into our command queues: If it has not been sent already,
 
1388          * we remove it and return success. Otherwise fail.
 
1390         struct AdapterCtlBlk *acb =
 
1391             (struct AdapterCtlBlk *)cmd->device->host->hostdata;
 
1392         struct DeviceCtlBlk *dcb;
 
1393         struct ScsiReqBlk *srb;
 
1394         dprintkl(KERN_INFO, "eh_abort: (pid#%li) target=<%02i-%i> cmd=%p\n",
 
1395                 cmd->pid, cmd->device->id, cmd->device->lun, cmd);
 
1397         dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
 
1399                 dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
 
1403         srb = find_cmd(cmd, &dcb->srb_waiting_list);
 
1405                 srb_waiting_remove(dcb, srb);
 
1406                 pci_unmap_srb_sense(acb, srb);
 
1407                 pci_unmap_srb(acb, srb);
 
1409                 srb_free_insert(acb, srb);
 
1410                 dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
 
1411                 cmd->result = DID_ABORT << 16;
 
1414         srb = find_cmd(cmd, &dcb->srb_going_list);
 
1416                 dprintkl(KERN_DEBUG, "eh_abort: Command in progress");
 
1417                 /* XXX: Should abort the command here */
 
1419                 dprintkl(KERN_DEBUG, "eh_abort: Command not found");
 
1426 static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 
1427                 struct ScsiReqBlk *srb)
 
1429         u8 *ptr = srb->msgout_buf + srb->msg_count;
 
1430         if (srb->msg_count > 1) {
 
1432                         "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
 
1433                         srb->msg_count, srb->msgout_buf[0],
 
1434                         srb->msgout_buf[1]);
 
1437         if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
 
1438                 dcb->sync_offset = 0;
 
1439                 dcb->min_nego_period = 200 >> 2;
 
1440         } else if (dcb->sync_offset == 0)
 
1441                 dcb->sync_offset = SYNC_NEGO_OFFSET;
 
1443         *ptr++ = MSG_EXTENDED;  /* (01h) */
 
1444         *ptr++ = 3;             /* length */
 
1445         *ptr++ = EXTENDED_SDTR; /* (01h) */
 
1446         *ptr++ = dcb->min_nego_period;  /* Transfer period (in 4ns) */
 
1447         *ptr++ = dcb->sync_offset;      /* Transfer period (max. REQ/ACK dist) */
 
1448         srb->msg_count += 5;
 
1449         srb->state |= SRB_DO_SYNC_NEGO;
 
1454 static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 
1455                 struct ScsiReqBlk *srb)
 
1457         u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
 
1458                    (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
 
1459         u8 *ptr = srb->msgout_buf + srb->msg_count;
 
1460         if (srb->msg_count > 1) {
 
1462                         "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
 
1463                         srb->msg_count, srb->msgout_buf[0],
 
1464                         srb->msgout_buf[1]);
 
1467         *ptr++ = MSG_EXTENDED;  /* (01h) */
 
1468         *ptr++ = 2;             /* length */
 
1469         *ptr++ = EXTENDED_WDTR; /* (03h) */
 
1471         srb->msg_count += 4;
 
1472         srb->state |= SRB_DO_WIDE_NEGO;
 
1477 /* Timer to work around chip flaw: When selecting and the bus is 
 
1478  * busy, we sometimes miss a Selection timeout IRQ */
 
1479 void selection_timeout_missed(unsigned long ptr);
 
1480 /* Sets the timer to wake us up */
 
1481 static void selto_timer(struct AdapterCtlBlk *acb)
 
1483         if (timer_pending(&acb->selto_timer))
 
1485         acb->selto_timer.function = selection_timeout_missed;
 
1486         acb->selto_timer.data = (unsigned long) acb;
 
1488             (jiffies + HZ, acb->scsi_host->last_reset + HZ / 2))
 
1489                 acb->selto_timer.expires =
 
1490                     acb->scsi_host->last_reset + HZ / 2 + 1;
 
1492                 acb->selto_timer.expires = jiffies + HZ + 1;
 
1493         add_timer(&acb->selto_timer);
 
1497 void selection_timeout_missed(unsigned long ptr)
 
1499         unsigned long flags;
 
1500         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
 
1501         struct ScsiReqBlk *srb;
 
1502         dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
 
1503         if (!acb->active_dcb || !acb->active_dcb->active_srb) {
 
1504                 dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
 
1507         DC395x_LOCK_IO(acb->scsi_host, flags);
 
1508         srb = acb->active_dcb->active_srb;
 
1510         DC395x_UNLOCK_IO(acb->scsi_host, flags);
 
1515 static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
 
1516                 struct ScsiReqBlk* srb)
 
1518         u16 s_stat2, return_code;
 
1519         u8 s_stat, scsicommand, i, identify_message;
 
1521         dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> srb=%p\n",
 
1522                 srb->cmd->pid, dcb->target_id, dcb->target_lun, srb);
 
1524         srb->tag_number = TAG_NONE;     /* acb->tag_max_num: had error read in eeprom */
 
1526         s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
 
1528         s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
 
1530         if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
 
1531                 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) BUSY %02x %04x\n",
 
1532                         srb->cmd->pid, s_stat, s_stat2);
 
1536                  * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
 
1537                  * Timeout, a Disconnect or a Reselction IRQ, so we would be screwed!
 
1538                  * (This is likely to be a bug in the hardware. Obviously, most people
 
1539                  *  only have one initiator per SCSI bus.)
 
1540                  * Instead let this fail and have the timer make sure the command is 
 
1541                  * tried again after a short time
 
1543                 /*selto_timer (acb); */
 
1547         if (acb->active_dcb) {
 
1548                 dprintkl(KERN_DEBUG, "start_scsi: (pid#%li) Attempt to start a"
 
1549                         "command while another command (pid#%li) is active.",
 
1551                         acb->active_dcb->active_srb ?
 
1552                             acb->active_dcb->active_srb->cmd->pid : 0);
 
1555         if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
 
1556                 dprintkdbg(DBG_KG, "start_scsi: (pid#%li) Failed (busy)\n",
 
1560         /* Allow starting of SCSI commands half a second before we allow the mid-level
 
1561          * to queue them again after a reset */
 
1562         if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
 
1563                 dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
 
1568         clear_fifo(acb, "start_scsi");
 
1569         DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
 
1570         DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
 
1571         DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
 
1572         DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
 
1573         srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
 
1575         identify_message = dcb->identify_msg;
 
1576         /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
 
1577         /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
 
1578         if (srb->flag & AUTO_REQSENSE)
 
1579                 identify_message &= 0xBF;
 
1581         if (((srb->cmd->cmnd[0] == INQUIRY)
 
1582              || (srb->cmd->cmnd[0] == REQUEST_SENSE)
 
1583              || (srb->flag & AUTO_REQSENSE))
 
1584             && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
 
1585                  && !(dcb->sync_mode & WIDE_NEGO_DONE))
 
1586                 || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
 
1587                     && !(dcb->sync_mode & SYNC_NEGO_DONE)))
 
1588             && (dcb->target_lun == 0)) {
 
1589                 srb->msgout_buf[0] = identify_message;
 
1591                 scsicommand = SCMD_SEL_ATNSTOP;
 
1592                 srb->state = SRB_MSGOUT;
 
1594                 if (dcb->sync_mode & WIDE_NEGO_ENABLE
 
1595                     && dcb->inquiry7 & SCSI_INQ_WBUS16) {
 
1596                         build_wdtr(acb, dcb, srb);
 
1600                 if (dcb->sync_mode & SYNC_NEGO_ENABLE
 
1601                     && dcb->inquiry7 & SCSI_INQ_SYNC) {
 
1602                         build_sdtr(acb, dcb, srb);
 
1605                 if (dcb->sync_mode & WIDE_NEGO_ENABLE
 
1606                     && dcb->inquiry7 & SCSI_INQ_WBUS16) {
 
1607                         build_wdtr(acb, dcb, srb);
 
1612         /* Send identify message */
 
1613         DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
 
1615         scsicommand = SCMD_SEL_ATN;
 
1616         srb->state = SRB_START_;
 
1617 #ifndef DC395x_NO_TAGQ
 
1618         if ((dcb->sync_mode & EN_TAG_QUEUEING)
 
1619             && (identify_message & 0xC0)) {
 
1620                 /* Send Tag message */
 
1623                 while (tag_mask & dcb->tag_mask
 
1624                        && tag_number <= dcb->max_command) {
 
1625                         tag_mask = tag_mask << 1;
 
1628                 if (tag_number >= dcb->max_command) {
 
1629                         dprintkl(KERN_WARNING, "start_scsi: (pid#%li) "
 
1630                                 "Out of tags target=<%02i-%i>)\n",
 
1631                                 srb->cmd->pid, srb->cmd->device->id,
 
1632                                 srb->cmd->device->lun);
 
1633                         srb->state = SRB_READY;
 
1634                         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
 
1639                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
 
1640                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
 
1641                 dcb->tag_mask |= tag_mask;
 
1642                 srb->tag_number = tag_number;
 
1643                 scsicommand = SCMD_SEL_ATN3;
 
1644                 srb->state = SRB_START_;
 
1648         /* Send CDB ..command block ......... */
 
1649         dprintkdbg(DBG_KG, "start_scsi: (pid#%li) <%02i-%i> cmnd=0x%02x tag=%i\n",
 
1650                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
 
1651                 srb->cmd->cmnd[0], srb->tag_number);
 
1652         if (srb->flag & AUTO_REQSENSE) {
 
1653                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
 
1654                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
 
1655                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
 
1656                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
 
1657                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
 
1658                               sizeof(srb->cmd->sense_buffer));
 
1659                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
 
1661                 ptr = (u8 *)srb->cmd->cmnd;
 
1662                 for (i = 0; i < srb->cmd->cmd_len; i++)
 
1663                         DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
 
1666         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
 
1667                        DO_HWRESELECT | DO_DATALATCH);
 
1668         if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
 
1670                  * If start_scsi return 1:
 
1671                  * we caught an interrupt (must be reset or reselection ... )
 
1672                  * : Let's process it first!
 
1674                 dprintkdbg(DBG_0, "start_scsi: (pid#%li) <%02i-%i> Failed - busy\n",
 
1675                         srb->cmd->pid, dcb->target_id, dcb->target_lun);
 
1676                 srb->state = SRB_READY;
 
1680                 /* This IRQ should NOT get lost, as we did not acknowledge it */
 
1683                  * If start_scsi returns 0:
 
1684                  * we know that the SCSI processor is free
 
1686                 srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
 
1687                 dcb->active_srb = srb;
 
1688                 acb->active_dcb = dcb;
 
1690                 /* it's important for atn stop */
 
1691                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
 
1692                                DO_DATALATCH | DO_HWRESELECT);
 
1694                 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
 
1700 #define DC395x_ENABLE_MSGOUT \
 
1701  DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
 
1702  srb->state |= SRB_MSGOUT
 
1706 static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
 
1707                 struct ScsiReqBlk *srb)
 
1709         srb->msgout_buf[0] = ABORT;
 
1711         DC395x_ENABLE_MSGOUT;
 
1712         srb->state &= ~SRB_MSGIN;
 
1713         srb->state |= SRB_MSGOUT;
 
1718  * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
 
1719  *                           have been triggered for this card.
 
1721  * @acb:         a pointer to the adpter control block
 
1722  * @scsi_status: the status return when we checked the card
 
1724 static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
 
1727         struct DeviceCtlBlk *dcb;
 
1728         struct ScsiReqBlk *srb;
 
1731         unsigned long flags;
 
1732         void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *, 
 
1735         DC395x_LOCK_IO(acb->scsi_host, flags);
 
1737         /* This acknowledges the IRQ */
 
1738         scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
 
1739         if ((scsi_status & 0x2007) == 0x2002)
 
1740                 dprintkl(KERN_DEBUG,
 
1741                         "COP after COP completed? %04x\n", scsi_status);
 
1742         if (debug_enabled(DBG_KG)) {
 
1743                 if (scsi_intstatus & INT_SELTIMEOUT)
 
1744                         dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
 
1746         /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
 
1748         if (timer_pending(&acb->selto_timer))
 
1749                 del_timer(&acb->selto_timer);
 
1751         if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
 
1752                 disconnect(acb);        /* bus free interrupt  */
 
1755         if (scsi_intstatus & INT_RESELECTED) {
 
1759         if (scsi_intstatus & INT_SELECT) {
 
1760                 dprintkl(KERN_INFO, "Host does not support target mode!\n");
 
1763         if (scsi_intstatus & INT_SCSIRESET) {
 
1764                 scsi_reset_detect(acb);
 
1767         if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
 
1768                 dcb = acb->active_dcb;
 
1770                         dprintkl(KERN_DEBUG,
 
1771                                 "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
 
1772                                 scsi_status, scsi_intstatus);
 
1775                 srb = dcb->active_srb;
 
1776                 if (dcb->flag & ABORT_DEV_) {
 
1777                         dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
 
1778                         enable_msgout_abort(acb, srb);
 
1781                 /* software sequential machine */
 
1782                 phase = (u16)srb->scsi_phase;
 
1786                  * call  dc395x_scsi_phase0[]... "phase entry"
 
1787                  * handle every phase before start transfer
 
1789                 /* data_out_phase0,     phase:0 */
 
1790                 /* data_in_phase0,      phase:1 */
 
1791                 /* command_phase0,      phase:2 */
 
1792                 /* status_phase0,       phase:3 */
 
1793                 /* nop0,                phase:4 PH_BUS_FREE .. initial phase */
 
1794                 /* nop0,                phase:5 PH_BUS_FREE .. initial phase */
 
1795                 /* msgout_phase0,       phase:6 */
 
1796                 /* msgin_phase0,        phase:7 */
 
1797                 dc395x_statev = dc395x_scsi_phase0[phase];
 
1798                 dc395x_statev(acb, srb, &scsi_status);
 
1801                  * if there were any exception occured scsi_status
 
1802                  * will be modify to bus free phase new scsi_status
 
1803                  * transfer out from ... previous dc395x_statev
 
1805                 srb->scsi_phase = scsi_status & PHASEMASK;
 
1806                 phase = (u16)scsi_status & PHASEMASK;
 
1809                  * call  dc395x_scsi_phase1[]... "phase entry" handle
 
1810                  * every phase to do transfer
 
1812                 /* data_out_phase1,     phase:0 */
 
1813                 /* data_in_phase1,      phase:1 */
 
1814                 /* command_phase1,      phase:2 */
 
1815                 /* status_phase1,       phase:3 */
 
1816                 /* nop1,                phase:4 PH_BUS_FREE .. initial phase */
 
1817                 /* nop1,                phase:5 PH_BUS_FREE .. initial phase */
 
1818                 /* msgout_phase1,       phase:6 */
 
1819                 /* msgin_phase1,        phase:7 */
 
1820                 dc395x_statev = dc395x_scsi_phase1[phase];
 
1821                 dc395x_statev(acb, srb, &scsi_status);
 
1824         DC395x_UNLOCK_IO(acb->scsi_host, flags);
 
1828 static irqreturn_t dc395x_interrupt(int irq, void *dev_id,
 
1829                 struct pt_regs *regs)
 
1831         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)dev_id;
 
1834         irqreturn_t handled = IRQ_NONE;
 
1837          * Check for pending interupt
 
1839         scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
 
1840         dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
 
1841         if (scsi_status & SCSIINTERRUPT) {
 
1842                 /* interupt pending - let's process it! */
 
1843                 dc395x_handle_interrupt(acb, scsi_status);
 
1844                 handled = IRQ_HANDLED;
 
1846         else if (dma_status & 0x20) {
 
1847                 /* Error from the DMA engine */
 
1848                 dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
 
1850                 dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
 
1851                 if (acb->active_dcb) {
 
1852                         acb->active_dcb-> flag |= ABORT_DEV_;
 
1853                         if (acb->active_dcb->active_srb)
 
1854                                 enable_msgout_abort(acb, acb->active_dcb->active_srb);
 
1856                 DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
 
1858                 dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
 
1861                 handled = IRQ_HANDLED;
 
1868 static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
1871         dprintkdbg(DBG_0, "msgout_phase0: (pid#%li)\n", srb->cmd->pid);
 
1872         if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
 
1873                 *pscsi_status = PH_BUS_FREE;    /*.. initial phase */
 
1875         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
1876         srb->state &= ~SRB_MSGOUT;
 
1880 static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
1885         dprintkdbg(DBG_0, "msgout_phase1: (pid#%li)\n", srb->cmd->pid);
 
1887         clear_fifo(acb, "msgout_phase1");
 
1888         if (!(srb->state & SRB_MSGOUT)) {
 
1889                 srb->state |= SRB_MSGOUT;
 
1890                 dprintkl(KERN_DEBUG,
 
1891                         "msgout_phase1: (pid#%li) Phase unexpected\n",
 
1892                         srb->cmd->pid); /* So what ? */
 
1894         if (!srb->msg_count) {
 
1895                 dprintkdbg(DBG_0, "msgout_phase1: (pid#%li) NOP msg\n",
 
1897                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
 
1898                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
1899                 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
 
1902         ptr = (u8 *)srb->msgout_buf;
 
1903         for (i = 0; i < srb->msg_count; i++)
 
1904                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
 
1906         if (srb->msgout_buf[0] == MSG_ABORT)
 
1907                 srb->state = SRB_ABORT_SENT;
 
1909         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
 
1913 static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
1916         dprintkdbg(DBG_0, "command_phase0: (pid#%li)\n", srb->cmd->pid);
 
1917         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
 
1921 static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
1924         struct DeviceCtlBlk *dcb;
 
1927         dprintkdbg(DBG_0, "command_phase1: (pid#%li)\n", srb->cmd->pid);
 
1929         clear_fifo(acb, "command_phase1");
 
1930         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
 
1931         if (!(srb->flag & AUTO_REQSENSE)) {
 
1932                 ptr = (u8 *)srb->cmd->cmnd;
 
1933                 for (i = 0; i < srb->cmd->cmd_len; i++) {
 
1934                         DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
 
1938                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
 
1939                 dcb = acb->active_dcb;
 
1941                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
 
1942                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
 
1943                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
 
1944                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
 
1945                               sizeof(srb->cmd->sense_buffer));
 
1946                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
 
1948         srb->state |= SRB_COMMAND;
 
1949         /* it's important for atn stop */
 
1950         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
 
1952         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
 
1957  * Verify that the remaining space in the hw sg lists is the same as
 
1958  * the count of remaining bytes in srb->total_xfer_length
 
1960 static void sg_verify_length(struct ScsiReqBlk *srb)
 
1962         if (debug_enabled(DBG_SG)) {
 
1964                 unsigned idx = srb->sg_index;
 
1965                 struct SGentry *psge = srb->segment_x + idx;
 
1966                 for (; idx < srb->sg_count; psge++, idx++)
 
1967                         len += psge->length;
 
1968                 if (len != srb->total_xfer_length)
 
1970                                "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
 
1971                                srb->total_xfer_length, len);
 
1977  * Compute the next Scatter Gather list index and adjust its length
 
1978  * and address if necessary; also compute virt_addr
 
1980 static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
 
1983         struct scatterlist *sg;
 
1984         struct scsi_cmnd *cmd = srb->cmd;
 
1985         int segment = cmd->use_sg;
 
1986         u32 xferred = srb->total_xfer_length - left; /* bytes transfered */
 
1987         struct SGentry *psge = srb->segment_x + srb->sg_index;
 
1990                 "sg_update_list: Transfered %i of %i bytes, %i remain\n",
 
1991                 xferred, srb->total_xfer_length, left);
 
1993                 /* nothing to update since we did not transfer any data */
 
1997         sg_verify_length(srb);
 
1998         srb->total_xfer_length = left;  /* update remaining count */
 
1999         for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
 
2000                 if (xferred >= psge->length) {
 
2001                         /* Complete SG entries done */
 
2002                         xferred -= psge->length;
 
2004                         /* Partial SG entry done */
 
2005                         psge->length -= xferred;
 
2006                         psge->address += xferred;
 
2007                         srb->sg_index = idx;
 
2008                         pci_dma_sync_single_for_device(srb->dcb->
 
2017         sg_verify_length(srb);
 
2019         /* we need the corresponding virtual address */
 
2021                 srb->virt_addr += xferred;
 
2025         /* We have to walk the scatterlist to find it */
 
2026         sg = (struct scatterlist *)cmd->request_buffer;
 
2028                 unsigned long mask =
 
2029                     ~((unsigned long)sg->length - 1) & PAGE_MASK;
 
2030                 if ((sg_dma_address(sg) & mask) == (psge->address & mask)) {
 
2031                         srb->virt_addr = (page_address(sg->page)
 
2033                                            (psge->address & PAGE_MASK));
 
2039         dprintkl(KERN_ERR, "sg_update_list: sg_to_virt failed\n");
 
2040         srb->virt_addr = NULL;
 
2045  * We have transfered a single byte (PIO mode?) and need to update
 
2046  * the count of bytes remaining (total_xfer_length) and update the sg
 
2047  * entry to either point to next byte in the current sg entry, or of
 
2048  * already at the end to point to the start of the next sg entry
 
2050 static void sg_subtract_one(struct ScsiReqBlk *srb)
 
2052         srb->total_xfer_length--;
 
2053         srb->segment_x[srb->sg_index].length--;
 
2054         if (srb->total_xfer_length &&
 
2055             !srb->segment_x[srb->sg_index].length) {
 
2056                 if (debug_enabled(DBG_PIO))
 
2057                         printk(" (next segment)");
 
2059                 sg_update_list(srb, srb->total_xfer_length);
 
2065  * cleanup_after_transfer
 
2067  * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
 
2068  * KG: Currently called from  StatusPhase1 ()
 
2069  * Should probably also be called from other places
 
2070  * Best might be to call it in DataXXPhase0, if new phase will differ 
 
2072 static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
 
2073                 struct ScsiReqBlk *srb)
 
2075         /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
 
2076         if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) {       /* read */
 
2077                 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
 
2078                         clear_fifo(acb, "cleanup/in");
 
2079                 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
 
2080                         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
 
2081         } else {                /* write */
 
2082                 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
 
2083                         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
 
2084                 if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
 
2085                         clear_fifo(acb, "cleanup/out");
 
2087         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
 
2092  * Those no of bytes will be transfered w/ PIO through the SCSI FIFO
 
2093  * Seems to be needed for unknown reasons; could be a hardware bug :-(
 
2095 #define DC395x_LASTPIO 4
 
2098 static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
2101         struct DeviceCtlBlk *dcb = srb->dcb;
 
2102         u16 scsi_status = *pscsi_status;
 
2103         u32 d_left_counter = 0;
 
2104         dprintkdbg(DBG_0, "data_out_phase0: (pid#%li) <%02i-%i>\n",
 
2105                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
 
2108          * KG: We need to drain the buffers before we draw any conclusions!
 
2109          * This means telling the DMA to push the rest into SCSI, telling
 
2110          * SCSI to push the rest to the bus.
 
2111          * However, the device might have been the one to stop us (phase
 
2112          * change), and the data in transit just needs to be accounted so
 
2113          * it can be retransmitted.)
 
2116          * KG: Stop DMA engine pushing more data into the SCSI FIFO
 
2117          * If we need more data, the DMA SG list will be freshly set up, anyway
 
2119         dprintkdbg(DBG_PIO, "data_out_phase0: "
 
2120                 "DMA{fifcnt=0x%02x fifostat=0x%02x} "
 
2121                 "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
 
2122                 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
 
2123                 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
 
2124                 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
 
2125                 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
 
2126                 srb->total_xfer_length);
 
2127         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
 
2129         if (!(srb->state & SRB_XFERPAD)) {
 
2130                 if (scsi_status & PARITYERROR)
 
2131                         srb->status |= PARITY_ERROR;
 
2134                  * KG: Right, we can't just rely on the SCSI_COUNTER, because this
 
2135                  * is the no of bytes it got from the DMA engine not the no it 
 
2136                  * transferred successfully to the device. (And the difference could
 
2137                  * be as much as the FIFO size, I guess ...)
 
2139                 if (!(scsi_status & SCSIXFERDONE)) {
 
2141                          * when data transfer from DMA FIFO to SCSI FIFO
 
2142                          * if there was some data left in SCSI FIFO
 
2145                             (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
 
2147                         if (dcb->sync_period & WIDE_SYNC)
 
2148                                 d_left_counter <<= 1;
 
2150                         dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
 
2151                                 "SCSI{fifocnt=0x%02x cnt=0x%08x} "
 
2152                                 "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
 
2153                                 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
 
2154                                 (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
 
2155                                 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
 
2156                                 DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
 
2157                                 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
 
2158                                 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
 
2159                                 DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
 
2162                  * calculate all the residue data that not yet tranfered
 
2163                  * SCSI transfer counter + left in SCSI FIFO data
 
2165                  * .....TRM_S1040_SCSI_COUNTER (24bits)
 
2166                  * The counter always decrement by one for every SCSI byte transfer.
 
2167                  * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
 
2168                  * The counter is SCSI FIFO offset counter (in units of bytes or! words)
 
2170                 if (srb->total_xfer_length > DC395x_LASTPIO)
 
2172                             DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
 
2174                 /* Is this a good idea? */
 
2175                 /*clear_fifo(acb, "DOP1"); */
 
2176                 /* KG: What is this supposed to be useful for? WIDE padding stuff? */
 
2177                 if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
 
2178                     && srb->cmd->request_bufflen % 2) {
 
2181                                 "data_out_phase0: Discard 1 byte (0x%02x)\n",
 
2185                  * KG: Oops again. Same thinko as above: The SCSI might have been
 
2186                  * faster than the DMA engine, so that it ran out of data.
 
2187                  * In that case, we have to do just nothing! 
 
2188                  * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
 
2191                  * KG: This is nonsense: We have been WRITING data to the bus
 
2192                  * If the SCSI engine has no bytes left, how should the DMA engine?
 
2194                 if (d_left_counter == 0) {
 
2195                         srb->total_xfer_length = 0;
 
2198                          * if transfer not yet complete
 
2199                          * there were some data residue in SCSI FIFO or
 
2200                          * SCSI transfer counter not empty
 
2203                             srb->total_xfer_length - d_left_counter;
 
2205                             (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
 
2206                         sg_update_list(srb, d_left_counter);
 
2207                         /* KG: Most ugly hack! Apparently, this works around a chip bug */
 
2208                         if ((srb->segment_x[srb->sg_index].length ==
 
2209                              diff && srb->cmd->use_sg)
 
2210                             || ((oldxferred & ~PAGE_MASK) ==
 
2213                                 dprintkl(KERN_INFO, "data_out_phase0: "
 
2214                                         "Work around chip bug (%i)?\n", diff);
 
2216                                     srb->total_xfer_length - diff;
 
2217                                 sg_update_list(srb, d_left_counter);
 
2218                                 /*srb->total_xfer_length -= diff; */
 
2219                                 /*srb->virt_addr += diff; */
 
2220                                 /*if (srb->cmd->use_sg) */
 
2221                                 /*      srb->sg_index++; */
 
2225         if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
 
2226                 cleanup_after_transfer(acb, srb);
 
2231 static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
2234         dprintkdbg(DBG_0, "data_out_phase1: (pid#%li) <%02i-%i>\n",
 
2235                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
 
2236         clear_fifo(acb, "data_out_phase1");
 
2237         /* do prepare before transfer when data out phase */
 
2238         data_io_transfer(acb, srb, XFERDATAOUT);
 
2242 static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
2245         u16 scsi_status = *pscsi_status;
 
2246         u32 d_left_counter = 0;
 
2247         dprintkdbg(DBG_0, "data_in_phase0: (pid#%li) <%02i-%i>\n",
 
2248                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
 
2251          * KG: DataIn is much more tricky than DataOut. When the device is finished
 
2252          * and switches to another phase, the SCSI engine should be finished too.
 
2253          * But: There might still be bytes left in its FIFO to be fetched by the DMA
 
2254          * engine and transferred to memory.
 
2255          * We should wait for the FIFOs to be emptied by that (is there any way to 
 
2256          * enforce this?) and then stop the DMA engine, because it might think, that
 
2257          * there are more bytes to follow. Yes, the device might disconnect prior to
 
2258          * having all bytes transferred! 
 
2259          * Also we should make sure that all data from the DMA engine buffer's really
 
2260          * made its way to the system memory! Some documentation on this would not
 
2261          * seem to be a bad idea, actually.
 
2263         if (!(srb->state & SRB_XFERPAD)) {
 
2264                 if (scsi_status & PARITYERROR) {
 
2265                         dprintkl(KERN_INFO, "data_in_phase0: (pid#%li) "
 
2266                                 "Parity Error\n", srb->cmd->pid);
 
2267                         srb->status |= PARITY_ERROR;
 
2270                  * KG: We should wait for the DMA FIFO to be empty ...
 
2271                  * but: it would be better to wait first for the SCSI FIFO and then the
 
2272                  * the DMA FIFO to become empty? How do we know, that the device not already
 
2273                  * sent data to the FIFO in a MsgIn phase, eg.?
 
2275                 if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
 
2278                         dprintkl(KERN_DEBUG,
 
2279                                 "DIP0: Wait for DMA FIFO to flush ...\n");
 
2280                         /*DC395x_write8  (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
 
2281                         /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
 
2282                         /*DC395x_write8  (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
 
2284                                (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
 
2286                         if (ctr < 6000000 - 1)
 
2288                                        "DIP0: Had to wait for DMA ...\n");
 
2291                                        "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
 
2292                         /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
 
2294                         dprintkdbg(DBG_KG, "data_in_phase0: "
 
2295                                 "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
 
2296                                 DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
 
2297                                 DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
 
2299                 /* Now: Check remainig data: The SCSI counters should tell us ... */
 
2300                 d_left_counter = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER)
 
2301                     + ((DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f)
 
2302                        << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
 
2304                 dprintkdbg(DBG_KG, "data_in_phase0: "
 
2305                         "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
 
2306                         "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
 
2307                         "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
 
2308                         DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
 
2309                         (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
 
2310                         DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
 
2311                         DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
 
2312                         DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
 
2313                         DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
 
2314                         srb->total_xfer_length, d_left_counter);
 
2316                 /* KG: Less than or equal to 4 bytes can not be transfered via DMA, it seems. */
 
2318                     && srb->total_xfer_length <= DC395x_LASTPIO) {
 
2319                         /*u32 addr = (srb->segment_x[srb->sg_index].address); */
 
2320                         /*sg_update_list (srb, d_left_counter); */
 
2321                         dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) to "
 
2322                                 "%p for remaining %i bytes:",
 
2323                                 DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x1f,
 
2324                                 (srb->dcb->sync_period & WIDE_SYNC) ?
 
2327                                 srb->total_xfer_length);
 
2328                         if (srb->dcb->sync_period & WIDE_SYNC)
 
2329                                 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
 
2331                         while (DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) != 0x40) {
 
2332                                 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
 
2334                                 *(srb->virt_addr)++ = byte;
 
2335                                 if (debug_enabled(DBG_PIO))
 
2336                                         printk(" %02x", byte);
 
2338                                 sg_subtract_one(srb);
 
2340                         if (srb->dcb->sync_period & WIDE_SYNC) {
 
2342                 /* Read the last byte ... */
 
2343                                 if (srb->total_xfer_length > 0) {
 
2344                                         u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
 
2346                                         *(srb->virt_addr)++ = byte;
 
2347                                         srb->total_xfer_length--;
 
2348                                         if (debug_enabled(DBG_PIO))
 
2349                                                 printk(" %02x", byte);
 
2352                                 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
 
2354                         /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
 
2355                         /*srb->total_xfer_length = 0; */
 
2356                         if (debug_enabled(DBG_PIO))
 
2359 #endif                          /* DC395x_LASTPIO */
 
2363                  * KG: This was in DATAOUT. Does it also belong here?
 
2364                  * Nobody seems to know what counter and fifo_cnt count exactly ...
 
2366                 if (!(scsi_status & SCSIXFERDONE)) {
 
2368                          * when data transfer from DMA FIFO to SCSI FIFO
 
2369                          * if there was some data left in SCSI FIFO
 
2372                             (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
 
2374                         if (srb->dcb->sync_period & WIDE_SYNC)
 
2375                                 d_left_counter <<= 1;
 
2377                          * if WIDE scsi SCSI FIFOCNT unit is word !!!
 
2379                          * KG: Seems to be correct ...
 
2383                 /* KG: This should not be needed any more! */
 
2384                 if (d_left_counter == 0
 
2385                     || (scsi_status & SCSIXFERCNT_2_ZERO)) {
 
2391                                     DC395x_read8(acb, TRM_S1040_DMA_STATUS);
 
2392                         } while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
 
2395                                        "Deadlock in DataInPhase0 waiting for DMA!!\n");
 
2396                         srb->total_xfer_length = 0;
 
2398                         srb->total_xfer_length = d_left_counter;
 
2399                 } else {        /* phase changed */
 
2402                          * when a transfer not yet complete 
 
2403                          * but be disconnected by target
 
2404                          * if transfer not yet complete
 
2405                          * there were some data residue in SCSI FIFO or
 
2406                          * SCSI transfer counter not empty
 
2408                         sg_update_list(srb, d_left_counter);
 
2411         /* KG: The target may decide to disconnect: Empty FIFO before! */
 
2412         if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
 
2413                 cleanup_after_transfer(acb, srb);
 
2418 static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
2421         dprintkdbg(DBG_0, "data_in_phase1: (pid#%li) <%02i-%i>\n",
 
2422                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
 
2423         data_io_transfer(acb, srb, XFERDATAIN);
 
2427 static void data_io_transfer(struct AdapterCtlBlk *acb, 
 
2428                 struct ScsiReqBlk *srb, u16 io_dir)
 
2430         struct DeviceCtlBlk *dcb = srb->dcb;
 
2433                 "data_io_transfer: (pid#%li) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
 
2434                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun,
 
2435                 ((io_dir & DMACMD_DIR) ? 'r' : 'w'),
 
2436                 srb->total_xfer_length, srb->sg_index, srb->sg_count);
 
2437         if (srb == acb->tmp_srb)
 
2438                 dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
 
2439         if (srb->sg_index >= srb->sg_count) {
 
2440                 /* can't happen? out of bounds error */
 
2444         if (srb->total_xfer_length > DC395x_LASTPIO) {
 
2445                 u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
 
2447                  * KG: What should we do: Use SCSI Cmd 0x90/0x92?
 
2448                  * Maybe, even ABORTXFER would be appropriate
 
2450                 if (dma_status & XFERPENDING) {
 
2451                         dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
 
2452                                 "Expect trouble!\n");
 
2453                         dump_register_info(acb, dcb, srb);
 
2454                         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
 
2456                 /* clear_fifo(acb, "IO"); */
 
2458                  * load what physical address of Scatter/Gather list table
 
2459                  * want to be transfer
 
2461                 srb->state |= SRB_DATA_XFER;
 
2462                 DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
 
2463                 if (srb->cmd->use_sg) { /* with S/G */
 
2464                         io_dir |= DMACMD_SG;
 
2465                         DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
 
2467                                        sizeof(struct SGentry) *
 
2469                         /* load how many bytes in the sg list table */
 
2470                         DC395x_write32(acb, TRM_S1040_DMA_XCNT,
 
2471                                        ((u32)(srb->sg_count -
 
2472                                               srb->sg_index) << 3));
 
2473                 } else {        /* without S/G */
 
2474                         io_dir &= ~DMACMD_SG;
 
2475                         DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
 
2476                                        srb->segment_x[0].address);
 
2477                         DC395x_write32(acb, TRM_S1040_DMA_XCNT,
 
2478                                        srb->segment_x[0].length);
 
2480                 /* load total transfer length (24bits) max value 16Mbyte */
 
2481                 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
 
2482                                srb->total_xfer_length);
 
2483                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
2484                 if (io_dir & DMACMD_DIR) {      /* read */
 
2485                         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
 
2487                         DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
 
2489                         DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
 
2490                         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
 
2496         else if (srb->total_xfer_length > 0) {  /* The last four bytes: Do PIO */
 
2498                  * load what physical address of Scatter/Gather list table
 
2499                  * want to be transfer
 
2501                 srb->state |= SRB_DATA_XFER;
 
2502                 /* load total transfer length (24bits) max value 16Mbyte */
 
2503                 DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
 
2504                                srb->total_xfer_length);
 
2505                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
2506                 if (io_dir & DMACMD_DIR) {      /* read */
 
2507                         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
 
2509                 } else {        /* write */
 
2510                         int ln = srb->total_xfer_length;
 
2511                         if (srb->dcb->sync_period & WIDE_SYNC)
 
2512                                 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
 
2515                                 "data_io_transfer: PIO %i bytes from %p:",
 
2516                                 srb->total_xfer_length, srb->virt_addr);
 
2518                         while (srb->total_xfer_length) {
 
2519                                 if (debug_enabled(DBG_PIO))
 
2520                                         printk(" %02x", (unsigned char) *(srb->virt_addr));
 
2523                                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 
 
2524                                      *(srb->virt_addr)++);
 
2526                                 sg_subtract_one(srb);
 
2528                         if (srb->dcb->sync_period & WIDE_SYNC) {
 
2530                                         DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
 
2531                                         if (debug_enabled(DBG_PIO))
 
2534                                 DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
 
2536                         /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
 
2537                         if (debug_enabled(DBG_PIO))
 
2539                         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
 
2543 #endif                          /* DC395x_LASTPIO */
 
2544         else {          /* xfer pad */
 
2545                 u8 data = 0, data2 = 0;
 
2546                 if (srb->sg_count) {
 
2547                         srb->adapter_status = H_OVER_UNDER_RUN;
 
2548                         srb->status |= OVER_RUN;
 
2551                  * KG: despite the fact that we are using 16 bits I/O ops
 
2552                  * the SCSI FIFO is only 8 bits according to the docs
 
2553                  * (we can set bit 1 in 0x8f to serialize FIFO access ...)
 
2555                 if (dcb->sync_period & WIDE_SYNC) {
 
2556                         DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
 
2557                         DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
 
2559                         if (io_dir & DMACMD_DIR) {
 
2560                                 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
 
2561                                 data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
 
2563                                 /* Danger, Robinson: If you find KGs
 
2564                                  * scattered over the wide disk, the driver
 
2565                                  * or chip is to blame :-( */
 
2566                                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
 
2567                                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
 
2569                         DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
 
2571                         DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
 
2572                         /* Danger, Robinson: If you find a collection of Ks on your disk
 
2573                          * something broke :-( */
 
2574                         if (io_dir & DMACMD_DIR)
 
2575                                 data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
 
2577                                 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
 
2579                 srb->state |= SRB_XFERPAD;
 
2580                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
2582                 bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
 
2583                 DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
 
2588 static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
2591         dprintkdbg(DBG_0, "status_phase0: (pid#%li) <%02i-%i>\n",
 
2592                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
 
2593         srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
 
2594         srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);      /* get message */
 
2595         srb->state = SRB_COMPLETED;
 
2596         *pscsi_status = PH_BUS_FREE;    /*.. initial phase */
 
2597         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
2598         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
 
2602 static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
2605         dprintkdbg(DBG_0, "status_phase1: (pid#%li) <%02i-%i>\n",
 
2606                 srb->cmd->pid, srb->cmd->device->id, srb->cmd->device->lun);
 
2607         srb->state = SRB_STATUS;
 
2608         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
2609         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
 
2613 /* Check if the message is complete */
 
2614 static inline u8 msgin_completed(u8 * msgbuf, u32 len)
 
2616         if (*msgbuf == EXTENDED_MESSAGE) {
 
2619                 if (len < msgbuf[1] + 2)
 
2621         } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f)  /* two byte messages */
 
2628 static inline void msgin_reject(struct AdapterCtlBlk *acb,
 
2629                 struct ScsiReqBlk *srb)
 
2631         srb->msgout_buf[0] = MESSAGE_REJECT;
 
2633         DC395x_ENABLE_MSGOUT;
 
2634         srb->state &= ~SRB_MSGIN;
 
2635         srb->state |= SRB_MSGOUT;
 
2636         dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
 
2638                 srb->dcb->target_id, srb->dcb->target_lun);
 
2642 static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
 
2643                 struct DeviceCtlBlk *dcb, u8 tag)
 
2645         struct ScsiReqBlk *srb = NULL;
 
2646         struct ScsiReqBlk *i;
 
2647         dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) tag=%i srb=%p\n",
 
2648                    srb->cmd->pid, tag, srb);
 
2650         if (!(dcb->tag_mask & (1 << tag)))
 
2651                 dprintkl(KERN_DEBUG,
 
2652                         "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
 
2653                         dcb->tag_mask, tag);
 
2655         if (list_empty(&dcb->srb_going_list))
 
2657         list_for_each_entry(i, &dcb->srb_going_list, list) {
 
2658                 if (i->tag_number == tag) {
 
2666         dprintkdbg(DBG_0, "msgin_qtag: (pid#%li) <%02i-%i>\n",
 
2667                 srb->cmd->pid, srb->dcb->target_id, srb->dcb->target_lun);
 
2668         if (dcb->flag & ABORT_DEV_) {
 
2669                 /*srb->state = SRB_ABORT_SENT; */
 
2670                 enable_msgout_abort(acb, srb);
 
2673         if (!(srb->state & SRB_DISCONNECT))
 
2676         memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
 
2677         srb->state |= dcb->active_srb->state;
 
2678         srb->state |= SRB_DATA_XFER;
 
2679         dcb->active_srb = srb;
 
2680         /* How can we make the DORS happy? */
 
2685         srb->state = SRB_UNEXPECT_RESEL;
 
2686         dcb->active_srb = srb;
 
2687         srb->msgout_buf[0] = MSG_ABORT_TAG;
 
2689         DC395x_ENABLE_MSGOUT;
 
2690         dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
 
2695 static inline void reprogram_regs(struct AdapterCtlBlk *acb,
 
2696                 struct DeviceCtlBlk *dcb)
 
2698         DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
 
2699         DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
 
2700         DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
 
2701         set_xfer_rate(acb, dcb);
 
2705 /* set async transfer mode */
 
2706 static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
 
2708         struct DeviceCtlBlk *dcb = srb->dcb;
 
2709         dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
 
2710                 dcb->target_id, dcb->target_lun);
 
2712         dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
 
2713         dcb->sync_mode |= SYNC_NEGO_DONE;
 
2714         /*dcb->sync_period &= 0; */
 
2715         dcb->sync_offset = 0;
 
2716         dcb->min_nego_period = 200 >> 2;        /* 200ns <=> 5 MHz */
 
2717         srb->state &= ~SRB_DO_SYNC_NEGO;
 
2718         reprogram_regs(acb, dcb);
 
2719         if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
 
2720             && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
 
2721                 build_wdtr(acb, dcb, srb);
 
2722                 DC395x_ENABLE_MSGOUT;
 
2723                 dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
 
2728 /* set sync transfer mode */
 
2729 static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
 
2731         struct DeviceCtlBlk *dcb = srb->dcb;
 
2734         dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
 
2735                 "(%02i.%01i MHz) Offset %i\n",
 
2736                 dcb->target_id, srb->msgin_buf[3] << 2,
 
2737                 (250 / srb->msgin_buf[3]),
 
2738                 ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
 
2741         if (srb->msgin_buf[4] > 15)
 
2742                 srb->msgin_buf[4] = 15;
 
2743         if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
 
2744                 dcb->sync_offset = 0;
 
2745         else if (dcb->sync_offset == 0)
 
2746                 dcb->sync_offset = srb->msgin_buf[4];
 
2747         if (srb->msgin_buf[4] > dcb->sync_offset)
 
2748                 srb->msgin_buf[4] = dcb->sync_offset;
 
2750                 dcb->sync_offset = srb->msgin_buf[4];
 
2752         while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
 
2753                             || dcb->min_nego_period >
 
2754                             clock_period[bval]))
 
2756         if (srb->msgin_buf[3] < clock_period[bval])
 
2758                         "msgin_set_sync: Increase sync nego period to %ins\n",
 
2759                         clock_period[bval] << 2);
 
2760         srb->msgin_buf[3] = clock_period[bval];
 
2761         dcb->sync_period &= 0xf0;
 
2762         dcb->sync_period |= ALT_SYNC | bval;
 
2763         dcb->min_nego_period = srb->msgin_buf[3];
 
2765         if (dcb->sync_period & WIDE_SYNC)
 
2771                 "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
 
2772                 dcb->target_id, (fact == 500) ? "Wide16" : "",
 
2773                 dcb->min_nego_period << 2, dcb->sync_offset,
 
2774                 (fact / dcb->min_nego_period),
 
2775                 ((fact % dcb->min_nego_period) * 10 +
 
2776                 dcb->min_nego_period / 2) / dcb->min_nego_period);
 
2778         if (!(srb->state & SRB_DO_SYNC_NEGO)) {
 
2779                 /* Reply with corrected SDTR Message */
 
2780                 dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
 
2781                         srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
 
2783                 memcpy(srb->msgout_buf, srb->msgin_buf, 5);
 
2785                 DC395x_ENABLE_MSGOUT;
 
2786                 dcb->sync_mode |= SYNC_NEGO_DONE;
 
2788                 if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
 
2789                     && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
 
2790                         build_wdtr(acb, dcb, srb);
 
2791                         DC395x_ENABLE_MSGOUT;
 
2792                         dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
 
2795         srb->state &= ~SRB_DO_SYNC_NEGO;
 
2796         dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
 
2798         reprogram_regs(acb, dcb);
 
2802 static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
 
2803                 struct ScsiReqBlk *srb)
 
2805         struct DeviceCtlBlk *dcb = srb->dcb;
 
2806         dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
 
2808         dcb->sync_period &= ~WIDE_SYNC;
 
2809         dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
 
2810         dcb->sync_mode |= WIDE_NEGO_DONE;
 
2811         srb->state &= ~SRB_DO_WIDE_NEGO;
 
2812         reprogram_regs(acb, dcb);
 
2813         if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
 
2814             && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
 
2815                 build_sdtr(acb, dcb, srb);
 
2816                 DC395x_ENABLE_MSGOUT;
 
2817                 dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
 
2821 static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
 
2823         struct DeviceCtlBlk *dcb = srb->dcb;
 
2824         u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
 
2825                    && acb->config & HCC_WIDE_CARD) ? 1 : 0;
 
2826         dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
 
2828         if (srb->msgin_buf[3] > wide)
 
2829                 srb->msgin_buf[3] = wide;
 
2831         if (!(srb->state & SRB_DO_WIDE_NEGO)) {
 
2832                 dprintkl(KERN_DEBUG,
 
2833                         "msgin_set_wide: Wide nego initiated <%02i>\n",
 
2835                 memcpy(srb->msgout_buf, srb->msgin_buf, 4);
 
2837                 srb->state |= SRB_DO_WIDE_NEGO;
 
2838                 DC395x_ENABLE_MSGOUT;
 
2841         dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
 
2842         if (srb->msgin_buf[3] > 0)
 
2843                 dcb->sync_period |= WIDE_SYNC;
 
2845                 dcb->sync_period &= ~WIDE_SYNC;
 
2846         srb->state &= ~SRB_DO_WIDE_NEGO;
 
2847         /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
 
2849                 "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
 
2850                 (8 << srb->msgin_buf[3]), dcb->target_id);
 
2851         reprogram_regs(acb, dcb);
 
2852         if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
 
2853             && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
 
2854                 build_sdtr(acb, dcb, srb);
 
2855                 DC395x_ENABLE_MSGOUT;
 
2856                 dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
 
2862  * extended message codes:
 
2867  *      00h     MODIFY DATA  POINTER
 
2868  *      01h     SYNCHRONOUS DATA TRANSFER REQUEST
 
2869  *      03h     WIDE DATA TRANSFER REQUEST
 
2870  *   04h - 7Fh  Reserved
 
2871  *   80h - FFh  Vendor specific
 
2873 static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
2876         struct DeviceCtlBlk *dcb = acb->active_dcb;
 
2877         dprintkdbg(DBG_0, "msgin_phase0: (pid#%li)\n", srb->cmd->pid);
 
2879         srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
 
2880         if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
 
2881                 /* Now eval the msg */
 
2882                 switch (srb->msgin_buf[0]) {
 
2884                         srb->state = SRB_DISCONNECT;
 
2887                 case SIMPLE_QUEUE_TAG:
 
2888                 case HEAD_OF_QUEUE_TAG:
 
2889                 case ORDERED_QUEUE_TAG:
 
2891                             msgin_qtag(acb, dcb,
 
2895                 case MESSAGE_REJECT:
 
2896                         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
 
2897                                        DO_CLRATN | DO_DATALATCH);
 
2898                         /* A sync nego message was rejected ! */
 
2899                         if (srb->state & SRB_DO_SYNC_NEGO) {
 
2900                                 msgin_set_async(acb, srb);
 
2903                         /* A wide nego message was rejected ! */
 
2904                         if (srb->state & SRB_DO_WIDE_NEGO) {
 
2905                                 msgin_set_nowide(acb, srb);
 
2908                         enable_msgout_abort(acb, srb);
 
2909                         /*srb->state |= SRB_ABORT_SENT */
 
2912                 case EXTENDED_MESSAGE:
 
2914                         if (srb->msgin_buf[1] == 3
 
2915                             && srb->msgin_buf[2] == EXTENDED_SDTR) {
 
2916                                 msgin_set_sync(acb, srb);
 
2920                         if (srb->msgin_buf[1] == 2
 
2921                             && srb->msgin_buf[2] == EXTENDED_WDTR
 
2922                             && srb->msgin_buf[3] <= 2) { /* sanity check ... */
 
2923                                 msgin_set_wide(acb, srb);
 
2926                         msgin_reject(acb, srb);
 
2929                 case MSG_IGNOREWIDE:
 
2930                         /* Discard  wide residual */
 
2931                         dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
 
2934                 case COMMAND_COMPLETE:
 
2935                         /* nothing has to be done */
 
2940                          * SAVE POINTER may be ignored as we have the struct
 
2941                          * ScsiReqBlk* associated with the scsi command.
 
2943                         dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
 
2944                                 "SAVE POINTER rem=%i Ignore\n",
 
2945                                 srb->cmd->pid, srb->total_xfer_length);
 
2948                 case RESTORE_POINTERS:
 
2949                         dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
 
2953                         dprintkdbg(DBG_0, "msgin_phase0: (pid#%li) "
 
2954                                 "<%02i-%i> ABORT msg\n",
 
2955                                 srb->cmd->pid, dcb->target_id,
 
2957                         dcb->flag |= ABORT_DEV_;
 
2958                         enable_msgout_abort(acb, srb);
 
2962                         /* reject unknown messages */
 
2963                         if (srb->msgin_buf[0] & IDENTIFY_BASE) {
 
2964                                 dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
 
2966                                 srb->msgout_buf[0] = dcb->identify_msg;
 
2967                                 DC395x_ENABLE_MSGOUT;
 
2968                                 srb->state |= SRB_MSGOUT;
 
2971                         msgin_reject(acb, srb);
 
2974                 /* Clear counter and MsgIn state */
 
2975                 srb->state &= ~SRB_MSGIN;
 
2978         *pscsi_status = PH_BUS_FREE;
 
2979         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important ... you know! */
 
2980         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
 
2984 static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
2987         dprintkdbg(DBG_0, "msgin_phase1: (pid#%li)\n", srb->cmd->pid);
 
2988         clear_fifo(acb, "msgin_phase1");
 
2989         DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
 
2990         if (!(srb->state & SRB_MSGIN)) {
 
2991                 srb->state &= ~SRB_DISCONNECT;
 
2992                 srb->state |= SRB_MSGIN;
 
2994         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
2996         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
 
3000 static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
3006 static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 
3012 static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
 
3014         struct DeviceCtlBlk *i;
 
3016         /* set all lun device's  period, offset */
 
3017         if (dcb->identify_msg & 0x07)
 
3020         if (acb->scan_devices) {
 
3021                 current_sync_offset = dcb->sync_offset;
 
3025         list_for_each_entry(i, &acb->dcb_list, list)
 
3026                 if (i->target_id == dcb->target_id) {
 
3027                         i->sync_period = dcb->sync_period;
 
3028                         i->sync_offset = dcb->sync_offset;
 
3029                         i->sync_mode = dcb->sync_mode;
 
3030                         i->min_nego_period = dcb->min_nego_period;
 
3035 static void disconnect(struct AdapterCtlBlk *acb)
 
3037         struct DeviceCtlBlk *dcb = acb->active_dcb;
 
3038         struct ScsiReqBlk *srb;
 
3041                 dprintkl(KERN_ERR, "disconnect: No such device\n");
 
3043                 /* Suspend queue for a while */
 
3044                 acb->scsi_host->last_reset =
 
3046                     HZ * acb->eeprom.delay_time;
 
3047                 clear_fifo(acb, "disconnectEx");
 
3048                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
 
3051         srb = dcb->active_srb;
 
3052         acb->active_dcb = NULL;
 
3053         dprintkdbg(DBG_0, "disconnect: (pid#%li)\n", srb->cmd->pid);
 
3055         srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
 
3056         clear_fifo(acb, "disconnect");
 
3057         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
 
3058         if (srb->state & SRB_UNEXPECT_RESEL) {
 
3060                         "disconnect: Unexpected reselection <%02i-%i>\n",
 
3061                         dcb->target_id, dcb->target_lun);
 
3063                 waiting_process_next(acb);
 
3064         } else if (srb->state & SRB_ABORT_SENT) {
 
3065                 dcb->flag &= ~ABORT_DEV_;
 
3066                 acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
 
3067                 dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
 
3068                 doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
 
3069                 waiting_process_next(acb);
 
3071                 if ((srb->state & (SRB_START_ + SRB_MSGOUT))
 
3073                          state & (SRB_DISCONNECT + SRB_COMPLETED))) {
 
3075                          * Selection time out 
 
3076                          * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
 
3078                         /* Unexp. Disc / Sel Timeout */
 
3079                         if (srb->state != SRB_START_
 
3080                             && srb->state != SRB_MSGOUT) {
 
3081                                 srb->state = SRB_READY;
 
3082                                 dprintkl(KERN_DEBUG,
 
3083                                         "disconnect: (pid#%li) Unexpected\n",
 
3085                                 srb->target_status = SCSI_STAT_SEL_TIMEOUT;
 
3088                                 /* Normal selection timeout */
 
3089                                 dprintkdbg(DBG_KG, "disconnect: (pid#%li) "
 
3090                                         "<%02i-%i> SelTO\n", srb->cmd->pid,
 
3091                                         dcb->target_id, dcb->target_lun);
 
3092                                 if (srb->retry_count++ > DC395x_MAX_RETRIES
 
3093                                     || acb->scan_devices) {
 
3094                                         srb->target_status =
 
3095                                             SCSI_STAT_SEL_TIMEOUT;
 
3099                                 srb_going_to_waiting_move(dcb, srb);
 
3101                                         "disconnect: (pid#%li) Retry\n",
 
3103                                 waiting_set_timer(acb, HZ / 20);
 
3105                 } else if (srb->state & SRB_DISCONNECT) {
 
3106                         u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
 
3108                          * SRB_DISCONNECT (This is what we expect!)
 
3111                                 dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
 
3112                                         " 0x%02x: ACK set! Other controllers?\n",
 
3114                                 /* It could come from another initiator, therefore don't do much ! */
 
3116                                 waiting_process_next(acb);
 
3117                 } else if (srb->state & SRB_COMPLETED) {
 
3123                         dcb->active_srb = NULL;
 
3124                         srb->state = SRB_FREE;
 
3125                         srb_done(acb, dcb, srb);
 
3131 static void reselect(struct AdapterCtlBlk *acb)
 
3133         struct DeviceCtlBlk *dcb = acb->active_dcb;
 
3134         struct ScsiReqBlk *srb = NULL;
 
3135         u16 rsel_tar_lun_id;
 
3138         dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
 
3140         clear_fifo(acb, "reselect");
 
3141         /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
 
3142         /* Read Reselected Target ID and LUN */
 
3143         rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
 
3144         if (dcb) {              /* Arbitration lost but Reselection win */
 
3145                 srb = dcb->active_srb;
 
3147                         dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
 
3148                                 "but active_srb == NULL\n");
 
3149                         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
3153                 if (!acb->scan_devices) {
 
3154                         dprintkdbg(DBG_KG, "reselect: (pid#%li) <%02i-%i> "
 
3155                                 "Arb lost but Resel win rsel=%i stat=0x%04x\n",
 
3156                                 srb->cmd->pid, dcb->target_id,
 
3157                                 dcb->target_lun, rsel_tar_lun_id,
 
3158                                 DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
 
3160                         /*srb->state |= SRB_DISCONNECT; */
 
3162                         srb->state = SRB_READY;
 
3164                         srb_going_to_waiting_move(dcb, srb);
 
3165                         waiting_set_timer(acb, HZ / 20);
 
3170         /* Read Reselected Target Id and LUN */
 
3171         if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
 
3172                 dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
 
3173                         "Got %i!\n", rsel_tar_lun_id);
 
3174         id = rsel_tar_lun_id & 0xff;
 
3175         lun = (rsel_tar_lun_id >> 8) & 7;
 
3176         dcb = find_dcb(acb, id, lun);
 
3178                 dprintkl(KERN_ERR, "reselect: From non existent device "
 
3179                         "<%02i-%i>\n", id, lun);
 
3180                 DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);      /* it's important for atn stop */
 
3183         acb->active_dcb = dcb;
 
3185         if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
 
3186                 dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
 
3187                         "disconnection? <%02i-%i>\n",
 
3188                         dcb->target_id, dcb->target_lun);
 
3190         if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
 
3192                 dcb->active_srb = srb;
 
3194                 /* There can be only one! */
 
3195                 srb = dcb->active_srb;
 
3196                 if (!srb || !(srb->state & SRB_DISCONNECT)) {
 
3200                         dprintkl(KERN_DEBUG,
 
3201                                 "reselect: w/o disconnected cmds <%02i-%i>\n",
 
3202                                 dcb->target_id, dcb->target_lun);
 
3204                         srb->state = SRB_UNEXPECT_RESEL;
 
3205                         dcb->active_srb = srb;
 
3206                         enable_msgout_abort(acb, srb);
 
3208                         if (dcb->flag & ABORT_DEV_) {
 
3209                                 /*srb->state = SRB_ABORT_SENT; */
 
3210                                 enable_msgout_abort(acb, srb);
 
3212                                 srb->state = SRB_DATA_XFER;
 
3216         srb->scsi_phase = PH_BUS_FREE;  /* initial phase */
 
3218         /* Program HA ID, target ID, period and offset */
 
3219         dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
 
3220         DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);     /* host   ID */
 
3221         DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);            /* target ID */
 
3222         DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);            /* offset    */
 
3223         DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);              /* sync period, wide */
 
3224         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);              /* it's important for atn stop */
 
3226         DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
 
3230 static inline u8 tagq_blacklist(char *name)
 
3232 #ifndef DC395x_NO_TAGQ
 
3235         for (i = 0; i < BADDEVCNT; i++)
 
3236                 if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
 
3246 static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
 
3248         /* Check for SCSI format (ANSI and Response data format) */
 
3249         if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
 
3250                 if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
 
3251                     && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
 
3252                     /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
 
3253                     /* ((dcb->dev_type == TYPE_DISK) 
 
3254                        || (dcb->dev_type == TYPE_MOD)) && */
 
3255                     !tagq_blacklist(((char *)ptr) + 8)) {
 
3256                         if (dcb->max_command == 1)
 
3258                                     dcb->acb->tag_max_num;
 
3259                         dcb->sync_mode |= EN_TAG_QUEUEING;
 
3260                         /*dcb->tag_mask = 0; */
 
3262                         dcb->max_command = 1;
 
3267 static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 
3268                 struct ScsiInqData *ptr)
 
3270         u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
 
3271         dcb->dev_type = bval1;
 
3272         /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
 
3273         disc_tagq_set(dcb, ptr);
 
3277 /* unmap mapped pci regions from SRB */
 
3278 static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
 
3280         struct scsi_cmnd *cmd = srb->cmd;
 
3281         enum dma_data_direction dir = cmd->sc_data_direction;
 
3282         if (cmd->use_sg && dir != PCI_DMA_NONE) {
 
3283                 /* unmap DC395x SG list */
 
3284                 dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
 
3285                         srb->sg_bus_addr, SEGMENTX_LEN);
 
3286                 pci_unmap_single(acb->dev, srb->sg_bus_addr,
 
3289                 dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
 
3290                         cmd->use_sg, cmd->request_buffer);
 
3291                 /* unmap the sg segments */
 
3292                 pci_unmap_sg(acb->dev,
 
3293                              (struct scatterlist *)cmd->request_buffer,
 
3295         } else if (cmd->request_buffer && dir != PCI_DMA_NONE) {
 
3296                 dprintkdbg(DBG_SG, "pci_unmap_srb: buffer=%08x(%05x)\n",
 
3297                         srb->segment_x[0].address, cmd->request_bufflen);
 
3298                 pci_unmap_single(acb->dev, srb->segment_x[0].address,
 
3299                                  cmd->request_bufflen, dir);
 
3304 /* unmap mapped pci sense buffer from SRB */
 
3305 static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
 
3306                 struct ScsiReqBlk *srb)
 
3308         if (!(srb->flag & AUTO_REQSENSE))
 
3310         /* Unmap sense buffer */
 
3311         dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
 
3312                srb->segment_x[0].address);
 
3313         pci_unmap_single(acb->dev, srb->segment_x[0].address,
 
3314                          srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
 
3315         /* Restore SG stuff */
 
3316         srb->total_xfer_length = srb->xferred;
 
3317         srb->segment_x[0].address =
 
3318             srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
 
3319         srb->segment_x[0].length =
 
3320             srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
 
3325  * Complete execution of a SCSI command
 
3326  * Signal completion to the generic SCSI driver  
 
3328 static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 
3329                 struct ScsiReqBlk *srb)
 
3332         struct scsi_cmnd *cmd = srb->cmd;
 
3333         struct ScsiInqData *ptr;
 
3334         enum dma_data_direction dir = cmd->sc_data_direction;
 
3337                 struct scatterlist* sg = (struct scatterlist *)cmd->request_buffer;
 
3338                 ptr = (struct ScsiInqData *)(page_address(sg->page) + sg->offset);
 
3340                 ptr = (struct ScsiInqData *)(cmd->request_buffer);
 
3343         dprintkdbg(DBG_1, "srb_done: (pid#%li) <%02i-%i>\n", srb->cmd->pid,
 
3344                 srb->cmd->device->id, srb->cmd->device->lun);
 
3345         dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p addr=%p\n",
 
3346                 srb, cmd->use_sg, srb->sg_index, srb->sg_count,
 
3347                 cmd->request_buffer, ptr);
 
3348         status = srb->target_status;
 
3349         if (srb->flag & AUTO_REQSENSE) {
 
3350                 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
 
3351                 pci_unmap_srb_sense(acb, srb);
 
3353                  ** target status..........................
 
3355                 srb->flag &= ~AUTO_REQSENSE;
 
3356                 srb->adapter_status = 0;
 
3357                 srb->target_status = CHECK_CONDITION << 1;
 
3358                 if (debug_enabled(DBG_1)) {
 
3359                         switch (cmd->sense_buffer[2] & 0x0f) {
 
3361                                 dprintkl(KERN_DEBUG,
 
3362                                      "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
 
3363                                      cmd->cmnd[0], dcb->target_id,
 
3364                                      dcb->target_lun, status, acb->scan_devices);
 
3366                         case UNIT_ATTENTION:
 
3367                                 dprintkl(KERN_DEBUG,
 
3368                                      "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
 
3369                                      cmd->cmnd[0], dcb->target_id,
 
3370                                      dcb->target_lun, status, acb->scan_devices);
 
3372                         case ILLEGAL_REQUEST:
 
3373                                 dprintkl(KERN_DEBUG,
 
3374                                      "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
 
3375                                      cmd->cmnd[0], dcb->target_id,
 
3376                                      dcb->target_lun, status, acb->scan_devices);
 
3379                                 dprintkl(KERN_DEBUG,
 
3380                                      "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
 
3381                                      cmd->cmnd[0], dcb->target_id,
 
3382                                      dcb->target_lun, status, acb->scan_devices);
 
3384                         case HARDWARE_ERROR:
 
3385                                 dprintkl(KERN_DEBUG,
 
3386                                      "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
 
3387                                      cmd->cmnd[0], dcb->target_id,
 
3388                                      dcb->target_lun, status, acb->scan_devices);
 
3391                         if (cmd->sense_buffer[7] >= 6)
 
3392                                 printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
 
3393                                         "(0x%08x 0x%08x)\n",
 
3394                                         cmd->sense_buffer[2], cmd->sense_buffer[12],
 
3395                                         cmd->sense_buffer[13],
 
3396                                         *((unsigned int *)(cmd->sense_buffer + 3)),
 
3397                                         *((unsigned int *)(cmd->sense_buffer + 8)));
 
3399                                 printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
 
3400                                         cmd->sense_buffer[2],
 
3401                                         *((unsigned int *)(cmd->sense_buffer + 3)));
 
3404                 if (status == (CHECK_CONDITION << 1)) {
 
3405                         cmd->result = DID_BAD_TARGET << 16;
 
3408                 dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
 
3410                 if (srb->total_xfer_length
 
3411                     && srb->total_xfer_length >= cmd->underflow)
 
3413                             MK_RES_LNX(DRIVER_SENSE, DID_OK,
 
3414                                        srb->end_message, CHECK_CONDITION);
 
3415                 /*SET_RES_DID(cmd->result,DID_OK) */
 
3418                             MK_RES_LNX(DRIVER_SENSE, DID_OK,
 
3419                                        srb->end_message, CHECK_CONDITION);
 
3424 /*************************************************************/
 
3427                  * target status..........................
 
3429                 if (status_byte(status) == CHECK_CONDITION) {
 
3430                         request_sense(acb, dcb, srb);
 
3432                 } else if (status_byte(status) == QUEUE_FULL) {
 
3433                         tempcnt = (u8)list_size(&dcb->srb_going_list);
 
3434                         dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
 
3435                              dcb->target_id, dcb->target_lun, tempcnt);
 
3438                         dcb->max_command = tempcnt;
 
3440                         srb_going_to_waiting_move(dcb, srb);
 
3441                         waiting_set_timer(acb, HZ / 20);
 
3442                         srb->adapter_status = 0;
 
3443                         srb->target_status = 0;
 
3445                 } else if (status == SCSI_STAT_SEL_TIMEOUT) {
 
3446                         srb->adapter_status = H_SEL_TIMEOUT;
 
3447                         srb->target_status = 0;
 
3448                         cmd->result = DID_NO_CONNECT << 16;
 
3450                         srb->adapter_status = 0;
 
3451                         SET_RES_DID(cmd->result, DID_ERROR);
 
3452                         SET_RES_MSG(cmd->result, srb->end_message);
 
3453                         SET_RES_TARGET(cmd->result, status);
 
3458                  ** process initiator status..........................
 
3460                 status = srb->adapter_status;
 
3461                 if (status & H_OVER_UNDER_RUN) {
 
3462                         srb->target_status = 0;
 
3463                         SET_RES_DID(cmd->result, DID_OK);
 
3464                         SET_RES_MSG(cmd->result, srb->end_message);
 
3465                 } else if (srb->status & PARITY_ERROR) {
 
3466                         SET_RES_DID(cmd->result, DID_PARITY);
 
3467                         SET_RES_MSG(cmd->result, srb->end_message);
 
3468                 } else {        /* No error */
 
3470                         srb->adapter_status = 0;
 
3471                         srb->target_status = 0;
 
3472                         SET_RES_DID(cmd->result, DID_OK);
 
3476         if (dir != PCI_DMA_NONE) {
 
3478                         pci_dma_sync_sg_for_cpu(acb->dev,
 
3479                                         (struct scatterlist *)cmd->
 
3480                                         request_buffer, cmd->use_sg, dir);
 
3481                 else if (cmd->request_buffer)
 
3482                         pci_dma_sync_single_for_cpu(acb->dev,
 
3483                                             srb->segment_x[0].address,
 
3484                                             cmd->request_bufflen, dir);
 
3487         if ((cmd->result & RES_DID) == 0 && cmd->cmnd[0] == INQUIRY
 
3488             && cmd->cmnd[2] == 0 && cmd->request_bufflen >= 8
 
3489             && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
 
3490                 dcb->inquiry7 = ptr->Flags;
 
3491 /* Check Error Conditions */
 
3494         /*if( srb->cmd->cmnd[0] == INQUIRY && */
 
3495         /*  (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
 
3496         if (cmd->cmnd[0] == INQUIRY && (cmd->result == (DID_OK << 16)
 
3497                                          || status_byte(cmd->
 
3501                 if (!dcb->init_tcq_flag) {
 
3502                         add_dev(acb, dcb, ptr);
 
3503                         dcb->init_tcq_flag = 1;
 
3509         /* Here is the info for Doug Gilbert's sg3 ... */
 
3510         cmd->resid = srb->total_xfer_length;
 
3511         /* This may be interpreted by sb. or not ... */
 
3512         cmd->SCp.this_residual = srb->total_xfer_length;
 
3513         cmd->SCp.buffers_residual = 0;
 
3514         if (debug_enabled(DBG_KG)) {
 
3515                 if (srb->total_xfer_length)
 
3516                         dprintkdbg(DBG_KG, "srb_done: (pid#%li) <%02i-%i> "
 
3517                                 "cmnd=0x%02x Missed %i bytes\n",
 
3518                                 cmd->pid, cmd->device->id, cmd->device->lun,
 
3519                                 cmd->cmnd[0], srb->total_xfer_length);
 
3522         srb_going_remove(dcb, srb);
 
3523         /* Add to free list */
 
3524         if (srb == acb->tmp_srb)
 
3525                 dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
 
3527                 dprintkdbg(DBG_0, "srb_done: (pid#%li) done result=0x%08x\n",
 
3528                         cmd->pid, cmd->result);
 
3529                 srb_free_insert(acb, srb);
 
3531         pci_unmap_srb(acb, srb);
 
3533         cmd->scsi_done(cmd);
 
3534         waiting_process_next(acb);
 
3538 /* abort all cmds in our queues */
 
3539 static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
 
3540                 struct scsi_cmnd *cmd, u8 force)
 
3542         struct DeviceCtlBlk *dcb;
 
3543         dprintkl(KERN_INFO, "doing_srb_done: pids ");
 
3545         list_for_each_entry(dcb, &acb->dcb_list, list) {
 
3546                 struct ScsiReqBlk *srb;
 
3547                 struct ScsiReqBlk *tmp;
 
3548                 struct scsi_cmnd *p;
 
3550                 list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
 
3551                         enum dma_data_direction dir;
 
3555                         dir = p->sc_data_direction;
 
3556                         result = MK_RES(0, did_flag, 0, 0);
 
3557                         printk("G:%li(%02i-%i) ", p->pid,
 
3558                                p->device->id, p->device->lun);
 
3559                         srb_going_remove(dcb, srb);
 
3561                         srb_free_insert(acb, srb);
 
3563                         pci_unmap_srb_sense(acb, srb);
 
3564                         pci_unmap_srb(acb, srb);
 
3566                                 /* For new EH, we normally don't need to give commands back,
 
3567                                  * as they all complete or all time out */
 
3571                 if (!list_empty(&dcb->srb_going_list))
 
3572                         dprintkl(KERN_DEBUG, 
 
3573                                "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
 
3574                                dcb->target_id, dcb->target_lun);
 
3576                         dprintkl(KERN_DEBUG,
 
3577                                "tag_mask for <%02i-%i> should be empty, is %08x!\n",
 
3578                                dcb->target_id, dcb->target_lun,
 
3582                 list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
 
3586                         result = MK_RES(0, did_flag, 0, 0);
 
3587                         printk("W:%li<%02i-%i>", p->pid, p->device->id,
 
3589                         srb_waiting_remove(dcb, srb);
 
3590                         srb_free_insert(acb, srb);
 
3592                         pci_unmap_srb_sense(acb, srb);
 
3593                         pci_unmap_srb(acb, srb);
 
3595                                 /* For new EH, we normally don't need to give commands back,
 
3596                                  * as they all complete or all time out */
 
3597                                 cmd->scsi_done(cmd);
 
3600                 if (!list_empty(&dcb->srb_waiting_list))
 
3601                         dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
 
3602                              list_size(&dcb->srb_waiting_list), dcb->target_id,
 
3604                 dcb->flag &= ~ABORT_DEV_;
 
3610 static void reset_scsi_bus(struct AdapterCtlBlk *acb)
 
3612         dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
 
3613         acb->acb_flag |= RESET_DEV;     /* RESET_DETECT, RESET_DONE, RESET_DEV */
 
3614         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
 
3616         while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
 
3621 static void set_basic_config(struct AdapterCtlBlk *acb)
 
3625         DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
 
3626         if (acb->config & HCC_PARITY)
 
3627                 bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
 
3629                 bval = PHASELATCH | INITIATOR | BLOCKRST;
 
3631         DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
 
3633         /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
 
3634         DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03);       /* was 0x13: default */
 
3635         /* program Host ID                  */
 
3636         DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
 
3637         /* set ansynchronous transfer       */
 
3638         DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
 
3639         /* Turn LED control off */
 
3640         wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
 
3641         DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
 
3643         wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
 
3645             DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
 
3646         DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
 
3647         /* Clear pending interrupt status */
 
3648         DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
 
3649         /* Enable SCSI interrupt    */
 
3650         DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
 
3651         DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
 
3652                       /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
 
3657 static void scsi_reset_detect(struct AdapterCtlBlk *acb)
 
3659         dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
 
3660         /* delay half a second */
 
3661         if (timer_pending(&acb->waiting_timer))
 
3662                 del_timer(&acb->waiting_timer);
 
3664         DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
 
3665         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
 
3666         /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
 
3668         /* Maybe we locked up the bus? Then lets wait even longer ... */
 
3669         acb->scsi_host->last_reset =
 
3670             jiffies + 5 * HZ / 2 +
 
3671             HZ * acb->eeprom.delay_time;
 
3673         clear_fifo(acb, "scsi_reset_detect");
 
3674         set_basic_config(acb);
 
3676         /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
 
3678         if (acb->acb_flag & RESET_DEV) {        /* RESET_DETECT, RESET_DONE, RESET_DEV */
 
3679                 acb->acb_flag |= RESET_DONE;
 
3681                 acb->acb_flag |= RESET_DETECT;
 
3682                 reset_dev_param(acb);
 
3683                 doing_srb_done(acb, DID_RESET, NULL, 1);
 
3684                 /*DC395x_RecoverSRB( acb ); */
 
3685                 acb->active_dcb = NULL;
 
3687                 waiting_process_next(acb);
 
3692 static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 
3693                 struct ScsiReqBlk *srb)
 
3695         struct scsi_cmnd *cmd = srb->cmd;
 
3696         dprintkdbg(DBG_1, "request_sense: (pid#%li) <%02i-%i>\n",
 
3697                 cmd->pid, cmd->device->id, cmd->device->lun);
 
3699         srb->flag |= AUTO_REQSENSE;
 
3700         srb->adapter_status = 0;
 
3701         srb->target_status = 0;
 
3703         /* KG: Can this prevent crap sense data ? */
 
3704         memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
 
3706         /* Save some data */
 
3707         srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
 
3708             srb->segment_x[0].address;
 
3709         srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
 
3710             srb->segment_x[0].length;
 
3711         srb->xferred = srb->total_xfer_length;
 
3712         /* srb->segment_x : a one entry of S/G list table */
 
3713         srb->total_xfer_length = sizeof(cmd->sense_buffer);
 
3714         srb->segment_x[0].length = sizeof(cmd->sense_buffer);
 
3715         /* Map sense buffer */
 
3716         srb->segment_x[0].address =
 
3717             pci_map_single(acb->dev, cmd->sense_buffer,
 
3718                            sizeof(cmd->sense_buffer), PCI_DMA_FROMDEVICE);
 
3719         dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
 
3720                cmd->sense_buffer, srb->segment_x[0].address,
 
3721                sizeof(cmd->sense_buffer));
 
3725         if (start_scsi(acb, dcb, srb)) {        /* Should only happen, if sb. else grabs the bus */
 
3726                 dprintkl(KERN_DEBUG,
 
3727                         "request_sense: (pid#%li) failed <%02i-%i>\n",
 
3728                         srb->cmd->pid, dcb->target_id, dcb->target_lun);
 
3729                 srb_going_to_waiting_move(dcb, srb);
 
3730                 waiting_set_timer(acb, HZ / 100);
 
3736  * device_alloc - Allocate a new device instance. This create the
 
3737  * devices instance and sets up all the data items. The adapter
 
3738  * instance is required to obtain confiuration information for this
 
3739  * device. This does *not* add this device to the adapters device
 
3742  * @acb: The adapter to obtain configuration information from.
 
3743  * @target: The target for the new device.
 
3744  * @lun: The lun for the new device.
 
3746  * Return the new device if succesfull or NULL on failure.
 
3748 static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
 
3751         struct NvRamType *eeprom = &acb->eeprom;
 
3752         u8 period_index = eeprom->target[target].period & 0x07;
 
3753         struct DeviceCtlBlk *dcb;
 
3755         dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
 
3756         dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
 
3760         INIT_LIST_HEAD(&dcb->srb_going_list);
 
3761         INIT_LIST_HEAD(&dcb->srb_waiting_list);
 
3762         dcb->active_srb = NULL;
 
3764         dcb->max_command = 1;
 
3765         dcb->target_id = target;
 
3766         dcb->target_lun = lun;
 
3767 #ifndef DC395x_NO_DISCONNECT
 
3769             IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
 
3771         dcb->identify_msg = IDENTIFY(0, lun);
 
3773         dcb->dev_mode = eeprom->target[target].cfg0;
 
3776         dcb->min_nego_period = clock_period[period_index];
 
3777         dcb->sync_period = 0;
 
3778         dcb->sync_offset = 0;
 
3781 #ifndef DC395x_NO_WIDE
 
3782         if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
 
3783             && (acb->config & HCC_WIDE_CARD))
 
3784                 dcb->sync_mode |= WIDE_NEGO_ENABLE;
 
3786 #ifndef DC395x_NO_SYNC
 
3787         if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
 
3788                 if (!(lun) || current_sync_offset)
 
3789                         dcb->sync_mode |= SYNC_NEGO_ENABLE;
 
3791         if (dcb->target_lun != 0) {
 
3793                 struct DeviceCtlBlk *p;
 
3794                 list_for_each_entry(p, &acb->dcb_list, list)
 
3795                         if (p->target_id == dcb->target_id)
 
3798                        "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
 
3799                        dcb->target_id, dcb->target_lun,
 
3800                        p->target_id, p->target_lun);
 
3801                 dcb->sync_mode = p->sync_mode;
 
3802                 dcb->sync_period = p->sync_period;
 
3803                 dcb->min_nego_period = p->min_nego_period;
 
3804                 dcb->sync_offset = p->sync_offset;
 
3805                 dcb->inquiry7 = p->inquiry7;
 
3812  * adapter_add_device - Adds the device instance to the adaptor instance.
 
3814  * @acb: The adapter device to be updated
 
3815  * @dcb: A newly created and intialised device instance to add.
 
3817 static void adapter_add_device(struct AdapterCtlBlk *acb,
 
3818                 struct DeviceCtlBlk *dcb)
 
3820         /* backpointer to adapter */
 
3823         /* set run_robin to this device if it is currently empty */
 
3824         if (list_empty(&acb->dcb_list))
 
3825                 acb->dcb_run_robin = dcb;
 
3827         /* add device to list */
 
3828         list_add_tail(&dcb->list, &acb->dcb_list);
 
3830         /* update device maps */
 
3831         acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
 
3832         acb->children[dcb->target_id][dcb->target_lun] = dcb;
 
3837  * adapter_remove_device - Removes the device instance from the adaptor
 
3838  * instance. The device instance is not check in any way or freed by this. 
 
3839  * The caller is expected to take care of that. This will simply remove the
 
3840  * device from the adapters data strcutures.
 
3842  * @acb: The adapter device to be updated
 
3843  * @dcb: A device that has previously been added to the adapter.
 
3845 static void adapter_remove_device(struct AdapterCtlBlk *acb,
 
3846                 struct DeviceCtlBlk *dcb)
 
3848         struct DeviceCtlBlk *i;
 
3849         struct DeviceCtlBlk *tmp;
 
3850         dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
 
3851                 dcb->target_id, dcb->target_lun);
 
3853         /* fix up any pointers to this device that we have in the adapter */
 
3854         if (acb->active_dcb == dcb)
 
3855                 acb->active_dcb = NULL;
 
3856         if (acb->dcb_run_robin == dcb)
 
3857                 acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
 
3859         /* unlink from list */
 
3860         list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
 
3866         /* clear map and children */    
 
3867         acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
 
3868         acb->children[dcb->target_id][dcb->target_lun] = NULL;
 
3874  * adapter_remove_and_free_device - Removes a single device from the adapter
 
3875  * and then frees the device information.
 
3877  * @acb: The adapter device to be updated
 
3878  * @dcb: A device that has previously been added to the adapter.
 
3880 static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
 
3881                 struct DeviceCtlBlk *dcb)
 
3883         if (list_size(&dcb->srb_going_list) > 1) {
 
3884                 dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
 
3885                            "Won't remove because of %i active requests.\n",
 
3886                            dcb->target_id, dcb->target_lun,
 
3887                            list_size(&dcb->srb_going_list));
 
3890         adapter_remove_device(acb, dcb);
 
3896  * adapter_remove_and_free_all_devices - Removes and frees all of the
 
3897  * devices associated with the specified adapter.
 
3899  * @acb: The adapter from which all devices should be removed.
 
3901 static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
 
3903         struct DeviceCtlBlk *dcb;
 
3904         struct DeviceCtlBlk *tmp;
 
3905         dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
 
3906                    list_size(&acb->dcb_list));
 
3908         list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
 
3909                 adapter_remove_and_free_device(acb, dcb);
 
3914  * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
 
3915  * scsi device that we need to deal with. We allocate a new device and then
 
3916  * insert that device into the adapters device list.
 
3918  * @scsi_device: The new scsi device that we need to handle.
 
3920 static int dc395x_slave_alloc(struct scsi_device *scsi_device)
 
3922         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
 
3923         struct DeviceCtlBlk *dcb;
 
3925         dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
 
3928         adapter_add_device(acb, dcb);
 
3935  * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
 
3936  * device that is going away.
 
3938  * @scsi_device: The new scsi device that we need to handle.
 
3940 static void dc395x_slave_destroy(struct scsi_device *scsi_device)
 
3942         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
 
3943         struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
 
3945                 adapter_remove_and_free_device(acb, dcb);
 
3952  * trms1040_wait_30us: wait for 30 us
 
3954  * Waits for 30us (using the chip by the looks of it..)
 
3956  * @io_port: base I/O address
 
3958 static void __devinit trms1040_wait_30us(unsigned long io_port)
 
3960         /* ScsiPortStallExecution(30); wait 30 us */
 
3961         outb(5, io_port + TRM_S1040_GEN_TIMER);
 
3962         while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
 
3968  * trms1040_write_cmd - write the secified command and address to
 
3971  * @io_port:    base I/O address
 
3972  * @cmd:        SB + op code (command) to send
 
3973  * @addr:       address to send
 
3975 static void __devinit trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
 
3980         /* program SB + OP code */
 
3981         for (i = 0; i < 3; i++, cmd <<= 1) {
 
3982                 send_data = NVR_SELECT;
 
3983                 if (cmd & 0x04) /* Start from bit 2 */
 
3984                         send_data |= NVR_BITOUT;
 
3986                 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
 
3987                 trms1040_wait_30us(io_port);
 
3988                 outb((send_data | NVR_CLOCK),
 
3989                      io_port + TRM_S1040_GEN_NVRAM);
 
3990                 trms1040_wait_30us(io_port);
 
3994         for (i = 0; i < 7; i++, addr <<= 1) {
 
3995                 send_data = NVR_SELECT;
 
3996                 if (addr & 0x40)        /* Start from bit 6 */
 
3997                         send_data |= NVR_BITOUT;
 
3999                 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
 
4000                 trms1040_wait_30us(io_port);
 
4001                 outb((send_data | NVR_CLOCK),
 
4002                      io_port + TRM_S1040_GEN_NVRAM);
 
4003                 trms1040_wait_30us(io_port);
 
4005         outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
 
4006         trms1040_wait_30us(io_port);
 
4011  * trms1040_set_data - store a single byte in the eeprom
 
4013  * Called from write all to write a single byte into the SSEEPROM
 
4014  * Which is done one bit at a time.
 
4016  * @io_port:    base I/O address
 
4017  * @addr:       offset into EEPROM
 
4018  * @byte:       bytes to write
 
4020 static void __devinit trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
 
4025         /* Send write command & address */
 
4026         trms1040_write_cmd(io_port, 0x05, addr);
 
4029         for (i = 0; i < 8; i++, byte <<= 1) {
 
4030                 send_data = NVR_SELECT;
 
4031                 if (byte & 0x80)        /* Start from bit 7 */
 
4032                         send_data |= NVR_BITOUT;
 
4034                 outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
 
4035                 trms1040_wait_30us(io_port);
 
4036                 outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
 
4037                 trms1040_wait_30us(io_port);
 
4039         outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
 
4040         trms1040_wait_30us(io_port);
 
4042         /* Disable chip select */
 
4043         outb(0, io_port + TRM_S1040_GEN_NVRAM);
 
4044         trms1040_wait_30us(io_port);
 
4046         outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
 
4047         trms1040_wait_30us(io_port);
 
4049         /* Wait for write ready */
 
4051                 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
 
4052                 trms1040_wait_30us(io_port);
 
4054                 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
 
4055                 trms1040_wait_30us(io_port);
 
4057                 if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
 
4061         /*  Disable chip select */
 
4062         outb(0, io_port + TRM_S1040_GEN_NVRAM);
 
4067  * trms1040_write_all - write 128 bytes to the eeprom
 
4069  * Write the supplied 128 bytes to the chips SEEPROM
 
4071  * @eeprom:     the data to write
 
4072  * @io_port:    the base io port
 
4074 static void __devinit trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
 
4076         u8 *b_eeprom = (u8 *)eeprom;
 
4079         /* Enable SEEPROM */
 
4080         outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
 
4081              io_port + TRM_S1040_GEN_CONTROL);
 
4084         trms1040_write_cmd(io_port, 0x04, 0xFF);
 
4085         outb(0, io_port + TRM_S1040_GEN_NVRAM);
 
4086         trms1040_wait_30us(io_port);
 
4089         for (addr = 0; addr < 128; addr++, b_eeprom++)
 
4090                 trms1040_set_data(io_port, addr, *b_eeprom);
 
4093         trms1040_write_cmd(io_port, 0x04, 0x00);
 
4094         outb(0, io_port + TRM_S1040_GEN_NVRAM);
 
4095         trms1040_wait_30us(io_port);
 
4097         /* Disable SEEPROM */
 
4098         outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
 
4099              io_port + TRM_S1040_GEN_CONTROL);
 
4104  * trms1040_get_data - get a single byte from the eeprom
 
4106  * Called from read all to read a single byte into the SSEEPROM
 
4107  * Which is done one bit at a time.
 
4109  * @io_port:    base I/O address
 
4110  * @addr:       offset into SEEPROM
 
4112  * Returns the the byte read.
 
4114 static u8 __devinit trms1040_get_data(unsigned long io_port, u8 addr)
 
4120         /* Send read command & address */
 
4121         trms1040_write_cmd(io_port, 0x06, addr);
 
4124         for (i = 0; i < 8; i++) {
 
4125                 outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
 
4126                 trms1040_wait_30us(io_port);
 
4127                 outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
 
4129                 /* Get data bit while falling edge */
 
4130                 read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
 
4132                 if (read_byte & NVR_BITIN)
 
4135                 trms1040_wait_30us(io_port);
 
4138         /* Disable chip select */
 
4139         outb(0, io_port + TRM_S1040_GEN_NVRAM);
 
4145  * trms1040_read_all - read all bytes from the eeprom
 
4147  * Read the 128 bytes from the SEEPROM.
 
4149  * @eeprom:     where to store the data
 
4150  * @io_port:    the base io port
 
4152 static void __devinit trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
 
4154         u8 *b_eeprom = (u8 *)eeprom;
 
4157         /* Enable SEEPROM */
 
4158         outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
 
4159              io_port + TRM_S1040_GEN_CONTROL);
 
4162         for (addr = 0; addr < 128; addr++, b_eeprom++)
 
4163                 *b_eeprom = trms1040_get_data(io_port, addr);
 
4165         /* Disable SEEPROM */
 
4166         outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
 
4167              io_port + TRM_S1040_GEN_CONTROL);
 
4173  * check_eeprom - get and check contents of the eeprom
 
4175  * Read seeprom 128 bytes into the memory provider in eeprom.
 
4176  * Checks the checksum and if it's not correct it uses a set of default
 
4179  * @eeprom:     caller allocated strcuture to read the eeprom data into
 
4180  * @io_port:    io port to read from
 
4182 static void __devinit check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
 
4184         u16 *w_eeprom = (u16 *)eeprom;
 
4190         trms1040_read_all(eeprom, io_port);     /* read eeprom */
 
4193         for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
 
4194              w_addr++, w_eeprom++)
 
4196         if (cksum != 0x1234) {
 
4198                  * Checksum is wrong.
 
4199                  * Load a set of defaults into the eeprom buffer
 
4201                 dprintkl(KERN_WARNING,
 
4202                         "EEProm checksum error: using default values and options.\n");
 
4203                 eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
 
4204                 eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
 
4205                 eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
 
4206                 eeprom->sub_sys_id[1] =
 
4207                     (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
 
4208                 eeprom->sub_class = 0x00;
 
4209                 eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
 
4210                 eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
 
4211                 eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
 
4212                 eeprom->device_id[1] =
 
4213                     (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
 
4214                 eeprom->reserved = 0x00;
 
4216                 for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
 
4217                      d_addr < 16; d_addr++, d_eeprom++)
 
4218                         *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
 
4220                 *d_eeprom++ = 0x04000F07;       /* max_tag,delay_time,channel_cfg,scsi_id */
 
4221                 *d_eeprom++ = 0x00000015;       /* reserved1,boot_lun,boot_target,reserved0 */
 
4222                 for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
 
4225                 /* Now load defaults (maybe set by boot/module params) */
 
4226                 set_safe_settings();
 
4228                 eeprom_override(eeprom);
 
4230                 eeprom->cksum = 0x00;
 
4231                 for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
 
4232                      w_addr < 63; w_addr++, w_eeprom++)
 
4235                 *w_eeprom = 0x1234 - cksum;
 
4236                 trms1040_write_all(eeprom, io_port);
 
4237                 eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
 
4239                 set_safe_settings();
 
4240                 eeprom_index_to_delay(eeprom);
 
4241                 eeprom_override(eeprom);
 
4247  * print_eeprom_settings - output the eeprom settings
 
4248  * to the kernel log so people can see what they were.
 
4250  * @eeprom: The eeprom data strucutre to show details for.
 
4252 static void __devinit print_eeprom_settings(struct NvRamType *eeprom)
 
4254         dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
 
4256                 eeprom->target[0].period,
 
4257                 clock_speed[eeprom->target[0].period] / 10,
 
4258                 clock_speed[eeprom->target[0].period] % 10,
 
4259                 eeprom->target[0].cfg0);
 
4260         dprintkl(KERN_INFO, "               AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
 
4261                 eeprom->channel_cfg, eeprom->max_tag,
 
4262                 1 << eeprom->max_tag, eeprom->delay_time);
 
4266 /* Free SG tables */
 
4267 static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
 
4270         const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
 
4272         for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
 
4273                 kfree(acb->srb_array[i].segment_x);
 
4278  * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
 
4279  * should never cross a page boundary */
 
4280 static int __devinit adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
 
4282         const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
 
4284         int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
 
4285         const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
 
4288         struct SGentry *ptr;
 
4290         for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
 
4291                 acb->srb_array[i].segment_x = NULL;
 
4293         dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
 
4295                 ptr = (struct SGentry *)kmalloc(PAGE_SIZE, GFP_KERNEL);
 
4297                         adapter_sg_tables_free(acb);
 
4300                 dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
 
4301                         PAGE_SIZE, ptr, srb_idx);
 
4303                 while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
 
4304                         acb->srb_array[srb_idx++].segment_x =
 
4305                             ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
 
4307         if (i < srbs_per_page)
 
4308                 acb->srb.segment_x =
 
4309                     ptr + (i * DC395x_MAX_SG_LISTENTRY);
 
4311                 dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
 
4318  * adapter_print_config - print adapter connection and termination
 
4321  * The io port in the adapter needs to have been set before calling
 
4324  * @acb: The adapter to print the information for.
 
4326 static void __devinit adapter_print_config(struct AdapterCtlBlk *acb)
 
4330         bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
 
4331         dprintkl(KERN_INFO, "%sConnectors: ",
 
4332                 ((bval & WIDESCSI) ? "(Wide) " : ""));
 
4333         if (!(bval & CON5068))
 
4334                 printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
 
4335         if (!(bval & CON68))
 
4336                 printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
 
4337         if (!(bval & CON50))
 
4339         if ((bval & (CON5068 | CON50 | CON68)) ==
 
4340             0 /*(CON5068 | CON50 | CON68) */ )
 
4341                 printk(" Oops! (All 3?) ");
 
4342         bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
 
4343         printk(" Termination: ");
 
4344         if (bval & DIS_TERM)
 
4345                 printk("Disabled\n");
 
4347                 if (bval & AUTOTERM)
 
4349                 if (bval & LOW8TERM)
 
4359  * adapter_init_params - Initialize the various parameters in the
 
4360  * adapter structure. Note that the pointer to the scsi_host is set
 
4361  * early (when this instance is created) and the io_port and irq
 
4362  * values are set later after they have been reserved. This just gets
 
4363  * everything set to a good starting position.
 
4365  * The eeprom structure in the adapter needs to have been set before
 
4366  * calling this function.
 
4368  * @acb: The adapter to initialize.
 
4370 static void __devinit adapter_init_params(struct AdapterCtlBlk *acb)
 
4372         struct NvRamType *eeprom = &acb->eeprom;
 
4375         /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
 
4376         /* NOTE: acb->io_port_base is set at port registration time */
 
4377         /* NOTE: acb->io_port_len is set at port registration time */
 
4379         INIT_LIST_HEAD(&acb->dcb_list);
 
4380         acb->dcb_run_robin = NULL;
 
4381         acb->active_dcb = NULL;
 
4383         INIT_LIST_HEAD(&acb->srb_free_list);
 
4384         /*  temp SRB for Q tag used or abort command used  */
 
4385         acb->tmp_srb = &acb->srb;
 
4386         init_timer(&acb->waiting_timer);
 
4387         init_timer(&acb->selto_timer);
 
4389         acb->srb_count = DC395x_MAX_SRB_CNT;
 
4391         acb->sel_timeout = DC395x_SEL_TIMEOUT;  /* timeout=250ms */
 
4392         /* NOTE: acb->irq_level is set at IRQ registration time */
 
4394         acb->tag_max_num = 1 << eeprom->max_tag;
 
4395         if (acb->tag_max_num > 30)
 
4396                 acb->tag_max_num = 30;
 
4398         acb->acb_flag = 0;      /* RESET_DETECT, RESET_DONE, RESET_DEV */
 
4399         acb->gmode2 = eeprom->channel_cfg;
 
4400         acb->config = 0;        /* NOTE: actually set in adapter_init_chip */
 
4402         if (eeprom->channel_cfg & NAC_SCANLUN)
 
4404         acb->scan_devices = 1;
 
4406         acb->scsi_host->this_id = eeprom->scsi_id;
 
4407         acb->hostid_bit = (1 << acb->scsi_host->this_id);
 
4409         for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
 
4410                 acb->dcb_map[i] = 0;
 
4414         /* link static array of srbs into the srb free list */
 
4415         for (i = 0; i < acb->srb_count - 1; i++)
 
4416                 srb_free_insert(acb, &acb->srb_array[i]);
 
4421  * adapter_init_host - Initialize the scsi host instance based on
 
4422  * values that we have already stored in the adapter instance. There's
 
4423  * some mention that a lot of these are deprecated, so we won't use
 
4424  * them (we'll use the ones in the adapter instance) but we'll fill
 
4425  * them in in case something else needs them.
 
4427  * The eeprom structure, irq and io ports in the adapter need to have
 
4428  * been set before calling this function.
 
4430  * @host: The scsi host instance to fill in the values for.
 
4432 static void __devinit adapter_init_scsi_host(struct Scsi_Host *host)
 
4434         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
 
4435         struct NvRamType *eeprom = &acb->eeprom;
 
4437         host->max_cmd_len = 24;
 
4438         host->can_queue = DC395x_MAX_CMD_QUEUE;
 
4439         host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
 
4440         host->this_id = (int)eeprom->scsi_id;
 
4441         host->io_port = acb->io_port_base;
 
4442         host->n_io_port = acb->io_port_len;
 
4443         host->dma_channel = -1;
 
4444         host->unique_id = acb->io_port_base;
 
4445         host->irq = acb->irq_level;
 
4446         host->last_reset = jiffies;
 
4449         if (host->max_id - 1 == eeprom->scsi_id)
 
4452 #ifdef CONFIG_SCSI_MULTI_LUN
 
4453         if (eeprom->channel_cfg & NAC_SCANLUN)
 
4465  * adapter_init_chip - Get the chip into a know state and figure out
 
4466  * some of the settings that apply to this adapter.
 
4468  * The io port in the adapter needs to have been set before calling
 
4469  * this function. The config will be configured correctly on return.
 
4471  * @acb: The adapter which we are to init.
 
4473 static void __devinit adapter_init_chip(struct AdapterCtlBlk *acb)
 
4475         struct NvRamType *eeprom = &acb->eeprom;
 
4477         /* Mask all the interrupt */
 
4478         DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
 
4479         DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
 
4481         /* Reset SCSI module */
 
4482         DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
 
4484         /* Reset PCI/DMA module */
 
4485         DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
 
4488         /* program configuration 0 */
 
4489         acb->config = HCC_AUTOTERM | HCC_PARITY;
 
4490         if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
 
4491                 acb->config |= HCC_WIDE_CARD;
 
4493         if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
 
4494                 acb->config |= HCC_SCSI_RESET;
 
4496         if (acb->config & HCC_SCSI_RESET) {
 
4497                 dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
 
4498                 DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
 
4500                 /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
 
4501                 /*spin_unlock_irq (&io_request_lock); */
 
4504                 acb->scsi_host->last_reset =
 
4506                     HZ * acb->eeprom.delay_time;
 
4508                 /*spin_lock_irq (&io_request_lock); */
 
4514  * init_adapter - Grab the resource for the card, setup the adapter
 
4515  * information, set the card into a known state, create the various
 
4516  * tables etc etc. This basically gets all adapter information all up
 
4517  * to date, intialised and gets the chip in sync with it.
 
4519  * @host:       This hosts adapter structure
 
4520  * @io_port:    The base I/O port
 
4523  * Returns 0 if the initialization succeeds, any other value on
 
4526 static int __devinit adapter_init(struct AdapterCtlBlk *acb,
 
4527         unsigned long io_port, u32 io_port_len, unsigned int irq)
 
4529         if (!request_region(io_port, io_port_len, DC395X_NAME)) {
 
4530                 dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
 
4533         /* store port base to indicate we have registered it */
 
4534         acb->io_port_base = io_port;
 
4535         acb->io_port_len = io_port_len;
 
4537         if (request_irq(irq, dc395x_interrupt, SA_SHIRQ, DC395X_NAME, acb)) {
 
4538                 /* release the region we just claimed */
 
4539                 dprintkl(KERN_INFO, "Failed to register IRQ\n");
 
4542         /* store irq to indicate we have registered it */
 
4543         acb->irq_level = irq;
 
4545         /* get eeprom configuration information and command line settings etc */
 
4546         check_eeprom(&acb->eeprom, io_port);
 
4547         print_eeprom_settings(&acb->eeprom);
 
4549         /* setup adapter control block */       
 
4550         adapter_init_params(acb);
 
4552         /* display card connectors/termination settings */
 
4553         adapter_print_config(acb);
 
4555         if (adapter_sg_tables_alloc(acb)) {
 
4556                 dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
 
4559         adapter_init_scsi_host(acb->scsi_host);
 
4560         adapter_init_chip(acb);
 
4561         set_basic_config(acb);
 
4564                 "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
 
4565                 "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
 
4566                 acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
 
4567                 sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
 
4572                 free_irq(acb->irq_level, acb);
 
4573         if (acb->io_port_base)
 
4574                 release_region(acb->io_port_base, acb->io_port_len);
 
4575         adapter_sg_tables_free(acb);
 
4582  * adapter_uninit_chip - cleanly shut down the scsi controller chip,
 
4583  * stopping all operations and disabling interrupt generation on the
 
4586  * @acb: The adapter which we are to shutdown.
 
4588 static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
 
4590         /* disable interrupts */
 
4591         DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
 
4592         DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
 
4594         /* reset the scsi bus */
 
4595         if (acb->config & HCC_SCSI_RESET)
 
4596                 reset_scsi_bus(acb);
 
4598         /* clear any pending interupt state */
 
4599         DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
 
4605  * adapter_uninit - Shut down the chip and release any resources that
 
4606  * we had allocated. Once this returns the adapter should not be used
 
4609  * @acb: The adapter which we are to un-initialize.
 
4611 static void adapter_uninit(struct AdapterCtlBlk *acb)
 
4613         unsigned long flags;
 
4614         DC395x_LOCK_IO(acb->scsi_host, flags);
 
4617         if (timer_pending(&acb->waiting_timer))
 
4618                 del_timer(&acb->waiting_timer);
 
4619         if (timer_pending(&acb->selto_timer))
 
4620                 del_timer(&acb->selto_timer);
 
4622         adapter_uninit_chip(acb);
 
4623         adapter_remove_and_free_all_devices(acb);
 
4624         DC395x_UNLOCK_IO(acb->scsi_host, flags);
 
4627                 free_irq(acb->irq_level, acb);
 
4628         if (acb->io_port_base)
 
4629                 release_region(acb->io_port_base, acb->io_port_len);
 
4631         adapter_sg_tables_free(acb);
 
4636 #define SPRINTF(args...) pos += sprintf(pos, args)
 
4640  if (YN) SPRINTF(" Yes ");\
 
4641  else SPRINTF(" No  ")
 
4643 static int dc395x_proc_info(struct Scsi_Host *host, char *buffer,
 
4644                 char **start, off_t offset, int length, int inout)
 
4646         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
 
4649         struct DeviceCtlBlk *dcb;
 
4650         unsigned long flags;
 
4653         if (inout)              /* Has data been written to the file ? */
 
4656         SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
 
4657         SPRINTF(" Driver Version " DC395X_VERSION "\n");
 
4659         DC395x_LOCK_IO(acb->scsi_host, flags);
 
4661         SPRINTF("SCSI Host Nr %i, ", host->host_no);
 
4662         SPRINTF("DC395U/UW/F DC315/U %s\n",
 
4663                 (acb->config & HCC_WIDE_CARD) ? "Wide" : "");
 
4664         SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base);
 
4665         SPRINTF("irq_level 0x%04x, ", acb->irq_level);
 
4666         SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
 
4668         SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun);
 
4669         SPRINTF("AdapterID %i\n", host->this_id);
 
4671         SPRINTF("tag_max_num %i", acb->tag_max_num);
 
4672         /*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
 
4673         SPRINTF(", FilterCfg 0x%02x",
 
4674                 DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
 
4675         SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time);
 
4678         SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list));
 
4680             ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
 
4681              acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
 
4682              acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
 
4683              acb->dcb_map[6], acb->dcb_map[7]);
 
4685             ("                      %02x %02x %02x %02x %02x %02x %02x %02x\n",
 
4686              acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
 
4687              acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
 
4688              acb->dcb_map[14], acb->dcb_map[15]);
 
4691             ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
 
4694         list_for_each_entry(dcb, &acb->dcb_list, list) {
 
4696                 SPRINTF("%02i %02i  %02i ", dev, dcb->target_id,
 
4698                 YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
 
4699                 YESNO(dcb->sync_offset);
 
4700                 YESNO(dcb->sync_period & WIDE_SYNC);
 
4701                 YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
 
4702                 YESNO(dcb->dev_mode & NTC_DO_SEND_START);
 
4703                 YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
 
4704                 nego_period = clock_period[dcb->sync_period & 0x07] << 2;
 
4705                 if (dcb->sync_offset)
 
4706                         SPRINTF("  %03i ns ", nego_period);
 
4708                         SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
 
4710                 if (dcb->sync_offset & 0x0f) {
 
4711                         spd = 1000 / (nego_period);
 
4712                         spd1 = 1000 % (nego_period);
 
4713                         spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
 
4714                         SPRINTF("   %2i.%1i M     %02i ", spd, spd1,
 
4715                                 (dcb->sync_offset & 0x0f));
 
4719                 /* Add more info ... */
 
4720                 SPRINTF("     %02i\n", dcb->max_command);
 
4724         if (timer_pending(&acb->waiting_timer))
 
4725                 SPRINTF("Waiting queue timer running\n");
 
4729         list_for_each_entry(dcb, &acb->dcb_list, list) {
 
4730                 struct ScsiReqBlk *srb;
 
4731                 if (!list_empty(&dcb->srb_waiting_list))
 
4732                         SPRINTF("DCB (%02i-%i): Waiting: %i:",
 
4733                                 dcb->target_id, dcb->target_lun,
 
4734                                 list_size(&dcb->srb_waiting_list));
 
4735                 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
 
4736                         SPRINTF(" %li", srb->cmd->pid);
 
4737                 if (!list_empty(&dcb->srb_going_list))
 
4738                         SPRINTF("\nDCB (%02i-%i): Going  : %i:",
 
4739                                 dcb->target_id, dcb->target_lun,
 
4740                                 list_size(&dcb->srb_going_list));
 
4741                 list_for_each_entry(srb, &dcb->srb_going_list, list)
 
4742                         SPRINTF(" %li", srb->cmd->pid);
 
4743                 if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
 
4747         if (debug_enabled(DBG_1)) {
 
4748                 SPRINTF("DCB list for ACB %p:\n", acb);
 
4749                 list_for_each_entry(dcb, &acb->dcb_list, list) {
 
4750                         SPRINTF("%p -> ", dcb);
 
4755         *start = buffer + offset;
 
4756         DC395x_UNLOCK_IO(acb->scsi_host, flags);
 
4758         if (pos - buffer < offset)
 
4760         else if (pos - buffer - offset < length)
 
4761                 return pos - buffer - offset;
 
4767 static struct scsi_host_template dc395x_driver_template = {
 
4768         .module                 = THIS_MODULE,
 
4769         .proc_name              = DC395X_NAME,
 
4770         .proc_info              = dc395x_proc_info,
 
4771         .name                   = DC395X_BANNER " " DC395X_VERSION,
 
4772         .queuecommand           = dc395x_queue_command,
 
4773         .bios_param             = dc395x_bios_param,
 
4774         .slave_alloc            = dc395x_slave_alloc,
 
4775         .slave_destroy          = dc395x_slave_destroy,
 
4776         .can_queue              = DC395x_MAX_CAN_QUEUE,
 
4778         .sg_tablesize           = DC395x_MAX_SG_TABLESIZE,
 
4779         .cmd_per_lun            = DC395x_MAX_CMD_PER_LUN,
 
4780         .eh_abort_handler       = dc395x_eh_abort,
 
4781         .eh_bus_reset_handler   = dc395x_eh_bus_reset,
 
4782         .unchecked_isa_dma      = 0,
 
4783         .use_clustering         = DISABLE_CLUSTERING,
 
4788  * banner_display - Display banner on first instance of driver
 
4791 static void banner_display(void)
 
4793         static int banner_done = 0;
 
4796                 dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
 
4803  * dc395x_init_one - Initialise a single instance of the adapter.
 
4805  * The PCI layer will call this once for each instance of the adapter
 
4806  * that it finds in the system. The pci_dev strcuture indicates which
 
4807  * instance we are being called from.
 
4809  * @dev: The PCI device to intialize.
 
4810  * @id: Looks like a pointer to the entry in our pci device table
 
4811  * that was actually matched by the PCI subsystem.
 
4813  * Returns 0 on success, or an error code (-ve) on failure.
 
4815 static int __devinit dc395x_init_one(struct pci_dev *dev,
 
4816                 const struct pci_device_id *id)
 
4818         struct Scsi_Host *scsi_host = NULL;
 
4819         struct AdapterCtlBlk *acb = NULL;
 
4820         unsigned long io_port_base;
 
4821         unsigned int io_port_len;
 
4824         dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
 
4827         if (pci_enable_device(dev))
 
4829                 dprintkl(KERN_INFO, "PCI Enable device failed.\n");
 
4832         io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
 
4833         io_port_len = pci_resource_len(dev, 0);
 
4835         dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
 
4837         /* allocate scsi host information (includes out adapter) */
 
4838         scsi_host = scsi_host_alloc(&dc395x_driver_template,
 
4839                                     sizeof(struct AdapterCtlBlk));
 
4841                 dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
 
4844         acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
 
4845         acb->scsi_host = scsi_host;
 
4848         /* initialise the adapter and everything we need */
 
4849         if (adapter_init(acb, io_port_base, io_port_len, irq)) {
 
4850                 dprintkl(KERN_INFO, "adapter init failed\n");
 
4854         pci_set_master(dev);
 
4856         /* get the scsi mid level to scan for new devices on the bus */
 
4857         if (scsi_add_host(scsi_host, &dev->dev)) {
 
4858                 dprintkl(KERN_ERR, "scsi_add_host failed\n");
 
4861         pci_set_drvdata(dev, scsi_host);
 
4862         scsi_scan_host(scsi_host);
 
4868                 adapter_uninit(acb);
 
4869         if (scsi_host != NULL)
 
4870                 scsi_host_put(scsi_host);
 
4871         pci_disable_device(dev);
 
4877  * dc395x_remove_one - Called to remove a single instance of the
 
4880  * @dev: The PCI device to intialize.
 
4882 static void __devexit dc395x_remove_one(struct pci_dev *dev)
 
4884         struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
 
4885         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
 
4887         dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
 
4889         scsi_remove_host(scsi_host);
 
4890         adapter_uninit(acb);
 
4891         pci_disable_device(dev);
 
4892         scsi_host_put(scsi_host);
 
4893         pci_set_drvdata(dev, NULL);
 
4897 static struct pci_device_id dc395x_pci_table[] = {
 
4899                 .vendor         = PCI_VENDOR_ID_TEKRAM,
 
4900                 .device         = PCI_DEVICE_ID_TEKRAM_TRMS1040,
 
4901                 .subvendor      = PCI_ANY_ID,
 
4902                 .subdevice      = PCI_ANY_ID,
 
4904         {}                      /* Terminating entry */
 
4906 MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
 
4909 static struct pci_driver dc395x_driver = {
 
4910         .name           = DC395X_NAME,
 
4911         .id_table       = dc395x_pci_table,
 
4912         .probe          = dc395x_init_one,
 
4913         .remove         = __devexit_p(dc395x_remove_one),
 
4918  * dc395x_module_init - Module initialization function
 
4920  * Used by both module and built-in driver to initialise this driver.
 
4922 static int __init dc395x_module_init(void)
 
4924         return pci_module_init(&dc395x_driver);
 
4929  * dc395x_module_exit - Module cleanup function.
 
4931 static void __exit dc395x_module_exit(void)
 
4933         pci_unregister_driver(&dc395x_driver);
 
4937 module_init(dc395x_module_init);
 
4938 module_exit(dc395x_module_exit);
 
4940 MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
 
4941 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
 
4942 MODULE_LICENSE("GPL");