Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/kyle/parisc-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / tmscsim.c
blob27866b0adfebd4901dbb1a38a4ebc752ecfd3d70
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 /* DEBUG options */
183 //#define DC390_DEBUG0
184 //#define DC390_DEBUG1
185 //#define DC390_DCBDEBUG
186 //#define DC390_PARSEDEBUG
187 //#define DC390_REMOVABLEDEBUG
188 //#define DC390_LOCKDEBUG
190 //#define NOP do{}while(0)
191 #define C_NOP
193 /* Debug definitions */
194 #ifdef DC390_DEBUG0
195 # define DEBUG0(x) x
196 #else
197 # define DEBUG0(x) C_NOP
198 #endif
199 #ifdef DC390_DEBUG1
200 # define DEBUG1(x) x
201 #else
202 # define DEBUG1(x) C_NOP
203 #endif
204 #ifdef DC390_DCBDEBUG
205 # define DCBDEBUG(x) x
206 #else
207 # define DCBDEBUG(x) C_NOP
208 #endif
209 #ifdef DC390_PARSEDEBUG
210 # define PARSEDEBUG(x) x
211 #else
212 # define PARSEDEBUG(x) C_NOP
213 #endif
214 #ifdef DC390_REMOVABLEDEBUG
215 # define REMOVABLEDEBUG(x) x
216 #else
217 # define REMOVABLEDEBUG(x) C_NOP
218 #endif
219 #define DCBDEBUG1(x) C_NOP
221 #include <linux/module.h>
222 #include <linux/delay.h>
223 #include <linux/signal.h>
224 #include <linux/errno.h>
225 #include <linux/kernel.h>
226 #include <linux/ioport.h>
227 #include <linux/pci.h>
228 #include <linux/proc_fs.h>
229 #include <linux/string.h>
230 #include <linux/mm.h>
231 #include <linux/blkdev.h>
232 #include <linux/timer.h>
233 #include <linux/interrupt.h>
234 #include <linux/init.h>
235 #include <linux/spinlock.h>
236 #include <linux/slab.h>
237 #include <asm/io.h>
239 #include <scsi/scsi.h>
240 #include <scsi/scsi_cmnd.h>
241 #include <scsi/scsi_device.h>
242 #include <scsi/scsi_host.h>
243 #include <scsi/scsicam.h>
244 #include <scsi/scsi_tcq.h>
247 #define DC390_BANNER "Tekram DC390/AM53C974"
248 #define DC390_VERSION "2.1d 2004-05-27"
250 #define PCI_DEVICE_ID_AMD53C974 PCI_DEVICE_ID_AMD_SCSI
252 #include "tmscsim.h"
255 static void dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
256 static void dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
257 static void dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
258 static void dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
259 static void dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
260 static void dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
261 static void dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
262 static void dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
263 static void dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
264 static void dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
265 static void dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
266 static void dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
267 static void dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
268 static void dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
270 static void dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB );
271 static void dc390_Disconnect( struct dc390_acb* pACB );
272 static void dc390_Reselect( struct dc390_acb* pACB );
273 static void dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB );
274 static void dc390_ScsiRstDetect( struct dc390_acb* pACB );
275 static void dc390_EnableMsgOut_Abort(struct dc390_acb*, struct dc390_srb*);
276 static void dc390_dumpinfo(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB);
277 static void dc390_ResetDevParam(struct dc390_acb* pACB);
279 static u32 dc390_laststatus = 0;
280 static u8 dc390_adapterCnt = 0;
282 static int disable_clustering;
283 module_param(disable_clustering, int, S_IRUGO);
284 MODULE_PARM_DESC(disable_clustering, "If you experience problems with your devices, try setting to 1");
286 /* Startup values, to be overriden on the commandline */
287 static int tmscsim[] = {-2, -2, -2, -2, -2, -2};
289 module_param_array(tmscsim, int, NULL, 0);
290 MODULE_PARM_DESC(tmscsim, "Host SCSI ID, Speed (0=10MHz), Device Flags, Adapter Flags, Max Tags (log2(tags)-1), DelayReset (s)");
291 MODULE_AUTHOR("C.L. Huang / Kurt Garloff");
292 MODULE_DESCRIPTION("SCSI host adapter driver for Tekram DC390 and other AMD53C974A based PCI SCSI adapters");
293 MODULE_LICENSE("GPL");
294 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
296 static void *dc390_phase0[]={
297 dc390_DataOut_0,
298 dc390_DataIn_0,
299 dc390_Command_0,
300 dc390_Status_0,
301 dc390_Nop_0,
302 dc390_Nop_0,
303 dc390_MsgOut_0,
304 dc390_MsgIn_0,
305 dc390_Nop_1
308 static void *dc390_phase1[]={
309 dc390_DataOutPhase,
310 dc390_DataInPhase,
311 dc390_CommandPhase,
312 dc390_StatusPhase,
313 dc390_Nop_0,
314 dc390_Nop_0,
315 dc390_MsgOutPhase,
316 dc390_MsgInPhase,
317 dc390_Nop_1
320 #ifdef DC390_DEBUG1
321 static char* dc390_p0_str[] = {
322 "dc390_DataOut_0",
323 "dc390_DataIn_0",
324 "dc390_Command_0",
325 "dc390_Status_0",
326 "dc390_Nop_0",
327 "dc390_Nop_0",
328 "dc390_MsgOut_0",
329 "dc390_MsgIn_0",
330 "dc390_Nop_1"
333 static char* dc390_p1_str[] = {
334 "dc390_DataOutPhase",
335 "dc390_DataInPhase",
336 "dc390_CommandPhase",
337 "dc390_StatusPhase",
338 "dc390_Nop_0",
339 "dc390_Nop_0",
340 "dc390_MsgOutPhase",
341 "dc390_MsgInPhase",
342 "dc390_Nop_1"
344 #endif
346 static u8 dc390_eepromBuf[MAX_ADAPTER_NUM][EE_LEN];
347 static u8 dc390_clock_period1[] = {4, 5, 6, 7, 8, 10, 13, 20};
348 static u8 dc390_clock_speed[] = {100,80,67,57,50, 40, 31, 20};
350 /***********************************************************************
351 * Functions for the management of the internal structures
352 * (DCBs, SRBs, Queueing)
354 **********************************************************************/
355 static void inline dc390_start_segment(struct dc390_srb* pSRB)
357 struct scatterlist *psgl = pSRB->pSegmentList;
359 /* start new sg segment */
360 pSRB->SGBusAddr = sg_dma_address(psgl);
361 pSRB->SGToBeXferLen = sg_dma_len(psgl);
364 static unsigned long inline dc390_advance_segment(struct dc390_srb* pSRB, u32 residue)
366 unsigned long xfer = pSRB->SGToBeXferLen - residue;
368 /* xfer more bytes transferred */
369 pSRB->SGBusAddr += xfer;
370 pSRB->TotalXferredLen += xfer;
371 pSRB->SGToBeXferLen = residue;
373 return xfer;
376 static struct dc390_dcb __inline__ *dc390_findDCB ( struct dc390_acb* pACB, u8 id, u8 lun)
378 struct dc390_dcb* pDCB = pACB->pLinkDCB; if (!pDCB) return NULL;
379 while (pDCB->TargetID != id || pDCB->TargetLUN != lun)
381 pDCB = pDCB->pNextDCB;
382 if (pDCB == pACB->pLinkDCB)
383 return NULL;
385 DCBDEBUG1( printk (KERN_DEBUG "DCB %p (%02x,%02x) found.\n", \
386 pDCB, pDCB->TargetID, pDCB->TargetLUN));
387 return pDCB;
390 /* Insert SRB oin top of free list */
391 static __inline__ void dc390_Free_insert (struct dc390_acb* pACB, struct dc390_srb* pSRB)
393 DEBUG0(printk ("DC390: Free SRB %p\n", pSRB));
394 pSRB->pNextSRB = pACB->pFreeSRB;
395 pACB->pFreeSRB = pSRB;
398 static __inline__ void dc390_Going_append (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
400 pDCB->GoingSRBCnt++;
401 DEBUG0(printk("DC390: Append SRB %p to Going\n", pSRB));
402 /* Append to the list of Going commands */
403 if( pDCB->pGoingSRB )
404 pDCB->pGoingLast->pNextSRB = pSRB;
405 else
406 pDCB->pGoingSRB = pSRB;
408 pDCB->pGoingLast = pSRB;
409 /* No next one in sent list */
410 pSRB->pNextSRB = NULL;
413 static __inline__ void dc390_Going_remove (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
415 DEBUG0(printk("DC390: Remove SRB %p from Going\n", pSRB));
416 if (pSRB == pDCB->pGoingSRB)
417 pDCB->pGoingSRB = pSRB->pNextSRB;
418 else
420 struct dc390_srb* psrb = pDCB->pGoingSRB;
421 while (psrb && psrb->pNextSRB != pSRB)
422 psrb = psrb->pNextSRB;
423 if (!psrb)
424 { printk (KERN_ERR "DC390: Remove non-ex. SRB %p from Going!\n", pSRB); return; }
425 psrb->pNextSRB = pSRB->pNextSRB;
426 if (pSRB == pDCB->pGoingLast)
427 pDCB->pGoingLast = psrb;
429 pDCB->GoingSRBCnt--;
432 static struct scatterlist* dc390_sg_build_single(struct scatterlist *sg, void *addr, unsigned int length)
434 sg_init_one(sg, addr, length);
435 return sg;
438 /* Create pci mapping */
439 static int dc390_pci_map (struct dc390_srb* pSRB)
441 int error = 0;
442 struct scsi_cmnd *pcmd = pSRB->pcmd;
443 struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
444 dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp));
446 /* Map sense buffer */
447 if (pSRB->SRBFlag & AUTO_REQSENSE) {
448 pSRB->pSegmentList = dc390_sg_build_single(&pSRB->Segmentx, pcmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
449 pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, 1,
450 DMA_FROM_DEVICE);
451 cmdp->saved_dma_handle = sg_dma_address(pSRB->pSegmentList);
453 /* TODO: error handling */
454 if (pSRB->SGcount != 1)
455 error = 1;
456 DEBUG1(printk("%s(): Mapped sense buffer %p at %x\n", __func__, pcmd->sense_buffer, cmdp->saved_dma_handle));
457 /* Map SG list */
458 } else if (scsi_sg_count(pcmd)) {
459 int nseg;
461 nseg = scsi_dma_map(pcmd);
463 pSRB->pSegmentList = scsi_sglist(pcmd);
464 pSRB->SGcount = nseg;
466 /* TODO: error handling */
467 if (nseg < 0)
468 error = 1;
469 DEBUG1(printk("%s(): Mapped SG %p with %d (%d) elements\n",\
470 __func__, scsi_sglist(pcmd), nseg, scsi_sg_count(pcmd)));
471 /* Map single segment */
472 } else
473 pSRB->SGcount = 0;
475 return error;
478 /* Remove pci mapping */
479 static void dc390_pci_unmap (struct dc390_srb* pSRB)
481 struct scsi_cmnd *pcmd = pSRB->pcmd;
482 struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
483 DEBUG1(dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp)));
485 if (pSRB->SRBFlag) {
486 pci_unmap_sg(pdev, &pSRB->Segmentx, 1, DMA_FROM_DEVICE);
487 DEBUG1(printk("%s(): Unmapped sense buffer at %x\n", __func__, cmdp->saved_dma_handle));
488 } else {
489 scsi_dma_unmap(pcmd);
490 DEBUG1(printk("%s(): Unmapped SG at %p with %d elements\n",
491 __func__, scsi_sglist(pcmd), scsi_sg_count(pcmd)));
495 static void __inline__
496 dc390_freetag (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
498 if (pSRB->TagNumber != SCSI_NO_TAG) {
499 pDCB->TagMask &= ~(1 << pSRB->TagNumber); /* free tag mask */
500 pSRB->TagNumber = SCSI_NO_TAG;
505 static int
506 dc390_StartSCSI( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
508 struct scsi_cmnd *scmd = pSRB->pcmd;
509 struct scsi_device *sdev = scmd->device;
510 u8 cmd, disc_allowed, try_sync_nego;
511 char tag[2];
513 pSRB->ScsiPhase = SCSI_NOP0;
515 if (pACB->Connected)
517 // Should not happen normally
518 printk (KERN_WARNING "DC390: Can't select when connected! (%08x,%02x)\n",
519 pSRB->SRBState, pSRB->SRBFlag);
520 pSRB->SRBState = SRB_READY;
521 pACB->SelConn++;
522 return 1;
524 if (time_before (jiffies, pACB->pScsiHost->last_reset))
526 DEBUG0(printk ("DC390: We were just reset and don't accept commands yet!\n"));
527 return 1;
529 /* KG: Moved pci mapping here */
530 dc390_pci_map(pSRB);
531 /* TODO: error handling */
532 DC390_write8 (Scsi_Dest_ID, pDCB->TargetID);
533 DC390_write8 (Sync_Period, pDCB->SyncPeriod);
534 DC390_write8 (Sync_Offset, pDCB->SyncOffset);
535 DC390_write8 (CtrlReg1, pDCB->CtrlR1);
536 DC390_write8 (CtrlReg3, pDCB->CtrlR3);
537 DC390_write8 (CtrlReg4, pDCB->CtrlR4);
538 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD); /* Flush FIFO */
539 DEBUG1(printk (KERN_INFO "DC390: Start SCSI command: %02x (Sync:%02x)\n",\
540 scmd->cmnd[0], pDCB->SyncMode));
542 /* Don't disconnect on AUTO_REQSENSE, cause it might be an
543 * Contingent Allegiance Condition (6.6), where no tags should be used.
544 * All other have to be allowed to disconnect to prevent Incorrect
545 * Initiator Connection (6.8.2/6.5.2) */
546 /* Changed KG, 99/06/06 */
547 if (! (pSRB->SRBFlag & AUTO_REQSENSE))
548 disc_allowed = pDCB->DevMode & EN_DISCONNECT_;
549 else
550 disc_allowed = 0;
552 if ((pDCB->SyncMode & SYNC_ENABLE) && pDCB->TargetLUN == 0 && sdev->sdtr &&
553 (((scmd->cmnd[0] == REQUEST_SENSE || (pSRB->SRBFlag & AUTO_REQSENSE)) &&
554 !(pDCB->SyncMode & SYNC_NEGO_DONE)) || scmd->cmnd[0] == INQUIRY))
555 try_sync_nego = 1;
556 else
557 try_sync_nego = 0;
559 pSRB->MsgCnt = 0;
560 cmd = SEL_W_ATN;
561 DC390_write8 (ScsiFifo, IDENTIFY(disc_allowed, pDCB->TargetLUN));
562 /* Change 99/05/31: Don't use tags when not disconnecting (BUSY) */
563 if ((pDCB->SyncMode & EN_TAG_QUEUEING) && disc_allowed && scsi_populate_tag_msg(scmd, tag)) {
564 DC390_write8(ScsiFifo, tag[0]);
565 pDCB->TagMask |= 1 << tag[1];
566 pSRB->TagNumber = tag[1];
567 DC390_write8(ScsiFifo, tag[1]);
568 DEBUG1(printk(KERN_INFO "DC390: Select w/DisCn for Cmd %li (SRB %p), block tag %02x\n", scmd->serial_number, pSRB, tag[1]));
569 cmd = SEL_W_ATN3;
570 } else {
571 /* No TagQ */
572 //no_tag:
573 DEBUG1(printk(KERN_INFO "DC390: Select w%s/DisCn for Cmd %li (SRB %p), No TagQ\n", disc_allowed ? "" : "o", scmd->serial_number, pSRB));
576 pSRB->SRBState = SRB_START_;
578 if (try_sync_nego)
580 u8 Sync_Off = pDCB->SyncOffset;
581 DEBUG0(printk (KERN_INFO "DC390: NEW Sync Nego code triggered (%i %i)\n", pDCB->TargetID, pDCB->TargetLUN));
582 pSRB->MsgOutBuf[0] = EXTENDED_MESSAGE;
583 pSRB->MsgOutBuf[1] = 3;
584 pSRB->MsgOutBuf[2] = EXTENDED_SDTR;
585 pSRB->MsgOutBuf[3] = pDCB->NegoPeriod;
586 if (!(Sync_Off & 0x0f)) Sync_Off = SYNC_NEGO_OFFSET;
587 pSRB->MsgOutBuf[4] = Sync_Off;
588 pSRB->MsgCnt = 5;
589 //pSRB->SRBState = SRB_MSGOUT_;
590 pSRB->SRBState |= DO_SYNC_NEGO;
591 cmd = SEL_W_ATN_STOP;
594 /* Command is written in CommandPhase, if SEL_W_ATN_STOP ... */
595 if (cmd != SEL_W_ATN_STOP)
597 if( pSRB->SRBFlag & AUTO_REQSENSE )
599 DC390_write8 (ScsiFifo, REQUEST_SENSE);
600 DC390_write8 (ScsiFifo, pDCB->TargetLUN << 5);
601 DC390_write8 (ScsiFifo, 0);
602 DC390_write8 (ScsiFifo, 0);
603 DC390_write8 (ScsiFifo, SCSI_SENSE_BUFFERSIZE);
604 DC390_write8 (ScsiFifo, 0);
605 DEBUG1(printk (KERN_DEBUG "DC390: AutoReqSense !\n"));
607 else /* write cmnd to bus */
609 u8 *ptr; u8 i;
610 ptr = (u8 *)scmd->cmnd;
611 for (i = 0; i < scmd->cmd_len; i++)
612 DC390_write8 (ScsiFifo, *(ptr++));
615 DEBUG0(if (pACB->pActiveDCB) \
616 printk (KERN_WARNING "DC390: ActiveDCB != 0\n"));
617 DEBUG0(if (pDCB->pActiveSRB) \
618 printk (KERN_WARNING "DC390: ActiveSRB != 0\n"));
619 //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
620 if (DC390_read8 (Scsi_Status) & INTERRUPT)
622 dc390_freetag (pDCB, pSRB);
623 DEBUG0(printk ("DC390: Interrupt during Start SCSI (pid %li, target %02i-%02i)\n",
624 scmd->serial_number, scmd->device->id, scmd->device->lun));
625 pSRB->SRBState = SRB_READY;
626 //DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
627 pACB->SelLost++;
628 return 1;
630 DC390_write8 (ScsiCmd, cmd);
631 pACB->pActiveDCB = pDCB;
632 pDCB->pActiveSRB = pSRB;
633 pACB->Connected = 1;
634 pSRB->ScsiPhase = SCSI_NOP1;
635 return 0;
639 static void __inline__
640 dc390_InvalidCmd(struct dc390_acb* pACB)
642 if (pACB->pActiveDCB->pActiveSRB->SRBState & (SRB_START_ | SRB_MSGOUT))
643 DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
647 static irqreturn_t __inline__
648 DC390_Interrupt(void *dev_id)
650 struct dc390_acb *pACB = dev_id;
651 struct dc390_dcb *pDCB;
652 struct dc390_srb *pSRB;
653 u8 sstatus=0;
654 u8 phase;
655 void (*stateV)( struct dc390_acb*, struct dc390_srb*, u8 *);
656 u8 istate, istatus;
658 sstatus = DC390_read8 (Scsi_Status);
659 if( !(sstatus & INTERRUPT) )
660 return IRQ_NONE;
662 DEBUG1(printk (KERN_DEBUG "sstatus=%02x,", sstatus));
664 //DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT);
665 //dstatus = DC390_read8 (DMA_Status);
666 //DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
668 spin_lock_irq(pACB->pScsiHost->host_lock);
670 istate = DC390_read8 (Intern_State);
671 istatus = DC390_read8 (INT_Status); /* This clears Scsi_Status, Intern_State and INT_Status ! */
673 DEBUG1(printk (KERN_INFO "Istatus(Res,Inv,Dis,Serv,Succ,ReS,SelA,Sel)=%02x,",istatus));
674 dc390_laststatus &= ~0x00ffffff;
675 dc390_laststatus |= /* dstatus<<24 | */ sstatus<<16 | istate<<8 | istatus;
677 if (sstatus & ILLEGAL_OP_ERR)
679 printk ("DC390: Illegal Operation detected (%08x)!\n", dc390_laststatus);
680 dc390_dumpinfo (pACB, pACB->pActiveDCB, pACB->pActiveDCB->pActiveSRB);
683 else if (istatus & INVALID_CMD)
685 printk ("DC390: Invalid Command detected (%08x)!\n", dc390_laststatus);
686 dc390_InvalidCmd( pACB );
687 goto unlock;
690 if (istatus & SCSI_RESET)
692 dc390_ScsiRstDetect( pACB );
693 goto unlock;
696 if (istatus & DISCONNECTED)
698 dc390_Disconnect( pACB );
699 goto unlock;
702 if (istatus & RESELECTED)
704 dc390_Reselect( pACB );
705 goto unlock;
708 else if (istatus & (SELECTED | SEL_ATTENTION))
710 printk (KERN_ERR "DC390: Target mode not supported!\n");
711 goto unlock;
714 if (istatus & (SUCCESSFUL_OP|SERVICE_REQUEST) )
716 pDCB = pACB->pActiveDCB;
717 if (!pDCB)
719 printk (KERN_ERR "DC390: Suc. op/ Serv. req: pActiveDCB = 0!\n");
720 goto unlock;
722 pSRB = pDCB->pActiveSRB;
723 if( pDCB->DCBFlag & ABORT_DEV_ )
724 dc390_EnableMsgOut_Abort (pACB, pSRB);
726 phase = pSRB->ScsiPhase;
727 DEBUG1(printk (KERN_INFO "DC390: [%i]%s(0) (%02x)\n", phase, dc390_p0_str[phase], sstatus));
728 stateV = (void *) dc390_phase0[phase];
729 ( *stateV )( pACB, pSRB, &sstatus );
731 pSRB->ScsiPhase = sstatus & 7;
732 phase = (u8) sstatus & 7;
733 DEBUG1(printk (KERN_INFO "DC390: [%i]%s(1) (%02x)\n", phase, dc390_p1_str[phase], sstatus));
734 stateV = (void *) dc390_phase1[phase];
735 ( *stateV )( pACB, pSRB, &sstatus );
738 unlock:
739 spin_unlock_irq(pACB->pScsiHost->host_lock);
740 return IRQ_HANDLED;
743 static irqreturn_t do_DC390_Interrupt(int irq, void *dev_id)
745 irqreturn_t ret;
746 DEBUG1(printk (KERN_INFO "DC390: Irq (%i) caught: ", irq));
747 /* Locking is done in DC390_Interrupt */
748 ret = DC390_Interrupt(dev_id);
749 DEBUG1(printk (".. IRQ returned\n"));
750 return ret;
753 static void
754 dc390_DataOut_0(struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
756 u8 sstatus;
757 u32 ResidCnt;
758 u8 dstate = 0;
760 sstatus = *psstatus;
762 if( !(pSRB->SRBState & SRB_XFERPAD) )
764 if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR) )
765 pSRB->SRBStatus |= PARITY_ERROR;
767 if( sstatus & COUNT_2_ZERO )
769 unsigned long timeout = jiffies + HZ;
771 /* Function called from the ISR with the host_lock held and interrupts disabled */
772 if (pSRB->SGToBeXferLen)
773 while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) {
774 spin_unlock_irq(pACB->pScsiHost->host_lock);
775 udelay(50);
776 spin_lock_irq(pACB->pScsiHost->host_lock);
778 if (!time_before(jiffies, timeout))
779 printk (KERN_CRIT "DC390: Deadlock in DataOut_0: DMA aborted unfinished: %06x bytes remain!!\n",
780 DC390_read32 (DMA_Wk_ByteCntr));
781 dc390_laststatus &= ~0xff000000;
782 dc390_laststatus |= dstate << 24;
783 pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
784 pSRB->SGIndex++;
785 if( pSRB->SGIndex < pSRB->SGcount )
787 pSRB->pSegmentList++;
789 dc390_start_segment(pSRB);
791 else
792 pSRB->SGToBeXferLen = 0;
794 else
796 ResidCnt = ((u32) DC390_read8 (Current_Fifo) & 0x1f) +
797 (((u32) DC390_read8 (CtcReg_High) << 16) |
798 ((u32) DC390_read8 (CtcReg_Mid) << 8) |
799 (u32) DC390_read8 (CtcReg_Low));
801 dc390_advance_segment(pSRB, ResidCnt);
804 if ((*psstatus & 7) != SCSI_DATA_OUT)
806 DC390_write8 (DMA_Cmd, WRITE_DIRECTION+DMA_IDLE_CMD);
807 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
811 static void
812 dc390_DataIn_0(struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
814 u8 sstatus, residual, bval;
815 u32 ResidCnt, i;
816 unsigned long xferCnt;
818 sstatus = *psstatus;
820 if( !(pSRB->SRBState & SRB_XFERPAD) )
822 if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR))
823 pSRB->SRBStatus |= PARITY_ERROR;
825 if( sstatus & COUNT_2_ZERO )
827 int dstate = 0;
828 unsigned long timeout = jiffies + HZ;
830 /* Function called from the ISR with the host_lock held and interrupts disabled */
831 if (pSRB->SGToBeXferLen)
832 while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) {
833 spin_unlock_irq(pACB->pScsiHost->host_lock);
834 udelay(50);
835 spin_lock_irq(pACB->pScsiHost->host_lock);
837 if (!time_before(jiffies, timeout)) {
838 printk (KERN_CRIT "DC390: Deadlock in DataIn_0: DMA aborted unfinished: %06x bytes remain!!\n",
839 DC390_read32 (DMA_Wk_ByteCntr));
840 printk (KERN_CRIT "DC390: DataIn_0: DMA State: %i\n", dstate);
842 dc390_laststatus &= ~0xff000000;
843 dc390_laststatus |= dstate << 24;
844 DEBUG1(ResidCnt = ((unsigned long) DC390_read8 (CtcReg_High) << 16) \
845 + ((unsigned long) DC390_read8 (CtcReg_Mid) << 8) \
846 + ((unsigned long) DC390_read8 (CtcReg_Low)));
847 DEBUG1(printk (KERN_DEBUG "Count_2_Zero (ResidCnt=%u,ToBeXfer=%lu),", ResidCnt, pSRB->SGToBeXferLen));
849 DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD);
851 pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
852 pSRB->SGIndex++;
853 if( pSRB->SGIndex < pSRB->SGcount )
855 pSRB->pSegmentList++;
857 dc390_start_segment(pSRB);
859 else
860 pSRB->SGToBeXferLen = 0;
862 else /* phase changed */
864 residual = 0;
865 bval = DC390_read8 (Current_Fifo);
866 while( bval & 0x1f )
868 DEBUG1(printk (KERN_DEBUG "Check for residuals,"));
869 if( (bval & 0x1f) == 1 )
871 for(i=0; i < 0x100; i++)
873 bval = DC390_read8 (Current_Fifo);
874 if( !(bval & 0x1f) )
875 goto din_1;
876 else if( i == 0x0ff )
878 residual = 1; /* ;1 residual byte */
879 goto din_1;
883 else
884 bval = DC390_read8 (Current_Fifo);
886 din_1:
887 DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_BLAST_CMD);
888 for (i = 0xa000; i; i--)
890 bval = DC390_read8 (DMA_Status);
891 if (bval & BLAST_COMPLETE)
892 break;
894 /* It seems a DMA Blast abort isn't that bad ... */
895 if (!i) printk (KERN_ERR "DC390: DMA Blast aborted unfinished!\n");
896 //DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD);
897 dc390_laststatus &= ~0xff000000;
898 dc390_laststatus |= bval << 24;
900 DEBUG1(printk (KERN_DEBUG "Blast: Read %i times DMA_Status %02x", 0xa000-i, bval));
901 ResidCnt = (((u32) DC390_read8 (CtcReg_High) << 16) |
902 ((u32) DC390_read8 (CtcReg_Mid) << 8)) |
903 (u32) DC390_read8 (CtcReg_Low);
905 xferCnt = dc390_advance_segment(pSRB, ResidCnt);
907 if (residual) {
908 size_t count = 1;
909 size_t offset = pSRB->SGBusAddr - sg_dma_address(pSRB->pSegmentList);
910 unsigned long flags;
911 u8 *ptr;
913 bval = DC390_read8 (ScsiFifo); /* get one residual byte */
915 local_irq_save(flags);
916 ptr = scsi_kmap_atomic_sg(pSRB->pSegmentList, pSRB->SGcount, &offset, &count);
917 if (likely(ptr)) {
918 *(ptr + offset) = bval;
919 scsi_kunmap_atomic_sg(ptr);
921 local_irq_restore(flags);
922 WARN_ON(!ptr);
924 /* 1 more byte read */
925 xferCnt += dc390_advance_segment(pSRB, pSRB->SGToBeXferLen - 1);
927 DEBUG1(printk (KERN_DEBUG "Xfered: %lu, Total: %lu, Remaining: %lu\n", xferCnt,\
928 pSRB->TotalXferredLen, pSRB->SGToBeXferLen));
931 if ((*psstatus & 7) != SCSI_DATA_IN)
933 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
934 DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD);
938 static void
939 dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
943 static void
944 dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
947 pSRB->TargetStatus = DC390_read8 (ScsiFifo);
948 //udelay (1);
949 pSRB->EndMessage = DC390_read8 (ScsiFifo); /* get message */
951 *psstatus = SCSI_NOP0;
952 pSRB->SRBState = SRB_COMPLETED;
953 DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);
956 static void
957 dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
959 if( pSRB->SRBState & (SRB_UNEXPECT_RESEL+SRB_ABORT_SENT) )
960 *psstatus = SCSI_NOP0;
961 //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
965 static void __inline__
966 dc390_reprog (struct dc390_acb* pACB, struct dc390_dcb* pDCB)
968 DC390_write8 (Sync_Period, pDCB->SyncPeriod);
969 DC390_write8 (Sync_Offset, pDCB->SyncOffset);
970 DC390_write8 (CtrlReg3, pDCB->CtrlR3);
971 DC390_write8 (CtrlReg4, pDCB->CtrlR4);
972 dc390_SetXferRate (pACB, pDCB);
976 #ifdef DC390_DEBUG0
977 static void
978 dc390_printMsg (u8 *MsgBuf, u8 len)
980 int i;
981 printk (" %02x", MsgBuf[0]);
982 for (i = 1; i < len; i++)
983 printk (" %02x", MsgBuf[i]);
984 printk ("\n");
986 #endif
988 #define DC390_ENABLE_MSGOUT DC390_write8 (ScsiCmd, SET_ATN_CMD)
990 /* reject_msg */
991 static void __inline__
992 dc390_MsgIn_reject (struct dc390_acb* pACB, struct dc390_srb* pSRB)
994 pSRB->MsgOutBuf[0] = MESSAGE_REJECT;
995 pSRB->MsgCnt = 1;
996 DC390_ENABLE_MSGOUT;
997 DEBUG0 (printk (KERN_INFO "DC390: Reject message\n"));
1000 /* abort command */
1001 static void
1002 dc390_EnableMsgOut_Abort ( struct dc390_acb* pACB, struct dc390_srb* pSRB )
1004 pSRB->MsgOutBuf[0] = ABORT;
1005 pSRB->MsgCnt = 1; DC390_ENABLE_MSGOUT;
1006 pSRB->pSRBDCB->DCBFlag &= ~ABORT_DEV_;
1009 static struct dc390_srb*
1010 dc390_MsgIn_QTag (struct dc390_acb* pACB, struct dc390_dcb* pDCB, s8 tag)
1012 struct dc390_srb* pSRB = pDCB->pGoingSRB;
1014 if (pSRB)
1016 struct scsi_cmnd *scmd = scsi_find_tag(pSRB->pcmd->device, tag);
1017 pSRB = (struct dc390_srb *)scmd->host_scribble;
1019 if (pDCB->DCBFlag & ABORT_DEV_)
1021 pSRB->SRBState = SRB_ABORT_SENT;
1022 dc390_EnableMsgOut_Abort( pACB, pSRB );
1025 if (!(pSRB->SRBState & SRB_DISCONNECT))
1026 goto mingx0;
1028 pDCB->pActiveSRB = pSRB;
1029 pSRB->SRBState = SRB_DATA_XFER;
1031 else
1033 mingx0:
1034 pSRB = pACB->pTmpSRB;
1035 pSRB->SRBState = SRB_UNEXPECT_RESEL;
1036 pDCB->pActiveSRB = pSRB;
1037 pSRB->MsgOutBuf[0] = ABORT_TAG;
1038 pSRB->MsgCnt = 1; DC390_ENABLE_MSGOUT;
1040 return pSRB;
1044 /* set async transfer mode */
1045 static void
1046 dc390_MsgIn_set_async (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1048 struct dc390_dcb* pDCB = pSRB->pSRBDCB;
1049 if (!(pSRB->SRBState & DO_SYNC_NEGO))
1050 printk (KERN_INFO "DC390: Target %i initiates Non-Sync?\n", pDCB->TargetID);
1051 pSRB->SRBState &= ~DO_SYNC_NEGO;
1052 pDCB->SyncMode &= ~(SYNC_ENABLE+SYNC_NEGO_DONE);
1053 pDCB->SyncPeriod = 0;
1054 pDCB->SyncOffset = 0;
1055 //pDCB->NegoPeriod = 50; /* 200ns <=> 5 MHz */
1056 pDCB->CtrlR3 = FAST_CLK; /* fast clock / normal scsi */
1057 pDCB->CtrlR4 &= 0x3f;
1058 pDCB->CtrlR4 |= pACB->glitch_cfg; /* glitch eater */
1059 dc390_reprog (pACB, pDCB);
1062 /* set sync transfer mode */
1063 static void
1064 dc390_MsgIn_set_sync (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1066 u8 bval;
1067 u16 wval, wval1;
1068 struct dc390_dcb* pDCB = pSRB->pSRBDCB;
1069 u8 oldsyncperiod = pDCB->SyncPeriod;
1070 u8 oldsyncoffset = pDCB->SyncOffset;
1072 if (!(pSRB->SRBState & DO_SYNC_NEGO))
1074 printk (KERN_INFO "DC390: Target %i initiates Sync: %ins %i ... answer ...\n",
1075 pDCB->TargetID, pSRB->MsgInBuf[3]<<2, pSRB->MsgInBuf[4]);
1077 /* reject */
1078 //dc390_MsgIn_reject (pACB, pSRB);
1079 //return dc390_MsgIn_set_async (pACB, pSRB);
1081 /* Reply with corrected SDTR Message */
1082 if (pSRB->MsgInBuf[4] > 15)
1084 printk (KERN_INFO "DC390: Lower Sync Offset to 15\n");
1085 pSRB->MsgInBuf[4] = 15;
1087 if (pSRB->MsgInBuf[3] < pDCB->NegoPeriod)
1089 printk (KERN_INFO "DC390: Set sync nego period to %ins\n", pDCB->NegoPeriod << 2);
1090 pSRB->MsgInBuf[3] = pDCB->NegoPeriod;
1092 memcpy (pSRB->MsgOutBuf, pSRB->MsgInBuf, 5);
1093 pSRB->MsgCnt = 5;
1094 DC390_ENABLE_MSGOUT;
1097 pSRB->SRBState &= ~DO_SYNC_NEGO;
1098 pDCB->SyncMode |= SYNC_ENABLE+SYNC_NEGO_DONE;
1099 pDCB->SyncOffset &= 0x0f0;
1100 pDCB->SyncOffset |= pSRB->MsgInBuf[4];
1101 pDCB->NegoPeriod = pSRB->MsgInBuf[3];
1103 wval = (u16) pSRB->MsgInBuf[3];
1104 wval = wval << 2; wval -= 3; wval1 = wval / 25; /* compute speed */
1105 if( (wval1 * 25) != wval) wval1++;
1106 bval = FAST_CLK+FAST_SCSI; /* fast clock / fast scsi */
1108 pDCB->CtrlR4 &= 0x3f; /* Glitch eater: 12ns less than normal */
1109 if (pACB->glitch_cfg != NS_TO_GLITCH(0))
1110 pDCB->CtrlR4 |= NS_TO_GLITCH(((GLITCH_TO_NS(pACB->glitch_cfg)) - 1));
1111 else
1112 pDCB->CtrlR4 |= NS_TO_GLITCH(0);
1113 if (wval1 < 4) pDCB->CtrlR4 |= NS_TO_GLITCH(0); /* Ultra */
1115 if (wval1 >= 8)
1117 wval1--; /* Timing computation differs by 1 from FAST_SCSI */
1118 bval = FAST_CLK; /* fast clock / normal scsi */
1119 pDCB->CtrlR4 |= pACB->glitch_cfg; /* glitch eater */
1122 pDCB->CtrlR3 = bval;
1123 pDCB->SyncPeriod = (u8)wval1;
1125 if ((oldsyncperiod != wval1 || oldsyncoffset != pDCB->SyncOffset) && pDCB->TargetLUN == 0)
1127 if (! (bval & FAST_SCSI)) wval1++;
1128 printk (KERN_INFO "DC390: Target %i: Sync transfer %i.%1i MHz, Offset %i\n", pDCB->TargetID,
1129 40/wval1, ((40%wval1)*10+wval1/2)/wval1, pDCB->SyncOffset & 0x0f);
1132 dc390_reprog (pACB, pDCB);
1136 /* handle RESTORE_PTR */
1137 /* This doesn't look very healthy... to-be-fixed */
1138 static void
1139 dc390_restore_ptr (struct dc390_acb* pACB, struct dc390_srb* pSRB)
1141 struct scsi_cmnd *pcmd = pSRB->pcmd;
1142 struct scatterlist *psgl;
1143 pSRB->TotalXferredLen = 0;
1144 pSRB->SGIndex = 0;
1145 if (scsi_sg_count(pcmd)) {
1146 size_t saved;
1147 pSRB->pSegmentList = scsi_sglist(pcmd);
1148 psgl = pSRB->pSegmentList;
1149 //dc390_pci_sync(pSRB);
1151 while (pSRB->TotalXferredLen + (unsigned long) sg_dma_len(psgl) < pSRB->Saved_Ptr)
1153 pSRB->TotalXferredLen += (unsigned long) sg_dma_len(psgl);
1154 pSRB->SGIndex++;
1155 if( pSRB->SGIndex < pSRB->SGcount )
1157 pSRB->pSegmentList++;
1159 dc390_start_segment(pSRB);
1161 else
1162 pSRB->SGToBeXferLen = 0;
1165 saved = pSRB->Saved_Ptr - pSRB->TotalXferredLen;
1166 pSRB->SGToBeXferLen -= saved;
1167 pSRB->SGBusAddr += saved;
1168 printk (KERN_INFO "DC390: Pointer restored. Segment %i, Total %li, Bus %08lx\n",
1169 pSRB->SGIndex, pSRB->Saved_Ptr, pSRB->SGBusAddr);
1171 } else {
1172 pSRB->SGcount = 0;
1173 printk (KERN_INFO "DC390: RESTORE_PTR message for Transfer without Scatter-Gather ??\n");
1176 pSRB->TotalXferredLen = pSRB->Saved_Ptr;
1180 /* According to the docs, the AM53C974 reads the message and
1181 * generates a Successful Operation IRQ before asserting ACK for
1182 * the last byte (how does it know whether it's the last ?) */
1183 /* The old code handled it in another way, indicating, that on
1184 * every message byte an IRQ is generated and every byte has to
1185 * be manually ACKed. Hmmm ? (KG, 98/11/28) */
1186 /* The old implementation was correct. Sigh! */
1188 /* Check if the message is complete */
1189 static u8 __inline__
1190 dc390_MsgIn_complete (u8 *msgbuf, u32 len)
1192 if (*msgbuf == EXTENDED_MESSAGE)
1194 if (len < 2) return 0;
1195 if (len < msgbuf[1] + 2) return 0;
1197 else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) // two byte messages
1198 if (len < 2) return 0;
1199 return 1;
1204 /* read and eval received messages */
1205 static void
1206 dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1208 struct dc390_dcb* pDCB = pACB->pActiveDCB;
1210 /* Read the msg */
1212 pSRB->MsgInBuf[pACB->MsgLen++] = DC390_read8 (ScsiFifo);
1213 //pSRB->SRBState = 0;
1215 /* Msg complete ? */
1216 if (dc390_MsgIn_complete (pSRB->MsgInBuf, pACB->MsgLen))
1218 DEBUG0 (printk (KERN_INFO "DC390: MsgIn:"); dc390_printMsg (pSRB->MsgInBuf, pACB->MsgLen));
1219 /* Now eval the msg */
1220 switch (pSRB->MsgInBuf[0])
1222 case DISCONNECT:
1223 pSRB->SRBState = SRB_DISCONNECT; break;
1225 case SIMPLE_QUEUE_TAG:
1226 case HEAD_OF_QUEUE_TAG:
1227 case ORDERED_QUEUE_TAG:
1228 pSRB = dc390_MsgIn_QTag (pACB, pDCB, pSRB->MsgInBuf[1]);
1229 break;
1231 case MESSAGE_REJECT:
1232 DC390_write8 (ScsiCmd, RESET_ATN_CMD);
1233 pDCB->NegoPeriod = 50; /* 200ns <=> 5 MHz */
1234 if( pSRB->SRBState & DO_SYNC_NEGO)
1235 dc390_MsgIn_set_async (pACB, pSRB);
1236 break;
1238 case EXTENDED_MESSAGE:
1239 /* reject every extended msg but SDTR */
1240 if (pSRB->MsgInBuf[1] != 3 || pSRB->MsgInBuf[2] != EXTENDED_SDTR)
1241 dc390_MsgIn_reject (pACB, pSRB);
1242 else
1244 if (pSRB->MsgInBuf[3] == 0 || pSRB->MsgInBuf[4] == 0)
1245 dc390_MsgIn_set_async (pACB, pSRB);
1246 else
1247 dc390_MsgIn_set_sync (pACB, pSRB);
1250 // nothing has to be done
1251 case COMMAND_COMPLETE: break;
1253 // SAVE POINTER may be ignored as we have the struct dc390_srb* associated with the
1254 // scsi command. Thanks, Gerard, for pointing it out.
1255 case SAVE_POINTERS:
1256 pSRB->Saved_Ptr = pSRB->TotalXferredLen;
1257 break;
1258 // The device might want to restart transfer with a RESTORE
1259 case RESTORE_POINTERS:
1260 DEBUG0(printk ("DC390: RESTORE POINTER message received ... try to handle\n"));
1261 dc390_restore_ptr (pACB, pSRB);
1262 break;
1264 // reject unknown messages
1265 default: dc390_MsgIn_reject (pACB, pSRB);
1268 /* Clear counter and MsgIn state */
1269 pSRB->SRBState &= ~SRB_MSGIN;
1270 pACB->MsgLen = 0;
1273 *psstatus = SCSI_NOP0;
1274 DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);
1275 //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1279 static void
1280 dc390_DataIO_Comm( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 ioDir)
1282 unsigned long lval;
1283 struct dc390_dcb* pDCB = pACB->pActiveDCB;
1285 if (pSRB == pACB->pTmpSRB)
1287 if (pDCB)
1288 printk(KERN_ERR "DC390: pSRB == pTmpSRB! (TagQ Error?) (%02i-%i)\n", pDCB->TargetID, pDCB->TargetLUN);
1289 else
1290 printk(KERN_ERR "DC390: pSRB == pTmpSRB! (TagQ Error?) (DCB 0!)\n");
1292 /* Try to recover - some broken disks react badly to tagged INQUIRY */
1293 if (pDCB && pACB->scan_devices && pDCB->GoingSRBCnt == 1) {
1294 pSRB = pDCB->pGoingSRB;
1295 pDCB->pActiveSRB = pSRB;
1296 } else {
1297 pSRB->pSRBDCB = pDCB;
1298 dc390_EnableMsgOut_Abort(pACB, pSRB);
1299 if (pDCB)
1300 pDCB->DCBFlag |= ABORT_DEV;
1301 return;
1305 if( pSRB->SGIndex < pSRB->SGcount )
1307 DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir);
1308 if( !pSRB->SGToBeXferLen )
1310 dc390_start_segment(pSRB);
1312 DEBUG1(printk (KERN_DEBUG " DC390: Next SG segment."));
1314 lval = pSRB->SGToBeXferLen;
1315 DEBUG1(printk (KERN_DEBUG " DC390: Start transfer: %li bytes (address %08lx)\n", lval, pSRB->SGBusAddr));
1316 DC390_write8 (CtcReg_Low, (u8) lval);
1317 lval >>= 8;
1318 DC390_write8 (CtcReg_Mid, (u8) lval);
1319 lval >>= 8;
1320 DC390_write8 (CtcReg_High, (u8) lval);
1322 DC390_write32 (DMA_XferCnt, pSRB->SGToBeXferLen);
1323 DC390_write32 (DMA_XferAddr, pSRB->SGBusAddr);
1325 //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir);
1326 pSRB->SRBState = SRB_DATA_XFER;
1328 DC390_write8 (ScsiCmd, DMA_COMMAND+INFO_XFER_CMD);
1330 DC390_write8 (DMA_Cmd, DMA_START_CMD | ioDir);
1331 //DEBUG1(DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT));
1332 //DEBUG1(printk (KERN_DEBUG "DC390: DMA_Status: %02x\n", DC390_read8 (DMA_Status)));
1333 //DEBUG1(DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT));
1335 else /* xfer pad */
1337 if( pSRB->SGcount )
1339 pSRB->AdaptStatus = H_OVER_UNDER_RUN;
1340 pSRB->SRBStatus |= OVER_RUN;
1341 DEBUG0(printk (KERN_WARNING " DC390: Overrun -"));
1343 DEBUG0(printk (KERN_WARNING " Clear transfer pad \n"));
1344 DC390_write8 (CtcReg_Low, 0);
1345 DC390_write8 (CtcReg_Mid, 0);
1346 DC390_write8 (CtcReg_High, 0);
1348 pSRB->SRBState |= SRB_XFERPAD;
1349 DC390_write8 (ScsiCmd, DMA_COMMAND+XFER_PAD_BYTE);
1351 DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir);
1352 DC390_write8 (DMA_Cmd, DMA_START_CMD | ioDir);
1358 static void
1359 dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1361 dc390_DataIO_Comm (pACB, pSRB, WRITE_DIRECTION);
1364 static void
1365 dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1367 dc390_DataIO_Comm (pACB, pSRB, READ_DIRECTION);
1370 static void
1371 dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1373 struct dc390_dcb* pDCB;
1374 u8 i, cnt;
1375 u8 *ptr;
1377 DC390_write8 (ScsiCmd, RESET_ATN_CMD);
1378 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1379 if( !(pSRB->SRBFlag & AUTO_REQSENSE) )
1381 cnt = (u8) pSRB->pcmd->cmd_len;
1382 ptr = (u8 *) pSRB->pcmd->cmnd;
1383 for(i=0; i < cnt; i++)
1384 DC390_write8 (ScsiFifo, *(ptr++));
1386 else
1388 DC390_write8 (ScsiFifo, REQUEST_SENSE);
1389 pDCB = pACB->pActiveDCB;
1390 DC390_write8 (ScsiFifo, pDCB->TargetLUN << 5);
1391 DC390_write8 (ScsiFifo, 0);
1392 DC390_write8 (ScsiFifo, 0);
1393 DC390_write8 (ScsiFifo, SCSI_SENSE_BUFFERSIZE);
1394 DC390_write8 (ScsiFifo, 0);
1395 DEBUG0(printk(KERN_DEBUG "DC390: AutoReqSense (CmndPhase)!\n"));
1397 pSRB->SRBState = SRB_COMMAND;
1398 DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1401 static void
1402 dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1404 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1405 pSRB->SRBState = SRB_STATUS;
1406 DC390_write8 (ScsiCmd, INITIATOR_CMD_CMPLTE);
1407 //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1410 static void
1411 dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1413 u8 bval, i, cnt;
1414 u8 *ptr;
1415 struct dc390_dcb* pDCB;
1417 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1418 pDCB = pACB->pActiveDCB;
1419 if( !(pSRB->SRBState & SRB_MSGOUT) )
1421 cnt = pSRB->MsgCnt;
1422 if( cnt )
1424 ptr = (u8 *) pSRB->MsgOutBuf;
1425 for(i=0; i < cnt; i++)
1426 DC390_write8 (ScsiFifo, *(ptr++));
1427 pSRB->MsgCnt = 0;
1428 if( (pDCB->DCBFlag & ABORT_DEV_) &&
1429 (pSRB->MsgOutBuf[0] == ABORT) )
1430 pSRB->SRBState = SRB_ABORT_SENT;
1432 else
1434 bval = ABORT; /* ??? MSG_NOP */
1435 if( (pSRB->pcmd->cmnd[0] == INQUIRY ) ||
1436 (pSRB->pcmd->cmnd[0] == REQUEST_SENSE) ||
1437 (pSRB->SRBFlag & AUTO_REQSENSE) )
1439 if( pDCB->SyncMode & SYNC_ENABLE )
1440 goto mop1;
1442 DC390_write8 (ScsiFifo, bval);
1444 DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1446 else
1448 mop1:
1449 printk (KERN_ERR "DC390: OLD Sync Nego code triggered! (%i %i)\n", pDCB->TargetID, pDCB->TargetLUN);
1450 DC390_write8 (ScsiFifo, EXTENDED_MESSAGE);
1451 DC390_write8 (ScsiFifo, 3); /* ;length of extended msg */
1452 DC390_write8 (ScsiFifo, EXTENDED_SDTR); /* ; sync nego */
1453 DC390_write8 (ScsiFifo, pDCB->NegoPeriod);
1454 if (pDCB->SyncOffset & 0x0f)
1455 DC390_write8 (ScsiFifo, pDCB->SyncOffset);
1456 else
1457 DC390_write8 (ScsiFifo, SYNC_NEGO_OFFSET);
1458 pSRB->SRBState |= DO_SYNC_NEGO;
1459 DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1463 static void
1464 dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1466 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1467 if( !(pSRB->SRBState & SRB_MSGIN) )
1469 pSRB->SRBState &= ~SRB_DISCONNECT;
1470 pSRB->SRBState |= SRB_MSGIN;
1472 DC390_write8 (ScsiCmd, INFO_XFER_CMD);
1473 //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1476 static void
1477 dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1481 static void
1482 dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
1487 static void
1488 dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB )
1490 u8 bval, i, cnt;
1491 struct dc390_dcb* ptr;
1493 if( !(pDCB->TargetLUN) )
1495 if( !pACB->scan_devices )
1497 ptr = pACB->pLinkDCB;
1498 cnt = pACB->DCBCnt;
1499 bval = pDCB->TargetID;
1500 for(i=0; i<cnt; i++)
1502 if( ptr->TargetID == bval )
1504 ptr->SyncPeriod = pDCB->SyncPeriod;
1505 ptr->SyncOffset = pDCB->SyncOffset;
1506 ptr->CtrlR3 = pDCB->CtrlR3;
1507 ptr->CtrlR4 = pDCB->CtrlR4;
1508 ptr->SyncMode = pDCB->SyncMode;
1510 ptr = ptr->pNextDCB;
1514 return;
1518 static void
1519 dc390_Disconnect( struct dc390_acb* pACB )
1521 struct dc390_dcb *pDCB;
1522 struct dc390_srb *pSRB, *psrb;
1523 u8 i, cnt;
1525 DEBUG0(printk(KERN_INFO "DISC,"));
1527 if (!pACB->Connected) printk(KERN_ERR "DC390: Disconnect not-connected bus?\n");
1528 pACB->Connected = 0;
1529 pDCB = pACB->pActiveDCB;
1530 if (!pDCB)
1532 DEBUG0(printk(KERN_ERR "ACB:%p->ActiveDCB:%p IOPort:%04x IRQ:%02x !\n",\
1533 pACB, pDCB, pACB->IOPortBase, pACB->IRQLevel));
1534 mdelay(400);
1535 DC390_read8 (INT_Status); /* Reset Pending INT */
1536 DC390_write8 (ScsiCmd, EN_SEL_RESEL);
1537 return;
1539 DC390_write8 (ScsiCmd, EN_SEL_RESEL);
1540 pSRB = pDCB->pActiveSRB;
1541 pACB->pActiveDCB = NULL;
1542 pSRB->ScsiPhase = SCSI_NOP0;
1543 if( pSRB->SRBState & SRB_UNEXPECT_RESEL )
1544 pSRB->SRBState = 0;
1545 else if( pSRB->SRBState & SRB_ABORT_SENT )
1547 pDCB->TagMask = 0;
1548 pDCB->DCBFlag = 0;
1549 cnt = pDCB->GoingSRBCnt;
1550 pDCB->GoingSRBCnt = 0;
1551 pSRB = pDCB->pGoingSRB;
1552 for( i=0; i < cnt; i++)
1554 psrb = pSRB->pNextSRB;
1555 dc390_Free_insert (pACB, pSRB);
1556 pSRB = psrb;
1558 pDCB->pGoingSRB = NULL;
1560 else
1562 if( (pSRB->SRBState & (SRB_START_+SRB_MSGOUT)) ||
1563 !(pSRB->SRBState & (SRB_DISCONNECT+SRB_COMPLETED)) )
1564 { /* Selection time out */
1565 pSRB->AdaptStatus = H_SEL_TIMEOUT;
1566 pSRB->TargetStatus = 0;
1567 goto disc1;
1569 else if (!(pSRB->SRBState & SRB_DISCONNECT) && (pSRB->SRBState & SRB_COMPLETED))
1571 disc1:
1572 dc390_freetag (pDCB, pSRB);
1573 pDCB->pActiveSRB = NULL;
1574 pSRB->SRBState = SRB_FREE;
1575 dc390_SRBdone( pACB, pDCB, pSRB);
1578 pACB->MsgLen = 0;
1582 static void
1583 dc390_Reselect( struct dc390_acb* pACB )
1585 struct dc390_dcb* pDCB;
1586 struct dc390_srb* pSRB;
1587 u8 id, lun;
1589 DEBUG0(printk(KERN_INFO "RSEL,"));
1590 pACB->Connected = 1;
1591 pDCB = pACB->pActiveDCB;
1592 if( pDCB )
1593 { /* Arbitration lost but Reselection won */
1594 DEBUG0(printk ("DC390: (ActiveDCB != 0: Arb. lost but resel. won)!\n"));
1595 pSRB = pDCB->pActiveSRB;
1596 if( !( pACB->scan_devices ) )
1598 struct scsi_cmnd *pcmd = pSRB->pcmd;
1599 scsi_set_resid(pcmd, scsi_bufflen(pcmd));
1600 SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1601 dc390_Going_remove(pDCB, pSRB);
1602 dc390_Free_insert(pACB, pSRB);
1603 pcmd->scsi_done (pcmd);
1604 DEBUG0(printk(KERN_DEBUG"DC390: Return SRB %p to free\n", pSRB));
1607 /* Get ID */
1608 lun = DC390_read8 (ScsiFifo);
1609 DEBUG0(printk ("Dev %02x,", lun));
1610 if (!(lun & (1 << pACB->pScsiHost->this_id)))
1611 printk (KERN_ERR "DC390: Reselection must select host adapter: %02x!\n", lun);
1612 else
1613 lun ^= 1 << pACB->pScsiHost->this_id; /* Mask AdapterID */
1614 id = 0; while (lun >>= 1) id++;
1615 /* Get LUN */
1616 lun = DC390_read8 (ScsiFifo);
1617 if (!(lun & IDENTIFY_BASE)) printk (KERN_ERR "DC390: Resel: Expect identify message!\n");
1618 lun &= 7;
1619 DEBUG0(printk ("(%02i-%i),", id, lun));
1620 pDCB = dc390_findDCB (pACB, id, lun);
1621 if (!pDCB)
1623 printk (KERN_ERR "DC390: Reselect from non existing device (%02i-%i)\n",
1624 id, lun);
1625 return;
1627 pACB->pActiveDCB = pDCB;
1628 /* TagQ: We expect a message soon, so never mind the exact SRB */
1629 if( pDCB->SyncMode & EN_TAG_QUEUEING )
1631 pSRB = pACB->pTmpSRB;
1632 pDCB->pActiveSRB = pSRB;
1634 else
1636 pSRB = pDCB->pActiveSRB;
1637 if( !pSRB || !(pSRB->SRBState & SRB_DISCONNECT) )
1639 pSRB= pACB->pTmpSRB;
1640 pSRB->SRBState = SRB_UNEXPECT_RESEL;
1641 printk (KERN_ERR "DC390: Reselect without outstanding cmnd (%02i-%i)\n",
1642 id, lun);
1643 pDCB->pActiveSRB = pSRB;
1644 dc390_EnableMsgOut_Abort ( pACB, pSRB );
1646 else
1648 if( pDCB->DCBFlag & ABORT_DEV_ )
1650 pSRB->SRBState = SRB_ABORT_SENT;
1651 printk (KERN_INFO "DC390: Reselect: Abort (%02i-%i)\n",
1652 id, lun);
1653 dc390_EnableMsgOut_Abort( pACB, pSRB );
1655 else
1656 pSRB->SRBState = SRB_DATA_XFER;
1660 DEBUG1(printk (KERN_DEBUG "Resel SRB(%p): TagNum (%02x)\n", pSRB, pSRB->TagNumber));
1661 pSRB->ScsiPhase = SCSI_NOP0;
1662 DC390_write8 (Scsi_Dest_ID, pDCB->TargetID);
1663 DC390_write8 (Sync_Period, pDCB->SyncPeriod);
1664 DC390_write8 (Sync_Offset, pDCB->SyncOffset);
1665 DC390_write8 (CtrlReg1, pDCB->CtrlR1);
1666 DC390_write8 (CtrlReg3, pDCB->CtrlR3);
1667 DC390_write8 (CtrlReg4, pDCB->CtrlR4); /* ; Glitch eater */
1668 DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD); /* ;to release the /ACK signal */
1671 static int __inline__
1672 dc390_RequestSense(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
1674 struct scsi_cmnd *pcmd;
1676 pcmd = pSRB->pcmd;
1678 REMOVABLEDEBUG(printk(KERN_INFO "DC390: RequestSense(Cmd %02x, Id %02x, LUN %02x)\n",\
1679 pcmd->cmnd[0], pDCB->TargetID, pDCB->TargetLUN));
1681 pSRB->SRBFlag |= AUTO_REQSENSE;
1682 pSRB->SavedTotXLen = pSRB->TotalXferredLen;
1683 pSRB->AdaptStatus = 0;
1684 pSRB->TargetStatus = 0; /* CHECK_CONDITION<<1; */
1686 /* We are called from SRBdone, original PCI mapping has been removed
1687 * already, new one is set up from StartSCSI */
1688 pSRB->SGIndex = 0;
1690 pSRB->TotalXferredLen = 0;
1691 pSRB->SGToBeXferLen = 0;
1692 return dc390_StartSCSI(pACB, pDCB, pSRB);
1696 static void
1697 dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
1699 u8 status;
1700 struct scsi_cmnd *pcmd;
1702 pcmd = pSRB->pcmd;
1703 /* KG: Moved pci_unmap here */
1704 dc390_pci_unmap(pSRB);
1706 status = pSRB->TargetStatus;
1708 DEBUG0(printk (" SRBdone (%02x,%08x), SRB %p, pid %li\n", status, pcmd->result,\
1709 pSRB, pcmd->serial_number));
1710 if(pSRB->SRBFlag & AUTO_REQSENSE)
1711 { /* Last command was a Request Sense */
1712 pSRB->SRBFlag &= ~AUTO_REQSENSE;
1713 pSRB->AdaptStatus = 0;
1714 pSRB->TargetStatus = SAM_STAT_CHECK_CONDITION;
1716 //pcmd->result = MK_RES(DRIVER_SENSE,DID_OK,0,status);
1717 if (status == SAM_STAT_CHECK_CONDITION)
1718 pcmd->result = MK_RES_LNX(0, DID_BAD_TARGET, 0, /*CHECK_CONDITION*/0);
1719 else /* Retry */
1721 if( pSRB->pcmd->cmnd[0] == TEST_UNIT_READY /* || pSRB->pcmd->cmnd[0] == START_STOP */)
1723 /* Don't retry on TEST_UNIT_READY */
1724 pcmd->result = MK_RES_LNX(DRIVER_SENSE, DID_OK, 0, SAM_STAT_CHECK_CONDITION);
1725 REMOVABLEDEBUG(printk(KERN_INFO "Cmd=%02x, Result=%08x, XferL=%08x\n",pSRB->pcmd->cmnd[0],\
1726 (u32) pcmd->result, (u32) pSRB->TotalXferredLen));
1727 } else {
1728 SET_RES_DRV(pcmd->result, DRIVER_SENSE);
1729 //pSRB->ScsiCmdLen = (u8) (pSRB->Segment1[0] >> 8);
1730 DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->serial_number, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
1731 pSRB->TotalXferredLen = 0;
1732 SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1735 goto cmd_done;
1737 if( status )
1739 if (status == SAM_STAT_CHECK_CONDITION)
1741 if (dc390_RequestSense(pACB, pDCB, pSRB)) {
1742 SET_RES_DID(pcmd->result, DID_ERROR);
1743 goto cmd_done;
1745 return;
1747 else if (status == SAM_STAT_TASK_SET_FULL)
1749 scsi_track_queue_full(pcmd->device, pDCB->GoingSRBCnt - 1);
1750 DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->serial_number, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
1751 pSRB->TotalXferredLen = 0;
1752 SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1754 else if (status == SAM_STAT_BUSY &&
1755 (pcmd->cmnd[0] == TEST_UNIT_READY || pcmd->cmnd[0] == INQUIRY) &&
1756 pACB->scan_devices)
1758 pSRB->AdaptStatus = 0;
1759 pSRB->TargetStatus = status;
1760 pcmd->result = MK_RES(0,0,pSRB->EndMessage,/*status*/0);
1762 else
1763 { /* Another error */
1764 pSRB->TotalXferredLen = 0;
1765 SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
1766 goto cmd_done;
1769 else
1770 { /* Target status == 0 */
1771 status = pSRB->AdaptStatus;
1772 if (status == H_OVER_UNDER_RUN)
1774 pSRB->TargetStatus = 0;
1775 SET_RES_DID(pcmd->result,DID_OK);
1776 SET_RES_MSG(pcmd->result,pSRB->EndMessage);
1778 else if (status == H_SEL_TIMEOUT)
1780 pcmd->result = MK_RES(0, DID_NO_CONNECT, 0, 0);
1781 /* Devices are removed below ... */
1783 else if( pSRB->SRBStatus & PARITY_ERROR)
1785 //pcmd->result = MK_RES(0,DID_PARITY,pSRB->EndMessage,0);
1786 SET_RES_DID(pcmd->result,DID_PARITY);
1787 SET_RES_MSG(pcmd->result,pSRB->EndMessage);
1789 else /* No error */
1791 pSRB->AdaptStatus = 0;
1792 pSRB->TargetStatus = 0;
1793 SET_RES_DID(pcmd->result,DID_OK);
1797 cmd_done:
1798 scsi_set_resid(pcmd, scsi_bufflen(pcmd) - pSRB->TotalXferredLen);
1800 dc390_Going_remove (pDCB, pSRB);
1801 /* Add to free list */
1802 dc390_Free_insert (pACB, pSRB);
1804 DEBUG0(printk (KERN_DEBUG "DC390: SRBdone: done pid %li\n", pcmd->serial_number));
1805 pcmd->scsi_done (pcmd);
1807 return;
1811 /* Remove all SRBs from Going list and inform midlevel */
1812 static void
1813 dc390_DoingSRB_Done(struct dc390_acb* pACB, struct scsi_cmnd *cmd)
1815 struct dc390_dcb *pDCB, *pdcb;
1816 struct dc390_srb *psrb, *psrb2;
1817 int i;
1818 struct scsi_cmnd *pcmd;
1820 pDCB = pACB->pLinkDCB;
1821 pdcb = pDCB;
1822 if (! pdcb) return;
1825 psrb = pdcb->pGoingSRB;
1826 for (i = 0; i < pdcb->GoingSRBCnt; i++)
1828 psrb2 = psrb->pNextSRB;
1829 pcmd = psrb->pcmd;
1830 dc390_Free_insert (pACB, psrb);
1831 psrb = psrb2;
1833 pdcb->GoingSRBCnt = 0;
1834 pdcb->pGoingSRB = NULL;
1835 pdcb->TagMask = 0;
1836 pdcb = pdcb->pNextDCB;
1837 } while( pdcb != pDCB );
1841 static void
1842 dc390_ResetSCSIBus( struct dc390_acb* pACB )
1844 //DC390_write8 (ScsiCmd, RST_DEVICE_CMD);
1845 //udelay (250);
1846 //DC390_write8 (ScsiCmd, NOP_CMD);
1848 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1849 DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1850 DC390_write8 (ScsiCmd, RST_SCSI_BUS_CMD);
1851 pACB->Connected = 0;
1853 return;
1856 static void
1857 dc390_ScsiRstDetect( struct dc390_acb* pACB )
1859 printk ("DC390: Rst_Detect: laststat = %08x\n", dc390_laststatus);
1860 //DEBUG0(printk(KERN_INFO "RST_DETECT,"));
1862 DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
1863 /* Unlock before ? */
1864 /* delay half a second */
1865 udelay (1000);
1866 DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
1867 pACB->pScsiHost->last_reset = jiffies + 5*HZ/2
1868 + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
1869 pACB->Connected = 0;
1871 if( pACB->ACBFlag & RESET_DEV )
1872 pACB->ACBFlag |= RESET_DONE;
1873 else
1874 { /* Reset was issued by sb else */
1875 pACB->ACBFlag |= RESET_DETECT;
1877 dc390_ResetDevParam( pACB );
1878 dc390_DoingSRB_Done( pACB, NULL);
1879 //dc390_RecoverSRB( pACB );
1880 pACB->pActiveDCB = NULL;
1881 pACB->ACBFlag = 0;
1883 return;
1886 static int DC390_queuecommand(struct scsi_cmnd *cmd,
1887 void (*done)(struct scsi_cmnd *))
1889 struct scsi_device *sdev = cmd->device;
1890 struct dc390_acb *acb = (struct dc390_acb *)sdev->host->hostdata;
1891 struct dc390_dcb *dcb = sdev->hostdata;
1892 struct dc390_srb *srb;
1894 if (sdev->queue_depth <= dcb->GoingSRBCnt)
1895 goto device_busy;
1896 if (acb->pActiveDCB)
1897 goto host_busy;
1898 if (acb->ACBFlag & (RESET_DETECT|RESET_DONE|RESET_DEV))
1899 goto host_busy;
1901 srb = acb->pFreeSRB;
1902 if (unlikely(srb == NULL))
1903 goto host_busy;
1905 cmd->scsi_done = done;
1906 cmd->result = 0;
1907 acb->Cmds++;
1909 acb->pFreeSRB = srb->pNextSRB;
1910 srb->pNextSRB = NULL;
1912 srb->pSRBDCB = dcb;
1913 srb->pcmd = cmd;
1914 cmd->host_scribble = (char *)srb;
1916 srb->SGIndex = 0;
1917 srb->AdaptStatus = 0;
1918 srb->TargetStatus = 0;
1919 srb->MsgCnt = 0;
1921 srb->SRBStatus = 0;
1922 srb->SRBFlag = 0;
1923 srb->SRBState = 0;
1924 srb->TotalXferredLen = 0;
1925 srb->SGBusAddr = 0;
1926 srb->SGToBeXferLen = 0;
1927 srb->ScsiPhase = 0;
1928 srb->EndMessage = 0;
1929 srb->TagNumber = SCSI_NO_TAG;
1931 if (dc390_StartSCSI(acb, dcb, srb)) {
1932 dc390_Free_insert(acb, srb);
1933 goto host_busy;
1936 dc390_Going_append(dcb, srb);
1938 return 0;
1940 host_busy:
1941 return SCSI_MLQUEUE_HOST_BUSY;
1943 device_busy:
1944 return SCSI_MLQUEUE_DEVICE_BUSY;
1947 static void dc390_dumpinfo (struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
1949 struct pci_dev *pdev;
1950 u16 pstat;
1952 if (!pDCB) pDCB = pACB->pActiveDCB;
1953 if (!pSRB && pDCB) pSRB = pDCB->pActiveSRB;
1955 if (pSRB)
1957 printk ("DC390: SRB: Xferred %08lx, Remain %08lx, State %08x, Phase %02x\n",
1958 pSRB->TotalXferredLen, pSRB->SGToBeXferLen, pSRB->SRBState,
1959 pSRB->ScsiPhase);
1960 printk ("DC390: AdpaterStatus: %02x, SRB Status %02x\n", pSRB->AdaptStatus, pSRB->SRBStatus);
1962 printk ("DC390: Status of last IRQ (DMA/SC/Int/IRQ): %08x\n", dc390_laststatus);
1963 printk ("DC390: Register dump: SCSI block:\n");
1964 printk ("DC390: XferCnt Cmd Stat IntS IRQS FFIS Ctl1 Ctl2 Ctl3 Ctl4\n");
1965 printk ("DC390: %06x %02x %02x %02x",
1966 DC390_read8(CtcReg_Low) + (DC390_read8(CtcReg_Mid) << 8) + (DC390_read8(CtcReg_High) << 16),
1967 DC390_read8(ScsiCmd), DC390_read8(Scsi_Status), DC390_read8(Intern_State));
1968 printk (" %02x %02x %02x %02x %02x %02x\n",
1969 DC390_read8(INT_Status), DC390_read8(Current_Fifo), DC390_read8(CtrlReg1),
1970 DC390_read8(CtrlReg2), DC390_read8(CtrlReg3), DC390_read8(CtrlReg4));
1971 DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT);
1972 if (DC390_read8(Current_Fifo) & 0x1f)
1974 printk ("DC390: FIFO:");
1975 while (DC390_read8(Current_Fifo) & 0x1f) printk (" %02x", DC390_read8(ScsiFifo));
1976 printk ("\n");
1978 printk ("DC390: Register dump: DMA engine:\n");
1979 printk ("DC390: Cmd STrCnt SBusA WrkBC WrkAC Stat SBusCtrl\n");
1980 printk ("DC390: %02x %08x %08x %08x %08x %02x %08x\n",
1981 DC390_read8(DMA_Cmd), DC390_read32(DMA_XferCnt), DC390_read32(DMA_XferAddr),
1982 DC390_read32(DMA_Wk_ByteCntr), DC390_read32(DMA_Wk_AddrCntr),
1983 DC390_read8(DMA_Status), DC390_read32(DMA_ScsiBusCtrl));
1984 DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
1986 pdev = pACB->pdev;
1987 pci_read_config_word(pdev, PCI_STATUS, &pstat);
1988 printk ("DC390: Register dump: PCI Status: %04x\n", pstat);
1989 printk ("DC390: In case of driver trouble read Documentation/scsi/tmscsim.txt\n");
1993 static int DC390_abort(struct scsi_cmnd *cmd)
1995 struct dc390_acb *pACB = (struct dc390_acb*) cmd->device->host->hostdata;
1996 struct dc390_dcb *pDCB = (struct dc390_dcb*) cmd->device->hostdata;
1998 scmd_printk(KERN_WARNING, cmd,
1999 "DC390: Abort command (pid %li)\n", cmd->serial_number);
2001 /* abort() is too stupid for already sent commands at the moment.
2002 * If it's called we are in trouble anyway, so let's dump some info
2003 * into the syslog at least. (KG, 98/08/20,99/06/20) */
2004 dc390_dumpinfo(pACB, pDCB, NULL);
2006 pDCB->DCBFlag |= ABORT_DEV_;
2007 printk(KERN_INFO "DC390: Aborted pid %li\n", cmd->serial_number);
2009 return FAILED;
2013 static void dc390_ResetDevParam( struct dc390_acb* pACB )
2015 struct dc390_dcb *pDCB, *pdcb;
2017 pDCB = pACB->pLinkDCB;
2018 if (! pDCB) return;
2019 pdcb = pDCB;
2022 pDCB->SyncMode &= ~SYNC_NEGO_DONE;
2023 pDCB->SyncPeriod = 0;
2024 pDCB->SyncOffset = 0;
2025 pDCB->TagMask = 0;
2026 pDCB->CtrlR3 = FAST_CLK;
2027 pDCB->CtrlR4 &= NEGATE_REQACKDATA | CTRL4_RESERVED | NEGATE_REQACK;
2028 pDCB->CtrlR4 |= pACB->glitch_cfg;
2029 pDCB = pDCB->pNextDCB;
2031 while( pdcb != pDCB );
2032 pACB->ACBFlag &= ~(RESET_DEV | RESET_DONE | RESET_DETECT);
2036 static int DC390_bus_reset (struct scsi_cmnd *cmd)
2038 struct dc390_acb* pACB = (struct dc390_acb*) cmd->device->host->hostdata;
2039 u8 bval;
2041 spin_lock_irq(cmd->device->host->host_lock);
2043 bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
2044 DC390_write8(CtrlReg1, bval); /* disable IRQ on bus reset */
2046 pACB->ACBFlag |= RESET_DEV;
2047 dc390_ResetSCSIBus(pACB);
2049 dc390_ResetDevParam(pACB);
2050 mdelay(1);
2051 pACB->pScsiHost->last_reset = jiffies + 3*HZ/2
2052 + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
2054 DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
2055 DC390_read8(INT_Status); /* Reset Pending INT */
2057 dc390_DoingSRB_Done(pACB, cmd);
2059 pACB->pActiveDCB = NULL;
2060 pACB->ACBFlag = 0;
2062 bval = DC390_read8(CtrlReg1) & ~DIS_INT_ON_SCSI_RST;
2063 DC390_write8(CtrlReg1, bval); /* re-enable interrupt */
2065 spin_unlock_irq(cmd->device->host->host_lock);
2067 return SUCCESS;
2071 * dc390_slave_alloc - Called by the scsi mid layer to tell us about a new
2072 * scsi device that we need to deal with.
2074 * @scsi_device: The new scsi device that we need to handle.
2076 static int dc390_slave_alloc(struct scsi_device *scsi_device)
2078 struct dc390_acb *pACB = (struct dc390_acb*) scsi_device->host->hostdata;
2079 struct dc390_dcb *pDCB, *pDCB2 = NULL;
2080 uint id = scsi_device->id;
2081 uint lun = scsi_device->lun;
2083 pDCB = kzalloc(sizeof(struct dc390_dcb), GFP_KERNEL);
2084 if (!pDCB)
2085 return -ENOMEM;
2087 if (!pACB->DCBCnt++) {
2088 pACB->pLinkDCB = pDCB;
2089 pACB->pDCBRunRobin = pDCB;
2090 } else {
2091 pACB->pLastDCB->pNextDCB = pDCB;
2094 pDCB->pNextDCB = pACB->pLinkDCB;
2095 pACB->pLastDCB = pDCB;
2097 pDCB->pDCBACB = pACB;
2098 pDCB->TargetID = id;
2099 pDCB->TargetLUN = lun;
2102 * Some values are for all LUNs: Copy them
2103 * In a clean way: We would have an own structure for a SCSI-ID
2105 if (lun && (pDCB2 = dc390_findDCB(pACB, id, 0))) {
2106 pDCB->DevMode = pDCB2->DevMode;
2107 pDCB->SyncMode = pDCB2->SyncMode & SYNC_NEGO_DONE;
2108 pDCB->SyncPeriod = pDCB2->SyncPeriod;
2109 pDCB->SyncOffset = pDCB2->SyncOffset;
2110 pDCB->NegoPeriod = pDCB2->NegoPeriod;
2112 pDCB->CtrlR3 = pDCB2->CtrlR3;
2113 pDCB->CtrlR4 = pDCB2->CtrlR4;
2114 } else {
2115 u8 index = pACB->AdapterIndex;
2116 PEEprom prom = (PEEprom) &dc390_eepromBuf[index][id << 2];
2118 pDCB->DevMode = prom->EE_MODE1;
2119 pDCB->NegoPeriod =
2120 (dc390_clock_period1[prom->EE_SPEED] * 25) >> 2;
2121 pDCB->CtrlR3 = FAST_CLK;
2122 pDCB->CtrlR4 = pACB->glitch_cfg | CTRL4_RESERVED;
2123 if (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION)
2124 pDCB->CtrlR4 |= NEGATE_REQACKDATA | NEGATE_REQACK;
2127 if (pDCB->DevMode & SYNC_NEGO_)
2128 pDCB->SyncMode |= SYNC_ENABLE;
2129 else {
2130 pDCB->SyncMode = 0;
2131 pDCB->SyncOffset &= ~0x0f;
2134 pDCB->CtrlR1 = pACB->pScsiHost->this_id;
2135 if (pDCB->DevMode & PARITY_CHK_)
2136 pDCB->CtrlR1 |= PARITY_ERR_REPO;
2138 pACB->scan_devices = 1;
2139 scsi_device->hostdata = pDCB;
2140 return 0;
2144 * dc390_slave_destroy - Called by the scsi mid layer to tell us about a
2145 * device that is going away.
2147 * @scsi_device: The scsi device that we need to remove.
2149 static void dc390_slave_destroy(struct scsi_device *scsi_device)
2151 struct dc390_acb* pACB = (struct dc390_acb*) scsi_device->host->hostdata;
2152 struct dc390_dcb* pDCB = (struct dc390_dcb*) scsi_device->hostdata;
2153 struct dc390_dcb* pPrevDCB = pACB->pLinkDCB;
2155 pACB->scan_devices = 0;
2157 BUG_ON(pDCB->GoingSRBCnt > 1);
2159 if (pDCB == pACB->pLinkDCB) {
2160 if (pACB->pLastDCB == pDCB) {
2161 pDCB->pNextDCB = NULL;
2162 pACB->pLastDCB = NULL;
2164 pACB->pLinkDCB = pDCB->pNextDCB;
2165 } else {
2166 while (pPrevDCB->pNextDCB != pDCB)
2167 pPrevDCB = pPrevDCB->pNextDCB;
2168 pPrevDCB->pNextDCB = pDCB->pNextDCB;
2169 if (pDCB == pACB->pLastDCB)
2170 pACB->pLastDCB = pPrevDCB;
2173 if (pDCB == pACB->pActiveDCB)
2174 pACB->pActiveDCB = NULL;
2175 if (pDCB == pACB->pLinkDCB)
2176 pACB->pLinkDCB = pDCB->pNextDCB;
2177 if (pDCB == pACB->pDCBRunRobin)
2178 pACB->pDCBRunRobin = pDCB->pNextDCB;
2179 kfree(pDCB);
2181 pACB->DCBCnt--;
2184 static int dc390_slave_configure(struct scsi_device *sdev)
2186 struct dc390_acb *acb = (struct dc390_acb *)sdev->host->hostdata;
2187 struct dc390_dcb *dcb = (struct dc390_dcb *)sdev->hostdata;
2189 acb->scan_devices = 0;
2190 if (sdev->tagged_supported && (dcb->DevMode & TAG_QUEUEING_)) {
2191 dcb->SyncMode |= EN_TAG_QUEUEING;
2192 scsi_activate_tcq(sdev, acb->TagMaxNum);
2195 return 0;
2198 static struct scsi_host_template driver_template = {
2199 .module = THIS_MODULE,
2200 .proc_name = "tmscsim",
2201 .name = DC390_BANNER " V" DC390_VERSION,
2202 .slave_alloc = dc390_slave_alloc,
2203 .slave_configure = dc390_slave_configure,
2204 .slave_destroy = dc390_slave_destroy,
2205 .queuecommand = DC390_queuecommand,
2206 .eh_abort_handler = DC390_abort,
2207 .eh_bus_reset_handler = DC390_bus_reset,
2208 .can_queue = 1,
2209 .this_id = 7,
2210 .sg_tablesize = SG_ALL,
2211 .cmd_per_lun = 1,
2212 .use_clustering = ENABLE_CLUSTERING,
2213 .max_sectors = 0x4000, /* 8MiB = 16 * 1024 * 512 */
2216 /***********************************************************************
2217 * Functions for access to DC390 EEPROM
2218 * and some to emulate it
2220 **********************************************************************/
2222 static void __devinit dc390_eeprom_prepare_read(struct pci_dev *pdev, u8 cmd)
2224 u8 carryFlag = 1, j = 0x80, bval;
2225 int i;
2227 for (i = 0; i < 9; i++) {
2228 if (carryFlag) {
2229 pci_write_config_byte(pdev, 0x80, 0x40);
2230 bval = 0xc0;
2231 } else
2232 bval = 0x80;
2234 udelay(160);
2235 pci_write_config_byte(pdev, 0x80, bval);
2236 udelay(160);
2237 pci_write_config_byte(pdev, 0x80, 0);
2238 udelay(160);
2240 carryFlag = (cmd & j) ? 1 : 0;
2241 j >>= 1;
2245 static u16 __devinit dc390_eeprom_get_data(struct pci_dev *pdev)
2247 int i;
2248 u16 wval = 0;
2249 u8 bval;
2251 for (i = 0; i < 16; i++) {
2252 wval <<= 1;
2254 pci_write_config_byte(pdev, 0x80, 0x80);
2255 udelay(160);
2256 pci_write_config_byte(pdev, 0x80, 0x40);
2257 udelay(160);
2258 pci_read_config_byte(pdev, 0x00, &bval);
2260 if (bval == 0x22)
2261 wval |= 1;
2264 return wval;
2267 static void __devinit dc390_read_eeprom(struct pci_dev *pdev, u16 *ptr)
2269 u8 cmd = EEPROM_READ, i;
2271 for (i = 0; i < 0x40; i++) {
2272 pci_write_config_byte(pdev, 0xc0, 0);
2273 udelay(160);
2275 dc390_eeprom_prepare_read(pdev, cmd++);
2276 *ptr++ = dc390_eeprom_get_data(pdev);
2278 pci_write_config_byte(pdev, 0x80, 0);
2279 pci_write_config_byte(pdev, 0x80, 0);
2280 udelay(160);
2284 /* Override EEprom values with explicitly set values */
2285 static void __devinit dc390_eeprom_override(u8 index)
2287 u8 *ptr = (u8 *) dc390_eepromBuf[index], id;
2289 /* Adapter Settings */
2290 if (tmscsim[0] != -2)
2291 ptr[EE_ADAPT_SCSI_ID] = (u8)tmscsim[0]; /* Adapter ID */
2292 if (tmscsim[3] != -2)
2293 ptr[EE_MODE2] = (u8)tmscsim[3];
2294 if (tmscsim[5] != -2)
2295 ptr[EE_DELAY] = tmscsim[5]; /* Reset delay */
2296 if (tmscsim[4] != -2)
2297 ptr[EE_TAG_CMD_NUM] = (u8)tmscsim[4]; /* Tagged Cmds */
2299 /* Device Settings */
2300 for (id = 0; id < MAX_SCSI_ID; id++) {
2301 if (tmscsim[2] != -2)
2302 ptr[id << 2] = (u8)tmscsim[2]; /* EE_MODE1 */
2303 if (tmscsim[1] != -2)
2304 ptr[(id << 2) + 1] = (u8)tmscsim[1]; /* EE_Speed */
2308 static int __devinitdata tmscsim_def[] = {
2310 0 /* 10MHz */,
2311 PARITY_CHK_ | SEND_START_ | EN_DISCONNECT_ | SYNC_NEGO_ | TAG_QUEUEING_,
2312 MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION | LUN_CHECK,
2313 3 /* 16 Tags per LUN */,
2314 1 /* s delay after Reset */,
2317 /* Copy defaults over set values where missing */
2318 static void __devinit dc390_fill_with_defaults (void)
2320 int i;
2322 for (i = 0; i < 6; i++) {
2323 if (tmscsim[i] < 0 || tmscsim[i] > 255)
2324 tmscsim[i] = tmscsim_def[i];
2327 /* Sanity checks */
2328 if (tmscsim[0] > 7)
2329 tmscsim[0] = 7;
2330 if (tmscsim[1] > 7)
2331 tmscsim[1] = 4;
2332 if (tmscsim[4] > 5)
2333 tmscsim[4] = 4;
2334 if (tmscsim[5] > 180)
2335 tmscsim[5] = 180;
2338 static void __devinit dc390_check_eeprom(struct pci_dev *pdev, u8 index)
2340 u8 interpd[] = {1, 3, 5, 10, 16, 30, 60, 120};
2341 u8 EEbuf[128];
2342 u16 *ptr = (u16 *)EEbuf, wval = 0;
2343 int i;
2345 dc390_read_eeprom(pdev, ptr);
2346 memcpy(dc390_eepromBuf[index], EEbuf, EE_ADAPT_SCSI_ID);
2347 memcpy(&dc390_eepromBuf[index][EE_ADAPT_SCSI_ID],
2348 &EEbuf[REAL_EE_ADAPT_SCSI_ID], EE_LEN - EE_ADAPT_SCSI_ID);
2350 dc390_eepromBuf[index][EE_DELAY] = interpd[dc390_eepromBuf[index][EE_DELAY]];
2352 for (i = 0; i < 0x40; i++, ptr++)
2353 wval += *ptr;
2355 /* no Tekram EEprom found */
2356 if (wval != 0x1234) {
2357 int speed;
2359 printk(KERN_INFO "DC390_init: No EEPROM found! Trying default settings ...\n");
2362 * XXX(hch): bogus, because we might have tekram and
2363 * non-tekram hbas in a single machine.
2365 dc390_fill_with_defaults();
2367 speed = dc390_clock_speed[tmscsim[1]];
2368 printk(KERN_INFO "DC390: Used defaults: AdaptID=%i, SpeedIdx=%i (%i.%i MHz), "
2369 "DevMode=0x%02x, AdaptMode=0x%02x, TaggedCmnds=%i (%i), DelayReset=%is\n",
2370 tmscsim[0], tmscsim[1], speed / 10, speed % 10,
2371 (u8)tmscsim[2], (u8)tmscsim[3], tmscsim[4], 2 << (tmscsim[4]), tmscsim[5]);
2375 static void __devinit dc390_init_hw(struct dc390_acb *pACB, u8 index)
2377 struct Scsi_Host *shost = pACB->pScsiHost;
2378 u8 dstate;
2380 /* Disable SCSI bus reset interrupt */
2381 DC390_write8(CtrlReg1, DIS_INT_ON_SCSI_RST | shost->this_id);
2383 if (pACB->Gmode2 & RST_SCSI_BUS) {
2384 dc390_ResetSCSIBus(pACB);
2385 udelay(1000);
2386 shost->last_reset = jiffies + HZ/2 +
2387 HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
2390 pACB->ACBFlag = 0;
2392 /* Reset Pending INT */
2393 DC390_read8(INT_Status);
2395 /* 250ms selection timeout */
2396 DC390_write8(Scsi_TimeOut, SEL_TIMEOUT);
2398 /* Conversion factor = 0 , 40MHz clock */
2399 DC390_write8(Clk_Factor, CLK_FREQ_40MHZ);
2401 /* NOP cmd - clear command register */
2402 DC390_write8(ScsiCmd, NOP_CMD);
2404 /* Enable Feature and SCSI-2 */
2405 DC390_write8(CtrlReg2, EN_FEATURE+EN_SCSI2_CMD);
2407 /* Fast clock */
2408 DC390_write8(CtrlReg3, FAST_CLK);
2410 /* Negation */
2411 DC390_write8(CtrlReg4, pACB->glitch_cfg | /* glitch eater */
2412 (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION) ?
2413 NEGATE_REQACKDATA : 0);
2415 /* Clear Transfer Count High: ID */
2416 DC390_write8(CtcReg_High, 0);
2417 DC390_write8(DMA_Cmd, DMA_IDLE_CMD);
2418 DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
2419 DC390_write32(DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
2421 dstate = DC390_read8(DMA_Status);
2422 DC390_write8(DMA_Status, dstate);
2425 static int __devinit dc390_probe_one(struct pci_dev *pdev,
2426 const struct pci_device_id *id)
2428 struct dc390_acb *pACB;
2429 struct Scsi_Host *shost;
2430 unsigned long io_port;
2431 int error = -ENODEV, i;
2433 if (pci_enable_device(pdev))
2434 goto out;
2436 pci_set_master(pdev);
2438 error = -ENOMEM;
2439 if (disable_clustering)
2440 driver_template.use_clustering = DISABLE_CLUSTERING;
2441 shost = scsi_host_alloc(&driver_template, sizeof(struct dc390_acb));
2442 if (!shost)
2443 goto out_disable_device;
2445 pACB = (struct dc390_acb *)shost->hostdata;
2446 memset(pACB, 0, sizeof(struct dc390_acb));
2448 dc390_check_eeprom(pdev, dc390_adapterCnt);
2449 dc390_eeprom_override(dc390_adapterCnt);
2451 io_port = pci_resource_start(pdev, 0);
2453 shost->this_id = dc390_eepromBuf[dc390_adapterCnt][EE_ADAPT_SCSI_ID];
2454 shost->io_port = io_port;
2455 shost->n_io_port = 0x80;
2456 shost->irq = pdev->irq;
2457 shost->base = io_port;
2458 shost->unique_id = io_port;
2459 shost->last_reset = jiffies;
2461 pACB->pScsiHost = shost;
2462 pACB->IOPortBase = (u16) io_port;
2463 pACB->IRQLevel = pdev->irq;
2465 shost->max_id = 8;
2467 if (shost->max_id - 1 ==
2468 dc390_eepromBuf[dc390_adapterCnt][EE_ADAPT_SCSI_ID])
2469 shost->max_id--;
2471 if (dc390_eepromBuf[dc390_adapterCnt][EE_MODE2] & LUN_CHECK)
2472 shost->max_lun = 8;
2473 else
2474 shost->max_lun = 1;
2476 pACB->pFreeSRB = pACB->SRB_array;
2477 pACB->SRBCount = MAX_SRB_CNT;
2478 pACB->AdapterIndex = dc390_adapterCnt;
2479 pACB->TagMaxNum =
2480 2 << dc390_eepromBuf[dc390_adapterCnt][EE_TAG_CMD_NUM];
2481 pACB->Gmode2 = dc390_eepromBuf[dc390_adapterCnt][EE_MODE2];
2483 for (i = 0; i < pACB->SRBCount-1; i++)
2484 pACB->SRB_array[i].pNextSRB = &pACB->SRB_array[i+1];
2485 pACB->SRB_array[pACB->SRBCount-1].pNextSRB = NULL;
2486 pACB->pTmpSRB = &pACB->TmpSRB;
2488 pACB->sel_timeout = SEL_TIMEOUT;
2489 pACB->glitch_cfg = EATER_25NS;
2490 pACB->pdev = pdev;
2492 if (!request_region(io_port, shost->n_io_port, "tmscsim")) {
2493 printk(KERN_ERR "DC390: register IO ports error!\n");
2494 goto out_host_put;
2497 /* Reset Pending INT */
2498 DC390_read8_(INT_Status, io_port);
2500 if (request_irq(pdev->irq, do_DC390_Interrupt, IRQF_SHARED,
2501 "tmscsim", pACB)) {
2502 printk(KERN_ERR "DC390: register IRQ error!\n");
2503 goto out_release_region;
2506 dc390_init_hw(pACB, dc390_adapterCnt);
2508 dc390_adapterCnt++;
2510 pci_set_drvdata(pdev, shost);
2512 error = scsi_add_host(shost, &pdev->dev);
2513 if (error)
2514 goto out_free_irq;
2515 scsi_scan_host(shost);
2516 return 0;
2518 out_free_irq:
2519 free_irq(pdev->irq, pACB);
2520 out_release_region:
2521 release_region(io_port, shost->n_io_port);
2522 out_host_put:
2523 scsi_host_put(shost);
2524 out_disable_device:
2525 pci_disable_device(pdev);
2526 out:
2527 return error;
2531 * dc390_remove_one - Called to remove a single instance of the adapter.
2533 * @dev: The PCI device to remove.
2535 static void __devexit dc390_remove_one(struct pci_dev *dev)
2537 struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
2538 unsigned long iflags;
2539 struct dc390_acb* pACB = (struct dc390_acb*) scsi_host->hostdata;
2540 u8 bval;
2542 scsi_remove_host(scsi_host);
2544 spin_lock_irqsave(scsi_host->host_lock, iflags);
2545 pACB->ACBFlag = RESET_DEV;
2546 bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
2547 DC390_write8 (CtrlReg1, bval); /* disable interrupt */
2548 if (pACB->Gmode2 & RST_SCSI_BUS)
2549 dc390_ResetSCSIBus(pACB);
2550 spin_unlock_irqrestore(scsi_host->host_lock, iflags);
2552 free_irq(scsi_host->irq, pACB);
2553 release_region(scsi_host->io_port, scsi_host->n_io_port);
2555 pci_disable_device(dev);
2556 scsi_host_put(scsi_host);
2557 pci_set_drvdata(dev, NULL);
2560 static struct pci_device_id tmscsim_pci_tbl[] = {
2561 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD53C974,
2562 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
2565 MODULE_DEVICE_TABLE(pci, tmscsim_pci_tbl);
2567 static struct pci_driver dc390_driver = {
2568 .name = "tmscsim",
2569 .id_table = tmscsim_pci_tbl,
2570 .probe = dc390_probe_one,
2571 .remove = __devexit_p(dc390_remove_one),
2574 static int __init dc390_module_init(void)
2576 if (!disable_clustering) {
2577 printk(KERN_INFO "DC390: clustering now enabled by default. If you get problems load\n");
2578 printk(KERN_INFO " with \"disable_clustering=1\" and report to maintainers\n");
2581 if (tmscsim[0] == -1 || tmscsim[0] > 15) {
2582 tmscsim[0] = 7;
2583 tmscsim[1] = 4;
2584 tmscsim[2] = PARITY_CHK_ | TAG_QUEUEING_;
2585 tmscsim[3] = MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION;
2586 tmscsim[4] = 2;
2587 tmscsim[5] = 10;
2588 printk (KERN_INFO "DC390: Using safe settings.\n");
2591 return pci_register_driver(&dc390_driver);
2594 static void __exit dc390_module_exit(void)
2596 pci_unregister_driver(&dc390_driver);
2599 module_init(dc390_module_init);
2600 module_exit(dc390_module_exit);
2602 #ifndef MODULE
2603 static int __init dc390_setup (char *str)
2605 int ints[8],i, im;
2607 get_options(str, ARRAY_SIZE(ints), ints);
2608 im = ints[0];
2610 if (im > 6) {
2611 printk (KERN_NOTICE "DC390: ignore extra params!\n");
2612 im = 6;
2615 for (i = 0; i < im; i++)
2616 tmscsim[i] = ints[i+1];
2617 /* dc390_checkparams (); */
2618 return 1;
2621 __setup("tmscsim=", dc390_setup);
2622 #endif