xref: /linux/drivers/scsi/dc395x.c (revision ca358af1d1bb5b9eb75f3998b20ab68f6b3ce69c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * dc395x.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Device Driver for Tekram DC395(U/UW/F), DC315(U)
51da177e4SLinus Torvalds  * PCI SCSI Bus Master Host Adapter
61da177e4SLinus Torvalds  * (SCSI chip set used Tekram ASIC TRM-S1040)
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:
91da177e4SLinus Torvalds  *  C.L. Huang <ching@tekram.com.tw>
101da177e4SLinus Torvalds  *  Erich Chen <erich@tekram.com.tw>
111da177e4SLinus Torvalds  *  (C) Copyright 1995-1999 Tekram Technology Co., Ltd.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *  Kurt Garloff <garloff@suse.de>
141da177e4SLinus Torvalds  *  (C) 1999-2000 Kurt Garloff
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *  Oliver Neukum <oliver@neukum.name>
171da177e4SLinus Torvalds  *  Ali Akcaagac <aliakc@web.de>
181da177e4SLinus Torvalds  *  Jamie Lenehan <lenehan@twibble.org>
191da177e4SLinus Torvalds  *  (C) 2003
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * License: GNU GPL
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *************************************************************************
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Redistribution and use in source and binary forms, with or without
261da177e4SLinus Torvalds  * modification, are permitted provided that the following conditions
271da177e4SLinus Torvalds  * are met:
281da177e4SLinus Torvalds  * 1. Redistributions of source code must retain the above copyright
291da177e4SLinus Torvalds  *    notice, this list of conditions and the following disclaimer.
301da177e4SLinus Torvalds  * 2. Redistributions in binary form must reproduce the above copyright
311da177e4SLinus Torvalds  *    notice, this list of conditions and the following disclaimer in the
321da177e4SLinus Torvalds  *    documentation and/or other materials provided with the distribution.
331da177e4SLinus Torvalds  * 3. The name of the author may not be used to endorse or promote products
341da177e4SLinus Torvalds  *    derived from this software without specific prior written permission.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
371da177e4SLinus Torvalds  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
381da177e4SLinus Torvalds  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
391da177e4SLinus Torvalds  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
401da177e4SLinus Torvalds  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
411da177e4SLinus Torvalds  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
421da177e4SLinus Torvalds  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
431da177e4SLinus Torvalds  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
441da177e4SLinus Torvalds  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
451da177e4SLinus Torvalds  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  ************************************************************************
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds #include <linux/module.h>
501da177e4SLinus Torvalds #include <linux/moduleparam.h>
511da177e4SLinus Torvalds #include <linux/delay.h>
521da177e4SLinus Torvalds #include <linux/ctype.h>
531da177e4SLinus Torvalds #include <linux/blkdev.h>
541da177e4SLinus Torvalds #include <linux/interrupt.h>
551da177e4SLinus Torvalds #include <linux/init.h>
561da177e4SLinus Torvalds #include <linux/spinlock.h>
571da177e4SLinus Torvalds #include <linux/pci.h>
581da177e4SLinus Torvalds #include <linux/list.h>
591da177e4SLinus Torvalds #include <linux/vmalloc.h>
605a0e3ad6STejun Heo #include <linux/slab.h>
611da177e4SLinus Torvalds #include <asm/io.h>
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #include <scsi/scsi.h>
641da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h>
651da177e4SLinus Torvalds #include <scsi/scsi_device.h>
661da177e4SLinus Torvalds #include <scsi/scsi_host.h>
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include "dc395x.h"
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define DC395X_NAME	"dc395x"
711da177e4SLinus Torvalds #define DC395X_BANNER	"Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
721da177e4SLinus Torvalds #define DC395X_VERSION	"v2.05, 2004/03/08"
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*---------------------------------------------------------------------------
751da177e4SLinus Torvalds                                   Features
761da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
771da177e4SLinus Torvalds /*
781da177e4SLinus Torvalds  * Set to disable parts of the driver
791da177e4SLinus Torvalds  */
801da177e4SLinus Torvalds /*#define DC395x_NO_DISCONNECT*/
811da177e4SLinus Torvalds /*#define DC395x_NO_TAGQ*/
821da177e4SLinus Torvalds /*#define DC395x_NO_SYNC*/
831da177e4SLinus Torvalds /*#define DC395x_NO_WIDE*/
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /*---------------------------------------------------------------------------
861da177e4SLinus Torvalds                                   Debugging
871da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  * Types of debugging that can be enabled and disabled
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds #define DBG_KG		0x0001
921da177e4SLinus Torvalds #define DBG_0		0x0002
931da177e4SLinus Torvalds #define DBG_1		0x0004
941da177e4SLinus Torvalds #define DBG_SG		0x0020
951da177e4SLinus Torvalds #define DBG_FIFO	0x0040
961da177e4SLinus Torvalds #define DBG_PIO		0x0080
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds  * Set set of things to output debugging for.
1011da177e4SLinus Torvalds  * Undefine to remove all debugging
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
1041da177e4SLinus Torvalds /*#define  DEBUG_MASK	DBG_0*/
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * Output a kernel mesage at the specified level and append the
1091da177e4SLinus Torvalds  * driver name and a ": " to the start of the message
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds #define dprintkl(level, format, arg...)  \
1121da177e4SLinus Torvalds     printk(level DC395X_NAME ": " format , ## arg)
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #ifdef DEBUG_MASK
1161da177e4SLinus Torvalds /*
1171da177e4SLinus Torvalds  * print a debug message - this is formated with KERN_DEBUG, then the
1181da177e4SLinus Torvalds  * driver name followed by a ": " and then the message is output.
1191da177e4SLinus Torvalds  * This also checks that the specified debug level is enabled before
1201da177e4SLinus Torvalds  * outputing the message
1211da177e4SLinus Torvalds  */
1221da177e4SLinus Torvalds #define dprintkdbg(type, format, arg...) \
1231da177e4SLinus Torvalds 	do { \
1241da177e4SLinus Torvalds 		if ((type) & (DEBUG_MASK)) \
1251da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG , format , ## arg); \
1261da177e4SLinus Torvalds 	} while (0)
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
1291da177e4SLinus Torvalds  * Check if the specified type of debugging is enabled
1301da177e4SLinus Torvalds  */
1311da177e4SLinus Torvalds #define debug_enabled(type)	((DEBUG_MASK) & (type))
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #else
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  * No debugging. Do nothing
1361da177e4SLinus Torvalds  */
1371da177e4SLinus Torvalds #define dprintkdbg(type, format, arg...) \
1381da177e4SLinus Torvalds 	do {} while (0)
1391da177e4SLinus Torvalds #define debug_enabled(type)	(0)
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds #endif
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds #ifndef PCI_VENDOR_ID_TEKRAM
1451da177e4SLinus Torvalds #define PCI_VENDOR_ID_TEKRAM                    0x1DE1	/* Vendor ID    */
1461da177e4SLinus Torvalds #endif
1471da177e4SLinus Torvalds #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
1481da177e4SLinus Torvalds #define PCI_DEVICE_ID_TEKRAM_TRMS1040           0x0391	/* Device ID    */
1491da177e4SLinus Torvalds #endif
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds #define DC395x_LOCK_IO(dev,flags)		spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
1531da177e4SLinus Torvalds #define DC395x_UNLOCK_IO(dev,flags)		spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds #define DC395x_read8(acb,address)		(u8)(inb(acb->io_port_base + (address)))
1561da177e4SLinus Torvalds #define DC395x_read16(acb,address)		(u16)(inw(acb->io_port_base + (address)))
1571da177e4SLinus Torvalds #define DC395x_read32(acb,address)		(u32)(inl(acb->io_port_base + (address)))
1581da177e4SLinus Torvalds #define DC395x_write8(acb,address,value)	outb((value), acb->io_port_base + (address))
1591da177e4SLinus Torvalds #define DC395x_write16(acb,address,value)	outw((value), acb->io_port_base + (address))
1601da177e4SLinus Torvalds #define DC395x_write32(acb,address,value)	outl((value), acb->io_port_base + (address))
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /* cmd->result */
1631da177e4SLinus Torvalds #define RES_TARGET		0x000000FF	/* Target State */
1641da177e4SLinus Torvalds #define RES_TARGET_LNX  STATUS_MASK	/* Only official ... */
1651da177e4SLinus Torvalds #define RES_ENDMSG		0x0000FF00	/* End Message */
1661da177e4SLinus Torvalds #define RES_DID			0x00FF0000	/* DID_ codes */
1671da177e4SLinus Torvalds #define RES_DRV			0xFF000000	/* DRIVER_ codes */
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds #define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt))
1701da177e4SLinus Torvalds #define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1)
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds #define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); }
1731da177e4SLinus Torvalds #define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; }
1741da177e4SLinus Torvalds #define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; }
1751da177e4SLinus Torvalds #define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; }
1761da177e4SLinus Torvalds #define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds #define TAG_NONE 255
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /*
1811da177e4SLinus Torvalds  * srb->segement_x is the hw sg list. It is always allocated as a
1821da177e4SLinus Torvalds  * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
1831da177e4SLinus Torvalds  * cross a page boundy.
1841da177e4SLinus Torvalds  */
1851da177e4SLinus Torvalds #define SEGMENTX_LEN	(sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
186b4b08e58SLinus Torvalds 
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds struct SGentry {
1891da177e4SLinus Torvalds 	u32 address;		/* bus! address */
1901da177e4SLinus Torvalds 	u32 length;
1911da177e4SLinus Torvalds };
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /* The SEEPROM structure for TRM_S1040 */
1941da177e4SLinus Torvalds struct NVRamTarget {
1951da177e4SLinus Torvalds 	u8 cfg0;		/* Target configuration byte 0  */
1961da177e4SLinus Torvalds 	u8 period;		/* Target period                */
1971da177e4SLinus Torvalds 	u8 cfg2;		/* Target configuration byte 2  */
1981da177e4SLinus Torvalds 	u8 cfg3;		/* Target configuration byte 3  */
1991da177e4SLinus Torvalds };
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds struct NvRamType {
2021da177e4SLinus Torvalds 	u8 sub_vendor_id[2];	/* 0,1  Sub Vendor ID   */
2031da177e4SLinus Torvalds 	u8 sub_sys_id[2];	/* 2,3  Sub System ID   */
2041da177e4SLinus Torvalds 	u8 sub_class;		/* 4    Sub Class       */
2051da177e4SLinus Torvalds 	u8 vendor_id[2];	/* 5,6  Vendor ID       */
2061da177e4SLinus Torvalds 	u8 device_id[2];	/* 7,8  Device ID       */
2071da177e4SLinus Torvalds 	u8 reserved;		/* 9    Reserved        */
2081da177e4SLinus Torvalds 	struct NVRamTarget target[DC395x_MAX_SCSI_ID];
2091da177e4SLinus Torvalds 						/** 10,11,12,13
2101da177e4SLinus Torvalds 						 ** 14,15,16,17
2111da177e4SLinus Torvalds 						 ** ....
2121da177e4SLinus Torvalds 						 ** ....
2131da177e4SLinus Torvalds 						 ** 70,71,72,73
2141da177e4SLinus Torvalds 						 */
2151da177e4SLinus Torvalds 	u8 scsi_id;		/* 74 Host Adapter SCSI ID      */
2161da177e4SLinus Torvalds 	u8 channel_cfg;		/* 75 Channel configuration     */
2171da177e4SLinus Torvalds 	u8 delay_time;		/* 76 Power on delay time       */
2181da177e4SLinus Torvalds 	u8 max_tag;		/* 77 Maximum tags              */
2191da177e4SLinus Torvalds 	u8 reserved0;		/* 78  */
2201da177e4SLinus Torvalds 	u8 boot_target;		/* 79  */
2211da177e4SLinus Torvalds 	u8 boot_lun;		/* 80  */
2221da177e4SLinus Torvalds 	u8 reserved1;		/* 81  */
2231da177e4SLinus Torvalds 	u16 reserved2[22];	/* 82,..125 */
2241da177e4SLinus Torvalds 	u16 cksum;		/* 126,127 */
2251da177e4SLinus Torvalds };
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds struct ScsiReqBlk {
2281da177e4SLinus Torvalds 	struct list_head list;		/* next/prev ptrs for srb lists */
2291da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
2301da177e4SLinus Torvalds 	struct scsi_cmnd *cmd;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	struct SGentry *segment_x;	/* Linear array of hw sg entries (up to 64 entries) */
233cdb8c2a6SGuennadi Liakhovetski 	dma_addr_t sg_bus_addr;	        /* Bus address of sg list (ie, of segment_x) */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	u8 sg_count;			/* No of HW sg entries for this request */
2361da177e4SLinus Torvalds 	u8 sg_index;			/* Index of HW sg entry for this request */
23725985edcSLucas De Marchi 	size_t total_xfer_length;	/* Total number of bytes remaining to be transferred */
238cdb8c2a6SGuennadi Liakhovetski 	size_t request_length;		/* Total number of bytes in this request */
2391da177e4SLinus Torvalds 	/*
2401da177e4SLinus Torvalds 	 * The sense buffer handling function, request_sense, uses
2411da177e4SLinus Torvalds 	 * the first hw sg entry (segment_x[0]) and the transfer
2421da177e4SLinus Torvalds 	 * length (total_xfer_length). While doing this it stores the
2431da177e4SLinus Torvalds 	 * original values into the last sg hw list
2441da177e4SLinus Torvalds 	 * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
2451da177e4SLinus Torvalds 	 * total_xfer_length in xferred. These values are restored in
2461da177e4SLinus Torvalds 	 * pci_unmap_srb_sense. This is the only place xferred is used.
2471da177e4SLinus Torvalds 	 */
248cdb8c2a6SGuennadi Liakhovetski 	size_t xferred;		        /* Saved copy of total_xfer_length */
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	u16 state;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	u8 msgin_buf[6];
2531da177e4SLinus Torvalds 	u8 msgout_buf[6];
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	u8 adapter_status;
2561da177e4SLinus Torvalds 	u8 target_status;
2571da177e4SLinus Torvalds 	u8 msg_count;
2581da177e4SLinus Torvalds 	u8 end_message;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	u8 tag_number;
2611da177e4SLinus Torvalds 	u8 status;
2621da177e4SLinus Torvalds 	u8 retry_count;
2631da177e4SLinus Torvalds 	u8 flag;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	u8 scsi_phase;
2661da177e4SLinus Torvalds };
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds struct DeviceCtlBlk {
2691da177e4SLinus Torvalds 	struct list_head list;		/* next/prev ptrs for the dcb list */
2701da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb;
2711da177e4SLinus Torvalds 	struct list_head srb_going_list;	/* head of going srb list */
2721da177e4SLinus Torvalds 	struct list_head srb_waiting_list;	/* head of waiting srb list */
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	struct ScsiReqBlk *active_srb;
2751da177e4SLinus Torvalds 	u32 tag_mask;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	u16 max_command;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	u8 target_id;		/* SCSI Target ID  (SCSI Only) */
2801da177e4SLinus Torvalds 	u8 target_lun;		/* SCSI Log.  Unit (SCSI Only) */
2811da177e4SLinus Torvalds 	u8 identify_msg;
2821da177e4SLinus Torvalds 	u8 dev_mode;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	u8 inquiry7;		/* To store Inquiry flags */
2851da177e4SLinus Torvalds 	u8 sync_mode;		/* 0:async mode */
2861da177e4SLinus Torvalds 	u8 min_nego_period;	/* for nego. */
2871da177e4SLinus Torvalds 	u8 sync_period;		/* for reg.  */
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	u8 sync_offset;		/* for reg. and nego.(low nibble) */
2901da177e4SLinus Torvalds 	u8 flag;
2911da177e4SLinus Torvalds 	u8 dev_type;
2921da177e4SLinus Torvalds 	u8 init_tcq_flag;
2931da177e4SLinus Torvalds };
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds struct AdapterCtlBlk {
2961da177e4SLinus Torvalds 	struct Scsi_Host *scsi_host;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	unsigned long io_port_base;
2991da177e4SLinus Torvalds 	unsigned long io_port_len;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	struct list_head dcb_list;		/* head of going dcb list */
3021da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb_run_robin;
3031da177e4SLinus Torvalds 	struct DeviceCtlBlk *active_dcb;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	struct list_head srb_free_list;		/* head of free srb list */
3061da177e4SLinus Torvalds 	struct ScsiReqBlk *tmp_srb;
3071da177e4SLinus Torvalds 	struct timer_list waiting_timer;
3081da177e4SLinus Torvalds 	struct timer_list selto_timer;
3091da177e4SLinus Torvalds 
31053a8936cSHannes Reinecke 	unsigned long last_reset;
31153a8936cSHannes Reinecke 
3121da177e4SLinus Torvalds 	u16 srb_count;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	u8 sel_timeout;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	unsigned int irq_level;
3171da177e4SLinus Torvalds 	u8 tag_max_num;
3181da177e4SLinus Torvalds 	u8 acb_flag;
3191da177e4SLinus Torvalds 	u8 gmode2;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	u8 config;
3221da177e4SLinus Torvalds 	u8 lun_chk;
3231da177e4SLinus Torvalds 	u8 scan_devices;
3241da177e4SLinus Torvalds 	u8 hostid_bit;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	u8 dcb_map[DC395x_MAX_SCSI_ID];
3271da177e4SLinus Torvalds 	struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	struct pci_dev *dev;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	u8 msg_len;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
3341da177e4SLinus Torvalds 	struct ScsiReqBlk srb;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	struct NvRamType eeprom;	/* eeprom settings for this adapter */
3371da177e4SLinus Torvalds };
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /*---------------------------------------------------------------------------
3411da177e4SLinus Torvalds                             Forward declarations
3421da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
3431da177e4SLinus Torvalds static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3441da177e4SLinus Torvalds 		u16 *pscsi_status);
3451da177e4SLinus Torvalds static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3461da177e4SLinus Torvalds 		u16 *pscsi_status);
3471da177e4SLinus Torvalds static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3481da177e4SLinus Torvalds 		u16 *pscsi_status);
3491da177e4SLinus Torvalds static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3501da177e4SLinus Torvalds 		u16 *pscsi_status);
3511da177e4SLinus Torvalds static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3521da177e4SLinus Torvalds 		u16 *pscsi_status);
3531da177e4SLinus Torvalds static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3541da177e4SLinus Torvalds 		u16 *pscsi_status);
3551da177e4SLinus Torvalds static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3561da177e4SLinus Torvalds 		u16 *pscsi_status);
3571da177e4SLinus Torvalds static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3581da177e4SLinus Torvalds 		u16 *pscsi_status);
3591da177e4SLinus Torvalds static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3601da177e4SLinus Torvalds 		u16 *pscsi_status);
3611da177e4SLinus Torvalds static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3621da177e4SLinus Torvalds 		u16 *pscsi_status);
3631da177e4SLinus Torvalds static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3641da177e4SLinus Torvalds 		u16 *pscsi_status);
3651da177e4SLinus Torvalds static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3661da177e4SLinus Torvalds 		u16 *pscsi_status);
3671da177e4SLinus Torvalds static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3681da177e4SLinus Torvalds 		u16 *pscsi_status);
3691da177e4SLinus Torvalds static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3701da177e4SLinus Torvalds 		u16 *pscsi_status);
3711da177e4SLinus Torvalds static void set_basic_config(struct AdapterCtlBlk *acb);
3721da177e4SLinus Torvalds static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
3731da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3741da177e4SLinus Torvalds static void reset_scsi_bus(struct AdapterCtlBlk *acb);
3751da177e4SLinus Torvalds static void data_io_transfer(struct AdapterCtlBlk *acb,
3761da177e4SLinus Torvalds 		struct ScsiReqBlk *srb, u16 io_dir);
3771da177e4SLinus Torvalds static void disconnect(struct AdapterCtlBlk *acb);
3781da177e4SLinus Torvalds static void reselect(struct AdapterCtlBlk *acb);
3791da177e4SLinus Torvalds static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3801da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3811da177e4SLinus Torvalds static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
3821da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3831da177e4SLinus Torvalds static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
3841da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3851da177e4SLinus Torvalds static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
3861da177e4SLinus Torvalds 		struct scsi_cmnd *cmd, u8 force);
3871da177e4SLinus Torvalds static void scsi_reset_detect(struct AdapterCtlBlk *acb);
3881da177e4SLinus Torvalds static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
3891da177e4SLinus Torvalds static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3901da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3911da177e4SLinus Torvalds static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3921da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3931da177e4SLinus Torvalds static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3941da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3951da177e4SLinus Torvalds static void set_xfer_rate(struct AdapterCtlBlk *acb,
3961da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb);
397af53b89bSKees Cook static void waiting_timeout(struct timer_list *t);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /*---------------------------------------------------------------------------
4011da177e4SLinus Torvalds                                  Static Data
4021da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
4031da177e4SLinus Torvalds static u16 current_sync_offset = 0;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds static void *dc395x_scsi_phase0[] = {
4061da177e4SLinus Torvalds 	data_out_phase0,/* phase:0 */
4071da177e4SLinus Torvalds 	data_in_phase0,	/* phase:1 */
4081da177e4SLinus Torvalds 	command_phase0,	/* phase:2 */
4091da177e4SLinus Torvalds 	status_phase0,	/* phase:3 */
4101da177e4SLinus Torvalds 	nop0,		/* phase:4 PH_BUS_FREE .. initial phase */
4111da177e4SLinus Torvalds 	nop0,		/* phase:5 PH_BUS_FREE .. initial phase */
4121da177e4SLinus Torvalds 	msgout_phase0,	/* phase:6 */
4131da177e4SLinus Torvalds 	msgin_phase0,	/* phase:7 */
4141da177e4SLinus Torvalds };
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds static void *dc395x_scsi_phase1[] = {
4171da177e4SLinus Torvalds 	data_out_phase1,/* phase:0 */
4181da177e4SLinus Torvalds 	data_in_phase1,	/* phase:1 */
4191da177e4SLinus Torvalds 	command_phase1,	/* phase:2 */
4201da177e4SLinus Torvalds 	status_phase1,	/* phase:3 */
4211da177e4SLinus Torvalds 	nop1,		/* phase:4 PH_BUS_FREE .. initial phase */
4221da177e4SLinus Torvalds 	nop1,		/* phase:5 PH_BUS_FREE .. initial phase */
4231da177e4SLinus Torvalds 	msgout_phase1,	/* phase:6 */
4241da177e4SLinus Torvalds 	msgin_phase1,	/* phase:7 */
4251da177e4SLinus Torvalds };
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds /*
4281da177e4SLinus Torvalds  *Fast20:	000	 50ns, 20.0 MHz
4291da177e4SLinus Torvalds  *		001	 75ns, 13.3 MHz
4301da177e4SLinus Torvalds  *		010	100ns, 10.0 MHz
4311da177e4SLinus Torvalds  *		011	125ns,  8.0 MHz
4321da177e4SLinus Torvalds  *		100	150ns,  6.6 MHz
4331da177e4SLinus Torvalds  *		101	175ns,  5.7 MHz
4341da177e4SLinus Torvalds  *		110	200ns,  5.0 MHz
4351da177e4SLinus Torvalds  *		111	250ns,  4.0 MHz
4361da177e4SLinus Torvalds  *
4371da177e4SLinus Torvalds  *Fast40(LVDS):	000	 25ns, 40.0 MHz
4381da177e4SLinus Torvalds  *		001	 50ns, 20.0 MHz
4391da177e4SLinus Torvalds  *		010	 75ns, 13.3 MHz
4401da177e4SLinus Torvalds  *		011	100ns, 10.0 MHz
4411da177e4SLinus Torvalds  *		100	125ns,  8.0 MHz
4421da177e4SLinus Torvalds  *		101	150ns,  6.6 MHz
4431da177e4SLinus Torvalds  *		110	175ns,  5.7 MHz
4441da177e4SLinus Torvalds  *		111	200ns,  5.0 MHz
4451da177e4SLinus Torvalds  */
4461da177e4SLinus Torvalds /*static u8	clock_period[] = {12,19,25,31,37,44,50,62};*/
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
4491da177e4SLinus Torvalds static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
4501da177e4SLinus Torvalds static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds /*---------------------------------------------------------------------------
4541da177e4SLinus Torvalds                                 Configuration
4551da177e4SLinus Torvalds   ---------------------------------------------------------------------------*/
4561da177e4SLinus Torvalds /*
4571da177e4SLinus Torvalds  * Module/boot parameters currently effect *all* instances of the
4581da177e4SLinus Torvalds  * card in the system.
4591da177e4SLinus Torvalds  */
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds /*
4621da177e4SLinus Torvalds  * Command line parameters are stored in a structure below.
4631da177e4SLinus Torvalds  * These are the index's into the structure for the various
4641da177e4SLinus Torvalds  * command line options.
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds #define CFG_ADAPTER_ID		0
4671da177e4SLinus Torvalds #define CFG_MAX_SPEED		1
4681da177e4SLinus Torvalds #define CFG_DEV_MODE		2
4691da177e4SLinus Torvalds #define CFG_ADAPTER_MODE	3
4701da177e4SLinus Torvalds #define CFG_TAGS		4
4711da177e4SLinus Torvalds #define CFG_RESET_DELAY		5
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds #define CFG_NUM			6	/* number of configuration items */
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /*
4771da177e4SLinus Torvalds  * Value used to indicate that a command line override
4781da177e4SLinus Torvalds  * hasn't been used to modify the value.
4791da177e4SLinus Torvalds  */
4801da177e4SLinus Torvalds #define CFG_PARAM_UNSET -1
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds /*
4841da177e4SLinus Torvalds  * Hold command line parameters.
4851da177e4SLinus Torvalds  */
4861da177e4SLinus Torvalds struct ParameterData {
4871da177e4SLinus Torvalds 	int value;		/* value of this setting */
4881da177e4SLinus Torvalds 	int min;		/* minimum value */
4891da177e4SLinus Torvalds 	int max;		/* maximum value */
4901da177e4SLinus Torvalds 	int def;		/* default value */
4911da177e4SLinus Torvalds 	int safe;		/* safe value */
4921da177e4SLinus Torvalds };
4936f039790SGreg Kroah-Hartman static struct ParameterData cfg_data[] = {
4941da177e4SLinus Torvalds 	{ /* adapter id */
4951da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
4961da177e4SLinus Torvalds 		0,
4971da177e4SLinus Torvalds 		15,
4981da177e4SLinus Torvalds 		7,
4991da177e4SLinus Torvalds 		7
5001da177e4SLinus Torvalds 	},
5011da177e4SLinus Torvalds 	{ /* max speed */
5021da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5031da177e4SLinus Torvalds 		  0,
5041da177e4SLinus Torvalds 		  7,
5051da177e4SLinus Torvalds 		  1,	/* 13.3Mhz */
5061da177e4SLinus Torvalds 		  4,	/*  6.7Hmz */
5071da177e4SLinus Torvalds 	},
5081da177e4SLinus Torvalds 	{ /* dev mode */
5091da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5101da177e4SLinus Torvalds 		0,
5111da177e4SLinus Torvalds 		0x3f,
5121da177e4SLinus Torvalds 		NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
5131da177e4SLinus Torvalds 			NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
5141da177e4SLinus Torvalds 			NTC_DO_SEND_START,
5151da177e4SLinus Torvalds 		NTC_DO_PARITY_CHK | NTC_DO_SEND_START
5161da177e4SLinus Torvalds 	},
5171da177e4SLinus Torvalds 	{ /* adapter mode */
5181da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5191da177e4SLinus Torvalds 		0,
5201da177e4SLinus Torvalds 		0x2f,
5211da177e4SLinus Torvalds 		NAC_SCANLUN |
5221da177e4SLinus Torvalds 		NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
5231da177e4SLinus Torvalds 			/*| NAC_ACTIVE_NEG*/,
5241da177e4SLinus Torvalds 		NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
5251da177e4SLinus Torvalds 	},
5261da177e4SLinus Torvalds 	{ /* tags */
5271da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5281da177e4SLinus Torvalds 		0,
5291da177e4SLinus Torvalds 		5,
5301da177e4SLinus Torvalds 		3,	/* 16 tags (??) */
5311da177e4SLinus Torvalds 		2,
5321da177e4SLinus Torvalds 	},
5331da177e4SLinus Torvalds 	{ /* reset delay */
5341da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5351da177e4SLinus Torvalds 		0,
5361da177e4SLinus Torvalds 		180,
5371da177e4SLinus Torvalds 		1,	/* 1 second */
5381da177e4SLinus Torvalds 		10,	/* 10 seconds */
5391da177e4SLinus Torvalds 	}
5401da177e4SLinus Torvalds };
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds /*
54459c51591SMichael Opdenacker  * Safe settings. If set to zero the BIOS/default values with
5451da177e4SLinus Torvalds  * command line overrides will be used. If set to 1 then safe and
5461da177e4SLinus Torvalds  * slow settings will be used.
5471da177e4SLinus Torvalds  */
54890ab5ee9SRusty Russell static bool use_safe_settings = 0;
5491da177e4SLinus Torvalds module_param_named(safe, use_safe_settings, bool, 0);
5501da177e4SLinus Torvalds MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
5541da177e4SLinus Torvalds MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
5571da177e4SLinus Torvalds 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");
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
5601da177e4SLinus Torvalds MODULE_PARM_DESC(dev_mode, "Device mode.");
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
5631da177e4SLinus Torvalds MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
5661da177e4SLinus Torvalds MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
5691da177e4SLinus Torvalds MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds /**
5731da177e4SLinus Torvalds  * set_safe_settings - if the use_safe_settings option is set then
5741da177e4SLinus Torvalds  * set all values to the safe and slow values.
5751da177e4SLinus Torvalds  **/
5766f039790SGreg Kroah-Hartman static void set_safe_settings(void)
5771da177e4SLinus Torvalds {
5781da177e4SLinus Torvalds 	if (use_safe_settings)
5791da177e4SLinus Torvalds 	{
5801da177e4SLinus Torvalds 		int i;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Using safe settings.\n");
5831da177e4SLinus Torvalds 		for (i = 0; i < CFG_NUM; i++)
5841da177e4SLinus Torvalds 		{
5851da177e4SLinus Torvalds 			cfg_data[i].value = cfg_data[i].safe;
5861da177e4SLinus Torvalds 		}
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds /**
5921da177e4SLinus Torvalds  * fix_settings - reset any boot parameters which are out of range
5931da177e4SLinus Torvalds  * back to the default values.
5941da177e4SLinus Torvalds  **/
5956f039790SGreg Kroah-Hartman static void fix_settings(void)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	int i;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	dprintkdbg(DBG_1,
6001da177e4SLinus Torvalds 		"setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
6011da177e4SLinus Torvalds 		"AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
6021da177e4SLinus Torvalds 		cfg_data[CFG_ADAPTER_ID].value,
6031da177e4SLinus Torvalds 		cfg_data[CFG_MAX_SPEED].value,
6041da177e4SLinus Torvalds 		cfg_data[CFG_DEV_MODE].value,
6051da177e4SLinus Torvalds 		cfg_data[CFG_ADAPTER_MODE].value,
6061da177e4SLinus Torvalds 		cfg_data[CFG_TAGS].value,
6071da177e4SLinus Torvalds 		cfg_data[CFG_RESET_DELAY].value);
6081da177e4SLinus Torvalds 	for (i = 0; i < CFG_NUM; i++)
6091da177e4SLinus Torvalds 	{
6101da177e4SLinus Torvalds 		if (cfg_data[i].value < cfg_data[i].min
6111da177e4SLinus Torvalds 		    || cfg_data[i].value > cfg_data[i].max)
6121da177e4SLinus Torvalds 			cfg_data[i].value = cfg_data[i].def;
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds /*
6191da177e4SLinus Torvalds  * Mapping from the eeprom delay index value (index into this array)
62059c51591SMichael Opdenacker  * to the number of actual seconds that the delay should be for.
6211da177e4SLinus Torvalds  */
6226f039790SGreg Kroah-Hartman static char eeprom_index_to_delay_map[] =
6231da177e4SLinus Torvalds 	{ 1, 3, 5, 10, 16, 30, 60, 120 };
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds /**
6271da177e4SLinus Torvalds  * eeprom_index_to_delay - Take the eeprom delay setting and convert it
6281da177e4SLinus Torvalds  * into a number of seconds.
6291da177e4SLinus Torvalds  *
6301da177e4SLinus Torvalds  * @eeprom: The eeprom structure in which we find the delay index to map.
6311da177e4SLinus Torvalds  **/
6326f039790SGreg Kroah-Hartman static void eeprom_index_to_delay(struct NvRamType *eeprom)
6331da177e4SLinus Torvalds {
6341da177e4SLinus Torvalds 	eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds /**
6391da177e4SLinus Torvalds  * delay_to_eeprom_index - Take a delay in seconds and return the
6401da177e4SLinus Torvalds  * closest eeprom index which will delay for at least that amount of
6411da177e4SLinus Torvalds  * seconds.
6421da177e4SLinus Torvalds  *
6431da177e4SLinus Torvalds  * @delay: The delay, in seconds, to find the eeprom index for.
6441da177e4SLinus Torvalds  **/
6456f039790SGreg Kroah-Hartman static int delay_to_eeprom_index(int delay)
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds 	u8 idx = 0;
6481da177e4SLinus Torvalds 	while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
6491da177e4SLinus Torvalds 		idx++;
6501da177e4SLinus Torvalds 	return idx;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds /**
6551da177e4SLinus Torvalds  * eeprom_override - Override the eeprom settings, in the provided
6561da177e4SLinus Torvalds  * eeprom structure, with values that have been set on the command
6571da177e4SLinus Torvalds  * line.
6581da177e4SLinus Torvalds  *
6591da177e4SLinus Torvalds  * @eeprom: The eeprom data to override with command line options.
6601da177e4SLinus Torvalds  **/
6616f039790SGreg Kroah-Hartman static void eeprom_override(struct NvRamType *eeprom)
6621da177e4SLinus Torvalds {
6631da177e4SLinus Torvalds 	u8 id;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	/* Adapter Settings */
6661da177e4SLinus Torvalds 	if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
6671da177e4SLinus Torvalds 		eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
6701da177e4SLinus Torvalds 		eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
6731da177e4SLinus Torvalds 		eeprom->delay_time = delay_to_eeprom_index(
6741da177e4SLinus Torvalds 					cfg_data[CFG_RESET_DELAY].value);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
6771da177e4SLinus Torvalds 		eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	/* Device Settings */
6801da177e4SLinus Torvalds 	for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
6811da177e4SLinus Torvalds 		if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
6821da177e4SLinus Torvalds 			eeprom->target[id].cfg0 =
6831da177e4SLinus Torvalds 				(u8)cfg_data[CFG_DEV_MODE].value;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 		if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
6861da177e4SLinus Torvalds 			eeprom->target[id].period =
6871da177e4SLinus Torvalds 				(u8)cfg_data[CFG_MAX_SPEED].value;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds /*---------------------------------------------------------------------------
6941da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds static unsigned int list_size(struct list_head *head)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	unsigned int count = 0;
6991da177e4SLinus Torvalds 	struct list_head *pos;
7001da177e4SLinus Torvalds 	list_for_each(pos, head)
7011da177e4SLinus Torvalds 		count++;
7021da177e4SLinus Torvalds 	return count;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
7071da177e4SLinus Torvalds 		struct DeviceCtlBlk *pos)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	int use_next = 0;
7101da177e4SLinus Torvalds 	struct DeviceCtlBlk* next = NULL;
7111da177e4SLinus Torvalds 	struct DeviceCtlBlk* i;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	if (list_empty(head))
7141da177e4SLinus Torvalds 		return NULL;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	/* find supplied dcb and then select the next one */
7171da177e4SLinus Torvalds 	list_for_each_entry(i, head, list)
7181da177e4SLinus Torvalds 		if (use_next) {
7191da177e4SLinus Torvalds 			next = i;
7201da177e4SLinus Torvalds 			break;
7211da177e4SLinus Torvalds 		} else if (i == pos) {
7221da177e4SLinus Torvalds 			use_next = 1;
7231da177e4SLinus Torvalds 		}
7241da177e4SLinus Torvalds 	/* if no next one take the head one (ie, wraparound) */
7251da177e4SLinus Torvalds 	if (!next)
7261da177e4SLinus Torvalds         	list_for_each_entry(i, head, list) {
7271da177e4SLinus Torvalds         		next = i;
7281da177e4SLinus Torvalds         		break;
7291da177e4SLinus Torvalds         	}
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	return next;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	if (srb->tag_number < 255) {
7381da177e4SLinus Torvalds 		dcb->tag_mask &= ~(1 << srb->tag_number);	/* free tag mask */
7391da177e4SLinus Torvalds 		srb->tag_number = 255;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds /* Find cmd in SRB list */
74577933d72SJesper Juhl static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
7461da177e4SLinus Torvalds 		struct list_head *head)
7471da177e4SLinus Torvalds {
7481da177e4SLinus Torvalds 	struct ScsiReqBlk *i;
7491da177e4SLinus Torvalds 	list_for_each_entry(i, head, list)
7501da177e4SLinus Torvalds 		if (i->cmd == cmd)
7511da177e4SLinus Torvalds 			return i;
7521da177e4SLinus Torvalds 	return NULL;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds /* Sets the timer to wake us up */
7561da177e4SLinus Torvalds static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
7591da177e4SLinus Torvalds 		return;
76053a8936cSHannes Reinecke 	if (time_before(jiffies + to, acb->last_reset - HZ / 2))
7611da177e4SLinus Torvalds 		acb->waiting_timer.expires =
76253a8936cSHannes Reinecke 		    acb->last_reset - HZ / 2 + 1;
7631da177e4SLinus Torvalds 	else
7641da177e4SLinus Torvalds 		acb->waiting_timer.expires = jiffies + to + 1;
7651da177e4SLinus Torvalds 	add_timer(&acb->waiting_timer);
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds /* Send the next command from the waiting list to the bus */
7701da177e4SLinus Torvalds static void waiting_process_next(struct AdapterCtlBlk *acb)
7711da177e4SLinus Torvalds {
7721da177e4SLinus Torvalds 	struct DeviceCtlBlk *start = NULL;
7731da177e4SLinus Torvalds 	struct DeviceCtlBlk *pos;
7741da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
7751da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
7761da177e4SLinus Torvalds 	struct list_head *dcb_list_head = &acb->dcb_list;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	if (acb->active_dcb
7791da177e4SLinus Torvalds 	    || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
7801da177e4SLinus Torvalds 		return;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
7831da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	if (list_empty(dcb_list_head))
7861da177e4SLinus Torvalds 		return;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	/*
7891da177e4SLinus Torvalds 	 * Find the starting dcb. Need to find it again in the list
7901da177e4SLinus Torvalds 	 * since the list may have changed since we set the ptr to it
7911da177e4SLinus Torvalds 	 */
7921da177e4SLinus Torvalds 	list_for_each_entry(dcb, dcb_list_head, list)
7931da177e4SLinus Torvalds 		if (dcb == acb->dcb_run_robin) {
7941da177e4SLinus Torvalds 			start = dcb;
7951da177e4SLinus Torvalds 			break;
7961da177e4SLinus Torvalds 		}
7971da177e4SLinus Torvalds 	if (!start) {
7981da177e4SLinus Torvalds 		/* This can happen! */
7991da177e4SLinus Torvalds 		start = list_entry(dcb_list_head->next, typeof(*start), list);
8001da177e4SLinus Torvalds 		acb->dcb_run_robin = start;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	/*
8051da177e4SLinus Torvalds 	 * Loop over the dcb, but we start somewhere (potentially) in
8061da177e4SLinus Torvalds 	 * the middle of the loop so we need to manully do this.
8071da177e4SLinus Torvalds 	 */
8081da177e4SLinus Torvalds 	pos = start;
8091da177e4SLinus Torvalds 	do {
8101da177e4SLinus Torvalds 		struct list_head *waiting_list_head = &pos->srb_waiting_list;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 		/* Make sure, the next another device gets scheduled ... */
8131da177e4SLinus Torvalds 		acb->dcb_run_robin = dcb_get_next(dcb_list_head,
8141da177e4SLinus Torvalds 						  acb->dcb_run_robin);
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 		if (list_empty(waiting_list_head) ||
8171da177e4SLinus Torvalds 		    pos->max_command <= list_size(&pos->srb_going_list)) {
8181da177e4SLinus Torvalds 			/* move to next dcb */
8191da177e4SLinus Torvalds 			pos = dcb_get_next(dcb_list_head, pos);
8201da177e4SLinus Torvalds 		} else {
8211da177e4SLinus Torvalds 			srb = list_entry(waiting_list_head->next,
8221da177e4SLinus Torvalds 					 struct ScsiReqBlk, list);
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 			/* Try to send to the bus */
8251da177e4SLinus Torvalds 			if (!start_scsi(acb, pos, srb))
826cd29660dSChristoph Hellwig 				list_move(&srb->list, &pos->srb_going_list);
8271da177e4SLinus Torvalds 			else
8281da177e4SLinus Torvalds 				waiting_set_timer(acb, HZ/50);
8291da177e4SLinus Torvalds 			break;
8301da177e4SLinus Torvalds 		}
8311da177e4SLinus Torvalds 	} while (pos != start);
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds /* Wake up waiting queue */
836af53b89bSKees Cook static void waiting_timeout(struct timer_list *t)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	unsigned long flags;
839af53b89bSKees Cook 	struct AdapterCtlBlk *acb = from_timer(acb, t, waiting_timer);
8401da177e4SLinus Torvalds 	dprintkdbg(DBG_1,
8411da177e4SLinus Torvalds 		"waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
8421da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
8431da177e4SLinus Torvalds 	waiting_process_next(acb);
8441da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds /* Get the DCB for a given ID/LUN combination */
8491da177e4SLinus Torvalds static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
8501da177e4SLinus Torvalds {
8511da177e4SLinus Torvalds 	return acb->children[id][lun];
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds /* Send SCSI Request Block (srb) to adapter (acb) */
8561da177e4SLinus Torvalds static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
8571da177e4SLinus Torvalds {
8581da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
8611da177e4SLinus Torvalds 	    acb->active_dcb ||
8621da177e4SLinus Torvalds 	    (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
863cd29660dSChristoph Hellwig 		list_add_tail(&srb->list, &dcb->srb_waiting_list);
8641da177e4SLinus Torvalds 		waiting_process_next(acb);
8651da177e4SLinus Torvalds 		return;
8661da177e4SLinus Torvalds 	}
8671da177e4SLinus Torvalds 
868cd29660dSChristoph Hellwig 	if (!start_scsi(acb, dcb, srb)) {
869cd29660dSChristoph Hellwig 		list_add_tail(&srb->list, &dcb->srb_going_list);
870cd29660dSChristoph Hellwig 	} else {
871cd29660dSChristoph Hellwig 		list_add(&srb->list, &dcb->srb_waiting_list);
8721da177e4SLinus Torvalds 		waiting_set_timer(acb, HZ / 50);
8731da177e4SLinus Torvalds 	}
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds /* Prepare SRB for being sent to Device DCB w/ command *cmd */
8771da177e4SLinus Torvalds static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
8781da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
8791da177e4SLinus Torvalds {
880a862ea31SFUJITA Tomonori 	int nseg;
8811da177e4SLinus Torvalds 	enum dma_data_direction dir = cmd->sc_data_direction;
8825cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "build_srb: (0x%p) <%02i-%i>\n",
8835cd049a5SChristoph Hellwig 		cmd, dcb->target_id, dcb->target_lun);
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	srb->dcb = dcb;
8861da177e4SLinus Torvalds 	srb->cmd = cmd;
8871da177e4SLinus Torvalds 	srb->sg_count = 0;
8881da177e4SLinus Torvalds 	srb->total_xfer_length = 0;
8891da177e4SLinus Torvalds 	srb->sg_bus_addr = 0;
8901da177e4SLinus Torvalds 	srb->sg_index = 0;
8911da177e4SLinus Torvalds 	srb->adapter_status = 0;
8921da177e4SLinus Torvalds 	srb->target_status = 0;
8931da177e4SLinus Torvalds 	srb->msg_count = 0;
8941da177e4SLinus Torvalds 	srb->status = 0;
8951da177e4SLinus Torvalds 	srb->flag = 0;
8961da177e4SLinus Torvalds 	srb->state = 0;
8971da177e4SLinus Torvalds 	srb->retry_count = 0;
8981da177e4SLinus Torvalds 	srb->tag_number = TAG_NONE;
8991da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
9001da177e4SLinus Torvalds 	srb->end_message = 0;
9011da177e4SLinus Torvalds 
902a862ea31SFUJITA Tomonori 	nseg = scsi_dma_map(cmd);
903a862ea31SFUJITA Tomonori 	BUG_ON(nseg < 0);
904a862ea31SFUJITA Tomonori 
905a862ea31SFUJITA Tomonori 	if (dir == PCI_DMA_NONE || !nseg) {
9061da177e4SLinus Torvalds 		dprintkdbg(DBG_0,
9071da177e4SLinus Torvalds 			"build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
908a862ea31SFUJITA Tomonori 			   cmd->bufflen, scsi_sglist(cmd), scsi_sg_count(cmd),
909a862ea31SFUJITA Tomonori 			   srb->segment_x[0].address);
910a862ea31SFUJITA Tomonori 	} else {
9111da177e4SLinus Torvalds 		int i;
912a862ea31SFUJITA Tomonori 		u32 reqlen = scsi_bufflen(cmd);
913a862ea31SFUJITA Tomonori 		struct scatterlist *sg;
9141da177e4SLinus Torvalds 		struct SGentry *sgp = srb->segment_x;
915a862ea31SFUJITA Tomonori 
916a862ea31SFUJITA Tomonori 		srb->sg_count = nseg;
917a862ea31SFUJITA Tomonori 
9181da177e4SLinus Torvalds 		dprintkdbg(DBG_0,
9191da177e4SLinus Torvalds 			   "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
920a862ea31SFUJITA Tomonori 			   reqlen, scsi_sglist(cmd), scsi_sg_count(cmd),
9211da177e4SLinus Torvalds 			   srb->sg_count);
9221da177e4SLinus Torvalds 
923a862ea31SFUJITA Tomonori 		scsi_for_each_sg(cmd, sg, srb->sg_count, i) {
924a862ea31SFUJITA Tomonori 			u32 busaddr = (u32)sg_dma_address(sg);
925a862ea31SFUJITA Tomonori 			u32 seglen = (u32)sg->length;
926b4b08e58SLinus Torvalds 			sgp[i].address = busaddr;
9271da177e4SLinus Torvalds 			sgp[i].length = seglen;
9281da177e4SLinus Torvalds 			srb->total_xfer_length += seglen;
9291da177e4SLinus Torvalds 		}
9301da177e4SLinus Torvalds 		sgp += srb->sg_count - 1;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 		/*
9331da177e4SLinus Torvalds 		 * adjust last page if too big as it is allocated
9341da177e4SLinus Torvalds 		 * on even page boundaries
9351da177e4SLinus Torvalds 		 */
9361da177e4SLinus Torvalds 		if (srb->total_xfer_length > reqlen) {
9371da177e4SLinus Torvalds 			sgp->length -= (srb->total_xfer_length - reqlen);
9381da177e4SLinus Torvalds 			srb->total_xfer_length = reqlen;
9391da177e4SLinus Torvalds 		}
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 		/* Fixup for WIDE padding - make sure length is even */
9421da177e4SLinus Torvalds 		if (dcb->sync_period & WIDE_SYNC &&
9431da177e4SLinus Torvalds 		    srb->total_xfer_length % 2) {
9441da177e4SLinus Torvalds 			srb->total_xfer_length++;
9451da177e4SLinus Torvalds 			sgp->length++;
9461da177e4SLinus Torvalds 		}
9471da177e4SLinus Torvalds 
948dfda5e21SChristoph Hellwig 		srb->sg_bus_addr = dma_map_single(&dcb->acb->dev->dev,
949dfda5e21SChristoph Hellwig 				srb->segment_x, SEGMENTX_LEN, DMA_TO_DEVICE);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 		dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
9521da177e4SLinus Torvalds 			srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
9531da177e4SLinus Torvalds 	}
954cdb8c2a6SGuennadi Liakhovetski 
955cdb8c2a6SGuennadi Liakhovetski 	srb->request_length = srb->total_xfer_length;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds /**
9601da177e4SLinus Torvalds  * dc395x_queue_command - queue scsi command passed from the mid
9611da177e4SLinus Torvalds  * layer, invoke 'done' on completion
9621da177e4SLinus Torvalds  *
9631da177e4SLinus Torvalds  * @cmd: pointer to scsi command object
9641da177e4SLinus Torvalds  * @done: function pointer to be invoked on completion
9651da177e4SLinus Torvalds  *
9661da177e4SLinus Torvalds  * Returns 1 if the adapter (host) is busy, else returns 0. One
9671da177e4SLinus Torvalds  * reason for an adapter to be busy is that the number
9681da177e4SLinus Torvalds  * of outstanding queued commands is already equal to
9691da177e4SLinus Torvalds  * struct Scsi_Host::can_queue .
9701da177e4SLinus Torvalds  *
9711da177e4SLinus Torvalds  * Required: if struct Scsi_Host::can_queue is ever non-zero
9721da177e4SLinus Torvalds  *           then this function is required.
9731da177e4SLinus Torvalds  *
9741da177e4SLinus Torvalds  * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
9751da177e4SLinus Torvalds  *        and is expected to be held on return.
9761da177e4SLinus Torvalds  *
9771da177e4SLinus Torvalds  **/
978f281233dSJeff Garzik static int dc395x_queue_command_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
9811da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
9821da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb =
9831da177e4SLinus Torvalds 	    (struct AdapterCtlBlk *)cmd->device->host->hostdata;
9845cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "queue_command: (0x%p) <%02i-%i> cmnd=0x%02x\n",
9859cb78c16SHannes Reinecke 		cmd, cmd->device->id, (u8)cmd->device->lun, cmd->cmnd[0]);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	/* Assume BAD_TARGET; will be cleared later */
9881da177e4SLinus Torvalds 	cmd->result = DID_BAD_TARGET << 16;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	/* ignore invalid targets */
9911da177e4SLinus Torvalds 	if (cmd->device->id >= acb->scsi_host->max_id ||
9921da177e4SLinus Torvalds 	    cmd->device->lun >= acb->scsi_host->max_lun ||
9931da177e4SLinus Torvalds 	    cmd->device->lun >31) {
9941da177e4SLinus Torvalds 		goto complete;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	/* does the specified lun on the specified device exist */
9981da177e4SLinus Torvalds 	if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
9991da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
10009cb78c16SHannes Reinecke 			cmd->device->id, (u8)cmd->device->lun);
10011da177e4SLinus Torvalds 		goto complete;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	/* do we have a DCB for the device */
10051da177e4SLinus Torvalds 	dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
10061da177e4SLinus Torvalds 	if (!dcb) {
10071da177e4SLinus Torvalds 		/* should never happen */
10081da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
10099cb78c16SHannes Reinecke 			cmd->device->id, (u8)cmd->device->lun);
10101da177e4SLinus Torvalds 		goto complete;
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	/* set callback and clear result in the command */
10141da177e4SLinus Torvalds 	cmd->scsi_done = done;
10151da177e4SLinus Torvalds 	cmd->result = 0;
10161da177e4SLinus Torvalds 
1017cd29660dSChristoph Hellwig 	srb = list_first_entry_or_null(&acb->srb_free_list,
1018cd29660dSChristoph Hellwig 			struct ScsiReqBlk, list);
1019cd29660dSChristoph Hellwig 	if (!srb) {
10201da177e4SLinus Torvalds 		/*
10211da177e4SLinus Torvalds 		 * Return 1 since we are unable to queue this command at this
10221da177e4SLinus Torvalds 		 * point in time.
10231da177e4SLinus Torvalds 		 */
10241da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "queue_command: No free srb's\n");
10251da177e4SLinus Torvalds 		return 1;
10261da177e4SLinus Torvalds 	}
1027cd29660dSChristoph Hellwig 	list_del(&srb->list);
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	build_srb(cmd, dcb, srb);
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	if (!list_empty(&dcb->srb_waiting_list)) {
10321da177e4SLinus Torvalds 		/* append to waiting queue */
1033cd29660dSChristoph Hellwig 		list_add_tail(&srb->list, &dcb->srb_waiting_list);
10341da177e4SLinus Torvalds 		waiting_process_next(acb);
10351da177e4SLinus Torvalds 	} else {
10361da177e4SLinus Torvalds 		/* process immediately */
10371da177e4SLinus Torvalds 		send_srb(acb, srb);
10381da177e4SLinus Torvalds 	}
10395cd049a5SChristoph Hellwig 	dprintkdbg(DBG_1, "queue_command: (0x%p) done\n", cmd);
10401da177e4SLinus Torvalds 	return 0;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds complete:
10431da177e4SLinus Torvalds 	/*
10441da177e4SLinus Torvalds 	 * Complete the command immediatey, and then return 0 to
10451da177e4SLinus Torvalds 	 * indicate that we have handled the command. This is usually
10461da177e4SLinus Torvalds 	 * done when the commad is for things like non existent
10471da177e4SLinus Torvalds 	 * devices.
10481da177e4SLinus Torvalds 	 */
10491da177e4SLinus Torvalds 	done(cmd);
10501da177e4SLinus Torvalds 	return 0;
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
1053f281233dSJeff Garzik static DEF_SCSI_QCMD(dc395x_queue_command)
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds static void dump_register_info(struct AdapterCtlBlk *acb,
10561da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
10571da177e4SLinus Torvalds {
10581da177e4SLinus Torvalds 	u16 pstat;
10591da177e4SLinus Torvalds 	struct pci_dev *dev = acb->dev;
10601da177e4SLinus Torvalds 	pci_read_config_word(dev, PCI_STATUS, &pstat);
10611da177e4SLinus Torvalds 	if (!dcb)
10621da177e4SLinus Torvalds 		dcb = acb->active_dcb;
10631da177e4SLinus Torvalds 	if (!srb && dcb)
10641da177e4SLinus Torvalds 		srb = dcb->active_srb;
10651da177e4SLinus Torvalds 	if (srb) {
10661da177e4SLinus Torvalds 		if (!srb->cmd)
10671da177e4SLinus Torvalds 			dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
10681da177e4SLinus Torvalds 				srb, srb->cmd);
10691da177e4SLinus Torvalds 		else
10705cd049a5SChristoph Hellwig 			dprintkl(KERN_INFO, "dump: srb=%p cmd=%p "
10711da177e4SLinus Torvalds 				 "cmnd=0x%02x <%02i-%i>\n",
10725cd049a5SChristoph Hellwig 				srb, srb->cmd,
10731da177e4SLinus Torvalds 				srb->cmd->cmnd[0], srb->cmd->device->id,
10749cb78c16SHannes Reinecke 				(u8)srb->cmd->device->lun);
10753c5c6658SAl Viro 		printk("  sglist=%p cnt=%i idx=%i len=%zu\n",
10761da177e4SLinus Torvalds 		       srb->segment_x, srb->sg_count, srb->sg_index,
10771da177e4SLinus Torvalds 		       srb->total_xfer_length);
10781da177e4SLinus Torvalds 		printk("  state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
10791da177e4SLinus Torvalds 		       srb->state, srb->status, srb->scsi_phase,
10801da177e4SLinus Torvalds 		       (acb->active_dcb) ? "" : "not");
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
10831da177e4SLinus Torvalds 		"signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
10841da177e4SLinus Torvalds 		"rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
10851da177e4SLinus Torvalds 		"config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
10861da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
10871da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
10881da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
10891da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
10901da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
10911da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
10921da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
10931da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
10941da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
10951da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
10961da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
10971da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
10981da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
10991da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
11001da177e4SLinus Torvalds 		"irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
11011da177e4SLinus Torvalds 		"ctctr=0x%08x addr=0x%08x:0x%08x}\n",
11021da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
11031da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
11041da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
11051da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_STATUS),
11061da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_INTEN),
11071da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
11081da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_XCNT),
11091da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
11101da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
11111da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
11121da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
11131da177e4SLinus Torvalds 		"pci{status=0x%04x}\n",
11141da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
11151da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_GEN_STATUS),
11161da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_GEN_TIMER),
11171da177e4SLinus Torvalds 		pstat);
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds #if debug_enabled(DBG_FIFO)
11241da177e4SLinus Torvalds 	u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
11251da177e4SLinus Torvalds 	u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
11261da177e4SLinus Torvalds 	if (!(fifocnt & 0x40))
11271da177e4SLinus Torvalds 		dprintkdbg(DBG_FIFO,
11281da177e4SLinus Torvalds 			"clear_fifo: (%i bytes) on phase %02x in %s\n",
11291da177e4SLinus Torvalds 			fifocnt & 0x3f, lines, txt);
11301da177e4SLinus Torvalds #endif
11311da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds static void reset_dev_param(struct AdapterCtlBlk *acb)
11361da177e4SLinus Torvalds {
11371da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
11381da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
11391da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
11421da177e4SLinus Torvalds 		u8 period_index;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 		dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
11451da177e4SLinus Torvalds 		dcb->sync_period = 0;
11461da177e4SLinus Torvalds 		dcb->sync_offset = 0;
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 		dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
11491da177e4SLinus Torvalds 		period_index = eeprom->target[dcb->target_id].period & 0x07;
11501da177e4SLinus Torvalds 		dcb->min_nego_period = clock_period[period_index];
11511da177e4SLinus Torvalds 		if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
11521da177e4SLinus Torvalds 		    || !(acb->config & HCC_WIDE_CARD))
11531da177e4SLinus Torvalds 			dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds /*
11591da177e4SLinus Torvalds  * perform a hard reset on the SCSI bus
11601da177e4SLinus Torvalds  * @cmd - some command for this host (for fetching hooks)
11611da177e4SLinus Torvalds  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
11621da177e4SLinus Torvalds  */
116368b3aa7cSJeff Garzik  static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
11641da177e4SLinus Torvalds {
11651da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb =
11661da177e4SLinus Torvalds 		(struct AdapterCtlBlk *)cmd->device->host->hostdata;
11671da177e4SLinus Torvalds 	dprintkl(KERN_INFO,
11685cd049a5SChristoph Hellwig 		"eh_bus_reset: (0%p) target=<%02i-%i> cmd=%p\n",
11699cb78c16SHannes Reinecke 		cmd, cmd->device->id, (u8)cmd->device->lun, cmd);
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
11721da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	/*
11751da177e4SLinus Torvalds 	 * disable interrupt
11761da177e4SLinus Torvalds 	 */
11771da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
11781da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
11791da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
11801da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	reset_scsi_bus(acb);
11831da177e4SLinus Torvalds 	udelay(500);
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	/* We may be in serious trouble. Wait some seconds */
118653a8936cSHannes Reinecke 	acb->last_reset =
11871da177e4SLinus Torvalds 	    jiffies + 3 * HZ / 2 +
11881da177e4SLinus Torvalds 	    HZ * acb->eeprom.delay_time;
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	/*
11911da177e4SLinus Torvalds 	 * re-enable interrupt
11921da177e4SLinus Torvalds 	 */
11931da177e4SLinus Torvalds 	/* Clear SCSI FIFO          */
11941da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
11951da177e4SLinus Torvalds 	clear_fifo(acb, "eh_bus_reset");
11961da177e4SLinus Torvalds 	/* Delete pending IRQ */
11971da177e4SLinus Torvalds 	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
11981da177e4SLinus Torvalds 	set_basic_config(acb);
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	reset_dev_param(acb);
12011da177e4SLinus Torvalds 	doing_srb_done(acb, DID_RESET, cmd, 0);
12021da177e4SLinus Torvalds 	acb->active_dcb = NULL;
12031da177e4SLinus Torvalds 	acb->acb_flag = 0;	/* RESET_DETECT, RESET_DONE ,RESET_DEV */
12041da177e4SLinus Torvalds 	waiting_process_next(acb);
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	return SUCCESS;
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
120968b3aa7cSJeff Garzik  static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
121068b3aa7cSJeff Garzik  {
121168b3aa7cSJeff Garzik  	int rc;
121268b3aa7cSJeff Garzik  
121368b3aa7cSJeff Garzik  	spin_lock_irq(cmd->device->host->host_lock);
121468b3aa7cSJeff Garzik  	rc = __dc395x_eh_bus_reset(cmd);
121568b3aa7cSJeff Garzik  	spin_unlock_irq(cmd->device->host->host_lock);
121668b3aa7cSJeff Garzik  
121768b3aa7cSJeff Garzik  	return rc;
121868b3aa7cSJeff Garzik  }
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds /*
12211da177e4SLinus Torvalds  * abort an errant SCSI command
12221da177e4SLinus Torvalds  * @cmd - command to be aborted
12231da177e4SLinus Torvalds  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
12241da177e4SLinus Torvalds  */
12251da177e4SLinus Torvalds static int dc395x_eh_abort(struct scsi_cmnd *cmd)
12261da177e4SLinus Torvalds {
12271da177e4SLinus Torvalds 	/*
12281da177e4SLinus Torvalds 	 * Look into our command queues: If it has not been sent already,
12291da177e4SLinus Torvalds 	 * we remove it and return success. Otherwise fail.
12301da177e4SLinus Torvalds 	 */
12311da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb =
12321da177e4SLinus Torvalds 	    (struct AdapterCtlBlk *)cmd->device->host->hostdata;
12331da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
12341da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
12355cd049a5SChristoph Hellwig 	dprintkl(KERN_INFO, "eh_abort: (0x%p) target=<%02i-%i> cmd=%p\n",
12369cb78c16SHannes Reinecke 		cmd, cmd->device->id, (u8)cmd->device->lun, cmd);
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
12391da177e4SLinus Torvalds 	if (!dcb) {
12401da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
12411da177e4SLinus Torvalds 		return FAILED;
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	srb = find_cmd(cmd, &dcb->srb_waiting_list);
12451da177e4SLinus Torvalds 	if (srb) {
1246cd29660dSChristoph Hellwig 		list_del(&srb->list);
12471da177e4SLinus Torvalds 		pci_unmap_srb_sense(acb, srb);
12481da177e4SLinus Torvalds 		pci_unmap_srb(acb, srb);
12491da177e4SLinus Torvalds 		free_tag(dcb, srb);
1250cd29660dSChristoph Hellwig 		list_add_tail(&srb->list, &acb->srb_free_list);
12511da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
12521da177e4SLinus Torvalds 		cmd->result = DID_ABORT << 16;
12531da177e4SLinus Torvalds 		return SUCCESS;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 	srb = find_cmd(cmd, &dcb->srb_going_list);
12561da177e4SLinus Torvalds 	if (srb) {
1257cdb8c2a6SGuennadi Liakhovetski 		dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n");
12581da177e4SLinus Torvalds 		/* XXX: Should abort the command here */
12591da177e4SLinus Torvalds 	} else {
1260cdb8c2a6SGuennadi Liakhovetski 		dprintkl(KERN_DEBUG, "eh_abort: Command not found\n");
12611da177e4SLinus Torvalds 	}
12621da177e4SLinus Torvalds 	return FAILED;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds /* SDTR */
12671da177e4SLinus Torvalds static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
12681da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
12691da177e4SLinus Torvalds {
12701da177e4SLinus Torvalds 	u8 *ptr = srb->msgout_buf + srb->msg_count;
12711da177e4SLinus Torvalds 	if (srb->msg_count > 1) {
12721da177e4SLinus Torvalds 		dprintkl(KERN_INFO,
12731da177e4SLinus Torvalds 			"build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
12741da177e4SLinus Torvalds 			srb->msg_count, srb->msgout_buf[0],
12751da177e4SLinus Torvalds 			srb->msgout_buf[1]);
12761da177e4SLinus Torvalds 		return;
12771da177e4SLinus Torvalds 	}
12781da177e4SLinus Torvalds 	if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
12791da177e4SLinus Torvalds 		dcb->sync_offset = 0;
12801da177e4SLinus Torvalds 		dcb->min_nego_period = 200 >> 2;
12811da177e4SLinus Torvalds 	} else if (dcb->sync_offset == 0)
12821da177e4SLinus Torvalds 		dcb->sync_offset = SYNC_NEGO_OFFSET;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	*ptr++ = MSG_EXTENDED;	/* (01h) */
12851da177e4SLinus Torvalds 	*ptr++ = 3;		/* length */
12861da177e4SLinus Torvalds 	*ptr++ = EXTENDED_SDTR;	/* (01h) */
12871da177e4SLinus Torvalds 	*ptr++ = dcb->min_nego_period;	/* Transfer period (in 4ns) */
12881da177e4SLinus Torvalds 	*ptr++ = dcb->sync_offset;	/* Transfer period (max. REQ/ACK dist) */
12891da177e4SLinus Torvalds 	srb->msg_count += 5;
12901da177e4SLinus Torvalds 	srb->state |= SRB_DO_SYNC_NEGO;
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds /* WDTR */
12951da177e4SLinus Torvalds static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
12961da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
12971da177e4SLinus Torvalds {
12981da177e4SLinus Torvalds 	u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
12991da177e4SLinus Torvalds 		   (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
13001da177e4SLinus Torvalds 	u8 *ptr = srb->msgout_buf + srb->msg_count;
13011da177e4SLinus Torvalds 	if (srb->msg_count > 1) {
13021da177e4SLinus Torvalds 		dprintkl(KERN_INFO,
13031da177e4SLinus Torvalds 			"build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
13041da177e4SLinus Torvalds 			srb->msg_count, srb->msgout_buf[0],
13051da177e4SLinus Torvalds 			srb->msgout_buf[1]);
13061da177e4SLinus Torvalds 		return;
13071da177e4SLinus Torvalds 	}
13081da177e4SLinus Torvalds 	*ptr++ = MSG_EXTENDED;	/* (01h) */
13091da177e4SLinus Torvalds 	*ptr++ = 2;		/* length */
13101da177e4SLinus Torvalds 	*ptr++ = EXTENDED_WDTR;	/* (03h) */
13111da177e4SLinus Torvalds 	*ptr++ = wide;
13121da177e4SLinus Torvalds 	srb->msg_count += 4;
13131da177e4SLinus Torvalds 	srb->state |= SRB_DO_WIDE_NEGO;
13141da177e4SLinus Torvalds }
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds #if 0
13181da177e4SLinus Torvalds /* Timer to work around chip flaw: When selecting and the bus is
13191da177e4SLinus Torvalds  * busy, we sometimes miss a Selection timeout IRQ */
13201da177e4SLinus Torvalds void selection_timeout_missed(unsigned long ptr);
13211da177e4SLinus Torvalds /* Sets the timer to wake us up */
13221da177e4SLinus Torvalds static void selto_timer(struct AdapterCtlBlk *acb)
13231da177e4SLinus Torvalds {
13241da177e4SLinus Torvalds 	if (timer_pending(&acb->selto_timer))
13251da177e4SLinus Torvalds 		return;
13261da177e4SLinus Torvalds 	acb->selto_timer.function = selection_timeout_missed;
13271da177e4SLinus Torvalds 	acb->selto_timer.data = (unsigned long) acb;
13281da177e4SLinus Torvalds 	if (time_before
132953a8936cSHannes Reinecke 	    (jiffies + HZ, acb->last_reset + HZ / 2))
13301da177e4SLinus Torvalds 		acb->selto_timer.expires =
133153a8936cSHannes Reinecke 		    acb->last_reset + HZ / 2 + 1;
13321da177e4SLinus Torvalds 	else
13331da177e4SLinus Torvalds 		acb->selto_timer.expires = jiffies + HZ + 1;
13341da177e4SLinus Torvalds 	add_timer(&acb->selto_timer);
13351da177e4SLinus Torvalds }
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds void selection_timeout_missed(unsigned long ptr)
13391da177e4SLinus Torvalds {
13401da177e4SLinus Torvalds 	unsigned long flags;
13411da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
13421da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
13431da177e4SLinus Torvalds 	dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
13441da177e4SLinus Torvalds 	if (!acb->active_dcb || !acb->active_dcb->active_srb) {
13451da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
13461da177e4SLinus Torvalds 		return;
13471da177e4SLinus Torvalds 	}
13481da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
13491da177e4SLinus Torvalds 	srb = acb->active_dcb->active_srb;
13501da177e4SLinus Torvalds 	disconnect(acb);
13511da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds #endif
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
13571da177e4SLinus Torvalds 		struct ScsiReqBlk* srb)
13581da177e4SLinus Torvalds {
13591da177e4SLinus Torvalds 	u16 s_stat2, return_code;
13601da177e4SLinus Torvalds 	u8 s_stat, scsicommand, i, identify_message;
13611da177e4SLinus Torvalds 	u8 *ptr;
13625cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> srb=%p\n",
13635cd049a5SChristoph Hellwig 		dcb->target_id, dcb->target_lun, srb);
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	srb->tag_number = TAG_NONE;	/* acb->tag_max_num: had error read in eeprom */
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
13681da177e4SLinus Torvalds 	s_stat2 = 0;
13691da177e4SLinus Torvalds 	s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
13701da177e4SLinus Torvalds #if 1
13711da177e4SLinus Torvalds 	if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
13725cd049a5SChristoph Hellwig 		dprintkdbg(DBG_KG, "start_scsi: (0x%p) BUSY %02x %04x\n",
13735cd049a5SChristoph Hellwig 			s_stat, s_stat2);
13741da177e4SLinus Torvalds 		/*
13751da177e4SLinus Torvalds 		 * Try anyway?
13761da177e4SLinus Torvalds 		 *
13771da177e4SLinus Torvalds 		 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1378b71a8eb0SUwe Kleine-König 		 * Timeout, a Disconnect or a Reselection IRQ, so we would be screwed!
13791da177e4SLinus Torvalds 		 * (This is likely to be a bug in the hardware. Obviously, most people
13801da177e4SLinus Torvalds 		 *  only have one initiator per SCSI bus.)
13811da177e4SLinus Torvalds 		 * Instead let this fail and have the timer make sure the command is
13821da177e4SLinus Torvalds 		 * tried again after a short time
13831da177e4SLinus Torvalds 		 */
13841da177e4SLinus Torvalds 		/*selto_timer (acb); */
13851da177e4SLinus Torvalds 		return 1;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds #endif
13881da177e4SLinus Torvalds 	if (acb->active_dcb) {
13895cd049a5SChristoph Hellwig 		dprintkl(KERN_DEBUG, "start_scsi: (0x%p) Attempt to start a"
13905cd049a5SChristoph Hellwig 			"command while another command (0x%p) is active.",
13915cd049a5SChristoph Hellwig 			srb->cmd,
13921da177e4SLinus Torvalds 			acb->active_dcb->active_srb ?
13935cd049a5SChristoph Hellwig 			    acb->active_dcb->active_srb->cmd : 0);
13941da177e4SLinus Torvalds 		return 1;
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds 	if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
13975cd049a5SChristoph Hellwig 		dprintkdbg(DBG_KG, "start_scsi: (0x%p) Failed (busy)\n", srb->cmd);
13981da177e4SLinus Torvalds 		return 1;
13991da177e4SLinus Torvalds 	}
14001da177e4SLinus Torvalds 	/* Allow starting of SCSI commands half a second before we allow the mid-level
14011da177e4SLinus Torvalds 	 * to queue them again after a reset */
140253a8936cSHannes Reinecke 	if (time_before(jiffies, acb->last_reset - HZ / 2)) {
14031da177e4SLinus Torvalds 		dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
14041da177e4SLinus Torvalds 		return 1;
14051da177e4SLinus Torvalds 	}
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	/* Flush FIFO */
14081da177e4SLinus Torvalds 	clear_fifo(acb, "start_scsi");
14091da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
14101da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
14111da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
14121da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
14131da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	identify_message = dcb->identify_msg;
14161da177e4SLinus Torvalds 	/*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
14171da177e4SLinus Torvalds 	/* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
14181da177e4SLinus Torvalds 	if (srb->flag & AUTO_REQSENSE)
14191da177e4SLinus Torvalds 		identify_message &= 0xBF;
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	if (((srb->cmd->cmnd[0] == INQUIRY)
14221da177e4SLinus Torvalds 	     || (srb->cmd->cmnd[0] == REQUEST_SENSE)
14231da177e4SLinus Torvalds 	     || (srb->flag & AUTO_REQSENSE))
14241da177e4SLinus Torvalds 	    && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
14251da177e4SLinus Torvalds 		 && !(dcb->sync_mode & WIDE_NEGO_DONE))
14261da177e4SLinus Torvalds 		|| ((dcb->sync_mode & SYNC_NEGO_ENABLE)
14271da177e4SLinus Torvalds 		    && !(dcb->sync_mode & SYNC_NEGO_DONE)))
14281da177e4SLinus Torvalds 	    && (dcb->target_lun == 0)) {
14291da177e4SLinus Torvalds 		srb->msgout_buf[0] = identify_message;
14301da177e4SLinus Torvalds 		srb->msg_count = 1;
14311da177e4SLinus Torvalds 		scsicommand = SCMD_SEL_ATNSTOP;
14321da177e4SLinus Torvalds 		srb->state = SRB_MSGOUT;
14331da177e4SLinus Torvalds #ifndef SYNC_FIRST
14341da177e4SLinus Torvalds 		if (dcb->sync_mode & WIDE_NEGO_ENABLE
14351da177e4SLinus Torvalds 		    && dcb->inquiry7 & SCSI_INQ_WBUS16) {
14361da177e4SLinus Torvalds 			build_wdtr(acb, dcb, srb);
14371da177e4SLinus Torvalds 			goto no_cmd;
14381da177e4SLinus Torvalds 		}
14391da177e4SLinus Torvalds #endif
14401da177e4SLinus Torvalds 		if (dcb->sync_mode & SYNC_NEGO_ENABLE
14411da177e4SLinus Torvalds 		    && dcb->inquiry7 & SCSI_INQ_SYNC) {
14421da177e4SLinus Torvalds 			build_sdtr(acb, dcb, srb);
14431da177e4SLinus Torvalds 			goto no_cmd;
14441da177e4SLinus Torvalds 		}
14451da177e4SLinus Torvalds 		if (dcb->sync_mode & WIDE_NEGO_ENABLE
14461da177e4SLinus Torvalds 		    && dcb->inquiry7 & SCSI_INQ_WBUS16) {
14471da177e4SLinus Torvalds 			build_wdtr(acb, dcb, srb);
14481da177e4SLinus Torvalds 			goto no_cmd;
14491da177e4SLinus Torvalds 		}
14501da177e4SLinus Torvalds 		srb->msg_count = 0;
14511da177e4SLinus Torvalds 	}
14521da177e4SLinus Torvalds 	/* Send identify message */
14531da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	scsicommand = SCMD_SEL_ATN;
14561da177e4SLinus Torvalds 	srb->state = SRB_START_;
14571da177e4SLinus Torvalds #ifndef DC395x_NO_TAGQ
14581da177e4SLinus Torvalds 	if ((dcb->sync_mode & EN_TAG_QUEUEING)
14591da177e4SLinus Torvalds 	    && (identify_message & 0xC0)) {
14601da177e4SLinus Torvalds 		/* Send Tag message */
14611da177e4SLinus Torvalds 		u32 tag_mask = 1;
14621da177e4SLinus Torvalds 		u8 tag_number = 0;
14631da177e4SLinus Torvalds 		while (tag_mask & dcb->tag_mask
1464d8187b94SRoel Kluin 		       && tag_number < dcb->max_command) {
14651da177e4SLinus Torvalds 			tag_mask = tag_mask << 1;
14661da177e4SLinus Torvalds 			tag_number++;
14671da177e4SLinus Torvalds 		}
14681da177e4SLinus Torvalds 		if (tag_number >= dcb->max_command) {
14695cd049a5SChristoph Hellwig 			dprintkl(KERN_WARNING, "start_scsi: (0x%p) "
14701da177e4SLinus Torvalds 				"Out of tags target=<%02i-%i>)\n",
14715cd049a5SChristoph Hellwig 				srb->cmd, srb->cmd->device->id,
14729cb78c16SHannes Reinecke 				(u8)srb->cmd->device->lun);
14731da177e4SLinus Torvalds 			srb->state = SRB_READY;
14741da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
14751da177e4SLinus Torvalds 				       DO_HWRESELECT);
14761da177e4SLinus Torvalds 			return 1;
14771da177e4SLinus Torvalds 		}
14781da177e4SLinus Torvalds 		/* Send Tag id */
14791da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
14801da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
14811da177e4SLinus Torvalds 		dcb->tag_mask |= tag_mask;
14821da177e4SLinus Torvalds 		srb->tag_number = tag_number;
14831da177e4SLinus Torvalds 		scsicommand = SCMD_SEL_ATN3;
14841da177e4SLinus Torvalds 		srb->state = SRB_START_;
14851da177e4SLinus Torvalds 	}
14861da177e4SLinus Torvalds #endif
14871da177e4SLinus Torvalds /*polling:*/
14881da177e4SLinus Torvalds 	/* Send CDB ..command block ......... */
14895cd049a5SChristoph Hellwig 	dprintkdbg(DBG_KG, "start_scsi: (0x%p) <%02i-%i> cmnd=0x%02x tag=%i\n",
14909cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun,
14911da177e4SLinus Torvalds 		srb->cmd->cmnd[0], srb->tag_number);
14921da177e4SLinus Torvalds 	if (srb->flag & AUTO_REQSENSE) {
14931da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
14941da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
14951da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
14961da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1497b80ca4f7SFUJITA Tomonori 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
14981da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
14991da177e4SLinus Torvalds 	} else {
15001da177e4SLinus Torvalds 		ptr = (u8 *)srb->cmd->cmnd;
15011da177e4SLinus Torvalds 		for (i = 0; i < srb->cmd->cmd_len; i++)
15021da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
15031da177e4SLinus Torvalds 	}
15041da177e4SLinus Torvalds       no_cmd:
15051da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
15061da177e4SLinus Torvalds 		       DO_HWRESELECT | DO_DATALATCH);
15071da177e4SLinus Torvalds 	if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
15081da177e4SLinus Torvalds 		/*
15091da177e4SLinus Torvalds 		 * If start_scsi return 1:
15101da177e4SLinus Torvalds 		 * we caught an interrupt (must be reset or reselection ... )
15111da177e4SLinus Torvalds 		 * : Let's process it first!
15121da177e4SLinus Torvalds 		 */
15135cd049a5SChristoph Hellwig 		dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> Failed - busy\n",
15145cd049a5SChristoph Hellwig 			srb->cmd, dcb->target_id, dcb->target_lun);
15151da177e4SLinus Torvalds 		srb->state = SRB_READY;
15161da177e4SLinus Torvalds 		free_tag(dcb, srb);
15171da177e4SLinus Torvalds 		srb->msg_count = 0;
15181da177e4SLinus Torvalds 		return_code = 1;
15191da177e4SLinus Torvalds 		/* This IRQ should NOT get lost, as we did not acknowledge it */
15201da177e4SLinus Torvalds 	} else {
15211da177e4SLinus Torvalds 		/*
15221da177e4SLinus Torvalds 		 * If start_scsi returns 0:
15231da177e4SLinus Torvalds 		 * we know that the SCSI processor is free
15241da177e4SLinus Torvalds 		 */
15251da177e4SLinus Torvalds 		srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
15261da177e4SLinus Torvalds 		dcb->active_srb = srb;
15271da177e4SLinus Torvalds 		acb->active_dcb = dcb;
15281da177e4SLinus Torvalds 		return_code = 0;
15291da177e4SLinus Torvalds 		/* it's important for atn stop */
15301da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
15311da177e4SLinus Torvalds 			       DO_DATALATCH | DO_HWRESELECT);
15321da177e4SLinus Torvalds 		/* SCSI command */
15331da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
15341da177e4SLinus Torvalds 	}
15351da177e4SLinus Torvalds 	return return_code;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds #define DC395x_ENABLE_MSGOUT \
15401da177e4SLinus Torvalds  DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
15411da177e4SLinus Torvalds  srb->state |= SRB_MSGOUT
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds /* abort command */
15451da177e4SLinus Torvalds static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
15461da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
15471da177e4SLinus Torvalds {
15481da177e4SLinus Torvalds 	srb->msgout_buf[0] = ABORT;
15491da177e4SLinus Torvalds 	srb->msg_count = 1;
15501da177e4SLinus Torvalds 	DC395x_ENABLE_MSGOUT;
15511da177e4SLinus Torvalds 	srb->state &= ~SRB_MSGIN;
15521da177e4SLinus Torvalds 	srb->state |= SRB_MSGOUT;
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds /**
15571da177e4SLinus Torvalds  * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
15581da177e4SLinus Torvalds  *                           have been triggered for this card.
15591da177e4SLinus Torvalds  *
15601da177e4SLinus Torvalds  * @acb:	 a pointer to the adpter control block
15611da177e4SLinus Torvalds  * @scsi_status: the status return when we checked the card
15621da177e4SLinus Torvalds  **/
15631da177e4SLinus Torvalds static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
15641da177e4SLinus Torvalds 		u16 scsi_status)
15651da177e4SLinus Torvalds {
15661da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
15671da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
15681da177e4SLinus Torvalds 	u16 phase;
15691da177e4SLinus Torvalds 	u8 scsi_intstatus;
15701da177e4SLinus Torvalds 	unsigned long flags;
15711da177e4SLinus Torvalds 	void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
15721da177e4SLinus Torvalds 			      u16 *);
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	/* This acknowledges the IRQ */
15771da177e4SLinus Torvalds 	scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
15781da177e4SLinus Torvalds 	if ((scsi_status & 0x2007) == 0x2002)
15791da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
15801da177e4SLinus Torvalds 			"COP after COP completed? %04x\n", scsi_status);
15811da177e4SLinus Torvalds 	if (debug_enabled(DBG_KG)) {
15821da177e4SLinus Torvalds 		if (scsi_intstatus & INT_SELTIMEOUT)
15831da177e4SLinus Torvalds 			dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
15841da177e4SLinus Torvalds 	}
15851da177e4SLinus Torvalds 	/*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	if (timer_pending(&acb->selto_timer))
15881da177e4SLinus Torvalds 		del_timer(&acb->selto_timer);
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 	if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
15911da177e4SLinus Torvalds 		disconnect(acb);	/* bus free interrupt  */
15921da177e4SLinus Torvalds 		goto out_unlock;
15931da177e4SLinus Torvalds 	}
15941da177e4SLinus Torvalds 	if (scsi_intstatus & INT_RESELECTED) {
15951da177e4SLinus Torvalds 		reselect(acb);
15961da177e4SLinus Torvalds 		goto out_unlock;
15971da177e4SLinus Torvalds 	}
15981da177e4SLinus Torvalds 	if (scsi_intstatus & INT_SELECT) {
15991da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Host does not support target mode!\n");
16001da177e4SLinus Torvalds 		goto out_unlock;
16011da177e4SLinus Torvalds 	}
16021da177e4SLinus Torvalds 	if (scsi_intstatus & INT_SCSIRESET) {
16031da177e4SLinus Torvalds 		scsi_reset_detect(acb);
16041da177e4SLinus Torvalds 		goto out_unlock;
16051da177e4SLinus Torvalds 	}
16061da177e4SLinus Torvalds 	if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
16071da177e4SLinus Torvalds 		dcb = acb->active_dcb;
16081da177e4SLinus Torvalds 		if (!dcb) {
16091da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
16101da177e4SLinus Torvalds 				"Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
16111da177e4SLinus Torvalds 				scsi_status, scsi_intstatus);
16121da177e4SLinus Torvalds 			goto out_unlock;
16131da177e4SLinus Torvalds 		}
16141da177e4SLinus Torvalds 		srb = dcb->active_srb;
16151da177e4SLinus Torvalds 		if (dcb->flag & ABORT_DEV_) {
16161da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
16171da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
16181da177e4SLinus Torvalds 		}
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 		/* software sequential machine */
16211da177e4SLinus Torvalds 		phase = (u16)srb->scsi_phase;
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 		/*
16241da177e4SLinus Torvalds 		 * 62037 or 62137
16251da177e4SLinus Torvalds 		 * call  dc395x_scsi_phase0[]... "phase entry"
16261da177e4SLinus Torvalds 		 * handle every phase before start transfer
16271da177e4SLinus Torvalds 		 */
16281da177e4SLinus Torvalds 		/* data_out_phase0,	phase:0 */
16291da177e4SLinus Torvalds 		/* data_in_phase0,	phase:1 */
16301da177e4SLinus Torvalds 		/* command_phase0,	phase:2 */
16311da177e4SLinus Torvalds 		/* status_phase0,	phase:3 */
16321da177e4SLinus Torvalds 		/* nop0,		phase:4 PH_BUS_FREE .. initial phase */
16331da177e4SLinus Torvalds 		/* nop0,		phase:5 PH_BUS_FREE .. initial phase */
16341da177e4SLinus Torvalds 		/* msgout_phase0,	phase:6 */
16351da177e4SLinus Torvalds 		/* msgin_phase0,	phase:7 */
16361da177e4SLinus Torvalds 		dc395x_statev = dc395x_scsi_phase0[phase];
16371da177e4SLinus Torvalds 		dc395x_statev(acb, srb, &scsi_status);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 		/*
164025985edcSLucas De Marchi 		 * if there were any exception occurred scsi_status
16411da177e4SLinus Torvalds 		 * will be modify to bus free phase new scsi_status
16421da177e4SLinus Torvalds 		 * transfer out from ... previous dc395x_statev
16431da177e4SLinus Torvalds 		 */
16441da177e4SLinus Torvalds 		srb->scsi_phase = scsi_status & PHASEMASK;
16451da177e4SLinus Torvalds 		phase = (u16)scsi_status & PHASEMASK;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 		/*
16481da177e4SLinus Torvalds 		 * call  dc395x_scsi_phase1[]... "phase entry" handle
16491da177e4SLinus Torvalds 		 * every phase to do transfer
16501da177e4SLinus Torvalds 		 */
16511da177e4SLinus Torvalds 		/* data_out_phase1,	phase:0 */
16521da177e4SLinus Torvalds 		/* data_in_phase1,	phase:1 */
16531da177e4SLinus Torvalds 		/* command_phase1,	phase:2 */
16541da177e4SLinus Torvalds 		/* status_phase1,	phase:3 */
16551da177e4SLinus Torvalds 		/* nop1,		phase:4 PH_BUS_FREE .. initial phase */
16561da177e4SLinus Torvalds 		/* nop1,		phase:5 PH_BUS_FREE .. initial phase */
16571da177e4SLinus Torvalds 		/* msgout_phase1,	phase:6 */
16581da177e4SLinus Torvalds 		/* msgin_phase1,	phase:7 */
16591da177e4SLinus Torvalds 		dc395x_statev = dc395x_scsi_phase1[phase];
16601da177e4SLinus Torvalds 		dc395x_statev(acb, srb, &scsi_status);
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds       out_unlock:
16631da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
16641da177e4SLinus Torvalds }
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 
16677d12e780SDavid Howells static irqreturn_t dc395x_interrupt(int irq, void *dev_id)
16681da177e4SLinus Torvalds {
1669c7bec5abSJeff Garzik 	struct AdapterCtlBlk *acb = dev_id;
16701da177e4SLinus Torvalds 	u16 scsi_status;
16711da177e4SLinus Torvalds 	u8 dma_status;
16721da177e4SLinus Torvalds 	irqreturn_t handled = IRQ_NONE;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	/*
16753a4fa0a2SRobert P. J. Day 	 * Check for pending interrupt
16761da177e4SLinus Torvalds 	 */
16771da177e4SLinus Torvalds 	scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
16781da177e4SLinus Torvalds 	dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
16791da177e4SLinus Torvalds 	if (scsi_status & SCSIINTERRUPT) {
16803a4fa0a2SRobert P. J. Day 		/* interrupt pending - let's process it! */
16811da177e4SLinus Torvalds 		dc395x_handle_interrupt(acb, scsi_status);
16821da177e4SLinus Torvalds 		handled = IRQ_HANDLED;
16831da177e4SLinus Torvalds 	}
16841da177e4SLinus Torvalds 	else if (dma_status & 0x20) {
16851da177e4SLinus Torvalds 		/* Error from the DMA engine */
16861da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
16871da177e4SLinus Torvalds #if 0
16881da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
16891da177e4SLinus Torvalds 		if (acb->active_dcb) {
16901da177e4SLinus Torvalds 			acb->active_dcb-> flag |= ABORT_DEV_;
16911da177e4SLinus Torvalds 			if (acb->active_dcb->active_srb)
16921da177e4SLinus Torvalds 				enable_msgout_abort(acb, acb->active_dcb->active_srb);
16931da177e4SLinus Torvalds 		}
16941da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
16951da177e4SLinus Torvalds #else
16961da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
16971da177e4SLinus Torvalds 		acb = NULL;
16981da177e4SLinus Torvalds #endif
16991da177e4SLinus Torvalds 		handled = IRQ_HANDLED;
17001da177e4SLinus Torvalds 	}
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	return handled;
17031da177e4SLinus Torvalds }
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
17071da177e4SLinus Torvalds 		u16 *pscsi_status)
17081da177e4SLinus Torvalds {
17095cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgout_phase0: (0x%p)\n", srb->cmd);
17101da177e4SLinus Torvalds 	if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
17111da177e4SLinus Torvalds 		*pscsi_status = PH_BUS_FREE;	/*.. initial phase */
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
17141da177e4SLinus Torvalds 	srb->state &= ~SRB_MSGOUT;
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
17191da177e4SLinus Torvalds 		u16 *pscsi_status)
17201da177e4SLinus Torvalds {
17211da177e4SLinus Torvalds 	u16 i;
17221da177e4SLinus Torvalds 	u8 *ptr;
17235cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgout_phase1: (0x%p)\n", srb->cmd);
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 	clear_fifo(acb, "msgout_phase1");
17261da177e4SLinus Torvalds 	if (!(srb->state & SRB_MSGOUT)) {
17271da177e4SLinus Torvalds 		srb->state |= SRB_MSGOUT;
17281da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
17295cd049a5SChristoph Hellwig 			"msgout_phase1: (0x%p) Phase unexpected\n",
17305cd049a5SChristoph Hellwig 			srb->cmd);	/* So what ? */
17311da177e4SLinus Torvalds 	}
17321da177e4SLinus Torvalds 	if (!srb->msg_count) {
17335cd049a5SChristoph Hellwig 		dprintkdbg(DBG_0, "msgout_phase1: (0x%p) NOP msg\n",
17345cd049a5SChristoph Hellwig 			srb->cmd);
17351da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
17361da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
17371da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
17381da177e4SLinus Torvalds 		return;
17391da177e4SLinus Torvalds 	}
17401da177e4SLinus Torvalds 	ptr = (u8 *)srb->msgout_buf;
17411da177e4SLinus Torvalds 	for (i = 0; i < srb->msg_count; i++)
17421da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
17431da177e4SLinus Torvalds 	srb->msg_count = 0;
17441da177e4SLinus Torvalds 	if (srb->msgout_buf[0] == MSG_ABORT)
17451da177e4SLinus Torvalds 		srb->state = SRB_ABORT_SENT;
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
17521da177e4SLinus Torvalds 		u16 *pscsi_status)
17531da177e4SLinus Torvalds {
17545cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "command_phase0: (0x%p)\n", srb->cmd);
17551da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
17561da177e4SLinus Torvalds }
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
17601da177e4SLinus Torvalds 		u16 *pscsi_status)
17611da177e4SLinus Torvalds {
17621da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
17631da177e4SLinus Torvalds 	u8 *ptr;
17641da177e4SLinus Torvalds 	u16 i;
17655cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "command_phase1: (0x%p)\n", srb->cmd);
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	clear_fifo(acb, "command_phase1");
17681da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
17691da177e4SLinus Torvalds 	if (!(srb->flag & AUTO_REQSENSE)) {
17701da177e4SLinus Torvalds 		ptr = (u8 *)srb->cmd->cmnd;
17711da177e4SLinus Torvalds 		for (i = 0; i < srb->cmd->cmd_len; i++) {
17721da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
17731da177e4SLinus Torvalds 			ptr++;
17741da177e4SLinus Torvalds 		}
17751da177e4SLinus Torvalds 	} else {
17761da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
17771da177e4SLinus Torvalds 		dcb = acb->active_dcb;
17781da177e4SLinus Torvalds 		/* target id */
17791da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
17801da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
17811da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1782b80ca4f7SFUJITA Tomonori 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
17831da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
17841da177e4SLinus Torvalds 	}
17851da177e4SLinus Torvalds 	srb->state |= SRB_COMMAND;
17861da177e4SLinus Torvalds 	/* it's important for atn stop */
17871da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
17881da177e4SLinus Torvalds 	/* SCSI command */
17891da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
17901da177e4SLinus Torvalds }
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds /*
17941da177e4SLinus Torvalds  * Verify that the remaining space in the hw sg lists is the same as
17951da177e4SLinus Torvalds  * the count of remaining bytes in srb->total_xfer_length
17961da177e4SLinus Torvalds  */
17971da177e4SLinus Torvalds static void sg_verify_length(struct ScsiReqBlk *srb)
17981da177e4SLinus Torvalds {
17991da177e4SLinus Torvalds 	if (debug_enabled(DBG_SG)) {
18001da177e4SLinus Torvalds 		unsigned len = 0;
18011da177e4SLinus Torvalds 		unsigned idx = srb->sg_index;
18021da177e4SLinus Torvalds 		struct SGentry *psge = srb->segment_x + idx;
18031da177e4SLinus Torvalds 		for (; idx < srb->sg_count; psge++, idx++)
18041da177e4SLinus Torvalds 			len += psge->length;
18051da177e4SLinus Torvalds 		if (len != srb->total_xfer_length)
18061da177e4SLinus Torvalds 			dprintkdbg(DBG_SG,
18071da177e4SLinus Torvalds 			       "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
18081da177e4SLinus Torvalds 			       srb->total_xfer_length, len);
18091da177e4SLinus Torvalds 	}
18101da177e4SLinus Torvalds }
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds /*
18141da177e4SLinus Torvalds  * Compute the next Scatter Gather list index and adjust its length
1815cdb8c2a6SGuennadi Liakhovetski  * and address if necessary
18161da177e4SLinus Torvalds  */
18171da177e4SLinus Torvalds static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
18181da177e4SLinus Torvalds {
18191da177e4SLinus Torvalds 	u8 idx;
182025985edcSLucas De Marchi 	u32 xferred = srb->total_xfer_length - left; /* bytes transferred */
18211da177e4SLinus Torvalds 	struct SGentry *psge = srb->segment_x + srb->sg_index;
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
182425985edcSLucas De Marchi 		"sg_update_list: Transferred %i of %i bytes, %i remain\n",
18251da177e4SLinus Torvalds 		xferred, srb->total_xfer_length, left);
18261da177e4SLinus Torvalds 	if (xferred == 0) {
18271da177e4SLinus Torvalds 		/* nothing to update since we did not transfer any data */
18281da177e4SLinus Torvalds 		return;
18291da177e4SLinus Torvalds 	}
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	sg_verify_length(srb);
18321da177e4SLinus Torvalds 	srb->total_xfer_length = left;	/* update remaining count */
18331da177e4SLinus Torvalds 	for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
18341da177e4SLinus Torvalds 		if (xferred >= psge->length) {
18351da177e4SLinus Torvalds 			/* Complete SG entries done */
18361da177e4SLinus Torvalds 			xferred -= psge->length;
18371da177e4SLinus Torvalds 		} else {
18381da177e4SLinus Torvalds 			/* Partial SG entry done */
1839dfda5e21SChristoph Hellwig 			dma_sync_single_for_cpu(&srb->dcb->acb->dev->dev,
1840dfda5e21SChristoph Hellwig 					srb->sg_bus_addr, SEGMENTX_LEN,
1841dfda5e21SChristoph Hellwig 					DMA_TO_DEVICE);
18421da177e4SLinus Torvalds 			psge->length -= xferred;
18431da177e4SLinus Torvalds 			psge->address += xferred;
18441da177e4SLinus Torvalds 			srb->sg_index = idx;
1845dfda5e21SChristoph Hellwig 			dma_sync_single_for_device(&srb->dcb->acb->dev->dev,
1846dfda5e21SChristoph Hellwig 					srb->sg_bus_addr, SEGMENTX_LEN,
1847dfda5e21SChristoph Hellwig 					DMA_TO_DEVICE);
18481da177e4SLinus Torvalds 			break;
18491da177e4SLinus Torvalds 		}
18501da177e4SLinus Torvalds 		psge++;
18511da177e4SLinus Torvalds 	}
18521da177e4SLinus Torvalds 	sg_verify_length(srb);
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds /*
185725985edcSLucas De Marchi  * We have transferred a single byte (PIO mode?) and need to update
18581da177e4SLinus Torvalds  * the count of bytes remaining (total_xfer_length) and update the sg
18591da177e4SLinus Torvalds  * entry to either point to next byte in the current sg entry, or of
18601da177e4SLinus Torvalds  * already at the end to point to the start of the next sg entry
18611da177e4SLinus Torvalds  */
18621da177e4SLinus Torvalds static void sg_subtract_one(struct ScsiReqBlk *srb)
18631da177e4SLinus Torvalds {
1864cdb8c2a6SGuennadi Liakhovetski 	sg_update_list(srb, srb->total_xfer_length - 1);
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds /*
18691da177e4SLinus Torvalds  * cleanup_after_transfer
18701da177e4SLinus Torvalds  *
18711da177e4SLinus Torvalds  * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
18721da177e4SLinus Torvalds  * KG: Currently called from  StatusPhase1 ()
18731da177e4SLinus Torvalds  * Should probably also be called from other places
18741da177e4SLinus Torvalds  * Best might be to call it in DataXXPhase0, if new phase will differ
18751da177e4SLinus Torvalds  */
18761da177e4SLinus Torvalds static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
18771da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
18781da177e4SLinus Torvalds {
18791da177e4SLinus Torvalds 	/*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
18801da177e4SLinus Torvalds 	if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) {	/* read */
18811da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
18821da177e4SLinus Torvalds 			clear_fifo(acb, "cleanup/in");
18831da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
18841da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
18851da177e4SLinus Torvalds 	} else {		/* write */
18861da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
18871da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
18881da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
18891da177e4SLinus Torvalds 			clear_fifo(acb, "cleanup/out");
18901da177e4SLinus Torvalds 	}
18911da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds /*
189625985edcSLucas De Marchi  * Those no of bytes will be transferred w/ PIO through the SCSI FIFO
18971da177e4SLinus Torvalds  * Seems to be needed for unknown reasons; could be a hardware bug :-(
18981da177e4SLinus Torvalds  */
18991da177e4SLinus Torvalds #define DC395x_LASTPIO 4
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
19031da177e4SLinus Torvalds 		u16 *pscsi_status)
19041da177e4SLinus Torvalds {
19051da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
19061da177e4SLinus Torvalds 	u16 scsi_status = *pscsi_status;
19071da177e4SLinus Torvalds 	u32 d_left_counter = 0;
19085cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_out_phase0: (0x%p) <%02i-%i>\n",
19099cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	/*
19121da177e4SLinus Torvalds 	 * KG: We need to drain the buffers before we draw any conclusions!
19131da177e4SLinus Torvalds 	 * This means telling the DMA to push the rest into SCSI, telling
19141da177e4SLinus Torvalds 	 * SCSI to push the rest to the bus.
19151da177e4SLinus Torvalds 	 * However, the device might have been the one to stop us (phase
19161da177e4SLinus Torvalds 	 * change), and the data in transit just needs to be accounted so
19171da177e4SLinus Torvalds 	 * it can be retransmitted.)
19181da177e4SLinus Torvalds 	 */
19191da177e4SLinus Torvalds 	/*
19201da177e4SLinus Torvalds 	 * KG: Stop DMA engine pushing more data into the SCSI FIFO
19211da177e4SLinus Torvalds 	 * If we need more data, the DMA SG list will be freshly set up, anyway
19221da177e4SLinus Torvalds 	 */
19231da177e4SLinus Torvalds 	dprintkdbg(DBG_PIO, "data_out_phase0: "
1924cdb8c2a6SGuennadi Liakhovetski 		"DMA{fifocnt=0x%02x fifostat=0x%02x} "
19251da177e4SLinus Torvalds 		"SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
19261da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
19271da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
19281da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
19291da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
19301da177e4SLinus Torvalds 		srb->total_xfer_length);
19311da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	if (!(srb->state & SRB_XFERPAD)) {
19341da177e4SLinus Torvalds 		if (scsi_status & PARITYERROR)
19351da177e4SLinus Torvalds 			srb->status |= PARITY_ERROR;
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 		/*
19381da177e4SLinus Torvalds 		 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
19391da177e4SLinus Torvalds 		 * is the no of bytes it got from the DMA engine not the no it
19401da177e4SLinus Torvalds 		 * transferred successfully to the device. (And the difference could
19411da177e4SLinus Torvalds 		 * be as much as the FIFO size, I guess ...)
19421da177e4SLinus Torvalds 		 */
19431da177e4SLinus Torvalds 		if (!(scsi_status & SCSIXFERDONE)) {
19441da177e4SLinus Torvalds 			/*
19451da177e4SLinus Torvalds 			 * when data transfer from DMA FIFO to SCSI FIFO
19461da177e4SLinus Torvalds 			 * if there was some data left in SCSI FIFO
19471da177e4SLinus Torvalds 			 */
19481da177e4SLinus Torvalds 			d_left_counter =
19491da177e4SLinus Torvalds 			    (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
19501da177e4SLinus Torvalds 				  0x1F);
19511da177e4SLinus Torvalds 			if (dcb->sync_period & WIDE_SYNC)
19521da177e4SLinus Torvalds 				d_left_counter <<= 1;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 			dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
19551da177e4SLinus Torvalds 				"SCSI{fifocnt=0x%02x cnt=0x%08x} "
19561da177e4SLinus Torvalds 				"DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
19571da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
19581da177e4SLinus Torvalds 				(dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
19591da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
19601da177e4SLinus Torvalds 				DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
19611da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
19621da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
19631da177e4SLinus Torvalds 				DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
19641da177e4SLinus Torvalds 		}
19651da177e4SLinus Torvalds 		/*
1966b4b08e58SLinus Torvalds 		 * calculate all the residue data that not yet tranfered
19671da177e4SLinus Torvalds 		 * SCSI transfer counter + left in SCSI FIFO data
19681da177e4SLinus Torvalds 		 *
19691da177e4SLinus Torvalds 		 * .....TRM_S1040_SCSI_COUNTER (24bits)
19701da177e4SLinus Torvalds 		 * The counter always decrement by one for every SCSI byte transfer.
19711da177e4SLinus Torvalds 		 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
19721da177e4SLinus Torvalds 		 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
19731da177e4SLinus Torvalds 		 */
19741da177e4SLinus Torvalds 		if (srb->total_xfer_length > DC395x_LASTPIO)
19751da177e4SLinus Torvalds 			d_left_counter +=
19761da177e4SLinus Torvalds 			    DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 		/* Is this a good idea? */
19791da177e4SLinus Torvalds 		/*clear_fifo(acb, "DOP1"); */
19801da177e4SLinus Torvalds 		/* KG: What is this supposed to be useful for? WIDE padding stuff? */
19811da177e4SLinus Torvalds 		if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
1982a862ea31SFUJITA Tomonori 		    && scsi_bufflen(srb->cmd) % 2) {
19831da177e4SLinus Torvalds 			d_left_counter = 0;
19841da177e4SLinus Torvalds 			dprintkl(KERN_INFO,
19851da177e4SLinus Torvalds 				"data_out_phase0: Discard 1 byte (0x%02x)\n",
19861da177e4SLinus Torvalds 				scsi_status);
19871da177e4SLinus Torvalds 		}
19881da177e4SLinus Torvalds 		/*
19891da177e4SLinus Torvalds 		 * KG: Oops again. Same thinko as above: The SCSI might have been
19901da177e4SLinus Torvalds 		 * faster than the DMA engine, so that it ran out of data.
19911da177e4SLinus Torvalds 		 * In that case, we have to do just nothing!
19921da177e4SLinus Torvalds 		 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
19931da177e4SLinus Torvalds 		 */
19941da177e4SLinus Torvalds 		/*
19951da177e4SLinus Torvalds 		 * KG: This is nonsense: We have been WRITING data to the bus
19961da177e4SLinus Torvalds 		 * If the SCSI engine has no bytes left, how should the DMA engine?
19971da177e4SLinus Torvalds 		 */
19981da177e4SLinus Torvalds 		if (d_left_counter == 0) {
19991da177e4SLinus Torvalds 			srb->total_xfer_length = 0;
20001da177e4SLinus Torvalds 		} else {
20011da177e4SLinus Torvalds 			/*
20021da177e4SLinus Torvalds 			 * if transfer not yet complete
20031da177e4SLinus Torvalds 			 * there were some data residue in SCSI FIFO or
20041da177e4SLinus Torvalds 			 * SCSI transfer counter not empty
20051da177e4SLinus Torvalds 			 */
20061da177e4SLinus Torvalds 			long oldxferred =
20071da177e4SLinus Torvalds 			    srb->total_xfer_length - d_left_counter;
20081da177e4SLinus Torvalds 			const int diff =
20091da177e4SLinus Torvalds 			    (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
20101da177e4SLinus Torvalds 			sg_update_list(srb, d_left_counter);
20111da177e4SLinus Torvalds 			/* KG: Most ugly hack! Apparently, this works around a chip bug */
20121da177e4SLinus Torvalds 			if ((srb->segment_x[srb->sg_index].length ==
2013a862ea31SFUJITA Tomonori 			     diff && scsi_sg_count(srb->cmd))
20141da177e4SLinus Torvalds 			    || ((oldxferred & ~PAGE_MASK) ==
20151da177e4SLinus Torvalds 				(PAGE_SIZE - diff))
20161da177e4SLinus Torvalds 			    ) {
20171da177e4SLinus Torvalds 				dprintkl(KERN_INFO, "data_out_phase0: "
20181da177e4SLinus Torvalds 					"Work around chip bug (%i)?\n", diff);
20191da177e4SLinus Torvalds 				d_left_counter =
20201da177e4SLinus Torvalds 				    srb->total_xfer_length - diff;
20211da177e4SLinus Torvalds 				sg_update_list(srb, d_left_counter);
20221da177e4SLinus Torvalds 				/*srb->total_xfer_length -= diff; */
20231da177e4SLinus Torvalds 				/*srb->virt_addr += diff; */
20241da177e4SLinus Torvalds 				/*if (srb->cmd->use_sg) */
20251da177e4SLinus Torvalds 				/*      srb->sg_index++; */
20261da177e4SLinus Torvalds 			}
20271da177e4SLinus Torvalds 		}
20281da177e4SLinus Torvalds 	}
20291da177e4SLinus Torvalds 	if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
20301da177e4SLinus Torvalds 		cleanup_after_transfer(acb, srb);
20311da177e4SLinus Torvalds 	}
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
20361da177e4SLinus Torvalds 		u16 *pscsi_status)
20371da177e4SLinus Torvalds {
20385cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_out_phase1: (0x%p) <%02i-%i>\n",
20399cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
20401da177e4SLinus Torvalds 	clear_fifo(acb, "data_out_phase1");
20411da177e4SLinus Torvalds 	/* do prepare before transfer when data out phase */
20421da177e4SLinus Torvalds 	data_io_transfer(acb, srb, XFERDATAOUT);
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
20461da177e4SLinus Torvalds 		u16 *pscsi_status)
20471da177e4SLinus Torvalds {
20481da177e4SLinus Torvalds 	u16 scsi_status = *pscsi_status;
2049cdb8c2a6SGuennadi Liakhovetski 
20505cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_in_phase0: (0x%p) <%02i-%i>\n",
20519cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	/*
20541da177e4SLinus Torvalds 	 * KG: DataIn is much more tricky than DataOut. When the device is finished
20551da177e4SLinus Torvalds 	 * and switches to another phase, the SCSI engine should be finished too.
20561da177e4SLinus Torvalds 	 * But: There might still be bytes left in its FIFO to be fetched by the DMA
20571da177e4SLinus Torvalds 	 * engine and transferred to memory.
20581da177e4SLinus Torvalds 	 * We should wait for the FIFOs to be emptied by that (is there any way to
20591da177e4SLinus Torvalds 	 * enforce this?) and then stop the DMA engine, because it might think, that
20601da177e4SLinus Torvalds 	 * there are more bytes to follow. Yes, the device might disconnect prior to
20611da177e4SLinus Torvalds 	 * having all bytes transferred!
20621da177e4SLinus Torvalds 	 * Also we should make sure that all data from the DMA engine buffer's really
20631da177e4SLinus Torvalds 	 * made its way to the system memory! Some documentation on this would not
20641da177e4SLinus Torvalds 	 * seem to be a bad idea, actually.
20651da177e4SLinus Torvalds 	 */
20661da177e4SLinus Torvalds 	if (!(srb->state & SRB_XFERPAD)) {
2067cdb8c2a6SGuennadi Liakhovetski 		u32 d_left_counter;
2068cdb8c2a6SGuennadi Liakhovetski 		unsigned int sc, fc;
2069cdb8c2a6SGuennadi Liakhovetski 
20701da177e4SLinus Torvalds 		if (scsi_status & PARITYERROR) {
20715cd049a5SChristoph Hellwig 			dprintkl(KERN_INFO, "data_in_phase0: (0x%p) "
20725cd049a5SChristoph Hellwig 				"Parity Error\n", srb->cmd);
20731da177e4SLinus Torvalds 			srb->status |= PARITY_ERROR;
20741da177e4SLinus Torvalds 		}
20751da177e4SLinus Torvalds 		/*
20761da177e4SLinus Torvalds 		 * KG: We should wait for the DMA FIFO to be empty ...
20771da177e4SLinus Torvalds 		 * but: it would be better to wait first for the SCSI FIFO and then the
20781da177e4SLinus Torvalds 		 * the DMA FIFO to become empty? How do we know, that the device not already
20791da177e4SLinus Torvalds 		 * sent data to the FIFO in a MsgIn phase, eg.?
20801da177e4SLinus Torvalds 		 */
20811da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
20821da177e4SLinus Torvalds #if 0
20831da177e4SLinus Torvalds 			int ctr = 6000000;
20841da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
20851da177e4SLinus Torvalds 				"DIP0: Wait for DMA FIFO to flush ...\n");
20861da177e4SLinus Torvalds 			/*DC395x_write8  (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
20871da177e4SLinus Torvalds 			/*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
20881da177e4SLinus Torvalds 			/*DC395x_write8  (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
20891da177e4SLinus Torvalds 			while (!
20901da177e4SLinus Torvalds 			       (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
20911da177e4SLinus Torvalds 				0x80) && --ctr);
20921da177e4SLinus Torvalds 			if (ctr < 6000000 - 1)
20931da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG
20941da177e4SLinus Torvalds 				       "DIP0: Had to wait for DMA ...\n");
20951da177e4SLinus Torvalds 			if (!ctr)
20961da177e4SLinus Torvalds 				dprintkl(KERN_ERR,
20971da177e4SLinus Torvalds 				       "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
20981da177e4SLinus Torvalds 			/*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
20991da177e4SLinus Torvalds #endif
21001da177e4SLinus Torvalds 			dprintkdbg(DBG_KG, "data_in_phase0: "
21011da177e4SLinus Torvalds 				"DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
21021da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
21031da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
21041da177e4SLinus Torvalds 		}
21051da177e4SLinus Torvalds 		/* Now: Check remainig data: The SCSI counters should tell us ... */
2106cdb8c2a6SGuennadi Liakhovetski 		sc = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2107cdb8c2a6SGuennadi Liakhovetski 		fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2108cdb8c2a6SGuennadi Liakhovetski 		d_left_counter = sc + ((fc & 0x1f)
21091da177e4SLinus Torvalds 		       << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
21101da177e4SLinus Torvalds 			   0));
21111da177e4SLinus Torvalds 		dprintkdbg(DBG_KG, "data_in_phase0: "
21121da177e4SLinus Torvalds 			"SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
21131da177e4SLinus Torvalds 			"DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
21141da177e4SLinus Torvalds 			"Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2115cdb8c2a6SGuennadi Liakhovetski 			fc,
21161da177e4SLinus Torvalds 			(srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2117cdb8c2a6SGuennadi Liakhovetski 			sc,
2118cdb8c2a6SGuennadi Liakhovetski 			fc,
21191da177e4SLinus Torvalds 			DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
21201da177e4SLinus Torvalds 			DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
21211da177e4SLinus Torvalds 			srb->total_xfer_length, d_left_counter);
21221da177e4SLinus Torvalds #if DC395x_LASTPIO
212325985edcSLucas De Marchi 		/* KG: Less than or equal to 4 bytes can not be transferred via DMA, it seems. */
21241da177e4SLinus Torvalds 		if (d_left_counter
21251da177e4SLinus Torvalds 		    && srb->total_xfer_length <= DC395x_LASTPIO) {
2126cdb8c2a6SGuennadi Liakhovetski 			size_t left_io = srb->total_xfer_length;
2127cdb8c2a6SGuennadi Liakhovetski 
21281da177e4SLinus Torvalds 			/*u32 addr = (srb->segment_x[srb->sg_index].address); */
21291da177e4SLinus Torvalds 			/*sg_update_list (srb, d_left_counter); */
2130cdb8c2a6SGuennadi Liakhovetski 			dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) "
2131cdb8c2a6SGuennadi Liakhovetski 				   "for remaining %i bytes:",
2132cdb8c2a6SGuennadi Liakhovetski 				fc & 0x1f,
21331da177e4SLinus Torvalds 				(srb->dcb->sync_period & WIDE_SYNC) ?
21341da177e4SLinus Torvalds 				    "words" : "bytes",
21351da177e4SLinus Torvalds 				srb->total_xfer_length);
21361da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC)
21371da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
21381da177e4SLinus Torvalds 					      CFG2_WIDEFIFO);
2139cdb8c2a6SGuennadi Liakhovetski 			while (left_io) {
2140cdb8c2a6SGuennadi Liakhovetski 				unsigned char *virt, *base = NULL;
2141cdb8c2a6SGuennadi Liakhovetski 				unsigned long flags = 0;
2142cdb8c2a6SGuennadi Liakhovetski 				size_t len = left_io;
2143cdb8c2a6SGuennadi Liakhovetski 				size_t offset = srb->request_length - left_io;
2144a862ea31SFUJITA Tomonori 
2145cdb8c2a6SGuennadi Liakhovetski 				local_irq_save(flags);
2146cdb8c2a6SGuennadi Liakhovetski 				/* Assumption: it's inside one page as it's at most 4 bytes and
2147cdb8c2a6SGuennadi Liakhovetski 				   I just assume it's on a 4-byte boundary */
2148a862ea31SFUJITA Tomonori 				base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2149cdb8c2a6SGuennadi Liakhovetski 							   srb->sg_count, &offset, &len);
2150cdb8c2a6SGuennadi Liakhovetski 				virt = base + offset;
2151a862ea31SFUJITA Tomonori 
2152cdb8c2a6SGuennadi Liakhovetski 				left_io -= len;
2153cdb8c2a6SGuennadi Liakhovetski 
2154cdb8c2a6SGuennadi Liakhovetski 				while (len) {
2155cdb8c2a6SGuennadi Liakhovetski 					u8 byte;
2156cdb8c2a6SGuennadi Liakhovetski 					byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2157cdb8c2a6SGuennadi Liakhovetski 					*virt++ = byte;
2158cdb8c2a6SGuennadi Liakhovetski 
21591da177e4SLinus Torvalds 					if (debug_enabled(DBG_PIO))
21601da177e4SLinus Torvalds 						printk(" %02x", byte);
2161cdb8c2a6SGuennadi Liakhovetski 
21621da177e4SLinus Torvalds 					d_left_counter--;
21631da177e4SLinus Torvalds 					sg_subtract_one(srb);
2164cdb8c2a6SGuennadi Liakhovetski 
2165cdb8c2a6SGuennadi Liakhovetski 					len--;
2166cdb8c2a6SGuennadi Liakhovetski 
2167cdb8c2a6SGuennadi Liakhovetski 					fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2168cdb8c2a6SGuennadi Liakhovetski 
2169cdb8c2a6SGuennadi Liakhovetski 					if (fc == 0x40) {
2170cdb8c2a6SGuennadi Liakhovetski 						left_io = 0;
2171cdb8c2a6SGuennadi Liakhovetski 						break;
21721da177e4SLinus Torvalds 					}
2173cdb8c2a6SGuennadi Liakhovetski 				}
2174cdb8c2a6SGuennadi Liakhovetski 
2175cdb8c2a6SGuennadi Liakhovetski 				WARN_ON((fc != 0x40) == !d_left_counter);
2176cdb8c2a6SGuennadi Liakhovetski 
2177cdb8c2a6SGuennadi Liakhovetski 				if (fc == 0x40 && (srb->dcb->sync_period & WIDE_SYNC)) {
21781da177e4SLinus Torvalds 					/* Read the last byte ... */
21791da177e4SLinus Torvalds 					if (srb->total_xfer_length > 0) {
21801da177e4SLinus Torvalds 						u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2181cdb8c2a6SGuennadi Liakhovetski 
2182cdb8c2a6SGuennadi Liakhovetski 						*virt++ = byte;
21831da177e4SLinus Torvalds 						srb->total_xfer_length--;
21841da177e4SLinus Torvalds 						if (debug_enabled(DBG_PIO))
21851da177e4SLinus Torvalds 							printk(" %02x", byte);
21861da177e4SLinus Torvalds 					}
2187cdb8c2a6SGuennadi Liakhovetski 
21881da177e4SLinus Torvalds 					DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
21891da177e4SLinus Torvalds 				}
2190cdb8c2a6SGuennadi Liakhovetski 
2191cdb8c2a6SGuennadi Liakhovetski 				scsi_kunmap_atomic_sg(base);
2192cdb8c2a6SGuennadi Liakhovetski 				local_irq_restore(flags);
2193cdb8c2a6SGuennadi Liakhovetski 			}
21941da177e4SLinus Torvalds 			/*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
21951da177e4SLinus Torvalds 			/*srb->total_xfer_length = 0; */
21961da177e4SLinus Torvalds 			if (debug_enabled(DBG_PIO))
21971da177e4SLinus Torvalds 				printk("\n");
21981da177e4SLinus Torvalds 		}
21991da177e4SLinus Torvalds #endif				/* DC395x_LASTPIO */
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds #if 0
22021da177e4SLinus Torvalds 		/*
22031da177e4SLinus Torvalds 		 * KG: This was in DATAOUT. Does it also belong here?
22041da177e4SLinus Torvalds 		 * Nobody seems to know what counter and fifo_cnt count exactly ...
22051da177e4SLinus Torvalds 		 */
22061da177e4SLinus Torvalds 		if (!(scsi_status & SCSIXFERDONE)) {
22071da177e4SLinus Torvalds 			/*
22081da177e4SLinus Torvalds 			 * when data transfer from DMA FIFO to SCSI FIFO
22091da177e4SLinus Torvalds 			 * if there was some data left in SCSI FIFO
22101da177e4SLinus Torvalds 			 */
22111da177e4SLinus Torvalds 			d_left_counter =
22121da177e4SLinus Torvalds 			    (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
22131da177e4SLinus Torvalds 				  0x1F);
22141da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC)
22151da177e4SLinus Torvalds 				d_left_counter <<= 1;
22161da177e4SLinus Torvalds 			/*
22171da177e4SLinus Torvalds 			 * if WIDE scsi SCSI FIFOCNT unit is word !!!
22181da177e4SLinus Torvalds 			 * so need to *= 2
22191da177e4SLinus Torvalds 			 * KG: Seems to be correct ...
22201da177e4SLinus Torvalds 			 */
22211da177e4SLinus Torvalds 		}
22221da177e4SLinus Torvalds #endif
22231da177e4SLinus Torvalds 		/* KG: This should not be needed any more! */
22241da177e4SLinus Torvalds 		if (d_left_counter == 0
22251da177e4SLinus Torvalds 		    || (scsi_status & SCSIXFERCNT_2_ZERO)) {
22261da177e4SLinus Torvalds #if 0
22271da177e4SLinus Torvalds 			int ctr = 6000000;
22281da177e4SLinus Torvalds 			u8 TempDMAstatus;
22291da177e4SLinus Torvalds 			do {
22301da177e4SLinus Torvalds 				TempDMAstatus =
22311da177e4SLinus Torvalds 				    DC395x_read8(acb, TRM_S1040_DMA_STATUS);
22321da177e4SLinus Torvalds 			} while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
22331da177e4SLinus Torvalds 			if (!ctr)
22341da177e4SLinus Torvalds 				dprintkl(KERN_ERR,
22351da177e4SLinus Torvalds 				       "Deadlock in DataInPhase0 waiting for DMA!!\n");
22361da177e4SLinus Torvalds 			srb->total_xfer_length = 0;
22371da177e4SLinus Torvalds #endif
22381da177e4SLinus Torvalds 			srb->total_xfer_length = d_left_counter;
22391da177e4SLinus Torvalds 		} else {	/* phase changed */
22401da177e4SLinus Torvalds 			/*
22411da177e4SLinus Torvalds 			 * parsing the case:
22421da177e4SLinus Torvalds 			 * when a transfer not yet complete
22431da177e4SLinus Torvalds 			 * but be disconnected by target
22441da177e4SLinus Torvalds 			 * if transfer not yet complete
22451da177e4SLinus Torvalds 			 * there were some data residue in SCSI FIFO or
22461da177e4SLinus Torvalds 			 * SCSI transfer counter not empty
22471da177e4SLinus Torvalds 			 */
22481da177e4SLinus Torvalds 			sg_update_list(srb, d_left_counter);
22491da177e4SLinus Torvalds 		}
22501da177e4SLinus Torvalds 	}
22511da177e4SLinus Torvalds 	/* KG: The target may decide to disconnect: Empty FIFO before! */
22521da177e4SLinus Torvalds 	if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
22531da177e4SLinus Torvalds 		cleanup_after_transfer(acb, srb);
22541da177e4SLinus Torvalds 	}
22551da177e4SLinus Torvalds }
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
22591da177e4SLinus Torvalds 		u16 *pscsi_status)
22601da177e4SLinus Torvalds {
22615cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_in_phase1: (0x%p) <%02i-%i>\n",
22629cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
22631da177e4SLinus Torvalds 	data_io_transfer(acb, srb, XFERDATAIN);
22641da177e4SLinus Torvalds }
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds static void data_io_transfer(struct AdapterCtlBlk *acb,
22681da177e4SLinus Torvalds 		struct ScsiReqBlk *srb, u16 io_dir)
22691da177e4SLinus Torvalds {
22701da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
22711da177e4SLinus Torvalds 	u8 bval;
22721da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
22735cd049a5SChristoph Hellwig 		"data_io_transfer: (0x%p) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
22749cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun,
22751da177e4SLinus Torvalds 		((io_dir & DMACMD_DIR) ? 'r' : 'w'),
22761da177e4SLinus Torvalds 		srb->total_xfer_length, srb->sg_index, srb->sg_count);
22771da177e4SLinus Torvalds 	if (srb == acb->tmp_srb)
22781da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
22791da177e4SLinus Torvalds 	if (srb->sg_index >= srb->sg_count) {
22801da177e4SLinus Torvalds 		/* can't happen? out of bounds error */
22811da177e4SLinus Torvalds 		return;
22821da177e4SLinus Torvalds 	}
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds 	if (srb->total_xfer_length > DC395x_LASTPIO) {
22851da177e4SLinus Torvalds 		u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
22861da177e4SLinus Torvalds 		/*
22871da177e4SLinus Torvalds 		 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
22881da177e4SLinus Torvalds 		 * Maybe, even ABORTXFER would be appropriate
22891da177e4SLinus Torvalds 		 */
22901da177e4SLinus Torvalds 		if (dma_status & XFERPENDING) {
22911da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
22921da177e4SLinus Torvalds 				"Expect trouble!\n");
22931da177e4SLinus Torvalds 			dump_register_info(acb, dcb, srb);
22941da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
22951da177e4SLinus Torvalds 		}
22961da177e4SLinus Torvalds 		/* clear_fifo(acb, "IO"); */
22971da177e4SLinus Torvalds 		/*
22981da177e4SLinus Torvalds 		 * load what physical address of Scatter/Gather list table
22991da177e4SLinus Torvalds 		 * want to be transfer
23001da177e4SLinus Torvalds 		 */
23011da177e4SLinus Torvalds 		srb->state |= SRB_DATA_XFER;
23021da177e4SLinus Torvalds 		DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
2303a862ea31SFUJITA Tomonori 		if (scsi_sg_count(srb->cmd)) {	/* with S/G */
23041da177e4SLinus Torvalds 			io_dir |= DMACMD_SG;
23051da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
23061da177e4SLinus Torvalds 				       srb->sg_bus_addr +
23071da177e4SLinus Torvalds 				       sizeof(struct SGentry) *
23081da177e4SLinus Torvalds 				       srb->sg_index);
23091da177e4SLinus Torvalds 			/* load how many bytes in the sg list table */
23101da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XCNT,
23111da177e4SLinus Torvalds 				       ((u32)(srb->sg_count -
23121da177e4SLinus Torvalds 					      srb->sg_index) << 3));
23131da177e4SLinus Torvalds 		} else {	/* without S/G */
23141da177e4SLinus Torvalds 			io_dir &= ~DMACMD_SG;
23151da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
23161da177e4SLinus Torvalds 				       srb->segment_x[0].address);
23171da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XCNT,
23181da177e4SLinus Torvalds 				       srb->segment_x[0].length);
23191da177e4SLinus Torvalds 		}
23201da177e4SLinus Torvalds 		/* load total transfer length (24bits) max value 16Mbyte */
23211da177e4SLinus Torvalds 		DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
23221da177e4SLinus Torvalds 			       srb->total_xfer_length);
23231da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
23241da177e4SLinus Torvalds 		if (io_dir & DMACMD_DIR) {	/* read */
23251da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
23261da177e4SLinus Torvalds 				      SCMD_DMA_IN);
23271da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
23281da177e4SLinus Torvalds 		} else {
23291da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
23301da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
23311da177e4SLinus Torvalds 				      SCMD_DMA_OUT);
23321da177e4SLinus Torvalds 		}
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	}
23351da177e4SLinus Torvalds #if DC395x_LASTPIO
23361da177e4SLinus Torvalds 	else if (srb->total_xfer_length > 0) {	/* The last four bytes: Do PIO */
23371da177e4SLinus Torvalds 		/*
23381da177e4SLinus Torvalds 		 * load what physical address of Scatter/Gather list table
23391da177e4SLinus Torvalds 		 * want to be transfer
23401da177e4SLinus Torvalds 		 */
23411da177e4SLinus Torvalds 		srb->state |= SRB_DATA_XFER;
23421da177e4SLinus Torvalds 		/* load total transfer length (24bits) max value 16Mbyte */
23431da177e4SLinus Torvalds 		DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
23441da177e4SLinus Torvalds 			       srb->total_xfer_length);
23451da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
23461da177e4SLinus Torvalds 		if (io_dir & DMACMD_DIR) {	/* read */
23471da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
23481da177e4SLinus Torvalds 				      SCMD_FIFO_IN);
23491da177e4SLinus Torvalds 		} else {	/* write */
23501da177e4SLinus Torvalds 			int ln = srb->total_xfer_length;
2351cdb8c2a6SGuennadi Liakhovetski 			size_t left_io = srb->total_xfer_length;
2352cdb8c2a6SGuennadi Liakhovetski 
23531da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC)
23541da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
23551da177e4SLinus Torvalds 				     CFG2_WIDEFIFO);
23561da177e4SLinus Torvalds 
2357cdb8c2a6SGuennadi Liakhovetski 			while (left_io) {
2358cdb8c2a6SGuennadi Liakhovetski 				unsigned char *virt, *base = NULL;
2359cdb8c2a6SGuennadi Liakhovetski 				unsigned long flags = 0;
2360cdb8c2a6SGuennadi Liakhovetski 				size_t len = left_io;
2361cdb8c2a6SGuennadi Liakhovetski 				size_t offset = srb->request_length - left_io;
2362a862ea31SFUJITA Tomonori 
2363cdb8c2a6SGuennadi Liakhovetski 				local_irq_save(flags);
2364cdb8c2a6SGuennadi Liakhovetski 				/* Again, max 4 bytes */
2365a862ea31SFUJITA Tomonori 				base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2366cdb8c2a6SGuennadi Liakhovetski 							   srb->sg_count, &offset, &len);
2367cdb8c2a6SGuennadi Liakhovetski 				virt = base + offset;
2368a862ea31SFUJITA Tomonori 
2369cdb8c2a6SGuennadi Liakhovetski 				left_io -= len;
2370cdb8c2a6SGuennadi Liakhovetski 
2371cdb8c2a6SGuennadi Liakhovetski 				while (len--) {
23721da177e4SLinus Torvalds 					if (debug_enabled(DBG_PIO))
2373cdb8c2a6SGuennadi Liakhovetski 						printk(" %02x", *virt);
23741da177e4SLinus Torvalds 
2375cdb8c2a6SGuennadi Liakhovetski 					DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *virt++);
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds 					sg_subtract_one(srb);
23781da177e4SLinus Torvalds 				}
2379cdb8c2a6SGuennadi Liakhovetski 
2380cdb8c2a6SGuennadi Liakhovetski 				scsi_kunmap_atomic_sg(base);
2381cdb8c2a6SGuennadi Liakhovetski 				local_irq_restore(flags);
2382cdb8c2a6SGuennadi Liakhovetski 			}
23831da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC) {
23841da177e4SLinus Torvalds 				if (ln % 2) {
23851da177e4SLinus Torvalds 					DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
23861da177e4SLinus Torvalds 					if (debug_enabled(DBG_PIO))
23871da177e4SLinus Torvalds 						printk(" |00");
23881da177e4SLinus Torvalds 				}
23891da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
23901da177e4SLinus Torvalds 			}
23911da177e4SLinus Torvalds 			/*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
23921da177e4SLinus Torvalds 			if (debug_enabled(DBG_PIO))
23931da177e4SLinus Torvalds 				printk("\n");
23941da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
23951da177e4SLinus Torvalds 					  SCMD_FIFO_OUT);
23961da177e4SLinus Torvalds 		}
23971da177e4SLinus Torvalds 	}
23981da177e4SLinus Torvalds #endif				/* DC395x_LASTPIO */
23991da177e4SLinus Torvalds 	else {		/* xfer pad */
24001da177e4SLinus Torvalds 		u8 data = 0, data2 = 0;
24011da177e4SLinus Torvalds 		if (srb->sg_count) {
24021da177e4SLinus Torvalds 			srb->adapter_status = H_OVER_UNDER_RUN;
24031da177e4SLinus Torvalds 			srb->status |= OVER_RUN;
24041da177e4SLinus Torvalds 		}
24051da177e4SLinus Torvalds 		/*
24061da177e4SLinus Torvalds 		 * KG: despite the fact that we are using 16 bits I/O ops
24071da177e4SLinus Torvalds 		 * the SCSI FIFO is only 8 bits according to the docs
24081da177e4SLinus Torvalds 		 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
24091da177e4SLinus Torvalds 		 */
24101da177e4SLinus Torvalds 		if (dcb->sync_period & WIDE_SYNC) {
24111da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
24121da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
24131da177e4SLinus Torvalds 				      CFG2_WIDEFIFO);
24141da177e4SLinus Torvalds 			if (io_dir & DMACMD_DIR) {
24151da177e4SLinus Torvalds 				data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
24161da177e4SLinus Torvalds 				data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
24171da177e4SLinus Torvalds 			} else {
24181da177e4SLinus Torvalds 				/* Danger, Robinson: If you find KGs
24191da177e4SLinus Torvalds 				 * scattered over the wide disk, the driver
24201da177e4SLinus Torvalds 				 * or chip is to blame :-( */
24211da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
24221da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
24231da177e4SLinus Torvalds 			}
24241da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
24251da177e4SLinus Torvalds 		} else {
24261da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
24271da177e4SLinus Torvalds 			/* Danger, Robinson: If you find a collection of Ks on your disk
24281da177e4SLinus Torvalds 			 * something broke :-( */
24291da177e4SLinus Torvalds 			if (io_dir & DMACMD_DIR)
24301da177e4SLinus Torvalds 				data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
24311da177e4SLinus Torvalds 			else
24321da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
24331da177e4SLinus Torvalds 		}
24341da177e4SLinus Torvalds 		srb->state |= SRB_XFERPAD;
24351da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
24361da177e4SLinus Torvalds 		/* SCSI command */
24371da177e4SLinus Torvalds 		bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
24381da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
24391da177e4SLinus Torvalds 	}
24401da177e4SLinus Torvalds }
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
24441da177e4SLinus Torvalds 		u16 *pscsi_status)
24451da177e4SLinus Torvalds {
24465cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "status_phase0: (0x%p) <%02i-%i>\n",
24479cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
24481da177e4SLinus Torvalds 	srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
24491da177e4SLinus Torvalds 	srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);	/* get message */
24501da177e4SLinus Torvalds 	srb->state = SRB_COMPLETED;
24511da177e4SLinus Torvalds 	*pscsi_status = PH_BUS_FREE;	/*.. initial phase */
24521da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
24531da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
24541da177e4SLinus Torvalds }
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
24581da177e4SLinus Torvalds 		u16 *pscsi_status)
24591da177e4SLinus Torvalds {
24605cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "status_phase1: (0x%p) <%02i-%i>\n",
24619cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
24621da177e4SLinus Torvalds 	srb->state = SRB_STATUS;
24631da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
24641da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
24651da177e4SLinus Torvalds }
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds /* Check if the message is complete */
24691da177e4SLinus Torvalds static inline u8 msgin_completed(u8 * msgbuf, u32 len)
24701da177e4SLinus Torvalds {
24711da177e4SLinus Torvalds 	if (*msgbuf == EXTENDED_MESSAGE) {
24721da177e4SLinus Torvalds 		if (len < 2)
24731da177e4SLinus Torvalds 			return 0;
24741da177e4SLinus Torvalds 		if (len < msgbuf[1] + 2)
24751da177e4SLinus Torvalds 			return 0;
24761da177e4SLinus Torvalds 	} else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f)	/* two byte messages */
24771da177e4SLinus Torvalds 		if (len < 2)
24781da177e4SLinus Torvalds 			return 0;
24791da177e4SLinus Torvalds 	return 1;
24801da177e4SLinus Torvalds }
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds /* reject_msg */
24831da177e4SLinus Torvalds static inline void msgin_reject(struct AdapterCtlBlk *acb,
24841da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
24851da177e4SLinus Torvalds {
24861da177e4SLinus Torvalds 	srb->msgout_buf[0] = MESSAGE_REJECT;
24871da177e4SLinus Torvalds 	srb->msg_count = 1;
24881da177e4SLinus Torvalds 	DC395x_ENABLE_MSGOUT;
24891da177e4SLinus Torvalds 	srb->state &= ~SRB_MSGIN;
24901da177e4SLinus Torvalds 	srb->state |= SRB_MSGOUT;
24911da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
24921da177e4SLinus Torvalds 		srb->msgin_buf[0],
24931da177e4SLinus Torvalds 		srb->dcb->target_id, srb->dcb->target_lun);
24941da177e4SLinus Torvalds }
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
24981da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb, u8 tag)
24991da177e4SLinus Torvalds {
25001da177e4SLinus Torvalds 	struct ScsiReqBlk *srb = NULL;
25011da177e4SLinus Torvalds 	struct ScsiReqBlk *i;
25025cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_qtag: (0x%p) tag=%i srb=%p\n",
25035cd049a5SChristoph Hellwig 		   srb->cmd, tag, srb);
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds 	if (!(dcb->tag_mask & (1 << tag)))
25061da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
25071da177e4SLinus Torvalds 			"msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
25081da177e4SLinus Torvalds 			dcb->tag_mask, tag);
25091da177e4SLinus Torvalds 
25101da177e4SLinus Torvalds 	if (list_empty(&dcb->srb_going_list))
25111da177e4SLinus Torvalds 		goto mingx0;
25121da177e4SLinus Torvalds 	list_for_each_entry(i, &dcb->srb_going_list, list) {
25131da177e4SLinus Torvalds 		if (i->tag_number == tag) {
25141da177e4SLinus Torvalds 			srb = i;
25151da177e4SLinus Torvalds 			break;
25161da177e4SLinus Torvalds 		}
25171da177e4SLinus Torvalds 	}
25181da177e4SLinus Torvalds 	if (!srb)
25191da177e4SLinus Torvalds 		goto mingx0;
25201da177e4SLinus Torvalds 
25215cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_qtag: (0x%p) <%02i-%i>\n",
25225cd049a5SChristoph Hellwig 		srb->cmd, srb->dcb->target_id, srb->dcb->target_lun);
25231da177e4SLinus Torvalds 	if (dcb->flag & ABORT_DEV_) {
25241da177e4SLinus Torvalds 		/*srb->state = SRB_ABORT_SENT; */
25251da177e4SLinus Torvalds 		enable_msgout_abort(acb, srb);
25261da177e4SLinus Torvalds 	}
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 	if (!(srb->state & SRB_DISCONNECT))
25291da177e4SLinus Torvalds 		goto mingx0;
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds 	memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
25321da177e4SLinus Torvalds 	srb->state |= dcb->active_srb->state;
25331da177e4SLinus Torvalds 	srb->state |= SRB_DATA_XFER;
25341da177e4SLinus Torvalds 	dcb->active_srb = srb;
25351da177e4SLinus Torvalds 	/* How can we make the DORS happy? */
25361da177e4SLinus Torvalds 	return srb;
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds       mingx0:
25391da177e4SLinus Torvalds 	srb = acb->tmp_srb;
25401da177e4SLinus Torvalds 	srb->state = SRB_UNEXPECT_RESEL;
25411da177e4SLinus Torvalds 	dcb->active_srb = srb;
25421da177e4SLinus Torvalds 	srb->msgout_buf[0] = MSG_ABORT_TAG;
25431da177e4SLinus Torvalds 	srb->msg_count = 1;
25441da177e4SLinus Torvalds 	DC395x_ENABLE_MSGOUT;
25451da177e4SLinus Torvalds 	dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
25461da177e4SLinus Torvalds 	return srb;
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds static inline void reprogram_regs(struct AdapterCtlBlk *acb,
25511da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
25521da177e4SLinus Torvalds {
25531da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
25541da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
25551da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
25561da177e4SLinus Torvalds 	set_xfer_rate(acb, dcb);
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds /* set async transfer mode */
25611da177e4SLinus Torvalds static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
25621da177e4SLinus Torvalds {
25631da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
25641da177e4SLinus Torvalds 	dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
25651da177e4SLinus Torvalds 		dcb->target_id, dcb->target_lun);
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds 	dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
25681da177e4SLinus Torvalds 	dcb->sync_mode |= SYNC_NEGO_DONE;
25691da177e4SLinus Torvalds 	/*dcb->sync_period &= 0; */
25701da177e4SLinus Torvalds 	dcb->sync_offset = 0;
25711da177e4SLinus Torvalds 	dcb->min_nego_period = 200 >> 2;	/* 200ns <=> 5 MHz */
25721da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_SYNC_NEGO;
25731da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
25741da177e4SLinus Torvalds 	if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
25751da177e4SLinus Torvalds 	    && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
25761da177e4SLinus Torvalds 		build_wdtr(acb, dcb, srb);
25771da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
25781da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
25791da177e4SLinus Torvalds 	}
25801da177e4SLinus Torvalds }
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds /* set sync transfer mode */
25841da177e4SLinus Torvalds static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
25851da177e4SLinus Torvalds {
25861da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
25871da177e4SLinus Torvalds 	u8 bval;
25881da177e4SLinus Torvalds 	int fact;
25891da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
25901da177e4SLinus Torvalds 		"(%02i.%01i MHz) Offset %i\n",
25911da177e4SLinus Torvalds 		dcb->target_id, srb->msgin_buf[3] << 2,
25921da177e4SLinus Torvalds 		(250 / srb->msgin_buf[3]),
25931da177e4SLinus Torvalds 		((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
25941da177e4SLinus Torvalds 		srb->msgin_buf[4]);
25951da177e4SLinus Torvalds 
25961da177e4SLinus Torvalds 	if (srb->msgin_buf[4] > 15)
25971da177e4SLinus Torvalds 		srb->msgin_buf[4] = 15;
25981da177e4SLinus Torvalds 	if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
25991da177e4SLinus Torvalds 		dcb->sync_offset = 0;
26001da177e4SLinus Torvalds 	else if (dcb->sync_offset == 0)
26011da177e4SLinus Torvalds 		dcb->sync_offset = srb->msgin_buf[4];
26021da177e4SLinus Torvalds 	if (srb->msgin_buf[4] > dcb->sync_offset)
26031da177e4SLinus Torvalds 		srb->msgin_buf[4] = dcb->sync_offset;
26041da177e4SLinus Torvalds 	else
26051da177e4SLinus Torvalds 		dcb->sync_offset = srb->msgin_buf[4];
26061da177e4SLinus Torvalds 	bval = 0;
26071da177e4SLinus Torvalds 	while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
26081da177e4SLinus Torvalds 			    || dcb->min_nego_period >
26091da177e4SLinus Torvalds 			    clock_period[bval]))
26101da177e4SLinus Torvalds 		bval++;
26111da177e4SLinus Torvalds 	if (srb->msgin_buf[3] < clock_period[bval])
26121da177e4SLinus Torvalds 		dprintkl(KERN_INFO,
26131da177e4SLinus Torvalds 			"msgin_set_sync: Increase sync nego period to %ins\n",
26141da177e4SLinus Torvalds 			clock_period[bval] << 2);
26151da177e4SLinus Torvalds 	srb->msgin_buf[3] = clock_period[bval];
26161da177e4SLinus Torvalds 	dcb->sync_period &= 0xf0;
26171da177e4SLinus Torvalds 	dcb->sync_period |= ALT_SYNC | bval;
26181da177e4SLinus Torvalds 	dcb->min_nego_period = srb->msgin_buf[3];
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 	if (dcb->sync_period & WIDE_SYNC)
26211da177e4SLinus Torvalds 		fact = 500;
26221da177e4SLinus Torvalds 	else
26231da177e4SLinus Torvalds 		fact = 250;
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	dprintkl(KERN_INFO,
26261da177e4SLinus Torvalds 		"Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
26271da177e4SLinus Torvalds 		dcb->target_id, (fact == 500) ? "Wide16" : "",
26281da177e4SLinus Torvalds 		dcb->min_nego_period << 2, dcb->sync_offset,
26291da177e4SLinus Torvalds 		(fact / dcb->min_nego_period),
26301da177e4SLinus Torvalds 		((fact % dcb->min_nego_period) * 10 +
26311da177e4SLinus Torvalds 		dcb->min_nego_period / 2) / dcb->min_nego_period);
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds 	if (!(srb->state & SRB_DO_SYNC_NEGO)) {
26341da177e4SLinus Torvalds 		/* Reply with corrected SDTR Message */
26351da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
26361da177e4SLinus Torvalds 			srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds 		memcpy(srb->msgout_buf, srb->msgin_buf, 5);
26391da177e4SLinus Torvalds 		srb->msg_count = 5;
26401da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
26411da177e4SLinus Torvalds 		dcb->sync_mode |= SYNC_NEGO_DONE;
26421da177e4SLinus Torvalds 	} else {
26431da177e4SLinus Torvalds 		if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
26441da177e4SLinus Torvalds 		    && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
26451da177e4SLinus Torvalds 			build_wdtr(acb, dcb, srb);
26461da177e4SLinus Torvalds 			DC395x_ENABLE_MSGOUT;
26471da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
26481da177e4SLinus Torvalds 		}
26491da177e4SLinus Torvalds 	}
26501da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_SYNC_NEGO;
26511da177e4SLinus Torvalds 	dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
26581da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
26591da177e4SLinus Torvalds {
26601da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
26611da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	dcb->sync_period &= ~WIDE_SYNC;
26641da177e4SLinus Torvalds 	dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
26651da177e4SLinus Torvalds 	dcb->sync_mode |= WIDE_NEGO_DONE;
26661da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_WIDE_NEGO;
26671da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
26681da177e4SLinus Torvalds 	if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
26691da177e4SLinus Torvalds 	    && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
26701da177e4SLinus Torvalds 		build_sdtr(acb, dcb, srb);
26711da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
26721da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
26731da177e4SLinus Torvalds 	}
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
26771da177e4SLinus Torvalds {
26781da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
26791da177e4SLinus Torvalds 	u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
26801da177e4SLinus Torvalds 		   && acb->config & HCC_WIDE_CARD) ? 1 : 0;
26811da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds 	if (srb->msgin_buf[3] > wide)
26841da177e4SLinus Torvalds 		srb->msgin_buf[3] = wide;
26851da177e4SLinus Torvalds 	/* Completed */
26861da177e4SLinus Torvalds 	if (!(srb->state & SRB_DO_WIDE_NEGO)) {
26871da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
26881da177e4SLinus Torvalds 			"msgin_set_wide: Wide nego initiated <%02i>\n",
26891da177e4SLinus Torvalds 			dcb->target_id);
26901da177e4SLinus Torvalds 		memcpy(srb->msgout_buf, srb->msgin_buf, 4);
26911da177e4SLinus Torvalds 		srb->msg_count = 4;
26921da177e4SLinus Torvalds 		srb->state |= SRB_DO_WIDE_NEGO;
26931da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
26941da177e4SLinus Torvalds 	}
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
26971da177e4SLinus Torvalds 	if (srb->msgin_buf[3] > 0)
26981da177e4SLinus Torvalds 		dcb->sync_period |= WIDE_SYNC;
26991da177e4SLinus Torvalds 	else
27001da177e4SLinus Torvalds 		dcb->sync_period &= ~WIDE_SYNC;
27011da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_WIDE_NEGO;
27021da177e4SLinus Torvalds 	/*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
27031da177e4SLinus Torvalds 	dprintkdbg(DBG_1,
27041da177e4SLinus Torvalds 		"msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
27051da177e4SLinus Torvalds 		(8 << srb->msgin_buf[3]), dcb->target_id);
27061da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
27071da177e4SLinus Torvalds 	if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
27081da177e4SLinus Torvalds 	    && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
27091da177e4SLinus Torvalds 		build_sdtr(acb, dcb, srb);
27101da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
27111da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
27121da177e4SLinus Torvalds 	}
27131da177e4SLinus Torvalds }
27141da177e4SLinus Torvalds 
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds /*
27171da177e4SLinus Torvalds  * extended message codes:
27181da177e4SLinus Torvalds  *
27191da177e4SLinus Torvalds  *	code	description
27201da177e4SLinus Torvalds  *
27211da177e4SLinus Torvalds  *	02h	Reserved
27221da177e4SLinus Torvalds  *	00h	MODIFY DATA  POINTER
27231da177e4SLinus Torvalds  *	01h	SYNCHRONOUS DATA TRANSFER REQUEST
27241da177e4SLinus Torvalds  *	03h	WIDE DATA TRANSFER REQUEST
27251da177e4SLinus Torvalds  *   04h - 7Fh	Reserved
27261da177e4SLinus Torvalds  *   80h - FFh	Vendor specific
27271da177e4SLinus Torvalds  */
27281da177e4SLinus Torvalds static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
27291da177e4SLinus Torvalds 		u16 *pscsi_status)
27301da177e4SLinus Torvalds {
27311da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = acb->active_dcb;
27325cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_phase0: (0x%p)\n", srb->cmd);
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds 	srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
27351da177e4SLinus Torvalds 	if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
27361da177e4SLinus Torvalds 		/* Now eval the msg */
27371da177e4SLinus Torvalds 		switch (srb->msgin_buf[0]) {
27381da177e4SLinus Torvalds 		case DISCONNECT:
27391da177e4SLinus Torvalds 			srb->state = SRB_DISCONNECT;
27401da177e4SLinus Torvalds 			break;
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds 		case SIMPLE_QUEUE_TAG:
27431da177e4SLinus Torvalds 		case HEAD_OF_QUEUE_TAG:
27441da177e4SLinus Torvalds 		case ORDERED_QUEUE_TAG:
27451da177e4SLinus Torvalds 			srb =
27461da177e4SLinus Torvalds 			    msgin_qtag(acb, dcb,
27471da177e4SLinus Torvalds 					      srb->msgin_buf[1]);
27481da177e4SLinus Torvalds 			break;
27491da177e4SLinus Torvalds 
27501da177e4SLinus Torvalds 		case MESSAGE_REJECT:
27511da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
27521da177e4SLinus Torvalds 				       DO_CLRATN | DO_DATALATCH);
27531da177e4SLinus Torvalds 			/* A sync nego message was rejected ! */
27541da177e4SLinus Torvalds 			if (srb->state & SRB_DO_SYNC_NEGO) {
27551da177e4SLinus Torvalds 				msgin_set_async(acb, srb);
27561da177e4SLinus Torvalds 				break;
27571da177e4SLinus Torvalds 			}
27581da177e4SLinus Torvalds 			/* A wide nego message was rejected ! */
27591da177e4SLinus Torvalds 			if (srb->state & SRB_DO_WIDE_NEGO) {
27601da177e4SLinus Torvalds 				msgin_set_nowide(acb, srb);
27611da177e4SLinus Torvalds 				break;
27621da177e4SLinus Torvalds 			}
27631da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
27641da177e4SLinus Torvalds 			/*srb->state |= SRB_ABORT_SENT */
27651da177e4SLinus Torvalds 			break;
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds 		case EXTENDED_MESSAGE:
27681da177e4SLinus Torvalds 			/* SDTR */
27691da177e4SLinus Torvalds 			if (srb->msgin_buf[1] == 3
27701da177e4SLinus Torvalds 			    && srb->msgin_buf[2] == EXTENDED_SDTR) {
27711da177e4SLinus Torvalds 				msgin_set_sync(acb, srb);
27721da177e4SLinus Torvalds 				break;
27731da177e4SLinus Torvalds 			}
27741da177e4SLinus Torvalds 			/* WDTR */
27751da177e4SLinus Torvalds 			if (srb->msgin_buf[1] == 2
27761da177e4SLinus Torvalds 			    && srb->msgin_buf[2] == EXTENDED_WDTR
27771da177e4SLinus Torvalds 			    && srb->msgin_buf[3] <= 2) { /* sanity check ... */
27781da177e4SLinus Torvalds 				msgin_set_wide(acb, srb);
27791da177e4SLinus Torvalds 				break;
27801da177e4SLinus Torvalds 			}
27811da177e4SLinus Torvalds 			msgin_reject(acb, srb);
27821da177e4SLinus Torvalds 			break;
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds 		case MSG_IGNOREWIDE:
27851da177e4SLinus Torvalds 			/* Discard  wide residual */
27861da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
27871da177e4SLinus Torvalds 			break;
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 		case COMMAND_COMPLETE:
27901da177e4SLinus Torvalds 			/* nothing has to be done */
27911da177e4SLinus Torvalds 			break;
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds 		case SAVE_POINTERS:
27941da177e4SLinus Torvalds 			/*
27951da177e4SLinus Torvalds 			 * SAVE POINTER may be ignored as we have the struct
27961da177e4SLinus Torvalds 			 * ScsiReqBlk* associated with the scsi command.
27971da177e4SLinus Torvalds 			 */
27985cd049a5SChristoph Hellwig 			dprintkdbg(DBG_0, "msgin_phase0: (0x%p) "
27991da177e4SLinus Torvalds 				"SAVE POINTER rem=%i Ignore\n",
28005cd049a5SChristoph Hellwig 				srb->cmd, srb->total_xfer_length);
28011da177e4SLinus Torvalds 			break;
28021da177e4SLinus Torvalds 
28031da177e4SLinus Torvalds 		case RESTORE_POINTERS:
28041da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
28051da177e4SLinus Torvalds 			break;
28061da177e4SLinus Torvalds 
28071da177e4SLinus Torvalds 		case ABORT:
28085cd049a5SChristoph Hellwig 			dprintkdbg(DBG_0, "msgin_phase0: (0x%p) "
28091da177e4SLinus Torvalds 				"<%02i-%i> ABORT msg\n",
28105cd049a5SChristoph Hellwig 				srb->cmd, dcb->target_id,
28111da177e4SLinus Torvalds 				dcb->target_lun);
28121da177e4SLinus Torvalds 			dcb->flag |= ABORT_DEV_;
28131da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
28141da177e4SLinus Torvalds 			break;
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 		default:
28171da177e4SLinus Torvalds 			/* reject unknown messages */
28181da177e4SLinus Torvalds 			if (srb->msgin_buf[0] & IDENTIFY_BASE) {
28191da177e4SLinus Torvalds 				dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
28201da177e4SLinus Torvalds 				srb->msg_count = 1;
28211da177e4SLinus Torvalds 				srb->msgout_buf[0] = dcb->identify_msg;
28221da177e4SLinus Torvalds 				DC395x_ENABLE_MSGOUT;
28231da177e4SLinus Torvalds 				srb->state |= SRB_MSGOUT;
28241da177e4SLinus Torvalds 				/*break; */
28251da177e4SLinus Torvalds 			}
28261da177e4SLinus Torvalds 			msgin_reject(acb, srb);
28271da177e4SLinus Torvalds 		}
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds 		/* Clear counter and MsgIn state */
28301da177e4SLinus Torvalds 		srb->state &= ~SRB_MSGIN;
28311da177e4SLinus Torvalds 		acb->msg_len = 0;
28321da177e4SLinus Torvalds 	}
28331da177e4SLinus Torvalds 	*pscsi_status = PH_BUS_FREE;
28341da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important ... you know! */
28351da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
28361da177e4SLinus Torvalds }
28371da177e4SLinus Torvalds 
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
28401da177e4SLinus Torvalds 		u16 *pscsi_status)
28411da177e4SLinus Torvalds {
28425cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_phase1: (0x%p)\n", srb->cmd);
28431da177e4SLinus Torvalds 	clear_fifo(acb, "msgin_phase1");
28441da177e4SLinus Torvalds 	DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
28451da177e4SLinus Torvalds 	if (!(srb->state & SRB_MSGIN)) {
28461da177e4SLinus Torvalds 		srb->state &= ~SRB_DISCONNECT;
28471da177e4SLinus Torvalds 		srb->state |= SRB_MSGIN;
28481da177e4SLinus Torvalds 	}
28491da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
28501da177e4SLinus Torvalds 	/* SCSI command */
28511da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds 
28551da177e4SLinus Torvalds static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
28561da177e4SLinus Torvalds 		u16 *pscsi_status)
28571da177e4SLinus Torvalds {
28581da177e4SLinus Torvalds }
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
28621da177e4SLinus Torvalds 		u16 *pscsi_status)
28631da177e4SLinus Torvalds {
28641da177e4SLinus Torvalds }
28651da177e4SLinus Torvalds 
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
28681da177e4SLinus Torvalds {
28691da177e4SLinus Torvalds 	struct DeviceCtlBlk *i;
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds 	/* set all lun device's  period, offset */
28721da177e4SLinus Torvalds 	if (dcb->identify_msg & 0x07)
28731da177e4SLinus Torvalds 		return;
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 	if (acb->scan_devices) {
28761da177e4SLinus Torvalds 		current_sync_offset = dcb->sync_offset;
28771da177e4SLinus Torvalds 		return;
28781da177e4SLinus Torvalds 	}
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds 	list_for_each_entry(i, &acb->dcb_list, list)
28811da177e4SLinus Torvalds 		if (i->target_id == dcb->target_id) {
28821da177e4SLinus Torvalds 			i->sync_period = dcb->sync_period;
28831da177e4SLinus Torvalds 			i->sync_offset = dcb->sync_offset;
28841da177e4SLinus Torvalds 			i->sync_mode = dcb->sync_mode;
28851da177e4SLinus Torvalds 			i->min_nego_period = dcb->min_nego_period;
28861da177e4SLinus Torvalds 		}
28871da177e4SLinus Torvalds }
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds static void disconnect(struct AdapterCtlBlk *acb)
28911da177e4SLinus Torvalds {
28921da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = acb->active_dcb;
28931da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
28941da177e4SLinus Torvalds 
28951da177e4SLinus Torvalds 	if (!dcb) {
28961da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "disconnect: No such device\n");
28971da177e4SLinus Torvalds 		udelay(500);
28981da177e4SLinus Torvalds 		/* Suspend queue for a while */
289953a8936cSHannes Reinecke 		acb->last_reset =
29001da177e4SLinus Torvalds 		    jiffies + HZ / 2 +
29011da177e4SLinus Torvalds 		    HZ * acb->eeprom.delay_time;
29021da177e4SLinus Torvalds 		clear_fifo(acb, "disconnectEx");
29031da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
29041da177e4SLinus Torvalds 		return;
29051da177e4SLinus Torvalds 	}
29061da177e4SLinus Torvalds 	srb = dcb->active_srb;
29071da177e4SLinus Torvalds 	acb->active_dcb = NULL;
29085cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "disconnect: (0x%p)\n", srb->cmd);
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
29111da177e4SLinus Torvalds 	clear_fifo(acb, "disconnect");
29121da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
29131da177e4SLinus Torvalds 	if (srb->state & SRB_UNEXPECT_RESEL) {
29141da177e4SLinus Torvalds 		dprintkl(KERN_ERR,
29151da177e4SLinus Torvalds 			"disconnect: Unexpected reselection <%02i-%i>\n",
29161da177e4SLinus Torvalds 			dcb->target_id, dcb->target_lun);
29171da177e4SLinus Torvalds 		srb->state = 0;
29181da177e4SLinus Torvalds 		waiting_process_next(acb);
29191da177e4SLinus Torvalds 	} else if (srb->state & SRB_ABORT_SENT) {
29201da177e4SLinus Torvalds 		dcb->flag &= ~ABORT_DEV_;
292153a8936cSHannes Reinecke 		acb->last_reset = jiffies + HZ / 2 + 1;
29221da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
29231da177e4SLinus Torvalds 		doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
29241da177e4SLinus Torvalds 		waiting_process_next(acb);
29251da177e4SLinus Torvalds 	} else {
29261da177e4SLinus Torvalds 		if ((srb->state & (SRB_START_ + SRB_MSGOUT))
29271da177e4SLinus Torvalds 		    || !(srb->
29281da177e4SLinus Torvalds 			 state & (SRB_DISCONNECT + SRB_COMPLETED))) {
29291da177e4SLinus Torvalds 			/*
29301da177e4SLinus Torvalds 			 * Selection time out
29311da177e4SLinus Torvalds 			 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
29321da177e4SLinus Torvalds 			 */
29331da177e4SLinus Torvalds 			/* Unexp. Disc / Sel Timeout */
29341da177e4SLinus Torvalds 			if (srb->state != SRB_START_
29351da177e4SLinus Torvalds 			    && srb->state != SRB_MSGOUT) {
29361da177e4SLinus Torvalds 				srb->state = SRB_READY;
29371da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
29385cd049a5SChristoph Hellwig 					"disconnect: (0x%p) Unexpected\n",
29395cd049a5SChristoph Hellwig 					srb->cmd);
29401da177e4SLinus Torvalds 				srb->target_status = SCSI_STAT_SEL_TIMEOUT;
29411da177e4SLinus Torvalds 				goto disc1;
29421da177e4SLinus Torvalds 			} else {
29431da177e4SLinus Torvalds 				/* Normal selection timeout */
29445cd049a5SChristoph Hellwig 				dprintkdbg(DBG_KG, "disconnect: (0x%p) "
29455cd049a5SChristoph Hellwig 					"<%02i-%i> SelTO\n", srb->cmd,
29461da177e4SLinus Torvalds 					dcb->target_id, dcb->target_lun);
29471da177e4SLinus Torvalds 				if (srb->retry_count++ > DC395x_MAX_RETRIES
29481da177e4SLinus Torvalds 				    || acb->scan_devices) {
29491da177e4SLinus Torvalds 					srb->target_status =
29501da177e4SLinus Torvalds 					    SCSI_STAT_SEL_TIMEOUT;
29511da177e4SLinus Torvalds 					goto disc1;
29521da177e4SLinus Torvalds 				}
29531da177e4SLinus Torvalds 				free_tag(dcb, srb);
2954cd29660dSChristoph Hellwig 				list_move(&srb->list, &dcb->srb_waiting_list);
29551da177e4SLinus Torvalds 				dprintkdbg(DBG_KG,
29565cd049a5SChristoph Hellwig 					"disconnect: (0x%p) Retry\n",
29575cd049a5SChristoph Hellwig 					srb->cmd);
29581da177e4SLinus Torvalds 				waiting_set_timer(acb, HZ / 20);
29591da177e4SLinus Torvalds 			}
29601da177e4SLinus Torvalds 		} else if (srb->state & SRB_DISCONNECT) {
29611da177e4SLinus Torvalds 			u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
29621da177e4SLinus Torvalds 			/*
29631da177e4SLinus Torvalds 			 * SRB_DISCONNECT (This is what we expect!)
29641da177e4SLinus Torvalds 			 */
29651da177e4SLinus Torvalds 			if (bval & 0x40) {
29661da177e4SLinus Torvalds 				dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
29671da177e4SLinus Torvalds 					" 0x%02x: ACK set! Other controllers?\n",
29681da177e4SLinus Torvalds 					bval);
29691da177e4SLinus Torvalds 				/* It could come from another initiator, therefore don't do much ! */
29701da177e4SLinus Torvalds 			} else
29711da177e4SLinus Torvalds 				waiting_process_next(acb);
29721da177e4SLinus Torvalds 		} else if (srb->state & SRB_COMPLETED) {
29731da177e4SLinus Torvalds 		      disc1:
29741da177e4SLinus Torvalds 			/*
29751da177e4SLinus Torvalds 			 ** SRB_COMPLETED
29761da177e4SLinus Torvalds 			 */
29771da177e4SLinus Torvalds 			free_tag(dcb, srb);
29781da177e4SLinus Torvalds 			dcb->active_srb = NULL;
29791da177e4SLinus Torvalds 			srb->state = SRB_FREE;
29801da177e4SLinus Torvalds 			srb_done(acb, dcb, srb);
29811da177e4SLinus Torvalds 		}
29821da177e4SLinus Torvalds 	}
29831da177e4SLinus Torvalds }
29841da177e4SLinus Torvalds 
29851da177e4SLinus Torvalds 
29861da177e4SLinus Torvalds static void reselect(struct AdapterCtlBlk *acb)
29871da177e4SLinus Torvalds {
29881da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = acb->active_dcb;
29891da177e4SLinus Torvalds 	struct ScsiReqBlk *srb = NULL;
29901da177e4SLinus Torvalds 	u16 rsel_tar_lun_id;
29911da177e4SLinus Torvalds 	u8 id, lun;
29921da177e4SLinus Torvalds 	u8 arblostflag = 0;
29931da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds 	clear_fifo(acb, "reselect");
29961da177e4SLinus Torvalds 	/*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
29971da177e4SLinus Torvalds 	/* Read Reselected Target ID and LUN */
29981da177e4SLinus Torvalds 	rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
29991da177e4SLinus Torvalds 	if (dcb) {		/* Arbitration lost but Reselection win */
30001da177e4SLinus Torvalds 		srb = dcb->active_srb;
30011da177e4SLinus Torvalds 		if (!srb) {
30021da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
30031da177e4SLinus Torvalds 				"but active_srb == NULL\n");
30041da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
30051da177e4SLinus Torvalds 			return;
30061da177e4SLinus Torvalds 		}
30071da177e4SLinus Torvalds 		/* Why the if ? */
30081da177e4SLinus Torvalds 		if (!acb->scan_devices) {
30095cd049a5SChristoph Hellwig 			dprintkdbg(DBG_KG, "reselect: (0x%p) <%02i-%i> "
30101da177e4SLinus Torvalds 				"Arb lost but Resel win rsel=%i stat=0x%04x\n",
30115cd049a5SChristoph Hellwig 				srb->cmd, dcb->target_id,
30121da177e4SLinus Torvalds 				dcb->target_lun, rsel_tar_lun_id,
30131da177e4SLinus Torvalds 				DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
30141da177e4SLinus Torvalds 			arblostflag = 1;
30151da177e4SLinus Torvalds 			/*srb->state |= SRB_DISCONNECT; */
30161da177e4SLinus Torvalds 
30171da177e4SLinus Torvalds 			srb->state = SRB_READY;
30181da177e4SLinus Torvalds 			free_tag(dcb, srb);
3019cd29660dSChristoph Hellwig 			list_move(&srb->list, &dcb->srb_waiting_list);
30201da177e4SLinus Torvalds 			waiting_set_timer(acb, HZ / 20);
30211da177e4SLinus Torvalds 
30221da177e4SLinus Torvalds 			/* return; */
30231da177e4SLinus Torvalds 		}
30241da177e4SLinus Torvalds 	}
30251da177e4SLinus Torvalds 	/* Read Reselected Target Id and LUN */
30261da177e4SLinus Torvalds 	if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
30271da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
30281da177e4SLinus Torvalds 			"Got %i!\n", rsel_tar_lun_id);
30291da177e4SLinus Torvalds 	id = rsel_tar_lun_id & 0xff;
30301da177e4SLinus Torvalds 	lun = (rsel_tar_lun_id >> 8) & 7;
30311da177e4SLinus Torvalds 	dcb = find_dcb(acb, id, lun);
30321da177e4SLinus Torvalds 	if (!dcb) {
30331da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "reselect: From non existent device "
30341da177e4SLinus Torvalds 			"<%02i-%i>\n", id, lun);
30351da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
30361da177e4SLinus Torvalds 		return;
30371da177e4SLinus Torvalds 	}
30381da177e4SLinus Torvalds 	acb->active_dcb = dcb;
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds 	if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
30411da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
30421da177e4SLinus Torvalds 			"disconnection? <%02i-%i>\n",
30431da177e4SLinus Torvalds 			dcb->target_id, dcb->target_lun);
30441da177e4SLinus Torvalds 
30451da177e4SLinus Torvalds 	if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
30461da177e4SLinus Torvalds 		srb = acb->tmp_srb;
30471da177e4SLinus Torvalds 		dcb->active_srb = srb;
30481da177e4SLinus Torvalds 	} else {
30491da177e4SLinus Torvalds 		/* There can be only one! */
30501da177e4SLinus Torvalds 		srb = dcb->active_srb;
30511da177e4SLinus Torvalds 		if (!srb || !(srb->state & SRB_DISCONNECT)) {
30521da177e4SLinus Torvalds 			/*
30531da177e4SLinus Torvalds 			 * abort command
30541da177e4SLinus Torvalds 			 */
30551da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
30561da177e4SLinus Torvalds 				"reselect: w/o disconnected cmds <%02i-%i>\n",
30571da177e4SLinus Torvalds 				dcb->target_id, dcb->target_lun);
30581da177e4SLinus Torvalds 			srb = acb->tmp_srb;
30591da177e4SLinus Torvalds 			srb->state = SRB_UNEXPECT_RESEL;
30601da177e4SLinus Torvalds 			dcb->active_srb = srb;
30611da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
30621da177e4SLinus Torvalds 		} else {
30631da177e4SLinus Torvalds 			if (dcb->flag & ABORT_DEV_) {
30641da177e4SLinus Torvalds 				/*srb->state = SRB_ABORT_SENT; */
30651da177e4SLinus Torvalds 				enable_msgout_abort(acb, srb);
30661da177e4SLinus Torvalds 			} else
30671da177e4SLinus Torvalds 				srb->state = SRB_DATA_XFER;
30681da177e4SLinus Torvalds 
30691da177e4SLinus Torvalds 		}
30701da177e4SLinus Torvalds 	}
30711da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
30721da177e4SLinus Torvalds 
30731da177e4SLinus Torvalds 	/* Program HA ID, target ID, period and offset */
30741da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
30751da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);	/* host   ID */
30761da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);		/* target ID */
30771da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);		/* offset    */
30781da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);		/* sync period, wide */
30791da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);		/* it's important for atn stop */
30801da177e4SLinus Torvalds 	/* SCSI command */
30811da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
30821da177e4SLinus Torvalds }
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 
30851da177e4SLinus Torvalds static inline u8 tagq_blacklist(char *name)
30861da177e4SLinus Torvalds {
30871da177e4SLinus Torvalds #ifndef DC395x_NO_TAGQ
30881da177e4SLinus Torvalds #if 0
30891da177e4SLinus Torvalds 	u8 i;
30901da177e4SLinus Torvalds 	for (i = 0; i < BADDEVCNT; i++)
30911da177e4SLinus Torvalds 		if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
30921da177e4SLinus Torvalds 			return 1;
30931da177e4SLinus Torvalds #endif
30941da177e4SLinus Torvalds 	return 0;
30951da177e4SLinus Torvalds #else
30961da177e4SLinus Torvalds 	return 1;
30971da177e4SLinus Torvalds #endif
30981da177e4SLinus Torvalds }
30991da177e4SLinus Torvalds 
31001da177e4SLinus Torvalds 
31011da177e4SLinus Torvalds static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
31021da177e4SLinus Torvalds {
31031da177e4SLinus Torvalds 	/* Check for SCSI format (ANSI and Response data format) */
31041da177e4SLinus Torvalds 	if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
31051da177e4SLinus Torvalds 		if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
31061da177e4SLinus Torvalds 		    && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
31071da177e4SLinus Torvalds 		    /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
31081da177e4SLinus Torvalds 		    /* ((dcb->dev_type == TYPE_DISK)
31091da177e4SLinus Torvalds 		       || (dcb->dev_type == TYPE_MOD)) && */
31101da177e4SLinus Torvalds 		    !tagq_blacklist(((char *)ptr) + 8)) {
31111da177e4SLinus Torvalds 			if (dcb->max_command == 1)
31121da177e4SLinus Torvalds 				dcb->max_command =
31131da177e4SLinus Torvalds 				    dcb->acb->tag_max_num;
31141da177e4SLinus Torvalds 			dcb->sync_mode |= EN_TAG_QUEUEING;
31151da177e4SLinus Torvalds 			/*dcb->tag_mask = 0; */
31161da177e4SLinus Torvalds 		} else
31171da177e4SLinus Torvalds 			dcb->max_command = 1;
31181da177e4SLinus Torvalds 	}
31191da177e4SLinus Torvalds }
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds 
31221da177e4SLinus Torvalds static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
31231da177e4SLinus Torvalds 		struct ScsiInqData *ptr)
31241da177e4SLinus Torvalds {
31251da177e4SLinus Torvalds 	u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
31261da177e4SLinus Torvalds 	dcb->dev_type = bval1;
31271da177e4SLinus Torvalds 	/* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
31281da177e4SLinus Torvalds 	disc_tagq_set(dcb, ptr);
31291da177e4SLinus Torvalds }
31301da177e4SLinus Torvalds 
31311da177e4SLinus Torvalds 
31321da177e4SLinus Torvalds /* unmap mapped pci regions from SRB */
31331da177e4SLinus Torvalds static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
31341da177e4SLinus Torvalds {
31351da177e4SLinus Torvalds 	struct scsi_cmnd *cmd = srb->cmd;
31361da177e4SLinus Torvalds 	enum dma_data_direction dir = cmd->sc_data_direction;
3137a862ea31SFUJITA Tomonori 
3138a862ea31SFUJITA Tomonori 	if (scsi_sg_count(cmd) && dir != PCI_DMA_NONE) {
31391da177e4SLinus Torvalds 		/* unmap DC395x SG list */
31401da177e4SLinus Torvalds 		dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
31411da177e4SLinus Torvalds 			srb->sg_bus_addr, SEGMENTX_LEN);
3142dfda5e21SChristoph Hellwig 		dma_unmap_single(&acb->dev->dev, srb->sg_bus_addr, SEGMENTX_LEN,
3143dfda5e21SChristoph Hellwig 				DMA_TO_DEVICE);
31441da177e4SLinus Torvalds 		dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
3145a862ea31SFUJITA Tomonori 			   scsi_sg_count(cmd), scsi_bufflen(cmd));
31461da177e4SLinus Torvalds 		/* unmap the sg segments */
3147a862ea31SFUJITA Tomonori 		scsi_dma_unmap(cmd);
31481da177e4SLinus Torvalds 	}
31491da177e4SLinus Torvalds }
31501da177e4SLinus Torvalds 
31511da177e4SLinus Torvalds 
31521da177e4SLinus Torvalds /* unmap mapped pci sense buffer from SRB */
31531da177e4SLinus Torvalds static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
31541da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
31551da177e4SLinus Torvalds {
31561da177e4SLinus Torvalds 	if (!(srb->flag & AUTO_REQSENSE))
31571da177e4SLinus Torvalds 		return;
31581da177e4SLinus Torvalds 	/* Unmap sense buffer */
31591da177e4SLinus Torvalds 	dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
31601da177e4SLinus Torvalds 	       srb->segment_x[0].address);
3161dfda5e21SChristoph Hellwig 	dma_unmap_single(&acb->dev->dev, srb->segment_x[0].address,
3162dfda5e21SChristoph Hellwig 			 srb->segment_x[0].length, DMA_FROM_DEVICE);
31631da177e4SLinus Torvalds 	/* Restore SG stuff */
31641da177e4SLinus Torvalds 	srb->total_xfer_length = srb->xferred;
31651da177e4SLinus Torvalds 	srb->segment_x[0].address =
31661da177e4SLinus Torvalds 	    srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
31671da177e4SLinus Torvalds 	srb->segment_x[0].length =
31681da177e4SLinus Torvalds 	    srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
31691da177e4SLinus Torvalds }
31701da177e4SLinus Torvalds 
31711da177e4SLinus Torvalds 
31721da177e4SLinus Torvalds /*
31731da177e4SLinus Torvalds  * Complete execution of a SCSI command
31741da177e4SLinus Torvalds  * Signal completion to the generic SCSI driver
31751da177e4SLinus Torvalds  */
31761da177e4SLinus Torvalds static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
31771da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
31781da177e4SLinus Torvalds {
31791da177e4SLinus Torvalds 	u8 tempcnt, status;
31801da177e4SLinus Torvalds 	struct scsi_cmnd *cmd = srb->cmd;
31811da177e4SLinus Torvalds 	enum dma_data_direction dir = cmd->sc_data_direction;
31829dc399deSGuennadi Liakhovetski 	int ckc_only = 1;
31831da177e4SLinus Torvalds 
31845cd049a5SChristoph Hellwig 	dprintkdbg(DBG_1, "srb_done: (0x%p) <%02i-%i>\n", srb->cmd,
31859cb78c16SHannes Reinecke 		srb->cmd->device->id, (u8)srb->cmd->device->lun);
31869dc399deSGuennadi Liakhovetski 	dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n",
3187a862ea31SFUJITA Tomonori 		   srb, scsi_sg_count(cmd), srb->sg_index, srb->sg_count,
3188a862ea31SFUJITA Tomonori 		   scsi_sgtalbe(cmd));
31891da177e4SLinus Torvalds 	status = srb->target_status;
31901da177e4SLinus Torvalds 	if (srb->flag & AUTO_REQSENSE) {
31911da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
31921da177e4SLinus Torvalds 		pci_unmap_srb_sense(acb, srb);
31931da177e4SLinus Torvalds 		/*
31941da177e4SLinus Torvalds 		 ** target status..........................
31951da177e4SLinus Torvalds 		 */
31961da177e4SLinus Torvalds 		srb->flag &= ~AUTO_REQSENSE;
31971da177e4SLinus Torvalds 		srb->adapter_status = 0;
31981da177e4SLinus Torvalds 		srb->target_status = CHECK_CONDITION << 1;
31991da177e4SLinus Torvalds 		if (debug_enabled(DBG_1)) {
32001da177e4SLinus Torvalds 			switch (cmd->sense_buffer[2] & 0x0f) {
32011da177e4SLinus Torvalds 			case NOT_READY:
32021da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
32031da177e4SLinus Torvalds 				     "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
32041da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
32051da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
32061da177e4SLinus Torvalds 				break;
32071da177e4SLinus Torvalds 			case UNIT_ATTENTION:
32081da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
32091da177e4SLinus Torvalds 				     "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
32101da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
32111da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
32121da177e4SLinus Torvalds 				break;
32131da177e4SLinus Torvalds 			case ILLEGAL_REQUEST:
32141da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
32151da177e4SLinus Torvalds 				     "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
32161da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
32171da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
32181da177e4SLinus Torvalds 				break;
32191da177e4SLinus Torvalds 			case MEDIUM_ERROR:
32201da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
32211da177e4SLinus Torvalds 				     "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
32221da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
32231da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
32241da177e4SLinus Torvalds 				break;
32251da177e4SLinus Torvalds 			case HARDWARE_ERROR:
32261da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
32271da177e4SLinus Torvalds 				     "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
32281da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
32291da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
32301da177e4SLinus Torvalds 				break;
32311da177e4SLinus Torvalds 			}
32321da177e4SLinus Torvalds 			if (cmd->sense_buffer[7] >= 6)
32331da177e4SLinus Torvalds 				printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
32341da177e4SLinus Torvalds 					"(0x%08x 0x%08x)\n",
32351da177e4SLinus Torvalds 					cmd->sense_buffer[2], cmd->sense_buffer[12],
32361da177e4SLinus Torvalds 					cmd->sense_buffer[13],
32371da177e4SLinus Torvalds 					*((unsigned int *)(cmd->sense_buffer + 3)),
32381da177e4SLinus Torvalds 					*((unsigned int *)(cmd->sense_buffer + 8)));
32391da177e4SLinus Torvalds 			else
32401da177e4SLinus Torvalds 				printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
32411da177e4SLinus Torvalds 					cmd->sense_buffer[2],
32421da177e4SLinus Torvalds 					*((unsigned int *)(cmd->sense_buffer + 3)));
32431da177e4SLinus Torvalds 		}
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds 		if (status == (CHECK_CONDITION << 1)) {
32461da177e4SLinus Torvalds 			cmd->result = DID_BAD_TARGET << 16;
32471da177e4SLinus Torvalds 			goto ckc_e;
32481da177e4SLinus Torvalds 		}
32491da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
32501da177e4SLinus Torvalds 
32511da177e4SLinus Torvalds 		if (srb->total_xfer_length
32521da177e4SLinus Torvalds 		    && srb->total_xfer_length >= cmd->underflow)
32531da177e4SLinus Torvalds 			cmd->result =
32541da177e4SLinus Torvalds 			    MK_RES_LNX(DRIVER_SENSE, DID_OK,
32551da177e4SLinus Torvalds 				       srb->end_message, CHECK_CONDITION);
32561da177e4SLinus Torvalds 		/*SET_RES_DID(cmd->result,DID_OK) */
32571da177e4SLinus Torvalds 		else
32581da177e4SLinus Torvalds 			cmd->result =
32591da177e4SLinus Torvalds 			    MK_RES_LNX(DRIVER_SENSE, DID_OK,
32601da177e4SLinus Torvalds 				       srb->end_message, CHECK_CONDITION);
32611da177e4SLinus Torvalds 
32621da177e4SLinus Torvalds 		goto ckc_e;
32631da177e4SLinus Torvalds 	}
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds /*************************************************************/
32661da177e4SLinus Torvalds 	if (status) {
32671da177e4SLinus Torvalds 		/*
32681da177e4SLinus Torvalds 		 * target status..........................
32691da177e4SLinus Torvalds 		 */
32701da177e4SLinus Torvalds 		if (status_byte(status) == CHECK_CONDITION) {
32711da177e4SLinus Torvalds 			request_sense(acb, dcb, srb);
32721da177e4SLinus Torvalds 			return;
32731da177e4SLinus Torvalds 		} else if (status_byte(status) == QUEUE_FULL) {
32741da177e4SLinus Torvalds 			tempcnt = (u8)list_size(&dcb->srb_going_list);
32751da177e4SLinus Torvalds 			dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
32761da177e4SLinus Torvalds 			     dcb->target_id, dcb->target_lun, tempcnt);
32771da177e4SLinus Torvalds 			if (tempcnt > 1)
32781da177e4SLinus Torvalds 				tempcnt--;
32791da177e4SLinus Torvalds 			dcb->max_command = tempcnt;
32801da177e4SLinus Torvalds 			free_tag(dcb, srb);
3281cd29660dSChristoph Hellwig 			list_move(&srb->list, &dcb->srb_waiting_list);
32821da177e4SLinus Torvalds 			waiting_set_timer(acb, HZ / 20);
32831da177e4SLinus Torvalds 			srb->adapter_status = 0;
32841da177e4SLinus Torvalds 			srb->target_status = 0;
32851da177e4SLinus Torvalds 			return;
32861da177e4SLinus Torvalds 		} else if (status == SCSI_STAT_SEL_TIMEOUT) {
32871da177e4SLinus Torvalds 			srb->adapter_status = H_SEL_TIMEOUT;
32881da177e4SLinus Torvalds 			srb->target_status = 0;
32891da177e4SLinus Torvalds 			cmd->result = DID_NO_CONNECT << 16;
32901da177e4SLinus Torvalds 		} else {
32911da177e4SLinus Torvalds 			srb->adapter_status = 0;
32921da177e4SLinus Torvalds 			SET_RES_DID(cmd->result, DID_ERROR);
32931da177e4SLinus Torvalds 			SET_RES_MSG(cmd->result, srb->end_message);
32941da177e4SLinus Torvalds 			SET_RES_TARGET(cmd->result, status);
32951da177e4SLinus Torvalds 
32961da177e4SLinus Torvalds 		}
32971da177e4SLinus Torvalds 	} else {
32981da177e4SLinus Torvalds 		/*
32991da177e4SLinus Torvalds 		 ** process initiator status..........................
33001da177e4SLinus Torvalds 		 */
33011da177e4SLinus Torvalds 		status = srb->adapter_status;
33021da177e4SLinus Torvalds 		if (status & H_OVER_UNDER_RUN) {
33031da177e4SLinus Torvalds 			srb->target_status = 0;
33041da177e4SLinus Torvalds 			SET_RES_DID(cmd->result, DID_OK);
33051da177e4SLinus Torvalds 			SET_RES_MSG(cmd->result, srb->end_message);
33061da177e4SLinus Torvalds 		} else if (srb->status & PARITY_ERROR) {
33071da177e4SLinus Torvalds 			SET_RES_DID(cmd->result, DID_PARITY);
33081da177e4SLinus Torvalds 			SET_RES_MSG(cmd->result, srb->end_message);
33091da177e4SLinus Torvalds 		} else {	/* No error */
33101da177e4SLinus Torvalds 
33111da177e4SLinus Torvalds 			srb->adapter_status = 0;
33121da177e4SLinus Torvalds 			srb->target_status = 0;
33131da177e4SLinus Torvalds 			SET_RES_DID(cmd->result, DID_OK);
33141da177e4SLinus Torvalds 		}
33151da177e4SLinus Torvalds 	}
33161da177e4SLinus Torvalds 
33179dc399deSGuennadi Liakhovetski 	ckc_only = 0;
33181da177e4SLinus Torvalds /* Check Error Conditions */
33191da177e4SLinus Torvalds       ckc_e:
33201da177e4SLinus Torvalds 
33213a5bd702SChristoph Hellwig 	pci_unmap_srb(acb, srb);
33223a5bd702SChristoph Hellwig 
33239dc399deSGuennadi Liakhovetski 	if (cmd->cmnd[0] == INQUIRY) {
33249dc399deSGuennadi Liakhovetski 		unsigned char *base = NULL;
33259dc399deSGuennadi Liakhovetski 		struct ScsiInqData *ptr;
33269dc399deSGuennadi Liakhovetski 		unsigned long flags = 0;
3327a862ea31SFUJITA Tomonori 		struct scatterlist* sg = scsi_sglist(cmd);
33289dc399deSGuennadi Liakhovetski 		size_t offset = 0, len = sizeof(struct ScsiInqData);
33299dc399deSGuennadi Liakhovetski 
33309dc399deSGuennadi Liakhovetski 		local_irq_save(flags);
3331a862ea31SFUJITA Tomonori 		base = scsi_kmap_atomic_sg(sg, scsi_sg_count(cmd), &offset, &len);
33329dc399deSGuennadi Liakhovetski 		ptr = (struct ScsiInqData *)(base + offset);
33339dc399deSGuennadi Liakhovetski 
33349dc399deSGuennadi Liakhovetski 		if (!ckc_only && (cmd->result & RES_DID) == 0
3335a862ea31SFUJITA Tomonori 		    && cmd->cmnd[2] == 0 && scsi_bufflen(cmd) >= 8
33369dc399deSGuennadi Liakhovetski 		    && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
33379dc399deSGuennadi Liakhovetski 			dcb->inquiry7 = ptr->Flags;
33389dc399deSGuennadi Liakhovetski 
33391da177e4SLinus Torvalds 	/*if( srb->cmd->cmnd[0] == INQUIRY && */
33401da177e4SLinus Torvalds 	/*  (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
3341c65be1a6SJohannes Thumshirn 		if ((cmd->result == (DID_OK << 16) ||
3342c65be1a6SJohannes Thumshirn 		     status_byte(cmd->result) == CHECK_CONDITION)) {
33431da177e4SLinus Torvalds 			if (!dcb->init_tcq_flag) {
33441da177e4SLinus Torvalds 				add_dev(acb, dcb, ptr);
33451da177e4SLinus Torvalds 				dcb->init_tcq_flag = 1;
33461da177e4SLinus Torvalds 			}
33471da177e4SLinus Torvalds 		}
33481da177e4SLinus Torvalds 
33499dc399deSGuennadi Liakhovetski 		scsi_kunmap_atomic_sg(base);
33509dc399deSGuennadi Liakhovetski 		local_irq_restore(flags);
33519dc399deSGuennadi Liakhovetski 	}
33521da177e4SLinus Torvalds 
33531da177e4SLinus Torvalds 	/* Here is the info for Doug Gilbert's sg3 ... */
3354a862ea31SFUJITA Tomonori 	scsi_set_resid(cmd, srb->total_xfer_length);
33551da177e4SLinus Torvalds 	/* This may be interpreted by sb. or not ... */
33561da177e4SLinus Torvalds 	cmd->SCp.this_residual = srb->total_xfer_length;
33571da177e4SLinus Torvalds 	cmd->SCp.buffers_residual = 0;
33581da177e4SLinus Torvalds 	if (debug_enabled(DBG_KG)) {
33591da177e4SLinus Torvalds 		if (srb->total_xfer_length)
33605cd049a5SChristoph Hellwig 			dprintkdbg(DBG_KG, "srb_done: (0x%p) <%02i-%i> "
33611da177e4SLinus Torvalds 				"cmnd=0x%02x Missed %i bytes\n",
33629cb78c16SHannes Reinecke 				cmd, cmd->device->id, (u8)cmd->device->lun,
33631da177e4SLinus Torvalds 				cmd->cmnd[0], srb->total_xfer_length);
33641da177e4SLinus Torvalds 	}
33651da177e4SLinus Torvalds 
3366cd29660dSChristoph Hellwig 	if (srb != acb->tmp_srb) {
33671da177e4SLinus Torvalds 		/* Add to free list */
33685cd049a5SChristoph Hellwig 		dprintkdbg(DBG_0, "srb_done: (0x%p) done result=0x%08x\n",
33695cd049a5SChristoph Hellwig 			cmd, cmd->result);
3370cd29660dSChristoph Hellwig 		list_move_tail(&srb->list, &acb->srb_free_list);
3371cd29660dSChristoph Hellwig 	} else {
3372cd29660dSChristoph Hellwig 		dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
33731da177e4SLinus Torvalds 	}
33741da177e4SLinus Torvalds 
33751da177e4SLinus Torvalds 	cmd->scsi_done(cmd);
33761da177e4SLinus Torvalds 	waiting_process_next(acb);
33771da177e4SLinus Torvalds }
33781da177e4SLinus Torvalds 
33791da177e4SLinus Torvalds 
33801da177e4SLinus Torvalds /* abort all cmds in our queues */
33811da177e4SLinus Torvalds static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
33821da177e4SLinus Torvalds 		struct scsi_cmnd *cmd, u8 force)
33831da177e4SLinus Torvalds {
33841da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
33851da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "doing_srb_done: pids ");
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
33881da177e4SLinus Torvalds 		struct ScsiReqBlk *srb;
33891da177e4SLinus Torvalds 		struct ScsiReqBlk *tmp;
33901da177e4SLinus Torvalds 		struct scsi_cmnd *p;
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds 		list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
33931da177e4SLinus Torvalds 			enum dma_data_direction dir;
33941da177e4SLinus Torvalds 			int result;
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds 			p = srb->cmd;
33971da177e4SLinus Torvalds 			dir = p->sc_data_direction;
33981da177e4SLinus Torvalds 			result = MK_RES(0, did_flag, 0, 0);
33995cd049a5SChristoph Hellwig 			printk("G:%p(%02i-%i) ", p,
34009cb78c16SHannes Reinecke 			       p->device->id, (u8)p->device->lun);
3401cd29660dSChristoph Hellwig 			list_del(&srb->list);
34021da177e4SLinus Torvalds 			free_tag(dcb, srb);
3403cd29660dSChristoph Hellwig 			list_add_tail(&srb->list, &acb->srb_free_list);
34041da177e4SLinus Torvalds 			p->result = result;
34051da177e4SLinus Torvalds 			pci_unmap_srb_sense(acb, srb);
34061da177e4SLinus Torvalds 			pci_unmap_srb(acb, srb);
34071da177e4SLinus Torvalds 			if (force) {
34081da177e4SLinus Torvalds 				/* For new EH, we normally don't need to give commands back,
34091da177e4SLinus Torvalds 				 * as they all complete or all time out */
34101da177e4SLinus Torvalds 				p->scsi_done(p);
34111da177e4SLinus Torvalds 			}
34121da177e4SLinus Torvalds 		}
34131da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_going_list))
34141da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
34151da177e4SLinus Torvalds 			       "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
34161da177e4SLinus Torvalds 			       dcb->target_id, dcb->target_lun);
34171da177e4SLinus Torvalds 		if (dcb->tag_mask)
34181da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
34191da177e4SLinus Torvalds 			       "tag_mask for <%02i-%i> should be empty, is %08x!\n",
34201da177e4SLinus Torvalds 			       dcb->target_id, dcb->target_lun,
34211da177e4SLinus Torvalds 			       dcb->tag_mask);
34221da177e4SLinus Torvalds 
34231da177e4SLinus Torvalds 		/* Waiting queue */
34241da177e4SLinus Torvalds 		list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
34251da177e4SLinus Torvalds 			int result;
34261da177e4SLinus Torvalds 			p = srb->cmd;
34271da177e4SLinus Torvalds 
34281da177e4SLinus Torvalds 			result = MK_RES(0, did_flag, 0, 0);
34295cd049a5SChristoph Hellwig 			printk("W:%p<%02i-%i>", p, p->device->id,
34309cb78c16SHannes Reinecke 			       (u8)p->device->lun);
3431cd29660dSChristoph Hellwig 			list_move_tail(&srb->list, &acb->srb_free_list);
34321da177e4SLinus Torvalds 			p->result = result;
34331da177e4SLinus Torvalds 			pci_unmap_srb_sense(acb, srb);
34341da177e4SLinus Torvalds 			pci_unmap_srb(acb, srb);
34351da177e4SLinus Torvalds 			if (force) {
34361da177e4SLinus Torvalds 				/* For new EH, we normally don't need to give commands back,
34371da177e4SLinus Torvalds 				 * as they all complete or all time out */
34381da177e4SLinus Torvalds 				cmd->scsi_done(cmd);
34391da177e4SLinus Torvalds 			}
34401da177e4SLinus Torvalds 		}
34411da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_waiting_list))
34421da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
34431da177e4SLinus Torvalds 			     list_size(&dcb->srb_waiting_list), dcb->target_id,
34441da177e4SLinus Torvalds 			     dcb->target_lun);
34451da177e4SLinus Torvalds 		dcb->flag &= ~ABORT_DEV_;
34461da177e4SLinus Torvalds 	}
34471da177e4SLinus Torvalds 	printk("\n");
34481da177e4SLinus Torvalds }
34491da177e4SLinus Torvalds 
34501da177e4SLinus Torvalds 
34511da177e4SLinus Torvalds static void reset_scsi_bus(struct AdapterCtlBlk *acb)
34521da177e4SLinus Torvalds {
34531da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
34541da177e4SLinus Torvalds 	acb->acb_flag |= RESET_DEV;	/* RESET_DETECT, RESET_DONE, RESET_DEV */
34551da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
34561da177e4SLinus Torvalds 
34571da177e4SLinus Torvalds 	while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
34581da177e4SLinus Torvalds 		/* nothing */;
34591da177e4SLinus Torvalds }
34601da177e4SLinus Torvalds 
34611da177e4SLinus Torvalds 
34621da177e4SLinus Torvalds static void set_basic_config(struct AdapterCtlBlk *acb)
34631da177e4SLinus Torvalds {
34641da177e4SLinus Torvalds 	u8 bval;
34651da177e4SLinus Torvalds 	u16 wval;
34661da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
34671da177e4SLinus Torvalds 	if (acb->config & HCC_PARITY)
34681da177e4SLinus Torvalds 		bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
34691da177e4SLinus Torvalds 	else
34701da177e4SLinus Torvalds 		bval = PHASELATCH | INITIATOR | BLOCKRST;
34711da177e4SLinus Torvalds 
34721da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
34731da177e4SLinus Torvalds 
34741da177e4SLinus Torvalds 	/* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
34751da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03);	/* was 0x13: default */
34761da177e4SLinus Torvalds 	/* program Host ID                  */
34771da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
34781da177e4SLinus Torvalds 	/* set ansynchronous transfer       */
34791da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
34801da177e4SLinus Torvalds 	/* Turn LED control off */
34811da177e4SLinus Torvalds 	wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
34821da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
34831da177e4SLinus Torvalds 	/* DMA config          */
34841da177e4SLinus Torvalds 	wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
34851da177e4SLinus Torvalds 	wval |=
34861da177e4SLinus Torvalds 	    DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
34871da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
34881da177e4SLinus Torvalds 	/* Clear pending interrupt status */
34891da177e4SLinus Torvalds 	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
34901da177e4SLinus Torvalds 	/* Enable SCSI interrupt    */
34911da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
34921da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
34931da177e4SLinus Torvalds 		      /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
34941da177e4SLinus Torvalds 		      );
34951da177e4SLinus Torvalds }
34961da177e4SLinus Torvalds 
34971da177e4SLinus Torvalds 
34981da177e4SLinus Torvalds static void scsi_reset_detect(struct AdapterCtlBlk *acb)
34991da177e4SLinus Torvalds {
35001da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
35011da177e4SLinus Torvalds 	/* delay half a second */
35021da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
35031da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
35041da177e4SLinus Torvalds 
35051da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
35061da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
35071da177e4SLinus Torvalds 	/*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
35081da177e4SLinus Torvalds 	udelay(500);
35091da177e4SLinus Torvalds 	/* Maybe we locked up the bus? Then lets wait even longer ... */
351053a8936cSHannes Reinecke 	acb->last_reset =
35111da177e4SLinus Torvalds 	    jiffies + 5 * HZ / 2 +
35121da177e4SLinus Torvalds 	    HZ * acb->eeprom.delay_time;
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds 	clear_fifo(acb, "scsi_reset_detect");
35151da177e4SLinus Torvalds 	set_basic_config(acb);
35161da177e4SLinus Torvalds 	/*1.25 */
35171da177e4SLinus Torvalds 	/*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
35181da177e4SLinus Torvalds 
35191da177e4SLinus Torvalds 	if (acb->acb_flag & RESET_DEV) {	/* RESET_DETECT, RESET_DONE, RESET_DEV */
35201da177e4SLinus Torvalds 		acb->acb_flag |= RESET_DONE;
35211da177e4SLinus Torvalds 	} else {
35221da177e4SLinus Torvalds 		acb->acb_flag |= RESET_DETECT;
35231da177e4SLinus Torvalds 		reset_dev_param(acb);
35241da177e4SLinus Torvalds 		doing_srb_done(acb, DID_RESET, NULL, 1);
35251da177e4SLinus Torvalds 		/*DC395x_RecoverSRB( acb ); */
35261da177e4SLinus Torvalds 		acb->active_dcb = NULL;
35271da177e4SLinus Torvalds 		acb->acb_flag = 0;
35281da177e4SLinus Torvalds 		waiting_process_next(acb);
35291da177e4SLinus Torvalds 	}
35301da177e4SLinus Torvalds }
35311da177e4SLinus Torvalds 
35321da177e4SLinus Torvalds 
35331da177e4SLinus Torvalds static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
35341da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
35351da177e4SLinus Torvalds {
35361da177e4SLinus Torvalds 	struct scsi_cmnd *cmd = srb->cmd;
35375cd049a5SChristoph Hellwig 	dprintkdbg(DBG_1, "request_sense: (0x%p) <%02i-%i>\n",
35389cb78c16SHannes Reinecke 		cmd, cmd->device->id, (u8)cmd->device->lun);
35391da177e4SLinus Torvalds 
35401da177e4SLinus Torvalds 	srb->flag |= AUTO_REQSENSE;
35411da177e4SLinus Torvalds 	srb->adapter_status = 0;
35421da177e4SLinus Torvalds 	srb->target_status = 0;
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds 	/* KG: Can this prevent crap sense data ? */
3545b80ca4f7SFUJITA Tomonori 	memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 	/* Save some data */
35481da177e4SLinus Torvalds 	srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
35491da177e4SLinus Torvalds 	    srb->segment_x[0].address;
35501da177e4SLinus Torvalds 	srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
35511da177e4SLinus Torvalds 	    srb->segment_x[0].length;
35521da177e4SLinus Torvalds 	srb->xferred = srb->total_xfer_length;
35531da177e4SLinus Torvalds 	/* srb->segment_x : a one entry of S/G list table */
3554b80ca4f7SFUJITA Tomonori 	srb->total_xfer_length = SCSI_SENSE_BUFFERSIZE;
3555b80ca4f7SFUJITA Tomonori 	srb->segment_x[0].length = SCSI_SENSE_BUFFERSIZE;
35561da177e4SLinus Torvalds 	/* Map sense buffer */
3557dfda5e21SChristoph Hellwig 	srb->segment_x[0].address = dma_map_single(&acb->dev->dev,
3558dfda5e21SChristoph Hellwig 			cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
3559dfda5e21SChristoph Hellwig 			DMA_FROM_DEVICE);
35601da177e4SLinus Torvalds 	dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
35611da177e4SLinus Torvalds 	       cmd->sense_buffer, srb->segment_x[0].address,
3562b80ca4f7SFUJITA Tomonori 	       SCSI_SENSE_BUFFERSIZE);
35631da177e4SLinus Torvalds 	srb->sg_count = 1;
35641da177e4SLinus Torvalds 	srb->sg_index = 0;
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds 	if (start_scsi(acb, dcb, srb)) {	/* Should only happen, if sb. else grabs the bus */
35671da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
35685cd049a5SChristoph Hellwig 			"request_sense: (0x%p) failed <%02i-%i>\n",
35695cd049a5SChristoph Hellwig 			srb->cmd, dcb->target_id, dcb->target_lun);
3570cd29660dSChristoph Hellwig 		list_move(&srb->list, &dcb->srb_waiting_list);
35711da177e4SLinus Torvalds 		waiting_set_timer(acb, HZ / 100);
35721da177e4SLinus Torvalds 	}
35731da177e4SLinus Torvalds }
35741da177e4SLinus Torvalds 
35751da177e4SLinus Torvalds 
35761da177e4SLinus Torvalds /**
35771da177e4SLinus Torvalds  * device_alloc - Allocate a new device instance. This create the
35781da177e4SLinus Torvalds  * devices instance and sets up all the data items. The adapter
35791da177e4SLinus Torvalds  * instance is required to obtain confiuration information for this
35801da177e4SLinus Torvalds  * device. This does *not* add this device to the adapters device
35811da177e4SLinus Torvalds  * list.
35821da177e4SLinus Torvalds  *
35831da177e4SLinus Torvalds  * @acb: The adapter to obtain configuration information from.
35841da177e4SLinus Torvalds  * @target: The target for the new device.
35851da177e4SLinus Torvalds  * @lun: The lun for the new device.
35861da177e4SLinus Torvalds  *
3587d6e05edcSAndreas Mohr  * Return the new device if successful or NULL on failure.
35881da177e4SLinus Torvalds  **/
35891da177e4SLinus Torvalds static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
35901da177e4SLinus Torvalds 		u8 target, u8 lun)
35911da177e4SLinus Torvalds {
35921da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
35931da177e4SLinus Torvalds 	u8 period_index = eeprom->target[target].period & 0x07;
35941da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
35951da177e4SLinus Torvalds 
35961da177e4SLinus Torvalds 	dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
35971da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
35981da177e4SLinus Torvalds 	if (!dcb)
35991da177e4SLinus Torvalds 		return NULL;
36001da177e4SLinus Torvalds 	dcb->acb = NULL;
36011da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dcb->srb_going_list);
36021da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dcb->srb_waiting_list);
36031da177e4SLinus Torvalds 	dcb->active_srb = NULL;
36041da177e4SLinus Torvalds 	dcb->tag_mask = 0;
36051da177e4SLinus Torvalds 	dcb->max_command = 1;
36061da177e4SLinus Torvalds 	dcb->target_id = target;
36071da177e4SLinus Torvalds 	dcb->target_lun = lun;
3608208afec4SDan Carpenter 	dcb->dev_mode = eeprom->target[target].cfg0;
36091da177e4SLinus Torvalds #ifndef DC395x_NO_DISCONNECT
36101da177e4SLinus Torvalds 	dcb->identify_msg =
36111da177e4SLinus Torvalds 	    IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
36121da177e4SLinus Torvalds #else
36131da177e4SLinus Torvalds 	dcb->identify_msg = IDENTIFY(0, lun);
36141da177e4SLinus Torvalds #endif
36151da177e4SLinus Torvalds 	dcb->inquiry7 = 0;
36161da177e4SLinus Torvalds 	dcb->sync_mode = 0;
36171da177e4SLinus Torvalds 	dcb->min_nego_period = clock_period[period_index];
36181da177e4SLinus Torvalds 	dcb->sync_period = 0;
36191da177e4SLinus Torvalds 	dcb->sync_offset = 0;
36201da177e4SLinus Torvalds 	dcb->flag = 0;
36211da177e4SLinus Torvalds 
36221da177e4SLinus Torvalds #ifndef DC395x_NO_WIDE
36231da177e4SLinus Torvalds 	if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
36241da177e4SLinus Torvalds 	    && (acb->config & HCC_WIDE_CARD))
36251da177e4SLinus Torvalds 		dcb->sync_mode |= WIDE_NEGO_ENABLE;
36261da177e4SLinus Torvalds #endif
36271da177e4SLinus Torvalds #ifndef DC395x_NO_SYNC
36281da177e4SLinus Torvalds 	if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
36291da177e4SLinus Torvalds 		if (!(lun) || current_sync_offset)
36301da177e4SLinus Torvalds 			dcb->sync_mode |= SYNC_NEGO_ENABLE;
36311da177e4SLinus Torvalds #endif
36321da177e4SLinus Torvalds 	if (dcb->target_lun != 0) {
36331da177e4SLinus Torvalds 		/* Copy settings */
36341da177e4SLinus Torvalds 		struct DeviceCtlBlk *p;
36351da177e4SLinus Torvalds 		list_for_each_entry(p, &acb->dcb_list, list)
36361da177e4SLinus Torvalds 			if (p->target_id == dcb->target_id)
36371da177e4SLinus Torvalds 				break;
36381da177e4SLinus Torvalds 		dprintkdbg(DBG_1,
36391da177e4SLinus Torvalds 		       "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
36401da177e4SLinus Torvalds 		       dcb->target_id, dcb->target_lun,
36411da177e4SLinus Torvalds 		       p->target_id, p->target_lun);
36421da177e4SLinus Torvalds 		dcb->sync_mode = p->sync_mode;
36431da177e4SLinus Torvalds 		dcb->sync_period = p->sync_period;
36441da177e4SLinus Torvalds 		dcb->min_nego_period = p->min_nego_period;
36451da177e4SLinus Torvalds 		dcb->sync_offset = p->sync_offset;
36461da177e4SLinus Torvalds 		dcb->inquiry7 = p->inquiry7;
36471da177e4SLinus Torvalds 	}
36481da177e4SLinus Torvalds 	return dcb;
36491da177e4SLinus Torvalds }
36501da177e4SLinus Torvalds 
36511da177e4SLinus Torvalds 
36521da177e4SLinus Torvalds /**
36531da177e4SLinus Torvalds  * adapter_add_device - Adds the device instance to the adaptor instance.
36541da177e4SLinus Torvalds  *
36551da177e4SLinus Torvalds  * @acb: The adapter device to be updated
3656b595076aSUwe Kleine-König  * @dcb: A newly created and initialised device instance to add.
36571da177e4SLinus Torvalds  **/
36581da177e4SLinus Torvalds static void adapter_add_device(struct AdapterCtlBlk *acb,
36591da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
36601da177e4SLinus Torvalds {
36611da177e4SLinus Torvalds 	/* backpointer to adapter */
36621da177e4SLinus Torvalds 	dcb->acb = acb;
36631da177e4SLinus Torvalds 
36641da177e4SLinus Torvalds 	/* set run_robin to this device if it is currently empty */
36651da177e4SLinus Torvalds 	if (list_empty(&acb->dcb_list))
36661da177e4SLinus Torvalds 		acb->dcb_run_robin = dcb;
36671da177e4SLinus Torvalds 
36681da177e4SLinus Torvalds 	/* add device to list */
36691da177e4SLinus Torvalds 	list_add_tail(&dcb->list, &acb->dcb_list);
36701da177e4SLinus Torvalds 
36711da177e4SLinus Torvalds 	/* update device maps */
36721da177e4SLinus Torvalds 	acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
36731da177e4SLinus Torvalds 	acb->children[dcb->target_id][dcb->target_lun] = dcb;
36741da177e4SLinus Torvalds }
36751da177e4SLinus Torvalds 
36761da177e4SLinus Torvalds 
36771da177e4SLinus Torvalds /**
36781da177e4SLinus Torvalds  * adapter_remove_device - Removes the device instance from the adaptor
36791da177e4SLinus Torvalds  * instance. The device instance is not check in any way or freed by this.
36801da177e4SLinus Torvalds  * The caller is expected to take care of that. This will simply remove the
36811da177e4SLinus Torvalds  * device from the adapters data strcutures.
36821da177e4SLinus Torvalds  *
36831da177e4SLinus Torvalds  * @acb: The adapter device to be updated
36841da177e4SLinus Torvalds  * @dcb: A device that has previously been added to the adapter.
36851da177e4SLinus Torvalds  **/
36861da177e4SLinus Torvalds static void adapter_remove_device(struct AdapterCtlBlk *acb,
36871da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
36881da177e4SLinus Torvalds {
36891da177e4SLinus Torvalds 	struct DeviceCtlBlk *i;
36901da177e4SLinus Torvalds 	struct DeviceCtlBlk *tmp;
36911da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
36921da177e4SLinus Torvalds 		dcb->target_id, dcb->target_lun);
36931da177e4SLinus Torvalds 
36941da177e4SLinus Torvalds 	/* fix up any pointers to this device that we have in the adapter */
36951da177e4SLinus Torvalds 	if (acb->active_dcb == dcb)
36961da177e4SLinus Torvalds 		acb->active_dcb = NULL;
36971da177e4SLinus Torvalds 	if (acb->dcb_run_robin == dcb)
36981da177e4SLinus Torvalds 		acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
36991da177e4SLinus Torvalds 
37001da177e4SLinus Torvalds 	/* unlink from list */
37011da177e4SLinus Torvalds 	list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
37021da177e4SLinus Torvalds 		if (dcb == i) {
37031da177e4SLinus Torvalds 			list_del(&i->list);
37041da177e4SLinus Torvalds 			break;
37051da177e4SLinus Torvalds 		}
37061da177e4SLinus Torvalds 
37071da177e4SLinus Torvalds 	/* clear map and children */
37081da177e4SLinus Torvalds 	acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
37091da177e4SLinus Torvalds 	acb->children[dcb->target_id][dcb->target_lun] = NULL;
37101da177e4SLinus Torvalds 	dcb->acb = NULL;
37111da177e4SLinus Torvalds }
37121da177e4SLinus Torvalds 
37131da177e4SLinus Torvalds 
37141da177e4SLinus Torvalds /**
37151da177e4SLinus Torvalds  * adapter_remove_and_free_device - Removes a single device from the adapter
37161da177e4SLinus Torvalds  * and then frees the device information.
37171da177e4SLinus Torvalds  *
37181da177e4SLinus Torvalds  * @acb: The adapter device to be updated
37191da177e4SLinus Torvalds  * @dcb: A device that has previously been added to the adapter.
37201da177e4SLinus Torvalds  */
37211da177e4SLinus Torvalds static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
37221da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
37231da177e4SLinus Torvalds {
37241da177e4SLinus Torvalds 	if (list_size(&dcb->srb_going_list) > 1) {
37251da177e4SLinus Torvalds 		dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
37261da177e4SLinus Torvalds 		           "Won't remove because of %i active requests.\n",
37271da177e4SLinus Torvalds 			   dcb->target_id, dcb->target_lun,
37281da177e4SLinus Torvalds 			   list_size(&dcb->srb_going_list));
37291da177e4SLinus Torvalds 		return;
37301da177e4SLinus Torvalds 	}
37311da177e4SLinus Torvalds 	adapter_remove_device(acb, dcb);
37321da177e4SLinus Torvalds 	kfree(dcb);
37331da177e4SLinus Torvalds }
37341da177e4SLinus Torvalds 
37351da177e4SLinus Torvalds 
37361da177e4SLinus Torvalds /**
37371da177e4SLinus Torvalds  * adapter_remove_and_free_all_devices - Removes and frees all of the
37381da177e4SLinus Torvalds  * devices associated with the specified adapter.
37391da177e4SLinus Torvalds  *
37401da177e4SLinus Torvalds  * @acb: The adapter from which all devices should be removed.
37411da177e4SLinus Torvalds  **/
37421da177e4SLinus Torvalds static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
37431da177e4SLinus Torvalds {
37441da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
37451da177e4SLinus Torvalds 	struct DeviceCtlBlk *tmp;
37461da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
37471da177e4SLinus Torvalds 		   list_size(&acb->dcb_list));
37481da177e4SLinus Torvalds 
37491da177e4SLinus Torvalds 	list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
37501da177e4SLinus Torvalds 		adapter_remove_and_free_device(acb, dcb);
37511da177e4SLinus Torvalds }
37521da177e4SLinus Torvalds 
37531da177e4SLinus Torvalds 
37541da177e4SLinus Torvalds /**
37551da177e4SLinus Torvalds  * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
37561da177e4SLinus Torvalds  * scsi device that we need to deal with. We allocate a new device and then
37571da177e4SLinus Torvalds  * insert that device into the adapters device list.
37581da177e4SLinus Torvalds  *
37591da177e4SLinus Torvalds  * @scsi_device: The new scsi device that we need to handle.
37601da177e4SLinus Torvalds  **/
37611da177e4SLinus Torvalds static int dc395x_slave_alloc(struct scsi_device *scsi_device)
37621da177e4SLinus Torvalds {
37631da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
37641da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
37651da177e4SLinus Torvalds 
37661da177e4SLinus Torvalds 	dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
37671da177e4SLinus Torvalds 	if (!dcb)
37681da177e4SLinus Torvalds 		return -ENOMEM;
37691da177e4SLinus Torvalds 	adapter_add_device(acb, dcb);
37701da177e4SLinus Torvalds 
37711da177e4SLinus Torvalds 	return 0;
37721da177e4SLinus Torvalds }
37731da177e4SLinus Torvalds 
37741da177e4SLinus Torvalds 
37751da177e4SLinus Torvalds /**
37761da177e4SLinus Torvalds  * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
37771da177e4SLinus Torvalds  * device that is going away.
37781da177e4SLinus Torvalds  *
37791da177e4SLinus Torvalds  * @scsi_device: The new scsi device that we need to handle.
37801da177e4SLinus Torvalds  **/
37811da177e4SLinus Torvalds static void dc395x_slave_destroy(struct scsi_device *scsi_device)
37821da177e4SLinus Torvalds {
37831da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
37841da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
37851da177e4SLinus Torvalds 	if (dcb)
37861da177e4SLinus Torvalds 		adapter_remove_and_free_device(acb, dcb);
37871da177e4SLinus Torvalds }
37881da177e4SLinus Torvalds 
37891da177e4SLinus Torvalds 
37901da177e4SLinus Torvalds 
37911da177e4SLinus Torvalds 
37921da177e4SLinus Torvalds /**
37931da177e4SLinus Torvalds  * trms1040_wait_30us: wait for 30 us
37941da177e4SLinus Torvalds  *
37951da177e4SLinus Torvalds  * Waits for 30us (using the chip by the looks of it..)
37961da177e4SLinus Torvalds  *
37971da177e4SLinus Torvalds  * @io_port: base I/O address
37981da177e4SLinus Torvalds  **/
37996f039790SGreg Kroah-Hartman static void trms1040_wait_30us(unsigned long io_port)
38001da177e4SLinus Torvalds {
38011da177e4SLinus Torvalds 	/* ScsiPortStallExecution(30); wait 30 us */
38021da177e4SLinus Torvalds 	outb(5, io_port + TRM_S1040_GEN_TIMER);
38031da177e4SLinus Torvalds 	while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
38041da177e4SLinus Torvalds 		/* nothing */ ;
38051da177e4SLinus Torvalds }
38061da177e4SLinus Torvalds 
38071da177e4SLinus Torvalds 
38081da177e4SLinus Torvalds /**
38091da177e4SLinus Torvalds  * trms1040_write_cmd - write the secified command and address to
38101da177e4SLinus Torvalds  * chip
38111da177e4SLinus Torvalds  *
38121da177e4SLinus Torvalds  * @io_port:	base I/O address
38131da177e4SLinus Torvalds  * @cmd:	SB + op code (command) to send
38141da177e4SLinus Torvalds  * @addr:	address to send
38151da177e4SLinus Torvalds  **/
38166f039790SGreg Kroah-Hartman static void trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
38171da177e4SLinus Torvalds {
38181da177e4SLinus Torvalds 	int i;
38191da177e4SLinus Torvalds 	u8 send_data;
38201da177e4SLinus Torvalds 
38211da177e4SLinus Torvalds 	/* program SB + OP code */
38221da177e4SLinus Torvalds 	for (i = 0; i < 3; i++, cmd <<= 1) {
38231da177e4SLinus Torvalds 		send_data = NVR_SELECT;
38241da177e4SLinus Torvalds 		if (cmd & 0x04)	/* Start from bit 2 */
38251da177e4SLinus Torvalds 			send_data |= NVR_BITOUT;
38261da177e4SLinus Torvalds 
38271da177e4SLinus Torvalds 		outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
38281da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38291da177e4SLinus Torvalds 		outb((send_data | NVR_CLOCK),
38301da177e4SLinus Torvalds 		     io_port + TRM_S1040_GEN_NVRAM);
38311da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38321da177e4SLinus Torvalds 	}
38331da177e4SLinus Torvalds 
38341da177e4SLinus Torvalds 	/* send address */
38351da177e4SLinus Torvalds 	for (i = 0; i < 7; i++, addr <<= 1) {
38361da177e4SLinus Torvalds 		send_data = NVR_SELECT;
38371da177e4SLinus Torvalds 		if (addr & 0x40)	/* Start from bit 6 */
38381da177e4SLinus Torvalds 			send_data |= NVR_BITOUT;
38391da177e4SLinus Torvalds 
38401da177e4SLinus Torvalds 		outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
38411da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38421da177e4SLinus Torvalds 		outb((send_data | NVR_CLOCK),
38431da177e4SLinus Torvalds 		     io_port + TRM_S1040_GEN_NVRAM);
38441da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38451da177e4SLinus Torvalds 	}
38461da177e4SLinus Torvalds 	outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
38471da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
38481da177e4SLinus Torvalds }
38491da177e4SLinus Torvalds 
38501da177e4SLinus Torvalds 
38511da177e4SLinus Torvalds /**
38521da177e4SLinus Torvalds  * trms1040_set_data - store a single byte in the eeprom
38531da177e4SLinus Torvalds  *
38541da177e4SLinus Torvalds  * Called from write all to write a single byte into the SSEEPROM
38551da177e4SLinus Torvalds  * Which is done one bit at a time.
38561da177e4SLinus Torvalds  *
38571da177e4SLinus Torvalds  * @io_port:	base I/O address
38581da177e4SLinus Torvalds  * @addr:	offset into EEPROM
38591da177e4SLinus Torvalds  * @byte:	bytes to write
38601da177e4SLinus Torvalds  **/
38616f039790SGreg Kroah-Hartman static void trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
38621da177e4SLinus Torvalds {
38631da177e4SLinus Torvalds 	int i;
38641da177e4SLinus Torvalds 	u8 send_data;
38651da177e4SLinus Torvalds 
38661da177e4SLinus Torvalds 	/* Send write command & address */
38671da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x05, addr);
38681da177e4SLinus Torvalds 
38691da177e4SLinus Torvalds 	/* Write data */
38701da177e4SLinus Torvalds 	for (i = 0; i < 8; i++, byte <<= 1) {
38711da177e4SLinus Torvalds 		send_data = NVR_SELECT;
38721da177e4SLinus Torvalds 		if (byte & 0x80)	/* Start from bit 7 */
38731da177e4SLinus Torvalds 			send_data |= NVR_BITOUT;
38741da177e4SLinus Torvalds 
38751da177e4SLinus Torvalds 		outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
38761da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38771da177e4SLinus Torvalds 		outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
38781da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38791da177e4SLinus Torvalds 	}
38801da177e4SLinus Torvalds 	outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
38811da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
38821da177e4SLinus Torvalds 
38831da177e4SLinus Torvalds 	/* Disable chip select */
38841da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
38851da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
38861da177e4SLinus Torvalds 
38871da177e4SLinus Torvalds 	outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
38881da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
38891da177e4SLinus Torvalds 
38901da177e4SLinus Torvalds 	/* Wait for write ready */
38911da177e4SLinus Torvalds 	while (1) {
38921da177e4SLinus Torvalds 		outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
38931da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38941da177e4SLinus Torvalds 
38951da177e4SLinus Torvalds 		outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
38961da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38971da177e4SLinus Torvalds 
38981da177e4SLinus Torvalds 		if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
38991da177e4SLinus Torvalds 			break;
39001da177e4SLinus Torvalds 	}
39011da177e4SLinus Torvalds 
39021da177e4SLinus Torvalds 	/*  Disable chip select */
39031da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
39041da177e4SLinus Torvalds }
39051da177e4SLinus Torvalds 
39061da177e4SLinus Torvalds 
39071da177e4SLinus Torvalds /**
39081da177e4SLinus Torvalds  * trms1040_write_all - write 128 bytes to the eeprom
39091da177e4SLinus Torvalds  *
39101da177e4SLinus Torvalds  * Write the supplied 128 bytes to the chips SEEPROM
39111da177e4SLinus Torvalds  *
39121da177e4SLinus Torvalds  * @eeprom:	the data to write
39131da177e4SLinus Torvalds  * @io_port:	the base io port
39141da177e4SLinus Torvalds  **/
39156f039790SGreg Kroah-Hartman static void trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
39161da177e4SLinus Torvalds {
39171da177e4SLinus Torvalds 	u8 *b_eeprom = (u8 *)eeprom;
39181da177e4SLinus Torvalds 	u8 addr;
39191da177e4SLinus Torvalds 
39201da177e4SLinus Torvalds 	/* Enable SEEPROM */
39211da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
39221da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
39231da177e4SLinus Torvalds 
39241da177e4SLinus Torvalds 	/* write enable */
39251da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x04, 0xFF);
39261da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
39271da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
39281da177e4SLinus Torvalds 
39291da177e4SLinus Torvalds 	/* write */
39301da177e4SLinus Torvalds 	for (addr = 0; addr < 128; addr++, b_eeprom++)
39311da177e4SLinus Torvalds 		trms1040_set_data(io_port, addr, *b_eeprom);
39321da177e4SLinus Torvalds 
39331da177e4SLinus Torvalds 	/* write disable */
39341da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x04, 0x00);
39351da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
39361da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
39371da177e4SLinus Torvalds 
39381da177e4SLinus Torvalds 	/* Disable SEEPROM */
39391da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
39401da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
39411da177e4SLinus Torvalds }
39421da177e4SLinus Torvalds 
39431da177e4SLinus Torvalds 
39441da177e4SLinus Torvalds /**
39451da177e4SLinus Torvalds  * trms1040_get_data - get a single byte from the eeprom
39461da177e4SLinus Torvalds  *
39471da177e4SLinus Torvalds  * Called from read all to read a single byte into the SSEEPROM
39481da177e4SLinus Torvalds  * Which is done one bit at a time.
39491da177e4SLinus Torvalds  *
39501da177e4SLinus Torvalds  * @io_port:	base I/O address
39511da177e4SLinus Torvalds  * @addr:	offset into SEEPROM
39521da177e4SLinus Torvalds  *
395359c51591SMichael Opdenacker  * Returns the byte read.
39541da177e4SLinus Torvalds  **/
39556f039790SGreg Kroah-Hartman static u8 trms1040_get_data(unsigned long io_port, u8 addr)
39561da177e4SLinus Torvalds {
39571da177e4SLinus Torvalds 	int i;
39581da177e4SLinus Torvalds 	u8 read_byte;
39591da177e4SLinus Torvalds 	u8 result = 0;
39601da177e4SLinus Torvalds 
39611da177e4SLinus Torvalds 	/* Send read command & address */
39621da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x06, addr);
39631da177e4SLinus Torvalds 
39641da177e4SLinus Torvalds 	/* read data */
39651da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
39661da177e4SLinus Torvalds 		outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
39671da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
39681da177e4SLinus Torvalds 		outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
39691da177e4SLinus Torvalds 
39701da177e4SLinus Torvalds 		/* Get data bit while falling edge */
39711da177e4SLinus Torvalds 		read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
39721da177e4SLinus Torvalds 		result <<= 1;
39731da177e4SLinus Torvalds 		if (read_byte & NVR_BITIN)
39741da177e4SLinus Torvalds 			result |= 1;
39751da177e4SLinus Torvalds 
39761da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
39771da177e4SLinus Torvalds 	}
39781da177e4SLinus Torvalds 
39791da177e4SLinus Torvalds 	/* Disable chip select */
39801da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
39811da177e4SLinus Torvalds 	return result;
39821da177e4SLinus Torvalds }
39831da177e4SLinus Torvalds 
39841da177e4SLinus Torvalds 
39851da177e4SLinus Torvalds /**
39861da177e4SLinus Torvalds  * trms1040_read_all - read all bytes from the eeprom
39871da177e4SLinus Torvalds  *
39881da177e4SLinus Torvalds  * Read the 128 bytes from the SEEPROM.
39891da177e4SLinus Torvalds  *
39901da177e4SLinus Torvalds  * @eeprom:	where to store the data
39911da177e4SLinus Torvalds  * @io_port:	the base io port
39921da177e4SLinus Torvalds  **/
39936f039790SGreg Kroah-Hartman static void trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
39941da177e4SLinus Torvalds {
39951da177e4SLinus Torvalds 	u8 *b_eeprom = (u8 *)eeprom;
39961da177e4SLinus Torvalds 	u8 addr;
39971da177e4SLinus Torvalds 
39981da177e4SLinus Torvalds 	/* Enable SEEPROM */
39991da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
40001da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
40011da177e4SLinus Torvalds 
40021da177e4SLinus Torvalds 	/* read details */
40031da177e4SLinus Torvalds 	for (addr = 0; addr < 128; addr++, b_eeprom++)
40041da177e4SLinus Torvalds 		*b_eeprom = trms1040_get_data(io_port, addr);
40051da177e4SLinus Torvalds 
40061da177e4SLinus Torvalds 	/* Disable SEEPROM */
40071da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
40081da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
40091da177e4SLinus Torvalds }
40101da177e4SLinus Torvalds 
40111da177e4SLinus Torvalds 
40121da177e4SLinus Torvalds 
40131da177e4SLinus Torvalds /**
40141da177e4SLinus Torvalds  * check_eeprom - get and check contents of the eeprom
40151da177e4SLinus Torvalds  *
40161da177e4SLinus Torvalds  * Read seeprom 128 bytes into the memory provider in eeprom.
40171da177e4SLinus Torvalds  * Checks the checksum and if it's not correct it uses a set of default
40181da177e4SLinus Torvalds  * values.
40191da177e4SLinus Torvalds  *
40201da177e4SLinus Torvalds  * @eeprom:	caller allocated strcuture to read the eeprom data into
40211da177e4SLinus Torvalds  * @io_port:	io port to read from
40221da177e4SLinus Torvalds  **/
40236f039790SGreg Kroah-Hartman static void check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
40241da177e4SLinus Torvalds {
40251da177e4SLinus Torvalds 	u16 *w_eeprom = (u16 *)eeprom;
40261da177e4SLinus Torvalds 	u16 w_addr;
40271da177e4SLinus Torvalds 	u16 cksum;
40281da177e4SLinus Torvalds 	u32 d_addr;
40291da177e4SLinus Torvalds 	u32 *d_eeprom;
40301da177e4SLinus Torvalds 
40311da177e4SLinus Torvalds 	trms1040_read_all(eeprom, io_port);	/* read eeprom */
40321da177e4SLinus Torvalds 
40331da177e4SLinus Torvalds 	cksum = 0;
40341da177e4SLinus Torvalds 	for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
40351da177e4SLinus Torvalds 	     w_addr++, w_eeprom++)
40361da177e4SLinus Torvalds 		cksum += *w_eeprom;
40371da177e4SLinus Torvalds 	if (cksum != 0x1234) {
40381da177e4SLinus Torvalds 		/*
40391da177e4SLinus Torvalds 		 * Checksum is wrong.
40401da177e4SLinus Torvalds 		 * Load a set of defaults into the eeprom buffer
40411da177e4SLinus Torvalds 		 */
40421da177e4SLinus Torvalds 		dprintkl(KERN_WARNING,
40431da177e4SLinus Torvalds 			"EEProm checksum error: using default values and options.\n");
40441da177e4SLinus Torvalds 		eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
40451da177e4SLinus Torvalds 		eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
40461da177e4SLinus Torvalds 		eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
40471da177e4SLinus Torvalds 		eeprom->sub_sys_id[1] =
40481da177e4SLinus Torvalds 		    (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
40491da177e4SLinus Torvalds 		eeprom->sub_class = 0x00;
40501da177e4SLinus Torvalds 		eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
40511da177e4SLinus Torvalds 		eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
40521da177e4SLinus Torvalds 		eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
40531da177e4SLinus Torvalds 		eeprom->device_id[1] =
40541da177e4SLinus Torvalds 		    (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
40551da177e4SLinus Torvalds 		eeprom->reserved = 0x00;
40561da177e4SLinus Torvalds 
40571da177e4SLinus Torvalds 		for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
40581da177e4SLinus Torvalds 		     d_addr < 16; d_addr++, d_eeprom++)
40591da177e4SLinus Torvalds 			*d_eeprom = 0x00000077;	/* cfg3,cfg2,period,cfg0 */
40601da177e4SLinus Torvalds 
40611da177e4SLinus Torvalds 		*d_eeprom++ = 0x04000F07;	/* max_tag,delay_time,channel_cfg,scsi_id */
40621da177e4SLinus Torvalds 		*d_eeprom++ = 0x00000015;	/* reserved1,boot_lun,boot_target,reserved0 */
40631da177e4SLinus Torvalds 		for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
40641da177e4SLinus Torvalds 			*d_eeprom = 0x00;
40651da177e4SLinus Torvalds 
40661da177e4SLinus Torvalds 		/* Now load defaults (maybe set by boot/module params) */
40671da177e4SLinus Torvalds 		set_safe_settings();
40681da177e4SLinus Torvalds 		fix_settings();
40691da177e4SLinus Torvalds 		eeprom_override(eeprom);
40701da177e4SLinus Torvalds 
40711da177e4SLinus Torvalds 		eeprom->cksum = 0x00;
40721da177e4SLinus Torvalds 		for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
40731da177e4SLinus Torvalds 		     w_addr < 63; w_addr++, w_eeprom++)
40741da177e4SLinus Torvalds 			cksum += *w_eeprom;
40751da177e4SLinus Torvalds 
40761da177e4SLinus Torvalds 		*w_eeprom = 0x1234 - cksum;
40771da177e4SLinus Torvalds 		trms1040_write_all(eeprom, io_port);
40781da177e4SLinus Torvalds 		eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
40791da177e4SLinus Torvalds 	} else {
40801da177e4SLinus Torvalds 		set_safe_settings();
40811da177e4SLinus Torvalds 		eeprom_index_to_delay(eeprom);
40821da177e4SLinus Torvalds 		eeprom_override(eeprom);
40831da177e4SLinus Torvalds 	}
40841da177e4SLinus Torvalds }
40851da177e4SLinus Torvalds 
40861da177e4SLinus Torvalds 
40871da177e4SLinus Torvalds /**
40881da177e4SLinus Torvalds  * print_eeprom_settings - output the eeprom settings
40891da177e4SLinus Torvalds  * to the kernel log so people can see what they were.
40901da177e4SLinus Torvalds  *
40911da177e4SLinus Torvalds  * @eeprom: The eeprom data strucutre to show details for.
40921da177e4SLinus Torvalds  **/
40936f039790SGreg Kroah-Hartman static void print_eeprom_settings(struct NvRamType *eeprom)
40941da177e4SLinus Torvalds {
40951da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
40961da177e4SLinus Torvalds 		eeprom->scsi_id,
40971da177e4SLinus Torvalds 		eeprom->target[0].period,
40981da177e4SLinus Torvalds 		clock_speed[eeprom->target[0].period] / 10,
40991da177e4SLinus Torvalds 		clock_speed[eeprom->target[0].period] % 10,
41001da177e4SLinus Torvalds 		eeprom->target[0].cfg0);
41011da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "               AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
41021da177e4SLinus Torvalds 		eeprom->channel_cfg, eeprom->max_tag,
41031da177e4SLinus Torvalds 		1 << eeprom->max_tag, eeprom->delay_time);
41041da177e4SLinus Torvalds }
41051da177e4SLinus Torvalds 
41061da177e4SLinus Torvalds 
41071da177e4SLinus Torvalds /* Free SG tables */
41081da177e4SLinus Torvalds static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
41091da177e4SLinus Torvalds {
41101da177e4SLinus Torvalds 	int i;
41111da177e4SLinus Torvalds 	const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
41121da177e4SLinus Torvalds 
41131da177e4SLinus Torvalds 	for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
41141da177e4SLinus Torvalds 		kfree(acb->srb_array[i].segment_x);
41151da177e4SLinus Torvalds }
41161da177e4SLinus Torvalds 
41171da177e4SLinus Torvalds 
41181da177e4SLinus Torvalds /*
41191da177e4SLinus Torvalds  * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
41201da177e4SLinus Torvalds  * should never cross a page boundary */
41216f039790SGreg Kroah-Hartman static int adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
41221da177e4SLinus Torvalds {
41231da177e4SLinus Torvalds 	const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
41241da177e4SLinus Torvalds 	                            *SEGMENTX_LEN;
41251da177e4SLinus Torvalds 	int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
41261da177e4SLinus Torvalds 	const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
41271da177e4SLinus Torvalds 	int srb_idx = 0;
41281da177e4SLinus Torvalds 	unsigned i = 0;
41293f649ab7SKees Cook 	struct SGentry *ptr;
41301da177e4SLinus Torvalds 
4131b4b08e58SLinus Torvalds 	for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
41321da177e4SLinus Torvalds 		acb->srb_array[i].segment_x = NULL;
41331da177e4SLinus Torvalds 
41341da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
41351da177e4SLinus Torvalds 	while (pages--) {
41365cbded58SRobert P. J. Day 		ptr = kmalloc(PAGE_SIZE, GFP_KERNEL);
41371da177e4SLinus Torvalds 		if (!ptr) {
41381da177e4SLinus Torvalds 			adapter_sg_tables_free(acb);
41391da177e4SLinus Torvalds 			return 1;
41401da177e4SLinus Torvalds 		}
41411da177e4SLinus Torvalds 		dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
41421da177e4SLinus Torvalds 			PAGE_SIZE, ptr, srb_idx);
41431da177e4SLinus Torvalds 		i = 0;
41441da177e4SLinus Torvalds 		while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
41451da177e4SLinus Torvalds 			acb->srb_array[srb_idx++].segment_x =
41461da177e4SLinus Torvalds 			    ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
41471da177e4SLinus Torvalds 	}
41481da177e4SLinus Torvalds 	if (i < srbs_per_page)
41491da177e4SLinus Torvalds 		acb->srb.segment_x =
41501da177e4SLinus Torvalds 		    ptr + (i * DC395x_MAX_SG_LISTENTRY);
41511da177e4SLinus Torvalds 	else
41521da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
41531da177e4SLinus Torvalds 	return 0;
41541da177e4SLinus Torvalds }
41551da177e4SLinus Torvalds 
41561da177e4SLinus Torvalds 
41571da177e4SLinus Torvalds 
41581da177e4SLinus Torvalds /**
41591da177e4SLinus Torvalds  * adapter_print_config - print adapter connection and termination
41601da177e4SLinus Torvalds  * config
41611da177e4SLinus Torvalds  *
41621da177e4SLinus Torvalds  * The io port in the adapter needs to have been set before calling
41631da177e4SLinus Torvalds  * this function.
41641da177e4SLinus Torvalds  *
41651da177e4SLinus Torvalds  * @acb: The adapter to print the information for.
41661da177e4SLinus Torvalds  **/
41676f039790SGreg Kroah-Hartman static void adapter_print_config(struct AdapterCtlBlk *acb)
41681da177e4SLinus Torvalds {
41691da177e4SLinus Torvalds 	u8 bval;
41701da177e4SLinus Torvalds 
41711da177e4SLinus Torvalds 	bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
41721da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "%sConnectors: ",
41731da177e4SLinus Torvalds 		((bval & WIDESCSI) ? "(Wide) " : ""));
41741da177e4SLinus Torvalds 	if (!(bval & CON5068))
41751da177e4SLinus Torvalds 		printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
41761da177e4SLinus Torvalds 	if (!(bval & CON68))
41771da177e4SLinus Torvalds 		printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
41781da177e4SLinus Torvalds 	if (!(bval & CON50))
41791da177e4SLinus Torvalds 		printk("int50 ");
41801da177e4SLinus Torvalds 	if ((bval & (CON5068 | CON50 | CON68)) ==
41811da177e4SLinus Torvalds 	    0 /*(CON5068 | CON50 | CON68) */ )
41821da177e4SLinus Torvalds 		printk(" Oops! (All 3?) ");
41831da177e4SLinus Torvalds 	bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
41841da177e4SLinus Torvalds 	printk(" Termination: ");
41851da177e4SLinus Torvalds 	if (bval & DIS_TERM)
41861da177e4SLinus Torvalds 		printk("Disabled\n");
41871da177e4SLinus Torvalds 	else {
41881da177e4SLinus Torvalds 		if (bval & AUTOTERM)
41891da177e4SLinus Torvalds 			printk("Auto ");
41901da177e4SLinus Torvalds 		if (bval & LOW8TERM)
41911da177e4SLinus Torvalds 			printk("Low ");
41921da177e4SLinus Torvalds 		if (bval & UP8TERM)
41931da177e4SLinus Torvalds 			printk("High ");
41941da177e4SLinus Torvalds 		printk("\n");
41951da177e4SLinus Torvalds 	}
41961da177e4SLinus Torvalds }
41971da177e4SLinus Torvalds 
41981da177e4SLinus Torvalds 
41991da177e4SLinus Torvalds /**
42001da177e4SLinus Torvalds  * adapter_init_params - Initialize the various parameters in the
42011da177e4SLinus Torvalds  * adapter structure. Note that the pointer to the scsi_host is set
42021da177e4SLinus Torvalds  * early (when this instance is created) and the io_port and irq
42031da177e4SLinus Torvalds  * values are set later after they have been reserved. This just gets
42041da177e4SLinus Torvalds  * everything set to a good starting position.
42051da177e4SLinus Torvalds  *
42061da177e4SLinus Torvalds  * The eeprom structure in the adapter needs to have been set before
42071da177e4SLinus Torvalds  * calling this function.
42081da177e4SLinus Torvalds  *
42091da177e4SLinus Torvalds  * @acb: The adapter to initialize.
42101da177e4SLinus Torvalds  **/
42116f039790SGreg Kroah-Hartman static void adapter_init_params(struct AdapterCtlBlk *acb)
42121da177e4SLinus Torvalds {
42131da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
42141da177e4SLinus Torvalds 	int i;
42151da177e4SLinus Torvalds 
42161da177e4SLinus Torvalds 	/* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
42171da177e4SLinus Torvalds 	/* NOTE: acb->io_port_base is set at port registration time */
42181da177e4SLinus Torvalds 	/* NOTE: acb->io_port_len is set at port registration time */
42191da177e4SLinus Torvalds 
42201da177e4SLinus Torvalds 	INIT_LIST_HEAD(&acb->dcb_list);
42211da177e4SLinus Torvalds 	acb->dcb_run_robin = NULL;
42221da177e4SLinus Torvalds 	acb->active_dcb = NULL;
42231da177e4SLinus Torvalds 
42241da177e4SLinus Torvalds 	INIT_LIST_HEAD(&acb->srb_free_list);
42251da177e4SLinus Torvalds 	/*  temp SRB for Q tag used or abort command used  */
42261da177e4SLinus Torvalds 	acb->tmp_srb = &acb->srb;
4227af53b89bSKees Cook 	timer_setup(&acb->waiting_timer, waiting_timeout, 0);
4228af53b89bSKees Cook 	timer_setup(&acb->selto_timer, NULL, 0);
42291da177e4SLinus Torvalds 
42301da177e4SLinus Torvalds 	acb->srb_count = DC395x_MAX_SRB_CNT;
42311da177e4SLinus Torvalds 
42321da177e4SLinus Torvalds 	acb->sel_timeout = DC395x_SEL_TIMEOUT;	/* timeout=250ms */
42331da177e4SLinus Torvalds 	/* NOTE: acb->irq_level is set at IRQ registration time */
42341da177e4SLinus Torvalds 
42351da177e4SLinus Torvalds 	acb->tag_max_num = 1 << eeprom->max_tag;
42361da177e4SLinus Torvalds 	if (acb->tag_max_num > 30)
42371da177e4SLinus Torvalds 		acb->tag_max_num = 30;
42381da177e4SLinus Torvalds 
42391da177e4SLinus Torvalds 	acb->acb_flag = 0;	/* RESET_DETECT, RESET_DONE, RESET_DEV */
42401da177e4SLinus Torvalds 	acb->gmode2 = eeprom->channel_cfg;
42411da177e4SLinus Torvalds 	acb->config = 0;	/* NOTE: actually set in adapter_init_chip */
42421da177e4SLinus Torvalds 
42431da177e4SLinus Torvalds 	if (eeprom->channel_cfg & NAC_SCANLUN)
42441da177e4SLinus Torvalds 		acb->lun_chk = 1;
42451da177e4SLinus Torvalds 	acb->scan_devices = 1;
42461da177e4SLinus Torvalds 
42471da177e4SLinus Torvalds 	acb->scsi_host->this_id = eeprom->scsi_id;
42481da177e4SLinus Torvalds 	acb->hostid_bit = (1 << acb->scsi_host->this_id);
42491da177e4SLinus Torvalds 
42501da177e4SLinus Torvalds 	for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
42511da177e4SLinus Torvalds 		acb->dcb_map[i] = 0;
42521da177e4SLinus Torvalds 
42531da177e4SLinus Torvalds 	acb->msg_len = 0;
42541da177e4SLinus Torvalds 
42551da177e4SLinus Torvalds 	/* link static array of srbs into the srb free list */
42561da177e4SLinus Torvalds 	for (i = 0; i < acb->srb_count - 1; i++)
4257cd29660dSChristoph Hellwig 		list_add_tail(&acb->srb_array[i].list, &acb->srb_free_list);
42581da177e4SLinus Torvalds }
42591da177e4SLinus Torvalds 
42601da177e4SLinus Torvalds 
42611da177e4SLinus Torvalds /**
42621da177e4SLinus Torvalds  * adapter_init_host - Initialize the scsi host instance based on
42631da177e4SLinus Torvalds  * values that we have already stored in the adapter instance. There's
42641da177e4SLinus Torvalds  * some mention that a lot of these are deprecated, so we won't use
42651da177e4SLinus Torvalds  * them (we'll use the ones in the adapter instance) but we'll fill
42661da177e4SLinus Torvalds  * them in in case something else needs them.
42671da177e4SLinus Torvalds  *
42681da177e4SLinus Torvalds  * The eeprom structure, irq and io ports in the adapter need to have
42691da177e4SLinus Torvalds  * been set before calling this function.
42701da177e4SLinus Torvalds  *
42711da177e4SLinus Torvalds  * @host: The scsi host instance to fill in the values for.
42721da177e4SLinus Torvalds  **/
42736f039790SGreg Kroah-Hartman static void adapter_init_scsi_host(struct Scsi_Host *host)
42741da177e4SLinus Torvalds {
42751da177e4SLinus Torvalds         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
42761da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
42771da177e4SLinus Torvalds 
42781da177e4SLinus Torvalds 	host->max_cmd_len = 24;
42791da177e4SLinus Torvalds 	host->can_queue = DC395x_MAX_CMD_QUEUE;
42801da177e4SLinus Torvalds 	host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
42811da177e4SLinus Torvalds 	host->this_id = (int)eeprom->scsi_id;
42821da177e4SLinus Torvalds 	host->io_port = acb->io_port_base;
42831da177e4SLinus Torvalds 	host->n_io_port = acb->io_port_len;
42841da177e4SLinus Torvalds 	host->dma_channel = -1;
42851da177e4SLinus Torvalds 	host->unique_id = acb->io_port_base;
42861da177e4SLinus Torvalds 	host->irq = acb->irq_level;
428753a8936cSHannes Reinecke 	acb->last_reset = jiffies;
42881da177e4SLinus Torvalds 
42891da177e4SLinus Torvalds 	host->max_id = 16;
42901da177e4SLinus Torvalds 	if (host->max_id - 1 == eeprom->scsi_id)
42911da177e4SLinus Torvalds 		host->max_id--;
42921da177e4SLinus Torvalds 
42931da177e4SLinus Torvalds 	if (eeprom->channel_cfg & NAC_SCANLUN)
42941da177e4SLinus Torvalds 		host->max_lun = 8;
42951da177e4SLinus Torvalds 	else
42961da177e4SLinus Torvalds 		host->max_lun = 1;
42971da177e4SLinus Torvalds }
42981da177e4SLinus Torvalds 
42991da177e4SLinus Torvalds 
43001da177e4SLinus Torvalds /**
43011da177e4SLinus Torvalds  * adapter_init_chip - Get the chip into a know state and figure out
43021da177e4SLinus Torvalds  * some of the settings that apply to this adapter.
43031da177e4SLinus Torvalds  *
43041da177e4SLinus Torvalds  * The io port in the adapter needs to have been set before calling
43051da177e4SLinus Torvalds  * this function. The config will be configured correctly on return.
43061da177e4SLinus Torvalds  *
43071da177e4SLinus Torvalds  * @acb: The adapter which we are to init.
43081da177e4SLinus Torvalds  **/
43096f039790SGreg Kroah-Hartman static void adapter_init_chip(struct AdapterCtlBlk *acb)
43101da177e4SLinus Torvalds {
43111da177e4SLinus Torvalds         struct NvRamType *eeprom = &acb->eeprom;
43121da177e4SLinus Torvalds 
43131da177e4SLinus Torvalds         /* Mask all the interrupt */
43141da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
43151da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
43161da177e4SLinus Torvalds 
43171da177e4SLinus Torvalds 	/* Reset SCSI module */
43181da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
43191da177e4SLinus Torvalds 
43201da177e4SLinus Torvalds 	/* Reset PCI/DMA module */
43211da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
43221da177e4SLinus Torvalds 	udelay(20);
43231da177e4SLinus Torvalds 
43241da177e4SLinus Torvalds 	/* program configuration 0 */
43251da177e4SLinus Torvalds 	acb->config = HCC_AUTOTERM | HCC_PARITY;
43261da177e4SLinus Torvalds 	if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
43271da177e4SLinus Torvalds 		acb->config |= HCC_WIDE_CARD;
43281da177e4SLinus Torvalds 
43291da177e4SLinus Torvalds 	if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
43301da177e4SLinus Torvalds 		acb->config |= HCC_SCSI_RESET;
43311da177e4SLinus Torvalds 
43321da177e4SLinus Torvalds 	if (acb->config & HCC_SCSI_RESET) {
43331da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
43341da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
43351da177e4SLinus Torvalds 
43361da177e4SLinus Torvalds 		/*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
43371da177e4SLinus Torvalds 		/*spin_unlock_irq (&io_request_lock); */
43381da177e4SLinus Torvalds 		udelay(500);
43391da177e4SLinus Torvalds 
434053a8936cSHannes Reinecke 		acb->last_reset =
43411da177e4SLinus Torvalds 		    jiffies + HZ / 2 +
43421da177e4SLinus Torvalds 		    HZ * acb->eeprom.delay_time;
43431da177e4SLinus Torvalds 
43441da177e4SLinus Torvalds 		/*spin_lock_irq (&io_request_lock); */
43451da177e4SLinus Torvalds 	}
43461da177e4SLinus Torvalds }
43471da177e4SLinus Torvalds 
43481da177e4SLinus Torvalds 
43491da177e4SLinus Torvalds /**
43501da177e4SLinus Torvalds  * init_adapter - Grab the resource for the card, setup the adapter
43511da177e4SLinus Torvalds  * information, set the card into a known state, create the various
43521da177e4SLinus Torvalds  * tables etc etc. This basically gets all adapter information all up
4353b595076aSUwe Kleine-König  * to date, initialised and gets the chip in sync with it.
43541da177e4SLinus Torvalds  *
43551da177e4SLinus Torvalds  * @host:	This hosts adapter structure
43561da177e4SLinus Torvalds  * @io_port:	The base I/O port
43571da177e4SLinus Torvalds  * @irq:	IRQ
43581da177e4SLinus Torvalds  *
43591da177e4SLinus Torvalds  * Returns 0 if the initialization succeeds, any other value on
43601da177e4SLinus Torvalds  * failure.
43611da177e4SLinus Torvalds  **/
43626f039790SGreg Kroah-Hartman static int adapter_init(struct AdapterCtlBlk *acb, unsigned long io_port,
43636f039790SGreg Kroah-Hartman 			u32 io_port_len, unsigned int irq)
43641da177e4SLinus Torvalds {
43651da177e4SLinus Torvalds 	if (!request_region(io_port, io_port_len, DC395X_NAME)) {
43661da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
43671da177e4SLinus Torvalds 		goto failed;
43681da177e4SLinus Torvalds 	}
43691da177e4SLinus Torvalds 	/* store port base to indicate we have registered it */
43701da177e4SLinus Torvalds 	acb->io_port_base = io_port;
43711da177e4SLinus Torvalds 	acb->io_port_len = io_port_len;
43721da177e4SLinus Torvalds 
43731d6f359aSThomas Gleixner 	if (request_irq(irq, dc395x_interrupt, IRQF_SHARED, DC395X_NAME, acb)) {
43741da177e4SLinus Torvalds 	    	/* release the region we just claimed */
43751da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Failed to register IRQ\n");
43761da177e4SLinus Torvalds 		goto failed;
43771da177e4SLinus Torvalds 	}
43781da177e4SLinus Torvalds 	/* store irq to indicate we have registered it */
43791da177e4SLinus Torvalds 	acb->irq_level = irq;
43801da177e4SLinus Torvalds 
43811da177e4SLinus Torvalds 	/* get eeprom configuration information and command line settings etc */
43821da177e4SLinus Torvalds 	check_eeprom(&acb->eeprom, io_port);
43831da177e4SLinus Torvalds  	print_eeprom_settings(&acb->eeprom);
43841da177e4SLinus Torvalds 
43851da177e4SLinus Torvalds 	/* setup adapter control block */
43861da177e4SLinus Torvalds 	adapter_init_params(acb);
43871da177e4SLinus Torvalds 
43881da177e4SLinus Torvalds 	/* display card connectors/termination settings */
43891da177e4SLinus Torvalds  	adapter_print_config(acb);
43901da177e4SLinus Torvalds 
43911da177e4SLinus Torvalds 	if (adapter_sg_tables_alloc(acb)) {
43921da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
43931da177e4SLinus Torvalds 		goto failed;
43941da177e4SLinus Torvalds 	}
43951da177e4SLinus Torvalds 	adapter_init_scsi_host(acb->scsi_host);
43961da177e4SLinus Torvalds 	adapter_init_chip(acb);
43971da177e4SLinus Torvalds 	set_basic_config(acb);
43981da177e4SLinus Torvalds 
43991da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
44001da177e4SLinus Torvalds 		"adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
44011da177e4SLinus Torvalds 		"size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
44021da177e4SLinus Torvalds 		acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
44031da177e4SLinus Torvalds 		sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
44041da177e4SLinus Torvalds 	return 0;
44051da177e4SLinus Torvalds 
44061da177e4SLinus Torvalds failed:
44071da177e4SLinus Torvalds 	if (acb->irq_level)
44081da177e4SLinus Torvalds 		free_irq(acb->irq_level, acb);
44091da177e4SLinus Torvalds 	if (acb->io_port_base)
44101da177e4SLinus Torvalds 		release_region(acb->io_port_base, acb->io_port_len);
44111da177e4SLinus Torvalds 	adapter_sg_tables_free(acb);
44121da177e4SLinus Torvalds 
44131da177e4SLinus Torvalds 	return 1;
44141da177e4SLinus Torvalds }
44151da177e4SLinus Torvalds 
44161da177e4SLinus Torvalds 
44171da177e4SLinus Torvalds /**
44181da177e4SLinus Torvalds  * adapter_uninit_chip - cleanly shut down the scsi controller chip,
44191da177e4SLinus Torvalds  * stopping all operations and disabling interrupt generation on the
44201da177e4SLinus Torvalds  * card.
44211da177e4SLinus Torvalds  *
44221da177e4SLinus Torvalds  * @acb: The adapter which we are to shutdown.
44231da177e4SLinus Torvalds  **/
44241da177e4SLinus Torvalds static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
44251da177e4SLinus Torvalds {
44261da177e4SLinus Torvalds 	/* disable interrupts */
44271da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
44281da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
44291da177e4SLinus Torvalds 
44301da177e4SLinus Torvalds 	/* reset the scsi bus */
44311da177e4SLinus Torvalds 	if (acb->config & HCC_SCSI_RESET)
44321da177e4SLinus Torvalds 		reset_scsi_bus(acb);
44331da177e4SLinus Torvalds 
44343a4fa0a2SRobert P. J. Day 	/* clear any pending interrupt state */
44351da177e4SLinus Torvalds 	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
44361da177e4SLinus Torvalds }
44371da177e4SLinus Torvalds 
44381da177e4SLinus Torvalds 
44391da177e4SLinus Torvalds 
44401da177e4SLinus Torvalds /**
44411da177e4SLinus Torvalds  * adapter_uninit - Shut down the chip and release any resources that
44421da177e4SLinus Torvalds  * we had allocated. Once this returns the adapter should not be used
44431da177e4SLinus Torvalds  * anymore.
44441da177e4SLinus Torvalds  *
44451da177e4SLinus Torvalds  * @acb: The adapter which we are to un-initialize.
44461da177e4SLinus Torvalds  **/
44471da177e4SLinus Torvalds static void adapter_uninit(struct AdapterCtlBlk *acb)
44481da177e4SLinus Torvalds {
44491da177e4SLinus Torvalds 	unsigned long flags;
44501da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
44511da177e4SLinus Torvalds 
44521da177e4SLinus Torvalds 	/* remove timers */
44531da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
44541da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
44551da177e4SLinus Torvalds 	if (timer_pending(&acb->selto_timer))
44561da177e4SLinus Torvalds 		del_timer(&acb->selto_timer);
44571da177e4SLinus Torvalds 
44581da177e4SLinus Torvalds 	adapter_uninit_chip(acb);
44591da177e4SLinus Torvalds 	adapter_remove_and_free_all_devices(acb);
44601da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
44611da177e4SLinus Torvalds 
44621da177e4SLinus Torvalds 	if (acb->irq_level)
44631da177e4SLinus Torvalds 		free_irq(acb->irq_level, acb);
44641da177e4SLinus Torvalds 	if (acb->io_port_base)
44651da177e4SLinus Torvalds 		release_region(acb->io_port_base, acb->io_port_len);
44661da177e4SLinus Torvalds 
44671da177e4SLinus Torvalds 	adapter_sg_tables_free(acb);
44681da177e4SLinus Torvalds }
44691da177e4SLinus Torvalds 
44701da177e4SLinus Torvalds 
44711da177e4SLinus Torvalds #undef YESNO
44721da177e4SLinus Torvalds #define YESNO(YN) \
44730c3de38fSRasmus Villemoes  if (YN) seq_printf(m, " Yes ");\
44740c3de38fSRasmus Villemoes  else seq_printf(m, " No  ")
44751da177e4SLinus Torvalds 
4476d32812aeSAl Viro static int dc395x_show_info(struct seq_file *m, struct Scsi_Host *host)
44771da177e4SLinus Torvalds {
44781da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
44791da177e4SLinus Torvalds 	int spd, spd1;
44801da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
44811da177e4SLinus Torvalds 	unsigned long flags;
44821da177e4SLinus Torvalds 	int dev;
44831da177e4SLinus Torvalds 
44843d30079cSRasmus Villemoes 	seq_puts(m, DC395X_BANNER " PCI SCSI Host Adapter\n"
44853d30079cSRasmus Villemoes 		" Driver Version " DC395X_VERSION "\n");
44861da177e4SLinus Torvalds 
44871da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
44881da177e4SLinus Torvalds 
44890c3de38fSRasmus Villemoes 	seq_printf(m, "SCSI Host Nr %i, ", host->host_no);
44900c3de38fSRasmus Villemoes 	seq_printf(m, "DC395U/UW/F DC315/U %s\n",
44911da177e4SLinus Torvalds 		(acb->config & HCC_WIDE_CARD) ? "Wide" : "");
44920c3de38fSRasmus Villemoes 	seq_printf(m, "io_port_base 0x%04lx, ", acb->io_port_base);
44930c3de38fSRasmus Villemoes 	seq_printf(m, "irq_level 0x%04x, ", acb->irq_level);
44940c3de38fSRasmus Villemoes 	seq_printf(m, " SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
44951da177e4SLinus Torvalds 
44960c3de38fSRasmus Villemoes 	seq_printf(m, "MaxID %i, MaxLUN %llu, ", host->max_id, host->max_lun);
44970c3de38fSRasmus Villemoes 	seq_printf(m, "AdapterID %i\n", host->this_id);
44981da177e4SLinus Torvalds 
44990c3de38fSRasmus Villemoes 	seq_printf(m, "tag_max_num %i", acb->tag_max_num);
45000c3de38fSRasmus Villemoes 	/*seq_printf(m, ", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
45010c3de38fSRasmus Villemoes 	seq_printf(m, ", FilterCfg 0x%02x",
45021da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
45030c3de38fSRasmus Villemoes 	seq_printf(m, ", DelayReset %is\n", acb->eeprom.delay_time);
45040c3de38fSRasmus Villemoes 	/*seq_printf(m, "\n"); */
45051da177e4SLinus Torvalds 
45060c3de38fSRasmus Villemoes 	seq_printf(m, "Nr of DCBs: %i\n", list_size(&acb->dcb_list));
4507*ca358af1SAndy Shevchenko 	seq_printf(m, "Map of attached LUNs: %8ph\n", &acb->dcb_map[0]);
4508*ca358af1SAndy Shevchenko 	seq_printf(m, "                      %8ph\n", &acb->dcb_map[8]);
45091da177e4SLinus Torvalds 
451091c40f24SRasmus Villemoes 	seq_puts(m,
451191c40f24SRasmus Villemoes 		 "Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
45121da177e4SLinus Torvalds 
45131da177e4SLinus Torvalds 	dev = 0;
45141da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
45151da177e4SLinus Torvalds 		int nego_period;
45160c3de38fSRasmus Villemoes 		seq_printf(m, "%02i %02i  %02i ", dev, dcb->target_id,
45171da177e4SLinus Torvalds 			dcb->target_lun);
45181da177e4SLinus Torvalds 		YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
45191da177e4SLinus Torvalds 		YESNO(dcb->sync_offset);
45201da177e4SLinus Torvalds 		YESNO(dcb->sync_period & WIDE_SYNC);
45211da177e4SLinus Torvalds 		YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
45221da177e4SLinus Torvalds 		YESNO(dcb->dev_mode & NTC_DO_SEND_START);
45231da177e4SLinus Torvalds 		YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
45241da177e4SLinus Torvalds 		nego_period = clock_period[dcb->sync_period & 0x07] << 2;
45251da177e4SLinus Torvalds 		if (dcb->sync_offset)
45260c3de38fSRasmus Villemoes 			seq_printf(m, "  %03i ns ", nego_period);
45271da177e4SLinus Torvalds 		else
45280c3de38fSRasmus Villemoes 			seq_printf(m, " (%03i ns)", (dcb->min_nego_period << 2));
45291da177e4SLinus Torvalds 
45301da177e4SLinus Torvalds 		if (dcb->sync_offset & 0x0f) {
45311da177e4SLinus Torvalds 			spd = 1000 / (nego_period);
45321da177e4SLinus Torvalds 			spd1 = 1000 % (nego_period);
45331da177e4SLinus Torvalds 			spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
45340c3de38fSRasmus Villemoes 			seq_printf(m, "   %2i.%1i M     %02i ", spd, spd1,
45351da177e4SLinus Torvalds 				(dcb->sync_offset & 0x0f));
45361da177e4SLinus Torvalds 		} else
453791c40f24SRasmus Villemoes 			seq_puts(m, "                 ");
45381da177e4SLinus Torvalds 
45391da177e4SLinus Torvalds 		/* Add more info ... */
45400c3de38fSRasmus Villemoes 		seq_printf(m, "     %02i\n", dcb->max_command);
45411da177e4SLinus Torvalds 		dev++;
45421da177e4SLinus Torvalds 	}
45431da177e4SLinus Torvalds 
45441da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
454591c40f24SRasmus Villemoes 		seq_puts(m, "Waiting queue timer running\n");
45461da177e4SLinus Torvalds 	else
4547f50332ffSRasmus Villemoes 		seq_putc(m, '\n');
45481da177e4SLinus Torvalds 
45491da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
45501da177e4SLinus Torvalds 		struct ScsiReqBlk *srb;
45511da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_waiting_list))
45520c3de38fSRasmus Villemoes 			seq_printf(m, "DCB (%02i-%i): Waiting: %i:",
45531da177e4SLinus Torvalds 				dcb->target_id, dcb->target_lun,
45541da177e4SLinus Torvalds 				list_size(&dcb->srb_waiting_list));
45551da177e4SLinus Torvalds                 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
45560c3de38fSRasmus Villemoes 			seq_printf(m, " %p", srb->cmd);
45571da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_going_list))
45580c3de38fSRasmus Villemoes 			seq_printf(m, "\nDCB (%02i-%i): Going  : %i:",
45591da177e4SLinus Torvalds 				dcb->target_id, dcb->target_lun,
45601da177e4SLinus Torvalds 				list_size(&dcb->srb_going_list));
45611da177e4SLinus Torvalds 		list_for_each_entry(srb, &dcb->srb_going_list, list)
45620c3de38fSRasmus Villemoes 			seq_printf(m, " %p", srb->cmd);
45631da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
4564f50332ffSRasmus Villemoes 			seq_putc(m, '\n');
45651da177e4SLinus Torvalds 	}
45661da177e4SLinus Torvalds 
45671da177e4SLinus Torvalds 	if (debug_enabled(DBG_1)) {
45680c3de38fSRasmus Villemoes 		seq_printf(m, "DCB list for ACB %p:\n", acb);
45691da177e4SLinus Torvalds 		list_for_each_entry(dcb, &acb->dcb_list, list) {
45700c3de38fSRasmus Villemoes 			seq_printf(m, "%p -> ", dcb);
45711da177e4SLinus Torvalds 		}
457291c40f24SRasmus Villemoes 		seq_puts(m, "END\n");
45731da177e4SLinus Torvalds 	}
45741da177e4SLinus Torvalds 
45751da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
45761da177e4SLinus Torvalds 	return 0;
45771da177e4SLinus Torvalds }
45781da177e4SLinus Torvalds 
45791da177e4SLinus Torvalds 
45801da177e4SLinus Torvalds static struct scsi_host_template dc395x_driver_template = {
45811da177e4SLinus Torvalds 	.module                 = THIS_MODULE,
45821da177e4SLinus Torvalds 	.proc_name              = DC395X_NAME,
4583d32812aeSAl Viro 	.show_info              = dc395x_show_info,
45841da177e4SLinus Torvalds 	.name                   = DC395X_BANNER " " DC395X_VERSION,
45851da177e4SLinus Torvalds 	.queuecommand           = dc395x_queue_command,
45861da177e4SLinus Torvalds 	.slave_alloc            = dc395x_slave_alloc,
45871da177e4SLinus Torvalds 	.slave_destroy          = dc395x_slave_destroy,
45881da177e4SLinus Torvalds 	.can_queue              = DC395x_MAX_CAN_QUEUE,
45891da177e4SLinus Torvalds 	.this_id                = 7,
45901da177e4SLinus Torvalds 	.sg_tablesize           = DC395x_MAX_SG_TABLESIZE,
45911da177e4SLinus Torvalds 	.cmd_per_lun            = DC395x_MAX_CMD_PER_LUN,
45921da177e4SLinus Torvalds 	.eh_abort_handler       = dc395x_eh_abort,
45931da177e4SLinus Torvalds 	.eh_bus_reset_handler   = dc395x_eh_bus_reset,
45944af14d11SChristoph Hellwig 	.dma_boundary		= PAGE_SIZE - 1,
45951da177e4SLinus Torvalds };
45961da177e4SLinus Torvalds 
45971da177e4SLinus Torvalds 
45981da177e4SLinus Torvalds /**
45991da177e4SLinus Torvalds  * banner_display - Display banner on first instance of driver
46001da177e4SLinus Torvalds  * initialized.
46011da177e4SLinus Torvalds  **/
46021da177e4SLinus Torvalds static void banner_display(void)
46031da177e4SLinus Torvalds {
46041da177e4SLinus Torvalds 	static int banner_done = 0;
46051da177e4SLinus Torvalds 	if (!banner_done)
46061da177e4SLinus Torvalds 	{
46071da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
46081da177e4SLinus Torvalds 		banner_done = 1;
46091da177e4SLinus Torvalds 	}
46101da177e4SLinus Torvalds }
46111da177e4SLinus Torvalds 
46121da177e4SLinus Torvalds 
46131da177e4SLinus Torvalds /**
46141da177e4SLinus Torvalds  * dc395x_init_one - Initialise a single instance of the adapter.
46151da177e4SLinus Torvalds  *
46161da177e4SLinus Torvalds  * The PCI layer will call this once for each instance of the adapter
46171da177e4SLinus Torvalds  * that it finds in the system. The pci_dev strcuture indicates which
46181da177e4SLinus Torvalds  * instance we are being called from.
46191da177e4SLinus Torvalds  *
4620b595076aSUwe Kleine-König  * @dev: The PCI device to initialize.
46211da177e4SLinus Torvalds  * @id: Looks like a pointer to the entry in our pci device table
46221da177e4SLinus Torvalds  * that was actually matched by the PCI subsystem.
46231da177e4SLinus Torvalds  *
46241da177e4SLinus Torvalds  * Returns 0 on success, or an error code (-ve) on failure.
46251da177e4SLinus Torvalds  **/
46266f039790SGreg Kroah-Hartman static int dc395x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
46271da177e4SLinus Torvalds {
46281da177e4SLinus Torvalds 	struct Scsi_Host *scsi_host = NULL;
46291da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = NULL;
46301da177e4SLinus Torvalds 	unsigned long io_port_base;
46311da177e4SLinus Torvalds 	unsigned int io_port_len;
46321da177e4SLinus Torvalds 	unsigned int irq;
46331da177e4SLinus Torvalds 
46341da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
46351da177e4SLinus Torvalds 	banner_display();
46361da177e4SLinus Torvalds 
46371da177e4SLinus Torvalds 	if (pci_enable_device(dev))
46381da177e4SLinus Torvalds 	{
46391da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "PCI Enable device failed.\n");
46401da177e4SLinus Torvalds 		return -ENODEV;
46411da177e4SLinus Torvalds 	}
46421da177e4SLinus Torvalds 	io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
46431da177e4SLinus Torvalds 	io_port_len = pci_resource_len(dev, 0);
46441da177e4SLinus Torvalds 	irq = dev->irq;
46451da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
46461da177e4SLinus Torvalds 
46471da177e4SLinus Torvalds 	/* allocate scsi host information (includes out adapter) */
46481da177e4SLinus Torvalds 	scsi_host = scsi_host_alloc(&dc395x_driver_template,
46491da177e4SLinus Torvalds 				    sizeof(struct AdapterCtlBlk));
46501da177e4SLinus Torvalds 	if (!scsi_host) {
46511da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
46521da177e4SLinus Torvalds 		goto fail;
46531da177e4SLinus Torvalds 	}
46541da177e4SLinus Torvalds  	acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
46551da177e4SLinus Torvalds  	acb->scsi_host = scsi_host;
46561da177e4SLinus Torvalds  	acb->dev = dev;
46571da177e4SLinus Torvalds 
46581da177e4SLinus Torvalds 	/* initialise the adapter and everything we need */
46591da177e4SLinus Torvalds  	if (adapter_init(acb, io_port_base, io_port_len, irq)) {
46601da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "adapter init failed\n");
46611da177e4SLinus Torvalds 		goto fail;
46621da177e4SLinus Torvalds 	}
46631da177e4SLinus Torvalds 
46641da177e4SLinus Torvalds 	pci_set_master(dev);
46651da177e4SLinus Torvalds 
46661da177e4SLinus Torvalds 	/* get the scsi mid level to scan for new devices on the bus */
46671da177e4SLinus Torvalds 	if (scsi_add_host(scsi_host, &dev->dev)) {
46681da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "scsi_add_host failed\n");
46691da177e4SLinus Torvalds 		goto fail;
46701da177e4SLinus Torvalds 	}
46711da177e4SLinus Torvalds 	pci_set_drvdata(dev, scsi_host);
46721da177e4SLinus Torvalds 	scsi_scan_host(scsi_host);
46731da177e4SLinus Torvalds 
46741da177e4SLinus Torvalds 	return 0;
46751da177e4SLinus Torvalds 
46761da177e4SLinus Torvalds fail:
46771da177e4SLinus Torvalds 	if (acb != NULL)
46781da177e4SLinus Torvalds 		adapter_uninit(acb);
46791da177e4SLinus Torvalds 	if (scsi_host != NULL)
46801da177e4SLinus Torvalds 		scsi_host_put(scsi_host);
46811da177e4SLinus Torvalds 	pci_disable_device(dev);
46821da177e4SLinus Torvalds 	return -ENODEV;
46831da177e4SLinus Torvalds }
46841da177e4SLinus Torvalds 
46851da177e4SLinus Torvalds 
46861da177e4SLinus Torvalds /**
46871da177e4SLinus Torvalds  * dc395x_remove_one - Called to remove a single instance of the
46881da177e4SLinus Torvalds  * adapter.
46891da177e4SLinus Torvalds  *
4690b595076aSUwe Kleine-König  * @dev: The PCI device to initialize.
46911da177e4SLinus Torvalds  **/
46926f039790SGreg Kroah-Hartman static void dc395x_remove_one(struct pci_dev *dev)
46931da177e4SLinus Torvalds {
46941da177e4SLinus Torvalds 	struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
46951da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
46961da177e4SLinus Torvalds 
46971da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
46981da177e4SLinus Torvalds 
46991da177e4SLinus Torvalds 	scsi_remove_host(scsi_host);
47001da177e4SLinus Torvalds 	adapter_uninit(acb);
47011da177e4SLinus Torvalds 	pci_disable_device(dev);
47021da177e4SLinus Torvalds 	scsi_host_put(scsi_host);
47031da177e4SLinus Torvalds }
47041da177e4SLinus Torvalds 
47051da177e4SLinus Torvalds 
47061da177e4SLinus Torvalds static struct pci_device_id dc395x_pci_table[] = {
47071da177e4SLinus Torvalds 	{
47081da177e4SLinus Torvalds 		.vendor		= PCI_VENDOR_ID_TEKRAM,
47091da177e4SLinus Torvalds 		.device		= PCI_DEVICE_ID_TEKRAM_TRMS1040,
47101da177e4SLinus Torvalds 		.subvendor	= PCI_ANY_ID,
47111da177e4SLinus Torvalds 		.subdevice	= PCI_ANY_ID,
47121da177e4SLinus Torvalds 	 },
47131da177e4SLinus Torvalds 	{}			/* Terminating entry */
47141da177e4SLinus Torvalds };
47151da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
47161da177e4SLinus Torvalds 
47171da177e4SLinus Torvalds 
47181da177e4SLinus Torvalds static struct pci_driver dc395x_driver = {
47191da177e4SLinus Torvalds 	.name           = DC395X_NAME,
47201da177e4SLinus Torvalds 	.id_table       = dc395x_pci_table,
47211da177e4SLinus Torvalds 	.probe          = dc395x_init_one,
47226f039790SGreg Kroah-Hartman 	.remove         = dc395x_remove_one,
47231da177e4SLinus Torvalds };
47241da177e4SLinus Torvalds 
47251da177e4SLinus Torvalds 
47261da177e4SLinus Torvalds /**
47271da177e4SLinus Torvalds  * dc395x_module_init - Module initialization function
47281da177e4SLinus Torvalds  *
47291da177e4SLinus Torvalds  * Used by both module and built-in driver to initialise this driver.
47301da177e4SLinus Torvalds  **/
47311da177e4SLinus Torvalds static int __init dc395x_module_init(void)
47321da177e4SLinus Torvalds {
4733dcbccbdeSHenrik Kretzschmar 	return pci_register_driver(&dc395x_driver);
47341da177e4SLinus Torvalds }
47351da177e4SLinus Torvalds 
47361da177e4SLinus Torvalds 
47371da177e4SLinus Torvalds /**
47381da177e4SLinus Torvalds  * dc395x_module_exit - Module cleanup function.
47391da177e4SLinus Torvalds  **/
47401da177e4SLinus Torvalds static void __exit dc395x_module_exit(void)
47411da177e4SLinus Torvalds {
47421da177e4SLinus Torvalds 	pci_unregister_driver(&dc395x_driver);
47431da177e4SLinus Torvalds }
47441da177e4SLinus Torvalds 
47451da177e4SLinus Torvalds 
47461da177e4SLinus Torvalds module_init(dc395x_module_init);
47471da177e4SLinus Torvalds module_exit(dc395x_module_exit);
47481da177e4SLinus Torvalds 
47491da177e4SLinus Torvalds MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
47501da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
47511da177e4SLinus Torvalds MODULE_LICENSE("GPL");
4752