MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / scsi / tmscsim.c
blob8a1ed11fb21c8fd68f9a8ea782dc0462d87c5907
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 ***********************************************************************/
182 /* Uncomment SA_INTERRUPT, if the driver refuses to share its IRQ with other devices */
183 #define DC390_IRQ SA_SHIRQ /* | SA_INTERRUPT */
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
193 //#define NOP do{}while(0)
194 #define C_NOP
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
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>
243 #include <scsi/scsi.h>
244 #include <scsi/scsi_cmnd.h>
245 #include <scsi/scsi_device.h>
246 #include <scsi/scsi_host.h>
247 #include <scsi/scsicam.h>
250 #define DC390_BANNER "Tekram DC390/AM53C974"
251 #define DC390_VERSION "2.1d 2004-05-27"
253 #define PCI_DEVICE_ID_AMD53C974 PCI_DEVICE_ID_AMD_SCSI
256 static struct pci_device_id tmscsim_pci_tbl[] = {
258 .vendor = PCI_VENDOR_ID_AMD,
259 .device = PCI_DEVICE_ID_AMD53C974,
260 .subvendor = PCI_ANY_ID,
261 .subdevice = PCI_ANY_ID,
263 { } /* Terminating entry */
265 MODULE_DEVICE_TABLE(pci, tmscsim_pci_tbl);
267 #include "tmscsim.h"
269 static u8 dc390_StartSCSI( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB );
270 static void dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
271 static void dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
272 static void dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
273 static void dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
274 static void dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
275 static void dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
276 static void dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
277 static void dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
278 static void dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
279 static void dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
280 static void dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
281 static void dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
282 static void dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
283 static void dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
285 static void dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB );
286 static void dc390_Disconnect( struct dc390_acb* pACB );
287 static void dc390_Reselect( struct dc390_acb* pACB );
288 static void dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB );
289 static void dc390_DoingSRB_Done( struct dc390_acb* pACB, struct scsi_cmnd * cmd);
290 static void dc390_ScsiRstDetect( struct dc390_acb* pACB );
291 static void dc390_ResetSCSIBus( struct dc390_acb* pACB );
292 static void dc390_EnableMsgOut_Abort(struct dc390_acb*, struct dc390_srb*);
293 static irqreturn_t do_DC390_Interrupt( int, void *, struct pt_regs *);
295 static int dc390_initAdapter(struct Scsi_Host *psh, unsigned long io_port, u8 Irq, u8 index );
296 static void dc390_updateDCB (struct dc390_acb* pACB, struct dc390_dcb* pDCB);
298 static struct dc390_acb* dc390_pACB_start= NULL;
299 static struct dc390_acb* dc390_pACB_current = NULL;
300 static u32 dc390_laststatus = 0;
301 static u8 dc390_adapterCnt = 0;
303 /* Startup values, to be overriden on the commandline */
304 static int tmscsim[] = {-2, -2, -2, -2, -2, -2};
305 static int tmscsim_paramnum = ARRAY_SIZE(tmscsim);
307 module_param_array(tmscsim, int, tmscsim_paramnum, 0);
308 MODULE_PARM_DESC(tmscsim, "Host SCSI ID, Speed (0=10MHz), Device Flags, Adapter Flags, Max Tags (log2(tags)-1), DelayReset (s)");
309 MODULE_AUTHOR("C.L. Huang / Kurt Garloff");
310 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram DC390 and other AMD53C974A based PCI SCSI adapters");
311 MODULE_LICENSE("GPL");
312 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
314 static void *dc390_phase0[]={
315 dc390_DataOut_0,
316 dc390_DataIn_0,
317 dc390_Command_0,
318 dc390_Status_0,
319 dc390_Nop_0,
320 dc390_Nop_0,
321 dc390_MsgOut_0,
322 dc390_MsgIn_0,
323 dc390_Nop_1
326 static void *dc390_phase1[]={
327 dc390_DataOutPhase,
328 dc390_DataInPhase,
329 dc390_CommandPhase,
330 dc390_StatusPhase,
331 dc390_Nop_0,
332 dc390_Nop_0,
333 dc390_MsgOutPhase,
334 dc390_MsgInPhase,
335 dc390_Nop_1
338 #ifdef DC390_DEBUG1
339 static char* dc390_p0_str[] = {
340 "dc390_DataOut_0",
341 "dc390_DataIn_0",
342 "dc390_Command_0",
343 "dc390_Status_0",
344 "dc390_Nop_0",
345 "dc390_Nop_0",
346 "dc390_MsgOut_0",
347 "dc390_MsgIn_0",
348 "dc390_Nop_1"
351 static char* dc390_p1_str[] = {
352 "dc390_DataOutPhase",
353 "dc390_DataInPhase",
354 "dc390_CommandPhase",
355 "dc390_StatusPhase",
356 "dc390_Nop_0",
357 "dc390_Nop_0",
358 "dc390_MsgOutPhase",
359 "dc390_MsgInPhase",
360 "dc390_Nop_1"
362 #endif
364 /* Devices erroneously pretending to be able to do TagQ */
365 static u8 dc390_baddevname1[2][28] ={
366 "SEAGATE ST3390N 9546",
367 "HP C3323-300 4269"};
368 #define BADDEVCNT 2
370 static char* dc390_adapname = "DC390";
371 static u8 dc390_eepromBuf[MAX_ADAPTER_NUM][EE_LEN];
372 static u8 dc390_clock_period1[] = {4, 5, 6, 7, 8, 10, 13, 20};
373 static u8 dc390_clock_speed[] = {100,80,67,57,50, 40, 31, 20};
375 /***********************************************************************
376 * Functions for access to DC390 EEPROM
377 * and some to emulate it
379 **********************************************************************/
382 static void __devinit dc390_EnDisableCE(u8 mode, struct pci_dev *pdev, u8 *regval)
384 u8 bval;
386 bval = 0;
387 if(mode == ENABLE_CE)
388 *regval = 0xc0;
389 else
390 *regval = 0x80;
391 pci_write_config_byte(pdev, *regval, bval);
392 if(mode == DISABLE_CE)
393 pci_write_config_byte(pdev, *regval, bval);
394 udelay(160);
398 /* Override EEprom values with explicitly set values */
399 static void __devinit dc390_EEprom_Override (u8 index)
401 u8 *ptr = (u8 *) dc390_eepromBuf[index];
402 u8 id;
404 /* Adapter Settings */
405 if (tmscsim[0] != -2)
406 ptr[EE_ADAPT_SCSI_ID] = (u8)tmscsim[0]; /* Adapter ID */
407 if (tmscsim[3] != -2)
408 ptr[EE_MODE2] = (u8)tmscsim[3];
409 if (tmscsim[5] != -2)
410 ptr[EE_DELAY] = tmscsim[5]; /* Reset delay */
411 if (tmscsim[4] != -2)
412 ptr[EE_TAG_CMD_NUM] = (u8)tmscsim[4]; /* Tagged Cmds */
414 /* Device Settings */
415 for (id = 0; id < MAX_SCSI_ID; id++)
417 if (tmscsim[2] != -2)
418 ptr[id<<2] = (u8)tmscsim[2]; /* EE_MODE1 */
419 if (tmscsim[1] != -2)
420 ptr[(id<<2) + 1] = (u8)tmscsim[1]; /* EE_Speed */
424 /* Handle "-1" case */
425 static void __devinit dc390_check_for_safe_settings (void)
427 if (tmscsim[0] == -1 || tmscsim[0] > 15) /* modules-2.0.0 passes -1 as string */
429 tmscsim[0] = 7; tmscsim[1] = 4;
430 tmscsim[2] = 0x09; tmscsim[3] = 0x0f;
431 tmscsim[4] = 2; tmscsim[5] = 10;
432 printk (KERN_INFO "DC390: Using safe settings.\n");
437 static int __initdata tmscsim_def[] = {7, 0 /* 10MHz */,
438 PARITY_CHK_ | SEND_START_ | EN_DISCONNECT_
439 | SYNC_NEGO_ | TAG_QUEUEING_,
440 MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION
441 /* | NO_SEEK */
442 # ifdef CONFIG_SCSI_MULTI_LUN
443 | LUN_CHECK
444 # endif
445 , 3 /* 16 Tags per LUN */, 1 /* s delay after Reset */ };
447 /* Copy defaults over set values where missing */
448 static void __devinit dc390_fill_with_defaults (void)
450 int i;
451 PARSEDEBUG(printk(KERN_INFO "DC390: setup %08x %08x %08x %08x %08x %08x\n", tmscsim[0],\
452 tmscsim[1], tmscsim[2], tmscsim[3], tmscsim[4], tmscsim[5]));
453 for (i = 0; i < 6; i++)
455 if (tmscsim[i] < 0 || tmscsim[i] > 255)
456 tmscsim[i] = tmscsim_def[i];
458 /* Sanity checks */
459 if (tmscsim[0] > 7) tmscsim[0] = 7;
460 if (tmscsim[1] > 7) tmscsim[1] = 4;
461 if (tmscsim[4] > 5) tmscsim[4] = 4;
462 if (tmscsim[5] > 180) tmscsim[5] = 180;
465 #ifndef MODULE
466 /* Override defaults on cmdline:
467 * tmscsim: AdaptID, MaxSpeed (Index), DevMode (Bitmapped), AdaptMode (Bitmapped)
469 static int __init dc390_setup (char *str)
471 int ints[8];
472 int i, im;
473 (void)get_options (str, ARRAY_SIZE(ints), ints);
474 im = ints[0];
475 if (im > 6)
477 printk (KERN_NOTICE "DC390: ignore extra params!\n");
478 im = 6;
480 for (i = 0; i < im; i++)
481 tmscsim[i] = ints[i+1];
482 /* dc390_checkparams (); */
483 return 1;
486 __setup("tmscsim=", dc390_setup);
487 #endif
489 static void __devinit dc390_EEpromOutDI(struct pci_dev *pdev, u8 *regval, u8 Carry)
491 u8 bval;
493 bval = 0;
494 if(Carry)
496 bval = 0x40;
497 *regval = 0x80;
498 pci_write_config_byte(pdev, *regval, bval);
500 udelay(160);
501 bval |= 0x80;
502 pci_write_config_byte(pdev, *regval, bval);
503 udelay(160);
504 bval = 0;
505 pci_write_config_byte(pdev, *regval, bval);
506 udelay(160);
510 static u8 __devinit dc390_EEpromInDO(struct pci_dev *pdev)
512 u8 bval;
514 pci_write_config_byte(pdev, 0x80, 0x80);
515 udelay(160);
516 pci_write_config_byte(pdev, 0x80, 0x40);
517 udelay(160);
518 pci_read_config_byte(pdev, 0x00, &bval);
519 if(bval == 0x22)
520 return(1);
521 else
522 return(0);
526 static u16 __devinit dc390_EEpromGetData1(struct pci_dev *pdev)
528 u8 i;
529 u8 carryFlag;
530 u16 wval;
532 wval = 0;
533 for(i=0; i<16; i++)
535 wval <<= 1;
536 carryFlag = dc390_EEpromInDO(pdev);
537 wval |= carryFlag;
539 return(wval);
543 static void __devinit dc390_Prepare(struct pci_dev *pdev, u8 *regval, u8 EEpromCmd)
545 u8 i,j;
546 u8 carryFlag;
548 carryFlag = 1;
549 j = 0x80;
550 for(i=0; i<9; i++)
552 dc390_EEpromOutDI(pdev, regval, carryFlag);
553 carryFlag = (EEpromCmd & j) ? 1 : 0;
554 j >>= 1;
559 static void __devinit dc390_ReadEEprom(struct pci_dev *pdev, u16 *ptr)
561 u8 regval,cmd;
562 u8 i;
564 cmd = EEPROM_READ;
565 for(i=0; i<0x40; i++)
567 dc390_EnDisableCE(ENABLE_CE, pdev, &regval);
568 dc390_Prepare(pdev, &regval, cmd++);
569 *ptr++ = dc390_EEpromGetData1(pdev);
570 dc390_EnDisableCE(DISABLE_CE, pdev, &regval);
575 static void __devinit dc390_interpret_delay (u8 index)
577 char interpd [] = {1,3,5,10,16,30,60,120};
578 dc390_eepromBuf[index][EE_DELAY] = interpd [dc390_eepromBuf[index][EE_DELAY]];
581 static u8 __devinit dc390_CheckEEpromCheckSum(struct pci_dev *pdev, u8 index)
583 u8 i;
584 char EEbuf[128];
585 u16 wval, *ptr = (u16 *)EEbuf;
587 dc390_ReadEEprom(pdev, ptr);
588 memcpy (dc390_eepromBuf[index], EEbuf, EE_ADAPT_SCSI_ID);
589 memcpy (&dc390_eepromBuf[index][EE_ADAPT_SCSI_ID],
590 &EEbuf[REAL_EE_ADAPT_SCSI_ID], EE_LEN - EE_ADAPT_SCSI_ID);
591 dc390_interpret_delay (index);
593 wval = 0;
594 for(i=0; i<0x40; i++, ptr++)
595 wval += *ptr;
596 return (wval == 0x1234 ? 0 : 1);
600 /***********************************************************************
601 * Functions for the management of the internal structures
602 * (DCBs, SRBs, Queueing)
604 **********************************************************************/
605 static struct dc390_dcb __inline__ *dc390_findDCB ( struct dc390_acb* pACB, u8 id, u8 lun)
607 struct dc390_dcb* pDCB = pACB->pLinkDCB; if (!pDCB) return NULL;
608 while (pDCB->TargetID != id || pDCB->TargetLUN != lun)
610 pDCB = pDCB->pNextDCB;
611 if (pDCB == pACB->pLinkDCB)
612 return NULL;
614 DCBDEBUG1( printk (KERN_DEBUG "DCB %p (%02x,%02x) found.\n", \
615 pDCB, pDCB->TargetID, pDCB->TargetLUN));
616 return pDCB;
619 /* Queueing philosphy:
620 * There are a couple of lists:
621 * - Query: Contains the Scsi Commands not yet turned into SRBs (per ACB)
622 * (Note: For new EH, it is unnecessary!)
623 * - Waiting: Contains a list of SRBs not yet sent (per DCB)
624 * - Free: List of free SRB slots
626 * If there are no waiting commands for the DCB, the new one is sent to the bus
627 * otherwise the oldest one is taken from the Waiting list and the new one is
628 * queued to the Waiting List
630 * Lists are managed using two pointers and eventually a counter
633 /* Return next free SRB */
634 static __inline__ struct dc390_srb* dc390_Free_get ( struct dc390_acb* pACB )
636 struct dc390_srb* pSRB;
638 pSRB = pACB->pFreeSRB;
639 DEBUG0(printk ("DC390: Get Free SRB %p\n", pSRB));
640 if( pSRB )
642 pACB->pFreeSRB = pSRB->pNextSRB;
643 pSRB->pNextSRB = NULL;
646 return( pSRB );
649 /* Insert SRB oin top of free list */
650 static __inline__ void dc390_Free_insert (struct dc390_acb* pACB, struct dc390_srb* pSRB)
652 DEBUG0(printk ("DC390: Free SRB %p\n", pSRB));
653 pSRB->pNextSRB = pACB->pFreeSRB;
654 pACB->pFreeSRB = pSRB;
658 /* Inserts a SRB to the top of the Waiting list */
659 static __inline__ void dc390_Waiting_insert ( struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
661 DEBUG0(printk ("DC390: Insert pSRB %p cmd %li to Waiting\n", pSRB, pSRB->pcmd->pid));
662 pSRB->pNextSRB = pDCB->pWaitingSRB;
663 if (!pDCB->pWaitingSRB)
664 pDCB->pWaitLast = pSRB;
665 pDCB->pWaitingSRB = pSRB;
666 pDCB->WaitSRBCnt++;
670 /* Queue SRB to waiting list */
671 static __inline__ void dc390_Waiting_append ( struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
673 DEBUG0(printk ("DC390: Append pSRB %p cmd %li to Waiting\n", pSRB, pSRB->pcmd->pid));
674 if( pDCB->pWaitingSRB )
675 pDCB->pWaitLast->pNextSRB = pSRB;
676 else
677 pDCB->pWaitingSRB = pSRB;
679 pDCB->pWaitLast = pSRB;
680 pSRB->pNextSRB = NULL;
681 pDCB->WaitSRBCnt++;
682 pDCB->pDCBACB->CmdInQ++;
685 static __inline__ void dc390_Going_append (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
687 pDCB->GoingSRBCnt++;
688 DEBUG0(printk("DC390: Append SRB %p to Going\n", pSRB));
689 /* Append to the list of Going commands */
690 if( pDCB->pGoingSRB )
691 pDCB->pGoingLast->pNextSRB = pSRB;
692 else
693 pDCB->pGoingSRB = pSRB;
695 pDCB->pGoingLast = pSRB;
696 /* No next one in sent list */
697 pSRB->pNextSRB = NULL;
700 static __inline__ void dc390_Going_remove (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
702 DEBUG0(printk("DC390: Remove SRB %p from Going\n", pSRB));
703 if (pSRB == pDCB->pGoingSRB)
704 pDCB->pGoingSRB = pSRB->pNextSRB;
705 else
707 struct dc390_srb* psrb = pDCB->pGoingSRB;
708 while (psrb && psrb->pNextSRB != pSRB)
709 psrb = psrb->pNextSRB;
710 if (!psrb)
711 { printk (KERN_ERR "DC390: Remove non-ex. SRB %p from Going!\n", pSRB); return; }
712 psrb->pNextSRB = pSRB->pNextSRB;
713 if (pSRB == pDCB->pGoingLast)
714 pDCB->pGoingLast = psrb;
716 pDCB->GoingSRBCnt--;
719 /* Moves SRB from Going list to the top of Waiting list */
720 static void dc390_Going_to_Waiting ( struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
722 DEBUG0(printk(KERN_INFO "DC390: Going_to_Waiting (SRB %p) pid = %li\n", pSRB, pSRB->pcmd->pid));
723 /* Remove SRB from Going */
724 dc390_Going_remove (pDCB, pSRB);
725 /* Insert on top of Waiting */
726 dc390_Waiting_insert (pDCB, pSRB);
727 /* Tag Mask must be freed elsewhere ! (KG, 99/06/18) */
730 /* Moves first SRB from Waiting list to Going list */
731 static __inline__ void dc390_Waiting_to_Going ( struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
733 /* Remove from waiting list */
734 DEBUG0(printk("DC390: Remove SRB %p from head of Waiting\n", pSRB));
735 pDCB->pWaitingSRB = pSRB->pNextSRB;
736 if( !pDCB->pWaitingSRB ) pDCB->pWaitLast = NULL;
737 pDCB->WaitSRBCnt--;
738 dc390_Going_append (pDCB, pSRB);
741 static void DC390_waiting_timed_out (unsigned long ptr);
742 /* Sets the timer to wake us up */
743 static void dc390_waiting_timer (struct dc390_acb* pACB, unsigned long to)
745 if (timer_pending (&pACB->Waiting_Timer)) return;
746 init_timer (&pACB->Waiting_Timer);
747 pACB->Waiting_Timer.function = DC390_waiting_timed_out;
748 pACB->Waiting_Timer.data = (unsigned long)pACB;
749 if (time_before (jiffies + to, pACB->pScsiHost->last_reset))
750 pACB->Waiting_Timer.expires = pACB->pScsiHost->last_reset + 1;
751 else
752 pACB->Waiting_Timer.expires = jiffies + to + 1;
753 add_timer (&pACB->Waiting_Timer);
757 /* Send the next command from the waiting list to the bus */
758 static void dc390_Waiting_process ( struct dc390_acb* pACB )
760 struct dc390_dcb *ptr, *ptr1;
761 struct dc390_srb *pSRB;
763 if( (pACB->pActiveDCB) || (pACB->ACBFlag & (RESET_DETECT+RESET_DONE+RESET_DEV) ) )
764 return;
765 if (timer_pending (&pACB->Waiting_Timer)) del_timer (&pACB->Waiting_Timer);
766 ptr = pACB->pDCBRunRobin;
767 if( !ptr )
769 ptr = pACB->pLinkDCB;
770 pACB->pDCBRunRobin = ptr;
772 ptr1 = ptr;
773 if (!ptr1) return;
776 pACB->pDCBRunRobin = ptr1->pNextDCB;
777 if( !( pSRB = ptr1->pWaitingSRB ) ||
778 ( ptr1->MaxCommand <= ptr1->GoingSRBCnt ))
779 ptr1 = ptr1->pNextDCB;
780 else
782 /* Try to send to the bus */
783 if( !dc390_StartSCSI(pACB, ptr1, pSRB) )
784 dc390_Waiting_to_Going (ptr1, pSRB);
785 else
786 dc390_waiting_timer (pACB, HZ/5);
787 break;
789 } while (ptr1 != ptr);
790 return;
793 /* Wake up waiting queue */
794 static void DC390_waiting_timed_out (unsigned long ptr)
796 struct dc390_acb* pACB = (struct dc390_acb*)ptr;
797 unsigned long iflags;
798 DEBUG0(printk ("DC390: Debug: Waiting queue woken up by timer!\n"));
799 spin_lock_irqsave(pACB->pScsiHost->host_lock, iflags);
800 dc390_Waiting_process (pACB);
801 spin_unlock_irqrestore(pACB->pScsiHost->host_lock, iflags);
804 /***********************************************************************
805 * Function: static void dc390_SendSRB (struct dc390_acb* pACB, struct dc390_srb* pSRB)
807 * Purpose: Send SCSI Request Block (pSRB) to adapter (pACB)
809 ***********************************************************************/
811 static void dc390_SendSRB( struct dc390_acb* pACB, struct dc390_srb* pSRB )
813 struct dc390_dcb* pDCB;
815 pDCB = pSRB->pSRBDCB;
816 if( (pDCB->MaxCommand <= pDCB->GoingSRBCnt) || (pACB->pActiveDCB) ||
817 (pACB->ACBFlag & (RESET_DETECT+RESET_DONE+RESET_DEV)) )
819 dc390_Waiting_append (pDCB, pSRB);
820 dc390_Waiting_process (pACB);
821 return;
824 #if 0
825 if( pDCB->pWaitingSRB )
827 dc390_Waiting_append (pDCB, pSRB);
828 /* pSRB = GetWaitingSRB(pDCB); */ /* non-existent */
829 pSRB = pDCB->pWaitingSRB;
830 /* Remove from waiting list */
831 pDCB->pWaitingSRB = pSRB->pNextSRB;
832 pSRB->pNextSRB = NULL;
833 if (!pDCB->pWaitingSRB) pDCB->pWaitLast = NULL;
835 #endif
837 if (!dc390_StartSCSI(pACB, pDCB, pSRB))
838 dc390_Going_append (pDCB, pSRB);
839 else {
840 dc390_Waiting_insert (pDCB, pSRB);
841 dc390_waiting_timer (pACB, HZ/5);
845 static struct scatterlist* dc390_sg_build_single(struct scatterlist *sg, void *addr, unsigned int length)
847 memset(sg, 0, sizeof(struct scatterlist));
848 sg->page = virt_to_page(addr);
849 sg->length = length;
850 sg->offset = (unsigned long)addr & ~PAGE_MASK;
851 return sg;
854 /* Create pci mapping */
855 static int dc390_pci_map (struct dc390_srb* pSRB)
857 int error = 0;
858 struct scsi_cmnd *pcmd = pSRB->pcmd;
859 struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
860 dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp));
862 /* Map sense buffer */
863 if (pSRB->SRBFlag & AUTO_REQSENSE) {
864 pSRB->pSegmentList = dc390_sg_build_single(&pSRB->Segmentx, pcmd->sense_buffer, sizeof(pcmd->sense_buffer));
865 pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, 1,
866 DMA_FROM_DEVICE);
867 cmdp->saved_dma_handle = sg_dma_address(pSRB->pSegmentList);
869 /* TODO: error handling */
870 if (pSRB->SGcount != 1)
871 error = 1;
872 DEBUG1(printk("%s(): Mapped sense buffer %p at %x\n", __FUNCTION__, pcmd->sense_buffer, cmdp->saved_dma_handle));
873 /* Map SG list */
874 } else if (pcmd->use_sg) {
875 pSRB->pSegmentList = (struct scatterlist *) pcmd->request_buffer;
876 pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, pcmd->use_sg,
877 pcmd->sc_data_direction);
878 /* TODO: error handling */
879 if (!pSRB->SGcount)
880 error = 1;
881 DEBUG1(printk("%s(): Mapped SG %p with %d (%d) elements\n",\
882 __FUNCTION__, pcmd->request_buffer, pSRB->SGcount, pcmd->use_sg));
883 /* Map single segment */
884 } else if (pcmd->request_buffer && pcmd->request_bufflen) {
885 pSRB->pSegmentList = dc390_sg_build_single(&pSRB->Segmentx, pcmd->request_buffer, pcmd->request_bufflen);
886 pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, 1,
887 pcmd->sc_data_direction);
888 cmdp->saved_dma_handle = sg_dma_address(pSRB->pSegmentList);
890 /* TODO: error handling */
891 if (pSRB->SGcount != 1)
892 error = 1;
893 DEBUG1(printk("%s(): Mapped request buffer %p at %x\n", __FUNCTION__, pcmd->request_buffer, cmdp->saved_dma_handle));
894 /* No mapping !? */
895 } else
896 pSRB->SGcount = 0;
898 return error;
901 /* Remove pci mapping */
902 static void dc390_pci_unmap (struct dc390_srb* pSRB)
904 struct scsi_cmnd *pcmd = pSRB->pcmd;
905 struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
906 DEBUG1(dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp)));
908 if (pSRB->SRBFlag) {
909 pci_unmap_sg(pdev, &pSRB->Segmentx, 1, DMA_FROM_DEVICE);
910 DEBUG1(printk("%s(): Unmapped sense buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
911 } else if (pcmd->use_sg) {
912 pci_unmap_sg(pdev, pcmd->request_buffer, pcmd->use_sg, pcmd->sc_data_direction);
913 DEBUG1(printk("%s(): Unmapped SG at %p with %d elements\n", __FUNCTION__, pcmd->request_buffer, pcmd->use_sg));
914 } else if (pcmd->request_buffer && pcmd->request_bufflen) {
915 pci_unmap_sg(pdev, &pSRB->Segmentx, 1, pcmd->sc_data_direction);
916 DEBUG1(printk("%s(): Unmapped request buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
921 /***********************************************************************
922 * Function: static void dc390_BuildSRB (Scsi_Cmd *pcmd, struct dc390_dcb* pDCB,
923 * struct dc390_srb* pSRB)
925 * Purpose: Prepare SRB for being sent to Device DCB w/ command *pcmd
927 ***********************************************************************/
929 static void dc390_BuildSRB (struct scsi_cmnd *pcmd, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
931 pSRB->pSRBDCB = pDCB;
932 pSRB->pcmd = pcmd;
934 pSRB->SGIndex = 0;
935 pSRB->AdaptStatus = 0;
936 pSRB->TargetStatus = 0;
937 pSRB->MsgCnt = 0;
938 if( pDCB->DevType != TYPE_TAPE )
939 pSRB->RetryCnt = 1;
940 else
941 pSRB->RetryCnt = 0;
942 pSRB->SRBStatus = 0;
943 pSRB->SRBFlag = 0;
944 pSRB->SRBState = 0;
945 pSRB->TotalXferredLen = 0;
946 pSRB->SGBusAddr = 0;
947 pSRB->SGToBeXferLen = 0;
948 pSRB->ScsiPhase = 0;
949 pSRB->EndMessage = 0;
950 pSRB->TagNumber = 255;
951 /* KG: deferred PCI mapping to dc390_StartSCSI */
954 /***********************************************************************
955 * Function : static int DC390_queue_command (struct scsi_cmnd *cmd,
956 * void (*done)(struct scsi_cmnd *))
958 * Purpose : enqueues a SCSI command
960 * Inputs : cmd - SCSI command, done - callback function called on
961 * completion, with a pointer to the command descriptor.
963 * Returns : (depending on kernel version)
964 * 2.0.x: always return 0
965 * 2.1.x: old model: (use_new_eh_code == 0): like 2.0.x
966 * TO BE DONE:
967 * new model: return 0 if successful, or must not be re-queued
968 * return 1 if command cannot be queued (queue full)
969 * command will be inserted in midlevel queue then ...
971 ***********************************************************************/
973 static int DC390_queue_command(struct scsi_cmnd *cmd,
974 void (* done)(struct scsi_cmnd *))
976 struct dc390_dcb* pDCB = (struct dc390_dcb*) cmd->device->hostdata;
977 struct dc390_srb* pSRB;
978 struct dc390_acb* pACB = (struct dc390_acb*) cmd->device->host->hostdata;
980 pACB->Cmds++;
981 cmd->scsi_done = done;
982 cmd->result = 0;
984 pSRB = dc390_Free_get(pACB);
985 if (!pSRB)
986 goto requeue;
988 dc390_BuildSRB(cmd, pDCB, pSRB);
989 if (pDCB->pWaitingSRB) {
990 dc390_Waiting_append(pDCB, pSRB);
991 dc390_Waiting_process(pACB);
992 } else
993 dc390_SendSRB(pACB, pSRB);
995 DEBUG1(printk (KERN_DEBUG " ... command (pid %li) queued successfully.\n", cmd->pid));
996 return(0);
998 requeue:
999 return 1;
1002 static void dc390_dumpinfo (struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
1004 struct pci_dev *pdev;
1005 u16 pstat;
1007 if (!pDCB) pDCB = pACB->pActiveDCB;
1008 if (!pSRB && pDCB) pSRB = pDCB->pActiveSRB;
1010 if (pSRB)
1012 printk ("DC390: SRB: Xferred %08lx, Remain %08lx, State %08x, Phase %02x\n",
1013 pSRB->TotalXferredLen, pSRB->SGToBeXferLen, pSRB->SRBState,
1014 pSRB->ScsiPhase);
1015 printk ("DC390: AdpaterStatus: %02x, SRB Status %02x\n", pSRB->AdaptStatus, pSRB->SRBStatus);
1017 printk ("DC390: Status of last IRQ (DMA/SC/Int/IRQ): %08x\n", dc390_laststatus);
1018 printk ("DC390: Register dump: SCSI block:\n");
1019 printk ("DC390: XferCnt Cmd Stat IntS IRQS FFIS Ctl1 Ctl2 Ctl3 Ctl4\n");
1020 printk ("DC390: %06x %02x %02x %02x",
1021 DC390_read8(CtcReg_Low) + (DC390_read8(CtcReg_Mid) << 8) + (DC390_read8(CtcReg_High) << 16),
1022 DC390_read8(ScsiCmd), DC390_read8(Scsi_Status), DC390_read8(Intern_State));
1023 printk (" %02x %02x %02x %02x %02x %02x\n",
1024 DC390_read8(INT_Status), DC390_read8(Current_Fifo), DC390_read8(CtrlReg1),
1025 DC390_read8(CtrlReg2), DC390_read8(CtrlReg3), DC390_read8(CtrlReg4));
1026 DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT);
1027 if (DC390_read8(Current_Fifo) & 0x1f)
1029 printk ("DC390: FIFO:");
1030 while (DC390_read8(Current_Fifo) & 0x1f) printk (" %02x", DC390_read8(ScsiFifo));
1031 printk ("\n");
1033 printk ("DC390: Register dump: DMA engine:\n");
1034 printk ("DC390: Cmd STrCnt SBusA WrkBC WrkAC Stat SBusCtrl\n");
1035 printk ("DC390: %02x %08x %08x %08x %08x %02x %08x\n",
1036 DC390_read8(DMA_Cmd), DC390_read32(DMA_XferCnt), DC390_read32(DMA_XferAddr),
1037 DC390_read32(DMA_Wk_ByteCntr), DC390_read32(DMA_Wk_AddrCntr),
1038 DC390_read8(DMA_Status), DC390_read32(DMA_ScsiBusCtrl));
1039 DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
1041 pdev = pACB->pdev;
1042 pci_read_config_word(pdev, PCI_STATUS, &pstat);
1043 printk ("DC390: Register dump: PCI Status: %04x\n", pstat);
1044 printk ("DC390: In case of driver trouble read Documentation/scsi/tmscsim.txt\n");
1048 static int DC390_abort(struct scsi_cmnd *cmd)
1050 struct dc390_acb *pACB = (struct dc390_acb*) cmd->device->host->hostdata;
1051 struct dc390_dcb *pDCB = (struct dc390_dcb*) cmd->device->hostdata;
1052 struct dc390_srb *pSRB, *psrb;
1054 printk("DC390: Abort command (pid %li, Device %02i-%02i)\n",
1055 cmd->pid, cmd->device->id, cmd->device->lun);
1057 pSRB = pDCB->pWaitingSRB;
1058 if (!pSRB)
1059 goto on_going;
1061 /* Now scan Waiting queue */
1062 if (pSRB->pcmd != cmd) {
1063 psrb = pSRB;
1064 if (!(psrb->pNextSRB))
1065 goto on_going;
1067 while (psrb->pNextSRB->pcmd != cmd) {
1068 psrb = psrb->pNextSRB;
1069 if (!(psrb->pNextSRB) || psrb == pSRB)
1070 goto on_going;
1073 pSRB = psrb->pNextSRB;
1074 psrb->pNextSRB = pSRB->pNextSRB;
1075 if (pSRB == pDCB->pWaitLast)
1076 pDCB->pWaitLast = psrb;
1077 } else
1078 pDCB->pWaitingSRB = pSRB->pNextSRB;
1080 dc390_Free_insert(pACB, pSRB);
1081 pDCB->WaitSRBCnt--;
1082 INIT_LIST_HEAD((struct list_head*)&cmd->SCp);
1084 return SUCCESS;
1086 on_going:
1087 /* abort() is too stupid for already sent commands at the moment.
1088 * If it's called we are in trouble anyway, so let's dump some info
1089 * into the syslog at least. (KG, 98/08/20,99/06/20) */
1090 dc390_dumpinfo(pACB, pDCB, pSRB);
1092 pDCB->DCBFlag |= ABORT_DEV_;
1093 printk(KERN_INFO "DC390: Aborted pid %li\n", cmd->pid);
1095 return FAILED;
1099 static void dc390_ResetDevParam( struct dc390_acb* pACB )
1101 struct dc390_dcb *pDCB, *pdcb;
1103 pDCB = pACB->pLinkDCB;
1104 if (! pDCB) return;
1105 pdcb = pDCB;
1108 pDCB->SyncMode &= ~SYNC_NEGO_DONE;
1109 pDCB->SyncPeriod = 0;
1110 pDCB->SyncOffset = 0;
1111 pDCB->TagMask = 0;
1112 pDCB->CtrlR3 = FAST_CLK;
1113 pDCB->CtrlR4 &= NEGATE_REQACKDATA | CTRL4_RESERVED | NEGATE_REQACK;
1114 pDCB->CtrlR4 |= pACB->glitch_cfg;
1115 pDCB = pDCB->pNextDCB;
1117 while( pdcb != pDCB );
1118 pACB->ACBFlag &= ~(RESET_DEV | RESET_DONE | RESET_DETECT);
1122 static int DC390_bus_reset (struct scsi_cmnd *cmd)
1124 struct dc390_acb* pACB = (struct dc390_acb*) cmd->device->host->hostdata;
1125 u8 bval;
1127 del_timer (&pACB->Waiting_Timer);
1129 bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
1130 DC390_write8(CtrlReg1, bval); /* disable IRQ on bus reset */
1132 pACB->ACBFlag |= RESET_DEV;
1133 dc390_ResetSCSIBus(pACB);
1135 dc390_ResetDevParam(pACB);
1136 udelay(1000);
1137 pACB->pScsiHost->last_reset = jiffies + 3*HZ/2
1138 + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
1140 DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
1141 DC390_read8(INT_Status); /* Reset Pending INT */
1143 dc390_DoingSRB_Done(pACB, cmd);
1145 pACB->pActiveDCB = NULL;
1146 pACB->ACBFlag = 0;
1148 bval = DC390_read8(CtrlReg1) & ~DIS_INT_ON_SCSI_RST;
1149 DC390_write8(CtrlReg1, bval); /* re-enable interrupt */
1151 dc390_Waiting_process(pACB);
1153 return SUCCESS;
1156 #include "scsiiom.c"
1158 /***********************************************************************
1159 * Function : static void dc390_updateDCB()
1161 * Purpose : Set the configuration dependent DCB parameters
1162 ***********************************************************************/
1164 static void dc390_updateDCB (struct dc390_acb* pACB, struct dc390_dcb* pDCB)
1166 pDCB->SyncMode &= EN_TAG_QUEUEING | SYNC_NEGO_DONE /*| EN_ATN_STOP*/;
1167 if (pDCB->DevMode & TAG_QUEUEING_) {
1168 //if (pDCB->SyncMode & EN_TAG_QUEUEING) pDCB->MaxCommand = pACB->TagMaxNum;
1169 } else {
1170 pDCB->SyncMode &= ~EN_TAG_QUEUEING;
1171 pDCB->MaxCommand = 1;
1174 if( pDCB->DevMode & SYNC_NEGO_ )
1175 pDCB->SyncMode |= SYNC_ENABLE;
1176 else {
1177 pDCB->SyncMode &= ~(SYNC_NEGO_DONE | SYNC_ENABLE);
1178 pDCB->SyncOffset &= ~0x0f;
1181 //if (! (pDCB->DevMode & EN_DISCONNECT_)) pDCB->SyncMode &= ~EN_ATN_STOP;
1183 pDCB->CtrlR1 = pACB->pScsiHost->this_id;
1184 if( pDCB->DevMode & PARITY_CHK_ )
1185 pDCB->CtrlR1 |= PARITY_ERR_REPO;
1188 /***********************************************************************
1189 * Function : static void dc390_initSRB()
1191 * Purpose : initialize the internal structures for a given SRB
1193 * Inputs : psrb - pointer to this scsi request block structure
1194 ***********************************************************************/
1196 static void __inline__ dc390_initSRB( struct dc390_srb* psrb )
1198 /* psrb->PhysSRB = virt_to_phys( psrb ); */
1202 static void dc390_linkSRB( struct dc390_acb* pACB )
1204 u32 count, i;
1206 count = pACB->SRBCount;
1207 for( i=0; i<count; i++)
1209 if( i != count-1 )
1210 pACB->SRB_array[i].pNextSRB = &pACB->SRB_array[i+1];
1211 else
1212 pACB->SRB_array[i].pNextSRB = NULL;
1213 dc390_initSRB( &pACB->SRB_array[i] );
1218 /***********************************************************************
1219 * Function : static void dc390_initACB ()
1221 * Purpose : initialize the internal structures for a given SCSI host
1223 * Inputs : psh - pointer to this host adapter's structure
1224 * io_port, Irq, index: Resources and adapter index
1225 ***********************************************************************/
1227 static void __devinit dc390_initACB (struct Scsi_Host *psh, unsigned long io_port, u8 Irq, u8 index)
1229 struct dc390_acb* pACB;
1231 psh->can_queue = MAX_CMD_QUEUE;
1232 psh->cmd_per_lun = MAX_CMD_PER_LUN;
1233 psh->this_id = (int) dc390_eepromBuf[index][EE_ADAPT_SCSI_ID];
1234 psh->io_port = io_port;
1235 psh->n_io_port = 0x80;
1236 psh->irq = Irq;
1237 psh->base = io_port;
1238 psh->unique_id = io_port;
1239 psh->dma_channel = -1;
1240 psh->last_reset = jiffies;
1242 pACB = (struct dc390_acb*) psh->hostdata;
1244 pACB->pScsiHost = psh;
1245 pACB->IOPortBase = (u16) io_port;
1246 pACB->IRQLevel = Irq;
1248 DEBUG0(printk (KERN_INFO "DC390: Adapter index %i, ID %i, IO 0x%08x, IRQ 0x%02x\n", \
1249 index, psh->this_id, (int)io_port, Irq));
1251 psh->max_id = 8;
1253 if( psh->max_id - 1 == dc390_eepromBuf[index][EE_ADAPT_SCSI_ID] )
1254 psh->max_id--;
1255 psh->max_lun = 1;
1256 if( dc390_eepromBuf[index][EE_MODE2] & LUN_CHECK )
1257 psh->max_lun = 8;
1259 pACB->pLinkDCB = NULL;
1260 pACB->pDCBRunRobin = NULL;
1261 pACB->pActiveDCB = NULL;
1262 pACB->pFreeSRB = pACB->SRB_array;
1263 pACB->SRBCount = MAX_SRB_CNT;
1264 pACB->AdapterIndex = index;
1265 pACB->status = 0;
1266 pACB->DCBCnt = 0;
1267 pACB->TagMaxNum = 2 << dc390_eepromBuf[index][EE_TAG_CMD_NUM];
1268 pACB->ACBFlag = 0;
1269 pACB->scan_devices = 1;
1270 pACB->MsgLen = 0;
1271 pACB->Ignore_IRQ = 0;
1272 pACB->Gmode2 = dc390_eepromBuf[index][EE_MODE2];
1273 dc390_linkSRB( pACB );
1274 pACB->pTmpSRB = &pACB->TmpSRB;
1275 dc390_initSRB( pACB->pTmpSRB );
1276 pACB->sel_timeout = SEL_TIMEOUT;
1277 pACB->glitch_cfg = EATER_25NS;
1278 pACB->Cmds = pACB->CmdInQ = pACB->CmdOutOfSRB = 0;
1279 pACB->SelLost = pACB->SelConn = 0;
1280 init_timer (&pACB->Waiting_Timer);
1284 /***********************************************************************
1285 * Function : static int dc390_initAdapter ()
1287 * Purpose : initialize the SCSI chip ctrl registers
1289 * Inputs : psh - pointer to this host adapter's structure
1290 * io_port, Irq, index: Resources
1292 * Outputs: 0 on success, -1 on error
1293 ***********************************************************************/
1295 static int __devinit dc390_initAdapter (struct Scsi_Host *psh, unsigned long io_port, u8 Irq, u8 index)
1297 struct dc390_acb *pACB, *pACB2;
1298 u8 dstate;
1299 int i;
1301 pACB = (struct dc390_acb*) psh->hostdata;
1303 if (request_region (io_port, psh->n_io_port, "tmscsim") == NULL) {
1304 printk(KERN_ERR "DC390: register IO ports error!\n");
1305 return( -1 );
1308 DC390_read8_ (INT_Status, io_port); /* Reset Pending INT */
1310 if( (i = request_irq(Irq, do_DC390_Interrupt, DC390_IRQ, "tmscsim", pACB) ))
1312 printk(KERN_ERR "DC390: register IRQ error!\n");
1313 release_region (io_port, psh->n_io_port);
1314 return( -1 );
1317 if( !dc390_pACB_start )
1319 pACB2 = NULL;
1320 dc390_pACB_start = pACB;
1321 dc390_pACB_current = pACB;
1322 pACB->pNextACB = NULL;
1324 else
1326 pACB2 = dc390_pACB_current;
1327 dc390_pACB_current->pNextACB = pACB;
1328 dc390_pACB_current = pACB;
1329 pACB->pNextACB = NULL;
1332 DC390_write8 (CtrlReg1, DIS_INT_ON_SCSI_RST | psh->this_id); /* Disable SCSI bus reset interrupt */
1334 if (pACB->Gmode2 & RST_SCSI_BUS)
1336 dc390_ResetSCSIBus( pACB );
1337 udelay (1000);
1338 pACB->pScsiHost->last_reset = jiffies + HZ/2
1339 + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
1341 for( i=0; i<(500 + 1000*dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY]); i++ )
1342 udelay(1000);
1345 pACB->ACBFlag = 0;
1346 DC390_read8 (INT_Status); /* Reset Pending INT */
1348 DC390_write8 (Scsi_TimeOut, SEL_TIMEOUT); /* 250ms selection timeout */
1349 DC390_write8 (Clk_Factor, CLK_FREQ_40MHZ); /* Conversion factor = 0 , 40MHz clock */
1350 DC390_write8 (ScsiCmd, NOP_CMD); /* NOP cmd - clear command register */
1351 DC390_write8 (CtrlReg2, EN_FEATURE+EN_SCSI2_CMD); /* Enable Feature and SCSI-2 */
1352 DC390_write8 (CtrlReg3, FAST_CLK); /* fast clock */
1353 DC390_write8 (CtrlReg4, pACB->glitch_cfg | /* glitch eater */
1354 (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION) ? NEGATE_REQACKDATA : 0); /* Negation */
1355 DC390_write8 (CtcReg_High, 0); /* Clear Transfer Count High: ID */
1356 DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1357 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1358 DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
1359 dstate = DC390_read8 (DMA_Status);
1360 DC390_write8 (DMA_Status, dstate); /* clear */
1362 return(0);
1366 static void __devinit dc390_set_pci_cfg (struct pci_dev *pdev)
1368 u16 cmd;
1370 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1371 cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_IO;
1372 pci_write_config_word(pdev, PCI_COMMAND, cmd);
1373 pci_write_config_word(pdev, PCI_STATUS, (PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY));
1377 * dc390_slave_alloc - Called by the scsi mid layer to tell us about a new
1378 * scsi device that we need to deal with.
1380 * @scsi_device: The new scsi device that we need to handle.
1382 static int dc390_slave_alloc(struct scsi_device *scsi_device)
1384 struct dc390_acb *pACB = (struct dc390_acb*) scsi_device->host->hostdata;
1385 struct dc390_dcb *pDCB, *pDCB2 = NULL;
1386 uint id = scsi_device->id;
1387 uint lun = scsi_device->lun;
1389 pDCB = kmalloc(sizeof(struct dc390_dcb), GFP_KERNEL);
1390 if (!pDCB)
1391 return -ENOMEM;
1392 memset(pDCB, 0, sizeof(struct dc390_dcb));
1394 if (!pACB->DCBCnt++) {
1395 pACB->pLinkDCB = pDCB;
1396 pACB->pDCBRunRobin = pDCB;
1397 } else {
1398 pACB->pLastDCB->pNextDCB = pDCB;
1401 pDCB->pNextDCB = pACB->pLinkDCB;
1402 pACB->pLastDCB = pDCB;
1404 pDCB->pDCBACB = pACB;
1405 pDCB->TargetID = id;
1406 pDCB->TargetLUN = lun;
1407 pDCB->MaxCommand = 1;
1410 * Some values are for all LUNs: Copy them
1411 * In a clean way: We would have an own structure for a SCSI-ID
1413 if (lun && (pDCB2 = dc390_findDCB(pACB, id, 0))) {
1414 pDCB->DevMode = pDCB2->DevMode;
1415 pDCB->SyncMode = pDCB2->SyncMode;
1416 pDCB->SyncPeriod = pDCB2->SyncPeriod;
1417 pDCB->SyncOffset = pDCB2->SyncOffset;
1418 pDCB->NegoPeriod = pDCB2->NegoPeriod;
1420 pDCB->CtrlR3 = pDCB2->CtrlR3;
1421 pDCB->CtrlR4 = pDCB2->CtrlR4;
1422 pDCB->Inquiry7 = pDCB2->Inquiry7;
1423 } else {
1424 u8 index = pACB->AdapterIndex;
1425 PEEprom prom = (PEEprom) &dc390_eepromBuf[index][id << 2];
1427 pDCB->DevMode = prom->EE_MODE1;
1428 pDCB->NegoPeriod =
1429 (dc390_clock_period1[prom->EE_SPEED] * 25) >> 2;
1430 pDCB->CtrlR3 = FAST_CLK;
1431 pDCB->CtrlR4 = pACB->glitch_cfg | CTRL4_RESERVED;
1432 if (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION)
1433 pDCB->CtrlR4 |= NEGATE_REQACKDATA | NEGATE_REQACK;
1436 dc390_updateDCB(pACB, pDCB);
1438 pACB->scan_devices = 1;
1439 scsi_device->hostdata = pDCB;
1440 return 0;
1444 * dc390_slave_destroy - Called by the scsi mid layer to tell us about a
1445 * device that is going away.
1447 * @scsi_device: The scsi device that we need to remove.
1449 static void dc390_slave_destroy(struct scsi_device *scsi_device)
1451 struct dc390_acb* pACB = (struct dc390_acb*) scsi_device->host->hostdata;
1452 struct dc390_dcb* pDCB = (struct dc390_dcb*) scsi_device->hostdata;
1453 struct dc390_dcb* pPrevDCB = pACB->pLinkDCB;
1455 pACB->scan_devices = 0;
1457 BUG_ON(pDCB->GoingSRBCnt > 1);
1459 if (pDCB == pACB->pLinkDCB) {
1460 if (pACB->pLastDCB == pDCB) {
1461 pDCB->pNextDCB = NULL;
1462 pACB->pLastDCB = NULL;
1464 pACB->pLinkDCB = pDCB->pNextDCB;
1465 } else {
1466 while (pPrevDCB->pNextDCB != pDCB)
1467 pPrevDCB = pPrevDCB->pNextDCB;
1468 pPrevDCB->pNextDCB = pDCB->pNextDCB;
1469 if (pDCB == pACB->pLastDCB)
1470 pACB->pLastDCB = pPrevDCB;
1473 if (pDCB == pACB->pActiveDCB)
1474 pACB->pActiveDCB = NULL;
1475 if (pDCB == pACB->pLinkDCB)
1476 pACB->pLinkDCB = pDCB->pNextDCB;
1477 if (pDCB == pACB->pDCBRunRobin)
1478 pACB->pDCBRunRobin = pDCB->pNextDCB;
1479 kfree(pDCB);
1481 pACB->DCBCnt--;
1484 static int dc390_slave_configure(struct scsi_device *scsi_device)
1486 struct dc390_acb* pACB = (struct dc390_acb*) scsi_device->host->hostdata;
1487 pACB->scan_devices = 0;
1488 return 0;
1491 static struct scsi_host_template driver_template = {
1492 .module = THIS_MODULE,
1493 .proc_name = "tmscsim",
1494 .name = DC390_BANNER " V" DC390_VERSION,
1495 .slave_alloc = dc390_slave_alloc,
1496 .slave_configure = dc390_slave_configure,
1497 .slave_destroy = dc390_slave_destroy,
1498 .queuecommand = DC390_queue_command,
1499 .eh_abort_handler = DC390_abort,
1500 .eh_bus_reset_handler = DC390_bus_reset,
1501 .can_queue = 42,
1502 .this_id = 7,
1503 .sg_tablesize = SG_ALL,
1504 .cmd_per_lun = 16,
1505 .use_clustering = DISABLE_CLUSTERING,
1508 static int __devinit dc390_init_one(struct pci_dev *dev,
1509 const struct pci_device_id *id)
1511 struct Scsi_Host *scsi_host;
1512 unsigned long io_port;
1513 u8 irq;
1514 struct dc390_acb* pACB;
1515 int ret = -ENOMEM;
1517 if (pci_enable_device(dev))
1518 return -ENODEV;
1520 io_port = pci_resource_start(dev, 0);
1521 irq = dev->irq;
1523 /* allocate scsi host information (includes out adapter) */
1524 scsi_host = scsi_host_alloc(&driver_template, sizeof(struct dc390_acb));
1525 if (!scsi_host)
1526 goto nomem;
1528 pACB = (struct dc390_acb*) scsi_host->hostdata;
1530 if (dc390_CheckEEpromCheckSum (dev, dc390_adapterCnt)) {
1531 int speed;
1532 dc390_adapname = "AM53C974";
1533 printk(KERN_INFO "DC390_init: No EEPROM found! Trying default settings ...\n");
1534 dc390_check_for_safe_settings();
1535 dc390_fill_with_defaults();
1536 dc390_EEprom_Override(dc390_adapterCnt);
1537 speed = dc390_clock_speed[tmscsim[1]];
1538 printk(KERN_INFO "DC390: Used defaults: AdaptID=%i, SpeedIdx=%i (%i.%i MHz),"
1539 " DevMode=0x%02x, AdaptMode=0x%02x, TaggedCmnds=%i (%i), DelayReset=%is\n",
1540 tmscsim[0], tmscsim[1], speed/10, speed%10,
1541 (u8)tmscsim[2], (u8)tmscsim[3], tmscsim[4], 2 << (tmscsim[4]), tmscsim[5]);
1542 } else {
1543 dc390_check_for_safe_settings();
1544 dc390_EEprom_Override(dc390_adapterCnt);
1547 DEBUG0(printk(KERN_INFO "DC390: pSH = %8x, Index %02i\n", (u32) scsi_host, dc390_adapterCnt));
1549 dc390_initACB(scsi_host, io_port, irq, dc390_adapterCnt);
1551 pACB->pdev = dev;
1553 if (dc390_initAdapter(scsi_host, io_port, irq, dc390_adapterCnt)) {
1554 scsi_unregister(scsi_host);
1555 ret = -EBUSY;
1556 goto busy;
1559 pci_set_master(dev);
1560 dc390_set_pci_cfg(dev);
1561 dc390_adapterCnt++;
1563 /* get the scsi mid level to scan for new devices on the bus */
1564 if (scsi_add_host(scsi_host, &dev->dev)) {
1565 ret = -ENODEV;
1566 goto nodev;
1568 pci_set_drvdata(dev, scsi_host);
1569 scsi_scan_host(scsi_host);
1571 return 0;
1573 nodev:
1574 busy:
1575 scsi_host_put(scsi_host);
1576 nomem:
1577 pci_disable_device(dev);
1578 return ret;
1582 * dc390_remove_one - Called to remove a single instance of the adapter.
1584 * @dev: The PCI device to remove.
1586 static void __devexit dc390_remove_one(struct pci_dev *dev)
1588 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
1589 unsigned long iflags;
1590 struct dc390_acb* pACB = (struct dc390_acb*) scsi_host->hostdata;
1591 u8 bval;
1593 scsi_remove_host(scsi_host);
1595 spin_lock_irqsave(scsi_host->host_lock, iflags);
1596 pACB->ACBFlag = RESET_DEV;
1597 bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
1598 DC390_write8 (CtrlReg1, bval); /* disable interrupt */
1599 if (pACB->Gmode2 & RST_SCSI_BUS)
1600 dc390_ResetSCSIBus(pACB);
1601 spin_unlock_irqrestore(scsi_host->host_lock, iflags);
1603 del_timer_sync(&pACB->Waiting_Timer);
1605 free_irq(scsi_host->irq, pACB);
1606 release_region(scsi_host->io_port, scsi_host->n_io_port);
1608 pci_disable_device(dev);
1609 scsi_host_put(scsi_host);
1610 pci_set_drvdata(dev, NULL);
1613 static struct pci_driver dc390_driver = {
1614 .name = "tmscsim",
1615 .id_table = tmscsim_pci_tbl,
1616 .probe = dc390_init_one,
1617 .remove = __devexit_p(dc390_remove_one),
1620 static int __init dc390_module_init(void)
1622 return pci_module_init(&dc390_driver);
1625 static void __exit dc390_module_exit(void)
1627 pci_unregister_driver(&dc390_driver);
1630 module_init(dc390_module_init);
1631 module_exit(dc390_module_exit);