IPoIB/cm: Optimize stale connection detection
[linux-2.6] / drivers / scsi / tmscsim.c
1 /************************************************************************
2  *      FILE NAME : TMSCSIM.C                                           *
3  *           BY   : C.L. Huang,  ching@tekram.com.tw                    *
4  *      Description: Device Driver for Tekram DC-390(T) PCI SCSI        *
5  *                   Bus Master Host Adapter                            *
6  * (C)Copyright 1995-1996 Tekram Technology Co., Ltd.                   *
7  ************************************************************************
8  * (C) Copyright: put under GNU GPL in 10/96                            *
9  *                              (see Documentation/scsi/tmscsim.txt)    *
10  ************************************************************************
11  * $Id: tmscsim.c,v 2.60.2.30 2000/12/20 01:07:12 garloff Exp $         *
12  *      Enhancements and bugfixes by                                    *
13  *      Kurt Garloff <kurt@garloff.de>  <garloff@suse.de>               *
14  ************************************************************************
15  *      HISTORY:                                                        *
16  *                                                                      *
17  *      REV#    DATE    NAME    DESCRIPTION                             *
18  *      1.00  96/04/24  CLH     First release                           *
19  *      1.01  96/06/12  CLH     Fixed bug of Media Change for Removable *
20  *                              Device, scan all LUN. Support Pre2.0.10 *
21  *      1.02  96/06/18  CLH     Fixed bug of Command timeout ...        *
22  *      1.03  96/09/25  KG      Added tmscsim_proc_info()               *
23  *      1.04  96/10/11  CLH     Updating for support KV 2.0.x           *
24  *      1.05  96/10/18  KG      Fixed bug in DC390_abort(null ptr deref)*
25  *      1.06  96/10/25  KG      Fixed module support                    *
26  *      1.07  96/11/09  KG      Fixed tmscsim_proc_info()               *
27  *      1.08  96/11/18  KG      Fixed null ptr in DC390_Disconnect()    *
28  *      1.09  96/11/30  KG      Added register the allocated IO space   *
29  *      1.10  96/12/05  CLH     Modified tmscsim_proc_info(), and reset *
30  *                              pending interrupt in DC390_detect()     *
31  *      1.11  97/02/05  KG/CLH  Fixeds problem with partitions greater  *
32  *                              than 1GB                                *
33  *      1.12  98/02/15  MJ      Rewritten PCI probing                   *
34  *      1.13  98/04/08  KG      Support for non DC390, __initfunc decls,*
35  *                              changed max devs from 10 to 16          *
36  *      1.14a 98/05/05  KG      Dynamic DCB allocation, add-single-dev  *
37  *                              for LUNs if LUN_SCAN (BIOS) not set     *
38  *                              runtime config using /proc interface    *
39  *      1.14b 98/05/06  KG      eliminated cli (); sti (); spinlocks    *
40  *      1.14c 98/05/07  KG      2.0.x compatibility                     *
41  *      1.20a 98/05/07  KG      changed names of funcs to be consistent *
42  *                              DC390_ (entry points), dc390_ (internal)*
43  *                              reworked locking                        *
44  *      1.20b 98/05/12  KG      bugs: version, kfree, _ctmp             *
45  *                              debug output                            *
46  *      1.20c 98/05/12  KG      bugs: kfree, parsing, EEpromDefaults    *
47  *      1.20d 98/05/14  KG      bugs: list linkage, clear flag after    *
48  *                              reset on startup, code cleanup          *
49  *      1.20e 98/05/15  KG      spinlock comments, name space cleanup   *
50  *                              pLastDCB now part of ACB structure      *
51  *                              added stats, timeout for 2.1, TagQ bug  *
52  *                              RESET and INQUIRY interface commands    *
53  *      1.20f 98/05/18  KG      spinlocks fixes, max_lun fix, free DCBs *
54  *                              for missing LUNs, pending int           *
55  *      1.20g 98/05/19  KG      Clean up: Avoid short                   *
56  *      1.20h 98/05/21  KG      Remove AdaptSCSIID, max_lun ...         *
57  *      1.20i 98/05/21  KG      Aiiie: Bug with TagQMask                *
58  *      1.20j 98/05/24  KG      Handle STAT_BUSY, handle pACB->pLinkDCB *
59  *                              == 0 in remove_dev and DoingSRB_Done    *
60  *      1.20k 98/05/25  KG      DMA_INT (experimental)                  *
61  *      1.20l 98/05/27  KG      remove DMA_INT; DMA_IDLE cmds added;    *
62  *      1.20m 98/06/10  KG      glitch configurable; made some global   *
63  *                              vars part of ACB; use DC390_readX       *
64  *      1.20n 98/06/11  KG      startup params                          *
65  *      1.20o 98/06/15  KG      added TagMaxNum to boot/module params   *
66  *                              Device Nr -> Idx, TagMaxNum power of 2  *
67  *      1.20p 98/06/17  KG      Docu updates. Reset depends on settings *
68  *                              pci_set_master added; 2.0.xx: pcibios_* *
69  *                              used instead of MechNum things ...      *
70  *      1.20q 98/06/23  KG      Changed defaults. Added debug code for  *
71  *                              removable media and fixed it. TagMaxNum *
72  *                              fixed for DC390. Locking: ACB, DRV for  *
73  *                              better IRQ sharing. Spelling: Queueing  *
74  *                              Parsing and glitch_cfg changes. Display *
75  *                              real SyncSpeed value. Made DisConn      *
76  *                              functional (!)                          *
77  *      1.20r 98/06/30  KG      Debug macros, allow disabling DsCn, set *
78  *                              BIT4 in CtrlR4, EN_PAGE_INT, 2.0 module *
79  *                              param -1 fixed.                         *
80  *      1.20s 98/08/20  KG      Debug info on abort(), try to check PCI,*
81  *                              phys_to_bus instead of phys_to_virt,    *
82  *                              fixed sel. process, fixed locking,      *
83  *                              added MODULE_XXX infos, changed IRQ     *
84  *                              request flags, disable DMA_INT          *
85  *      1.20t 98/09/07  KG      TagQ report fixed; Write Erase DMA Stat;*
86  *                              initfunc -> __init; better abort;       *
87  *                              Timeout for XFER_DONE & BLAST_COMPLETE; *
88  *                              Allow up to 33 commands being processed *
89  *      2.0a  98/10/14  KG      Max Cmnds back to 17. DMA_Stat clearing *
90  *                              all flags. Clear within while() loops   *
91  *                              in DataIn_0/Out_0. Null ptr in dumpinfo *
92  *                              for pSRB==0. Better locking during init.*
93  *                              bios_param() now respects part. table.  *
94  *      2.0b  98/10/24  KG      Docu fixes. Timeout Msg in DMA Blast.   *
95  *                              Disallow illegal idx in INQUIRY/REMOVE  *
96  *      2.0c  98/11/19  KG      Cleaned up detect/init for SMP boxes,   *
97  *                              Write Erase DMA (1.20t) caused problems *
98  *      2.0d  98/12/25  KG      Christmas release ;-) Message handling  *
99  *                              completely reworked. Handle target ini- *
100  *                              tiated SDTR correctly.                  *
101  *      2.0d1 99/01/25  KG      Try to handle RESTORE_PTR               *
102  *      2.0d2 99/02/08  KG      Check for failure of kmalloc, correct   *
103  *                              inclusion of scsicam.h, DelayReset      *
104  *      2.0d3 99/05/31  KG      DRIVER_OK -> DID_OK, DID_NO_CONNECT,    *
105  *                              detect Target mode and warn.            *
106  *                              pcmd->result handling cleaned up.       *
107  *      2.0d4 99/06/01  KG      Cleaned selection process. Found bug    *
108  *                              which prevented more than 16 tags. Now: *
109  *                              24. SDTR cleanup. Cleaner multi-LUN     *
110  *                              handling. Don't modify ControlRegs/FIFO *
111  *                              when connected.                         *
112  *      2.0d5 99/06/01  KG      Clear DevID, Fix INQUIRY after cfg chg. *
113  *      2.0d6 99/06/02  KG      Added ADD special command to allow cfg. *
114  *                              before detection. Reset SYNC_NEGO_DONE  *
115  *                              after a bus reset.                      *
116  *      2.0d7 99/06/03  KG      Fixed bugs wrt add,remove commands      *
117  *      2.0d8 99/06/04  KG      Removed copying of cmnd into CmdBlock.  *
118  *                              Fixed Oops in _release().               *
119  *      2.0d9 99/06/06  KG      Also tag queue INQUIRY, T_U_R, ...      *
120  *                              Allow arb. no. of Tagged Cmnds. Max 32  *
121  *      2.0d1099/06/20  KG      TagMaxNo changes now honoured! Queueing *
122  *                              clearified (renamed ..) TagMask handling*
123  *                              cleaned.                                *
124  *      2.0d1199/06/28  KG      cmd->result now identical to 2.0d2      *
125  *      2.0d1299/07/04  KG      Changed order of processing in IRQ      *
126  *      2.0d1399/07/05  KG      Don't update DCB fields if removed      *
127  *      2.0d1499/07/05  KG      remove_dev: Move kfree() to the end     *
128  *      2.0d1599/07/12  KG      use_new_eh_code: 0, ULONG -> UINT where *
129  *                              appropriate                             *
130  *      2.0d1699/07/13  KG      Reenable StartSCSI interrupt, Retry msg *
131  *      2.0d1799/07/15  KG      Remove debug msg. Disable recfg. when   *
132  *                              there are queued cmnds                  *
133  *      2.0d1899/07/18  KG      Selection timeout: Don't requeue        *
134  *      2.0d1999/07/18  KG      Abort: Only call scsi_done if dequeued  *
135  *      2.0d2099/07/19  KG      Rst_Detect: DoingSRB_Done               *
136  *      2.0d2199/08/15  KG      dev_id for request/free_irq, cmnd[0] for*
137  *                              RETRY, SRBdone does DID_ABORT for the   *
138  *                              cmd passed by DC390_reset()             *
139  *      2.0d2299/08/25  KG      dev_id fixed. can_queue: 42             *
140  *      2.0d2399/08/25  KG      Removed some debugging code. dev_id     *
141  *                              now is set to pACB. Use u8,u16,u32.     *
142  *      2.0d2499/11/14  KG      Unreg. I/O if failed IRQ alloc. Call    *
143  *                              done () w/ DID_BAD_TARGET in case of    *
144  *                              missing DCB. We are old EH!!            *
145  *      2.0d2500/01/15  KG      2.3.3x compat from Andreas Schultz      *
146  *                              set unique_id. Disable RETRY message.   *
147  *      2.0d2600/01/29  KG      Go to new EH.                           *
148  *      2.0d2700/01/31  KG      ... but maintain 2.0 compat.            *
149  *                              and fix DCB freeing                     *
150  *      2.0d2800/02/14  KG      Queue statistics fixed, dump special cmd*
151  *                              Waiting_Timer for failed StartSCSI      *
152  *                              New EH: Don't return cmnds to ML on RST *
153  *                              Use old EH (don't have new EH fns yet)  *
154  *                              Reset: Unlock, but refuse to queue      *
155  *                              2.3 __setup function                    *
156  *      2.0e  00/05/22  KG      Return residual for 2.3                 *
157  *      2.0e1 00/05/25  KG      Compile fixes for 2.3.99                *
158  *      2.0e2 00/05/27  KG      Jeff Garzik's pci_enable_device()       *
159  *      2.0e3 00/09/29  KG      Some 2.4 changes. Don't try Sync Nego   *
160  *                              before INQUIRY has reported ability.    *
161  *                              Recognise INQUIRY as scanning command.  *
162  *      2.0e4 00/10/13  KG      Allow compilation into 2.4 kernel       *
163  *      2.0e5 00/11/17  KG      Store Inq.flags in DCB                  *
164  *      2.0e6 00/11/22  KG      2.4 init function (Thx to O.Schumann)   *
165  *                              2.4 PCI device table (Thx to A.Richter) *
166  *      2.0e7 00/11/28  KG      Allow overriding of BIOS settings       *
167  *      2.0f  00/12/20  KG      Handle failed INQUIRYs during scan      *
168  *      2.1a  03/11/29  GL, KG  Initial fixing for 2.6. Convert to      *
169  *                              use the current PCI-mapping API, update *
170  *                              command-queuing.                        *
171  *      2.1b  04/04/13  GL      Fix for 64-bit platforms                *
172  *      2.1b1 04/01/31  GL      (applied 05.04) Remove internal         *
173  *                              command-queuing.                        *
174  *      2.1b2 04/02/01  CH      (applied 05.04) Fix error-handling      *
175  *      2.1c  04/05/23  GL      Update to use the new pci_driver API,   *
176  *                              some scsi EH updates, more cleanup.     *
177  *      2.1d  04/05/27  GL      Moved setting of scan_devices to        *
178  *                              slave_alloc/_configure/_destroy, as     *
179  *                              suggested by CH.                        *
180  ***********************************************************************/
181
182 /* DEBUG options */
183 //#define DC390_DEBUG0
184 //#define DC390_DEBUG1
185 //#define DC390_DCBDEBUG
186 //#define DC390_PARSEDEBUG
187 //#define DC390_REMOVABLEDEBUG
188 //#define DC390_LOCKDEBUG
189
190 //#define NOP do{}while(0)
191 #define C_NOP
192
193 /* Debug definitions */
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
254 static void dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
255 static void dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
256 static void dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
257 static void dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
258 static void dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
259 static void dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
260 static void dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
261 static void dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
262 static void dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
263 static void dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
264 static void dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
265 static void dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
266 static void dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
267 static void dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
268
269 static void dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB );
270 static void dc390_Disconnect( struct dc390_acb* pACB );
271 static void dc390_Reselect( struct dc390_acb* pACB );
272 static void dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB );
273 static void dc390_ScsiRstDetect( struct dc390_acb* pACB );
274 static void dc390_EnableMsgOut_Abort(struct dc390_acb*, struct dc390_srb*);
275 static void dc390_dumpinfo(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB);
276 static void dc390_ResetDevParam(struct dc390_acb* pACB);
277
278 static u32      dc390_laststatus = 0;
279 static u8       dc390_adapterCnt = 0;
280
281 static int disable_clustering;
282 module_param(disable_clustering, int, S_IRUGO);
283 MODULE_PARM_DESC(disable_clustering, "If you experience problems with your devices, try setting to 1");
284
285 /* Startup values, to be overriden on the commandline */
286 static int tmscsim[] = {-2, -2, -2, -2, -2, -2};
287
288 module_param_array(tmscsim, int, NULL, 0);
289 MODULE_PARM_DESC(tmscsim, "Host SCSI ID, Speed (0=10MHz), Device Flags, Adapter Flags, Max Tags (log2(tags)-1), DelayReset (s)");
290 MODULE_AUTHOR("C.L. Huang / Kurt Garloff");
291 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram DC390 and other AMD53C974A based PCI SCSI adapters");
292 MODULE_LICENSE("GPL");
293 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
294
295 static 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
307 static 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
320 static 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      
332 static 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
345 static u8  dc390_eepromBuf[MAX_ADAPTER_NUM][EE_LEN];
346 static u8  dc390_clock_period1[] = {4, 5, 6, 7, 8, 10, 13, 20};
347 static u8  dc390_clock_speed[] = {100,80,67,57,50, 40, 31, 20};
348
349 /***********************************************************************
350  * Functions for the management of the internal structures 
351  * (DCBs, SRBs, Queueing)
352  *
353  **********************************************************************/
354 static void inline dc390_start_segment(struct dc390_srb* pSRB)
355 {
356         struct scatterlist *psgl = pSRB->pSegmentList;
357
358         /* start new sg segment */
359         pSRB->SGBusAddr = sg_dma_address(psgl);
360         pSRB->SGToBeXferLen = sg_dma_len(psgl);
361 }
362
363 static unsigned long inline dc390_advance_segment(struct dc390_srb* pSRB, u32 residue)
364 {
365         unsigned long xfer = pSRB->SGToBeXferLen - residue;
366
367         /* xfer more bytes transferred */
368         pSRB->SGBusAddr += xfer;
369         pSRB->TotalXferredLen += xfer;
370         pSRB->SGToBeXferLen = residue;
371
372         return xfer;
373 }
374
375 static 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 /* Insert SRB oin top of free list */
390 static __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
397 static __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     /* Append to the list of Going commands */
402     if( pDCB->pGoingSRB )
403         pDCB->pGoingLast->pNextSRB = pSRB;
404     else
405         pDCB->pGoingSRB = pSRB;
406
407     pDCB->pGoingLast = pSRB;
408     /* No next one in sent list */
409     pSRB->pNextSRB = NULL;
410 }
411
412 static __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
431 static struct scatterlist* dc390_sg_build_single(struct scatterlist *sg, void *addr, unsigned int length)
432 {
433         memset(sg, 0, sizeof(struct scatterlist));
434         sg->page        = virt_to_page(addr);
435         sg->length      = length;
436         sg->offset      = (unsigned long)addr & ~PAGE_MASK;
437         return sg;
438 }
439
440 /* Create pci mapping */
441 static int dc390_pci_map (struct dc390_srb* pSRB)
442 {
443         int error = 0;
444         struct scsi_cmnd *pcmd = pSRB->pcmd;
445         struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
446         dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp));
447
448         /* Map sense buffer */
449         if (pSRB->SRBFlag & AUTO_REQSENSE) {
450                 pSRB->pSegmentList      = dc390_sg_build_single(&pSRB->Segmentx, pcmd->sense_buffer, sizeof(pcmd->sense_buffer));
451                 pSRB->SGcount           = pci_map_sg(pdev, pSRB->pSegmentList, 1,
452                                                      DMA_FROM_DEVICE);
453                 cmdp->saved_dma_handle  = sg_dma_address(pSRB->pSegmentList);
454
455                 /* TODO: error handling */
456                 if (pSRB->SGcount != 1)
457                         error = 1;
458                 DEBUG1(printk("%s(): Mapped sense buffer %p at %x\n", __FUNCTION__, pcmd->sense_buffer, cmdp->saved_dma_handle));
459         /* Map SG list */
460         } else if (pcmd->use_sg) {
461                 pSRB->pSegmentList      = (struct scatterlist *) pcmd->request_buffer;
462                 pSRB->SGcount           = pci_map_sg(pdev, pSRB->pSegmentList, pcmd->use_sg,
463                                                      pcmd->sc_data_direction);
464                 /* TODO: error handling */
465                 if (!pSRB->SGcount)
466                         error = 1;
467                 DEBUG1(printk("%s(): Mapped SG %p with %d (%d) elements\n",\
468                               __FUNCTION__, pcmd->request_buffer, pSRB->SGcount, pcmd->use_sg));
469         /* Map single segment */
470         } else if (pcmd->request_buffer && pcmd->request_bufflen) {
471                 pSRB->pSegmentList      = dc390_sg_build_single(&pSRB->Segmentx, pcmd->request_buffer, pcmd->request_bufflen);
472                 pSRB->SGcount           = pci_map_sg(pdev, pSRB->pSegmentList, 1,
473                                                      pcmd->sc_data_direction);
474                 cmdp->saved_dma_handle  = sg_dma_address(pSRB->pSegmentList);
475
476                 /* TODO: error handling */
477                 if (pSRB->SGcount != 1)
478                         error = 1;
479                 DEBUG1(printk("%s(): Mapped request buffer %p at %x\n", __FUNCTION__, pcmd->request_buffer, cmdp->saved_dma_handle));
480         /* No mapping !? */     
481         } else
482                 pSRB->SGcount = 0;
483
484         return error;
485 }
486
487 /* Remove pci mapping */
488 static void dc390_pci_unmap (struct dc390_srb* pSRB)
489 {
490         struct scsi_cmnd *pcmd = pSRB->pcmd;
491         struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
492         DEBUG1(dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp)));
493
494         if (pSRB->SRBFlag) {
495                 pci_unmap_sg(pdev, &pSRB->Segmentx, 1, DMA_FROM_DEVICE);
496                 DEBUG1(printk("%s(): Unmapped sense buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
497         } else if (pcmd->use_sg) {
498                 pci_unmap_sg(pdev, pcmd->request_buffer, pcmd->use_sg, pcmd->sc_data_direction);
499                 DEBUG1(printk("%s(): Unmapped SG at %p with %d elements\n", __FUNCTION__, pcmd->request_buffer, pcmd->use_sg));
500         } else if (pcmd->request_buffer && pcmd->request_bufflen) {
501                 pci_unmap_sg(pdev, &pSRB->Segmentx, 1, pcmd->sc_data_direction);
502                 DEBUG1(printk("%s(): Unmapped request buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
503         }
504 }
505
506 static void __inline__
507 dc390_freetag (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
508 {
509         if (pSRB->TagNumber != SCSI_NO_TAG) {
510                 pDCB->TagMask &= ~(1 << pSRB->TagNumber);   /* free tag mask */
511                 pSRB->TagNumber = SCSI_NO_TAG;
512         }
513 }
514
515
516 static int
517 dc390_StartSCSI( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
518 {
519     struct scsi_cmnd *scmd = pSRB->pcmd;
520     struct scsi_device *sdev = scmd->device;
521     u8 cmd, disc_allowed, try_sync_nego;
522     char tag[2];
523
524     pSRB->ScsiPhase = SCSI_NOP0;
525
526     if (pACB->Connected)
527     {
528         // Should not happen normally
529         printk (KERN_WARNING "DC390: Can't select when connected! (%08x,%02x)\n",
530                 pSRB->SRBState, pSRB->SRBFlag);
531         pSRB->SRBState = SRB_READY;
532         pACB->SelConn++;
533         return 1;
534     }
535     if (time_before (jiffies, pACB->pScsiHost->last_reset))
536     {
537         DEBUG0(printk ("DC390: We were just reset and don't accept commands yet!\n"));
538         return 1;
539     }
540     /* KG: Moved pci mapping here */
541     dc390_pci_map(pSRB);
542     /* TODO: error handling */
543     DC390_write8 (Scsi_Dest_ID, pDCB->TargetID);
544     DC390_write8 (Sync_Period, pDCB->SyncPeriod);
545     DC390_write8 (Sync_Offset, pDCB->SyncOffset);
546     DC390_write8 (CtrlReg1, pDCB->CtrlR1);
547     DC390_write8 (CtrlReg3, pDCB->CtrlR3);
548     DC390_write8 (CtrlReg4, pDCB->CtrlR4);
549     DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);             /* Flush FIFO */
550     DEBUG1(printk (KERN_INFO "DC390: Start SCSI command: %02x (Sync:%02x)\n",\
551             scmd->cmnd[0], pDCB->SyncMode));
552
553     /* Don't disconnect on AUTO_REQSENSE, cause it might be an
554      * Contingent Allegiance Condition (6.6), where no tags should be used.
555      * All other have to be allowed to disconnect to prevent Incorrect 
556      * Initiator Connection (6.8.2/6.5.2) */
557     /* Changed KG, 99/06/06 */
558     if (! (pSRB->SRBFlag & AUTO_REQSENSE))
559         disc_allowed = pDCB->DevMode & EN_DISCONNECT_;
560     else
561         disc_allowed = 0;
562
563     if ((pDCB->SyncMode & SYNC_ENABLE) && pDCB->TargetLUN == 0 && sdev->sdtr &&
564         (((scmd->cmnd[0] == REQUEST_SENSE || (pSRB->SRBFlag & AUTO_REQSENSE)) &&
565           !(pDCB->SyncMode & SYNC_NEGO_DONE)) || scmd->cmnd[0] == INQUIRY))
566       try_sync_nego = 1;
567     else
568       try_sync_nego = 0;
569
570     pSRB->MsgCnt = 0;
571     cmd = SEL_W_ATN;
572     DC390_write8 (ScsiFifo, IDENTIFY(disc_allowed, pDCB->TargetLUN));
573     /* Change 99/05/31: Don't use tags when not disconnecting (BUSY) */
574     if ((pDCB->SyncMode & EN_TAG_QUEUEING) && disc_allowed && scsi_populate_tag_msg(scmd, tag)) {
575         DC390_write8(ScsiFifo, tag[0]);
576         pDCB->TagMask |= 1 << tag[1];
577         pSRB->TagNumber = tag[1];
578         DC390_write8(ScsiFifo, tag[1]);
579         DEBUG1(printk(KERN_INFO "DC390: Select w/DisCn for Cmd %li (SRB %p), block tag %02x\n", scmd->pid, pSRB, tag[1]));
580         cmd = SEL_W_ATN3;
581     } else {
582         /* No TagQ */
583 //no_tag:
584         DEBUG1(printk(KERN_INFO "DC390: Select w%s/DisCn for Cmd %li (SRB %p), No TagQ\n", disc_allowed ? "" : "o", scmd->pid, pSRB));
585     }
586
587     pSRB->SRBState = SRB_START_;
588
589     if (try_sync_nego)
590       { 
591         u8 Sync_Off = pDCB->SyncOffset;
592         DEBUG0(printk (KERN_INFO "DC390: NEW Sync Nego code triggered (%i %i)\n", pDCB->TargetID, pDCB->TargetLUN));
593         pSRB->MsgOutBuf[0] = EXTENDED_MESSAGE;
594         pSRB->MsgOutBuf[1] = 3;
595         pSRB->MsgOutBuf[2] = EXTENDED_SDTR;
596         pSRB->MsgOutBuf[3] = pDCB->NegoPeriod;
597         if (!(Sync_Off & 0x0f)) Sync_Off = SYNC_NEGO_OFFSET;
598         pSRB->MsgOutBuf[4] = Sync_Off;
599         pSRB->MsgCnt = 5;
600         //pSRB->SRBState = SRB_MSGOUT_;
601         pSRB->SRBState |= DO_SYNC_NEGO;
602         cmd = SEL_W_ATN_STOP;
603       }
604
605     /* Command is written in CommandPhase, if SEL_W_ATN_STOP ... */
606     if (cmd != SEL_W_ATN_STOP)
607       {
608         if( pSRB->SRBFlag & AUTO_REQSENSE )
609           {
610             DC390_write8 (ScsiFifo, REQUEST_SENSE);
611             DC390_write8 (ScsiFifo, pDCB->TargetLUN << 5);
612             DC390_write8 (ScsiFifo, 0);
613             DC390_write8 (ScsiFifo, 0);
614             DC390_write8 (ScsiFifo, sizeof(scmd->sense_buffer));
615             DC390_write8 (ScsiFifo, 0);
616             DEBUG1(printk (KERN_DEBUG "DC390: AutoReqSense !\n"));
617           }
618         else    /* write cmnd to bus */ 
619           {
620             u8 *ptr; u8 i;
621             ptr = (u8 *)scmd->cmnd;
622             for (i = 0; i < scmd->cmd_len; i++)
623               DC390_write8 (ScsiFifo, *(ptr++));
624           }
625       }
626     DEBUG0(if (pACB->pActiveDCB)        \
627            printk (KERN_WARNING "DC390: ActiveDCB != 0\n"));
628     DEBUG0(if (pDCB->pActiveSRB)        \
629            printk (KERN_WARNING "DC390: ActiveSRB != 0\n"));
630     //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
631     if (DC390_read8 (Scsi_Status) & INTERRUPT)
632     {
633         dc390_freetag (pDCB, pSRB);
634         DEBUG0(printk ("DC390: Interrupt during Start SCSI (pid %li, target %02i-%02i)\n",
635                 scmd->pid, scmd->device->id, scmd->device->lun));
636         pSRB->SRBState = SRB_READY;
637         //DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
638         pACB->SelLost++;
639         return 1;
640     }
641     DC390_write8 (ScsiCmd, cmd);
642     pACB->pActiveDCB = pDCB;
643     pDCB->pActiveSRB = pSRB;
644     pACB->Connected = 1;
645     pSRB->ScsiPhase = SCSI_NOP1;
646     return 0;
647 }
648
649
650 static void __inline__
651 dc390_InvalidCmd(struct dc390_acb* pACB)
652 {
653         if (pACB->pActiveDCB->pActiveSRB->SRBState & (SRB_START_ | SRB_MSGOUT))
654                 DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
655 }
656
657
658 static irqreturn_t __inline__
659 DC390_Interrupt(void *dev_id)
660 {
661     struct dc390_acb *pACB = dev_id;
662     struct dc390_dcb *pDCB;
663     struct dc390_srb *pSRB;
664     u8  sstatus=0;
665     u8  phase;
666     void   (*stateV)( struct dc390_acb*, struct dc390_srb*, u8 *);
667     u8  istate, istatus;
668
669     sstatus = DC390_read8 (Scsi_Status);
670     if( !(sstatus & INTERRUPT) )
671         return IRQ_NONE;
672
673     DEBUG1(printk (KERN_DEBUG "sstatus=%02x,", sstatus));
674
675     //DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT);
676     //dstatus = DC390_read8 (DMA_Status);
677     //DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
678
679     spin_lock_irq(pACB->pScsiHost->host_lock);
680
681     istate = DC390_read8 (Intern_State);
682     istatus = DC390_read8 (INT_Status); /* This clears Scsi_Status, Intern_State and INT_Status ! */
683
684     DEBUG1(printk (KERN_INFO "Istatus(Res,Inv,Dis,Serv,Succ,ReS,SelA,Sel)=%02x,",istatus));
685     dc390_laststatus &= ~0x00ffffff;
686     dc390_laststatus |= /* dstatus<<24 | */ sstatus<<16 | istate<<8 | istatus;
687
688     if (sstatus & ILLEGAL_OP_ERR)
689     {
690         printk ("DC390: Illegal Operation detected (%08x)!\n", dc390_laststatus);
691         dc390_dumpinfo (pACB, pACB->pActiveDCB, pACB->pActiveDCB->pActiveSRB);
692     }
693         
694     else if (istatus &  INVALID_CMD)
695     {
696         printk ("DC390: Invalid Command detected (%08x)!\n", dc390_laststatus);
697         dc390_InvalidCmd( pACB );
698         goto unlock;
699     }
700
701     if (istatus &  SCSI_RESET)
702     {
703         dc390_ScsiRstDetect( pACB );
704         goto unlock;
705     }
706
707     if (istatus &  DISCONNECTED)
708     {
709         dc390_Disconnect( pACB );
710         goto unlock;
711     }
712
713     if (istatus &  RESELECTED)
714     {
715         dc390_Reselect( pACB );
716         goto unlock;
717     }
718
719     else if (istatus & (SELECTED | SEL_ATTENTION))
720     {
721         printk (KERN_ERR "DC390: Target mode not supported!\n");
722         goto unlock;
723     }
724
725     if (istatus & (SUCCESSFUL_OP|SERVICE_REQUEST) )
726     {
727         pDCB = pACB->pActiveDCB;
728         if (!pDCB)
729         {
730                 printk (KERN_ERR "DC390: Suc. op/ Serv. req: pActiveDCB = 0!\n");
731                 goto unlock;
732         }
733         pSRB = pDCB->pActiveSRB;
734         if( pDCB->DCBFlag & ABORT_DEV_ )
735           dc390_EnableMsgOut_Abort (pACB, pSRB);
736
737         phase = pSRB->ScsiPhase;
738         DEBUG1(printk (KERN_INFO "DC390: [%i]%s(0) (%02x)\n", phase, dc390_p0_str[phase], sstatus));
739         stateV = (void *) dc390_phase0[phase];
740         ( *stateV )( pACB, pSRB, &sstatus );
741
742         pSRB->ScsiPhase = sstatus & 7;
743         phase = (u8) sstatus & 7;
744         DEBUG1(printk (KERN_INFO "DC390: [%i]%s(1) (%02x)\n", phase, dc390_p1_str[phase], sstatus));
745         stateV = (void *) dc390_phase1[phase];
746         ( *stateV )( pACB, pSRB, &sstatus );
747     }
748
749  unlock:
750     spin_unlock_irq(pACB->pScsiHost->host_lock);
751     return IRQ_HANDLED;
752 }
753
754 static irqreturn_t do_DC390_Interrupt(int irq, void *dev_id)
755 {
756     irqreturn_t ret;
757     DEBUG1(printk (KERN_INFO "DC390: Irq (%i) caught: ", irq));
758     /* Locking is done in DC390_Interrupt */
759     ret = DC390_Interrupt(dev_id);
760     DEBUG1(printk (".. IRQ returned\n"));
761     return ret;
762 }
763
764 static void
765 dc390_DataOut_0(struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
766 {
767     u8   sstatus;
768     u32  ResidCnt;
769     u8   dstate = 0;
770
771     sstatus = *psstatus;
772
773     if( !(pSRB->SRBState & SRB_XFERPAD) )
774     {
775         if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR) )
776             pSRB->SRBStatus |= PARITY_ERROR;
777
778         if( sstatus & COUNT_2_ZERO )
779         {
780             unsigned long timeout = jiffies + HZ;
781
782             /* Function called from the ISR with the host_lock held and interrupts disabled */
783             if (pSRB->SGToBeXferLen)
784                 while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) {
785                     spin_unlock_irq(pACB->pScsiHost->host_lock);
786                     udelay(50);
787                     spin_lock_irq(pACB->pScsiHost->host_lock);
788                 }
789             if (!time_before(jiffies, timeout))
790                 printk (KERN_CRIT "DC390: Deadlock in DataOut_0: DMA aborted unfinished: %06x bytes remain!!\n",
791                         DC390_read32 (DMA_Wk_ByteCntr));
792             dc390_laststatus &= ~0xff000000;
793             dc390_laststatus |= dstate << 24;
794             pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
795             pSRB->SGIndex++;
796             if( pSRB->SGIndex < pSRB->SGcount )
797             {
798                 pSRB->pSegmentList++;
799
800                 dc390_start_segment(pSRB);
801             }
802             else
803                 pSRB->SGToBeXferLen = 0;
804         }
805         else
806         {
807             ResidCnt = ((u32) DC390_read8 (Current_Fifo) & 0x1f) +
808                     (((u32) DC390_read8 (CtcReg_High) << 16) |
809                      ((u32) DC390_read8 (CtcReg_Mid) << 8) |
810                      (u32) DC390_read8 (CtcReg_Low));
811
812             dc390_advance_segment(pSRB, ResidCnt);
813         }
814     }
815     if ((*psstatus & 7) != SCSI_DATA_OUT)
816     {
817             DC390_write8 (DMA_Cmd, WRITE_DIRECTION+DMA_IDLE_CMD);
818             DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
819     }       
820 }
821
822 static void
823 dc390_DataIn_0(struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
824 {
825     u8   sstatus, residual, bval;
826     u32  ResidCnt, i;
827     unsigned long   xferCnt;
828
829     sstatus = *psstatus;
830
831     if( !(pSRB->SRBState & SRB_XFERPAD) )
832     {
833         if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR))
834             pSRB->SRBStatus |= PARITY_ERROR;
835
836         if( sstatus & COUNT_2_ZERO )
837         {
838             int dstate = 0;
839             unsigned long timeout = jiffies + HZ;
840
841             /* Function called from the ISR with the host_lock held and interrupts disabled */
842             if (pSRB->SGToBeXferLen)
843                 while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) {
844                     spin_unlock_irq(pACB->pScsiHost->host_lock);
845                     udelay(50);
846                     spin_lock_irq(pACB->pScsiHost->host_lock);
847                 }
848             if (!time_before(jiffies, timeout)) {
849                 printk (KERN_CRIT "DC390: Deadlock in DataIn_0: DMA aborted unfinished: %06x bytes remain!!\n",
850                         DC390_read32 (DMA_Wk_ByteCntr));
851                 printk (KERN_CRIT "DC390: DataIn_0: DMA State: %i\n", dstate);
852             }
853             dc390_laststatus &= ~0xff000000;
854             dc390_laststatus |= dstate << 24;
855             DEBUG1(ResidCnt = ((unsigned long) DC390_read8 (CtcReg_High) << 16) \
856                 + ((unsigned long) DC390_read8 (CtcReg_Mid) << 8)               \
857                 + ((unsigned long) DC390_read8 (CtcReg_Low)));
858             DEBUG1(printk (KERN_DEBUG "Count_2_Zero (ResidCnt=%u,ToBeXfer=%lu),", ResidCnt, pSRB->SGToBeXferLen));
859
860             DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD);
861
862             pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
863             pSRB->SGIndex++;
864             if( pSRB->SGIndex < pSRB->SGcount )
865             {
866                 pSRB->pSegmentList++;
867
868                 dc390_start_segment(pSRB);
869             }
870             else
871                 pSRB->SGToBeXferLen = 0;
872         }
873         else    /* phase changed */
874         {
875             residual = 0;
876             bval = DC390_read8 (Current_Fifo);
877             while( bval & 0x1f )
878             {
879                 DEBUG1(printk (KERN_DEBUG "Check for residuals,"));
880                 if( (bval & 0x1f) == 1 )
881                 {
882                     for(i=0; i < 0x100; i++)
883                     {
884                         bval = DC390_read8 (Current_Fifo);
885                         if( !(bval & 0x1f) )
886                             goto din_1;
887                         else if( i == 0x0ff )
888                         {
889                             residual = 1;   /* ;1 residual byte */
890                             goto din_1;
891                         }
892                     }
893                 }
894                 else
895                     bval = DC390_read8 (Current_Fifo);
896             }
897 din_1:
898             DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_BLAST_CMD);
899             for (i = 0xa000; i; i--)
900             {
901                 bval = DC390_read8 (DMA_Status);
902                 if (bval & BLAST_COMPLETE)
903                     break;
904             }
905             /* It seems a DMA Blast abort isn't that bad ... */
906             if (!i) printk (KERN_ERR "DC390: DMA Blast aborted unfinished!\n");
907             //DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD);
908             dc390_laststatus &= ~0xff000000;
909             dc390_laststatus |= bval << 24;
910
911             DEBUG1(printk (KERN_DEBUG "Blast: Read %i times DMA_Status %02x", 0xa000-i, bval));
912             ResidCnt = (((u32) DC390_read8 (CtcReg_High) << 16) |
913                         ((u32) DC390_read8 (CtcReg_Mid) << 8)) |
914                     (u32) DC390_read8 (CtcReg_Low);
915
916             xferCnt = dc390_advance_segment(pSRB, ResidCnt);
917
918             if (residual) {
919                 size_t count = 1;
920                 size_t offset = pSRB->SGBusAddr - sg_dma_address(pSRB->pSegmentList);
921                 unsigned long flags;
922                 u8 *ptr;
923
924                 bval = DC390_read8 (ScsiFifo);      /* get one residual byte */
925
926                 local_irq_save(flags);
927                 ptr = scsi_kmap_atomic_sg(pSRB->pSegmentList, pSRB->SGcount, &offset, &count);
928                 if (likely(ptr)) {
929                         *(ptr + offset) = bval;
930                         scsi_kunmap_atomic_sg(ptr);
931                 }
932                 local_irq_restore(flags);
933                 WARN_ON(!ptr);
934
935                 /* 1 more byte read */
936                 xferCnt += dc390_advance_segment(pSRB, pSRB->SGToBeXferLen - 1);
937             }
938             DEBUG1(printk (KERN_DEBUG "Xfered: %lu, Total: %lu, Remaining: %lu\n", xferCnt,\
939                            pSRB->TotalXferredLen, pSRB->SGToBeXferLen));
940         }
941     }
942     if ((*psstatus & 7) != SCSI_DATA_IN)
943     {
944             DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
945             DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD);
946     }
947 }
948
949 static void
950 dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
951 {
952 }
953
954 static void
955 dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
956 {
957
958     pSRB->TargetStatus = DC390_read8 (ScsiFifo);
959     //udelay (1);
960     pSRB->EndMessage = DC390_read8 (ScsiFifo);  /* get message */
961
962     *psstatus = SCSI_NOP0;
963     pSRB->SRBState = SRB_COMPLETED;
964     DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);
965 }
966
967 static void
968 dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
969 {
970     if( pSRB->SRBState & (SRB_UNEXPECT_RESEL+SRB_ABORT_SENT) )
971         *psstatus = SCSI_NOP0;
972     //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
973 }
974
975
976 static void __inline__
977 dc390_reprog (struct dc390_acb* pACB, struct dc390_dcb* pDCB)
978 {
979   DC390_write8 (Sync_Period, pDCB->SyncPeriod);
980   DC390_write8 (Sync_Offset, pDCB->SyncOffset);
981   DC390_write8 (CtrlReg3, pDCB->CtrlR3);
982   DC390_write8 (CtrlReg4, pDCB->CtrlR4);
983   dc390_SetXferRate (pACB, pDCB);
984 }
985
986
987 #ifdef DC390_DEBUG0
988 static void
989 dc390_printMsg (u8 *MsgBuf, u8 len)
990 {
991   int i;
992   printk (" %02x", MsgBuf[0]);
993   for (i = 1; i < len; i++)
994     printk (" %02x", MsgBuf[i]);
995   printk ("\n");
996 }
997 #endif
998
999 #define DC390_ENABLE_MSGOUT DC390_write8 (ScsiCmd, SET_ATN_CMD)
1000
1001 /* reject_msg */
1002 static void __inline__
1003 dc390_MsgIn_reject (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1004 {
1005   pSRB->MsgOutBuf[0] = MESSAGE_REJECT;
1006   pSRB->MsgCnt = 1;
1007   DC390_ENABLE_MSGOUT;
1008   DEBUG0 (printk (KERN_INFO "DC390: Reject message\n"));
1009 }
1010
1011 /* abort command */
1012 static void
1013 dc390_EnableMsgOut_Abort ( struct dc390_acb* pACB, struct dc390_srb* pSRB )
1014 {
1015     pSRB->MsgOutBuf[0] = ABORT; 
1016     pSRB->MsgCnt = 1; DC390_ENABLE_MSGOUT;
1017     pSRB->pSRBDCB->DCBFlag &= ~ABORT_DEV_;
1018 }
1019
1020 static struct dc390_srb*
1021 dc390_MsgIn_QTag (struct dc390_acb* pACB, struct dc390_dcb* pDCB, s8 tag)
1022 {
1023   struct dc390_srb* pSRB = pDCB->pGoingSRB;
1024
1025   if (pSRB)
1026     {
1027         struct scsi_cmnd *scmd = scsi_find_tag(pSRB->pcmd->device, tag);
1028         pSRB = (struct dc390_srb *)scmd->host_scribble;
1029
1030         if (pDCB->DCBFlag & ABORT_DEV_)
1031         {
1032           pSRB->SRBState = SRB_ABORT_SENT;
1033           dc390_EnableMsgOut_Abort( pACB, pSRB );
1034         }
1035
1036         if (!(pSRB->SRBState & SRB_DISCONNECT))
1037                 goto mingx0;
1038
1039         pDCB->pActiveSRB = pSRB;
1040         pSRB->SRBState = SRB_DATA_XFER;
1041     }
1042   else
1043     {
1044     mingx0:
1045       pSRB = pACB->pTmpSRB;
1046       pSRB->SRBState = SRB_UNEXPECT_RESEL;
1047       pDCB->pActiveSRB = pSRB;
1048       pSRB->MsgOutBuf[0] = ABORT_TAG;
1049       pSRB->MsgCnt = 1; DC390_ENABLE_MSGOUT;
1050     }
1051   return pSRB;
1052 }
1053
1054
1055 /* set async transfer mode */
1056 static void 
1057 dc390_MsgIn_set_async (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1058 {
1059   struct dc390_dcb* pDCB = pSRB->pSRBDCB;
1060   if (!(pSRB->SRBState & DO_SYNC_NEGO)) 
1061     printk (KERN_INFO "DC390: Target %i initiates Non-Sync?\n", pDCB->TargetID);
1062   pSRB->SRBState &= ~DO_SYNC_NEGO;
1063   pDCB->SyncMode &= ~(SYNC_ENABLE+SYNC_NEGO_DONE);
1064   pDCB->SyncPeriod = 0;
1065   pDCB->SyncOffset = 0;
1066   //pDCB->NegoPeriod = 50; /* 200ns <=> 5 MHz */
1067   pDCB->CtrlR3 = FAST_CLK;      /* fast clock / normal scsi */
1068   pDCB->CtrlR4 &= 0x3f;
1069   pDCB->CtrlR4 |= pACB->glitch_cfg;     /* glitch eater */
1070   dc390_reprog (pACB, pDCB);
1071 }
1072
1073 /* set sync transfer mode */
1074 static void
1075 dc390_MsgIn_set_sync (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1076 {
1077   u8 bval;
1078   u16 wval, wval1;
1079   struct dc390_dcb* pDCB = pSRB->pSRBDCB;
1080   u8 oldsyncperiod = pDCB->SyncPeriod;
1081   u8 oldsyncoffset = pDCB->SyncOffset;
1082   
1083   if (!(pSRB->SRBState & DO_SYNC_NEGO))
1084     {
1085       printk (KERN_INFO "DC390: Target %i initiates Sync: %ins %i ... answer ...\n", 
1086               pDCB->TargetID, pSRB->MsgInBuf[3]<<2, pSRB->MsgInBuf[4]);
1087
1088       /* reject */
1089       //dc390_MsgIn_reject (pACB, pSRB);
1090       //return dc390_MsgIn_set_async (pACB, pSRB);
1091
1092       /* Reply with corrected SDTR Message */
1093       if (pSRB->MsgInBuf[4] > 15)
1094         { 
1095           printk (KERN_INFO "DC390: Lower Sync Offset to 15\n");
1096           pSRB->MsgInBuf[4] = 15;
1097         }
1098       if (pSRB->MsgInBuf[3] < pDCB->NegoPeriod)
1099         {
1100           printk (KERN_INFO "DC390: Set sync nego period to %ins\n", pDCB->NegoPeriod << 2);
1101           pSRB->MsgInBuf[3] = pDCB->NegoPeriod;
1102         }
1103       memcpy (pSRB->MsgOutBuf, pSRB->MsgInBuf, 5);
1104       pSRB->MsgCnt = 5;
1105       DC390_ENABLE_MSGOUT;
1106     }
1107
1108   pSRB->SRBState &= ~DO_SYNC_NEGO;
1109   pDCB->SyncMode |= SYNC_ENABLE+SYNC_NEGO_DONE;
1110   pDCB->SyncOffset &= 0x0f0;
1111   pDCB->SyncOffset |= pSRB->MsgInBuf[4];
1112   pDCB->NegoPeriod = pSRB->MsgInBuf[3];
1113
1114   wval = (u16) pSRB->MsgInBuf[3];
1115   wval = wval << 2; wval -= 3; wval1 = wval / 25;       /* compute speed */
1116   if( (wval1 * 25) != wval) wval1++;
1117   bval = FAST_CLK+FAST_SCSI;    /* fast clock / fast scsi */
1118
1119   pDCB->CtrlR4 &= 0x3f;         /* Glitch eater: 12ns less than normal */
1120   if (pACB->glitch_cfg != NS_TO_GLITCH(0))
1121     pDCB->CtrlR4 |= NS_TO_GLITCH(((GLITCH_TO_NS(pACB->glitch_cfg)) - 1));
1122   else
1123     pDCB->CtrlR4 |= NS_TO_GLITCH(0);
1124   if (wval1 < 4) pDCB->CtrlR4 |= NS_TO_GLITCH(0); /* Ultra */
1125
1126   if (wval1 >= 8)
1127     {
1128       wval1--;  /* Timing computation differs by 1 from FAST_SCSI */
1129       bval = FAST_CLK;          /* fast clock / normal scsi */
1130       pDCB->CtrlR4 |= pACB->glitch_cfg;         /* glitch eater */
1131     }
1132
1133   pDCB->CtrlR3 = bval;
1134   pDCB->SyncPeriod = (u8)wval1;
1135   
1136   if ((oldsyncperiod != wval1 || oldsyncoffset != pDCB->SyncOffset) && pDCB->TargetLUN == 0)
1137     {
1138       if (! (bval & FAST_SCSI)) wval1++;
1139       printk (KERN_INFO "DC390: Target %i: Sync transfer %i.%1i MHz, Offset %i\n", pDCB->TargetID, 
1140               40/wval1, ((40%wval1)*10+wval1/2)/wval1, pDCB->SyncOffset & 0x0f);
1141     }
1142   
1143   dc390_reprog (pACB, pDCB);
1144 }
1145
1146
1147 /* handle RESTORE_PTR */
1148 /* This doesn't look very healthy... to-be-fixed */
1149 static void 
1150 dc390_restore_ptr (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1151 {
1152     struct scsi_cmnd *pcmd = pSRB->pcmd;
1153     struct scatterlist *psgl;
1154     pSRB->TotalXferredLen = 0;
1155     pSRB->SGIndex = 0;
1156     if (pcmd->use_sg) {
1157         size_t saved;
1158         pSRB->pSegmentList = (struct scatterlist *)pcmd->request_buffer;
1159         psgl = pSRB->pSegmentList;
1160         //dc390_pci_sync(pSRB);
1161
1162         while (pSRB->TotalXferredLen + (unsigned long) sg_dma_len(psgl) < pSRB->Saved_Ptr)
1163         {
1164             pSRB->TotalXferredLen += (unsigned long) sg_dma_len(psgl);
1165             pSRB->SGIndex++;
1166             if( pSRB->SGIndex < pSRB->SGcount )
1167             {
1168                 pSRB->pSegmentList++;
1169
1170                 dc390_start_segment(pSRB);
1171             }
1172             else
1173                 pSRB->SGToBeXferLen = 0;
1174         }
1175
1176         saved = pSRB->Saved_Ptr - pSRB->TotalXferredLen;
1177         pSRB->SGToBeXferLen -= saved;
1178         pSRB->SGBusAddr += saved;
1179         printk (KERN_INFO "DC390: Pointer restored. Segment %i, Total %li, Bus %08lx\n",
1180                 pSRB->SGIndex, pSRB->Saved_Ptr, pSRB->SGBusAddr);
1181
1182     } else if(pcmd->request_buffer) {
1183         //dc390_pci_sync(pSRB);
1184
1185         sg_dma_len(&pSRB->Segmentx) = pcmd->request_bufflen - pSRB->Saved_Ptr;
1186         pSRB->SGcount = 1;
1187         pSRB->pSegmentList = (struct scatterlist *) &pSRB->Segmentx;
1188     } else {
1189          pSRB->SGcount = 0;
1190          printk (KERN_INFO "DC390: RESTORE_PTR message for Transfer without Scatter-Gather ??\n");
1191     }
1192
1193   pSRB->TotalXferredLen = pSRB->Saved_Ptr;
1194 }
1195
1196
1197 /* According to the docs, the AM53C974 reads the message and 
1198  * generates a Successful Operation IRQ before asserting ACK for
1199  * the last byte (how does it know whether it's the last ?) */
1200 /* The old code handled it in another way, indicating, that on
1201  * every message byte an IRQ is generated and every byte has to
1202  * be manually ACKed. Hmmm ?  (KG, 98/11/28) */
1203 /* The old implementation was correct. Sigh! */
1204
1205 /* Check if the message is complete */
1206 static u8 __inline__
1207 dc390_MsgIn_complete (u8 *msgbuf, u32 len)
1208
1209   if (*msgbuf == EXTENDED_MESSAGE)
1210   {
1211         if (len < 2) return 0;
1212         if (len < msgbuf[1] + 2) return 0;
1213   }
1214   else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) // two byte messages
1215         if (len < 2) return 0;
1216   return 1;
1217 }
1218
1219
1220
1221 /* read and eval received messages */
1222 static void
1223 dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1224 {
1225     struct dc390_dcb*   pDCB = pACB->pActiveDCB;
1226
1227     /* Read the msg */
1228
1229     pSRB->MsgInBuf[pACB->MsgLen++] = DC390_read8 (ScsiFifo);
1230     //pSRB->SRBState = 0;
1231
1232     /* Msg complete ? */
1233     if (dc390_MsgIn_complete (pSRB->MsgInBuf, pACB->MsgLen))
1234       {
1235         DEBUG0 (printk (KERN_INFO "DC390: MsgIn:"); dc390_printMsg (pSRB->MsgInBuf, pACB->MsgLen));
1236         /* Now eval the msg */
1237         switch (pSRB->MsgInBuf[0]) 
1238           {
1239           case DISCONNECT: 
1240             pSRB->SRBState = SRB_DISCONNECT; break;
1241             
1242           case SIMPLE_QUEUE_TAG:
1243           case HEAD_OF_QUEUE_TAG:
1244           case ORDERED_QUEUE_TAG:
1245             pSRB = dc390_MsgIn_QTag (pACB, pDCB, pSRB->MsgInBuf[1]);
1246             break;
1247             
1248           case MESSAGE_REJECT: 
1249             DC390_write8 (ScsiCmd, RESET_ATN_CMD);
1250             pDCB->NegoPeriod = 50; /* 200ns <=> 5 MHz */
1251             if( pSRB->SRBState & DO_SYNC_NEGO)
1252               dc390_MsgIn_set_async (pACB, pSRB);
1253             break;
1254             
1255           case EXTENDED_MESSAGE:
1256             /* reject every extended msg but SDTR */
1257             if (pSRB->MsgInBuf[1] != 3 || pSRB->MsgInBuf[2] != EXTENDED_SDTR)
1258               dc390_MsgIn_reject (pACB, pSRB);
1259             else
1260               {
1261                 if (pSRB->MsgInBuf[3] == 0 || pSRB->MsgInBuf[4] == 0)
1262                   dc390_MsgIn_set_async (pACB, pSRB);
1263                 else
1264                   dc390_MsgIn_set_sync (pACB, pSRB);
1265               }
1266             
1267             // nothing has to be done
1268           case COMMAND_COMPLETE: break;
1269             
1270             // SAVE POINTER may be ignored as we have the struct dc390_srb* associated with the
1271             // scsi command. Thanks, Gerard, for pointing it out.
1272           case SAVE_POINTERS: 
1273             pSRB->Saved_Ptr = pSRB->TotalXferredLen;
1274             break;
1275             // The device might want to restart transfer with a RESTORE
1276           case RESTORE_POINTERS:
1277             DEBUG0(printk ("DC390: RESTORE POINTER message received ... try to handle\n"));
1278             dc390_restore_ptr (pACB, pSRB);
1279             break;
1280
1281             // reject unknown messages
1282           default: dc390_MsgIn_reject (pACB, pSRB);
1283           }
1284         
1285         /* Clear counter and MsgIn state */
1286         pSRB->SRBState &= ~SRB_MSGIN;
1287         pACB->MsgLen = 0;
1288       }
1289
1290     *psstatus = SCSI_NOP0;
1291     DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);
1292     //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1293 }
1294
1295
1296 static void
1297 dc390_DataIO_Comm( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 ioDir)
1298 {
1299     unsigned long  lval;
1300     struct dc390_dcb*   pDCB = pACB->pActiveDCB;
1301
1302     if (pSRB == pACB->pTmpSRB)
1303     {
1304         if (pDCB)
1305                 printk(KERN_ERR "DC390: pSRB == pTmpSRB! (TagQ Error?) (%02i-%i)\n", pDCB->TargetID, pDCB->TargetLUN);
1306         else
1307                 printk(KERN_ERR "DC390: pSRB == pTmpSRB! (TagQ Error?) (DCB 0!)\n");
1308
1309         /* Try to recover - some broken disks react badly to tagged INQUIRY */
1310         if (pDCB && pACB->scan_devices && pDCB->GoingSRBCnt == 1) {
1311                 pSRB = pDCB->pGoingSRB;
1312                 pDCB->pActiveSRB = pSRB;
1313         } else {
1314                 pSRB->pSRBDCB = pDCB;
1315                 dc390_EnableMsgOut_Abort(pACB, pSRB);
1316                 if (pDCB)
1317                         pDCB->DCBFlag |= ABORT_DEV;
1318                 return;
1319         }
1320     }
1321
1322     if( pSRB->SGIndex < pSRB->SGcount )
1323     {
1324         DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir);
1325         if( !pSRB->SGToBeXferLen )
1326         {
1327             dc390_start_segment(pSRB);
1328
1329             DEBUG1(printk (KERN_DEBUG " DC390: Next SG segment."));
1330         }
1331         lval = pSRB->SGToBeXferLen;
1332         DEBUG1(printk (KERN_DEBUG " DC390: Start transfer: %li bytes (address %08lx)\n", lval, pSRB->SGBusAddr));
1333         DC390_write8 (CtcReg_Low, (u8) lval);
1334         lval >>= 8;
1335         DC390_write8 (CtcReg_Mid, (u8) lval);
1336         lval >>= 8;
1337         DC390_write8 (CtcReg_High, (u8) lval);
1338
1339         DC390_write32 (DMA_XferCnt, pSRB->SGToBeXferLen);
1340         DC390_write32 (DMA_XferAddr, pSRB->SGBusAddr);
1341
1342         //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir);
1343         pSRB->SRBState = SRB_DATA_XFER;
1344
1345         DC390_write8 (ScsiCmd, DMA_COMMAND+INFO_XFER_CMD);
1346
1347         DC390_write8 (DMA_Cmd, DMA_START_CMD | ioDir);
1348         //DEBUG1(DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT));
1349         //DEBUG1(printk (KERN_DEBUG "DC390: DMA_Status: %02x\n", DC390_read8 (DMA_Status)));
1350         //DEBUG1(DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT));
1351     }
1352     else    /* xfer pad */
1353     {
1354         if( pSRB->SGcount )
1355         {
1356             pSRB->AdaptStatus = H_OVER_UNDER_RUN;
1357             pSRB->SRBStatus |= OVER_RUN;
1358             DEBUG0(printk (KERN_WARNING " DC390: Overrun -"));
1359         }
1360         DEBUG0(printk (KERN_WARNING " Clear transfer pad \n"));
1361         DC390_write8 (CtcReg_Low, 0);
1362         DC390_write8 (CtcReg_Mid, 0);
1363         DC390_write8 (CtcReg_High, 0);
1364
1365         pSRB->SRBState |= SRB_XFERPAD;
1366         DC390_write8 (ScsiCmd, DMA_COMMAND+XFER_PAD_BYTE);
1367 /*
1368         DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir);
1369         DC390_write8 (DMA_Cmd, DMA_START_CMD | ioDir);
1370 */
1371     }
1372 }
1373
1374
1375 static void
1376 dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1377 {
1378     dc390_DataIO_Comm (pACB, pSRB, WRITE_DIRECTION);
1379 }
1380
1381 static void
1382 dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1383 {
1384     dc390_DataIO_Comm (pACB, pSRB, READ_DIRECTION);
1385 }
1386
1387 static void
1388 dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1389 {
1390     struct dc390_dcb*   pDCB;
1391     u8  i, cnt;
1392     u8     *ptr;
1393
1394     DC390_write8 (ScsiCmd, RESET_ATN_CMD);
1395     DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1396     if( !(pSRB->SRBFlag & AUTO_REQSENSE) )
1397     {
1398         cnt = (u8) pSRB->pcmd->cmd_len;
1399         ptr = (u8 *) pSRB->pcmd->cmnd;
1400         for(i=0; i < cnt; i++)
1401             DC390_write8 (ScsiFifo, *(ptr++));
1402     }
1403     else
1404     {
1405         DC390_write8 (ScsiFifo, REQUEST_SENSE);
1406         pDCB = pACB->pActiveDCB;
1407         DC390_write8 (ScsiFifo, pDCB->TargetLUN << 5);
1408         DC390_write8 (ScsiFifo, 0);
1409         DC390_write8 (ScsiFifo, 0);
1410         DC390_write8 (ScsiFifo, sizeof(pSRB->pcmd->sense_buffer));
1411         DC390_write8 (ScsiFifo, 0);
1412         DEBUG0(printk(KERN_DEBUG "DC390: AutoReqSense (CmndPhase)!\n"));
1413     }
1414     pSRB->SRBState = SRB_COMMAND;
1415     DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1416 }
1417
1418 static void
1419 dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1420 {
1421     DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1422     pSRB->SRBState = SRB_STATUS;
1423     DC390_write8 (ScsiCmd, INITIATOR_CMD_CMPLTE);
1424     //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1425 }
1426
1427 static void
1428 dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1429 {
1430     u8   bval, i, cnt;
1431     u8     *ptr;
1432     struct dc390_dcb*    pDCB;
1433
1434     DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1435     pDCB = pACB->pActiveDCB;
1436     if( !(pSRB->SRBState & SRB_MSGOUT) )
1437     {
1438         cnt = pSRB->MsgCnt;
1439         if( cnt )
1440         {
1441             ptr = (u8 *) pSRB->MsgOutBuf;
1442             for(i=0; i < cnt; i++)
1443                 DC390_write8 (ScsiFifo, *(ptr++));
1444             pSRB->MsgCnt = 0;
1445             if( (pDCB->DCBFlag & ABORT_DEV_) &&
1446                 (pSRB->MsgOutBuf[0] == ABORT) )
1447                 pSRB->SRBState = SRB_ABORT_SENT;
1448         }
1449         else
1450         {
1451             bval = ABORT;       /* ??? MSG_NOP */
1452             if( (pSRB->pcmd->cmnd[0] == INQUIRY ) ||
1453                 (pSRB->pcmd->cmnd[0] == REQUEST_SENSE) ||
1454                 (pSRB->SRBFlag & AUTO_REQSENSE) )
1455             {
1456                 if( pDCB->SyncMode & SYNC_ENABLE )
1457                     goto  mop1;
1458             }
1459             DC390_write8 (ScsiFifo, bval);
1460         }
1461         DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1462     }
1463     else
1464     {
1465 mop1:
1466         printk (KERN_ERR "DC390: OLD Sync Nego code triggered! (%i %i)\n", pDCB->TargetID, pDCB->TargetLUN);
1467         DC390_write8 (ScsiFifo, EXTENDED_MESSAGE);
1468         DC390_write8 (ScsiFifo, 3);     /*    ;length of extended msg */
1469         DC390_write8 (ScsiFifo, EXTENDED_SDTR); /*    ; sync nego */
1470         DC390_write8 (ScsiFifo, pDCB->NegoPeriod);
1471         if (pDCB->SyncOffset & 0x0f)
1472                     DC390_write8 (ScsiFifo, pDCB->SyncOffset);
1473         else
1474                     DC390_write8 (ScsiFifo, SYNC_NEGO_OFFSET);              
1475         pSRB->SRBState |= DO_SYNC_NEGO;
1476         DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1477     }
1478 }
1479
1480 static void
1481 dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1482 {
1483     DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1484     if( !(pSRB->SRBState & SRB_MSGIN) )
1485     {
1486         pSRB->SRBState &= ~SRB_DISCONNECT;
1487         pSRB->SRBState |= SRB_MSGIN;
1488     }
1489     DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1490     //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1491 }
1492
1493 static void
1494 dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1495 {
1496 }
1497
1498 static void
1499 dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1500 {
1501 }
1502
1503
1504 static void
1505 dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB )
1506 {
1507     u8  bval, i, cnt;
1508     struct dc390_dcb*   ptr;
1509
1510     if( !(pDCB->TargetLUN) )
1511     {
1512         if( !pACB->scan_devices )
1513         {
1514             ptr = pACB->pLinkDCB;
1515             cnt = pACB->DCBCnt;
1516             bval = pDCB->TargetID;
1517             for(i=0; i<cnt; i++)
1518             {
1519                 if( ptr->TargetID == bval )
1520                 {
1521                     ptr->SyncPeriod = pDCB->SyncPeriod;
1522                     ptr->SyncOffset = pDCB->SyncOffset;
1523                     ptr->CtrlR3 = pDCB->CtrlR3;
1524                     ptr->CtrlR4 = pDCB->CtrlR4;
1525                     ptr->SyncMode = pDCB->SyncMode;
1526                 }
1527                 ptr = ptr->pNextDCB;
1528             }
1529         }
1530     }
1531     return;
1532 }
1533
1534
1535 static void
1536 dc390_Disconnect( struct dc390_acb* pACB )
1537 {
1538     struct dc390_dcb *pDCB;
1539     struct dc390_srb *pSRB, *psrb;
1540     u8  i, cnt;
1541
1542     DEBUG0(printk(KERN_INFO "DISC,"));
1543
1544     if (!pACB->Connected) printk(KERN_ERR "DC390: Disconnect not-connected bus?\n");
1545     pACB->Connected = 0;
1546     pDCB = pACB->pActiveDCB;
1547     if (!pDCB)
1548      {
1549         DEBUG0(printk(KERN_ERR "ACB:%p->ActiveDCB:%p IOPort:%04x IRQ:%02x !\n",\
1550                pACB, pDCB, pACB->IOPortBase, pACB->IRQLevel));
1551         mdelay(400);
1552         DC390_read8 (INT_Status);       /* Reset Pending INT */
1553         DC390_write8 (ScsiCmd, EN_SEL_RESEL);
1554         return;
1555      }
1556     DC390_write8 (ScsiCmd, EN_SEL_RESEL);
1557     pSRB = pDCB->pActiveSRB;
1558     pACB->pActiveDCB = NULL;
1559     pSRB->ScsiPhase = SCSI_NOP0;
1560     if( pSRB->SRBState & SRB_UNEXPECT_RESEL )
1561         pSRB->SRBState = 0;
1562     else if( pSRB->SRBState & SRB_ABORT_SENT )
1563     {
1564         pDCB->TagMask = 0;
1565         pDCB->DCBFlag = 0;
1566         cnt = pDCB->GoingSRBCnt;
1567         pDCB->GoingSRBCnt = 0;
1568         pSRB = pDCB->pGoingSRB;
1569         for( i=0; i < cnt; i++)
1570         {
1571             psrb = pSRB->pNextSRB;
1572             dc390_Free_insert (pACB, pSRB);
1573             pSRB = psrb;
1574         }
1575         pDCB->pGoingSRB = NULL;
1576     }
1577     else
1578     {
1579         if( (pSRB->SRBState & (SRB_START_+SRB_MSGOUT)) ||
1580            !(pSRB->SRBState & (SRB_DISCONNECT+SRB_COMPLETED)) )
1581         {       /* Selection time out */
1582                 pSRB->TargetStatus = SCSI_STAT_SEL_TIMEOUT;
1583                 goto  disc1;
1584         }
1585         else if (!(pSRB->SRBState & SRB_DISCONNECT) && (pSRB->SRBState & SRB_COMPLETED))
1586         {
1587 disc1:
1588             dc390_freetag (pDCB, pSRB);
1589             pDCB->pActiveSRB = NULL;
1590             pSRB->SRBState = SRB_FREE;
1591             dc390_SRBdone( pACB, pDCB, pSRB);
1592         }
1593     }
1594     pACB->MsgLen = 0;
1595 }
1596
1597
1598 static void
1599 dc390_Reselect( struct dc390_acb* pACB )
1600 {
1601     struct dc390_dcb*   pDCB;
1602     struct dc390_srb*   pSRB;
1603     u8  id, lun;
1604
1605     DEBUG0(printk(KERN_INFO "RSEL,"));
1606     pACB->Connected = 1;
1607     pDCB = pACB->pActiveDCB;
1608     if( pDCB )
1609     {   /* Arbitration lost but Reselection won */
1610         DEBUG0(printk ("DC390: (ActiveDCB != 0: Arb. lost but resel. won)!\n"));
1611         pSRB = pDCB->pActiveSRB;
1612         if( !( pACB->scan_devices ) )
1613         {
1614             struct scsi_cmnd *pcmd = pSRB->pcmd;
1615             pcmd->resid = pcmd->request_bufflen;
1616             SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1617             dc390_Going_remove(pDCB, pSRB);
1618             dc390_Free_insert(pACB, pSRB);
1619             pcmd->scsi_done (pcmd);
1620             DEBUG0(printk(KERN_DEBUG"DC390: Return SRB %p to free\n", pSRB));
1621         }
1622     }
1623     /* Get ID */
1624     lun = DC390_read8 (ScsiFifo);
1625     DEBUG0(printk ("Dev %02x,", lun));
1626     if (!(lun & (1 << pACB->pScsiHost->this_id)))
1627       printk (KERN_ERR "DC390: Reselection must select host adapter: %02x!\n", lun);
1628     else
1629       lun ^= 1 << pACB->pScsiHost->this_id; /* Mask AdapterID */
1630     id = 0; while (lun >>= 1) id++;
1631     /* Get LUN */
1632     lun = DC390_read8 (ScsiFifo);
1633     if (!(lun & IDENTIFY_BASE)) printk (KERN_ERR "DC390: Resel: Expect identify message!\n");
1634     lun &= 7;
1635     DEBUG0(printk ("(%02i-%i),", id, lun));
1636     pDCB = dc390_findDCB (pACB, id, lun);
1637     if (!pDCB)
1638     {
1639         printk (KERN_ERR "DC390: Reselect from non existing device (%02i-%i)\n",
1640                     id, lun);
1641         return;
1642     }
1643     pACB->pActiveDCB = pDCB;
1644     /* TagQ: We expect a message soon, so never mind the exact SRB */
1645     if( pDCB->SyncMode & EN_TAG_QUEUEING )
1646     {
1647         pSRB = pACB->pTmpSRB;
1648         pDCB->pActiveSRB = pSRB;
1649     }
1650     else
1651     {
1652         pSRB = pDCB->pActiveSRB;
1653         if( !pSRB || !(pSRB->SRBState & SRB_DISCONNECT) )
1654         {
1655             pSRB= pACB->pTmpSRB;
1656             pSRB->SRBState = SRB_UNEXPECT_RESEL;
1657             printk (KERN_ERR "DC390: Reselect without outstanding cmnd (%02i-%i)\n",
1658                     id, lun);
1659             pDCB->pActiveSRB = pSRB;
1660             dc390_EnableMsgOut_Abort ( pACB, pSRB );
1661         }
1662         else
1663         {
1664             if( pDCB->DCBFlag & ABORT_DEV_ )
1665             {
1666                 pSRB->SRBState = SRB_ABORT_SENT;
1667                 printk (KERN_INFO "DC390: Reselect: Abort (%02i-%i)\n",
1668                         id, lun);
1669                 dc390_EnableMsgOut_Abort( pACB, pSRB );
1670             }
1671             else
1672                 pSRB->SRBState = SRB_DATA_XFER;
1673         }
1674     }
1675
1676     DEBUG1(printk (KERN_DEBUG "Resel SRB(%p): TagNum (%02x)\n", pSRB, pSRB->TagNumber));
1677     pSRB->ScsiPhase = SCSI_NOP0;
1678     DC390_write8 (Scsi_Dest_ID, pDCB->TargetID);
1679     DC390_write8 (Sync_Period, pDCB->SyncPeriod);
1680     DC390_write8 (Sync_Offset, pDCB->SyncOffset);
1681     DC390_write8 (CtrlReg1, pDCB->CtrlR1);
1682     DC390_write8 (CtrlReg3, pDCB->CtrlR3);
1683     DC390_write8 (CtrlReg4, pDCB->CtrlR4);      /* ; Glitch eater */
1684     DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);   /* ;to release the /ACK signal */
1685 }
1686
1687 static int __inline__
1688 dc390_RequestSense(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
1689 {
1690         struct scsi_cmnd *pcmd;
1691
1692         pcmd = pSRB->pcmd;
1693
1694         REMOVABLEDEBUG(printk(KERN_INFO "DC390: RequestSense(Cmd %02x, Id %02x, LUN %02x)\n",\
1695                               pcmd->cmnd[0], pDCB->TargetID, pDCB->TargetLUN));
1696
1697         pSRB->SRBFlag |= AUTO_REQSENSE;
1698         pSRB->SavedSGCount = pcmd->use_sg;
1699         pSRB->SavedTotXLen = pSRB->TotalXferredLen;
1700         pSRB->AdaptStatus = 0;
1701         pSRB->TargetStatus = 0; /* CHECK_CONDITION<<1; */
1702
1703         /* We are called from SRBdone, original PCI mapping has been removed
1704          * already, new one is set up from StartSCSI */
1705         pSRB->SGIndex = 0;
1706
1707         pSRB->TotalXferredLen = 0;
1708         pSRB->SGToBeXferLen = 0;
1709         return dc390_StartSCSI(pACB, pDCB, pSRB);
1710 }
1711
1712
1713 static void
1714 dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
1715 {
1716     u8 status;
1717     struct scsi_cmnd *pcmd;
1718
1719     pcmd = pSRB->pcmd;
1720     /* KG: Moved pci_unmap here */
1721     dc390_pci_unmap(pSRB);
1722
1723     status = pSRB->TargetStatus;
1724
1725     DEBUG0(printk (" SRBdone (%02x,%08x), SRB %p, pid %li\n", status, pcmd->result,\
1726                 pSRB, pcmd->pid));
1727     if(pSRB->SRBFlag & AUTO_REQSENSE)
1728     {   /* Last command was a Request Sense */
1729         pSRB->SRBFlag &= ~AUTO_REQSENSE;
1730         pSRB->AdaptStatus = 0;
1731         pSRB->TargetStatus = CHECK_CONDITION << 1;
1732
1733         //pcmd->result = MK_RES(DRIVER_SENSE,DID_OK,0,status);
1734         if (status == (CHECK_CONDITION << 1))
1735             pcmd->result = MK_RES_LNX(0, DID_BAD_TARGET, 0, /*CHECK_CONDITION*/0);
1736         else /* Retry */
1737         {
1738             if( pSRB->pcmd->cmnd[0] == TEST_UNIT_READY /* || pSRB->pcmd->cmnd[0] == START_STOP */)
1739             {
1740                 /* Don't retry on TEST_UNIT_READY */
1741                 pcmd->result = MK_RES_LNX(DRIVER_SENSE,DID_OK,0,CHECK_CONDITION);
1742                 REMOVABLEDEBUG(printk(KERN_INFO "Cmd=%02x, Result=%08x, XferL=%08x\n",pSRB->pcmd->cmnd[0],\
1743                        (u32) pcmd->result, (u32) pSRB->TotalXferredLen));
1744             } else {
1745                 SET_RES_DRV(pcmd->result, DRIVER_SENSE);
1746                 pcmd->use_sg = pSRB->SavedSGCount;
1747                 //pSRB->ScsiCmdLen       = (u8) (pSRB->Segment1[0] >> 8);
1748                 DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->pid, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
1749                 pSRB->TotalXferredLen = 0;
1750                 SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1751             }
1752         }
1753         goto cmd_done;
1754     }
1755     if( status )
1756     {
1757         if( status_byte(status) == CHECK_CONDITION )
1758         {
1759             if (dc390_RequestSense(pACB, pDCB, pSRB)) {
1760                 SET_RES_DID(pcmd->result, DID_ERROR);
1761                 goto cmd_done;
1762             }
1763             return;
1764         }
1765         else if( status_byte(status) == QUEUE_FULL )
1766         {
1767             scsi_track_queue_full(pcmd->device, pDCB->GoingSRBCnt - 1);
1768             pcmd->use_sg = pSRB->SavedSGCount;
1769             DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->pid, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
1770             pSRB->TotalXferredLen = 0;
1771             SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1772         }
1773         else if(status == SCSI_STAT_SEL_TIMEOUT)
1774         {
1775             pSRB->AdaptStatus = H_SEL_TIMEOUT;
1776             pSRB->TargetStatus = 0;
1777             pcmd->result = MK_RES(0,DID_NO_CONNECT,0,0);
1778             /* Devices are removed below ... */
1779         }
1780         else if (status_byte(status) == BUSY && 
1781                  (pcmd->cmnd[0] == TEST_UNIT_READY || pcmd->cmnd[0] == INQUIRY) &&
1782                  pACB->scan_devices)
1783         {
1784             pSRB->AdaptStatus = 0;
1785             pSRB->TargetStatus = status;
1786             pcmd->result = MK_RES(0,0,pSRB->EndMessage,/*status*/0);
1787         }
1788         else
1789         {   /* Another error */
1790             pSRB->TotalXferredLen = 0;
1791             SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1792             goto cmd_done;
1793         }
1794     }
1795     else
1796     {   /*  Target status == 0 */
1797         status = pSRB->AdaptStatus;
1798         if(status & H_OVER_UNDER_RUN)
1799         {
1800             pSRB->TargetStatus = 0;
1801             SET_RES_DID(pcmd->result,DID_OK);
1802             SET_RES_MSG(pcmd->result,pSRB->EndMessage);
1803         }
1804         else if( pSRB->SRBStatus & PARITY_ERROR)
1805         {
1806             //pcmd->result = MK_RES(0,DID_PARITY,pSRB->EndMessage,0);
1807             SET_RES_DID(pcmd->result,DID_PARITY);
1808             SET_RES_MSG(pcmd->result,pSRB->EndMessage);
1809         }
1810         else                   /* No error */
1811         {
1812             pSRB->AdaptStatus = 0;
1813             pSRB->TargetStatus = 0;
1814             SET_RES_DID(pcmd->result,DID_OK);
1815         }
1816     }
1817
1818 cmd_done:
1819     pcmd->resid = pcmd->request_bufflen - pSRB->TotalXferredLen;
1820
1821     dc390_Going_remove (pDCB, pSRB);
1822     /* Add to free list */
1823     dc390_Free_insert (pACB, pSRB);
1824
1825     DEBUG0(printk (KERN_DEBUG "DC390: SRBdone: done pid %li\n", pcmd->pid));
1826     pcmd->scsi_done (pcmd);
1827
1828     return;
1829 }
1830
1831
1832 /* Remove all SRBs from Going list and inform midlevel */
1833 static void
1834 dc390_DoingSRB_Done(struct dc390_acb* pACB, struct scsi_cmnd *cmd)
1835 {
1836     struct dc390_dcb *pDCB, *pdcb;
1837     struct dc390_srb *psrb, *psrb2;
1838     int i;
1839     struct scsi_cmnd *pcmd;
1840
1841     pDCB = pACB->pLinkDCB;
1842     pdcb = pDCB;
1843     if (! pdcb) return;
1844     do
1845     {
1846         psrb = pdcb->pGoingSRB;
1847         for (i = 0; i < pdcb->GoingSRBCnt; i++)
1848         {
1849             psrb2 = psrb->pNextSRB;
1850             pcmd = psrb->pcmd;
1851             dc390_Free_insert (pACB, psrb);
1852             psrb  = psrb2;
1853         }
1854         pdcb->GoingSRBCnt = 0;
1855         pdcb->pGoingSRB = NULL;
1856         pdcb->TagMask = 0;
1857         pdcb = pdcb->pNextDCB;
1858     } while( pdcb != pDCB );
1859 }
1860
1861
1862 static void
1863 dc390_ResetSCSIBus( struct dc390_acb* pACB )
1864 {
1865     //DC390_write8 (ScsiCmd, RST_DEVICE_CMD);
1866     //udelay (250);
1867     //DC390_write8 (ScsiCmd, NOP_CMD);
1868
1869     DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1870     DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1871     DC390_write8 (ScsiCmd, RST_SCSI_BUS_CMD);
1872     pACB->Connected = 0;
1873
1874     return;
1875 }
1876
1877 static void
1878 dc390_ScsiRstDetect( struct dc390_acb* pACB )
1879 {
1880     printk ("DC390: Rst_Detect: laststat = %08x\n", dc390_laststatus);
1881     //DEBUG0(printk(KERN_INFO "RST_DETECT,"));
1882
1883     DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1884     /* Unlock before ? */
1885     /* delay half a second */
1886     udelay (1000);
1887     DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1888     pACB->pScsiHost->last_reset = jiffies + 5*HZ/2
1889                     + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
1890     pACB->Connected = 0;
1891
1892     if( pACB->ACBFlag & RESET_DEV )
1893         pACB->ACBFlag |= RESET_DONE;
1894     else
1895     {   /* Reset was issued by sb else */
1896         pACB->ACBFlag |= RESET_DETECT;
1897
1898         dc390_ResetDevParam( pACB );
1899         dc390_DoingSRB_Done( pACB, NULL);
1900         //dc390_RecoverSRB( pACB );
1901         pACB->pActiveDCB = NULL;
1902         pACB->ACBFlag = 0;
1903     }
1904     return;
1905 }
1906
1907 static int DC390_queuecommand(struct scsi_cmnd *cmd,
1908                 void (*done)(struct scsi_cmnd *))
1909 {
1910         struct scsi_device *sdev = cmd->device;
1911         struct dc390_acb *acb = (struct dc390_acb *)sdev->host->hostdata;
1912         struct dc390_dcb *dcb = sdev->hostdata;
1913         struct dc390_srb *srb;
1914
1915         if (sdev->queue_depth <= dcb->GoingSRBCnt)
1916                 goto device_busy;
1917         if (acb->pActiveDCB)
1918                 goto host_busy;
1919         if (acb->ACBFlag & (RESET_DETECT|RESET_DONE|RESET_DEV))
1920                 goto host_busy;
1921
1922         srb = acb->pFreeSRB;
1923         if (unlikely(srb == NULL))
1924                 goto host_busy;
1925
1926         cmd->scsi_done = done;
1927         cmd->result = 0;
1928         acb->Cmds++;
1929
1930         acb->pFreeSRB = srb->pNextSRB;
1931         srb->pNextSRB = NULL;
1932
1933         srb->pSRBDCB = dcb;
1934         srb->pcmd = cmd;
1935         cmd->host_scribble = (char *)srb;
1936     
1937         srb->SGIndex = 0;
1938         srb->AdaptStatus = 0;
1939         srb->TargetStatus = 0;
1940         srb->MsgCnt = 0;
1941
1942         srb->SRBStatus = 0;
1943         srb->SRBFlag = 0;
1944         srb->SRBState = 0;
1945         srb->TotalXferredLen = 0;
1946         srb->SGBusAddr = 0;
1947         srb->SGToBeXferLen = 0;
1948         srb->ScsiPhase = 0;
1949         srb->EndMessage = 0;
1950         srb->TagNumber = SCSI_NO_TAG;
1951
1952         if (dc390_StartSCSI(acb, dcb, srb)) {
1953                 dc390_Free_insert(acb, srb);
1954                 goto host_busy;
1955         }
1956
1957         dc390_Going_append(dcb, srb);
1958
1959         return 0;
1960
1961  host_busy:
1962         return SCSI_MLQUEUE_HOST_BUSY;
1963
1964  device_busy:
1965         return SCSI_MLQUEUE_DEVICE_BUSY;
1966 }
1967
1968 static void dc390_dumpinfo (struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
1969 {
1970     struct pci_dev *pdev;
1971     u16 pstat;
1972
1973     if (!pDCB) pDCB = pACB->pActiveDCB;
1974     if (!pSRB && pDCB) pSRB = pDCB->pActiveSRB;
1975
1976     if (pSRB) 
1977     {
1978         printk ("DC390: SRB: Xferred %08lx, Remain %08lx, State %08x, Phase %02x\n",
1979                 pSRB->TotalXferredLen, pSRB->SGToBeXferLen, pSRB->SRBState,
1980                 pSRB->ScsiPhase);
1981         printk ("DC390: AdpaterStatus: %02x, SRB Status %02x\n", pSRB->AdaptStatus, pSRB->SRBStatus);
1982     }
1983     printk ("DC390: Status of last IRQ (DMA/SC/Int/IRQ): %08x\n", dc390_laststatus);
1984     printk ("DC390: Register dump: SCSI block:\n");
1985     printk ("DC390: XferCnt  Cmd Stat IntS IRQS FFIS Ctl1 Ctl2 Ctl3 Ctl4\n");
1986     printk ("DC390:  %06x   %02x   %02x   %02x",
1987             DC390_read8(CtcReg_Low) + (DC390_read8(CtcReg_Mid) << 8) + (DC390_read8(CtcReg_High) << 16),
1988             DC390_read8(ScsiCmd), DC390_read8(Scsi_Status), DC390_read8(Intern_State));
1989     printk ("   %02x   %02x   %02x   %02x   %02x   %02x\n",
1990             DC390_read8(INT_Status), DC390_read8(Current_Fifo), DC390_read8(CtrlReg1),
1991             DC390_read8(CtrlReg2), DC390_read8(CtrlReg3), DC390_read8(CtrlReg4));
1992     DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT);
1993     if (DC390_read8(Current_Fifo) & 0x1f)
1994       {
1995         printk ("DC390: FIFO:");
1996         while (DC390_read8(Current_Fifo) & 0x1f) printk (" %02x", DC390_read8(ScsiFifo));
1997         printk ("\n");
1998       }
1999     printk ("DC390: Register dump: DMA engine:\n");
2000     printk ("DC390: Cmd   STrCnt    SBusA    WrkBC    WrkAC Stat SBusCtrl\n");
2001     printk ("DC390:  %02x %08x %08x %08x %08x   %02x %08x\n",
2002             DC390_read8(DMA_Cmd), DC390_read32(DMA_XferCnt), DC390_read32(DMA_XferAddr),
2003             DC390_read32(DMA_Wk_ByteCntr), DC390_read32(DMA_Wk_AddrCntr),
2004             DC390_read8(DMA_Status), DC390_read32(DMA_ScsiBusCtrl));
2005     DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
2006
2007     pdev = pACB->pdev;
2008     pci_read_config_word(pdev, PCI_STATUS, &pstat);
2009     printk ("DC390: Register dump: PCI Status: %04x\n", pstat);
2010     printk ("DC390: In case of driver trouble read Documentation/scsi/tmscsim.txt\n");
2011 }
2012
2013
2014 static int DC390_abort(struct scsi_cmnd *cmd)
2015 {
2016         struct dc390_acb *pACB = (struct dc390_acb*) cmd->device->host->hostdata;
2017         struct dc390_dcb *pDCB = (struct dc390_dcb*) cmd->device->hostdata;
2018
2019         scmd_printk(KERN_WARNING, cmd,
2020                 "DC390: Abort command (pid %li)\n", cmd->pid);
2021
2022         /* abort() is too stupid for already sent commands at the moment. 
2023          * If it's called we are in trouble anyway, so let's dump some info 
2024          * into the syslog at least. (KG, 98/08/20,99/06/20) */
2025         dc390_dumpinfo(pACB, pDCB, NULL);
2026
2027         pDCB->DCBFlag |= ABORT_DEV_;
2028         printk(KERN_INFO "DC390: Aborted pid %li\n", cmd->pid);
2029
2030         return FAILED;
2031 }
2032
2033
2034 static void dc390_ResetDevParam( struct dc390_acb* pACB )
2035 {
2036     struct dc390_dcb *pDCB, *pdcb;
2037
2038     pDCB = pACB->pLinkDCB;
2039     if (! pDCB) return;
2040     pdcb = pDCB;
2041     do
2042     {
2043         pDCB->SyncMode &= ~SYNC_NEGO_DONE;
2044         pDCB->SyncPeriod = 0;
2045         pDCB->SyncOffset = 0;
2046         pDCB->TagMask = 0;
2047         pDCB->CtrlR3 = FAST_CLK;
2048         pDCB->CtrlR4 &= NEGATE_REQACKDATA | CTRL4_RESERVED | NEGATE_REQACK;
2049         pDCB->CtrlR4 |= pACB->glitch_cfg;
2050         pDCB = pDCB->pNextDCB;
2051     }
2052     while( pdcb != pDCB );
2053     pACB->ACBFlag &= ~(RESET_DEV | RESET_DONE | RESET_DETECT);
2054
2055 }
2056
2057 static int DC390_bus_reset (struct scsi_cmnd *cmd)
2058 {
2059         struct dc390_acb*    pACB = (struct dc390_acb*) cmd->device->host->hostdata;
2060         u8   bval;
2061
2062         spin_lock_irq(cmd->device->host->host_lock);
2063
2064         bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
2065         DC390_write8(CtrlReg1, bval);   /* disable IRQ on bus reset */
2066
2067         pACB->ACBFlag |= RESET_DEV;
2068         dc390_ResetSCSIBus(pACB);
2069
2070         dc390_ResetDevParam(pACB);
2071         mdelay(1);
2072         pACB->pScsiHost->last_reset = jiffies + 3*HZ/2 
2073                 + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
2074     
2075         DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
2076         DC390_read8(INT_Status);                /* Reset Pending INT */
2077
2078         dc390_DoingSRB_Done(pACB, cmd);
2079
2080         pACB->pActiveDCB = NULL;
2081         pACB->ACBFlag = 0;
2082
2083         bval = DC390_read8(CtrlReg1) & ~DIS_INT_ON_SCSI_RST;
2084         DC390_write8(CtrlReg1, bval);   /* re-enable interrupt */
2085
2086         spin_unlock_irq(cmd->device->host->host_lock);
2087
2088         return SUCCESS;
2089 }
2090
2091 /**
2092  * dc390_slave_alloc - Called by the scsi mid layer to tell us about a new
2093  * scsi device that we need to deal with.
2094  *
2095  * @scsi_device: The new scsi device that we need to handle.
2096  */
2097 static int dc390_slave_alloc(struct scsi_device *scsi_device)
2098 {
2099         struct dc390_acb *pACB = (struct dc390_acb*) scsi_device->host->hostdata;
2100         struct dc390_dcb *pDCB, *pDCB2 = NULL;
2101         uint id = scsi_device->id;
2102         uint lun = scsi_device->lun;
2103
2104         pDCB = kmalloc(sizeof(struct dc390_dcb), GFP_KERNEL);
2105         if (!pDCB)
2106                 return -ENOMEM;
2107         memset(pDCB, 0, sizeof(struct dc390_dcb));
2108
2109         if (!pACB->DCBCnt++) {
2110                 pACB->pLinkDCB = pDCB;
2111                 pACB->pDCBRunRobin = pDCB;
2112         } else {
2113                 pACB->pLastDCB->pNextDCB = pDCB;
2114         }
2115    
2116         pDCB->pNextDCB = pACB->pLinkDCB;
2117         pACB->pLastDCB = pDCB;
2118
2119         pDCB->pDCBACB = pACB;
2120         pDCB->TargetID = id;
2121         pDCB->TargetLUN = lun;
2122
2123         /*
2124          * Some values are for all LUNs: Copy them 
2125          * In a clean way: We would have an own structure for a SCSI-ID 
2126          */
2127         if (lun && (pDCB2 = dc390_findDCB(pACB, id, 0))) {
2128                 pDCB->DevMode = pDCB2->DevMode;
2129                 pDCB->SyncMode = pDCB2->SyncMode & SYNC_NEGO_DONE;
2130                 pDCB->SyncPeriod = pDCB2->SyncPeriod;
2131                 pDCB->SyncOffset = pDCB2->SyncOffset;
2132                 pDCB->NegoPeriod = pDCB2->NegoPeriod;
2133       
2134                 pDCB->CtrlR3 = pDCB2->CtrlR3;
2135                 pDCB->CtrlR4 = pDCB2->CtrlR4;
2136         } else {
2137                 u8 index = pACB->AdapterIndex;
2138                 PEEprom prom = (PEEprom) &dc390_eepromBuf[index][id << 2];
2139
2140                 pDCB->DevMode = prom->EE_MODE1;
2141                 pDCB->NegoPeriod =
2142                         (dc390_clock_period1[prom->EE_SPEED] * 25) >> 2;
2143                 pDCB->CtrlR3 = FAST_CLK;
2144                 pDCB->CtrlR4 = pACB->glitch_cfg | CTRL4_RESERVED;
2145                 if (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION)
2146                         pDCB->CtrlR4 |= NEGATE_REQACKDATA | NEGATE_REQACK;
2147         }
2148
2149         if (pDCB->DevMode & SYNC_NEGO_)
2150                 pDCB->SyncMode |= SYNC_ENABLE;
2151         else {
2152                 pDCB->SyncMode = 0;
2153                 pDCB->SyncOffset &= ~0x0f;
2154         }
2155
2156         pDCB->CtrlR1 = pACB->pScsiHost->this_id;
2157         if (pDCB->DevMode & PARITY_CHK_)
2158                 pDCB->CtrlR1 |= PARITY_ERR_REPO;
2159
2160         pACB->scan_devices = 1;
2161         scsi_device->hostdata = pDCB;
2162         return 0;
2163 }
2164
2165 /**
2166  * dc390_slave_destroy - Called by the scsi mid layer to tell us about a
2167  * device that is going away.
2168  *
2169  * @scsi_device: The scsi device that we need to remove.
2170  */
2171 static void dc390_slave_destroy(struct scsi_device *scsi_device)
2172 {
2173         struct dc390_acb* pACB = (struct dc390_acb*) scsi_device->host->hostdata;
2174         struct dc390_dcb* pDCB = (struct dc390_dcb*) scsi_device->hostdata;
2175         struct dc390_dcb* pPrevDCB = pACB->pLinkDCB;
2176
2177         pACB->scan_devices = 0;
2178
2179         BUG_ON(pDCB->GoingSRBCnt > 1);
2180         
2181         if (pDCB == pACB->pLinkDCB) {
2182                 if (pACB->pLastDCB == pDCB) {
2183                         pDCB->pNextDCB = NULL;
2184                         pACB->pLastDCB = NULL;
2185                 }
2186                 pACB->pLinkDCB = pDCB->pNextDCB;
2187         } else {
2188                 while (pPrevDCB->pNextDCB != pDCB)
2189                         pPrevDCB = pPrevDCB->pNextDCB;
2190                 pPrevDCB->pNextDCB = pDCB->pNextDCB;
2191                 if (pDCB == pACB->pLastDCB)
2192                         pACB->pLastDCB = pPrevDCB;
2193         }
2194
2195         if (pDCB == pACB->pActiveDCB)
2196                 pACB->pActiveDCB = NULL;
2197         if (pDCB == pACB->pLinkDCB)
2198                 pACB->pLinkDCB = pDCB->pNextDCB;
2199         if (pDCB == pACB->pDCBRunRobin)
2200                 pACB->pDCBRunRobin = pDCB->pNextDCB;
2201         kfree(pDCB); 
2202         
2203         pACB->DCBCnt--;
2204 }
2205
2206 static int dc390_slave_configure(struct scsi_device *sdev)
2207 {
2208         struct dc390_acb *acb = (struct dc390_acb *)sdev->host->hostdata;
2209         struct dc390_dcb *dcb = (struct dc390_dcb *)sdev->hostdata;
2210
2211         acb->scan_devices = 0;
2212         if (sdev->tagged_supported && (dcb->DevMode & TAG_QUEUEING_)) {
2213                 dcb->SyncMode |= EN_TAG_QUEUEING;
2214                 scsi_activate_tcq(sdev, acb->TagMaxNum);
2215         }
2216
2217         return 0;
2218 }
2219
2220 static struct scsi_host_template driver_template = {
2221         .module                 = THIS_MODULE,
2222         .proc_name              = "tmscsim", 
2223         .name                   = DC390_BANNER " V" DC390_VERSION,
2224         .slave_alloc            = dc390_slave_alloc,
2225         .slave_configure        = dc390_slave_configure,
2226         .slave_destroy          = dc390_slave_destroy,
2227         .queuecommand           = DC390_queuecommand,
2228         .eh_abort_handler       = DC390_abort,
2229         .eh_bus_reset_handler   = DC390_bus_reset,
2230         .can_queue              = 1,
2231         .this_id                = 7,
2232         .sg_tablesize           = SG_ALL,
2233         .cmd_per_lun            = 1,
2234         .use_clustering         = ENABLE_CLUSTERING,
2235         .max_sectors            = 0x4000, /* 8MiB = 16 * 1024 * 512 */
2236 };
2237
2238 /***********************************************************************
2239  * Functions for access to DC390 EEPROM
2240  * and some to emulate it
2241  *
2242  **********************************************************************/
2243
2244 static void __devinit dc390_eeprom_prepare_read(struct pci_dev *pdev, u8 cmd)
2245 {
2246         u8 carryFlag = 1, j = 0x80, bval;
2247         int i;
2248
2249         for (i = 0; i < 9; i++) {
2250                 if (carryFlag) {
2251                         pci_write_config_byte(pdev, 0x80, 0x40);
2252                         bval = 0xc0;
2253                 } else
2254                         bval = 0x80;
2255
2256                 udelay(160);
2257                 pci_write_config_byte(pdev, 0x80, bval);
2258                 udelay(160);
2259                 pci_write_config_byte(pdev, 0x80, 0);
2260                 udelay(160);
2261
2262                 carryFlag = (cmd & j) ? 1 : 0;
2263                 j >>= 1;
2264         }
2265 }
2266
2267 static u16 __devinit dc390_eeprom_get_data(struct pci_dev *pdev)
2268 {
2269         int i;
2270         u16 wval = 0;
2271         u8 bval;
2272
2273         for (i = 0; i < 16; i++) {
2274                 wval <<= 1;
2275
2276                 pci_write_config_byte(pdev, 0x80, 0x80);
2277                 udelay(160);
2278                 pci_write_config_byte(pdev, 0x80, 0x40);
2279                 udelay(160);
2280                 pci_read_config_byte(pdev, 0x00, &bval);
2281
2282                 if (bval == 0x22)
2283                         wval |= 1;
2284         }
2285
2286         return wval;
2287 }
2288
2289 static void __devinit dc390_read_eeprom(struct pci_dev *pdev, u16 *ptr)
2290 {
2291         u8 cmd = EEPROM_READ, i;
2292
2293         for (i = 0; i < 0x40; i++) {
2294                 pci_write_config_byte(pdev, 0xc0, 0);
2295                 udelay(160);
2296
2297                 dc390_eeprom_prepare_read(pdev, cmd++);
2298                 *ptr++ = dc390_eeprom_get_data(pdev);
2299
2300                 pci_write_config_byte(pdev, 0x80, 0);
2301                 pci_write_config_byte(pdev, 0x80, 0);
2302                 udelay(160);
2303         }
2304 }
2305
2306 /* Override EEprom values with explicitly set values */
2307 static void __devinit dc390_eeprom_override(u8 index)
2308 {
2309         u8 *ptr = (u8 *) dc390_eepromBuf[index], id;
2310
2311         /* Adapter Settings */
2312         if (tmscsim[0] != -2)
2313                 ptr[EE_ADAPT_SCSI_ID] = (u8)tmscsim[0]; /* Adapter ID */
2314         if (tmscsim[3] != -2)
2315                 ptr[EE_MODE2] = (u8)tmscsim[3];
2316         if (tmscsim[5] != -2)
2317                 ptr[EE_DELAY] = tmscsim[5];             /* Reset delay */
2318         if (tmscsim[4] != -2)
2319                 ptr[EE_TAG_CMD_NUM] = (u8)tmscsim[4];   /* Tagged Cmds */
2320
2321         /* Device Settings */
2322         for (id = 0; id < MAX_SCSI_ID; id++) {
2323                 if (tmscsim[2] != -2)
2324                         ptr[id << 2] = (u8)tmscsim[2];          /* EE_MODE1 */
2325                 if (tmscsim[1] != -2)
2326                         ptr[(id << 2) + 1] = (u8)tmscsim[1];    /* EE_Speed */
2327         }
2328 }
2329
2330 static int __devinitdata tmscsim_def[] = {
2331         7,
2332         0 /* 10MHz */,
2333         PARITY_CHK_ | SEND_START_ | EN_DISCONNECT_ | SYNC_NEGO_ | TAG_QUEUEING_,
2334         MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION | LUN_CHECK,
2335         3 /* 16 Tags per LUN */,
2336         1 /* s delay after Reset */,
2337 };
2338
2339 /* Copy defaults over set values where missing */
2340 static void __devinit dc390_fill_with_defaults (void)
2341 {
2342         int i;
2343
2344         for (i = 0; i < 6; i++) {
2345                 if (tmscsim[i] < 0 || tmscsim[i] > 255)
2346                         tmscsim[i] = tmscsim_def[i];
2347         }
2348
2349         /* Sanity checks */
2350         if (tmscsim[0] > 7)
2351                 tmscsim[0] = 7;
2352         if (tmscsim[1] > 7)
2353                 tmscsim[1] = 4;
2354         if (tmscsim[4] > 5)
2355                 tmscsim[4] = 4;
2356         if (tmscsim[5] > 180)
2357                 tmscsim[5] = 180;
2358 }
2359
2360 static void __devinit dc390_check_eeprom(struct pci_dev *pdev, u8 index)
2361 {
2362         u8 interpd[] = {1, 3, 5, 10, 16, 30, 60, 120};
2363         u8 EEbuf[128];
2364         u16 *ptr = (u16 *)EEbuf, wval = 0;
2365         int i;
2366
2367         dc390_read_eeprom(pdev, ptr);
2368         memcpy(dc390_eepromBuf[index], EEbuf, EE_ADAPT_SCSI_ID);
2369         memcpy(&dc390_eepromBuf[index][EE_ADAPT_SCSI_ID], 
2370                &EEbuf[REAL_EE_ADAPT_SCSI_ID], EE_LEN - EE_ADAPT_SCSI_ID);
2371
2372         dc390_eepromBuf[index][EE_DELAY] = interpd[dc390_eepromBuf[index][EE_DELAY]];
2373
2374         for (i = 0; i < 0x40; i++, ptr++)
2375                 wval += *ptr;
2376
2377         /* no Tekram EEprom found */
2378         if (wval != 0x1234) {
2379                 int speed;
2380
2381                 printk(KERN_INFO "DC390_init: No EEPROM found! Trying default settings ...\n");
2382
2383                 /*
2384                  * XXX(hch): bogus, because we might have tekram and
2385                  *           non-tekram hbas in a single machine.
2386                  */
2387                 dc390_fill_with_defaults();
2388
2389                 speed = dc390_clock_speed[tmscsim[1]];
2390                 printk(KERN_INFO "DC390: Used defaults: AdaptID=%i, SpeedIdx=%i (%i.%i MHz), "
2391                        "DevMode=0x%02x, AdaptMode=0x%02x, TaggedCmnds=%i (%i), DelayReset=%is\n", 
2392                        tmscsim[0], tmscsim[1], speed / 10, speed % 10,
2393                        (u8)tmscsim[2], (u8)tmscsim[3], tmscsim[4], 2 << (tmscsim[4]), tmscsim[5]);
2394         }
2395 }
2396
2397 static void __devinit dc390_init_hw(struct dc390_acb *pACB, u8 index)
2398 {
2399         struct Scsi_Host *shost = pACB->pScsiHost;
2400         u8 dstate;
2401
2402         /* Disable SCSI bus reset interrupt */
2403         DC390_write8(CtrlReg1, DIS_INT_ON_SCSI_RST | shost->this_id);
2404
2405         if (pACB->Gmode2 & RST_SCSI_BUS) {
2406                 dc390_ResetSCSIBus(pACB);
2407                 udelay(1000);
2408                 shost->last_reset = jiffies + HZ/2 +
2409                         HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
2410         }
2411
2412         pACB->ACBFlag = 0;
2413
2414         /* Reset Pending INT */
2415         DC390_read8(INT_Status);
2416         
2417         /* 250ms selection timeout */
2418         DC390_write8(Scsi_TimeOut, SEL_TIMEOUT);
2419         
2420         /* Conversion factor = 0 , 40MHz clock */
2421         DC390_write8(Clk_Factor, CLK_FREQ_40MHZ);
2422         
2423         /* NOP cmd - clear command register */
2424         DC390_write8(ScsiCmd, NOP_CMD);
2425         
2426         /* Enable Feature and SCSI-2 */
2427         DC390_write8(CtrlReg2, EN_FEATURE+EN_SCSI2_CMD);
2428         
2429         /* Fast clock */
2430         DC390_write8(CtrlReg3, FAST_CLK);
2431
2432         /* Negation */
2433         DC390_write8(CtrlReg4, pACB->glitch_cfg | /* glitch eater */
2434                 (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION) ?
2435                  NEGATE_REQACKDATA : 0);
2436         
2437         /* Clear Transfer Count High: ID */
2438         DC390_write8(CtcReg_High, 0);
2439         DC390_write8(DMA_Cmd, DMA_IDLE_CMD);
2440         DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
2441         DC390_write32(DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
2442
2443         dstate = DC390_read8(DMA_Status);
2444         DC390_write8(DMA_Status, dstate);
2445 }
2446
2447 static int __devinit dc390_probe_one(struct pci_dev *pdev,
2448                                     const struct pci_device_id *id)
2449 {
2450         struct dc390_acb *pACB;
2451         struct Scsi_Host *shost;
2452         unsigned long io_port;
2453         int error = -ENODEV, i;
2454
2455         if (pci_enable_device(pdev))
2456                 goto out;
2457
2458         pci_set_master(pdev);
2459
2460         error = -ENOMEM;
2461         if (disable_clustering)
2462                 driver_template.use_clustering = DISABLE_CLUSTERING;
2463         shost = scsi_host_alloc(&driver_template, sizeof(struct dc390_acb));
2464         if (!shost)
2465                 goto out_disable_device;
2466
2467         pACB = (struct dc390_acb *)shost->hostdata;
2468         memset(pACB, 0, sizeof(struct dc390_acb));
2469
2470         dc390_check_eeprom(pdev, dc390_adapterCnt);
2471         dc390_eeprom_override(dc390_adapterCnt);
2472
2473         io_port = pci_resource_start(pdev, 0);
2474
2475         shost->this_id = dc390_eepromBuf[dc390_adapterCnt][EE_ADAPT_SCSI_ID];
2476         shost->io_port = io_port;
2477         shost->n_io_port = 0x80;
2478         shost->irq = pdev->irq;
2479         shost->base = io_port;
2480         shost->unique_id = io_port;
2481         shost->last_reset = jiffies;
2482         
2483         pACB->pScsiHost = shost;
2484         pACB->IOPortBase = (u16) io_port;
2485         pACB->IRQLevel = pdev->irq;
2486         
2487         shost->max_id = 8;
2488
2489         if (shost->max_id - 1 ==
2490             dc390_eepromBuf[dc390_adapterCnt][EE_ADAPT_SCSI_ID])
2491                 shost->max_id--;
2492
2493         if (dc390_eepromBuf[dc390_adapterCnt][EE_MODE2] & LUN_CHECK)
2494                 shost->max_lun = 8;
2495         else
2496                 shost->max_lun = 1;
2497
2498         pACB->pFreeSRB = pACB->SRB_array;
2499         pACB->SRBCount = MAX_SRB_CNT;
2500         pACB->AdapterIndex = dc390_adapterCnt;
2501         pACB->TagMaxNum =
2502                 2 << dc390_eepromBuf[dc390_adapterCnt][EE_TAG_CMD_NUM];
2503         pACB->Gmode2 = dc390_eepromBuf[dc390_adapterCnt][EE_MODE2];
2504
2505         for (i = 0; i < pACB->SRBCount-1; i++)
2506                 pACB->SRB_array[i].pNextSRB = &pACB->SRB_array[i+1];
2507         pACB->SRB_array[pACB->SRBCount-1].pNextSRB = NULL;
2508         pACB->pTmpSRB = &pACB->TmpSRB;
2509
2510         pACB->sel_timeout = SEL_TIMEOUT;
2511         pACB->glitch_cfg = EATER_25NS;
2512         pACB->pdev = pdev;
2513
2514         if (!request_region(io_port, shost->n_io_port, "tmscsim")) {
2515                 printk(KERN_ERR "DC390: register IO ports error!\n");
2516                 goto out_host_put;
2517         }
2518
2519         /* Reset Pending INT */
2520         DC390_read8_(INT_Status, io_port);
2521
2522         if (request_irq(pdev->irq, do_DC390_Interrupt, IRQF_SHARED,
2523                                 "tmscsim", pACB)) {
2524                 printk(KERN_ERR "DC390: register IRQ error!\n");
2525                 goto out_release_region;
2526         }
2527
2528         dc390_init_hw(pACB, dc390_adapterCnt);
2529         
2530         dc390_adapterCnt++;
2531
2532         pci_set_drvdata(pdev, shost);
2533
2534         error = scsi_add_host(shost, &pdev->dev);
2535         if (error)
2536                 goto out_free_irq;
2537         scsi_scan_host(shost);
2538         return 0;
2539
2540  out_free_irq:
2541         free_irq(pdev->irq, pACB);
2542  out_release_region:
2543         release_region(io_port, shost->n_io_port);
2544  out_host_put:
2545         scsi_host_put(shost);
2546  out_disable_device:
2547         pci_disable_device(pdev);
2548  out:
2549         return error;
2550 }
2551
2552 /**
2553  * dc390_remove_one - Called to remove a single instance of the adapter.
2554  *
2555  * @dev: The PCI device to remove.
2556  */
2557 static void __devexit dc390_remove_one(struct pci_dev *dev)
2558 {
2559         struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
2560         unsigned long iflags;
2561         struct dc390_acb* pACB = (struct dc390_acb*) scsi_host->hostdata;
2562         u8 bval;
2563
2564         scsi_remove_host(scsi_host);
2565
2566         spin_lock_irqsave(scsi_host->host_lock, iflags);
2567         pACB->ACBFlag = RESET_DEV;
2568         bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
2569         DC390_write8 (CtrlReg1, bval);  /* disable interrupt */
2570         if (pACB->Gmode2 & RST_SCSI_BUS)
2571                 dc390_ResetSCSIBus(pACB);
2572         spin_unlock_irqrestore(scsi_host->host_lock, iflags);
2573
2574         free_irq(scsi_host->irq, pACB);
2575         release_region(scsi_host->io_port, scsi_host->n_io_port);
2576
2577         pci_disable_device(dev);
2578         scsi_host_put(scsi_host);
2579         pci_set_drvdata(dev, NULL);
2580 }
2581
2582 static struct pci_device_id tmscsim_pci_tbl[] = {
2583         { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD53C974,
2584                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
2585         { }
2586 };
2587 MODULE_DEVICE_TABLE(pci, tmscsim_pci_tbl);
2588
2589 static struct pci_driver dc390_driver = {
2590         .name           = "tmscsim",
2591         .id_table       = tmscsim_pci_tbl,
2592         .probe          = dc390_probe_one,
2593         .remove         = __devexit_p(dc390_remove_one),
2594 };
2595
2596 static int __init dc390_module_init(void)
2597 {
2598         if (!disable_clustering)
2599                 printk(KERN_INFO "DC390: clustering now enabled by default. If you get problems load\n"
2600                        "\twith \"disable_clustering=1\" and report to maintainers\n");
2601
2602         if (tmscsim[0] == -1 || tmscsim[0] > 15) {
2603                 tmscsim[0] = 7;
2604                 tmscsim[1] = 4;
2605                 tmscsim[2] = PARITY_CHK_ | TAG_QUEUEING_;
2606                 tmscsim[3] = MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION;
2607                 tmscsim[4] = 2;
2608                 tmscsim[5] = 10;
2609                 printk (KERN_INFO "DC390: Using safe settings.\n");
2610         }
2611
2612         return pci_register_driver(&dc390_driver);
2613 }
2614
2615 static void __exit dc390_module_exit(void)
2616 {
2617         pci_unregister_driver(&dc390_driver);
2618 }
2619
2620 module_init(dc390_module_init);
2621 module_exit(dc390_module_exit);
2622
2623 #ifndef MODULE
2624 static int __init dc390_setup (char *str)
2625 {       
2626         int ints[8],i, im;
2627
2628         get_options(str, ARRAY_SIZE(ints), ints);
2629         im = ints[0];
2630
2631         if (im > 6) {
2632                 printk (KERN_NOTICE "DC390: ignore extra params!\n");
2633                 im = 6;
2634         }
2635
2636         for (i = 0; i < im; i++)
2637                 tmscsim[i] = ints[i+1];
2638         /* dc390_checkparams (); */
2639         return 1;
2640 }
2641
2642 __setup("tmscsim=", dc390_setup);
2643 #endif