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