Merge to Fedora kernel-2.6.7-1.441
[linux-2.6.git] / 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 /* Uncomment SA_INTERRUPT, if the driver refuses to share its IRQ with other devices */
183 #define DC390_IRQ SA_SHIRQ /* | SA_INTERRUPT */
184
185 /* DEBUG options */
186 //#define DC390_DEBUG0
187 //#define DC390_DEBUG1
188 //#define DC390_DCBDEBUG
189 //#define DC390_PARSEDEBUG
190 //#define DC390_REMOVABLEDEBUG
191 //#define DC390_LOCKDEBUG
192
193 //#define NOP do{}while(0)
194 #define C_NOP
195
196 /* Debug definitions */
197 #ifdef DC390_DEBUG0
198 # define DEBUG0(x) x
199 #else
200 # define DEBUG0(x) C_NOP
201 #endif
202 #ifdef DC390_DEBUG1
203 # define DEBUG1(x) x
204 #else
205 # define DEBUG1(x) C_NOP
206 #endif
207 #ifdef DC390_DCBDEBUG
208 # define DCBDEBUG(x) x
209 #else
210 # define DCBDEBUG(x) C_NOP
211 #endif
212 #ifdef DC390_PARSEDEBUG
213 # define PARSEDEBUG(x) x
214 #else
215 # define PARSEDEBUG(x) C_NOP
216 #endif
217 #ifdef DC390_REMOVABLEDEBUG
218 # define REMOVABLEDEBUG(x) x
219 #else
220 # define REMOVABLEDEBUG(x) C_NOP
221 #endif
222 #define DCBDEBUG1(x) C_NOP
223
224 /* Includes */
225 #include <linux/module.h>
226 #include <asm/dma.h>
227 #include <asm/io.h>
228 #include <asm/system.h>
229 #include <linux/delay.h>
230 #include <linux/signal.h>
231 #include <linux/sched.h>
232 #include <linux/errno.h>
233 #include <linux/kernel.h>
234 #include <linux/ioport.h>
235 #include <linux/pci.h>
236 #include <linux/proc_fs.h>
237 #include <linux/string.h>
238 #include <linux/ctype.h>
239 #include <linux/mm.h>
240 #include <linux/config.h>
241 #include <linux/version.h>
242 #include <linux/blkdev.h>
243 #include <linux/timer.h>
244 #include <linux/interrupt.h>
245
246 #include "scsi.h"
247 #include <scsi/scsi_host.h>
248 #include <linux/stat.h>
249 #include <scsi/scsicam.h>
250
251 #include "dc390.h"
252
253 #define PCI_DEVICE_ID_AMD53C974         PCI_DEVICE_ID_AMD_SCSI
254
255 /* Locking */
256
257 /* Note: Starting from 2.1.9x, the mid-level scsi code issues a 
258  * spinlock_irqsave (&io_request_lock) before calling the driver's 
259  * routines, so we don't need to lock, except in the IRQ handler.
260  * The policy 3, let the midlevel scsi code do the io_request_locks
261  * and us locking on a driver specific lock, shouldn't hurt anybody; it
262  * just causes a minor performance degradation for setting the locks.
263  */
264
265 /* spinlock things
266  * level 3: lock on both adapter specific locks and (global) io_request_lock
267  * level 2: lock on adapter specific locks only
268  * level 1: rely on the locking of the mid level code (io_request_lock)
269  * undef  : traditional save_flags; cli; restore_flags;
270  */
271
272 #include <linux/init.h>
273 #include <linux/spinlock.h>
274
275 static struct pci_device_id tmscsim_pci_tbl[] = {
276         {
277                 .vendor         = PCI_VENDOR_ID_AMD,
278                 .device         = PCI_DEVICE_ID_AMD53C974,
279                 .subvendor      = PCI_ANY_ID,
280                 .subdevice      = PCI_ANY_ID,
281         },
282         { }             /* Terminating entry */
283 };
284 MODULE_DEVICE_TABLE(pci, tmscsim_pci_tbl);
285
286 #define USE_SPINLOCKS 1
287
288 #define DC390_IFLAGS unsigned long iflags
289 #define DC390_LOCK_IO(dev) spin_lock_irqsave (((struct Scsi_Host *)dev)->host_lock, iflags)
290 #define DC390_UNLOCK_IO(dev) spin_unlock_irqrestore (((struct Scsi_Host *)dev)->host_lock, iflags)
291
292 /* These macros are used for uniform access to 2.0.x and 2.1.x PCI config space*/
293
294 #define PDEV pdev
295 #define PDEVDECL struct pci_dev *pdev
296 #define PDEVDECL0 struct pci_dev *pdev = NULL
297 #define PDEVDECL1 struct pci_dev *pdev
298 #define PDEVSET pACB->pdev=pdev
299 #define PDEVSET1 pdev=pACB->pdev
300 #define PCI_WRITE_CONFIG_BYTE(pd, rv, bv) pci_write_config_byte (pd, rv, bv)
301 #define PCI_READ_CONFIG_BYTE(pd, rv, bv) pci_read_config_byte (pd, rv, bv)
302 #define PCI_WRITE_CONFIG_WORD(pd, rv, bv) pci_write_config_word (pd, rv, bv)
303 #define PCI_READ_CONFIG_WORD(pd, rv, bv) pci_read_config_word (pd, rv, bv)
304 #define PCI_PRESENT (1)
305 #define PCI_GET_IO_AND_IRQ do{io_port = pci_resource_start (pdev, 0); irq = pdev->irq;} while(0)
306
307 #include "tmscsim.h"
308
309 #ifndef __init
310 # define __init
311 #endif
312
313 static UCHAR dc390_StartSCSI( PACB pACB, PDCB pDCB, PSRB pSRB );
314 static void dc390_DataOut_0( PACB pACB, PSRB pSRB, PUCHAR psstatus);
315 static void dc390_DataIn_0( PACB pACB, PSRB pSRB, PUCHAR psstatus);
316 static void dc390_Command_0( PACB pACB, PSRB pSRB, PUCHAR psstatus);
317 static void dc390_Status_0( PACB pACB, PSRB pSRB, PUCHAR psstatus);
318 static void dc390_MsgOut_0( PACB pACB, PSRB pSRB, PUCHAR psstatus);
319 static void dc390_MsgIn_0( PACB pACB, PSRB pSRB, PUCHAR psstatus);
320 static void dc390_DataOutPhase( PACB pACB, PSRB pSRB, PUCHAR psstatus);
321 static void dc390_DataInPhase( PACB pACB, PSRB pSRB, PUCHAR psstatus);
322 static void dc390_CommandPhase( PACB pACB, PSRB pSRB, PUCHAR psstatus);
323 static void dc390_StatusPhase( PACB pACB, PSRB pSRB, PUCHAR psstatus);
324 static void dc390_MsgOutPhase( PACB pACB, PSRB pSRB, PUCHAR psstatus);
325 static void dc390_MsgInPhase( PACB pACB, PSRB pSRB, PUCHAR psstatus);
326 static void dc390_Nop_0( PACB pACB, PSRB pSRB, PUCHAR psstatus);
327 static void dc390_Nop_1( PACB pACB, PSRB pSRB, PUCHAR psstatus);
328
329 static void dc390_SetXferRate( PACB pACB, PDCB pDCB );
330 static void dc390_Disconnect( PACB pACB );
331 static void dc390_Reselect( PACB pACB );
332 static void dc390_SRBdone( PACB pACB, PDCB pDCB, PSRB pSRB );
333 static void dc390_DoingSRB_Done( PACB pACB, PSCSICMD cmd );
334 static void dc390_ScsiRstDetect( PACB pACB );
335 static void dc390_ResetSCSIBus( PACB pACB );
336 static void __inline__ dc390_RequestSense( PACB pACB, PDCB pDCB, PSRB pSRB );
337 static void __inline__ dc390_InvalidCmd( PACB pACB );
338 static void __inline__ dc390_EnableMsgOut_Abort (PACB, PSRB);
339 static void dc390_remove_dev (PACB pACB, PDCB pDCB);
340 static irqreturn_t do_DC390_Interrupt( int, void *, struct pt_regs *);
341
342 static int    dc390_initAdapter( PSH psh, ULONG io_port, UCHAR Irq, UCHAR index );
343 static void   dc390_initDCB( PACB pACB, PDCB *ppDCB, UCHAR id, UCHAR lun);
344 static void   dc390_updateDCB (PACB pACB, PDCB pDCB);
345
346 static int DC390_release(struct Scsi_Host *host);
347 static int dc390_shutdown (struct Scsi_Host *host);
348 static int DC390_proc_info (struct Scsi_Host *shpnt, char *buffer, char **start,
349                             off_t offset, int length, int inout);
350
351 static PACB     dc390_pACB_start= NULL;
352 static PACB     dc390_pACB_current = NULL;
353 static ULONG    dc390_lastabortedpid = 0;
354 static UINT     dc390_laststatus = 0;
355 static UCHAR    dc390_adapterCnt = 0;
356
357 /* Startup values, to be overriden on the commandline */
358 static int tmscsim[] = {-2, -2, -2, -2, -2, -2};
359 static int tmscsim_paramnum = ARRAY_SIZE(tmscsim);
360
361 module_param_array(tmscsim, int, tmscsim_paramnum, 0);
362 MODULE_PARM_DESC(tmscsim, "Host SCSI ID, Speed (0=10MHz), Device Flags, Adapter Flags, Max Tags (log2(tags)-1), DelayReset (s)");
363 MODULE_AUTHOR("C.L. Huang / Kurt Garloff");
364 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram DC390 and other AMD53C974A based PCI SCSI adapters");
365 MODULE_LICENSE("GPL");
366 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
367
368 static PVOID dc390_phase0[]={
369        dc390_DataOut_0,
370        dc390_DataIn_0,
371        dc390_Command_0,
372        dc390_Status_0,
373        dc390_Nop_0,
374        dc390_Nop_0,
375        dc390_MsgOut_0,
376        dc390_MsgIn_0,
377        dc390_Nop_1
378        };
379
380 static PVOID dc390_phase1[]={
381        dc390_DataOutPhase,
382        dc390_DataInPhase,
383        dc390_CommandPhase,
384        dc390_StatusPhase,
385        dc390_Nop_0,
386        dc390_Nop_0,
387        dc390_MsgOutPhase,
388        dc390_MsgInPhase,
389        dc390_Nop_1
390        };
391
392 #ifdef DC390_DEBUG1
393 static char* dc390_p0_str[] = {
394        "dc390_DataOut_0",
395        "dc390_DataIn_0",
396        "dc390_Command_0",
397        "dc390_Status_0",
398        "dc390_Nop_0",
399        "dc390_Nop_0",
400        "dc390_MsgOut_0",
401        "dc390_MsgIn_0",
402        "dc390_Nop_1"
403        };
404      
405 static char* dc390_p1_str[] = {
406        "dc390_DataOutPhase",
407        "dc390_DataInPhase",
408        "dc390_CommandPhase",
409        "dc390_StatusPhase",
410        "dc390_Nop_0",
411        "dc390_Nop_0",
412        "dc390_MsgOutPhase",
413        "dc390_MsgInPhase",
414        "dc390_Nop_1"
415        };
416 #endif   
417
418 /* Devices erroneously pretending to be able to do TagQ */
419 static UCHAR  dc390_baddevname1[2][28] ={
420        "SEAGATE ST3390N         9546",
421        "HP      C3323-300       4269"};
422 #define BADDEVCNT       2
423
424 static char*  dc390_adapname = "DC390";
425 static UCHAR  dc390_eepromBuf[MAX_ADAPTER_NUM][EE_LEN];
426 static UCHAR  dc390_clock_period1[] = {4, 5, 6, 7, 8, 10, 13, 20};
427 static UCHAR  dc390_clock_speed[] = {100,80,67,57,50, 40, 31, 20};
428
429 /***********************************************************************
430  * Functions for access to DC390 EEPROM
431  * and some to emulate it
432  *
433  **********************************************************************/
434
435
436 static void __init dc390_EnDisableCE( UCHAR mode, PDEVDECL, PUCHAR regval )
437 {
438     UCHAR bval;
439
440     bval = 0;
441     if(mode == ENABLE_CE)
442         *regval = 0xc0;
443     else
444         *regval = 0x80;
445     PCI_WRITE_CONFIG_BYTE(PDEV, *regval, bval);
446     if(mode == DISABLE_CE)
447         PCI_WRITE_CONFIG_BYTE(PDEV, *regval, bval);
448     udelay(160);
449 }
450
451
452 /* Override EEprom values with explicitly set values */
453 static void __init dc390_EEprom_Override (UCHAR index)
454 {
455     PUCHAR ptr;
456     UCHAR  id;
457     ptr = (PUCHAR) dc390_eepromBuf[index];
458     
459     /* Adapter Settings */
460     if (tmscsim[0] != -2)
461         ptr[EE_ADAPT_SCSI_ID] = (UCHAR)tmscsim[0];      /* Adapter ID */
462     if (tmscsim[3] != -2)
463         ptr[EE_MODE2] = (UCHAR)tmscsim[3];
464     if (tmscsim[5] != -2)
465         ptr[EE_DELAY] = tmscsim[5];                     /* Reset delay */
466     if (tmscsim[4] != -2)
467         ptr[EE_TAG_CMD_NUM] = (UCHAR)tmscsim[4];        /* Tagged Cmds */
468     
469     /* Device Settings */
470     for (id = 0; id < MAX_SCSI_ID; id++)
471     {
472         if (tmscsim[2] != -2)
473                 ptr[id<<2] = (UCHAR)tmscsim[2];         /* EE_MODE1 */
474         if (tmscsim[1] != -2)
475                 ptr[(id<<2) + 1] = (UCHAR)tmscsim[1];   /* EE_Speed */
476     }
477 }
478
479 /* Handle "-1" case */
480 static void __init dc390_check_for_safe_settings (void)
481 {
482         if (tmscsim[0] == -1 || tmscsim[0] > 15) /* modules-2.0.0 passes -1 as string */
483         {
484                 tmscsim[0] = 7; tmscsim[1] = 4;
485                 tmscsim[2] = 0x09; tmscsim[3] = 0x0f;
486                 tmscsim[4] = 2; tmscsim[5] = 10;
487                 printk (KERN_INFO "DC390: Using safe settings.\n");
488         }
489 }
490
491
492 static int __initdata tmscsim_def[] = {7, 0 /* 10MHz */,
493                 PARITY_CHK_ | SEND_START_ | EN_DISCONNECT_
494                 | SYNC_NEGO_ | TAG_QUEUEING_,
495                 MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION
496                 /* | NO_SEEK */
497 # ifdef CONFIG_SCSI_MULTI_LUN
498                 | LUN_CHECK
499 # endif
500                 , 3 /* 16 Tags per LUN */, 1 /* s delay after Reset */ };
501
502 /* Copy defaults over set values where missing */
503 static void __init dc390_fill_with_defaults (void)
504 {
505         int i;
506         PARSEDEBUG(printk(KERN_INFO "DC390: setup %08x %08x %08x %08x %08x %08x\n", tmscsim[0],\
507                           tmscsim[1], tmscsim[2], tmscsim[3], tmscsim[4], tmscsim[5]));
508         for (i = 0; i < 6; i++)
509         {
510                 if (tmscsim[i] < 0 || tmscsim[i] > 255)
511                         tmscsim[i] = tmscsim_def[i];
512         }
513         /* Sanity checks */
514         if (tmscsim[0] >   7) tmscsim[0] =   7;
515         if (tmscsim[1] >   7) tmscsim[1] =   4;
516         if (tmscsim[4] >   5) tmscsim[4] =   4;
517         if (tmscsim[5] > 180) tmscsim[5] = 180;
518 }
519
520 /* Override defaults on cmdline:
521  * tmscsim: AdaptID, MaxSpeed (Index), DevMode (Bitmapped), AdaptMode (Bitmapped)
522  */
523 static int __init dc390_setup (char *str)
524 {       
525         int ints[8];
526         int i, im;
527         (void)get_options (str, ARRAY_SIZE(ints), ints);
528         im = ints[0];
529         if (im > 6)
530         {
531                 printk (KERN_NOTICE "DC390: ignore extra params!\n");
532                 im = 6;
533         }
534         for (i = 0; i < im; i++)
535                 tmscsim[i] = ints[i+1];
536         /* dc390_checkparams (); */
537         return 1;
538 }
539 #ifndef MODULE
540 __setup("tmscsim=", dc390_setup);
541 #endif
542
543 static void __init dc390_EEpromOutDI( PDEVDECL, PUCHAR regval, UCHAR Carry )
544 {
545     UCHAR bval;
546
547     bval = 0;
548     if(Carry)
549     {
550         bval = 0x40;
551         *regval = 0x80;
552         PCI_WRITE_CONFIG_BYTE(PDEV, *regval, bval);
553     }
554     udelay(160);
555     bval |= 0x80;
556     PCI_WRITE_CONFIG_BYTE(PDEV, *regval, bval);
557     udelay(160);
558     bval = 0;
559     PCI_WRITE_CONFIG_BYTE(PDEV, *regval, bval);
560     udelay(160);
561 }
562
563
564 static UCHAR __init dc390_EEpromInDO( PDEVDECL )
565 {
566     UCHAR bval;
567
568     PCI_WRITE_CONFIG_BYTE(PDEV, 0x80, 0x80);
569     udelay(160);
570     PCI_WRITE_CONFIG_BYTE(PDEV, 0x80, 0x40);
571     udelay(160);
572     PCI_READ_CONFIG_BYTE(PDEV, 0x00, &bval);
573     if(bval == 0x22)
574         return(1);
575     else
576         return(0);
577 }
578
579
580 static USHORT __init dc390_EEpromGetData1( PDEVDECL )
581 {
582     UCHAR i;
583     UCHAR carryFlag;
584     USHORT wval;
585
586     wval = 0;
587     for(i=0; i<16; i++)
588     {
589         wval <<= 1;
590         carryFlag = dc390_EEpromInDO(PDEV);
591         wval |= carryFlag;
592     }
593     return(wval);
594 }
595
596
597 static void __init dc390_Prepare( PDEVDECL, PUCHAR regval, UCHAR EEpromCmd )
598 {
599     UCHAR i,j;
600     UCHAR carryFlag;
601
602     carryFlag = 1;
603     j = 0x80;
604     for(i=0; i<9; i++)
605     {
606         dc390_EEpromOutDI(PDEV,regval,carryFlag);
607         carryFlag = (EEpromCmd & j) ? 1 : 0;
608         j >>= 1;
609     }
610 }
611
612
613 static void __init dc390_ReadEEprom( PDEVDECL, PUSHORT ptr)
614 {
615     UCHAR   regval,cmd;
616     UCHAR   i;
617
618     cmd = EEPROM_READ;
619     for(i=0; i<0x40; i++)
620     {
621         dc390_EnDisableCE(ENABLE_CE, PDEV, &regval);
622         dc390_Prepare(PDEV, &regval, cmd++);
623         *ptr++ = dc390_EEpromGetData1(PDEV);
624         dc390_EnDisableCE(DISABLE_CE, PDEV, &regval);
625     }
626 }
627
628
629 static void __init dc390_interpret_delay (UCHAR index)
630 {
631     char interpd [] = {1,3,5,10,16,30,60,120};
632     dc390_eepromBuf[index][EE_DELAY] = interpd [dc390_eepromBuf[index][EE_DELAY]];
633 }
634
635 static UCHAR __init dc390_CheckEEpromCheckSum( PDEVDECL, UCHAR index )
636 {
637     UCHAR  i;
638     char  EEbuf[128];
639     USHORT wval, *ptr = (PUSHORT)EEbuf;
640
641     dc390_ReadEEprom( PDEV, ptr );
642     memcpy (dc390_eepromBuf[index], EEbuf, EE_ADAPT_SCSI_ID);
643     memcpy (&dc390_eepromBuf[index][EE_ADAPT_SCSI_ID], 
644             &EEbuf[REAL_EE_ADAPT_SCSI_ID], EE_LEN - EE_ADAPT_SCSI_ID);
645     dc390_interpret_delay (index);
646     
647     wval = 0;
648     for(i=0; i<0x40; i++, ptr++)
649         wval += *ptr;
650     return (wval == 0x1234 ? 0 : 1);
651 }
652
653
654 /***********************************************************************
655  * Functions for the management of the internal structures 
656  * (DCBs, SRBs, Queueing)
657  *
658  **********************************************************************/
659 static PDCB __inline__ dc390_findDCB ( PACB pACB, UCHAR id, UCHAR lun)
660 {
661    PDCB pDCB = pACB->pLinkDCB; if (!pDCB) return 0;
662    while (pDCB->TargetID != id || pDCB->TargetLUN != lun)
663      {
664         pDCB = pDCB->pNextDCB;
665         if (pDCB == pACB->pLinkDCB)
666           {
667              DCBDEBUG(printk (KERN_WARNING "DC390: DCB not found (DCB=%p, DCBmap[%2x]=%2x)\n",
668                               pDCB, id, pACB->DCBmap[id]));
669              return 0;
670           }
671      }
672    DCBDEBUG1( printk (KERN_DEBUG "DCB %p (%02x,%02x) found.\n", \
673                       pDCB, pDCB->TargetID, pDCB->TargetLUN));
674    return pDCB;
675 }
676
677 /* Queueing philosphy:
678  * There are a couple of lists:
679  * - Query: Contains the Scsi Commands not yet turned into SRBs (per ACB)
680  *   (Note: For new EH, it is unnecessary!)
681  * - Waiting: Contains a list of SRBs not yet sent (per DCB)
682  * - Free: List of free SRB slots
683  * 
684  * If there are no waiting commands for the DCB, the new one is sent to the bus
685  * otherwise the oldest one is taken from the Waiting list and the new one is 
686  * queued to the Waiting List
687  * 
688  * Lists are managed using two pointers and eventually a counter
689  */
690
691
692 #if 0
693 /* Look for a SCSI cmd in a SRB queue */
694 static PSRB dc390_find_cmd_in_SRBq (PSCSICMD cmd, PSRB queue)
695 {
696     PSRB q = queue;
697     while (q)
698     {
699         if (q->pcmd == cmd) return q;
700         q = q->pNextSRB;
701         if (q == queue) return 0;
702     }
703     return q;
704 }
705 #endif
706
707 /* Return next free SRB */
708 static __inline__ PSRB dc390_Free_get ( PACB pACB )
709 {
710     PSRB   pSRB;
711
712     pSRB = pACB->pFreeSRB;
713     DEBUG0(printk ("DC390: Get Free SRB %p\n", pSRB));
714     if( pSRB )
715     {
716         pACB->pFreeSRB = pSRB->pNextSRB;
717         pSRB->pNextSRB = NULL;
718     }
719
720     return( pSRB );
721 }
722
723 /* Insert SRB oin top of free list */
724 static __inline__ void dc390_Free_insert (PACB pACB, PSRB pSRB)
725 {
726     DEBUG0(printk ("DC390: Free SRB %p\n", pSRB));
727     pSRB->pNextSRB = pACB->pFreeSRB;
728     pACB->pFreeSRB = pSRB;
729 }
730
731
732 /* Inserts a SRB to the top of the Waiting list */
733 static __inline__ void dc390_Waiting_insert ( PDCB pDCB, PSRB pSRB )
734 {
735     DEBUG0(printk ("DC390: Insert pSRB %p cmd %li to Waiting\n", pSRB, pSRB->pcmd->pid));
736     pSRB->pNextSRB = pDCB->pWaitingSRB;
737     if (!pDCB->pWaitingSRB)
738         pDCB->pWaitLast = pSRB;
739     pDCB->pWaitingSRB = pSRB;
740     pDCB->WaitSRBCnt++;
741 }
742
743
744 /* Queue SRB to waiting list */
745 static __inline__ void dc390_Waiting_append ( PDCB pDCB, PSRB pSRB)
746 {
747         DEBUG0(printk ("DC390: Append pSRB %p cmd %li to Waiting\n", pSRB, pSRB->pcmd->pid));
748     if( pDCB->pWaitingSRB )
749         pDCB->pWaitLast->pNextSRB = pSRB;
750     else
751         pDCB->pWaitingSRB = pSRB;
752
753     pDCB->pWaitLast = pSRB;
754     pSRB->pNextSRB = NULL;
755     pDCB->WaitSRBCnt++;
756     pDCB->pDCBACB->CmdInQ++;
757 }
758
759 static __inline__ void dc390_Going_append (PDCB pDCB, PSRB pSRB)
760 {
761     pDCB->GoingSRBCnt++;
762     DEBUG0(printk("DC390: Append SRB %p to Going\n", pSRB));
763     /* Append to the list of Going commands */
764     if( pDCB->pGoingSRB )
765         pDCB->pGoingLast->pNextSRB = pSRB;
766     else
767         pDCB->pGoingSRB = pSRB;
768
769     pDCB->pGoingLast = pSRB;
770     /* No next one in sent list */
771     pSRB->pNextSRB = NULL;
772 }
773
774 static __inline__ void dc390_Going_remove (PDCB pDCB, PSRB pSRB)
775 {
776         DEBUG0(printk("DC390: Remove SRB %p from Going\n", pSRB));
777    if (pSRB == pDCB->pGoingSRB)
778         pDCB->pGoingSRB = pSRB->pNextSRB;
779    else
780      {
781         PSRB psrb = pDCB->pGoingSRB;
782         while (psrb && psrb->pNextSRB != pSRB)
783           psrb = psrb->pNextSRB;
784         if (!psrb) 
785           { printk (KERN_ERR "DC390: Remove non-ex. SRB %p from Going!\n", pSRB); return; }
786         psrb->pNextSRB = pSRB->pNextSRB;
787         if (pSRB == pDCB->pGoingLast)
788           pDCB->pGoingLast = psrb;
789      }
790    pDCB->GoingSRBCnt--;
791 }
792
793 /* Moves SRB from Going list to the top of Waiting list */
794 static void dc390_Going_to_Waiting ( PDCB pDCB, PSRB pSRB )
795 {
796     DEBUG0(printk(KERN_INFO "DC390: Going_to_Waiting (SRB %p) pid = %li\n", pSRB, pSRB->pcmd->pid));
797     /* Remove SRB from Going */
798     dc390_Going_remove (pDCB, pSRB);
799     /* Insert on top of Waiting */
800     dc390_Waiting_insert (pDCB, pSRB);
801     /* Tag Mask must be freed elsewhere ! (KG, 99/06/18) */
802 }
803
804 /* Moves first SRB from Waiting list to Going list */
805 static __inline__ void dc390_Waiting_to_Going ( PDCB pDCB, PSRB pSRB )
806 {       
807         /* Remove from waiting list */
808         DEBUG0(printk("DC390: Remove SRB %p from head of Waiting\n", pSRB));
809         pDCB->pWaitingSRB = pSRB->pNextSRB;
810         if( !pDCB->pWaitingSRB ) pDCB->pWaitLast = NULL;
811         pDCB->WaitSRBCnt--;
812         dc390_Going_append (pDCB, pSRB);
813 }
814
815 static void DC390_waiting_timed_out (unsigned long ptr);
816 /* Sets the timer to wake us up */
817 static void dc390_waiting_timer (PACB pACB, unsigned long to)
818 {
819         if (timer_pending (&pACB->Waiting_Timer)) return;
820         init_timer (&pACB->Waiting_Timer);
821         pACB->Waiting_Timer.function = DC390_waiting_timed_out;
822         pACB->Waiting_Timer.data = (unsigned long)pACB;
823         if (time_before (jiffies + to, pACB->pScsiHost->last_reset))
824                 pACB->Waiting_Timer.expires = pACB->pScsiHost->last_reset + 1;
825         else
826                 pACB->Waiting_Timer.expires = jiffies + to + 1;
827         add_timer (&pACB->Waiting_Timer);
828 }
829
830
831 /* Send the next command from the waiting list to the bus */
832 static void dc390_Waiting_process ( PACB pACB )
833 {
834     PDCB   ptr, ptr1;
835     PSRB   pSRB;
836
837     if( (pACB->pActiveDCB) || (pACB->ACBFlag & (RESET_DETECT+RESET_DONE+RESET_DEV) ) )
838         return;
839     if (timer_pending (&pACB->Waiting_Timer)) del_timer (&pACB->Waiting_Timer);
840     ptr = pACB->pDCBRunRobin;
841     if( !ptr )
842       {
843         ptr = pACB->pLinkDCB;
844         pACB->pDCBRunRobin = ptr;
845       }
846     ptr1 = ptr;
847     if (!ptr1) return;
848     do 
849       {
850         pACB->pDCBRunRobin = ptr1->pNextDCB;
851         if( !( pSRB = ptr1->pWaitingSRB ) ||
852             ( ptr1->MaxCommand <= ptr1->GoingSRBCnt ))
853           ptr1 = ptr1->pNextDCB;
854         else
855           {
856             /* Try to send to the bus */
857             if( !dc390_StartSCSI(pACB, ptr1, pSRB) )
858               dc390_Waiting_to_Going (ptr1, pSRB);
859             else
860               dc390_waiting_timer (pACB, HZ/5);
861             break;
862           }
863       } while (ptr1 != ptr);
864     return;
865 }
866
867 /* Wake up waiting queue */
868 static void DC390_waiting_timed_out (unsigned long ptr)
869 {
870         PACB pACB = (PACB)ptr;
871         DC390_IFLAGS;
872         DEBUG0(printk ("DC390: Debug: Waiting queue woken up by timer!\n"));
873         DC390_LOCK_IO(pACB->pScsiHost);
874         dc390_Waiting_process (pACB);
875         DC390_UNLOCK_IO(pACB->pScsiHost);
876 }
877
878 /***********************************************************************
879  * Function: static void dc390_SendSRB (PACB pACB, PSRB pSRB)
880  *
881  * Purpose: Send SCSI Request Block (pSRB) to adapter (pACB)
882  *
883  ***********************************************************************/
884
885 static void dc390_SendSRB( PACB pACB, PSRB pSRB )
886 {
887     PDCB   pDCB;
888
889     pDCB = pSRB->pSRBDCB;
890     if( (pDCB->MaxCommand <= pDCB->GoingSRBCnt) || (pACB->pActiveDCB) ||
891         (pACB->ACBFlag & (RESET_DETECT+RESET_DONE+RESET_DEV)) )
892     {
893         dc390_Waiting_append (pDCB, pSRB);
894         dc390_Waiting_process (pACB);
895         return;
896     }
897
898 #if 0
899     if( pDCB->pWaitingSRB )
900     {
901         dc390_Waiting_append (pDCB, pSRB);
902 /*      pSRB = GetWaitingSRB(pDCB); */  /* non-existent */
903         pSRB = pDCB->pWaitingSRB;
904         /* Remove from waiting list */
905         pDCB->pWaitingSRB = pSRB->pNextSRB;
906         pSRB->pNextSRB = NULL;
907         if (!pDCB->pWaitingSRB) pDCB->pWaitLast = NULL;
908     }
909 #endif
910         
911     if (!dc390_StartSCSI(pACB, pDCB, pSRB))
912         dc390_Going_append (pDCB, pSRB);
913     else {
914         dc390_Waiting_insert (pDCB, pSRB);
915         dc390_waiting_timer (pACB, HZ/5);
916     }
917 }
918
919 static struct scatterlist* dc390_sg_build_single(struct scatterlist *sg, void *addr, unsigned int length)
920 {
921         memset(sg, 0, sizeof(struct scatterlist));
922         sg->page        = virt_to_page(addr);
923         sg->length      = length;
924         sg->offset      = (unsigned long)addr & ~PAGE_MASK;
925         return sg;
926 }
927
928 /* Create pci mapping */
929 static int dc390_pci_map (PSRB pSRB)
930 {
931         int error = 0;
932         Scsi_Cmnd *pcmd = pSRB->pcmd;
933         struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
934         dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp));
935
936         /* Map sense buffer */
937         if (pSRB->SRBFlag & AUTO_REQSENSE) {
938                 pSRB->pSegmentList      = dc390_sg_build_single(&pSRB->Segmentx, pcmd->sense_buffer, sizeof(pcmd->sense_buffer));
939                 pSRB->SGcount           = pci_map_sg(pdev, pSRB->pSegmentList, 1,
940                                                      DMA_FROM_DEVICE);
941                 cmdp->saved_dma_handle  = sg_dma_address(pSRB->pSegmentList);
942
943                 /* TODO: error handling */
944                 if (pSRB->SGcount != 1)
945                         error = 1;
946                 DEBUG1(printk("%s(): Mapped sense buffer %p at %x\n", __FUNCTION__, pcmd->sense_buffer, cmdp->saved_dma_handle));
947         /* Map SG list */
948         } else if (pcmd->use_sg) {
949                 pSRB->pSegmentList      = (PSGL) pcmd->request_buffer;
950                 pSRB->SGcount           = pci_map_sg(pdev, pSRB->pSegmentList, pcmd->use_sg,
951                                                      scsi_to_pci_dma_dir(pcmd->sc_data_direction));
952                 /* TODO: error handling */
953                 if (!pSRB->SGcount)
954                         error = 1;
955                 DEBUG1(printk("%s(): Mapped SG %p with %d (%d) elements\n",\
956                               __FUNCTION__, pcmd->request_buffer, pSRB->SGcount, pcmd->use_sg));
957         /* Map single segment */
958         } else if (pcmd->request_buffer && pcmd->request_bufflen) {
959                 pSRB->pSegmentList      = dc390_sg_build_single(&pSRB->Segmentx, pcmd->request_buffer, pcmd->request_bufflen);
960                 pSRB->SGcount           = pci_map_sg(pdev, pSRB->pSegmentList, 1,
961                                                      scsi_to_pci_dma_dir(pcmd->sc_data_direction));
962                 cmdp->saved_dma_handle  = sg_dma_address(pSRB->pSegmentList);
963
964                 /* TODO: error handling */
965                 if (pSRB->SGcount != 1)
966                         error = 1;
967                 DEBUG1(printk("%s(): Mapped request buffer %p at %x\n", __FUNCTION__, pcmd->request_buffer, cmdp->saved_dma_handle));
968         /* No mapping !? */     
969         } else
970                 pSRB->SGcount = 0;
971
972         return error;
973 }
974
975 /* Remove pci mapping */
976 static void dc390_pci_unmap (PSRB pSRB)
977 {
978         Scsi_Cmnd* pcmd = pSRB->pcmd;
979         struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
980         DEBUG1(dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp)));
981
982         if (pSRB->SRBFlag) {
983                 pci_unmap_sg(pdev, &pSRB->Segmentx, 1, DMA_FROM_DEVICE);
984                 DEBUG1(printk("%s(): Unmapped sense buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
985         } else if (pcmd->use_sg) {
986                 pci_unmap_sg(pdev, pcmd->request_buffer, pcmd->use_sg, scsi_to_pci_dma_dir(pcmd->sc_data_direction));
987                 DEBUG1(printk("%s(): Unmapped SG at %p with %d elements\n", __FUNCTION__, pcmd->request_buffer, pcmd->use_sg));
988         } else if (pcmd->request_buffer && pcmd->request_bufflen) {
989                 pci_unmap_sg(pdev, &pSRB->Segmentx, 1, scsi_to_pci_dma_dir(pcmd->sc_data_direction));
990                 DEBUG1(printk("%s(): Unmapped request buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
991         }
992 }
993
994
995 /***********************************************************************
996  * Function: static void dc390_BuildSRB (Scsi_Cmd *pcmd, PDCB pDCB, 
997  *                                       PSRB pSRB)
998  *
999  * Purpose: Prepare SRB for being sent to Device DCB w/ command *pcmd
1000  *
1001  ***********************************************************************/
1002
1003 static void dc390_BuildSRB (Scsi_Cmnd* pcmd, PDCB pDCB, PSRB pSRB)
1004 {
1005     pSRB->pSRBDCB = pDCB;
1006     pSRB->pcmd = pcmd;
1007     //pSRB->ScsiCmdLen = pcmd->cmd_len;
1008     //memcpy (pSRB->CmdBlock, pcmd->cmnd, pcmd->cmd_len);
1009     
1010     pSRB->SGIndex = 0;
1011     pSRB->AdaptStatus = 0;
1012     pSRB->TargetStatus = 0;
1013     pSRB->MsgCnt = 0;
1014     if( pDCB->DevType != TYPE_TAPE )
1015         pSRB->RetryCnt = 1;
1016     else
1017         pSRB->RetryCnt = 0;
1018     pSRB->SRBStatus = 0;
1019     pSRB->SRBFlag = 0;
1020     pSRB->SRBState = 0;
1021     pSRB->TotalXferredLen = 0;
1022     pSRB->SGBusAddr = 0;
1023     pSRB->SGToBeXferLen = 0;
1024     pSRB->ScsiPhase = 0;
1025     pSRB->EndMessage = 0;
1026     pSRB->TagNumber = 255;
1027     /* KG: deferred PCI mapping to dc390_StartSCSI */
1028 }
1029
1030 /***********************************************************************
1031  * Function : static int DC390_queue_command (Scsi_Cmnd *cmd,
1032  *                                             void (*done)(Scsi_Cmnd *))
1033  *
1034  * Purpose : enqueues a SCSI command
1035  *
1036  * Inputs : cmd - SCSI command, done - callback function called on 
1037  *          completion, with a pointer to the command descriptor.
1038  *
1039  * Returns : (depending on kernel version)
1040  * 2.0.x: always return 0
1041  * 2.1.x: old model: (use_new_eh_code == 0): like 2.0.x
1042  *        TO BE DONE:
1043  *        new model: return 0 if successful, or must not be re-queued
1044  *                   return 1 if command cannot be queued (queue full)
1045  *                   command will be inserted in midlevel queue then ...
1046  *
1047  ***********************************************************************/
1048
1049 static int DC390_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *))
1050 {
1051     PDCB   pDCB = (PDCB) cmd->device->hostdata;
1052     PSRB   pSRB;
1053     PACB   pACB = (PACB) cmd->device->host->hostdata;
1054
1055     DEBUG0(/*  if(pACB->scan_devices) */        \
1056         printk(KERN_INFO "DC390: Queue Cmd=%02x,Tgt=%d,LUN=%d (pid=%li), buffer=%p\n",\
1057                cmd->cmnd[0],cmd->device->id,cmd->device->lun,cmd->pid, cmd->buffer));
1058
1059     /* TODO: Change the policy: Always accept TEST_UNIT_READY or INQUIRY 
1060      * commands and alloc a DCB for the device if not yet there. DCB will
1061      * be removed in dc390_SRBdone if SEL_TIMEOUT */
1062     if (!(pACB->scan_devices) && !(pACB->DCBmap[cmd->device->id] & (1 << cmd->device->lun))) {
1063         printk(KERN_INFO "DC390: Ignore target %02x lun %02x\n",
1064                 cmd->device->id, cmd->device->lun); 
1065         goto fail;
1066     }
1067
1068     /* Should it be: BUG_ON(!pDCB); ? */
1069
1070     if (!pDCB)
1071     {  /* should never happen */
1072         printk (KERN_ERR "DC390: no DCB found, target %02x lun %02x\n", 
1073                 cmd->device->id, cmd->device->lun);
1074         goto fail;
1075     }
1076
1077     pACB->Cmds++;
1078     cmd->scsi_done = done;
1079     cmd->result = 0;
1080
1081     pSRB = dc390_Free_get(pACB);
1082     if (!pSRB)
1083             goto requeue;
1084
1085     dc390_BuildSRB(cmd, pDCB, pSRB);
1086     if (pDCB->pWaitingSRB) {
1087             dc390_Waiting_append(pDCB, pSRB);
1088             dc390_Waiting_process(pACB);
1089     } else
1090             dc390_SendSRB(pACB, pSRB);
1091
1092     DEBUG1(printk (KERN_DEBUG " ... command (pid %li) queued successfully.\n", cmd->pid));
1093     return(0);
1094
1095  requeue:
1096     return 1;
1097  fail:
1098     cmd->result = DID_BAD_TARGET << 16;
1099     done(cmd);
1100     return 0;
1101 }
1102
1103 /* We ignore mapping problems, as we expect everybody to respect 
1104  * valid partition tables. Waiting for complaints ;-) */
1105
1106 #ifdef CONFIG_SCSI_DC390T_TRADMAP
1107 /* 
1108  * The next function, partsize(), is copied from scsicam.c.
1109  *
1110  * This is ugly code duplication, but I didn't find another way to solve it:
1111  * We want to respect the partition table and if it fails, we apply the 
1112  * DC390 BIOS heuristic. Too bad, just calling scsicam_bios_param() doesn't do
1113  * the job, because we don't know, whether the values returned are from
1114  * the part. table or determined by setsize(). Unfortunately the setsize() 
1115  * values differ from the ones chosen by the DC390 BIOS.
1116  *
1117  * Looking forward to seeing suggestions for a better solution! KG, 98/10/14
1118  */
1119 #include <asm/unaligned.h>
1120
1121 /*
1122  * Function : static int partsize(unsigned char *buf, unsigned long 
1123  *     capacity,unsigned int *cyls, unsigned int *hds, unsigned int *secs);
1124  *
1125  * Purpose : to determine the BIOS mapping used to create the partition
1126  *      table, storing the results in *cyls, *hds, and *secs 
1127  *
1128  * Returns : -1 on failure, 0 on success.
1129  *
1130  */
1131
1132 static int partsize(unsigned char *buf, unsigned long capacity,
1133     unsigned int  *cyls, unsigned int *hds, unsigned int *secs) {
1134     struct partition *p, *largest = NULL;
1135     int i, largest_cyl;
1136     int cyl, ext_cyl, end_head, end_cyl, end_sector;
1137     unsigned int logical_end, physical_end, ext_physical_end;
1138     
1139
1140     if (*(unsigned short *) (buf+64) == 0xAA55) {
1141         for (largest_cyl = -1, p = (struct partition *) buf, 
1142             i = 0; i < 4; ++i, ++p) {
1143             if (!p->sys_ind)
1144                 continue;
1145             cyl = p->cyl + ((p->sector & 0xc0) << 2);
1146             if (cyl > largest_cyl) {
1147                 largest_cyl = cyl;
1148                 largest = p;
1149             }
1150         }
1151     }
1152
1153     if (largest) {
1154         end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2);
1155         end_head = largest->end_head;
1156         end_sector = largest->end_sector & 0x3f;
1157
1158         physical_end =  end_cyl * (end_head + 1) * end_sector +
1159             end_head * end_sector + end_sector;
1160
1161         /* This is the actual _sector_ number at the end */
1162         logical_end = get_unaligned(&largest->start_sect)
1163                         + get_unaligned(&largest->nr_sects);
1164
1165         /* This is for >1023 cylinders */
1166         ext_cyl= (logical_end-(end_head * end_sector + end_sector))
1167                                         /(end_head + 1) / end_sector;
1168         ext_physical_end = ext_cyl * (end_head + 1) * end_sector +
1169             end_head * end_sector + end_sector;
1170
1171         if ((logical_end == physical_end) ||
1172             (end_cyl==1023 && ext_physical_end==logical_end)) {
1173             *secs = end_sector;
1174             *hds = end_head + 1;
1175             *cyls = capacity / ((end_head + 1) * end_sector);
1176             return 0;
1177         }
1178     }
1179     return -1;
1180 }
1181
1182 /***********************************************************************
1183  * Function:
1184  *   DC390_bios_param
1185  *
1186  * Description:
1187  *   Return the disk geometry for the given SCSI device.
1188  *   Respect the partition table, otherwise try own heuristic
1189  *
1190  * Note:
1191  *   In contrary to other externally callable funcs (DC390_), we don't lock
1192  ***********************************************************************/
1193 static int DC390_bios_param (struct scsi_device *sdev, struct block_device *bdev,
1194                              sector_t capacity, int geom[])
1195 {
1196     int heads, sectors, cylinders;
1197     PACB pACB = (PACB) sdev->host->hostdata;
1198     int ret_code = -1;
1199     int size = capacity;
1200     unsigned char *buf;
1201
1202     if ((buf = scsi_bios_ptable(bdev)))
1203     {
1204         /* try to infer mapping from partition table */
1205         ret_code = partsize (buf, (unsigned long) size, (unsigned int *) geom + 2,
1206                              (unsigned int *) geom + 0, (unsigned int *) geom + 1);
1207         kfree (buf);
1208     }
1209     if (ret_code == -1)
1210     {
1211         heads = 64;
1212         sectors = 32;
1213         cylinders = size / (heads * sectors);
1214
1215         if ( (pACB->Gmode2 & GREATER_1G) && (cylinders > 1024) )
1216         {
1217                 heads = 255;
1218                 sectors = 63;
1219                 cylinders = size / (heads * sectors);
1220         }
1221
1222         geom[0] = heads;
1223         geom[1] = sectors;
1224         geom[2] = cylinders;
1225     }
1226
1227     return (0);
1228 }
1229 #else
1230 static int DC390_bios_param (struct scsi_device *sdev, struct block_device *bdev,
1231                              sector_t capacity, int geom[])
1232 {
1233     return scsicam_bios_param (bdev, capacity, geom);
1234 }
1235 #endif
1236
1237 static void dc390_dumpinfo (PACB pACB, PDCB pDCB, PSRB pSRB)
1238 {
1239     USHORT pstat; PDEVDECL1;
1240     if (!pDCB) pDCB = pACB->pActiveDCB;
1241     if (!pSRB && pDCB) pSRB = pDCB->pActiveSRB;
1242
1243     if (pSRB) 
1244     {
1245         printk ("DC390: SRB: Xferred %08lx, Remain %08lx, State %08x, Phase %02x\n",
1246                 pSRB->TotalXferredLen, pSRB->SGToBeXferLen, pSRB->SRBState,
1247                 pSRB->ScsiPhase);
1248         printk ("DC390: AdpaterStatus: %02x, SRB Status %02x\n", pSRB->AdaptStatus, pSRB->SRBStatus);
1249     }
1250     printk ("DC390: Status of last IRQ (DMA/SC/Int/IRQ): %08x\n", dc390_laststatus);
1251     printk ("DC390: Register dump: SCSI block:\n");
1252     printk ("DC390: XferCnt  Cmd Stat IntS IRQS FFIS Ctl1 Ctl2 Ctl3 Ctl4\n");
1253     printk ("DC390:  %06x   %02x   %02x   %02x",
1254             DC390_read8(CtcReg_Low) + (DC390_read8(CtcReg_Mid) << 8) + (DC390_read8(CtcReg_High) << 16),
1255             DC390_read8(ScsiCmd), DC390_read8(Scsi_Status), DC390_read8(Intern_State));
1256     printk ("   %02x   %02x   %02x   %02x   %02x   %02x\n",
1257             DC390_read8(INT_Status), DC390_read8(Current_Fifo), DC390_read8(CtrlReg1),
1258             DC390_read8(CtrlReg2), DC390_read8(CtrlReg3), DC390_read8(CtrlReg4));
1259     DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT);
1260     if (DC390_read8(Current_Fifo) & 0x1f)
1261       {
1262         printk ("DC390: FIFO:");
1263         while (DC390_read8(Current_Fifo) & 0x1f) printk (" %02x", DC390_read8(ScsiFifo));
1264         printk ("\n");
1265       }
1266     printk ("DC390: Register dump: DMA engine:\n");
1267     printk ("DC390: Cmd   STrCnt    SBusA    WrkBC    WrkAC Stat SBusCtrl\n");
1268     printk ("DC390:  %02x %08x %08x %08x %08x   %02x %08x\n",
1269             DC390_read8(DMA_Cmd), DC390_read32(DMA_XferCnt), DC390_read32(DMA_XferAddr),
1270             DC390_read32(DMA_Wk_ByteCntr), DC390_read32(DMA_Wk_AddrCntr),
1271             DC390_read8(DMA_Status), DC390_read32(DMA_ScsiBusCtrl));
1272     DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
1273     PDEVSET1; PCI_READ_CONFIG_WORD(PDEV, PCI_STATUS, &pstat);
1274     printk ("DC390: Register dump: PCI Status: %04x\n", pstat);
1275     printk ("DC390: In case of driver trouble read linux/Documentation/scsi/tmscsim.txt\n");
1276 }
1277
1278
1279 /***********************************************************************
1280  * Function : int DC390_abort (Scsi_Cmnd *cmd)
1281  *
1282  * Purpose : Abort an errant SCSI command
1283  *
1284  * Inputs : cmd - command to abort
1285  *
1286  * Returns : 0 on success, -1 on failure.
1287  *
1288  * Status: Buggy !
1289  ***********************************************************************/
1290
1291 static int DC390_abort (Scsi_Cmnd *cmd)
1292 {
1293     PDCB  pDCB = (PDCB) cmd->device->hostdata;
1294     PSRB  pSRB, psrb;
1295     UINT  count, i;
1296     int   status;
1297     //ULONG sbac;
1298     PACB  pACB = (PACB) cmd->device->host->hostdata;
1299
1300     printk ("DC390: Abort command (pid %li, Device %02i-%02i)\n",
1301             cmd->pid, cmd->device->id, cmd->device->lun);
1302
1303     if( !pDCB ) goto  NOT_RUN;
1304
1305     /* Added 98/07/02 KG */
1306     /*
1307     pSRB = pDCB->pActiveSRB;
1308     if (pSRB && pSRB->pcmd == cmd )
1309         goto ON_GOING;
1310      */
1311     
1312     pSRB = pDCB->pWaitingSRB;
1313     if( !pSRB )
1314         goto  ON_GOING;
1315
1316     /* Now scan Waiting queue */
1317     if( pSRB->pcmd == cmd )
1318     {
1319         pDCB->pWaitingSRB = pSRB->pNextSRB;
1320         goto  IN_WAIT;
1321     }
1322     else
1323     {
1324         psrb = pSRB;
1325         if( !(psrb->pNextSRB) )
1326             goto ON_GOING;
1327         while( psrb->pNextSRB->pcmd != cmd )
1328         {
1329             psrb = psrb->pNextSRB;
1330             if( !(psrb->pNextSRB) || psrb == pSRB)
1331                 goto ON_GOING;
1332         }
1333         pSRB = psrb->pNextSRB;
1334         psrb->pNextSRB = pSRB->pNextSRB;
1335         if( pSRB == pDCB->pWaitLast )
1336             pDCB->pWaitLast = psrb;
1337 IN_WAIT:
1338         dc390_Free_insert (pACB, pSRB);
1339         pDCB->WaitSRBCnt--;
1340         INIT_LIST_HEAD((struct list_head*)&cmd->SCp);
1341         status = SCSI_ABORT_SUCCESS;
1342         goto  ABO_X;
1343     }
1344
1345     /* SRB has already been sent ! */
1346 ON_GOING:
1347     /* abort() is too stupid for already sent commands at the moment. 
1348      * If it's called we are in trouble anyway, so let's dump some info 
1349      * into the syslog at least. (KG, 98/08/20,99/06/20) */
1350     dc390_dumpinfo (pACB, pDCB, pSRB);
1351     pSRB = pDCB->pGoingSRB;
1352     pDCB->DCBFlag |= ABORT_DEV_;
1353     /* Now for the hard part: The command is currently processed */
1354     for( count = pDCB->GoingSRBCnt, i=0; i<count; i++)
1355     {
1356         if( pSRB->pcmd != cmd )
1357             pSRB = pSRB->pNextSRB;
1358         else
1359         {
1360             if( (pACB->pActiveDCB == pDCB) && (pDCB->pActiveSRB == pSRB) )
1361             {
1362                 status = SCSI_ABORT_BUSY;
1363                 printk ("DC390: Abort current command (pid %li, SRB %p)\n",
1364                         cmd->pid, pSRB);
1365                 goto  ABO_X;
1366             }
1367             else
1368             {
1369                 status = SCSI_ABORT_SNOOZE;
1370                 goto  ABO_X;
1371             }
1372         }
1373     }
1374
1375 NOT_RUN:
1376     status = SCSI_ABORT_NOT_RUNNING;
1377
1378 ABO_X:
1379     cmd->result = DID_ABORT << 16;
1380     printk(KERN_INFO "DC390: Aborted pid %li with status %i\n", cmd->pid, status);
1381 #if 0
1382     if (cmd->pid == dc390_lastabortedpid) /* repeated failure ? */
1383         {
1384                 /* Let's do something to help the bus getting clean again */
1385                 DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1386                 DC390_write8 (ScsiCmd, DMA_COMMAND);
1387                 //DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1388                 //DC390_write8 (ScsiCmd, RESET_ATN_CMD);
1389                 DC390_write8 (ScsiCmd, NOP_CMD);
1390                 //udelay (10000);
1391                 //DC390_read8 (INT_Status);
1392                 //DC390_write8 (ScsiCmd, EN_SEL_RESEL);
1393         }
1394     sbac = DC390_read32 (DMA_ScsiBusCtrl);
1395     if (sbac & SCSI_BUSY)
1396     {   /* clear BSY, SEL and ATN */
1397         printk (KERN_WARNING "DC390: Reset SCSI device: ");
1398         //DC390_write32 (DMA_ScsiBusCtrl, (sbac | SCAM) & ~SCSI_LINES);
1399         //udelay (250);
1400         //sbac = DC390_read32 (DMA_ScsiBusCtrl);
1401         //printk ("%08lx ", sbac);
1402         //DC390_write32 (DMA_ScsiBusCtrl, sbac & ~(SCSI_LINES | SCAM));
1403         //udelay (100);
1404         //sbac = DC390_read32 (DMA_ScsiBusCtrl);
1405         //printk ("%08lx ", sbac);
1406         DC390_write8 (ScsiCmd, RST_DEVICE_CMD);
1407         udelay (250);
1408         DC390_write8 (ScsiCmd, NOP_CMD);
1409         sbac = DC390_read32 (DMA_ScsiBusCtrl);
1410         printk ("%08lx\n", sbac);
1411     }
1412 #endif
1413     dc390_lastabortedpid = cmd->pid;
1414     //do_DC390_Interrupt (pACB->IRQLevel, 0, 0);
1415 #ifndef USE_NEW_EH      
1416     if (status == SCSI_ABORT_SUCCESS) cmd->scsi_done(cmd);
1417 #endif  
1418     return( status );
1419 }
1420
1421
1422 static void dc390_ResetDevParam( PACB pACB )
1423 {
1424     PDCB   pDCB, pdcb;
1425
1426     pDCB = pACB->pLinkDCB;
1427     if (! pDCB) return;
1428     pdcb = pDCB;
1429     do
1430     {
1431         pDCB->SyncMode &= ~SYNC_NEGO_DONE;
1432         pDCB->SyncPeriod = 0;
1433         pDCB->SyncOffset = 0;
1434         pDCB->TagMask = 0;
1435         pDCB->CtrlR3 = FAST_CLK;
1436         pDCB->CtrlR4 &= NEGATE_REQACKDATA | CTRL4_RESERVED | NEGATE_REQACK;
1437         pDCB->CtrlR4 |= pACB->glitch_cfg;
1438         pDCB = pDCB->pNextDCB;
1439     }
1440     while( pdcb != pDCB );
1441     pACB->ACBFlag &= ~(RESET_DEV | RESET_DONE | RESET_DETECT);
1442
1443 }
1444
1445 #if 0
1446 /* Moves all SRBs from Going to Waiting for all DCBs */
1447 static void dc390_RecoverSRB( PACB pACB )
1448 {
1449     PDCB   pDCB, pdcb;
1450     PSRB   psrb, psrb2;
1451     UINT   cnt, i;
1452
1453     pDCB = pACB->pLinkDCB;
1454     if( !pDCB ) return;
1455     pdcb = pDCB;
1456     do
1457     {
1458         cnt = pdcb->GoingSRBCnt;
1459         psrb = pdcb->pGoingSRB;
1460         for (i=0; i<cnt; i++)
1461         {
1462             psrb2 = psrb;
1463             psrb = psrb->pNextSRB;
1464 /*          dc390_RewaitSRB( pDCB, psrb ); */
1465             if( pdcb->pWaitingSRB )
1466             {
1467                 psrb2->pNextSRB = pdcb->pWaitingSRB;
1468                 pdcb->pWaitingSRB = psrb2;
1469             }
1470             else
1471             {
1472                 pdcb->pWaitingSRB = psrb2;
1473                 pdcb->pWaitLast = psrb2;
1474                 psrb2->pNextSRB = NULL;
1475             }
1476         }
1477         pdcb->GoingSRBCnt = 0;
1478         pdcb->pGoingSRB = NULL;
1479         pdcb->TagMask = 0;
1480         pdcb = pdcb->pNextDCB;
1481     } while( pdcb != pDCB );
1482 }
1483 #endif
1484
1485 /***********************************************************************
1486  * Function : int DC390_reset (Scsi_Cmnd *cmd, ...)
1487  *
1488  * Purpose : perform a hard reset on the SCSI bus
1489  *
1490  * Inputs : cmd - command which caused the SCSI RESET
1491  *          resetFlags - how hard to try
1492  *
1493  * Returns : 0 on success.
1494  ***********************************************************************/
1495
1496 static int DC390_reset (Scsi_Cmnd *cmd)
1497 {
1498     UCHAR   bval;
1499     PACB    pACB = (PACB) cmd->device->host->hostdata;
1500
1501     printk(KERN_INFO "DC390: RESET ... ");
1502
1503     if (timer_pending (&pACB->Waiting_Timer)) del_timer (&pACB->Waiting_Timer);
1504     bval = DC390_read8 (CtrlReg1);
1505     bval |= DIS_INT_ON_SCSI_RST;
1506     DC390_write8 (CtrlReg1, bval);      /* disable IRQ on bus reset */
1507
1508     pACB->ACBFlag |= RESET_DEV;
1509     dc390_ResetSCSIBus( pACB );
1510
1511     dc390_ResetDevParam( pACB );
1512     udelay (1000);
1513     pACB->pScsiHost->last_reset = jiffies + 3*HZ/2 
1514                 + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
1515     
1516     DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1517     DC390_read8 (INT_Status);           /* Reset Pending INT */
1518
1519     dc390_DoingSRB_Done( pACB, cmd );
1520     /* dc390_RecoverSRB (pACB); */
1521     pACB->pActiveDCB = NULL;
1522
1523     pACB->ACBFlag = 0;
1524     bval = DC390_read8 (CtrlReg1);
1525     bval &= ~DIS_INT_ON_SCSI_RST;
1526     DC390_write8 (CtrlReg1, bval);      /* re-enable interrupt */
1527
1528     dc390_Waiting_process( pACB );
1529
1530     printk("done\n");
1531     return( SCSI_RESET_SUCCESS );
1532 }
1533
1534 #include "scsiiom.c"
1535
1536
1537 /***********************************************************************
1538  * Function : static void dc390_initDCB()
1539  *
1540  * Purpose :  initialize the internal structures for a DCB (to be malloced)
1541  *
1542  * Inputs : SCSI id and lun
1543  ***********************************************************************/
1544
1545 static void dc390_initDCB( PACB pACB, PDCB *ppDCB, UCHAR id, UCHAR lun )
1546 {
1547     PEEprom     prom;
1548     UCHAR       index;
1549     PDCB        pDCB, pDCB2 = 0;
1550
1551     pDCB = kmalloc (sizeof(DC390_DCB), GFP_ATOMIC);
1552     DCBDEBUG(printk (KERN_INFO "DC390: alloc mem for DCB (ID %i, LUN %i): %p\n",        \
1553                      id, lun, pDCB));
1554
1555     *ppDCB = pDCB;
1556     if (!pDCB)
1557         return;
1558
1559     if( pACB->DCBCnt == 0 )
1560     {
1561         pACB->pLinkDCB = pDCB;
1562         pACB->pDCBRunRobin = pDCB;
1563     }
1564     else
1565     {
1566         pACB->pLastDCB->pNextDCB = pDCB;
1567     }
1568    
1569     pACB->DCBCnt++;
1570    
1571     pDCB->pNextDCB = pACB->pLinkDCB;
1572     pACB->pLastDCB = pDCB;
1573
1574     pDCB->pDCBACB = pACB;
1575     pDCB->TargetID = id;
1576     pDCB->TargetLUN = lun;
1577     pDCB->pWaitingSRB = NULL;
1578     pDCB->pGoingSRB = NULL;
1579     pDCB->GoingSRBCnt = 0;
1580     pDCB->WaitSRBCnt = 0;
1581     pDCB->pActiveSRB = NULL;
1582     pDCB->TagMask = 0;
1583     pDCB->MaxCommand = 1;
1584     index = pACB->AdapterIndex;
1585     pDCB->DCBFlag = 0;
1586
1587     /* Is there a corresp. LUN==0 device ? */
1588     if (lun != 0)
1589         pDCB2 = dc390_findDCB (pACB, id, 0);
1590     prom = (PEEprom) &dc390_eepromBuf[index][id << 2];
1591     /* Some values are for all LUNs: Copy them */
1592     /* In a clean way: We would have an own structure for a SCSI-ID */
1593     if (pDCB2)
1594     {
1595       pDCB->DevMode = pDCB2->DevMode;
1596       pDCB->SyncMode = pDCB2->SyncMode;
1597       pDCB->SyncPeriod = pDCB2->SyncPeriod;
1598       pDCB->SyncOffset = pDCB2->SyncOffset;
1599       pDCB->NegoPeriod = pDCB2->NegoPeriod;
1600       
1601       pDCB->CtrlR3 = pDCB2->CtrlR3;
1602       pDCB->CtrlR4 = pDCB2->CtrlR4;
1603       pDCB->Inquiry7 = pDCB2->Inquiry7;
1604     }
1605     else
1606     {           
1607       pDCB->DevMode = prom->EE_MODE1;
1608       pDCB->SyncMode = 0;
1609       pDCB->SyncPeriod = 0;
1610       pDCB->SyncOffset = 0;
1611       pDCB->NegoPeriod = (dc390_clock_period1[prom->EE_SPEED] * 25) >> 2;
1612             
1613       pDCB->CtrlR3 = FAST_CLK;
1614       
1615       pDCB->CtrlR4 = pACB->glitch_cfg | CTRL4_RESERVED;
1616       if( dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION)
1617         pDCB->CtrlR4 |= NEGATE_REQACKDATA | NEGATE_REQACK;
1618       pDCB->Inquiry7 = 0;
1619     }
1620
1621     pACB->DCBmap[id] |= (1 << lun);
1622     dc390_updateDCB(pACB, pDCB);
1623 }
1624
1625 /***********************************************************************
1626  * Function : static void dc390_updateDCB()
1627  *
1628  * Purpose :  Set the configuration dependent DCB parameters
1629  ***********************************************************************/
1630
1631 static void dc390_updateDCB (PACB pACB, PDCB pDCB)
1632 {
1633   pDCB->SyncMode &= EN_TAG_QUEUEING | SYNC_NEGO_DONE /*| EN_ATN_STOP*/;
1634   if (pDCB->DevMode & TAG_QUEUEING_) {
1635         //if (pDCB->SyncMode & EN_TAG_QUEUEING) pDCB->MaxCommand = pACB->TagMaxNum;
1636   } else {
1637         pDCB->SyncMode &= ~EN_TAG_QUEUEING;
1638         pDCB->MaxCommand = 1;
1639   }
1640
1641   if( pDCB->DevMode & SYNC_NEGO_ )
1642         pDCB->SyncMode |= SYNC_ENABLE;
1643   else {
1644         pDCB->SyncMode &= ~(SYNC_NEGO_DONE | SYNC_ENABLE);
1645         pDCB->SyncOffset &= ~0x0f;
1646   }
1647
1648   //if (! (pDCB->DevMode & EN_DISCONNECT_)) pDCB->SyncMode &= ~EN_ATN_STOP; 
1649
1650   pDCB->CtrlR1 = pACB->pScsiHost->this_id;
1651   if( pDCB->DevMode & PARITY_CHK_ )
1652         pDCB->CtrlR1 |= PARITY_ERR_REPO;
1653 }  
1654
1655 /***********************************************************************
1656  * Function : static void dc390_initSRB()
1657  *
1658  * Purpose :  initialize the internal structures for a given SRB
1659  *
1660  * Inputs : psrb - pointer to this scsi request block structure
1661  ***********************************************************************/
1662
1663 static void __inline__ dc390_initSRB( PSRB psrb )
1664 {
1665   /* psrb->PhysSRB = virt_to_phys( psrb ); */
1666 }
1667
1668
1669 static void dc390_linkSRB( PACB pACB )
1670 {
1671     UINT   count, i;
1672
1673     count = pACB->SRBCount;
1674     for( i=0; i<count; i++)
1675     {
1676         if( i != count-1 )
1677             pACB->SRB_array[i].pNextSRB = &pACB->SRB_array[i+1];
1678         else
1679             pACB->SRB_array[i].pNextSRB = NULL;
1680         dc390_initSRB( &pACB->SRB_array[i] );
1681     }
1682 }
1683
1684
1685 /***********************************************************************
1686  * Function : static void dc390_initACB ()
1687  *
1688  * Purpose :  initialize the internal structures for a given SCSI host
1689  *
1690  * Inputs : psh - pointer to this host adapter's structure
1691  *          io_port, Irq, index: Resources and adapter index
1692  ***********************************************************************/
1693
1694 static void __init dc390_initACB (PSH psh, ULONG io_port, UCHAR Irq, UCHAR index)
1695 {
1696     PACB    pACB;
1697     UCHAR   i;
1698
1699     psh->can_queue = MAX_CMD_QUEUE;
1700     psh->cmd_per_lun = MAX_CMD_PER_LUN;
1701     psh->this_id = (int) dc390_eepromBuf[index][EE_ADAPT_SCSI_ID];
1702     psh->io_port = io_port;
1703     psh->n_io_port = 0x80;
1704     psh->irq = Irq;
1705     psh->base = io_port;
1706     psh->unique_id = io_port;
1707     psh->dma_channel = -1;
1708     psh->last_reset = jiffies;
1709         
1710     pACB = (PACB) psh->hostdata;
1711
1712     pACB->pScsiHost = psh;
1713     pACB->IOPortBase = (USHORT) io_port;
1714     pACB->IRQLevel = Irq;
1715
1716     DEBUG0(printk (KERN_INFO "DC390: Adapter index %i, ID %i, IO 0x%08x, IRQ 0x%02x\n", \
1717                    index, psh->this_id, (int)io_port, Irq));
1718    
1719     psh->max_id = 8;
1720
1721     if( psh->max_id - 1 == dc390_eepromBuf[index][EE_ADAPT_SCSI_ID] )
1722         psh->max_id--;
1723     psh->max_lun = 1;
1724     if( dc390_eepromBuf[index][EE_MODE2] & LUN_CHECK )
1725         psh->max_lun = 8;
1726
1727     pACB->pLinkDCB = NULL;
1728     pACB->pDCBRunRobin = NULL;
1729     pACB->pActiveDCB = NULL;
1730     pACB->pFreeSRB = pACB->SRB_array;
1731     pACB->SRBCount = MAX_SRB_CNT;
1732     pACB->AdapterIndex = index;
1733     pACB->status = 0;
1734     pACB->DCBCnt = 0;
1735     pACB->TagMaxNum = 2 << dc390_eepromBuf[index][EE_TAG_CMD_NUM];
1736     pACB->ACBFlag = 0;
1737     pACB->scan_devices = 1;
1738     pACB->MsgLen = 0;
1739     pACB->Ignore_IRQ = 0;
1740     pACB->Gmode2 = dc390_eepromBuf[index][EE_MODE2];
1741     dc390_linkSRB( pACB );
1742     pACB->pTmpSRB = &pACB->TmpSRB;
1743     dc390_initSRB( pACB->pTmpSRB );
1744     for(i=0; i<MAX_SCSI_ID; i++)
1745         pACB->DCBmap[i] = 0;
1746     pACB->sel_timeout = SEL_TIMEOUT;
1747     pACB->glitch_cfg = EATER_25NS;
1748     pACB->Cmds = pACB->CmdInQ = pACB->CmdOutOfSRB = 0;
1749     pACB->SelLost = pACB->SelConn = 0;
1750     init_timer (&pACB->Waiting_Timer);
1751 }
1752
1753
1754 /***********************************************************************
1755  * Function : static int dc390_initAdapter ()
1756  *
1757  * Purpose :  initialize the SCSI chip ctrl registers
1758  *
1759  * Inputs : psh - pointer to this host adapter's structure
1760  *          io_port, Irq, index: Resources
1761  *
1762  * Outputs: 0 on success, -1 on error
1763  ***********************************************************************/
1764
1765 static int __init dc390_initAdapter (PSH psh, ULONG io_port, UCHAR Irq, UCHAR index)
1766 {
1767     PACB   pACB, pACB2;
1768     UCHAR  dstate;
1769     int    i;
1770     
1771     pACB = (PACB) psh->hostdata;
1772
1773     if (request_region (io_port, psh->n_io_port, "tmscsim") == NULL) {
1774         printk(KERN_ERR "DC390: register IO ports error!\n");
1775         return( -1 );
1776     }
1777
1778     DC390_read8_ (INT_Status, io_port);         /* Reset Pending INT */
1779
1780     if( (i = request_irq(Irq, do_DC390_Interrupt, DC390_IRQ, "tmscsim", pACB) ))
1781       {
1782         printk(KERN_ERR "DC390: register IRQ error!\n");
1783         release_region (io_port, psh->n_io_port);
1784         return( -1 );
1785       }
1786
1787     if( !dc390_pACB_start )
1788       {
1789         pACB2 = NULL;
1790         dc390_pACB_start = pACB;
1791         dc390_pACB_current = pACB;
1792         pACB->pNextACB = NULL;
1793       }
1794     else
1795       {
1796         pACB2 = dc390_pACB_current;
1797         dc390_pACB_current->pNextACB = pACB;
1798         dc390_pACB_current = pACB;
1799         pACB->pNextACB = NULL;
1800       }
1801
1802     DC390_write8 (CtrlReg1, DIS_INT_ON_SCSI_RST | psh->this_id);        /* Disable SCSI bus reset interrupt */
1803
1804     if (pACB->Gmode2 & RST_SCSI_BUS)
1805     {
1806         dc390_ResetSCSIBus( pACB );
1807         udelay (1000);
1808         pACB->pScsiHost->last_reset = jiffies + HZ/2
1809                     + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
1810         /*
1811         for( i=0; i<(500 + 1000*dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY]); i++ )
1812                 udelay(1000);
1813          */
1814     }
1815     pACB->ACBFlag = 0;
1816     DC390_read8 (INT_Status);                           /* Reset Pending INT */
1817     
1818     DC390_write8 (Scsi_TimeOut, SEL_TIMEOUT);           /* 250ms selection timeout */
1819     DC390_write8 (Clk_Factor, CLK_FREQ_40MHZ);          /* Conversion factor = 0 , 40MHz clock */
1820     DC390_write8 (ScsiCmd, NOP_CMD);                    /* NOP cmd - clear command register */
1821     DC390_write8 (CtrlReg2, EN_FEATURE+EN_SCSI2_CMD);   /* Enable Feature and SCSI-2 */
1822     DC390_write8 (CtrlReg3, FAST_CLK);                  /* fast clock */
1823     DC390_write8 (CtrlReg4, pACB->glitch_cfg |                  /* glitch eater */
1824                 (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION) ? NEGATE_REQACKDATA : 0);  /* Negation */
1825     DC390_write8 (CtcReg_High, 0);                      /* Clear Transfer Count High: ID */
1826     DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1827     DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1828     DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
1829     dstate = DC390_read8 (DMA_Status);
1830     DC390_write8 (DMA_Status, dstate);  /* clear */
1831
1832     return(0);
1833 }
1834
1835
1836 /***********************************************************************
1837  * Function : static int DC390_init (struct Scsi_Host *host, ...)
1838  *
1839  * Purpose :  initialize the internal structures for a given SCSI host
1840  *
1841  * Inputs : host - pointer to this host adapter's structure
1842  *          io_port - IO ports mapped to this adapter
1843  *          irq - IRQ assigned to this adpater
1844  *          struct pci_dev - PCI access handle
1845  *          index - Adapter index
1846  *
1847  * Outputs: 0 on success, -1 on error
1848  *
1849  * Note: written in capitals, because the locking is only done here,
1850  *      not in DC390_detect, called from outside 
1851  ***********************************************************************/
1852 static int __init dc390_init (PSH psh, unsigned long io_port, u8 irq, struct pci_dev *pdev, UCHAR index)
1853 {
1854     PACB  pACB;
1855
1856     if (dc390_CheckEEpromCheckSum (PDEV, index))
1857     {
1858         int speed;
1859         dc390_adapname = "AM53C974";
1860         printk (KERN_INFO "DC390_init: No EEPROM found! Trying default settings ...\n");
1861         dc390_check_for_safe_settings ();
1862         dc390_fill_with_defaults ();
1863         dc390_EEprom_Override (index);
1864         speed = dc390_clock_speed[tmscsim[1]];
1865         printk (KERN_INFO "DC390: Used defaults: AdaptID=%i, SpeedIdx=%i (%i.%i MHz),"
1866                 " DevMode=0x%02x, AdaptMode=0x%02x, TaggedCmnds=%i (%i), DelayReset=%is\n", 
1867                 tmscsim[0], tmscsim[1], speed/10, speed%10,
1868                 (UCHAR)tmscsim[2], (UCHAR)tmscsim[3], tmscsim[4], 2 << (tmscsim[4]), tmscsim[5]);
1869     }
1870     else
1871     {
1872         dc390_check_for_safe_settings ();
1873         dc390_EEprom_Override (index);
1874     }
1875     pACB = (PACB) psh->hostdata;
1876
1877     DEBUG0(printk(KERN_INFO "DC390: pSH = %8x, Index %02i\n", (UINT) psh, index));
1878
1879     dc390_initACB( psh, io_port, irq, index );
1880         
1881     PDEVSET;
1882
1883     if( !dc390_initAdapter( psh, io_port, irq, index ) )
1884     {
1885         return (0);
1886     }
1887     else
1888     {
1889         scsi_unregister( psh );
1890         return( -1 );
1891     }
1892 }
1893
1894 static void __init dc390_set_pci_cfg (PDEVDECL)
1895 {
1896         USHORT cmd;
1897         PCI_READ_CONFIG_WORD (PDEV, PCI_COMMAND, &cmd);
1898         cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_IO;
1899         PCI_WRITE_CONFIG_WORD (PDEV, PCI_COMMAND, cmd);
1900         PCI_WRITE_CONFIG_WORD (PDEV, PCI_STATUS, (PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY));
1901 }
1902
1903 /**
1904  * dc390_slave_alloc - Called by the scsi mid layer to tell us about a new
1905  * scsi device that we need to deal with.
1906  *
1907  * @scsi_device: The new scsi device that we need to handle.
1908  */
1909 static int dc390_slave_alloc(struct scsi_device *scsi_device)
1910 {
1911         PDCB pDCB;
1912         PACB pACB = (PACB) scsi_device->host->hostdata;
1913         dc390_initDCB(pACB, &pDCB, scsi_device->id, scsi_device->lun);
1914         if (pDCB != NULL) {
1915                 scsi_device->hostdata = pDCB;
1916                 pACB->scan_devices = 1;
1917                 return 0;
1918         }
1919         return -ENOMEM;
1920 }
1921
1922 /**
1923  * dc390_slave_destroy - Called by the scsi mid layer to tell us about a
1924  * device that is going away.
1925  *
1926  * @scsi_device: The scsi device that we need to remove.
1927  */
1928 static void dc390_slave_destroy(struct scsi_device *scsi_device)
1929 {
1930         PACB pACB = (PACB) scsi_device->host->hostdata;
1931         PDCB pDCB = (PDCB) scsi_device->hostdata;
1932         pACB->scan_devices = 0;
1933         if (pDCB != NULL)
1934                 dc390_remove_dev(pACB, pDCB);
1935         else
1936                 printk(KERN_ERR"%s() called for non-existing device!\n", __FUNCTION__);
1937 }
1938
1939 static int dc390_slave_configure(struct scsi_device *scsi_device)
1940 {
1941         PACB pACB = (PACB) scsi_device->host->hostdata;
1942         pACB->scan_devices = 0;
1943         return 0;
1944 }
1945
1946 static Scsi_Host_Template driver_template = {
1947         .module                 = THIS_MODULE,
1948         .proc_name              = "tmscsim", 
1949         .proc_info              = DC390_proc_info,
1950         .name                   = DC390_BANNER " V" DC390_VERSION,
1951         .slave_alloc            = dc390_slave_alloc,
1952         .slave_configure        = dc390_slave_configure,
1953         .slave_destroy          = dc390_slave_destroy,
1954         .queuecommand           = DC390_queue_command,
1955         .eh_abort_handler       = DC390_abort,
1956         .eh_bus_reset_handler   = DC390_reset,
1957         .bios_param             = DC390_bios_param,
1958         .can_queue              = 42,
1959         .this_id                = 7,
1960         .sg_tablesize           = SG_ALL,
1961         .cmd_per_lun            = 16,
1962         .use_clustering         = DISABLE_CLUSTERING,
1963 };
1964
1965 static int __devinit dc390_init_one(struct pci_dev *dev,
1966                                     const struct pci_device_id *id)
1967 {
1968         struct Scsi_Host *scsi_host;
1969         unsigned long io_port;
1970         u8 irq;
1971         PACB  pACB;
1972         int ret = -ENOMEM;
1973
1974         if (pci_enable_device(dev))
1975                 return -ENODEV;
1976
1977         io_port = pci_resource_start(dev, 0);
1978         irq = dev->irq;
1979
1980         /* allocate scsi host information (includes out adapter) */
1981         scsi_host = scsi_host_alloc(&driver_template, sizeof(struct _ACB));
1982         if (!scsi_host)
1983                 goto nomem;
1984
1985         pACB = (PACB) scsi_host->hostdata;
1986
1987         if (dc390_init(scsi_host, io_port, irq, dev, dc390_adapterCnt)) {
1988                 ret = -EBUSY;
1989                 goto busy;
1990         }
1991
1992         pci_set_master(dev);
1993         dc390_set_pci_cfg(dev);
1994         dc390_adapterCnt++;
1995
1996         /* get the scsi mid level to scan for new devices on the bus */
1997         if (scsi_add_host(scsi_host, &dev->dev)) {
1998                 ret = -ENODEV;
1999                 goto nodev;
2000         }
2001         pci_set_drvdata(dev, scsi_host);
2002         scsi_scan_host(scsi_host);
2003
2004         return 0;
2005
2006 nodev:
2007 busy:
2008         scsi_host_put(scsi_host);
2009 nomem:
2010         pci_disable_device(dev);
2011         return ret;
2012 }
2013
2014 /**
2015  * dc390_remove_one - Called to remove a single instance of the adapter.
2016  *
2017  * @dev: The PCI device to remove.
2018  */
2019 static void __devexit dc390_remove_one(struct pci_dev *dev)
2020 {
2021         struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
2022
2023         scsi_remove_host(scsi_host);
2024         DC390_release(scsi_host);
2025         pci_disable_device(dev);
2026         scsi_host_put(scsi_host);
2027         pci_set_drvdata(dev, NULL);
2028 }
2029
2030 /********************************************************************
2031  * Function: DC390_proc_info(char* buffer, char **start,
2032  *                           off_t offset, int length, int hostno, int inout)
2033  *
2034  * Purpose: return SCSI Adapter/Device Info
2035  *
2036  * Input: buffer: Pointer to a buffer where to write info
2037  *        start :
2038  *        offset:
2039  *        hostno: Host adapter index
2040  *        inout : Read (=0) or set(!=0) info
2041  *
2042  * Output: buffer: contains info
2043  *         length; length of info in buffer
2044  *
2045  * return value: length
2046  *
2047  ********************************************************************/
2048
2049 #undef SPRINTF
2050 #define SPRINTF(args...) pos += sprintf(pos, ## args)
2051
2052 #define YESNO(YN)               \
2053  if (YN) SPRINTF(" Yes ");      \
2054  else SPRINTF(" No  ")
2055
2056
2057 static int DC390_proc_info (struct Scsi_Host *shpnt, char *buffer, char **start,
2058                             off_t offset, int length, int inout)
2059 {
2060   int dev, spd, spd1;
2061   char *pos = buffer;
2062   PACB pACB;
2063   PDCB pDCB;
2064
2065   pACB = dc390_pACB_start;
2066
2067   while(pACB != (PACB)-1)
2068      {
2069         if (shpnt == pACB->pScsiHost)
2070                 break;
2071         pACB = pACB->pNextACB;
2072      }
2073
2074   if (pACB == (PACB)-1) return(-ESRCH);
2075
2076   if(inout) /* Has data been written to the file ? */
2077       return -ENOSYS;
2078    
2079   SPRINTF("Tekram DC390/AM53C974 PCI SCSI Host Adapter, ");
2080   SPRINTF("Driver Version %s\n", DC390_VERSION);
2081
2082   SPRINTF("SCSI Host Nr %i, ", shpnt->host_no);
2083   SPRINTF("%s Adapter Nr %i\n", dc390_adapname, pACB->AdapterIndex);
2084   SPRINTF("IOPortBase 0x%04x, ", pACB->IOPortBase);
2085   SPRINTF("IRQ %02i\n", pACB->IRQLevel);
2086
2087   SPRINTF("MaxID %i, MaxLUN %i, ", shpnt->max_id, shpnt->max_lun);
2088   SPRINTF("AdapterID %i, SelTimeout %i ms, DelayReset %i s\n", 
2089           shpnt->this_id, (pACB->sel_timeout*164)/100,
2090           dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY]);
2091
2092   SPRINTF("TagMaxNum %i, Status 0x%02x, ACBFlag 0x%02x, GlitchEater %i ns\n",
2093           pACB->TagMaxNum, pACB->status, pACB->ACBFlag, GLITCH_TO_NS(pACB->glitch_cfg)*12);
2094
2095   SPRINTF("Statistics: Cmnds %li, Cmnds not sent directly %i, Out of SRB conds %i\n",
2096           pACB->Cmds, pACB->CmdInQ, pACB->CmdOutOfSRB);
2097   SPRINTF("            Lost arbitrations %i, Sel. connected %i, Connected: %s\n", 
2098           pACB->SelLost, pACB->SelConn, pACB->Connected? "Yes": "No");
2099    
2100   SPRINTF("Nr of DCBs: %i\n", pACB->DCBCnt);
2101   SPRINTF("Map of attached LUNs: %02x %02x %02x %02x %02x %02x %02x %02x\n",
2102           pACB->DCBmap[0], pACB->DCBmap[1], pACB->DCBmap[2], pACB->DCBmap[3], 
2103           pACB->DCBmap[4], pACB->DCBmap[5], pACB->DCBmap[6], pACB->DCBmap[7]);
2104
2105   SPRINTF("Idx ID LUN Prty Sync DsCn SndS TagQ NegoPeriod SyncSpeed SyncOffs MaxCmd\n");
2106
2107   pDCB = pACB->pLinkDCB;
2108   for (dev = 0; dev < pACB->DCBCnt; dev++)
2109      {
2110       SPRINTF("%02i  %02i  %02i ", dev, pDCB->TargetID, pDCB->TargetLUN);
2111       YESNO(pDCB->DevMode & PARITY_CHK_);
2112       YESNO(pDCB->SyncMode & SYNC_NEGO_DONE);
2113       YESNO(pDCB->DevMode & EN_DISCONNECT_);
2114       YESNO(pDCB->DevMode & SEND_START_);
2115       YESNO(pDCB->SyncMode & EN_TAG_QUEUEING);
2116       if (pDCB->SyncOffset & 0x0f)
2117       {
2118          int sp = pDCB->SyncPeriod; if (! (pDCB->CtrlR3 & FAST_SCSI)) sp++;
2119          SPRINTF("  %03i ns ", (pDCB->NegoPeriod) << 2);
2120          spd = 40/(sp); spd1 = 40%(sp);
2121          spd1 = (spd1 * 10 + sp/2) / (sp);
2122          SPRINTF("   %2i.%1i M      %02i", spd, spd1, (pDCB->SyncOffset & 0x0f));
2123       }
2124       else SPRINTF(" (%03i ns)                 ", (pDCB->NegoPeriod) << 2);
2125       /* Add more info ...*/
2126       SPRINTF ("      %02i\n", pDCB->MaxCommand);
2127       pDCB = pDCB->pNextDCB;
2128      }
2129     if (timer_pending(&pACB->Waiting_Timer)) SPRINTF ("Waiting queue timer running\n");
2130     else SPRINTF ("\n");
2131     pDCB = pACB->pLinkDCB;
2132         
2133     for (dev = 0; dev < pACB->DCBCnt; dev++)
2134     {
2135         PSRB pSRB;
2136         if (pDCB->WaitSRBCnt) 
2137                     SPRINTF ("DCB (%02i-%i): Waiting: %i:", pDCB->TargetID, pDCB->TargetLUN,
2138                              pDCB->WaitSRBCnt);
2139         for (pSRB = pDCB->pWaitingSRB; pSRB; pSRB = pSRB->pNextSRB)
2140                 SPRINTF(" %li", pSRB->pcmd->pid);
2141         if (pDCB->GoingSRBCnt) 
2142                     SPRINTF ("\nDCB (%02i-%i): Going  : %i:", pDCB->TargetID, pDCB->TargetLUN,
2143                              pDCB->GoingSRBCnt);
2144         for (pSRB = pDCB->pGoingSRB; pSRB; pSRB = pSRB->pNextSRB)
2145 #if 0 //def DC390_DEBUGTRACE
2146                 SPRINTF(" %s\n  ", pSRB->debugtrace);
2147 #else
2148                 SPRINTF(" %li", pSRB->pcmd->pid);
2149 #endif
2150         if (pDCB->WaitSRBCnt || pDCB->GoingSRBCnt) SPRINTF ("\n");
2151         pDCB = pDCB->pNextDCB;
2152     }
2153         
2154 #ifdef DC390_DEBUGDCB
2155     SPRINTF ("DCB list for ACB %p:\n", pACB);
2156     pDCB = pACB->pLinkDCB;
2157     SPRINTF ("%p", pDCB);
2158     for (dev = 0; dev < pACB->DCBCnt; dev++, pDCB=pDCB->pNextDCB)
2159         SPRINTF ("->%p", pDCB->pNextDCB);
2160     SPRINTF("\n");
2161 #endif
2162   
2163   *start = buffer + offset;
2164
2165   if (pos - buffer < offset)
2166     return 0;
2167   else if (pos - buffer - offset < length)
2168     return pos - buffer - offset;
2169   else
2170     return length;
2171 }
2172
2173 #undef YESNO
2174 #undef SPRINTF
2175
2176 /***********************************************************************
2177  * Function : static int dc390_shutdown (struct Scsi_Host *host)
2178  *
2179  * Purpose : does a clean (we hope) shutdown of the SCSI chip.
2180  *           Use prior to dumping core, unloading the driver, etc.
2181  *
2182  * Returns : 0 on success
2183  ***********************************************************************/
2184 static int dc390_shutdown (struct Scsi_Host *host)
2185 {
2186     UCHAR    bval;
2187     PACB pACB = (PACB) host->hostdata;
2188    
2189 /*  pACB->soft_reset(host); */
2190
2191     printk(KERN_INFO "DC390: shutdown\n");
2192
2193     pACB->ACBFlag = RESET_DEV;
2194     bval = DC390_read8 (CtrlReg1);
2195     bval |= DIS_INT_ON_SCSI_RST;
2196     DC390_write8 (CtrlReg1, bval);      /* disable interrupt */
2197     if (pACB->Gmode2 & RST_SCSI_BUS)
2198                 dc390_ResetSCSIBus (pACB);
2199
2200     if (timer_pending (&pACB->Waiting_Timer)) del_timer (&pACB->Waiting_Timer);
2201     return( 0 );
2202 }
2203
2204 static void dc390_freeDCBs (struct Scsi_Host *host)
2205 {
2206     PDCB pDCB, nDCB;
2207     PACB pACB = (PACB) host->hostdata;
2208     
2209     pDCB = pACB->pLinkDCB;
2210     if (!pDCB) return;
2211     do
2212     {
2213         nDCB = pDCB->pNextDCB;
2214         DCBDEBUG(printk (KERN_INFO "DC390: Free DCB (ID %i, LUN %i): %p\n",\
2215                          pDCB->TargetID, pDCB->TargetLUN, pDCB));
2216         //kfree (pDCB);
2217         dc390_remove_dev (pACB, pDCB);
2218         pDCB = nDCB;
2219     } while (pDCB && pACB->pLinkDCB);
2220
2221 }
2222
2223 static int DC390_release (struct Scsi_Host *host)
2224 {
2225     DC390_IFLAGS;
2226     PACB pACB = (PACB) host->hostdata;
2227
2228     DC390_LOCK_IO(host);
2229
2230     /* TO DO: We should check for outstanding commands first. */
2231     dc390_shutdown (host);
2232
2233     if (host->irq != SCSI_IRQ_NONE)
2234     {
2235         DEBUG0(printk(KERN_INFO "DC390: Free IRQ %i\n",host->irq));
2236         free_irq (host->irq, pACB);
2237     }
2238
2239     release_region(host->io_port,host->n_io_port);
2240     dc390_freeDCBs (host);
2241     DC390_UNLOCK_IO(host);
2242     return( 1 );
2243 }
2244
2245 static struct pci_driver dc390_driver = {
2246         .name           = "tmscsim",
2247         .id_table       = tmscsim_pci_tbl,
2248         .probe          = dc390_init_one,
2249         .remove         = __devexit_p(dc390_remove_one),
2250 };
2251
2252 static int __init dc390_module_init(void)
2253 {
2254         return pci_module_init(&dc390_driver);
2255 }
2256
2257 static void __exit dc390_module_exit(void)
2258 {
2259         pci_unregister_driver(&dc390_driver);
2260 }
2261
2262 module_init(dc390_module_init);
2263 module_exit(dc390_module_exit);