xref: /linux/drivers/scsi/dc395x.c (revision 208afec4f3be8c51ad6eebe6611dd6d2ad2fa298)
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/scsicam.h>	/* needed for scsicam_bios_param */
651da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h>
661da177e4SLinus Torvalds #include <scsi/scsi_device.h>
671da177e4SLinus Torvalds #include <scsi/scsi_host.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 /* cmd->result */
1641da177e4SLinus Torvalds #define RES_TARGET		0x000000FF	/* Target State */
1651da177e4SLinus Torvalds #define RES_TARGET_LNX  STATUS_MASK	/* Only official ... */
1661da177e4SLinus Torvalds #define RES_ENDMSG		0x0000FF00	/* End Message */
1671da177e4SLinus Torvalds #define RES_DID			0x00FF0000	/* DID_ codes */
1681da177e4SLinus Torvalds #define RES_DRV			0xFF000000	/* DRIVER_ codes */
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds #define MK_RES(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt))
1711da177e4SLinus Torvalds #define MK_RES_LNX(drv,did,msg,tgt) ((int)(drv)<<24 | (int)(did)<<16 | (int)(msg)<<8 | (int)(tgt)<<1)
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds #define SET_RES_TARGET(who,tgt) { who &= ~RES_TARGET; who |= (int)(tgt); }
1741da177e4SLinus Torvalds #define SET_RES_TARGET_LNX(who,tgt) { who &= ~RES_TARGET_LNX; who |= (int)(tgt) << 1; }
1751da177e4SLinus Torvalds #define SET_RES_MSG(who,msg) { who &= ~RES_ENDMSG; who |= (int)(msg) << 8; }
1761da177e4SLinus Torvalds #define SET_RES_DID(who,did) { who &= ~RES_DID; who |= (int)(did) << 16; }
1771da177e4SLinus Torvalds #define SET_RES_DRV(who,drv) { who &= ~RES_DRV; who |= (int)(drv) << 24; }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds #define TAG_NONE 255
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /*
1821da177e4SLinus Torvalds  * srb->segement_x is the hw sg list. It is always allocated as a
1831da177e4SLinus Torvalds  * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
1841da177e4SLinus Torvalds  * cross a page boundy.
1851da177e4SLinus Torvalds  */
1861da177e4SLinus Torvalds #define SEGMENTX_LEN	(sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
187b4b08e58SLinus Torvalds 
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds struct SGentry {
1901da177e4SLinus Torvalds 	u32 address;		/* bus! address */
1911da177e4SLinus Torvalds 	u32 length;
1921da177e4SLinus Torvalds };
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /* The SEEPROM structure for TRM_S1040 */
1951da177e4SLinus Torvalds struct NVRamTarget {
1961da177e4SLinus Torvalds 	u8 cfg0;		/* Target configuration byte 0  */
1971da177e4SLinus Torvalds 	u8 period;		/* Target period                */
1981da177e4SLinus Torvalds 	u8 cfg2;		/* Target configuration byte 2  */
1991da177e4SLinus Torvalds 	u8 cfg3;		/* Target configuration byte 3  */
2001da177e4SLinus Torvalds };
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds struct NvRamType {
2031da177e4SLinus Torvalds 	u8 sub_vendor_id[2];	/* 0,1  Sub Vendor ID   */
2041da177e4SLinus Torvalds 	u8 sub_sys_id[2];	/* 2,3  Sub System ID   */
2051da177e4SLinus Torvalds 	u8 sub_class;		/* 4    Sub Class       */
2061da177e4SLinus Torvalds 	u8 vendor_id[2];	/* 5,6  Vendor ID       */
2071da177e4SLinus Torvalds 	u8 device_id[2];	/* 7,8  Device ID       */
2081da177e4SLinus Torvalds 	u8 reserved;		/* 9    Reserved        */
2091da177e4SLinus Torvalds 	struct NVRamTarget target[DC395x_MAX_SCSI_ID];
2101da177e4SLinus Torvalds 						/** 10,11,12,13
2111da177e4SLinus Torvalds 						 ** 14,15,16,17
2121da177e4SLinus Torvalds 						 ** ....
2131da177e4SLinus Torvalds 						 ** ....
2141da177e4SLinus Torvalds 						 ** 70,71,72,73
2151da177e4SLinus Torvalds 						 */
2161da177e4SLinus Torvalds 	u8 scsi_id;		/* 74 Host Adapter SCSI ID      */
2171da177e4SLinus Torvalds 	u8 channel_cfg;		/* 75 Channel configuration     */
2181da177e4SLinus Torvalds 	u8 delay_time;		/* 76 Power on delay time       */
2191da177e4SLinus Torvalds 	u8 max_tag;		/* 77 Maximum tags              */
2201da177e4SLinus Torvalds 	u8 reserved0;		/* 78  */
2211da177e4SLinus Torvalds 	u8 boot_target;		/* 79  */
2221da177e4SLinus Torvalds 	u8 boot_lun;		/* 80  */
2231da177e4SLinus Torvalds 	u8 reserved1;		/* 81  */
2241da177e4SLinus Torvalds 	u16 reserved2[22];	/* 82,..125 */
2251da177e4SLinus Torvalds 	u16 cksum;		/* 126,127 */
2261da177e4SLinus Torvalds };
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds struct ScsiReqBlk {
2291da177e4SLinus Torvalds 	struct list_head list;		/* next/prev ptrs for srb lists */
2301da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
2311da177e4SLinus Torvalds 	struct scsi_cmnd *cmd;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	struct SGentry *segment_x;	/* Linear array of hw sg entries (up to 64 entries) */
234cdb8c2a6SGuennadi Liakhovetski 	dma_addr_t sg_bus_addr;	        /* Bus address of sg list (ie, of segment_x) */
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	u8 sg_count;			/* No of HW sg entries for this request */
2371da177e4SLinus Torvalds 	u8 sg_index;			/* Index of HW sg entry for this request */
23825985edcSLucas De Marchi 	size_t total_xfer_length;	/* Total number of bytes remaining to be transferred */
239cdb8c2a6SGuennadi Liakhovetski 	size_t request_length;		/* Total number of bytes in this request */
2401da177e4SLinus Torvalds 	/*
2411da177e4SLinus Torvalds 	 * The sense buffer handling function, request_sense, uses
2421da177e4SLinus Torvalds 	 * the first hw sg entry (segment_x[0]) and the transfer
2431da177e4SLinus Torvalds 	 * length (total_xfer_length). While doing this it stores the
2441da177e4SLinus Torvalds 	 * original values into the last sg hw list
2451da177e4SLinus Torvalds 	 * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
2461da177e4SLinus Torvalds 	 * total_xfer_length in xferred. These values are restored in
2471da177e4SLinus Torvalds 	 * pci_unmap_srb_sense. This is the only place xferred is used.
2481da177e4SLinus Torvalds 	 */
249cdb8c2a6SGuennadi Liakhovetski 	size_t xferred;		        /* Saved copy of total_xfer_length */
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	u16 state;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	u8 msgin_buf[6];
2541da177e4SLinus Torvalds 	u8 msgout_buf[6];
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	u8 adapter_status;
2571da177e4SLinus Torvalds 	u8 target_status;
2581da177e4SLinus Torvalds 	u8 msg_count;
2591da177e4SLinus Torvalds 	u8 end_message;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	u8 tag_number;
2621da177e4SLinus Torvalds 	u8 status;
2631da177e4SLinus Torvalds 	u8 retry_count;
2641da177e4SLinus Torvalds 	u8 flag;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	u8 scsi_phase;
2671da177e4SLinus Torvalds };
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds struct DeviceCtlBlk {
2701da177e4SLinus Torvalds 	struct list_head list;		/* next/prev ptrs for the dcb list */
2711da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb;
2721da177e4SLinus Torvalds 	struct list_head srb_going_list;	/* head of going srb list */
2731da177e4SLinus Torvalds 	struct list_head srb_waiting_list;	/* head of waiting srb list */
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	struct ScsiReqBlk *active_srb;
2761da177e4SLinus Torvalds 	u32 tag_mask;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	u16 max_command;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	u8 target_id;		/* SCSI Target ID  (SCSI Only) */
2811da177e4SLinus Torvalds 	u8 target_lun;		/* SCSI Log.  Unit (SCSI Only) */
2821da177e4SLinus Torvalds 	u8 identify_msg;
2831da177e4SLinus Torvalds 	u8 dev_mode;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	u8 inquiry7;		/* To store Inquiry flags */
2861da177e4SLinus Torvalds 	u8 sync_mode;		/* 0:async mode */
2871da177e4SLinus Torvalds 	u8 min_nego_period;	/* for nego. */
2881da177e4SLinus Torvalds 	u8 sync_period;		/* for reg.  */
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	u8 sync_offset;		/* for reg. and nego.(low nibble) */
2911da177e4SLinus Torvalds 	u8 flag;
2921da177e4SLinus Torvalds 	u8 dev_type;
2931da177e4SLinus Torvalds 	u8 init_tcq_flag;
2941da177e4SLinus Torvalds };
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds struct AdapterCtlBlk {
2971da177e4SLinus Torvalds 	struct Scsi_Host *scsi_host;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	unsigned long io_port_base;
3001da177e4SLinus Torvalds 	unsigned long io_port_len;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	struct list_head dcb_list;		/* head of going dcb list */
3031da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb_run_robin;
3041da177e4SLinus Torvalds 	struct DeviceCtlBlk *active_dcb;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	struct list_head srb_free_list;		/* head of free srb list */
3071da177e4SLinus Torvalds 	struct ScsiReqBlk *tmp_srb;
3081da177e4SLinus Torvalds 	struct timer_list waiting_timer;
3091da177e4SLinus Torvalds 	struct timer_list selto_timer;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	u16 srb_count;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	u8 sel_timeout;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	unsigned int irq_level;
3161da177e4SLinus Torvalds 	u8 tag_max_num;
3171da177e4SLinus Torvalds 	u8 acb_flag;
3181da177e4SLinus Torvalds 	u8 gmode2;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	u8 config;
3211da177e4SLinus Torvalds 	u8 lun_chk;
3221da177e4SLinus Torvalds 	u8 scan_devices;
3231da177e4SLinus Torvalds 	u8 hostid_bit;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	u8 dcb_map[DC395x_MAX_SCSI_ID];
3261da177e4SLinus Torvalds 	struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	struct pci_dev *dev;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	u8 msg_len;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
3331da177e4SLinus Torvalds 	struct ScsiReqBlk srb;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	struct NvRamType eeprom;	/* eeprom settings for this adapter */
3361da177e4SLinus Torvalds };
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds /*---------------------------------------------------------------------------
3401da177e4SLinus Torvalds                             Forward declarations
3411da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
3421da177e4SLinus Torvalds static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3431da177e4SLinus Torvalds 		u16 *pscsi_status);
3441da177e4SLinus Torvalds static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3451da177e4SLinus Torvalds 		u16 *pscsi_status);
3461da177e4SLinus Torvalds static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3471da177e4SLinus Torvalds 		u16 *pscsi_status);
3481da177e4SLinus Torvalds static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3491da177e4SLinus Torvalds 		u16 *pscsi_status);
3501da177e4SLinus Torvalds static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3511da177e4SLinus Torvalds 		u16 *pscsi_status);
3521da177e4SLinus Torvalds static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3531da177e4SLinus Torvalds 		u16 *pscsi_status);
3541da177e4SLinus Torvalds static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3551da177e4SLinus Torvalds 		u16 *pscsi_status);
3561da177e4SLinus Torvalds static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3571da177e4SLinus Torvalds 		u16 *pscsi_status);
3581da177e4SLinus Torvalds static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3591da177e4SLinus Torvalds 		u16 *pscsi_status);
3601da177e4SLinus Torvalds static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3611da177e4SLinus Torvalds 		u16 *pscsi_status);
3621da177e4SLinus Torvalds static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3631da177e4SLinus Torvalds 		u16 *pscsi_status);
3641da177e4SLinus Torvalds static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3651da177e4SLinus Torvalds 		u16 *pscsi_status);
3661da177e4SLinus Torvalds static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3671da177e4SLinus Torvalds 		u16 *pscsi_status);
3681da177e4SLinus Torvalds static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
3691da177e4SLinus Torvalds 		u16 *pscsi_status);
3701da177e4SLinus Torvalds static void set_basic_config(struct AdapterCtlBlk *acb);
3711da177e4SLinus Torvalds static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
3721da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3731da177e4SLinus Torvalds static void reset_scsi_bus(struct AdapterCtlBlk *acb);
3741da177e4SLinus Torvalds static void data_io_transfer(struct AdapterCtlBlk *acb,
3751da177e4SLinus Torvalds 		struct ScsiReqBlk *srb, u16 io_dir);
3761da177e4SLinus Torvalds static void disconnect(struct AdapterCtlBlk *acb);
3771da177e4SLinus Torvalds static void reselect(struct AdapterCtlBlk *acb);
3781da177e4SLinus Torvalds static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3791da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3801da177e4SLinus Torvalds static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
3811da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3821da177e4SLinus Torvalds static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
3831da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3841da177e4SLinus Torvalds static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
3851da177e4SLinus Torvalds 		struct scsi_cmnd *cmd, u8 force);
3861da177e4SLinus Torvalds static void scsi_reset_detect(struct AdapterCtlBlk *acb);
3871da177e4SLinus Torvalds static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
3881da177e4SLinus Torvalds static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
3891da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3901da177e4SLinus Torvalds static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3911da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3921da177e4SLinus Torvalds static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
3931da177e4SLinus Torvalds 		struct ScsiReqBlk *srb);
3941da177e4SLinus Torvalds static void set_xfer_rate(struct AdapterCtlBlk *acb,
3951da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb);
3961da177e4SLinus Torvalds static void waiting_timeout(unsigned long ptr);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds /*---------------------------------------------------------------------------
4001da177e4SLinus Torvalds                                  Static Data
4011da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
4021da177e4SLinus Torvalds static u16 current_sync_offset = 0;
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds static void *dc395x_scsi_phase0[] = {
4051da177e4SLinus Torvalds 	data_out_phase0,/* phase:0 */
4061da177e4SLinus Torvalds 	data_in_phase0,	/* phase:1 */
4071da177e4SLinus Torvalds 	command_phase0,	/* phase:2 */
4081da177e4SLinus Torvalds 	status_phase0,	/* phase:3 */
4091da177e4SLinus Torvalds 	nop0,		/* phase:4 PH_BUS_FREE .. initial phase */
4101da177e4SLinus Torvalds 	nop0,		/* phase:5 PH_BUS_FREE .. initial phase */
4111da177e4SLinus Torvalds 	msgout_phase0,	/* phase:6 */
4121da177e4SLinus Torvalds 	msgin_phase0,	/* phase:7 */
4131da177e4SLinus Torvalds };
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static void *dc395x_scsi_phase1[] = {
4161da177e4SLinus Torvalds 	data_out_phase1,/* phase:0 */
4171da177e4SLinus Torvalds 	data_in_phase1,	/* phase:1 */
4181da177e4SLinus Torvalds 	command_phase1,	/* phase:2 */
4191da177e4SLinus Torvalds 	status_phase1,	/* phase:3 */
4201da177e4SLinus Torvalds 	nop1,		/* phase:4 PH_BUS_FREE .. initial phase */
4211da177e4SLinus Torvalds 	nop1,		/* phase:5 PH_BUS_FREE .. initial phase */
4221da177e4SLinus Torvalds 	msgout_phase1,	/* phase:6 */
4231da177e4SLinus Torvalds 	msgin_phase1,	/* phase:7 */
4241da177e4SLinus Torvalds };
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  *Fast20:	000	 50ns, 20.0 MHz
4281da177e4SLinus Torvalds  *		001	 75ns, 13.3 MHz
4291da177e4SLinus Torvalds  *		010	100ns, 10.0 MHz
4301da177e4SLinus Torvalds  *		011	125ns,  8.0 MHz
4311da177e4SLinus Torvalds  *		100	150ns,  6.6 MHz
4321da177e4SLinus Torvalds  *		101	175ns,  5.7 MHz
4331da177e4SLinus Torvalds  *		110	200ns,  5.0 MHz
4341da177e4SLinus Torvalds  *		111	250ns,  4.0 MHz
4351da177e4SLinus Torvalds  *
4361da177e4SLinus Torvalds  *Fast40(LVDS):	000	 25ns, 40.0 MHz
4371da177e4SLinus Torvalds  *		001	 50ns, 20.0 MHz
4381da177e4SLinus Torvalds  *		010	 75ns, 13.3 MHz
4391da177e4SLinus Torvalds  *		011	100ns, 10.0 MHz
4401da177e4SLinus Torvalds  *		100	125ns,  8.0 MHz
4411da177e4SLinus Torvalds  *		101	150ns,  6.6 MHz
4421da177e4SLinus Torvalds  *		110	175ns,  5.7 MHz
4431da177e4SLinus Torvalds  *		111	200ns,  5.0 MHz
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds /*static u8	clock_period[] = {12,19,25,31,37,44,50,62};*/
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
4481da177e4SLinus Torvalds static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
4491da177e4SLinus Torvalds static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /*---------------------------------------------------------------------------
4531da177e4SLinus Torvalds                                 Configuration
4541da177e4SLinus Torvalds   ---------------------------------------------------------------------------*/
4551da177e4SLinus Torvalds /*
4561da177e4SLinus Torvalds  * Module/boot parameters currently effect *all* instances of the
4571da177e4SLinus Torvalds  * card in the system.
4581da177e4SLinus Torvalds  */
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds /*
4611da177e4SLinus Torvalds  * Command line parameters are stored in a structure below.
4621da177e4SLinus Torvalds  * These are the index's into the structure for the various
4631da177e4SLinus Torvalds  * command line options.
4641da177e4SLinus Torvalds  */
4651da177e4SLinus Torvalds #define CFG_ADAPTER_ID		0
4661da177e4SLinus Torvalds #define CFG_MAX_SPEED		1
4671da177e4SLinus Torvalds #define CFG_DEV_MODE		2
4681da177e4SLinus Torvalds #define CFG_ADAPTER_MODE	3
4691da177e4SLinus Torvalds #define CFG_TAGS		4
4701da177e4SLinus Torvalds #define CFG_RESET_DELAY		5
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds #define CFG_NUM			6	/* number of configuration items */
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds /*
4761da177e4SLinus Torvalds  * Value used to indicate that a command line override
4771da177e4SLinus Torvalds  * hasn't been used to modify the value.
4781da177e4SLinus Torvalds  */
4791da177e4SLinus Torvalds #define CFG_PARAM_UNSET -1
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds /*
4831da177e4SLinus Torvalds  * Hold command line parameters.
4841da177e4SLinus Torvalds  */
4851da177e4SLinus Torvalds struct ParameterData {
4861da177e4SLinus Torvalds 	int value;		/* value of this setting */
4871da177e4SLinus Torvalds 	int min;		/* minimum value */
4881da177e4SLinus Torvalds 	int max;		/* maximum value */
4891da177e4SLinus Torvalds 	int def;		/* default value */
4901da177e4SLinus Torvalds 	int safe;		/* safe value */
4911da177e4SLinus Torvalds };
4926f039790SGreg Kroah-Hartman static struct ParameterData cfg_data[] = {
4931da177e4SLinus Torvalds 	{ /* adapter id */
4941da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
4951da177e4SLinus Torvalds 		0,
4961da177e4SLinus Torvalds 		15,
4971da177e4SLinus Torvalds 		7,
4981da177e4SLinus Torvalds 		7
4991da177e4SLinus Torvalds 	},
5001da177e4SLinus Torvalds 	{ /* max speed */
5011da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5021da177e4SLinus Torvalds 		  0,
5031da177e4SLinus Torvalds 		  7,
5041da177e4SLinus Torvalds 		  1,	/* 13.3Mhz */
5051da177e4SLinus Torvalds 		  4,	/*  6.7Hmz */
5061da177e4SLinus Torvalds 	},
5071da177e4SLinus Torvalds 	{ /* dev mode */
5081da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5091da177e4SLinus Torvalds 		0,
5101da177e4SLinus Torvalds 		0x3f,
5111da177e4SLinus Torvalds 		NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
5121da177e4SLinus Torvalds 			NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
5131da177e4SLinus Torvalds 			NTC_DO_SEND_START,
5141da177e4SLinus Torvalds 		NTC_DO_PARITY_CHK | NTC_DO_SEND_START
5151da177e4SLinus Torvalds 	},
5161da177e4SLinus Torvalds 	{ /* adapter mode */
5171da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5181da177e4SLinus Torvalds 		0,
5191da177e4SLinus Torvalds 		0x2f,
5201da177e4SLinus Torvalds #ifdef CONFIG_SCSI_MULTI_LUN
5211da177e4SLinus Torvalds 			NAC_SCANLUN |
5221da177e4SLinus Torvalds #endif
5231da177e4SLinus Torvalds 		NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
5241da177e4SLinus Torvalds 			/*| NAC_ACTIVE_NEG*/,
5251da177e4SLinus Torvalds 		NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
5261da177e4SLinus Torvalds 	},
5271da177e4SLinus Torvalds 	{ /* tags */
5281da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5291da177e4SLinus Torvalds 		0,
5301da177e4SLinus Torvalds 		5,
5311da177e4SLinus Torvalds 		3,	/* 16 tags (??) */
5321da177e4SLinus Torvalds 		2,
5331da177e4SLinus Torvalds 	},
5341da177e4SLinus Torvalds 	{ /* reset delay */
5351da177e4SLinus Torvalds 		CFG_PARAM_UNSET,
5361da177e4SLinus Torvalds 		0,
5371da177e4SLinus Torvalds 		180,
5381da177e4SLinus Torvalds 		1,	/* 1 second */
5391da177e4SLinus Torvalds 		10,	/* 10 seconds */
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds };
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds /*
54559c51591SMichael Opdenacker  * Safe settings. If set to zero the BIOS/default values with
5461da177e4SLinus Torvalds  * command line overrides will be used. If set to 1 then safe and
5471da177e4SLinus Torvalds  * slow settings will be used.
5481da177e4SLinus Torvalds  */
54990ab5ee9SRusty Russell static bool use_safe_settings = 0;
5501da177e4SLinus Torvalds module_param_named(safe, use_safe_settings, bool, 0);
5511da177e4SLinus Torvalds MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
5551da177e4SLinus Torvalds MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
5581da177e4SLinus 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");
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
5611da177e4SLinus Torvalds MODULE_PARM_DESC(dev_mode, "Device mode.");
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
5641da177e4SLinus Torvalds MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
5671da177e4SLinus Torvalds MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
5701da177e4SLinus Torvalds MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds /**
5741da177e4SLinus Torvalds  * set_safe_settings - if the use_safe_settings option is set then
5751da177e4SLinus Torvalds  * set all values to the safe and slow values.
5761da177e4SLinus Torvalds  **/
5776f039790SGreg Kroah-Hartman static void set_safe_settings(void)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	if (use_safe_settings)
5801da177e4SLinus Torvalds 	{
5811da177e4SLinus Torvalds 		int i;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Using safe settings.\n");
5841da177e4SLinus Torvalds 		for (i = 0; i < CFG_NUM; i++)
5851da177e4SLinus Torvalds 		{
5861da177e4SLinus Torvalds 			cfg_data[i].value = cfg_data[i].safe;
5871da177e4SLinus Torvalds 		}
5881da177e4SLinus Torvalds 	}
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds /**
5931da177e4SLinus Torvalds  * fix_settings - reset any boot parameters which are out of range
5941da177e4SLinus Torvalds  * back to the default values.
5951da177e4SLinus Torvalds  **/
5966f039790SGreg Kroah-Hartman static void fix_settings(void)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	int i;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	dprintkdbg(DBG_1,
6011da177e4SLinus Torvalds 		"setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
6021da177e4SLinus Torvalds 		"AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
6031da177e4SLinus Torvalds 		cfg_data[CFG_ADAPTER_ID].value,
6041da177e4SLinus Torvalds 		cfg_data[CFG_MAX_SPEED].value,
6051da177e4SLinus Torvalds 		cfg_data[CFG_DEV_MODE].value,
6061da177e4SLinus Torvalds 		cfg_data[CFG_ADAPTER_MODE].value,
6071da177e4SLinus Torvalds 		cfg_data[CFG_TAGS].value,
6081da177e4SLinus Torvalds 		cfg_data[CFG_RESET_DELAY].value);
6091da177e4SLinus Torvalds 	for (i = 0; i < CFG_NUM; i++)
6101da177e4SLinus Torvalds 	{
6111da177e4SLinus Torvalds 		if (cfg_data[i].value < cfg_data[i].min
6121da177e4SLinus Torvalds 		    || cfg_data[i].value > cfg_data[i].max)
6131da177e4SLinus Torvalds 			cfg_data[i].value = cfg_data[i].def;
6141da177e4SLinus Torvalds 	}
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds /*
6201da177e4SLinus Torvalds  * Mapping from the eeprom delay index value (index into this array)
62159c51591SMichael Opdenacker  * to the number of actual seconds that the delay should be for.
6221da177e4SLinus Torvalds  */
6236f039790SGreg Kroah-Hartman static char eeprom_index_to_delay_map[] =
6241da177e4SLinus Torvalds 	{ 1, 3, 5, 10, 16, 30, 60, 120 };
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds /**
6281da177e4SLinus Torvalds  * eeprom_index_to_delay - Take the eeprom delay setting and convert it
6291da177e4SLinus Torvalds  * into a number of seconds.
6301da177e4SLinus Torvalds  *
6311da177e4SLinus Torvalds  * @eeprom: The eeprom structure in which we find the delay index to map.
6321da177e4SLinus Torvalds  **/
6336f039790SGreg Kroah-Hartman static void eeprom_index_to_delay(struct NvRamType *eeprom)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds /**
6401da177e4SLinus Torvalds  * delay_to_eeprom_index - Take a delay in seconds and return the
6411da177e4SLinus Torvalds  * closest eeprom index which will delay for at least that amount of
6421da177e4SLinus Torvalds  * seconds.
6431da177e4SLinus Torvalds  *
6441da177e4SLinus Torvalds  * @delay: The delay, in seconds, to find the eeprom index for.
6451da177e4SLinus Torvalds  **/
6466f039790SGreg Kroah-Hartman static int delay_to_eeprom_index(int delay)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	u8 idx = 0;
6491da177e4SLinus Torvalds 	while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
6501da177e4SLinus Torvalds 		idx++;
6511da177e4SLinus Torvalds 	return idx;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds /**
6561da177e4SLinus Torvalds  * eeprom_override - Override the eeprom settings, in the provided
6571da177e4SLinus Torvalds  * eeprom structure, with values that have been set on the command
6581da177e4SLinus Torvalds  * line.
6591da177e4SLinus Torvalds  *
6601da177e4SLinus Torvalds  * @eeprom: The eeprom data to override with command line options.
6611da177e4SLinus Torvalds  **/
6626f039790SGreg Kroah-Hartman static void eeprom_override(struct NvRamType *eeprom)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds 	u8 id;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/* Adapter Settings */
6671da177e4SLinus Torvalds 	if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
6681da177e4SLinus Torvalds 		eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
6711da177e4SLinus Torvalds 		eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
6741da177e4SLinus Torvalds 		eeprom->delay_time = delay_to_eeprom_index(
6751da177e4SLinus Torvalds 					cfg_data[CFG_RESET_DELAY].value);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
6781da177e4SLinus Torvalds 		eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	/* Device Settings */
6811da177e4SLinus Torvalds 	for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
6821da177e4SLinus Torvalds 		if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
6831da177e4SLinus Torvalds 			eeprom->target[id].cfg0 =
6841da177e4SLinus Torvalds 				(u8)cfg_data[CFG_DEV_MODE].value;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 		if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
6871da177e4SLinus Torvalds 			eeprom->target[id].period =
6881da177e4SLinus Torvalds 				(u8)cfg_data[CFG_MAX_SPEED].value;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /*---------------------------------------------------------------------------
6951da177e4SLinus Torvalds  ---------------------------------------------------------------------------*/
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds static unsigned int list_size(struct list_head *head)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	unsigned int count = 0;
7001da177e4SLinus Torvalds 	struct list_head *pos;
7011da177e4SLinus Torvalds 	list_for_each(pos, head)
7021da177e4SLinus Torvalds 		count++;
7031da177e4SLinus Torvalds 	return count;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
7081da177e4SLinus Torvalds 		struct DeviceCtlBlk *pos)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	int use_next = 0;
7111da177e4SLinus Torvalds 	struct DeviceCtlBlk* next = NULL;
7121da177e4SLinus Torvalds 	struct DeviceCtlBlk* i;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	if (list_empty(head))
7151da177e4SLinus Torvalds 		return NULL;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/* find supplied dcb and then select the next one */
7181da177e4SLinus Torvalds 	list_for_each_entry(i, head, list)
7191da177e4SLinus Torvalds 		if (use_next) {
7201da177e4SLinus Torvalds 			next = i;
7211da177e4SLinus Torvalds 			break;
7221da177e4SLinus Torvalds 		} else if (i == pos) {
7231da177e4SLinus Torvalds 			use_next = 1;
7241da177e4SLinus Torvalds 		}
7251da177e4SLinus Torvalds 	/* if no next one take the head one (ie, wraparound) */
7261da177e4SLinus Torvalds 	if (!next)
7271da177e4SLinus Torvalds         	list_for_each_entry(i, head, list) {
7281da177e4SLinus Torvalds         		next = i;
7291da177e4SLinus Torvalds         		break;
7301da177e4SLinus Torvalds         	}
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	return next;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
7371da177e4SLinus Torvalds {
7381da177e4SLinus Torvalds 	if (srb->tag_number < 255) {
7391da177e4SLinus Torvalds 		dcb->tag_mask &= ~(1 << srb->tag_number);	/* free tag mask */
7401da177e4SLinus Torvalds 		srb->tag_number = 255;
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds /* Find cmd in SRB list */
74677933d72SJesper Juhl static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
7471da177e4SLinus Torvalds 		struct list_head *head)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	struct ScsiReqBlk *i;
7501da177e4SLinus Torvalds 	list_for_each_entry(i, head, list)
7511da177e4SLinus Torvalds 		if (i->cmd == cmd)
7521da177e4SLinus Torvalds 			return i;
7531da177e4SLinus Torvalds 	return NULL;
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds static struct ScsiReqBlk *srb_get_free(struct AdapterCtlBlk *acb)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	struct list_head *head = &acb->srb_free_list;
7601da177e4SLinus Torvalds 	struct ScsiReqBlk *srb = NULL;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	if (!list_empty(head)) {
7631da177e4SLinus Torvalds 		srb = list_entry(head->next, struct ScsiReqBlk, list);
7641da177e4SLinus Torvalds 		list_del(head->next);
7651da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "srb_get_free: srb=%p\n", srb);
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds 	return srb;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds static void srb_free_insert(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "srb_free_insert: srb=%p\n", srb);
7741da177e4SLinus Torvalds 	list_add_tail(&srb->list, &acb->srb_free_list);
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds static void srb_waiting_insert(struct DeviceCtlBlk *dcb,
7791da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
7801da177e4SLinus Torvalds {
7815cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "srb_waiting_insert: (0x%p) <%02i-%i> srb=%p\n",
7825cd049a5SChristoph Hellwig 		srb->cmd, dcb->target_id, dcb->target_lun, srb);
7831da177e4SLinus Torvalds 	list_add(&srb->list, &dcb->srb_waiting_list);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds static void srb_waiting_append(struct DeviceCtlBlk *dcb,
7881da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
7891da177e4SLinus Torvalds {
7905cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "srb_waiting_append: (0x%p) <%02i-%i> srb=%p\n",
7915cd049a5SChristoph Hellwig 		 srb->cmd, dcb->target_id, dcb->target_lun, srb);
7921da177e4SLinus Torvalds 	list_add_tail(&srb->list, &dcb->srb_waiting_list);
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds static void srb_going_append(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
7971da177e4SLinus Torvalds {
7985cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "srb_going_append: (0x%p) <%02i-%i> srb=%p\n",
7995cd049a5SChristoph Hellwig 		srb->cmd, dcb->target_id, dcb->target_lun, srb);
8001da177e4SLinus Torvalds 	list_add_tail(&srb->list, &dcb->srb_going_list);
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds static void srb_going_remove(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	struct ScsiReqBlk *i;
8071da177e4SLinus Torvalds 	struct ScsiReqBlk *tmp;
8085cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "srb_going_remove: (0x%p) <%02i-%i> srb=%p\n",
8095cd049a5SChristoph Hellwig 		srb->cmd, dcb->target_id, dcb->target_lun, srb);
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	list_for_each_entry_safe(i, tmp, &dcb->srb_going_list, list)
8121da177e4SLinus Torvalds 		if (i == srb) {
8131da177e4SLinus Torvalds 			list_del(&srb->list);
8141da177e4SLinus Torvalds 			break;
8151da177e4SLinus Torvalds 		}
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds static void srb_waiting_remove(struct DeviceCtlBlk *dcb,
8201da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	struct ScsiReqBlk *i;
8231da177e4SLinus Torvalds 	struct ScsiReqBlk *tmp;
8245cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "srb_waiting_remove: (0x%p) <%02i-%i> srb=%p\n",
8255cd049a5SChristoph Hellwig 		srb->cmd, dcb->target_id, dcb->target_lun, srb);
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	list_for_each_entry_safe(i, tmp, &dcb->srb_waiting_list, list)
8281da177e4SLinus Torvalds 		if (i == srb) {
8291da177e4SLinus Torvalds 			list_del(&srb->list);
8301da177e4SLinus Torvalds 			break;
8311da177e4SLinus Torvalds 		}
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds static void srb_going_to_waiting_move(struct DeviceCtlBlk *dcb,
8361da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
8395cd049a5SChristoph Hellwig 		"srb_going_to_waiting_move: (0x%p) <%02i-%i> srb=%p\n",
8405cd049a5SChristoph Hellwig 		srb->cmd, dcb->target_id, dcb->target_lun, srb);
8411da177e4SLinus Torvalds 	list_move(&srb->list, &dcb->srb_waiting_list);
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds static void srb_waiting_to_going_move(struct DeviceCtlBlk *dcb,
8461da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
8495cd049a5SChristoph Hellwig 		"srb_waiting_to_going_move: (0x%p) <%02i-%i> srb=%p\n",
8505cd049a5SChristoph Hellwig 		srb->cmd, dcb->target_id, dcb->target_lun, srb);
8511da177e4SLinus Torvalds 	list_move(&srb->list, &dcb->srb_going_list);
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds /* Sets the timer to wake us up */
8561da177e4SLinus Torvalds static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
8571da177e4SLinus Torvalds {
8581da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
8591da177e4SLinus Torvalds 		return;
8601da177e4SLinus Torvalds 	init_timer(&acb->waiting_timer);
8611da177e4SLinus Torvalds 	acb->waiting_timer.function = waiting_timeout;
8621da177e4SLinus Torvalds 	acb->waiting_timer.data = (unsigned long) acb;
8631da177e4SLinus Torvalds 	if (time_before(jiffies + to, acb->scsi_host->last_reset - HZ / 2))
8641da177e4SLinus Torvalds 		acb->waiting_timer.expires =
8651da177e4SLinus Torvalds 		    acb->scsi_host->last_reset - HZ / 2 + 1;
8661da177e4SLinus Torvalds 	else
8671da177e4SLinus Torvalds 		acb->waiting_timer.expires = jiffies + to + 1;
8681da177e4SLinus Torvalds 	add_timer(&acb->waiting_timer);
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds /* Send the next command from the waiting list to the bus */
8731da177e4SLinus Torvalds static void waiting_process_next(struct AdapterCtlBlk *acb)
8741da177e4SLinus Torvalds {
8751da177e4SLinus Torvalds 	struct DeviceCtlBlk *start = NULL;
8761da177e4SLinus Torvalds 	struct DeviceCtlBlk *pos;
8771da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
8781da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
8791da177e4SLinus Torvalds 	struct list_head *dcb_list_head = &acb->dcb_list;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	if (acb->active_dcb
8821da177e4SLinus Torvalds 	    || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
8831da177e4SLinus Torvalds 		return;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
8861da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	if (list_empty(dcb_list_head))
8891da177e4SLinus Torvalds 		return;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	/*
8921da177e4SLinus Torvalds 	 * Find the starting dcb. Need to find it again in the list
8931da177e4SLinus Torvalds 	 * since the list may have changed since we set the ptr to it
8941da177e4SLinus Torvalds 	 */
8951da177e4SLinus Torvalds 	list_for_each_entry(dcb, dcb_list_head, list)
8961da177e4SLinus Torvalds 		if (dcb == acb->dcb_run_robin) {
8971da177e4SLinus Torvalds 			start = dcb;
8981da177e4SLinus Torvalds 			break;
8991da177e4SLinus Torvalds 		}
9001da177e4SLinus Torvalds 	if (!start) {
9011da177e4SLinus Torvalds 		/* This can happen! */
9021da177e4SLinus Torvalds 		start = list_entry(dcb_list_head->next, typeof(*start), list);
9031da177e4SLinus Torvalds 		acb->dcb_run_robin = start;
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/*
9081da177e4SLinus Torvalds 	 * Loop over the dcb, but we start somewhere (potentially) in
9091da177e4SLinus Torvalds 	 * the middle of the loop so we need to manully do this.
9101da177e4SLinus Torvalds 	 */
9111da177e4SLinus Torvalds 	pos = start;
9121da177e4SLinus Torvalds 	do {
9131da177e4SLinus Torvalds 		struct list_head *waiting_list_head = &pos->srb_waiting_list;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 		/* Make sure, the next another device gets scheduled ... */
9161da177e4SLinus Torvalds 		acb->dcb_run_robin = dcb_get_next(dcb_list_head,
9171da177e4SLinus Torvalds 						  acb->dcb_run_robin);
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 		if (list_empty(waiting_list_head) ||
9201da177e4SLinus Torvalds 		    pos->max_command <= list_size(&pos->srb_going_list)) {
9211da177e4SLinus Torvalds 			/* move to next dcb */
9221da177e4SLinus Torvalds 			pos = dcb_get_next(dcb_list_head, pos);
9231da177e4SLinus Torvalds 		} else {
9241da177e4SLinus Torvalds 			srb = list_entry(waiting_list_head->next,
9251da177e4SLinus Torvalds 					 struct ScsiReqBlk, list);
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 			/* Try to send to the bus */
9281da177e4SLinus Torvalds 			if (!start_scsi(acb, pos, srb))
9291da177e4SLinus Torvalds 				srb_waiting_to_going_move(pos, srb);
9301da177e4SLinus Torvalds 			else
9311da177e4SLinus Torvalds 				waiting_set_timer(acb, HZ/50);
9321da177e4SLinus Torvalds 			break;
9331da177e4SLinus Torvalds 		}
9341da177e4SLinus Torvalds 	} while (pos != start);
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds /* Wake up waiting queue */
9391da177e4SLinus Torvalds static void waiting_timeout(unsigned long ptr)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	unsigned long flags;
9421da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
9431da177e4SLinus Torvalds 	dprintkdbg(DBG_1,
9441da177e4SLinus Torvalds 		"waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
9451da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
9461da177e4SLinus Torvalds 	waiting_process_next(acb);
9471da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds /* Get the DCB for a given ID/LUN combination */
9521da177e4SLinus Torvalds static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	return acb->children[id][lun];
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds /* Send SCSI Request Block (srb) to adapter (acb) */
9591da177e4SLinus Torvalds static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
9641da177e4SLinus Torvalds 	    acb->active_dcb ||
9651da177e4SLinus Torvalds 	    (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
9661da177e4SLinus Torvalds 		srb_waiting_append(dcb, srb);
9671da177e4SLinus Torvalds 		waiting_process_next(acb);
9681da177e4SLinus Torvalds 		return;
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	if (!start_scsi(acb, dcb, srb))
9721da177e4SLinus Torvalds 		srb_going_append(dcb, srb);
9731da177e4SLinus Torvalds 	else {
9741da177e4SLinus Torvalds 		srb_waiting_insert(dcb, srb);
9751da177e4SLinus Torvalds 		waiting_set_timer(acb, HZ / 50);
9761da177e4SLinus Torvalds 	}
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds /* Prepare SRB for being sent to Device DCB w/ command *cmd */
9801da177e4SLinus Torvalds static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
9811da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
9821da177e4SLinus Torvalds {
983a862ea31SFUJITA Tomonori 	int nseg;
9841da177e4SLinus Torvalds 	enum dma_data_direction dir = cmd->sc_data_direction;
9855cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "build_srb: (0x%p) <%02i-%i>\n",
9865cd049a5SChristoph Hellwig 		cmd, dcb->target_id, dcb->target_lun);
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	srb->dcb = dcb;
9891da177e4SLinus Torvalds 	srb->cmd = cmd;
9901da177e4SLinus Torvalds 	srb->sg_count = 0;
9911da177e4SLinus Torvalds 	srb->total_xfer_length = 0;
9921da177e4SLinus Torvalds 	srb->sg_bus_addr = 0;
9931da177e4SLinus Torvalds 	srb->sg_index = 0;
9941da177e4SLinus Torvalds 	srb->adapter_status = 0;
9951da177e4SLinus Torvalds 	srb->target_status = 0;
9961da177e4SLinus Torvalds 	srb->msg_count = 0;
9971da177e4SLinus Torvalds 	srb->status = 0;
9981da177e4SLinus Torvalds 	srb->flag = 0;
9991da177e4SLinus Torvalds 	srb->state = 0;
10001da177e4SLinus Torvalds 	srb->retry_count = 0;
10011da177e4SLinus Torvalds 	srb->tag_number = TAG_NONE;
10021da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
10031da177e4SLinus Torvalds 	srb->end_message = 0;
10041da177e4SLinus Torvalds 
1005a862ea31SFUJITA Tomonori 	nseg = scsi_dma_map(cmd);
1006a862ea31SFUJITA Tomonori 	BUG_ON(nseg < 0);
1007a862ea31SFUJITA Tomonori 
1008a862ea31SFUJITA Tomonori 	if (dir == PCI_DMA_NONE || !nseg) {
10091da177e4SLinus Torvalds 		dprintkdbg(DBG_0,
10101da177e4SLinus Torvalds 			"build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
1011a862ea31SFUJITA Tomonori 			   cmd->bufflen, scsi_sglist(cmd), scsi_sg_count(cmd),
1012a862ea31SFUJITA Tomonori 			   srb->segment_x[0].address);
1013a862ea31SFUJITA Tomonori 	} else {
10141da177e4SLinus Torvalds 		int i;
1015a862ea31SFUJITA Tomonori 		u32 reqlen = scsi_bufflen(cmd);
1016a862ea31SFUJITA Tomonori 		struct scatterlist *sg;
10171da177e4SLinus Torvalds 		struct SGentry *sgp = srb->segment_x;
1018a862ea31SFUJITA Tomonori 
1019a862ea31SFUJITA Tomonori 		srb->sg_count = nseg;
1020a862ea31SFUJITA Tomonori 
10211da177e4SLinus Torvalds 		dprintkdbg(DBG_0,
10221da177e4SLinus Torvalds 			   "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
1023a862ea31SFUJITA Tomonori 			   reqlen, scsi_sglist(cmd), scsi_sg_count(cmd),
10241da177e4SLinus Torvalds 			   srb->sg_count);
10251da177e4SLinus Torvalds 
1026a862ea31SFUJITA Tomonori 		scsi_for_each_sg(cmd, sg, srb->sg_count, i) {
1027a862ea31SFUJITA Tomonori 			u32 busaddr = (u32)sg_dma_address(sg);
1028a862ea31SFUJITA Tomonori 			u32 seglen = (u32)sg->length;
1029b4b08e58SLinus Torvalds 			sgp[i].address = busaddr;
10301da177e4SLinus Torvalds 			sgp[i].length = seglen;
10311da177e4SLinus Torvalds 			srb->total_xfer_length += seglen;
10321da177e4SLinus Torvalds 		}
10331da177e4SLinus Torvalds 		sgp += srb->sg_count - 1;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 		/*
10361da177e4SLinus Torvalds 		 * adjust last page if too big as it is allocated
10371da177e4SLinus Torvalds 		 * on even page boundaries
10381da177e4SLinus Torvalds 		 */
10391da177e4SLinus Torvalds 		if (srb->total_xfer_length > reqlen) {
10401da177e4SLinus Torvalds 			sgp->length -= (srb->total_xfer_length - reqlen);
10411da177e4SLinus Torvalds 			srb->total_xfer_length = reqlen;
10421da177e4SLinus Torvalds 		}
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 		/* Fixup for WIDE padding - make sure length is even */
10451da177e4SLinus Torvalds 		if (dcb->sync_period & WIDE_SYNC &&
10461da177e4SLinus Torvalds 		    srb->total_xfer_length % 2) {
10471da177e4SLinus Torvalds 			srb->total_xfer_length++;
10481da177e4SLinus Torvalds 			sgp->length++;
10491da177e4SLinus Torvalds 		}
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 		srb->sg_bus_addr = pci_map_single(dcb->acb->dev,
10521da177e4SLinus Torvalds 						srb->segment_x,
10531da177e4SLinus Torvalds 				            	SEGMENTX_LEN,
10541da177e4SLinus Torvalds 				            	PCI_DMA_TODEVICE);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 		dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
10571da177e4SLinus Torvalds 			srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
10581da177e4SLinus Torvalds 	}
1059cdb8c2a6SGuennadi Liakhovetski 
1060cdb8c2a6SGuennadi Liakhovetski 	srb->request_length = srb->total_xfer_length;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds /**
10651da177e4SLinus Torvalds  * dc395x_queue_command - queue scsi command passed from the mid
10661da177e4SLinus Torvalds  * layer, invoke 'done' on completion
10671da177e4SLinus Torvalds  *
10681da177e4SLinus Torvalds  * @cmd: pointer to scsi command object
10691da177e4SLinus Torvalds  * @done: function pointer to be invoked on completion
10701da177e4SLinus Torvalds  *
10711da177e4SLinus Torvalds  * Returns 1 if the adapter (host) is busy, else returns 0. One
10721da177e4SLinus Torvalds  * reason for an adapter to be busy is that the number
10731da177e4SLinus Torvalds  * of outstanding queued commands is already equal to
10741da177e4SLinus Torvalds  * struct Scsi_Host::can_queue .
10751da177e4SLinus Torvalds  *
10761da177e4SLinus Torvalds  * Required: if struct Scsi_Host::can_queue is ever non-zero
10771da177e4SLinus Torvalds  *           then this function is required.
10781da177e4SLinus Torvalds  *
10791da177e4SLinus Torvalds  * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
10801da177e4SLinus Torvalds  *        and is expected to be held on return.
10811da177e4SLinus Torvalds  *
10821da177e4SLinus Torvalds  **/
1083f281233dSJeff Garzik static int dc395x_queue_command_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
10841da177e4SLinus Torvalds {
10851da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
10861da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
10871da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb =
10881da177e4SLinus Torvalds 	    (struct AdapterCtlBlk *)cmd->device->host->hostdata;
10895cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "queue_command: (0x%p) <%02i-%i> cmnd=0x%02x\n",
10905cd049a5SChristoph Hellwig 		cmd, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	/* Assume BAD_TARGET; will be cleared later */
10931da177e4SLinus Torvalds 	cmd->result = DID_BAD_TARGET << 16;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	/* ignore invalid targets */
10961da177e4SLinus Torvalds 	if (cmd->device->id >= acb->scsi_host->max_id ||
10971da177e4SLinus Torvalds 	    cmd->device->lun >= acb->scsi_host->max_lun ||
10981da177e4SLinus Torvalds 	    cmd->device->lun >31) {
10991da177e4SLinus Torvalds 		goto complete;
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	/* does the specified lun on the specified device exist */
11031da177e4SLinus Torvalds 	if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
11041da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
11051da177e4SLinus Torvalds 			cmd->device->id, cmd->device->lun);
11061da177e4SLinus Torvalds 		goto complete;
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	/* do we have a DCB for the device */
11101da177e4SLinus Torvalds 	dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
11111da177e4SLinus Torvalds 	if (!dcb) {
11121da177e4SLinus Torvalds 		/* should never happen */
11131da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
11141da177e4SLinus Torvalds 			cmd->device->id, cmd->device->lun);
11151da177e4SLinus Torvalds 		goto complete;
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	/* set callback and clear result in the command */
11191da177e4SLinus Torvalds 	cmd->scsi_done = done;
11201da177e4SLinus Torvalds 	cmd->result = 0;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	srb = srb_get_free(acb);
11231da177e4SLinus Torvalds 	if (!srb)
11241da177e4SLinus Torvalds 	{
11251da177e4SLinus Torvalds 		/*
11261da177e4SLinus Torvalds 		 * Return 1 since we are unable to queue this command at this
11271da177e4SLinus Torvalds 		 * point in time.
11281da177e4SLinus Torvalds 		 */
11291da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "queue_command: No free srb's\n");
11301da177e4SLinus Torvalds 		return 1;
11311da177e4SLinus Torvalds 	}
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	build_srb(cmd, dcb, srb);
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	if (!list_empty(&dcb->srb_waiting_list)) {
11361da177e4SLinus Torvalds 		/* append to waiting queue */
11371da177e4SLinus Torvalds 		srb_waiting_append(dcb, srb);
11381da177e4SLinus Torvalds 		waiting_process_next(acb);
11391da177e4SLinus Torvalds 	} else {
11401da177e4SLinus Torvalds 		/* process immediately */
11411da177e4SLinus Torvalds 		send_srb(acb, srb);
11421da177e4SLinus Torvalds 	}
11435cd049a5SChristoph Hellwig 	dprintkdbg(DBG_1, "queue_command: (0x%p) done\n", cmd);
11441da177e4SLinus Torvalds 	return 0;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds complete:
11471da177e4SLinus Torvalds 	/*
11481da177e4SLinus Torvalds 	 * Complete the command immediatey, and then return 0 to
11491da177e4SLinus Torvalds 	 * indicate that we have handled the command. This is usually
11501da177e4SLinus Torvalds 	 * done when the commad is for things like non existent
11511da177e4SLinus Torvalds 	 * devices.
11521da177e4SLinus Torvalds 	 */
11531da177e4SLinus Torvalds 	done(cmd);
11541da177e4SLinus Torvalds 	return 0;
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
1157f281233dSJeff Garzik static DEF_SCSI_QCMD(dc395x_queue_command)
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds /*
11601da177e4SLinus Torvalds  * Return the disk geometry for the given SCSI device.
11611da177e4SLinus Torvalds  */
11621da177e4SLinus Torvalds static int dc395x_bios_param(struct scsi_device *sdev,
11631da177e4SLinus Torvalds 		struct block_device *bdev, sector_t capacity, int *info)
11641da177e4SLinus Torvalds {
11651da177e4SLinus Torvalds #ifdef CONFIG_SCSI_DC395x_TRMS1040_TRADMAP
11661da177e4SLinus Torvalds 	int heads, sectors, cylinders;
11671da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb;
11681da177e4SLinus Torvalds 	int size = capacity;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "dc395x_bios_param..............\n");
11711da177e4SLinus Torvalds 	acb = (struct AdapterCtlBlk *)sdev->host->hostdata;
11721da177e4SLinus Torvalds 	heads = 64;
11731da177e4SLinus Torvalds 	sectors = 32;
11741da177e4SLinus Torvalds 	cylinders = size / (heads * sectors);
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	if ((acb->gmode2 & NAC_GREATER_1G) && (cylinders > 1024)) {
11771da177e4SLinus Torvalds 		heads = 255;
11781da177e4SLinus Torvalds 		sectors = 63;
11791da177e4SLinus Torvalds 		cylinders = size / (heads * sectors);
11801da177e4SLinus Torvalds 	}
11811da177e4SLinus Torvalds 	geom[0] = heads;
11821da177e4SLinus Torvalds 	geom[1] = sectors;
11831da177e4SLinus Torvalds 	geom[2] = cylinders;
11841da177e4SLinus Torvalds 	return 0;
11851da177e4SLinus Torvalds #else
11861da177e4SLinus Torvalds 	return scsicam_bios_param(bdev, capacity, info);
11871da177e4SLinus Torvalds #endif
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds static void dump_register_info(struct AdapterCtlBlk *acb,
11921da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
11931da177e4SLinus Torvalds {
11941da177e4SLinus Torvalds 	u16 pstat;
11951da177e4SLinus Torvalds 	struct pci_dev *dev = acb->dev;
11961da177e4SLinus Torvalds 	pci_read_config_word(dev, PCI_STATUS, &pstat);
11971da177e4SLinus Torvalds 	if (!dcb)
11981da177e4SLinus Torvalds 		dcb = acb->active_dcb;
11991da177e4SLinus Torvalds 	if (!srb && dcb)
12001da177e4SLinus Torvalds 		srb = dcb->active_srb;
12011da177e4SLinus Torvalds 	if (srb) {
12021da177e4SLinus Torvalds 		if (!srb->cmd)
12031da177e4SLinus Torvalds 			dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
12041da177e4SLinus Torvalds 				srb, srb->cmd);
12051da177e4SLinus Torvalds 		else
12065cd049a5SChristoph Hellwig 			dprintkl(KERN_INFO, "dump: srb=%p cmd=%p "
12071da177e4SLinus Torvalds 				 "cmnd=0x%02x <%02i-%i>\n",
12085cd049a5SChristoph Hellwig 				srb, srb->cmd,
12091da177e4SLinus Torvalds 				srb->cmd->cmnd[0], srb->cmd->device->id,
12101da177e4SLinus Torvalds 			       	srb->cmd->device->lun);
12113c5c6658SAl Viro 		printk("  sglist=%p cnt=%i idx=%i len=%zu\n",
12121da177e4SLinus Torvalds 		       srb->segment_x, srb->sg_count, srb->sg_index,
12131da177e4SLinus Torvalds 		       srb->total_xfer_length);
12141da177e4SLinus Torvalds 		printk("  state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
12151da177e4SLinus Torvalds 		       srb->state, srb->status, srb->scsi_phase,
12161da177e4SLinus Torvalds 		       (acb->active_dcb) ? "" : "not");
12171da177e4SLinus Torvalds 	}
12181da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
12191da177e4SLinus Torvalds 		"signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
12201da177e4SLinus Torvalds 		"rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
12211da177e4SLinus Torvalds 		"config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
12221da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
12231da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
12241da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
12251da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
12261da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
12271da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
12281da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
12291da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
12301da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
12311da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
12321da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
12331da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
12341da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
12351da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
12361da177e4SLinus Torvalds 		"irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
12371da177e4SLinus Torvalds 		"ctctr=0x%08x addr=0x%08x:0x%08x}\n",
12381da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
12391da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
12401da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
12411da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_STATUS),
12421da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_INTEN),
12431da177e4SLinus Torvalds 		DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
12441da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_XCNT),
12451da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
12461da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
12471da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
12481da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
12491da177e4SLinus Torvalds 		"pci{status=0x%04x}\n",
12501da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
12511da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_GEN_STATUS),
12521da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_GEN_TIMER),
12531da177e4SLinus Torvalds 		pstat);
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
12581da177e4SLinus Torvalds {
12591da177e4SLinus Torvalds #if debug_enabled(DBG_FIFO)
12601da177e4SLinus Torvalds 	u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
12611da177e4SLinus Torvalds 	u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
12621da177e4SLinus Torvalds 	if (!(fifocnt & 0x40))
12631da177e4SLinus Torvalds 		dprintkdbg(DBG_FIFO,
12641da177e4SLinus Torvalds 			"clear_fifo: (%i bytes) on phase %02x in %s\n",
12651da177e4SLinus Torvalds 			fifocnt & 0x3f, lines, txt);
12661da177e4SLinus Torvalds #endif
12671da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds static void reset_dev_param(struct AdapterCtlBlk *acb)
12721da177e4SLinus Torvalds {
12731da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
12741da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
12751da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
12781da177e4SLinus Torvalds 		u8 period_index;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 		dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
12811da177e4SLinus Torvalds 		dcb->sync_period = 0;
12821da177e4SLinus Torvalds 		dcb->sync_offset = 0;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 		dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
12851da177e4SLinus Torvalds 		period_index = eeprom->target[dcb->target_id].period & 0x07;
12861da177e4SLinus Torvalds 		dcb->min_nego_period = clock_period[period_index];
12871da177e4SLinus Torvalds 		if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
12881da177e4SLinus Torvalds 		    || !(acb->config & HCC_WIDE_CARD))
12891da177e4SLinus Torvalds 			dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
12901da177e4SLinus Torvalds 	}
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds /*
12951da177e4SLinus Torvalds  * perform a hard reset on the SCSI bus
12961da177e4SLinus Torvalds  * @cmd - some command for this host (for fetching hooks)
12971da177e4SLinus Torvalds  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
12981da177e4SLinus Torvalds  */
129968b3aa7cSJeff Garzik  static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb =
13021da177e4SLinus Torvalds 		(struct AdapterCtlBlk *)cmd->device->host->hostdata;
13031da177e4SLinus Torvalds 	dprintkl(KERN_INFO,
13045cd049a5SChristoph Hellwig 		"eh_bus_reset: (0%p) target=<%02i-%i> cmd=%p\n",
13055cd049a5SChristoph Hellwig 		cmd, cmd->device->id, cmd->device->lun, cmd);
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
13081da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	/*
13111da177e4SLinus Torvalds 	 * disable interrupt
13121da177e4SLinus Torvalds 	 */
13131da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
13141da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
13151da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
13161da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	reset_scsi_bus(acb);
13191da177e4SLinus Torvalds 	udelay(500);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	/* We may be in serious trouble. Wait some seconds */
13221da177e4SLinus Torvalds 	acb->scsi_host->last_reset =
13231da177e4SLinus Torvalds 	    jiffies + 3 * HZ / 2 +
13241da177e4SLinus Torvalds 	    HZ * acb->eeprom.delay_time;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	/*
13271da177e4SLinus Torvalds 	 * re-enable interrupt
13281da177e4SLinus Torvalds 	 */
13291da177e4SLinus Torvalds 	/* Clear SCSI FIFO          */
13301da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
13311da177e4SLinus Torvalds 	clear_fifo(acb, "eh_bus_reset");
13321da177e4SLinus Torvalds 	/* Delete pending IRQ */
13331da177e4SLinus Torvalds 	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
13341da177e4SLinus Torvalds 	set_basic_config(acb);
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	reset_dev_param(acb);
13371da177e4SLinus Torvalds 	doing_srb_done(acb, DID_RESET, cmd, 0);
13381da177e4SLinus Torvalds 	acb->active_dcb = NULL;
13391da177e4SLinus Torvalds 	acb->acb_flag = 0;	/* RESET_DETECT, RESET_DONE ,RESET_DEV */
13401da177e4SLinus Torvalds 	waiting_process_next(acb);
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	return SUCCESS;
13431da177e4SLinus Torvalds }
13441da177e4SLinus Torvalds 
134568b3aa7cSJeff Garzik  static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
134668b3aa7cSJeff Garzik  {
134768b3aa7cSJeff Garzik  	int rc;
134868b3aa7cSJeff Garzik  
134968b3aa7cSJeff Garzik  	spin_lock_irq(cmd->device->host->host_lock);
135068b3aa7cSJeff Garzik  	rc = __dc395x_eh_bus_reset(cmd);
135168b3aa7cSJeff Garzik  	spin_unlock_irq(cmd->device->host->host_lock);
135268b3aa7cSJeff Garzik  
135368b3aa7cSJeff Garzik  	return rc;
135468b3aa7cSJeff Garzik  }
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds /*
13571da177e4SLinus Torvalds  * abort an errant SCSI command
13581da177e4SLinus Torvalds  * @cmd - command to be aborted
13591da177e4SLinus Torvalds  * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
13601da177e4SLinus Torvalds  */
13611da177e4SLinus Torvalds static int dc395x_eh_abort(struct scsi_cmnd *cmd)
13621da177e4SLinus Torvalds {
13631da177e4SLinus Torvalds 	/*
13641da177e4SLinus Torvalds 	 * Look into our command queues: If it has not been sent already,
13651da177e4SLinus Torvalds 	 * we remove it and return success. Otherwise fail.
13661da177e4SLinus Torvalds 	 */
13671da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb =
13681da177e4SLinus Torvalds 	    (struct AdapterCtlBlk *)cmd->device->host->hostdata;
13691da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
13701da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
13715cd049a5SChristoph Hellwig 	dprintkl(KERN_INFO, "eh_abort: (0x%p) target=<%02i-%i> cmd=%p\n",
13725cd049a5SChristoph Hellwig 		cmd, cmd->device->id, cmd->device->lun, cmd);
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
13751da177e4SLinus Torvalds 	if (!dcb) {
13761da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
13771da177e4SLinus Torvalds 		return FAILED;
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	srb = find_cmd(cmd, &dcb->srb_waiting_list);
13811da177e4SLinus Torvalds 	if (srb) {
13821da177e4SLinus Torvalds 		srb_waiting_remove(dcb, srb);
13831da177e4SLinus Torvalds 		pci_unmap_srb_sense(acb, srb);
13841da177e4SLinus Torvalds 		pci_unmap_srb(acb, srb);
13851da177e4SLinus Torvalds 		free_tag(dcb, srb);
13861da177e4SLinus Torvalds 		srb_free_insert(acb, srb);
13871da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
13881da177e4SLinus Torvalds 		cmd->result = DID_ABORT << 16;
13891da177e4SLinus Torvalds 		return SUCCESS;
13901da177e4SLinus Torvalds 	}
13911da177e4SLinus Torvalds 	srb = find_cmd(cmd, &dcb->srb_going_list);
13921da177e4SLinus Torvalds 	if (srb) {
1393cdb8c2a6SGuennadi Liakhovetski 		dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n");
13941da177e4SLinus Torvalds 		/* XXX: Should abort the command here */
13951da177e4SLinus Torvalds 	} else {
1396cdb8c2a6SGuennadi Liakhovetski 		dprintkl(KERN_DEBUG, "eh_abort: Command not found\n");
13971da177e4SLinus Torvalds 	}
13981da177e4SLinus Torvalds 	return FAILED;
13991da177e4SLinus Torvalds }
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds /* SDTR */
14031da177e4SLinus Torvalds static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
14041da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
14051da177e4SLinus Torvalds {
14061da177e4SLinus Torvalds 	u8 *ptr = srb->msgout_buf + srb->msg_count;
14071da177e4SLinus Torvalds 	if (srb->msg_count > 1) {
14081da177e4SLinus Torvalds 		dprintkl(KERN_INFO,
14091da177e4SLinus Torvalds 			"build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
14101da177e4SLinus Torvalds 			srb->msg_count, srb->msgout_buf[0],
14111da177e4SLinus Torvalds 			srb->msgout_buf[1]);
14121da177e4SLinus Torvalds 		return;
14131da177e4SLinus Torvalds 	}
14141da177e4SLinus Torvalds 	if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
14151da177e4SLinus Torvalds 		dcb->sync_offset = 0;
14161da177e4SLinus Torvalds 		dcb->min_nego_period = 200 >> 2;
14171da177e4SLinus Torvalds 	} else if (dcb->sync_offset == 0)
14181da177e4SLinus Torvalds 		dcb->sync_offset = SYNC_NEGO_OFFSET;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	*ptr++ = MSG_EXTENDED;	/* (01h) */
14211da177e4SLinus Torvalds 	*ptr++ = 3;		/* length */
14221da177e4SLinus Torvalds 	*ptr++ = EXTENDED_SDTR;	/* (01h) */
14231da177e4SLinus Torvalds 	*ptr++ = dcb->min_nego_period;	/* Transfer period (in 4ns) */
14241da177e4SLinus Torvalds 	*ptr++ = dcb->sync_offset;	/* Transfer period (max. REQ/ACK dist) */
14251da177e4SLinus Torvalds 	srb->msg_count += 5;
14261da177e4SLinus Torvalds 	srb->state |= SRB_DO_SYNC_NEGO;
14271da177e4SLinus Torvalds }
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds /* WDTR */
14311da177e4SLinus Torvalds static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
14321da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
14331da177e4SLinus Torvalds {
14341da177e4SLinus Torvalds 	u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
14351da177e4SLinus Torvalds 		   (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
14361da177e4SLinus Torvalds 	u8 *ptr = srb->msgout_buf + srb->msg_count;
14371da177e4SLinus Torvalds 	if (srb->msg_count > 1) {
14381da177e4SLinus Torvalds 		dprintkl(KERN_INFO,
14391da177e4SLinus Torvalds 			"build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
14401da177e4SLinus Torvalds 			srb->msg_count, srb->msgout_buf[0],
14411da177e4SLinus Torvalds 			srb->msgout_buf[1]);
14421da177e4SLinus Torvalds 		return;
14431da177e4SLinus Torvalds 	}
14441da177e4SLinus Torvalds 	*ptr++ = MSG_EXTENDED;	/* (01h) */
14451da177e4SLinus Torvalds 	*ptr++ = 2;		/* length */
14461da177e4SLinus Torvalds 	*ptr++ = EXTENDED_WDTR;	/* (03h) */
14471da177e4SLinus Torvalds 	*ptr++ = wide;
14481da177e4SLinus Torvalds 	srb->msg_count += 4;
14491da177e4SLinus Torvalds 	srb->state |= SRB_DO_WIDE_NEGO;
14501da177e4SLinus Torvalds }
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds #if 0
14541da177e4SLinus Torvalds /* Timer to work around chip flaw: When selecting and the bus is
14551da177e4SLinus Torvalds  * busy, we sometimes miss a Selection timeout IRQ */
14561da177e4SLinus Torvalds void selection_timeout_missed(unsigned long ptr);
14571da177e4SLinus Torvalds /* Sets the timer to wake us up */
14581da177e4SLinus Torvalds static void selto_timer(struct AdapterCtlBlk *acb)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds 	if (timer_pending(&acb->selto_timer))
14611da177e4SLinus Torvalds 		return;
14621da177e4SLinus Torvalds 	acb->selto_timer.function = selection_timeout_missed;
14631da177e4SLinus Torvalds 	acb->selto_timer.data = (unsigned long) acb;
14641da177e4SLinus Torvalds 	if (time_before
14651da177e4SLinus Torvalds 	    (jiffies + HZ, acb->scsi_host->last_reset + HZ / 2))
14661da177e4SLinus Torvalds 		acb->selto_timer.expires =
14671da177e4SLinus Torvalds 		    acb->scsi_host->last_reset + HZ / 2 + 1;
14681da177e4SLinus Torvalds 	else
14691da177e4SLinus Torvalds 		acb->selto_timer.expires = jiffies + HZ + 1;
14701da177e4SLinus Torvalds 	add_timer(&acb->selto_timer);
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds void selection_timeout_missed(unsigned long ptr)
14751da177e4SLinus Torvalds {
14761da177e4SLinus Torvalds 	unsigned long flags;
14771da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
14781da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
14791da177e4SLinus Torvalds 	dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
14801da177e4SLinus Torvalds 	if (!acb->active_dcb || !acb->active_dcb->active_srb) {
14811da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
14821da177e4SLinus Torvalds 		return;
14831da177e4SLinus Torvalds 	}
14841da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
14851da177e4SLinus Torvalds 	srb = acb->active_dcb->active_srb;
14861da177e4SLinus Torvalds 	disconnect(acb);
14871da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
14881da177e4SLinus Torvalds }
14891da177e4SLinus Torvalds #endif
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
14931da177e4SLinus Torvalds 		struct ScsiReqBlk* srb)
14941da177e4SLinus Torvalds {
14951da177e4SLinus Torvalds 	u16 s_stat2, return_code;
14961da177e4SLinus Torvalds 	u8 s_stat, scsicommand, i, identify_message;
14971da177e4SLinus Torvalds 	u8 *ptr;
14985cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> srb=%p\n",
14995cd049a5SChristoph Hellwig 		dcb->target_id, dcb->target_lun, srb);
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 	srb->tag_number = TAG_NONE;	/* acb->tag_max_num: had error read in eeprom */
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
15041da177e4SLinus Torvalds 	s_stat2 = 0;
15051da177e4SLinus Torvalds 	s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
15061da177e4SLinus Torvalds #if 1
15071da177e4SLinus Torvalds 	if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
15085cd049a5SChristoph Hellwig 		dprintkdbg(DBG_KG, "start_scsi: (0x%p) BUSY %02x %04x\n",
15095cd049a5SChristoph Hellwig 			s_stat, s_stat2);
15101da177e4SLinus Torvalds 		/*
15111da177e4SLinus Torvalds 		 * Try anyway?
15121da177e4SLinus Torvalds 		 *
15131da177e4SLinus Torvalds 		 * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
1514b71a8eb0SUwe Kleine-König 		 * Timeout, a Disconnect or a Reselection IRQ, so we would be screwed!
15151da177e4SLinus Torvalds 		 * (This is likely to be a bug in the hardware. Obviously, most people
15161da177e4SLinus Torvalds 		 *  only have one initiator per SCSI bus.)
15171da177e4SLinus Torvalds 		 * Instead let this fail and have the timer make sure the command is
15181da177e4SLinus Torvalds 		 * tried again after a short time
15191da177e4SLinus Torvalds 		 */
15201da177e4SLinus Torvalds 		/*selto_timer (acb); */
15211da177e4SLinus Torvalds 		return 1;
15221da177e4SLinus Torvalds 	}
15231da177e4SLinus Torvalds #endif
15241da177e4SLinus Torvalds 	if (acb->active_dcb) {
15255cd049a5SChristoph Hellwig 		dprintkl(KERN_DEBUG, "start_scsi: (0x%p) Attempt to start a"
15265cd049a5SChristoph Hellwig 			"command while another command (0x%p) is active.",
15275cd049a5SChristoph Hellwig 			srb->cmd,
15281da177e4SLinus Torvalds 			acb->active_dcb->active_srb ?
15295cd049a5SChristoph Hellwig 			    acb->active_dcb->active_srb->cmd : 0);
15301da177e4SLinus Torvalds 		return 1;
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 	if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
15335cd049a5SChristoph Hellwig 		dprintkdbg(DBG_KG, "start_scsi: (0x%p) Failed (busy)\n", srb->cmd);
15341da177e4SLinus Torvalds 		return 1;
15351da177e4SLinus Torvalds 	}
15361da177e4SLinus Torvalds 	/* Allow starting of SCSI commands half a second before we allow the mid-level
15371da177e4SLinus Torvalds 	 * to queue them again after a reset */
15381da177e4SLinus Torvalds 	if (time_before(jiffies, acb->scsi_host->last_reset - HZ / 2)) {
15391da177e4SLinus Torvalds 		dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
15401da177e4SLinus Torvalds 		return 1;
15411da177e4SLinus Torvalds 	}
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	/* Flush FIFO */
15441da177e4SLinus Torvalds 	clear_fifo(acb, "start_scsi");
15451da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
15461da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
15471da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
15481da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
15491da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 	identify_message = dcb->identify_msg;
15521da177e4SLinus Torvalds 	/*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
15531da177e4SLinus Torvalds 	/* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
15541da177e4SLinus Torvalds 	if (srb->flag & AUTO_REQSENSE)
15551da177e4SLinus Torvalds 		identify_message &= 0xBF;
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	if (((srb->cmd->cmnd[0] == INQUIRY)
15581da177e4SLinus Torvalds 	     || (srb->cmd->cmnd[0] == REQUEST_SENSE)
15591da177e4SLinus Torvalds 	     || (srb->flag & AUTO_REQSENSE))
15601da177e4SLinus Torvalds 	    && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
15611da177e4SLinus Torvalds 		 && !(dcb->sync_mode & WIDE_NEGO_DONE))
15621da177e4SLinus Torvalds 		|| ((dcb->sync_mode & SYNC_NEGO_ENABLE)
15631da177e4SLinus Torvalds 		    && !(dcb->sync_mode & SYNC_NEGO_DONE)))
15641da177e4SLinus Torvalds 	    && (dcb->target_lun == 0)) {
15651da177e4SLinus Torvalds 		srb->msgout_buf[0] = identify_message;
15661da177e4SLinus Torvalds 		srb->msg_count = 1;
15671da177e4SLinus Torvalds 		scsicommand = SCMD_SEL_ATNSTOP;
15681da177e4SLinus Torvalds 		srb->state = SRB_MSGOUT;
15691da177e4SLinus Torvalds #ifndef SYNC_FIRST
15701da177e4SLinus Torvalds 		if (dcb->sync_mode & WIDE_NEGO_ENABLE
15711da177e4SLinus Torvalds 		    && dcb->inquiry7 & SCSI_INQ_WBUS16) {
15721da177e4SLinus Torvalds 			build_wdtr(acb, dcb, srb);
15731da177e4SLinus Torvalds 			goto no_cmd;
15741da177e4SLinus Torvalds 		}
15751da177e4SLinus Torvalds #endif
15761da177e4SLinus Torvalds 		if (dcb->sync_mode & SYNC_NEGO_ENABLE
15771da177e4SLinus Torvalds 		    && dcb->inquiry7 & SCSI_INQ_SYNC) {
15781da177e4SLinus Torvalds 			build_sdtr(acb, dcb, srb);
15791da177e4SLinus Torvalds 			goto no_cmd;
15801da177e4SLinus Torvalds 		}
15811da177e4SLinus Torvalds 		if (dcb->sync_mode & WIDE_NEGO_ENABLE
15821da177e4SLinus Torvalds 		    && dcb->inquiry7 & SCSI_INQ_WBUS16) {
15831da177e4SLinus Torvalds 			build_wdtr(acb, dcb, srb);
15841da177e4SLinus Torvalds 			goto no_cmd;
15851da177e4SLinus Torvalds 		}
15861da177e4SLinus Torvalds 		srb->msg_count = 0;
15871da177e4SLinus Torvalds 	}
15881da177e4SLinus Torvalds 	/* Send identify message */
15891da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	scsicommand = SCMD_SEL_ATN;
15921da177e4SLinus Torvalds 	srb->state = SRB_START_;
15931da177e4SLinus Torvalds #ifndef DC395x_NO_TAGQ
15941da177e4SLinus Torvalds 	if ((dcb->sync_mode & EN_TAG_QUEUEING)
15951da177e4SLinus Torvalds 	    && (identify_message & 0xC0)) {
15961da177e4SLinus Torvalds 		/* Send Tag message */
15971da177e4SLinus Torvalds 		u32 tag_mask = 1;
15981da177e4SLinus Torvalds 		u8 tag_number = 0;
15991da177e4SLinus Torvalds 		while (tag_mask & dcb->tag_mask
1600d8187b94SRoel Kluin 		       && tag_number < dcb->max_command) {
16011da177e4SLinus Torvalds 			tag_mask = tag_mask << 1;
16021da177e4SLinus Torvalds 			tag_number++;
16031da177e4SLinus Torvalds 		}
16041da177e4SLinus Torvalds 		if (tag_number >= dcb->max_command) {
16055cd049a5SChristoph Hellwig 			dprintkl(KERN_WARNING, "start_scsi: (0x%p) "
16061da177e4SLinus Torvalds 				"Out of tags target=<%02i-%i>)\n",
16075cd049a5SChristoph Hellwig 				srb->cmd, srb->cmd->device->id,
16081da177e4SLinus Torvalds 				srb->cmd->device->lun);
16091da177e4SLinus Torvalds 			srb->state = SRB_READY;
16101da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
16111da177e4SLinus Torvalds 				       DO_HWRESELECT);
16121da177e4SLinus Torvalds 			return 1;
16131da177e4SLinus Torvalds 		}
16141da177e4SLinus Torvalds 		/* Send Tag id */
16151da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_SIMPLE_QTAG);
16161da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
16171da177e4SLinus Torvalds 		dcb->tag_mask |= tag_mask;
16181da177e4SLinus Torvalds 		srb->tag_number = tag_number;
16191da177e4SLinus Torvalds 		scsicommand = SCMD_SEL_ATN3;
16201da177e4SLinus Torvalds 		srb->state = SRB_START_;
16211da177e4SLinus Torvalds 	}
16221da177e4SLinus Torvalds #endif
16231da177e4SLinus Torvalds /*polling:*/
16241da177e4SLinus Torvalds 	/* Send CDB ..command block ......... */
16255cd049a5SChristoph Hellwig 	dprintkdbg(DBG_KG, "start_scsi: (0x%p) <%02i-%i> cmnd=0x%02x tag=%i\n",
16265cd049a5SChristoph Hellwig 		srb->cmd, srb->cmd->device->id, srb->cmd->device->lun,
16271da177e4SLinus Torvalds 		srb->cmd->cmnd[0], srb->tag_number);
16281da177e4SLinus Torvalds 	if (srb->flag & AUTO_REQSENSE) {
16291da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
16301da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
16311da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
16321da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1633b80ca4f7SFUJITA Tomonori 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
16341da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
16351da177e4SLinus Torvalds 	} else {
16361da177e4SLinus Torvalds 		ptr = (u8 *)srb->cmd->cmnd;
16371da177e4SLinus Torvalds 		for (i = 0; i < srb->cmd->cmd_len; i++)
16381da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds       no_cmd:
16411da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
16421da177e4SLinus Torvalds 		       DO_HWRESELECT | DO_DATALATCH);
16431da177e4SLinus Torvalds 	if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
16441da177e4SLinus Torvalds 		/*
16451da177e4SLinus Torvalds 		 * If start_scsi return 1:
16461da177e4SLinus Torvalds 		 * we caught an interrupt (must be reset or reselection ... )
16471da177e4SLinus Torvalds 		 * : Let's process it first!
16481da177e4SLinus Torvalds 		 */
16495cd049a5SChristoph Hellwig 		dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> Failed - busy\n",
16505cd049a5SChristoph Hellwig 			srb->cmd, dcb->target_id, dcb->target_lun);
16511da177e4SLinus Torvalds 		srb->state = SRB_READY;
16521da177e4SLinus Torvalds 		free_tag(dcb, srb);
16531da177e4SLinus Torvalds 		srb->msg_count = 0;
16541da177e4SLinus Torvalds 		return_code = 1;
16551da177e4SLinus Torvalds 		/* This IRQ should NOT get lost, as we did not acknowledge it */
16561da177e4SLinus Torvalds 	} else {
16571da177e4SLinus Torvalds 		/*
16581da177e4SLinus Torvalds 		 * If start_scsi returns 0:
16591da177e4SLinus Torvalds 		 * we know that the SCSI processor is free
16601da177e4SLinus Torvalds 		 */
16611da177e4SLinus Torvalds 		srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
16621da177e4SLinus Torvalds 		dcb->active_srb = srb;
16631da177e4SLinus Torvalds 		acb->active_dcb = dcb;
16641da177e4SLinus Torvalds 		return_code = 0;
16651da177e4SLinus Torvalds 		/* it's important for atn stop */
16661da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
16671da177e4SLinus Torvalds 			       DO_DATALATCH | DO_HWRESELECT);
16681da177e4SLinus Torvalds 		/* SCSI command */
16691da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
16701da177e4SLinus Torvalds 	}
16711da177e4SLinus Torvalds 	return return_code;
16721da177e4SLinus Torvalds }
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds #define DC395x_ENABLE_MSGOUT \
16761da177e4SLinus Torvalds  DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
16771da177e4SLinus Torvalds  srb->state |= SRB_MSGOUT
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds /* abort command */
16811da177e4SLinus Torvalds static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
16821da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
16831da177e4SLinus Torvalds {
16841da177e4SLinus Torvalds 	srb->msgout_buf[0] = ABORT;
16851da177e4SLinus Torvalds 	srb->msg_count = 1;
16861da177e4SLinus Torvalds 	DC395x_ENABLE_MSGOUT;
16871da177e4SLinus Torvalds 	srb->state &= ~SRB_MSGIN;
16881da177e4SLinus Torvalds 	srb->state |= SRB_MSGOUT;
16891da177e4SLinus Torvalds }
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds /**
16931da177e4SLinus Torvalds  * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
16941da177e4SLinus Torvalds  *                           have been triggered for this card.
16951da177e4SLinus Torvalds  *
16961da177e4SLinus Torvalds  * @acb:	 a pointer to the adpter control block
16971da177e4SLinus Torvalds  * @scsi_status: the status return when we checked the card
16981da177e4SLinus Torvalds  **/
16991da177e4SLinus Torvalds static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
17001da177e4SLinus Torvalds 		u16 scsi_status)
17011da177e4SLinus Torvalds {
17021da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
17031da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
17041da177e4SLinus Torvalds 	u16 phase;
17051da177e4SLinus Torvalds 	u8 scsi_intstatus;
17061da177e4SLinus Torvalds 	unsigned long flags;
17071da177e4SLinus Torvalds 	void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
17081da177e4SLinus Torvalds 			      u16 *);
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	/* This acknowledges the IRQ */
17131da177e4SLinus Torvalds 	scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
17141da177e4SLinus Torvalds 	if ((scsi_status & 0x2007) == 0x2002)
17151da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
17161da177e4SLinus Torvalds 			"COP after COP completed? %04x\n", scsi_status);
17171da177e4SLinus Torvalds 	if (debug_enabled(DBG_KG)) {
17181da177e4SLinus Torvalds 		if (scsi_intstatus & INT_SELTIMEOUT)
17191da177e4SLinus Torvalds 			dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
17201da177e4SLinus Torvalds 	}
17211da177e4SLinus Torvalds 	/*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	if (timer_pending(&acb->selto_timer))
17241da177e4SLinus Torvalds 		del_timer(&acb->selto_timer);
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
17271da177e4SLinus Torvalds 		disconnect(acb);	/* bus free interrupt  */
17281da177e4SLinus Torvalds 		goto out_unlock;
17291da177e4SLinus Torvalds 	}
17301da177e4SLinus Torvalds 	if (scsi_intstatus & INT_RESELECTED) {
17311da177e4SLinus Torvalds 		reselect(acb);
17321da177e4SLinus Torvalds 		goto out_unlock;
17331da177e4SLinus Torvalds 	}
17341da177e4SLinus Torvalds 	if (scsi_intstatus & INT_SELECT) {
17351da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Host does not support target mode!\n");
17361da177e4SLinus Torvalds 		goto out_unlock;
17371da177e4SLinus Torvalds 	}
17381da177e4SLinus Torvalds 	if (scsi_intstatus & INT_SCSIRESET) {
17391da177e4SLinus Torvalds 		scsi_reset_detect(acb);
17401da177e4SLinus Torvalds 		goto out_unlock;
17411da177e4SLinus Torvalds 	}
17421da177e4SLinus Torvalds 	if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
17431da177e4SLinus Torvalds 		dcb = acb->active_dcb;
17441da177e4SLinus Torvalds 		if (!dcb) {
17451da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
17461da177e4SLinus Torvalds 				"Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
17471da177e4SLinus Torvalds 				scsi_status, scsi_intstatus);
17481da177e4SLinus Torvalds 			goto out_unlock;
17491da177e4SLinus Torvalds 		}
17501da177e4SLinus Torvalds 		srb = dcb->active_srb;
17511da177e4SLinus Torvalds 		if (dcb->flag & ABORT_DEV_) {
17521da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
17531da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
17541da177e4SLinus Torvalds 		}
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 		/* software sequential machine */
17571da177e4SLinus Torvalds 		phase = (u16)srb->scsi_phase;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 		/*
17601da177e4SLinus Torvalds 		 * 62037 or 62137
17611da177e4SLinus Torvalds 		 * call  dc395x_scsi_phase0[]... "phase entry"
17621da177e4SLinus Torvalds 		 * handle every phase before start transfer
17631da177e4SLinus Torvalds 		 */
17641da177e4SLinus Torvalds 		/* data_out_phase0,	phase:0 */
17651da177e4SLinus Torvalds 		/* data_in_phase0,	phase:1 */
17661da177e4SLinus Torvalds 		/* command_phase0,	phase:2 */
17671da177e4SLinus Torvalds 		/* status_phase0,	phase:3 */
17681da177e4SLinus Torvalds 		/* nop0,		phase:4 PH_BUS_FREE .. initial phase */
17691da177e4SLinus Torvalds 		/* nop0,		phase:5 PH_BUS_FREE .. initial phase */
17701da177e4SLinus Torvalds 		/* msgout_phase0,	phase:6 */
17711da177e4SLinus Torvalds 		/* msgin_phase0,	phase:7 */
17721da177e4SLinus Torvalds 		dc395x_statev = dc395x_scsi_phase0[phase];
17731da177e4SLinus Torvalds 		dc395x_statev(acb, srb, &scsi_status);
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 		/*
177625985edcSLucas De Marchi 		 * if there were any exception occurred scsi_status
17771da177e4SLinus Torvalds 		 * will be modify to bus free phase new scsi_status
17781da177e4SLinus Torvalds 		 * transfer out from ... previous dc395x_statev
17791da177e4SLinus Torvalds 		 */
17801da177e4SLinus Torvalds 		srb->scsi_phase = scsi_status & PHASEMASK;
17811da177e4SLinus Torvalds 		phase = (u16)scsi_status & PHASEMASK;
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 		/*
17841da177e4SLinus Torvalds 		 * call  dc395x_scsi_phase1[]... "phase entry" handle
17851da177e4SLinus Torvalds 		 * every phase to do transfer
17861da177e4SLinus Torvalds 		 */
17871da177e4SLinus Torvalds 		/* data_out_phase1,	phase:0 */
17881da177e4SLinus Torvalds 		/* data_in_phase1,	phase:1 */
17891da177e4SLinus Torvalds 		/* command_phase1,	phase:2 */
17901da177e4SLinus Torvalds 		/* status_phase1,	phase:3 */
17911da177e4SLinus Torvalds 		/* nop1,		phase:4 PH_BUS_FREE .. initial phase */
17921da177e4SLinus Torvalds 		/* nop1,		phase:5 PH_BUS_FREE .. initial phase */
17931da177e4SLinus Torvalds 		/* msgout_phase1,	phase:6 */
17941da177e4SLinus Torvalds 		/* msgin_phase1,	phase:7 */
17951da177e4SLinus Torvalds 		dc395x_statev = dc395x_scsi_phase1[phase];
17961da177e4SLinus Torvalds 		dc395x_statev(acb, srb, &scsi_status);
17971da177e4SLinus Torvalds 	}
17981da177e4SLinus Torvalds       out_unlock:
17991da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
18001da177e4SLinus Torvalds }
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 
18037d12e780SDavid Howells static irqreturn_t dc395x_interrupt(int irq, void *dev_id)
18041da177e4SLinus Torvalds {
1805c7bec5abSJeff Garzik 	struct AdapterCtlBlk *acb = dev_id;
18061da177e4SLinus Torvalds 	u16 scsi_status;
18071da177e4SLinus Torvalds 	u8 dma_status;
18081da177e4SLinus Torvalds 	irqreturn_t handled = IRQ_NONE;
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	/*
18113a4fa0a2SRobert P. J. Day 	 * Check for pending interrupt
18121da177e4SLinus Torvalds 	 */
18131da177e4SLinus Torvalds 	scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
18141da177e4SLinus Torvalds 	dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
18151da177e4SLinus Torvalds 	if (scsi_status & SCSIINTERRUPT) {
18163a4fa0a2SRobert P. J. Day 		/* interrupt pending - let's process it! */
18171da177e4SLinus Torvalds 		dc395x_handle_interrupt(acb, scsi_status);
18181da177e4SLinus Torvalds 		handled = IRQ_HANDLED;
18191da177e4SLinus Torvalds 	}
18201da177e4SLinus Torvalds 	else if (dma_status & 0x20) {
18211da177e4SLinus Torvalds 		/* Error from the DMA engine */
18221da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
18231da177e4SLinus Torvalds #if 0
18241da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
18251da177e4SLinus Torvalds 		if (acb->active_dcb) {
18261da177e4SLinus Torvalds 			acb->active_dcb-> flag |= ABORT_DEV_;
18271da177e4SLinus Torvalds 			if (acb->active_dcb->active_srb)
18281da177e4SLinus Torvalds 				enable_msgout_abort(acb, acb->active_dcb->active_srb);
18291da177e4SLinus Torvalds 		}
18301da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
18311da177e4SLinus Torvalds #else
18321da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
18331da177e4SLinus Torvalds 		acb = NULL;
18341da177e4SLinus Torvalds #endif
18351da177e4SLinus Torvalds 		handled = IRQ_HANDLED;
18361da177e4SLinus Torvalds 	}
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 	return handled;
18391da177e4SLinus Torvalds }
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
18431da177e4SLinus Torvalds 		u16 *pscsi_status)
18441da177e4SLinus Torvalds {
18455cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgout_phase0: (0x%p)\n", srb->cmd);
18461da177e4SLinus Torvalds 	if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
18471da177e4SLinus Torvalds 		*pscsi_status = PH_BUS_FREE;	/*.. initial phase */
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
18501da177e4SLinus Torvalds 	srb->state &= ~SRB_MSGOUT;
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
18551da177e4SLinus Torvalds 		u16 *pscsi_status)
18561da177e4SLinus Torvalds {
18571da177e4SLinus Torvalds 	u16 i;
18581da177e4SLinus Torvalds 	u8 *ptr;
18595cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgout_phase1: (0x%p)\n", srb->cmd);
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	clear_fifo(acb, "msgout_phase1");
18621da177e4SLinus Torvalds 	if (!(srb->state & SRB_MSGOUT)) {
18631da177e4SLinus Torvalds 		srb->state |= SRB_MSGOUT;
18641da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
18655cd049a5SChristoph Hellwig 			"msgout_phase1: (0x%p) Phase unexpected\n",
18665cd049a5SChristoph Hellwig 			srb->cmd);	/* So what ? */
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds 	if (!srb->msg_count) {
18695cd049a5SChristoph Hellwig 		dprintkdbg(DBG_0, "msgout_phase1: (0x%p) NOP msg\n",
18705cd049a5SChristoph Hellwig 			srb->cmd);
18711da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, MSG_NOP);
18721da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
18731da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
18741da177e4SLinus Torvalds 		return;
18751da177e4SLinus Torvalds 	}
18761da177e4SLinus Torvalds 	ptr = (u8 *)srb->msgout_buf;
18771da177e4SLinus Torvalds 	for (i = 0; i < srb->msg_count; i++)
18781da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
18791da177e4SLinus Torvalds 	srb->msg_count = 0;
18801da177e4SLinus Torvalds 	if (srb->msgout_buf[0] == MSG_ABORT)
18811da177e4SLinus Torvalds 		srb->state = SRB_ABORT_SENT;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
18881da177e4SLinus Torvalds 		u16 *pscsi_status)
18891da177e4SLinus Torvalds {
18905cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "command_phase0: (0x%p)\n", srb->cmd);
18911da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
18961da177e4SLinus Torvalds 		u16 *pscsi_status)
18971da177e4SLinus Torvalds {
18981da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
18991da177e4SLinus Torvalds 	u8 *ptr;
19001da177e4SLinus Torvalds 	u16 i;
19015cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "command_phase1: (0x%p)\n", srb->cmd);
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	clear_fifo(acb, "command_phase1");
19041da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
19051da177e4SLinus Torvalds 	if (!(srb->flag & AUTO_REQSENSE)) {
19061da177e4SLinus Torvalds 		ptr = (u8 *)srb->cmd->cmnd;
19071da177e4SLinus Torvalds 		for (i = 0; i < srb->cmd->cmd_len; i++) {
19081da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
19091da177e4SLinus Torvalds 			ptr++;
19101da177e4SLinus Torvalds 		}
19111da177e4SLinus Torvalds 	} else {
19121da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
19131da177e4SLinus Torvalds 		dcb = acb->active_dcb;
19141da177e4SLinus Torvalds 		/* target id */
19151da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
19161da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
19171da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
1918b80ca4f7SFUJITA Tomonori 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
19191da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
19201da177e4SLinus Torvalds 	}
19211da177e4SLinus Torvalds 	srb->state |= SRB_COMMAND;
19221da177e4SLinus Torvalds 	/* it's important for atn stop */
19231da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
19241da177e4SLinus Torvalds 	/* SCSI command */
19251da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds /*
19301da177e4SLinus Torvalds  * Verify that the remaining space in the hw sg lists is the same as
19311da177e4SLinus Torvalds  * the count of remaining bytes in srb->total_xfer_length
19321da177e4SLinus Torvalds  */
19331da177e4SLinus Torvalds static void sg_verify_length(struct ScsiReqBlk *srb)
19341da177e4SLinus Torvalds {
19351da177e4SLinus Torvalds 	if (debug_enabled(DBG_SG)) {
19361da177e4SLinus Torvalds 		unsigned len = 0;
19371da177e4SLinus Torvalds 		unsigned idx = srb->sg_index;
19381da177e4SLinus Torvalds 		struct SGentry *psge = srb->segment_x + idx;
19391da177e4SLinus Torvalds 		for (; idx < srb->sg_count; psge++, idx++)
19401da177e4SLinus Torvalds 			len += psge->length;
19411da177e4SLinus Torvalds 		if (len != srb->total_xfer_length)
19421da177e4SLinus Torvalds 			dprintkdbg(DBG_SG,
19431da177e4SLinus Torvalds 			       "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
19441da177e4SLinus Torvalds 			       srb->total_xfer_length, len);
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds }
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds /*
19501da177e4SLinus Torvalds  * Compute the next Scatter Gather list index and adjust its length
1951cdb8c2a6SGuennadi Liakhovetski  * and address if necessary
19521da177e4SLinus Torvalds  */
19531da177e4SLinus Torvalds static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
19541da177e4SLinus Torvalds {
19551da177e4SLinus Torvalds 	u8 idx;
195625985edcSLucas De Marchi 	u32 xferred = srb->total_xfer_length - left; /* bytes transferred */
19571da177e4SLinus Torvalds 	struct SGentry *psge = srb->segment_x + srb->sg_index;
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
196025985edcSLucas De Marchi 		"sg_update_list: Transferred %i of %i bytes, %i remain\n",
19611da177e4SLinus Torvalds 		xferred, srb->total_xfer_length, left);
19621da177e4SLinus Torvalds 	if (xferred == 0) {
19631da177e4SLinus Torvalds 		/* nothing to update since we did not transfer any data */
19641da177e4SLinus Torvalds 		return;
19651da177e4SLinus Torvalds 	}
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	sg_verify_length(srb);
19681da177e4SLinus Torvalds 	srb->total_xfer_length = left;	/* update remaining count */
19691da177e4SLinus Torvalds 	for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
19701da177e4SLinus Torvalds 		if (xferred >= psge->length) {
19711da177e4SLinus Torvalds 			/* Complete SG entries done */
19721da177e4SLinus Torvalds 			xferred -= psge->length;
19731da177e4SLinus Torvalds 		} else {
19741da177e4SLinus Torvalds 			/* Partial SG entry done */
19751da177e4SLinus Torvalds 			psge->length -= xferred;
19761da177e4SLinus Torvalds 			psge->address += xferred;
19771da177e4SLinus Torvalds 			srb->sg_index = idx;
19781da177e4SLinus Torvalds 			pci_dma_sync_single_for_device(srb->dcb->
19791da177e4SLinus Torvalds 					    acb->dev,
19801da177e4SLinus Torvalds 					    srb->sg_bus_addr,
19811da177e4SLinus Torvalds 					    SEGMENTX_LEN,
19821da177e4SLinus Torvalds 					    PCI_DMA_TODEVICE);
19831da177e4SLinus Torvalds 			break;
19841da177e4SLinus Torvalds 		}
19851da177e4SLinus Torvalds 		psge++;
19861da177e4SLinus Torvalds 	}
19871da177e4SLinus Torvalds 	sg_verify_length(srb);
19881da177e4SLinus Torvalds }
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds /*
199225985edcSLucas De Marchi  * We have transferred a single byte (PIO mode?) and need to update
19931da177e4SLinus Torvalds  * the count of bytes remaining (total_xfer_length) and update the sg
19941da177e4SLinus Torvalds  * entry to either point to next byte in the current sg entry, or of
19951da177e4SLinus Torvalds  * already at the end to point to the start of the next sg entry
19961da177e4SLinus Torvalds  */
19971da177e4SLinus Torvalds static void sg_subtract_one(struct ScsiReqBlk *srb)
19981da177e4SLinus Torvalds {
1999cdb8c2a6SGuennadi Liakhovetski 	sg_update_list(srb, srb->total_xfer_length - 1);
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds /*
20041da177e4SLinus Torvalds  * cleanup_after_transfer
20051da177e4SLinus Torvalds  *
20061da177e4SLinus Torvalds  * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
20071da177e4SLinus Torvalds  * KG: Currently called from  StatusPhase1 ()
20081da177e4SLinus Torvalds  * Should probably also be called from other places
20091da177e4SLinus Torvalds  * Best might be to call it in DataXXPhase0, if new phase will differ
20101da177e4SLinus Torvalds  */
20111da177e4SLinus Torvalds static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
20121da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
20131da177e4SLinus Torvalds {
20141da177e4SLinus Torvalds 	/*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
20151da177e4SLinus Torvalds 	if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) {	/* read */
20161da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
20171da177e4SLinus Torvalds 			clear_fifo(acb, "cleanup/in");
20181da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
20191da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
20201da177e4SLinus Torvalds 	} else {		/* write */
20211da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
20221da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
20231da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
20241da177e4SLinus Torvalds 			clear_fifo(acb, "cleanup/out");
20251da177e4SLinus Torvalds 	}
20261da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds /*
203125985edcSLucas De Marchi  * Those no of bytes will be transferred w/ PIO through the SCSI FIFO
20321da177e4SLinus Torvalds  * Seems to be needed for unknown reasons; could be a hardware bug :-(
20331da177e4SLinus Torvalds  */
20341da177e4SLinus Torvalds #define DC395x_LASTPIO 4
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
20381da177e4SLinus Torvalds 		u16 *pscsi_status)
20391da177e4SLinus Torvalds {
20401da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
20411da177e4SLinus Torvalds 	u16 scsi_status = *pscsi_status;
20421da177e4SLinus Torvalds 	u32 d_left_counter = 0;
20435cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_out_phase0: (0x%p) <%02i-%i>\n",
20445cd049a5SChristoph Hellwig 		srb->cmd, srb->cmd->device->id, srb->cmd->device->lun);
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 	/*
20471da177e4SLinus Torvalds 	 * KG: We need to drain the buffers before we draw any conclusions!
20481da177e4SLinus Torvalds 	 * This means telling the DMA to push the rest into SCSI, telling
20491da177e4SLinus Torvalds 	 * SCSI to push the rest to the bus.
20501da177e4SLinus Torvalds 	 * However, the device might have been the one to stop us (phase
20511da177e4SLinus Torvalds 	 * change), and the data in transit just needs to be accounted so
20521da177e4SLinus Torvalds 	 * it can be retransmitted.)
20531da177e4SLinus Torvalds 	 */
20541da177e4SLinus Torvalds 	/*
20551da177e4SLinus Torvalds 	 * KG: Stop DMA engine pushing more data into the SCSI FIFO
20561da177e4SLinus Torvalds 	 * If we need more data, the DMA SG list will be freshly set up, anyway
20571da177e4SLinus Torvalds 	 */
20581da177e4SLinus Torvalds 	dprintkdbg(DBG_PIO, "data_out_phase0: "
2059cdb8c2a6SGuennadi Liakhovetski 		"DMA{fifocnt=0x%02x fifostat=0x%02x} "
20601da177e4SLinus Torvalds 		"SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
20611da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
20621da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
20631da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
20641da177e4SLinus Torvalds 		DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
20651da177e4SLinus Torvalds 		srb->total_xfer_length);
20661da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 	if (!(srb->state & SRB_XFERPAD)) {
20691da177e4SLinus Torvalds 		if (scsi_status & PARITYERROR)
20701da177e4SLinus Torvalds 			srb->status |= PARITY_ERROR;
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 		/*
20731da177e4SLinus Torvalds 		 * KG: Right, we can't just rely on the SCSI_COUNTER, because this
20741da177e4SLinus Torvalds 		 * is the no of bytes it got from the DMA engine not the no it
20751da177e4SLinus Torvalds 		 * transferred successfully to the device. (And the difference could
20761da177e4SLinus Torvalds 		 * be as much as the FIFO size, I guess ...)
20771da177e4SLinus Torvalds 		 */
20781da177e4SLinus Torvalds 		if (!(scsi_status & SCSIXFERDONE)) {
20791da177e4SLinus Torvalds 			/*
20801da177e4SLinus Torvalds 			 * when data transfer from DMA FIFO to SCSI FIFO
20811da177e4SLinus Torvalds 			 * if there was some data left in SCSI FIFO
20821da177e4SLinus Torvalds 			 */
20831da177e4SLinus Torvalds 			d_left_counter =
20841da177e4SLinus Torvalds 			    (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
20851da177e4SLinus Torvalds 				  0x1F);
20861da177e4SLinus Torvalds 			if (dcb->sync_period & WIDE_SYNC)
20871da177e4SLinus Torvalds 				d_left_counter <<= 1;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 			dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
20901da177e4SLinus Torvalds 				"SCSI{fifocnt=0x%02x cnt=0x%08x} "
20911da177e4SLinus Torvalds 				"DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
20921da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
20931da177e4SLinus Torvalds 				(dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
20941da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
20951da177e4SLinus Torvalds 				DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
20961da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
20971da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
20981da177e4SLinus Torvalds 				DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
20991da177e4SLinus Torvalds 		}
21001da177e4SLinus Torvalds 		/*
2101b4b08e58SLinus Torvalds 		 * calculate all the residue data that not yet tranfered
21021da177e4SLinus Torvalds 		 * SCSI transfer counter + left in SCSI FIFO data
21031da177e4SLinus Torvalds 		 *
21041da177e4SLinus Torvalds 		 * .....TRM_S1040_SCSI_COUNTER (24bits)
21051da177e4SLinus Torvalds 		 * The counter always decrement by one for every SCSI byte transfer.
21061da177e4SLinus Torvalds 		 * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
21071da177e4SLinus Torvalds 		 * The counter is SCSI FIFO offset counter (in units of bytes or! words)
21081da177e4SLinus Torvalds 		 */
21091da177e4SLinus Torvalds 		if (srb->total_xfer_length > DC395x_LASTPIO)
21101da177e4SLinus Torvalds 			d_left_counter +=
21111da177e4SLinus Torvalds 			    DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 		/* Is this a good idea? */
21141da177e4SLinus Torvalds 		/*clear_fifo(acb, "DOP1"); */
21151da177e4SLinus Torvalds 		/* KG: What is this supposed to be useful for? WIDE padding stuff? */
21161da177e4SLinus Torvalds 		if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
2117a862ea31SFUJITA Tomonori 		    && scsi_bufflen(srb->cmd) % 2) {
21181da177e4SLinus Torvalds 			d_left_counter = 0;
21191da177e4SLinus Torvalds 			dprintkl(KERN_INFO,
21201da177e4SLinus Torvalds 				"data_out_phase0: Discard 1 byte (0x%02x)\n",
21211da177e4SLinus Torvalds 				scsi_status);
21221da177e4SLinus Torvalds 		}
21231da177e4SLinus Torvalds 		/*
21241da177e4SLinus Torvalds 		 * KG: Oops again. Same thinko as above: The SCSI might have been
21251da177e4SLinus Torvalds 		 * faster than the DMA engine, so that it ran out of data.
21261da177e4SLinus Torvalds 		 * In that case, we have to do just nothing!
21271da177e4SLinus Torvalds 		 * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
21281da177e4SLinus Torvalds 		 */
21291da177e4SLinus Torvalds 		/*
21301da177e4SLinus Torvalds 		 * KG: This is nonsense: We have been WRITING data to the bus
21311da177e4SLinus Torvalds 		 * If the SCSI engine has no bytes left, how should the DMA engine?
21321da177e4SLinus Torvalds 		 */
21331da177e4SLinus Torvalds 		if (d_left_counter == 0) {
21341da177e4SLinus Torvalds 			srb->total_xfer_length = 0;
21351da177e4SLinus Torvalds 		} else {
21361da177e4SLinus Torvalds 			/*
21371da177e4SLinus Torvalds 			 * if transfer not yet complete
21381da177e4SLinus Torvalds 			 * there were some data residue in SCSI FIFO or
21391da177e4SLinus Torvalds 			 * SCSI transfer counter not empty
21401da177e4SLinus Torvalds 			 */
21411da177e4SLinus Torvalds 			long oldxferred =
21421da177e4SLinus Torvalds 			    srb->total_xfer_length - d_left_counter;
21431da177e4SLinus Torvalds 			const int diff =
21441da177e4SLinus Torvalds 			    (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
21451da177e4SLinus Torvalds 			sg_update_list(srb, d_left_counter);
21461da177e4SLinus Torvalds 			/* KG: Most ugly hack! Apparently, this works around a chip bug */
21471da177e4SLinus Torvalds 			if ((srb->segment_x[srb->sg_index].length ==
2148a862ea31SFUJITA Tomonori 			     diff && scsi_sg_count(srb->cmd))
21491da177e4SLinus Torvalds 			    || ((oldxferred & ~PAGE_MASK) ==
21501da177e4SLinus Torvalds 				(PAGE_SIZE - diff))
21511da177e4SLinus Torvalds 			    ) {
21521da177e4SLinus Torvalds 				dprintkl(KERN_INFO, "data_out_phase0: "
21531da177e4SLinus Torvalds 					"Work around chip bug (%i)?\n", diff);
21541da177e4SLinus Torvalds 				d_left_counter =
21551da177e4SLinus Torvalds 				    srb->total_xfer_length - diff;
21561da177e4SLinus Torvalds 				sg_update_list(srb, d_left_counter);
21571da177e4SLinus Torvalds 				/*srb->total_xfer_length -= diff; */
21581da177e4SLinus Torvalds 				/*srb->virt_addr += diff; */
21591da177e4SLinus Torvalds 				/*if (srb->cmd->use_sg) */
21601da177e4SLinus Torvalds 				/*      srb->sg_index++; */
21611da177e4SLinus Torvalds 			}
21621da177e4SLinus Torvalds 		}
21631da177e4SLinus Torvalds 	}
21641da177e4SLinus Torvalds 	if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
21651da177e4SLinus Torvalds 		cleanup_after_transfer(acb, srb);
21661da177e4SLinus Torvalds 	}
21671da177e4SLinus Torvalds }
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
21711da177e4SLinus Torvalds 		u16 *pscsi_status)
21721da177e4SLinus Torvalds {
21735cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_out_phase1: (0x%p) <%02i-%i>\n",
21745cd049a5SChristoph Hellwig 		srb->cmd, srb->cmd->device->id, srb->cmd->device->lun);
21751da177e4SLinus Torvalds 	clear_fifo(acb, "data_out_phase1");
21761da177e4SLinus Torvalds 	/* do prepare before transfer when data out phase */
21771da177e4SLinus Torvalds 	data_io_transfer(acb, srb, XFERDATAOUT);
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
21811da177e4SLinus Torvalds 		u16 *pscsi_status)
21821da177e4SLinus Torvalds {
21831da177e4SLinus Torvalds 	u16 scsi_status = *pscsi_status;
2184cdb8c2a6SGuennadi Liakhovetski 
21855cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_in_phase0: (0x%p) <%02i-%i>\n",
21865cd049a5SChristoph Hellwig 		srb->cmd, srb->cmd->device->id, srb->cmd->device->lun);
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	/*
21891da177e4SLinus Torvalds 	 * KG: DataIn is much more tricky than DataOut. When the device is finished
21901da177e4SLinus Torvalds 	 * and switches to another phase, the SCSI engine should be finished too.
21911da177e4SLinus Torvalds 	 * But: There might still be bytes left in its FIFO to be fetched by the DMA
21921da177e4SLinus Torvalds 	 * engine and transferred to memory.
21931da177e4SLinus Torvalds 	 * We should wait for the FIFOs to be emptied by that (is there any way to
21941da177e4SLinus Torvalds 	 * enforce this?) and then stop the DMA engine, because it might think, that
21951da177e4SLinus Torvalds 	 * there are more bytes to follow. Yes, the device might disconnect prior to
21961da177e4SLinus Torvalds 	 * having all bytes transferred!
21971da177e4SLinus Torvalds 	 * Also we should make sure that all data from the DMA engine buffer's really
21981da177e4SLinus Torvalds 	 * made its way to the system memory! Some documentation on this would not
21991da177e4SLinus Torvalds 	 * seem to be a bad idea, actually.
22001da177e4SLinus Torvalds 	 */
22011da177e4SLinus Torvalds 	if (!(srb->state & SRB_XFERPAD)) {
2202cdb8c2a6SGuennadi Liakhovetski 		u32 d_left_counter;
2203cdb8c2a6SGuennadi Liakhovetski 		unsigned int sc, fc;
2204cdb8c2a6SGuennadi Liakhovetski 
22051da177e4SLinus Torvalds 		if (scsi_status & PARITYERROR) {
22065cd049a5SChristoph Hellwig 			dprintkl(KERN_INFO, "data_in_phase0: (0x%p) "
22075cd049a5SChristoph Hellwig 				"Parity Error\n", srb->cmd);
22081da177e4SLinus Torvalds 			srb->status |= PARITY_ERROR;
22091da177e4SLinus Torvalds 		}
22101da177e4SLinus Torvalds 		/*
22111da177e4SLinus Torvalds 		 * KG: We should wait for the DMA FIFO to be empty ...
22121da177e4SLinus Torvalds 		 * but: it would be better to wait first for the SCSI FIFO and then the
22131da177e4SLinus Torvalds 		 * the DMA FIFO to become empty? How do we know, that the device not already
22141da177e4SLinus Torvalds 		 * sent data to the FIFO in a MsgIn phase, eg.?
22151da177e4SLinus Torvalds 		 */
22161da177e4SLinus Torvalds 		if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
22171da177e4SLinus Torvalds #if 0
22181da177e4SLinus Torvalds 			int ctr = 6000000;
22191da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
22201da177e4SLinus Torvalds 				"DIP0: Wait for DMA FIFO to flush ...\n");
22211da177e4SLinus Torvalds 			/*DC395x_write8  (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
22221da177e4SLinus Torvalds 			/*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
22231da177e4SLinus Torvalds 			/*DC395x_write8  (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
22241da177e4SLinus Torvalds 			while (!
22251da177e4SLinus Torvalds 			       (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
22261da177e4SLinus Torvalds 				0x80) && --ctr);
22271da177e4SLinus Torvalds 			if (ctr < 6000000 - 1)
22281da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG
22291da177e4SLinus Torvalds 				       "DIP0: Had to wait for DMA ...\n");
22301da177e4SLinus Torvalds 			if (!ctr)
22311da177e4SLinus Torvalds 				dprintkl(KERN_ERR,
22321da177e4SLinus Torvalds 				       "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
22331da177e4SLinus Torvalds 			/*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
22341da177e4SLinus Torvalds #endif
22351da177e4SLinus Torvalds 			dprintkdbg(DBG_KG, "data_in_phase0: "
22361da177e4SLinus Torvalds 				"DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
22371da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
22381da177e4SLinus Torvalds 				DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
22391da177e4SLinus Torvalds 		}
22401da177e4SLinus Torvalds 		/* Now: Check remainig data: The SCSI counters should tell us ... */
2241cdb8c2a6SGuennadi Liakhovetski 		sc = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
2242cdb8c2a6SGuennadi Liakhovetski 		fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2243cdb8c2a6SGuennadi Liakhovetski 		d_left_counter = sc + ((fc & 0x1f)
22441da177e4SLinus Torvalds 		       << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
22451da177e4SLinus Torvalds 			   0));
22461da177e4SLinus Torvalds 		dprintkdbg(DBG_KG, "data_in_phase0: "
22471da177e4SLinus Torvalds 			"SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
22481da177e4SLinus Torvalds 			"DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
22491da177e4SLinus Torvalds 			"Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
2250cdb8c2a6SGuennadi Liakhovetski 			fc,
22511da177e4SLinus Torvalds 			(srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
2252cdb8c2a6SGuennadi Liakhovetski 			sc,
2253cdb8c2a6SGuennadi Liakhovetski 			fc,
22541da177e4SLinus Torvalds 			DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
22551da177e4SLinus Torvalds 			DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
22561da177e4SLinus Torvalds 			srb->total_xfer_length, d_left_counter);
22571da177e4SLinus Torvalds #if DC395x_LASTPIO
225825985edcSLucas De Marchi 		/* KG: Less than or equal to 4 bytes can not be transferred via DMA, it seems. */
22591da177e4SLinus Torvalds 		if (d_left_counter
22601da177e4SLinus Torvalds 		    && srb->total_xfer_length <= DC395x_LASTPIO) {
2261cdb8c2a6SGuennadi Liakhovetski 			size_t left_io = srb->total_xfer_length;
2262cdb8c2a6SGuennadi Liakhovetski 
22631da177e4SLinus Torvalds 			/*u32 addr = (srb->segment_x[srb->sg_index].address); */
22641da177e4SLinus Torvalds 			/*sg_update_list (srb, d_left_counter); */
2265cdb8c2a6SGuennadi Liakhovetski 			dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) "
2266cdb8c2a6SGuennadi Liakhovetski 				   "for remaining %i bytes:",
2267cdb8c2a6SGuennadi Liakhovetski 				fc & 0x1f,
22681da177e4SLinus Torvalds 				(srb->dcb->sync_period & WIDE_SYNC) ?
22691da177e4SLinus Torvalds 				    "words" : "bytes",
22701da177e4SLinus Torvalds 				srb->total_xfer_length);
22711da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC)
22721da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
22731da177e4SLinus Torvalds 					      CFG2_WIDEFIFO);
2274cdb8c2a6SGuennadi Liakhovetski 			while (left_io) {
2275cdb8c2a6SGuennadi Liakhovetski 				unsigned char *virt, *base = NULL;
2276cdb8c2a6SGuennadi Liakhovetski 				unsigned long flags = 0;
2277cdb8c2a6SGuennadi Liakhovetski 				size_t len = left_io;
2278cdb8c2a6SGuennadi Liakhovetski 				size_t offset = srb->request_length - left_io;
2279a862ea31SFUJITA Tomonori 
2280cdb8c2a6SGuennadi Liakhovetski 				local_irq_save(flags);
2281cdb8c2a6SGuennadi Liakhovetski 				/* Assumption: it's inside one page as it's at most 4 bytes and
2282cdb8c2a6SGuennadi Liakhovetski 				   I just assume it's on a 4-byte boundary */
2283a862ea31SFUJITA Tomonori 				base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2284cdb8c2a6SGuennadi Liakhovetski 							   srb->sg_count, &offset, &len);
2285cdb8c2a6SGuennadi Liakhovetski 				virt = base + offset;
2286a862ea31SFUJITA Tomonori 
2287cdb8c2a6SGuennadi Liakhovetski 				left_io -= len;
2288cdb8c2a6SGuennadi Liakhovetski 
2289cdb8c2a6SGuennadi Liakhovetski 				while (len) {
2290cdb8c2a6SGuennadi Liakhovetski 					u8 byte;
2291cdb8c2a6SGuennadi Liakhovetski 					byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2292cdb8c2a6SGuennadi Liakhovetski 					*virt++ = byte;
2293cdb8c2a6SGuennadi Liakhovetski 
22941da177e4SLinus Torvalds 					if (debug_enabled(DBG_PIO))
22951da177e4SLinus Torvalds 						printk(" %02x", byte);
2296cdb8c2a6SGuennadi Liakhovetski 
22971da177e4SLinus Torvalds 					d_left_counter--;
22981da177e4SLinus Torvalds 					sg_subtract_one(srb);
2299cdb8c2a6SGuennadi Liakhovetski 
2300cdb8c2a6SGuennadi Liakhovetski 					len--;
2301cdb8c2a6SGuennadi Liakhovetski 
2302cdb8c2a6SGuennadi Liakhovetski 					fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
2303cdb8c2a6SGuennadi Liakhovetski 
2304cdb8c2a6SGuennadi Liakhovetski 					if (fc == 0x40) {
2305cdb8c2a6SGuennadi Liakhovetski 						left_io = 0;
2306cdb8c2a6SGuennadi Liakhovetski 						break;
23071da177e4SLinus Torvalds 					}
2308cdb8c2a6SGuennadi Liakhovetski 				}
2309cdb8c2a6SGuennadi Liakhovetski 
2310cdb8c2a6SGuennadi Liakhovetski 				WARN_ON((fc != 0x40) == !d_left_counter);
2311cdb8c2a6SGuennadi Liakhovetski 
2312cdb8c2a6SGuennadi Liakhovetski 				if (fc == 0x40 && (srb->dcb->sync_period & WIDE_SYNC)) {
23131da177e4SLinus Torvalds 					/* Read the last byte ... */
23141da177e4SLinus Torvalds 					if (srb->total_xfer_length > 0) {
23151da177e4SLinus Torvalds 						u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2316cdb8c2a6SGuennadi Liakhovetski 
2317cdb8c2a6SGuennadi Liakhovetski 						*virt++ = byte;
23181da177e4SLinus Torvalds 						srb->total_xfer_length--;
23191da177e4SLinus Torvalds 						if (debug_enabled(DBG_PIO))
23201da177e4SLinus Torvalds 							printk(" %02x", byte);
23211da177e4SLinus Torvalds 					}
2322cdb8c2a6SGuennadi Liakhovetski 
23231da177e4SLinus Torvalds 					DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
23241da177e4SLinus Torvalds 				}
2325cdb8c2a6SGuennadi Liakhovetski 
2326cdb8c2a6SGuennadi Liakhovetski 				scsi_kunmap_atomic_sg(base);
2327cdb8c2a6SGuennadi Liakhovetski 				local_irq_restore(flags);
2328cdb8c2a6SGuennadi Liakhovetski 			}
23291da177e4SLinus Torvalds 			/*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
23301da177e4SLinus Torvalds 			/*srb->total_xfer_length = 0; */
23311da177e4SLinus Torvalds 			if (debug_enabled(DBG_PIO))
23321da177e4SLinus Torvalds 				printk("\n");
23331da177e4SLinus Torvalds 		}
23341da177e4SLinus Torvalds #endif				/* DC395x_LASTPIO */
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds #if 0
23371da177e4SLinus Torvalds 		/*
23381da177e4SLinus Torvalds 		 * KG: This was in DATAOUT. Does it also belong here?
23391da177e4SLinus Torvalds 		 * Nobody seems to know what counter and fifo_cnt count exactly ...
23401da177e4SLinus Torvalds 		 */
23411da177e4SLinus Torvalds 		if (!(scsi_status & SCSIXFERDONE)) {
23421da177e4SLinus Torvalds 			/*
23431da177e4SLinus Torvalds 			 * when data transfer from DMA FIFO to SCSI FIFO
23441da177e4SLinus Torvalds 			 * if there was some data left in SCSI FIFO
23451da177e4SLinus Torvalds 			 */
23461da177e4SLinus Torvalds 			d_left_counter =
23471da177e4SLinus Torvalds 			    (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
23481da177e4SLinus Torvalds 				  0x1F);
23491da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC)
23501da177e4SLinus Torvalds 				d_left_counter <<= 1;
23511da177e4SLinus Torvalds 			/*
23521da177e4SLinus Torvalds 			 * if WIDE scsi SCSI FIFOCNT unit is word !!!
23531da177e4SLinus Torvalds 			 * so need to *= 2
23541da177e4SLinus Torvalds 			 * KG: Seems to be correct ...
23551da177e4SLinus Torvalds 			 */
23561da177e4SLinus Torvalds 		}
23571da177e4SLinus Torvalds #endif
23581da177e4SLinus Torvalds 		/* KG: This should not be needed any more! */
23591da177e4SLinus Torvalds 		if (d_left_counter == 0
23601da177e4SLinus Torvalds 		    || (scsi_status & SCSIXFERCNT_2_ZERO)) {
23611da177e4SLinus Torvalds #if 0
23621da177e4SLinus Torvalds 			int ctr = 6000000;
23631da177e4SLinus Torvalds 			u8 TempDMAstatus;
23641da177e4SLinus Torvalds 			do {
23651da177e4SLinus Torvalds 				TempDMAstatus =
23661da177e4SLinus Torvalds 				    DC395x_read8(acb, TRM_S1040_DMA_STATUS);
23671da177e4SLinus Torvalds 			} while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
23681da177e4SLinus Torvalds 			if (!ctr)
23691da177e4SLinus Torvalds 				dprintkl(KERN_ERR,
23701da177e4SLinus Torvalds 				       "Deadlock in DataInPhase0 waiting for DMA!!\n");
23711da177e4SLinus Torvalds 			srb->total_xfer_length = 0;
23721da177e4SLinus Torvalds #endif
23731da177e4SLinus Torvalds 			srb->total_xfer_length = d_left_counter;
23741da177e4SLinus Torvalds 		} else {	/* phase changed */
23751da177e4SLinus Torvalds 			/*
23761da177e4SLinus Torvalds 			 * parsing the case:
23771da177e4SLinus Torvalds 			 * when a transfer not yet complete
23781da177e4SLinus Torvalds 			 * but be disconnected by target
23791da177e4SLinus Torvalds 			 * if transfer not yet complete
23801da177e4SLinus Torvalds 			 * there were some data residue in SCSI FIFO or
23811da177e4SLinus Torvalds 			 * SCSI transfer counter not empty
23821da177e4SLinus Torvalds 			 */
23831da177e4SLinus Torvalds 			sg_update_list(srb, d_left_counter);
23841da177e4SLinus Torvalds 		}
23851da177e4SLinus Torvalds 	}
23861da177e4SLinus Torvalds 	/* KG: The target may decide to disconnect: Empty FIFO before! */
23871da177e4SLinus Torvalds 	if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
23881da177e4SLinus Torvalds 		cleanup_after_transfer(acb, srb);
23891da177e4SLinus Torvalds 	}
23901da177e4SLinus Torvalds }
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
23941da177e4SLinus Torvalds 		u16 *pscsi_status)
23951da177e4SLinus Torvalds {
23965cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "data_in_phase1: (0x%p) <%02i-%i>\n",
23975cd049a5SChristoph Hellwig 		srb->cmd, srb->cmd->device->id, srb->cmd->device->lun);
23981da177e4SLinus Torvalds 	data_io_transfer(acb, srb, XFERDATAIN);
23991da177e4SLinus Torvalds }
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds static void data_io_transfer(struct AdapterCtlBlk *acb,
24031da177e4SLinus Torvalds 		struct ScsiReqBlk *srb, u16 io_dir)
24041da177e4SLinus Torvalds {
24051da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
24061da177e4SLinus Torvalds 	u8 bval;
24071da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
24085cd049a5SChristoph Hellwig 		"data_io_transfer: (0x%p) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
24095cd049a5SChristoph Hellwig 		srb->cmd, srb->cmd->device->id, srb->cmd->device->lun,
24101da177e4SLinus Torvalds 		((io_dir & DMACMD_DIR) ? 'r' : 'w'),
24111da177e4SLinus Torvalds 		srb->total_xfer_length, srb->sg_index, srb->sg_count);
24121da177e4SLinus Torvalds 	if (srb == acb->tmp_srb)
24131da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
24141da177e4SLinus Torvalds 	if (srb->sg_index >= srb->sg_count) {
24151da177e4SLinus Torvalds 		/* can't happen? out of bounds error */
24161da177e4SLinus Torvalds 		return;
24171da177e4SLinus Torvalds 	}
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds 	if (srb->total_xfer_length > DC395x_LASTPIO) {
24201da177e4SLinus Torvalds 		u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
24211da177e4SLinus Torvalds 		/*
24221da177e4SLinus Torvalds 		 * KG: What should we do: Use SCSI Cmd 0x90/0x92?
24231da177e4SLinus Torvalds 		 * Maybe, even ABORTXFER would be appropriate
24241da177e4SLinus Torvalds 		 */
24251da177e4SLinus Torvalds 		if (dma_status & XFERPENDING) {
24261da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
24271da177e4SLinus Torvalds 				"Expect trouble!\n");
24281da177e4SLinus Torvalds 			dump_register_info(acb, dcb, srb);
24291da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
24301da177e4SLinus Torvalds 		}
24311da177e4SLinus Torvalds 		/* clear_fifo(acb, "IO"); */
24321da177e4SLinus Torvalds 		/*
24331da177e4SLinus Torvalds 		 * load what physical address of Scatter/Gather list table
24341da177e4SLinus Torvalds 		 * want to be transfer
24351da177e4SLinus Torvalds 		 */
24361da177e4SLinus Torvalds 		srb->state |= SRB_DATA_XFER;
24371da177e4SLinus Torvalds 		DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
2438a862ea31SFUJITA Tomonori 		if (scsi_sg_count(srb->cmd)) {	/* with S/G */
24391da177e4SLinus Torvalds 			io_dir |= DMACMD_SG;
24401da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
24411da177e4SLinus Torvalds 				       srb->sg_bus_addr +
24421da177e4SLinus Torvalds 				       sizeof(struct SGentry) *
24431da177e4SLinus Torvalds 				       srb->sg_index);
24441da177e4SLinus Torvalds 			/* load how many bytes in the sg list table */
24451da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XCNT,
24461da177e4SLinus Torvalds 				       ((u32)(srb->sg_count -
24471da177e4SLinus Torvalds 					      srb->sg_index) << 3));
24481da177e4SLinus Torvalds 		} else {	/* without S/G */
24491da177e4SLinus Torvalds 			io_dir &= ~DMACMD_SG;
24501da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
24511da177e4SLinus Torvalds 				       srb->segment_x[0].address);
24521da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_DMA_XCNT,
24531da177e4SLinus Torvalds 				       srb->segment_x[0].length);
24541da177e4SLinus Torvalds 		}
24551da177e4SLinus Torvalds 		/* load total transfer length (24bits) max value 16Mbyte */
24561da177e4SLinus Torvalds 		DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
24571da177e4SLinus Torvalds 			       srb->total_xfer_length);
24581da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
24591da177e4SLinus Torvalds 		if (io_dir & DMACMD_DIR) {	/* read */
24601da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
24611da177e4SLinus Torvalds 				      SCMD_DMA_IN);
24621da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
24631da177e4SLinus Torvalds 		} else {
24641da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
24651da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
24661da177e4SLinus Torvalds 				      SCMD_DMA_OUT);
24671da177e4SLinus Torvalds 		}
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds 	}
24701da177e4SLinus Torvalds #if DC395x_LASTPIO
24711da177e4SLinus Torvalds 	else if (srb->total_xfer_length > 0) {	/* The last four bytes: Do PIO */
24721da177e4SLinus Torvalds 		/*
24731da177e4SLinus Torvalds 		 * load what physical address of Scatter/Gather list table
24741da177e4SLinus Torvalds 		 * want to be transfer
24751da177e4SLinus Torvalds 		 */
24761da177e4SLinus Torvalds 		srb->state |= SRB_DATA_XFER;
24771da177e4SLinus Torvalds 		/* load total transfer length (24bits) max value 16Mbyte */
24781da177e4SLinus Torvalds 		DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
24791da177e4SLinus Torvalds 			       srb->total_xfer_length);
24801da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
24811da177e4SLinus Torvalds 		if (io_dir & DMACMD_DIR) {	/* read */
24821da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
24831da177e4SLinus Torvalds 				      SCMD_FIFO_IN);
24841da177e4SLinus Torvalds 		} else {	/* write */
24851da177e4SLinus Torvalds 			int ln = srb->total_xfer_length;
2486cdb8c2a6SGuennadi Liakhovetski 			size_t left_io = srb->total_xfer_length;
2487cdb8c2a6SGuennadi Liakhovetski 
24881da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC)
24891da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
24901da177e4SLinus Torvalds 				     CFG2_WIDEFIFO);
24911da177e4SLinus Torvalds 
2492cdb8c2a6SGuennadi Liakhovetski 			while (left_io) {
2493cdb8c2a6SGuennadi Liakhovetski 				unsigned char *virt, *base = NULL;
2494cdb8c2a6SGuennadi Liakhovetski 				unsigned long flags = 0;
2495cdb8c2a6SGuennadi Liakhovetski 				size_t len = left_io;
2496cdb8c2a6SGuennadi Liakhovetski 				size_t offset = srb->request_length - left_io;
2497a862ea31SFUJITA Tomonori 
2498cdb8c2a6SGuennadi Liakhovetski 				local_irq_save(flags);
2499cdb8c2a6SGuennadi Liakhovetski 				/* Again, max 4 bytes */
2500a862ea31SFUJITA Tomonori 				base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
2501cdb8c2a6SGuennadi Liakhovetski 							   srb->sg_count, &offset, &len);
2502cdb8c2a6SGuennadi Liakhovetski 				virt = base + offset;
2503a862ea31SFUJITA Tomonori 
2504cdb8c2a6SGuennadi Liakhovetski 				left_io -= len;
2505cdb8c2a6SGuennadi Liakhovetski 
2506cdb8c2a6SGuennadi Liakhovetski 				while (len--) {
25071da177e4SLinus Torvalds 					if (debug_enabled(DBG_PIO))
2508cdb8c2a6SGuennadi Liakhovetski 						printk(" %02x", *virt);
25091da177e4SLinus Torvalds 
2510cdb8c2a6SGuennadi Liakhovetski 					DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *virt++);
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds 					sg_subtract_one(srb);
25131da177e4SLinus Torvalds 				}
2514cdb8c2a6SGuennadi Liakhovetski 
2515cdb8c2a6SGuennadi Liakhovetski 				scsi_kunmap_atomic_sg(base);
2516cdb8c2a6SGuennadi Liakhovetski 				local_irq_restore(flags);
2517cdb8c2a6SGuennadi Liakhovetski 			}
25181da177e4SLinus Torvalds 			if (srb->dcb->sync_period & WIDE_SYNC) {
25191da177e4SLinus Torvalds 				if (ln % 2) {
25201da177e4SLinus Torvalds 					DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
25211da177e4SLinus Torvalds 					if (debug_enabled(DBG_PIO))
25221da177e4SLinus Torvalds 						printk(" |00");
25231da177e4SLinus Torvalds 				}
25241da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
25251da177e4SLinus Torvalds 			}
25261da177e4SLinus Torvalds 			/*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
25271da177e4SLinus Torvalds 			if (debug_enabled(DBG_PIO))
25281da177e4SLinus Torvalds 				printk("\n");
25291da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
25301da177e4SLinus Torvalds 					  SCMD_FIFO_OUT);
25311da177e4SLinus Torvalds 		}
25321da177e4SLinus Torvalds 	}
25331da177e4SLinus Torvalds #endif				/* DC395x_LASTPIO */
25341da177e4SLinus Torvalds 	else {		/* xfer pad */
25351da177e4SLinus Torvalds 		u8 data = 0, data2 = 0;
25361da177e4SLinus Torvalds 		if (srb->sg_count) {
25371da177e4SLinus Torvalds 			srb->adapter_status = H_OVER_UNDER_RUN;
25381da177e4SLinus Torvalds 			srb->status |= OVER_RUN;
25391da177e4SLinus Torvalds 		}
25401da177e4SLinus Torvalds 		/*
25411da177e4SLinus Torvalds 		 * KG: despite the fact that we are using 16 bits I/O ops
25421da177e4SLinus Torvalds 		 * the SCSI FIFO is only 8 bits according to the docs
25431da177e4SLinus Torvalds 		 * (we can set bit 1 in 0x8f to serialize FIFO access ...)
25441da177e4SLinus Torvalds 		 */
25451da177e4SLinus Torvalds 		if (dcb->sync_period & WIDE_SYNC) {
25461da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
25471da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
25481da177e4SLinus Torvalds 				      CFG2_WIDEFIFO);
25491da177e4SLinus Torvalds 			if (io_dir & DMACMD_DIR) {
25501da177e4SLinus Torvalds 				data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
25511da177e4SLinus Torvalds 				data2 = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
25521da177e4SLinus Torvalds 			} else {
25531da177e4SLinus Torvalds 				/* Danger, Robinson: If you find KGs
25541da177e4SLinus Torvalds 				 * scattered over the wide disk, the driver
25551da177e4SLinus Torvalds 				 * or chip is to blame :-( */
25561da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
25571da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
25581da177e4SLinus Torvalds 			}
25591da177e4SLinus Torvalds 			DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
25601da177e4SLinus Torvalds 		} else {
25611da177e4SLinus Torvalds 			DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
25621da177e4SLinus Torvalds 			/* Danger, Robinson: If you find a collection of Ks on your disk
25631da177e4SLinus Torvalds 			 * something broke :-( */
25641da177e4SLinus Torvalds 			if (io_dir & DMACMD_DIR)
25651da177e4SLinus Torvalds 				data = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
25661da177e4SLinus Torvalds 			else
25671da177e4SLinus Torvalds 				DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
25681da177e4SLinus Torvalds 		}
25691da177e4SLinus Torvalds 		srb->state |= SRB_XFERPAD;
25701da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
25711da177e4SLinus Torvalds 		/* SCSI command */
25721da177e4SLinus Torvalds 		bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
25731da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
25741da177e4SLinus Torvalds 	}
25751da177e4SLinus Torvalds }
25761da177e4SLinus Torvalds 
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
25791da177e4SLinus Torvalds 		u16 *pscsi_status)
25801da177e4SLinus Torvalds {
25815cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "status_phase0: (0x%p) <%02i-%i>\n",
25825cd049a5SChristoph Hellwig 		srb->cmd, srb->cmd->device->id, srb->cmd->device->lun);
25831da177e4SLinus Torvalds 	srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
25841da177e4SLinus Torvalds 	srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);	/* get message */
25851da177e4SLinus Torvalds 	srb->state = SRB_COMPLETED;
25861da177e4SLinus Torvalds 	*pscsi_status = PH_BUS_FREE;	/*.. initial phase */
25871da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
25881da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
25891da177e4SLinus Torvalds }
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
25931da177e4SLinus Torvalds 		u16 *pscsi_status)
25941da177e4SLinus Torvalds {
25955cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "status_phase1: (0x%p) <%02i-%i>\n",
25965cd049a5SChristoph Hellwig 		srb->cmd, srb->cmd->device->id, srb->cmd->device->lun);
25971da177e4SLinus Torvalds 	srb->state = SRB_STATUS;
25981da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
25991da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
26001da177e4SLinus Torvalds }
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds /* Check if the message is complete */
26041da177e4SLinus Torvalds static inline u8 msgin_completed(u8 * msgbuf, u32 len)
26051da177e4SLinus Torvalds {
26061da177e4SLinus Torvalds 	if (*msgbuf == EXTENDED_MESSAGE) {
26071da177e4SLinus Torvalds 		if (len < 2)
26081da177e4SLinus Torvalds 			return 0;
26091da177e4SLinus Torvalds 		if (len < msgbuf[1] + 2)
26101da177e4SLinus Torvalds 			return 0;
26111da177e4SLinus Torvalds 	} else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f)	/* two byte messages */
26121da177e4SLinus Torvalds 		if (len < 2)
26131da177e4SLinus Torvalds 			return 0;
26141da177e4SLinus Torvalds 	return 1;
26151da177e4SLinus Torvalds }
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds /* reject_msg */
26181da177e4SLinus Torvalds static inline void msgin_reject(struct AdapterCtlBlk *acb,
26191da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
26201da177e4SLinus Torvalds {
26211da177e4SLinus Torvalds 	srb->msgout_buf[0] = MESSAGE_REJECT;
26221da177e4SLinus Torvalds 	srb->msg_count = 1;
26231da177e4SLinus Torvalds 	DC395x_ENABLE_MSGOUT;
26241da177e4SLinus Torvalds 	srb->state &= ~SRB_MSGIN;
26251da177e4SLinus Torvalds 	srb->state |= SRB_MSGOUT;
26261da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
26271da177e4SLinus Torvalds 		srb->msgin_buf[0],
26281da177e4SLinus Torvalds 		srb->dcb->target_id, srb->dcb->target_lun);
26291da177e4SLinus Torvalds }
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
26331da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb, u8 tag)
26341da177e4SLinus Torvalds {
26351da177e4SLinus Torvalds 	struct ScsiReqBlk *srb = NULL;
26361da177e4SLinus Torvalds 	struct ScsiReqBlk *i;
26375cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_qtag: (0x%p) tag=%i srb=%p\n",
26385cd049a5SChristoph Hellwig 		   srb->cmd, tag, srb);
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds 	if (!(dcb->tag_mask & (1 << tag)))
26411da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
26421da177e4SLinus Torvalds 			"msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
26431da177e4SLinus Torvalds 			dcb->tag_mask, tag);
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds 	if (list_empty(&dcb->srb_going_list))
26461da177e4SLinus Torvalds 		goto mingx0;
26471da177e4SLinus Torvalds 	list_for_each_entry(i, &dcb->srb_going_list, list) {
26481da177e4SLinus Torvalds 		if (i->tag_number == tag) {
26491da177e4SLinus Torvalds 			srb = i;
26501da177e4SLinus Torvalds 			break;
26511da177e4SLinus Torvalds 		}
26521da177e4SLinus Torvalds 	}
26531da177e4SLinus Torvalds 	if (!srb)
26541da177e4SLinus Torvalds 		goto mingx0;
26551da177e4SLinus Torvalds 
26565cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_qtag: (0x%p) <%02i-%i>\n",
26575cd049a5SChristoph Hellwig 		srb->cmd, srb->dcb->target_id, srb->dcb->target_lun);
26581da177e4SLinus Torvalds 	if (dcb->flag & ABORT_DEV_) {
26591da177e4SLinus Torvalds 		/*srb->state = SRB_ABORT_SENT; */
26601da177e4SLinus Torvalds 		enable_msgout_abort(acb, srb);
26611da177e4SLinus Torvalds 	}
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	if (!(srb->state & SRB_DISCONNECT))
26641da177e4SLinus Torvalds 		goto mingx0;
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds 	memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
26671da177e4SLinus Torvalds 	srb->state |= dcb->active_srb->state;
26681da177e4SLinus Torvalds 	srb->state |= SRB_DATA_XFER;
26691da177e4SLinus Torvalds 	dcb->active_srb = srb;
26701da177e4SLinus Torvalds 	/* How can we make the DORS happy? */
26711da177e4SLinus Torvalds 	return srb;
26721da177e4SLinus Torvalds 
26731da177e4SLinus Torvalds       mingx0:
26741da177e4SLinus Torvalds 	srb = acb->tmp_srb;
26751da177e4SLinus Torvalds 	srb->state = SRB_UNEXPECT_RESEL;
26761da177e4SLinus Torvalds 	dcb->active_srb = srb;
26771da177e4SLinus Torvalds 	srb->msgout_buf[0] = MSG_ABORT_TAG;
26781da177e4SLinus Torvalds 	srb->msg_count = 1;
26791da177e4SLinus Torvalds 	DC395x_ENABLE_MSGOUT;
26801da177e4SLinus Torvalds 	dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
26811da177e4SLinus Torvalds 	return srb;
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds static inline void reprogram_regs(struct AdapterCtlBlk *acb,
26861da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
26871da177e4SLinus Torvalds {
26881da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
26891da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
26901da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
26911da177e4SLinus Torvalds 	set_xfer_rate(acb, dcb);
26921da177e4SLinus Torvalds }
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds /* set async transfer mode */
26961da177e4SLinus Torvalds static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
26971da177e4SLinus Torvalds {
26981da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
26991da177e4SLinus Torvalds 	dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
27001da177e4SLinus Torvalds 		dcb->target_id, dcb->target_lun);
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
27031da177e4SLinus Torvalds 	dcb->sync_mode |= SYNC_NEGO_DONE;
27041da177e4SLinus Torvalds 	/*dcb->sync_period &= 0; */
27051da177e4SLinus Torvalds 	dcb->sync_offset = 0;
27061da177e4SLinus Torvalds 	dcb->min_nego_period = 200 >> 2;	/* 200ns <=> 5 MHz */
27071da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_SYNC_NEGO;
27081da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
27091da177e4SLinus Torvalds 	if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
27101da177e4SLinus Torvalds 	    && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
27111da177e4SLinus Torvalds 		build_wdtr(acb, dcb, srb);
27121da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
27131da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
27141da177e4SLinus Torvalds 	}
27151da177e4SLinus Torvalds }
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds /* set sync transfer mode */
27191da177e4SLinus Torvalds static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
27201da177e4SLinus Torvalds {
27211da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
27221da177e4SLinus Torvalds 	u8 bval;
27231da177e4SLinus Torvalds 	int fact;
27241da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
27251da177e4SLinus Torvalds 		"(%02i.%01i MHz) Offset %i\n",
27261da177e4SLinus Torvalds 		dcb->target_id, srb->msgin_buf[3] << 2,
27271da177e4SLinus Torvalds 		(250 / srb->msgin_buf[3]),
27281da177e4SLinus Torvalds 		((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
27291da177e4SLinus Torvalds 		srb->msgin_buf[4]);
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 	if (srb->msgin_buf[4] > 15)
27321da177e4SLinus Torvalds 		srb->msgin_buf[4] = 15;
27331da177e4SLinus Torvalds 	if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
27341da177e4SLinus Torvalds 		dcb->sync_offset = 0;
27351da177e4SLinus Torvalds 	else if (dcb->sync_offset == 0)
27361da177e4SLinus Torvalds 		dcb->sync_offset = srb->msgin_buf[4];
27371da177e4SLinus Torvalds 	if (srb->msgin_buf[4] > dcb->sync_offset)
27381da177e4SLinus Torvalds 		srb->msgin_buf[4] = dcb->sync_offset;
27391da177e4SLinus Torvalds 	else
27401da177e4SLinus Torvalds 		dcb->sync_offset = srb->msgin_buf[4];
27411da177e4SLinus Torvalds 	bval = 0;
27421da177e4SLinus Torvalds 	while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
27431da177e4SLinus Torvalds 			    || dcb->min_nego_period >
27441da177e4SLinus Torvalds 			    clock_period[bval]))
27451da177e4SLinus Torvalds 		bval++;
27461da177e4SLinus Torvalds 	if (srb->msgin_buf[3] < clock_period[bval])
27471da177e4SLinus Torvalds 		dprintkl(KERN_INFO,
27481da177e4SLinus Torvalds 			"msgin_set_sync: Increase sync nego period to %ins\n",
27491da177e4SLinus Torvalds 			clock_period[bval] << 2);
27501da177e4SLinus Torvalds 	srb->msgin_buf[3] = clock_period[bval];
27511da177e4SLinus Torvalds 	dcb->sync_period &= 0xf0;
27521da177e4SLinus Torvalds 	dcb->sync_period |= ALT_SYNC | bval;
27531da177e4SLinus Torvalds 	dcb->min_nego_period = srb->msgin_buf[3];
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds 	if (dcb->sync_period & WIDE_SYNC)
27561da177e4SLinus Torvalds 		fact = 500;
27571da177e4SLinus Torvalds 	else
27581da177e4SLinus Torvalds 		fact = 250;
27591da177e4SLinus Torvalds 
27601da177e4SLinus Torvalds 	dprintkl(KERN_INFO,
27611da177e4SLinus Torvalds 		"Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
27621da177e4SLinus Torvalds 		dcb->target_id, (fact == 500) ? "Wide16" : "",
27631da177e4SLinus Torvalds 		dcb->min_nego_period << 2, dcb->sync_offset,
27641da177e4SLinus Torvalds 		(fact / dcb->min_nego_period),
27651da177e4SLinus Torvalds 		((fact % dcb->min_nego_period) * 10 +
27661da177e4SLinus Torvalds 		dcb->min_nego_period / 2) / dcb->min_nego_period);
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds 	if (!(srb->state & SRB_DO_SYNC_NEGO)) {
27691da177e4SLinus Torvalds 		/* Reply with corrected SDTR Message */
27701da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
27711da177e4SLinus Torvalds 			srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds 		memcpy(srb->msgout_buf, srb->msgin_buf, 5);
27741da177e4SLinus Torvalds 		srb->msg_count = 5;
27751da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
27761da177e4SLinus Torvalds 		dcb->sync_mode |= SYNC_NEGO_DONE;
27771da177e4SLinus Torvalds 	} else {
27781da177e4SLinus Torvalds 		if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
27791da177e4SLinus Torvalds 		    && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
27801da177e4SLinus Torvalds 			build_wdtr(acb, dcb, srb);
27811da177e4SLinus Torvalds 			DC395x_ENABLE_MSGOUT;
27821da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
27831da177e4SLinus Torvalds 		}
27841da177e4SLinus Torvalds 	}
27851da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_SYNC_NEGO;
27861da177e4SLinus Torvalds 	dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
27871da177e4SLinus Torvalds 
27881da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
27891da177e4SLinus Torvalds }
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
27931da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
27941da177e4SLinus Torvalds {
27951da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
27961da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
27971da177e4SLinus Torvalds 
27981da177e4SLinus Torvalds 	dcb->sync_period &= ~WIDE_SYNC;
27991da177e4SLinus Torvalds 	dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
28001da177e4SLinus Torvalds 	dcb->sync_mode |= WIDE_NEGO_DONE;
28011da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_WIDE_NEGO;
28021da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
28031da177e4SLinus Torvalds 	if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
28041da177e4SLinus Torvalds 	    && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
28051da177e4SLinus Torvalds 		build_sdtr(acb, dcb, srb);
28061da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
28071da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
28081da177e4SLinus Torvalds 	}
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
28121da177e4SLinus Torvalds {
28131da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = srb->dcb;
28141da177e4SLinus Torvalds 	u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
28151da177e4SLinus Torvalds 		   && acb->config & HCC_WIDE_CARD) ? 1 : 0;
28161da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds 	if (srb->msgin_buf[3] > wide)
28191da177e4SLinus Torvalds 		srb->msgin_buf[3] = wide;
28201da177e4SLinus Torvalds 	/* Completed */
28211da177e4SLinus Torvalds 	if (!(srb->state & SRB_DO_WIDE_NEGO)) {
28221da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
28231da177e4SLinus Torvalds 			"msgin_set_wide: Wide nego initiated <%02i>\n",
28241da177e4SLinus Torvalds 			dcb->target_id);
28251da177e4SLinus Torvalds 		memcpy(srb->msgout_buf, srb->msgin_buf, 4);
28261da177e4SLinus Torvalds 		srb->msg_count = 4;
28271da177e4SLinus Torvalds 		srb->state |= SRB_DO_WIDE_NEGO;
28281da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
28291da177e4SLinus Torvalds 	}
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds 	dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
28321da177e4SLinus Torvalds 	if (srb->msgin_buf[3] > 0)
28331da177e4SLinus Torvalds 		dcb->sync_period |= WIDE_SYNC;
28341da177e4SLinus Torvalds 	else
28351da177e4SLinus Torvalds 		dcb->sync_period &= ~WIDE_SYNC;
28361da177e4SLinus Torvalds 	srb->state &= ~SRB_DO_WIDE_NEGO;
28371da177e4SLinus Torvalds 	/*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
28381da177e4SLinus Torvalds 	dprintkdbg(DBG_1,
28391da177e4SLinus Torvalds 		"msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
28401da177e4SLinus Torvalds 		(8 << srb->msgin_buf[3]), dcb->target_id);
28411da177e4SLinus Torvalds 	reprogram_regs(acb, dcb);
28421da177e4SLinus Torvalds 	if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
28431da177e4SLinus Torvalds 	    && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
28441da177e4SLinus Torvalds 		build_sdtr(acb, dcb, srb);
28451da177e4SLinus Torvalds 		DC395x_ENABLE_MSGOUT;
28461da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
28471da177e4SLinus Torvalds 	}
28481da177e4SLinus Torvalds }
28491da177e4SLinus Torvalds 
28501da177e4SLinus Torvalds 
28511da177e4SLinus Torvalds /*
28521da177e4SLinus Torvalds  * extended message codes:
28531da177e4SLinus Torvalds  *
28541da177e4SLinus Torvalds  *	code	description
28551da177e4SLinus Torvalds  *
28561da177e4SLinus Torvalds  *	02h	Reserved
28571da177e4SLinus Torvalds  *	00h	MODIFY DATA  POINTER
28581da177e4SLinus Torvalds  *	01h	SYNCHRONOUS DATA TRANSFER REQUEST
28591da177e4SLinus Torvalds  *	03h	WIDE DATA TRANSFER REQUEST
28601da177e4SLinus Torvalds  *   04h - 7Fh	Reserved
28611da177e4SLinus Torvalds  *   80h - FFh	Vendor specific
28621da177e4SLinus Torvalds  */
28631da177e4SLinus Torvalds static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
28641da177e4SLinus Torvalds 		u16 *pscsi_status)
28651da177e4SLinus Torvalds {
28661da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = acb->active_dcb;
28675cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_phase0: (0x%p)\n", srb->cmd);
28681da177e4SLinus Torvalds 
28691da177e4SLinus Torvalds 	srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
28701da177e4SLinus Torvalds 	if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
28711da177e4SLinus Torvalds 		/* Now eval the msg */
28721da177e4SLinus Torvalds 		switch (srb->msgin_buf[0]) {
28731da177e4SLinus Torvalds 		case DISCONNECT:
28741da177e4SLinus Torvalds 			srb->state = SRB_DISCONNECT;
28751da177e4SLinus Torvalds 			break;
28761da177e4SLinus Torvalds 
28771da177e4SLinus Torvalds 		case SIMPLE_QUEUE_TAG:
28781da177e4SLinus Torvalds 		case HEAD_OF_QUEUE_TAG:
28791da177e4SLinus Torvalds 		case ORDERED_QUEUE_TAG:
28801da177e4SLinus Torvalds 			srb =
28811da177e4SLinus Torvalds 			    msgin_qtag(acb, dcb,
28821da177e4SLinus Torvalds 					      srb->msgin_buf[1]);
28831da177e4SLinus Torvalds 			break;
28841da177e4SLinus Torvalds 
28851da177e4SLinus Torvalds 		case MESSAGE_REJECT:
28861da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
28871da177e4SLinus Torvalds 				       DO_CLRATN | DO_DATALATCH);
28881da177e4SLinus Torvalds 			/* A sync nego message was rejected ! */
28891da177e4SLinus Torvalds 			if (srb->state & SRB_DO_SYNC_NEGO) {
28901da177e4SLinus Torvalds 				msgin_set_async(acb, srb);
28911da177e4SLinus Torvalds 				break;
28921da177e4SLinus Torvalds 			}
28931da177e4SLinus Torvalds 			/* A wide nego message was rejected ! */
28941da177e4SLinus Torvalds 			if (srb->state & SRB_DO_WIDE_NEGO) {
28951da177e4SLinus Torvalds 				msgin_set_nowide(acb, srb);
28961da177e4SLinus Torvalds 				break;
28971da177e4SLinus Torvalds 			}
28981da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
28991da177e4SLinus Torvalds 			/*srb->state |= SRB_ABORT_SENT */
29001da177e4SLinus Torvalds 			break;
29011da177e4SLinus Torvalds 
29021da177e4SLinus Torvalds 		case EXTENDED_MESSAGE:
29031da177e4SLinus Torvalds 			/* SDTR */
29041da177e4SLinus Torvalds 			if (srb->msgin_buf[1] == 3
29051da177e4SLinus Torvalds 			    && srb->msgin_buf[2] == EXTENDED_SDTR) {
29061da177e4SLinus Torvalds 				msgin_set_sync(acb, srb);
29071da177e4SLinus Torvalds 				break;
29081da177e4SLinus Torvalds 			}
29091da177e4SLinus Torvalds 			/* WDTR */
29101da177e4SLinus Torvalds 			if (srb->msgin_buf[1] == 2
29111da177e4SLinus Torvalds 			    && srb->msgin_buf[2] == EXTENDED_WDTR
29121da177e4SLinus Torvalds 			    && srb->msgin_buf[3] <= 2) { /* sanity check ... */
29131da177e4SLinus Torvalds 				msgin_set_wide(acb, srb);
29141da177e4SLinus Torvalds 				break;
29151da177e4SLinus Torvalds 			}
29161da177e4SLinus Torvalds 			msgin_reject(acb, srb);
29171da177e4SLinus Torvalds 			break;
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 		case MSG_IGNOREWIDE:
29201da177e4SLinus Torvalds 			/* Discard  wide residual */
29211da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
29221da177e4SLinus Torvalds 			break;
29231da177e4SLinus Torvalds 
29241da177e4SLinus Torvalds 		case COMMAND_COMPLETE:
29251da177e4SLinus Torvalds 			/* nothing has to be done */
29261da177e4SLinus Torvalds 			break;
29271da177e4SLinus Torvalds 
29281da177e4SLinus Torvalds 		case SAVE_POINTERS:
29291da177e4SLinus Torvalds 			/*
29301da177e4SLinus Torvalds 			 * SAVE POINTER may be ignored as we have the struct
29311da177e4SLinus Torvalds 			 * ScsiReqBlk* associated with the scsi command.
29321da177e4SLinus Torvalds 			 */
29335cd049a5SChristoph Hellwig 			dprintkdbg(DBG_0, "msgin_phase0: (0x%p) "
29341da177e4SLinus Torvalds 				"SAVE POINTER rem=%i Ignore\n",
29355cd049a5SChristoph Hellwig 				srb->cmd, srb->total_xfer_length);
29361da177e4SLinus Torvalds 			break;
29371da177e4SLinus Torvalds 
29381da177e4SLinus Torvalds 		case RESTORE_POINTERS:
29391da177e4SLinus Torvalds 			dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
29401da177e4SLinus Torvalds 			break;
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds 		case ABORT:
29435cd049a5SChristoph Hellwig 			dprintkdbg(DBG_0, "msgin_phase0: (0x%p) "
29441da177e4SLinus Torvalds 				"<%02i-%i> ABORT msg\n",
29455cd049a5SChristoph Hellwig 				srb->cmd, dcb->target_id,
29461da177e4SLinus Torvalds 				dcb->target_lun);
29471da177e4SLinus Torvalds 			dcb->flag |= ABORT_DEV_;
29481da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
29491da177e4SLinus Torvalds 			break;
29501da177e4SLinus Torvalds 
29511da177e4SLinus Torvalds 		default:
29521da177e4SLinus Torvalds 			/* reject unknown messages */
29531da177e4SLinus Torvalds 			if (srb->msgin_buf[0] & IDENTIFY_BASE) {
29541da177e4SLinus Torvalds 				dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
29551da177e4SLinus Torvalds 				srb->msg_count = 1;
29561da177e4SLinus Torvalds 				srb->msgout_buf[0] = dcb->identify_msg;
29571da177e4SLinus Torvalds 				DC395x_ENABLE_MSGOUT;
29581da177e4SLinus Torvalds 				srb->state |= SRB_MSGOUT;
29591da177e4SLinus Torvalds 				/*break; */
29601da177e4SLinus Torvalds 			}
29611da177e4SLinus Torvalds 			msgin_reject(acb, srb);
29621da177e4SLinus Torvalds 		}
29631da177e4SLinus Torvalds 
29641da177e4SLinus Torvalds 		/* Clear counter and MsgIn state */
29651da177e4SLinus Torvalds 		srb->state &= ~SRB_MSGIN;
29661da177e4SLinus Torvalds 		acb->msg_len = 0;
29671da177e4SLinus Torvalds 	}
29681da177e4SLinus Torvalds 	*pscsi_status = PH_BUS_FREE;
29691da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important ... you know! */
29701da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
29711da177e4SLinus Torvalds }
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds 
29741da177e4SLinus Torvalds static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
29751da177e4SLinus Torvalds 		u16 *pscsi_status)
29761da177e4SLinus Torvalds {
29775cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "msgin_phase1: (0x%p)\n", srb->cmd);
29781da177e4SLinus Torvalds 	clear_fifo(acb, "msgin_phase1");
29791da177e4SLinus Torvalds 	DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
29801da177e4SLinus Torvalds 	if (!(srb->state & SRB_MSGIN)) {
29811da177e4SLinus Torvalds 		srb->state &= ~SRB_DISCONNECT;
29821da177e4SLinus Torvalds 		srb->state |= SRB_MSGIN;
29831da177e4SLinus Torvalds 	}
29841da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
29851da177e4SLinus Torvalds 	/* SCSI command */
29861da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
29871da177e4SLinus Torvalds }
29881da177e4SLinus Torvalds 
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
29911da177e4SLinus Torvalds 		u16 *pscsi_status)
29921da177e4SLinus Torvalds {
29931da177e4SLinus Torvalds }
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
29971da177e4SLinus Torvalds 		u16 *pscsi_status)
29981da177e4SLinus Torvalds {
29991da177e4SLinus Torvalds }
30001da177e4SLinus Torvalds 
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
30031da177e4SLinus Torvalds {
30041da177e4SLinus Torvalds 	struct DeviceCtlBlk *i;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	/* set all lun device's  period, offset */
30071da177e4SLinus Torvalds 	if (dcb->identify_msg & 0x07)
30081da177e4SLinus Torvalds 		return;
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 	if (acb->scan_devices) {
30111da177e4SLinus Torvalds 		current_sync_offset = dcb->sync_offset;
30121da177e4SLinus Torvalds 		return;
30131da177e4SLinus Torvalds 	}
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds 	list_for_each_entry(i, &acb->dcb_list, list)
30161da177e4SLinus Torvalds 		if (i->target_id == dcb->target_id) {
30171da177e4SLinus Torvalds 			i->sync_period = dcb->sync_period;
30181da177e4SLinus Torvalds 			i->sync_offset = dcb->sync_offset;
30191da177e4SLinus Torvalds 			i->sync_mode = dcb->sync_mode;
30201da177e4SLinus Torvalds 			i->min_nego_period = dcb->min_nego_period;
30211da177e4SLinus Torvalds 		}
30221da177e4SLinus Torvalds }
30231da177e4SLinus Torvalds 
30241da177e4SLinus Torvalds 
30251da177e4SLinus Torvalds static void disconnect(struct AdapterCtlBlk *acb)
30261da177e4SLinus Torvalds {
30271da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = acb->active_dcb;
30281da177e4SLinus Torvalds 	struct ScsiReqBlk *srb;
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 	if (!dcb) {
30311da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "disconnect: No such device\n");
30321da177e4SLinus Torvalds 		udelay(500);
30331da177e4SLinus Torvalds 		/* Suspend queue for a while */
30341da177e4SLinus Torvalds 		acb->scsi_host->last_reset =
30351da177e4SLinus Torvalds 		    jiffies + HZ / 2 +
30361da177e4SLinus Torvalds 		    HZ * acb->eeprom.delay_time;
30371da177e4SLinus Torvalds 		clear_fifo(acb, "disconnectEx");
30381da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
30391da177e4SLinus Torvalds 		return;
30401da177e4SLinus Torvalds 	}
30411da177e4SLinus Torvalds 	srb = dcb->active_srb;
30421da177e4SLinus Torvalds 	acb->active_dcb = NULL;
30435cd049a5SChristoph Hellwig 	dprintkdbg(DBG_0, "disconnect: (0x%p)\n", srb->cmd);
30441da177e4SLinus Torvalds 
30451da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
30461da177e4SLinus Torvalds 	clear_fifo(acb, "disconnect");
30471da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
30481da177e4SLinus Torvalds 	if (srb->state & SRB_UNEXPECT_RESEL) {
30491da177e4SLinus Torvalds 		dprintkl(KERN_ERR,
30501da177e4SLinus Torvalds 			"disconnect: Unexpected reselection <%02i-%i>\n",
30511da177e4SLinus Torvalds 			dcb->target_id, dcb->target_lun);
30521da177e4SLinus Torvalds 		srb->state = 0;
30531da177e4SLinus Torvalds 		waiting_process_next(acb);
30541da177e4SLinus Torvalds 	} else if (srb->state & SRB_ABORT_SENT) {
30551da177e4SLinus Torvalds 		dcb->flag &= ~ABORT_DEV_;
30561da177e4SLinus Torvalds 		acb->scsi_host->last_reset = jiffies + HZ / 2 + 1;
30571da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
30581da177e4SLinus Torvalds 		doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
30591da177e4SLinus Torvalds 		waiting_process_next(acb);
30601da177e4SLinus Torvalds 	} else {
30611da177e4SLinus Torvalds 		if ((srb->state & (SRB_START_ + SRB_MSGOUT))
30621da177e4SLinus Torvalds 		    || !(srb->
30631da177e4SLinus Torvalds 			 state & (SRB_DISCONNECT + SRB_COMPLETED))) {
30641da177e4SLinus Torvalds 			/*
30651da177e4SLinus Torvalds 			 * Selection time out
30661da177e4SLinus Torvalds 			 * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
30671da177e4SLinus Torvalds 			 */
30681da177e4SLinus Torvalds 			/* Unexp. Disc / Sel Timeout */
30691da177e4SLinus Torvalds 			if (srb->state != SRB_START_
30701da177e4SLinus Torvalds 			    && srb->state != SRB_MSGOUT) {
30711da177e4SLinus Torvalds 				srb->state = SRB_READY;
30721da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
30735cd049a5SChristoph Hellwig 					"disconnect: (0x%p) Unexpected\n",
30745cd049a5SChristoph Hellwig 					srb->cmd);
30751da177e4SLinus Torvalds 				srb->target_status = SCSI_STAT_SEL_TIMEOUT;
30761da177e4SLinus Torvalds 				goto disc1;
30771da177e4SLinus Torvalds 			} else {
30781da177e4SLinus Torvalds 				/* Normal selection timeout */
30795cd049a5SChristoph Hellwig 				dprintkdbg(DBG_KG, "disconnect: (0x%p) "
30805cd049a5SChristoph Hellwig 					"<%02i-%i> SelTO\n", srb->cmd,
30811da177e4SLinus Torvalds 					dcb->target_id, dcb->target_lun);
30821da177e4SLinus Torvalds 				if (srb->retry_count++ > DC395x_MAX_RETRIES
30831da177e4SLinus Torvalds 				    || acb->scan_devices) {
30841da177e4SLinus Torvalds 					srb->target_status =
30851da177e4SLinus Torvalds 					    SCSI_STAT_SEL_TIMEOUT;
30861da177e4SLinus Torvalds 					goto disc1;
30871da177e4SLinus Torvalds 				}
30881da177e4SLinus Torvalds 				free_tag(dcb, srb);
30891da177e4SLinus Torvalds 				srb_going_to_waiting_move(dcb, srb);
30901da177e4SLinus Torvalds 				dprintkdbg(DBG_KG,
30915cd049a5SChristoph Hellwig 					"disconnect: (0x%p) Retry\n",
30925cd049a5SChristoph Hellwig 					srb->cmd);
30931da177e4SLinus Torvalds 				waiting_set_timer(acb, HZ / 20);
30941da177e4SLinus Torvalds 			}
30951da177e4SLinus Torvalds 		} else if (srb->state & SRB_DISCONNECT) {
30961da177e4SLinus Torvalds 			u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
30971da177e4SLinus Torvalds 			/*
30981da177e4SLinus Torvalds 			 * SRB_DISCONNECT (This is what we expect!)
30991da177e4SLinus Torvalds 			 */
31001da177e4SLinus Torvalds 			if (bval & 0x40) {
31011da177e4SLinus Torvalds 				dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
31021da177e4SLinus Torvalds 					" 0x%02x: ACK set! Other controllers?\n",
31031da177e4SLinus Torvalds 					bval);
31041da177e4SLinus Torvalds 				/* It could come from another initiator, therefore don't do much ! */
31051da177e4SLinus Torvalds 			} else
31061da177e4SLinus Torvalds 				waiting_process_next(acb);
31071da177e4SLinus Torvalds 		} else if (srb->state & SRB_COMPLETED) {
31081da177e4SLinus Torvalds 		      disc1:
31091da177e4SLinus Torvalds 			/*
31101da177e4SLinus Torvalds 			 ** SRB_COMPLETED
31111da177e4SLinus Torvalds 			 */
31121da177e4SLinus Torvalds 			free_tag(dcb, srb);
31131da177e4SLinus Torvalds 			dcb->active_srb = NULL;
31141da177e4SLinus Torvalds 			srb->state = SRB_FREE;
31151da177e4SLinus Torvalds 			srb_done(acb, dcb, srb);
31161da177e4SLinus Torvalds 		}
31171da177e4SLinus Torvalds 	}
31181da177e4SLinus Torvalds }
31191da177e4SLinus Torvalds 
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds static void reselect(struct AdapterCtlBlk *acb)
31221da177e4SLinus Torvalds {
31231da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = acb->active_dcb;
31241da177e4SLinus Torvalds 	struct ScsiReqBlk *srb = NULL;
31251da177e4SLinus Torvalds 	u16 rsel_tar_lun_id;
31261da177e4SLinus Torvalds 	u8 id, lun;
31271da177e4SLinus Torvalds 	u8 arblostflag = 0;
31281da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds 	clear_fifo(acb, "reselect");
31311da177e4SLinus Torvalds 	/*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
31321da177e4SLinus Torvalds 	/* Read Reselected Target ID and LUN */
31331da177e4SLinus Torvalds 	rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
31341da177e4SLinus Torvalds 	if (dcb) {		/* Arbitration lost but Reselection win */
31351da177e4SLinus Torvalds 		srb = dcb->active_srb;
31361da177e4SLinus Torvalds 		if (!srb) {
31371da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
31381da177e4SLinus Torvalds 				"but active_srb == NULL\n");
31391da177e4SLinus Torvalds 			DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
31401da177e4SLinus Torvalds 			return;
31411da177e4SLinus Torvalds 		}
31421da177e4SLinus Torvalds 		/* Why the if ? */
31431da177e4SLinus Torvalds 		if (!acb->scan_devices) {
31445cd049a5SChristoph Hellwig 			dprintkdbg(DBG_KG, "reselect: (0x%p) <%02i-%i> "
31451da177e4SLinus Torvalds 				"Arb lost but Resel win rsel=%i stat=0x%04x\n",
31465cd049a5SChristoph Hellwig 				srb->cmd, dcb->target_id,
31471da177e4SLinus Torvalds 				dcb->target_lun, rsel_tar_lun_id,
31481da177e4SLinus Torvalds 				DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
31491da177e4SLinus Torvalds 			arblostflag = 1;
31501da177e4SLinus Torvalds 			/*srb->state |= SRB_DISCONNECT; */
31511da177e4SLinus Torvalds 
31521da177e4SLinus Torvalds 			srb->state = SRB_READY;
31531da177e4SLinus Torvalds 			free_tag(dcb, srb);
31541da177e4SLinus Torvalds 			srb_going_to_waiting_move(dcb, srb);
31551da177e4SLinus Torvalds 			waiting_set_timer(acb, HZ / 20);
31561da177e4SLinus Torvalds 
31571da177e4SLinus Torvalds 			/* return; */
31581da177e4SLinus Torvalds 		}
31591da177e4SLinus Torvalds 	}
31601da177e4SLinus Torvalds 	/* Read Reselected Target Id and LUN */
31611da177e4SLinus Torvalds 	if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
31621da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
31631da177e4SLinus Torvalds 			"Got %i!\n", rsel_tar_lun_id);
31641da177e4SLinus Torvalds 	id = rsel_tar_lun_id & 0xff;
31651da177e4SLinus Torvalds 	lun = (rsel_tar_lun_id >> 8) & 7;
31661da177e4SLinus Torvalds 	dcb = find_dcb(acb, id, lun);
31671da177e4SLinus Torvalds 	if (!dcb) {
31681da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "reselect: From non existent device "
31691da177e4SLinus Torvalds 			"<%02i-%i>\n", id, lun);
31701da177e4SLinus Torvalds 		DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);	/* it's important for atn stop */
31711da177e4SLinus Torvalds 		return;
31721da177e4SLinus Torvalds 	}
31731da177e4SLinus Torvalds 	acb->active_dcb = dcb;
31741da177e4SLinus Torvalds 
31751da177e4SLinus Torvalds 	if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
31761da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
31771da177e4SLinus Torvalds 			"disconnection? <%02i-%i>\n",
31781da177e4SLinus Torvalds 			dcb->target_id, dcb->target_lun);
31791da177e4SLinus Torvalds 
31801da177e4SLinus Torvalds 	if (dcb->sync_mode & EN_TAG_QUEUEING /*&& !arblostflag */) {
31811da177e4SLinus Torvalds 		srb = acb->tmp_srb;
31821da177e4SLinus Torvalds 		dcb->active_srb = srb;
31831da177e4SLinus Torvalds 	} else {
31841da177e4SLinus Torvalds 		/* There can be only one! */
31851da177e4SLinus Torvalds 		srb = dcb->active_srb;
31861da177e4SLinus Torvalds 		if (!srb || !(srb->state & SRB_DISCONNECT)) {
31871da177e4SLinus Torvalds 			/*
31881da177e4SLinus Torvalds 			 * abort command
31891da177e4SLinus Torvalds 			 */
31901da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
31911da177e4SLinus Torvalds 				"reselect: w/o disconnected cmds <%02i-%i>\n",
31921da177e4SLinus Torvalds 				dcb->target_id, dcb->target_lun);
31931da177e4SLinus Torvalds 			srb = acb->tmp_srb;
31941da177e4SLinus Torvalds 			srb->state = SRB_UNEXPECT_RESEL;
31951da177e4SLinus Torvalds 			dcb->active_srb = srb;
31961da177e4SLinus Torvalds 			enable_msgout_abort(acb, srb);
31971da177e4SLinus Torvalds 		} else {
31981da177e4SLinus Torvalds 			if (dcb->flag & ABORT_DEV_) {
31991da177e4SLinus Torvalds 				/*srb->state = SRB_ABORT_SENT; */
32001da177e4SLinus Torvalds 				enable_msgout_abort(acb, srb);
32011da177e4SLinus Torvalds 			} else
32021da177e4SLinus Torvalds 				srb->state = SRB_DATA_XFER;
32031da177e4SLinus Torvalds 
32041da177e4SLinus Torvalds 		}
32051da177e4SLinus Torvalds 	}
32061da177e4SLinus Torvalds 	srb->scsi_phase = PH_BUS_FREE;	/* initial phase */
32071da177e4SLinus Torvalds 
32081da177e4SLinus Torvalds 	/* Program HA ID, target ID, period and offset */
32091da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
32101da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);	/* host   ID */
32111da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);		/* target ID */
32121da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);		/* offset    */
32131da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);		/* sync period, wide */
32141da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);		/* it's important for atn stop */
32151da177e4SLinus Torvalds 	/* SCSI command */
32161da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
32171da177e4SLinus Torvalds }
32181da177e4SLinus Torvalds 
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds static inline u8 tagq_blacklist(char *name)
32211da177e4SLinus Torvalds {
32221da177e4SLinus Torvalds #ifndef DC395x_NO_TAGQ
32231da177e4SLinus Torvalds #if 0
32241da177e4SLinus Torvalds 	u8 i;
32251da177e4SLinus Torvalds 	for (i = 0; i < BADDEVCNT; i++)
32261da177e4SLinus Torvalds 		if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
32271da177e4SLinus Torvalds 			return 1;
32281da177e4SLinus Torvalds #endif
32291da177e4SLinus Torvalds 	return 0;
32301da177e4SLinus Torvalds #else
32311da177e4SLinus Torvalds 	return 1;
32321da177e4SLinus Torvalds #endif
32331da177e4SLinus Torvalds }
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
32371da177e4SLinus Torvalds {
32381da177e4SLinus Torvalds 	/* Check for SCSI format (ANSI and Response data format) */
32391da177e4SLinus Torvalds 	if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
32401da177e4SLinus Torvalds 		if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
32411da177e4SLinus Torvalds 		    && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
32421da177e4SLinus Torvalds 		    /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
32431da177e4SLinus Torvalds 		    /* ((dcb->dev_type == TYPE_DISK)
32441da177e4SLinus Torvalds 		       || (dcb->dev_type == TYPE_MOD)) && */
32451da177e4SLinus Torvalds 		    !tagq_blacklist(((char *)ptr) + 8)) {
32461da177e4SLinus Torvalds 			if (dcb->max_command == 1)
32471da177e4SLinus Torvalds 				dcb->max_command =
32481da177e4SLinus Torvalds 				    dcb->acb->tag_max_num;
32491da177e4SLinus Torvalds 			dcb->sync_mode |= EN_TAG_QUEUEING;
32501da177e4SLinus Torvalds 			/*dcb->tag_mask = 0; */
32511da177e4SLinus Torvalds 		} else
32521da177e4SLinus Torvalds 			dcb->max_command = 1;
32531da177e4SLinus Torvalds 	}
32541da177e4SLinus Torvalds }
32551da177e4SLinus Torvalds 
32561da177e4SLinus Torvalds 
32571da177e4SLinus Torvalds static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
32581da177e4SLinus Torvalds 		struct ScsiInqData *ptr)
32591da177e4SLinus Torvalds {
32601da177e4SLinus Torvalds 	u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
32611da177e4SLinus Torvalds 	dcb->dev_type = bval1;
32621da177e4SLinus Torvalds 	/* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
32631da177e4SLinus Torvalds 	disc_tagq_set(dcb, ptr);
32641da177e4SLinus Torvalds }
32651da177e4SLinus Torvalds 
32661da177e4SLinus Torvalds 
32671da177e4SLinus Torvalds /* unmap mapped pci regions from SRB */
32681da177e4SLinus Torvalds static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
32691da177e4SLinus Torvalds {
32701da177e4SLinus Torvalds 	struct scsi_cmnd *cmd = srb->cmd;
32711da177e4SLinus Torvalds 	enum dma_data_direction dir = cmd->sc_data_direction;
3272a862ea31SFUJITA Tomonori 
3273a862ea31SFUJITA Tomonori 	if (scsi_sg_count(cmd) && dir != PCI_DMA_NONE) {
32741da177e4SLinus Torvalds 		/* unmap DC395x SG list */
32751da177e4SLinus Torvalds 		dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
32761da177e4SLinus Torvalds 			srb->sg_bus_addr, SEGMENTX_LEN);
32771da177e4SLinus Torvalds 		pci_unmap_single(acb->dev, srb->sg_bus_addr,
32781da177e4SLinus Torvalds 				 SEGMENTX_LEN,
32791da177e4SLinus Torvalds 				 PCI_DMA_TODEVICE);
32801da177e4SLinus Torvalds 		dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
3281a862ea31SFUJITA Tomonori 			   scsi_sg_count(cmd), scsi_bufflen(cmd));
32821da177e4SLinus Torvalds 		/* unmap the sg segments */
3283a862ea31SFUJITA Tomonori 		scsi_dma_unmap(cmd);
32841da177e4SLinus Torvalds 	}
32851da177e4SLinus Torvalds }
32861da177e4SLinus Torvalds 
32871da177e4SLinus Torvalds 
32881da177e4SLinus Torvalds /* unmap mapped pci sense buffer from SRB */
32891da177e4SLinus Torvalds static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
32901da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
32911da177e4SLinus Torvalds {
32921da177e4SLinus Torvalds 	if (!(srb->flag & AUTO_REQSENSE))
32931da177e4SLinus Torvalds 		return;
32941da177e4SLinus Torvalds 	/* Unmap sense buffer */
32951da177e4SLinus Torvalds 	dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
32961da177e4SLinus Torvalds 	       srb->segment_x[0].address);
32971da177e4SLinus Torvalds 	pci_unmap_single(acb->dev, srb->segment_x[0].address,
32981da177e4SLinus Torvalds 			 srb->segment_x[0].length, PCI_DMA_FROMDEVICE);
32991da177e4SLinus Torvalds 	/* Restore SG stuff */
33001da177e4SLinus Torvalds 	srb->total_xfer_length = srb->xferred;
33011da177e4SLinus Torvalds 	srb->segment_x[0].address =
33021da177e4SLinus Torvalds 	    srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
33031da177e4SLinus Torvalds 	srb->segment_x[0].length =
33041da177e4SLinus Torvalds 	    srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
33051da177e4SLinus Torvalds }
33061da177e4SLinus Torvalds 
33071da177e4SLinus Torvalds 
33081da177e4SLinus Torvalds /*
33091da177e4SLinus Torvalds  * Complete execution of a SCSI command
33101da177e4SLinus Torvalds  * Signal completion to the generic SCSI driver
33111da177e4SLinus Torvalds  */
33121da177e4SLinus Torvalds static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
33131da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
33141da177e4SLinus Torvalds {
33151da177e4SLinus Torvalds 	u8 tempcnt, status;
33161da177e4SLinus Torvalds 	struct scsi_cmnd *cmd = srb->cmd;
33171da177e4SLinus Torvalds 	enum dma_data_direction dir = cmd->sc_data_direction;
33189dc399deSGuennadi Liakhovetski 	int ckc_only = 1;
33191da177e4SLinus Torvalds 
33205cd049a5SChristoph Hellwig 	dprintkdbg(DBG_1, "srb_done: (0x%p) <%02i-%i>\n", srb->cmd,
33211da177e4SLinus Torvalds 		srb->cmd->device->id, srb->cmd->device->lun);
33229dc399deSGuennadi Liakhovetski 	dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n",
3323a862ea31SFUJITA Tomonori 		   srb, scsi_sg_count(cmd), srb->sg_index, srb->sg_count,
3324a862ea31SFUJITA Tomonori 		   scsi_sgtalbe(cmd));
33251da177e4SLinus Torvalds 	status = srb->target_status;
33261da177e4SLinus Torvalds 	if (srb->flag & AUTO_REQSENSE) {
33271da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
33281da177e4SLinus Torvalds 		pci_unmap_srb_sense(acb, srb);
33291da177e4SLinus Torvalds 		/*
33301da177e4SLinus Torvalds 		 ** target status..........................
33311da177e4SLinus Torvalds 		 */
33321da177e4SLinus Torvalds 		srb->flag &= ~AUTO_REQSENSE;
33331da177e4SLinus Torvalds 		srb->adapter_status = 0;
33341da177e4SLinus Torvalds 		srb->target_status = CHECK_CONDITION << 1;
33351da177e4SLinus Torvalds 		if (debug_enabled(DBG_1)) {
33361da177e4SLinus Torvalds 			switch (cmd->sense_buffer[2] & 0x0f) {
33371da177e4SLinus Torvalds 			case NOT_READY:
33381da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
33391da177e4SLinus Torvalds 				     "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
33401da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
33411da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
33421da177e4SLinus Torvalds 				break;
33431da177e4SLinus Torvalds 			case UNIT_ATTENTION:
33441da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
33451da177e4SLinus Torvalds 				     "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
33461da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
33471da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
33481da177e4SLinus Torvalds 				break;
33491da177e4SLinus Torvalds 			case ILLEGAL_REQUEST:
33501da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
33511da177e4SLinus Torvalds 				     "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
33521da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
33531da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
33541da177e4SLinus Torvalds 				break;
33551da177e4SLinus Torvalds 			case MEDIUM_ERROR:
33561da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
33571da177e4SLinus Torvalds 				     "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
33581da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
33591da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
33601da177e4SLinus Torvalds 				break;
33611da177e4SLinus Torvalds 			case HARDWARE_ERROR:
33621da177e4SLinus Torvalds 				dprintkl(KERN_DEBUG,
33631da177e4SLinus Torvalds 				     "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
33641da177e4SLinus Torvalds 				     cmd->cmnd[0], dcb->target_id,
33651da177e4SLinus Torvalds 				     dcb->target_lun, status, acb->scan_devices);
33661da177e4SLinus Torvalds 				break;
33671da177e4SLinus Torvalds 			}
33681da177e4SLinus Torvalds 			if (cmd->sense_buffer[7] >= 6)
33691da177e4SLinus Torvalds 				printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
33701da177e4SLinus Torvalds 					"(0x%08x 0x%08x)\n",
33711da177e4SLinus Torvalds 					cmd->sense_buffer[2], cmd->sense_buffer[12],
33721da177e4SLinus Torvalds 					cmd->sense_buffer[13],
33731da177e4SLinus Torvalds 					*((unsigned int *)(cmd->sense_buffer + 3)),
33741da177e4SLinus Torvalds 					*((unsigned int *)(cmd->sense_buffer + 8)));
33751da177e4SLinus Torvalds 			else
33761da177e4SLinus Torvalds 				printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
33771da177e4SLinus Torvalds 					cmd->sense_buffer[2],
33781da177e4SLinus Torvalds 					*((unsigned int *)(cmd->sense_buffer + 3)));
33791da177e4SLinus Torvalds 		}
33801da177e4SLinus Torvalds 
33811da177e4SLinus Torvalds 		if (status == (CHECK_CONDITION << 1)) {
33821da177e4SLinus Torvalds 			cmd->result = DID_BAD_TARGET << 16;
33831da177e4SLinus Torvalds 			goto ckc_e;
33841da177e4SLinus Torvalds 		}
33851da177e4SLinus Torvalds 		dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds 		if (srb->total_xfer_length
33881da177e4SLinus Torvalds 		    && srb->total_xfer_length >= cmd->underflow)
33891da177e4SLinus Torvalds 			cmd->result =
33901da177e4SLinus Torvalds 			    MK_RES_LNX(DRIVER_SENSE, DID_OK,
33911da177e4SLinus Torvalds 				       srb->end_message, CHECK_CONDITION);
33921da177e4SLinus Torvalds 		/*SET_RES_DID(cmd->result,DID_OK) */
33931da177e4SLinus Torvalds 		else
33941da177e4SLinus Torvalds 			cmd->result =
33951da177e4SLinus Torvalds 			    MK_RES_LNX(DRIVER_SENSE, DID_OK,
33961da177e4SLinus Torvalds 				       srb->end_message, CHECK_CONDITION);
33971da177e4SLinus Torvalds 
33981da177e4SLinus Torvalds 		goto ckc_e;
33991da177e4SLinus Torvalds 	}
34001da177e4SLinus Torvalds 
34011da177e4SLinus Torvalds /*************************************************************/
34021da177e4SLinus Torvalds 	if (status) {
34031da177e4SLinus Torvalds 		/*
34041da177e4SLinus Torvalds 		 * target status..........................
34051da177e4SLinus Torvalds 		 */
34061da177e4SLinus Torvalds 		if (status_byte(status) == CHECK_CONDITION) {
34071da177e4SLinus Torvalds 			request_sense(acb, dcb, srb);
34081da177e4SLinus Torvalds 			return;
34091da177e4SLinus Torvalds 		} else if (status_byte(status) == QUEUE_FULL) {
34101da177e4SLinus Torvalds 			tempcnt = (u8)list_size(&dcb->srb_going_list);
34111da177e4SLinus Torvalds 			dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
34121da177e4SLinus Torvalds 			     dcb->target_id, dcb->target_lun, tempcnt);
34131da177e4SLinus Torvalds 			if (tempcnt > 1)
34141da177e4SLinus Torvalds 				tempcnt--;
34151da177e4SLinus Torvalds 			dcb->max_command = tempcnt;
34161da177e4SLinus Torvalds 			free_tag(dcb, srb);
34171da177e4SLinus Torvalds 			srb_going_to_waiting_move(dcb, srb);
34181da177e4SLinus Torvalds 			waiting_set_timer(acb, HZ / 20);
34191da177e4SLinus Torvalds 			srb->adapter_status = 0;
34201da177e4SLinus Torvalds 			srb->target_status = 0;
34211da177e4SLinus Torvalds 			return;
34221da177e4SLinus Torvalds 		} else if (status == SCSI_STAT_SEL_TIMEOUT) {
34231da177e4SLinus Torvalds 			srb->adapter_status = H_SEL_TIMEOUT;
34241da177e4SLinus Torvalds 			srb->target_status = 0;
34251da177e4SLinus Torvalds 			cmd->result = DID_NO_CONNECT << 16;
34261da177e4SLinus Torvalds 		} else {
34271da177e4SLinus Torvalds 			srb->adapter_status = 0;
34281da177e4SLinus Torvalds 			SET_RES_DID(cmd->result, DID_ERROR);
34291da177e4SLinus Torvalds 			SET_RES_MSG(cmd->result, srb->end_message);
34301da177e4SLinus Torvalds 			SET_RES_TARGET(cmd->result, status);
34311da177e4SLinus Torvalds 
34321da177e4SLinus Torvalds 		}
34331da177e4SLinus Torvalds 	} else {
34341da177e4SLinus Torvalds 		/*
34351da177e4SLinus Torvalds 		 ** process initiator status..........................
34361da177e4SLinus Torvalds 		 */
34371da177e4SLinus Torvalds 		status = srb->adapter_status;
34381da177e4SLinus Torvalds 		if (status & H_OVER_UNDER_RUN) {
34391da177e4SLinus Torvalds 			srb->target_status = 0;
34401da177e4SLinus Torvalds 			SET_RES_DID(cmd->result, DID_OK);
34411da177e4SLinus Torvalds 			SET_RES_MSG(cmd->result, srb->end_message);
34421da177e4SLinus Torvalds 		} else if (srb->status & PARITY_ERROR) {
34431da177e4SLinus Torvalds 			SET_RES_DID(cmd->result, DID_PARITY);
34441da177e4SLinus Torvalds 			SET_RES_MSG(cmd->result, srb->end_message);
34451da177e4SLinus Torvalds 		} else {	/* No error */
34461da177e4SLinus Torvalds 
34471da177e4SLinus Torvalds 			srb->adapter_status = 0;
34481da177e4SLinus Torvalds 			srb->target_status = 0;
34491da177e4SLinus Torvalds 			SET_RES_DID(cmd->result, DID_OK);
34501da177e4SLinus Torvalds 		}
34511da177e4SLinus Torvalds 	}
34521da177e4SLinus Torvalds 
3453a862ea31SFUJITA Tomonori 	if (dir != PCI_DMA_NONE && scsi_sg_count(cmd))
3454a862ea31SFUJITA Tomonori 		pci_dma_sync_sg_for_cpu(acb->dev, scsi_sglist(cmd),
3455a862ea31SFUJITA Tomonori 					scsi_sg_count(cmd), dir);
3456a862ea31SFUJITA Tomonori 
34579dc399deSGuennadi Liakhovetski 	ckc_only = 0;
34581da177e4SLinus Torvalds /* Check Error Conditions */
34591da177e4SLinus Torvalds       ckc_e:
34601da177e4SLinus Torvalds 
34619dc399deSGuennadi Liakhovetski 	if (cmd->cmnd[0] == INQUIRY) {
34629dc399deSGuennadi Liakhovetski 		unsigned char *base = NULL;
34639dc399deSGuennadi Liakhovetski 		struct ScsiInqData *ptr;
34649dc399deSGuennadi Liakhovetski 		unsigned long flags = 0;
3465a862ea31SFUJITA Tomonori 		struct scatterlist* sg = scsi_sglist(cmd);
34669dc399deSGuennadi Liakhovetski 		size_t offset = 0, len = sizeof(struct ScsiInqData);
34679dc399deSGuennadi Liakhovetski 
34689dc399deSGuennadi Liakhovetski 		local_irq_save(flags);
3469a862ea31SFUJITA Tomonori 		base = scsi_kmap_atomic_sg(sg, scsi_sg_count(cmd), &offset, &len);
34709dc399deSGuennadi Liakhovetski 		ptr = (struct ScsiInqData *)(base + offset);
34719dc399deSGuennadi Liakhovetski 
34729dc399deSGuennadi Liakhovetski 		if (!ckc_only && (cmd->result & RES_DID) == 0
3473a862ea31SFUJITA Tomonori 		    && cmd->cmnd[2] == 0 && scsi_bufflen(cmd) >= 8
34749dc399deSGuennadi Liakhovetski 		    && dir != PCI_DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
34759dc399deSGuennadi Liakhovetski 			dcb->inquiry7 = ptr->Flags;
34769dc399deSGuennadi Liakhovetski 
34771da177e4SLinus Torvalds 	/*if( srb->cmd->cmnd[0] == INQUIRY && */
34781da177e4SLinus Torvalds 	/*  (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
34799dc399deSGuennadi Liakhovetski 		if ((cmd->result == (DID_OK << 16)
34809dc399deSGuennadi Liakhovetski 		     || status_byte(cmd->result) &
34811da177e4SLinus Torvalds 		     CHECK_CONDITION)) {
34821da177e4SLinus Torvalds 			if (!dcb->init_tcq_flag) {
34831da177e4SLinus Torvalds 				add_dev(acb, dcb, ptr);
34841da177e4SLinus Torvalds 				dcb->init_tcq_flag = 1;
34851da177e4SLinus Torvalds 			}
34861da177e4SLinus Torvalds 		}
34871da177e4SLinus Torvalds 
34889dc399deSGuennadi Liakhovetski 		scsi_kunmap_atomic_sg(base);
34899dc399deSGuennadi Liakhovetski 		local_irq_restore(flags);
34909dc399deSGuennadi Liakhovetski 	}
34911da177e4SLinus Torvalds 
34921da177e4SLinus Torvalds 	/* Here is the info for Doug Gilbert's sg3 ... */
3493a862ea31SFUJITA Tomonori 	scsi_set_resid(cmd, srb->total_xfer_length);
34941da177e4SLinus Torvalds 	/* This may be interpreted by sb. or not ... */
34951da177e4SLinus Torvalds 	cmd->SCp.this_residual = srb->total_xfer_length;
34961da177e4SLinus Torvalds 	cmd->SCp.buffers_residual = 0;
34971da177e4SLinus Torvalds 	if (debug_enabled(DBG_KG)) {
34981da177e4SLinus Torvalds 		if (srb->total_xfer_length)
34995cd049a5SChristoph Hellwig 			dprintkdbg(DBG_KG, "srb_done: (0x%p) <%02i-%i> "
35001da177e4SLinus Torvalds 				"cmnd=0x%02x Missed %i bytes\n",
35015cd049a5SChristoph Hellwig 				cmd, cmd->device->id, cmd->device->lun,
35021da177e4SLinus Torvalds 				cmd->cmnd[0], srb->total_xfer_length);
35031da177e4SLinus Torvalds 	}
35041da177e4SLinus Torvalds 
35051da177e4SLinus Torvalds 	srb_going_remove(dcb, srb);
35061da177e4SLinus Torvalds 	/* Add to free list */
35071da177e4SLinus Torvalds 	if (srb == acb->tmp_srb)
35081da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
35091da177e4SLinus Torvalds 	else {
35105cd049a5SChristoph Hellwig 		dprintkdbg(DBG_0, "srb_done: (0x%p) done result=0x%08x\n",
35115cd049a5SChristoph Hellwig 			cmd, cmd->result);
35121da177e4SLinus Torvalds 		srb_free_insert(acb, srb);
35131da177e4SLinus Torvalds 	}
35141da177e4SLinus Torvalds 	pci_unmap_srb(acb, srb);
35151da177e4SLinus Torvalds 
35161da177e4SLinus Torvalds 	cmd->scsi_done(cmd);
35171da177e4SLinus Torvalds 	waiting_process_next(acb);
35181da177e4SLinus Torvalds }
35191da177e4SLinus Torvalds 
35201da177e4SLinus Torvalds 
35211da177e4SLinus Torvalds /* abort all cmds in our queues */
35221da177e4SLinus Torvalds static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
35231da177e4SLinus Torvalds 		struct scsi_cmnd *cmd, u8 force)
35241da177e4SLinus Torvalds {
35251da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
35261da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "doing_srb_done: pids ");
35271da177e4SLinus Torvalds 
35281da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
35291da177e4SLinus Torvalds 		struct ScsiReqBlk *srb;
35301da177e4SLinus Torvalds 		struct ScsiReqBlk *tmp;
35311da177e4SLinus Torvalds 		struct scsi_cmnd *p;
35321da177e4SLinus Torvalds 
35331da177e4SLinus Torvalds 		list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
35341da177e4SLinus Torvalds 			enum dma_data_direction dir;
35351da177e4SLinus Torvalds 			int result;
35361da177e4SLinus Torvalds 
35371da177e4SLinus Torvalds 			p = srb->cmd;
35381da177e4SLinus Torvalds 			dir = p->sc_data_direction;
35391da177e4SLinus Torvalds 			result = MK_RES(0, did_flag, 0, 0);
35405cd049a5SChristoph Hellwig 			printk("G:%p(%02i-%i) ", p,
35411da177e4SLinus Torvalds 			       p->device->id, p->device->lun);
35421da177e4SLinus Torvalds 			srb_going_remove(dcb, srb);
35431da177e4SLinus Torvalds 			free_tag(dcb, srb);
35441da177e4SLinus Torvalds 			srb_free_insert(acb, srb);
35451da177e4SLinus Torvalds 			p->result = result;
35461da177e4SLinus Torvalds 			pci_unmap_srb_sense(acb, srb);
35471da177e4SLinus Torvalds 			pci_unmap_srb(acb, srb);
35481da177e4SLinus Torvalds 			if (force) {
35491da177e4SLinus Torvalds 				/* For new EH, we normally don't need to give commands back,
35501da177e4SLinus Torvalds 				 * as they all complete or all time out */
35511da177e4SLinus Torvalds 				p->scsi_done(p);
35521da177e4SLinus Torvalds 			}
35531da177e4SLinus Torvalds 		}
35541da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_going_list))
35551da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
35561da177e4SLinus Torvalds 			       "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
35571da177e4SLinus Torvalds 			       dcb->target_id, dcb->target_lun);
35581da177e4SLinus Torvalds 		if (dcb->tag_mask)
35591da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG,
35601da177e4SLinus Torvalds 			       "tag_mask for <%02i-%i> should be empty, is %08x!\n",
35611da177e4SLinus Torvalds 			       dcb->target_id, dcb->target_lun,
35621da177e4SLinus Torvalds 			       dcb->tag_mask);
35631da177e4SLinus Torvalds 
35641da177e4SLinus Torvalds 		/* Waiting queue */
35651da177e4SLinus Torvalds 		list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
35661da177e4SLinus Torvalds 			int result;
35671da177e4SLinus Torvalds 			p = srb->cmd;
35681da177e4SLinus Torvalds 
35691da177e4SLinus Torvalds 			result = MK_RES(0, did_flag, 0, 0);
35705cd049a5SChristoph Hellwig 			printk("W:%p<%02i-%i>", p, p->device->id,
35711da177e4SLinus Torvalds 			       p->device->lun);
35721da177e4SLinus Torvalds 			srb_waiting_remove(dcb, srb);
35731da177e4SLinus Torvalds 			srb_free_insert(acb, srb);
35741da177e4SLinus Torvalds 			p->result = result;
35751da177e4SLinus Torvalds 			pci_unmap_srb_sense(acb, srb);
35761da177e4SLinus Torvalds 			pci_unmap_srb(acb, srb);
35771da177e4SLinus Torvalds 			if (force) {
35781da177e4SLinus Torvalds 				/* For new EH, we normally don't need to give commands back,
35791da177e4SLinus Torvalds 				 * as they all complete or all time out */
35801da177e4SLinus Torvalds 				cmd->scsi_done(cmd);
35811da177e4SLinus Torvalds 			}
35821da177e4SLinus Torvalds 		}
35831da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_waiting_list))
35841da177e4SLinus Torvalds 			dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
35851da177e4SLinus Torvalds 			     list_size(&dcb->srb_waiting_list), dcb->target_id,
35861da177e4SLinus Torvalds 			     dcb->target_lun);
35871da177e4SLinus Torvalds 		dcb->flag &= ~ABORT_DEV_;
35881da177e4SLinus Torvalds 	}
35891da177e4SLinus Torvalds 	printk("\n");
35901da177e4SLinus Torvalds }
35911da177e4SLinus Torvalds 
35921da177e4SLinus Torvalds 
35931da177e4SLinus Torvalds static void reset_scsi_bus(struct AdapterCtlBlk *acb)
35941da177e4SLinus Torvalds {
35951da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
35961da177e4SLinus Torvalds 	acb->acb_flag |= RESET_DEV;	/* RESET_DETECT, RESET_DONE, RESET_DEV */
35971da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
35981da177e4SLinus Torvalds 
35991da177e4SLinus Torvalds 	while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
36001da177e4SLinus Torvalds 		/* nothing */;
36011da177e4SLinus Torvalds }
36021da177e4SLinus Torvalds 
36031da177e4SLinus Torvalds 
36041da177e4SLinus Torvalds static void set_basic_config(struct AdapterCtlBlk *acb)
36051da177e4SLinus Torvalds {
36061da177e4SLinus Torvalds 	u8 bval;
36071da177e4SLinus Torvalds 	u16 wval;
36081da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
36091da177e4SLinus Torvalds 	if (acb->config & HCC_PARITY)
36101da177e4SLinus Torvalds 		bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
36111da177e4SLinus Torvalds 	else
36121da177e4SLinus Torvalds 		bval = PHASELATCH | INITIATOR | BLOCKRST;
36131da177e4SLinus Torvalds 
36141da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
36151da177e4SLinus Torvalds 
36161da177e4SLinus Torvalds 	/* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
36171da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03);	/* was 0x13: default */
36181da177e4SLinus Torvalds 	/* program Host ID                  */
36191da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
36201da177e4SLinus Torvalds 	/* set ansynchronous transfer       */
36211da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
36221da177e4SLinus Torvalds 	/* Turn LED control off */
36231da177e4SLinus Torvalds 	wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
36241da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
36251da177e4SLinus Torvalds 	/* DMA config          */
36261da177e4SLinus Torvalds 	wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
36271da177e4SLinus Torvalds 	wval |=
36281da177e4SLinus Torvalds 	    DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
36291da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
36301da177e4SLinus Torvalds 	/* Clear pending interrupt status */
36311da177e4SLinus Torvalds 	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
36321da177e4SLinus Torvalds 	/* Enable SCSI interrupt    */
36331da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
36341da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
36351da177e4SLinus Torvalds 		      /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
36361da177e4SLinus Torvalds 		      );
36371da177e4SLinus Torvalds }
36381da177e4SLinus Torvalds 
36391da177e4SLinus Torvalds 
36401da177e4SLinus Torvalds static void scsi_reset_detect(struct AdapterCtlBlk *acb)
36411da177e4SLinus Torvalds {
36421da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
36431da177e4SLinus Torvalds 	/* delay half a second */
36441da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
36451da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
36461da177e4SLinus Torvalds 
36471da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
36481da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
36491da177e4SLinus Torvalds 	/*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
36501da177e4SLinus Torvalds 	udelay(500);
36511da177e4SLinus Torvalds 	/* Maybe we locked up the bus? Then lets wait even longer ... */
36521da177e4SLinus Torvalds 	acb->scsi_host->last_reset =
36531da177e4SLinus Torvalds 	    jiffies + 5 * HZ / 2 +
36541da177e4SLinus Torvalds 	    HZ * acb->eeprom.delay_time;
36551da177e4SLinus Torvalds 
36561da177e4SLinus Torvalds 	clear_fifo(acb, "scsi_reset_detect");
36571da177e4SLinus Torvalds 	set_basic_config(acb);
36581da177e4SLinus Torvalds 	/*1.25 */
36591da177e4SLinus Torvalds 	/*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
36601da177e4SLinus Torvalds 
36611da177e4SLinus Torvalds 	if (acb->acb_flag & RESET_DEV) {	/* RESET_DETECT, RESET_DONE, RESET_DEV */
36621da177e4SLinus Torvalds 		acb->acb_flag |= RESET_DONE;
36631da177e4SLinus Torvalds 	} else {
36641da177e4SLinus Torvalds 		acb->acb_flag |= RESET_DETECT;
36651da177e4SLinus Torvalds 		reset_dev_param(acb);
36661da177e4SLinus Torvalds 		doing_srb_done(acb, DID_RESET, NULL, 1);
36671da177e4SLinus Torvalds 		/*DC395x_RecoverSRB( acb ); */
36681da177e4SLinus Torvalds 		acb->active_dcb = NULL;
36691da177e4SLinus Torvalds 		acb->acb_flag = 0;
36701da177e4SLinus Torvalds 		waiting_process_next(acb);
36711da177e4SLinus Torvalds 	}
36721da177e4SLinus Torvalds }
36731da177e4SLinus Torvalds 
36741da177e4SLinus Torvalds 
36751da177e4SLinus Torvalds static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
36761da177e4SLinus Torvalds 		struct ScsiReqBlk *srb)
36771da177e4SLinus Torvalds {
36781da177e4SLinus Torvalds 	struct scsi_cmnd *cmd = srb->cmd;
36795cd049a5SChristoph Hellwig 	dprintkdbg(DBG_1, "request_sense: (0x%p) <%02i-%i>\n",
36805cd049a5SChristoph Hellwig 		cmd, cmd->device->id, cmd->device->lun);
36811da177e4SLinus Torvalds 
36821da177e4SLinus Torvalds 	srb->flag |= AUTO_REQSENSE;
36831da177e4SLinus Torvalds 	srb->adapter_status = 0;
36841da177e4SLinus Torvalds 	srb->target_status = 0;
36851da177e4SLinus Torvalds 
36861da177e4SLinus Torvalds 	/* KG: Can this prevent crap sense data ? */
3687b80ca4f7SFUJITA Tomonori 	memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
36881da177e4SLinus Torvalds 
36891da177e4SLinus Torvalds 	/* Save some data */
36901da177e4SLinus Torvalds 	srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
36911da177e4SLinus Torvalds 	    srb->segment_x[0].address;
36921da177e4SLinus Torvalds 	srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
36931da177e4SLinus Torvalds 	    srb->segment_x[0].length;
36941da177e4SLinus Torvalds 	srb->xferred = srb->total_xfer_length;
36951da177e4SLinus Torvalds 	/* srb->segment_x : a one entry of S/G list table */
3696b80ca4f7SFUJITA Tomonori 	srb->total_xfer_length = SCSI_SENSE_BUFFERSIZE;
3697b80ca4f7SFUJITA Tomonori 	srb->segment_x[0].length = SCSI_SENSE_BUFFERSIZE;
36981da177e4SLinus Torvalds 	/* Map sense buffer */
36991da177e4SLinus Torvalds 	srb->segment_x[0].address =
37001da177e4SLinus Torvalds 	    pci_map_single(acb->dev, cmd->sense_buffer,
3701b80ca4f7SFUJITA Tomonori 			   SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
37021da177e4SLinus Torvalds 	dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
37031da177e4SLinus Torvalds 	       cmd->sense_buffer, srb->segment_x[0].address,
3704b80ca4f7SFUJITA Tomonori 	       SCSI_SENSE_BUFFERSIZE);
37051da177e4SLinus Torvalds 	srb->sg_count = 1;
37061da177e4SLinus Torvalds 	srb->sg_index = 0;
37071da177e4SLinus Torvalds 
37081da177e4SLinus Torvalds 	if (start_scsi(acb, dcb, srb)) {	/* Should only happen, if sb. else grabs the bus */
37091da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG,
37105cd049a5SChristoph Hellwig 			"request_sense: (0x%p) failed <%02i-%i>\n",
37115cd049a5SChristoph Hellwig 			srb->cmd, dcb->target_id, dcb->target_lun);
37121da177e4SLinus Torvalds 		srb_going_to_waiting_move(dcb, srb);
37131da177e4SLinus Torvalds 		waiting_set_timer(acb, HZ / 100);
37141da177e4SLinus Torvalds 	}
37151da177e4SLinus Torvalds }
37161da177e4SLinus Torvalds 
37171da177e4SLinus Torvalds 
37181da177e4SLinus Torvalds /**
37191da177e4SLinus Torvalds  * device_alloc - Allocate a new device instance. This create the
37201da177e4SLinus Torvalds  * devices instance and sets up all the data items. The adapter
37211da177e4SLinus Torvalds  * instance is required to obtain confiuration information for this
37221da177e4SLinus Torvalds  * device. This does *not* add this device to the adapters device
37231da177e4SLinus Torvalds  * list.
37241da177e4SLinus Torvalds  *
37251da177e4SLinus Torvalds  * @acb: The adapter to obtain configuration information from.
37261da177e4SLinus Torvalds  * @target: The target for the new device.
37271da177e4SLinus Torvalds  * @lun: The lun for the new device.
37281da177e4SLinus Torvalds  *
3729d6e05edcSAndreas Mohr  * Return the new device if successful or NULL on failure.
37301da177e4SLinus Torvalds  **/
37311da177e4SLinus Torvalds static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
37321da177e4SLinus Torvalds 		u8 target, u8 lun)
37331da177e4SLinus Torvalds {
37341da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
37351da177e4SLinus Torvalds 	u8 period_index = eeprom->target[target].period & 0x07;
37361da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
37371da177e4SLinus Torvalds 
37381da177e4SLinus Torvalds 	dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
37391da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
37401da177e4SLinus Torvalds 	if (!dcb)
37411da177e4SLinus Torvalds 		return NULL;
37421da177e4SLinus Torvalds 	dcb->acb = NULL;
37431da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dcb->srb_going_list);
37441da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dcb->srb_waiting_list);
37451da177e4SLinus Torvalds 	dcb->active_srb = NULL;
37461da177e4SLinus Torvalds 	dcb->tag_mask = 0;
37471da177e4SLinus Torvalds 	dcb->max_command = 1;
37481da177e4SLinus Torvalds 	dcb->target_id = target;
37491da177e4SLinus Torvalds 	dcb->target_lun = lun;
3750*208afec4SDan Carpenter 	dcb->dev_mode = eeprom->target[target].cfg0;
37511da177e4SLinus Torvalds #ifndef DC395x_NO_DISCONNECT
37521da177e4SLinus Torvalds 	dcb->identify_msg =
37531da177e4SLinus Torvalds 	    IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
37541da177e4SLinus Torvalds #else
37551da177e4SLinus Torvalds 	dcb->identify_msg = IDENTIFY(0, lun);
37561da177e4SLinus Torvalds #endif
37571da177e4SLinus Torvalds 	dcb->inquiry7 = 0;
37581da177e4SLinus Torvalds 	dcb->sync_mode = 0;
37591da177e4SLinus Torvalds 	dcb->min_nego_period = clock_period[period_index];
37601da177e4SLinus Torvalds 	dcb->sync_period = 0;
37611da177e4SLinus Torvalds 	dcb->sync_offset = 0;
37621da177e4SLinus Torvalds 	dcb->flag = 0;
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds #ifndef DC395x_NO_WIDE
37651da177e4SLinus Torvalds 	if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
37661da177e4SLinus Torvalds 	    && (acb->config & HCC_WIDE_CARD))
37671da177e4SLinus Torvalds 		dcb->sync_mode |= WIDE_NEGO_ENABLE;
37681da177e4SLinus Torvalds #endif
37691da177e4SLinus Torvalds #ifndef DC395x_NO_SYNC
37701da177e4SLinus Torvalds 	if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
37711da177e4SLinus Torvalds 		if (!(lun) || current_sync_offset)
37721da177e4SLinus Torvalds 			dcb->sync_mode |= SYNC_NEGO_ENABLE;
37731da177e4SLinus Torvalds #endif
37741da177e4SLinus Torvalds 	if (dcb->target_lun != 0) {
37751da177e4SLinus Torvalds 		/* Copy settings */
37761da177e4SLinus Torvalds 		struct DeviceCtlBlk *p;
37771da177e4SLinus Torvalds 		list_for_each_entry(p, &acb->dcb_list, list)
37781da177e4SLinus Torvalds 			if (p->target_id == dcb->target_id)
37791da177e4SLinus Torvalds 				break;
37801da177e4SLinus Torvalds 		dprintkdbg(DBG_1,
37811da177e4SLinus Torvalds 		       "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
37821da177e4SLinus Torvalds 		       dcb->target_id, dcb->target_lun,
37831da177e4SLinus Torvalds 		       p->target_id, p->target_lun);
37841da177e4SLinus Torvalds 		dcb->sync_mode = p->sync_mode;
37851da177e4SLinus Torvalds 		dcb->sync_period = p->sync_period;
37861da177e4SLinus Torvalds 		dcb->min_nego_period = p->min_nego_period;
37871da177e4SLinus Torvalds 		dcb->sync_offset = p->sync_offset;
37881da177e4SLinus Torvalds 		dcb->inquiry7 = p->inquiry7;
37891da177e4SLinus Torvalds 	}
37901da177e4SLinus Torvalds 	return dcb;
37911da177e4SLinus Torvalds }
37921da177e4SLinus Torvalds 
37931da177e4SLinus Torvalds 
37941da177e4SLinus Torvalds /**
37951da177e4SLinus Torvalds  * adapter_add_device - Adds the device instance to the adaptor instance.
37961da177e4SLinus Torvalds  *
37971da177e4SLinus Torvalds  * @acb: The adapter device to be updated
3798b595076aSUwe Kleine-König  * @dcb: A newly created and initialised device instance to add.
37991da177e4SLinus Torvalds  **/
38001da177e4SLinus Torvalds static void adapter_add_device(struct AdapterCtlBlk *acb,
38011da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
38021da177e4SLinus Torvalds {
38031da177e4SLinus Torvalds 	/* backpointer to adapter */
38041da177e4SLinus Torvalds 	dcb->acb = acb;
38051da177e4SLinus Torvalds 
38061da177e4SLinus Torvalds 	/* set run_robin to this device if it is currently empty */
38071da177e4SLinus Torvalds 	if (list_empty(&acb->dcb_list))
38081da177e4SLinus Torvalds 		acb->dcb_run_robin = dcb;
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds 	/* add device to list */
38111da177e4SLinus Torvalds 	list_add_tail(&dcb->list, &acb->dcb_list);
38121da177e4SLinus Torvalds 
38131da177e4SLinus Torvalds 	/* update device maps */
38141da177e4SLinus Torvalds 	acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
38151da177e4SLinus Torvalds 	acb->children[dcb->target_id][dcb->target_lun] = dcb;
38161da177e4SLinus Torvalds }
38171da177e4SLinus Torvalds 
38181da177e4SLinus Torvalds 
38191da177e4SLinus Torvalds /**
38201da177e4SLinus Torvalds  * adapter_remove_device - Removes the device instance from the adaptor
38211da177e4SLinus Torvalds  * instance. The device instance is not check in any way or freed by this.
38221da177e4SLinus Torvalds  * The caller is expected to take care of that. This will simply remove the
38231da177e4SLinus Torvalds  * device from the adapters data strcutures.
38241da177e4SLinus Torvalds  *
38251da177e4SLinus Torvalds  * @acb: The adapter device to be updated
38261da177e4SLinus Torvalds  * @dcb: A device that has previously been added to the adapter.
38271da177e4SLinus Torvalds  **/
38281da177e4SLinus Torvalds static void adapter_remove_device(struct AdapterCtlBlk *acb,
38291da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
38301da177e4SLinus Torvalds {
38311da177e4SLinus Torvalds 	struct DeviceCtlBlk *i;
38321da177e4SLinus Torvalds 	struct DeviceCtlBlk *tmp;
38331da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
38341da177e4SLinus Torvalds 		dcb->target_id, dcb->target_lun);
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds 	/* fix up any pointers to this device that we have in the adapter */
38371da177e4SLinus Torvalds 	if (acb->active_dcb == dcb)
38381da177e4SLinus Torvalds 		acb->active_dcb = NULL;
38391da177e4SLinus Torvalds 	if (acb->dcb_run_robin == dcb)
38401da177e4SLinus Torvalds 		acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
38411da177e4SLinus Torvalds 
38421da177e4SLinus Torvalds 	/* unlink from list */
38431da177e4SLinus Torvalds 	list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
38441da177e4SLinus Torvalds 		if (dcb == i) {
38451da177e4SLinus Torvalds 			list_del(&i->list);
38461da177e4SLinus Torvalds 			break;
38471da177e4SLinus Torvalds 		}
38481da177e4SLinus Torvalds 
38491da177e4SLinus Torvalds 	/* clear map and children */
38501da177e4SLinus Torvalds 	acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
38511da177e4SLinus Torvalds 	acb->children[dcb->target_id][dcb->target_lun] = NULL;
38521da177e4SLinus Torvalds 	dcb->acb = NULL;
38531da177e4SLinus Torvalds }
38541da177e4SLinus Torvalds 
38551da177e4SLinus Torvalds 
38561da177e4SLinus Torvalds /**
38571da177e4SLinus Torvalds  * adapter_remove_and_free_device - Removes a single device from the adapter
38581da177e4SLinus Torvalds  * and then frees the device information.
38591da177e4SLinus Torvalds  *
38601da177e4SLinus Torvalds  * @acb: The adapter device to be updated
38611da177e4SLinus Torvalds  * @dcb: A device that has previously been added to the adapter.
38621da177e4SLinus Torvalds  */
38631da177e4SLinus Torvalds static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
38641da177e4SLinus Torvalds 		struct DeviceCtlBlk *dcb)
38651da177e4SLinus Torvalds {
38661da177e4SLinus Torvalds 	if (list_size(&dcb->srb_going_list) > 1) {
38671da177e4SLinus Torvalds 		dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
38681da177e4SLinus Torvalds 		           "Won't remove because of %i active requests.\n",
38691da177e4SLinus Torvalds 			   dcb->target_id, dcb->target_lun,
38701da177e4SLinus Torvalds 			   list_size(&dcb->srb_going_list));
38711da177e4SLinus Torvalds 		return;
38721da177e4SLinus Torvalds 	}
38731da177e4SLinus Torvalds 	adapter_remove_device(acb, dcb);
38741da177e4SLinus Torvalds 	kfree(dcb);
38751da177e4SLinus Torvalds }
38761da177e4SLinus Torvalds 
38771da177e4SLinus Torvalds 
38781da177e4SLinus Torvalds /**
38791da177e4SLinus Torvalds  * adapter_remove_and_free_all_devices - Removes and frees all of the
38801da177e4SLinus Torvalds  * devices associated with the specified adapter.
38811da177e4SLinus Torvalds  *
38821da177e4SLinus Torvalds  * @acb: The adapter from which all devices should be removed.
38831da177e4SLinus Torvalds  **/
38841da177e4SLinus Torvalds static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
38851da177e4SLinus Torvalds {
38861da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
38871da177e4SLinus Torvalds 	struct DeviceCtlBlk *tmp;
38881da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
38891da177e4SLinus Torvalds 		   list_size(&acb->dcb_list));
38901da177e4SLinus Torvalds 
38911da177e4SLinus Torvalds 	list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
38921da177e4SLinus Torvalds 		adapter_remove_and_free_device(acb, dcb);
38931da177e4SLinus Torvalds }
38941da177e4SLinus Torvalds 
38951da177e4SLinus Torvalds 
38961da177e4SLinus Torvalds /**
38971da177e4SLinus Torvalds  * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
38981da177e4SLinus Torvalds  * scsi device that we need to deal with. We allocate a new device and then
38991da177e4SLinus Torvalds  * insert that device into the adapters device list.
39001da177e4SLinus Torvalds  *
39011da177e4SLinus Torvalds  * @scsi_device: The new scsi device that we need to handle.
39021da177e4SLinus Torvalds  **/
39031da177e4SLinus Torvalds static int dc395x_slave_alloc(struct scsi_device *scsi_device)
39041da177e4SLinus Torvalds {
39051da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
39061da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
39071da177e4SLinus Torvalds 
39081da177e4SLinus Torvalds 	dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
39091da177e4SLinus Torvalds 	if (!dcb)
39101da177e4SLinus Torvalds 		return -ENOMEM;
39111da177e4SLinus Torvalds 	adapter_add_device(acb, dcb);
39121da177e4SLinus Torvalds 
39131da177e4SLinus Torvalds 	return 0;
39141da177e4SLinus Torvalds }
39151da177e4SLinus Torvalds 
39161da177e4SLinus Torvalds 
39171da177e4SLinus Torvalds /**
39181da177e4SLinus Torvalds  * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
39191da177e4SLinus Torvalds  * device that is going away.
39201da177e4SLinus Torvalds  *
39211da177e4SLinus Torvalds  * @scsi_device: The new scsi device that we need to handle.
39221da177e4SLinus Torvalds  **/
39231da177e4SLinus Torvalds static void dc395x_slave_destroy(struct scsi_device *scsi_device)
39241da177e4SLinus Torvalds {
39251da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
39261da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
39271da177e4SLinus Torvalds 	if (dcb)
39281da177e4SLinus Torvalds 		adapter_remove_and_free_device(acb, dcb);
39291da177e4SLinus Torvalds }
39301da177e4SLinus Torvalds 
39311da177e4SLinus Torvalds 
39321da177e4SLinus Torvalds 
39331da177e4SLinus Torvalds 
39341da177e4SLinus Torvalds /**
39351da177e4SLinus Torvalds  * trms1040_wait_30us: wait for 30 us
39361da177e4SLinus Torvalds  *
39371da177e4SLinus Torvalds  * Waits for 30us (using the chip by the looks of it..)
39381da177e4SLinus Torvalds  *
39391da177e4SLinus Torvalds  * @io_port: base I/O address
39401da177e4SLinus Torvalds  **/
39416f039790SGreg Kroah-Hartman static void trms1040_wait_30us(unsigned long io_port)
39421da177e4SLinus Torvalds {
39431da177e4SLinus Torvalds 	/* ScsiPortStallExecution(30); wait 30 us */
39441da177e4SLinus Torvalds 	outb(5, io_port + TRM_S1040_GEN_TIMER);
39451da177e4SLinus Torvalds 	while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
39461da177e4SLinus Torvalds 		/* nothing */ ;
39471da177e4SLinus Torvalds }
39481da177e4SLinus Torvalds 
39491da177e4SLinus Torvalds 
39501da177e4SLinus Torvalds /**
39511da177e4SLinus Torvalds  * trms1040_write_cmd - write the secified command and address to
39521da177e4SLinus Torvalds  * chip
39531da177e4SLinus Torvalds  *
39541da177e4SLinus Torvalds  * @io_port:	base I/O address
39551da177e4SLinus Torvalds  * @cmd:	SB + op code (command) to send
39561da177e4SLinus Torvalds  * @addr:	address to send
39571da177e4SLinus Torvalds  **/
39586f039790SGreg Kroah-Hartman static void trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
39591da177e4SLinus Torvalds {
39601da177e4SLinus Torvalds 	int i;
39611da177e4SLinus Torvalds 	u8 send_data;
39621da177e4SLinus Torvalds 
39631da177e4SLinus Torvalds 	/* program SB + OP code */
39641da177e4SLinus Torvalds 	for (i = 0; i < 3; i++, cmd <<= 1) {
39651da177e4SLinus Torvalds 		send_data = NVR_SELECT;
39661da177e4SLinus Torvalds 		if (cmd & 0x04)	/* Start from bit 2 */
39671da177e4SLinus Torvalds 			send_data |= NVR_BITOUT;
39681da177e4SLinus Torvalds 
39691da177e4SLinus Torvalds 		outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
39701da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
39711da177e4SLinus Torvalds 		outb((send_data | NVR_CLOCK),
39721da177e4SLinus Torvalds 		     io_port + TRM_S1040_GEN_NVRAM);
39731da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
39741da177e4SLinus Torvalds 	}
39751da177e4SLinus Torvalds 
39761da177e4SLinus Torvalds 	/* send address */
39771da177e4SLinus Torvalds 	for (i = 0; i < 7; i++, addr <<= 1) {
39781da177e4SLinus Torvalds 		send_data = NVR_SELECT;
39791da177e4SLinus Torvalds 		if (addr & 0x40)	/* Start from bit 6 */
39801da177e4SLinus Torvalds 			send_data |= NVR_BITOUT;
39811da177e4SLinus Torvalds 
39821da177e4SLinus Torvalds 		outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
39831da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
39841da177e4SLinus Torvalds 		outb((send_data | NVR_CLOCK),
39851da177e4SLinus Torvalds 		     io_port + TRM_S1040_GEN_NVRAM);
39861da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
39871da177e4SLinus Torvalds 	}
39881da177e4SLinus Torvalds 	outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
39891da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
39901da177e4SLinus Torvalds }
39911da177e4SLinus Torvalds 
39921da177e4SLinus Torvalds 
39931da177e4SLinus Torvalds /**
39941da177e4SLinus Torvalds  * trms1040_set_data - store a single byte in the eeprom
39951da177e4SLinus Torvalds  *
39961da177e4SLinus Torvalds  * Called from write all to write a single byte into the SSEEPROM
39971da177e4SLinus Torvalds  * Which is done one bit at a time.
39981da177e4SLinus Torvalds  *
39991da177e4SLinus Torvalds  * @io_port:	base I/O address
40001da177e4SLinus Torvalds  * @addr:	offset into EEPROM
40011da177e4SLinus Torvalds  * @byte:	bytes to write
40021da177e4SLinus Torvalds  **/
40036f039790SGreg Kroah-Hartman static void trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
40041da177e4SLinus Torvalds {
40051da177e4SLinus Torvalds 	int i;
40061da177e4SLinus Torvalds 	u8 send_data;
40071da177e4SLinus Torvalds 
40081da177e4SLinus Torvalds 	/* Send write command & address */
40091da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x05, addr);
40101da177e4SLinus Torvalds 
40111da177e4SLinus Torvalds 	/* Write data */
40121da177e4SLinus Torvalds 	for (i = 0; i < 8; i++, byte <<= 1) {
40131da177e4SLinus Torvalds 		send_data = NVR_SELECT;
40141da177e4SLinus Torvalds 		if (byte & 0x80)	/* Start from bit 7 */
40151da177e4SLinus Torvalds 			send_data |= NVR_BITOUT;
40161da177e4SLinus Torvalds 
40171da177e4SLinus Torvalds 		outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
40181da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
40191da177e4SLinus Torvalds 		outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
40201da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
40211da177e4SLinus Torvalds 	}
40221da177e4SLinus Torvalds 	outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
40231da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
40241da177e4SLinus Torvalds 
40251da177e4SLinus Torvalds 	/* Disable chip select */
40261da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
40271da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
40281da177e4SLinus Torvalds 
40291da177e4SLinus Torvalds 	outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
40301da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
40311da177e4SLinus Torvalds 
40321da177e4SLinus Torvalds 	/* Wait for write ready */
40331da177e4SLinus Torvalds 	while (1) {
40341da177e4SLinus Torvalds 		outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
40351da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
40361da177e4SLinus Torvalds 
40371da177e4SLinus Torvalds 		outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
40381da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
40391da177e4SLinus Torvalds 
40401da177e4SLinus Torvalds 		if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
40411da177e4SLinus Torvalds 			break;
40421da177e4SLinus Torvalds 	}
40431da177e4SLinus Torvalds 
40441da177e4SLinus Torvalds 	/*  Disable chip select */
40451da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
40461da177e4SLinus Torvalds }
40471da177e4SLinus Torvalds 
40481da177e4SLinus Torvalds 
40491da177e4SLinus Torvalds /**
40501da177e4SLinus Torvalds  * trms1040_write_all - write 128 bytes to the eeprom
40511da177e4SLinus Torvalds  *
40521da177e4SLinus Torvalds  * Write the supplied 128 bytes to the chips SEEPROM
40531da177e4SLinus Torvalds  *
40541da177e4SLinus Torvalds  * @eeprom:	the data to write
40551da177e4SLinus Torvalds  * @io_port:	the base io port
40561da177e4SLinus Torvalds  **/
40576f039790SGreg Kroah-Hartman static void trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
40581da177e4SLinus Torvalds {
40591da177e4SLinus Torvalds 	u8 *b_eeprom = (u8 *)eeprom;
40601da177e4SLinus Torvalds 	u8 addr;
40611da177e4SLinus Torvalds 
40621da177e4SLinus Torvalds 	/* Enable SEEPROM */
40631da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
40641da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
40651da177e4SLinus Torvalds 
40661da177e4SLinus Torvalds 	/* write enable */
40671da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x04, 0xFF);
40681da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
40691da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
40701da177e4SLinus Torvalds 
40711da177e4SLinus Torvalds 	/* write */
40721da177e4SLinus Torvalds 	for (addr = 0; addr < 128; addr++, b_eeprom++)
40731da177e4SLinus Torvalds 		trms1040_set_data(io_port, addr, *b_eeprom);
40741da177e4SLinus Torvalds 
40751da177e4SLinus Torvalds 	/* write disable */
40761da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x04, 0x00);
40771da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
40781da177e4SLinus Torvalds 	trms1040_wait_30us(io_port);
40791da177e4SLinus Torvalds 
40801da177e4SLinus Torvalds 	/* Disable SEEPROM */
40811da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
40821da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
40831da177e4SLinus Torvalds }
40841da177e4SLinus Torvalds 
40851da177e4SLinus Torvalds 
40861da177e4SLinus Torvalds /**
40871da177e4SLinus Torvalds  * trms1040_get_data - get a single byte from the eeprom
40881da177e4SLinus Torvalds  *
40891da177e4SLinus Torvalds  * Called from read all to read a single byte into the SSEEPROM
40901da177e4SLinus Torvalds  * Which is done one bit at a time.
40911da177e4SLinus Torvalds  *
40921da177e4SLinus Torvalds  * @io_port:	base I/O address
40931da177e4SLinus Torvalds  * @addr:	offset into SEEPROM
40941da177e4SLinus Torvalds  *
409559c51591SMichael Opdenacker  * Returns the byte read.
40961da177e4SLinus Torvalds  **/
40976f039790SGreg Kroah-Hartman static u8 trms1040_get_data(unsigned long io_port, u8 addr)
40981da177e4SLinus Torvalds {
40991da177e4SLinus Torvalds 	int i;
41001da177e4SLinus Torvalds 	u8 read_byte;
41011da177e4SLinus Torvalds 	u8 result = 0;
41021da177e4SLinus Torvalds 
41031da177e4SLinus Torvalds 	/* Send read command & address */
41041da177e4SLinus Torvalds 	trms1040_write_cmd(io_port, 0x06, addr);
41051da177e4SLinus Torvalds 
41061da177e4SLinus Torvalds 	/* read data */
41071da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
41081da177e4SLinus Torvalds 		outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
41091da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
41101da177e4SLinus Torvalds 		outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
41111da177e4SLinus Torvalds 
41121da177e4SLinus Torvalds 		/* Get data bit while falling edge */
41131da177e4SLinus Torvalds 		read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
41141da177e4SLinus Torvalds 		result <<= 1;
41151da177e4SLinus Torvalds 		if (read_byte & NVR_BITIN)
41161da177e4SLinus Torvalds 			result |= 1;
41171da177e4SLinus Torvalds 
41181da177e4SLinus Torvalds 		trms1040_wait_30us(io_port);
41191da177e4SLinus Torvalds 	}
41201da177e4SLinus Torvalds 
41211da177e4SLinus Torvalds 	/* Disable chip select */
41221da177e4SLinus Torvalds 	outb(0, io_port + TRM_S1040_GEN_NVRAM);
41231da177e4SLinus Torvalds 	return result;
41241da177e4SLinus Torvalds }
41251da177e4SLinus Torvalds 
41261da177e4SLinus Torvalds 
41271da177e4SLinus Torvalds /**
41281da177e4SLinus Torvalds  * trms1040_read_all - read all bytes from the eeprom
41291da177e4SLinus Torvalds  *
41301da177e4SLinus Torvalds  * Read the 128 bytes from the SEEPROM.
41311da177e4SLinus Torvalds  *
41321da177e4SLinus Torvalds  * @eeprom:	where to store the data
41331da177e4SLinus Torvalds  * @io_port:	the base io port
41341da177e4SLinus Torvalds  **/
41356f039790SGreg Kroah-Hartman static void trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
41361da177e4SLinus Torvalds {
41371da177e4SLinus Torvalds 	u8 *b_eeprom = (u8 *)eeprom;
41381da177e4SLinus Torvalds 	u8 addr;
41391da177e4SLinus Torvalds 
41401da177e4SLinus Torvalds 	/* Enable SEEPROM */
41411da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
41421da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
41431da177e4SLinus Torvalds 
41441da177e4SLinus Torvalds 	/* read details */
41451da177e4SLinus Torvalds 	for (addr = 0; addr < 128; addr++, b_eeprom++)
41461da177e4SLinus Torvalds 		*b_eeprom = trms1040_get_data(io_port, addr);
41471da177e4SLinus Torvalds 
41481da177e4SLinus Torvalds 	/* Disable SEEPROM */
41491da177e4SLinus Torvalds 	outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
41501da177e4SLinus Torvalds 	     io_port + TRM_S1040_GEN_CONTROL);
41511da177e4SLinus Torvalds }
41521da177e4SLinus Torvalds 
41531da177e4SLinus Torvalds 
41541da177e4SLinus Torvalds 
41551da177e4SLinus Torvalds /**
41561da177e4SLinus Torvalds  * check_eeprom - get and check contents of the eeprom
41571da177e4SLinus Torvalds  *
41581da177e4SLinus Torvalds  * Read seeprom 128 bytes into the memory provider in eeprom.
41591da177e4SLinus Torvalds  * Checks the checksum and if it's not correct it uses a set of default
41601da177e4SLinus Torvalds  * values.
41611da177e4SLinus Torvalds  *
41621da177e4SLinus Torvalds  * @eeprom:	caller allocated strcuture to read the eeprom data into
41631da177e4SLinus Torvalds  * @io_port:	io port to read from
41641da177e4SLinus Torvalds  **/
41656f039790SGreg Kroah-Hartman static void check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
41661da177e4SLinus Torvalds {
41671da177e4SLinus Torvalds 	u16 *w_eeprom = (u16 *)eeprom;
41681da177e4SLinus Torvalds 	u16 w_addr;
41691da177e4SLinus Torvalds 	u16 cksum;
41701da177e4SLinus Torvalds 	u32 d_addr;
41711da177e4SLinus Torvalds 	u32 *d_eeprom;
41721da177e4SLinus Torvalds 
41731da177e4SLinus Torvalds 	trms1040_read_all(eeprom, io_port);	/* read eeprom */
41741da177e4SLinus Torvalds 
41751da177e4SLinus Torvalds 	cksum = 0;
41761da177e4SLinus Torvalds 	for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
41771da177e4SLinus Torvalds 	     w_addr++, w_eeprom++)
41781da177e4SLinus Torvalds 		cksum += *w_eeprom;
41791da177e4SLinus Torvalds 	if (cksum != 0x1234) {
41801da177e4SLinus Torvalds 		/*
41811da177e4SLinus Torvalds 		 * Checksum is wrong.
41821da177e4SLinus Torvalds 		 * Load a set of defaults into the eeprom buffer
41831da177e4SLinus Torvalds 		 */
41841da177e4SLinus Torvalds 		dprintkl(KERN_WARNING,
41851da177e4SLinus Torvalds 			"EEProm checksum error: using default values and options.\n");
41861da177e4SLinus Torvalds 		eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
41871da177e4SLinus Torvalds 		eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
41881da177e4SLinus Torvalds 		eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
41891da177e4SLinus Torvalds 		eeprom->sub_sys_id[1] =
41901da177e4SLinus Torvalds 		    (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
41911da177e4SLinus Torvalds 		eeprom->sub_class = 0x00;
41921da177e4SLinus Torvalds 		eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
41931da177e4SLinus Torvalds 		eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
41941da177e4SLinus Torvalds 		eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
41951da177e4SLinus Torvalds 		eeprom->device_id[1] =
41961da177e4SLinus Torvalds 		    (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
41971da177e4SLinus Torvalds 		eeprom->reserved = 0x00;
41981da177e4SLinus Torvalds 
41991da177e4SLinus Torvalds 		for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
42001da177e4SLinus Torvalds 		     d_addr < 16; d_addr++, d_eeprom++)
42011da177e4SLinus Torvalds 			*d_eeprom = 0x00000077;	/* cfg3,cfg2,period,cfg0 */
42021da177e4SLinus Torvalds 
42031da177e4SLinus Torvalds 		*d_eeprom++ = 0x04000F07;	/* max_tag,delay_time,channel_cfg,scsi_id */
42041da177e4SLinus Torvalds 		*d_eeprom++ = 0x00000015;	/* reserved1,boot_lun,boot_target,reserved0 */
42051da177e4SLinus Torvalds 		for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
42061da177e4SLinus Torvalds 			*d_eeprom = 0x00;
42071da177e4SLinus Torvalds 
42081da177e4SLinus Torvalds 		/* Now load defaults (maybe set by boot/module params) */
42091da177e4SLinus Torvalds 		set_safe_settings();
42101da177e4SLinus Torvalds 		fix_settings();
42111da177e4SLinus Torvalds 		eeprom_override(eeprom);
42121da177e4SLinus Torvalds 
42131da177e4SLinus Torvalds 		eeprom->cksum = 0x00;
42141da177e4SLinus Torvalds 		for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
42151da177e4SLinus Torvalds 		     w_addr < 63; w_addr++, w_eeprom++)
42161da177e4SLinus Torvalds 			cksum += *w_eeprom;
42171da177e4SLinus Torvalds 
42181da177e4SLinus Torvalds 		*w_eeprom = 0x1234 - cksum;
42191da177e4SLinus Torvalds 		trms1040_write_all(eeprom, io_port);
42201da177e4SLinus Torvalds 		eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
42211da177e4SLinus Torvalds 	} else {
42221da177e4SLinus Torvalds 		set_safe_settings();
42231da177e4SLinus Torvalds 		eeprom_index_to_delay(eeprom);
42241da177e4SLinus Torvalds 		eeprom_override(eeprom);
42251da177e4SLinus Torvalds 	}
42261da177e4SLinus Torvalds }
42271da177e4SLinus Torvalds 
42281da177e4SLinus Torvalds 
42291da177e4SLinus Torvalds /**
42301da177e4SLinus Torvalds  * print_eeprom_settings - output the eeprom settings
42311da177e4SLinus Torvalds  * to the kernel log so people can see what they were.
42321da177e4SLinus Torvalds  *
42331da177e4SLinus Torvalds  * @eeprom: The eeprom data strucutre to show details for.
42341da177e4SLinus Torvalds  **/
42356f039790SGreg Kroah-Hartman static void print_eeprom_settings(struct NvRamType *eeprom)
42361da177e4SLinus Torvalds {
42371da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
42381da177e4SLinus Torvalds 		eeprom->scsi_id,
42391da177e4SLinus Torvalds 		eeprom->target[0].period,
42401da177e4SLinus Torvalds 		clock_speed[eeprom->target[0].period] / 10,
42411da177e4SLinus Torvalds 		clock_speed[eeprom->target[0].period] % 10,
42421da177e4SLinus Torvalds 		eeprom->target[0].cfg0);
42431da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "               AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
42441da177e4SLinus Torvalds 		eeprom->channel_cfg, eeprom->max_tag,
42451da177e4SLinus Torvalds 		1 << eeprom->max_tag, eeprom->delay_time);
42461da177e4SLinus Torvalds }
42471da177e4SLinus Torvalds 
42481da177e4SLinus Torvalds 
42491da177e4SLinus Torvalds /* Free SG tables */
42501da177e4SLinus Torvalds static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
42511da177e4SLinus Torvalds {
42521da177e4SLinus Torvalds 	int i;
42531da177e4SLinus Torvalds 	const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
42541da177e4SLinus Torvalds 
42551da177e4SLinus Torvalds 	for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
42561da177e4SLinus Torvalds 		kfree(acb->srb_array[i].segment_x);
42571da177e4SLinus Torvalds }
42581da177e4SLinus Torvalds 
42591da177e4SLinus Torvalds 
42601da177e4SLinus Torvalds /*
42611da177e4SLinus Torvalds  * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
42621da177e4SLinus Torvalds  * should never cross a page boundary */
42636f039790SGreg Kroah-Hartman static int adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
42641da177e4SLinus Torvalds {
42651da177e4SLinus Torvalds 	const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
42661da177e4SLinus Torvalds 	                            *SEGMENTX_LEN;
42671da177e4SLinus Torvalds 	int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
42681da177e4SLinus Torvalds 	const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
42691da177e4SLinus Torvalds 	int srb_idx = 0;
42701da177e4SLinus Torvalds 	unsigned i = 0;
42718144f213SAndrew Morton 	struct SGentry *uninitialized_var(ptr);
42721da177e4SLinus Torvalds 
4273b4b08e58SLinus Torvalds 	for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
42741da177e4SLinus Torvalds 		acb->srb_array[i].segment_x = NULL;
42751da177e4SLinus Torvalds 
42761da177e4SLinus Torvalds 	dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
42771da177e4SLinus Torvalds 	while (pages--) {
42785cbded58SRobert P. J. Day 		ptr = kmalloc(PAGE_SIZE, GFP_KERNEL);
42791da177e4SLinus Torvalds 		if (!ptr) {
42801da177e4SLinus Torvalds 			adapter_sg_tables_free(acb);
42811da177e4SLinus Torvalds 			return 1;
42821da177e4SLinus Torvalds 		}
42831da177e4SLinus Torvalds 		dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
42841da177e4SLinus Torvalds 			PAGE_SIZE, ptr, srb_idx);
42851da177e4SLinus Torvalds 		i = 0;
42861da177e4SLinus Torvalds 		while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
42871da177e4SLinus Torvalds 			acb->srb_array[srb_idx++].segment_x =
42881da177e4SLinus Torvalds 			    ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
42891da177e4SLinus Torvalds 	}
42901da177e4SLinus Torvalds 	if (i < srbs_per_page)
42911da177e4SLinus Torvalds 		acb->srb.segment_x =
42921da177e4SLinus Torvalds 		    ptr + (i * DC395x_MAX_SG_LISTENTRY);
42931da177e4SLinus Torvalds 	else
42941da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
42951da177e4SLinus Torvalds 	return 0;
42961da177e4SLinus Torvalds }
42971da177e4SLinus Torvalds 
42981da177e4SLinus Torvalds 
42991da177e4SLinus Torvalds 
43001da177e4SLinus Torvalds /**
43011da177e4SLinus Torvalds  * adapter_print_config - print adapter connection and termination
43021da177e4SLinus Torvalds  * config
43031da177e4SLinus Torvalds  *
43041da177e4SLinus Torvalds  * The io port in the adapter needs to have been set before calling
43051da177e4SLinus Torvalds  * this function.
43061da177e4SLinus Torvalds  *
43071da177e4SLinus Torvalds  * @acb: The adapter to print the information for.
43081da177e4SLinus Torvalds  **/
43096f039790SGreg Kroah-Hartman static void adapter_print_config(struct AdapterCtlBlk *acb)
43101da177e4SLinus Torvalds {
43111da177e4SLinus Torvalds 	u8 bval;
43121da177e4SLinus Torvalds 
43131da177e4SLinus Torvalds 	bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
43141da177e4SLinus Torvalds 	dprintkl(KERN_INFO, "%sConnectors: ",
43151da177e4SLinus Torvalds 		((bval & WIDESCSI) ? "(Wide) " : ""));
43161da177e4SLinus Torvalds 	if (!(bval & CON5068))
43171da177e4SLinus Torvalds 		printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
43181da177e4SLinus Torvalds 	if (!(bval & CON68))
43191da177e4SLinus Torvalds 		printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
43201da177e4SLinus Torvalds 	if (!(bval & CON50))
43211da177e4SLinus Torvalds 		printk("int50 ");
43221da177e4SLinus Torvalds 	if ((bval & (CON5068 | CON50 | CON68)) ==
43231da177e4SLinus Torvalds 	    0 /*(CON5068 | CON50 | CON68) */ )
43241da177e4SLinus Torvalds 		printk(" Oops! (All 3?) ");
43251da177e4SLinus Torvalds 	bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
43261da177e4SLinus Torvalds 	printk(" Termination: ");
43271da177e4SLinus Torvalds 	if (bval & DIS_TERM)
43281da177e4SLinus Torvalds 		printk("Disabled\n");
43291da177e4SLinus Torvalds 	else {
43301da177e4SLinus Torvalds 		if (bval & AUTOTERM)
43311da177e4SLinus Torvalds 			printk("Auto ");
43321da177e4SLinus Torvalds 		if (bval & LOW8TERM)
43331da177e4SLinus Torvalds 			printk("Low ");
43341da177e4SLinus Torvalds 		if (bval & UP8TERM)
43351da177e4SLinus Torvalds 			printk("High ");
43361da177e4SLinus Torvalds 		printk("\n");
43371da177e4SLinus Torvalds 	}
43381da177e4SLinus Torvalds }
43391da177e4SLinus Torvalds 
43401da177e4SLinus Torvalds 
43411da177e4SLinus Torvalds /**
43421da177e4SLinus Torvalds  * adapter_init_params - Initialize the various parameters in the
43431da177e4SLinus Torvalds  * adapter structure. Note that the pointer to the scsi_host is set
43441da177e4SLinus Torvalds  * early (when this instance is created) and the io_port and irq
43451da177e4SLinus Torvalds  * values are set later after they have been reserved. This just gets
43461da177e4SLinus Torvalds  * everything set to a good starting position.
43471da177e4SLinus Torvalds  *
43481da177e4SLinus Torvalds  * The eeprom structure in the adapter needs to have been set before
43491da177e4SLinus Torvalds  * calling this function.
43501da177e4SLinus Torvalds  *
43511da177e4SLinus Torvalds  * @acb: The adapter to initialize.
43521da177e4SLinus Torvalds  **/
43536f039790SGreg Kroah-Hartman static void adapter_init_params(struct AdapterCtlBlk *acb)
43541da177e4SLinus Torvalds {
43551da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
43561da177e4SLinus Torvalds 	int i;
43571da177e4SLinus Torvalds 
43581da177e4SLinus Torvalds 	/* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
43591da177e4SLinus Torvalds 	/* NOTE: acb->io_port_base is set at port registration time */
43601da177e4SLinus Torvalds 	/* NOTE: acb->io_port_len is set at port registration time */
43611da177e4SLinus Torvalds 
43621da177e4SLinus Torvalds 	INIT_LIST_HEAD(&acb->dcb_list);
43631da177e4SLinus Torvalds 	acb->dcb_run_robin = NULL;
43641da177e4SLinus Torvalds 	acb->active_dcb = NULL;
43651da177e4SLinus Torvalds 
43661da177e4SLinus Torvalds 	INIT_LIST_HEAD(&acb->srb_free_list);
43671da177e4SLinus Torvalds 	/*  temp SRB for Q tag used or abort command used  */
43681da177e4SLinus Torvalds 	acb->tmp_srb = &acb->srb;
43691da177e4SLinus Torvalds 	init_timer(&acb->waiting_timer);
43701da177e4SLinus Torvalds 	init_timer(&acb->selto_timer);
43711da177e4SLinus Torvalds 
43721da177e4SLinus Torvalds 	acb->srb_count = DC395x_MAX_SRB_CNT;
43731da177e4SLinus Torvalds 
43741da177e4SLinus Torvalds 	acb->sel_timeout = DC395x_SEL_TIMEOUT;	/* timeout=250ms */
43751da177e4SLinus Torvalds 	/* NOTE: acb->irq_level is set at IRQ registration time */
43761da177e4SLinus Torvalds 
43771da177e4SLinus Torvalds 	acb->tag_max_num = 1 << eeprom->max_tag;
43781da177e4SLinus Torvalds 	if (acb->tag_max_num > 30)
43791da177e4SLinus Torvalds 		acb->tag_max_num = 30;
43801da177e4SLinus Torvalds 
43811da177e4SLinus Torvalds 	acb->acb_flag = 0;	/* RESET_DETECT, RESET_DONE, RESET_DEV */
43821da177e4SLinus Torvalds 	acb->gmode2 = eeprom->channel_cfg;
43831da177e4SLinus Torvalds 	acb->config = 0;	/* NOTE: actually set in adapter_init_chip */
43841da177e4SLinus Torvalds 
43851da177e4SLinus Torvalds 	if (eeprom->channel_cfg & NAC_SCANLUN)
43861da177e4SLinus Torvalds 		acb->lun_chk = 1;
43871da177e4SLinus Torvalds 	acb->scan_devices = 1;
43881da177e4SLinus Torvalds 
43891da177e4SLinus Torvalds 	acb->scsi_host->this_id = eeprom->scsi_id;
43901da177e4SLinus Torvalds 	acb->hostid_bit = (1 << acb->scsi_host->this_id);
43911da177e4SLinus Torvalds 
43921da177e4SLinus Torvalds 	for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
43931da177e4SLinus Torvalds 		acb->dcb_map[i] = 0;
43941da177e4SLinus Torvalds 
43951da177e4SLinus Torvalds 	acb->msg_len = 0;
43961da177e4SLinus Torvalds 
43971da177e4SLinus Torvalds 	/* link static array of srbs into the srb free list */
43981da177e4SLinus Torvalds 	for (i = 0; i < acb->srb_count - 1; i++)
43991da177e4SLinus Torvalds 		srb_free_insert(acb, &acb->srb_array[i]);
44001da177e4SLinus Torvalds }
44011da177e4SLinus Torvalds 
44021da177e4SLinus Torvalds 
44031da177e4SLinus Torvalds /**
44041da177e4SLinus Torvalds  * adapter_init_host - Initialize the scsi host instance based on
44051da177e4SLinus Torvalds  * values that we have already stored in the adapter instance. There's
44061da177e4SLinus Torvalds  * some mention that a lot of these are deprecated, so we won't use
44071da177e4SLinus Torvalds  * them (we'll use the ones in the adapter instance) but we'll fill
44081da177e4SLinus Torvalds  * them in in case something else needs them.
44091da177e4SLinus Torvalds  *
44101da177e4SLinus Torvalds  * The eeprom structure, irq and io ports in the adapter need to have
44111da177e4SLinus Torvalds  * been set before calling this function.
44121da177e4SLinus Torvalds  *
44131da177e4SLinus Torvalds  * @host: The scsi host instance to fill in the values for.
44141da177e4SLinus Torvalds  **/
44156f039790SGreg Kroah-Hartman static void adapter_init_scsi_host(struct Scsi_Host *host)
44161da177e4SLinus Torvalds {
44171da177e4SLinus Torvalds         struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
44181da177e4SLinus Torvalds 	struct NvRamType *eeprom = &acb->eeprom;
44191da177e4SLinus Torvalds 
44201da177e4SLinus Torvalds 	host->max_cmd_len = 24;
44211da177e4SLinus Torvalds 	host->can_queue = DC395x_MAX_CMD_QUEUE;
44221da177e4SLinus Torvalds 	host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
44231da177e4SLinus Torvalds 	host->this_id = (int)eeprom->scsi_id;
44241da177e4SLinus Torvalds 	host->io_port = acb->io_port_base;
44251da177e4SLinus Torvalds 	host->n_io_port = acb->io_port_len;
44261da177e4SLinus Torvalds 	host->dma_channel = -1;
44271da177e4SLinus Torvalds 	host->unique_id = acb->io_port_base;
44281da177e4SLinus Torvalds 	host->irq = acb->irq_level;
44291da177e4SLinus Torvalds 	host->last_reset = jiffies;
44301da177e4SLinus Torvalds 
44311da177e4SLinus Torvalds 	host->max_id = 16;
44321da177e4SLinus Torvalds 	if (host->max_id - 1 == eeprom->scsi_id)
44331da177e4SLinus Torvalds 		host->max_id--;
44341da177e4SLinus Torvalds 
44351da177e4SLinus Torvalds #ifdef CONFIG_SCSI_MULTI_LUN
44361da177e4SLinus Torvalds 	if (eeprom->channel_cfg & NAC_SCANLUN)
44371da177e4SLinus Torvalds 		host->max_lun = 8;
44381da177e4SLinus Torvalds 	else
44391da177e4SLinus Torvalds 		host->max_lun = 1;
44401da177e4SLinus Torvalds #else
44411da177e4SLinus Torvalds 	host->max_lun = 1;
44421da177e4SLinus Torvalds #endif
44431da177e4SLinus Torvalds 
44441da177e4SLinus Torvalds }
44451da177e4SLinus Torvalds 
44461da177e4SLinus Torvalds 
44471da177e4SLinus Torvalds /**
44481da177e4SLinus Torvalds  * adapter_init_chip - Get the chip into a know state and figure out
44491da177e4SLinus Torvalds  * some of the settings that apply to this adapter.
44501da177e4SLinus Torvalds  *
44511da177e4SLinus Torvalds  * The io port in the adapter needs to have been set before calling
44521da177e4SLinus Torvalds  * this function. The config will be configured correctly on return.
44531da177e4SLinus Torvalds  *
44541da177e4SLinus Torvalds  * @acb: The adapter which we are to init.
44551da177e4SLinus Torvalds  **/
44566f039790SGreg Kroah-Hartman static void adapter_init_chip(struct AdapterCtlBlk *acb)
44571da177e4SLinus Torvalds {
44581da177e4SLinus Torvalds         struct NvRamType *eeprom = &acb->eeprom;
44591da177e4SLinus Torvalds 
44601da177e4SLinus Torvalds         /* Mask all the interrupt */
44611da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
44621da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
44631da177e4SLinus Torvalds 
44641da177e4SLinus Torvalds 	/* Reset SCSI module */
44651da177e4SLinus Torvalds 	DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
44661da177e4SLinus Torvalds 
44671da177e4SLinus Torvalds 	/* Reset PCI/DMA module */
44681da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
44691da177e4SLinus Torvalds 	udelay(20);
44701da177e4SLinus Torvalds 
44711da177e4SLinus Torvalds 	/* program configuration 0 */
44721da177e4SLinus Torvalds 	acb->config = HCC_AUTOTERM | HCC_PARITY;
44731da177e4SLinus Torvalds 	if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
44741da177e4SLinus Torvalds 		acb->config |= HCC_WIDE_CARD;
44751da177e4SLinus Torvalds 
44761da177e4SLinus Torvalds 	if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
44771da177e4SLinus Torvalds 		acb->config |= HCC_SCSI_RESET;
44781da177e4SLinus Torvalds 
44791da177e4SLinus Torvalds 	if (acb->config & HCC_SCSI_RESET) {
44801da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
44811da177e4SLinus Torvalds 		DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
44821da177e4SLinus Torvalds 
44831da177e4SLinus Torvalds 		/*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
44841da177e4SLinus Torvalds 		/*spin_unlock_irq (&io_request_lock); */
44851da177e4SLinus Torvalds 		udelay(500);
44861da177e4SLinus Torvalds 
44871da177e4SLinus Torvalds 		acb->scsi_host->last_reset =
44881da177e4SLinus Torvalds 		    jiffies + HZ / 2 +
44891da177e4SLinus Torvalds 		    HZ * acb->eeprom.delay_time;
44901da177e4SLinus Torvalds 
44911da177e4SLinus Torvalds 		/*spin_lock_irq (&io_request_lock); */
44921da177e4SLinus Torvalds 	}
44931da177e4SLinus Torvalds }
44941da177e4SLinus Torvalds 
44951da177e4SLinus Torvalds 
44961da177e4SLinus Torvalds /**
44971da177e4SLinus Torvalds  * init_adapter - Grab the resource for the card, setup the adapter
44981da177e4SLinus Torvalds  * information, set the card into a known state, create the various
44991da177e4SLinus Torvalds  * tables etc etc. This basically gets all adapter information all up
4500b595076aSUwe Kleine-König  * to date, initialised and gets the chip in sync with it.
45011da177e4SLinus Torvalds  *
45021da177e4SLinus Torvalds  * @host:	This hosts adapter structure
45031da177e4SLinus Torvalds  * @io_port:	The base I/O port
45041da177e4SLinus Torvalds  * @irq:	IRQ
45051da177e4SLinus Torvalds  *
45061da177e4SLinus Torvalds  * Returns 0 if the initialization succeeds, any other value on
45071da177e4SLinus Torvalds  * failure.
45081da177e4SLinus Torvalds  **/
45096f039790SGreg Kroah-Hartman static int adapter_init(struct AdapterCtlBlk *acb, unsigned long io_port,
45106f039790SGreg Kroah-Hartman 			u32 io_port_len, unsigned int irq)
45111da177e4SLinus Torvalds {
45121da177e4SLinus Torvalds 	if (!request_region(io_port, io_port_len, DC395X_NAME)) {
45131da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
45141da177e4SLinus Torvalds 		goto failed;
45151da177e4SLinus Torvalds 	}
45161da177e4SLinus Torvalds 	/* store port base to indicate we have registered it */
45171da177e4SLinus Torvalds 	acb->io_port_base = io_port;
45181da177e4SLinus Torvalds 	acb->io_port_len = io_port_len;
45191da177e4SLinus Torvalds 
45201d6f359aSThomas Gleixner 	if (request_irq(irq, dc395x_interrupt, IRQF_SHARED, DC395X_NAME, acb)) {
45211da177e4SLinus Torvalds 	    	/* release the region we just claimed */
45221da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "Failed to register IRQ\n");
45231da177e4SLinus Torvalds 		goto failed;
45241da177e4SLinus Torvalds 	}
45251da177e4SLinus Torvalds 	/* store irq to indicate we have registered it */
45261da177e4SLinus Torvalds 	acb->irq_level = irq;
45271da177e4SLinus Torvalds 
45281da177e4SLinus Torvalds 	/* get eeprom configuration information and command line settings etc */
45291da177e4SLinus Torvalds 	check_eeprom(&acb->eeprom, io_port);
45301da177e4SLinus Torvalds  	print_eeprom_settings(&acb->eeprom);
45311da177e4SLinus Torvalds 
45321da177e4SLinus Torvalds 	/* setup adapter control block */
45331da177e4SLinus Torvalds 	adapter_init_params(acb);
45341da177e4SLinus Torvalds 
45351da177e4SLinus Torvalds 	/* display card connectors/termination settings */
45361da177e4SLinus Torvalds  	adapter_print_config(acb);
45371da177e4SLinus Torvalds 
45381da177e4SLinus Torvalds 	if (adapter_sg_tables_alloc(acb)) {
45391da177e4SLinus Torvalds 		dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
45401da177e4SLinus Torvalds 		goto failed;
45411da177e4SLinus Torvalds 	}
45421da177e4SLinus Torvalds 	adapter_init_scsi_host(acb->scsi_host);
45431da177e4SLinus Torvalds 	adapter_init_chip(acb);
45441da177e4SLinus Torvalds 	set_basic_config(acb);
45451da177e4SLinus Torvalds 
45461da177e4SLinus Torvalds 	dprintkdbg(DBG_0,
45471da177e4SLinus Torvalds 		"adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
45481da177e4SLinus Torvalds 		"size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
45491da177e4SLinus Torvalds 		acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
45501da177e4SLinus Torvalds 		sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
45511da177e4SLinus Torvalds 	return 0;
45521da177e4SLinus Torvalds 
45531da177e4SLinus Torvalds failed:
45541da177e4SLinus Torvalds 	if (acb->irq_level)
45551da177e4SLinus Torvalds 		free_irq(acb->irq_level, acb);
45561da177e4SLinus Torvalds 	if (acb->io_port_base)
45571da177e4SLinus Torvalds 		release_region(acb->io_port_base, acb->io_port_len);
45581da177e4SLinus Torvalds 	adapter_sg_tables_free(acb);
45591da177e4SLinus Torvalds 
45601da177e4SLinus Torvalds 	return 1;
45611da177e4SLinus Torvalds }
45621da177e4SLinus Torvalds 
45631da177e4SLinus Torvalds 
45641da177e4SLinus Torvalds /**
45651da177e4SLinus Torvalds  * adapter_uninit_chip - cleanly shut down the scsi controller chip,
45661da177e4SLinus Torvalds  * stopping all operations and disabling interrupt generation on the
45671da177e4SLinus Torvalds  * card.
45681da177e4SLinus Torvalds  *
45691da177e4SLinus Torvalds  * @acb: The adapter which we are to shutdown.
45701da177e4SLinus Torvalds  **/
45711da177e4SLinus Torvalds static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
45721da177e4SLinus Torvalds {
45731da177e4SLinus Torvalds 	/* disable interrupts */
45741da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
45751da177e4SLinus Torvalds 	DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
45761da177e4SLinus Torvalds 
45771da177e4SLinus Torvalds 	/* reset the scsi bus */
45781da177e4SLinus Torvalds 	if (acb->config & HCC_SCSI_RESET)
45791da177e4SLinus Torvalds 		reset_scsi_bus(acb);
45801da177e4SLinus Torvalds 
45813a4fa0a2SRobert P. J. Day 	/* clear any pending interrupt state */
45821da177e4SLinus Torvalds 	DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
45831da177e4SLinus Torvalds }
45841da177e4SLinus Torvalds 
45851da177e4SLinus Torvalds 
45861da177e4SLinus Torvalds 
45871da177e4SLinus Torvalds /**
45881da177e4SLinus Torvalds  * adapter_uninit - Shut down the chip and release any resources that
45891da177e4SLinus Torvalds  * we had allocated. Once this returns the adapter should not be used
45901da177e4SLinus Torvalds  * anymore.
45911da177e4SLinus Torvalds  *
45921da177e4SLinus Torvalds  * @acb: The adapter which we are to un-initialize.
45931da177e4SLinus Torvalds  **/
45941da177e4SLinus Torvalds static void adapter_uninit(struct AdapterCtlBlk *acb)
45951da177e4SLinus Torvalds {
45961da177e4SLinus Torvalds 	unsigned long flags;
45971da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
45981da177e4SLinus Torvalds 
45991da177e4SLinus Torvalds 	/* remove timers */
46001da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
46011da177e4SLinus Torvalds 		del_timer(&acb->waiting_timer);
46021da177e4SLinus Torvalds 	if (timer_pending(&acb->selto_timer))
46031da177e4SLinus Torvalds 		del_timer(&acb->selto_timer);
46041da177e4SLinus Torvalds 
46051da177e4SLinus Torvalds 	adapter_uninit_chip(acb);
46061da177e4SLinus Torvalds 	adapter_remove_and_free_all_devices(acb);
46071da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
46081da177e4SLinus Torvalds 
46091da177e4SLinus Torvalds 	if (acb->irq_level)
46101da177e4SLinus Torvalds 		free_irq(acb->irq_level, acb);
46111da177e4SLinus Torvalds 	if (acb->io_port_base)
46121da177e4SLinus Torvalds 		release_region(acb->io_port_base, acb->io_port_len);
46131da177e4SLinus Torvalds 
46141da177e4SLinus Torvalds 	adapter_sg_tables_free(acb);
46151da177e4SLinus Torvalds }
46161da177e4SLinus Torvalds 
46171da177e4SLinus Torvalds 
46181da177e4SLinus Torvalds #undef SPRINTF
46191da177e4SLinus Torvalds #define SPRINTF(args...) pos += sprintf(pos, args)
46201da177e4SLinus Torvalds 
46211da177e4SLinus Torvalds #undef YESNO
46221da177e4SLinus Torvalds #define YESNO(YN) \
46231da177e4SLinus Torvalds  if (YN) SPRINTF(" Yes ");\
46241da177e4SLinus Torvalds  else SPRINTF(" No  ")
46251da177e4SLinus Torvalds 
46261da177e4SLinus Torvalds static int dc395x_proc_info(struct Scsi_Host *host, char *buffer,
46271da177e4SLinus Torvalds 		char **start, off_t offset, int length, int inout)
46281da177e4SLinus Torvalds {
46291da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
46301da177e4SLinus Torvalds 	int spd, spd1;
46311da177e4SLinus Torvalds 	char *pos = buffer;
46321da177e4SLinus Torvalds 	struct DeviceCtlBlk *dcb;
46331da177e4SLinus Torvalds 	unsigned long flags;
46341da177e4SLinus Torvalds 	int dev;
46351da177e4SLinus Torvalds 
46361da177e4SLinus Torvalds 	if (inout)		/* Has data been written to the file ? */
46371da177e4SLinus Torvalds 		return -EPERM;
46381da177e4SLinus Torvalds 
46391da177e4SLinus Torvalds 	SPRINTF(DC395X_BANNER " PCI SCSI Host Adapter\n");
46401da177e4SLinus Torvalds 	SPRINTF(" Driver Version " DC395X_VERSION "\n");
46411da177e4SLinus Torvalds 
46421da177e4SLinus Torvalds 	DC395x_LOCK_IO(acb->scsi_host, flags);
46431da177e4SLinus Torvalds 
46441da177e4SLinus Torvalds 	SPRINTF("SCSI Host Nr %i, ", host->host_no);
46451da177e4SLinus Torvalds 	SPRINTF("DC395U/UW/F DC315/U %s\n",
46461da177e4SLinus Torvalds 		(acb->config & HCC_WIDE_CARD) ? "Wide" : "");
46471da177e4SLinus Torvalds 	SPRINTF("io_port_base 0x%04lx, ", acb->io_port_base);
46481da177e4SLinus Torvalds 	SPRINTF("irq_level 0x%04x, ", acb->irq_level);
46491da177e4SLinus Torvalds 	SPRINTF(" SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
46501da177e4SLinus Torvalds 
46511da177e4SLinus Torvalds 	SPRINTF("MaxID %i, MaxLUN %i, ", host->max_id, host->max_lun);
46521da177e4SLinus Torvalds 	SPRINTF("AdapterID %i\n", host->this_id);
46531da177e4SLinus Torvalds 
46541da177e4SLinus Torvalds 	SPRINTF("tag_max_num %i", acb->tag_max_num);
46551da177e4SLinus Torvalds 	/*SPRINTF(", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
46561da177e4SLinus Torvalds 	SPRINTF(", FilterCfg 0x%02x",
46571da177e4SLinus Torvalds 		DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
46581da177e4SLinus Torvalds 	SPRINTF(", DelayReset %is\n", acb->eeprom.delay_time);
46591da177e4SLinus Torvalds 	/*SPRINTF("\n"); */
46601da177e4SLinus Torvalds 
46611da177e4SLinus Torvalds 	SPRINTF("Nr of DCBs: %i\n", list_size(&acb->dcb_list));
46621da177e4SLinus Torvalds 	SPRINTF
46631da177e4SLinus Torvalds 	    ("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
46641da177e4SLinus Torvalds 	     acb->dcb_map[0], acb->dcb_map[1], acb->dcb_map[2],
46651da177e4SLinus Torvalds 	     acb->dcb_map[3], acb->dcb_map[4], acb->dcb_map[5],
46661da177e4SLinus Torvalds 	     acb->dcb_map[6], acb->dcb_map[7]);
46671da177e4SLinus Torvalds 	SPRINTF
46681da177e4SLinus Torvalds 	    ("                      %02x %02x %02x %02x %02x %02x %02x %02x\n",
46691da177e4SLinus Torvalds 	     acb->dcb_map[8], acb->dcb_map[9], acb->dcb_map[10],
46701da177e4SLinus Torvalds 	     acb->dcb_map[11], acb->dcb_map[12], acb->dcb_map[13],
46711da177e4SLinus Torvalds 	     acb->dcb_map[14], acb->dcb_map[15]);
46721da177e4SLinus Torvalds 
46731da177e4SLinus Torvalds 	SPRINTF
46741da177e4SLinus Torvalds 	    ("Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
46751da177e4SLinus Torvalds 
46761da177e4SLinus Torvalds 	dev = 0;
46771da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
46781da177e4SLinus Torvalds 		int nego_period;
46791da177e4SLinus Torvalds 		SPRINTF("%02i %02i  %02i ", dev, dcb->target_id,
46801da177e4SLinus Torvalds 			dcb->target_lun);
46811da177e4SLinus Torvalds 		YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
46821da177e4SLinus Torvalds 		YESNO(dcb->sync_offset);
46831da177e4SLinus Torvalds 		YESNO(dcb->sync_period & WIDE_SYNC);
46841da177e4SLinus Torvalds 		YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
46851da177e4SLinus Torvalds 		YESNO(dcb->dev_mode & NTC_DO_SEND_START);
46861da177e4SLinus Torvalds 		YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
46871da177e4SLinus Torvalds 		nego_period = clock_period[dcb->sync_period & 0x07] << 2;
46881da177e4SLinus Torvalds 		if (dcb->sync_offset)
46891da177e4SLinus Torvalds 			SPRINTF("  %03i ns ", nego_period);
46901da177e4SLinus Torvalds 		else
46911da177e4SLinus Torvalds 			SPRINTF(" (%03i ns)", (dcb->min_nego_period << 2));
46921da177e4SLinus Torvalds 
46931da177e4SLinus Torvalds 		if (dcb->sync_offset & 0x0f) {
46941da177e4SLinus Torvalds 			spd = 1000 / (nego_period);
46951da177e4SLinus Torvalds 			spd1 = 1000 % (nego_period);
46961da177e4SLinus Torvalds 			spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
46971da177e4SLinus Torvalds 			SPRINTF("   %2i.%1i M     %02i ", spd, spd1,
46981da177e4SLinus Torvalds 				(dcb->sync_offset & 0x0f));
46991da177e4SLinus Torvalds 		} else
47001da177e4SLinus Torvalds 			SPRINTF("                 ");
47011da177e4SLinus Torvalds 
47021da177e4SLinus Torvalds 		/* Add more info ... */
47031da177e4SLinus Torvalds 		SPRINTF("     %02i\n", dcb->max_command);
47041da177e4SLinus Torvalds 		dev++;
47051da177e4SLinus Torvalds 	}
47061da177e4SLinus Torvalds 
47071da177e4SLinus Torvalds 	if (timer_pending(&acb->waiting_timer))
47081da177e4SLinus Torvalds 		SPRINTF("Waiting queue timer running\n");
47091da177e4SLinus Torvalds 	else
47101da177e4SLinus Torvalds 		SPRINTF("\n");
47111da177e4SLinus Torvalds 
47121da177e4SLinus Torvalds 	list_for_each_entry(dcb, &acb->dcb_list, list) {
47131da177e4SLinus Torvalds 		struct ScsiReqBlk *srb;
47141da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_waiting_list))
47151da177e4SLinus Torvalds 			SPRINTF("DCB (%02i-%i): Waiting: %i:",
47161da177e4SLinus Torvalds 				dcb->target_id, dcb->target_lun,
47171da177e4SLinus Torvalds 				list_size(&dcb->srb_waiting_list));
47181da177e4SLinus Torvalds                 list_for_each_entry(srb, &dcb->srb_waiting_list, list)
47195cd049a5SChristoph Hellwig 			SPRINTF(" %p", srb->cmd);
47201da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_going_list))
47211da177e4SLinus Torvalds 			SPRINTF("\nDCB (%02i-%i): Going  : %i:",
47221da177e4SLinus Torvalds 				dcb->target_id, dcb->target_lun,
47231da177e4SLinus Torvalds 				list_size(&dcb->srb_going_list));
47241da177e4SLinus Torvalds 		list_for_each_entry(srb, &dcb->srb_going_list, list)
47255cd049a5SChristoph Hellwig 			SPRINTF(" %p", srb->cmd);
47261da177e4SLinus Torvalds 		if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
47271da177e4SLinus Torvalds 			SPRINTF("\n");
47281da177e4SLinus Torvalds 	}
47291da177e4SLinus Torvalds 
47301da177e4SLinus Torvalds 	if (debug_enabled(DBG_1)) {
47311da177e4SLinus Torvalds 		SPRINTF("DCB list for ACB %p:\n", acb);
47321da177e4SLinus Torvalds 		list_for_each_entry(dcb, &acb->dcb_list, list) {
47331da177e4SLinus Torvalds 			SPRINTF("%p -> ", dcb);
47341da177e4SLinus Torvalds 		}
47351da177e4SLinus Torvalds 		SPRINTF("END\n");
47361da177e4SLinus Torvalds 	}
47371da177e4SLinus Torvalds 
47381da177e4SLinus Torvalds 	*start = buffer + offset;
47391da177e4SLinus Torvalds 	DC395x_UNLOCK_IO(acb->scsi_host, flags);
47401da177e4SLinus Torvalds 
47411da177e4SLinus Torvalds 	if (pos - buffer < offset)
47421da177e4SLinus Torvalds 		return 0;
47431da177e4SLinus Torvalds 	else if (pos - buffer - offset < length)
47441da177e4SLinus Torvalds 		return pos - buffer - offset;
47451da177e4SLinus Torvalds 	else
47461da177e4SLinus Torvalds 		return length;
47471da177e4SLinus Torvalds }
47481da177e4SLinus Torvalds 
47491da177e4SLinus Torvalds 
47501da177e4SLinus Torvalds static struct scsi_host_template dc395x_driver_template = {
47511da177e4SLinus Torvalds 	.module                 = THIS_MODULE,
47521da177e4SLinus Torvalds 	.proc_name              = DC395X_NAME,
47531da177e4SLinus Torvalds 	.proc_info              = dc395x_proc_info,
47541da177e4SLinus Torvalds 	.name                   = DC395X_BANNER " " DC395X_VERSION,
47551da177e4SLinus Torvalds 	.queuecommand           = dc395x_queue_command,
47561da177e4SLinus Torvalds 	.bios_param             = dc395x_bios_param,
47571da177e4SLinus Torvalds 	.slave_alloc            = dc395x_slave_alloc,
47581da177e4SLinus Torvalds 	.slave_destroy          = dc395x_slave_destroy,
47591da177e4SLinus Torvalds 	.can_queue              = DC395x_MAX_CAN_QUEUE,
47601da177e4SLinus Torvalds 	.this_id                = 7,
47611da177e4SLinus Torvalds 	.sg_tablesize           = DC395x_MAX_SG_TABLESIZE,
47621da177e4SLinus Torvalds 	.cmd_per_lun            = DC395x_MAX_CMD_PER_LUN,
47631da177e4SLinus Torvalds 	.eh_abort_handler       = dc395x_eh_abort,
47641da177e4SLinus Torvalds 	.eh_bus_reset_handler   = dc395x_eh_bus_reset,
47651da177e4SLinus Torvalds 	.use_clustering         = DISABLE_CLUSTERING,
47661da177e4SLinus Torvalds };
47671da177e4SLinus Torvalds 
47681da177e4SLinus Torvalds 
47691da177e4SLinus Torvalds /**
47701da177e4SLinus Torvalds  * banner_display - Display banner on first instance of driver
47711da177e4SLinus Torvalds  * initialized.
47721da177e4SLinus Torvalds  **/
47731da177e4SLinus Torvalds static void banner_display(void)
47741da177e4SLinus Torvalds {
47751da177e4SLinus Torvalds 	static int banner_done = 0;
47761da177e4SLinus Torvalds 	if (!banner_done)
47771da177e4SLinus Torvalds 	{
47781da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
47791da177e4SLinus Torvalds 		banner_done = 1;
47801da177e4SLinus Torvalds 	}
47811da177e4SLinus Torvalds }
47821da177e4SLinus Torvalds 
47831da177e4SLinus Torvalds 
47841da177e4SLinus Torvalds /**
47851da177e4SLinus Torvalds  * dc395x_init_one - Initialise a single instance of the adapter.
47861da177e4SLinus Torvalds  *
47871da177e4SLinus Torvalds  * The PCI layer will call this once for each instance of the adapter
47881da177e4SLinus Torvalds  * that it finds in the system. The pci_dev strcuture indicates which
47891da177e4SLinus Torvalds  * instance we are being called from.
47901da177e4SLinus Torvalds  *
4791b595076aSUwe Kleine-König  * @dev: The PCI device to initialize.
47921da177e4SLinus Torvalds  * @id: Looks like a pointer to the entry in our pci device table
47931da177e4SLinus Torvalds  * that was actually matched by the PCI subsystem.
47941da177e4SLinus Torvalds  *
47951da177e4SLinus Torvalds  * Returns 0 on success, or an error code (-ve) on failure.
47961da177e4SLinus Torvalds  **/
47976f039790SGreg Kroah-Hartman static int dc395x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
47981da177e4SLinus Torvalds {
47991da177e4SLinus Torvalds 	struct Scsi_Host *scsi_host = NULL;
48001da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = NULL;
48011da177e4SLinus Torvalds 	unsigned long io_port_base;
48021da177e4SLinus Torvalds 	unsigned int io_port_len;
48031da177e4SLinus Torvalds 	unsigned int irq;
48041da177e4SLinus Torvalds 
48051da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
48061da177e4SLinus Torvalds 	banner_display();
48071da177e4SLinus Torvalds 
48081da177e4SLinus Torvalds 	if (pci_enable_device(dev))
48091da177e4SLinus Torvalds 	{
48101da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "PCI Enable device failed.\n");
48111da177e4SLinus Torvalds 		return -ENODEV;
48121da177e4SLinus Torvalds 	}
48131da177e4SLinus Torvalds 	io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
48141da177e4SLinus Torvalds 	io_port_len = pci_resource_len(dev, 0);
48151da177e4SLinus Torvalds 	irq = dev->irq;
48161da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
48171da177e4SLinus Torvalds 
48181da177e4SLinus Torvalds 	/* allocate scsi host information (includes out adapter) */
48191da177e4SLinus Torvalds 	scsi_host = scsi_host_alloc(&dc395x_driver_template,
48201da177e4SLinus Torvalds 				    sizeof(struct AdapterCtlBlk));
48211da177e4SLinus Torvalds 	if (!scsi_host) {
48221da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
48231da177e4SLinus Torvalds 		goto fail;
48241da177e4SLinus Torvalds 	}
48251da177e4SLinus Torvalds  	acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
48261da177e4SLinus Torvalds  	acb->scsi_host = scsi_host;
48271da177e4SLinus Torvalds  	acb->dev = dev;
48281da177e4SLinus Torvalds 
48291da177e4SLinus Torvalds 	/* initialise the adapter and everything we need */
48301da177e4SLinus Torvalds  	if (adapter_init(acb, io_port_base, io_port_len, irq)) {
48311da177e4SLinus Torvalds 		dprintkl(KERN_INFO, "adapter init failed\n");
48321da177e4SLinus Torvalds 		goto fail;
48331da177e4SLinus Torvalds 	}
48341da177e4SLinus Torvalds 
48351da177e4SLinus Torvalds 	pci_set_master(dev);
48361da177e4SLinus Torvalds 
48371da177e4SLinus Torvalds 	/* get the scsi mid level to scan for new devices on the bus */
48381da177e4SLinus Torvalds 	if (scsi_add_host(scsi_host, &dev->dev)) {
48391da177e4SLinus Torvalds 		dprintkl(KERN_ERR, "scsi_add_host failed\n");
48401da177e4SLinus Torvalds 		goto fail;
48411da177e4SLinus Torvalds 	}
48421da177e4SLinus Torvalds 	pci_set_drvdata(dev, scsi_host);
48431da177e4SLinus Torvalds 	scsi_scan_host(scsi_host);
48441da177e4SLinus Torvalds 
48451da177e4SLinus Torvalds 	return 0;
48461da177e4SLinus Torvalds 
48471da177e4SLinus Torvalds fail:
48481da177e4SLinus Torvalds 	if (acb != NULL)
48491da177e4SLinus Torvalds 		adapter_uninit(acb);
48501da177e4SLinus Torvalds 	if (scsi_host != NULL)
48511da177e4SLinus Torvalds 		scsi_host_put(scsi_host);
48521da177e4SLinus Torvalds 	pci_disable_device(dev);
48531da177e4SLinus Torvalds 	return -ENODEV;
48541da177e4SLinus Torvalds }
48551da177e4SLinus Torvalds 
48561da177e4SLinus Torvalds 
48571da177e4SLinus Torvalds /**
48581da177e4SLinus Torvalds  * dc395x_remove_one - Called to remove a single instance of the
48591da177e4SLinus Torvalds  * adapter.
48601da177e4SLinus Torvalds  *
4861b595076aSUwe Kleine-König  * @dev: The PCI device to initialize.
48621da177e4SLinus Torvalds  **/
48636f039790SGreg Kroah-Hartman static void dc395x_remove_one(struct pci_dev *dev)
48641da177e4SLinus Torvalds {
48651da177e4SLinus Torvalds 	struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
48661da177e4SLinus Torvalds 	struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
48671da177e4SLinus Torvalds 
48681da177e4SLinus Torvalds 	dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
48691da177e4SLinus Torvalds 
48701da177e4SLinus Torvalds 	scsi_remove_host(scsi_host);
48711da177e4SLinus Torvalds 	adapter_uninit(acb);
48721da177e4SLinus Torvalds 	pci_disable_device(dev);
48731da177e4SLinus Torvalds 	scsi_host_put(scsi_host);
48741da177e4SLinus Torvalds 	pci_set_drvdata(dev, NULL);
48751da177e4SLinus Torvalds }
48761da177e4SLinus Torvalds 
48771da177e4SLinus Torvalds 
48781da177e4SLinus Torvalds static struct pci_device_id dc395x_pci_table[] = {
48791da177e4SLinus Torvalds 	{
48801da177e4SLinus Torvalds 		.vendor		= PCI_VENDOR_ID_TEKRAM,
48811da177e4SLinus Torvalds 		.device		= PCI_DEVICE_ID_TEKRAM_TRMS1040,
48821da177e4SLinus Torvalds 		.subvendor	= PCI_ANY_ID,
48831da177e4SLinus Torvalds 		.subdevice	= PCI_ANY_ID,
48841da177e4SLinus Torvalds 	 },
48851da177e4SLinus Torvalds 	{}			/* Terminating entry */
48861da177e4SLinus Torvalds };
48871da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
48881da177e4SLinus Torvalds 
48891da177e4SLinus Torvalds 
48901da177e4SLinus Torvalds static struct pci_driver dc395x_driver = {
48911da177e4SLinus Torvalds 	.name           = DC395X_NAME,
48921da177e4SLinus Torvalds 	.id_table       = dc395x_pci_table,
48931da177e4SLinus Torvalds 	.probe          = dc395x_init_one,
48946f039790SGreg Kroah-Hartman 	.remove         = dc395x_remove_one,
48951da177e4SLinus Torvalds };
48961da177e4SLinus Torvalds 
48971da177e4SLinus Torvalds 
48981da177e4SLinus Torvalds /**
48991da177e4SLinus Torvalds  * dc395x_module_init - Module initialization function
49001da177e4SLinus Torvalds  *
49011da177e4SLinus Torvalds  * Used by both module and built-in driver to initialise this driver.
49021da177e4SLinus Torvalds  **/
49031da177e4SLinus Torvalds static int __init dc395x_module_init(void)
49041da177e4SLinus Torvalds {
4905dcbccbdeSHenrik Kretzschmar 	return pci_register_driver(&dc395x_driver);
49061da177e4SLinus Torvalds }
49071da177e4SLinus Torvalds 
49081da177e4SLinus Torvalds 
49091da177e4SLinus Torvalds /**
49101da177e4SLinus Torvalds  * dc395x_module_exit - Module cleanup function.
49111da177e4SLinus Torvalds  **/
49121da177e4SLinus Torvalds static void __exit dc395x_module_exit(void)
49131da177e4SLinus Torvalds {
49141da177e4SLinus Torvalds 	pci_unregister_driver(&dc395x_driver);
49151da177e4SLinus Torvalds }
49161da177e4SLinus Torvalds 
49171da177e4SLinus Torvalds 
49181da177e4SLinus Torvalds module_init(dc395x_module_init);
49191da177e4SLinus Torvalds module_exit(dc395x_module_exit);
49201da177e4SLinus Torvalds 
49211da177e4SLinus Torvalds MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
49221da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
49231da177e4SLinus Torvalds MODULE_LICENSE("GPL");
4924