PageRenderTime 136ms CodeModel.GetById 22ms app.highlight 97ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/scsi/dc395x.c

http://github.com/mirrors/linux
C | 4757 lines | 3213 code | 594 blank | 950 comment | 492 complexity | c369baa06d6fcb78d1cc634b9caa8afb MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * dc395x.c
   3 *
   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)
   7 *
   8 * Authors:
   9 *  C.L. Huang <ching@tekram.com.tw>
  10 *  Erich Chen <erich@tekram.com.tw>
  11 *  (C) Copyright 1995-1999 Tekram Technology Co., Ltd.
  12 *
  13 *  Kurt Garloff <garloff@suse.de>
  14 *  (C) 1999-2000 Kurt Garloff
  15 *
  16 *  Oliver Neukum <oliver@neukum.name>
  17 *  Ali Akcaagac <aliakc@web.de>
  18 *  Jamie Lenehan <lenehan@twibble.org>
  19 *  (C) 2003
  20 *
  21 * License: GNU GPL
  22 *
  23 *************************************************************************
  24 *
  25 * Redistribution and use in source and binary forms, with or without
  26 * modification, are permitted provided that the following conditions
  27 * are met:
  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.
  35 *
  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.
  46 *
  47 ************************************************************************
  48 */
  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>
  60#include <linux/slab.h>
  61#include <asm/io.h>
  62
  63#include <scsi/scsi.h>
  64#include <scsi/scsi_cmnd.h>
  65#include <scsi/scsi_device.h>
  66#include <scsi/scsi_host.h>
  67
  68#include "dc395x.h"
  69
  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"
  73
  74/*---------------------------------------------------------------------------
  75                                  Features
  76 ---------------------------------------------------------------------------*/
  77/*
  78 * Set to disable parts of the driver
  79 */
  80/*#define DC395x_NO_DISCONNECT*/
  81/*#define DC395x_NO_TAGQ*/
  82/*#define DC395x_NO_SYNC*/
  83/*#define DC395x_NO_WIDE*/
  84
  85/*---------------------------------------------------------------------------
  86                                  Debugging
  87 ---------------------------------------------------------------------------*/
  88/*
  89 * Types of debugging that can be enabled and disabled
  90 */
  91#define DBG_KG		0x0001
  92#define DBG_0		0x0002
  93#define DBG_1		0x0004
  94#define DBG_SG		0x0020
  95#define DBG_FIFO	0x0040
  96#define DBG_PIO		0x0080
  97
  98
  99/*
 100 * Set set of things to output debugging for.
 101 * Undefine to remove all debugging
 102 */
 103/*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
 104/*#define  DEBUG_MASK	DBG_0*/
 105
 106
 107/*
 108 * Output a kernel mesage at the specified level and append the
 109 * driver name and a ": " to the start of the message
 110 */
 111#define dprintkl(level, format, arg...)  \
 112    printk(level DC395X_NAME ": " format , ## arg)
 113
 114
 115#ifdef DEBUG_MASK
 116/*
 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
 121 */
 122#define dprintkdbg(type, format, arg...) \
 123	do { \
 124		if ((type) & (DEBUG_MASK)) \
 125			dprintkl(KERN_DEBUG , format , ## arg); \
 126	} while (0)
 127
 128/*
 129 * Check if the specified type of debugging is enabled
 130 */
 131#define debug_enabled(type)	((DEBUG_MASK) & (type))
 132
 133#else
 134/*
 135 * No debugging. Do nothing
 136 */
 137#define dprintkdbg(type, format, arg...) \
 138	do {} while (0)
 139#define debug_enabled(type)	(0)
 140
 141#endif
 142
 143
 144#ifndef PCI_VENDOR_ID_TEKRAM
 145#define PCI_VENDOR_ID_TEKRAM                    0x1DE1	/* Vendor ID    */
 146#endif
 147#ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
 148#define PCI_DEVICE_ID_TEKRAM_TRMS1040           0x0391	/* Device ID    */
 149#endif
 150
 151
 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)
 154
 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))
 161
 162/* cmd->result */
 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 */
 168
 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)
 171
 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; }
 177
 178#define TAG_NONE 255
 179
 180/*
 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.
 184 */
 185#define SEGMENTX_LEN	(sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
 186
 187
 188struct SGentry {
 189	u32 address;		/* bus! address */
 190	u32 length;
 191};
 192
 193/* The SEEPROM structure for TRM_S1040 */
 194struct NVRamTarget {
 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  */
 199};
 200
 201struct NvRamType {
 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];
 209						/** 10,11,12,13
 210						 ** 14,15,16,17
 211						 ** ....
 212						 ** ....
 213						 ** 70,71,72,73
 214						 */
 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 */
 225};
 226
 227struct ScsiReqBlk {
 228	struct list_head list;		/* next/prev ptrs for srb lists */
 229	struct DeviceCtlBlk *dcb;
 230	struct scsi_cmnd *cmd;
 231
 232	struct SGentry *segment_x;	/* Linear array of hw sg entries (up to 64 entries) */
 233	dma_addr_t sg_bus_addr;	        /* Bus address of sg list (ie, of segment_x) */
 234
 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	size_t total_xfer_length;	/* Total number of bytes remaining to be transferred */
 238	size_t request_length;		/* Total number of bytes in this request */
 239	/*
 240	 * The sense buffer handling function, request_sense, uses
 241	 * the first hw sg entry (segment_x[0]) and the transfer
 242	 * length (total_xfer_length). While doing this it stores the
 243	 * original values into the last sg hw list
 244	 * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
 245	 * total_xfer_length in xferred. These values are restored in
 246	 * pci_unmap_srb_sense. This is the only place xferred is used.
 247	 */
 248	size_t xferred;		        /* Saved copy of total_xfer_length */
 249
 250	u16 state;
 251
 252	u8 msgin_buf[6];
 253	u8 msgout_buf[6];
 254
 255	u8 adapter_status;
 256	u8 target_status;
 257	u8 msg_count;
 258	u8 end_message;
 259
 260	u8 tag_number;
 261	u8 status;
 262	u8 retry_count;
 263	u8 flag;
 264
 265	u8 scsi_phase;
 266};
 267
 268struct DeviceCtlBlk {
 269	struct list_head list;		/* next/prev ptrs for the dcb list */
 270	struct AdapterCtlBlk *acb;
 271	struct list_head srb_going_list;	/* head of going srb list */
 272	struct list_head srb_waiting_list;	/* head of waiting srb list */
 273
 274	struct ScsiReqBlk *active_srb;
 275	u32 tag_mask;
 276
 277	u16 max_command;
 278
 279	u8 target_id;		/* SCSI Target ID  (SCSI Only) */
 280	u8 target_lun;		/* SCSI Log.  Unit (SCSI Only) */
 281	u8 identify_msg;
 282	u8 dev_mode;
 283
 284	u8 inquiry7;		/* To store Inquiry flags */
 285	u8 sync_mode;		/* 0:async mode */
 286	u8 min_nego_period;	/* for nego. */
 287	u8 sync_period;		/* for reg.  */
 288
 289	u8 sync_offset;		/* for reg. and nego.(low nibble) */
 290	u8 flag;
 291	u8 dev_type;
 292	u8 init_tcq_flag;
 293};
 294
 295struct AdapterCtlBlk {
 296	struct Scsi_Host *scsi_host;
 297
 298	unsigned long io_port_base;
 299	unsigned long io_port_len;
 300
 301	struct list_head dcb_list;		/* head of going dcb list */
 302	struct DeviceCtlBlk *dcb_run_robin;
 303	struct DeviceCtlBlk *active_dcb;
 304
 305	struct list_head srb_free_list;		/* head of free srb list */
 306	struct ScsiReqBlk *tmp_srb;
 307	struct timer_list waiting_timer;
 308	struct timer_list selto_timer;
 309
 310	unsigned long last_reset;
 311
 312	u16 srb_count;
 313
 314	u8 sel_timeout;
 315
 316	unsigned int irq_level;
 317	u8 tag_max_num;
 318	u8 acb_flag;
 319	u8 gmode2;
 320
 321	u8 config;
 322	u8 lun_chk;
 323	u8 scan_devices;
 324	u8 hostid_bit;
 325
 326	u8 dcb_map[DC395x_MAX_SCSI_ID];
 327	struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
 328
 329	struct pci_dev *dev;
 330
 331	u8 msg_len;
 332
 333	struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
 334	struct ScsiReqBlk srb;
 335
 336	struct NvRamType eeprom;	/* eeprom settings for this adapter */
 337};
 338
 339
 340/*---------------------------------------------------------------------------
 341                            Forward declarations
 342 ---------------------------------------------------------------------------*/
 343static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 344		u16 *pscsi_status);
 345static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 346		u16 *pscsi_status);
 347static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 348		u16 *pscsi_status);
 349static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 350		u16 *pscsi_status);
 351static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 352		u16 *pscsi_status);
 353static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 354		u16 *pscsi_status);
 355static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 356		u16 *pscsi_status);
 357static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 358		u16 *pscsi_status);
 359static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 360		u16 *pscsi_status);
 361static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 362		u16 *pscsi_status);
 363static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 364		u16 *pscsi_status);
 365static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 366		u16 *pscsi_status);
 367static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
 368		u16 *pscsi_status);
 369static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb, 
 370		u16 *pscsi_status);
 371static void set_basic_config(struct AdapterCtlBlk *acb);
 372static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
 373		struct ScsiReqBlk *srb);
 374static void reset_scsi_bus(struct AdapterCtlBlk *acb);
 375static void data_io_transfer(struct AdapterCtlBlk *acb,
 376		struct ScsiReqBlk *srb, u16 io_dir);
 377static void disconnect(struct AdapterCtlBlk *acb);
 378static void reselect(struct AdapterCtlBlk *acb);
 379static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 380		struct ScsiReqBlk *srb);
 381static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
 382		struct ScsiReqBlk *srb);
 383static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
 384		struct ScsiReqBlk *srb);
 385static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
 386		struct scsi_cmnd *cmd, u8 force);
 387static void scsi_reset_detect(struct AdapterCtlBlk *acb);
 388static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
 389static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
 390		struct ScsiReqBlk *srb);
 391static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 392		struct ScsiReqBlk *srb);
 393static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
 394		struct ScsiReqBlk *srb);
 395static void set_xfer_rate(struct AdapterCtlBlk *acb,
 396		struct DeviceCtlBlk *dcb);
 397static void waiting_timeout(struct timer_list *t);
 398
 399
 400/*---------------------------------------------------------------------------
 401                                 Static Data
 402 ---------------------------------------------------------------------------*/
 403static u16 current_sync_offset = 0;
 404
 405static void *dc395x_scsi_phase0[] = {
 406	data_out_phase0,/* phase:0 */
 407	data_in_phase0,	/* phase:1 */
 408	command_phase0,	/* phase:2 */
 409	status_phase0,	/* phase:3 */
 410	nop0,		/* phase:4 PH_BUS_FREE .. initial phase */
 411	nop0,		/* phase:5 PH_BUS_FREE .. initial phase */
 412	msgout_phase0,	/* phase:6 */
 413	msgin_phase0,	/* phase:7 */
 414};
 415
 416static void *dc395x_scsi_phase1[] = {
 417	data_out_phase1,/* phase:0 */
 418	data_in_phase1,	/* phase:1 */
 419	command_phase1,	/* phase:2 */
 420	status_phase1,	/* phase:3 */
 421	nop1,		/* phase:4 PH_BUS_FREE .. initial phase */
 422	nop1,		/* phase:5 PH_BUS_FREE .. initial phase */
 423	msgout_phase1,	/* phase:6 */
 424	msgin_phase1,	/* phase:7 */
 425};
 426
 427/*
 428 *Fast20:	000	 50ns, 20.0 MHz
 429 *		001	 75ns, 13.3 MHz
 430 *		010	100ns, 10.0 MHz
 431 *		011	125ns,  8.0 MHz
 432 *		100	150ns,  6.6 MHz
 433 *		101	175ns,  5.7 MHz
 434 *		110	200ns,  5.0 MHz
 435 *		111	250ns,  4.0 MHz
 436 *
 437 *Fast40(LVDS):	000	 25ns, 40.0 MHz
 438 *		001	 50ns, 20.0 MHz
 439 *		010	 75ns, 13.3 MHz
 440 *		011	100ns, 10.0 MHz
 441 *		100	125ns,  8.0 MHz
 442 *		101	150ns,  6.6 MHz
 443 *		110	175ns,  5.7 MHz
 444 *		111	200ns,  5.0 MHz
 445 */
 446/*static u8	clock_period[] = {12,19,25,31,37,44,50,62};*/
 447
 448/* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
 449static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
 450static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
 451
 452
 453/*---------------------------------------------------------------------------
 454                                Configuration
 455  ---------------------------------------------------------------------------*/
 456/*
 457 * Module/boot parameters currently effect *all* instances of the
 458 * card in the system.
 459 */
 460
 461/*
 462 * Command line parameters are stored in a structure below.
 463 * These are the index's into the structure for the various
 464 * command line options.
 465 */
 466#define CFG_ADAPTER_ID		0
 467#define CFG_MAX_SPEED		1
 468#define CFG_DEV_MODE		2
 469#define CFG_ADAPTER_MODE	3
 470#define CFG_TAGS		4
 471#define CFG_RESET_DELAY		5
 472
 473#define CFG_NUM			6	/* number of configuration items */
 474
 475
 476/*
 477 * Value used to indicate that a command line override
 478 * hasn't been used to modify the value.
 479 */
 480#define CFG_PARAM_UNSET -1
 481
 482
 483/*
 484 * Hold command line parameters.
 485 */
 486struct ParameterData {
 487	int value;		/* value of this setting */
 488	int min;		/* minimum value */
 489	int max;		/* maximum value */
 490	int def;		/* default value */
 491	int safe;		/* safe value */
 492};
 493static struct ParameterData cfg_data[] = {
 494	{ /* adapter id */
 495		CFG_PARAM_UNSET,
 496		0,
 497		15,
 498		7,
 499		7
 500	},
 501	{ /* max speed */
 502		CFG_PARAM_UNSET,
 503		  0,
 504		  7,
 505		  1,	/* 13.3Mhz */
 506		  4,	/*  6.7Hmz */
 507	},
 508	{ /* dev mode */
 509		CFG_PARAM_UNSET,
 510		0,
 511		0x3f,
 512		NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
 513			NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
 514			NTC_DO_SEND_START,
 515		NTC_DO_PARITY_CHK | NTC_DO_SEND_START
 516	},
 517	{ /* adapter mode */
 518		CFG_PARAM_UNSET,
 519		0,
 520		0x2f,
 521		NAC_SCANLUN |
 522		NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
 523			/*| NAC_ACTIVE_NEG*/,
 524		NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
 525	},
 526	{ /* tags */
 527		CFG_PARAM_UNSET,
 528		0,
 529		5,
 530		3,	/* 16 tags (??) */
 531		2,
 532	},
 533	{ /* reset delay */
 534		CFG_PARAM_UNSET,
 535		0,
 536		180,
 537		1,	/* 1 second */
 538		10,	/* 10 seconds */
 539	}
 540};
 541
 542
 543/*
 544 * Safe settings. If set to zero the BIOS/default values with
 545 * command line overrides will be used. If set to 1 then safe and
 546 * slow settings will be used.
 547 */
 548static bool use_safe_settings = 0;
 549module_param_named(safe, use_safe_settings, bool, 0);
 550MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
 551
 552
 553module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
 554MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
 555
 556module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
 557MODULE_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");
 558
 559module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
 560MODULE_PARM_DESC(dev_mode, "Device mode.");
 561
 562module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
 563MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
 564
 565module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
 566MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
 567
 568module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
 569MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
 570
 571
 572/**
 573 * set_safe_settings - if the use_safe_settings option is set then
 574 * set all values to the safe and slow values.
 575 **/
 576static void set_safe_settings(void)
 577{
 578	if (use_safe_settings)
 579	{
 580		int i;
 581
 582		dprintkl(KERN_INFO, "Using safe settings.\n");
 583		for (i = 0; i < CFG_NUM; i++)
 584		{
 585			cfg_data[i].value = cfg_data[i].safe;
 586		}
 587	}
 588}
 589
 590
 591/**
 592 * fix_settings - reset any boot parameters which are out of range
 593 * back to the default values.
 594 **/
 595static void fix_settings(void)
 596{
 597	int i;
 598
 599	dprintkdbg(DBG_1,
 600		"setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
 601		"AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
 602		cfg_data[CFG_ADAPTER_ID].value,
 603		cfg_data[CFG_MAX_SPEED].value,
 604		cfg_data[CFG_DEV_MODE].value,
 605		cfg_data[CFG_ADAPTER_MODE].value,
 606		cfg_data[CFG_TAGS].value,
 607		cfg_data[CFG_RESET_DELAY].value);
 608	for (i = 0; i < CFG_NUM; i++)
 609	{
 610		if (cfg_data[i].value < cfg_data[i].min
 611		    || cfg_data[i].value > cfg_data[i].max)
 612			cfg_data[i].value = cfg_data[i].def;
 613	}
 614}
 615
 616
 617
 618/*
 619 * Mapping from the eeprom delay index value (index into this array)
 620 * to the number of actual seconds that the delay should be for.
 621 */
 622static char eeprom_index_to_delay_map[] =
 623	{ 1, 3, 5, 10, 16, 30, 60, 120 };
 624
 625
 626/**
 627 * eeprom_index_to_delay - Take the eeprom delay setting and convert it
 628 * into a number of seconds.
 629 *
 630 * @eeprom: The eeprom structure in which we find the delay index to map.
 631 **/
 632static void eeprom_index_to_delay(struct NvRamType *eeprom)
 633{
 634	eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
 635}
 636
 637
 638/**
 639 * delay_to_eeprom_index - Take a delay in seconds and return the
 640 * closest eeprom index which will delay for at least that amount of
 641 * seconds.
 642 *
 643 * @delay: The delay, in seconds, to find the eeprom index for.
 644 **/
 645static int delay_to_eeprom_index(int delay)
 646{
 647	u8 idx = 0;
 648	while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
 649		idx++;
 650	return idx;
 651}
 652
 653
 654/**
 655 * eeprom_override - Override the eeprom settings, in the provided
 656 * eeprom structure, with values that have been set on the command
 657 * line.
 658 *
 659 * @eeprom: The eeprom data to override with command line options.
 660 **/
 661static void eeprom_override(struct NvRamType *eeprom)
 662{
 663	u8 id;
 664
 665	/* Adapter Settings */
 666	if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
 667		eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
 668
 669	if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
 670		eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
 671
 672	if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
 673		eeprom->delay_time = delay_to_eeprom_index(
 674					cfg_data[CFG_RESET_DELAY].value);
 675
 676	if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
 677		eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
 678
 679	/* Device Settings */
 680	for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
 681		if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
 682			eeprom->target[id].cfg0 =
 683				(u8)cfg_data[CFG_DEV_MODE].value;
 684
 685		if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
 686			eeprom->target[id].period =
 687				(u8)cfg_data[CFG_MAX_SPEED].value;
 688
 689	}
 690}
 691
 692
 693/*---------------------------------------------------------------------------
 694 ---------------------------------------------------------------------------*/
 695
 696static unsigned int list_size(struct list_head *head)
 697{
 698	unsigned int count = 0;
 699	struct list_head *pos;
 700	list_for_each(pos, head)
 701		count++;
 702	return count;
 703}
 704
 705
 706static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
 707		struct DeviceCtlBlk *pos)
 708{
 709	int use_next = 0;
 710	struct DeviceCtlBlk* next = NULL;
 711	struct DeviceCtlBlk* i;
 712
 713	if (list_empty(head))
 714		return NULL;
 715
 716	/* find supplied dcb and then select the next one */
 717	list_for_each_entry(i, head, list)
 718		if (use_next) {
 719			next = i;
 720			break;
 721		} else if (i == pos) {
 722			use_next = 1;
 723		}
 724	/* if no next one take the head one (ie, wraparound) */
 725	if (!next)
 726        	list_for_each_entry(i, head, list) {
 727        		next = i;
 728        		break;
 729        	}
 730
 731	return next;
 732}
 733
 734
 735static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
 736{
 737	if (srb->tag_number < 255) {
 738		dcb->tag_mask &= ~(1 << srb->tag_number);	/* free tag mask */
 739		srb->tag_number = 255;
 740	}
 741}
 742
 743
 744/* Find cmd in SRB list */
 745static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
 746		struct list_head *head)
 747{
 748	struct ScsiReqBlk *i;
 749	list_for_each_entry(i, head, list)
 750		if (i->cmd == cmd)
 751			return i;
 752	return NULL;
 753}
 754
 755/* Sets the timer to wake us up */
 756static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
 757{
 758	if (timer_pending(&acb->waiting_timer))
 759		return;
 760	if (time_before(jiffies + to, acb->last_reset - HZ / 2))
 761		acb->waiting_timer.expires =
 762		    acb->last_reset - HZ / 2 + 1;
 763	else
 764		acb->waiting_timer.expires = jiffies + to + 1;
 765	add_timer(&acb->waiting_timer);
 766}
 767
 768
 769/* Send the next command from the waiting list to the bus */
 770static void waiting_process_next(struct AdapterCtlBlk *acb)
 771{
 772	struct DeviceCtlBlk *start = NULL;
 773	struct DeviceCtlBlk *pos;
 774	struct DeviceCtlBlk *dcb;
 775	struct ScsiReqBlk *srb;
 776	struct list_head *dcb_list_head = &acb->dcb_list;
 777
 778	if (acb->active_dcb
 779	    || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
 780		return;
 781
 782	if (timer_pending(&acb->waiting_timer))
 783		del_timer(&acb->waiting_timer);
 784
 785	if (list_empty(dcb_list_head))
 786		return;
 787
 788	/*
 789	 * Find the starting dcb. Need to find it again in the list
 790	 * since the list may have changed since we set the ptr to it
 791	 */
 792	list_for_each_entry(dcb, dcb_list_head, list)
 793		if (dcb == acb->dcb_run_robin) {
 794			start = dcb;
 795			break;
 796		}
 797	if (!start) {
 798		/* This can happen! */
 799		start = list_entry(dcb_list_head->next, typeof(*start), list);
 800		acb->dcb_run_robin = start;
 801	}
 802
 803
 804	/*
 805	 * Loop over the dcb, but we start somewhere (potentially) in
 806	 * the middle of the loop so we need to manully do this.
 807	 */
 808	pos = start;
 809	do {
 810		struct list_head *waiting_list_head = &pos->srb_waiting_list;
 811
 812		/* Make sure, the next another device gets scheduled ... */
 813		acb->dcb_run_robin = dcb_get_next(dcb_list_head,
 814						  acb->dcb_run_robin);
 815
 816		if (list_empty(waiting_list_head) ||
 817		    pos->max_command <= list_size(&pos->srb_going_list)) {
 818			/* move to next dcb */
 819			pos = dcb_get_next(dcb_list_head, pos);
 820		} else {
 821			srb = list_entry(waiting_list_head->next,
 822					 struct ScsiReqBlk, list);
 823
 824			/* Try to send to the bus */
 825			if (!start_scsi(acb, pos, srb))
 826				list_move(&srb->list, &pos->srb_going_list);
 827			else
 828				waiting_set_timer(acb, HZ/50);
 829			break;
 830		}
 831	} while (pos != start);
 832}
 833
 834
 835/* Wake up waiting queue */
 836static void waiting_timeout(struct timer_list *t)
 837{
 838	unsigned long flags;
 839	struct AdapterCtlBlk *acb = from_timer(acb, t, waiting_timer);
 840	dprintkdbg(DBG_1,
 841		"waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
 842	DC395x_LOCK_IO(acb->scsi_host, flags);
 843	waiting_process_next(acb);
 844	DC395x_UNLOCK_IO(acb->scsi_host, flags);
 845}
 846
 847
 848/* Get the DCB for a given ID/LUN combination */
 849static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
 850{
 851	return acb->children[id][lun];
 852}
 853
 854
 855/* Send SCSI Request Block (srb) to adapter (acb) */
 856static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
 857{
 858	struct DeviceCtlBlk *dcb = srb->dcb;
 859
 860	if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
 861	    acb->active_dcb ||
 862	    (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
 863		list_add_tail(&srb->list, &dcb->srb_waiting_list);
 864		waiting_process_next(acb);
 865		return;
 866	}
 867
 868	if (!start_scsi(acb, dcb, srb)) {
 869		list_add_tail(&srb->list, &dcb->srb_going_list);
 870	} else {
 871		list_add(&srb->list, &dcb->srb_waiting_list);
 872		waiting_set_timer(acb, HZ / 50);
 873	}
 874}
 875
 876/* Prepare SRB for being sent to Device DCB w/ command *cmd */
 877static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
 878		struct ScsiReqBlk *srb)
 879{
 880	int nseg;
 881	enum dma_data_direction dir = cmd->sc_data_direction;
 882	dprintkdbg(DBG_0, "build_srb: (0x%p) <%02i-%i>\n",
 883		cmd, dcb->target_id, dcb->target_lun);
 884
 885	srb->dcb = dcb;
 886	srb->cmd = cmd;
 887	srb->sg_count = 0;
 888	srb->total_xfer_length = 0;
 889	srb->sg_bus_addr = 0;
 890	srb->sg_index = 0;
 891	srb->adapter_status = 0;
 892	srb->target_status = 0;
 893	srb->msg_count = 0;
 894	srb->status = 0;
 895	srb->flag = 0;
 896	srb->state = 0;
 897	srb->retry_count = 0;
 898	srb->tag_number = TAG_NONE;
 899	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
 900	srb->end_message = 0;
 901
 902	nseg = scsi_dma_map(cmd);
 903	BUG_ON(nseg < 0);
 904
 905	if (dir == PCI_DMA_NONE || !nseg) {
 906		dprintkdbg(DBG_0,
 907			"build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
 908			   cmd->bufflen, scsi_sglist(cmd), scsi_sg_count(cmd),
 909			   srb->segment_x[0].address);
 910	} else {
 911		int i;
 912		u32 reqlen = scsi_bufflen(cmd);
 913		struct scatterlist *sg;
 914		struct SGentry *sgp = srb->segment_x;
 915
 916		srb->sg_count = nseg;
 917
 918		dprintkdbg(DBG_0,
 919			   "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
 920			   reqlen, scsi_sglist(cmd), scsi_sg_count(cmd),
 921			   srb->sg_count);
 922
 923		scsi_for_each_sg(cmd, sg, srb->sg_count, i) {
 924			u32 busaddr = (u32)sg_dma_address(sg);
 925			u32 seglen = (u32)sg->length;
 926			sgp[i].address = busaddr;
 927			sgp[i].length = seglen;
 928			srb->total_xfer_length += seglen;
 929		}
 930		sgp += srb->sg_count - 1;
 931
 932		/*
 933		 * adjust last page if too big as it is allocated
 934		 * on even page boundaries
 935		 */
 936		if (srb->total_xfer_length > reqlen) {
 937			sgp->length -= (srb->total_xfer_length - reqlen);
 938			srb->total_xfer_length = reqlen;
 939		}
 940
 941		/* Fixup for WIDE padding - make sure length is even */
 942		if (dcb->sync_period & WIDE_SYNC &&
 943		    srb->total_xfer_length % 2) {
 944			srb->total_xfer_length++;
 945			sgp->length++;
 946		}
 947
 948		srb->sg_bus_addr = dma_map_single(&dcb->acb->dev->dev,
 949				srb->segment_x, SEGMENTX_LEN, DMA_TO_DEVICE);
 950
 951		dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
 952			srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
 953	}
 954
 955	srb->request_length = srb->total_xfer_length;
 956}
 957
 958
 959/**
 960 * dc395x_queue_command - queue scsi command passed from the mid
 961 * layer, invoke 'done' on completion
 962 *
 963 * @cmd: pointer to scsi command object
 964 * @done: function pointer to be invoked on completion
 965 *
 966 * Returns 1 if the adapter (host) is busy, else returns 0. One
 967 * reason for an adapter to be busy is that the number
 968 * of outstanding queued commands is already equal to
 969 * struct Scsi_Host::can_queue .
 970 *
 971 * Required: if struct Scsi_Host::can_queue is ever non-zero
 972 *           then this function is required.
 973 *
 974 * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
 975 *        and is expected to be held on return.
 976 *
 977 **/
 978static int dc395x_queue_command_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
 979{
 980	struct DeviceCtlBlk *dcb;
 981	struct ScsiReqBlk *srb;
 982	struct AdapterCtlBlk *acb =
 983	    (struct AdapterCtlBlk *)cmd->device->host->hostdata;
 984	dprintkdbg(DBG_0, "queue_command: (0x%p) <%02i-%i> cmnd=0x%02x\n",
 985		cmd, cmd->device->id, (u8)cmd->device->lun, cmd->cmnd[0]);
 986
 987	/* Assume BAD_TARGET; will be cleared later */
 988	cmd->result = DID_BAD_TARGET << 16;
 989
 990	/* ignore invalid targets */
 991	if (cmd->device->id >= acb->scsi_host->max_id ||
 992	    cmd->device->lun >= acb->scsi_host->max_lun ||
 993	    cmd->device->lun >31) {
 994		goto complete;
 995	}
 996
 997	/* does the specified lun on the specified device exist */
 998	if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
 999		dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
1000			cmd->device->id, (u8)cmd->device->lun);
1001		goto complete;
1002	}
1003
1004	/* do we have a DCB for the device */
1005	dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1006	if (!dcb) {
1007		/* should never happen */
1008		dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
1009			cmd->device->id, (u8)cmd->device->lun);
1010		goto complete;
1011	}
1012
1013	/* set callback and clear result in the command */
1014	cmd->scsi_done = done;
1015	cmd->result = 0;
1016
1017	srb = list_first_entry_or_null(&acb->srb_free_list,
1018			struct ScsiReqBlk, list);
1019	if (!srb) {
1020		/*
1021		 * Return 1 since we are unable to queue this command at this
1022		 * point in time.
1023		 */
1024		dprintkdbg(DBG_0, "queue_command: No free srb's\n");
1025		return 1;
1026	}
1027	list_del(&srb->list);
1028
1029	build_srb(cmd, dcb, srb);
1030
1031	if (!list_empty(&dcb->srb_waiting_list)) {
1032		/* append to waiting queue */
1033		list_add_tail(&srb->list, &dcb->srb_waiting_list);
1034		waiting_process_next(acb);
1035	} else {
1036		/* process immediately */
1037		send_srb(acb, srb);
1038	}
1039	dprintkdbg(DBG_1, "queue_command: (0x%p) done\n", cmd);
1040	return 0;
1041
1042complete:
1043	/*
1044	 * Complete the command immediatey, and then return 0 to
1045	 * indicate that we have handled the command. This is usually
1046	 * done when the commad is for things like non existent
1047	 * devices.
1048	 */
1049	done(cmd);
1050	return 0;
1051}
1052
1053static DEF_SCSI_QCMD(dc395x_queue_command)
1054
1055static void dump_register_info(struct AdapterCtlBlk *acb,
1056		struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
1057{
1058	u16 pstat;
1059	struct pci_dev *dev = acb->dev;
1060	pci_read_config_word(dev, PCI_STATUS, &pstat);
1061	if (!dcb)
1062		dcb = acb->active_dcb;
1063	if (!srb && dcb)
1064		srb = dcb->active_srb;
1065	if (srb) {
1066		if (!srb->cmd)
1067			dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
1068				srb, srb->cmd);
1069		else
1070			dprintkl(KERN_INFO, "dump: srb=%p cmd=%p "
1071				 "cmnd=0x%02x <%02i-%i>\n",
1072				srb, srb->cmd,
1073				srb->cmd->cmnd[0], srb->cmd->device->id,
1074				(u8)srb->cmd->device->lun);
1075		printk("  sglist=%p cnt=%i idx=%i len=%zu\n",
1076		       srb->segment_x, srb->sg_count, srb->sg_index,
1077		       srb->total_xfer_length);
1078		printk("  state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
1079		       srb->state, srb->status, srb->scsi_phase,
1080		       (acb->active_dcb) ? "" : "not");
1081	}
1082	dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
1083		"signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
1084		"rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
1085		"config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
1086		DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
1087		DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
1088		DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
1089		DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
1090		DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
1091		DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
1092		DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
1093		DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
1094		DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
1095		DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
1096		DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
1097		DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
1098		DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
1099	dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
1100		"irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
1101		"ctctr=0x%08x addr=0x%08x:0x%08x}\n",
1102		DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
1103		DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
1104		DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
1105		DC395x_read8(acb, TRM_S1040_DMA_STATUS),
1106		DC395x_read8(acb, TRM_S1040_DMA_INTEN),
1107		DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
1108		DC395x_read32(acb, TRM_S1040_DMA_XCNT),
1109		DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
1110		DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
1111		DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
1112	dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
1113		"pci{status=0x%04x}\n",
1114		DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
1115		DC395x_read8(acb, TRM_S1040_GEN_STATUS),
1116		DC395x_read8(acb, TRM_S1040_GEN_TIMER),
1117		pstat);
1118}
1119
1120
1121static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
1122{
1123#if debug_enabled(DBG_FIFO)
1124	u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1125	u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
1126	if (!(fifocnt & 0x40))
1127		dprintkdbg(DBG_FIFO,
1128			"clear_fifo: (%i bytes) on phase %02x in %s\n",
1129			fifocnt & 0x3f, lines, txt);
1130#endif
1131	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
1132}
1133
1134
1135static void reset_dev_param(struct AdapterCtlBlk *acb)
1136{
1137	struct DeviceCtlBlk *dcb;
1138	struct NvRamType *eeprom = &acb->eeprom;
1139	dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
1140
1141	list_for_each_entry(dcb, &acb->dcb_list, list) {
1142		u8 period_index;
1143
1144		dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
1145		dcb->sync_period = 0;
1146		dcb->sync_offset = 0;
1147
1148		dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
1149		period_index = eeprom->target[dcb->target_id].period & 0x07;
1150		dcb->min_nego_period = clock_period[period_index];
1151		if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
1152		    || !(acb->config & HCC_WIDE_CARD))
1153			dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
1154	}
1155}
1156
1157
1158/*
1159 * perform a hard reset on the SCSI bus
1160 * @cmd - some command for this host (for fetching hooks)
1161 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1162 */
1163static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1164{
1165	struct AdapterCtlBlk *acb =
1166		(struct AdapterCtlBlk *)cmd->device->host->hostdata;
1167	dprintkl(KERN_INFO,
1168		"eh_bus_reset: (0%p) target=<%02i-%i> cmd=%p\n",
1169		cmd, cmd->device->id, (u8)cmd->device->lun, cmd);
1170
1171	if (timer_pending(&acb->waiting_timer))
1172		del_timer(&acb->waiting_timer);
1173
1174	/*
1175	 * disable interrupt    
1176	 */
1177	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
1178	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
1179	DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
1180	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
1181
1182	reset_scsi_bus(acb);
1183	udelay(500);
1184
1185	/* We may be in serious trouble. Wait some seconds */
1186	acb->last_reset =
1187	    jiffies + 3 * HZ / 2 +
1188	    HZ * acb->eeprom.delay_time;
1189
1190	/*
1191	 * re-enable interrupt      
1192	 */
1193	/* Clear SCSI FIFO          */
1194	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
1195	clear_fifo(acb, "eh_bus_reset");
1196	/* Delete pending IRQ */
1197	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1198	set_basic_config(acb);
1199
1200	reset_dev_param(acb);
1201	doing_srb_done(acb, DID_RESET, cmd, 0);
1202	acb->active_dcb = NULL;
1203	acb->acb_flag = 0;	/* RESET_DETECT, RESET_DONE ,RESET_DEV */
1204	waiting_process_next(acb);
1205
1206	return SUCCESS;
1207}
1208
1209static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
1210{
1211	int rc;
1212
1213	spin_lock_irq(cmd->device->host->host_lock);
1214	rc = __dc395x_eh_bus_reset(cmd);
1215	spin_unlock_irq(cmd->device->host->host_lock);
1216
1217	return rc;
1218}
1219
1220/*
1221 * abort an errant SCSI command
1222 * @cmd - command to be aborted
1223 * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
1224 */
1225static int dc395x_eh_abort(struct scsi_cmnd *cmd)
1226{
1227	/*
1228	 * Look into our command queues: If it has not been sent already,
1229	 * we remove it and return success. Otherwise fail.
1230	 */
1231	struct AdapterCtlBlk *acb =
1232	    (struct AdapterCtlBlk *)cmd->device->host->hostdata;
1233	struct DeviceCtlBlk *dcb;
1234	struct ScsiReqBlk *srb;
1235	dprintkl(KERN_INFO, "eh_abort: (0x%p) target=<%02i-%i> cmd=%p\n",
1236		cmd, cmd->device->id, (u8)cmd->device->lun, cmd);
1237
1238	dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
1239	if (!dcb) {
1240		dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
1241		return FAILED;
1242	}
1243
1244	srb = find_cmd(cmd, &dcb->srb_waiting_list);
1245	if (srb) {
1246		list_del(&srb->list);
1247		pci_unmap_srb_sense(acb, srb);
1248		pci_unmap_srb(acb, srb);
1249		free_tag(dcb, srb);
1250		list_add_tail(&srb->list, &acb->srb_free_list);
1251		dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
1252		cmd->result = DID_ABORT << 16;
1253		return SUCCESS;
1254	}
1255	srb = find_cmd(cmd, &dcb->srb_going_list);
1256	if (srb) {
1257		dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n");
1258		/* XXX: Should abort the command here */
1259	} else {
1260		dprintkl(KERN_DEBUG, "eh_abort: Command not found\n");
1261	}
1262	return FAILED;
1263}
1264
1265
1266/* SDTR */
1267static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1268		struct ScsiReqBlk *srb)
1269{
1270	u8 *ptr = srb->msgout_buf + srb->msg_count;
1271	if (srb->msg_count > 1) {
1272		dprintkl(KERN_INFO,
1273			"build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1274			srb->msg_count, srb->msgout_buf[0],
1275			srb->msgout_buf[1]);
1276		return;
1277	}
1278	if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
1279		dcb->sync_offset = 0;
1280		dcb->min_nego_period = 200 >> 2;
1281	} else if (dcb->sync_offset == 0)
1282		dcb->sync_offset = SYNC_NEGO_OFFSET;
1283
1284	*ptr++ = MSG_EXTENDED;	/* (01h) */
1285	*ptr++ = 3;		/* length */
1286	*ptr++ = EXTENDED_SDTR;	/* (01h) */
1287	*ptr++ = dcb->min_nego_period;	/* Transfer period (in 4ns) */
1288	*ptr++ = dcb->sync_offset;	/* Transfer period (max. REQ/ACK dist) */
1289	srb->msg_count += 5;
1290	srb->state |= SRB_DO_SYNC_NEGO;
1291}
1292
1293
1294/* WDTR */
1295static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
1296		struct ScsiReqBlk *srb)
1297{
1298	u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
1299		   (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
1300	u8 *ptr = srb->msgout_buf + srb->msg_count;
1301	if (srb->msg_count > 1) {
1302		dprintkl(KERN_INFO,
1303			"build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
1304			srb->msg_count, srb->msgout_buf[0],
1305			srb->msgout_buf[1]);
1306		return;
1307	}
1308	*ptr++ = MSG_EXTENDED;	/* (01h) */
1309	*ptr++ = 2;		/* length */
1310	*ptr++ = EXTENDED_WDTR;	/* (03h) */
1311	*ptr++ = wide;
1312	srb->msg_count += 4;
1313	srb->state |= SRB_DO_WIDE_NEGO;
1314}
1315
1316
1317#if 0
1318/* Timer to work around chip flaw: When selecting and the bus is 
1319 * busy, we sometimes miss a Selection timeout IRQ */
1320void selection_timeout_missed(unsigned long ptr);
1321/* Sets the timer to wake us up */
1322static void selto_timer(struct AdapterCtlBlk *acb)
1323{
1324	if (timer_pending(&acb->selto_timer))
1325		return;
1326	acb->selto_timer.function = selection_timeout_missed;
1327	acb->selto_timer.data = (unsigned long) acb;
1328	if (time_before
1329	    (jiffies + HZ, acb->last_reset + HZ / 2))
1330		acb->selto_timer.expires =
1331		    acb->last_reset + HZ / 2 + 1;
1332	else
1333		acb->selto_timer.expires = jiffies + HZ + 1;
1334	add_timer(&acb->selto_timer);
1335}
1336
1337
1338void selection_timeout_missed(unsigned long ptr)
1339{
1340	unsigned long flags;
1341	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
1342	struct ScsiReqBlk *srb;
1343	dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
1344	if (!acb->active_dcb || !acb->active_dcb->active_srb) {
1345		dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
1346		return;
1347	}
1348	DC395x_LOCK_IO(acb->scsi_host, flags);
1349	srb = acb->active_dcb->active_srb;
1350	disconnect(acb);
1351	DC395x_UNLOCK_IO(acb->scsi_host, flags);
1352}
1353#endif
1354
1355
1356static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
1357		struct ScsiReqBlk* srb)
1358{
1359	u16 s_stat2, return_code;
1360	u8 s_stat, scsicommand, i, identify_message;
1361	u8 *ptr;
1362	dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> srb=%p\n",
1363		dcb->target_id, dcb->target_lun, srb);
1364
1365	srb->tag_number = TAG_NONE;	/* acb->tag_max_num: had error read in eeprom */
1366
1367	s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
1368	s_stat2 = 0;
1369	s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1370#if 1
1371	if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
1372		dprintkdbg(DBG_KG, "start_scsi: (0x%p) BUSY %02x %04x\n",
1373			s_stat, s_stat2);
1374		/*
1375		 * Try anyway?
1376		 *
1377		 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1378		 * Timeout, a Disconnect or a Reselection IRQ, so we would be screwed!
1379		 * (This is likely to be a bug in the hardware. Obviously, most people
1380		 *  only have one initiator per SCSI bus.)
1381		 * Instead let this fail and have the timer make sure the command is 
1382		 * tried again after a short time
1383		 */
1384		/*selto_timer (acb); */
1385		return 1;
1386	}
1387#endif
1388	if (acb->active_dcb) {
1389		dprintkl(KERN_DEBUG, "start_scsi: (0x%p) Attempt to start a"
1390			"command while another command (0x%p) is active.",
1391			srb->cmd,
1392			acb->active_dcb->active_srb ?
1393			    acb->active_dcb->active_srb->cmd : 0);
1394		return 1;
1395	}
1396	if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1397		dprintkdbg(DBG_KG, "start_scsi: (0x%p) Failed (busy)\n", srb->cmd);
1398		return 1;
1399	}
1400	/* Allow starting of SCSI commands half a second before we allow the mid-level
1401	 * to queue them again after a reset */
1402	if (time_before(jiffies, acb->last_reset - HZ / 2)) {
1403		dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
1404		return 1;
1405	}
1406
1407	/* Flush FIFO */
1408	clear_fifo(acb, "start_scsi");
1409	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
1410	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
1411	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
1412	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
1413	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
1414
1415	identify_message = dcb->identify_msg;
1416	/*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
1417	/* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
1418	if (srb->flag & AUTO_REQSENSE)
1419		identify_message &= 0xBF;
1420
1421	if (((srb->cmd->cmnd[0] == INQUIRY)
1422	     || (srb->cmd->cmnd[0] == REQUEST_SENSE)
1423	     || (srb->flag & AUTO_REQSENSE))
1424	    && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
1425		 && !(dcb->sync_mode & WIDE_NEGO_DONE))
1426		|| ((dcb->sync_mode & SYNC_NEGO_ENABLE)
1427		    && !(dcb->sync_mode & SYNC_NEGO_DONE)))
1428	    && (dcb->target_lun == 0)) {
1429		srb->msgout_buf[0] = identify_message;
1430		srb->msg_count = 1;
1431		scsicommand = SCMD_SEL_ATNSTOP;
1432		srb->state = SRB_MSGOUT;
1433#ifndef SYNC_FIRST
1434		if (dcb->sync_mode & WIDE_NEGO_ENABLE
1435		    && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1436			build_wdtr(acb, dcb, srb);
1437			goto no_cmd;
1438		}
1439#endif
1440		if (dcb->sync_mode & SYNC_NEGO_ENABLE
1441		    && dcb->inquiry7 & SCSI_INQ_SYNC) {
1442			build_sdtr(acb, dcb, srb);
1443			goto no_cmd;
1444		}
1445		if (dcb->sync_mode & WIDE_NEGO_ENABLE
1446		    && dcb->inquiry7 & SCSI_INQ_WBUS16) {
1447			build_wdtr(acb, dcb, srb);
1448			goto no_cmd;
1449		}
1450		srb->msg_count = 0;
1451	}
1452	/* Send identify message */
1453	DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
1454
1455	scsicommand = SCMD_SEL_ATN;
1456	srb->state = SRB_START_;
1457#ifndef DC395x_NO_TAGQ
1458	if ((dcb->sync_mode & EN_TAG_QUEUEING)
1459	    && (identify_message & 0xC0)) {
1460		/* Send Tag message */
1461		u32 tag_mask = 1;
1462		u8 tag_number = 0;
1463		while (tag_mask & dcb->tag_mask
1464		       && tag_number < dcb->max_command) {
1465			tag_mask = tag_mask << 1;
1466			tag_number++;
1467		}
1468		if (tag_number >= dcb->max_command) {
1469			dprintkl(KERN_WARNING, "start_scsi: (0x%p) "
1470				"Out of tags target=<%02i-%i>)\n",
1471				srb->cmd, srb->cmd->device->id,
1472				(u8)srb->cmd->device->lun);
1473			srb->state = SRB_READY;
1474			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1475				       DO_HWRESELECT);
1476			return 1;
1477		}
1478		/* Send Tag id */
1479		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
1480		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
1481		dcb->tag_mask |= tag_mask;
1482		srb->tag_number = tag_number;
1483		scsicommand = SCMD_SEL_ATN3;
1484		srb->state = SRB_START_;
1485	}
1486#endif
1487/*polling:*/
1488	/* Send CDB ..command block ......... */
1489	dprintkdbg(DBG_KG, "start_scsi: (0x%p) <%02i-%i> cmnd=0x%02x tag=%i\n",
1490		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun,
1491		srb->cmd->cmnd[0], srb->tag_number);
1492	if (srb->flag & AUTO_REQSENSE) {
1493		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
1494		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
1495		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1496		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1497		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
1498		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1499	} else {
1500		ptr = (u8 *)srb->cmd->cmnd;
1501		for (i = 0; i < srb->cmd->cmd_len; i++)
1502			DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
1503	}
1504      no_cmd:
1505	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1506		       DO_HWRESELECT | DO_DATALATCH);
1507	if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
1508		/* 
1509		 * If start_scsi return 1:
1510		 * we caught an interrupt (must be reset or reselection ... )
1511		 * : Let's process it first!
1512		 */
1513		dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> Failed - busy\n",
1514			srb->cmd, dcb->target_id, dcb->target_lun);
1515		srb->state = SRB_READY;
1516		free_tag(dcb, srb);
1517		srb->msg_count = 0;
1518		return_code = 1;
1519		/* This IRQ should NOT get lost, as we did not acknowledge it */
1520	} else {
1521		/* 
1522		 * If start_scsi returns 0:
1523		 * we know that the SCSI processor is free
1524		 */
1525		srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
1526		dcb->active_srb = srb;
1527		acb->active_dcb = dcb;
1528		return_code = 0;
1529		/* it's important for atn stop */
1530		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
1531			       DO_DATALATCH | DO_HWRESELECT);
1532		/* SCSI command */
1533		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
1534	}
1535	return return_code;
1536}
1537
1538
1539#define DC395x_ENABLE_MSGOUT \
1540 DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
1541 srb->state |= SRB_MSGOUT
1542
1543
1544/* abort command */
1545static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
1546		struct ScsiReqBlk *srb)
1547{
1548	srb->msgout_buf[0] = ABORT;
1549	srb->msg_count = 1;
1550	DC395x_ENABLE_MSGOUT;
1551	srb->state &= ~SRB_MSGIN;
1552	srb->state |= SRB_MSGOUT;
1553}
1554
1555
1556/**
1557 * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
1558 *                           have been triggered for this card.
1559 *
1560 * @acb:	 a pointer to the adpter control block
1561 * @scsi_status: the status return when we checked the card
1562 **/
1563static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
1564		u16 scsi_status)
1565{
1566	struct DeviceCtlBlk *dcb;
1567	struct ScsiReqBlk *srb;
1568	u16 phase;
1569	u8 scsi_intstatus;
1570	unsigned long flags;
1571	void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *, 
1572			      u16 *);
1573
1574	DC395x_LOCK_IO(acb->scsi_host, flags);
1575
1576	/* This acknowledges the IRQ */
1577	scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
1578	if ((scsi_status & 0x2007) == 0x2002)
1579		dprintkl(KERN_DEBUG,
1580			"COP after COP completed? %04x\n", scsi_status);
1581	if (debug_enabled(DBG_KG)) {
1582		if (scsi_intstatus & INT_SELTIMEOUT)
1583			dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
1584	}
1585	/*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
1586
1587	if (timer_pending(&acb->selto_timer))
1588		del_timer(&acb->selto_timer);
1589
1590	if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
1591		disconnect(acb);	/* bus free interrupt  */
1592		goto out_unlock;
1593	}
1594	if (scsi_intstatus & INT_RESELECTED) {
1595		reselect(acb);
1596		goto out_unlock;
1597	}
1598	if (scsi_intstatus & INT_SELECT) {
1599		dprintkl(KERN_INFO, "Host does not support target mode!\n");
1600		goto out_unlock;
1601	}
1602	if (scsi_intstatus & INT_SCSIRESET) {
1603		scsi_reset_detect(acb);
1604		goto out_unlock;
1605	}
1606	if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
1607		dcb = acb->active_dcb;
1608		if (!dcb) {
1609			dprintkl(KERN_DEBUG,
1610				"Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
1611				scsi_status, scsi_intstatus);
1612			goto out_unlock;
1613		}
1614		srb = dcb->active_srb;
1615		if (dcb->flag & ABORT_DEV_) {
1616			dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
1617			enable_msgout_abort(acb, srb);
1618		}
1619
1620		/* software sequential machine */
1621		phase = (u16)srb->scsi_phase;
1622
1623		/* 
1624		 * 62037 or 62137
1625		 * call  dc395x_scsi_phase0[]... "phase entry"
1626		 * handle every phase before start transfer
1627		 */
1628		/* data_out_phase0,	phase:0 */
1629		/* data_in_phase0,	phase:1 */
1630		/* command_phase0,	phase:2 */
1631		/* status_phase0,	phase:3 */
1632		/* nop0,		phase:4 PH_BUS_FREE .. initial phase */
1633		/* nop0,		phase:5 PH_BUS_FREE .. initial phase */
1634		/* msgout_phase0,	phase:6 */
1635		/* msgin_phase0,	phase:7 */
1636		dc395x_statev = dc395x_scsi_phase0[phase];
1637		dc395x_statev(acb, srb, &scsi_status);
1638
1639		/* 
1640		 * if there were any exception occurred scsi_status
1641		 * will be modify to bus free phase new scsi_status
1642		 * transfer out from ... previous dc395x_statev
1643		 */
1644		srb->scsi_phase = scsi_status & PHASEMASK;
1645		phase = (u16)scsi_status & PHASEMASK;
1646
1647		/* 
1648		 * call  dc395x_scsi_phase1[]... "phase entry" handle
1649		 * every phase to do transfer
1650		 */
1651		/* data_out_phase1,	phase:0 */
1652		/* data_in_phase1,	phase:1 */
1653		/* command_phase1,	phase:2 */
1654		/* status_phase1,	phase:3 */
1655		/* nop1,		phase:4 PH_BUS_FREE .. initial phase */
1656		/* nop1,		phase:5 PH_BUS_FREE .. initial phase */
1657		/* msgout_phase1,	phase:6 */
1658		/* msgin_phase1,	phase:7 */
1659		dc395x_statev = dc395x_scsi_phase1[phase];
1660		dc395x_statev(acb, srb, &scsi_status);
1661	}
1662      out_unlock:
1663	DC395x_UNLOCK_IO(acb->scsi_host, flags);
1664}
1665
1666
1667static irqreturn_t dc395x_interrupt(int irq, void *dev_id)
1668{
1669	struct AdapterCtlBlk *acb = dev_id;
1670	u16 scsi_status;
1671	u8 dma_status;
1672	irqreturn_t handled = IRQ_NONE;
1673
1674	/*
1675	 * Check for pending interrupt
1676	 */
1677	scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
1678	dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
1679	if (scsi_status & SCSIINTERRUPT) {
1680		/* interrupt pending - let's process it! */
1681		dc395x_handle_interrupt(acb, scsi_status);
1682		handled = IRQ_HANDLED;
1683	}
1684	else if (dma_status & 0x20) {
1685		/* Error from the DMA engine */
1686		dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
1687#if 0
1688		dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
1689		if (acb->active_dcb) {
1690			acb->active_dcb-> flag |= ABORT_DEV_;
1691			if (acb->active_dcb->active_srb)
1692				enable_msgout_abort(a

Large files files are truncated, but you can click here to view the full file