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