diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 15:20:36 -0700 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 15:20:36 -0700 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/scsi/tmscsim.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/scsi/tmscsim.c')
-rw-r--r-- | drivers/scsi/tmscsim.c | 2693 |
1 files changed, 2693 insertions, 0 deletions
diff --git a/drivers/scsi/tmscsim.c b/drivers/scsi/tmscsim.c new file mode 100644 index 00000000000..ee9df02efd5 --- /dev/null +++ b/drivers/scsi/tmscsim.c @@ -0,0 +1,2693 @@ +/************************************************************************ + * FILE NAME : TMSCSIM.C * + * BY : C.L. Huang, ching@tekram.com.tw * + * Description: Device Driver for Tekram DC-390(T) PCI SCSI * + * Bus Master Host Adapter * + * (C)Copyright 1995-1996 Tekram Technology Co., Ltd. * + ************************************************************************ + * (C) Copyright: put under GNU GPL in 10/96 * + * (see Documentation/scsi/tmscsim.txt) * + ************************************************************************ + * $Id: tmscsim.c,v 2.60.2.30 2000/12/20 01:07:12 garloff Exp $ * + * Enhancements and bugfixes by * + * Kurt Garloff <kurt@garloff.de> <garloff@suse.de> * + ************************************************************************ + * HISTORY: * + * * + * REV# DATE NAME DESCRIPTION * + * 1.00 96/04/24 CLH First release * + * 1.01 96/06/12 CLH Fixed bug of Media Change for Removable * + * Device, scan all LUN. Support Pre2.0.10 * + * 1.02 96/06/18 CLH Fixed bug of Command timeout ... * + * 1.03 96/09/25 KG Added tmscsim_proc_info() * + * 1.04 96/10/11 CLH Updating for support KV 2.0.x * + * 1.05 96/10/18 KG Fixed bug in DC390_abort(null ptr deref)* + * 1.06 96/10/25 KG Fixed module support * + * 1.07 96/11/09 KG Fixed tmscsim_proc_info() * + * 1.08 96/11/18 KG Fixed null ptr in DC390_Disconnect() * + * 1.09 96/11/30 KG Added register the allocated IO space * + * 1.10 96/12/05 CLH Modified tmscsim_proc_info(), and reset * + * pending interrupt in DC390_detect() * + * 1.11 97/02/05 KG/CLH Fixeds problem with partitions greater * + * than 1GB * + * 1.12 98/02/15 MJ Rewritten PCI probing * + * 1.13 98/04/08 KG Support for non DC390, __initfunc decls,* + * changed max devs from 10 to 16 * + * 1.14a 98/05/05 KG Dynamic DCB allocation, add-single-dev * + * for LUNs if LUN_SCAN (BIOS) not set * + * runtime config using /proc interface * + * 1.14b 98/05/06 KG eliminated cli (); sti (); spinlocks * + * 1.14c 98/05/07 KG 2.0.x compatibility * + * 1.20a 98/05/07 KG changed names of funcs to be consistent * + * DC390_ (entry points), dc390_ (internal)* + * reworked locking * + * 1.20b 98/05/12 KG bugs: version, kfree, _ctmp * + * debug output * + * 1.20c 98/05/12 KG bugs: kfree, parsing, EEpromDefaults * + * 1.20d 98/05/14 KG bugs: list linkage, clear flag after * + * reset on startup, code cleanup * + * 1.20e 98/05/15 KG spinlock comments, name space cleanup * + * pLastDCB now part of ACB structure * + * added stats, timeout for 2.1, TagQ bug * + * RESET and INQUIRY interface commands * + * 1.20f 98/05/18 KG spinlocks fixes, max_lun fix, free DCBs * + * for missing LUNs, pending int * + * 1.20g 98/05/19 KG Clean up: Avoid short * + * 1.20h 98/05/21 KG Remove AdaptSCSIID, max_lun ... * + * 1.20i 98/05/21 KG Aiiie: Bug with TagQMask * + * 1.20j 98/05/24 KG Handle STAT_BUSY, handle pACB->pLinkDCB * + * == 0 in remove_dev and DoingSRB_Done * + * 1.20k 98/05/25 KG DMA_INT (experimental) * + * 1.20l 98/05/27 KG remove DMA_INT; DMA_IDLE cmds added; * + * 1.20m 98/06/10 KG glitch configurable; made some global * + * vars part of ACB; use DC390_readX * + * 1.20n 98/06/11 KG startup params * + * 1.20o 98/06/15 KG added TagMaxNum to boot/module params * + * Device Nr -> Idx, TagMaxNum power of 2 * + * 1.20p 98/06/17 KG Docu updates. Reset depends on settings * + * pci_set_master added; 2.0.xx: pcibios_* * + * used instead of MechNum things ... * + * 1.20q 98/06/23 KG Changed defaults. Added debug code for * + * removable media and fixed it. TagMaxNum * + * fixed for DC390. Locking: ACB, DRV for * + * better IRQ sharing. Spelling: Queueing * + * Parsing and glitch_cfg changes. Display * + * real SyncSpeed value. Made DisConn * + * functional (!) * + * 1.20r 98/06/30 KG Debug macros, allow disabling DsCn, set * + * BIT4 in CtrlR4, EN_PAGE_INT, 2.0 module * + * param -1 fixed. * + * 1.20s 98/08/20 KG Debug info on abort(), try to check PCI,* + * phys_to_bus instead of phys_to_virt, * + * fixed sel. process, fixed locking, * + * added MODULE_XXX infos, changed IRQ * + * request flags, disable DMA_INT * + * 1.20t 98/09/07 KG TagQ report fixed; Write Erase DMA Stat;* + * initfunc -> __init; better abort; * + * Timeout for XFER_DONE & BLAST_COMPLETE; * + * Allow up to 33 commands being processed * + * 2.0a 98/10/14 KG Max Cmnds back to 17. DMA_Stat clearing * + * all flags. Clear within while() loops * + * in DataIn_0/Out_0. Null ptr in dumpinfo * + * for pSRB==0. Better locking during init.* + * bios_param() now respects part. table. * + * 2.0b 98/10/24 KG Docu fixes. Timeout Msg in DMA Blast. * + * Disallow illegal idx in INQUIRY/REMOVE * + * 2.0c 98/11/19 KG Cleaned up detect/init for SMP boxes, * + * Write Erase DMA (1.20t) caused problems * + * 2.0d 98/12/25 KG Christmas release ;-) Message handling * + * completely reworked. Handle target ini- * + * tiated SDTR correctly. * + * 2.0d1 99/01/25 KG Try to handle RESTORE_PTR * + * 2.0d2 99/02/08 KG Check for failure of kmalloc, correct * + * inclusion of scsicam.h, DelayReset * + * 2.0d3 99/05/31 KG DRIVER_OK -> DID_OK, DID_NO_CONNECT, * + * detect Target mode and warn. * + * pcmd->result handling cleaned up. * + * 2.0d4 99/06/01 KG Cleaned selection process. Found bug * + * which prevented more than 16 tags. Now: * + * 24. SDTR cleanup. Cleaner multi-LUN * + * handling. Don't modify ControlRegs/FIFO * + * when connected. * + * 2.0d5 99/06/01 KG Clear DevID, Fix INQUIRY after cfg chg. * + * 2.0d6 99/06/02 KG Added ADD special command to allow cfg. * + * before detection. Reset SYNC_NEGO_DONE * + * after a bus reset. * + * 2.0d7 99/06/03 KG Fixed bugs wrt add,remove commands * + * 2.0d8 99/06/04 KG Removed copying of cmnd into CmdBlock. * + * Fixed Oops in _release(). * + * 2.0d9 99/06/06 KG Also tag queue INQUIRY, T_U_R, ... * + * Allow arb. no. of Tagged Cmnds. Max 32 * + * 2.0d1099/06/20 KG TagMaxNo changes now honoured! Queueing * + * clearified (renamed ..) TagMask handling* + * cleaned. * + * 2.0d1199/06/28 KG cmd->result now identical to 2.0d2 * + * 2.0d1299/07/04 KG Changed order of processing in IRQ * + * 2.0d1399/07/05 KG Don't update DCB fields if removed * + * 2.0d1499/07/05 KG remove_dev: Move kfree() to the end * + * 2.0d1599/07/12 KG use_new_eh_code: 0, ULONG -> UINT where * + * appropriate * + * 2.0d1699/07/13 KG Reenable StartSCSI interrupt, Retry msg * + * 2.0d1799/07/15 KG Remove debug msg. Disable recfg. when * + * there are queued cmnds * + * 2.0d1899/07/18 KG Selection timeout: Don't requeue * + * 2.0d1999/07/18 KG Abort: Only call scsi_done if dequeued * + * 2.0d2099/07/19 KG Rst_Detect: DoingSRB_Done * + * 2.0d2199/08/15 KG dev_id for request/free_irq, cmnd[0] for* + * RETRY, SRBdone does DID_ABORT for the * + * cmd passed by DC390_reset() * + * 2.0d2299/08/25 KG dev_id fixed. can_queue: 42 * + * 2.0d2399/08/25 KG Removed some debugging code. dev_id * + * now is set to pACB. Use u8,u16,u32. * + * 2.0d2499/11/14 KG Unreg. I/O if failed IRQ alloc. Call * + * done () w/ DID_BAD_TARGET in case of * + * missing DCB. We are old EH!! * + * 2.0d2500/01/15 KG 2.3.3x compat from Andreas Schultz * + * set unique_id. Disable RETRY message. * + * 2.0d2600/01/29 KG Go to new EH. * + * 2.0d2700/01/31 KG ... but maintain 2.0 compat. * + * and fix DCB freeing * + * 2.0d2800/02/14 KG Queue statistics fixed, dump special cmd* + * Waiting_Timer for failed StartSCSI * + * New EH: Don't return cmnds to ML on RST * + * Use old EH (don't have new EH fns yet) * + * Reset: Unlock, but refuse to queue * + * 2.3 __setup function * + * 2.0e 00/05/22 KG Return residual for 2.3 * + * 2.0e1 00/05/25 KG Compile fixes for 2.3.99 * + * 2.0e2 00/05/27 KG Jeff Garzik's pci_enable_device() * + * 2.0e3 00/09/29 KG Some 2.4 changes. Don't try Sync Nego * + * before INQUIRY has reported ability. * + * Recognise INQUIRY as scanning command. * + * 2.0e4 00/10/13 KG Allow compilation into 2.4 kernel * + * 2.0e5 00/11/17 KG Store Inq.flags in DCB * + * 2.0e6 00/11/22 KG 2.4 init function (Thx to O.Schumann) * + * 2.4 PCI device table (Thx to A.Richter) * + * 2.0e7 00/11/28 KG Allow overriding of BIOS settings * + * 2.0f 00/12/20 KG Handle failed INQUIRYs during scan * + * 2.1a 03/11/29 GL, KG Initial fixing for 2.6. Convert to * + * use the current PCI-mapping API, update * + * command-queuing. * + * 2.1b 04/04/13 GL Fix for 64-bit platforms * + * 2.1b1 04/01/31 GL (applied 05.04) Remove internal * + * command-queuing. * + * 2.1b2 04/02/01 CH (applied 05.04) Fix error-handling * + * 2.1c 04/05/23 GL Update to use the new pci_driver API, * + * some scsi EH updates, more cleanup. * + * 2.1d 04/05/27 GL Moved setting of scan_devices to * + * slave_alloc/_configure/_destroy, as * + * suggested by CH. * + ***********************************************************************/ + +/* DEBUG options */ +//#define DC390_DEBUG0 +//#define DC390_DEBUG1 +//#define DC390_DCBDEBUG +//#define DC390_PARSEDEBUG +//#define DC390_REMOVABLEDEBUG +//#define DC390_LOCKDEBUG + +//#define NOP do{}while(0) +#define C_NOP + +/* Debug definitions */ +#ifdef DC390_DEBUG0 +# define DEBUG0(x) x +#else +# define DEBUG0(x) C_NOP +#endif +#ifdef DC390_DEBUG1 +# define DEBUG1(x) x +#else +# define DEBUG1(x) C_NOP +#endif +#ifdef DC390_DCBDEBUG +# define DCBDEBUG(x) x +#else +# define DCBDEBUG(x) C_NOP +#endif +#ifdef DC390_PARSEDEBUG +# define PARSEDEBUG(x) x +#else +# define PARSEDEBUG(x) C_NOP +#endif +#ifdef DC390_REMOVABLEDEBUG +# define REMOVABLEDEBUG(x) x +#else +# define REMOVABLEDEBUG(x) C_NOP +#endif +#define DCBDEBUG1(x) C_NOP + +#include <linux/config.h> +#include <linux/module.h> +#include <linux/delay.h> +#include <linux/signal.h> +#include <linux/sched.h> +#include <linux/errno.h> +#include <linux/kernel.h> +#include <linux/ioport.h> +#include <linux/pci.h> +#include <linux/proc_fs.h> +#include <linux/string.h> +#include <linux/mm.h> +#include <linux/blkdev.h> +#include <linux/timer.h> +#include <linux/interrupt.h> +#include <linux/init.h> +#include <linux/spinlock.h> +#include <asm/io.h> + +#include <scsi/scsi.h> +#include <scsi/scsi_cmnd.h> +#include <scsi/scsi_device.h> +#include <scsi/scsi_host.h> +#include <scsi/scsicam.h> +#include <scsi/scsi_tcq.h> + + +#define DC390_BANNER "Tekram DC390/AM53C974" +#define DC390_VERSION "2.1d 2004-05-27" + +#define PCI_DEVICE_ID_AMD53C974 PCI_DEVICE_ID_AMD_SCSI + +#include "tmscsim.h" + + +static void dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); +static void dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus); + +static void dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB ); +static void dc390_Disconnect( struct dc390_acb* pACB ); +static void dc390_Reselect( struct dc390_acb* pACB ); +static void dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB ); +static void dc390_ScsiRstDetect( struct dc390_acb* pACB ); +static void dc390_EnableMsgOut_Abort(struct dc390_acb*, struct dc390_srb*); +static void dc390_dumpinfo(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB); +static void dc390_ResetDevParam(struct dc390_acb* pACB); + +static u32 dc390_laststatus = 0; +static u8 dc390_adapterCnt = 0; + +/* Startup values, to be overriden on the commandline */ +static int tmscsim[] = {-2, -2, -2, -2, -2, -2}; + +module_param_array(tmscsim, int, NULL, 0); +MODULE_PARM_DESC(tmscsim, "Host SCSI ID, Speed (0=10MHz), Device Flags, Adapter Flags, Max Tags (log2(tags)-1), DelayReset (s)"); +MODULE_AUTHOR("C.L. Huang / Kurt Garloff"); +MODULE_DESCRIPTION("SCSI host adapter driver for Tekram DC390 and other AMD53C974A based PCI SCSI adapters"); +MODULE_LICENSE("GPL"); +MODULE_SUPPORTED_DEVICE("sd,sr,sg,st"); + +static void *dc390_phase0[]={ + dc390_DataOut_0, + dc390_DataIn_0, + dc390_Command_0, + dc390_Status_0, + dc390_Nop_0, + dc390_Nop_0, + dc390_MsgOut_0, + dc390_MsgIn_0, + dc390_Nop_1 + }; + +static void *dc390_phase1[]={ + dc390_DataOutPhase, + dc390_DataInPhase, + dc390_CommandPhase, + dc390_StatusPhase, + dc390_Nop_0, + dc390_Nop_0, + dc390_MsgOutPhase, + dc390_MsgInPhase, + dc390_Nop_1 + }; + +#ifdef DC390_DEBUG1 +static char* dc390_p0_str[] = { + "dc390_DataOut_0", + "dc390_DataIn_0", + "dc390_Command_0", + "dc390_Status_0", + "dc390_Nop_0", + "dc390_Nop_0", + "dc390_MsgOut_0", + "dc390_MsgIn_0", + "dc390_Nop_1" + }; + +static char* dc390_p1_str[] = { + "dc390_DataOutPhase", + "dc390_DataInPhase", + "dc390_CommandPhase", + "dc390_StatusPhase", + "dc390_Nop_0", + "dc390_Nop_0", + "dc390_MsgOutPhase", + "dc390_MsgInPhase", + "dc390_Nop_1" + }; +#endif + +static u8 dc390_eepromBuf[MAX_ADAPTER_NUM][EE_LEN]; +static u8 dc390_clock_period1[] = {4, 5, 6, 7, 8, 10, 13, 20}; +static u8 dc390_clock_speed[] = {100,80,67,57,50, 40, 31, 20}; + +/*********************************************************************** + * Functions for the management of the internal structures + * (DCBs, SRBs, Queueing) + * + **********************************************************************/ +static struct dc390_dcb __inline__ *dc390_findDCB ( struct dc390_acb* pACB, u8 id, u8 lun) +{ + struct dc390_dcb* pDCB = pACB->pLinkDCB; if (!pDCB) return NULL; + while (pDCB->TargetID != id || pDCB->TargetLUN != lun) + { + pDCB = pDCB->pNextDCB; + if (pDCB == pACB->pLinkDCB) + return NULL; + } + DCBDEBUG1( printk (KERN_DEBUG "DCB %p (%02x,%02x) found.\n", \ + pDCB, pDCB->TargetID, pDCB->TargetLUN)); + return pDCB; +} + +/* Insert SRB oin top of free list */ +static __inline__ void dc390_Free_insert (struct dc390_acb* pACB, struct dc390_srb* pSRB) +{ + DEBUG0(printk ("DC390: Free SRB %p\n", pSRB)); + pSRB->pNextSRB = pACB->pFreeSRB; + pACB->pFreeSRB = pSRB; +} + +static __inline__ void dc390_Going_append (struct dc390_dcb* pDCB, struct dc390_srb* pSRB) +{ + pDCB->GoingSRBCnt++; + DEBUG0(printk("DC390: Append SRB %p to Going\n", pSRB)); + /* Append to the list of Going commands */ + if( pDCB->pGoingSRB ) + pDCB->pGoingLast->pNextSRB = pSRB; + else + pDCB->pGoingSRB = pSRB; + + pDCB->pGoingLast = pSRB; + /* No next one in sent list */ + pSRB->pNextSRB = NULL; +} + +static __inline__ void dc390_Going_remove (struct dc390_dcb* pDCB, struct dc390_srb* pSRB) +{ + DEBUG0(printk("DC390: Remove SRB %p from Going\n", pSRB)); + if (pSRB == pDCB->pGoingSRB) + pDCB->pGoingSRB = pSRB->pNextSRB; + else + { + struct dc390_srb* psrb = pDCB->pGoingSRB; + while (psrb && psrb->pNextSRB != pSRB) + psrb = psrb->pNextSRB; + if (!psrb) + { printk (KERN_ERR "DC390: Remove non-ex. SRB %p from Going!\n", pSRB); return; } + psrb->pNextSRB = pSRB->pNextSRB; + if (pSRB == pDCB->pGoingLast) + pDCB->pGoingLast = psrb; + } + pDCB->GoingSRBCnt--; +} + +static struct scatterlist* dc390_sg_build_single(struct scatterlist *sg, void *addr, unsigned int length) +{ + memset(sg, 0, sizeof(struct scatterlist)); + sg->page = virt_to_page(addr); + sg->length = length; + sg->offset = (unsigned long)addr & ~PAGE_MASK; + return sg; +} + +/* Create pci mapping */ +static int dc390_pci_map (struct dc390_srb* pSRB) +{ + int error = 0; + struct scsi_cmnd *pcmd = pSRB->pcmd; + struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev; + dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp)); + + /* Map sense buffer */ + if (pSRB->SRBFlag & AUTO_REQSENSE) { + pSRB->pSegmentList = dc390_sg_build_single(&pSRB->Segmentx, pcmd->sense_buffer, sizeof(pcmd->sense_buffer)); + pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, 1, + DMA_FROM_DEVICE); + cmdp->saved_dma_handle = sg_dma_address(pSRB->pSegmentList); + + /* TODO: error handling */ + if (pSRB->SGcount != 1) + error = 1; + DEBUG1(printk("%s(): Mapped sense buffer %p at %x\n", __FUNCTION__, pcmd->sense_buffer, cmdp->saved_dma_handle)); + /* Map SG list */ + } else if (pcmd->use_sg) { + pSRB->pSegmentList = (struct scatterlist *) pcmd->request_buffer; + pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, pcmd->use_sg, + pcmd->sc_data_direction); + /* TODO: error handling */ + if (!pSRB->SGcount) + error = 1; + DEBUG1(printk("%s(): Mapped SG %p with %d (%d) elements\n",\ + __FUNCTION__, pcmd->request_buffer, pSRB->SGcount, pcmd->use_sg)); + /* Map single segment */ + } else if (pcmd->request_buffer && pcmd->request_bufflen) { + pSRB->pSegmentList = dc390_sg_build_single(&pSRB->Segmentx, pcmd->request_buffer, pcmd->request_bufflen); + pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, 1, + pcmd->sc_data_direction); + cmdp->saved_dma_handle = sg_dma_address(pSRB->pSegmentList); + + /* TODO: error handling */ + if (pSRB->SGcount != 1) + error = 1; + DEBUG1(printk("%s(): Mapped request buffer %p at %x\n", __FUNCTION__, pcmd->request_buffer, cmdp->saved_dma_handle)); + /* No mapping !? */ + } else + pSRB->SGcount = 0; + + return error; +} + +/* Remove pci mapping */ +static void dc390_pci_unmap (struct dc390_srb* pSRB) +{ + struct scsi_cmnd *pcmd = pSRB->pcmd; + struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev; + DEBUG1(dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp))); + + if (pSRB->SRBFlag) { + pci_unmap_sg(pdev, &pSRB->Segmentx, 1, DMA_FROM_DEVICE); + DEBUG1(printk("%s(): Unmapped sense buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle)); + } else if (pcmd->use_sg) { + pci_unmap_sg(pdev, pcmd->request_buffer, pcmd->use_sg, pcmd->sc_data_direction); + DEBUG1(printk("%s(): Unmapped SG at %p with %d elements\n", __FUNCTION__, pcmd->request_buffer, pcmd->use_sg)); + } else if (pcmd->request_buffer && pcmd->request_bufflen) { + pci_unmap_sg(pdev, &pSRB->Segmentx, 1, pcmd->sc_data_direction); + DEBUG1(printk("%s(): Unmapped request buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle)); + } +} + +static void __inline__ +dc390_freetag (struct dc390_dcb* pDCB, struct dc390_srb* pSRB) +{ + if (pSRB->TagNumber != SCSI_NO_TAG) { + pDCB->TagMask &= ~(1 << pSRB->TagNumber); /* free tag mask */ + pSRB->TagNumber = SCSI_NO_TAG; + } +} + + +static int +dc390_StartSCSI( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB ) +{ + struct scsi_cmnd *scmd = pSRB->pcmd; + struct scsi_device *sdev = scmd->device; + u8 cmd, disc_allowed, try_sync_nego; + char tag[2]; + + pSRB->ScsiPhase = SCSI_NOP0; + + if (pACB->Connected) + { + // Should not happen normally + printk (KERN_WARNING "DC390: Can't select when connected! (%08x,%02x)\n", + pSRB->SRBState, pSRB->SRBFlag); + pSRB->SRBState = SRB_READY; + pACB->SelConn++; + return 1; + } + if (time_before (jiffies, pACB->pScsiHost->last_reset)) + { + DEBUG0(printk ("DC390: We were just reset and don't accept commands yet!\n")); + return 1; + } + /* KG: Moved pci mapping here */ + dc390_pci_map(pSRB); + /* TODO: error handling */ + DC390_write8 (Scsi_Dest_ID, pDCB->TargetID); + DC390_write8 (Sync_Period, pDCB->SyncPeriod); + DC390_write8 (Sync_Offset, pDCB->SyncOffset); + DC390_write8 (CtrlReg1, pDCB->CtrlR1); + DC390_write8 (CtrlReg3, pDCB->CtrlR3); + DC390_write8 (CtrlReg4, pDCB->CtrlR4); + DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD); /* Flush FIFO */ + DEBUG1(printk (KERN_INFO "DC390: Start SCSI command: %02x (Sync:%02x)\n",\ + scmd->cmnd[0], pDCB->SyncMode)); + + /* Don't disconnect on AUTO_REQSENSE, cause it might be an + * Contingent Allegiance Condition (6.6), where no tags should be used. + * All other have to be allowed to disconnect to prevent Incorrect + * Initiator Connection (6.8.2/6.5.2) */ + /* Changed KG, 99/06/06 */ + if (! (pSRB->SRBFlag & AUTO_REQSENSE)) + disc_allowed = pDCB->DevMode & EN_DISCONNECT_; + else + disc_allowed = 0; + + if ((pDCB->SyncMode & SYNC_ENABLE) && pDCB->TargetLUN == 0 && sdev->sdtr && + (((scmd->cmnd[0] == REQUEST_SENSE || (pSRB->SRBFlag & AUTO_REQSENSE)) && + !(pDCB->SyncMode & SYNC_NEGO_DONE)) || scmd->cmnd[0] == INQUIRY)) + try_sync_nego = 1; + else + try_sync_nego = 0; + + pSRB->MsgCnt = 0; + cmd = SEL_W_ATN; + DC390_write8 (ScsiFifo, IDENTIFY(disc_allowed, pDCB->TargetLUN)); + /* Change 99/05/31: Don't use tags when not disconnecting (BUSY) */ + if ((pDCB->SyncMode & EN_TAG_QUEUEING) && disc_allowed && scsi_populate_tag_msg(scmd, tag)) { + DC390_write8(ScsiFifo, tag[0]); + pDCB->TagMask |= 1 << tag[1]; + pSRB->TagNumber = tag[1]; + DC390_write8(ScsiFifo, tag[1]); + DEBUG1(printk(KERN_INFO "DC390: Select w/DisCn for Cmd %li (SRB %p), block tag %02x\n", scmd->pid, pSRB, tag[1])); + cmd = SEL_W_ATN3; + } else { + /* No TagQ */ +//no_tag: + DEBUG1(printk(KERN_INFO "DC390: Select w%s/DisCn for Cmd %li (SRB %p), No TagQ\n", disc_allowed ? "" : "o", scmd->pid, pSRB)); + } + + pSRB->SRBState = SRB_START_; + + if (try_sync_nego) + { + u8 Sync_Off = pDCB->SyncOffset; + DEBUG0(printk (KERN_INFO "DC390: NEW Sync Nego code triggered (%i %i)\n", pDCB->TargetID, pDCB->TargetLUN)); + pSRB->MsgOutBuf[0] = EXTENDED_MESSAGE; + pSRB->MsgOutBuf[1] = 3; + pSRB->MsgOutBuf[2] = EXTENDED_SDTR; + pSRB->MsgOutBuf[3] = pDCB->NegoPeriod; + if (!(Sync_Off & 0x0f)) Sync_Off = SYNC_NEGO_OFFSET; + pSRB->MsgOutBuf[4] = Sync_Off; + pSRB->MsgCnt = 5; + //pSRB->SRBState = SRB_MSGOUT_; + pSRB->SRBState |= DO_SYNC_NEGO; + cmd = SEL_W_ATN_STOP; + } + + /* Command is written in CommandPhase, if SEL_W_ATN_STOP ... */ + if (cmd != SEL_W_ATN_STOP) + { + if( pSRB->SRBFlag & AUTO_REQSENSE ) + { + DC390_write8 (ScsiFifo, REQUEST_SENSE); + DC390_write8 (ScsiFifo, pDCB->TargetLUN << 5); + DC390_write8 (ScsiFifo, 0); + DC390_write8 (ScsiFifo, 0); + DC390_write8 (ScsiFifo, sizeof(scmd->sense_buffer)); + DC390_write8 (ScsiFifo, 0); + DEBUG1(printk (KERN_DEBUG "DC390: AutoReqSense !\n")); + } + else /* write cmnd to bus */ + { + u8 *ptr; u8 i; + ptr = (u8 *)scmd->cmnd; + for (i = 0; i < scmd->cmd_len; i++) + DC390_write8 (ScsiFifo, *(ptr++)); + } + } + DEBUG0(if (pACB->pActiveDCB) \ + printk (KERN_WARNING "DC390: ActiveDCB != 0\n")); + DEBUG0(if (pDCB->pActiveSRB) \ + printk (KERN_WARNING "DC390: ActiveSRB != 0\n")); + //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD); + if (DC390_read8 (Scsi_Status) & INTERRUPT) + { + dc390_freetag (pDCB, pSRB); + DEBUG0(printk ("DC390: Interrupt during Start SCSI (pid %li, target %02i-%02i)\n", + scmd->pid, scmd->device->id, scmd->device->lun)); + pSRB->SRBState = SRB_READY; + //DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD); + pACB->SelLost++; + return 1; + } + DC390_write8 (ScsiCmd, cmd); + pACB->pActiveDCB = pDCB; + pDCB->pActiveSRB = pSRB; + pACB->Connected = 1; + pSRB->ScsiPhase = SCSI_NOP1; + return 0; +} + +//#define DMA_INT EN_DMA_INT /*| EN_PAGE_INT*/ +#define DMA_INT 0 + +#if DMA_INT +/* This is similar to AM53C974.c ... */ +static u8 +dc390_dma_intr (struct dc390_acb* pACB) +{ + struct dc390_srb* pSRB; + u8 dstate; + DEBUG0(u16 pstate; struct pci_dev *pdev = pACB->pdev); + + DEBUG0(pci_read_config_word(pdev, PCI_STATUS, &pstate)); + DEBUG0(if (pstate & (PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY))\ + { printk(KERN_WARNING "DC390: PCI state = %04x!\n", pstate); \ + pci_write_config_word(pdev, PCI_STATUS, (PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY));}); + + dstate = DC390_read8 (DMA_Status); + + if (! pACB->pActiveDCB || ! pACB->pActiveDCB->pActiveSRB) return dstate; + else pSRB = pACB->pActiveDCB->pActiveSRB; + + if (dstate & (DMA_XFER_ABORT | DMA_XFER_ERROR | POWER_DOWN | PCI_MS_ABORT)) + { + printk (KERN_ERR "DC390: DMA error (%02x)!\n", dstate); + return dstate; + } + if (dstate & DMA_XFER_DONE) + { + u32 residual, xferCnt; int ctr = 6000000; + if (! (DC390_read8 (DMA_Cmd) & READ_DIRECTION)) + { + do + { + DEBUG1(printk (KERN_DEBUG "DC390: read residual bytes ... \n")); + dstate = DC390_read8 (DMA_Status); + residual = DC390_read8 (CtcReg_Low) | DC390_read8 (CtcReg_Mid) << 8 | + DC390_read8 (CtcReg_High) << 16; + residual += DC390_read8 (Current_Fifo) & 0x1f; + } while (residual && ! (dstate & SCSI_INTERRUPT) && --ctr); + if (!ctr) printk (KERN_CRIT "DC390: dma_intr: DMA aborted unfinished: %06x bytes remain!!\n", DC390_read32 (DMA_Wk_ByteCntr)); + /* residual = ... */ + } + else + residual = 0; + + /* ??? */ + + xferCnt = pSRB->SGToBeXferLen - residual; + pSRB->SGBusAddr += xferCnt; + pSRB->TotalXferredLen += xferCnt; + pSRB->SGToBeXferLen = residual; +# ifdef DC390_DEBUG0 + printk (KERN_INFO "DC390: DMA: residual = %i, xfer = %i\n", + (unsigned int)residual, (unsigned int)xferCnt); +# endif + + DC390_write8 (DMA_Cmd, DMA_IDLE_CMD); + } + dc390_laststatus &= ~0xff000000; dc390_laststatus |= dstate << 24; + return dstate; +} +#endif + + +static void __inline__ +dc390_InvalidCmd(struct dc390_acb* pACB) +{ + if (pACB->pActiveDCB->pActiveSRB->SRBState & (SRB_START_ | SRB_MSGOUT)) + DC390_write8(ScsiCmd, CLEAR_FIFO_CMD); +} + + +static irqreturn_t __inline__ +DC390_Interrupt(int irq, void *dev_id, struct pt_regs *regs) +{ + struct dc390_acb *pACB = (struct dc390_acb*)dev_id; + struct dc390_dcb *pDCB; + struct dc390_srb *pSRB; + u8 sstatus=0; + u8 phase; + void (*stateV)( struct dc390_acb*, struct dc390_srb*, u8 *); + u8 istate, istatus; +#if DMA_INT + u8 dstatus; +#endif + + sstatus = DC390_read8 (Scsi_Status); + if( !(sstatus & INTERRUPT) ) + return IRQ_NONE; + + DEBUG1(printk (KERN_DEBUG "sstatus=%02x,", sstatus)); + +#if DMA_INT + spin_lock_irq(pACB->pScsiHost->host_lock); + dstatus = dc390_dma_intr (pACB); + spin_unlock_irq(pACB->pScsiHost->host_lock); + + DEBUG1(printk (KERN_DEBUG "dstatus=%02x,", dstatus)); + if (! (dstatus & SCSI_INTERRUPT)) + { + DEBUG0(printk (KERN_WARNING "DC390 Int w/o SCSI actions (only DMA?)\n")); + return IRQ_NONE; + } +#else + //DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT); + //dstatus = DC390_read8 (DMA_Status); + //DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT); +#endif + + spin_lock_irq(pACB->pScsiHost->host_lock); + + istate = DC390_read8 (Intern_State); + istatus = DC390_read8 (INT_Status); /* This clears Scsi_Status, Intern_State and INT_Status ! */ + + DEBUG1(printk (KERN_INFO "Istatus(Res,Inv,Dis,Serv,Succ,ReS,SelA,Sel)=%02x,",istatus)); + dc390_laststatus &= ~0x00ffffff; + dc390_laststatus |= /* dstatus<<24 | */ sstatus<<16 | istate<<8 | istatus; + + if (sstatus & ILLEGAL_OP_ERR) + { + printk ("DC390: Illegal Operation detected (%08x)!\n", dc390_laststatus); + dc390_dumpinfo (pACB, pACB->pActiveDCB, pACB->pActiveDCB->pActiveSRB); + } + + else if (istatus & INVALID_CMD) + { + printk ("DC390: Invalid Command detected (%08x)!\n", dc390_laststatus); + dc390_InvalidCmd( pACB ); + goto unlock; + } + + if (istatus & SCSI_RESET) + { + dc390_ScsiRstDetect( pACB ); + goto unlock; + } + + if (istatus & DISCONNECTED) + { + dc390_Disconnect( pACB ); + goto unlock; + } + + if (istatus & RESELECTED) + { + dc390_Reselect( pACB ); + goto unlock; + } + + else if (istatus & (SELECTED | SEL_ATTENTION)) + { + printk (KERN_ERR "DC390: Target mode not supported!\n"); + goto unlock; + } + + if (istatus & (SUCCESSFUL_OP|SERVICE_REQUEST) ) + { + pDCB = pACB->pActiveDCB; + if (!pDCB) + { + printk (KERN_ERR "DC390: Suc. op/ Serv. req: pActiveDCB = 0!\n"); + goto unlock; + } + pSRB = pDCB->pActiveSRB; + if( pDCB->DCBFlag & ABORT_DEV_ ) + dc390_EnableMsgOut_Abort (pACB, pSRB); + + phase = pSRB->ScsiPhase; + DEBUG1(printk (KERN_INFO "DC390: [%i]%s(0) (%02x)\n", phase, dc390_p0_str[phase], sstatus)); + stateV = (void *) dc390_phase0[phase]; + ( *stateV )( pACB, pSRB, &sstatus ); + + pSRB->ScsiPhase = sstatus & 7; + phase = (u8) sstatus & 7; + DEBUG1(printk (KERN_INFO "DC390: [%i]%s(1) (%02x)\n", phase, dc390_p1_str[phase], sstatus)); + stateV = (void *) dc390_phase1[phase]; + ( *stateV )( pACB, pSRB, &sstatus ); + } + + unlock: + spin_unlock_irq(pACB->pScsiHost->host_lock); + return IRQ_HANDLED; +} + +static irqreturn_t do_DC390_Interrupt( int irq, void *dev_id, struct pt_regs *regs) +{ + irqreturn_t ret; + DEBUG1(printk (KERN_INFO "DC390: Irq (%i) caught: ", irq)); + /* Locking is done in DC390_Interrupt */ + ret = DC390_Interrupt(irq, dev_id, regs); + DEBUG1(printk (".. IRQ returned\n")); + return ret; +} + +static void +dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus) +{ + u8 sstatus; + struct scatterlist *psgl; + u32 ResidCnt, xferCnt; + u8 dstate = 0; + + sstatus = *psstatus; + + if( !(pSRB->SRBState & SRB_XFERPAD) ) + { + if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR) ) + pSRB->SRBStatus |= PARITY_ERROR; + + if( sstatus & COUNT_2_ZERO ) + { + unsigned long timeout = jiffies + HZ; + + /* Function called from the ISR with the host_lock held and interrupts disabled */ + if (pSRB->SGToBeXferLen) + while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) { + spin_unlock_irq(pACB->pScsiHost->host_lock); + udelay(50); + spin_lock_irq(pACB->pScsiHost->host_lock); + } + if (!time_before(jiffies, timeout)) + printk (KERN_CRIT "DC390: Deadlock in DataOut_0: DMA aborted unfinished: %06x bytes remain!!\n", + DC390_read32 (DMA_Wk_ByteCntr)); + dc390_laststatus &= ~0xff000000; + dc390_laststatus |= dstate << 24; + pSRB->TotalXferredLen += pSRB->SGToBeXferLen; + pSRB->SGIndex++; + if( pSRB->SGIndex < pSRB->SGcount ) + { + pSRB->pSegmentList++; + psgl = pSRB->pSegmentList; + + pSRB->SGBusAddr = cpu_to_le32(pci_dma_lo32(sg_dma_address(psgl))); + pSRB->SGToBeXferLen = cpu_to_le32(sg_dma_len(psgl)); + } + else + pSRB->SGToBeXferLen = 0; + } + else + { + ResidCnt = (u32) DC390_read8 (Current_Fifo) & 0x1f; + ResidCnt |= (u32) DC390_read8 (CtcReg_High) << 16; + ResidCnt |= (u32) DC390_read8 (CtcReg_Mid) << 8; + ResidCnt += (u32) DC390_read8 (CtcReg_Low); + + xferCnt = pSRB->SGToBeXferLen - ResidCnt; + pSRB->SGBusAddr += xferCnt; + pSRB->TotalXferredLen += xferCnt; + pSRB->SGToBeXferLen = ResidCnt; + } + } + if ((*psstatus & 7) != SCSI_DATA_OUT) + { + DC390_write8 (DMA_Cmd, WRITE_DIRECTION+DMA_IDLE_CMD); /* | DMA_INT */ + DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD); + } +} + +static void +dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus) +{ + u8 sstatus, residual, bval; + struct scatterlist *psgl; + u32 ResidCnt, i; + unsigned long xferCnt; + u8 *ptr; + + sstatus = *psstatus; + + if( !(pSRB->SRBState & SRB_XFERPAD) ) + { + if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR)) + pSRB->SRBStatus |= PARITY_ERROR; + + if( sstatus & COUNT_2_ZERO ) + { + int dstate = 0; + unsigned long timeout = jiffies + HZ; + + /* Function called from the ISR with the host_lock held and interrupts disabled */ + if (pSRB->SGToBeXferLen) + while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) { + spin_unlock_irq(pACB->pScsiHost->host_lock); + udelay(50); + spin_lock_irq(pACB->pScsiHost->host_lock); + } + if (!time_before(jiffies, timeout)) { + printk (KERN_CRIT "DC390: Deadlock in DataIn_0: DMA aborted unfinished: %06x bytes remain!!\n", + DC390_read32 (DMA_Wk_ByteCntr)); + printk (KERN_CRIT "DC390: DataIn_0: DMA State: %i\n", dstate); + } + dc390_laststatus &= ~0xff000000; + dc390_laststatus |= dstate << 24; + DEBUG1(ResidCnt = ((unsigned long) DC390_read8 (CtcReg_High) << 16) \ + + ((unsigned long) DC390_read8 (CtcReg_Mid) << 8) \ + + ((unsigned long) DC390_read8 (CtcReg_Low))); + DEBUG1(printk (KERN_DEBUG "Count_2_Zero (ResidCnt=%i,ToBeXfer=%li),", ResidCnt, pSRB->SGToBeXferLen)); + + DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD); /* | DMA_INT */ + + pSRB->TotalXferredLen += pSRB->SGToBeXferLen; + pSRB->SGIndex++; + if( pSRB->SGIndex < pSRB->SGcount ) + { + pSRB->pSegmentList++; + psgl = pSRB->pSegmentList; + + pSRB->SGBusAddr = cpu_to_le32(pci_dma_lo32(sg_dma_address(psgl))); + pSRB->SGToBeXferLen = cpu_to_le32(sg_dma_len(psgl)); + } + else + pSRB->SGToBeXferLen = 0; + } + else /* phase changed */ + { + residual = 0; + bval = DC390_read8 (Current_Fifo); + while( bval & 0x1f ) + { + DEBUG1(printk (KERN_DEBUG "Check for residuals,")); + if( (bval & 0x1f) == 1 ) + { + for(i=0; i < 0x100; i++) + { + bval = DC390_read8 (Current_Fifo); + if( !(bval & 0x1f) ) + goto din_1; + else if( i == 0x0ff ) + { + residual = 1; /* ;1 residual byte */ + goto din_1; + } + } + } + else + bval = DC390_read8 (Current_Fifo); + } +din_1: + DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_BLAST_CMD); + for (i = 0xa000; i; i--) + { + bval = DC390_read8 (DMA_Status); + if (bval & BLAST_COMPLETE) + break; + } + /* It seems a DMA Blast abort isn't that bad ... */ + if (!i) printk (KERN_ERR "DC390: DMA Blast aborted unfinished!\n"); + //DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD); /* | DMA_INT */ + dc390_laststatus &= ~0xff000000; dc390_laststatus |= bval << 24; + + DEBUG1(printk (KERN_DEBUG "Blast: Read %i times DMA_Status %02x", 0xa000-i, bval)); + ResidCnt = (u32) DC390_read8 (CtcReg_High); + ResidCnt <<= 8; + ResidCnt |= (u32) DC390_read8 (CtcReg_Mid); + ResidCnt <<= 8; + ResidCnt |= (u32) DC390_read8 (CtcReg_Low); + + xferCnt = pSRB->SGToBeXferLen - ResidCnt; |