1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191#define C_NOP
192
193
194#ifdef DC390_DEBUG0
195# define DEBUG0(x) x
196#else
197# define DEBUG0(x) C_NOP
198#endif
199#ifdef DC390_DEBUG1
200# define DEBUG1(x) x
201#else
202# define DEBUG1(x) C_NOP
203#endif
204#ifdef DC390_DCBDEBUG
205# define DCBDEBUG(x) x
206#else
207# define DCBDEBUG(x) C_NOP
208#endif
209#ifdef DC390_PARSEDEBUG
210# define PARSEDEBUG(x) x
211#else
212# define PARSEDEBUG(x) C_NOP
213#endif
214#ifdef DC390_REMOVABLEDEBUG
215# define REMOVABLEDEBUG(x) x
216#else
217# define REMOVABLEDEBUG(x) C_NOP
218#endif
219#define DCBDEBUG1(x) C_NOP
220
221#include <linux/module.h>
222#include <linux/delay.h>
223#include <linux/signal.h>
224#include <linux/errno.h>
225#include <linux/kernel.h>
226#include <linux/ioport.h>
227#include <linux/pci.h>
228#include <linux/proc_fs.h>
229#include <linux/string.h>
230#include <linux/mm.h>
231#include <linux/blkdev.h>
232#include <linux/timer.h>
233#include <linux/interrupt.h>
234#include <linux/init.h>
235#include <linux/spinlock.h>
236#include <asm/io.h>
237
238#include <scsi/scsi.h>
239#include <scsi/scsi_cmnd.h>
240#include <scsi/scsi_device.h>
241#include <scsi/scsi_host.h>
242#include <scsi/scsicam.h>
243#include <scsi/scsi_tcq.h>
244
245
246#define DC390_BANNER "Tekram DC390/AM53C974"
247#define DC390_VERSION "2.1d 2004-05-27"
248
249#define PCI_DEVICE_ID_AMD53C974 PCI_DEVICE_ID_AMD_SCSI
250
251#include "tmscsim.h"
252
253
254static void dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
255static void dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
256static void dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
257static void dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
258static void dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
259static void dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
260static void dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
261static void dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
262static void dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
263static void dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
264static void dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
265static void dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
266static void dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
267static void dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
268
269static void dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB );
270static void dc390_Disconnect( struct dc390_acb* pACB );
271static void dc390_Reselect( struct dc390_acb* pACB );
272static void dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB );
273static void dc390_ScsiRstDetect( struct dc390_acb* pACB );
274static void dc390_EnableMsgOut_Abort(struct dc390_acb*, struct dc390_srb*);
275static void dc390_dumpinfo(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB);
276static void dc390_ResetDevParam(struct dc390_acb* pACB);
277
278static u32 dc390_laststatus = 0;
279static u8 dc390_adapterCnt = 0;
280
281static int disable_clustering;
282module_param(disable_clustering, int, S_IRUGO);
283MODULE_PARM_DESC(disable_clustering, "If you experience problems with your devices, try setting to 1");
284
285
286static int tmscsim[] = {-2, -2, -2, -2, -2, -2};
287
288module_param_array(tmscsim, int, NULL, 0);
289MODULE_PARM_DESC(tmscsim, "Host SCSI ID, Speed (0=10MHz), Device Flags, Adapter Flags, Max Tags (log2(tags)-1), DelayReset (s)");
290MODULE_AUTHOR("C.L. Huang / Kurt Garloff");
291MODULE_DESCRIPTION("SCSI host adapter driver for Tekram DC390 and other AMD53C974A based PCI SCSI adapters");
292MODULE_LICENSE("GPL");
293MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
294
295static void *dc390_phase0[]={
296 dc390_DataOut_0,
297 dc390_DataIn_0,
298 dc390_Command_0,
299 dc390_Status_0,
300 dc390_Nop_0,
301 dc390_Nop_0,
302 dc390_MsgOut_0,
303 dc390_MsgIn_0,
304 dc390_Nop_1
305 };
306
307static void *dc390_phase1[]={
308 dc390_DataOutPhase,
309 dc390_DataInPhase,
310 dc390_CommandPhase,
311 dc390_StatusPhase,
312 dc390_Nop_0,
313 dc390_Nop_0,
314 dc390_MsgOutPhase,
315 dc390_MsgInPhase,
316 dc390_Nop_1
317 };
318
319#ifdef DC390_DEBUG1
320static char* dc390_p0_str[] = {
321 "dc390_DataOut_0",
322 "dc390_DataIn_0",
323 "dc390_Command_0",
324 "dc390_Status_0",
325 "dc390_Nop_0",
326 "dc390_Nop_0",
327 "dc390_MsgOut_0",
328 "dc390_MsgIn_0",
329 "dc390_Nop_1"
330 };
331
332static char* dc390_p1_str[] = {
333 "dc390_DataOutPhase",
334 "dc390_DataInPhase",
335 "dc390_CommandPhase",
336 "dc390_StatusPhase",
337 "dc390_Nop_0",
338 "dc390_Nop_0",
339 "dc390_MsgOutPhase",
340 "dc390_MsgInPhase",
341 "dc390_Nop_1"
342 };
343#endif
344
345static u8 dc390_eepromBuf[MAX_ADAPTER_NUM][EE_LEN];
346static u8 dc390_clock_period1[] = {4, 5, 6, 7, 8, 10, 13, 20};
347static u8 dc390_clock_speed[] = {100,80,67,57,50, 40, 31, 20};
348
349
350
351
352
353
354static void inline dc390_start_segment(struct dc390_srb* pSRB)
355{
356 struct scatterlist *psgl = pSRB->pSegmentList;
357
358
359 pSRB->SGBusAddr = sg_dma_address(psgl);
360 pSRB->SGToBeXferLen = sg_dma_len(psgl);
361}
362
363static unsigned long inline dc390_advance_segment(struct dc390_srb* pSRB, u32 residue)
364{
365 unsigned long xfer = pSRB->SGToBeXferLen - residue;
366
367
368 pSRB->SGBusAddr += xfer;
369 pSRB->TotalXferredLen += xfer;
370 pSRB->SGToBeXferLen = residue;
371
372 return xfer;
373}
374
375static struct dc390_dcb __inline__ *dc390_findDCB ( struct dc390_acb* pACB, u8 id, u8 lun)
376{
377 struct dc390_dcb* pDCB = pACB->pLinkDCB; if (!pDCB) return NULL;
378 while (pDCB->TargetID != id || pDCB->TargetLUN != lun)
379 {
380 pDCB = pDCB->pNextDCB;
381 if (pDCB == pACB->pLinkDCB)
382 return NULL;
383 }
384 DCBDEBUG1( printk (KERN_DEBUG "DCB %p (%02x,%02x) found.\n", \
385 pDCB, pDCB->TargetID, pDCB->TargetLUN));
386 return pDCB;
387}
388
389
390static __inline__ void dc390_Free_insert (struct dc390_acb* pACB, struct dc390_srb* pSRB)
391{
392 DEBUG0(printk ("DC390: Free SRB %p\n", pSRB));
393 pSRB->pNextSRB = pACB->pFreeSRB;
394 pACB->pFreeSRB = pSRB;
395}
396
397static __inline__ void dc390_Going_append (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
398{
399 pDCB->GoingSRBCnt++;
400 DEBUG0(printk("DC390: Append SRB %p to Going\n", pSRB));
401
402 if( pDCB->pGoingSRB )
403 pDCB->pGoingLast->pNextSRB = pSRB;
404 else
405 pDCB->pGoingSRB = pSRB;
406
407 pDCB->pGoingLast = pSRB;
408
409 pSRB->pNextSRB = NULL;
410}
411
412static __inline__ void dc390_Going_remove (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
413{
414 DEBUG0(printk("DC390: Remove SRB %p from Going\n", pSRB));
415 if (pSRB == pDCB->pGoingSRB)
416 pDCB->pGoingSRB = pSRB->pNextSRB;
417 else
418 {
419 struct dc390_srb* psrb = pDCB->pGoingSRB;
420 while (psrb && psrb->pNextSRB != pSRB)
421 psrb = psrb->pNextSRB;
422 if (!psrb)
423 { printk (KERN_ERR "DC390: Remove non-ex. SRB %p from Going!\n", pSRB); return; }
424 psrb->pNextSRB = pSRB->pNextSRB;
425 if (pSRB == pDCB->pGoingLast)
426 pDCB->pGoingLast = psrb;
427 }
428 pDCB->GoingSRBCnt--;
429}
430
431static struct scatterlist* dc390_sg_build_single(struct scatterlist *sg, void *addr, unsigned int length)
432{
433 sg_init_one(sg, addr, length);
434 return sg;
435}
436
437
438static int dc390_pci_map (struct dc390_srb* pSRB)
439{
440 int error = 0;
441 struct scsi_cmnd *pcmd = pSRB->pcmd;
442 struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
443 dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp));
444
445
446 if (pSRB->SRBFlag & AUTO_REQSENSE) {
447 pSRB->pSegmentList = dc390_sg_build_single(&pSRB->Segmentx, pcmd->sense_buffer, sizeof(pcmd->sense_buffer));
448 pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, 1,
449 DMA_FROM_DEVICE);
450 cmdp->saved_dma_handle = sg_dma_address(pSRB->pSegmentList);
451
452
453 if (pSRB->SGcount != 1)
454 error = 1;
455 DEBUG1(printk("%s(): Mapped sense buffer %p at %x\n", __FUNCTION__, pcmd->sense_buffer, cmdp->saved_dma_handle));
456
457 } else if (scsi_sg_count(pcmd)) {
458 int nseg;
459
460 nseg = scsi_dma_map(pcmd);
461
462 pSRB->pSegmentList = scsi_sglist(pcmd);
463 pSRB->SGcount = nseg;
464
465
466 if (nseg < 0)
467 error = 1;
468 DEBUG1(printk("%s(): Mapped SG %p with %d (%d) elements\n",\
469 __FUNCTION__, scsi_sglist(pcmd), nseg, scsi_sg_count(pcmd)));
470
471 } else
472 pSRB->SGcount = 0;
473
474 return error;
475}
476
477
478static void dc390_pci_unmap (struct dc390_srb* pSRB)
479{
480 struct scsi_cmnd *pcmd = pSRB->pcmd;
481 struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
482 DEBUG1(dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp)));
483
484 if (pSRB->SRBFlag) {
485 pci_unmap_sg(pdev, &pSRB->Segmentx, 1, DMA_FROM_DEVICE);
486 DEBUG1(printk("%s(): Unmapped sense buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
487 } else {
488 scsi_dma_unmap(pcmd);
489 DEBUG1(printk("%s(): Unmapped SG at %p with %d elements\n",
490 __FUNCTION__, scsi_sglist(pcmd), scsi_sg_count(pcmd)));
491 }
492}
493
494static void __inline__
495dc390_freetag (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
496{
497 if (pSRB->TagNumber != SCSI_NO_TAG) {
498 pDCB->TagMask &= ~(1 << pSRB->TagNumber);
499 pSRB->TagNumber = SCSI_NO_TAG;
500 }
501}
502
503
504static int
505dc390_StartSCSI( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
506{
507 struct scsi_cmnd *scmd = pSRB->pcmd;
508 struct scsi_device *sdev = scmd->device;
509 u8 cmd, disc_allowed, try_sync_nego;
510 char tag[2];
511
512 pSRB->ScsiPhase = SCSI_NOP0;
513
514 if (pACB->Connected)
515 {
516
517 printk (KERN_WARNING "DC390: Can't select when connected! (%08x,%02x)\n",
518 pSRB->SRBState, pSRB->SRBFlag);
519 pSRB->SRBState = SRB_READY;
520 pACB->SelConn++;
521 return 1;
522 }
523 if (time_before (jiffies, pACB->pScsiHost->last_reset))
524 {
525 DEBUG0(printk ("DC390: We were just reset and don't accept commands yet!\n"));
526 return 1;
527 }
528
529 dc390_pci_map(pSRB);
530
531 DC390_write8 (Scsi_Dest_ID, pDCB->TargetID);
532 DC390_write8 (Sync_Period, pDCB->SyncPeriod);
533 DC390_write8 (Sync_Offset, pDCB->SyncOffset);
534 DC390_write8 (CtrlReg1, pDCB->CtrlR1);
535 DC390_write8 (CtrlReg3, pDCB->CtrlR3);
536 DC390_write8 (CtrlReg4, pDCB->CtrlR4);
537 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
538 DEBUG1(printk (KERN_INFO "DC390: Start SCSI command: %02x (Sync:%02x)\n",\
539 scmd->cmnd[0], pDCB->SyncMode));
540
541
542
543
544
545
546 if (! (pSRB->SRBFlag & AUTO_REQSENSE))
547 disc_allowed = pDCB->DevMode & EN_DISCONNECT_;
548 else
549 disc_allowed = 0;
550
551 if ((pDCB->SyncMode & SYNC_ENABLE) && pDCB->TargetLUN == 0 && sdev->sdtr &&
552 (((scmd->cmnd[0] == REQUEST_SENSE || (pSRB->SRBFlag & AUTO_REQSENSE)) &&
553 !(pDCB->SyncMode & SYNC_NEGO_DONE)) || scmd->cmnd[0] == INQUIRY))
554 try_sync_nego = 1;
555 else
556 try_sync_nego = 0;
557
558 pSRB->MsgCnt = 0;
559 cmd = SEL_W_ATN;
560 DC390_write8 (ScsiFifo, IDENTIFY(disc_allowed, pDCB->TargetLUN));
561
562 if ((pDCB->SyncMode & EN_TAG_QUEUEING) && disc_allowed && scsi_populate_tag_msg(scmd, tag)) {
563 DC390_write8(ScsiFifo, tag[0]);
564 pDCB->TagMask |= 1 << tag[1];
565 pSRB->TagNumber = tag[1];
566 DC390_write8(ScsiFifo, tag[1]);
567 DEBUG1(printk(KERN_INFO "DC390: Select w/DisCn for Cmd %li (SRB %p), block tag %02x\n", scmd->serial_number, pSRB, tag[1]));
568 cmd = SEL_W_ATN3;
569 } else {
570
571
572 DEBUG1(printk(KERN_INFO "DC390: Select w%s/DisCn for Cmd %li (SRB %p), No TagQ\n", disc_allowed ? "" : "o", scmd->serial_number, pSRB));
573 }
574
575 pSRB->SRBState = SRB_START_;
576
577 if (try_sync_nego)
578 {
579 u8 Sync_Off = pDCB->SyncOffset;
580 DEBUG0(printk (KERN_INFO "DC390: NEW Sync Nego code triggered (%i %i)\n", pDCB->TargetID, pDCB->TargetLUN));
581 pSRB->MsgOutBuf[0] = EXTENDED_MESSAGE;
582 pSRB->MsgOutBuf[1] = 3;
583 pSRB->MsgOutBuf[2] = EXTENDED_SDTR;
584 pSRB->MsgOutBuf[3] = pDCB->NegoPeriod;
585 if (!(Sync_Off & 0x0f)) Sync_Off = SYNC_NEGO_OFFSET;
586 pSRB->MsgOutBuf[4] = Sync_Off;
587 pSRB->MsgCnt = 5;
588
589 pSRB->SRBState |= DO_SYNC_NEGO;
590 cmd = SEL_W_ATN_STOP;
591 }
592
593
594 if (cmd != SEL_W_ATN_STOP)
595 {
596 if( pSRB->SRBFlag & AUTO_REQSENSE )
597 {
598 DC390_write8 (ScsiFifo, REQUEST_SENSE);
599 DC390_write8 (ScsiFifo, pDCB->TargetLUN << 5);
600 DC390_write8 (ScsiFifo, 0);
601 DC390_write8 (ScsiFifo, 0);
602 DC390_write8 (ScsiFifo, sizeof(scmd->sense_buffer));
603 DC390_write8 (ScsiFifo, 0);
604 DEBUG1(printk (KERN_DEBUG "DC390: AutoReqSense !\n"));
605 }
606 else
607 {
608 u8 *ptr; u8 i;
609 ptr = (u8 *)scmd->cmnd;
610 for (i = 0; i < scmd->cmd_len; i++)
611 DC390_write8 (ScsiFifo, *(ptr++));
612 }
613 }
614 DEBUG0(if (pACB->pActiveDCB) \
615 printk (KERN_WARNING "DC390: ActiveDCB != 0\n"));
616 DEBUG0(if (pDCB->pActiveSRB) \
617 printk (KERN_WARNING "DC390: ActiveSRB != 0\n"));
618
619 if (DC390_read8 (Scsi_Status) & INTERRUPT)
620 {
621 dc390_freetag (pDCB, pSRB);
622 DEBUG0(printk ("DC390: Interrupt during Start SCSI (pid %li, target %02i-%02i)\n",
623 scmd->serial_number, scmd->device->id, scmd->device->lun));
624 pSRB->SRBState = SRB_READY;
625
626 pACB->SelLost++;
627 return 1;
628 }
629 DC390_write8 (ScsiCmd, cmd);
630 pACB->pActiveDCB = pDCB;
631 pDCB->pActiveSRB = pSRB;
632 pACB->Connected = 1;
633 pSRB->ScsiPhase = SCSI_NOP1;
634 return 0;
635}
636
637
638static void __inline__
639dc390_InvalidCmd(struct dc390_acb* pACB)
640{
641 if (pACB->pActiveDCB->pActiveSRB->SRBState & (SRB_START_ | SRB_MSGOUT))
642 DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
643}
644
645
646static irqreturn_t __inline__
647DC390_Interrupt(void *dev_id)
648{
649 struct dc390_acb *pACB = dev_id;
650 struct dc390_dcb *pDCB;
651 struct dc390_srb *pSRB;
652 u8 sstatus=0;
653 u8 phase;
654 void (*stateV)( struct dc390_acb*, struct dc390_srb*, u8 *);
655 u8 istate, istatus;
656
657 sstatus = DC390_read8 (Scsi_Status);
658 if( !(sstatus & INTERRUPT) )
659 return IRQ_NONE;
660
661 DEBUG1(printk (KERN_DEBUG "sstatus=%02x,", sstatus));
662
663
664
665
666
667 spin_lock_irq(pACB->pScsiHost->host_lock);
668
669 istate = DC390_read8 (Intern_State);
670 istatus = DC390_read8 (INT_Status);
671
672 DEBUG1(printk (KERN_INFO "Istatus(Res,Inv,Dis,Serv,Succ,ReS,SelA,Sel)=%02x,",istatus));
673 dc390_laststatus &= ~0x00ffffff;
674 dc390_laststatus |= sstatus<<16 | istate<<8 | istatus;
675
676 if (sstatus & ILLEGAL_OP_ERR)
677 {
678 printk ("DC390: Illegal Operation detected (%08x)!\n", dc390_laststatus);
679 dc390_dumpinfo (pACB, pACB->pActiveDCB, pACB->pActiveDCB->pActiveSRB);
680 }
681
682 else if (istatus & INVALID_CMD)
683 {
684 printk ("DC390: Invalid Command detected (%08x)!\n", dc390_laststatus);
685 dc390_InvalidCmd( pACB );
686 goto unlock;
687 }
688
689 if (istatus & SCSI_RESET)
690 {
691 dc390_ScsiRstDetect( pACB );
692 goto unlock;
693 }
694
695 if (istatus & DISCONNECTED)
696 {
697 dc390_Disconnect( pACB );
698 goto unlock;
699 }
700
701 if (istatus & RESELECTED)
702 {
703 dc390_Reselect( pACB );
704 goto unlock;
705 }
706
707 else if (istatus & (SELECTED | SEL_ATTENTION))
708 {
709 printk (KERN_ERR "DC390: Target mode not supported!\n");
710 goto unlock;
711 }
712
713 if (istatus & (SUCCESSFUL_OP|SERVICE_REQUEST) )
714 {
715 pDCB = pACB->pActiveDCB;
716 if (!pDCB)
717 {
718 printk (KERN_ERR "DC390: Suc. op/ Serv. req: pActiveDCB = 0!\n");
719 goto unlock;
720 }
721 pSRB = pDCB->pActiveSRB;
722 if( pDCB->DCBFlag & ABORT_DEV_ )
723 dc390_EnableMsgOut_Abort (pACB, pSRB);
724
725 phase = pSRB->ScsiPhase;
726 DEBUG1(printk (KERN_INFO "DC390: [%i]%s(0) (%02x)\n", phase, dc390_p0_str[phase], sstatus));
727 stateV = (void *) dc390_phase0[phase];
728 ( *stateV )( pACB, pSRB, &sstatus );
729
730 pSRB->ScsiPhase = sstatus & 7;
731 phase = (u8) sstatus & 7;
732 DEBUG1(printk (KERN_INFO "DC390: [%i]%s(1) (%02x)\n", phase, dc390_p1_str[phase], sstatus));
733 stateV = (void *) dc390_phase1[phase];
734 ( *stateV )( pACB, pSRB, &sstatus );
735 }
736
737 unlock:
738 spin_unlock_irq(pACB->pScsiHost->host_lock);
739 return IRQ_HANDLED;
740}
741
742static irqreturn_t do_DC390_Interrupt(int irq, void *dev_id)
743{
744 irqreturn_t ret;
745 DEBUG1(printk (KERN_INFO "DC390: Irq (%i) caught: ", irq));
746
747 ret = DC390_Interrupt(dev_id);
748 DEBUG1(printk (".. IRQ returned\n"));
749 return ret;
750}
751
752static void
753dc390_DataOut_0(struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
754{
755 u8 sstatus;
756 u32 ResidCnt;
757 u8 dstate = 0;
758
759 sstatus = *psstatus;
760
761 if( !(pSRB->SRBState & SRB_XFERPAD) )
762 {
763 if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR) )
764 pSRB->SRBStatus |= PARITY_ERROR;
765
766 if( sstatus & COUNT_2_ZERO )
767 {
768 unsigned long timeout = jiffies + HZ;
769
770
771 if (pSRB->SGToBeXferLen)
772 while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) {
773 spin_unlock_irq(pACB->pScsiHost->host_lock);
774 udelay(50);
775 spin_lock_irq(pACB->pScsiHost->host_lock);
776 }
777 if (!time_before(jiffies, timeout))
778 printk (KERN_CRIT "DC390: Deadlock in DataOut_0: DMA aborted unfinished: %06x bytes remain!!\n",
779 DC390_read32 (DMA_Wk_ByteCntr));
780 dc390_laststatus &= ~0xff000000;
781 dc390_laststatus |= dstate << 24;
782 pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
783 pSRB->SGIndex++;
784 if( pSRB->SGIndex < pSRB->SGcount )
785 {
786 pSRB->pSegmentList++;
787
788 dc390_start_segment(pSRB);
789 }
790 else
791 pSRB->SGToBeXferLen = 0;
792 }
793 else
794 {
795 ResidCnt = ((u32) DC390_read8 (Current_Fifo) & 0x1f) +
796 (((u32) DC390_read8 (CtcReg_High) << 16) |
797 ((u32) DC390_read8 (CtcReg_Mid) << 8) |
798 (u32) DC390_read8 (CtcReg_Low));
799
800 dc390_advance_segment(pSRB, ResidCnt);
801 }
802 }
803 if ((*psstatus & 7) != SCSI_DATA_OUT)
804 {
805 DC390_write8 (DMA_Cmd, WRITE_DIRECTION+DMA_IDLE_CMD);
806 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
807 }
808}
809
810static void
811dc390_DataIn_0(struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
812{
813 u8 sstatus, residual, bval;
814 u32 ResidCnt, i;
815 unsigned long xferCnt;
816
817 sstatus = *psstatus;
818
819 if( !(pSRB->SRBState & SRB_XFERPAD) )
820 {
821 if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR))
822 pSRB->SRBStatus |= PARITY_ERROR;
823
824 if( sstatus & COUNT_2_ZERO )
825 {
826 int dstate = 0;
827 unsigned long timeout = jiffies + HZ;
828
829
830 if (pSRB->SGToBeXferLen)
831 while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) {
832 spin_unlock_irq(pACB->pScsiHost->host_lock);
833 udelay(50);
834 spin_lock_irq(pACB->pScsiHost->host_lock);
835 }
836 if (!time_before(jiffies, timeout)) {
837 printk (KERN_CRIT "DC390: Deadlock in DataIn_0: DMA aborted unfinished: %06x bytes remain!!\n",
838 DC390_read32 (DMA_Wk_ByteCntr));
839 printk (KERN_CRIT "DC390: DataIn_0: DMA State: %i\n", dstate);
840 }
841 dc390_laststatus &= ~0xff000000;
842 dc390_laststatus |= dstate << 24;
843 DEBUG1(ResidCnt = ((unsigned long) DC390_read8 (CtcReg_High) << 16) \
844 + ((unsigned long) DC390_read8 (CtcReg_Mid) << 8) \
845 + ((unsigned long) DC390_read8 (CtcReg_Low)));
846 DEBUG1(printk (KERN_DEBUG "Count_2_Zero (ResidCnt=%u,ToBeXfer=%lu),", ResidCnt, pSRB->SGToBeXferLen));
847
848 DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD);
849
850 pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
851 pSRB->SGIndex++;
852 if( pSRB->SGIndex < pSRB->SGcount )
853 {
854 pSRB->pSegmentList++;
855
856 dc390_start_segment(pSRB);
857 }
858 else
859 pSRB->SGToBeXferLen = 0;
860 }
861 else
862 {
863 residual = 0;
864 bval = DC390_read8 (Current_Fifo);
865 while( bval & 0x1f )
866 {
867 DEBUG1(printk (KERN_DEBUG "Check for residuals,"));
868 if( (bval & 0x1f) == 1 )
869 {
870 for(i=0; i < 0x100; i++)
871 {
872 bval = DC390_read8 (Current_Fifo);
873 if( !(bval & 0x1f) )
874 goto din_1;
875 else if( i == 0x0ff )
876 {
877 residual = 1;
878 goto din_1;
879 }
880 }
881 }
882 else
883 bval = DC390_read8 (Current_Fifo);
884 }
885din_1:
886 DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_BLAST_CMD);
887 for (i = 0xa000; i; i--)
888 {
889 bval = DC390_read8 (DMA_Status);
890 if (bval & BLAST_COMPLETE)
891 break;
892 }
893
894 if (!i) printk (KERN_ERR "DC390: DMA Blast aborted unfinished!\n");
895
896 dc390_laststatus &= ~0xff000000;
897 dc390_laststatus |= bval << 24;
898
899 DEBUG1(printk (KERN_DEBUG "Blast: Read %i times DMA_Status %02x", 0xa000-i, bval));
900 ResidCnt = (((u32) DC390_read8 (CtcReg_High) << 16) |
901 ((u32) DC390_read8 (CtcReg_Mid) << 8)) |
902 (u32) DC390_read8 (CtcReg_Low);
903
904 xferCnt = dc390_advance_segment(pSRB, ResidCnt);
905
906 if (residual) {
907 size_t count = 1;
908 size_t offset = pSRB->SGBusAddr - sg_dma_address(pSRB->pSegmentList);
909 unsigned long flags;
910 u8 *ptr;
911
912 bval = DC390_read8 (ScsiFifo);
913
914 local_irq_save(flags);
915 ptr = scsi_kmap_atomic_sg(pSRB->pSegmentList, pSRB->SGcount, &offset, &count);
916 if (likely(ptr)) {
917 *(ptr + offset) = bval;
918 scsi_kunmap_atomic_sg(ptr);
919 }
920 local_irq_restore(flags);
921 WARN_ON(!ptr);
922
923
924 xferCnt += dc390_advance_segment(pSRB, pSRB->SGToBeXferLen - 1);
925 }
926 DEBUG1(printk (KERN_DEBUG "Xfered: %lu, Total: %lu, Remaining: %lu\n", xferCnt,\
927 pSRB->TotalXferredLen, pSRB->SGToBeXferLen));
928 }
929 }
930 if ((*psstatus & 7) != SCSI_DATA_IN)
931 {
932 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
933 DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD);
934 }
935}
936
937static void
938dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
939{
940}
941
942static void
943dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
944{
945
946 pSRB->TargetStatus = DC390_read8 (ScsiFifo);
947
948 pSRB->EndMessage = DC390_read8 (ScsiFifo);
949
950 *psstatus = SCSI_NOP0;
951 pSRB->SRBState = SRB_COMPLETED;
952 DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);
953}
954
955static void
956dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
957{
958 if( pSRB->SRBState & (SRB_UNEXPECT_RESEL+SRB_ABORT_SENT) )
959 *psstatus = SCSI_NOP0;
960
961}
962
963
964static void __inline__
965dc390_reprog (struct dc390_acb* pACB, struct dc390_dcb* pDCB)
966{
967 DC390_write8 (Sync_Period, pDCB->SyncPeriod);
968 DC390_write8 (Sync_Offset, pDCB->SyncOffset);
969 DC390_write8 (CtrlReg3, pDCB->CtrlR3);
970 DC390_write8 (CtrlReg4, pDCB->CtrlR4);
971 dc390_SetXferRate (pACB, pDCB);
972}
973
974
975#ifdef DC390_DEBUG0
976static void
977dc390_printMsg (u8 *MsgBuf, u8 len)
978{
979 int i;
980 printk (" %02x", MsgBuf[0]);
981 for (i = 1; i < len; i++)
982 printk (" %02x", MsgBuf[i]);
983 printk ("\n");
984}
985#endif
986
987#define DC390_ENABLE_MSGOUT DC390_write8 (ScsiCmd, SET_ATN_CMD)
988
989
990static void __inline__
991dc390_MsgIn_reject (struct dc390_acb* pACB, struct dc390_srb* pSRB)
992{
993 pSRB->MsgOutBuf[0] = MESSAGE_REJECT;
994 pSRB->MsgCnt = 1;
995 DC390_ENABLE_MSGOUT;
996 DEBUG0 (printk (KERN_INFO "DC390: Reject message\n"));
997}
998
999
1000static void
1001dc390_EnableMsgOut_Abort ( struct dc390_acb* pACB, struct dc390_srb* pSRB )
1002{
1003 pSRB->MsgOutBuf[0] = ABORT;
1004 pSRB->MsgCnt = 1; DC390_ENABLE_MSGOUT;
1005 pSRB->pSRBDCB->DCBFlag &= ~ABORT_DEV_;
1006}
1007
1008static struct dc390_srb*
1009dc390_MsgIn_QTag (struct dc390_acb* pACB, struct dc390_dcb* pDCB, s8 tag)
1010{
1011 struct dc390_srb* pSRB = pDCB->pGoingSRB;
1012
1013 if (pSRB)
1014 {
1015 struct scsi_cmnd *scmd = scsi_find_tag(pSRB->pcmd->device, tag);
1016 pSRB = (struct dc390_srb *)scmd->host_scribble;
1017
1018 if (pDCB->DCBFlag & ABORT_DEV_)
1019 {
1020 pSRB->SRBState = SRB_ABORT_SENT;
1021 dc390_EnableMsgOut_Abort( pACB, pSRB );
1022 }
1023
1024 if (!(pSRB->SRBState & SRB_DISCONNECT))
1025 goto mingx0;
1026
1027 pDCB->pActiveSRB = pSRB;
1028 pSRB->SRBState = SRB_DATA_XFER;
1029 }
1030 else
1031 {
1032 mingx0:
1033 pSRB = pACB->pTmpSRB;
1034 pSRB->SRBState = SRB_UNEXPECT_RESEL;
1035 pDCB->pActiveSRB = pSRB;
1036 pSRB->MsgOutBuf[0] = ABORT_TAG;
1037 pSRB->MsgCnt = 1; DC390_ENABLE_MSGOUT;
1038 }
1039 return pSRB;
1040}
1041
1042
1043
1044static void
1045dc390_MsgIn_set_async (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1046{
1047 struct dc390_dcb* pDCB = pSRB->pSRBDCB;
1048 if (!(pSRB->SRBState & DO_SYNC_NEGO))
1049 printk (KERN_INFO "DC390: Target %i initiates Non-Sync?\n", pDCB->TargetID);
1050 pSRB->SRBState &= ~DO_SYNC_NEGO;
1051 pDCB->SyncMode &= ~(SYNC_ENABLE+SYNC_NEGO_DONE);
1052 pDCB->SyncPeriod = 0;
1053 pDCB->SyncOffset = 0;
1054
1055 pDCB->CtrlR3 = FAST_CLK;
1056 pDCB->CtrlR4 &= 0x3f;
1057 pDCB->CtrlR4 |= pACB->glitch_cfg;
1058 dc390_reprog (pACB, pDCB);
1059}
1060
1061
1062static void
1063dc390_MsgIn_set_sync (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1064{
1065 u8 bval;
1066 u16 wval, wval1;
1067 struct dc390_dcb* pDCB = pSRB->pSRBDCB;
1068 u8 oldsyncperiod = pDCB->SyncPeriod;
1069 u8 oldsyncoffset = pDCB->SyncOffset;
1070
1071 if (!(pSRB->SRBState & DO_SYNC_NEGO))
1072 {
1073 printk (KERN_INFO "DC390: Target %i initiates Sync: %ins %i ... answer ...\n",
1074 pDCB->TargetID, pSRB->MsgInBuf[3]<<2, pSRB->MsgInBuf[4]);
1075
1076
1077
1078
1079
1080
1081 if (pSRB->MsgInBuf[4] > 15)
1082 {
1083 printk (KERN_INFO "DC390: Lower Sync Offset to 15\n");
1084 pSRB->MsgInBuf[4] = 15;
1085 }
1086 if (pSRB->MsgInBuf[3] < pDCB->NegoPeriod)
1087 {
1088 printk (KERN_INFO "DC390: Set sync nego period to %ins\n", pDCB->NegoPeriod << 2);
1089 pSRB->MsgInBuf[3] = pDCB->NegoPeriod;
1090 }
1091 memcpy (pSRB->MsgOutBuf, pSRB->MsgInBuf, 5);
1092 pSRB->MsgCnt = 5;
1093 DC390_ENABLE_MSGOUT;
1094 }
1095
1096 pSRB->SRBState &= ~DO_SYNC_NEGO;
1097 pDCB->SyncMode |= SYNC_ENABLE+SYNC_NEGO_DONE;
1098 pDCB->SyncOffset &= 0x0f0;
1099 pDCB->SyncOffset |= pSRB->MsgInBuf[4];
1100 pDCB->NegoPeriod = pSRB->MsgInBuf[3];
1101
1102 wval = (u16) pSRB->MsgInBuf[3];
1103 wval = wval << 2; wval -= 3; wval1 = wval / 25;
1104 if( (wval1 * 25) != wval) wval1++;
1105 bval = FAST_CLK+FAST_SCSI;
1106
1107 pDCB->CtrlR4 &= 0x3f;
1108 if (pACB->glitch_cfg != NS_TO_GLITCH(0))
1109 pDCB->CtrlR4 |= NS_TO_GLITCH(((GLITCH_TO_NS(pACB->glitch_cfg)) - 1));
1110 else
1111 pDCB->CtrlR4 |= NS_TO_GLITCH(0);
1112 if (wval1 < 4) pDCB->CtrlR4 |= NS_TO_GLITCH(0);
1113
1114 if (wval1 >= 8)
1115 {
1116 wval1--;
1117 bval = FAST_CLK;
1118 pDCB->CtrlR4 |= pACB->glitch_cfg;
1119 }
1120
1121 pDCB->CtrlR3 = bval;
1122 pDCB->SyncPeriod = (u8)wval1;
1123
1124 if ((oldsyncperiod != wval1 || oldsyncoffset != pDCB->SyncOffset) && pDCB->TargetLUN == 0)
1125 {
1126 if (! (bval & FAST_SCSI)) wval1++;
1127 printk (KERN_INFO "DC390: Target %i: Sync transfer %i.%1i MHz, Offset %i\n", pDCB->TargetID,
1128 40/wval1, ((40%wval1)*10+wval1/2)/wval1, pDCB->SyncOffset & 0x0f);
1129 }
1130
1131 dc390_reprog (pACB, pDCB);
1132}
1133
1134
1135
1136
1137static void
1138dc390_restore_ptr (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1139{
1140 struct scsi_cmnd *pcmd = pSRB->pcmd;
1141 struct scatterlist *psgl;
1142 pSRB->TotalXferredLen = 0;
1143 pSRB->SGIndex = 0;
1144 if (scsi_sg_count(pcmd)) {
1145 size_t saved;
1146 pSRB->pSegmentList = scsi_sglist(pcmd);
1147 psgl = pSRB->pSegmentList;
1148
1149
1150 while (pSRB->TotalXferredLen + (unsigned long) sg_dma_len(psgl) < pSRB->Saved_Ptr)
1151 {
1152 pSRB->TotalXferredLen += (unsigned long) sg_dma_len(psgl);
1153 pSRB->SGIndex++;
1154 if( pSRB->SGIndex < pSRB->SGcount )
1155 {
1156 pSRB->pSegmentList++;
1157
1158 dc390_start_segment(pSRB);
1159 }
1160 else
1161 pSRB->SGToBeXferLen = 0;
1162 }
1163
1164 saved = pSRB->Saved_Ptr - pSRB->TotalXferredLen;
1165 pSRB->SGToBeXferLen -= saved;
1166 pSRB->SGBusAddr += saved;
1167 printk (KERN_INFO "DC390: Pointer restored. Segment %i, Total %li, Bus %08lx\n",
1168 pSRB->SGIndex, pSRB->Saved_Ptr, pSRB->SGBusAddr);
1169
1170 } else {
1171 pSRB->SGcount = 0;
1172 printk (KERN_INFO "DC390: RESTORE_PTR message for Transfer without Scatter-Gather ??\n");
1173 }
1174
1175 pSRB->TotalXferredLen = pSRB->Saved_Ptr;
1176}
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188static u8 __inline__
1189dc390_MsgIn_complete (u8 *msgbuf, u32 len)
1190{
1191 if (*msgbuf == EXTENDED_MESSAGE)
1192 {
1193 if (len < 2) return 0;
1194 if (len < msgbuf[1] + 2) return 0;
1195 }
1196 else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f)
1197 if (len < 2) return 0;
1198 return 1;
1199}
1200
1201
1202
1203
1204static void
1205dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1206{
1207 struct dc390_dcb* pDCB = pACB->pActiveDCB;
1208
1209
1210
1211 pSRB->MsgInBuf[pACB->MsgLen++] = DC390_read8 (ScsiFifo);
1212
1213
1214
1215 if (dc390_MsgIn_complete (pSRB->MsgInBuf, pACB->MsgLen))
1216 {
1217 DEBUG0 (printk (KERN_INFO "DC390: MsgIn:"); dc390_printMsg (pSRB->MsgInBuf, pACB->MsgLen));
1218
1219 switch (pSRB->MsgInBuf[0])
1220 {
1221 case DISCONNECT:
1222 pSRB->SRBState = SRB_DISCONNECT; break;
1223
1224 case SIMPLE_QUEUE_TAG:
1225 case HEAD_OF_QUEUE_TAG:
1226 case ORDERED_QUEUE_TAG:
1227 pSRB = dc390_MsgIn_QTag (pACB, pDCB, pSRB->MsgInBuf[1]);
1228 break;
1229
1230 case MESSAGE_REJECT:
1231 DC390_write8 (ScsiCmd, RESET_ATN_CMD);
1232 pDCB->NegoPeriod = 50;
1233 if( pSRB->SRBState & DO_SYNC_NEGO)
1234 dc390_MsgIn_set_async (pACB, pSRB);
1235 break;
1236
1237 case EXTENDED_MESSAGE:
1238
1239 if (pSRB->MsgInBuf[1] != 3 || pSRB->MsgInBuf[2] != EXTENDED_SDTR)
1240 dc390_MsgIn_reject (pACB, pSRB);
1241 else
1242 {
1243 if (pSRB->MsgInBuf[3] == 0 || pSRB->MsgInBuf[4] == 0)
1244 dc390_MsgIn_set_async (pACB, pSRB);
1245 else
1246 dc390_MsgIn_set_sync (pACB, pSRB);
1247 }
1248
1249
1250 case COMMAND_COMPLETE: break;
1251
1252
1253
1254 case SAVE_POINTERS:
1255 pSRB->Saved_Ptr = pSRB->TotalXferredLen;
1256 break;
1257
1258 case RESTORE_POINTERS:
1259 DEBUG0(printk ("DC390: RESTORE POINTER message received ... try to handle\n"));
1260 dc390_restore_ptr (pACB, pSRB);
1261 break;
1262
1263
1264 default: dc390_MsgIn_reject (pACB, pSRB);
1265 }
1266
1267
1268 pSRB->SRBState &= ~SRB_MSGIN;
1269 pACB->MsgLen = 0;
1270 }
1271
1272 *psstatus = SCSI_NOP0;
1273 DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);
1274
1275}
1276
1277
1278static void
1279dc390_DataIO_Comm( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 ioDir)
1280{
1281 unsigned long lval;
1282 struct dc390_dcb* pDCB = pACB->pActiveDCB;
1283
1284 if (pSRB == pACB->pTmpSRB)
1285 {
1286 if (pDCB)
1287 printk(KERN_ERR "DC390: pSRB == pTmpSRB! (TagQ Error?) (%02i-%i)\n", pDCB->TargetID, pDCB->TargetLUN);
1288 else
1289 printk(KERN_ERR "DC390: pSRB == pTmpSRB! (TagQ Error?) (DCB 0!)\n");
1290
1291
1292 if (pDCB && pACB->scan_devices && pDCB->GoingSRBCnt == 1) {
1293 pSRB = pDCB->pGoingSRB;
1294 pDCB->pActiveSRB = pSRB;
1295 } else {
1296 pSRB->pSRBDCB = pDCB;
1297 dc390_EnableMsgOut_Abort(pACB, pSRB);
1298 if (pDCB)
1299 pDCB->DCBFlag |= ABORT_DEV;
1300 return;
1301 }
1302 }
1303
1304 if( pSRB->SGIndex < pSRB->SGcount )
1305 {
1306 DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir);
1307 if( !pSRB->SGToBeXferLen )
1308 {
1309 dc390_start_segment(pSRB);
1310
1311 DEBUG1(printk (KERN_DEBUG " DC390: Next SG segment."));
1312 }
1313 lval = pSRB->SGToBeXferLen;
1314 DEBUG1(printk (KERN_DEBUG " DC390: Start transfer: %li bytes (address %08lx)\n", lval, pSRB->SGBusAddr));
1315 DC390_write8 (CtcReg_Low, (u8) lval);
1316 lval >>= 8;
1317 DC390_write8 (CtcReg_Mid, (u8) lval);
1318 lval >>= 8;
1319 DC390_write8 (CtcReg_High, (u8) lval);
1320
1321 DC390_write32 (DMA_XferCnt, pSRB->SGToBeXferLen);
1322 DC390_write32 (DMA_XferAddr, pSRB->SGBusAddr);
1323
1324
1325 pSRB->SRBState = SRB_DATA_XFER;
1326
1327 DC390_write8 (ScsiCmd, DMA_COMMAND+INFO_XFER_CMD);
1328
1329 DC390_write8 (DMA_Cmd, DMA_START_CMD | ioDir);
1330
1331
1332
1333 }
1334 else
1335 {
1336 if( pSRB->SGcount )
1337 {
1338 pSRB->AdaptStatus = H_OVER_UNDER_RUN;
1339 pSRB->SRBStatus |= OVER_RUN;
1340 DEBUG0(printk (KERN_WARNING " DC390: Overrun -"));
1341 }
1342 DEBUG0(printk (KERN_WARNING " Clear transfer pad \n"));
1343 DC390_write8 (CtcReg_Low, 0);
1344 DC390_write8 (CtcReg_Mid, 0);
1345 DC390_write8 (CtcReg_High, 0);
1346
1347 pSRB->SRBState |= SRB_XFERPAD;
1348 DC390_write8 (ScsiCmd, DMA_COMMAND+XFER_PAD_BYTE);
1349
1350
1351
1352
1353 }
1354}
1355
1356
1357static void
1358dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1359{
1360 dc390_DataIO_Comm (pACB, pSRB, WRITE_DIRECTION);
1361}
1362
1363static void
1364dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1365{
1366 dc390_DataIO_Comm (pACB, pSRB, READ_DIRECTION);
1367}
1368
1369static void
1370dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1371{
1372 struct dc390_dcb* pDCB;
1373 u8 i, cnt;
1374 u8 *ptr;
1375
1376 DC390_write8 (ScsiCmd, RESET_ATN_CMD);
1377 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1378 if( !(pSRB->SRBFlag & AUTO_REQSENSE) )
1379 {
1380 cnt = (u8) pSRB->pcmd->cmd_len;
1381 ptr = (u8 *) pSRB->pcmd->cmnd;
1382 for(i=0; i < cnt; i++)
1383 DC390_write8 (ScsiFifo, *(ptr++));
1384 }
1385 else
1386 {
1387 DC390_write8 (ScsiFifo, REQUEST_SENSE);
1388 pDCB = pACB->pActiveDCB;
1389 DC390_write8 (ScsiFifo, pDCB->TargetLUN << 5);
1390 DC390_write8 (ScsiFifo, 0);
1391 DC390_write8 (ScsiFifo, 0);
1392 DC390_write8 (ScsiFifo, sizeof(pSRB->pcmd->sense_buffer));
1393 DC390_write8 (ScsiFifo, 0);
1394 DEBUG0(printk(KERN_DEBUG "DC390: AutoReqSense (CmndPhase)!\n"));
1395 }
1396 pSRB->SRBState = SRB_COMMAND;
1397 DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1398}
1399
1400static void
1401dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1402{
1403 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1404 pSRB->SRBState = SRB_STATUS;
1405 DC390_write8 (ScsiCmd, INITIATOR_CMD_CMPLTE);
1406
1407}
1408
1409static void
1410dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1411{
1412 u8 bval, i, cnt;
1413 u8 *ptr;
1414 struct dc390_dcb* pDCB;
1415
1416 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1417 pDCB = pACB->pActiveDCB;
1418 if( !(pSRB->SRBState & SRB_MSGOUT) )
1419 {
1420 cnt = pSRB->MsgCnt;
1421 if( cnt )
1422 {
1423 ptr = (u8 *) pSRB->MsgOutBuf;
1424 for(i=0; i < cnt; i++)
1425 DC390_write8 (ScsiFifo, *(ptr++));
1426 pSRB->MsgCnt = 0;
1427 if( (pDCB->DCBFlag & ABORT_DEV_) &&
1428 (pSRB->MsgOutBuf[0] == ABORT) )
1429 pSRB->SRBState = SRB_ABORT_SENT;
1430 }
1431 else
1432 {
1433 bval = ABORT;
1434 if( (pSRB->pcmd->cmnd[0] == INQUIRY ) ||
1435 (pSRB->pcmd->cmnd[0] == REQUEST_SENSE) ||
1436 (pSRB->SRBFlag & AUTO_REQSENSE) )
1437 {
1438 if( pDCB->SyncMode & SYNC_ENABLE )
1439 goto mop1;
1440 }
1441 DC390_write8 (ScsiFifo, bval);
1442 }
1443 DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1444 }
1445 else
1446 {
1447mop1:
1448 printk (KERN_ERR "DC390: OLD Sync Nego code triggered! (%i %i)\n", pDCB->TargetID, pDCB->TargetLUN);
1449 DC390_write8 (ScsiFifo, EXTENDED_MESSAGE);
1450 DC390_write8 (ScsiFifo, 3);
1451 DC390_write8 (ScsiFifo, EXTENDED_SDTR);
1452 DC390_write8 (ScsiFifo, pDCB->NegoPeriod);
1453 if (pDCB->SyncOffset & 0x0f)
1454 DC390_write8 (ScsiFifo, pDCB->SyncOffset);
1455 else
1456 DC390_write8 (ScsiFifo, SYNC_NEGO_OFFSET);
1457 pSRB->SRBState |= DO_SYNC_NEGO;
1458 DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1459 }
1460}
1461
1462static void
1463dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1464{
1465 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1466 if( !(pSRB->SRBState & SRB_MSGIN) )
1467 {
1468 pSRB->SRBState &= ~SRB_DISCONNECT;
1469 pSRB->SRBState |= SRB_MSGIN;
1470 }
1471 DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1472
1473}
1474
1475static void
1476dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1477{
1478}
1479
1480static void
1481dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1482{
1483}
1484
1485
1486static void
1487dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB )
1488{
1489 u8 bval, i, cnt;
1490 struct dc390_dcb* ptr;
1491
1492 if( !(pDCB->TargetLUN) )
1493 {
1494 if( !pACB->scan_devices )
1495 {
1496 ptr = pACB->pLinkDCB;
1497 cnt = pACB->DCBCnt;
1498 bval = pDCB->TargetID;
1499 for(i=0; i<cnt; i++)
1500 {
1501 if( ptr->TargetID == bval )
1502 {
1503 ptr->SyncPeriod = pDCB->SyncPeriod;
1504 ptr->SyncOffset = pDCB->SyncOffset;
1505 ptr->CtrlR3 = pDCB->CtrlR3;
1506 ptr->CtrlR4 = pDCB->CtrlR4;
1507 ptr->SyncMode = pDCB->SyncMode;
1508 }
1509 ptr = ptr->pNextDCB;
1510 }
1511 }
1512 }
1513 return;
1514}
1515
1516
1517static void
1518dc390_Disconnect( struct dc390_acb* pACB )
1519{
1520 struct dc390_dcb *pDCB;
1521 struct dc390_srb *pSRB, *psrb;
1522 u8 i, cnt;
1523
1524 DEBUG0(printk(KERN_INFO "DISC,"));
1525
1526 if (!pACB->Connected) printk(KERN_ERR "DC390: Disconnect not-connected bus?\n");
1527 pACB->Connected = 0;
1528 pDCB = pACB->pActiveDCB;
1529 if (!pDCB)
1530 {
1531 DEBUG0(printk(KERN_ERR "ACB:%p->ActiveDCB:%p IOPort:%04x IRQ:%02x !\n",\
1532 pACB, pDCB, pACB->IOPortBase, pACB->IRQLevel));
1533 mdelay(400);
1534 DC390_read8 (INT_Status);
1535 DC390_write8 (ScsiCmd, EN_SEL_RESEL);
1536 return;
1537 }
1538 DC390_write8 (ScsiCmd, EN_SEL_RESEL);
1539 pSRB = pDCB->pActiveSRB;
1540 pACB->pActiveDCB = NULL;
1541 pSRB->ScsiPhase = SCSI_NOP0;
1542 if( pSRB->SRBState & SRB_UNEXPECT_RESEL )
1543 pSRB->SRBState = 0;
1544 else if( pSRB->SRBState & SRB_ABORT_SENT )
1545 {
1546 pDCB->TagMask = 0;
1547 pDCB->DCBFlag = 0;
1548 cnt = pDCB->GoingSRBCnt;
1549 pDCB->GoingSRBCnt = 0;
1550 pSRB = pDCB->pGoingSRB;
1551 for( i=0; i < cnt; i++)
1552 {
1553 psrb = pSRB->pNextSRB;
1554 dc390_Free_insert (pACB, pSRB);
1555 pSRB = psrb;
1556 }
1557 pDCB->pGoingSRB = NULL;
1558 }
1559 else
1560 {
1561 if( (pSRB->SRBState & (SRB_START_+SRB_MSGOUT)) ||
1562 !(pSRB->SRBState & (SRB_DISCONNECT+SRB_COMPLETED)) )
1563 {
1564 pSRB->AdaptStatus = H_SEL_TIMEOUT;
1565 pSRB->TargetStatus = 0;
1566 goto disc1;
1567 }
1568 else if (!(pSRB->SRBState & SRB_DISCONNECT) && (pSRB->SRBState & SRB_COMPLETED))
1569 {
1570disc1:
1571 dc390_freetag (pDCB, pSRB);
1572 pDCB->pActiveSRB = NULL;
1573 pSRB->SRBState = SRB_FREE;
1574 dc390_SRBdone( pACB, pDCB, pSRB);
1575 }
1576 }
1577 pACB->MsgLen = 0;
1578}
1579
1580
1581static void
1582dc390_Reselect( struct dc390_acb* pACB )
1583{
1584 struct dc390_dcb* pDCB;
1585 struct dc390_srb* pSRB;
1586 u8 id, lun;
1587
1588 DEBUG0(printk(KERN_INFO "RSEL,"));
1589 pACB->Connected = 1;
1590 pDCB = pACB->pActiveDCB;
1591 if( pDCB )
1592 {
1593 DEBUG0(printk ("DC390: (ActiveDCB != 0: Arb. lost but resel. won)!\n"));
1594 pSRB = pDCB->pActiveSRB;
1595 if( !( pACB->scan_devices ) )
1596 {
1597 struct scsi_cmnd *pcmd = pSRB->pcmd;
1598 scsi_set_resid(pcmd, scsi_bufflen(pcmd));
1599 SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1600 dc390_Going_remove(pDCB, pSRB);
1601 dc390_Free_insert(pACB, pSRB);
1602 pcmd->scsi_done (pcmd);
1603 DEBUG0(printk(KERN_DEBUG"DC390: Return SRB %p to free\n", pSRB));
1604 }
1605 }
1606
1607 lun = DC390_read8 (ScsiFifo);
1608 DEBUG0(printk ("Dev %02x,", lun));
1609 if (!(lun & (1 << pACB->pScsiHost->this_id)))
1610 printk (KERN_ERR "DC390: Reselection must select host adapter: %02x!\n", lun);
1611 else
1612 lun ^= 1 << pACB->pScsiHost->this_id;
1613 id = 0; while (lun >>= 1) id++;
1614
1615 lun = DC390_read8 (ScsiFifo);
1616 if (!(lun & IDENTIFY_BASE)) printk (KERN_ERR "DC390: Resel: Expect identify message!\n");
1617 lun &= 7;
1618 DEBUG0(printk ("(%02i-%i),", id, lun));
1619 pDCB = dc390_findDCB (pACB, id, lun);
1620 if (!pDCB)
1621 {
1622 printk (KERN_ERR "DC390: Reselect from non existing device (%02i-%i)\n",
1623 id, lun);
1624 return;
1625 }
1626 pACB->pActiveDCB = pDCB;
1627
1628 if( pDCB->SyncMode & EN_TAG_QUEUEING )
1629 {
1630 pSRB = pACB->pTmpSRB;
1631 pDCB->pActiveSRB = pSRB;
1632 }
1633 else
1634 {
1635 pSRB = pDCB->pActiveSRB;
1636 if( !pSRB || !(pSRB->SRBState & SRB_DISCONNECT) )
1637 {
1638 pSRB= pACB->pTmpSRB;
1639 pSRB->SRBState = SRB_UNEXPECT_RESEL;
1640 printk (KERN_ERR "DC390: Reselect without outstanding cmnd (%02i-%i)\n",
1641 id, lun);
1642 pDCB->pActiveSRB = pSRB;
1643 dc390_EnableMsgOut_Abort ( pACB, pSRB );
1644 }
1645 else
1646 {
1647 if( pDCB->DCBFlag & ABORT_DEV_ )
1648 {
1649 pSRB->SRBState = SRB_ABORT_SENT;
1650 printk (KERN_INFO "DC390: Reselect: Abort (%02i-%i)\n",
1651 id, lun);
1652 dc390_EnableMsgOut_Abort( pACB, pSRB );
1653 }
1654 else
1655 pSRB->SRBState = SRB_DATA_XFER;
1656 }
1657 }
1658
1659 DEBUG1(printk (KERN_DEBUG "Resel SRB(%p): TagNum (%02x)\n", pSRB, pSRB->TagNumber));
1660 pSRB->ScsiPhase = SCSI_NOP0;
1661 DC390_write8 (Scsi_Dest_ID, pDCB->TargetID);
1662 DC390_write8 (Sync_Period, pDCB->SyncPeriod);
1663 DC390_write8 (Sync_Offset, pDCB->SyncOffset);
1664 DC390_write8 (CtrlReg1, pDCB->CtrlR1);
1665 DC390_write8 (CtrlReg3, pDCB->CtrlR3);
1666 DC390_write8 (CtrlReg4, pDCB->CtrlR4);
1667 DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);
1668}
1669
1670static int __inline__
1671dc390_RequestSense(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
1672{
1673 struct scsi_cmnd *pcmd;
1674
1675 pcmd = pSRB->pcmd;
1676
1677 REMOVABLEDEBUG(printk(KERN_INFO "DC390: RequestSense(Cmd %02x, Id %02x, LUN %02x)\n",\
1678 pcmd->cmnd[0], pDCB->TargetID, pDCB->TargetLUN));
1679
1680 pSRB->SRBFlag |= AUTO_REQSENSE;
1681 pSRB->SavedTotXLen = pSRB->TotalXferredLen;
1682 pSRB->AdaptStatus = 0;
1683 pSRB->TargetStatus = 0;
1684
1685
1686
1687 pSRB->SGIndex = 0;
1688
1689 pSRB->TotalXferredLen = 0;
1690 pSRB->SGToBeXferLen = 0;
1691 return dc390_StartSCSI(pACB, pDCB, pSRB);
1692}
1693
1694
1695static void
1696dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
1697{
1698 u8 status;
1699 struct scsi_cmnd *pcmd;
1700
1701 pcmd = pSRB->pcmd;
1702
1703 dc390_pci_unmap(pSRB);
1704
1705 status = pSRB->TargetStatus;
1706
1707 DEBUG0(printk (" SRBdone (%02x,%08x), SRB %p, pid %li\n", status, pcmd->result,\
1708 pSRB, pcmd->serial_number));
1709 if(pSRB->SRBFlag & AUTO_REQSENSE)
1710 {
1711 pSRB->SRBFlag &= ~AUTO_REQSENSE;
1712 pSRB->AdaptStatus = 0;
1713 pSRB->TargetStatus = SAM_STAT_CHECK_CONDITION;
1714
1715
1716 if (status == SAM_STAT_CHECK_CONDITION)
1717 pcmd->result = MK_RES_LNX(0, DID_BAD_TARGET, 0, 0);
1718 else
1719 {
1720 if( pSRB->pcmd->cmnd[0] == TEST_UNIT_READY )
1721 {
1722
1723 pcmd->result = MK_RES_LNX(DRIVER_SENSE, DID_OK, 0, SAM_STAT_CHECK_CONDITION);
1724 REMOVABLEDEBUG(printk(KERN_INFO "Cmd=%02x, Result=%08x, XferL=%08x\n",pSRB->pcmd->cmnd[0],\
1725 (u32) pcmd->result, (u32) pSRB->TotalXferredLen));
1726 } else {
1727 SET_RES_DRV(pcmd->result, DRIVER_SENSE);
1728
1729 DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->serial_number, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
1730 pSRB->TotalXferredLen = 0;
1731 SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1732 }
1733 }
1734 goto cmd_done;
1735 }
1736 if( status )
1737 {
1738 if (status == SAM_STAT_CHECK_CONDITION)
1739 {
1740 if (dc390_RequestSense(pACB, pDCB, pSRB)) {
1741 SET_RES_DID(pcmd->result, DID_ERROR);
1742 goto cmd_done;
1743 }
1744 return;
1745 }
1746 else if (status == SAM_STAT_TASK_SET_FULL)
1747 {
1748 scsi_track_queue_full(pcmd->device, pDCB->GoingSRBCnt - 1);
1749 DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->serial_number, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
1750 pSRB->TotalXferredLen = 0;
1751 SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1752 }
1753 else if (status == SAM_STAT_BUSY &&
1754 (pcmd->cmnd[0] == TEST_UNIT_READY || pcmd->cmnd[0] == INQUIRY) &&
1755 pACB->scan_devices)
1756 {
1757 pSRB->AdaptStatus = 0;
1758 pSRB->TargetStatus = status;
1759 pcmd->result = MK_RES(0,0,pSRB->EndMessage,0);
1760 }
1761 else
1762 {
1763 pSRB->TotalXferredLen = 0;
1764 SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1765 goto cmd_done;
1766 }
1767 }
1768 else
1769 {
1770 status = pSRB->AdaptStatus;
1771 if (status == H_OVER_UNDER_RUN)
1772 {
1773 pSRB->TargetStatus = 0;
1774 SET_RES_DID(pcmd->result,DID_OK);
1775 SET_RES_MSG(pcmd->result,pSRB->EndMessage);
1776 }
1777 else if (status == H_SEL_TIMEOUT)
1778 {
1779 pcmd->result = MK_RES(0, DID_NO_CONNECT, 0, 0);
1780
1781 }
1782 else if( pSRB->SRBStatus & PARITY_ERROR)
1783 {
1784
1785 SET_RES_DID(pcmd->result,DID_PARITY);
1786 SET_RES_MSG(pcmd->result,pSRB->EndMessage);
1787 }
1788 else
1789 {
1790 pSRB->AdaptStatus = 0;
1791 pSRB->TargetStatus = 0;
1792 SET_RES_DID(pcmd->result,DID_OK);
1793 }
1794 }
1795
1796cmd_done:
1797 scsi_set_resid(pcmd, scsi_bufflen(pcmd) - pSRB->TotalXferredLen);
1798
1799 dc390_Going_remove (pDCB, pSRB);
1800
1801 dc390_Free_insert (pACB, pSRB);
1802
1803 DEBUG0(printk (KERN_DEBUG "DC390: SRBdone: done pid %li\n", pcmd->serial_number));
1804 pcmd->scsi_done (pcmd);
1805
1806 return;
1807}
1808
1809
1810
1811static void
1812dc390_DoingSRB_Done(struct dc390_acb* pACB, struct scsi_cmnd *cmd)
1813{
1814 struct dc390_dcb *pDCB, *pdcb;
1815 struct dc390_srb *psrb, *psrb2;
1816 int i;
1817 struct scsi_cmnd *pcmd;
1818
1819 pDCB = pACB->pLinkDCB;
1820 pdcb = pDCB;
1821 if (! pdcb) return;
1822 do
1823 {
1824 psrb = pdcb->pGoingSRB;
1825 for (i = 0; i < pdcb->GoingSRBCnt; i++)
1826 {
1827 psrb2 = psrb->pNextSRB;
1828 pcmd = psrb->pcmd;
1829 dc390_Free_insert (pACB, psrb);
1830 psrb = psrb2;
1831 }
1832 pdcb->GoingSRBCnt = 0;
1833 pdcb->pGoingSRB = NULL;
1834 pdcb->TagMask = 0;
1835 pdcb = pdcb->pNextDCB;
1836 } while( pdcb != pDCB );
1837}
1838
1839
1840static void
1841dc390_ResetSCSIBus( struct dc390_acb* pACB )
1842{
1843
1844
1845
1846
1847 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1848 DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1849 DC390_write8 (ScsiCmd, RST_SCSI_BUS_CMD);
1850 pACB->Connected = 0;
1851
1852 return;
1853}
1854
1855static void
1856dc390_ScsiRstDetect( struct dc390_acb* pACB )
1857{
1858 printk ("DC390: Rst_Detect: laststat = %08x\n", dc390_laststatus);
1859
1860
1861 DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1862
1863
1864 udelay (1000);
1865 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1866 pACB->pScsiHost->last_reset = jiffies + 5*HZ/2
1867 + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
1868 pACB->Connected = 0;
1869
1870 if( pACB->ACBFlag & RESET_DEV )
1871 pACB->ACBFlag |= RESET_DONE;
1872 else
1873 {
1874 pACB->ACBFlag |= RESET_DETECT;
1875
1876 dc390_ResetDevParam( pACB );
1877 dc390_DoingSRB_Done( pACB, NULL);
1878
1879 pACB->pActiveDCB = NULL;
1880 pACB->ACBFlag = 0;
1881 }
1882 return;
1883}
1884
1885static int DC390_queuecommand(struct scsi_cmnd *cmd,
1886 void (*done)(struct scsi_cmnd *))
1887{
1888 struct scsi_device *sdev = cmd->device;
1889 struct dc390_acb *acb = (struct dc390_acb *)sdev->host->hostdata;
1890 struct dc390_dcb *dcb = sdev->hostdata;
1891 struct dc390_srb *srb;
1892
1893 if (sdev->queue_depth <= dcb->GoingSRBCnt)
1894 goto device_busy;
1895 if (acb->pActiveDCB)
1896 goto host_busy;
1897 if (acb->ACBFlag & (RESET_DETECT|RESET_DONE|RESET_DEV))
1898 goto host_busy;
1899
1900 srb = acb->pFreeSRB;
1901 if (unlikely(srb == NULL))
1902 goto host_busy;
1903
1904 cmd->scsi_done = done;
1905 cmd->result = 0;
1906 acb->Cmds++;
1907
1908 acb->pFreeSRB = srb->pNextSRB;
1909 srb->pNextSRB = NULL;
1910
1911 srb->pSRBDCB = dcb;
1912 srb->pcmd = cmd;
1913 cmd->host_scribble = (char *)srb;
1914
1915 srb->SGIndex = 0;
1916 srb->AdaptStatus = 0;
1917 srb->TargetStatus = 0;
1918 srb->MsgCnt = 0;
1919
1920 srb->SRBStatus = 0;
1921 srb->SRBFlag = 0;
1922 srb->SRBState = 0;
1923 srb->TotalXferredLen = 0;
1924 srb->SGBusAddr = 0;
1925 srb->SGToBeXferLen = 0;
1926 srb->ScsiPhase = 0;
1927 srb->EndMessage = 0;
1928 srb->TagNumber = SCSI_NO_TAG;
1929
1930 if (dc390_StartSCSI(acb, dcb, srb)) {
1931 dc390_Free_insert(acb, srb);
1932 goto host_busy;
1933 }
1934
1935 dc390_Going_append(dcb, srb);
1936
1937 return 0;
1938
1939 host_busy:
1940 return SCSI_MLQUEUE_HOST_BUSY;
1941
1942 device_busy:
1943 return SCSI_MLQUEUE_DEVICE_BUSY;
1944}
1945
1946static void dc390_dumpinfo (struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
1947{
1948 struct pci_dev *pdev;
1949 u16 pstat;
1950
1951 if (!pDCB) pDCB = pACB->pActiveDCB;
1952 if (!pSRB && pDCB) pSRB = pDCB->pActiveSRB;
1953
1954 if (pSRB)
1955 {
1956 printk ("DC390: SRB: Xferred %08lx, Remain %08lx, State %08x, Phase %02x\n",
1957 pSRB->TotalXferredLen, pSRB->SGToBeXferLen, pSRB->SRBState,
1958 pSRB->ScsiPhase);
1959 printk ("DC390: AdpaterStatus: %02x, SRB Status %02x\n", pSRB->AdaptStatus, pSRB->SRBStatus);
1960 }
1961 printk ("DC390: Status of last IRQ (DMA/SC/Int/IRQ): %08x\n", dc390_laststatus);
1962 printk ("DC390: Register dump: SCSI block:\n");
1963 printk ("DC390: XferCnt Cmd Stat IntS IRQS FFIS Ctl1 Ctl2 Ctl3 Ctl4\n");
1964 printk ("DC390: %06x %02x %02x %02x",
1965 DC390_read8(CtcReg_Low) + (DC390_read8(CtcReg_Mid) << 8) + (DC390_read8(CtcReg_High) << 16),
1966 DC390_read8(ScsiCmd), DC390_read8(Scsi_Status), DC390_read8(Intern_State));
1967 printk (" %02x %02x %02x %02x %02x %02x\n",
1968 DC390_read8(INT_Status), DC390_read8(Current_Fifo), DC390_read8(CtrlReg1),
1969 DC390_read8(CtrlReg2), DC390_read8(CtrlReg3), DC390_read8(CtrlReg4));
1970 DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT);
1971 if (DC390_read8(Current_Fifo) & 0x1f)
1972 {
1973 printk ("DC390: FIFO:");
1974 while (DC390_read8(Current_Fifo) & 0x1f) printk (" %02x", DC390_read8(ScsiFifo));
1975 printk ("\n");
1976 }
1977 printk ("DC390: Register dump: DMA engine:\n");
1978 printk ("DC390: Cmd STrCnt SBusA WrkBC WrkAC Stat SBusCtrl\n");
1979 printk ("DC390: %02x %08x %08x %08x %08x %02x %08x\n",
1980 DC390_read8(DMA_Cmd), DC390_read32(DMA_XferCnt), DC390_read32(DMA_XferAddr),
1981 DC390_read32(DMA_Wk_ByteCntr), DC390_read32(DMA_Wk_AddrCntr),
1982 DC390_read8(DMA_Status), DC390_read32(DMA_ScsiBusCtrl));
1983 DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
1984
1985 pdev = pACB->pdev;
1986 pci_read_config_word(pdev, PCI_STATUS, &pstat);
1987 printk ("DC390: Register dump: PCI Status: %04x\n", pstat);
1988 printk ("DC390: In case of driver trouble read Documentation/scsi/tmscsim.txt\n");
1989}
1990
1991
1992static int DC390_abort(struct scsi_cmnd *cmd)
1993{
1994 struct dc390_acb *pACB = (struct dc390_acb*) cmd->device->host->hostdata;
1995 struct dc390_dcb *pDCB = (struct dc390_dcb*) cmd->device->hostdata;
1996
1997 scmd_printk(KERN_WARNING, cmd,
1998 "DC390: Abort command (pid %li)\n", cmd->serial_number);
1999
2000
2001
2002
2003 dc390_dumpinfo(pACB, pDCB, NULL);
2004
2005 pDCB->DCBFlag |= ABORT_DEV_;
2006 printk(KERN_INFO "DC390: Aborted pid %li\n", cmd->serial_number);
2007
2008 return FAILED;
2009}
2010
2011
2012static void dc390_ResetDevParam( struct dc390_acb* pACB )
2013{
2014 struct dc390_dcb *pDCB, *pdcb;
2015
2016 pDCB = pACB->pLinkDCB;
2017 if (! pDCB) return;
2018 pdcb = pDCB;
2019 do
2020 {
2021 pDCB->SyncMode &= ~SYNC_NEGO_DONE;
2022 pDCB->SyncPeriod = 0;
2023 pDCB->SyncOffset = 0;
2024 pDCB->TagMask = 0;
2025 pDCB->CtrlR3 = FAST_CLK;
2026 pDCB->CtrlR4 &= NEGATE_REQACKDATA | CTRL4_RESERVED | NEGATE_REQACK;
2027 pDCB->CtrlR4 |= pACB->glitch_cfg;
2028 pDCB = pDCB->pNextDCB;
2029 }
2030 while( pdcb != pDCB );
2031 pACB->ACBFlag &= ~(RESET_DEV | RESET_DONE | RESET_DETECT);
2032
2033}
2034
2035static int DC390_bus_reset (struct scsi_cmnd *cmd)
2036{
2037 struct dc390_acb* pACB = (struct dc390_acb*) cmd->device->host->hostdata;
2038 u8 bval;
2039
2040 spin_lock_irq(cmd->device->host->host_lock);
2041
2042 bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
2043 DC390_write8(CtrlReg1, bval);
2044
2045 pACB->ACBFlag |= RESET_DEV;
2046 dc390_ResetSCSIBus(pACB);
2047
2048 dc390_ResetDevParam(pACB);
2049 mdelay(1);
2050 pACB->pScsiHost->last_reset = jiffies + 3*HZ/2
2051 + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
2052
2053 DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
2054 DC390_read8(INT_Status);
2055
2056 dc390_DoingSRB_Done(pACB, cmd);
2057
2058 pACB->pActiveDCB = NULL;
2059 pACB->ACBFlag = 0;
2060
2061 bval = DC390_read8(CtrlReg1) & ~DIS_INT_ON_SCSI_RST;
2062 DC390_write8(CtrlReg1, bval);
2063
2064 spin_unlock_irq(cmd->device->host->host_lock);
2065
2066 return SUCCESS;
2067}
2068
2069
2070
2071
2072
2073
2074
2075static int dc390_slave_alloc(struct scsi_device *scsi_device)
2076{
2077 struct dc390_acb *pACB = (struct dc390_acb*) scsi_device->host->hostdata;
2078 struct dc390_dcb *pDCB, *pDCB2 = NULL;
2079 uint id = scsi_device->id;
2080 uint lun = scsi_device->lun;
2081
2082 pDCB = kzalloc(sizeof(struct dc390_dcb), GFP_KERNEL);
2083 if (!pDCB)
2084 return -ENOMEM;
2085
2086 if (!pACB->DCBCnt++) {
2087 pACB->pLinkDCB = pDCB;
2088 pACB->pDCBRunRobin = pDCB;
2089 } else {
2090 pACB->pLastDCB->pNextDCB = pDCB;
2091 }
2092
2093 pDCB->pNextDCB = pACB->pLinkDCB;
2094 pACB->pLastDCB = pDCB;
2095
2096 pDCB->pDCBACB = pACB;
2097 pDCB->TargetID = id;
2098 pDCB->TargetLUN = lun;
2099
2100
2101
2102
2103
2104 if (lun && (pDCB2 = dc390_findDCB(pACB, id, 0))) {
2105 pDCB->DevMode = pDCB2->DevMode;
2106 pDCB->SyncMode = pDCB2->SyncMode & SYNC_NEGO_DONE;
2107 pDCB->SyncPeriod = pDCB2->SyncPeriod;
2108 pDCB->SyncOffset = pDCB2->SyncOffset;
2109 pDCB->NegoPeriod = pDCB2->NegoPeriod;
2110
2111 pDCB->CtrlR3 = pDCB2->CtrlR3;
2112 pDCB->CtrlR4 = pDCB2->CtrlR4;
2113 } else {
2114 u8 index = pACB->AdapterIndex;
2115 PEEprom prom = (PEEprom) &dc390_eepromBuf[index][id << 2];
2116
2117 pDCB->DevMode = prom->EE_MODE1;
2118 pDCB->NegoPeriod =
2119 (dc390_clock_period1[prom->EE_SPEED] * 25) >> 2;
2120 pDCB->CtrlR3 = FAST_CLK;
2121 pDCB->CtrlR4 = pACB->glitch_cfg | CTRL4_RESERVED;
2122 if (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION)
2123 pDCB->CtrlR4 |= NEGATE_REQACKDATA | NEGATE_REQACK;
2124 }
2125
2126 if (pDCB->DevMode & SYNC_NEGO_)
2127 pDCB->SyncMode |= SYNC_ENABLE;
2128 else {
2129 pDCB->SyncMode = 0;
2130 pDCB->SyncOffset &= ~0x0f;
2131 }
2132
2133 pDCB->CtrlR1 = pACB->pScsiHost->this_id;
2134 if (pDCB->DevMode & PARITY_CHK_)
2135 pDCB->CtrlR1 |= PARITY_ERR_REPO;
2136
2137 pACB->scan_devices = 1;
2138 scsi_device->hostdata = pDCB;
2139 return 0;
2140}
2141
2142
2143
2144
2145
2146
2147
2148static void dc390_slave_destroy(struct scsi_device *scsi_device)
2149{
2150 struct dc390_acb* pACB = (struct dc390_acb*) scsi_device->host->hostdata;
2151 struct dc390_dcb* pDCB = (struct dc390_dcb*) scsi_device->hostdata;
2152 struct dc390_dcb* pPrevDCB = pACB->pLinkDCB;
2153
2154 pACB->scan_devices = 0;
2155
2156 BUG_ON(pDCB->GoingSRBCnt > 1);
2157
2158 if (pDCB == pACB->pLinkDCB) {
2159 if (pACB->pLastDCB == pDCB) {
2160 pDCB->pNextDCB = NULL;
2161 pACB->pLastDCB = NULL;
2162 }
2163 pACB->pLinkDCB = pDCB->pNextDCB;
2164 } else {
2165 while (pPrevDCB->pNextDCB != pDCB)
2166 pPrevDCB = pPrevDCB->pNextDCB;
2167 pPrevDCB->pNextDCB = pDCB->pNextDCB;
2168 if (pDCB == pACB->pLastDCB)
2169 pACB->pLastDCB = pPrevDCB;
2170 }
2171
2172 if (pDCB == pACB->pActiveDCB)
2173 pACB->pActiveDCB = NULL;
2174 if (pDCB == pACB->pLinkDCB)
2175 pACB->pLinkDCB = pDCB->pNextDCB;
2176 if (pDCB == pACB->pDCBRunRobin)
2177 pACB->pDCBRunRobin = pDCB->pNextDCB;
2178 kfree(pDCB);
2179
2180 pACB->DCBCnt--;
2181}
2182
2183static int dc390_slave_configure(struct scsi_device *sdev)
2184{
2185 struct dc390_acb *acb = (struct dc390_acb *)sdev->host->hostdata;
2186 struct dc390_dcb *dcb = (struct dc390_dcb *)sdev->hostdata;
2187
2188 acb->scan_devices = 0;
2189 if (sdev->tagged_supported && (dcb->DevMode & TAG_QUEUEING_)) {
2190 dcb->SyncMode |= EN_TAG_QUEUEING;
2191 scsi_activate_tcq(sdev, acb->TagMaxNum);
2192 }
2193
2194 return 0;
2195}
2196
2197static struct scsi_host_template driver_template = {
2198 .module = THIS_MODULE,
2199 .proc_name = "tmscsim",
2200 .name = DC390_BANNER " V" DC390_VERSION,
2201 .slave_alloc = dc390_slave_alloc,
2202 .slave_configure = dc390_slave_configure,
2203 .slave_destroy = dc390_slave_destroy,
2204 .queuecommand = DC390_queuecommand,
2205 .eh_abort_handler = DC390_abort,
2206 .eh_bus_reset_handler = DC390_bus_reset,
2207 .can_queue = 1,
2208 .this_id = 7,
2209 .sg_tablesize = SG_ALL,
2210 .cmd_per_lun = 1,
2211 .use_clustering = ENABLE_CLUSTERING,
2212 .max_sectors = 0x4000,
2213};
2214
2215
2216
2217
2218
2219
2220
2221static void __devinit dc390_eeprom_prepare_read(struct pci_dev *pdev, u8 cmd)
2222{
2223 u8 carryFlag = 1, j = 0x80, bval;
2224 int i;
2225
2226 for (i = 0; i < 9; i++) {
2227 if (carryFlag) {
2228 pci_write_config_byte(pdev, 0x80, 0x40);
2229 bval = 0xc0;
2230 } else
2231 bval = 0x80;
2232
2233 udelay(160);
2234 pci_write_config_byte(pdev, 0x80, bval);
2235 udelay(160);
2236 pci_write_config_byte(pdev, 0x80, 0);
2237 udelay(160);
2238
2239 carryFlag = (cmd & j) ? 1 : 0;
2240 j >>= 1;
2241 }
2242}
2243
2244static u16 __devinit dc390_eeprom_get_data(struct pci_dev *pdev)
2245{
2246 int i;
2247 u16 wval = 0;
2248 u8 bval;
2249
2250 for (i = 0; i < 16; i++) {
2251 wval <<= 1;
2252
2253 pci_write_config_byte(pdev, 0x80, 0x80);
2254 udelay(160);
2255 pci_write_config_byte(pdev, 0x80, 0x40);
2256 udelay(160);
2257 pci_read_config_byte(pdev, 0x00, &bval);
2258
2259 if (bval == 0x22)
2260 wval |= 1;
2261 }
2262
2263 return wval;
2264}
2265
2266static void __devinit dc390_read_eeprom(struct pci_dev *pdev, u16 *ptr)
2267{
2268 u8 cmd = EEPROM_READ, i;
2269
2270 for (i = 0; i < 0x40; i++) {
2271 pci_write_config_byte(pdev, 0xc0, 0);
2272 udelay(160);
2273
2274 dc390_eeprom_prepare_read(pdev, cmd++);
2275 *ptr++ = dc390_eeprom_get_data(pdev);
2276
2277 pci_write_config_byte(pdev, 0x80, 0);
2278 pci_write_config_byte(pdev, 0x80, 0);
2279 udelay(160);
2280 }
2281}
2282
2283
2284static void __devinit dc390_eeprom_override(u8 index)
2285{
2286 u8 *ptr = (u8 *) dc390_eepromBuf[index], id;
2287
2288
2289 if (tmscsim[0] != -2)
2290 ptr[EE_ADAPT_SCSI_ID] = (u8)tmscsim[0];
2291 if (tmscsim[3] != -2)
2292 ptr[EE_MODE2] = (u8)tmscsim[3];
2293 if (tmscsim[5] != -2)
2294 ptr[EE_DELAY] = tmscsim[5];
2295 if (tmscsim[4] != -2)
2296 ptr[EE_TAG_CMD_NUM] = (u8)tmscsim[4];
2297
2298
2299 for (id = 0; id < MAX_SCSI_ID; id++) {
2300 if (tmscsim[2] != -2)
2301 ptr[id << 2] = (u8)tmscsim[2];
2302 if (tmscsim[1] != -2)
2303 ptr[(id << 2) + 1] = (u8)tmscsim[1];
2304 }
2305}
2306
2307static int __devinitdata tmscsim_def[] = {
2308 7,
2309 0 ,
2310 PARITY_CHK_ | SEND_START_ | EN_DISCONNECT_ | SYNC_NEGO_ | TAG_QUEUEING_,
2311 MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION | LUN_CHECK,
2312 3 ,
2313 1 ,
2314};
2315
2316
2317static void __devinit dc390_fill_with_defaults (void)
2318{
2319 int i;
2320
2321 for (i = 0; i < 6; i++) {
2322 if (tmscsim[i] < 0 || tmscsim[i] > 255)
2323 tmscsim[i] = tmscsim_def[i];
2324 }
2325
2326
2327 if (tmscsim[0] > 7)
2328 tmscsim[0] = 7;
2329 if (tmscsim[1] > 7)
2330 tmscsim[1] = 4;
2331 if (tmscsim[4] > 5)
2332 tmscsim[4] = 4;
2333 if (tmscsim[5] > 180)
2334 tmscsim[5] = 180;
2335}
2336
2337static void __devinit dc390_check_eeprom(struct pci_dev *pdev, u8 index)
2338{
2339 u8 interpd[] = {1, 3, 5, 10, 16, 30, 60, 120};
2340 u8 EEbuf[128];
2341 u16 *ptr = (u16 *)EEbuf, wval = 0;
2342 int i;
2343
2344 dc390_read_eeprom(pdev, ptr);
2345 memcpy(dc390_eepromBuf[index], EEbuf, EE_ADAPT_SCSI_ID);
2346 memcpy(&dc390_eepromBuf[index][EE_ADAPT_SCSI_ID],
2347 &EEbuf[REAL_EE_ADAPT_SCSI_ID], EE_LEN - EE_ADAPT_SCSI_ID);
2348
2349 dc390_eepromBuf[index][EE_DELAY] = interpd[dc390_eepromBuf[index][EE_DELAY]];
2350
2351 for (i = 0; i < 0x40; i++, ptr++)
2352 wval += *ptr;
2353
2354
2355 if (wval != 0x1234) {
2356 int speed;
2357
2358 printk(KERN_INFO "DC390_init: No EEPROM found! Trying default settings ...\n");
2359
2360
2361
2362
2363
2364 dc390_fill_with_defaults();
2365
2366 speed = dc390_clock_speed[tmscsim[1]];
2367 printk(KERN_INFO "DC390: Used defaults: AdaptID=%i, SpeedIdx=%i (%i.%i MHz), "
2368 "DevMode=0x%02x, AdaptMode=0x%02x, TaggedCmnds=%i (%i), DelayReset=%is\n",
2369 tmscsim[0], tmscsim[1], speed / 10, speed % 10,
2370 (u8)tmscsim[2], (u8)tmscsim[3], tmscsim[4], 2 << (tmscsim[4]), tmscsim[5]);
2371 }
2372}
2373
2374static void __devinit dc390_init_hw(struct dc390_acb *pACB, u8 index)
2375{
2376 struct Scsi_Host *shost = pACB->pScsiHost;
2377 u8 dstate;
2378
2379
2380 DC390_write8(CtrlReg1, DIS_INT_ON_SCSI_RST | shost->this_id);
2381
2382 if (pACB->Gmode2 & RST_SCSI_BUS) {
2383 dc390_ResetSCSIBus(pACB);
2384 udelay(1000);
2385 shost->last_reset = jiffies + HZ/2 +
2386 HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
2387 }
2388
2389 pACB->ACBFlag = 0;
2390
2391
2392 DC390_read8(INT_Status);
2393
2394
2395 DC390_write8(Scsi_TimeOut, SEL_TIMEOUT);
2396
2397
2398 DC390_write8(Clk_Factor, CLK_FREQ_40MHZ);
2399
2400
2401 DC390_write8(ScsiCmd, NOP_CMD);
2402
2403
2404 DC390_write8(CtrlReg2, EN_FEATURE+EN_SCSI2_CMD);
2405
2406
2407 DC390_write8(CtrlReg3, FAST_CLK);
2408
2409
2410 DC390_write8(CtrlReg4, pACB->glitch_cfg |
2411 (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION) ?
2412 NEGATE_REQACKDATA : 0);
2413
2414
2415 DC390_write8(CtcReg_High, 0);
2416 DC390_write8(DMA_Cmd, DMA_IDLE_CMD);
2417 DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
2418 DC390_write32(DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
2419
2420 dstate = DC390_read8(DMA_Status);
2421 DC390_write8(DMA_Status, dstate);
2422}
2423
2424static int __devinit dc390_probe_one(struct pci_dev *pdev,
2425 const struct pci_device_id *id)
2426{
2427 struct dc390_acb *pACB;
2428 struct Scsi_Host *shost;
2429 unsigned long io_port;
2430 int error = -ENODEV, i;
2431
2432 if (pci_enable_device(pdev))
2433 goto out;
2434
2435 pci_set_master(pdev);
2436
2437 error = -ENOMEM;
2438 if (disable_clustering)
2439 driver_template.use_clustering = DISABLE_CLUSTERING;
2440 shost = scsi_host_alloc(&driver_template, sizeof(struct dc390_acb));
2441 if (!shost)
2442 goto out_disable_device;
2443
2444 pACB = (struct dc390_acb *)shost->hostdata;
2445 memset(pACB, 0, sizeof(struct dc390_acb));
2446
2447 dc390_check_eeprom(pdev, dc390_adapterCnt);
2448 dc390_eeprom_override(dc390_adapterCnt);
2449
2450 io_port = pci_resource_start(pdev, 0);
2451
2452 shost->this_id = dc390_eepromBuf[dc390_adapterCnt][EE_ADAPT_SCSI_ID];
2453 shost->io_port = io_port;
2454 shost->n_io_port = 0x80;
2455 shost->irq = pdev->irq;
2456 shost->base = io_port;
2457 shost->unique_id = io_port;
2458 shost->last_reset = jiffies;
2459
2460 pACB->pScsiHost = shost;
2461 pACB->IOPortBase = (u16) io_port;
2462 pACB->IRQLevel = pdev->irq;
2463
2464 shost->max_id = 8;
2465
2466 if (shost->max_id - 1 ==
2467 dc390_eepromBuf[dc390_adapterCnt][EE_ADAPT_SCSI_ID])
2468 shost->max_id--;
2469
2470 if (dc390_eepromBuf[dc390_adapterCnt][EE_MODE2] & LUN_CHECK)
2471 shost->max_lun = 8;
2472 else
2473 shost->max_lun = 1;
2474
2475 pACB->pFreeSRB = pACB->SRB_array;
2476 pACB->SRBCount = MAX_SRB_CNT;
2477 pACB->AdapterIndex = dc390_adapterCnt;
2478 pACB->TagMaxNum =
2479 2 << dc390_eepromBuf[dc390_adapterCnt][EE_TAG_CMD_NUM];
2480 pACB->Gmode2 = dc390_eepromBuf[dc390_adapterCnt][EE_MODE2];
2481
2482 for (i = 0; i < pACB->SRBCount-1; i++)
2483 pACB->SRB_array[i].pNextSRB = &pACB->SRB_array[i+1];
2484 pACB->SRB_array[pACB->SRBCount-1].pNextSRB = NULL;
2485 pACB->pTmpSRB = &pACB->TmpSRB;
2486
2487 pACB->sel_timeout = SEL_TIMEOUT;
2488 pACB->glitch_cfg = EATER_25NS;
2489 pACB->pdev = pdev;
2490
2491 if (!request_region(io_port, shost->n_io_port, "tmscsim")) {
2492 printk(KERN_ERR "DC390: register IO ports error!\n");
2493 goto out_host_put;
2494 }
2495
2496
2497 DC390_read8_(INT_Status, io_port);
2498
2499 if (request_irq(pdev->irq, do_DC390_Interrupt, IRQF_SHARED,
2500 "tmscsim", pACB)) {
2501 printk(KERN_ERR "DC390: register IRQ error!\n");
2502 goto out_release_region;
2503 }
2504
2505 dc390_init_hw(pACB, dc390_adapterCnt);
2506
2507 dc390_adapterCnt++;
2508
2509 pci_set_drvdata(pdev, shost);
2510
2511 error = scsi_add_host(shost, &pdev->dev);
2512 if (error)
2513 goto out_free_irq;
2514 scsi_scan_host(shost);
2515 return 0;
2516
2517 out_free_irq:
2518 free_irq(pdev->irq, pACB);
2519 out_release_region:
2520 release_region(io_port, shost->n_io_port);
2521 out_host_put:
2522 scsi_host_put(shost);
2523 out_disable_device:
2524 pci_disable_device(pdev);
2525 out:
2526 return error;
2527}
2528
2529
2530
2531
2532
2533
2534static void __devexit dc390_remove_one(struct pci_dev *dev)
2535{
2536 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
2537 unsigned long iflags;
2538 struct dc390_acb* pACB = (struct dc390_acb*) scsi_host->hostdata;
2539 u8 bval;
2540
2541 scsi_remove_host(scsi_host);
2542
2543 spin_lock_irqsave(scsi_host->host_lock, iflags);
2544 pACB->ACBFlag = RESET_DEV;
2545 bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
2546 DC390_write8 (CtrlReg1, bval);
2547 if (pACB->Gmode2 & RST_SCSI_BUS)
2548 dc390_ResetSCSIBus(pACB);
2549 spin_unlock_irqrestore(scsi_host->host_lock, iflags);
2550
2551 free_irq(scsi_host->irq, pACB);
2552 release_region(scsi_host->io_port, scsi_host->n_io_port);
2553
2554 pci_disable_device(dev);
2555 scsi_host_put(scsi_host);
2556 pci_set_drvdata(dev, NULL);
2557}
2558
2559static struct pci_device_id tmscsim_pci_tbl[] = {
2560 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD53C974,
2561 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
2562 { }
2563};
2564MODULE_DEVICE_TABLE(pci, tmscsim_pci_tbl);
2565
2566static struct pci_driver dc390_driver = {
2567 .name = "tmscsim",
2568 .id_table = tmscsim_pci_tbl,
2569 .probe = dc390_probe_one,
2570 .remove = __devexit_p(dc390_remove_one),
2571};
2572
2573static int __init dc390_module_init(void)
2574{
2575 if (!disable_clustering)
2576 printk(KERN_INFO "DC390: clustering now enabled by default. If you get problems load\n"
2577 "\twith \"disable_clustering=1\" and report to maintainers\n");
2578
2579 if (tmscsim[0] == -1 || tmscsim[0] > 15) {
2580 tmscsim[0] = 7;
2581 tmscsim[1] = 4;
2582 tmscsim[2] = PARITY_CHK_ | TAG_QUEUEING_;
2583 tmscsim[3] = MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION;
2584 tmscsim[4] = 2;
2585 tmscsim[5] = 10;
2586 printk (KERN_INFO "DC390: Using safe settings.\n");
2587 }
2588
2589 return pci_register_driver(&dc390_driver);
2590}
2591
2592static void __exit dc390_module_exit(void)
2593{
2594 pci_unregister_driver(&dc390_driver);
2595}
2596
2597module_init(dc390_module_init);
2598module_exit(dc390_module_exit);
2599
2600#ifndef MODULE
2601static int __init dc390_setup (char *str)
2602{
2603 int ints[8],i, im;
2604
2605 get_options(str, ARRAY_SIZE(ints), ints);
2606 im = ints[0];
2607
2608 if (im > 6) {
2609 printk (KERN_NOTICE "DC390: ignore extra params!\n");
2610 im = 6;
2611 }
2612
2613 for (i = 0; i < im; i++)
2614 tmscsim[i] = ints[i+1];
2615
2616 return 1;
2617}
2618
2619__setup("tmscsim=", dc390_setup);
2620#endif
2621