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