197 # define DEBUG0(x) C_NOP
202 # define DEBUG1(x) C_NOP
204 #ifdef DC390_DCBDEBUG
205 # define DCBDEBUG(x) x
207 # define DCBDEBUG(x) C_NOP
209 #ifdef DC390_PARSEDEBUG
210 # define PARSEDEBUG(x) x
212 # define PARSEDEBUG(x) C_NOP
214 #ifdef DC390_REMOVABLEDEBUG
215 # define REMOVABLEDEBUG(x) x
217 # define REMOVABLEDEBUG(x) C_NOP
219 #define DCBDEBUG1(x) C_NOP
221 #include <linux/module.h>
223 #include <linux/signal.h>
224 #include <linux/errno.h>
225 #include <linux/kernel.h>
227 #include <linux/pci.h>
229 #include <linux/string.h>
236 #include <linux/slab.h>
239 #include <scsi/scsi.h>
247 #define DC390_BANNER "Tekram DC390/AM53C974"
248 #define DC390_VERSION "2.1d 2004-05-27"
250 #define PCI_DEVICE_ID_AMD53C974 PCI_DEVICE_ID_AMD_SCSI
271 static void dc390_Disconnect(
struct dc390_acb* pACB );
272 static void dc390_Reselect(
struct dc390_acb* pACB );
274 static void dc390_ScsiRstDetect(
struct dc390_acb* pACB );
277 static void dc390_ResetDevParam(
struct dc390_acb* pACB);
279 static u32 dc390_laststatus = 0;
280 static u8 dc390_adapterCnt = 0;
282 static int disable_clustering;
284 MODULE_PARM_DESC(disable_clustering,
"If you experience problems with your devices, try setting to 1");
287 static int tmscsim[] = {-2, -2, -2, -2, -2, -2};
290 MODULE_PARM_DESC(tmscsim,
"Host SCSI ID, Speed (0=10MHz), Device Flags, Adapter Flags, Max Tags (log2(tags)-1), DelayReset (s)");
292 MODULE_DESCRIPTION(
"SCSI host adapter driver for Tekram DC390 and other AMD53C974A based PCI SCSI adapters");
296 static void *dc390_phase0[]={
308 static void *dc390_phase1[]={
321 static char* dc390_p0_str[] = {
333 static char* dc390_p1_str[] = {
334 "dc390_DataOutPhase",
336 "dc390_CommandPhase",
347 static u8 dc390_clock_period1[] = {4, 5, 6, 7, 8, 10, 13, 20};
348 static u8 dc390_clock_speed[] = {100,80,67,57,50, 40, 31, 20};
355 static void inline dc390_start_segment(
struct dc390_srb* pSRB)
364 static unsigned long inline dc390_advance_segment(
struct dc390_srb* pSRB,
u32 residue)
401 DEBUG0(
printk(
"DC390: Append SRB %p to Going\n", pSRB));
415 DEBUG0(
printk(
"DC390: Remove SRB %p from Going\n", pSRB));
421 while (psrb && psrb->
pNextSRB != pSRB)
424 {
printk (
KERN_ERR "DC390: Remove non-ex. SRB %p from Going!\n", pSRB);
return; }
439 static int dc390_pci_map (
struct dc390_srb* pSRB)
458 }
else if (scsi_sg_count(pcmd)) {
469 DEBUG1(
printk(
"%s(): Mapped SG %p with %d (%d) elements\n",\
470 __func__, scsi_sglist(pcmd), nseg, scsi_sg_count(pcmd)));
479 static void dc390_pci_unmap (
struct dc390_srb* pSRB)
490 DEBUG1(
printk(
"%s(): Unmapped SG at %p with %d elements\n",
491 __func__, scsi_sglist(pcmd), scsi_sg_count(pcmd)));
510 u8 cmd, disc_allowed, try_sync_nego;
526 DEBUG0(
printk (
"DC390: We were just reset and don't accept commands yet!\n"));
573 DEBUG1(
printk(
KERN_INFO "DC390: Select w%s/DisCn for SRB %p, No TagQ\n", disc_allowed ?
"" :
"o", pSRB));
622 dc390_freetag (pDCB, pSRB);
623 DEBUG0(
printk (
"DC390: Interrupt during Start SCSI (target %02i-%02i)\n",
648 DC390_Interrupt(
void *
dev_id)
659 if( !(sstatus & INTERRUPT) )
668 spin_lock_irq(pACB->
pScsiHost->host_lock);
674 dc390_laststatus &= ~0x00ffffff;
675 dc390_laststatus |= sstatus<<16 | istate<<8 | istatus;
679 printk (
"DC390: Illegal Operation detected (%08x)!\n", dc390_laststatus);
685 printk (
"DC390: Invalid Command detected (%08x)!\n", dc390_laststatus);
686 dc390_InvalidCmd( pACB );
692 dc390_ScsiRstDetect( pACB );
698 dc390_Disconnect( pACB );
704 dc390_Reselect( pACB );
724 dc390_EnableMsgOut_Abort (pACB, pSRB);
728 stateV = (
void *) dc390_phase0[phase];
729 ( *stateV )( pACB, pSRB, &sstatus );
731 pSRB->ScsiPhase = sstatus & 7;
732 phase = (
u8) sstatus & 7;
734 stateV = (
void *) dc390_phase1[phase];
735 ( *stateV )( pACB, pSRB, &sstatus );
739 spin_unlock_irq(pACB->pScsiHost->host_lock);
743 static irqreturn_t do_DC390_Interrupt(
int irq,
void *dev_id)
748 ret = DC390_Interrupt(dev_id);
774 spin_unlock_irq(pACB->
pScsiHost->host_lock);
776 spin_lock_irq(pACB->
pScsiHost->host_lock);
779 printk (
KERN_CRIT "DC390: Deadlock in DataOut_0: DMA aborted unfinished: %06x bytes remain!!\n",
781 dc390_laststatus &= ~0xff000000;
782 dc390_laststatus |= dstate << 24;
789 dc390_start_segment(pSRB);
801 dc390_advance_segment(pSRB, ResidCnt);
816 unsigned long xferCnt;
825 if( sstatus & COUNT_2_ZERO )
833 spin_unlock_irq(pACB->
pScsiHost->host_lock);
835 spin_lock_irq(pACB->
pScsiHost->host_lock);
838 printk (
KERN_CRIT "DC390: Deadlock in DataIn_0: DMA aborted unfinished: %06x bytes remain!!\n",
842 dc390_laststatus &= ~0xff000000;
843 dc390_laststatus |= dstate << 24;
857 dc390_start_segment(pSRB);
869 if( (bval & 0x1f) == 1 )
871 for(i=0; i < 0x100; i++)
876 else if( i == 0x0ff )
888 for (i = 0xa000;
i; i--)
895 if (!i)
printk (
KERN_ERR "DC390: DMA Blast aborted unfinished!\n");
897 dc390_laststatus &= ~0xff000000;
898 dc390_laststatus |= bval << 24;
905 xferCnt = dc390_advance_segment(pSRB, ResidCnt);
925 xferCnt += dc390_advance_segment(pSRB, pSRB->
SGToBeXferLen - 1);
972 dc390_SetXferRate (pACB, pDCB);
978 dc390_printMsg (
u8 *MsgBuf,
u8 len)
981 printk (
" %02x", MsgBuf[0]);
982 for (i = 1; i < len; i++)
983 printk (
" %02x", MsgBuf[i]);
988 #define DC390_ENABLE_MSGOUT DC390_write8 (ScsiCmd, SET_ATN_CMD)
1016 struct scsi_cmnd *scmd = scsi_find_tag(pSRB->
pcmd->device, tag);
1022 dc390_EnableMsgOut_Abort( pACB, pSRB );
1059 dc390_reprog (pACB, pDCB);
1074 printk (
KERN_INFO "DC390: Target %i initiates Sync: %ins %i ... answer ...\n",
1104 wval = wval << 2; wval -= 3; wval1 = wval / 25;
1105 if( (wval1 * 25) != wval) wval1++;
1125 if ((oldsyncperiod != wval1 || oldsyncoffset != pDCB->
SyncOffset) && pDCB->
TargetLUN == 0)
1127 if (! (bval & FAST_SCSI)) wval1++;
1129 40/wval1, ((40%wval1)*10+wval1/2)/wval1, pDCB->
SyncOffset & 0x0f);
1132 dc390_reprog (pACB, pDCB);
1145 if (scsi_sg_count(pcmd)) {
1159 dc390_start_segment(pSRB);
1168 printk (
KERN_INFO "DC390: Pointer restored. Segment %i, Total %li, Bus %08lx\n",
1173 printk (
KERN_INFO "DC390: RESTORE_PTR message for Transfer without Scatter-Gather ??\n");
1194 if (len < 2)
return 0;
1195 if (len < msgbuf[1] + 2)
return 0;
1197 else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f)
1198 if (len < 2)
return 0;
1228 pSRB = dc390_MsgIn_QTag (pACB, pDCB, pSRB->
MsgInBuf[1]);
1235 dc390_MsgIn_set_async (pACB, pSRB);
1241 dc390_MsgIn_reject (pACB, pSRB);
1245 dc390_MsgIn_set_async (pACB, pSRB);
1247 dc390_MsgIn_set_sync (pACB, pSRB);
1260 DEBUG0(
printk (
"DC390: RESTORE POINTER message received ... try to handle\n"));
1261 dc390_restore_ptr (pACB, pSRB);
1265 default: dc390_MsgIn_reject (pACB, pSRB);
1290 printk(
KERN_ERR "DC390: pSRB == pTmpSRB! (TagQ Error?) (DCB 0!)\n");
1298 dc390_EnableMsgOut_Abort(pACB, pSRB);
1310 dc390_start_segment(pSRB);
1381 cnt = (
u8) pSRB->
pcmd->cmd_len;
1382 ptr = (
u8 *) pSRB->
pcmd->cmnd;
1383 for(i=0; i <
cnt; i++)
1425 for(i=0; i < cnt; i++)
1500 for(i=0; i<
cnt; i++)
1519 dc390_Disconnect(
struct dc390_acb* pACB )
1552 for( i=0; i <
cnt; i++)
1555 dc390_Free_insert (pACB, pSRB);
1572 dc390_freetag (pDCB, pSRB);
1575 dc390_SRBdone( pACB, pDCB, pSRB);
1583 dc390_Reselect(
struct dc390_acb* pACB )
1594 DEBUG0(
printk (
"DC390: (ActiveDCB != 0: Arb. lost but resel. won)!\n"));
1599 scsi_set_resid(pcmd, scsi_bufflen(pcmd));
1601 dc390_Going_remove(pDCB, pSRB);
1602 dc390_Free_insert(pACB, pSRB);
1610 if (!(lun & (1 << pACB->
pScsiHost->this_id)))
1611 printk (
KERN_ERR "DC390: Reselection must select host adapter: %02x!\n", lun);
1614 id = 0;
while (lun >>= 1)
id++;
1620 pDCB = dc390_findDCB (pACB,
id, lun);
1623 printk (
KERN_ERR "DC390: Reselect from non existing device (%02i-%i)\n",
1641 printk (
KERN_ERR "DC390: Reselect without outstanding cmnd (%02i-%i)\n",
1644 dc390_EnableMsgOut_Abort ( pACB, pSRB );
1653 dc390_EnableMsgOut_Abort( pACB, pSRB );
1692 return dc390_StartSCSI(pACB, pDCB, pSRB);
1704 dc390_pci_unmap(pSRB);
1740 if (dc390_RequestSense(pACB, pDCB, pSRB)) {
1799 dc390_Going_remove (pDCB, pSRB);
1801 dc390_Free_insert (pACB, pSRB);
1829 dc390_Free_insert (pACB, psrb);
1836 }
while( pdcb != pDCB );
1841 dc390_ResetSCSIBus(
struct dc390_acb* pACB )
1856 dc390_ScsiRstDetect(
struct dc390_acb* pACB )
1858 printk (
"DC390: Rst_Detect: laststat = %08x\n", dc390_laststatus);
1876 dc390_ResetDevParam( pACB );
1877 dc390_DoingSRB_Done( pACB,
NULL);
1885 static int DC390_queuecommand_lck(
struct scsi_cmnd *cmd,
1916 srb->AdaptStatus = 0;
1917 srb->TargetStatus = 0;
1923 srb->TotalXferredLen = 0;
1925 srb->SGToBeXferLen = 0;
1927 srb->EndMessage = 0;
1930 if (dc390_StartSCSI(acb, dcb,
srb)) {
1931 dc390_Free_insert(acb,
srb);
1935 dc390_Going_append(dcb,
srb);
1953 if (!pDCB) pDCB = pACB->pActiveDCB;
1954 if (!pSRB && pDCB) pSRB = pDCB->pActiveSRB;
1958 printk (
"DC390: SRB: Xferred %08lx, Remain %08lx, State %08x, Phase %02x\n",
1959 pSRB->TotalXferredLen, pSRB->SGToBeXferLen, pSRB->SRBState,
1961 printk (
"DC390: AdpaterStatus: %02x, SRB Status %02x\n", pSRB->AdaptStatus, pSRB->SRBStatus);
1963 printk (
"DC390: Status of last IRQ (DMA/SC/Int/IRQ): %08x\n", dc390_laststatus);
1964 printk (
"DC390: Register dump: SCSI block:\n");
1965 printk (
"DC390: XferCnt Cmd Stat IntS IRQS FFIS Ctl1 Ctl2 Ctl3 Ctl4\n");
1966 printk (
"DC390: %06x %02x %02x %02x",
1969 printk (
" %02x %02x %02x %02x %02x %02x\n",
1979 printk (
"DC390: Register dump: DMA engine:\n");
1980 printk (
"DC390: Cmd STrCnt SBusA WrkBC WrkAC Stat SBusCtrl\n");
1981 printk (
"DC390: %02x %08x %08x %08x %08x %02x %08x\n",
1988 pci_read_config_word(pdev,
PCI_STATUS, &pstat);
1989 printk (
"DC390: Register dump: PCI Status: %04x\n", pstat);
1990 printk (
"DC390: In case of driver trouble read Documentation/scsi/tmscsim.txt\n");
1994 static int DC390_abort(
struct scsi_cmnd *cmd)
2004 dc390_dumpinfo(pACB, pDCB,
NULL);
2013 static void dc390_ResetDevParam(
struct dc390_acb* pACB )
2031 while( pdcb != pDCB );
2036 static int DC390_bus_reset (
struct scsi_cmnd *cmd)
2041 spin_lock_irq(cmd->
device->host->host_lock);
2047 dc390_ResetSCSIBus(pACB);
2049 dc390_ResetDevParam(pACB);
2057 dc390_DoingSRB_Done(pACB, cmd);
2065 spin_unlock_irq(cmd->
device->host->host_lock);
2080 uint id = scsi_device->
id;
2105 if (lun && (pDCB2 = dc390_findDCB(pACB,
id, 0))) {
2106 pDCB->
DevMode = pDCB2->DevMode;
2112 pDCB->
CtrlR3 = pDCB2->CtrlR3;
2113 pDCB->
CtrlR4 = pDCB2->CtrlR4;
2120 (dc390_clock_period1[prom->
EE_SPEED] * 25) >> 2;
2149 static void dc390_slave_destroy(
struct scsi_device *scsi_device)
2184 static int dc390_slave_configure(
struct scsi_device *sdev)
2192 scsi_activate_tcq(sdev, acb->
TagMaxNum);
2200 .proc_name =
"tmscsim",
2202 .slave_alloc = dc390_slave_alloc,
2203 .slave_configure = dc390_slave_configure,
2204 .slave_destroy = dc390_slave_destroy,
2205 .queuecommand = DC390_queuecommand,
2206 .eh_abort_handler = DC390_abort,
2207 .eh_bus_reset_handler = DC390_bus_reset,
2213 .max_sectors = 0x4000,
2224 u8 carryFlag = 1,
j = 0x80, bval;
2227 for (i = 0; i < 9; i++) {
2229 pci_write_config_byte(pdev, 0x80, 0x40);
2235 pci_write_config_byte(pdev, 0x80, bval);
2237 pci_write_config_byte(pdev, 0x80, 0);
2240 carryFlag = (cmd &
j) ? 1 : 0;
2251 for (i = 0; i < 16; i++) {
2254 pci_write_config_byte(pdev, 0x80, 0x80);
2256 pci_write_config_byte(pdev, 0x80, 0x40);
2258 pci_read_config_byte(pdev, 0x00, &bval);
2271 for (i = 0; i < 0x40; i++) {
2272 pci_write_config_byte(pdev, 0xc0, 0);
2275 dc390_eeprom_prepare_read(pdev, cmd++);
2276 *ptr++ = dc390_eeprom_get_data(pdev);
2278 pci_write_config_byte(pdev, 0x80, 0);
2279 pci_write_config_byte(pdev, 0x80, 0);
2285 static void __devinit dc390_eeprom_override(
u8 index)
2287 u8 *ptr = (
u8 *) dc390_eepromBuf[index],
id;
2290 if (tmscsim[0] != -2)
2292 if (tmscsim[3] != -2)
2294 if (tmscsim[5] != -2)
2296 if (tmscsim[4] != -2)
2301 if (tmscsim[2] != -2)
2302 ptr[
id << 2] = (
u8)tmscsim[2];
2303 if (tmscsim[1] != -2)
2304 ptr[(
id << 2) + 1] = (
u8)tmscsim[1];
2318 static void __devinit dc390_fill_with_defaults (
void)
2322 for (i = 0; i < 6; i++) {
2323 if (tmscsim[i] < 0 || tmscsim[i] > 255)
2324 tmscsim[
i] = tmscsim_def[
i];
2334 if (tmscsim[5] > 180)
2340 u8 interpd[] = {1, 3, 5, 10, 16, 30, 60, 120};
2342 u16 *ptr = (
u16 *)EEbuf, wval = 0;
2345 dc390_read_eeprom(pdev, ptr);
2352 for (i = 0; i < 0x40; i++, ptr++)
2356 if (wval != 0x1234) {
2359 printk(
KERN_INFO "DC390_init: No EEPROM found! Trying default settings ...\n");
2365 dc390_fill_with_defaults();
2367 speed = dc390_clock_speed[tmscsim[1]];
2368 printk(
KERN_INFO "DC390: Used defaults: AdaptID=%i, SpeedIdx=%i (%i.%i MHz), "
2369 "DevMode=0x%02x, AdaptMode=0x%02x, TaggedCmnds=%i (%i), DelayReset=%is\n",
2370 tmscsim[0], tmscsim[1], speed / 10, speed % 10,
2371 (
u8)tmscsim[2], (
u8)tmscsim[3], tmscsim[4], 2 << (tmscsim[4]), tmscsim[5]);
2384 dc390_ResetSCSIBus(pACB);
2439 if (disable_clustering)
2443 goto out_disable_device;
2445 pACB = (
struct dc390_acb *)shost->hostdata;
2448 dc390_check_eeprom(pdev, dc390_adapterCnt);
2449 dc390_eeprom_override(dc390_adapterCnt);
2483 for (i = 0; i < pACB->
SRBCount-1; i++)
2503 goto out_release_region;
2506 dc390_init_hw(pACB, dc390_adapterCnt);
2510 pci_set_drvdata(pdev, shost);
2512 error = scsi_add_host(shost, &pdev->
dev);
2537 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
2538 unsigned long iflags;
2549 dc390_ResetSCSIBus(pACB);
2550 spin_unlock_irqrestore(scsi_host->
host_lock, iflags);
2557 pci_set_drvdata(dev,
NULL);
2569 .id_table = tmscsim_pci_tbl,
2570 .probe = dc390_probe_one,
2574 static int __init dc390_module_init(
void)
2576 if (!disable_clustering) {
2577 printk(
KERN_INFO "DC390: clustering now enabled by default. If you get problems load\n");
2578 printk(
KERN_INFO " with \"disable_clustering=1\" and report to maintainers\n");
2581 if (tmscsim[0] == -1 || tmscsim[0] > 15) {
2591 return pci_register_driver(&dc390_driver);
2594 static void __exit dc390_module_exit(
void)
2603 static int __init dc390_setup (
char *
str)
2615 for (i = 0; i < im; i++)
2616 tmscsim[i] = ints[i+1];
2621 __setup(
"tmscsim=", dc390_setup);