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