xref: /linux/drivers/scsi/dc395x.c (revision cbd9a3347c757383f3d2b50cf7cfd03eb479c481)
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>
679c2d2670SHannes Reinecke #include <scsi/scsi_transport_spi.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include "dc395x.h"
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds #define DC395X_NAME	"dc395x"
721da177e4SLinus Torvalds #define DC395X_BANNER	"Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
731da177e4SLinus Torvalds #define DC395X_VERSION	"v2.05, 2004/03/08"
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /*---------------------------------------------------------------------------
761da177e4SLinus Torvalds                                   Features
771da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
781da177e4SLinus Torvalds /*
791da177e4SLinus Torvalds  * Set to disable parts of the driver
801da177e4SLinus Torvalds  */
811da177e4SLinus Torvalds /*#define DC395x_NO_DISCONNECT*/
821da177e4SLinus Torvalds /*#define DC395x_NO_TAGQ*/
831da177e4SLinus Torvalds /*#define DC395x_NO_SYNC*/
841da177e4SLinus Torvalds /*#define DC395x_NO_WIDE*/
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /*---------------------------------------------------------------------------
871da177e4SLinus Torvalds                                   Debugging
881da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
891da177e4SLinus Torvalds /*
901da177e4SLinus Torvalds  * Types of debugging that can be enabled and disabled
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds #define DBG_KG		0x0001
931da177e4SLinus Torvalds #define DBG_0		0x0002
941da177e4SLinus Torvalds #define DBG_1		0x0004
951da177e4SLinus Torvalds #define DBG_SG		0x0020
961da177e4SLinus Torvalds #define DBG_FIFO	0x0040
971da177e4SLinus Torvalds #define DBG_PIO		0x0080
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /*
1011da177e4SLinus Torvalds  * Set set of things to output debugging for.
1021da177e4SLinus Torvalds  * Undefine to remove all debugging
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
1051da177e4SLinus Torvalds /*#define  DEBUG_MASK	DBG_0*/
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * Output a kernel mesage at the specified level and append the
1101da177e4SLinus Torvalds  * driver name and a ": " to the start of the message
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds #define dprintkl(level, format, arg...)  \
1131da177e4SLinus Torvalds     printk(level DC395X_NAME ": " format , ## arg)
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #ifdef DEBUG_MASK
1171da177e4SLinus Torvalds /*
1181da177e4SLinus Torvalds  * print a debug message - this is formated with KERN_DEBUG, then the
1191da177e4SLinus Torvalds  * driver name followed by a ": " and then the message is output.
1201da177e4SLinus Torvalds  * This also checks that the specified debug level is enabled before
1211da177e4SLinus Torvalds  * outputing the message
1221da177e4SLinus Torvalds  */
1231da177e4SLinus Torvalds #define dprintkdbg(type, format, arg...) \
1241da177e4SLinus Torvalds 	do { \
1251da177e4SLinus Torvalds 		if ((type) & (DEBUG_MASK)) \
1261da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG , format , ## arg); \
1271da177e4SLinus Torvalds 	} while (0)
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
1301da177e4SLinus Torvalds  * Check if the specified type of debugging is enabled
1311da177e4SLinus Torvalds  */
1321da177e4SLinus Torvalds #define debug_enabled(type)	((DEBUG_MASK) & (type))
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #else
1351da177e4SLinus Torvalds /*
1361da177e4SLinus Torvalds  * No debugging. Do nothing
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds #define dprintkdbg(type, format, arg...) \
1391da177e4SLinus Torvalds 	do {} while (0)
1401da177e4SLinus Torvalds #define debug_enabled(type)	(0)
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds #endif
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds #ifndef PCI_VENDOR_ID_TEKRAM
1461da177e4SLinus Torvalds #define PCI_VENDOR_ID_TEKRAM                    0x1DE1	/* Vendor ID    */
1471da177e4SLinus Torvalds #endif
1481da177e4SLinus Torvalds #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
1491da177e4SLinus Torvalds #define PCI_DEVICE_ID_TEKRAM_TRMS1040           0x0391	/* Device ID    */
1501da177e4SLinus Torvalds #endif
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds #define DC395x_LOCK_IO(dev,flags)		spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
1541da177e4SLinus Torvalds #define DC395x_UNLOCK_IO(dev,flags)		spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds #define DC395x_read8(acb,address)		(u8)(inb(acb->io_port_base + (address)))
1571da177e4SLinus Torvalds #define DC395x_read16(acb,address)		(u16)(inw(acb->io_port_base + (address)))
1581da177e4SLinus Torvalds #define DC395x_read32(acb,address)		(u32)(inl(acb->io_port_base + (address)))
1591da177e4SLinus Torvalds #define DC395x_write8(acb,address,value)	outb((value), acb->io_port_base + (address))
1601da177e4SLinus Torvalds #define DC395x_write16(acb,address,value)	outw((value), acb->io_port_base + (address))
1611da177e4SLinus Torvalds #define DC395x_write32(acb,address,value)	outl((value), acb->io_port_base + (address))
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds #define TAG_NONE 255
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds /*
1661da177e4SLinus Torvalds  * srb->segement_x is the hw sg list. It is always allocated as a
1671da177e4SLinus Torvalds  * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
1681da177e4SLinus Torvalds  * cross a page boundy.
1691da177e4SLinus Torvalds  */
1701da177e4SLinus Torvalds #define SEGMENTX_LEN	(sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
171b4b08e58SLinus Torvalds 
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds struct SGentry {
1741da177e4SLinus Torvalds 	u32 address;		/* bus! address */
1751da177e4SLinus Torvalds 	u32 length;
1761da177e4SLinus Torvalds };
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* The SEEPROM structure for TRM_S1040 */
1791da177e4SLinus Torvalds struct NVRamTarget {
1801da177e4SLinus Torvalds 	u8 cfg0;		/* Target configuration byte 0  */
1811da177e4SLinus Torvalds 	u8 period;		/* Target period                */
1821da177e4SLinus Torvalds 	u8 cfg2;		/* Target configuration byte 2  */
1831da177e4SLinus Torvalds 	u8 cfg3;		/* Target configuration byte 3  */
1841da177e4SLinus Torvalds };
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds struct NvRamType {
1871da177e4SLinus Torvalds 	u8 sub_vendor_id[2];	/* 0,1  Sub Vendor ID   */
1881da177e4SLinus Torvalds 	u8 sub_sys_id[2];	/* 2,3  Sub System ID   */
1891da177e4SLinus Torvalds 	u8 sub_class;		/* 4    Sub Class       */
1901da177e4SLinus Torvalds 	u8 vendor_id[2];	/* 5,6  Vendor ID       */
1911da177e4SLinus Torvalds 	u8 device_id[2];	/* 7,8  Device ID       */
1921da177e4SLinus Torvalds 	u8 reserved;		/* 9    Reserved        */
1931da177e4SLinus Torvalds 	struct NVRamTarget target[DC395x_MAX_SCSI_ID];
1941da177e4SLinus Torvalds 						/** 10,11,12,13
1951da177e4SLinus Torvalds 						 ** 14,15,16,17
1961da177e4SLinus Torvalds 						 ** ....
1971da177e4SLinus Torvalds 						 ** ....
1981da177e4SLinus Torvalds 						 ** 70,71,72,73
1991da177e4SLinus Torvalds 						 */
2001da177e4SLinus Torvalds 	u8 scsi_id;		/* 74 Host Adapter SCSI ID      */
2011da177e4SLinus Torvalds 	u8 channel_cfg;		/* 75 Channel configuration     */
2021da177e4SLinus Torvalds 	u8 delay_time;		/* 76 Power on delay time       */
2031da177e4SLinus Torvalds 	u8 max_tag;		/* 77 Maximum tags              */
2041da177e4SLinus Torvalds 	u8 reserved0;		/* 78  */
2051da177e4SLinus Torvalds 	u8 boot_target;		/* 79  */
2061da177e4SLinus Torvalds 	u8 boot_lun;		/* 80  */
2071da177e4SLinus Torvalds 	u8 reserved1;		/* 81  */
2081da177e4SLinus Torvalds 	u16 reserved2[22];	/* 82,..125 */
2091da177e4SLinus Torvalds 	u16 cksum;		/* 126,127 */
2101da177e4SLinus Torvalds };
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds struct ScsiReqBlk {
2131da177e4SLinus Torvalds 	struct list_head list;		/* next/prev ptrs for srb lists */
2141da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
2151da177e4SLinus Torvalds 	struct scsi_cmnd *cmd;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	struct SGentry *segment_x;	/* Linear array of hw sg entries (up to 64 entries) */
218cdb8c2a6SGuennadi Liakhovetski 	dma_addr_t sg_bus_addr;	        /* Bus address of sg list (ie, of segment_x) */
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	u8 sg_count;			/* No of HW sg entries for this request */
2211da177e4SLinus Torvalds 	u8 sg_index;			/* Index of HW sg entry for this request */
22225985edcSLucas De Marchi 	size_t total_xfer_length;	/* Total number of bytes remaining to be transferred */
223cdb8c2a6SGuennadi Liakhovetski 	size_t request_length;		/* Total number of bytes in this request */
2241da177e4SLinus Torvalds 	/*
2251da177e4SLinus Torvalds 	 * The sense buffer handling function, request_sense, uses
2261da177e4SLinus Torvalds 	 * the first hw sg entry (segment_x[0]) and the transfer
2271da177e4SLinus Torvalds 	 * length (total_xfer_length). While doing this it stores the
2281da177e4SLinus Torvalds 	 * original values into the last sg hw list
2291da177e4SLinus Torvalds 	 * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
2301da177e4SLinus Torvalds 	 * total_xfer_length in xferred. These values are restored in
2311da177e4SLinus Torvalds 	 * pci_unmap_srb_sense. This is the only place xferred is used.
2321da177e4SLinus Torvalds 	 */
233cdb8c2a6SGuennadi Liakhovetski 	size_t xferred;		        /* Saved copy of total_xfer_length */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	u16 state;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	u8 msgin_buf[6];
2381da177e4SLinus Torvalds 	u8 msgout_buf[6];
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	u8 adapter_status;
2411da177e4SLinus Torvalds 	u8 target_status;
2421da177e4SLinus Torvalds 	u8 msg_count;
2431da177e4SLinus Torvalds 	u8 end_message;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	u8 tag_number;
2461da177e4SLinus Torvalds 	u8 status;
2471da177e4SLinus Torvalds 	u8 retry_count;
2481da177e4SLinus Torvalds 	u8 flag;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	u8 scsi_phase;
2511da177e4SLinus Torvalds };
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds struct DeviceCtlBlk {
2541da177e4SLinus Torvalds 	struct list_head list;		/* next/prev ptrs for the dcb list */
2551da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb;
2561da177e4SLinus Torvalds 	struct list_head srb_going_list;	/* head of going srb list */
2571da177e4SLinus Torvalds 	struct list_head srb_waiting_list;	/* head of waiting srb list */
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	struct ScsiReqBlk *active_srb;
2601da177e4SLinus Torvalds 	u32 tag_mask;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	u16 max_command;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	u8 target_id;		/* SCSI Target ID  (SCSI Only) */
2651da177e4SLinus Torvalds 	u8 target_lun;		/* SCSI Log.  Unit (SCSI Only) */
2661da177e4SLinus Torvalds 	u8 identify_msg;
2671da177e4SLinus Torvalds 	u8 dev_mode;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	u8 inquiry7;		/* To store Inquiry flags */
2701da177e4SLinus Torvalds 	u8 sync_mode;		/* 0:async mode */
2711da177e4SLinus Torvalds 	u8 min_nego_period;	/* for nego. */
2721da177e4SLinus Torvalds 	u8 sync_period;		/* for reg.  */
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	u8 sync_offset;		/* for reg. and nego.(low nibble) */
2751da177e4SLinus Torvalds 	u8 flag;
2761da177e4SLinus Torvalds 	u8 dev_type;
2771da177e4SLinus Torvalds 	u8 init_tcq_flag;
2781da177e4SLinus Torvalds };
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds struct AdapterCtlBlk {
2811da177e4SLinus Torvalds 	struct Scsi_Host *scsi_host;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	unsigned long io_port_base;
2841da177e4SLinus Torvalds 	unsigned long io_port_len;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	struct list_head dcb_list;		/* head of going dcb list */
2871da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb_run_robin;
2881da177e4SLinus Torvalds 	struct DeviceCtlBlk *active_dcb;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	struct list_head srb_free_list;		/* head of free srb list */
2911da177e4SLinus Torvalds 	struct ScsiReqBlk *tmp_srb;
2921da177e4SLinus Torvalds 	struct timer_list waiting_timer;
2931da177e4SLinus Torvalds 	struct timer_list selto_timer;
2941da177e4SLinus Torvalds 
29553a8936cSHannes Reinecke 	unsigned long last_reset;
29653a8936cSHannes Reinecke 
2971da177e4SLinus Torvalds 	u16 srb_count;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	u8 sel_timeout;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	unsigned int irq_level;
3021da177e4SLinus Torvalds 	u8 tag_max_num;
3031da177e4SLinus Torvalds 	u8 acb_flag;
3041da177e4SLinus Torvalds 	u8 gmode2;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	u8 config;
3071da177e4SLinus Torvalds 	u8 lun_chk;
3081da177e4SLinus Torvalds 	u8 scan_devices;
3091da177e4SLinus Torvalds 	u8 hostid_bit;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	u8 dcb_map[DC395x_MAX_SCSI_ID];
3121da177e4SLinus Torvalds 	struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	struct pci_dev *dev;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	u8 msg_len;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
3191da177e4SLinus Torvalds 	struct ScsiReqBlk srb;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	struct NvRamType eeprom;	/* eeprom settings for this adapter */
3221da177e4SLinus Torvalds };
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds /*---------------------------------------------------------------------------
3261da177e4SLinus Torvalds                             Forward declarations
3271da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
3281da177e4SLinus Torvalds static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3291da177e4SLinus Torvalds 		u16 *pscsi_status);
3301da177e4SLinus Torvalds static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3311da177e4SLinus Torvalds 		u16 *pscsi_status);
3321da177e4SLinus Torvalds static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3331da177e4SLinus Torvalds 		u16 *pscsi_status);
3341da177e4SLinus Torvalds static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3351da177e4SLinus Torvalds 		u16 *pscsi_status);
3361da177e4SLinus Torvalds static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3371da177e4SLinus Torvalds 		u16 *pscsi_status);
3381da177e4SLinus Torvalds static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3391da177e4SLinus Torvalds 		u16 *pscsi_status);
3401da177e4SLinus Torvalds static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3411da177e4SLinus Torvalds 		u16 *pscsi_status);
3421da177e4SLinus Torvalds static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3431da177e4SLinus Torvalds 		u16 *pscsi_status);
3441da177e4SLinus Torvalds static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3451da177e4SLinus Torvalds 		u16 *pscsi_status);
3461da177e4SLinus Torvalds static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3471da177e4SLinus Torvalds 		u16 *pscsi_status);
3481da177e4SLinus Torvalds static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3491da177e4SLinus Torvalds 		u16 *pscsi_status);
3501da177e4SLinus Torvalds static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3511da177e4SLinus Torvalds 		u16 *pscsi_status);
3521da177e4SLinus Torvalds static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3531da177e4SLinus Torvalds 		u16 *pscsi_status);
3541da177e4SLinus Torvalds static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3551da177e4SLinus Torvalds 		u16 *pscsi_status);
3561da177e4SLinus Torvalds static void set_basic_config(struct AdapterCtlBlk *acb);
3571da177e4SLinus Torvalds static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
3581da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3591da177e4SLinus Torvalds static void reset_scsi_bus(struct AdapterCtlBlk *acb);
3601da177e4SLinus Torvalds static void data_io_transfer(struct AdapterCtlBlk *acb,
3611da177e4SLinus Torvalds 		struct ScsiReqBlk *srb, u16 io_dir);
3621da177e4SLinus Torvalds static void disconnect(struct AdapterCtlBlk *acb);
3631da177e4SLinus Torvalds static void reselect(struct AdapterCtlBlk *acb);
3641da177e4SLinus Torvalds static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3651da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3661da177e4SLinus Torvalds static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
3671da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3681da177e4SLinus Torvalds static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
3691da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3701da177e4SLinus Torvalds static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
3711da177e4SLinus Torvalds 		struct scsi_cmnd *cmd, u8 force);
3721da177e4SLinus Torvalds static void scsi_reset_detect(struct AdapterCtlBlk *acb);
3731da177e4SLinus Torvalds static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
3741da177e4SLinus Torvalds static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3751da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3761da177e4SLinus Torvalds static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3771da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3781da177e4SLinus Torvalds static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3791da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3801da177e4SLinus Torvalds static void set_xfer_rate(struct AdapterCtlBlk *acb,
3811da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb);
382af53b89bSKees Cook static void waiting_timeout(struct timer_list *t);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds /*---------------------------------------------------------------------------
3861da177e4SLinus Torvalds                                  Static Data
3871da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
3881da177e4SLinus Torvalds static u16 current_sync_offset = 0;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds static void *dc395x_scsi_phase0[] = {
3911da177e4SLinus Torvalds 	data_out_phase0,/* phase:0 */
3921da177e4SLinus Torvalds 	data_in_phase0,	/* phase:1 */
3931da177e4SLinus Torvalds 	command_phase0,	/* phase:2 */
3941da177e4SLinus Torvalds 	status_phase0,	/* phase:3 */
3951da177e4SLinus Torvalds 	nop0,		/* phase:4 PH_BUS_FREE .. initial phase */
3961da177e4SLinus Torvalds 	nop0,		/* phase:5 PH_BUS_FREE .. initial phase */
3971da177e4SLinus Torvalds 	msgout_phase0,	/* phase:6 */
3981da177e4SLinus Torvalds 	msgin_phase0,	/* phase:7 */
3991da177e4SLinus Torvalds };
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds static void *dc395x_scsi_phase1[] = {
4021da177e4SLinus Torvalds 	data_out_phase1,/* phase:0 */
4031da177e4SLinus Torvalds 	data_in_phase1,	/* phase:1 */
4041da177e4SLinus Torvalds 	command_phase1,	/* phase:2 */
4051da177e4SLinus Torvalds 	status_phase1,	/* phase:3 */
4061da177e4SLinus Torvalds 	nop1,		/* phase:4 PH_BUS_FREE .. initial phase */
4071da177e4SLinus Torvalds 	nop1,		/* phase:5 PH_BUS_FREE .. initial phase */
4081da177e4SLinus Torvalds 	msgout_phase1,	/* phase:6 */
4091da177e4SLinus Torvalds 	msgin_phase1,	/* phase:7 */
4101da177e4SLinus Torvalds };
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds /*
4131da177e4SLinus Torvalds  *Fast20:	000	 50ns, 20.0 MHz
4141da177e4SLinus Torvalds  *		001	 75ns, 13.3 MHz
4151da177e4SLinus Torvalds  *		010	100ns, 10.0 MHz
4161da177e4SLinus Torvalds  *		011	125ns,  8.0 MHz
4171da177e4SLinus Torvalds  *		100	150ns,  6.6 MHz
4181da177e4SLinus Torvalds  *		101	175ns,  5.7 MHz
4191da177e4SLinus Torvalds  *		110	200ns,  5.0 MHz
4201da177e4SLinus Torvalds  *		111	250ns,  4.0 MHz
4211da177e4SLinus Torvalds  *
4221da177e4SLinus Torvalds  *Fast40(LVDS):	000	 25ns, 40.0 MHz
4231da177e4SLinus Torvalds  *		001	 50ns, 20.0 MHz
4241da177e4SLinus Torvalds  *		010	 75ns, 13.3 MHz
4251da177e4SLinus Torvalds  *		011	100ns, 10.0 MHz
4261da177e4SLinus Torvalds  *		100	125ns,  8.0 MHz
4271da177e4SLinus Torvalds  *		101	150ns,  6.6 MHz
4281da177e4SLinus Torvalds  *		110	175ns,  5.7 MHz
4291da177e4SLinus Torvalds  *		111	200ns,  5.0 MHz
4301da177e4SLinus Torvalds  */
4311da177e4SLinus Torvalds /*static u8	clock_period[] = {12,19,25,31,37,44,50,62};*/
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
4341da177e4SLinus Torvalds static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
4351da177e4SLinus Torvalds static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /*---------------------------------------------------------------------------
4391da177e4SLinus Torvalds                                 Configuration
4401da177e4SLinus Torvalds   ---------------------------------------------------------------------------*/
4411da177e4SLinus Torvalds /*
4421da177e4SLinus Torvalds  * Module/boot parameters currently effect *all* instances of the
4431da177e4SLinus Torvalds  * card in the system.
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /*
4471da177e4SLinus Torvalds  * Command line parameters are stored in a structure below.
4481da177e4SLinus Torvalds  * These are the index's into the structure for the various
4491da177e4SLinus Torvalds  * command line options.
4501da177e4SLinus Torvalds  */
4511da177e4SLinus Torvalds #define CFG_ADAPTER_ID		0
4521da177e4SLinus Torvalds #define CFG_MAX_SPEED		1
4531da177e4SLinus Torvalds #define CFG_DEV_MODE		2
4541da177e4SLinus Torvalds #define CFG_ADAPTER_MODE	3
4551da177e4SLinus Torvalds #define CFG_TAGS		4
4561da177e4SLinus Torvalds #define CFG_RESET_DELAY		5
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds #define CFG_NUM			6	/* number of configuration items */
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds /*
4621da177e4SLinus Torvalds  * Value used to indicate that a command line override
4631da177e4SLinus Torvalds  * hasn't been used to modify the value.
4641da177e4SLinus Torvalds  */
4651da177e4SLinus Torvalds #define CFG_PARAM_UNSET -1
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /*
4691da177e4SLinus Torvalds  * Hold command line parameters.
4701da177e4SLinus Torvalds  */
4711da177e4SLinus Torvalds struct ParameterData {
4721da177e4SLinus Torvalds 	int value;		/* value of this setting */
4731da177e4SLinus Torvalds 	int min;		/* minimum value */
4741da177e4SLinus Torvalds 	int max;		/* maximum value */
4751da177e4SLinus Torvalds 	int def;		/* default value */
4761da177e4SLinus Torvalds 	int safe;		/* safe value */
4771da177e4SLinus Torvalds };
4786f039790SGreg Kroah-Hartman static struct ParameterData cfg_data[] = {
4791da177e4SLinus Torvalds 	{ /* adapter id */
4801da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
4811da177e4SLinus Torvalds 		0,
4821da177e4SLinus Torvalds 		15,
4831da177e4SLinus Torvalds 		7,
4841da177e4SLinus Torvalds 		7
4851da177e4SLinus Torvalds 	},
4861da177e4SLinus Torvalds 	{ /* max speed */
4871da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
4881da177e4SLinus Torvalds 		  0,
4891da177e4SLinus Torvalds 		  7,
4901da177e4SLinus Torvalds 		  1,	/* 13.3Mhz */
4911da177e4SLinus Torvalds 		  4,	/*  6.7Hmz */
4921da177e4SLinus Torvalds 	},
4931da177e4SLinus Torvalds 	{ /* dev mode */
4941da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
4951da177e4SLinus Torvalds 		0,
4961da177e4SLinus Torvalds 		0x3f,
4971da177e4SLinus Torvalds 		NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
4981da177e4SLinus Torvalds 			NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
4991da177e4SLinus Torvalds 			NTC_DO_SEND_START,
5001da177e4SLinus Torvalds 		NTC_DO_PARITY_CHK | NTC_DO_SEND_START
5011da177e4SLinus Torvalds 	},
5021da177e4SLinus Torvalds 	{ /* adapter mode */
5031da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5041da177e4SLinus Torvalds 		0,
5051da177e4SLinus Torvalds 		0x2f,
5061da177e4SLinus Torvalds 		NAC_SCANLUN |
5071da177e4SLinus Torvalds 		NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
5081da177e4SLinus Torvalds 			/*| NAC_ACTIVE_NEG*/,
5091da177e4SLinus Torvalds 		NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
5101da177e4SLinus Torvalds 	},
5111da177e4SLinus Torvalds 	{ /* tags */
5121da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5131da177e4SLinus Torvalds 		0,
5141da177e4SLinus Torvalds 		5,
5151da177e4SLinus Torvalds 		3,	/* 16 tags (??) */
5161da177e4SLinus Torvalds 		2,
5171da177e4SLinus Torvalds 	},
5181da177e4SLinus Torvalds 	{ /* reset delay */
5191da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5201da177e4SLinus Torvalds 		0,
5211da177e4SLinus Torvalds 		180,
5221da177e4SLinus Torvalds 		1,	/* 1 second */
5231da177e4SLinus Torvalds 		10,	/* 10 seconds */
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds };
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds /*
52959c51591SMichael Opdenacker  * Safe settings. If set to zero the BIOS/default values with
5301da177e4SLinus Torvalds  * command line overrides will be used. If set to 1 then safe and
5311da177e4SLinus Torvalds  * slow settings will be used.
5321da177e4SLinus Torvalds  */
53390ab5ee9SRusty Russell static bool use_safe_settings = 0;
5341da177e4SLinus Torvalds module_param_named(safe, use_safe_settings, bool, 0);
5351da177e4SLinus Torvalds MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
5391da177e4SLinus Torvalds MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
5421da177e4SLinus 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");
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
5451da177e4SLinus Torvalds MODULE_PARM_DESC(dev_mode, "Device mode.");
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
5481da177e4SLinus Torvalds MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
5511da177e4SLinus Torvalds MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
5541da177e4SLinus Torvalds MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /**
5581da177e4SLinus Torvalds  * set_safe_settings - if the use_safe_settings option is set then
5591da177e4SLinus Torvalds  * set all values to the safe and slow values.
5601da177e4SLinus Torvalds  **/
5616f039790SGreg Kroah-Hartman static void set_safe_settings(void)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	if (use_safe_settings)
5641da177e4SLinus Torvalds 	{
5651da177e4SLinus Torvalds 		int i;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Using safe settings.\n");
5681da177e4SLinus Torvalds 		for (i = 0; i < CFG_NUM; i++)
5691da177e4SLinus Torvalds 		{
5701da177e4SLinus Torvalds 			cfg_data[i].value = cfg_data[i].safe;
5711da177e4SLinus Torvalds 		}
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds /**
5771da177e4SLinus Torvalds  * fix_settings - reset any boot parameters which are out of range
5781da177e4SLinus Torvalds  * back to the default values.
5791da177e4SLinus Torvalds  **/
5806f039790SGreg Kroah-Hartman static void fix_settings(void)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	int i;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	dprintkdbg(DBG_1,
5851da177e4SLinus Torvalds 		"setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
5861da177e4SLinus Torvalds 		"AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
5871da177e4SLinus Torvalds 		cfg_data[CFG_ADAPTER_ID].value,
5881da177e4SLinus Torvalds 		cfg_data[CFG_MAX_SPEED].value,
5891da177e4SLinus Torvalds 		cfg_data[CFG_DEV_MODE].value,
5901da177e4SLinus Torvalds 		cfg_data[CFG_ADAPTER_MODE].value,
5911da177e4SLinus Torvalds 		cfg_data[CFG_TAGS].value,
5921da177e4SLinus Torvalds 		cfg_data[CFG_RESET_DELAY].value);
5931da177e4SLinus Torvalds 	for (i = 0; i < CFG_NUM; i++)
5941da177e4SLinus Torvalds 	{
5951da177e4SLinus Torvalds 		if (cfg_data[i].value < cfg_data[i].min
5961da177e4SLinus Torvalds 		    || cfg_data[i].value > cfg_data[i].max)
5971da177e4SLinus Torvalds 			cfg_data[i].value = cfg_data[i].def;
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds /*
6041da177e4SLinus Torvalds  * Mapping from the eeprom delay index value (index into this array)
60559c51591SMichael Opdenacker  * to the number of actual seconds that the delay should be for.
6061da177e4SLinus Torvalds  */
6076f039790SGreg Kroah-Hartman static char eeprom_index_to_delay_map[] =
6081da177e4SLinus Torvalds 	{ 1, 3, 5, 10, 16, 30, 60, 120 };
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds /**
6121da177e4SLinus Torvalds  * eeprom_index_to_delay - Take the eeprom delay setting and convert it
6131da177e4SLinus Torvalds  * into a number of seconds.
6141da177e4SLinus Torvalds  *
6151da177e4SLinus Torvalds  * @eeprom: The eeprom structure in which we find the delay index to map.
6161da177e4SLinus Torvalds  **/
6176f039790SGreg Kroah-Hartman static void eeprom_index_to_delay(struct NvRamType *eeprom)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds /**
6241da177e4SLinus Torvalds  * delay_to_eeprom_index - Take a delay in seconds and return the
6251da177e4SLinus Torvalds  * closest eeprom index which will delay for at least that amount of
6261da177e4SLinus Torvalds  * seconds.
6271da177e4SLinus Torvalds  *
6281da177e4SLinus Torvalds  * @delay: The delay, in seconds, to find the eeprom index for.
6291da177e4SLinus Torvalds  **/
6306f039790SGreg Kroah-Hartman static int delay_to_eeprom_index(int delay)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	u8 idx = 0;
6331da177e4SLinus Torvalds 	while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
6341da177e4SLinus Torvalds 		idx++;
6351da177e4SLinus Torvalds 	return idx;
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds /**
6401da177e4SLinus Torvalds  * eeprom_override - Override the eeprom settings, in the provided
6411da177e4SLinus Torvalds  * eeprom structure, with values that have been set on the command
6421da177e4SLinus Torvalds  * line.
6431da177e4SLinus Torvalds  *
6441da177e4SLinus Torvalds  * @eeprom: The eeprom data to override with command line options.
6451da177e4SLinus Torvalds  **/
6466f039790SGreg Kroah-Hartman static void eeprom_override(struct NvRamType *eeprom)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	u8 id;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	/* Adapter Settings */
6511da177e4SLinus Torvalds 	if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
6521da177e4SLinus Torvalds 		eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
6551da177e4SLinus Torvalds 		eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
6581da177e4SLinus Torvalds 		eeprom->delay_time = delay_to_eeprom_index(
6591da177e4SLinus Torvalds 					cfg_data[CFG_RESET_DELAY].value);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
6621da177e4SLinus Torvalds 		eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	/* Device Settings */
6651da177e4SLinus Torvalds 	for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
6661da177e4SLinus Torvalds 		if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
6671da177e4SLinus Torvalds 			eeprom->target[id].cfg0 =
6681da177e4SLinus Torvalds 				(u8)cfg_data[CFG_DEV_MODE].value;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 		if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
6711da177e4SLinus Torvalds 			eeprom->target[id].period =
6721da177e4SLinus Torvalds 				(u8)cfg_data[CFG_MAX_SPEED].value;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /*---------------------------------------------------------------------------
6791da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds static unsigned int list_size(struct list_head *head)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	unsigned int count = 0;
6841da177e4SLinus Torvalds 	struct list_head *pos;
6851da177e4SLinus Torvalds 	list_for_each(pos, head)
6861da177e4SLinus Torvalds 		count++;
6871da177e4SLinus Torvalds 	return count;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
6921da177e4SLinus Torvalds 		struct DeviceCtlBlk *pos)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	int use_next = 0;
6951da177e4SLinus Torvalds 	struct DeviceCtlBlk* next = NULL;
6961da177e4SLinus Torvalds 	struct DeviceCtlBlk* i;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (list_empty(head))
6991da177e4SLinus Torvalds 		return NULL;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	/* find supplied dcb and then select the next one */
7021da177e4SLinus Torvalds 	list_for_each_entry(i, head, list)
7031da177e4SLinus Torvalds 		if (use_next) {
7041da177e4SLinus Torvalds 			next = i;
7051da177e4SLinus Torvalds 			break;
7061da177e4SLinus Torvalds 		} else if (i == pos) {
7071da177e4SLinus Torvalds 			use_next = 1;
7081da177e4SLinus Torvalds 		}
7091da177e4SLinus Torvalds 	/* if no next one take the head one (ie, wraparound) */
7101da177e4SLinus Torvalds 	if (!next)
7111da177e4SLinus Torvalds         	list_for_each_entry(i, head, list) {
7121da177e4SLinus Torvalds         		next = i;
7131da177e4SLinus Torvalds         		break;
7141da177e4SLinus Torvalds         	}
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	return next;
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	if (srb->tag_number < 255) {
7231da177e4SLinus Torvalds 		dcb->tag_mask &= ~(1 << srb->tag_number);	/* free tag mask */
7241da177e4SLinus Torvalds 		srb->tag_number = 255;
7251da177e4SLinus Torvalds 	}
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds /* Find cmd in SRB list */
73077933d72SJesper Juhl static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
7311da177e4SLinus Torvalds 		struct list_head *head)
7321da177e4SLinus Torvalds {
7331da177e4SLinus Torvalds 	struct ScsiReqBlk *i;
7341da177e4SLinus Torvalds 	list_for_each_entry(i, head, list)
7351da177e4SLinus Torvalds 		if (i->cmd == cmd)
7361da177e4SLinus Torvalds 			return i;
7371da177e4SLinus Torvalds 	return NULL;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds /* Sets the timer to wake us up */
7411da177e4SLinus Torvalds static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
7441da177e4SLinus Torvalds 		return;
74553a8936cSHannes Reinecke 	if (time_before(jiffies + to, acb->last_reset - HZ / 2))
7461da177e4SLinus Torvalds 		acb->waiting_timer.expires =
74753a8936cSHannes Reinecke 		    acb->last_reset - HZ / 2 + 1;
7481da177e4SLinus Torvalds 	else
7491da177e4SLinus Torvalds 		acb->waiting_timer.expires = jiffies + to + 1;
7501da177e4SLinus Torvalds 	add_timer(&acb->waiting_timer);
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /* Send the next command from the waiting list to the bus */
7551da177e4SLinus Torvalds static void waiting_process_next(struct AdapterCtlBlk *acb)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds 	struct DeviceCtlBlk *start = NULL;
7581da177e4SLinus Torvalds 	struct DeviceCtlBlk *pos;
7591da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
7601da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
7611da177e4SLinus Torvalds 	struct list_head *dcb_list_head = &acb->dcb_list;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	if (acb->active_dcb
7641da177e4SLinus Torvalds 	    || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
7651da177e4SLinus Torvalds 		return;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
7681da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	if (list_empty(dcb_list_head))
7711da177e4SLinus Torvalds 		return;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	/*
7741da177e4SLinus Torvalds 	 * Find the starting dcb. Need to find it again in the list
7751da177e4SLinus Torvalds 	 * since the list may have changed since we set the ptr to it
7761da177e4SLinus Torvalds 	 */
7771da177e4SLinus Torvalds 	list_for_each_entry(dcb, dcb_list_head, list)
7781da177e4SLinus Torvalds 		if (dcb == acb->dcb_run_robin) {
7791da177e4SLinus Torvalds 			start = dcb;
7801da177e4SLinus Torvalds 			break;
7811da177e4SLinus Torvalds 		}
7821da177e4SLinus Torvalds 	if (!start) {
7831da177e4SLinus Torvalds 		/* This can happen! */
7841da177e4SLinus Torvalds 		start = list_entry(dcb_list_head->next, typeof(*start), list);
7851da177e4SLinus Torvalds 		acb->dcb_run_robin = start;
7861da177e4SLinus Torvalds 	}
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	/*
7901da177e4SLinus Torvalds 	 * Loop over the dcb, but we start somewhere (potentially) in
7911da177e4SLinus Torvalds 	 * the middle of the loop so we need to manully do this.
7921da177e4SLinus Torvalds 	 */
7931da177e4SLinus Torvalds 	pos = start;
7941da177e4SLinus Torvalds 	do {
7951da177e4SLinus Torvalds 		struct list_head *waiting_list_head = &pos->srb_waiting_list;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 		/* Make sure, the next another device gets scheduled ... */
7981da177e4SLinus Torvalds 		acb->dcb_run_robin = dcb_get_next(dcb_list_head,
7991da177e4SLinus Torvalds 						  acb->dcb_run_robin);
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 		if (list_empty(waiting_list_head) ||
8021da177e4SLinus Torvalds 		    pos->max_command <= list_size(&pos->srb_going_list)) {
8031da177e4SLinus Torvalds 			/* move to next dcb */
8041da177e4SLinus Torvalds 			pos = dcb_get_next(dcb_list_head, pos);
8051da177e4SLinus Torvalds 		} else {
8061da177e4SLinus Torvalds 			srb = list_entry(waiting_list_head->next,
8071da177e4SLinus Torvalds 					 struct ScsiReqBlk, list);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 			/* Try to send to the bus */
8101da177e4SLinus Torvalds 			if (!start_scsi(acb, pos, srb))
811cd29660dSChristoph Hellwig 				list_move(&srb->list, &pos->srb_going_list);
8121da177e4SLinus Torvalds 			else
8131da177e4SLinus Torvalds 				waiting_set_timer(acb, HZ/50);
8141da177e4SLinus Torvalds 			break;
8151da177e4SLinus Torvalds 		}
8161da177e4SLinus Torvalds 	} while (pos != start);
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds /* Wake up waiting queue */
821af53b89bSKees Cook static void waiting_timeout(struct timer_list *t)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	unsigned long flags;
824af53b89bSKees Cook 	struct AdapterCtlBlk *acb = from_timer(acb, t, waiting_timer);
8251da177e4SLinus Torvalds 	dprintkdbg(DBG_1,
8261da177e4SLinus Torvalds 		"waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
8271da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
8281da177e4SLinus Torvalds 	waiting_process_next(acb);
8291da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds /* Get the DCB for a given ID/LUN combination */
8341da177e4SLinus Torvalds static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	return acb->children[id][lun];
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds /* Send SCSI Request Block (srb) to adapter (acb) */
8411da177e4SLinus Torvalds static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
8461da177e4SLinus Torvalds 	    acb->active_dcb ||
8471da177e4SLinus Torvalds 	    (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
848cd29660dSChristoph Hellwig 		list_add_tail(&srb->list, &dcb->srb_waiting_list);
8491da177e4SLinus Torvalds 		waiting_process_next(acb);
8501da177e4SLinus Torvalds 		return;
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 
853cd29660dSChristoph Hellwig 	if (!start_scsi(acb, dcb, srb)) {
854cd29660dSChristoph Hellwig 		list_add_tail(&srb->list, &dcb->srb_going_list);
855cd29660dSChristoph Hellwig 	} else {
856cd29660dSChristoph Hellwig 		list_add(&srb->list, &dcb->srb_waiting_list);
8571da177e4SLinus Torvalds 		waiting_set_timer(acb, HZ / 50);
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds /* Prepare SRB for being sent to Device DCB w/ command *cmd */
8621da177e4SLinus Torvalds static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
8631da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
8641da177e4SLinus Torvalds {
865a862ea31SFUJITA Tomonori 	int nseg;
8661da177e4SLinus Torvalds 	enum dma_data_direction dir = cmd->sc_data_direction;
8675cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "build_srb: (0x%p) <%02i-%i>\n",
8685cd049a5SChristoph Hellwig 		cmd, dcb->target_id, dcb->target_lun);
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	srb->dcb = dcb;
8711da177e4SLinus Torvalds 	srb->cmd = cmd;
8721da177e4SLinus Torvalds 	srb->sg_count = 0;
8731da177e4SLinus Torvalds 	srb->total_xfer_length = 0;
8741da177e4SLinus Torvalds 	srb->sg_bus_addr = 0;
8751da177e4SLinus Torvalds 	srb->sg_index = 0;
8761da177e4SLinus Torvalds 	srb->adapter_status = 0;
8771da177e4SLinus Torvalds 	srb->target_status = 0;
8781da177e4SLinus Torvalds 	srb->msg_count = 0;
8791da177e4SLinus Torvalds 	srb->status = 0;
8801da177e4SLinus Torvalds 	srb->flag = 0;
8811da177e4SLinus Torvalds 	srb->state = 0;
8821da177e4SLinus Torvalds 	srb->retry_count = 0;
8831da177e4SLinus Torvalds 	srb->tag_number = TAG_NONE;
8841da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
8851da177e4SLinus Torvalds 	srb->end_message = 0;
8861da177e4SLinus Torvalds 
887a862ea31SFUJITA Tomonori 	nseg = scsi_dma_map(cmd);
888a862ea31SFUJITA Tomonori 	BUG_ON(nseg < 0);
889a862ea31SFUJITA Tomonori 
890ece0eeffSSuraj Upadhyay 	if (dir == DMA_NONE || !nseg) {
8911da177e4SLinus Torvalds 		dprintkdbg(DBG_0,
8921da177e4SLinus Torvalds 			"build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
893a862ea31SFUJITA Tomonori 			   cmd->bufflen, scsi_sglist(cmd), scsi_sg_count(cmd),
894a862ea31SFUJITA Tomonori 			   srb->segment_x[0].address);
895a862ea31SFUJITA Tomonori 	} else {
8961da177e4SLinus Torvalds 		int i;
897a862ea31SFUJITA Tomonori 		u32 reqlen = scsi_bufflen(cmd);
898a862ea31SFUJITA Tomonori 		struct scatterlist *sg;
8991da177e4SLinus Torvalds 		struct SGentry *sgp = srb->segment_x;
900a862ea31SFUJITA Tomonori 
901a862ea31SFUJITA Tomonori 		srb->sg_count = nseg;
902a862ea31SFUJITA Tomonori 
9031da177e4SLinus Torvalds 		dprintkdbg(DBG_0,
9041da177e4SLinus Torvalds 			   "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
905a862ea31SFUJITA Tomonori 			   reqlen, scsi_sglist(cmd), scsi_sg_count(cmd),
9061da177e4SLinus Torvalds 			   srb->sg_count);
9071da177e4SLinus Torvalds 
908a862ea31SFUJITA Tomonori 		scsi_for_each_sg(cmd, sg, srb->sg_count, i) {
909a862ea31SFUJITA Tomonori 			u32 busaddr = (u32)sg_dma_address(sg);
910a862ea31SFUJITA Tomonori 			u32 seglen = (u32)sg->length;
911b4b08e58SLinus Torvalds 			sgp[i].address = busaddr;
9121da177e4SLinus Torvalds 			sgp[i].length = seglen;
9131da177e4SLinus Torvalds 			srb->total_xfer_length += seglen;
9141da177e4SLinus Torvalds 		}
9151da177e4SLinus Torvalds 		sgp += srb->sg_count - 1;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		/*
9181da177e4SLinus Torvalds 		 * adjust last page if too big as it is allocated
9191da177e4SLinus Torvalds 		 * on even page boundaries
9201da177e4SLinus Torvalds 		 */
9211da177e4SLinus Torvalds 		if (srb->total_xfer_length > reqlen) {
9221da177e4SLinus Torvalds 			sgp->length -= (srb->total_xfer_length - reqlen);
9231da177e4SLinus Torvalds 			srb->total_xfer_length = reqlen;
9241da177e4SLinus Torvalds 		}
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 		/* Fixup for WIDE padding - make sure length is even */
9271da177e4SLinus Torvalds 		if (dcb->sync_period & WIDE_SYNC &&
9281da177e4SLinus Torvalds 		    srb->total_xfer_length % 2) {
9291da177e4SLinus Torvalds 			srb->total_xfer_length++;
9301da177e4SLinus Torvalds 			sgp->length++;
9311da177e4SLinus Torvalds 		}
9321da177e4SLinus Torvalds 
933dfda5e21SChristoph Hellwig 		srb->sg_bus_addr = dma_map_single(&dcb->acb->dev->dev,
934dfda5e21SChristoph Hellwig 				srb->segment_x, SEGMENTX_LEN, DMA_TO_DEVICE);
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 		dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
9371da177e4SLinus Torvalds 			srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
9381da177e4SLinus Torvalds 	}
939cdb8c2a6SGuennadi Liakhovetski 
940cdb8c2a6SGuennadi Liakhovetski 	srb->request_length = srb->total_xfer_length;
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds /**
945167b7e6bSLee Jones  * dc395x_queue_command_lck - queue scsi command passed from the mid
9461da177e4SLinus Torvalds  * layer, invoke 'done' on completion
9471da177e4SLinus Torvalds  *
9481da177e4SLinus Torvalds  * @cmd: pointer to scsi command object
9491da177e4SLinus Torvalds  * @done: function pointer to be invoked on completion
9501da177e4SLinus Torvalds  *
9511da177e4SLinus Torvalds  * Returns 1 if the adapter (host) is busy, else returns 0. One
9521da177e4SLinus Torvalds  * reason for an adapter to be busy is that the number
9531da177e4SLinus Torvalds  * of outstanding queued commands is already equal to
9541da177e4SLinus Torvalds  * struct Scsi_Host::can_queue .
9551da177e4SLinus Torvalds  *
9561da177e4SLinus Torvalds  * Required: if struct Scsi_Host::can_queue is ever non-zero
9571da177e4SLinus Torvalds  *           then this function is required.
9581da177e4SLinus Torvalds  *
9591da177e4SLinus Torvalds  * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
9601da177e4SLinus Torvalds  *        and is expected to be held on return.
9611da177e4SLinus Torvalds  *
9621da177e4SLinus Torvalds  **/
963f281233dSJeff Garzik static int dc395x_queue_command_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
9641da177e4SLinus Torvalds {
9651da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
9661da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
9671da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb =
9681da177e4SLinus Torvalds 	    (struct AdapterCtlBlk *)cmd->device->host->hostdata;
9695cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "queue_command: (0x%p) <%02i-%i> cmnd=0x%02x\n",
9709cb78c16SHannes Reinecke 		cmd, cmd->device->id, (u8)cmd->device->lun, cmd->cmnd[0]);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	/* Assume BAD_TARGET; will be cleared later */
97375dd4439SHannes Reinecke 	set_host_byte(cmd, DID_BAD_TARGET);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	/* ignore invalid targets */
9761da177e4SLinus Torvalds 	if (cmd->device->id >= acb->scsi_host->max_id ||
9771da177e4SLinus Torvalds 	    cmd->device->lun >= acb->scsi_host->max_lun ||
9781da177e4SLinus Torvalds 	    cmd->device->lun >31) {
9791da177e4SLinus Torvalds 		goto complete;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	/* does the specified lun on the specified device exist */
9831da177e4SLinus Torvalds 	if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
9841da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
9859cb78c16SHannes Reinecke 			cmd->device->id, (u8)cmd->device->lun);
9861da177e4SLinus Torvalds 		goto complete;
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	/* do we have a DCB for the device */
9901da177e4SLinus Torvalds 	dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
9911da177e4SLinus Torvalds 	if (!dcb) {
9921da177e4SLinus Torvalds 		/* should never happen */
9931da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
9949cb78c16SHannes Reinecke 			cmd->device->id, (u8)cmd->device->lun);
9951da177e4SLinus Torvalds 		goto complete;
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	/* set callback and clear result in the command */
9991da177e4SLinus Torvalds 	cmd->scsi_done = done;
100075dd4439SHannes Reinecke 	set_host_byte(cmd, DID_OK);
100175dd4439SHannes Reinecke 	set_status_byte(cmd, SAM_STAT_GOOD);
10021da177e4SLinus Torvalds 
1003cd29660dSChristoph Hellwig 	srb = list_first_entry_or_null(&acb->srb_free_list,
1004cd29660dSChristoph Hellwig 			struct ScsiReqBlk, list);
1005cd29660dSChristoph Hellwig 	if (!srb) {
10061da177e4SLinus Torvalds 		/*
10071da177e4SLinus Torvalds 		 * Return 1 since we are unable to queue this command at this
10081da177e4SLinus Torvalds 		 * point in time.
10091da177e4SLinus Torvalds 		 */
10101da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "queue_command: No free srb's\n");
10111da177e4SLinus Torvalds 		return 1;
10121da177e4SLinus Torvalds 	}
1013cd29660dSChristoph Hellwig 	list_del(&srb->list);
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 	build_srb(cmd, dcb, srb);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	if (!list_empty(&dcb->srb_waiting_list)) {
10181da177e4SLinus Torvalds 		/* append to waiting queue */
1019cd29660dSChristoph Hellwig 		list_add_tail(&srb->list, &dcb->srb_waiting_list);
10201da177e4SLinus Torvalds 		waiting_process_next(acb);
10211da177e4SLinus Torvalds 	} else {
10221da177e4SLinus Torvalds 		/* process immediately */
10231da177e4SLinus Torvalds 		send_srb(acb, srb);
10241da177e4SLinus Torvalds 	}
10255cd049a5SChristoph Hellwig 	dprintkdbg(DBG_1, "queue_command: (0x%p) done\n", cmd);
10261da177e4SLinus Torvalds 	return 0;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds complete:
10291da177e4SLinus Torvalds 	/*
10301da177e4SLinus Torvalds 	 * Complete the command immediatey, and then return 0 to
10311da177e4SLinus Torvalds 	 * indicate that we have handled the command. This is usually
10321da177e4SLinus Torvalds 	 * done when the commad is for things like non existent
10331da177e4SLinus Torvalds 	 * devices.
10341da177e4SLinus Torvalds 	 */
10351da177e4SLinus Torvalds 	done(cmd);
10361da177e4SLinus Torvalds 	return 0;
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
1039f281233dSJeff Garzik static DEF_SCSI_QCMD(dc395x_queue_command)
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds static void dump_register_info(struct AdapterCtlBlk *acb,
10421da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
10431da177e4SLinus Torvalds {
10441da177e4SLinus Torvalds 	u16 pstat;
10451da177e4SLinus Torvalds 	struct pci_dev *dev = acb->dev;
10461da177e4SLinus Torvalds 	pci_read_config_word(dev, PCI_STATUS, &pstat);
10471da177e4SLinus Torvalds 	if (!dcb)
10481da177e4SLinus Torvalds 		dcb = acb->active_dcb;
10491da177e4SLinus Torvalds 	if (!srb && dcb)
10501da177e4SLinus Torvalds 		srb = dcb->active_srb;
10511da177e4SLinus Torvalds 	if (srb) {
10521da177e4SLinus Torvalds 		if (!srb->cmd)
10531da177e4SLinus Torvalds 			dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
10541da177e4SLinus Torvalds 				srb, srb->cmd);
10551da177e4SLinus Torvalds 		else
10565cd049a5SChristoph Hellwig 			dprintkl(KERN_INFO, "dump: srb=%p cmd=%p "
10571da177e4SLinus Torvalds 				 "cmnd=0x%02x <%02i-%i>\n",
10585cd049a5SChristoph Hellwig 				srb, srb->cmd,
10591da177e4SLinus Torvalds 				srb->cmd->cmnd[0], srb->cmd->device->id,
10609cb78c16SHannes Reinecke 				(u8)srb->cmd->device->lun);
10613c5c6658SAl Viro 		printk("  sglist=%p cnt=%i idx=%i len=%zu\n",
10621da177e4SLinus Torvalds 		       srb->segment_x, srb->sg_count, srb->sg_index,
10631da177e4SLinus Torvalds 		       srb->total_xfer_length);
10641da177e4SLinus Torvalds 		printk("  state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
10651da177e4SLinus Torvalds 		       srb->state, srb->status, srb->scsi_phase,
10661da177e4SLinus Torvalds 		       (acb->active_dcb) ? "" : "not");
10671da177e4SLinus Torvalds 	}
10681da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
10691da177e4SLinus Torvalds 		"signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
10701da177e4SLinus Torvalds 		"rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
10711da177e4SLinus Torvalds 		"config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
10721da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
10731da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
10741da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
10751da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
10761da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
10771da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
10781da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
10791da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
10801da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
10811da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
10821da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
10831da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
10841da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
10851da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
10861da177e4SLinus Torvalds 		"irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
10871da177e4SLinus Torvalds 		"ctctr=0x%08x addr=0x%08x:0x%08x}\n",
10881da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
10891da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
10901da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
10911da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_STATUS),
10921da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_INTEN),
10931da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
10941da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_XCNT),
10951da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
10961da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
10971da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
10981da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
10991da177e4SLinus Torvalds 		"pci{status=0x%04x}\n",
11001da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
11011da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_GEN_STATUS),
11021da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_GEN_TIMER),
11031da177e4SLinus Torvalds 		pstat);
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds #if debug_enabled(DBG_FIFO)
11101da177e4SLinus Torvalds 	u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
11111da177e4SLinus Torvalds 	u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
11121da177e4SLinus Torvalds 	if (!(fifocnt & 0x40))
11131da177e4SLinus Torvalds 		dprintkdbg(DBG_FIFO,
11141da177e4SLinus Torvalds 			"clear_fifo: (%i bytes) on phase %02x in %s\n",
11151da177e4SLinus Torvalds 			fifocnt & 0x3f, lines, txt);
11161da177e4SLinus Torvalds #endif
11171da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds static void reset_dev_param(struct AdapterCtlBlk *acb)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
11241da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
11251da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
11281da177e4SLinus Torvalds 		u8 period_index;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 		dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
11311da177e4SLinus Torvalds 		dcb->sync_period = 0;
11321da177e4SLinus Torvalds 		dcb->sync_offset = 0;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 		dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
11351da177e4SLinus Torvalds 		period_index = eeprom->target[dcb->target_id].period & 0x07;
11361da177e4SLinus Torvalds 		dcb->min_nego_period = clock_period[period_index];
11371da177e4SLinus Torvalds 		if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
11381da177e4SLinus Torvalds 		    || !(acb->config & HCC_WIDE_CARD))
11391da177e4SLinus Torvalds 			dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds /*
11451da177e4SLinus Torvalds  * perform a hard reset on the SCSI bus
11461da177e4SLinus Torvalds  * @cmd - some command for this host (for fetching hooks)
11471da177e4SLinus Torvalds  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
11481da177e4SLinus Torvalds  */
114968b3aa7cSJeff Garzik  static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
11501da177e4SLinus Torvalds {
11511da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb =
11521da177e4SLinus Torvalds 		(struct AdapterCtlBlk *)cmd->device->host->hostdata;
11531da177e4SLinus Torvalds 	dprintkl(KERN_INFO,
11545cd049a5SChristoph Hellwig 		"eh_bus_reset: (0%p) target=<%02i-%i> cmd=%p\n",
11559cb78c16SHannes Reinecke 		cmd, cmd->device->id, (u8)cmd->device->lun, cmd);
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
11581da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	/*
11611da177e4SLinus Torvalds 	 * disable interrupt
11621da177e4SLinus Torvalds 	 */
11631da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
11641da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
11651da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
11661da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	reset_scsi_bus(acb);
11691da177e4SLinus Torvalds 	udelay(500);
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	/* We may be in serious trouble. Wait some seconds */
117253a8936cSHannes Reinecke 	acb->last_reset =
11731da177e4SLinus Torvalds 	    jiffies + 3 * HZ / 2 +
11741da177e4SLinus Torvalds 	    HZ * acb->eeprom.delay_time;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	/*
11771da177e4SLinus Torvalds 	 * re-enable interrupt
11781da177e4SLinus Torvalds 	 */
11791da177e4SLinus Torvalds 	/* Clear SCSI FIFO          */
11801da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
11811da177e4SLinus Torvalds 	clear_fifo(acb, "eh_bus_reset");
11821da177e4SLinus Torvalds 	/* Delete pending IRQ */
11831da177e4SLinus Torvalds 	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
11841da177e4SLinus Torvalds 	set_basic_config(acb);
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	reset_dev_param(acb);
11871da177e4SLinus Torvalds 	doing_srb_done(acb, DID_RESET, cmd, 0);
11881da177e4SLinus Torvalds 	acb->active_dcb = NULL;
11891da177e4SLinus Torvalds 	acb->acb_flag = 0;	/* RESET_DETECT, RESET_DONE ,RESET_DEV */
11901da177e4SLinus Torvalds 	waiting_process_next(acb);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	return SUCCESS;
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds 
119568b3aa7cSJeff Garzik  static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
119668b3aa7cSJeff Garzik  {
119768b3aa7cSJeff Garzik  	int rc;
119868b3aa7cSJeff Garzik  
119968b3aa7cSJeff Garzik  	spin_lock_irq(cmd->device->host->host_lock);
120068b3aa7cSJeff Garzik  	rc = __dc395x_eh_bus_reset(cmd);
120168b3aa7cSJeff Garzik  	spin_unlock_irq(cmd->device->host->host_lock);
120268b3aa7cSJeff Garzik  
120368b3aa7cSJeff Garzik  	return rc;
120468b3aa7cSJeff Garzik  }
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds /*
12071da177e4SLinus Torvalds  * abort an errant SCSI command
12081da177e4SLinus Torvalds  * @cmd - command to be aborted
12091da177e4SLinus Torvalds  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
12101da177e4SLinus Torvalds  */
12111da177e4SLinus Torvalds static int dc395x_eh_abort(struct scsi_cmnd *cmd)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	/*
12141da177e4SLinus Torvalds 	 * Look into our command queues: If it has not been sent already,
12151da177e4SLinus Torvalds 	 * we remove it and return success. Otherwise fail.
12161da177e4SLinus Torvalds 	 */
12171da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb =
12181da177e4SLinus Torvalds 	    (struct AdapterCtlBlk *)cmd->device->host->hostdata;
12191da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
12201da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
12215cd049a5SChristoph Hellwig 	dprintkl(KERN_INFO, "eh_abort: (0x%p) target=<%02i-%i> cmd=%p\n",
12229cb78c16SHannes Reinecke 		cmd, cmd->device->id, (u8)cmd->device->lun, cmd);
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
12251da177e4SLinus Torvalds 	if (!dcb) {
12261da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
12271da177e4SLinus Torvalds 		return FAILED;
12281da177e4SLinus Torvalds 	}
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	srb = find_cmd(cmd, &dcb->srb_waiting_list);
12311da177e4SLinus Torvalds 	if (srb) {
1232cd29660dSChristoph Hellwig 		list_del(&srb->list);
12331da177e4SLinus Torvalds 		pci_unmap_srb_sense(acb, srb);
12341da177e4SLinus Torvalds 		pci_unmap_srb(acb, srb);
12351da177e4SLinus Torvalds 		free_tag(dcb, srb);
1236cd29660dSChristoph Hellwig 		list_add_tail(&srb->list, &acb->srb_free_list);
12371da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
123875dd4439SHannes Reinecke 		set_host_byte(cmd, DID_ABORT);
12391da177e4SLinus Torvalds 		return SUCCESS;
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 	srb = find_cmd(cmd, &dcb->srb_going_list);
12421da177e4SLinus Torvalds 	if (srb) {
1243cdb8c2a6SGuennadi Liakhovetski 		dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n");
12441da177e4SLinus Torvalds 		/* XXX: Should abort the command here */
12451da177e4SLinus Torvalds 	} else {
1246cdb8c2a6SGuennadi Liakhovetski 		dprintkl(KERN_DEBUG, "eh_abort: Command not found\n");
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 	return FAILED;
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds /* SDTR */
12531da177e4SLinus Torvalds static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
12541da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
12551da177e4SLinus Torvalds {
12561da177e4SLinus Torvalds 	u8 *ptr = srb->msgout_buf + srb->msg_count;
12571da177e4SLinus Torvalds 	if (srb->msg_count > 1) {
12581da177e4SLinus Torvalds 		dprintkl(KERN_INFO,
12591da177e4SLinus Torvalds 			"build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
12601da177e4SLinus Torvalds 			srb->msg_count, srb->msgout_buf[0],
12611da177e4SLinus Torvalds 			srb->msgout_buf[1]);
12621da177e4SLinus Torvalds 		return;
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 	if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
12651da177e4SLinus Torvalds 		dcb->sync_offset = 0;
12661da177e4SLinus Torvalds 		dcb->min_nego_period = 200 >> 2;
12671da177e4SLinus Torvalds 	} else if (dcb->sync_offset == 0)
12681da177e4SLinus Torvalds 		dcb->sync_offset = SYNC_NEGO_OFFSET;
12691da177e4SLinus Torvalds 
12709c2d2670SHannes Reinecke 	srb->msg_count += spi_populate_sync_msg(ptr, dcb->min_nego_period,
12719c2d2670SHannes Reinecke 						dcb->sync_offset);
12721da177e4SLinus Torvalds 	srb->state |= SRB_DO_SYNC_NEGO;
12731da177e4SLinus Torvalds }
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds /* WDTR */
12771da177e4SLinus Torvalds static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
12781da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
12791da177e4SLinus Torvalds {
12801da177e4SLinus Torvalds 	u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
12811da177e4SLinus Torvalds 		   (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
12821da177e4SLinus Torvalds 	u8 *ptr = srb->msgout_buf + srb->msg_count;
12831da177e4SLinus Torvalds 	if (srb->msg_count > 1) {
12841da177e4SLinus Torvalds 		dprintkl(KERN_INFO,
12851da177e4SLinus Torvalds 			"build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
12861da177e4SLinus Torvalds 			srb->msg_count, srb->msgout_buf[0],
12871da177e4SLinus Torvalds 			srb->msgout_buf[1]);
12881da177e4SLinus Torvalds 		return;
12891da177e4SLinus Torvalds 	}
12909c2d2670SHannes Reinecke 	srb->msg_count += spi_populate_width_msg(ptr, wide);
12911da177e4SLinus Torvalds 	srb->state |= SRB_DO_WIDE_NEGO;
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds #if 0
12961da177e4SLinus Torvalds /* Timer to work around chip flaw: When selecting and the bus is
12971da177e4SLinus Torvalds  * busy, we sometimes miss a Selection timeout IRQ */
12981da177e4SLinus Torvalds void selection_timeout_missed(unsigned long ptr);
12991da177e4SLinus Torvalds /* Sets the timer to wake us up */
13001da177e4SLinus Torvalds static void selto_timer(struct AdapterCtlBlk *acb)
13011da177e4SLinus Torvalds {
13021da177e4SLinus Torvalds 	if (timer_pending(&acb->selto_timer))
13031da177e4SLinus Torvalds 		return;
13041da177e4SLinus Torvalds 	acb->selto_timer.function = selection_timeout_missed;
13051da177e4SLinus Torvalds 	acb->selto_timer.data = (unsigned long) acb;
13061da177e4SLinus Torvalds 	if (time_before
130753a8936cSHannes Reinecke 	    (jiffies + HZ, acb->last_reset + HZ / 2))
13081da177e4SLinus Torvalds 		acb->selto_timer.expires =
130953a8936cSHannes Reinecke 		    acb->last_reset + HZ / 2 + 1;
13101da177e4SLinus Torvalds 	else
13111da177e4SLinus Torvalds 		acb->selto_timer.expires = jiffies + HZ + 1;
13121da177e4SLinus Torvalds 	add_timer(&acb->selto_timer);
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds void selection_timeout_missed(unsigned long ptr)
13171da177e4SLinus Torvalds {
13181da177e4SLinus Torvalds 	unsigned long flags;
13191da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
13201da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
13211da177e4SLinus Torvalds 	dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
13221da177e4SLinus Torvalds 	if (!acb->active_dcb || !acb->active_dcb->active_srb) {
13231da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
13241da177e4SLinus Torvalds 		return;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
13271da177e4SLinus Torvalds 	srb = acb->active_dcb->active_srb;
13281da177e4SLinus Torvalds 	disconnect(acb);
13291da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds #endif
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
13351da177e4SLinus Torvalds 		struct ScsiReqBlk* srb)
13361da177e4SLinus Torvalds {
13376c22079eSLee Jones 	u16 __maybe_unused s_stat2, return_code;
13381da177e4SLinus Torvalds 	u8 s_stat, scsicommand, i, identify_message;
13391da177e4SLinus Torvalds 	u8 *ptr;
13405cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> srb=%p\n",
13415cd049a5SChristoph Hellwig 		dcb->target_id, dcb->target_lun, srb);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	srb->tag_number = TAG_NONE;	/* acb->tag_max_num: had error read in eeprom */
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
13461da177e4SLinus Torvalds 	s_stat2 = 0;
13471da177e4SLinus Torvalds 	s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
13481da177e4SLinus Torvalds #if 1
13491da177e4SLinus Torvalds 	if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
13505cd049a5SChristoph Hellwig 		dprintkdbg(DBG_KG, "start_scsi: (0x%p) BUSY %02x %04x\n",
13515cd049a5SChristoph Hellwig 			s_stat, s_stat2);
13521da177e4SLinus Torvalds 		/*
13531da177e4SLinus Torvalds 		 * Try anyway?
13541da177e4SLinus Torvalds 		 *
13551da177e4SLinus Torvalds 		 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1356b71a8eb0SUwe Kleine-König 		 * Timeout, a Disconnect or a Reselection IRQ, so we would be screwed!
13571da177e4SLinus Torvalds 		 * (This is likely to be a bug in the hardware. Obviously, most people
13581da177e4SLinus Torvalds 		 *  only have one initiator per SCSI bus.)
13591da177e4SLinus Torvalds 		 * Instead let this fail and have the timer make sure the command is
13601da177e4SLinus Torvalds 		 * tried again after a short time
13611da177e4SLinus Torvalds 		 */
13621da177e4SLinus Torvalds 		/*selto_timer (acb); */
13631da177e4SLinus Torvalds 		return 1;
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds #endif
13661da177e4SLinus Torvalds 	if (acb->active_dcb) {
13675cd049a5SChristoph Hellwig 		dprintkl(KERN_DEBUG, "start_scsi: (0x%p) Attempt to start a"
13685cd049a5SChristoph Hellwig 			"command while another command (0x%p) is active.",
13695cd049a5SChristoph Hellwig 			srb->cmd,
13701da177e4SLinus Torvalds 			acb->active_dcb->active_srb ?
13715cd049a5SChristoph Hellwig 			    acb->active_dcb->active_srb->cmd : 0);
13721da177e4SLinus Torvalds 		return 1;
13731da177e4SLinus Torvalds 	}
13741da177e4SLinus Torvalds 	if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
13755cd049a5SChristoph Hellwig 		dprintkdbg(DBG_KG, "start_scsi: (0x%p) Failed (busy)\n", srb->cmd);
13761da177e4SLinus Torvalds 		return 1;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 	/* Allow starting of SCSI commands half a second before we allow the mid-level
13791da177e4SLinus Torvalds 	 * to queue them again after a reset */
138053a8936cSHannes Reinecke 	if (time_before(jiffies, acb->last_reset - HZ / 2)) {
13811da177e4SLinus Torvalds 		dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
13821da177e4SLinus Torvalds 		return 1;
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	/* Flush FIFO */
13861da177e4SLinus Torvalds 	clear_fifo(acb, "start_scsi");
13871da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
13881da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
13891da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
13901da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
13911da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	identify_message = dcb->identify_msg;
13941da177e4SLinus Torvalds 	/*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
13951da177e4SLinus Torvalds 	/* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
13961da177e4SLinus Torvalds 	if (srb->flag & AUTO_REQSENSE)
13971da177e4SLinus Torvalds 		identify_message &= 0xBF;
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	if (((srb->cmd->cmnd[0] == INQUIRY)
14001da177e4SLinus Torvalds 	     || (srb->cmd->cmnd[0] == REQUEST_SENSE)
14011da177e4SLinus Torvalds 	     || (srb->flag & AUTO_REQSENSE))
14021da177e4SLinus Torvalds 	    && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
14031da177e4SLinus Torvalds 		 && !(dcb->sync_mode & WIDE_NEGO_DONE))
14041da177e4SLinus Torvalds 		|| ((dcb->sync_mode & SYNC_NEGO_ENABLE)
14051da177e4SLinus Torvalds 		    && !(dcb->sync_mode & SYNC_NEGO_DONE)))
14061da177e4SLinus Torvalds 	    && (dcb->target_lun == 0)) {
14071da177e4SLinus Torvalds 		srb->msgout_buf[0] = identify_message;
14081da177e4SLinus Torvalds 		srb->msg_count = 1;
14091da177e4SLinus Torvalds 		scsicommand = SCMD_SEL_ATNSTOP;
14101da177e4SLinus Torvalds 		srb->state = SRB_MSGOUT;
14111da177e4SLinus Torvalds #ifndef SYNC_FIRST
14121da177e4SLinus Torvalds 		if (dcb->sync_mode & WIDE_NEGO_ENABLE
14131da177e4SLinus Torvalds 		    && dcb->inquiry7 & SCSI_INQ_WBUS16) {
14141da177e4SLinus Torvalds 			build_wdtr(acb, dcb, srb);
14151da177e4SLinus Torvalds 			goto no_cmd;
14161da177e4SLinus Torvalds 		}
14171da177e4SLinus Torvalds #endif
14181da177e4SLinus Torvalds 		if (dcb->sync_mode & SYNC_NEGO_ENABLE
14191da177e4SLinus Torvalds 		    && dcb->inquiry7 & SCSI_INQ_SYNC) {
14201da177e4SLinus Torvalds 			build_sdtr(acb, dcb, srb);
14211da177e4SLinus Torvalds 			goto no_cmd;
14221da177e4SLinus Torvalds 		}
14231da177e4SLinus Torvalds 		if (dcb->sync_mode & WIDE_NEGO_ENABLE
14241da177e4SLinus Torvalds 		    && dcb->inquiry7 & SCSI_INQ_WBUS16) {
14251da177e4SLinus Torvalds 			build_wdtr(acb, dcb, srb);
14261da177e4SLinus Torvalds 			goto no_cmd;
14271da177e4SLinus Torvalds 		}
14281da177e4SLinus Torvalds 		srb->msg_count = 0;
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds 	/* Send identify message */
14311da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	scsicommand = SCMD_SEL_ATN;
14341da177e4SLinus Torvalds 	srb->state = SRB_START_;
14351da177e4SLinus Torvalds #ifndef DC395x_NO_TAGQ
14361da177e4SLinus Torvalds 	if ((dcb->sync_mode & EN_TAG_QUEUEING)
14371da177e4SLinus Torvalds 	    && (identify_message & 0xC0)) {
14381da177e4SLinus Torvalds 		/* Send Tag message */
14391da177e4SLinus Torvalds 		u32 tag_mask = 1;
14401da177e4SLinus Torvalds 		u8 tag_number = 0;
14411da177e4SLinus Torvalds 		while (tag_mask & dcb->tag_mask
1442d8187b94SRoel Kluin 		       && tag_number < dcb->max_command) {
14431da177e4SLinus Torvalds 			tag_mask = tag_mask << 1;
14441da177e4SLinus Torvalds 			tag_number++;
14451da177e4SLinus Torvalds 		}
14461da177e4SLinus Torvalds 		if (tag_number >= dcb->max_command) {
14475cd049a5SChristoph Hellwig 			dprintkl(KERN_WARNING, "start_scsi: (0x%p) "
14481da177e4SLinus Torvalds 				"Out of tags target=<%02i-%i>)\n",
14495cd049a5SChristoph Hellwig 				srb->cmd, srb->cmd->device->id,
14509cb78c16SHannes Reinecke 				(u8)srb->cmd->device->lun);
14511da177e4SLinus Torvalds 			srb->state = SRB_READY;
14521da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
14531da177e4SLinus Torvalds 				       DO_HWRESELECT);
14541da177e4SLinus Torvalds 			return 1;
14551da177e4SLinus Torvalds 		}
14561da177e4SLinus Torvalds 		/* Send Tag id */
14579c2d2670SHannes Reinecke 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SIMPLE_QUEUE_TAG);
14581da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
14591da177e4SLinus Torvalds 		dcb->tag_mask |= tag_mask;
14601da177e4SLinus Torvalds 		srb->tag_number = tag_number;
14611da177e4SLinus Torvalds 		scsicommand = SCMD_SEL_ATN3;
14621da177e4SLinus Torvalds 		srb->state = SRB_START_;
14631da177e4SLinus Torvalds 	}
14641da177e4SLinus Torvalds #endif
14651da177e4SLinus Torvalds /*polling:*/
14661da177e4SLinus Torvalds 	/* Send CDB ..command block ......... */
14675cd049a5SChristoph Hellwig 	dprintkdbg(DBG_KG, "start_scsi: (0x%p) <%02i-%i> cmnd=0x%02x tag=%i\n",
14689cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun,
14691da177e4SLinus Torvalds 		srb->cmd->cmnd[0], srb->tag_number);
14701da177e4SLinus Torvalds 	if (srb->flag & AUTO_REQSENSE) {
14711da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
14721da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
14731da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
14741da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1475b80ca4f7SFUJITA Tomonori 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
14761da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
14771da177e4SLinus Torvalds 	} else {
14781da177e4SLinus Torvalds 		ptr = (u8 *)srb->cmd->cmnd;
14791da177e4SLinus Torvalds 		for (i = 0; i < srb->cmd->cmd_len; i++)
14801da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
14811da177e4SLinus Torvalds 	}
14821da177e4SLinus Torvalds       no_cmd:
14831da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
14841da177e4SLinus Torvalds 		       DO_HWRESELECT | DO_DATALATCH);
14851da177e4SLinus Torvalds 	if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
14861da177e4SLinus Torvalds 		/*
14871da177e4SLinus Torvalds 		 * If start_scsi return 1:
14881da177e4SLinus Torvalds 		 * we caught an interrupt (must be reset or reselection ... )
14891da177e4SLinus Torvalds 		 * : Let's process it first!
14901da177e4SLinus Torvalds 		 */
14915cd049a5SChristoph Hellwig 		dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> Failed - busy\n",
14925cd049a5SChristoph Hellwig 			srb->cmd, dcb->target_id, dcb->target_lun);
14931da177e4SLinus Torvalds 		srb->state = SRB_READY;
14941da177e4SLinus Torvalds 		free_tag(dcb, srb);
14951da177e4SLinus Torvalds 		srb->msg_count = 0;
14961da177e4SLinus Torvalds 		return_code = 1;
14971da177e4SLinus Torvalds 		/* This IRQ should NOT get lost, as we did not acknowledge it */
14981da177e4SLinus Torvalds 	} else {
14991da177e4SLinus Torvalds 		/*
15001da177e4SLinus Torvalds 		 * If start_scsi returns 0:
15011da177e4SLinus Torvalds 		 * we know that the SCSI processor is free
15021da177e4SLinus Torvalds 		 */
15031da177e4SLinus Torvalds 		srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
15041da177e4SLinus Torvalds 		dcb->active_srb = srb;
15051da177e4SLinus Torvalds 		acb->active_dcb = dcb;
15061da177e4SLinus Torvalds 		return_code = 0;
15071da177e4SLinus Torvalds 		/* it's important for atn stop */
15081da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
15091da177e4SLinus Torvalds 			       DO_DATALATCH | DO_HWRESELECT);
15101da177e4SLinus Torvalds 		/* SCSI command */
15111da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
15121da177e4SLinus Torvalds 	}
15131da177e4SLinus Torvalds 	return return_code;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds #define DC395x_ENABLE_MSGOUT \
15181da177e4SLinus Torvalds  DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
15191da177e4SLinus Torvalds  srb->state |= SRB_MSGOUT
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds /* abort command */
15231da177e4SLinus Torvalds static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
15241da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
15251da177e4SLinus Torvalds {
15261da177e4SLinus Torvalds 	srb->msgout_buf[0] = ABORT;
15271da177e4SLinus Torvalds 	srb->msg_count = 1;
15281da177e4SLinus Torvalds 	DC395x_ENABLE_MSGOUT;
15291da177e4SLinus Torvalds 	srb->state &= ~SRB_MSGIN;
15301da177e4SLinus Torvalds 	srb->state |= SRB_MSGOUT;
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds /**
15351da177e4SLinus Torvalds  * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
15361da177e4SLinus Torvalds  *                           have been triggered for this card.
15371da177e4SLinus Torvalds  *
15381da177e4SLinus Torvalds  * @acb:	 a pointer to the adpter control block
15391da177e4SLinus Torvalds  * @scsi_status: the status return when we checked the card
15401da177e4SLinus Torvalds  **/
15411da177e4SLinus Torvalds static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
15421da177e4SLinus Torvalds 		u16 scsi_status)
15431da177e4SLinus Torvalds {
15441da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
15451da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
15461da177e4SLinus Torvalds 	u16 phase;
15471da177e4SLinus Torvalds 	u8 scsi_intstatus;
15481da177e4SLinus Torvalds 	unsigned long flags;
15491da177e4SLinus Torvalds 	void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
15501da177e4SLinus Torvalds 			      u16 *);
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 	/* This acknowledges the IRQ */
15551da177e4SLinus Torvalds 	scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
15561da177e4SLinus Torvalds 	if ((scsi_status & 0x2007) == 0x2002)
15571da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
15581da177e4SLinus Torvalds 			"COP after COP completed? %04x\n", scsi_status);
15591da177e4SLinus Torvalds 	if (debug_enabled(DBG_KG)) {
15601da177e4SLinus Torvalds 		if (scsi_intstatus & INT_SELTIMEOUT)
15611da177e4SLinus Torvalds 			dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
15621da177e4SLinus Torvalds 	}
15631da177e4SLinus Torvalds 	/*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	if (timer_pending(&acb->selto_timer))
15661da177e4SLinus Torvalds 		del_timer(&acb->selto_timer);
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds 	if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
15691da177e4SLinus Torvalds 		disconnect(acb);	/* bus free interrupt  */
15701da177e4SLinus Torvalds 		goto out_unlock;
15711da177e4SLinus Torvalds 	}
15721da177e4SLinus Torvalds 	if (scsi_intstatus & INT_RESELECTED) {
15731da177e4SLinus Torvalds 		reselect(acb);
15741da177e4SLinus Torvalds 		goto out_unlock;
15751da177e4SLinus Torvalds 	}
15761da177e4SLinus Torvalds 	if (scsi_intstatus & INT_SELECT) {
15771da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Host does not support target mode!\n");
15781da177e4SLinus Torvalds 		goto out_unlock;
15791da177e4SLinus Torvalds 	}
15801da177e4SLinus Torvalds 	if (scsi_intstatus & INT_SCSIRESET) {
15811da177e4SLinus Torvalds 		scsi_reset_detect(acb);
15821da177e4SLinus Torvalds 		goto out_unlock;
15831da177e4SLinus Torvalds 	}
15841da177e4SLinus Torvalds 	if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
15851da177e4SLinus Torvalds 		dcb = acb->active_dcb;
15861da177e4SLinus Torvalds 		if (!dcb) {
15871da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
15881da177e4SLinus Torvalds 				"Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
15891da177e4SLinus Torvalds 				scsi_status, scsi_intstatus);
15901da177e4SLinus Torvalds 			goto out_unlock;
15911da177e4SLinus Torvalds 		}
15921da177e4SLinus Torvalds 		srb = dcb->active_srb;
15931da177e4SLinus Torvalds 		if (dcb->flag & ABORT_DEV_) {
15941da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
15951da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
15961da177e4SLinus Torvalds 		}
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 		/* software sequential machine */
15991da177e4SLinus Torvalds 		phase = (u16)srb->scsi_phase;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 		/*
16021da177e4SLinus Torvalds 		 * 62037 or 62137
16031da177e4SLinus Torvalds 		 * call  dc395x_scsi_phase0[]... "phase entry"
16041da177e4SLinus Torvalds 		 * handle every phase before start transfer
16051da177e4SLinus Torvalds 		 */
16061da177e4SLinus Torvalds 		/* data_out_phase0,	phase:0 */
16071da177e4SLinus Torvalds 		/* data_in_phase0,	phase:1 */
16081da177e4SLinus Torvalds 		/* command_phase0,	phase:2 */
16091da177e4SLinus Torvalds 		/* status_phase0,	phase:3 */
16101da177e4SLinus Torvalds 		/* nop0,		phase:4 PH_BUS_FREE .. initial phase */
16111da177e4SLinus Torvalds 		/* nop0,		phase:5 PH_BUS_FREE .. initial phase */
16121da177e4SLinus Torvalds 		/* msgout_phase0,	phase:6 */
16131da177e4SLinus Torvalds 		/* msgin_phase0,	phase:7 */
16141da177e4SLinus Torvalds 		dc395x_statev = dc395x_scsi_phase0[phase];
16151da177e4SLinus Torvalds 		dc395x_statev(acb, srb, &scsi_status);
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 		/*
161825985edcSLucas De Marchi 		 * if there were any exception occurred scsi_status
16191da177e4SLinus Torvalds 		 * will be modify to bus free phase new scsi_status
16201da177e4SLinus Torvalds 		 * transfer out from ... previous dc395x_statev
16211da177e4SLinus Torvalds 		 */
16221da177e4SLinus Torvalds 		srb->scsi_phase = scsi_status & PHASEMASK;
16231da177e4SLinus Torvalds 		phase = (u16)scsi_status & PHASEMASK;
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 		/*
16261da177e4SLinus Torvalds 		 * call  dc395x_scsi_phase1[]... "phase entry" handle
16271da177e4SLinus Torvalds 		 * every phase to do transfer
16281da177e4SLinus Torvalds 		 */
16291da177e4SLinus Torvalds 		/* data_out_phase1,	phase:0 */
16301da177e4SLinus Torvalds 		/* data_in_phase1,	phase:1 */
16311da177e4SLinus Torvalds 		/* command_phase1,	phase:2 */
16321da177e4SLinus Torvalds 		/* status_phase1,	phase:3 */
16331da177e4SLinus Torvalds 		/* nop1,		phase:4 PH_BUS_FREE .. initial phase */
16341da177e4SLinus Torvalds 		/* nop1,		phase:5 PH_BUS_FREE .. initial phase */
16351da177e4SLinus Torvalds 		/* msgout_phase1,	phase:6 */
16361da177e4SLinus Torvalds 		/* msgin_phase1,	phase:7 */
16371da177e4SLinus Torvalds 		dc395x_statev = dc395x_scsi_phase1[phase];
16381da177e4SLinus Torvalds 		dc395x_statev(acb, srb, &scsi_status);
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds       out_unlock:
16411da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
16421da177e4SLinus Torvalds }
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 
16457d12e780SDavid Howells static irqreturn_t dc395x_interrupt(int irq, void *dev_id)
16461da177e4SLinus Torvalds {
1647c7bec5abSJeff Garzik 	struct AdapterCtlBlk *acb = dev_id;
16481da177e4SLinus Torvalds 	u16 scsi_status;
16491da177e4SLinus Torvalds 	u8 dma_status;
16501da177e4SLinus Torvalds 	irqreturn_t handled = IRQ_NONE;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	/*
16533a4fa0a2SRobert P. J. Day 	 * Check for pending interrupt
16541da177e4SLinus Torvalds 	 */
16551da177e4SLinus Torvalds 	scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
16561da177e4SLinus Torvalds 	dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
16571da177e4SLinus Torvalds 	if (scsi_status & SCSIINTERRUPT) {
16583a4fa0a2SRobert P. J. Day 		/* interrupt pending - let's process it! */
16591da177e4SLinus Torvalds 		dc395x_handle_interrupt(acb, scsi_status);
16601da177e4SLinus Torvalds 		handled = IRQ_HANDLED;
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds 	else if (dma_status & 0x20) {
16631da177e4SLinus Torvalds 		/* Error from the DMA engine */
16641da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
16651da177e4SLinus Torvalds #if 0
16661da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
16671da177e4SLinus Torvalds 		if (acb->active_dcb) {
16681da177e4SLinus Torvalds 			acb->active_dcb-> flag |= ABORT_DEV_;
16691da177e4SLinus Torvalds 			if (acb->active_dcb->active_srb)
16701da177e4SLinus Torvalds 				enable_msgout_abort(acb, acb->active_dcb->active_srb);
16711da177e4SLinus Torvalds 		}
16721da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
16731da177e4SLinus Torvalds #else
16741da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
16751da177e4SLinus Torvalds 		acb = NULL;
16761da177e4SLinus Torvalds #endif
16771da177e4SLinus Torvalds 		handled = IRQ_HANDLED;
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 	return handled;
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
16851da177e4SLinus Torvalds 		u16 *pscsi_status)
16861da177e4SLinus Torvalds {
16875cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgout_phase0: (0x%p)\n", srb->cmd);
16881da177e4SLinus Torvalds 	if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
16891da177e4SLinus Torvalds 		*pscsi_status = PH_BUS_FREE;	/*.. initial phase */
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
16921da177e4SLinus Torvalds 	srb->state &= ~SRB_MSGOUT;
16931da177e4SLinus Torvalds }
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
16971da177e4SLinus Torvalds 		u16 *pscsi_status)
16981da177e4SLinus Torvalds {
16991da177e4SLinus Torvalds 	u16 i;
17001da177e4SLinus Torvalds 	u8 *ptr;
17015cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgout_phase1: (0x%p)\n", srb->cmd);
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	clear_fifo(acb, "msgout_phase1");
17041da177e4SLinus Torvalds 	if (!(srb->state & SRB_MSGOUT)) {
17051da177e4SLinus Torvalds 		srb->state |= SRB_MSGOUT;
17061da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
17075cd049a5SChristoph Hellwig 			"msgout_phase1: (0x%p) Phase unexpected\n",
17085cd049a5SChristoph Hellwig 			srb->cmd);	/* So what ? */
17091da177e4SLinus Torvalds 	}
17101da177e4SLinus Torvalds 	if (!srb->msg_count) {
17115cd049a5SChristoph Hellwig 		dprintkdbg(DBG_0, "msgout_phase1: (0x%p) NOP msg\n",
17125cd049a5SChristoph Hellwig 			srb->cmd);
17139c2d2670SHannes Reinecke 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, NOP);
17149c2d2670SHannes Reinecke 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
17159c2d2670SHannes Reinecke 		/* it's important for atn stop */
17161da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
17171da177e4SLinus Torvalds 		return;
17181da177e4SLinus Torvalds 	}
17191da177e4SLinus Torvalds 	ptr = (u8 *)srb->msgout_buf;
17201da177e4SLinus Torvalds 	for (i = 0; i < srb->msg_count; i++)
17211da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
17221da177e4SLinus Torvalds 	srb->msg_count = 0;
17239c2d2670SHannes Reinecke 	if (srb->msgout_buf[0] == ABORT_TASK_SET)
17241da177e4SLinus Torvalds 		srb->state = SRB_ABORT_SENT;
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
17271da177e4SLinus Torvalds }
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
17311da177e4SLinus Torvalds 		u16 *pscsi_status)
17321da177e4SLinus Torvalds {
17335cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "command_phase0: (0x%p)\n", srb->cmd);
17341da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
17351da177e4SLinus Torvalds }
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
17391da177e4SLinus Torvalds 		u16 *pscsi_status)
17401da177e4SLinus Torvalds {
17411da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
17421da177e4SLinus Torvalds 	u8 *ptr;
17431da177e4SLinus Torvalds 	u16 i;
17445cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "command_phase1: (0x%p)\n", srb->cmd);
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 	clear_fifo(acb, "command_phase1");
17471da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
17481da177e4SLinus Torvalds 	if (!(srb->flag & AUTO_REQSENSE)) {
17491da177e4SLinus Torvalds 		ptr = (u8 *)srb->cmd->cmnd;
17501da177e4SLinus Torvalds 		for (i = 0; i < srb->cmd->cmd_len; i++) {
17511da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
17521da177e4SLinus Torvalds 			ptr++;
17531da177e4SLinus Torvalds 		}
17541da177e4SLinus Torvalds 	} else {
17551da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
17561da177e4SLinus Torvalds 		dcb = acb->active_dcb;
17571da177e4SLinus Torvalds 		/* target id */
17581da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
17591da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
17601da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1761b80ca4f7SFUJITA Tomonori 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
17621da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
17631da177e4SLinus Torvalds 	}
17641da177e4SLinus Torvalds 	srb->state |= SRB_COMMAND;
17651da177e4SLinus Torvalds 	/* it's important for atn stop */
17661da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
17671da177e4SLinus Torvalds 	/* SCSI command */
17681da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds /*
17731da177e4SLinus Torvalds  * Verify that the remaining space in the hw sg lists is the same as
17741da177e4SLinus Torvalds  * the count of remaining bytes in srb->total_xfer_length
17751da177e4SLinus Torvalds  */
17761da177e4SLinus Torvalds static void sg_verify_length(struct ScsiReqBlk *srb)
17771da177e4SLinus Torvalds {
17781da177e4SLinus Torvalds 	if (debug_enabled(DBG_SG)) {
17791da177e4SLinus Torvalds 		unsigned len = 0;
17801da177e4SLinus Torvalds 		unsigned idx = srb->sg_index;
17811da177e4SLinus Torvalds 		struct SGentry *psge = srb->segment_x + idx;
17821da177e4SLinus Torvalds 		for (; idx < srb->sg_count; psge++, idx++)
17831da177e4SLinus Torvalds 			len += psge->length;
17841da177e4SLinus Torvalds 		if (len != srb->total_xfer_length)
17851da177e4SLinus Torvalds 			dprintkdbg(DBG_SG,
17861da177e4SLinus Torvalds 			       "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
17871da177e4SLinus Torvalds 			       srb->total_xfer_length, len);
17881da177e4SLinus Torvalds 	}
17891da177e4SLinus Torvalds }
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds /*
17931da177e4SLinus Torvalds  * Compute the next Scatter Gather list index and adjust its length
1794cdb8c2a6SGuennadi Liakhovetski  * and address if necessary
17951da177e4SLinus Torvalds  */
17961da177e4SLinus Torvalds static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
17971da177e4SLinus Torvalds {
17981da177e4SLinus Torvalds 	u8 idx;
179925985edcSLucas De Marchi 	u32 xferred = srb->total_xfer_length - left; /* bytes transferred */
18001da177e4SLinus Torvalds 	struct SGentry *psge = srb->segment_x + srb->sg_index;
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
180325985edcSLucas De Marchi 		"sg_update_list: Transferred %i of %i bytes, %i remain\n",
18041da177e4SLinus Torvalds 		xferred, srb->total_xfer_length, left);
18051da177e4SLinus Torvalds 	if (xferred == 0) {
18061da177e4SLinus Torvalds 		/* nothing to update since we did not transfer any data */
18071da177e4SLinus Torvalds 		return;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	sg_verify_length(srb);
18111da177e4SLinus Torvalds 	srb->total_xfer_length = left;	/* update remaining count */
18121da177e4SLinus Torvalds 	for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
18131da177e4SLinus Torvalds 		if (xferred >= psge->length) {
18141da177e4SLinus Torvalds 			/* Complete SG entries done */
18151da177e4SLinus Torvalds 			xferred -= psge->length;
18161da177e4SLinus Torvalds 		} else {
18171da177e4SLinus Torvalds 			/* Partial SG entry done */
1818dfda5e21SChristoph Hellwig 			dma_sync_single_for_cpu(&srb->dcb->acb->dev->dev,
1819dfda5e21SChristoph Hellwig 					srb->sg_bus_addr, SEGMENTX_LEN,
1820dfda5e21SChristoph Hellwig 					DMA_TO_DEVICE);
18211da177e4SLinus Torvalds 			psge->length -= xferred;
18221da177e4SLinus Torvalds 			psge->address += xferred;
18231da177e4SLinus Torvalds 			srb->sg_index = idx;
1824dfda5e21SChristoph Hellwig 			dma_sync_single_for_device(&srb->dcb->acb->dev->dev,
1825dfda5e21SChristoph Hellwig 					srb->sg_bus_addr, SEGMENTX_LEN,
1826dfda5e21SChristoph Hellwig 					DMA_TO_DEVICE);
18271da177e4SLinus Torvalds 			break;
18281da177e4SLinus Torvalds 		}
18291da177e4SLinus Torvalds 		psge++;
18301da177e4SLinus Torvalds 	}
18311da177e4SLinus Torvalds 	sg_verify_length(srb);
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds /*
183625985edcSLucas De Marchi  * We have transferred a single byte (PIO mode?) and need to update
18371da177e4SLinus Torvalds  * the count of bytes remaining (total_xfer_length) and update the sg
18381da177e4SLinus Torvalds  * entry to either point to next byte in the current sg entry, or of
18391da177e4SLinus Torvalds  * already at the end to point to the start of the next sg entry
18401da177e4SLinus Torvalds  */
18411da177e4SLinus Torvalds static void sg_subtract_one(struct ScsiReqBlk *srb)
18421da177e4SLinus Torvalds {
1843cdb8c2a6SGuennadi Liakhovetski 	sg_update_list(srb, srb->total_xfer_length - 1);
18441da177e4SLinus Torvalds }
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds /*
18481da177e4SLinus Torvalds  * cleanup_after_transfer
18491da177e4SLinus Torvalds  *
18501da177e4SLinus Torvalds  * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
18511da177e4SLinus Torvalds  * KG: Currently called from  StatusPhase1 ()
18521da177e4SLinus Torvalds  * Should probably also be called from other places
18531da177e4SLinus Torvalds  * Best might be to call it in DataXXPhase0, if new phase will differ
18541da177e4SLinus Torvalds  */
18551da177e4SLinus Torvalds static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
18561da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
18571da177e4SLinus Torvalds {
18581da177e4SLinus Torvalds 	/*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
18591da177e4SLinus Torvalds 	if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) {	/* read */
18601da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
18611da177e4SLinus Torvalds 			clear_fifo(acb, "cleanup/in");
18621da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
18631da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
18641da177e4SLinus Torvalds 	} else {		/* write */
18651da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
18661da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
18671da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
18681da177e4SLinus Torvalds 			clear_fifo(acb, "cleanup/out");
18691da177e4SLinus Torvalds 	}
18701da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
18711da177e4SLinus Torvalds }
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds /*
187525985edcSLucas De Marchi  * Those no of bytes will be transferred w/ PIO through the SCSI FIFO
18761da177e4SLinus Torvalds  * Seems to be needed for unknown reasons; could be a hardware bug :-(
18771da177e4SLinus Torvalds  */
18781da177e4SLinus Torvalds #define DC395x_LASTPIO 4
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
18821da177e4SLinus Torvalds 		u16 *pscsi_status)
18831da177e4SLinus Torvalds {
18841da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
18851da177e4SLinus Torvalds 	u16 scsi_status = *pscsi_status;
18861da177e4SLinus Torvalds 	u32 d_left_counter = 0;
18875cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_out_phase0: (0x%p) <%02i-%i>\n",
18889cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 	/*
18911da177e4SLinus Torvalds 	 * KG: We need to drain the buffers before we draw any conclusions!
18921da177e4SLinus Torvalds 	 * This means telling the DMA to push the rest into SCSI, telling
18931da177e4SLinus Torvalds 	 * SCSI to push the rest to the bus.
18941da177e4SLinus Torvalds 	 * However, the device might have been the one to stop us (phase
18951da177e4SLinus Torvalds 	 * change), and the data in transit just needs to be accounted so
18961da177e4SLinus Torvalds 	 * it can be retransmitted.)
18971da177e4SLinus Torvalds 	 */
18981da177e4SLinus Torvalds 	/*
18991da177e4SLinus Torvalds 	 * KG: Stop DMA engine pushing more data into the SCSI FIFO
19001da177e4SLinus Torvalds 	 * If we need more data, the DMA SG list will be freshly set up, anyway
19011da177e4SLinus Torvalds 	 */
19021da177e4SLinus Torvalds 	dprintkdbg(DBG_PIO, "data_out_phase0: "
1903cdb8c2a6SGuennadi Liakhovetski 		"DMA{fifocnt=0x%02x fifostat=0x%02x} "
19041da177e4SLinus Torvalds 		"SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
19051da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
19061da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
19071da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
19081da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
19091da177e4SLinus Torvalds 		srb->total_xfer_length);
19101da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	if (!(srb->state & SRB_XFERPAD)) {
19131da177e4SLinus Torvalds 		if (scsi_status & PARITYERROR)
19141da177e4SLinus Torvalds 			srb->status |= PARITY_ERROR;
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 		/*
19171da177e4SLinus Torvalds 		 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
19181da177e4SLinus Torvalds 		 * is the no of bytes it got from the DMA engine not the no it
19191da177e4SLinus Torvalds 		 * transferred successfully to the device. (And the difference could
19201da177e4SLinus Torvalds 		 * be as much as the FIFO size, I guess ...)
19211da177e4SLinus Torvalds 		 */
19221da177e4SLinus Torvalds 		if (!(scsi_status & SCSIXFERDONE)) {
19231da177e4SLinus Torvalds 			/*
19241da177e4SLinus Torvalds 			 * when data transfer from DMA FIFO to SCSI FIFO
19251da177e4SLinus Torvalds 			 * if there was some data left in SCSI FIFO
19261da177e4SLinus Torvalds 			 */
19271da177e4SLinus Torvalds 			d_left_counter =
19281da177e4SLinus Torvalds 			    (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
19291da177e4SLinus Torvalds 				  0x1F);
19301da177e4SLinus Torvalds 			if (dcb->sync_period & WIDE_SYNC)
19311da177e4SLinus Torvalds 				d_left_counter <<= 1;
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 			dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
19341da177e4SLinus Torvalds 				"SCSI{fifocnt=0x%02x cnt=0x%08x} "
19351da177e4SLinus Torvalds 				"DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
19361da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
19371da177e4SLinus Torvalds 				(dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
19381da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
19391da177e4SLinus Torvalds 				DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
19401da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
19411da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
19421da177e4SLinus Torvalds 				DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
19431da177e4SLinus Torvalds 		}
19441da177e4SLinus Torvalds 		/*
1945b4b08e58SLinus Torvalds 		 * calculate all the residue data that not yet tranfered
19461da177e4SLinus Torvalds 		 * SCSI transfer counter + left in SCSI FIFO data
19471da177e4SLinus Torvalds 		 *
19481da177e4SLinus Torvalds 		 * .....TRM_S1040_SCSI_COUNTER (24bits)
19491da177e4SLinus Torvalds 		 * The counter always decrement by one for every SCSI byte transfer.
19501da177e4SLinus Torvalds 		 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
19511da177e4SLinus Torvalds 		 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
19521da177e4SLinus Torvalds 		 */
19531da177e4SLinus Torvalds 		if (srb->total_xfer_length > DC395x_LASTPIO)
19541da177e4SLinus Torvalds 			d_left_counter +=
19551da177e4SLinus Torvalds 			    DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds 		/* Is this a good idea? */
19581da177e4SLinus Torvalds 		/*clear_fifo(acb, "DOP1"); */
19591da177e4SLinus Torvalds 		/* KG: What is this supposed to be useful for? WIDE padding stuff? */
19601da177e4SLinus Torvalds 		if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
1961a862ea31SFUJITA Tomonori 		    && scsi_bufflen(srb->cmd) % 2) {
19621da177e4SLinus Torvalds 			d_left_counter = 0;
19631da177e4SLinus Torvalds 			dprintkl(KERN_INFO,
19641da177e4SLinus Torvalds 				"data_out_phase0: Discard 1 byte (0x%02x)\n",
19651da177e4SLinus Torvalds 				scsi_status);
19661da177e4SLinus Torvalds 		}
19671da177e4SLinus Torvalds 		/*
19681da177e4SLinus Torvalds 		 * KG: Oops again. Same thinko as above: The SCSI might have been
19691da177e4SLinus Torvalds 		 * faster than the DMA engine, so that it ran out of data.
19701da177e4SLinus Torvalds 		 * In that case, we have to do just nothing!
19711da177e4SLinus Torvalds 		 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
19721da177e4SLinus Torvalds 		 */
19731da177e4SLinus Torvalds 		/*
19741da177e4SLinus Torvalds 		 * KG: This is nonsense: We have been WRITING data to the bus
19751da177e4SLinus Torvalds 		 * If the SCSI engine has no bytes left, how should the DMA engine?
19761da177e4SLinus Torvalds 		 */
19771da177e4SLinus Torvalds 		if (d_left_counter == 0) {
19781da177e4SLinus Torvalds 			srb->total_xfer_length = 0;
19791da177e4SLinus Torvalds 		} else {
19801da177e4SLinus Torvalds 			/*
19811da177e4SLinus Torvalds 			 * if transfer not yet complete
19821da177e4SLinus Torvalds 			 * there were some data residue in SCSI FIFO or
19831da177e4SLinus Torvalds 			 * SCSI transfer counter not empty
19841da177e4SLinus Torvalds 			 */
19851da177e4SLinus Torvalds 			long oldxferred =
19861da177e4SLinus Torvalds 			    srb->total_xfer_length - d_left_counter;
19871da177e4SLinus Torvalds 			const int diff =
19881da177e4SLinus Torvalds 			    (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
19891da177e4SLinus Torvalds 			sg_update_list(srb, d_left_counter);
19901da177e4SLinus Torvalds 			/* KG: Most ugly hack! Apparently, this works around a chip bug */
19911da177e4SLinus Torvalds 			if ((srb->segment_x[srb->sg_index].length ==
1992a862ea31SFUJITA Tomonori 			     diff && scsi_sg_count(srb->cmd))
19931da177e4SLinus Torvalds 			    || ((oldxferred & ~PAGE_MASK) ==
19941da177e4SLinus Torvalds 				(PAGE_SIZE - diff))
19951da177e4SLinus Torvalds 			    ) {
19961da177e4SLinus Torvalds 				dprintkl(KERN_INFO, "data_out_phase0: "
19971da177e4SLinus Torvalds 					"Work around chip bug (%i)?\n", diff);
19981da177e4SLinus Torvalds 				d_left_counter =
19991da177e4SLinus Torvalds 				    srb->total_xfer_length - diff;
20001da177e4SLinus Torvalds 				sg_update_list(srb, d_left_counter);
20011da177e4SLinus Torvalds 				/*srb->total_xfer_length -= diff; */
20021da177e4SLinus Torvalds 				/*srb->virt_addr += diff; */
20031da177e4SLinus Torvalds 				/*if (srb->cmd->use_sg) */
20041da177e4SLinus Torvalds 				/*      srb->sg_index++; */
20051da177e4SLinus Torvalds 			}
20061da177e4SLinus Torvalds 		}
20071da177e4SLinus Torvalds 	}
20081da177e4SLinus Torvalds 	if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
20091da177e4SLinus Torvalds 		cleanup_after_transfer(acb, srb);
20101da177e4SLinus Torvalds 	}
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
20151da177e4SLinus Torvalds 		u16 *pscsi_status)
20161da177e4SLinus Torvalds {
20175cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_out_phase1: (0x%p) <%02i-%i>\n",
20189cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
20191da177e4SLinus Torvalds 	clear_fifo(acb, "data_out_phase1");
20201da177e4SLinus Torvalds 	/* do prepare before transfer when data out phase */
20211da177e4SLinus Torvalds 	data_io_transfer(acb, srb, XFERDATAOUT);
20221da177e4SLinus Torvalds }
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
20251da177e4SLinus Torvalds 		u16 *pscsi_status)
20261da177e4SLinus Torvalds {
20271da177e4SLinus Torvalds 	u16 scsi_status = *pscsi_status;
2028cdb8c2a6SGuennadi Liakhovetski 
20295cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_in_phase0: (0x%p) <%02i-%i>\n",
20309cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	/*
20331da177e4SLinus Torvalds 	 * KG: DataIn is much more tricky than DataOut. When the device is finished
20341da177e4SLinus Torvalds 	 * and switches to another phase, the SCSI engine should be finished too.
20351da177e4SLinus Torvalds 	 * But: There might still be bytes left in its FIFO to be fetched by the DMA
20361da177e4SLinus Torvalds 	 * engine and transferred to memory.
20371da177e4SLinus Torvalds 	 * We should wait for the FIFOs to be emptied by that (is there any way to
20381da177e4SLinus Torvalds 	 * enforce this?) and then stop the DMA engine, because it might think, that
20391da177e4SLinus Torvalds 	 * there are more bytes to follow. Yes, the device might disconnect prior to
20401da177e4SLinus Torvalds 	 * having all bytes transferred!
20411da177e4SLinus Torvalds 	 * Also we should make sure that all data from the DMA engine buffer's really
20421da177e4SLinus Torvalds 	 * made its way to the system memory! Some documentation on this would not
20431da177e4SLinus Torvalds 	 * seem to be a bad idea, actually.
20441da177e4SLinus Torvalds 	 */
20451da177e4SLinus Torvalds 	if (!(srb->state & SRB_XFERPAD)) {
2046cdb8c2a6SGuennadi Liakhovetski 		u32 d_left_counter;
2047cdb8c2a6SGuennadi Liakhovetski 		unsigned int sc, fc;
2048cdb8c2a6SGuennadi Liakhovetski 
20491da177e4SLinus Torvalds 		if (scsi_status & PARITYERROR) {
20505cd049a5SChristoph Hellwig 			dprintkl(KERN_INFO, "data_in_phase0: (0x%p) "
20515cd049a5SChristoph Hellwig 				"Parity Error\n", srb->cmd);
20521da177e4SLinus Torvalds 			srb->status |= PARITY_ERROR;
20531da177e4SLinus Torvalds 		}
20541da177e4SLinus Torvalds 		/*
20551da177e4SLinus Torvalds 		 * KG: We should wait for the DMA FIFO to be empty ...
20561da177e4SLinus Torvalds 		 * but: it would be better to wait first for the SCSI FIFO and then the
20571da177e4SLinus Torvalds 		 * the DMA FIFO to become empty? How do we know, that the device not already
20581da177e4SLinus Torvalds 		 * sent data to the FIFO in a MsgIn phase, eg.?
20591da177e4SLinus Torvalds 		 */
20601da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
20611da177e4SLinus Torvalds #if 0
20621da177e4SLinus Torvalds 			int ctr = 6000000;
20631da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
20641da177e4SLinus Torvalds 				"DIP0: Wait for DMA FIFO to flush ...\n");
20651da177e4SLinus Torvalds 			/*DC395x_write8  (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
20661da177e4SLinus Torvalds 			/*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
20671da177e4SLinus Torvalds 			/*DC395x_write8  (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
20681da177e4SLinus Torvalds 			while (!
20691da177e4SLinus Torvalds 			       (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
20701da177e4SLinus Torvalds 				0x80) && --ctr);
20711da177e4SLinus Torvalds 			if (ctr < 6000000 - 1)
20721da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG
20731da177e4SLinus Torvalds 				       "DIP0: Had to wait for DMA ...\n");
20741da177e4SLinus Torvalds 			if (!ctr)
20751da177e4SLinus Torvalds 				dprintkl(KERN_ERR,
20761da177e4SLinus Torvalds 				       "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
20771da177e4SLinus Torvalds 			/*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
20781da177e4SLinus Torvalds #endif
20791da177e4SLinus Torvalds 			dprintkdbg(DBG_KG, "data_in_phase0: "
20801da177e4SLinus Torvalds 				"DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
20811da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
20821da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
20831da177e4SLinus Torvalds 		}
20841da177e4SLinus Torvalds 		/* Now: Check remainig data: The SCSI counters should tell us ... */
2085cdb8c2a6SGuennadi Liakhovetski 		sc = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2086cdb8c2a6SGuennadi Liakhovetski 		fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2087cdb8c2a6SGuennadi Liakhovetski 		d_left_counter = sc + ((fc & 0x1f)
20881da177e4SLinus Torvalds 		       << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
20891da177e4SLinus Torvalds 			   0));
20901da177e4SLinus Torvalds 		dprintkdbg(DBG_KG, "data_in_phase0: "
20911da177e4SLinus Torvalds 			"SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
20921da177e4SLinus Torvalds 			"DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
20931da177e4SLinus Torvalds 			"Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2094cdb8c2a6SGuennadi Liakhovetski 			fc,
20951da177e4SLinus Torvalds 			(srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2096cdb8c2a6SGuennadi Liakhovetski 			sc,
2097cdb8c2a6SGuennadi Liakhovetski 			fc,
20981da177e4SLinus Torvalds 			DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
20991da177e4SLinus Torvalds 			DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
21001da177e4SLinus Torvalds 			srb->total_xfer_length, d_left_counter);
21011da177e4SLinus Torvalds #if DC395x_LASTPIO
210225985edcSLucas De Marchi 		/* KG: Less than or equal to 4 bytes can not be transferred via DMA, it seems. */
21031da177e4SLinus Torvalds 		if (d_left_counter
21041da177e4SLinus Torvalds 		    && srb->total_xfer_length <= DC395x_LASTPIO) {
2105cdb8c2a6SGuennadi Liakhovetski 			size_t left_io = srb->total_xfer_length;
2106cdb8c2a6SGuennadi Liakhovetski 
21071da177e4SLinus Torvalds 			/*u32 addr = (srb->segment_x[srb->sg_index].address); */
21081da177e4SLinus Torvalds 			/*sg_update_list (srb, d_left_counter); */
2109cdb8c2a6SGuennadi Liakhovetski 			dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) "
2110cdb8c2a6SGuennadi Liakhovetski 				   "for remaining %i bytes:",
2111cdb8c2a6SGuennadi Liakhovetski 				fc & 0x1f,
21121da177e4SLinus Torvalds 				(srb->dcb->sync_period & WIDE_SYNC) ?
21131da177e4SLinus Torvalds 				    "words" : "bytes",
21141da177e4SLinus Torvalds 				srb->total_xfer_length);
21151da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC)
21161da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
21171da177e4SLinus Torvalds 					      CFG2_WIDEFIFO);
2118cdb8c2a6SGuennadi Liakhovetski 			while (left_io) {
2119cdb8c2a6SGuennadi Liakhovetski 				unsigned char *virt, *base = NULL;
2120cdb8c2a6SGuennadi Liakhovetski 				unsigned long flags = 0;
2121cdb8c2a6SGuennadi Liakhovetski 				size_t len = left_io;
2122cdb8c2a6SGuennadi Liakhovetski 				size_t offset = srb->request_length - left_io;
2123a862ea31SFUJITA Tomonori 
2124cdb8c2a6SGuennadi Liakhovetski 				local_irq_save(flags);
2125cdb8c2a6SGuennadi Liakhovetski 				/* Assumption: it's inside one page as it's at most 4 bytes and
2126cdb8c2a6SGuennadi Liakhovetski 				   I just assume it's on a 4-byte boundary */
2127a862ea31SFUJITA Tomonori 				base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2128cdb8c2a6SGuennadi Liakhovetski 							   srb->sg_count, &offset, &len);
2129cdb8c2a6SGuennadi Liakhovetski 				virt = base + offset;
2130a862ea31SFUJITA Tomonori 
2131cdb8c2a6SGuennadi Liakhovetski 				left_io -= len;
2132cdb8c2a6SGuennadi Liakhovetski 
2133cdb8c2a6SGuennadi Liakhovetski 				while (len) {
2134cdb8c2a6SGuennadi Liakhovetski 					u8 byte;
2135cdb8c2a6SGuennadi Liakhovetski 					byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2136cdb8c2a6SGuennadi Liakhovetski 					*virt++ = byte;
2137cdb8c2a6SGuennadi Liakhovetski 
21381da177e4SLinus Torvalds 					if (debug_enabled(DBG_PIO))
21391da177e4SLinus Torvalds 						printk(" %02x", byte);
2140cdb8c2a6SGuennadi Liakhovetski 
21411da177e4SLinus Torvalds 					d_left_counter--;
21421da177e4SLinus Torvalds 					sg_subtract_one(srb);
2143cdb8c2a6SGuennadi Liakhovetski 
2144cdb8c2a6SGuennadi Liakhovetski 					len--;
2145cdb8c2a6SGuennadi Liakhovetski 
2146cdb8c2a6SGuennadi Liakhovetski 					fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2147cdb8c2a6SGuennadi Liakhovetski 
2148cdb8c2a6SGuennadi Liakhovetski 					if (fc == 0x40) {
2149cdb8c2a6SGuennadi Liakhovetski 						left_io = 0;
2150cdb8c2a6SGuennadi Liakhovetski 						break;
21511da177e4SLinus Torvalds 					}
2152cdb8c2a6SGuennadi Liakhovetski 				}
2153cdb8c2a6SGuennadi Liakhovetski 
2154cdb8c2a6SGuennadi Liakhovetski 				WARN_ON((fc != 0x40) == !d_left_counter);
2155cdb8c2a6SGuennadi Liakhovetski 
2156cdb8c2a6SGuennadi Liakhovetski 				if (fc == 0x40 && (srb->dcb->sync_period & WIDE_SYNC)) {
21571da177e4SLinus Torvalds 					/* Read the last byte ... */
21581da177e4SLinus Torvalds 					if (srb->total_xfer_length > 0) {
21591da177e4SLinus Torvalds 						u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2160cdb8c2a6SGuennadi Liakhovetski 
2161cdb8c2a6SGuennadi Liakhovetski 						*virt++ = byte;
21621da177e4SLinus Torvalds 						srb->total_xfer_length--;
21631da177e4SLinus Torvalds 						if (debug_enabled(DBG_PIO))
21641da177e4SLinus Torvalds 							printk(" %02x", byte);
21651da177e4SLinus Torvalds 					}
2166cdb8c2a6SGuennadi Liakhovetski 
21671da177e4SLinus Torvalds 					DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
21681da177e4SLinus Torvalds 				}
2169cdb8c2a6SGuennadi Liakhovetski 
2170cdb8c2a6SGuennadi Liakhovetski 				scsi_kunmap_atomic_sg(base);
2171cdb8c2a6SGuennadi Liakhovetski 				local_irq_restore(flags);
2172cdb8c2a6SGuennadi Liakhovetski 			}
21731da177e4SLinus Torvalds 			/*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
21741da177e4SLinus Torvalds 			/*srb->total_xfer_length = 0; */
21751da177e4SLinus Torvalds 			if (debug_enabled(DBG_PIO))
21761da177e4SLinus Torvalds 				printk("\n");
21771da177e4SLinus Torvalds 		}
21781da177e4SLinus Torvalds #endif				/* DC395x_LASTPIO */
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds #if 0
21811da177e4SLinus Torvalds 		/*
21821da177e4SLinus Torvalds 		 * KG: This was in DATAOUT. Does it also belong here?
21831da177e4SLinus Torvalds 		 * Nobody seems to know what counter and fifo_cnt count exactly ...
21841da177e4SLinus Torvalds 		 */
21851da177e4SLinus Torvalds 		if (!(scsi_status & SCSIXFERDONE)) {
21861da177e4SLinus Torvalds 			/*
21871da177e4SLinus Torvalds 			 * when data transfer from DMA FIFO to SCSI FIFO
21881da177e4SLinus Torvalds 			 * if there was some data left in SCSI FIFO
21891da177e4SLinus Torvalds 			 */
21901da177e4SLinus Torvalds 			d_left_counter =
21911da177e4SLinus Torvalds 			    (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
21921da177e4SLinus Torvalds 				  0x1F);
21931da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC)
21941da177e4SLinus Torvalds 				d_left_counter <<= 1;
21951da177e4SLinus Torvalds 			/*
21961da177e4SLinus Torvalds 			 * if WIDE scsi SCSI FIFOCNT unit is word !!!
21971da177e4SLinus Torvalds 			 * so need to *= 2
21981da177e4SLinus Torvalds 			 * KG: Seems to be correct ...
21991da177e4SLinus Torvalds 			 */
22001da177e4SLinus Torvalds 		}
22011da177e4SLinus Torvalds #endif
22021da177e4SLinus Torvalds 		/* KG: This should not be needed any more! */
22031da177e4SLinus Torvalds 		if (d_left_counter == 0
22041da177e4SLinus Torvalds 		    || (scsi_status & SCSIXFERCNT_2_ZERO)) {
22051da177e4SLinus Torvalds #if 0
22061da177e4SLinus Torvalds 			int ctr = 6000000;
22071da177e4SLinus Torvalds 			u8 TempDMAstatus;
22081da177e4SLinus Torvalds 			do {
22091da177e4SLinus Torvalds 				TempDMAstatus =
22101da177e4SLinus Torvalds 				    DC395x_read8(acb, TRM_S1040_DMA_STATUS);
22111da177e4SLinus Torvalds 			} while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
22121da177e4SLinus Torvalds 			if (!ctr)
22131da177e4SLinus Torvalds 				dprintkl(KERN_ERR,
22141da177e4SLinus Torvalds 				       "Deadlock in DataInPhase0 waiting for DMA!!\n");
22151da177e4SLinus Torvalds 			srb->total_xfer_length = 0;
22161da177e4SLinus Torvalds #endif
22171da177e4SLinus Torvalds 			srb->total_xfer_length = d_left_counter;
22181da177e4SLinus Torvalds 		} else {	/* phase changed */
22191da177e4SLinus Torvalds 			/*
22201da177e4SLinus Torvalds 			 * parsing the case:
22211da177e4SLinus Torvalds 			 * when a transfer not yet complete
22221da177e4SLinus Torvalds 			 * but be disconnected by target
22231da177e4SLinus Torvalds 			 * if transfer not yet complete
22241da177e4SLinus Torvalds 			 * there were some data residue in SCSI FIFO or
22251da177e4SLinus Torvalds 			 * SCSI transfer counter not empty
22261da177e4SLinus Torvalds 			 */
22271da177e4SLinus Torvalds 			sg_update_list(srb, d_left_counter);
22281da177e4SLinus Torvalds 		}
22291da177e4SLinus Torvalds 	}
22301da177e4SLinus Torvalds 	/* KG: The target may decide to disconnect: Empty FIFO before! */
22311da177e4SLinus Torvalds 	if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
22321da177e4SLinus Torvalds 		cleanup_after_transfer(acb, srb);
22331da177e4SLinus Torvalds 	}
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 
22371da177e4SLinus Torvalds static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
22381da177e4SLinus Torvalds 		u16 *pscsi_status)
22391da177e4SLinus Torvalds {
22405cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_in_phase1: (0x%p) <%02i-%i>\n",
22419cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
22421da177e4SLinus Torvalds 	data_io_transfer(acb, srb, XFERDATAIN);
22431da177e4SLinus Torvalds }
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds static void data_io_transfer(struct AdapterCtlBlk *acb,
22471da177e4SLinus Torvalds 		struct ScsiReqBlk *srb, u16 io_dir)
22481da177e4SLinus Torvalds {
22491da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
22501da177e4SLinus Torvalds 	u8 bval;
22511da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
22525cd049a5SChristoph Hellwig 		"data_io_transfer: (0x%p) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
22539cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun,
22541da177e4SLinus Torvalds 		((io_dir & DMACMD_DIR) ? 'r' : 'w'),
22551da177e4SLinus Torvalds 		srb->total_xfer_length, srb->sg_index, srb->sg_count);
22561da177e4SLinus Torvalds 	if (srb == acb->tmp_srb)
22571da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
22581da177e4SLinus Torvalds 	if (srb->sg_index >= srb->sg_count) {
22591da177e4SLinus Torvalds 		/* can't happen? out of bounds error */
22601da177e4SLinus Torvalds 		return;
22611da177e4SLinus Torvalds 	}
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds 	if (srb->total_xfer_length > DC395x_LASTPIO) {
22641da177e4SLinus Torvalds 		u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
22651da177e4SLinus Torvalds 		/*
22661da177e4SLinus Torvalds 		 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
22671da177e4SLinus Torvalds 		 * Maybe, even ABORTXFER would be appropriate
22681da177e4SLinus Torvalds 		 */
22691da177e4SLinus Torvalds 		if (dma_status & XFERPENDING) {
22701da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
22711da177e4SLinus Torvalds 				"Expect trouble!\n");
22721da177e4SLinus Torvalds 			dump_register_info(acb, dcb, srb);
22731da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
22741da177e4SLinus Torvalds 		}
22751da177e4SLinus Torvalds 		/* clear_fifo(acb, "IO"); */
22761da177e4SLinus Torvalds 		/*
22771da177e4SLinus Torvalds 		 * load what physical address of Scatter/Gather list table
22781da177e4SLinus Torvalds 		 * want to be transfer
22791da177e4SLinus Torvalds 		 */
22801da177e4SLinus Torvalds 		srb->state |= SRB_DATA_XFER;
22811da177e4SLinus Torvalds 		DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
2282a862ea31SFUJITA Tomonori 		if (scsi_sg_count(srb->cmd)) {	/* with S/G */
22831da177e4SLinus Torvalds 			io_dir |= DMACMD_SG;
22841da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
22851da177e4SLinus Torvalds 				       srb->sg_bus_addr +
22861da177e4SLinus Torvalds 				       sizeof(struct SGentry) *
22871da177e4SLinus Torvalds 				       srb->sg_index);
22881da177e4SLinus Torvalds 			/* load how many bytes in the sg list table */
22891da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XCNT,
22901da177e4SLinus Torvalds 				       ((u32)(srb->sg_count -
22911da177e4SLinus Torvalds 					      srb->sg_index) << 3));
22921da177e4SLinus Torvalds 		} else {	/* without S/G */
22931da177e4SLinus Torvalds 			io_dir &= ~DMACMD_SG;
22941da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
22951da177e4SLinus Torvalds 				       srb->segment_x[0].address);
22961da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XCNT,
22971da177e4SLinus Torvalds 				       srb->segment_x[0].length);
22981da177e4SLinus Torvalds 		}
22991da177e4SLinus Torvalds 		/* load total transfer length (24bits) max value 16Mbyte */
23001da177e4SLinus Torvalds 		DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
23011da177e4SLinus Torvalds 			       srb->total_xfer_length);
23021da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
23031da177e4SLinus Torvalds 		if (io_dir & DMACMD_DIR) {	/* read */
23041da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
23051da177e4SLinus Torvalds 				      SCMD_DMA_IN);
23061da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
23071da177e4SLinus Torvalds 		} else {
23081da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
23091da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
23101da177e4SLinus Torvalds 				      SCMD_DMA_OUT);
23111da177e4SLinus Torvalds 		}
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 	}
23141da177e4SLinus Torvalds #if DC395x_LASTPIO
23151da177e4SLinus Torvalds 	else if (srb->total_xfer_length > 0) {	/* The last four bytes: Do PIO */
23161da177e4SLinus Torvalds 		/*
23171da177e4SLinus Torvalds 		 * load what physical address of Scatter/Gather list table
23181da177e4SLinus Torvalds 		 * want to be transfer
23191da177e4SLinus Torvalds 		 */
23201da177e4SLinus Torvalds 		srb->state |= SRB_DATA_XFER;
23211da177e4SLinus Torvalds 		/* load total transfer length (24bits) max value 16Mbyte */
23221da177e4SLinus Torvalds 		DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
23231da177e4SLinus Torvalds 			       srb->total_xfer_length);
23241da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
23251da177e4SLinus Torvalds 		if (io_dir & DMACMD_DIR) {	/* read */
23261da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
23271da177e4SLinus Torvalds 				      SCMD_FIFO_IN);
23281da177e4SLinus Torvalds 		} else {	/* write */
23291da177e4SLinus Torvalds 			int ln = srb->total_xfer_length;
2330cdb8c2a6SGuennadi Liakhovetski 			size_t left_io = srb->total_xfer_length;
2331cdb8c2a6SGuennadi Liakhovetski 
23321da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC)
23331da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
23341da177e4SLinus Torvalds 				     CFG2_WIDEFIFO);
23351da177e4SLinus Torvalds 
2336cdb8c2a6SGuennadi Liakhovetski 			while (left_io) {
2337cdb8c2a6SGuennadi Liakhovetski 				unsigned char *virt, *base = NULL;
2338cdb8c2a6SGuennadi Liakhovetski 				unsigned long flags = 0;
2339cdb8c2a6SGuennadi Liakhovetski 				size_t len = left_io;
2340cdb8c2a6SGuennadi Liakhovetski 				size_t offset = srb->request_length - left_io;
2341a862ea31SFUJITA Tomonori 
2342cdb8c2a6SGuennadi Liakhovetski 				local_irq_save(flags);
2343cdb8c2a6SGuennadi Liakhovetski 				/* Again, max 4 bytes */
2344a862ea31SFUJITA Tomonori 				base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2345cdb8c2a6SGuennadi Liakhovetski 							   srb->sg_count, &offset, &len);
2346cdb8c2a6SGuennadi Liakhovetski 				virt = base + offset;
2347a862ea31SFUJITA Tomonori 
2348cdb8c2a6SGuennadi Liakhovetski 				left_io -= len;
2349cdb8c2a6SGuennadi Liakhovetski 
2350cdb8c2a6SGuennadi Liakhovetski 				while (len--) {
23511da177e4SLinus Torvalds 					if (debug_enabled(DBG_PIO))
2352cdb8c2a6SGuennadi Liakhovetski 						printk(" %02x", *virt);
23531da177e4SLinus Torvalds 
2354cdb8c2a6SGuennadi Liakhovetski 					DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *virt++);
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 					sg_subtract_one(srb);
23571da177e4SLinus Torvalds 				}
2358cdb8c2a6SGuennadi Liakhovetski 
2359cdb8c2a6SGuennadi Liakhovetski 				scsi_kunmap_atomic_sg(base);
2360cdb8c2a6SGuennadi Liakhovetski 				local_irq_restore(flags);
2361cdb8c2a6SGuennadi Liakhovetski 			}
23621da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC) {
23631da177e4SLinus Torvalds 				if (ln % 2) {
23641da177e4SLinus Torvalds 					DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
23651da177e4SLinus Torvalds 					if (debug_enabled(DBG_PIO))
23661da177e4SLinus Torvalds 						printk(" |00");
23671da177e4SLinus Torvalds 				}
23681da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
23691da177e4SLinus Torvalds 			}
23701da177e4SLinus Torvalds 			/*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
23711da177e4SLinus Torvalds 			if (debug_enabled(DBG_PIO))
23721da177e4SLinus Torvalds 				printk("\n");
23731da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
23741da177e4SLinus Torvalds 					  SCMD_FIFO_OUT);
23751da177e4SLinus Torvalds 		}
23761da177e4SLinus Torvalds 	}
23771da177e4SLinus Torvalds #endif				/* DC395x_LASTPIO */
23781da177e4SLinus Torvalds 	else {		/* xfer pad */
23791da177e4SLinus Torvalds 		if (srb->sg_count) {
23801da177e4SLinus Torvalds 			srb->adapter_status = H_OVER_UNDER_RUN;
23811da177e4SLinus Torvalds 			srb->status |= OVER_RUN;
23821da177e4SLinus Torvalds 		}
23831da177e4SLinus Torvalds 		/*
23841da177e4SLinus Torvalds 		 * KG: despite the fact that we are using 16 bits I/O ops
23851da177e4SLinus Torvalds 		 * the SCSI FIFO is only 8 bits according to the docs
23861da177e4SLinus Torvalds 		 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
23871da177e4SLinus Torvalds 		 */
23881da177e4SLinus Torvalds 		if (dcb->sync_period & WIDE_SYNC) {
23891da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
23901da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
23911da177e4SLinus Torvalds 				      CFG2_WIDEFIFO);
23921da177e4SLinus Torvalds 			if (io_dir & DMACMD_DIR) {
239367056d90SLee Jones 				DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
239467056d90SLee Jones 				DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
23951da177e4SLinus Torvalds 			} else {
23961da177e4SLinus Torvalds 				/* Danger, Robinson: If you find KGs
23971da177e4SLinus Torvalds 				 * scattered over the wide disk, the driver
23981da177e4SLinus Torvalds 				 * or chip is to blame :-( */
23991da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
24001da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
24011da177e4SLinus Torvalds 			}
24021da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
24031da177e4SLinus Torvalds 		} else {
24041da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
24051da177e4SLinus Torvalds 			/* Danger, Robinson: If you find a collection of Ks on your disk
24061da177e4SLinus Torvalds 			 * something broke :-( */
24071da177e4SLinus Torvalds 			if (io_dir & DMACMD_DIR)
240867056d90SLee Jones 				DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
24091da177e4SLinus Torvalds 			else
24101da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
24111da177e4SLinus Torvalds 		}
24121da177e4SLinus Torvalds 		srb->state |= SRB_XFERPAD;
24131da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
24141da177e4SLinus Torvalds 		/* SCSI command */
24151da177e4SLinus Torvalds 		bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
24161da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
24171da177e4SLinus Torvalds 	}
24181da177e4SLinus Torvalds }
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
24221da177e4SLinus Torvalds 		u16 *pscsi_status)
24231da177e4SLinus Torvalds {
24245cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "status_phase0: (0x%p) <%02i-%i>\n",
24259cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
24261da177e4SLinus Torvalds 	srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
24271da177e4SLinus Torvalds 	srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);	/* get message */
24281da177e4SLinus Torvalds 	srb->state = SRB_COMPLETED;
24291da177e4SLinus Torvalds 	*pscsi_status = PH_BUS_FREE;	/*.. initial phase */
24301da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
24311da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
24321da177e4SLinus Torvalds }
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
24361da177e4SLinus Torvalds 		u16 *pscsi_status)
24371da177e4SLinus Torvalds {
24385cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "status_phase1: (0x%p) <%02i-%i>\n",
24399cb78c16SHannes Reinecke 		srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
24401da177e4SLinus Torvalds 	srb->state = SRB_STATUS;
24411da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
24421da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
24431da177e4SLinus Torvalds }
24441da177e4SLinus Torvalds 
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds /* Check if the message is complete */
24471da177e4SLinus Torvalds static inline u8 msgin_completed(u8 * msgbuf, u32 len)
24481da177e4SLinus Torvalds {
24491da177e4SLinus Torvalds 	if (*msgbuf == EXTENDED_MESSAGE) {
24501da177e4SLinus Torvalds 		if (len < 2)
24511da177e4SLinus Torvalds 			return 0;
24521da177e4SLinus Torvalds 		if (len < msgbuf[1] + 2)
24531da177e4SLinus Torvalds 			return 0;
24541da177e4SLinus Torvalds 	} else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f)	/* two byte messages */
24551da177e4SLinus Torvalds 		if (len < 2)
24561da177e4SLinus Torvalds 			return 0;
24571da177e4SLinus Torvalds 	return 1;
24581da177e4SLinus Torvalds }
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds /* reject_msg */
24611da177e4SLinus Torvalds static inline void msgin_reject(struct AdapterCtlBlk *acb,
24621da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
24631da177e4SLinus Torvalds {
24641da177e4SLinus Torvalds 	srb->msgout_buf[0] = MESSAGE_REJECT;
24651da177e4SLinus Torvalds 	srb->msg_count = 1;
24661da177e4SLinus Torvalds 	DC395x_ENABLE_MSGOUT;
24671da177e4SLinus Torvalds 	srb->state &= ~SRB_MSGIN;
24681da177e4SLinus Torvalds 	srb->state |= SRB_MSGOUT;
24691da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
24701da177e4SLinus Torvalds 		srb->msgin_buf[0],
24711da177e4SLinus Torvalds 		srb->dcb->target_id, srb->dcb->target_lun);
24721da177e4SLinus Torvalds }
24731da177e4SLinus Torvalds 
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
24761da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb, u8 tag)
24771da177e4SLinus Torvalds {
24781da177e4SLinus Torvalds 	struct ScsiReqBlk *srb = NULL;
24791da177e4SLinus Torvalds 	struct ScsiReqBlk *i;
24805cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_qtag: (0x%p) tag=%i srb=%p\n",
24815cd049a5SChristoph Hellwig 		   srb->cmd, tag, srb);
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 	if (!(dcb->tag_mask & (1 << tag)))
24841da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
24851da177e4SLinus Torvalds 			"msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
24861da177e4SLinus Torvalds 			dcb->tag_mask, tag);
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	if (list_empty(&dcb->srb_going_list))
24891da177e4SLinus Torvalds 		goto mingx0;
24901da177e4SLinus Torvalds 	list_for_each_entry(i, &dcb->srb_going_list, list) {
24911da177e4SLinus Torvalds 		if (i->tag_number == tag) {
24921da177e4SLinus Torvalds 			srb = i;
24931da177e4SLinus Torvalds 			break;
24941da177e4SLinus Torvalds 		}
24951da177e4SLinus Torvalds 	}
24961da177e4SLinus Torvalds 	if (!srb)
24971da177e4SLinus Torvalds 		goto mingx0;
24981da177e4SLinus Torvalds 
24995cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_qtag: (0x%p) <%02i-%i>\n",
25005cd049a5SChristoph Hellwig 		srb->cmd, srb->dcb->target_id, srb->dcb->target_lun);
25011da177e4SLinus Torvalds 	if (dcb->flag & ABORT_DEV_) {
25021da177e4SLinus Torvalds 		/*srb->state = SRB_ABORT_SENT; */
25031da177e4SLinus Torvalds 		enable_msgout_abort(acb, srb);
25041da177e4SLinus Torvalds 	}
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds 	if (!(srb->state & SRB_DISCONNECT))
25071da177e4SLinus Torvalds 		goto mingx0;
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds 	memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
25101da177e4SLinus Torvalds 	srb->state |= dcb->active_srb->state;
25111da177e4SLinus Torvalds 	srb->state |= SRB_DATA_XFER;
25121da177e4SLinus Torvalds 	dcb->active_srb = srb;
25131da177e4SLinus Torvalds 	/* How can we make the DORS happy? */
25141da177e4SLinus Torvalds 	return srb;
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds       mingx0:
25171da177e4SLinus Torvalds 	srb = acb->tmp_srb;
25181da177e4SLinus Torvalds 	srb->state = SRB_UNEXPECT_RESEL;
25191da177e4SLinus Torvalds 	dcb->active_srb = srb;
25209c2d2670SHannes Reinecke 	srb->msgout_buf[0] = ABORT_TASK;
25211da177e4SLinus Torvalds 	srb->msg_count = 1;
25221da177e4SLinus Torvalds 	DC395x_ENABLE_MSGOUT;
25231da177e4SLinus Torvalds 	dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
25241da177e4SLinus Torvalds 	return srb;
25251da177e4SLinus Torvalds }
25261da177e4SLinus Torvalds 
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds static inline void reprogram_regs(struct AdapterCtlBlk *acb,
25291da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
25301da177e4SLinus Torvalds {
25311da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
25321da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
25331da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
25341da177e4SLinus Torvalds 	set_xfer_rate(acb, dcb);
25351da177e4SLinus Torvalds }
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds /* set async transfer mode */
25391da177e4SLinus Torvalds static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
25401da177e4SLinus Torvalds {
25411da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
25421da177e4SLinus Torvalds 	dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
25431da177e4SLinus Torvalds 		dcb->target_id, dcb->target_lun);
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
25461da177e4SLinus Torvalds 	dcb->sync_mode |= SYNC_NEGO_DONE;
25471da177e4SLinus Torvalds 	/*dcb->sync_period &= 0; */
25481da177e4SLinus Torvalds 	dcb->sync_offset = 0;
25491da177e4SLinus Torvalds 	dcb->min_nego_period = 200 >> 2;	/* 200ns <=> 5 MHz */
25501da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_SYNC_NEGO;
25511da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
25521da177e4SLinus Torvalds 	if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
25531da177e4SLinus Torvalds 	    && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
25541da177e4SLinus Torvalds 		build_wdtr(acb, dcb, srb);
25551da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
25561da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
25571da177e4SLinus Torvalds 	}
25581da177e4SLinus Torvalds }
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds /* set sync transfer mode */
25621da177e4SLinus Torvalds static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
25631da177e4SLinus Torvalds {
25641da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
25651da177e4SLinus Torvalds 	u8 bval;
25661da177e4SLinus Torvalds 	int fact;
25671da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
25681da177e4SLinus Torvalds 		"(%02i.%01i MHz) Offset %i\n",
25691da177e4SLinus Torvalds 		dcb->target_id, srb->msgin_buf[3] << 2,
25701da177e4SLinus Torvalds 		(250 / srb->msgin_buf[3]),
25711da177e4SLinus Torvalds 		((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
25721da177e4SLinus Torvalds 		srb->msgin_buf[4]);
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	if (srb->msgin_buf[4] > 15)
25751da177e4SLinus Torvalds 		srb->msgin_buf[4] = 15;
25761da177e4SLinus Torvalds 	if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
25771da177e4SLinus Torvalds 		dcb->sync_offset = 0;
25781da177e4SLinus Torvalds 	else if (dcb->sync_offset == 0)
25791da177e4SLinus Torvalds 		dcb->sync_offset = srb->msgin_buf[4];
25801da177e4SLinus Torvalds 	if (srb->msgin_buf[4] > dcb->sync_offset)
25811da177e4SLinus Torvalds 		srb->msgin_buf[4] = dcb->sync_offset;
25821da177e4SLinus Torvalds 	else
25831da177e4SLinus Torvalds 		dcb->sync_offset = srb->msgin_buf[4];
25841da177e4SLinus Torvalds 	bval = 0;
25851da177e4SLinus Torvalds 	while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
25861da177e4SLinus Torvalds 			    || dcb->min_nego_period >
25871da177e4SLinus Torvalds 			    clock_period[bval]))
25881da177e4SLinus Torvalds 		bval++;
25891da177e4SLinus Torvalds 	if (srb->msgin_buf[3] < clock_period[bval])
25901da177e4SLinus Torvalds 		dprintkl(KERN_INFO,
25911da177e4SLinus Torvalds 			"msgin_set_sync: Increase sync nego period to %ins\n",
25921da177e4SLinus Torvalds 			clock_period[bval] << 2);
25931da177e4SLinus Torvalds 	srb->msgin_buf[3] = clock_period[bval];
25941da177e4SLinus Torvalds 	dcb->sync_period &= 0xf0;
25951da177e4SLinus Torvalds 	dcb->sync_period |= ALT_SYNC | bval;
25961da177e4SLinus Torvalds 	dcb->min_nego_period = srb->msgin_buf[3];
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	if (dcb->sync_period & WIDE_SYNC)
25991da177e4SLinus Torvalds 		fact = 500;
26001da177e4SLinus Torvalds 	else
26011da177e4SLinus Torvalds 		fact = 250;
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 	dprintkl(KERN_INFO,
26041da177e4SLinus Torvalds 		"Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
26051da177e4SLinus Torvalds 		dcb->target_id, (fact == 500) ? "Wide16" : "",
26061da177e4SLinus Torvalds 		dcb->min_nego_period << 2, dcb->sync_offset,
26071da177e4SLinus Torvalds 		(fact / dcb->min_nego_period),
26081da177e4SLinus Torvalds 		((fact % dcb->min_nego_period) * 10 +
26091da177e4SLinus Torvalds 		dcb->min_nego_period / 2) / dcb->min_nego_period);
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds 	if (!(srb->state & SRB_DO_SYNC_NEGO)) {
26121da177e4SLinus Torvalds 		/* Reply with corrected SDTR Message */
26131da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
26141da177e4SLinus Torvalds 			srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds 		memcpy(srb->msgout_buf, srb->msgin_buf, 5);
26171da177e4SLinus Torvalds 		srb->msg_count = 5;
26181da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
26191da177e4SLinus Torvalds 		dcb->sync_mode |= SYNC_NEGO_DONE;
26201da177e4SLinus Torvalds 	} else {
26211da177e4SLinus Torvalds 		if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
26221da177e4SLinus Torvalds 		    && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
26231da177e4SLinus Torvalds 			build_wdtr(acb, dcb, srb);
26241da177e4SLinus Torvalds 			DC395x_ENABLE_MSGOUT;
26251da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
26261da177e4SLinus Torvalds 		}
26271da177e4SLinus Torvalds 	}
26281da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_SYNC_NEGO;
26291da177e4SLinus Torvalds 	dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
26321da177e4SLinus Torvalds }
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 
26351da177e4SLinus Torvalds static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
26361da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
26371da177e4SLinus Torvalds {
26381da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
26391da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 	dcb->sync_period &= ~WIDE_SYNC;
26421da177e4SLinus Torvalds 	dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
26431da177e4SLinus Torvalds 	dcb->sync_mode |= WIDE_NEGO_DONE;
26441da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_WIDE_NEGO;
26451da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
26461da177e4SLinus Torvalds 	if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
26471da177e4SLinus Torvalds 	    && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
26481da177e4SLinus Torvalds 		build_sdtr(acb, dcb, srb);
26491da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
26501da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
26511da177e4SLinus Torvalds 	}
26521da177e4SLinus Torvalds }
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
26551da177e4SLinus Torvalds {
26561da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
26571da177e4SLinus Torvalds 	u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
26581da177e4SLinus Torvalds 		   && acb->config & HCC_WIDE_CARD) ? 1 : 0;
26591da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds 	if (srb->msgin_buf[3] > wide)
26621da177e4SLinus Torvalds 		srb->msgin_buf[3] = wide;
26631da177e4SLinus Torvalds 	/* Completed */
26641da177e4SLinus Torvalds 	if (!(srb->state & SRB_DO_WIDE_NEGO)) {
26651da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
26661da177e4SLinus Torvalds 			"msgin_set_wide: Wide nego initiated <%02i>\n",
26671da177e4SLinus Torvalds 			dcb->target_id);
26681da177e4SLinus Torvalds 		memcpy(srb->msgout_buf, srb->msgin_buf, 4);
26691da177e4SLinus Torvalds 		srb->msg_count = 4;
26701da177e4SLinus Torvalds 		srb->state |= SRB_DO_WIDE_NEGO;
26711da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
26721da177e4SLinus Torvalds 	}
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds 	dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
26751da177e4SLinus Torvalds 	if (srb->msgin_buf[3] > 0)
26761da177e4SLinus Torvalds 		dcb->sync_period |= WIDE_SYNC;
26771da177e4SLinus Torvalds 	else
26781da177e4SLinus Torvalds 		dcb->sync_period &= ~WIDE_SYNC;
26791da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_WIDE_NEGO;
26801da177e4SLinus Torvalds 	/*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
26811da177e4SLinus Torvalds 	dprintkdbg(DBG_1,
26821da177e4SLinus Torvalds 		"msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
26831da177e4SLinus Torvalds 		(8 << srb->msgin_buf[3]), dcb->target_id);
26841da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
26851da177e4SLinus Torvalds 	if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
26861da177e4SLinus Torvalds 	    && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
26871da177e4SLinus Torvalds 		build_sdtr(acb, dcb, srb);
26881da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
26891da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
26901da177e4SLinus Torvalds 	}
26911da177e4SLinus Torvalds }
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds /*
26951da177e4SLinus Torvalds  * extended message codes:
26961da177e4SLinus Torvalds  *
26971da177e4SLinus Torvalds  *	code	description
26981da177e4SLinus Torvalds  *
26991da177e4SLinus Torvalds  *	02h	Reserved
27001da177e4SLinus Torvalds  *	00h	MODIFY DATA  POINTER
27011da177e4SLinus Torvalds  *	01h	SYNCHRONOUS DATA TRANSFER REQUEST
27021da177e4SLinus Torvalds  *	03h	WIDE DATA TRANSFER REQUEST
27031da177e4SLinus Torvalds  *   04h - 7Fh	Reserved
27041da177e4SLinus Torvalds  *   80h - FFh	Vendor specific
27051da177e4SLinus Torvalds  */
27061da177e4SLinus Torvalds static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
27071da177e4SLinus Torvalds 		u16 *pscsi_status)
27081da177e4SLinus Torvalds {
27091da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = acb->active_dcb;
27105cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_phase0: (0x%p)\n", srb->cmd);
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds 	srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
27131da177e4SLinus Torvalds 	if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
27141da177e4SLinus Torvalds 		/* Now eval the msg */
27151da177e4SLinus Torvalds 		switch (srb->msgin_buf[0]) {
27161da177e4SLinus Torvalds 		case DISCONNECT:
27171da177e4SLinus Torvalds 			srb->state = SRB_DISCONNECT;
27181da177e4SLinus Torvalds 			break;
27191da177e4SLinus Torvalds 
27201da177e4SLinus Torvalds 		case SIMPLE_QUEUE_TAG:
27211da177e4SLinus Torvalds 		case HEAD_OF_QUEUE_TAG:
27221da177e4SLinus Torvalds 		case ORDERED_QUEUE_TAG:
27231da177e4SLinus Torvalds 			srb =
27241da177e4SLinus Torvalds 			    msgin_qtag(acb, dcb,
27251da177e4SLinus Torvalds 					      srb->msgin_buf[1]);
27261da177e4SLinus Torvalds 			break;
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds 		case MESSAGE_REJECT:
27291da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
27301da177e4SLinus Torvalds 				       DO_CLRATN | DO_DATALATCH);
27311da177e4SLinus Torvalds 			/* A sync nego message was rejected ! */
27321da177e4SLinus Torvalds 			if (srb->state & SRB_DO_SYNC_NEGO) {
27331da177e4SLinus Torvalds 				msgin_set_async(acb, srb);
27341da177e4SLinus Torvalds 				break;
27351da177e4SLinus Torvalds 			}
27361da177e4SLinus Torvalds 			/* A wide nego message was rejected ! */
27371da177e4SLinus Torvalds 			if (srb->state & SRB_DO_WIDE_NEGO) {
27381da177e4SLinus Torvalds 				msgin_set_nowide(acb, srb);
27391da177e4SLinus Torvalds 				break;
27401da177e4SLinus Torvalds 			}
27411da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
27421da177e4SLinus Torvalds 			/*srb->state |= SRB_ABORT_SENT */
27431da177e4SLinus Torvalds 			break;
27441da177e4SLinus Torvalds 
27451da177e4SLinus Torvalds 		case EXTENDED_MESSAGE:
27461da177e4SLinus Torvalds 			/* SDTR */
27471da177e4SLinus Torvalds 			if (srb->msgin_buf[1] == 3
27481da177e4SLinus Torvalds 			    && srb->msgin_buf[2] == EXTENDED_SDTR) {
27491da177e4SLinus Torvalds 				msgin_set_sync(acb, srb);
27501da177e4SLinus Torvalds 				break;
27511da177e4SLinus Torvalds 			}
27521da177e4SLinus Torvalds 			/* WDTR */
27531da177e4SLinus Torvalds 			if (srb->msgin_buf[1] == 2
27541da177e4SLinus Torvalds 			    && srb->msgin_buf[2] == EXTENDED_WDTR
27551da177e4SLinus Torvalds 			    && srb->msgin_buf[3] <= 2) { /* sanity check ... */
27561da177e4SLinus Torvalds 				msgin_set_wide(acb, srb);
27571da177e4SLinus Torvalds 				break;
27581da177e4SLinus Torvalds 			}
27591da177e4SLinus Torvalds 			msgin_reject(acb, srb);
27601da177e4SLinus Torvalds 			break;
27611da177e4SLinus Torvalds 
27629c2d2670SHannes Reinecke 		case IGNORE_WIDE_RESIDUE:
27631da177e4SLinus Torvalds 			/* Discard  wide residual */
27641da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
27651da177e4SLinus Torvalds 			break;
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds 		case COMMAND_COMPLETE:
27681da177e4SLinus Torvalds 			/* nothing has to be done */
27691da177e4SLinus Torvalds 			break;
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds 		case SAVE_POINTERS:
27721da177e4SLinus Torvalds 			/*
27731da177e4SLinus Torvalds 			 * SAVE POINTER may be ignored as we have the struct
27741da177e4SLinus Torvalds 			 * ScsiReqBlk* associated with the scsi command.
27751da177e4SLinus Torvalds 			 */
27765cd049a5SChristoph Hellwig 			dprintkdbg(DBG_0, "msgin_phase0: (0x%p) "
27771da177e4SLinus Torvalds 				"SAVE POINTER rem=%i Ignore\n",
27785cd049a5SChristoph Hellwig 				srb->cmd, srb->total_xfer_length);
27791da177e4SLinus Torvalds 			break;
27801da177e4SLinus Torvalds 
27811da177e4SLinus Torvalds 		case RESTORE_POINTERS:
27821da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
27831da177e4SLinus Torvalds 			break;
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds 		case ABORT:
27865cd049a5SChristoph Hellwig 			dprintkdbg(DBG_0, "msgin_phase0: (0x%p) "
27871da177e4SLinus Torvalds 				"<%02i-%i> ABORT msg\n",
27885cd049a5SChristoph Hellwig 				srb->cmd, dcb->target_id,
27891da177e4SLinus Torvalds 				dcb->target_lun);
27901da177e4SLinus Torvalds 			dcb->flag |= ABORT_DEV_;
27911da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
27921da177e4SLinus Torvalds 			break;
27931da177e4SLinus Torvalds 
27941da177e4SLinus Torvalds 		default:
27951da177e4SLinus Torvalds 			/* reject unknown messages */
27961da177e4SLinus Torvalds 			if (srb->msgin_buf[0] & IDENTIFY_BASE) {
27971da177e4SLinus Torvalds 				dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
27981da177e4SLinus Torvalds 				srb->msg_count = 1;
27991da177e4SLinus Torvalds 				srb->msgout_buf[0] = dcb->identify_msg;
28001da177e4SLinus Torvalds 				DC395x_ENABLE_MSGOUT;
28011da177e4SLinus Torvalds 				srb->state |= SRB_MSGOUT;
28021da177e4SLinus Torvalds 				/*break; */
28031da177e4SLinus Torvalds 			}
28041da177e4SLinus Torvalds 			msgin_reject(acb, srb);
28051da177e4SLinus Torvalds 		}
28061da177e4SLinus Torvalds 
28071da177e4SLinus Torvalds 		/* Clear counter and MsgIn state */
28081da177e4SLinus Torvalds 		srb->state &= ~SRB_MSGIN;
28091da177e4SLinus Torvalds 		acb->msg_len = 0;
28101da177e4SLinus Torvalds 	}
28111da177e4SLinus Torvalds 	*pscsi_status = PH_BUS_FREE;
28121da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important ... you know! */
28131da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
28141da177e4SLinus Torvalds }
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
28181da177e4SLinus Torvalds 		u16 *pscsi_status)
28191da177e4SLinus Torvalds {
28205cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_phase1: (0x%p)\n", srb->cmd);
28211da177e4SLinus Torvalds 	clear_fifo(acb, "msgin_phase1");
28221da177e4SLinus Torvalds 	DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
28231da177e4SLinus Torvalds 	if (!(srb->state & SRB_MSGIN)) {
28241da177e4SLinus Torvalds 		srb->state &= ~SRB_DISCONNECT;
28251da177e4SLinus Torvalds 		srb->state |= SRB_MSGIN;
28261da177e4SLinus Torvalds 	}
28271da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
28281da177e4SLinus Torvalds 	/* SCSI command */
28291da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
28301da177e4SLinus Torvalds }
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
28341da177e4SLinus Torvalds 		u16 *pscsi_status)
28351da177e4SLinus Torvalds {
28361da177e4SLinus Torvalds }
28371da177e4SLinus Torvalds 
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
28401da177e4SLinus Torvalds 		u16 *pscsi_status)
28411da177e4SLinus Torvalds {
28421da177e4SLinus Torvalds }
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
28461da177e4SLinus Torvalds {
28471da177e4SLinus Torvalds 	struct DeviceCtlBlk *i;
28481da177e4SLinus Torvalds 
28491da177e4SLinus Torvalds 	/* set all lun device's  period, offset */
28501da177e4SLinus Torvalds 	if (dcb->identify_msg & 0x07)
28511da177e4SLinus Torvalds 		return;
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds 	if (acb->scan_devices) {
28541da177e4SLinus Torvalds 		current_sync_offset = dcb->sync_offset;
28551da177e4SLinus Torvalds 		return;
28561da177e4SLinus Torvalds 	}
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds 	list_for_each_entry(i, &acb->dcb_list, list)
28591da177e4SLinus Torvalds 		if (i->target_id == dcb->target_id) {
28601da177e4SLinus Torvalds 			i->sync_period = dcb->sync_period;
28611da177e4SLinus Torvalds 			i->sync_offset = dcb->sync_offset;
28621da177e4SLinus Torvalds 			i->sync_mode = dcb->sync_mode;
28631da177e4SLinus Torvalds 			i->min_nego_period = dcb->min_nego_period;
28641da177e4SLinus Torvalds 		}
28651da177e4SLinus Torvalds }
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds static void disconnect(struct AdapterCtlBlk *acb)
28691da177e4SLinus Torvalds {
28701da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = acb->active_dcb;
28711da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds 	if (!dcb) {
28741da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "disconnect: No such device\n");
28751da177e4SLinus Torvalds 		udelay(500);
28761da177e4SLinus Torvalds 		/* Suspend queue for a while */
287753a8936cSHannes Reinecke 		acb->last_reset =
28781da177e4SLinus Torvalds 		    jiffies + HZ / 2 +
28791da177e4SLinus Torvalds 		    HZ * acb->eeprom.delay_time;
28801da177e4SLinus Torvalds 		clear_fifo(acb, "disconnectEx");
28811da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
28821da177e4SLinus Torvalds 		return;
28831da177e4SLinus Torvalds 	}
28841da177e4SLinus Torvalds 	srb = dcb->active_srb;
28851da177e4SLinus Torvalds 	acb->active_dcb = NULL;
28865cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "disconnect: (0x%p)\n", srb->cmd);
28871da177e4SLinus Torvalds 
28881da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
28891da177e4SLinus Torvalds 	clear_fifo(acb, "disconnect");
28901da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
28911da177e4SLinus Torvalds 	if (srb->state & SRB_UNEXPECT_RESEL) {
28921da177e4SLinus Torvalds 		dprintkl(KERN_ERR,
28931da177e4SLinus Torvalds 			"disconnect: Unexpected reselection <%02i-%i>\n",
28941da177e4SLinus Torvalds 			dcb->target_id, dcb->target_lun);
28951da177e4SLinus Torvalds 		srb->state = 0;
28961da177e4SLinus Torvalds 		waiting_process_next(acb);
28971da177e4SLinus Torvalds 	} else if (srb->state & SRB_ABORT_SENT) {
28981da177e4SLinus Torvalds 		dcb->flag &= ~ABORT_DEV_;
289953a8936cSHannes Reinecke 		acb->last_reset = jiffies + HZ / 2 + 1;
29001da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
29011da177e4SLinus Torvalds 		doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
29021da177e4SLinus Torvalds 		waiting_process_next(acb);
29031da177e4SLinus Torvalds 	} else {
29041da177e4SLinus Torvalds 		if ((srb->state & (SRB_START_ + SRB_MSGOUT))
29051da177e4SLinus Torvalds 		    || !(srb->
2906973c9208SJiapeng Chong 			 state & (SRB_DISCONNECT | SRB_COMPLETED))) {
29071da177e4SLinus Torvalds 			/*
29081da177e4SLinus Torvalds 			 * Selection time out
29091da177e4SLinus Torvalds 			 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
29101da177e4SLinus Torvalds 			 */
29111da177e4SLinus Torvalds 			/* Unexp. Disc / Sel Timeout */
29121da177e4SLinus Torvalds 			if (srb->state != SRB_START_
29131da177e4SLinus Torvalds 			    && srb->state != SRB_MSGOUT) {
29141da177e4SLinus Torvalds 				srb->state = SRB_READY;
29151da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
29165cd049a5SChristoph Hellwig 					"disconnect: (0x%p) Unexpected\n",
29175cd049a5SChristoph Hellwig 					srb->cmd);
29181da177e4SLinus Torvalds 				srb->target_status = SCSI_STAT_SEL_TIMEOUT;
29191da177e4SLinus Torvalds 				goto disc1;
29201da177e4SLinus Torvalds 			} else {
29211da177e4SLinus Torvalds 				/* Normal selection timeout */
29225cd049a5SChristoph Hellwig 				dprintkdbg(DBG_KG, "disconnect: (0x%p) "
29235cd049a5SChristoph Hellwig 					"<%02i-%i> SelTO\n", srb->cmd,
29241da177e4SLinus Torvalds 					dcb->target_id, dcb->target_lun);
29251da177e4SLinus Torvalds 				if (srb->retry_count++ > DC395x_MAX_RETRIES
29261da177e4SLinus Torvalds 				    || acb->scan_devices) {
29271da177e4SLinus Torvalds 					srb->target_status =
29281da177e4SLinus Torvalds 					    SCSI_STAT_SEL_TIMEOUT;
29291da177e4SLinus Torvalds 					goto disc1;
29301da177e4SLinus Torvalds 				}
29311da177e4SLinus Torvalds 				free_tag(dcb, srb);
2932cd29660dSChristoph Hellwig 				list_move(&srb->list, &dcb->srb_waiting_list);
29331da177e4SLinus Torvalds 				dprintkdbg(DBG_KG,
29345cd049a5SChristoph Hellwig 					"disconnect: (0x%p) Retry\n",
29355cd049a5SChristoph Hellwig 					srb->cmd);
29361da177e4SLinus Torvalds 				waiting_set_timer(acb, HZ / 20);
29371da177e4SLinus Torvalds 			}
29381da177e4SLinus Torvalds 		} else if (srb->state & SRB_DISCONNECT) {
29391da177e4SLinus Torvalds 			u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
29401da177e4SLinus Torvalds 			/*
29411da177e4SLinus Torvalds 			 * SRB_DISCONNECT (This is what we expect!)
29421da177e4SLinus Torvalds 			 */
29431da177e4SLinus Torvalds 			if (bval & 0x40) {
29441da177e4SLinus Torvalds 				dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
29451da177e4SLinus Torvalds 					" 0x%02x: ACK set! Other controllers?\n",
29461da177e4SLinus Torvalds 					bval);
29471da177e4SLinus Torvalds 				/* It could come from another initiator, therefore don't do much ! */
29481da177e4SLinus Torvalds 			} else
29491da177e4SLinus Torvalds 				waiting_process_next(acb);
29501da177e4SLinus Torvalds 		} else if (srb->state & SRB_COMPLETED) {
29511da177e4SLinus Torvalds 		      disc1:
29521da177e4SLinus Torvalds 			/*
29531da177e4SLinus Torvalds 			 ** SRB_COMPLETED
29541da177e4SLinus Torvalds 			 */
29551da177e4SLinus Torvalds 			free_tag(dcb, srb);
29561da177e4SLinus Torvalds 			dcb->active_srb = NULL;
29571da177e4SLinus Torvalds 			srb->state = SRB_FREE;
29581da177e4SLinus Torvalds 			srb_done(acb, dcb, srb);
29591da177e4SLinus Torvalds 		}
29601da177e4SLinus Torvalds 	}
29611da177e4SLinus Torvalds }
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds 
29641da177e4SLinus Torvalds static void reselect(struct AdapterCtlBlk *acb)
29651da177e4SLinus Torvalds {
29661da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = acb->active_dcb;
29671da177e4SLinus Torvalds 	struct ScsiReqBlk *srb = NULL;
29681da177e4SLinus Torvalds 	u16 rsel_tar_lun_id;
29691da177e4SLinus Torvalds 	u8 id, lun;
29701da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
29711da177e4SLinus Torvalds 
29721da177e4SLinus Torvalds 	clear_fifo(acb, "reselect");
29731da177e4SLinus Torvalds 	/*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
29741da177e4SLinus Torvalds 	/* Read Reselected Target ID and LUN */
29751da177e4SLinus Torvalds 	rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
29761da177e4SLinus Torvalds 	if (dcb) {		/* Arbitration lost but Reselection win */
29771da177e4SLinus Torvalds 		srb = dcb->active_srb;
29781da177e4SLinus Torvalds 		if (!srb) {
29791da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
29801da177e4SLinus Torvalds 				"but active_srb == NULL\n");
29811da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
29821da177e4SLinus Torvalds 			return;
29831da177e4SLinus Torvalds 		}
29841da177e4SLinus Torvalds 		/* Why the if ? */
29851da177e4SLinus Torvalds 		if (!acb->scan_devices) {
29865cd049a5SChristoph Hellwig 			dprintkdbg(DBG_KG, "reselect: (0x%p) <%02i-%i> "
29871da177e4SLinus Torvalds 				"Arb lost but Resel win rsel=%i stat=0x%04x\n",
29885cd049a5SChristoph Hellwig 				srb->cmd, dcb->target_id,
29891da177e4SLinus Torvalds 				dcb->target_lun, rsel_tar_lun_id,
29901da177e4SLinus Torvalds 				DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
29911da177e4SLinus Torvalds 			/*srb->state |= SRB_DISCONNECT; */
29921da177e4SLinus Torvalds 
29931da177e4SLinus Torvalds 			srb->state = SRB_READY;
29941da177e4SLinus Torvalds 			free_tag(dcb, srb);
2995cd29660dSChristoph Hellwig 			list_move(&srb->list, &dcb->srb_waiting_list);
29961da177e4SLinus Torvalds 			waiting_set_timer(acb, HZ / 20);
29971da177e4SLinus Torvalds 
29981da177e4SLinus Torvalds 			/* return; */
29991da177e4SLinus Torvalds 		}
30001da177e4SLinus Torvalds 	}
30011da177e4SLinus Torvalds 	/* Read Reselected Target Id and LUN */
30021da177e4SLinus Torvalds 	if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
30031da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
30041da177e4SLinus Torvalds 			"Got %i!\n", rsel_tar_lun_id);
30051da177e4SLinus Torvalds 	id = rsel_tar_lun_id & 0xff;
30061da177e4SLinus Torvalds 	lun = (rsel_tar_lun_id >> 8) & 7;
30071da177e4SLinus Torvalds 	dcb = find_dcb(acb, id, lun);
30081da177e4SLinus Torvalds 	if (!dcb) {
30091da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "reselect: From non existent device "
30101da177e4SLinus Torvalds 			"<%02i-%i>\n", id, lun);
30111da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
30121da177e4SLinus Torvalds 		return;
30131da177e4SLinus Torvalds 	}
30141da177e4SLinus Torvalds 	acb->active_dcb = dcb;
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds 	if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
30171da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
30181da177e4SLinus Torvalds 			"disconnection? <%02i-%i>\n",
30191da177e4SLinus Torvalds 			dcb->target_id, dcb->target_lun);
30201da177e4SLinus Torvalds 
302167056d90SLee Jones 	if (dcb->sync_mode & EN_TAG_QUEUEING) {
30221da177e4SLinus Torvalds 		srb = acb->tmp_srb;
30231da177e4SLinus Torvalds 		dcb->active_srb = srb;
30241da177e4SLinus Torvalds 	} else {
30251da177e4SLinus Torvalds 		/* There can be only one! */
30261da177e4SLinus Torvalds 		srb = dcb->active_srb;
30271da177e4SLinus Torvalds 		if (!srb || !(srb->state & SRB_DISCONNECT)) {
30281da177e4SLinus Torvalds 			/*
30291da177e4SLinus Torvalds 			 * abort command
30301da177e4SLinus Torvalds 			 */
30311da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
30321da177e4SLinus Torvalds 				"reselect: w/o disconnected cmds <%02i-%i>\n",
30331da177e4SLinus Torvalds 				dcb->target_id, dcb->target_lun);
30341da177e4SLinus Torvalds 			srb = acb->tmp_srb;
30351da177e4SLinus Torvalds 			srb->state = SRB_UNEXPECT_RESEL;
30361da177e4SLinus Torvalds 			dcb->active_srb = srb;
30371da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
30381da177e4SLinus Torvalds 		} else {
30391da177e4SLinus Torvalds 			if (dcb->flag & ABORT_DEV_) {
30401da177e4SLinus Torvalds 				/*srb->state = SRB_ABORT_SENT; */
30411da177e4SLinus Torvalds 				enable_msgout_abort(acb, srb);
30421da177e4SLinus Torvalds 			} else
30431da177e4SLinus Torvalds 				srb->state = SRB_DATA_XFER;
30441da177e4SLinus Torvalds 
30451da177e4SLinus Torvalds 		}
30461da177e4SLinus Torvalds 	}
30471da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
30481da177e4SLinus Torvalds 
30491da177e4SLinus Torvalds 	/* Program HA ID, target ID, period and offset */
30501da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
30511da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);	/* host   ID */
30521da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);		/* target ID */
30531da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);		/* offset    */
30541da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);		/* sync period, wide */
30551da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);		/* it's important for atn stop */
30561da177e4SLinus Torvalds 	/* SCSI command */
30571da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
30581da177e4SLinus Torvalds }
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds static inline u8 tagq_blacklist(char *name)
30621da177e4SLinus Torvalds {
30631da177e4SLinus Torvalds #ifndef DC395x_NO_TAGQ
30641da177e4SLinus Torvalds #if 0
30651da177e4SLinus Torvalds 	u8 i;
30661da177e4SLinus Torvalds 	for (i = 0; i < BADDEVCNT; i++)
30671da177e4SLinus Torvalds 		if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
30681da177e4SLinus Torvalds 			return 1;
30691da177e4SLinus Torvalds #endif
30701da177e4SLinus Torvalds 	return 0;
30711da177e4SLinus Torvalds #else
30721da177e4SLinus Torvalds 	return 1;
30731da177e4SLinus Torvalds #endif
30741da177e4SLinus Torvalds }
30751da177e4SLinus Torvalds 
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
30781da177e4SLinus Torvalds {
30791da177e4SLinus Torvalds 	/* Check for SCSI format (ANSI and Response data format) */
30801da177e4SLinus Torvalds 	if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
30811da177e4SLinus Torvalds 		if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
30821da177e4SLinus Torvalds 		    && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
30831da177e4SLinus Torvalds 		    /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
30841da177e4SLinus Torvalds 		    /* ((dcb->dev_type == TYPE_DISK)
30851da177e4SLinus Torvalds 		       || (dcb->dev_type == TYPE_MOD)) && */
30861da177e4SLinus Torvalds 		    !tagq_blacklist(((char *)ptr) + 8)) {
30871da177e4SLinus Torvalds 			if (dcb->max_command == 1)
30881da177e4SLinus Torvalds 				dcb->max_command =
30891da177e4SLinus Torvalds 				    dcb->acb->tag_max_num;
30901da177e4SLinus Torvalds 			dcb->sync_mode |= EN_TAG_QUEUEING;
30911da177e4SLinus Torvalds 			/*dcb->tag_mask = 0; */
30921da177e4SLinus Torvalds 		} else
30931da177e4SLinus Torvalds 			dcb->max_command = 1;
30941da177e4SLinus Torvalds 	}
30951da177e4SLinus Torvalds }
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds 
30981da177e4SLinus Torvalds static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
30991da177e4SLinus Torvalds 		struct ScsiInqData *ptr)
31001da177e4SLinus Torvalds {
31011da177e4SLinus Torvalds 	u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
31021da177e4SLinus Torvalds 	dcb->dev_type = bval1;
31031da177e4SLinus Torvalds 	/* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
31041da177e4SLinus Torvalds 	disc_tagq_set(dcb, ptr);
31051da177e4SLinus Torvalds }
31061da177e4SLinus Torvalds 
31071da177e4SLinus Torvalds 
31081da177e4SLinus Torvalds /* unmap mapped pci regions from SRB */
31091da177e4SLinus Torvalds static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
31101da177e4SLinus Torvalds {
31111da177e4SLinus Torvalds 	struct scsi_cmnd *cmd = srb->cmd;
31121da177e4SLinus Torvalds 	enum dma_data_direction dir = cmd->sc_data_direction;
3113a862ea31SFUJITA Tomonori 
3114ece0eeffSSuraj Upadhyay 	if (scsi_sg_count(cmd) && dir != DMA_NONE) {
31151da177e4SLinus Torvalds 		/* unmap DC395x SG list */
31161da177e4SLinus Torvalds 		dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
31171da177e4SLinus Torvalds 			srb->sg_bus_addr, SEGMENTX_LEN);
3118dfda5e21SChristoph Hellwig 		dma_unmap_single(&acb->dev->dev, srb->sg_bus_addr, SEGMENTX_LEN,
3119dfda5e21SChristoph Hellwig 				DMA_TO_DEVICE);
31201da177e4SLinus Torvalds 		dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
3121a862ea31SFUJITA Tomonori 			   scsi_sg_count(cmd), scsi_bufflen(cmd));
31221da177e4SLinus Torvalds 		/* unmap the sg segments */
3123a862ea31SFUJITA Tomonori 		scsi_dma_unmap(cmd);
31241da177e4SLinus Torvalds 	}
31251da177e4SLinus Torvalds }
31261da177e4SLinus Torvalds 
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds /* unmap mapped pci sense buffer from SRB */
31291da177e4SLinus Torvalds static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
31301da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
31311da177e4SLinus Torvalds {
31321da177e4SLinus Torvalds 	if (!(srb->flag & AUTO_REQSENSE))
31331da177e4SLinus Torvalds 		return;
31341da177e4SLinus Torvalds 	/* Unmap sense buffer */
31351da177e4SLinus Torvalds 	dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
31361da177e4SLinus Torvalds 	       srb->segment_x[0].address);
3137dfda5e21SChristoph Hellwig 	dma_unmap_single(&acb->dev->dev, srb->segment_x[0].address,
3138dfda5e21SChristoph Hellwig 			 srb->segment_x[0].length, DMA_FROM_DEVICE);
31391da177e4SLinus Torvalds 	/* Restore SG stuff */
31401da177e4SLinus Torvalds 	srb->total_xfer_length = srb->xferred;
31411da177e4SLinus Torvalds 	srb->segment_x[0].address =
31421da177e4SLinus Torvalds 	    srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
31431da177e4SLinus Torvalds 	srb->segment_x[0].length =
31441da177e4SLinus Torvalds 	    srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
31451da177e4SLinus Torvalds }
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 
31481da177e4SLinus Torvalds /*
31491da177e4SLinus Torvalds  * Complete execution of a SCSI command
31501da177e4SLinus Torvalds  * Signal completion to the generic SCSI driver
31511da177e4SLinus Torvalds  */
31521da177e4SLinus Torvalds static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
31531da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
31541da177e4SLinus Torvalds {
31551da177e4SLinus Torvalds 	u8 tempcnt, status;
31561da177e4SLinus Torvalds 	struct scsi_cmnd *cmd = srb->cmd;
31571da177e4SLinus Torvalds 	enum dma_data_direction dir = cmd->sc_data_direction;
31589dc399deSGuennadi Liakhovetski 	int ckc_only = 1;
31591da177e4SLinus Torvalds 
31605cd049a5SChristoph Hellwig 	dprintkdbg(DBG_1, "srb_done: (0x%p) <%02i-%i>\n", srb->cmd,
31619cb78c16SHannes Reinecke 		srb->cmd->device->id, (u8)srb->cmd->device->lun);
31629dc399deSGuennadi Liakhovetski 	dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n",
3163a862ea31SFUJITA Tomonori 		   srb, scsi_sg_count(cmd), srb->sg_index, srb->sg_count,
3164a862ea31SFUJITA Tomonori 		   scsi_sgtalbe(cmd));
31651da177e4SLinus Torvalds 	status = srb->target_status;
316675dd4439SHannes Reinecke 	set_host_byte(cmd, DID_OK);
316775dd4439SHannes Reinecke 	set_status_byte(cmd, SAM_STAT_GOOD);
31681da177e4SLinus Torvalds 	if (srb->flag & AUTO_REQSENSE) {
31691da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
31701da177e4SLinus Torvalds 		pci_unmap_srb_sense(acb, srb);
31711da177e4SLinus Torvalds 		/*
31721da177e4SLinus Torvalds 		 ** target status..........................
31731da177e4SLinus Torvalds 		 */
31741da177e4SLinus Torvalds 		srb->flag &= ~AUTO_REQSENSE;
31751da177e4SLinus Torvalds 		srb->adapter_status = 0;
317675dd4439SHannes Reinecke 		srb->target_status = SAM_STAT_CHECK_CONDITION;
31771da177e4SLinus Torvalds 		if (debug_enabled(DBG_1)) {
31781da177e4SLinus Torvalds 			switch (cmd->sense_buffer[2] & 0x0f) {
31791da177e4SLinus Torvalds 			case NOT_READY:
31801da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
31811da177e4SLinus Torvalds 				     "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
31821da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
31831da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
31841da177e4SLinus Torvalds 				break;
31851da177e4SLinus Torvalds 			case UNIT_ATTENTION:
31861da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
31871da177e4SLinus Torvalds 				     "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
31881da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
31891da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
31901da177e4SLinus Torvalds 				break;
31911da177e4SLinus Torvalds 			case ILLEGAL_REQUEST:
31921da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
31931da177e4SLinus Torvalds 				     "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
31941da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
31951da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
31961da177e4SLinus Torvalds 				break;
31971da177e4SLinus Torvalds 			case MEDIUM_ERROR:
31981da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
31991da177e4SLinus Torvalds 				     "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
32001da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
32011da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
32021da177e4SLinus Torvalds 				break;
32031da177e4SLinus Torvalds 			case HARDWARE_ERROR:
32041da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
32051da177e4SLinus Torvalds 				     "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
32061da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
32071da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
32081da177e4SLinus Torvalds 				break;
32091da177e4SLinus Torvalds 			}
32101da177e4SLinus Torvalds 			if (cmd->sense_buffer[7] >= 6)
32111da177e4SLinus Torvalds 				printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
32121da177e4SLinus Torvalds 					"(0x%08x 0x%08x)\n",
32131da177e4SLinus Torvalds 					cmd->sense_buffer[2], cmd->sense_buffer[12],
32141da177e4SLinus Torvalds 					cmd->sense_buffer[13],
32151da177e4SLinus Torvalds 					*((unsigned int *)(cmd->sense_buffer + 3)),
32161da177e4SLinus Torvalds 					*((unsigned int *)(cmd->sense_buffer + 8)));
32171da177e4SLinus Torvalds 			else
32181da177e4SLinus Torvalds 				printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
32191da177e4SLinus Torvalds 					cmd->sense_buffer[2],
32201da177e4SLinus Torvalds 					*((unsigned int *)(cmd->sense_buffer + 3)));
32211da177e4SLinus Torvalds 		}
32221da177e4SLinus Torvalds 
322375dd4439SHannes Reinecke 		if (status == SAM_STAT_CHECK_CONDITION) {
322475dd4439SHannes Reinecke 			set_host_byte(cmd, DID_BAD_TARGET);
32251da177e4SLinus Torvalds 			goto ckc_e;
32261da177e4SLinus Torvalds 		}
32271da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
32281da177e4SLinus Torvalds 
322975dd4439SHannes Reinecke 		set_status_byte(cmd, SAM_STAT_CHECK_CONDITION);
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds 		goto ckc_e;
32321da177e4SLinus Torvalds 	}
32331da177e4SLinus Torvalds 
32341da177e4SLinus Torvalds /*************************************************************/
32351da177e4SLinus Torvalds 	if (status) {
32361da177e4SLinus Torvalds 		/*
32371da177e4SLinus Torvalds 		 * target status..........................
32381da177e4SLinus Torvalds 		 */
323975dd4439SHannes Reinecke 		if (status == SAM_STAT_CHECK_CONDITION) {
32401da177e4SLinus Torvalds 			request_sense(acb, dcb, srb);
32411da177e4SLinus Torvalds 			return;
324275dd4439SHannes Reinecke 		} else if (status == SAM_STAT_TASK_SET_FULL) {
32431da177e4SLinus Torvalds 			tempcnt = (u8)list_size(&dcb->srb_going_list);
32441da177e4SLinus Torvalds 			dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
32451da177e4SLinus Torvalds 			     dcb->target_id, dcb->target_lun, tempcnt);
32461da177e4SLinus Torvalds 			if (tempcnt > 1)
32471da177e4SLinus Torvalds 				tempcnt--;
32481da177e4SLinus Torvalds 			dcb->max_command = tempcnt;
32491da177e4SLinus Torvalds 			free_tag(dcb, srb);
3250cd29660dSChristoph Hellwig 			list_move(&srb->list, &dcb->srb_waiting_list);
32511da177e4SLinus Torvalds 			waiting_set_timer(acb, HZ / 20);
32521da177e4SLinus Torvalds 			srb->adapter_status = 0;
32531da177e4SLinus Torvalds 			srb->target_status = 0;
32541da177e4SLinus Torvalds 			return;
32551da177e4SLinus Torvalds 		} else if (status == SCSI_STAT_SEL_TIMEOUT) {
32561da177e4SLinus Torvalds 			srb->adapter_status = H_SEL_TIMEOUT;
32571da177e4SLinus Torvalds 			srb->target_status = 0;
325875dd4439SHannes Reinecke 			set_host_byte(cmd, DID_NO_CONNECT);
32591da177e4SLinus Torvalds 		} else {
32601da177e4SLinus Torvalds 			srb->adapter_status = 0;
326175dd4439SHannes Reinecke 			set_host_byte(cmd, DID_ERROR);
326275dd4439SHannes Reinecke 			set_status_byte(cmd, status);
32631da177e4SLinus Torvalds 		}
32641da177e4SLinus Torvalds 	} else {
32651da177e4SLinus Torvalds 		/*
32661da177e4SLinus Torvalds 		 ** process initiator status..........................
32671da177e4SLinus Torvalds 		 */
32681da177e4SLinus Torvalds 		status = srb->adapter_status;
32691da177e4SLinus Torvalds 		if (status & H_OVER_UNDER_RUN) {
32701da177e4SLinus Torvalds 			srb->target_status = 0;
32715bfaafb6SHannes Reinecke 			scsi_msg_to_host_byte(cmd, srb->end_message);
32721da177e4SLinus Torvalds 		} else if (srb->status & PARITY_ERROR) {
327375dd4439SHannes Reinecke 			set_host_byte(cmd, DID_PARITY);
32741da177e4SLinus Torvalds 		} else {	/* No error */
32751da177e4SLinus Torvalds 
32761da177e4SLinus Torvalds 			srb->adapter_status = 0;
32771da177e4SLinus Torvalds 			srb->target_status = 0;
32781da177e4SLinus Torvalds 		}
32791da177e4SLinus Torvalds 	}
32801da177e4SLinus Torvalds 
32819dc399deSGuennadi Liakhovetski 	ckc_only = 0;
32821da177e4SLinus Torvalds /* Check Error Conditions */
32831da177e4SLinus Torvalds       ckc_e:
32841da177e4SLinus Torvalds 
32853a5bd702SChristoph Hellwig 	pci_unmap_srb(acb, srb);
32863a5bd702SChristoph Hellwig 
32879dc399deSGuennadi Liakhovetski 	if (cmd->cmnd[0] == INQUIRY) {
32889dc399deSGuennadi Liakhovetski 		unsigned char *base = NULL;
32899dc399deSGuennadi Liakhovetski 		struct ScsiInqData *ptr;
32909dc399deSGuennadi Liakhovetski 		unsigned long flags = 0;
3291a862ea31SFUJITA Tomonori 		struct scatterlist* sg = scsi_sglist(cmd);
32929dc399deSGuennadi Liakhovetski 		size_t offset = 0, len = sizeof(struct ScsiInqData);
32939dc399deSGuennadi Liakhovetski 
32949dc399deSGuennadi Liakhovetski 		local_irq_save(flags);
3295a862ea31SFUJITA Tomonori 		base = scsi_kmap_atomic_sg(sg, scsi_sg_count(cmd), &offset, &len);
32969dc399deSGuennadi Liakhovetski 		ptr = (struct ScsiInqData *)(base + offset);
32979dc399deSGuennadi Liakhovetski 
329875dd4439SHannes Reinecke 		if (!ckc_only && get_host_byte(cmd) == DID_OK
3299a862ea31SFUJITA Tomonori 		    && cmd->cmnd[2] == 0 && scsi_bufflen(cmd) >= 8
3300ece0eeffSSuraj Upadhyay 		    && dir != DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
33019dc399deSGuennadi Liakhovetski 			dcb->inquiry7 = ptr->Flags;
33029dc399deSGuennadi Liakhovetski 
33031da177e4SLinus Torvalds 	/*if( srb->cmd->cmnd[0] == INQUIRY && */
33041da177e4SLinus Torvalds 	/*  (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
330575dd4439SHannes Reinecke 		if ((get_host_byte(cmd) == DID_OK) ||
330675dd4439SHannes Reinecke 		    (get_status_byte(cmd) == SAM_STAT_CHECK_CONDITION)) {
33071da177e4SLinus Torvalds 			if (!dcb->init_tcq_flag) {
33081da177e4SLinus Torvalds 				add_dev(acb, dcb, ptr);
33091da177e4SLinus Torvalds 				dcb->init_tcq_flag = 1;
33101da177e4SLinus Torvalds 			}
33111da177e4SLinus Torvalds 		}
33121da177e4SLinus Torvalds 
33139dc399deSGuennadi Liakhovetski 		scsi_kunmap_atomic_sg(base);
33149dc399deSGuennadi Liakhovetski 		local_irq_restore(flags);
33159dc399deSGuennadi Liakhovetski 	}
33161da177e4SLinus Torvalds 
33171da177e4SLinus Torvalds 	/* Here is the info for Doug Gilbert's sg3 ... */
3318a862ea31SFUJITA Tomonori 	scsi_set_resid(cmd, srb->total_xfer_length);
33191da177e4SLinus Torvalds 	/* This may be interpreted by sb. or not ... */
33201da177e4SLinus Torvalds 	cmd->SCp.this_residual = srb->total_xfer_length;
33211da177e4SLinus Torvalds 	cmd->SCp.buffers_residual = 0;
33221da177e4SLinus Torvalds 	if (debug_enabled(DBG_KG)) {
33231da177e4SLinus Torvalds 		if (srb->total_xfer_length)
33245cd049a5SChristoph Hellwig 			dprintkdbg(DBG_KG, "srb_done: (0x%p) <%02i-%i> "
33251da177e4SLinus Torvalds 				"cmnd=0x%02x Missed %i bytes\n",
33269cb78c16SHannes Reinecke 				cmd, cmd->device->id, (u8)cmd->device->lun,
33271da177e4SLinus Torvalds 				cmd->cmnd[0], srb->total_xfer_length);
33281da177e4SLinus Torvalds 	}
33291da177e4SLinus Torvalds 
3330cd29660dSChristoph Hellwig 	if (srb != acb->tmp_srb) {
33311da177e4SLinus Torvalds 		/* Add to free list */
33325cd049a5SChristoph Hellwig 		dprintkdbg(DBG_0, "srb_done: (0x%p) done result=0x%08x\n",
33335cd049a5SChristoph Hellwig 			   cmd, cmd->result);
3334cd29660dSChristoph Hellwig 		list_move_tail(&srb->list, &acb->srb_free_list);
3335cd29660dSChristoph Hellwig 	} else {
3336cd29660dSChristoph Hellwig 		dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
33371da177e4SLinus Torvalds 	}
33381da177e4SLinus Torvalds 
33391da177e4SLinus Torvalds 	cmd->scsi_done(cmd);
33401da177e4SLinus Torvalds 	waiting_process_next(acb);
33411da177e4SLinus Torvalds }
33421da177e4SLinus Torvalds 
33431da177e4SLinus Torvalds 
33441da177e4SLinus Torvalds /* abort all cmds in our queues */
33451da177e4SLinus Torvalds static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
33461da177e4SLinus Torvalds 		struct scsi_cmnd *cmd, u8 force)
33471da177e4SLinus Torvalds {
33481da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
33491da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "doing_srb_done: pids ");
33501da177e4SLinus Torvalds 
33511da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
33521da177e4SLinus Torvalds 		struct ScsiReqBlk *srb;
33531da177e4SLinus Torvalds 		struct ScsiReqBlk *tmp;
33541da177e4SLinus Torvalds 		struct scsi_cmnd *p;
33551da177e4SLinus Torvalds 
33561da177e4SLinus Torvalds 		list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
33571da177e4SLinus Torvalds 			p = srb->cmd;
33585cd049a5SChristoph Hellwig 			printk("G:%p(%02i-%i) ", p,
33599cb78c16SHannes Reinecke 			       p->device->id, (u8)p->device->lun);
3360cd29660dSChristoph Hellwig 			list_del(&srb->list);
33611da177e4SLinus Torvalds 			free_tag(dcb, srb);
3362cd29660dSChristoph Hellwig 			list_add_tail(&srb->list, &acb->srb_free_list);
336375dd4439SHannes Reinecke 			set_host_byte(p, did_flag);
336475dd4439SHannes Reinecke 			set_status_byte(p, SAM_STAT_GOOD);
33651da177e4SLinus Torvalds 			pci_unmap_srb_sense(acb, srb);
33661da177e4SLinus Torvalds 			pci_unmap_srb(acb, srb);
33671da177e4SLinus Torvalds 			if (force) {
33681da177e4SLinus Torvalds 				/* For new EH, we normally don't need to give commands back,
33691da177e4SLinus Torvalds 				 * as they all complete or all time out */
33701da177e4SLinus Torvalds 				p->scsi_done(p);
33711da177e4SLinus Torvalds 			}
33721da177e4SLinus Torvalds 		}
33731da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_going_list))
33741da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
33751da177e4SLinus Torvalds 			       "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
33761da177e4SLinus Torvalds 			       dcb->target_id, dcb->target_lun);
33771da177e4SLinus Torvalds 		if (dcb->tag_mask)
33781da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
33791da177e4SLinus Torvalds 			       "tag_mask for <%02i-%i> should be empty, is %08x!\n",
33801da177e4SLinus Torvalds 			       dcb->target_id, dcb->target_lun,
33811da177e4SLinus Torvalds 			       dcb->tag_mask);
33821da177e4SLinus Torvalds 
33831da177e4SLinus Torvalds 		/* Waiting queue */
33841da177e4SLinus Torvalds 		list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
33851da177e4SLinus Torvalds 			p = srb->cmd;
33861da177e4SLinus Torvalds 
33875cd049a5SChristoph Hellwig 			printk("W:%p<%02i-%i>", p, p->device->id,
33889cb78c16SHannes Reinecke 			       (u8)p->device->lun);
3389cd29660dSChristoph Hellwig 			list_move_tail(&srb->list, &acb->srb_free_list);
339075dd4439SHannes Reinecke 			set_host_byte(p, did_flag);
339175dd4439SHannes Reinecke 			set_status_byte(p, SAM_STAT_GOOD);
33921da177e4SLinus Torvalds 			pci_unmap_srb_sense(acb, srb);
33931da177e4SLinus Torvalds 			pci_unmap_srb(acb, srb);
33941da177e4SLinus Torvalds 			if (force) {
33951da177e4SLinus Torvalds 				/* For new EH, we normally don't need to give commands back,
33961da177e4SLinus Torvalds 				 * as they all complete or all time out */
33971da177e4SLinus Torvalds 				cmd->scsi_done(cmd);
33981da177e4SLinus Torvalds 			}
33991da177e4SLinus Torvalds 		}
34001da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_waiting_list))
34011da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
34021da177e4SLinus Torvalds 			     list_size(&dcb->srb_waiting_list), dcb->target_id,
34031da177e4SLinus Torvalds 			     dcb->target_lun);
34041da177e4SLinus Torvalds 		dcb->flag &= ~ABORT_DEV_;
34051da177e4SLinus Torvalds 	}
34061da177e4SLinus Torvalds 	printk("\n");
34071da177e4SLinus Torvalds }
34081da177e4SLinus Torvalds 
34091da177e4SLinus Torvalds 
34101da177e4SLinus Torvalds static void reset_scsi_bus(struct AdapterCtlBlk *acb)
34111da177e4SLinus Torvalds {
34121da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
34131da177e4SLinus Torvalds 	acb->acb_flag |= RESET_DEV;	/* RESET_DETECT, RESET_DONE, RESET_DEV */
34141da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
34151da177e4SLinus Torvalds 
34161da177e4SLinus Torvalds 	while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
34171da177e4SLinus Torvalds 		/* nothing */;
34181da177e4SLinus Torvalds }
34191da177e4SLinus Torvalds 
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds static void set_basic_config(struct AdapterCtlBlk *acb)
34221da177e4SLinus Torvalds {
34231da177e4SLinus Torvalds 	u8 bval;
34241da177e4SLinus Torvalds 	u16 wval;
34251da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
34261da177e4SLinus Torvalds 	if (acb->config & HCC_PARITY)
34271da177e4SLinus Torvalds 		bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
34281da177e4SLinus Torvalds 	else
34291da177e4SLinus Torvalds 		bval = PHASELATCH | INITIATOR | BLOCKRST;
34301da177e4SLinus Torvalds 
34311da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
34321da177e4SLinus Torvalds 
34331da177e4SLinus Torvalds 	/* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
34341da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03);	/* was 0x13: default */
34351da177e4SLinus Torvalds 	/* program Host ID                  */
34361da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
34371da177e4SLinus Torvalds 	/* set ansynchronous transfer       */
34381da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
34391da177e4SLinus Torvalds 	/* Turn LED control off */
34401da177e4SLinus Torvalds 	wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
34411da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
34421da177e4SLinus Torvalds 	/* DMA config          */
34431da177e4SLinus Torvalds 	wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
34441da177e4SLinus Torvalds 	wval |=
34451da177e4SLinus Torvalds 	    DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
34461da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
34471da177e4SLinus Torvalds 	/* Clear pending interrupt status */
34481da177e4SLinus Torvalds 	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
34491da177e4SLinus Torvalds 	/* Enable SCSI interrupt    */
34501da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
34511da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
34521da177e4SLinus Torvalds 		      /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
34531da177e4SLinus Torvalds 		      );
34541da177e4SLinus Torvalds }
34551da177e4SLinus Torvalds 
34561da177e4SLinus Torvalds 
34571da177e4SLinus Torvalds static void scsi_reset_detect(struct AdapterCtlBlk *acb)
34581da177e4SLinus Torvalds {
34591da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
34601da177e4SLinus Torvalds 	/* delay half a second */
34611da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
34621da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
34631da177e4SLinus Torvalds 
34641da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
34651da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
34661da177e4SLinus Torvalds 	/*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
34671da177e4SLinus Torvalds 	udelay(500);
34681da177e4SLinus Torvalds 	/* Maybe we locked up the bus? Then lets wait even longer ... */
346953a8936cSHannes Reinecke 	acb->last_reset =
34701da177e4SLinus Torvalds 	    jiffies + 5 * HZ / 2 +
34711da177e4SLinus Torvalds 	    HZ * acb->eeprom.delay_time;
34721da177e4SLinus Torvalds 
34731da177e4SLinus Torvalds 	clear_fifo(acb, "scsi_reset_detect");
34741da177e4SLinus Torvalds 	set_basic_config(acb);
34751da177e4SLinus Torvalds 	/*1.25 */
34761da177e4SLinus Torvalds 	/*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
34771da177e4SLinus Torvalds 
34781da177e4SLinus Torvalds 	if (acb->acb_flag & RESET_DEV) {	/* RESET_DETECT, RESET_DONE, RESET_DEV */
34791da177e4SLinus Torvalds 		acb->acb_flag |= RESET_DONE;
34801da177e4SLinus Torvalds 	} else {
34811da177e4SLinus Torvalds 		acb->acb_flag |= RESET_DETECT;
34821da177e4SLinus Torvalds 		reset_dev_param(acb);
34831da177e4SLinus Torvalds 		doing_srb_done(acb, DID_RESET, NULL, 1);
34841da177e4SLinus Torvalds 		/*DC395x_RecoverSRB( acb ); */
34851da177e4SLinus Torvalds 		acb->active_dcb = NULL;
34861da177e4SLinus Torvalds 		acb->acb_flag = 0;
34871da177e4SLinus Torvalds 		waiting_process_next(acb);
34881da177e4SLinus Torvalds 	}
34891da177e4SLinus Torvalds }
34901da177e4SLinus Torvalds 
34911da177e4SLinus Torvalds 
34921da177e4SLinus Torvalds static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
34931da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
34941da177e4SLinus Torvalds {
34951da177e4SLinus Torvalds 	struct scsi_cmnd *cmd = srb->cmd;
34965cd049a5SChristoph Hellwig 	dprintkdbg(DBG_1, "request_sense: (0x%p) <%02i-%i>\n",
34979cb78c16SHannes Reinecke 		cmd, cmd->device->id, (u8)cmd->device->lun);
34981da177e4SLinus Torvalds 
34991da177e4SLinus Torvalds 	srb->flag |= AUTO_REQSENSE;
35001da177e4SLinus Torvalds 	srb->adapter_status = 0;
35011da177e4SLinus Torvalds 	srb->target_status = 0;
35021da177e4SLinus Torvalds 
35031da177e4SLinus Torvalds 	/* KG: Can this prevent crap sense data ? */
3504b80ca4f7SFUJITA Tomonori 	memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
35051da177e4SLinus Torvalds 
35061da177e4SLinus Torvalds 	/* Save some data */
35071da177e4SLinus Torvalds 	srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
35081da177e4SLinus Torvalds 	    srb->segment_x[0].address;
35091da177e4SLinus Torvalds 	srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
35101da177e4SLinus Torvalds 	    srb->segment_x[0].length;
35111da177e4SLinus Torvalds 	srb->xferred = srb->total_xfer_length;
35121da177e4SLinus Torvalds 	/* srb->segment_x : a one entry of S/G list table */
3513b80ca4f7SFUJITA Tomonori 	srb->total_xfer_length = SCSI_SENSE_BUFFERSIZE;
3514b80ca4f7SFUJITA Tomonori 	srb->segment_x[0].length = SCSI_SENSE_BUFFERSIZE;
35151da177e4SLinus Torvalds 	/* Map sense buffer */
3516dfda5e21SChristoph Hellwig 	srb->segment_x[0].address = dma_map_single(&acb->dev->dev,
3517dfda5e21SChristoph Hellwig 			cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
3518dfda5e21SChristoph Hellwig 			DMA_FROM_DEVICE);
35191da177e4SLinus Torvalds 	dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
35201da177e4SLinus Torvalds 	       cmd->sense_buffer, srb->segment_x[0].address,
3521b80ca4f7SFUJITA Tomonori 	       SCSI_SENSE_BUFFERSIZE);
35221da177e4SLinus Torvalds 	srb->sg_count = 1;
35231da177e4SLinus Torvalds 	srb->sg_index = 0;
35241da177e4SLinus Torvalds 
35251da177e4SLinus Torvalds 	if (start_scsi(acb, dcb, srb)) {	/* Should only happen, if sb. else grabs the bus */
35261da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
35275cd049a5SChristoph Hellwig 			"request_sense: (0x%p) failed <%02i-%i>\n",
35285cd049a5SChristoph Hellwig 			srb->cmd, dcb->target_id, dcb->target_lun);
3529cd29660dSChristoph Hellwig 		list_move(&srb->list, &dcb->srb_waiting_list);
35301da177e4SLinus Torvalds 		waiting_set_timer(acb, HZ / 100);
35311da177e4SLinus Torvalds 	}
35321da177e4SLinus Torvalds }
35331da177e4SLinus Torvalds 
35341da177e4SLinus Torvalds 
35351da177e4SLinus Torvalds /**
35361da177e4SLinus Torvalds  * device_alloc - Allocate a new device instance. This create the
35371da177e4SLinus Torvalds  * devices instance and sets up all the data items. The adapter
35381da177e4SLinus Torvalds  * instance is required to obtain confiuration information for this
35391da177e4SLinus Torvalds  * device. This does *not* add this device to the adapters device
35401da177e4SLinus Torvalds  * list.
35411da177e4SLinus Torvalds  *
35421da177e4SLinus Torvalds  * @acb: The adapter to obtain configuration information from.
35431da177e4SLinus Torvalds  * @target: The target for the new device.
35441da177e4SLinus Torvalds  * @lun: The lun for the new device.
35451da177e4SLinus Torvalds  *
3546d6e05edcSAndreas Mohr  * Return the new device if successful or NULL on failure.
35471da177e4SLinus Torvalds  **/
35481da177e4SLinus Torvalds static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
35491da177e4SLinus Torvalds 		u8 target, u8 lun)
35501da177e4SLinus Torvalds {
35511da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
35521da177e4SLinus Torvalds 	u8 period_index = eeprom->target[target].period & 0x07;
35531da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
35541da177e4SLinus Torvalds 
35551da177e4SLinus Torvalds 	dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
35561da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
35571da177e4SLinus Torvalds 	if (!dcb)
35581da177e4SLinus Torvalds 		return NULL;
35591da177e4SLinus Torvalds 	dcb->acb = NULL;
35601da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dcb->srb_going_list);
35611da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dcb->srb_waiting_list);
35621da177e4SLinus Torvalds 	dcb->active_srb = NULL;
35631da177e4SLinus Torvalds 	dcb->tag_mask = 0;
35641da177e4SLinus Torvalds 	dcb->max_command = 1;
35651da177e4SLinus Torvalds 	dcb->target_id = target;
35661da177e4SLinus Torvalds 	dcb->target_lun = lun;
3567208afec4SDan Carpenter 	dcb->dev_mode = eeprom->target[target].cfg0;
35681da177e4SLinus Torvalds #ifndef DC395x_NO_DISCONNECT
35691da177e4SLinus Torvalds 	dcb->identify_msg =
35701da177e4SLinus Torvalds 	    IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
35711da177e4SLinus Torvalds #else
35721da177e4SLinus Torvalds 	dcb->identify_msg = IDENTIFY(0, lun);
35731da177e4SLinus Torvalds #endif
35741da177e4SLinus Torvalds 	dcb->inquiry7 = 0;
35751da177e4SLinus Torvalds 	dcb->sync_mode = 0;
35761da177e4SLinus Torvalds 	dcb->min_nego_period = clock_period[period_index];
35771da177e4SLinus Torvalds 	dcb->sync_period = 0;
35781da177e4SLinus Torvalds 	dcb->sync_offset = 0;
35791da177e4SLinus Torvalds 	dcb->flag = 0;
35801da177e4SLinus Torvalds 
35811da177e4SLinus Torvalds #ifndef DC395x_NO_WIDE
35821da177e4SLinus Torvalds 	if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
35831da177e4SLinus Torvalds 	    && (acb->config & HCC_WIDE_CARD))
35841da177e4SLinus Torvalds 		dcb->sync_mode |= WIDE_NEGO_ENABLE;
35851da177e4SLinus Torvalds #endif
35861da177e4SLinus Torvalds #ifndef DC395x_NO_SYNC
35871da177e4SLinus Torvalds 	if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
35881da177e4SLinus Torvalds 		if (!(lun) || current_sync_offset)
35891da177e4SLinus Torvalds 			dcb->sync_mode |= SYNC_NEGO_ENABLE;
35901da177e4SLinus Torvalds #endif
35911da177e4SLinus Torvalds 	if (dcb->target_lun != 0) {
35921da177e4SLinus Torvalds 		/* Copy settings */
35931da177e4SLinus Torvalds 		struct DeviceCtlBlk *p;
35941da177e4SLinus Torvalds 		list_for_each_entry(p, &acb->dcb_list, list)
35951da177e4SLinus Torvalds 			if (p->target_id == dcb->target_id)
35961da177e4SLinus Torvalds 				break;
35971da177e4SLinus Torvalds 		dprintkdbg(DBG_1,
35981da177e4SLinus Torvalds 		       "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
35991da177e4SLinus Torvalds 		       dcb->target_id, dcb->target_lun,
36001da177e4SLinus Torvalds 		       p->target_id, p->target_lun);
36011da177e4SLinus Torvalds 		dcb->sync_mode = p->sync_mode;
36021da177e4SLinus Torvalds 		dcb->sync_period = p->sync_period;
36031da177e4SLinus Torvalds 		dcb->min_nego_period = p->min_nego_period;
36041da177e4SLinus Torvalds 		dcb->sync_offset = p->sync_offset;
36051da177e4SLinus Torvalds 		dcb->inquiry7 = p->inquiry7;
36061da177e4SLinus Torvalds 	}
36071da177e4SLinus Torvalds 	return dcb;
36081da177e4SLinus Torvalds }
36091da177e4SLinus Torvalds 
36101da177e4SLinus Torvalds 
36111da177e4SLinus Torvalds /**
36121da177e4SLinus Torvalds  * adapter_add_device - Adds the device instance to the adaptor instance.
36131da177e4SLinus Torvalds  *
36141da177e4SLinus Torvalds  * @acb: The adapter device to be updated
3615b595076aSUwe Kleine-König  * @dcb: A newly created and initialised device instance to add.
36161da177e4SLinus Torvalds  **/
36171da177e4SLinus Torvalds static void adapter_add_device(struct AdapterCtlBlk *acb,
36181da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
36191da177e4SLinus Torvalds {
36201da177e4SLinus Torvalds 	/* backpointer to adapter */
36211da177e4SLinus Torvalds 	dcb->acb = acb;
36221da177e4SLinus Torvalds 
36231da177e4SLinus Torvalds 	/* set run_robin to this device if it is currently empty */
36241da177e4SLinus Torvalds 	if (list_empty(&acb->dcb_list))
36251da177e4SLinus Torvalds 		acb->dcb_run_robin = dcb;
36261da177e4SLinus Torvalds 
36271da177e4SLinus Torvalds 	/* add device to list */
36281da177e4SLinus Torvalds 	list_add_tail(&dcb->list, &acb->dcb_list);
36291da177e4SLinus Torvalds 
36301da177e4SLinus Torvalds 	/* update device maps */
36311da177e4SLinus Torvalds 	acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
36321da177e4SLinus Torvalds 	acb->children[dcb->target_id][dcb->target_lun] = dcb;
36331da177e4SLinus Torvalds }
36341da177e4SLinus Torvalds 
36351da177e4SLinus Torvalds 
36361da177e4SLinus Torvalds /**
36371da177e4SLinus Torvalds  * adapter_remove_device - Removes the device instance from the adaptor
36381da177e4SLinus Torvalds  * instance. The device instance is not check in any way or freed by this.
36391da177e4SLinus Torvalds  * The caller is expected to take care of that. This will simply remove the
36401da177e4SLinus Torvalds  * device from the adapters data strcutures.
36411da177e4SLinus Torvalds  *
36421da177e4SLinus Torvalds  * @acb: The adapter device to be updated
36431da177e4SLinus Torvalds  * @dcb: A device that has previously been added to the adapter.
36441da177e4SLinus Torvalds  **/
36451da177e4SLinus Torvalds static void adapter_remove_device(struct AdapterCtlBlk *acb,
36461da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
36471da177e4SLinus Torvalds {
36481da177e4SLinus Torvalds 	struct DeviceCtlBlk *i;
36491da177e4SLinus Torvalds 	struct DeviceCtlBlk *tmp;
36501da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
36511da177e4SLinus Torvalds 		dcb->target_id, dcb->target_lun);
36521da177e4SLinus Torvalds 
36531da177e4SLinus Torvalds 	/* fix up any pointers to this device that we have in the adapter */
36541da177e4SLinus Torvalds 	if (acb->active_dcb == dcb)
36551da177e4SLinus Torvalds 		acb->active_dcb = NULL;
36561da177e4SLinus Torvalds 	if (acb->dcb_run_robin == dcb)
36571da177e4SLinus Torvalds 		acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
36581da177e4SLinus Torvalds 
36591da177e4SLinus Torvalds 	/* unlink from list */
36601da177e4SLinus Torvalds 	list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
36611da177e4SLinus Torvalds 		if (dcb == i) {
36621da177e4SLinus Torvalds 			list_del(&i->list);
36631da177e4SLinus Torvalds 			break;
36641da177e4SLinus Torvalds 		}
36651da177e4SLinus Torvalds 
36661da177e4SLinus Torvalds 	/* clear map and children */
36671da177e4SLinus Torvalds 	acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
36681da177e4SLinus Torvalds 	acb->children[dcb->target_id][dcb->target_lun] = NULL;
36691da177e4SLinus Torvalds 	dcb->acb = NULL;
36701da177e4SLinus Torvalds }
36711da177e4SLinus Torvalds 
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds /**
36741da177e4SLinus Torvalds  * adapter_remove_and_free_device - Removes a single device from the adapter
36751da177e4SLinus Torvalds  * and then frees the device information.
36761da177e4SLinus Torvalds  *
36771da177e4SLinus Torvalds  * @acb: The adapter device to be updated
36781da177e4SLinus Torvalds  * @dcb: A device that has previously been added to the adapter.
36791da177e4SLinus Torvalds  */
36801da177e4SLinus Torvalds static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
36811da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
36821da177e4SLinus Torvalds {
36831da177e4SLinus Torvalds 	if (list_size(&dcb->srb_going_list) > 1) {
36841da177e4SLinus Torvalds 		dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
36851da177e4SLinus Torvalds 		           "Won't remove because of %i active requests.\n",
36861da177e4SLinus Torvalds 			   dcb->target_id, dcb->target_lun,
36871da177e4SLinus Torvalds 			   list_size(&dcb->srb_going_list));
36881da177e4SLinus Torvalds 		return;
36891da177e4SLinus Torvalds 	}
36901da177e4SLinus Torvalds 	adapter_remove_device(acb, dcb);
36911da177e4SLinus Torvalds 	kfree(dcb);
36921da177e4SLinus Torvalds }
36931da177e4SLinus Torvalds 
36941da177e4SLinus Torvalds 
36951da177e4SLinus Torvalds /**
36961da177e4SLinus Torvalds  * adapter_remove_and_free_all_devices - Removes and frees all of the
36971da177e4SLinus Torvalds  * devices associated with the specified adapter.
36981da177e4SLinus Torvalds  *
36991da177e4SLinus Torvalds  * @acb: The adapter from which all devices should be removed.
37001da177e4SLinus Torvalds  **/
37011da177e4SLinus Torvalds static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
37021da177e4SLinus Torvalds {
37031da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
37041da177e4SLinus Torvalds 	struct DeviceCtlBlk *tmp;
37051da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
37061da177e4SLinus Torvalds 		   list_size(&acb->dcb_list));
37071da177e4SLinus Torvalds 
37081da177e4SLinus Torvalds 	list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
37091da177e4SLinus Torvalds 		adapter_remove_and_free_device(acb, dcb);
37101da177e4SLinus Torvalds }
37111da177e4SLinus Torvalds 
37121da177e4SLinus Torvalds 
37131da177e4SLinus Torvalds /**
37141da177e4SLinus Torvalds  * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
37151da177e4SLinus Torvalds  * scsi device that we need to deal with. We allocate a new device and then
37161da177e4SLinus Torvalds  * insert that device into the adapters device list.
37171da177e4SLinus Torvalds  *
37181da177e4SLinus Torvalds  * @scsi_device: The new scsi device that we need to handle.
37191da177e4SLinus Torvalds  **/
37201da177e4SLinus Torvalds static int dc395x_slave_alloc(struct scsi_device *scsi_device)
37211da177e4SLinus Torvalds {
37221da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
37231da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
37241da177e4SLinus Torvalds 
37251da177e4SLinus Torvalds 	dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
37261da177e4SLinus Torvalds 	if (!dcb)
37271da177e4SLinus Torvalds 		return -ENOMEM;
37281da177e4SLinus Torvalds 	adapter_add_device(acb, dcb);
37291da177e4SLinus Torvalds 
37301da177e4SLinus Torvalds 	return 0;
37311da177e4SLinus Torvalds }
37321da177e4SLinus Torvalds 
37331da177e4SLinus Torvalds 
37341da177e4SLinus Torvalds /**
37351da177e4SLinus Torvalds  * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
37361da177e4SLinus Torvalds  * device that is going away.
37371da177e4SLinus Torvalds  *
37381da177e4SLinus Torvalds  * @scsi_device: The new scsi device that we need to handle.
37391da177e4SLinus Torvalds  **/
37401da177e4SLinus Torvalds static void dc395x_slave_destroy(struct scsi_device *scsi_device)
37411da177e4SLinus Torvalds {
37421da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
37431da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
37441da177e4SLinus Torvalds 	if (dcb)
37451da177e4SLinus Torvalds 		adapter_remove_and_free_device(acb, dcb);
37461da177e4SLinus Torvalds }
37471da177e4SLinus Torvalds 
37481da177e4SLinus Torvalds 
37491da177e4SLinus Torvalds 
37501da177e4SLinus Torvalds 
37511da177e4SLinus Torvalds /**
37521da177e4SLinus Torvalds  * trms1040_wait_30us: wait for 30 us
37531da177e4SLinus Torvalds  *
37541da177e4SLinus Torvalds  * Waits for 30us (using the chip by the looks of it..)
37551da177e4SLinus Torvalds  *
37561da177e4SLinus Torvalds  * @io_port: base I/O address
37571da177e4SLinus Torvalds  **/
37586f039790SGreg Kroah-Hartman static void trms1040_wait_30us(unsigned long io_port)
37591da177e4SLinus Torvalds {
37601da177e4SLinus Torvalds 	/* ScsiPortStallExecution(30); wait 30 us */
37611da177e4SLinus Torvalds 	outb(5, io_port + TRM_S1040_GEN_TIMER);
37621da177e4SLinus Torvalds 	while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
37631da177e4SLinus Torvalds 		/* nothing */ ;
37641da177e4SLinus Torvalds }
37651da177e4SLinus Torvalds 
37661da177e4SLinus Torvalds 
37671da177e4SLinus Torvalds /**
37681da177e4SLinus Torvalds  * trms1040_write_cmd - write the secified command and address to
37691da177e4SLinus Torvalds  * chip
37701da177e4SLinus Torvalds  *
37711da177e4SLinus Torvalds  * @io_port:	base I/O address
37721da177e4SLinus Torvalds  * @cmd:	SB + op code (command) to send
37731da177e4SLinus Torvalds  * @addr:	address to send
37741da177e4SLinus Torvalds  **/
37756f039790SGreg Kroah-Hartman static void trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
37761da177e4SLinus Torvalds {
37771da177e4SLinus Torvalds 	int i;
37781da177e4SLinus Torvalds 	u8 send_data;
37791da177e4SLinus Torvalds 
37801da177e4SLinus Torvalds 	/* program SB + OP code */
37811da177e4SLinus Torvalds 	for (i = 0; i < 3; i++, cmd <<= 1) {
37821da177e4SLinus Torvalds 		send_data = NVR_SELECT;
37831da177e4SLinus Torvalds 		if (cmd & 0x04)	/* Start from bit 2 */
37841da177e4SLinus Torvalds 			send_data |= NVR_BITOUT;
37851da177e4SLinus Torvalds 
37861da177e4SLinus Torvalds 		outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
37871da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
37881da177e4SLinus Torvalds 		outb((send_data | NVR_CLOCK),
37891da177e4SLinus Torvalds 		     io_port + TRM_S1040_GEN_NVRAM);
37901da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
37911da177e4SLinus Torvalds 	}
37921da177e4SLinus Torvalds 
37931da177e4SLinus Torvalds 	/* send address */
37941da177e4SLinus Torvalds 	for (i = 0; i < 7; i++, addr <<= 1) {
37951da177e4SLinus Torvalds 		send_data = NVR_SELECT;
37961da177e4SLinus Torvalds 		if (addr & 0x40)	/* Start from bit 6 */
37971da177e4SLinus Torvalds 			send_data |= NVR_BITOUT;
37981da177e4SLinus Torvalds 
37991da177e4SLinus Torvalds 		outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
38001da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38011da177e4SLinus Torvalds 		outb((send_data | NVR_CLOCK),
38021da177e4SLinus Torvalds 		     io_port + TRM_S1040_GEN_NVRAM);
38031da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38041da177e4SLinus Torvalds 	}
38051da177e4SLinus Torvalds 	outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
38061da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
38071da177e4SLinus Torvalds }
38081da177e4SLinus Torvalds 
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds /**
38111da177e4SLinus Torvalds  * trms1040_set_data - store a single byte in the eeprom
38121da177e4SLinus Torvalds  *
38131da177e4SLinus Torvalds  * Called from write all to write a single byte into the SSEEPROM
38141da177e4SLinus Torvalds  * Which is done one bit at a time.
38151da177e4SLinus Torvalds  *
38161da177e4SLinus Torvalds  * @io_port:	base I/O address
38171da177e4SLinus Torvalds  * @addr:	offset into EEPROM
38181da177e4SLinus Torvalds  * @byte:	bytes to write
38191da177e4SLinus Torvalds  **/
38206f039790SGreg Kroah-Hartman static void trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
38211da177e4SLinus Torvalds {
38221da177e4SLinus Torvalds 	int i;
38231da177e4SLinus Torvalds 	u8 send_data;
38241da177e4SLinus Torvalds 
38251da177e4SLinus Torvalds 	/* Send write command & address */
38261da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x05, addr);
38271da177e4SLinus Torvalds 
38281da177e4SLinus Torvalds 	/* Write data */
38291da177e4SLinus Torvalds 	for (i = 0; i < 8; i++, byte <<= 1) {
38301da177e4SLinus Torvalds 		send_data = NVR_SELECT;
38311da177e4SLinus Torvalds 		if (byte & 0x80)	/* Start from bit 7 */
38321da177e4SLinus Torvalds 			send_data |= NVR_BITOUT;
38331da177e4SLinus Torvalds 
38341da177e4SLinus Torvalds 		outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
38351da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38361da177e4SLinus Torvalds 		outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
38371da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38381da177e4SLinus Torvalds 	}
38391da177e4SLinus Torvalds 	outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
38401da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
38411da177e4SLinus Torvalds 
38421da177e4SLinus Torvalds 	/* Disable chip select */
38431da177e4SLinus Torvalds 	outb(0, 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 	/* Wait for write ready */
38501da177e4SLinus Torvalds 	while (1) {
38511da177e4SLinus Torvalds 		outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
38521da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38531da177e4SLinus Torvalds 
38541da177e4SLinus Torvalds 		outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
38551da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
38561da177e4SLinus Torvalds 
38571da177e4SLinus Torvalds 		if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
38581da177e4SLinus Torvalds 			break;
38591da177e4SLinus Torvalds 	}
38601da177e4SLinus Torvalds 
38611da177e4SLinus Torvalds 	/*  Disable chip select */
38621da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
38631da177e4SLinus Torvalds }
38641da177e4SLinus Torvalds 
38651da177e4SLinus Torvalds 
38661da177e4SLinus Torvalds /**
38671da177e4SLinus Torvalds  * trms1040_write_all - write 128 bytes to the eeprom
38681da177e4SLinus Torvalds  *
38691da177e4SLinus Torvalds  * Write the supplied 128 bytes to the chips SEEPROM
38701da177e4SLinus Torvalds  *
38711da177e4SLinus Torvalds  * @eeprom:	the data to write
38721da177e4SLinus Torvalds  * @io_port:	the base io port
38731da177e4SLinus Torvalds  **/
38746f039790SGreg Kroah-Hartman static void trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
38751da177e4SLinus Torvalds {
38761da177e4SLinus Torvalds 	u8 *b_eeprom = (u8 *)eeprom;
38771da177e4SLinus Torvalds 	u8 addr;
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds 	/* Enable SEEPROM */
38801da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
38811da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
38821da177e4SLinus Torvalds 
38831da177e4SLinus Torvalds 	/* write enable */
38841da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x04, 0xFF);
38851da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
38861da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
38871da177e4SLinus Torvalds 
38881da177e4SLinus Torvalds 	/* write */
38891da177e4SLinus Torvalds 	for (addr = 0; addr < 128; addr++, b_eeprom++)
38901da177e4SLinus Torvalds 		trms1040_set_data(io_port, addr, *b_eeprom);
38911da177e4SLinus Torvalds 
38921da177e4SLinus Torvalds 	/* write disable */
38931da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x04, 0x00);
38941da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
38951da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
38961da177e4SLinus Torvalds 
38971da177e4SLinus Torvalds 	/* Disable SEEPROM */
38981da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
38991da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
39001da177e4SLinus Torvalds }
39011da177e4SLinus Torvalds 
39021da177e4SLinus Torvalds 
39031da177e4SLinus Torvalds /**
39041da177e4SLinus Torvalds  * trms1040_get_data - get a single byte from the eeprom
39051da177e4SLinus Torvalds  *
39061da177e4SLinus Torvalds  * Called from read all to read a single byte into the SSEEPROM
39071da177e4SLinus Torvalds  * Which is done one bit at a time.
39081da177e4SLinus Torvalds  *
39091da177e4SLinus Torvalds  * @io_port:	base I/O address
39101da177e4SLinus Torvalds  * @addr:	offset into SEEPROM
39111da177e4SLinus Torvalds  *
391259c51591SMichael Opdenacker  * Returns the byte read.
39131da177e4SLinus Torvalds  **/
39146f039790SGreg Kroah-Hartman static u8 trms1040_get_data(unsigned long io_port, u8 addr)
39151da177e4SLinus Torvalds {
39161da177e4SLinus Torvalds 	int i;
39171da177e4SLinus Torvalds 	u8 read_byte;
39181da177e4SLinus Torvalds 	u8 result = 0;
39191da177e4SLinus Torvalds 
39201da177e4SLinus Torvalds 	/* Send read command & address */
39211da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x06, addr);
39221da177e4SLinus Torvalds 
39231da177e4SLinus Torvalds 	/* read data */
39241da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
39251da177e4SLinus Torvalds 		outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
39261da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
39271da177e4SLinus Torvalds 		outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
39281da177e4SLinus Torvalds 
39291da177e4SLinus Torvalds 		/* Get data bit while falling edge */
39301da177e4SLinus Torvalds 		read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
39311da177e4SLinus Torvalds 		result <<= 1;
39321da177e4SLinus Torvalds 		if (read_byte & NVR_BITIN)
39331da177e4SLinus Torvalds 			result |= 1;
39341da177e4SLinus Torvalds 
39351da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
39361da177e4SLinus Torvalds 	}
39371da177e4SLinus Torvalds 
39381da177e4SLinus Torvalds 	/* Disable chip select */
39391da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
39401da177e4SLinus Torvalds 	return result;
39411da177e4SLinus Torvalds }
39421da177e4SLinus Torvalds 
39431da177e4SLinus Torvalds 
39441da177e4SLinus Torvalds /**
39451da177e4SLinus Torvalds  * trms1040_read_all - read all bytes from the eeprom
39461da177e4SLinus Torvalds  *
39471da177e4SLinus Torvalds  * Read the 128 bytes from the SEEPROM.
39481da177e4SLinus Torvalds  *
39491da177e4SLinus Torvalds  * @eeprom:	where to store the data
39501da177e4SLinus Torvalds  * @io_port:	the base io port
39511da177e4SLinus Torvalds  **/
39526f039790SGreg Kroah-Hartman static void trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
39531da177e4SLinus Torvalds {
39541da177e4SLinus Torvalds 	u8 *b_eeprom = (u8 *)eeprom;
39551da177e4SLinus Torvalds 	u8 addr;
39561da177e4SLinus Torvalds 
39571da177e4SLinus Torvalds 	/* Enable SEEPROM */
39581da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
39591da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
39601da177e4SLinus Torvalds 
39611da177e4SLinus Torvalds 	/* read details */
39621da177e4SLinus Torvalds 	for (addr = 0; addr < 128; addr++, b_eeprom++)
39631da177e4SLinus Torvalds 		*b_eeprom = trms1040_get_data(io_port, addr);
39641da177e4SLinus Torvalds 
39651da177e4SLinus Torvalds 	/* Disable SEEPROM */
39661da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
39671da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
39681da177e4SLinus Torvalds }
39691da177e4SLinus Torvalds 
39701da177e4SLinus Torvalds 
39711da177e4SLinus Torvalds 
39721da177e4SLinus Torvalds /**
39731da177e4SLinus Torvalds  * check_eeprom - get and check contents of the eeprom
39741da177e4SLinus Torvalds  *
39751da177e4SLinus Torvalds  * Read seeprom 128 bytes into the memory provider in eeprom.
39761da177e4SLinus Torvalds  * Checks the checksum and if it's not correct it uses a set of default
39771da177e4SLinus Torvalds  * values.
39781da177e4SLinus Torvalds  *
39791da177e4SLinus Torvalds  * @eeprom:	caller allocated strcuture to read the eeprom data into
39801da177e4SLinus Torvalds  * @io_port:	io port to read from
39811da177e4SLinus Torvalds  **/
39826f039790SGreg Kroah-Hartman static void check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
39831da177e4SLinus Torvalds {
39841da177e4SLinus Torvalds 	u16 *w_eeprom = (u16 *)eeprom;
39851da177e4SLinus Torvalds 	u16 w_addr;
39861da177e4SLinus Torvalds 	u16 cksum;
39871da177e4SLinus Torvalds 	u32 d_addr;
39881da177e4SLinus Torvalds 	u32 *d_eeprom;
39891da177e4SLinus Torvalds 
39901da177e4SLinus Torvalds 	trms1040_read_all(eeprom, io_port);	/* read eeprom */
39911da177e4SLinus Torvalds 
39921da177e4SLinus Torvalds 	cksum = 0;
39931da177e4SLinus Torvalds 	for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
39941da177e4SLinus Torvalds 	     w_addr++, w_eeprom++)
39951da177e4SLinus Torvalds 		cksum += *w_eeprom;
39961da177e4SLinus Torvalds 	if (cksum != 0x1234) {
39971da177e4SLinus Torvalds 		/*
39981da177e4SLinus Torvalds 		 * Checksum is wrong.
39991da177e4SLinus Torvalds 		 * Load a set of defaults into the eeprom buffer
40001da177e4SLinus Torvalds 		 */
40011da177e4SLinus Torvalds 		dprintkl(KERN_WARNING,
40021da177e4SLinus Torvalds 			"EEProm checksum error: using default values and options.\n");
40031da177e4SLinus Torvalds 		eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
40041da177e4SLinus Torvalds 		eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
40051da177e4SLinus Torvalds 		eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
40061da177e4SLinus Torvalds 		eeprom->sub_sys_id[1] =
40071da177e4SLinus Torvalds 		    (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
40081da177e4SLinus Torvalds 		eeprom->sub_class = 0x00;
40091da177e4SLinus Torvalds 		eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
40101da177e4SLinus Torvalds 		eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
40111da177e4SLinus Torvalds 		eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
40121da177e4SLinus Torvalds 		eeprom->device_id[1] =
40131da177e4SLinus Torvalds 		    (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
40141da177e4SLinus Torvalds 		eeprom->reserved = 0x00;
40151da177e4SLinus Torvalds 
40161da177e4SLinus Torvalds 		for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
40171da177e4SLinus Torvalds 		     d_addr < 16; d_addr++, d_eeprom++)
40181da177e4SLinus Torvalds 			*d_eeprom = 0x00000077;	/* cfg3,cfg2,period,cfg0 */
40191da177e4SLinus Torvalds 
40201da177e4SLinus Torvalds 		*d_eeprom++ = 0x04000F07;	/* max_tag,delay_time,channel_cfg,scsi_id */
40211da177e4SLinus Torvalds 		*d_eeprom++ = 0x00000015;	/* reserved1,boot_lun,boot_target,reserved0 */
40221da177e4SLinus Torvalds 		for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
40231da177e4SLinus Torvalds 			*d_eeprom = 0x00;
40241da177e4SLinus Torvalds 
40251da177e4SLinus Torvalds 		/* Now load defaults (maybe set by boot/module params) */
40261da177e4SLinus Torvalds 		set_safe_settings();
40271da177e4SLinus Torvalds 		fix_settings();
40281da177e4SLinus Torvalds 		eeprom_override(eeprom);
40291da177e4SLinus Torvalds 
40301da177e4SLinus Torvalds 		eeprom->cksum = 0x00;
40311da177e4SLinus Torvalds 		for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
40321da177e4SLinus Torvalds 		     w_addr < 63; w_addr++, w_eeprom++)
40331da177e4SLinus Torvalds 			cksum += *w_eeprom;
40341da177e4SLinus Torvalds 
40351da177e4SLinus Torvalds 		*w_eeprom = 0x1234 - cksum;
40361da177e4SLinus Torvalds 		trms1040_write_all(eeprom, io_port);
40371da177e4SLinus Torvalds 		eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
40381da177e4SLinus Torvalds 	} else {
40391da177e4SLinus Torvalds 		set_safe_settings();
40401da177e4SLinus Torvalds 		eeprom_index_to_delay(eeprom);
40411da177e4SLinus Torvalds 		eeprom_override(eeprom);
40421da177e4SLinus Torvalds 	}
40431da177e4SLinus Torvalds }
40441da177e4SLinus Torvalds 
40451da177e4SLinus Torvalds 
40461da177e4SLinus Torvalds /**
40471da177e4SLinus Torvalds  * print_eeprom_settings - output the eeprom settings
40481da177e4SLinus Torvalds  * to the kernel log so people can see what they were.
40491da177e4SLinus Torvalds  *
40501da177e4SLinus Torvalds  * @eeprom: The eeprom data strucutre to show details for.
40511da177e4SLinus Torvalds  **/
40526f039790SGreg Kroah-Hartman static void print_eeprom_settings(struct NvRamType *eeprom)
40531da177e4SLinus Torvalds {
40541da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
40551da177e4SLinus Torvalds 		eeprom->scsi_id,
40561da177e4SLinus Torvalds 		eeprom->target[0].period,
40571da177e4SLinus Torvalds 		clock_speed[eeprom->target[0].period] / 10,
40581da177e4SLinus Torvalds 		clock_speed[eeprom->target[0].period] % 10,
40591da177e4SLinus Torvalds 		eeprom->target[0].cfg0);
40601da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "               AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
40611da177e4SLinus Torvalds 		eeprom->channel_cfg, eeprom->max_tag,
40621da177e4SLinus Torvalds 		1 << eeprom->max_tag, eeprom->delay_time);
40631da177e4SLinus Torvalds }
40641da177e4SLinus Torvalds 
40651da177e4SLinus Torvalds 
40661da177e4SLinus Torvalds /* Free SG tables */
40671da177e4SLinus Torvalds static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
40681da177e4SLinus Torvalds {
40691da177e4SLinus Torvalds 	int i;
40701da177e4SLinus Torvalds 	const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
40711da177e4SLinus Torvalds 
40721da177e4SLinus Torvalds 	for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
40731da177e4SLinus Torvalds 		kfree(acb->srb_array[i].segment_x);
40741da177e4SLinus Torvalds }
40751da177e4SLinus Torvalds 
40761da177e4SLinus Torvalds 
40771da177e4SLinus Torvalds /*
40781da177e4SLinus Torvalds  * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
40791da177e4SLinus Torvalds  * should never cross a page boundary */
40806f039790SGreg Kroah-Hartman static int adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
40811da177e4SLinus Torvalds {
40821da177e4SLinus Torvalds 	const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
40831da177e4SLinus Torvalds 	                            *SEGMENTX_LEN;
40841da177e4SLinus Torvalds 	int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
40851da177e4SLinus Torvalds 	const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
40861da177e4SLinus Torvalds 	int srb_idx = 0;
40871da177e4SLinus Torvalds 	unsigned i = 0;
40883f649ab7SKees Cook 	struct SGentry *ptr;
40891da177e4SLinus Torvalds 
4090b4b08e58SLinus Torvalds 	for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
40911da177e4SLinus Torvalds 		acb->srb_array[i].segment_x = NULL;
40921da177e4SLinus Torvalds 
40931da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
40941da177e4SLinus Torvalds 	while (pages--) {
40955cbded58SRobert P. J. Day 		ptr = kmalloc(PAGE_SIZE, GFP_KERNEL);
40961da177e4SLinus Torvalds 		if (!ptr) {
40971da177e4SLinus Torvalds 			adapter_sg_tables_free(acb);
40981da177e4SLinus Torvalds 			return 1;
40991da177e4SLinus Torvalds 		}
41001da177e4SLinus Torvalds 		dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
41011da177e4SLinus Torvalds 			PAGE_SIZE, ptr, srb_idx);
41021da177e4SLinus Torvalds 		i = 0;
41031da177e4SLinus Torvalds 		while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
41041da177e4SLinus Torvalds 			acb->srb_array[srb_idx++].segment_x =
41051da177e4SLinus Torvalds 			    ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
41061da177e4SLinus Torvalds 	}
41071da177e4SLinus Torvalds 	if (i < srbs_per_page)
41081da177e4SLinus Torvalds 		acb->srb.segment_x =
41091da177e4SLinus Torvalds 		    ptr + (i * DC395x_MAX_SG_LISTENTRY);
41101da177e4SLinus Torvalds 	else
41111da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
41121da177e4SLinus Torvalds 	return 0;
41131da177e4SLinus Torvalds }
41141da177e4SLinus Torvalds 
41151da177e4SLinus Torvalds 
41161da177e4SLinus Torvalds 
41171da177e4SLinus Torvalds /**
41181da177e4SLinus Torvalds  * adapter_print_config - print adapter connection and termination
41191da177e4SLinus Torvalds  * config
41201da177e4SLinus Torvalds  *
41211da177e4SLinus Torvalds  * The io port in the adapter needs to have been set before calling
41221da177e4SLinus Torvalds  * this function.
41231da177e4SLinus Torvalds  *
41241da177e4SLinus Torvalds  * @acb: The adapter to print the information for.
41251da177e4SLinus Torvalds  **/
41266f039790SGreg Kroah-Hartman static void adapter_print_config(struct AdapterCtlBlk *acb)
41271da177e4SLinus Torvalds {
41281da177e4SLinus Torvalds 	u8 bval;
41291da177e4SLinus Torvalds 
41301da177e4SLinus Torvalds 	bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
41311da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "%sConnectors: ",
41321da177e4SLinus Torvalds 		((bval & WIDESCSI) ? "(Wide) " : ""));
41331da177e4SLinus Torvalds 	if (!(bval & CON5068))
41341da177e4SLinus Torvalds 		printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
41351da177e4SLinus Torvalds 	if (!(bval & CON68))
41361da177e4SLinus Torvalds 		printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
41371da177e4SLinus Torvalds 	if (!(bval & CON50))
41381da177e4SLinus Torvalds 		printk("int50 ");
41391da177e4SLinus Torvalds 	if ((bval & (CON5068 | CON50 | CON68)) ==
41401da177e4SLinus Torvalds 	    0 /*(CON5068 | CON50 | CON68) */ )
41411da177e4SLinus Torvalds 		printk(" Oops! (All 3?) ");
41421da177e4SLinus Torvalds 	bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
41431da177e4SLinus Torvalds 	printk(" Termination: ");
41441da177e4SLinus Torvalds 	if (bval & DIS_TERM)
41451da177e4SLinus Torvalds 		printk("Disabled\n");
41461da177e4SLinus Torvalds 	else {
41471da177e4SLinus Torvalds 		if (bval & AUTOTERM)
41481da177e4SLinus Torvalds 			printk("Auto ");
41491da177e4SLinus Torvalds 		if (bval & LOW8TERM)
41501da177e4SLinus Torvalds 			printk("Low ");
41511da177e4SLinus Torvalds 		if (bval & UP8TERM)
41521da177e4SLinus Torvalds 			printk("High ");
41531da177e4SLinus Torvalds 		printk("\n");
41541da177e4SLinus Torvalds 	}
41551da177e4SLinus Torvalds }
41561da177e4SLinus Torvalds 
41571da177e4SLinus Torvalds 
41581da177e4SLinus Torvalds /**
41591da177e4SLinus Torvalds  * adapter_init_params - Initialize the various parameters in the
41601da177e4SLinus Torvalds  * adapter structure. Note that the pointer to the scsi_host is set
41611da177e4SLinus Torvalds  * early (when this instance is created) and the io_port and irq
41621da177e4SLinus Torvalds  * values are set later after they have been reserved. This just gets
41631da177e4SLinus Torvalds  * everything set to a good starting position.
41641da177e4SLinus Torvalds  *
41651da177e4SLinus Torvalds  * The eeprom structure in the adapter needs to have been set before
41661da177e4SLinus Torvalds  * calling this function.
41671da177e4SLinus Torvalds  *
41681da177e4SLinus Torvalds  * @acb: The adapter to initialize.
41691da177e4SLinus Torvalds  **/
41706f039790SGreg Kroah-Hartman static void adapter_init_params(struct AdapterCtlBlk *acb)
41711da177e4SLinus Torvalds {
41721da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
41731da177e4SLinus Torvalds 	int i;
41741da177e4SLinus Torvalds 
41751da177e4SLinus Torvalds 	/* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
41761da177e4SLinus Torvalds 	/* NOTE: acb->io_port_base is set at port registration time */
41771da177e4SLinus Torvalds 	/* NOTE: acb->io_port_len is set at port registration time */
41781da177e4SLinus Torvalds 
41791da177e4SLinus Torvalds 	INIT_LIST_HEAD(&acb->dcb_list);
41801da177e4SLinus Torvalds 	acb->dcb_run_robin = NULL;
41811da177e4SLinus Torvalds 	acb->active_dcb = NULL;
41821da177e4SLinus Torvalds 
41831da177e4SLinus Torvalds 	INIT_LIST_HEAD(&acb->srb_free_list);
41841da177e4SLinus Torvalds 	/*  temp SRB for Q tag used or abort command used  */
41851da177e4SLinus Torvalds 	acb->tmp_srb = &acb->srb;
4186af53b89bSKees Cook 	timer_setup(&acb->waiting_timer, waiting_timeout, 0);
4187af53b89bSKees Cook 	timer_setup(&acb->selto_timer, NULL, 0);
41881da177e4SLinus Torvalds 
41891da177e4SLinus Torvalds 	acb->srb_count = DC395x_MAX_SRB_CNT;
41901da177e4SLinus Torvalds 
41911da177e4SLinus Torvalds 	acb->sel_timeout = DC395x_SEL_TIMEOUT;	/* timeout=250ms */
41921da177e4SLinus Torvalds 	/* NOTE: acb->irq_level is set at IRQ registration time */
41931da177e4SLinus Torvalds 
41941da177e4SLinus Torvalds 	acb->tag_max_num = 1 << eeprom->max_tag;
41951da177e4SLinus Torvalds 	if (acb->tag_max_num > 30)
41961da177e4SLinus Torvalds 		acb->tag_max_num = 30;
41971da177e4SLinus Torvalds 
41981da177e4SLinus Torvalds 	acb->acb_flag = 0;	/* RESET_DETECT, RESET_DONE, RESET_DEV */
41991da177e4SLinus Torvalds 	acb->gmode2 = eeprom->channel_cfg;
42001da177e4SLinus Torvalds 	acb->config = 0;	/* NOTE: actually set in adapter_init_chip */
42011da177e4SLinus Torvalds 
42021da177e4SLinus Torvalds 	if (eeprom->channel_cfg & NAC_SCANLUN)
42031da177e4SLinus Torvalds 		acb->lun_chk = 1;
42041da177e4SLinus Torvalds 	acb->scan_devices = 1;
42051da177e4SLinus Torvalds 
42061da177e4SLinus Torvalds 	acb->scsi_host->this_id = eeprom->scsi_id;
42071da177e4SLinus Torvalds 	acb->hostid_bit = (1 << acb->scsi_host->this_id);
42081da177e4SLinus Torvalds 
42091da177e4SLinus Torvalds 	for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
42101da177e4SLinus Torvalds 		acb->dcb_map[i] = 0;
42111da177e4SLinus Torvalds 
42121da177e4SLinus Torvalds 	acb->msg_len = 0;
42131da177e4SLinus Torvalds 
42141da177e4SLinus Torvalds 	/* link static array of srbs into the srb free list */
42151da177e4SLinus Torvalds 	for (i = 0; i < acb->srb_count - 1; i++)
4216cd29660dSChristoph Hellwig 		list_add_tail(&acb->srb_array[i].list, &acb->srb_free_list);
42171da177e4SLinus Torvalds }
42181da177e4SLinus Torvalds 
42191da177e4SLinus Torvalds 
42201da177e4SLinus Torvalds /**
4221167b7e6bSLee Jones  * adapter_init_scsi_host - Initialize the scsi host instance based on
42221da177e4SLinus Torvalds  * values that we have already stored in the adapter instance. There's
42231da177e4SLinus Torvalds  * some mention that a lot of these are deprecated, so we won't use
42241da177e4SLinus Torvalds  * them (we'll use the ones in the adapter instance) but we'll fill
42251da177e4SLinus Torvalds  * them in in case something else needs them.
42261da177e4SLinus Torvalds  *
42271da177e4SLinus Torvalds  * The eeprom structure, irq and io ports in the adapter need to have
42281da177e4SLinus Torvalds  * been set before calling this function.
42291da177e4SLinus Torvalds  *
42301da177e4SLinus Torvalds  * @host: The scsi host instance to fill in the values for.
42311da177e4SLinus Torvalds  **/
42326f039790SGreg Kroah-Hartman static void adapter_init_scsi_host(struct Scsi_Host *host)
42331da177e4SLinus Torvalds {
42341da177e4SLinus Torvalds         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
42351da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
42361da177e4SLinus Torvalds 
42371da177e4SLinus Torvalds 	host->max_cmd_len = 24;
42381da177e4SLinus Torvalds 	host->can_queue = DC395x_MAX_CMD_QUEUE;
42391da177e4SLinus Torvalds 	host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
42401da177e4SLinus Torvalds 	host->this_id = (int)eeprom->scsi_id;
42411da177e4SLinus Torvalds 	host->io_port = acb->io_port_base;
42421da177e4SLinus Torvalds 	host->n_io_port = acb->io_port_len;
42431da177e4SLinus Torvalds 	host->dma_channel = -1;
42441da177e4SLinus Torvalds 	host->unique_id = acb->io_port_base;
42451da177e4SLinus Torvalds 	host->irq = acb->irq_level;
424653a8936cSHannes Reinecke 	acb->last_reset = jiffies;
42471da177e4SLinus Torvalds 
42481da177e4SLinus Torvalds 	host->max_id = 16;
42491da177e4SLinus Torvalds 	if (host->max_id - 1 == eeprom->scsi_id)
42501da177e4SLinus Torvalds 		host->max_id--;
42511da177e4SLinus Torvalds 
42521da177e4SLinus Torvalds 	if (eeprom->channel_cfg & NAC_SCANLUN)
42531da177e4SLinus Torvalds 		host->max_lun = 8;
42541da177e4SLinus Torvalds 	else
42551da177e4SLinus Torvalds 		host->max_lun = 1;
42561da177e4SLinus Torvalds }
42571da177e4SLinus Torvalds 
42581da177e4SLinus Torvalds 
42591da177e4SLinus Torvalds /**
42601da177e4SLinus Torvalds  * adapter_init_chip - Get the chip into a know state and figure out
42611da177e4SLinus Torvalds  * some of the settings that apply to this adapter.
42621da177e4SLinus Torvalds  *
42631da177e4SLinus Torvalds  * The io port in the adapter needs to have been set before calling
42641da177e4SLinus Torvalds  * this function. The config will be configured correctly on return.
42651da177e4SLinus Torvalds  *
42661da177e4SLinus Torvalds  * @acb: The adapter which we are to init.
42671da177e4SLinus Torvalds  **/
42686f039790SGreg Kroah-Hartman static void adapter_init_chip(struct AdapterCtlBlk *acb)
42691da177e4SLinus Torvalds {
42701da177e4SLinus Torvalds         struct NvRamType *eeprom = &acb->eeprom;
42711da177e4SLinus Torvalds 
42721da177e4SLinus Torvalds         /* Mask all the interrupt */
42731da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
42741da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
42751da177e4SLinus Torvalds 
42761da177e4SLinus Torvalds 	/* Reset SCSI module */
42771da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
42781da177e4SLinus Torvalds 
42791da177e4SLinus Torvalds 	/* Reset PCI/DMA module */
42801da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
42811da177e4SLinus Torvalds 	udelay(20);
42821da177e4SLinus Torvalds 
42831da177e4SLinus Torvalds 	/* program configuration 0 */
42841da177e4SLinus Torvalds 	acb->config = HCC_AUTOTERM | HCC_PARITY;
42851da177e4SLinus Torvalds 	if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
42861da177e4SLinus Torvalds 		acb->config |= HCC_WIDE_CARD;
42871da177e4SLinus Torvalds 
42881da177e4SLinus Torvalds 	if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
42891da177e4SLinus Torvalds 		acb->config |= HCC_SCSI_RESET;
42901da177e4SLinus Torvalds 
42911da177e4SLinus Torvalds 	if (acb->config & HCC_SCSI_RESET) {
42921da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
42931da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
42941da177e4SLinus Torvalds 
42951da177e4SLinus Torvalds 		/*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
42961da177e4SLinus Torvalds 		/*spin_unlock_irq (&io_request_lock); */
42971da177e4SLinus Torvalds 		udelay(500);
42981da177e4SLinus Torvalds 
429953a8936cSHannes Reinecke 		acb->last_reset =
43001da177e4SLinus Torvalds 		    jiffies + HZ / 2 +
43011da177e4SLinus Torvalds 		    HZ * acb->eeprom.delay_time;
43021da177e4SLinus Torvalds 
43031da177e4SLinus Torvalds 		/*spin_lock_irq (&io_request_lock); */
43041da177e4SLinus Torvalds 	}
43051da177e4SLinus Torvalds }
43061da177e4SLinus Torvalds 
43071da177e4SLinus Torvalds 
43081da177e4SLinus Torvalds /**
4309167b7e6bSLee Jones  * adapter_init - Grab the resource for the card, setup the adapter
43101da177e4SLinus Torvalds  * information, set the card into a known state, create the various
43111da177e4SLinus Torvalds  * tables etc etc. This basically gets all adapter information all up
4312b595076aSUwe Kleine-König  * to date, initialised and gets the chip in sync with it.
43131da177e4SLinus Torvalds  *
431433c8ef95SLee Jones  * @acb:	The adapter which we are to init.
43151da177e4SLinus Torvalds  * @io_port:	The base I/O port
431633c8ef95SLee Jones  * @io_port_len: The I/O port size
43171da177e4SLinus Torvalds  * @irq:	IRQ
43181da177e4SLinus Torvalds  *
43191da177e4SLinus Torvalds  * Returns 0 if the initialization succeeds, any other value on
43201da177e4SLinus Torvalds  * failure.
43211da177e4SLinus Torvalds  **/
43226f039790SGreg Kroah-Hartman static int adapter_init(struct AdapterCtlBlk *acb, unsigned long io_port,
43236f039790SGreg Kroah-Hartman 			u32 io_port_len, unsigned int irq)
43241da177e4SLinus Torvalds {
43251da177e4SLinus Torvalds 	if (!request_region(io_port, io_port_len, DC395X_NAME)) {
43261da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
43271da177e4SLinus Torvalds 		goto failed;
43281da177e4SLinus Torvalds 	}
43291da177e4SLinus Torvalds 	/* store port base to indicate we have registered it */
43301da177e4SLinus Torvalds 	acb->io_port_base = io_port;
43311da177e4SLinus Torvalds 	acb->io_port_len = io_port_len;
43321da177e4SLinus Torvalds 
43331d6f359aSThomas Gleixner 	if (request_irq(irq, dc395x_interrupt, IRQF_SHARED, DC395X_NAME, acb)) {
43341da177e4SLinus Torvalds 	    	/* release the region we just claimed */
43351da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Failed to register IRQ\n");
43361da177e4SLinus Torvalds 		goto failed;
43371da177e4SLinus Torvalds 	}
43381da177e4SLinus Torvalds 	/* store irq to indicate we have registered it */
43391da177e4SLinus Torvalds 	acb->irq_level = irq;
43401da177e4SLinus Torvalds 
43411da177e4SLinus Torvalds 	/* get eeprom configuration information and command line settings etc */
43421da177e4SLinus Torvalds 	check_eeprom(&acb->eeprom, io_port);
43431da177e4SLinus Torvalds  	print_eeprom_settings(&acb->eeprom);
43441da177e4SLinus Torvalds 
43451da177e4SLinus Torvalds 	/* setup adapter control block */
43461da177e4SLinus Torvalds 	adapter_init_params(acb);
43471da177e4SLinus Torvalds 
43481da177e4SLinus Torvalds 	/* display card connectors/termination settings */
43491da177e4SLinus Torvalds  	adapter_print_config(acb);
43501da177e4SLinus Torvalds 
43511da177e4SLinus Torvalds 	if (adapter_sg_tables_alloc(acb)) {
43521da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
43531da177e4SLinus Torvalds 		goto failed;
43541da177e4SLinus Torvalds 	}
43551da177e4SLinus Torvalds 	adapter_init_scsi_host(acb->scsi_host);
43561da177e4SLinus Torvalds 	adapter_init_chip(acb);
43571da177e4SLinus Torvalds 	set_basic_config(acb);
43581da177e4SLinus Torvalds 
43591da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
43601da177e4SLinus Torvalds 		"adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
43611da177e4SLinus Torvalds 		"size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
43621da177e4SLinus Torvalds 		acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
43631da177e4SLinus Torvalds 		sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
43641da177e4SLinus Torvalds 	return 0;
43651da177e4SLinus Torvalds 
43661da177e4SLinus Torvalds failed:
43671da177e4SLinus Torvalds 	if (acb->irq_level)
43681da177e4SLinus Torvalds 		free_irq(acb->irq_level, acb);
43691da177e4SLinus Torvalds 	if (acb->io_port_base)
43701da177e4SLinus Torvalds 		release_region(acb->io_port_base, acb->io_port_len);
43711da177e4SLinus Torvalds 	adapter_sg_tables_free(acb);
43721da177e4SLinus Torvalds 
43731da177e4SLinus Torvalds 	return 1;
43741da177e4SLinus Torvalds }
43751da177e4SLinus Torvalds 
43761da177e4SLinus Torvalds 
43771da177e4SLinus Torvalds /**
43781da177e4SLinus Torvalds  * adapter_uninit_chip - cleanly shut down the scsi controller chip,
43791da177e4SLinus Torvalds  * stopping all operations and disabling interrupt generation on the
43801da177e4SLinus Torvalds  * card.
43811da177e4SLinus Torvalds  *
43821da177e4SLinus Torvalds  * @acb: The adapter which we are to shutdown.
43831da177e4SLinus Torvalds  **/
43841da177e4SLinus Torvalds static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
43851da177e4SLinus Torvalds {
43861da177e4SLinus Torvalds 	/* disable interrupts */
43871da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
43881da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
43891da177e4SLinus Torvalds 
43901da177e4SLinus Torvalds 	/* reset the scsi bus */
43911da177e4SLinus Torvalds 	if (acb->config & HCC_SCSI_RESET)
43921da177e4SLinus Torvalds 		reset_scsi_bus(acb);
43931da177e4SLinus Torvalds 
43943a4fa0a2SRobert P. J. Day 	/* clear any pending interrupt state */
43951da177e4SLinus Torvalds 	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
43961da177e4SLinus Torvalds }
43971da177e4SLinus Torvalds 
43981da177e4SLinus Torvalds 
43991da177e4SLinus Torvalds 
44001da177e4SLinus Torvalds /**
44011da177e4SLinus Torvalds  * adapter_uninit - Shut down the chip and release any resources that
44021da177e4SLinus Torvalds  * we had allocated. Once this returns the adapter should not be used
44031da177e4SLinus Torvalds  * anymore.
44041da177e4SLinus Torvalds  *
44051da177e4SLinus Torvalds  * @acb: The adapter which we are to un-initialize.
44061da177e4SLinus Torvalds  **/
44071da177e4SLinus Torvalds static void adapter_uninit(struct AdapterCtlBlk *acb)
44081da177e4SLinus Torvalds {
44091da177e4SLinus Torvalds 	unsigned long flags;
44101da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
44111da177e4SLinus Torvalds 
44121da177e4SLinus Torvalds 	/* remove timers */
44131da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
44141da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
44151da177e4SLinus Torvalds 	if (timer_pending(&acb->selto_timer))
44161da177e4SLinus Torvalds 		del_timer(&acb->selto_timer);
44171da177e4SLinus Torvalds 
44181da177e4SLinus Torvalds 	adapter_uninit_chip(acb);
44191da177e4SLinus Torvalds 	adapter_remove_and_free_all_devices(acb);
44201da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
44211da177e4SLinus Torvalds 
44221da177e4SLinus Torvalds 	if (acb->irq_level)
44231da177e4SLinus Torvalds 		free_irq(acb->irq_level, acb);
44241da177e4SLinus Torvalds 	if (acb->io_port_base)
44251da177e4SLinus Torvalds 		release_region(acb->io_port_base, acb->io_port_len);
44261da177e4SLinus Torvalds 
44271da177e4SLinus Torvalds 	adapter_sg_tables_free(acb);
44281da177e4SLinus Torvalds }
44291da177e4SLinus Torvalds 
44301da177e4SLinus Torvalds 
44311da177e4SLinus Torvalds #undef YESNO
44321da177e4SLinus Torvalds #define YESNO(YN) \
44330c3de38fSRasmus Villemoes  if (YN) seq_printf(m, " Yes ");\
44340c3de38fSRasmus Villemoes  else seq_printf(m, " No  ")
44351da177e4SLinus Torvalds 
4436d32812aeSAl Viro static int dc395x_show_info(struct seq_file *m, struct Scsi_Host *host)
44371da177e4SLinus Torvalds {
44381da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
44391da177e4SLinus Torvalds 	int spd, spd1;
44401da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
44411da177e4SLinus Torvalds 	unsigned long flags;
44421da177e4SLinus Torvalds 	int dev;
44431da177e4SLinus Torvalds 
44443d30079cSRasmus Villemoes 	seq_puts(m, DC395X_BANNER " PCI SCSI Host Adapter\n"
44453d30079cSRasmus Villemoes 		" Driver Version " DC395X_VERSION "\n");
44461da177e4SLinus Torvalds 
44471da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
44481da177e4SLinus Torvalds 
44490c3de38fSRasmus Villemoes 	seq_printf(m, "SCSI Host Nr %i, ", host->host_no);
44500c3de38fSRasmus Villemoes 	seq_printf(m, "DC395U/UW/F DC315/U %s\n",
44511da177e4SLinus Torvalds 		(acb->config & HCC_WIDE_CARD) ? "Wide" : "");
44520c3de38fSRasmus Villemoes 	seq_printf(m, "io_port_base 0x%04lx, ", acb->io_port_base);
44530c3de38fSRasmus Villemoes 	seq_printf(m, "irq_level 0x%04x, ", acb->irq_level);
44540c3de38fSRasmus Villemoes 	seq_printf(m, " SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
44551da177e4SLinus Torvalds 
44560c3de38fSRasmus Villemoes 	seq_printf(m, "MaxID %i, MaxLUN %llu, ", host->max_id, host->max_lun);
44570c3de38fSRasmus Villemoes 	seq_printf(m, "AdapterID %i\n", host->this_id);
44581da177e4SLinus Torvalds 
44590c3de38fSRasmus Villemoes 	seq_printf(m, "tag_max_num %i", acb->tag_max_num);
44600c3de38fSRasmus Villemoes 	/*seq_printf(m, ", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
44610c3de38fSRasmus Villemoes 	seq_printf(m, ", FilterCfg 0x%02x",
44621da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
44630c3de38fSRasmus Villemoes 	seq_printf(m, ", DelayReset %is\n", acb->eeprom.delay_time);
44640c3de38fSRasmus Villemoes 	/*seq_printf(m, "\n"); */
44651da177e4SLinus Torvalds 
44660c3de38fSRasmus Villemoes 	seq_printf(m, "Nr of DCBs: %i\n", list_size(&acb->dcb_list));
4467ca358af1SAndy Shevchenko 	seq_printf(m, "Map of attached LUNs: %8ph\n", &acb->dcb_map[0]);
4468ca358af1SAndy Shevchenko 	seq_printf(m, "                      %8ph\n", &acb->dcb_map[8]);
44691da177e4SLinus Torvalds 
447091c40f24SRasmus Villemoes 	seq_puts(m,
447191c40f24SRasmus Villemoes 		 "Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
44721da177e4SLinus Torvalds 
44731da177e4SLinus Torvalds 	dev = 0;
44741da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
44751da177e4SLinus Torvalds 		int nego_period;
44760c3de38fSRasmus Villemoes 		seq_printf(m, "%02i %02i  %02i ", dev, dcb->target_id,
44771da177e4SLinus Torvalds 			dcb->target_lun);
44781da177e4SLinus Torvalds 		YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
44791da177e4SLinus Torvalds 		YESNO(dcb->sync_offset);
44801da177e4SLinus Torvalds 		YESNO(dcb->sync_period & WIDE_SYNC);
44811da177e4SLinus Torvalds 		YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
44821da177e4SLinus Torvalds 		YESNO(dcb->dev_mode & NTC_DO_SEND_START);
44831da177e4SLinus Torvalds 		YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
44841da177e4SLinus Torvalds 		nego_period = clock_period[dcb->sync_period & 0x07] << 2;
44851da177e4SLinus Torvalds 		if (dcb->sync_offset)
44860c3de38fSRasmus Villemoes 			seq_printf(m, "  %03i ns ", nego_period);
44871da177e4SLinus Torvalds 		else
44880c3de38fSRasmus Villemoes 			seq_printf(m, " (%03i ns)", (dcb->min_nego_period << 2));
44891da177e4SLinus Torvalds 
44901da177e4SLinus Torvalds 		if (dcb->sync_offset & 0x0f) {
44911da177e4SLinus Torvalds 			spd = 1000 / (nego_period);
44921da177e4SLinus Torvalds 			spd1 = 1000 % (nego_period);
44931da177e4SLinus Torvalds 			spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
44940c3de38fSRasmus Villemoes 			seq_printf(m, "   %2i.%1i M     %02i ", spd, spd1,
44951da177e4SLinus Torvalds 				(dcb->sync_offset & 0x0f));
44961da177e4SLinus Torvalds 		} else
449791c40f24SRasmus Villemoes 			seq_puts(m, "                 ");
44981da177e4SLinus Torvalds 
44991da177e4SLinus Torvalds 		/* Add more info ... */
45000c3de38fSRasmus Villemoes 		seq_printf(m, "     %02i\n", dcb->max_command);
45011da177e4SLinus Torvalds 		dev++;
45021da177e4SLinus Torvalds 	}
45031da177e4SLinus Torvalds 
45041da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
450591c40f24SRasmus Villemoes 		seq_puts(m, "Waiting queue timer running\n");
45061da177e4SLinus Torvalds 	else
4507f50332ffSRasmus Villemoes 		seq_putc(m, '\n');
45081da177e4SLinus Torvalds 
45091da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
45101da177e4SLinus Torvalds 		struct ScsiReqBlk *srb;
45111da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_waiting_list))
45120c3de38fSRasmus Villemoes 			seq_printf(m, "DCB (%02i-%i): Waiting: %i:",
45131da177e4SLinus Torvalds 				dcb->target_id, dcb->target_lun,
45141da177e4SLinus Torvalds 				list_size(&dcb->srb_waiting_list));
45151da177e4SLinus Torvalds                 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
45160c3de38fSRasmus Villemoes 			seq_printf(m, " %p", srb->cmd);
45171da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_going_list))
45180c3de38fSRasmus Villemoes 			seq_printf(m, "\nDCB (%02i-%i): Going  : %i:",
45191da177e4SLinus Torvalds 				dcb->target_id, dcb->target_lun,
45201da177e4SLinus Torvalds 				list_size(&dcb->srb_going_list));
45211da177e4SLinus Torvalds 		list_for_each_entry(srb, &dcb->srb_going_list, list)
45220c3de38fSRasmus Villemoes 			seq_printf(m, " %p", srb->cmd);
45231da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
4524f50332ffSRasmus Villemoes 			seq_putc(m, '\n');
45251da177e4SLinus Torvalds 	}
45261da177e4SLinus Torvalds 
45271da177e4SLinus Torvalds 	if (debug_enabled(DBG_1)) {
45280c3de38fSRasmus Villemoes 		seq_printf(m, "DCB list for ACB %p:\n", acb);
45291da177e4SLinus Torvalds 		list_for_each_entry(dcb, &acb->dcb_list, list) {
45300c3de38fSRasmus Villemoes 			seq_printf(m, "%p -> ", dcb);
45311da177e4SLinus Torvalds 		}
453291c40f24SRasmus Villemoes 		seq_puts(m, "END\n");
45331da177e4SLinus Torvalds 	}
45341da177e4SLinus Torvalds 
45351da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
45361da177e4SLinus Torvalds 	return 0;
45371da177e4SLinus Torvalds }
45381da177e4SLinus Torvalds 
45391da177e4SLinus Torvalds 
45401da177e4SLinus Torvalds static struct scsi_host_template dc395x_driver_template = {
45411da177e4SLinus Torvalds 	.module                 = THIS_MODULE,
45421da177e4SLinus Torvalds 	.proc_name              = DC395X_NAME,
4543d32812aeSAl Viro 	.show_info              = dc395x_show_info,
45441da177e4SLinus Torvalds 	.name                   = DC395X_BANNER " " DC395X_VERSION,
45451da177e4SLinus Torvalds 	.queuecommand           = dc395x_queue_command,
45461da177e4SLinus Torvalds 	.slave_alloc            = dc395x_slave_alloc,
45471da177e4SLinus Torvalds 	.slave_destroy          = dc395x_slave_destroy,
45481da177e4SLinus Torvalds 	.can_queue              = DC395x_MAX_CAN_QUEUE,
45491da177e4SLinus Torvalds 	.this_id                = 7,
45501da177e4SLinus Torvalds 	.sg_tablesize           = DC395x_MAX_SG_TABLESIZE,
45511da177e4SLinus Torvalds 	.cmd_per_lun            = DC395x_MAX_CMD_PER_LUN,
45521da177e4SLinus Torvalds 	.eh_abort_handler       = dc395x_eh_abort,
45531da177e4SLinus Torvalds 	.eh_bus_reset_handler   = dc395x_eh_bus_reset,
45544af14d11SChristoph Hellwig 	.dma_boundary		= PAGE_SIZE - 1,
45551da177e4SLinus Torvalds };
45561da177e4SLinus Torvalds 
45571da177e4SLinus Torvalds 
45581da177e4SLinus Torvalds /**
45591da177e4SLinus Torvalds  * banner_display - Display banner on first instance of driver
45601da177e4SLinus Torvalds  * initialized.
45611da177e4SLinus Torvalds  **/
45621da177e4SLinus Torvalds static void banner_display(void)
45631da177e4SLinus Torvalds {
45641da177e4SLinus Torvalds 	static int banner_done = 0;
45651da177e4SLinus Torvalds 	if (!banner_done)
45661da177e4SLinus Torvalds 	{
45671da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
45681da177e4SLinus Torvalds 		banner_done = 1;
45691da177e4SLinus Torvalds 	}
45701da177e4SLinus Torvalds }
45711da177e4SLinus Torvalds 
45721da177e4SLinus Torvalds 
45731da177e4SLinus Torvalds /**
45741da177e4SLinus Torvalds  * dc395x_init_one - Initialise a single instance of the adapter.
45751da177e4SLinus Torvalds  *
45761da177e4SLinus Torvalds  * The PCI layer will call this once for each instance of the adapter
45771da177e4SLinus Torvalds  * that it finds in the system. The pci_dev strcuture indicates which
45781da177e4SLinus Torvalds  * instance we are being called from.
45791da177e4SLinus Torvalds  *
4580b595076aSUwe Kleine-König  * @dev: The PCI device to initialize.
45811da177e4SLinus Torvalds  * @id: Looks like a pointer to the entry in our pci device table
45821da177e4SLinus Torvalds  * that was actually matched by the PCI subsystem.
45831da177e4SLinus Torvalds  *
45841da177e4SLinus Torvalds  * Returns 0 on success, or an error code (-ve) on failure.
45851da177e4SLinus Torvalds  **/
45866f039790SGreg Kroah-Hartman static int dc395x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
45871da177e4SLinus Torvalds {
45881da177e4SLinus Torvalds 	struct Scsi_Host *scsi_host = NULL;
45891da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = NULL;
45901da177e4SLinus Torvalds 	unsigned long io_port_base;
45911da177e4SLinus Torvalds 	unsigned int io_port_len;
45921da177e4SLinus Torvalds 	unsigned int irq;
45931da177e4SLinus Torvalds 
45941da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
45951da177e4SLinus Torvalds 	banner_display();
45961da177e4SLinus Torvalds 
45971da177e4SLinus Torvalds 	if (pci_enable_device(dev))
45981da177e4SLinus Torvalds 	{
45991da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "PCI Enable device failed.\n");
46001da177e4SLinus Torvalds 		return -ENODEV;
46011da177e4SLinus Torvalds 	}
46021da177e4SLinus Torvalds 	io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
46031da177e4SLinus Torvalds 	io_port_len = pci_resource_len(dev, 0);
46041da177e4SLinus Torvalds 	irq = dev->irq;
46051da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
46061da177e4SLinus Torvalds 
46071da177e4SLinus Torvalds 	/* allocate scsi host information (includes out adapter) */
46081da177e4SLinus Torvalds 	scsi_host = scsi_host_alloc(&dc395x_driver_template,
46091da177e4SLinus Torvalds 				    sizeof(struct AdapterCtlBlk));
46101da177e4SLinus Torvalds 	if (!scsi_host) {
46111da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
46121da177e4SLinus Torvalds 		goto fail;
46131da177e4SLinus Torvalds 	}
46141da177e4SLinus Torvalds  	acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
46151da177e4SLinus Torvalds  	acb->scsi_host = scsi_host;
46161da177e4SLinus Torvalds  	acb->dev = dev;
46171da177e4SLinus Torvalds 
46181da177e4SLinus Torvalds 	/* initialise the adapter and everything we need */
46191da177e4SLinus Torvalds  	if (adapter_init(acb, io_port_base, io_port_len, irq)) {
46201da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "adapter init failed\n");
4621*cbd9a334STong Zhang 		acb = NULL;
46221da177e4SLinus Torvalds 		goto fail;
46231da177e4SLinus Torvalds 	}
46241da177e4SLinus Torvalds 
46251da177e4SLinus Torvalds 	pci_set_master(dev);
46261da177e4SLinus Torvalds 
46271da177e4SLinus Torvalds 	/* get the scsi mid level to scan for new devices on the bus */
46281da177e4SLinus Torvalds 	if (scsi_add_host(scsi_host, &dev->dev)) {
46291da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "scsi_add_host failed\n");
46301da177e4SLinus Torvalds 		goto fail;
46311da177e4SLinus Torvalds 	}
46321da177e4SLinus Torvalds 	pci_set_drvdata(dev, scsi_host);
46331da177e4SLinus Torvalds 	scsi_scan_host(scsi_host);
46341da177e4SLinus Torvalds 
46351da177e4SLinus Torvalds 	return 0;
46361da177e4SLinus Torvalds 
46371da177e4SLinus Torvalds fail:
46381da177e4SLinus Torvalds 	if (acb != NULL)
46391da177e4SLinus Torvalds 		adapter_uninit(acb);
46401da177e4SLinus Torvalds 	if (scsi_host != NULL)
46411da177e4SLinus Torvalds 		scsi_host_put(scsi_host);
46421da177e4SLinus Torvalds 	pci_disable_device(dev);
46431da177e4SLinus Torvalds 	return -ENODEV;
46441da177e4SLinus Torvalds }
46451da177e4SLinus Torvalds 
46461da177e4SLinus Torvalds 
46471da177e4SLinus Torvalds /**
46481da177e4SLinus Torvalds  * dc395x_remove_one - Called to remove a single instance of the
46491da177e4SLinus Torvalds  * adapter.
46501da177e4SLinus Torvalds  *
4651b595076aSUwe Kleine-König  * @dev: The PCI device to initialize.
46521da177e4SLinus Torvalds  **/
46536f039790SGreg Kroah-Hartman static void dc395x_remove_one(struct pci_dev *dev)
46541da177e4SLinus Torvalds {
46551da177e4SLinus Torvalds 	struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
46561da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
46571da177e4SLinus Torvalds 
46581da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
46591da177e4SLinus Torvalds 
46601da177e4SLinus Torvalds 	scsi_remove_host(scsi_host);
46611da177e4SLinus Torvalds 	adapter_uninit(acb);
46621da177e4SLinus Torvalds 	pci_disable_device(dev);
46631da177e4SLinus Torvalds 	scsi_host_put(scsi_host);
46641da177e4SLinus Torvalds }
46651da177e4SLinus Torvalds 
46661da177e4SLinus Torvalds 
46671da177e4SLinus Torvalds static struct pci_device_id dc395x_pci_table[] = {
46681da177e4SLinus Torvalds 	{
46691da177e4SLinus Torvalds 		.vendor		= PCI_VENDOR_ID_TEKRAM,
46701da177e4SLinus Torvalds 		.device		= PCI_DEVICE_ID_TEKRAM_TRMS1040,
46711da177e4SLinus Torvalds 		.subvendor	= PCI_ANY_ID,
46721da177e4SLinus Torvalds 		.subdevice	= PCI_ANY_ID,
46731da177e4SLinus Torvalds 	 },
46741da177e4SLinus Torvalds 	{}			/* Terminating entry */
46751da177e4SLinus Torvalds };
46761da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
46771da177e4SLinus Torvalds 
46781da177e4SLinus Torvalds 
46791da177e4SLinus Torvalds static struct pci_driver dc395x_driver = {
46801da177e4SLinus Torvalds 	.name           = DC395X_NAME,
46811da177e4SLinus Torvalds 	.id_table       = dc395x_pci_table,
46821da177e4SLinus Torvalds 	.probe          = dc395x_init_one,
46836f039790SGreg Kroah-Hartman 	.remove         = dc395x_remove_one,
46841da177e4SLinus Torvalds };
468575c31c80SLiu Shixin module_pci_driver(dc395x_driver);
46861da177e4SLinus Torvalds 
46871da177e4SLinus Torvalds MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
46881da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
46891da177e4SLinus Torvalds MODULE_LICENSE("GPL");
4690