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