File: /usr/src/linux-2.4.7/drivers/scsi/53c7,8xx.c

1     /*
2      * PERM_OPTIONS are driver options which will be enabled for all NCR boards
3      * in the system at driver initialization time.
4      *
5      * Don't THINK about touching these in PERM_OPTIONS : 
6      *   OPTION_IO_MAPPED 
7      * 	Memory mapped IO does not work under i86 Linux. 
8      *
9      *   OPTION_DEBUG_TEST1
10      *	Test 1 does bus mastering and interrupt tests, which will help weed 
11      *	out brain damaged main boards.
12      *
13      * These are development kernel changes.  Code for them included in this
14      * driver release may or may not work.  If you turn them on, you should be 
15      * running the latest copy of the development sources from
16      *
17      *	ftp://tsx-11.mit.edu/pub/linux/ALPHA/scsi/53c7,8xx
18      *
19      * and be subscribed to the ncr53c810@colorado.edu mailing list.  To
20      * subscribe, send mail to majordomo@colorado.edu with 
21      *
22      * 	subscribe ncr53c810
23      * 
24      * in the text.
25      *
26      *
27      *   OPTION_NO_ASYNC
28      *	Don't negotiate for asynchronous transfers on the first command 
29      *	when OPTION_ALWAYS_SYNCHRONOUS is set.  Useful for dain bramaged
30      *	devices which do something bad rather than sending a MESSAGE 
31      *	REJECT back to us like they should if they can't cope.
32      *
33      *   OPTION_SYNCHRONOUS
34      *	Enable support for synchronous transfers.  Target negotiated 
35      *	synchronous transfers will be responded to.  To initiate 
36      *	a synchronous transfer request,  call 
37      *
38      *	    request_synchronous (hostno, target) 
39      *
40      *	from within KGDB.
41      *
42      *   OPTION_ALWAYS_SYNCHRONOUS
43      *	Negotiate for synchronous transfers with every target after
44      *	driver initialization or a SCSI bus reset.  This is a bit dangerous, 
45      *	since there are some dain bramaged SCSI devices which will accept
46      *	SDTR messages but keep talking asynchronously.
47      *
48      *   OPTION_DISCONNECT
49      *	Enable support for disconnect/reconnect.  To change the 
50      *	default setting on a given host adapter, call
51      *
52      *	    request_disconnect (hostno, allow)
53      *
54      *	where allow is non-zero to allow, 0 to disallow.
55      * 
56      *  If you really want to run 10MHz FAST SCSI-II transfers, you should 
57      *  know that the NCR driver currently ignores parity information.  Most
58      *  systems do 5MHz SCSI fine.  I've seen a lot that have problems faster
59      *  than 8MHz.  To play it safe, we only request 5MHz transfers.
60      *
61      *  If you'd rather get 10MHz transfers, edit sdtr_message and change 
62      *  the fourth byte from 50 to 25.
63      */
64     
65     #include <linux/config.h>
66     
67     #ifdef CONFIG_SCSI_NCR53C7xx_sync
68     #ifdef CONFIG_SCSI_NCR53C7xx_DISCONNECT
69     #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|OPTION_DISCONNECT|\
70     	OPTION_SYNCHRONOUS|OPTION_ALWAYS_SYNCHRONOUS)
71     #else
72     #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|\
73     	OPTION_SYNCHRONOUS|OPTION_ALWAYS_SYNCHRONOUS)
74     #endif
75     #else
76     #ifdef CONFIG_SCSI_NCR53C7xx_DISCONNECT
77     #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|OPTION_DISCONNECT|\
78     	OPTION_SYNCHRONOUS)
79     #else
80     #define PERM_OPTIONS (OPTION_IO_MAPPED|OPTION_DEBUG_TEST1|OPTION_SYNCHRONOUS)
81     #endif
82     #endif
83     
84     /*
85      * Sponsored by 
86      *	iX Multiuser Multitasking Magazine
87      *	Hannover, Germany
88      *	hm@ix.de
89      *
90      * Copyright 1993, 1994, 1995 Drew Eckhardt
91      *      Visionary Computing 
92      *      (Unix and Linux consulting and custom programming)
93      *      drew@PoohSticks.ORG
94      *	+1 (303) 786-7975
95      *
96      * TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
97      * 
98      * For more information, please consult 
99      *
100      * NCR53C810 
101      * SCSI I/O Processor
102      * Programmer's Guide
103      *
104      * NCR 53C810
105      * PCI-SCSI I/O Processor
106      * Data Manual
107      *
108      * NCR 53C810/53C820
109      * PCI-SCSI I/O Processor Design In Guide
110      *
111      * For literature on Symbios Logic Inc. formerly NCR, SCSI, 
112      * and Communication products please call (800) 334-5454 or
113      * (719) 536-3300. 
114      * 
115      * PCI BIOS Specification Revision
116      * PCI Local Bus Specification
117      * PCI System Design Guide
118      *
119      * PCI Special Interest Group
120      * M/S HF3-15A
121      * 5200 N.E. Elam Young Parkway
122      * Hillsboro, Oregon 97124-6497
123      * +1 (503) 696-2000 
124      * +1 (800) 433-5177
125      */
126     
127     /*
128      * Design issues : 
129      * The cumulative latency needed to propagate a read/write request 
130      * through the file system, buffer cache, driver stacks, SCSI host, and 
131      * SCSI device is ultimately the limiting factor in throughput once we 
132      * have a sufficiently fast host adapter.
133      *  
134      * So, to maximize performance we want to keep the ratio of latency to data 
135      * transfer time to a minimum by
136      * 1.  Minimizing the total number of commands sent (typical command latency
137      *	including drive and bus mastering host overhead is as high as 4.5ms)
138      *	to transfer a given amount of data.  
139      *
140      *      This is accomplished by placing no arbitrary limit on the number
141      *	of scatter/gather buffers supported, since we can transfer 1K
142      *	per scatter/gather buffer without Eric's cluster patches, 
143      *	4K with.  
144      *
145      * 2.  Minimizing the number of fatal interrupts serviced, since
146      * 	fatal interrupts halt the SCSI I/O processor.  Basically,
147      *	this means offloading the practical maximum amount of processing 
148      *	to the SCSI chip.
149      * 
150      *	On the NCR53c810/820/720,  this is accomplished by using 
151      *		interrupt-on-the-fly signals when commands complete, 
152      *		and only handling fatal errors and SDTR / WDTR 	messages 
153      *		in the host code.
154      *
155      *	On the NCR53c710, interrupts are generated as on the NCR53c8x0,
156      *		only the lack of an interrupt-on-the-fly facility complicates
157      *		things.   Also, SCSI ID registers and commands are 
158      *		bit fielded rather than binary encoded.
159      *		
160      * 	On the NCR53c700 and NCR53c700-66, operations that are done via 
161      *		indirect, table mode on the more advanced chips must be
162      *	        replaced by calls through a jump table which 
163      *		acts as a surrogate for the DSA.  Unfortunately, this 
164      * 		will mean that we must service an interrupt for each 
165      *		disconnect/reconnect.
166      * 
167      * 3.  Eliminating latency by pipelining operations at the different levels.
168      * 	
169      *	This driver allows a configurable number of commands to be enqueued
170      *	for each target/lun combination (experimentally, I have discovered
171      *	that two seems to work best) and will ultimately allow for 
172      *	SCSI-II tagged queuing.
173      * 	
174      *
175      * Architecture : 
176      * This driver is built around a Linux queue of commands waiting to 
177      * be executed, and a shared Linux/NCR array of commands to start.  Commands
178      * are transferred to the array  by the run_process_issue_queue() function 
179      * which is called whenever a command completes.
180      *
181      * As commands are completed, the interrupt routine is triggered,
182      * looks for commands in the linked list of completed commands with
183      * valid status, removes these commands from a list of running commands, 
184      * calls the done routine, and flags their target/luns as not busy.
185      *
186      * Due to limitations in the intelligence of the NCR chips, certain
187      * concessions are made.  In many cases, it is easier to dynamically 
188      * generate/fix-up code rather than calculate on the NCR at run time.  
189      * So, code is generated or fixed up for
190      *
191      * - Handling data transfers, using a variable number of MOVE instructions
192      *	interspersed with CALL MSG_IN, WHEN MSGIN instructions.
193      *
194      * 	The DATAIN and DATAOUT routines	are separate, so that an incorrect
195      *	direction can be trapped, and space isn't wasted. 
196      *
197      *	It may turn out that we're better off using some sort 
198      *	of table indirect instruction in a loop with a variable
199      *	sized table on the NCR53c710 and newer chips.
200      *
201      * - Checking for reselection (NCR53c710 and better)
202      *
203      * - Handling the details of SCSI context switches (NCR53c710 and better),
204      *	such as reprogramming appropriate synchronous parameters, 
205      *	removing the dsa structure from the NCR's queue of outstanding
206      *	commands, etc.
207      *
208      */
209     
210     /* 
211      * Accommodate differences between stock 1.2.x and 1.3.x asm-i386/types.h
212      * so lusers can drop in 53c7,8xx.* and get something which compiles 
213      * without warnings.
214      */
215     
216     #include <linux/version.h>
217     
218     #ifdef MODULE
219     #include <linux/module.h>
220     #endif
221     
222     #include <asm/dma.h>
223     #include <asm/io.h>
224     #include <asm/system.h>
225     #include <linux/delay.h>
226     #include <linux/signal.h>
227     #include <linux/sched.h>
228     #include <linux/errno.h>
229     #include <linux/pci.h>
230     #include <linux/proc_fs.h>
231     #include <linux/string.h>
232     #include <linux/slab.h>
233     #include <linux/vmalloc.h>
234     #include <linux/mm.h>
235     #include <linux/ioport.h>
236     #include <linux/time.h>
237     #include <linux/blk.h>
238     #include <linux/init.h>
239     #include <linux/spinlock.h>
240     
241     #include "scsi.h"
242     #include "hosts.h"
243     #include "53c7,8xx.h"
244     #include "constants.h"
245     #include "sd.h"
246     #include <linux/stat.h>
247     #include <linux/stddef.h>
248     
249     static int check_address (unsigned long addr, int size);
250     static void dump_events (struct Scsi_Host *host, int count);
251     static Scsi_Cmnd * return_outstanding_commands (struct Scsi_Host *host, 
252         int free, int issue);
253     static void hard_reset (struct Scsi_Host *host);
254     static void ncr_scsi_reset (struct Scsi_Host *host);
255     static void print_lots (struct Scsi_Host *host);
256     static void set_synchronous (struct Scsi_Host *host, int target, int sxfer, 
257         int scntl3, int now_connected);
258     static int datapath_residual (struct Scsi_Host *host);
259     static const char * sbcl_to_phase (int sbcl);
260     static void print_progress (Scsi_Cmnd *cmd);
261     static void print_queues (struct Scsi_Host *host);
262     static void process_issue_queue (unsigned long flags);
263     static int shutdown (struct Scsi_Host *host);
264     static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result);
265     static int disable (struct Scsi_Host *host);
266     static int NCR53c8xx_run_tests (struct Scsi_Host *host);
267     static int NCR53c8xx_script_len;
268     static int NCR53c8xx_dsa_len;
269     static void NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs);
270     static void do_NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs);
271     static int ncr_halt (struct Scsi_Host *host);
272     static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd 
273         *cmd);
274     static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd);
275     static void print_dsa (struct Scsi_Host *host, u32 *dsa,
276         const char *prefix);
277     static int print_insn (struct Scsi_Host *host, const u32 *insn,
278         const char *prefix, int kernel);
279     
280     static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd);
281     static void NCR53c8x0_init_fixup (struct Scsi_Host *host);
282     static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct 
283         NCR53c7x0_cmd *cmd);
284     static void NCR53c8x0_soft_reset (struct Scsi_Host *host);
285     
286     /* INSMOD variables */
287     static long long perm_options = PERM_OPTIONS;
288     /* 14 = .5s; 15 is max; decreasing divides by two. */
289     static int selection_timeout = 14;
290     /* Size of event list (per host adapter) */
291     static int track_events = 0;
292     
293     static struct Scsi_Host *first_host = NULL;	/* Head of list of NCR boards */
294     static Scsi_Host_Template *the_template = NULL;	
295     
296     /*
297      * KNOWN BUGS :
298      * - There is some sort of conflict when the PPP driver is compiled with 
299      * 	support for 16 channels?
300      * 
301      * - On systems which predate the 1.3.x initialization order change,
302      *      the NCR driver will cause Cannot get free page messages to appear.  
303      *      These are harmless, but I don't know of an easy way to avoid them.
304      *
305      * - With OPTION_DISCONNECT, on two systems under unknown circumstances,
306      *	we get a PHASE MISMATCH with DSA set to zero (suggests that we 
307      *	are occurring somewhere in the reselection code) where 
308      *	DSP=some value DCMD|DBC=same value.  
309      * 	
310      *	Closer inspection suggests that we may be trying to execute
311      *	some portion of the DSA?
312      * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
313      * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
314      * scsi0 : no current command : unexpected phase MSGIN.
315      *         DSP=0x1c46cc, DCMD|DBC=0x1c46ac, DSA=0x0
316      *         DSPS=0x0, TEMP=0x1c3e70, DMODE=0x80
317      * scsi0 : DSP->
318      * 001c46cc : 0x001c46cc 0x00000000
319      * 001c46d4 : 0x001c5ea0 0x000011f8
320      *
321      *	Changed the print code in the phase_mismatch handler so
322      *	that we call print_lots to try to diagnose this.
323      *
324      */
325     
326     /* 
327      * Possible future direction of architecture for max performance :
328      *
329      * We're using a single start array for the NCR chip.  This is 
330      * sub-optimal, because we cannot add a command which would conflict with 
331      * an executing command to this start queue, and therefore must insert the 
332      * next command for a given I/T/L combination after the first has completed;
333      * incurring our interrupt latency between SCSI commands.
334      *
335      * To allow further pipelining of the NCR and host CPU operation, we want 
336      * to set things up so that immediately on termination of a command destined 
337      * for a given LUN, we get that LUN busy again.  
338      * 
339      * To do this, we need to add a 32 bit pointer to which is jumped to 
340      * on completion of a command.  If no new command is available, this 
341      * would point to the usual DSA issue queue select routine.
342      *
343      * If one were, it would point to a per-NCR53c7x0_cmd select routine 
344      * which starts execution immediately, inserting the command at the head 
345      * of the start queue if the NCR chip is selected or reselected.
346      *
347      * We would change so that we keep a list of outstanding commands 
348      * for each unit, rather than a single running_list.  We'd insert 
349      * a new command into the right running list; if the NCR didn't 
350      * have something running for that yet, we'd put it in the 
351      * start queue as well.  Some magic needs to happen to handle the 
352      * race condition between the first command terminating before the 
353      * new one is written.
354      *
355      * Potential for profiling : 
356      * Call do_gettimeofday(struct timeval *tv) to get 800ns resolution.
357      */
358     
359     
360     /*
361      * TODO : 
362      * 1.  To support WIDE transfers, not much needs to happen.  We
363      *	should do CHMOVE instructions instead of MOVEs when
364      *	we have scatter/gather segments of uneven length.  When
365      * 	we do this, we need to handle the case where we disconnect
366      *	between segments.
367      * 
368      * 2.  Currently, when Icky things happen we do a FATAL().  Instead,
369      *     we want to do an integrity check on the parts of the NCR hostdata
370      *     structure which were initialized at boot time; FATAL() if that 
371      *     fails, and otherwise try to recover.  Keep track of how many
372      *     times this has happened within a single SCSI command; if it 
373      *     gets excessive, then FATAL().
374      *
375      * 3.  Parity checking is currently disabled, and a few things should 
376      *     happen here now that we support synchronous SCSI transfers :
377      *     1.  On soft-reset, we should set the EPC (Enable Parity Checking)
378      *	   and AAP (Assert SATN/ on parity error) bits in SCNTL0.
379      *	
380      *     2.  We should enable the parity interrupt in the SIEN0 register.
381      * 
382      *     3.  intr_phase_mismatch() needs to believe that message out is 
383      *	   always an "acceptable" phase to have a mismatch in.  If 
384      *	   the old phase was MSG_IN, we should send a MESSAGE PARITY 
385      *	   error.  If the old phase was something else, we should send
386      *	   a INITIATOR_DETECTED_ERROR message.  Note that this could
387      *	   cause a RESTORE POINTERS message; so we should handle that 
388      *	   correctly first.  Instead, we should probably do an 
389      *	   initiator_abort.
390      *
391      * 4.  MPEE bit of CTEST4 should be set so we get interrupted if 
392      *     we detect an error.
393      *
394      *  
395      * 5.  The initial code has been tested on the NCR53c810.  I don't 
396      *     have access to NCR53c700, 700-66 (Forex boards), NCR53c710
397      *     (NCR Pentium systems), NCR53c720, NCR53c820, or NCR53c825 boards to 
398      *     finish development on those platforms.
399      *
400      *     NCR53c820/825/720 - need to add wide transfer support, including WDTR 
401      *     		negotiation, programming of wide transfer capabilities
402      *		on reselection and table indirect selection.
403      *
404      *     NCR53c710 - need to add fatal interrupt or GEN code for 
405      *		command completion signaling.   Need to modify all 
406      *		SDID, SCID, etc. registers, and table indirect select code 
407      *		since these use bit fielded (ie 1<<target) instead of 
408      *		binary encoded target ids.  Need to accommodate
409      *		different register mappings, probably scan through
410      *		the SCRIPT code and change the non SFBR register operand
411      *		of all MOVE instructions.
412      * 
413      *     NCR53c700/700-66 - need to add code to refix addresses on 
414      *		every nexus change, eliminate all table indirect code,
415      *		very messy.
416      *
417      * 6.  The NCR53c7x0 series is very popular on other platforms that 
418      *     could be running Linux - ie, some high performance AMIGA SCSI 
419      *     boards use it.  
420      *	
421      *     So, I should include #ifdef'd code so that it is 
422      *     compatible with these systems.
423      *	
424      *     Specifically, the little Endian assumptions I made in my 
425      *     bit fields need to change, and if the NCR doesn't see memory
426      *     the right way, we need to provide options to reverse words
427      *     when the scripts are relocated.
428      *
429      * 7.  Use ioremap() to access memory mapped boards.  
430      */
431     
432     /* 
433      * Allow for simultaneous existence of multiple SCSI scripts so we 
434      * can have a single driver binary for all of the family.
435      *
436      * - one for NCR53c700 and NCR53c700-66 chips	(not yet supported)
437      * - one for rest (only the NCR53c810, 815, 820, and 825 are currently 
438      *	supported)
439      * 
440      * So that we only need two SCSI scripts, we need to modify things so
441      * that we fixup register accesses in READ/WRITE instructions, and 
442      * we'll also have to accommodate the bit vs. binary encoding of IDs
443      * with the 7xx chips.
444      */
445     
446     /*
447      * Use pci_chips_ids to translate in both directions between PCI device ID 
448      * and chip numbers.  
449      */
450     
451     static struct {
452         unsigned short pci_device_id;
453         int chip;
454     /* 
455      * The revision field of the PCI_CLASS_REVISION register is compared 
456      * against each of these fields if the field is not -1.  If it
457      * is less than min_revision or larger than max_revision, a warning
458      * message is printed.
459      */
460         int max_revision;
461         int min_revision;
462     } pci_chip_ids[] = { 
463         {PCI_DEVICE_ID_NCR_53C810, 810, 2, 1}, 
464         {PCI_DEVICE_ID_NCR_53C815, 815, 3, 2},
465         {PCI_DEVICE_ID_NCR_53C820, 820, -1, -1},
466         {PCI_DEVICE_ID_NCR_53C825, 825, -1, -1}
467     };
468     
469     #define NPCI_CHIP_IDS (sizeof (pci_chip_ids) / sizeof(pci_chip_ids[0]))
470     
471     #define ROUNDUP(adr,type)	\
472       ((void *) (((long) (adr) + sizeof(type) - 1) & ~(sizeof(type) - 1)))
473     
474     /* 
475      * Forced detection and autoprobe code for various hardware.  Currently, 
476      * entry points for these are not included in init/main.c because if the 
477      * PCI BIOS code isn't working right, you're not going to be able to use 
478      * the hardware anyways; this way we force users to solve their 
479      * problems rather than forcing detection and blaming us when it 
480      * does not work.
481      */
482     
483     static struct override {
484         int chip;	/* 700, 70066, 710, 720, 810, 820 */
485         int board;	/* Any special board level gunk */
486         unsigned pci:1;
487         union {
488     	struct {
489     	    int base;	/* Memory address - indicates memory mapped regs */
490     	    int io_port;/* I/O port address - indicates I/O mapped regs */
491         	    int irq;	/* IRQ line */		
492         	    int dma;	/* DMA channel 		- often none */
493     	} normal;
494     	struct {
495     	    int bus;
496     	    int device;
497     	    int function;
498     	} pci;
499         } data;
500         long long options;
501     } overrides [4] = {{0,},};
502     static int commandline_current = 0;
503     static int no_overrides = 0;
504     
505     #if 0
506     #define OVERRIDE_LIMIT (sizeof(overrides) / sizeof(struct override))
507     #else
508     #define OVERRIDE_LIMIT commandline_current
509     #endif
510     
511     /*
512      * Function: issue_to_cmd
513      *
514      * Purpose: convert jump instruction in issue array to NCR53c7x0_cmd
515      *	structure pointer.  
516      *
517      * Inputs; issue - pointer to start of NOP or JUMP instruction
518      *	in issue array.
519      *
520      * Returns: pointer to command on success; 0 if opcode is NOP.
521      */
522     
523     static inline struct NCR53c7x0_cmd *
524     issue_to_cmd (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
525         u32 *issue)
526     {
527         return (issue[0] != hostdata->NOP_insn) ? 
528         /* 
529          * If the IF TRUE bit is set, it's a JUMP instruction.  The
530          * operand is a bus pointer to the dsa_begin routine for this DSA.  The
531          * dsa field of the NCR53c7x0_cmd structure starts with the 
532          * DSA code template.  By converting to a virtual address,
533          * subtracting the code template size, and offset of the 
534          * dsa field, we end up with a pointer to the start of the 
535          * structure (alternatively, we could use the 
536          * dsa_cmnd field, an anachronism from when we weren't
537          * sure what the relationship between the NCR structures
538          * and host structures were going to be.
539          */
540     	(struct NCR53c7x0_cmd *) ((char *) bus_to_virt (le32_to_cpu(issue[1])) - 
541     	    (hostdata->E_dsa_code_begin - hostdata->E_dsa_code_template) -
542     	    offsetof(struct NCR53c7x0_cmd, dsa)) 
543         /* If the IF TRUE bit is not set, it's a NOP */
544     	: NULL;
545     }
546     
547     
548     /*
549      * Function : static internal_setup(int board, int chip, char *str, int *ints)
550      *
551      * Purpose : LILO command line initialization of the overrides array,
552      * 
553      * Inputs : board - currently, unsupported.  chip - 700, 70066, 710, 720
554      * 	810, 815, 820, 825, although currently only the NCR53c810 is 
555      *	supported.
556      * 
557      */
558     
559     static void 
560     internal_setup(int board, int chip, char *str, int *ints) {
561         unsigned char pci;		/* Specifies a PCI override, with bus, device,
562     				   function */
563     
564         pci = (str && !strcmp (str, "pci")) ? 1 : 0;
565         
566     /*
567      * Override syntaxes are as follows : 
568      * ncr53c700,ncr53c700-66,ncr53c710,ncr53c720=mem,io,irq,dma
569      * ncr53c810,ncr53c820,ncr53c825=mem,io,irq or pci,bus,device,function
570      */
571     
572         if (commandline_current < OVERRIDE_LIMIT) {
573     	overrides[commandline_current].pci = pci ? 1 : 0;
574     	if (!pci) {
575     	    overrides[commandline_current].data.normal.base = ints[1];
576     	    overrides[commandline_current].data.normal.io_port = ints[2];
577     	    overrides[commandline_current].data.normal.irq = ints[3];
578         	    overrides[commandline_current].data.normal.dma = (ints[0] >= 4) ?
579         	    	ints[4] : DMA_NONE;
580     	    /* FIXME: options is now a long long */
581         	    overrides[commandline_current].options = (ints[0] >= 5) ?
582         	    	ints[5] : 0;
583     	} else {
584     	    overrides[commandline_current].data.pci.bus = ints[1];
585     	    overrides[commandline_current].data.pci.device = ints[2];
586     	    overrides[commandline_current].data.pci.function = ints[3];
587     	    /* FIXME: options is now a long long */
588         	    overrides[commandline_current].options = (ints[0] >= 4) ?
589         	    	ints[4] : 0;
590     	}
591     	overrides[commandline_current].board = board;
592     	overrides[commandline_current].chip = chip;
593     	++commandline_current;
594         	++no_overrides;
595         } else {
596     	printk ("53c7,7x0.c:internal_setup() : too many overrides\n");
597         }
598     }
599     
600     /*
601      * XXX - we might want to implement a single override function
602      *       with a chip type field, revamp the command line configuration,
603      * 	 etc.
604      */
605     
606     #define setup_wrapper(x) 				\
607     void ncr53c##x##_setup (char *str, int *ints) {		\
608         internal_setup (BOARD_GENERIC, x, str, ints);	\
609     }
610     
611     setup_wrapper(700)
612     setup_wrapper(70066)
613     setup_wrapper(710)
614     setup_wrapper(720)
615     setup_wrapper(810)
616     setup_wrapper(815)
617     setup_wrapper(820)
618     setup_wrapper(825)
619     
620     /* 
621      * FIXME: we should junk these, in favor of synchronous_want and 
622      * wide_want in the NCR53c7x0_hostdata structure.
623      */
624     
625     /* Template for "preferred" synchronous transfer parameters. */
626     
627     static const unsigned char sdtr_message[] = {
628     #ifdef CONFIG_SCSI_NCR53C7xx_FAST
629         EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 25 /* *4ns */, 8 /* off */ 
630     #else
631         EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 50 /* *4ns */, 8 /* off */ 
632     #endif
633     };
634     
635     /* Template to request asynchronous transfers */
636     
637     static const unsigned char async_message[] = {
638         EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 0, 0 /* asynchronous */
639     };
640     
641     /* Template for "preferred" WIDE transfer parameters */
642     
643     static const unsigned char wdtr_message[] = {
644         EXTENDED_MESSAGE, 2 /* length */, EXTENDED_WDTR, 1 /* 2^1 bytes */
645     };
646     
647     /*
648      * Function : struct Scsi_Host *find_host (int host)
649      * 
650      * Purpose : KGDB support function which translates a host number 
651      * 	to a host structure. 
652      *
653      * Inputs : host - number of SCSI host
654      *
655      * Returns : NULL on failure, pointer to host structure on success.
656      */
657     
658     #if 0
659     static struct Scsi_Host *
660     find_host (int host) {
661         struct Scsi_Host *h;
662         for (h = first_host; h && h->host_no != host; h = h->next);
663         if (!h) {
664     	printk (KERN_ALERT "scsi%d not found\n", host);
665     	return NULL;
666         } else if (h->hostt != the_template) {
667     	printk (KERN_ALERT "scsi%d is not a NCR board\n", host);
668     	return NULL;
669         }
670         return h;
671     }
672     
673     /*
674      * Function : request_synchronous (int host, int target)
675      * 
676      * Purpose : KGDB interface which will allow us to negotiate for 
677      * 	synchronous transfers.  This ill be replaced with a more 
678      * 	integrated function; perhaps a new entry in the scsi_host 
679      *	structure, accessible via an ioctl() or perhaps /proc/scsi.
680      *
681      * Inputs : host - number of SCSI host; target - number of target.
682      *
683      * Returns : 0 when negotiation has been setup for next SCSI command,
684      *	-1 on failure.
685      */
686     
687     static int
688     request_synchronous (int host, int target) {
689         struct Scsi_Host *h;
690         struct NCR53c7x0_hostdata *hostdata;
691         unsigned long flags;
692         if (target < 0) {
693     	printk (KERN_ALERT "target %d is bogus\n", target);
694     	return -1;
695         }
696         if (!(h = find_host (host)))
697     	return -1;
698         else if (h->this_id == target) {
699     	printk (KERN_ALERT "target %d is host ID\n", target);
700     	return -1;
701         } 
702         else if (target > h->max_id) {
703     	printk (KERN_ALERT "target %d exceeds maximum of %d\n", target,
704     	    h->max_id);
705     	return -1;
706         }
707         hostdata = (struct NCR53c7x0_hostdata *)h->hostdata;
708     
709         save_flags(flags);
710         cli();
711         if (hostdata->initiate_sdtr & (1 << target)) {
712     	restore_flags(flags);
713     	printk (KERN_ALERT "target %d already doing SDTR\n", target);
714     	return -1;
715         } 
716         hostdata->initiate_sdtr |= (1 << target);
717         restore_flags(flags);
718         return 0;
719     }
720     
721     /*
722      * Function : request_disconnect (int host, int on_or_off)
723      * 
724      * Purpose : KGDB support function, tells us to allow or disallow 
725      *	disconnections.
726      *
727      * Inputs : host - number of SCSI host; on_or_off - non-zero to allow,
728      *	zero to disallow.
729      *
730      * Returns : 0 on success, *	-1 on failure.
731      */
732     
733     static int 
734     request_disconnect (int host, int on_or_off) {
735         struct Scsi_Host *h;
736         struct NCR53c7x0_hostdata *hostdata;
737         if (!(h = find_host (host)))
738     	return -1;
739         hostdata = (struct NCR53c7x0_hostdata *) h->hostdata;
740         if (on_or_off) 
741     	hostdata->options |= OPTION_DISCONNECT;
742         else
743     	hostdata->options &= ~OPTION_DISCONNECT;
744         return 0;
745     }
746     #endif
747     
748     /*
749      * Function : static void NCR53c7x0_driver_init (struct Scsi_Host *host)
750      *
751      * Purpose : Initialize internal structures, as required on startup, or 
752      *	after a SCSI bus reset.
753      * 
754      * Inputs : host - pointer to this host adapter's structure
755      */
756     
757     static void 
758     NCR53c7x0_driver_init (struct Scsi_Host *host) {
759         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
760     	host->hostdata;
761         int i, j;
762         u32 *curr;
763         for (i = 0; i < 16; ++i) {
764     	hostdata->request_sense[i] = 0;
765         	for (j = 0; j < 8; ++j) 
766     	    hostdata->busy[i][j] = 0;
767     	set_synchronous (host, i, /* sxfer */ 0, hostdata->saved_scntl3, 0);
768         }
769         hostdata->issue_queue = NULL;
770         hostdata->running_list = hostdata->finished_queue = 
771     	hostdata->curr = NULL;
772         for (i = 0, curr = (u32 *) hostdata->schedule; 
773     	i < host->can_queue; ++i, curr += 2) {
774     	curr[0] = hostdata->NOP_insn;
775     	curr[1] = le32_to_cpu(0xdeadbeef);
776         }
777         curr[0] = le32_to_cpu(((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) | DBC_TCI_TRUE);
778         curr[1] = (u32) le32_to_cpu(virt_to_bus (hostdata->script) +
779     	hostdata->E_wait_reselect);
780         hostdata->reconnect_dsa_head = 0;
781         hostdata->addr_reconnect_dsa_head = (u32) 
782     	le32_to_cpu(virt_to_bus((void *) &(hostdata->reconnect_dsa_head)));
783         hostdata->expecting_iid = 0;
784         hostdata->expecting_sto = 0;
785         if (hostdata->options & OPTION_ALWAYS_SYNCHRONOUS) 
786     	hostdata->initiate_sdtr = le32_to_cpu(0xffff); 
787         else
788         	hostdata->initiate_sdtr = 0;
789         hostdata->talked_to = 0;
790         hostdata->idle = 1;
791     }
792     
793     /* 
794      * Function : static int ccf_to_clock (int ccf)
795      *
796      * Purpose :  Return the largest SCSI clock allowable for a given
797      *	clock conversion factor, allowing us to do synchronous periods
798      *	when we don't know what the SCSI clock is by taking at least 
799      *	as long as the device says we can.
800      *
801      * Inputs : ccf
802      *
803      * Returns : clock on success, -1 on failure.
804      */
805     
806     static int 
807     ccf_to_clock (int ccf) {
808         switch (ccf) {
809         case 1: return 25000000; 	/* Divide by 1.0 */
810         case 2: return 37500000;	/* Divide by 1.5 */
811         case 3: return 50000000;  	/* Divide by 2.0 */
812         case 0: 			/* Divide by 3.0 */
813         case 4: return 66000000; 
814         default: return -1;
815         }
816     }
817     
818     /* 
819      * Function : static int clock_to_ccf (int clock)
820      *
821      * Purpose :  Return the clock conversion factor for a given SCSI clock.
822      *
823      * Inputs : clock - SCSI clock expressed in Hz.
824      *
825      * Returns : ccf on success, -1 on failure.
826      */
827     
828     static int 
829     clock_to_ccf (int clock) {
830         if (clock < 16666666)
831     	return -1;
832         if (clock < 25000000)
833     	return 1; 	/* Divide by 1.0 */
834         else if (clock < 37500000)
835     	return 2; 	/* Divide by 1.5 */
836         else if (clock < 50000000)
837     	return 3;	/* Divide by 2.0 */
838         else if (clock < 66000000)
839     	return 4;	/* Divide by 3.0 */
840         else 
841     	return -1;
842     }
843         
844     /* 
845      * Function : static int NCR53c7x0_init (struct Scsi_Host *host)
846      *
847      * Purpose :  initialize the internal structures for a given SCSI host
848      *
849      * Inputs : host - pointer to this host adapter's structure
850      *
851      * Preconditions : when this function is called, the chip_type 
852      * 	field of the hostdata structure MUST have been set.
853      *
854      * Returns : 0 on success, -1 on failure.
855      */
856     
857     static int 
858     NCR53c7x0_init (struct Scsi_Host *host) {
859         NCR53c7x0_local_declare();
860         int i, ccf, expected_ccf;
861         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
862     	host->hostdata;
863         struct Scsi_Host *search;
864         /* 
865          * There are some things which we need to know about in order to provide
866          * a semblance of support.  Print 'em if they aren't what we expect, 
867          * otherwise don't add to the noise.
868          * 
869          * -1 means we don't know what to expect.
870          */
871         int expected_id = -1;
872         int expected_clock = -1;
873         int uninitialized = 0;
874         /* 
875          * FIXME : this is only on Intel boxes.  On other platforms, this
876          * will differ.
877          */
878         int expected_mapping = OPTION_IO_MAPPED;
879     
880         NCR53c7x0_local_setup(host);
881     
882         switch (hostdata->chip) {
883         case 820:
884         case 825:
885     #ifdef notyet
886     	host->max_id = 15;
887     #endif
888     	/* Fall through */
889         case 810:
890         case 815:
891         	hostdata->dstat_sir_intr = NCR53c8x0_dstat_sir_intr;
892         	hostdata->init_save_regs = NULL;
893         	hostdata->dsa_fixup = NCR53c8xx_dsa_fixup;
894         	hostdata->init_fixup = NCR53c8x0_init_fixup;
895         	hostdata->soft_reset = NCR53c8x0_soft_reset;
896     	hostdata->run_tests = NCR53c8xx_run_tests;
897     /* Is the SCSI clock ever anything else on these chips? */
898     	expected_clock = hostdata->scsi_clock = 40000000;
899     	expected_id = 7;
900         	break;
901         default:
902     	printk ("scsi%d : chip type of %d is not supported yet, detaching.\n",
903     	    host->host_no, hostdata->chip);
904     	scsi_unregister (host);
905     	return -1;
906         }
907     
908         /* Assign constants accessed by NCR */
909         hostdata->NCR53c7xx_zero = 0;			
910         hostdata->NCR53c7xx_msg_reject = le32_to_cpu(MESSAGE_REJECT);
911         hostdata->NCR53c7xx_msg_abort = le32_to_cpu(ABORT);
912         hostdata->NCR53c7xx_msg_nop = le32_to_cpu(NOP);
913         hostdata->NOP_insn = le32_to_cpu((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24);
914     
915         if (expected_mapping == -1 || 
916     	(hostdata->options & (OPTION_MEMORY_MAPPED)) != 
917     	(expected_mapping & OPTION_MEMORY_MAPPED))
918     	printk ("scsi%d : using %s mapped access\n", host->host_no, 
919     	    (hostdata->options & OPTION_MEMORY_MAPPED) ? "memory" : 
920     	    "io");
921     
922         hostdata->dmode = (hostdata->chip == 700 || hostdata->chip == 70066) ? 
923     	DMODE_REG_00 : DMODE_REG_10;
924         hostdata->istat = ((hostdata->chip / 100) == 8) ? 
925         	ISTAT_REG_800 : ISTAT_REG_700;
926     
927     /* Only the ISTAT register is readable when the NCR is running, so make 
928        sure it's halted. */
929         ncr_halt(host);
930     
931     /* 
932      * XXX - the NCR53c700 uses bitfielded registers for SCID, SDID, etc,
933      *	as does the 710 with one bit per SCSI ID.  Conversely, the NCR
934      * 	uses a normal, 3 bit binary representation of these values.
935      *
936      * Get the rest of the NCR documentation, and FIND OUT where the change
937      * was.
938      */
939     #if 0
940         tmp = hostdata->this_id_mask = NCR53c7x0_read8(SCID_REG);
941         for (host->this_id = 0; tmp != 1; tmp >>=1, ++host->this_id);
942     #else
943         host->this_id = NCR53c7x0_read8(SCID_REG) & 15;
944         if (host->this_id == 0)
945     	host->this_id = 7;	/* sanitize hostid---0 doesn't make sense */
946         hostdata->this_id_mask = 1 << host->this_id;
947     #endif
948     
949     /*
950      * Note : we should never encounter a board setup for ID0.  So,
951      * 	if we see ID0, assume that it was uninitialized and set it
952      * 	to the industry standard 7.
953      */
954         if (!host->this_id) {
955     	printk("scsi%d : initiator ID was %d, changing to 7\n",
956     	    host->host_no, host->this_id);
957     	host->this_id = 7;
958     	hostdata->this_id_mask = 1 << 7;
959     	uninitialized = 1;
960         };
961     
962         if (expected_id == -1 || host->this_id != expected_id)
963         	printk("scsi%d : using initiator ID %d\n", host->host_no,
964         	    host->this_id);
965     
966         /*
967          * Save important registers to allow a soft reset.
968          */
969     
970         if ((hostdata->chip / 100) == 8) {
971         /* 
972          * CTEST4 controls burst mode disable.
973          */
974     	hostdata->saved_ctest4 = NCR53c7x0_read8(CTEST4_REG_800) & 
975         	    CTEST4_800_SAVE;
976         } else {
977         /*
978          * CTEST7 controls cache snooping, burst mode, and support for 
979          * external differential drivers.
980          */
981     	hostdata->saved_ctest7 = NCR53c7x0_read8(CTEST7_REG) & CTEST7_SAVE;
982         }
983     
984         /*
985          * On NCR53c700 series chips, DCNTL controls the SCSI clock divisor,
986          * on 800 series chips, it allows for a totem-pole IRQ driver.
987          */
988     
989         hostdata->saved_dcntl = NCR53c7x0_read8(DCNTL_REG);
990     
991         /* 
992          * DCNTL_800_IRQM controls weather we are using an open drain
993          * driver (reset) or totem pole driver (set).  In all cases, 
994          * it's level active.  I suppose this is an issue when we're trying to 
995          * wire-or the same PCI INTx line?
996          */
997         if ((hostdata->chip / 100) == 8)
998     	hostdata->saved_dcntl &= ~DCNTL_800_IRQM;
999     
1000         /*
1001          * DMODE controls DMA burst length, and on 700 series chips,
1002          * 286 mode and bus width  
1003          */
1004         hostdata->saved_dmode = NCR53c7x0_read8(hostdata->dmode);
1005     
1006         /* 
1007          * Now that burst length and enabled/disabled status is known, 
1008          * clue the user in on it.  
1009          */
1010        
1011         if ((hostdata->chip / 100) == 8) {
1012     	if (hostdata->saved_ctest4 & CTEST4_800_BDIS) {
1013     	    printk ("scsi%d : burst mode disabled\n", host->host_no);
1014     	} else {
1015     	    switch (hostdata->saved_dmode & DMODE_BL_MASK) {
1016     	    case DMODE_BL_2: i = 2; break;
1017     	    case DMODE_BL_4: i = 4; break;
1018     	    case DMODE_BL_8: i = 8; break;
1019     	    case DMODE_BL_16: i = 16; break;
1020                 default: i = 0;
1021     	    }
1022     	    printk ("scsi%d : burst length %d\n", host->host_no, i);
1023     	}
1024         }
1025     
1026         /*
1027          * On NCR53c810 and NCR53c820 chips, SCNTL3 contails the synchronous
1028          * and normal clock conversion factors.
1029          */
1030         if (hostdata->chip / 100 == 8)  {
1031     	expected_ccf = clock_to_ccf (expected_clock);
1032     	hostdata->saved_scntl3 = NCR53c7x0_read8(SCNTL3_REG_800);
1033     	ccf = hostdata->saved_scntl3 & SCNTL3_800_CCF_MASK;
1034     	if (expected_ccf != -1 && ccf != expected_ccf && !ccf) {
1035     	    hostdata->saved_scntl3 = (hostdata->saved_scntl3 &
1036     		~SCNTL3_800_CCF_MASK) | expected_ccf;
1037     	    if (!uninitialized) {
1038     		printk ("scsi%d : reset ccf to %d from %d\n", 
1039     		    host->host_no, expected_ccf, ccf);
1040     		uninitialized = 1;
1041     	    }
1042     	}
1043         } else
1044         	ccf = 0;
1045     
1046         /*
1047          * If we don't have a SCSI clock programmed, pick one on the upper
1048          * bound of that allowed by NCR so that our transfers err on the 
1049          * slow side, since transfer period must be >= the agreed 
1050          * upon period.
1051          */
1052     
1053         if ((!hostdata->scsi_clock) && (hostdata->scsi_clock = ccf_to_clock (ccf)) 
1054     	== -1) {
1055     	printk ("scsi%d : clock conversion factor %d unknown.\n"
1056     		"         synchronous transfers disabled\n",
1057     		host->host_no, ccf);
1058     	hostdata->options &= ~OPTION_SYNCHRONOUS;
1059     	hostdata->scsi_clock = 0; 
1060         }
1061     
1062         if (expected_clock == -1 || hostdata->scsi_clock != expected_clock)
1063         	printk ("scsi%d : using %dMHz SCSI clock\n", host->host_no, 
1064     	    hostdata->scsi_clock / 1000000);
1065     
1066         for (i = 0; i < 16; ++i) 
1067     	hostdata->cmd_allocated[i] = 0;
1068     
1069         if (hostdata->init_save_regs)
1070         	hostdata->init_save_regs (host);
1071         if (hostdata->init_fixup)
1072         	hostdata->init_fixup (host);
1073     
1074         if (!the_template) {
1075     	the_template = host->hostt;
1076     	first_host = host;
1077         }
1078     
1079         /* 
1080          * Linux SCSI drivers have always been plagued with initialization 
1081          * problems - some didn't work with the BIOS disabled since they expected
1082          * initialization from it, some didn't work when the networking code
1083          * was enabled and registers got scrambled, etc.
1084          *
1085          * To avoid problems like this, in the future, we will do a soft 
1086          * reset on the SCSI chip, taking it back to a sane state.
1087          */
1088     
1089         hostdata->soft_reset (host);
1090     
1091     #if 1
1092         hostdata->debug_count_limit = -1;
1093     #else
1094         hostdata->debug_count_limit = 1;
1095     #endif
1096         hostdata->intrs = -1;
1097         hostdata->resets = -1;
1098         memcpy ((void *) hostdata->synchronous_want, (void *) sdtr_message, 
1099     	sizeof (hostdata->synchronous_want));
1100     
1101         NCR53c7x0_driver_init (host);
1102     
1103         /*
1104          * Set up an interrupt handler if we aren't already sharing an IRQ
1105          * with another board.
1106          */
1107     
1108         for (search = first_host; search && !(search->hostt == the_template &&
1109     	search->irq == host->irq && search != host); search=search->next);
1110     
1111         if (!search) {
1112     #ifdef __powerpc__
1113     	if (request_irq(host->irq, do_NCR53c7x0_intr, SA_SHIRQ, "53c7,8xx", NULL)) 
1114     #else
1115     	if (request_irq(host->irq, do_NCR53c7x0_intr, SA_INTERRUPT, "53c7,8xx", NULL))
1116     #endif
1117     	  {
1118     	  
1119     	  
1120     	    printk("scsi%d : IRQ%d not free, detaching\n"
1121     	           "         You have either a configuration problem, or a\n"
1122                        "         broken BIOS.  You may wish to manually assign\n"
1123     	           "         an interrupt to the NCR board rather than using\n"
1124                        "         an automatic setting.\n", 
1125     		host->host_no, host->irq);
1126     	    scsi_unregister (host);
1127     	    return -1;
1128     	} 
1129         } else {
1130     	printk("scsi%d : using interrupt handler previously installed for scsi%d\n",
1131     	    host->host_no, search->host_no);
1132         }
1133     
1134     
1135         if ((hostdata->run_tests && hostdata->run_tests(host) == -1) ||
1136             (hostdata->options & OPTION_DEBUG_TESTS_ONLY)) {
1137         	/* XXX Should disable interrupts, etc. here */
1138     	scsi_unregister (host);
1139         	return -1;
1140         } else {
1141     	if (host->io_port)  {
1142     	    host->n_io_port = 128;
1143     	    request_region (host->io_port, host->n_io_port, "ncr53c7,8xx");
1144     	}
1145         }
1146         
1147         if (NCR53c7x0_read8 (SBCL_REG) & SBCL_BSY) {
1148     	printk ("scsi%d : bus wedge, doing SCSI reset\n", host->host_no);
1149     	hard_reset (host);
1150         }
1151         return 0;
1152     }
1153     
1154     /* 
1155      * Function : static int normal_init(Scsi_Host_Template *tpnt, int board, 
1156      *	int chip, u32 base, int io_port, int irq, int dma, int pcivalid,
1157      *	unsigned char pci_bus, unsigned char pci_device_fn,
1158      *      struct pci_dev *pci_dev, long long options);
1159      *
1160      * Purpose : initializes a NCR53c7,8x0 based on base addresses,
1161      *	IRQ, and DMA channel.	
1162      *	
1163      *	Useful where a new NCR chip is backwards compatible with
1164      *	a supported chip, but the DEVICE ID has changed so it 
1165      *	doesn't show up when the autoprobe does a pcibios_find_device.
1166      *
1167      * Inputs : tpnt - Template for this SCSI adapter, board - board level
1168      *	product, chip - 810, 820, or 825, bus - PCI bus, device_fn -
1169      *	device and function encoding as used by PCI BIOS calls.
1170      * 
1171      * Returns : 0 on success, -1 on failure.
1172      *
1173      */
1174     
1175     static int  __init 
1176     normal_init (Scsi_Host_Template *tpnt, int board, int chip, 
1177         u32 base, int io_port, int irq, int dma, int pci_valid, 
1178         unsigned char pci_bus, unsigned char pci_device_fn,
1179         struct pci_dev *pci_dev, long long options)
1180     {
1181         struct Scsi_Host *instance;
1182         struct NCR53c7x0_hostdata *hostdata;
1183         char chip_str[80];
1184         int script_len = 0, dsa_len = 0, size = 0, max_cmd_size = 0, 
1185     	schedule_size = 0, ok = 0;
1186         void *tmp;
1187     
1188         options |= perm_options;
1189     
1190         switch (chip) {
1191         case 825:
1192         case 820:
1193         case 815:
1194         case 810:
1195     	schedule_size = (tpnt->can_queue + 1) * 8 /* JUMP instruction size */;
1196     	script_len = NCR53c8xx_script_len;
1197         	dsa_len = NCR53c8xx_dsa_len;
1198         	options |= OPTION_INTFLY;
1199         	sprintf (chip_str, "NCR53c%d", chip);
1200         	break;
1201         default:
1202         	printk("scsi-ncr53c7,8xx : unsupported SCSI chip %d\n", chip);
1203         	return -1;
1204         }
1205     
1206         printk("scsi-ncr53c7,8xx : %s at memory 0x%x, io 0x%x, irq %d",
1207         	chip_str, (unsigned) base, io_port, irq);
1208         if (dma == DMA_NONE)
1209         	printk("\n");
1210         else 
1211         	printk(", dma %d\n", dma);
1212     
1213         if ((chip / 100 == 8) && !pci_valid) 
1214     	printk ("scsi-ncr53c7,8xx : for better reliability and performance, please use the\n" 
1215     		"        PCI override instead.\n"
1216     		"	 Syntax : ncr53c8{10,15,20,25}=pci,<bus>,<device>,<function>\n"
1217     		"                 <bus> and <device> are usually 0.\n");
1218     
1219         if (options & OPTION_DEBUG_PROBE_ONLY) {
1220         	printk ("scsi-ncr53c7,8xx : probe only enabled, aborting initialization\n");
1221         	return -1;
1222         }
1223     
1224         max_cmd_size = sizeof(struct NCR53c7x0_cmd) + dsa_len +
1225         	/* Size of dynamic part of command structure : */
1226     	2 * /* Worst case : we don't know if we need DATA IN or DATA out */
1227     		( 2 * /* Current instructions per scatter/gather segment */ 
1228             	  tpnt->sg_tablesize + 
1229                       3 /* Current startup / termination required per phase */
1230     		) *
1231     	8 /* Each instruction is eight bytes */;
1232     
1233         /* Allocate fixed part of hostdata, dynamic part to hold appropriate
1234            SCSI SCRIPT(tm) plus a single, maximum-sized NCR53c7x0_cmd structure.
1235     
1236            We need a NCR53c7x0_cmd structure for scan_scsis() when we are 
1237            not loaded as a module, and when we're loaded as a module, we 
1238            can't use a non-dynamically allocated structure because modules
1239            are vmalloc()'d, which can allow structures to cross page 
1240            boundaries and breaks our physical/virtual address assumptions
1241            for DMA.
1242     
1243            So, we stick it past the end of our hostdata structure.
1244     
1245            ASSUMPTION : 
1246            	 Regardless of how many simultaneous SCSI commands we allow,
1247     	 the probe code only executes a _single_ instruction at a time,
1248     	 so we only need one here, and don't need to allocate NCR53c7x0_cmd
1249     	 structures for each target until we are no longer in scan_scsis
1250     	 and kmalloc() has become functional (memory_init() happens 
1251     	 after all device driver initialization).
1252         */
1253     
1254         size = sizeof(struct NCR53c7x0_hostdata) + script_len + 
1255         /* Note that alignment will be guaranteed, since we put the command
1256            allocated at probe time after the fixed-up SCSI script, which 
1257            consists of 32 bit words, aligned on a 32 bit boundary.  But
1258            on a 64bit machine we need 8 byte alignment for hostdata->free, so
1259            we add in another 4 bytes to take care of potential misalignment
1260            */
1261     	(sizeof(void *) - sizeof(u32)) + max_cmd_size + schedule_size;
1262     
1263         instance = scsi_register (tpnt, size);
1264         if (!instance)
1265     	return -1;
1266     
1267         /* FIXME : if we ever support an ISA NCR53c7xx based board, we
1268            need to check if the chip is running in a 16 bit mode, and if so 
1269            unregister it if it is past the 16M (0x1000000) mark */
1270        	
1271         hostdata = (struct NCR53c7x0_hostdata *) 
1272         	instance->hostdata;
1273         hostdata->size = size;
1274         hostdata->script_count = script_len / sizeof(u32);
1275         hostdata = (struct NCR53c7x0_hostdata *) instance->hostdata;
1276         hostdata->board = board;
1277         hostdata->chip = chip;
1278         if ((hostdata->pci_valid = pci_valid)) {
1279     	hostdata->pci_bus = pci_bus;
1280     	hostdata->pci_device_fn = pci_device_fn;
1281         }
1282     
1283         /*
1284          * Being memory mapped is more desirable, since 
1285          *
1286          * - Memory accesses may be faster.
1287          *
1288          * - The destination and source address spaces are the same for 
1289          *	 all instructions, meaning we don't have to twiddle dmode or 
1290          *	 any other registers.
1291          *
1292          * So, we try for memory mapped, and if we don't get it,
1293          * we go for port mapped, and that failing we tell the user
1294          * it can't work.
1295          */
1296     
1297         if (base) {
1298     	instance->base = (unsigned long) base;
1299     	/* Check for forced I/O mapping */
1300         	if (!(options & OPTION_IO_MAPPED)) {
1301     	    options |= OPTION_MEMORY_MAPPED;
1302     	    ok = 1;
1303     	}
1304         } else {
1305     	options &= ~OPTION_MEMORY_MAPPED;
1306         }
1307     
1308         if (io_port) {
1309     	instance->io_port = io_port;
1310     	options |= OPTION_IO_MAPPED;
1311     	ok = 1;
1312         } else {
1313     	options &= ~OPTION_IO_MAPPED;
1314         }
1315     
1316         if (!ok) {
1317     	printk ("scsi%d : not initializing, no I/O or memory mapping known \n",
1318     	    instance->host_no);
1319     	scsi_unregister (instance);
1320     	return -1;
1321         }
1322         instance->irq = irq;
1323         instance->dma_channel = dma;
1324         scsi_set_pci_device(instance, pci_dev);
1325     
1326         hostdata->options = options;
1327         hostdata->dsa_len = dsa_len;
1328         hostdata->max_cmd_size = max_cmd_size;
1329         hostdata->num_cmds = 1;
1330         /* Initialize single command */
1331         tmp = (hostdata->script + hostdata->script_count);
1332         hostdata->free = ROUNDUP(tmp, void *);
1333         hostdata->free->real = tmp;
1334         hostdata->free->size = max_cmd_size;
1335         hostdata->free->free = NULL;
1336         hostdata->free->next = NULL;
1337         hostdata->extra_allocate = 0;
1338     
1339         /* Allocate command start code space */
1340         hostdata->schedule = (chip == 700 || chip == 70066) ?
1341     	NULL : (u32 *) ((char *)hostdata->free + max_cmd_size);
1342     
1343     /* 
1344      * For diagnostic purposes, we don't really care how fast things blaze.
1345      * For profiling, we want to access the 800ns resolution system clock,
1346      * using a 'C' call on the host processor.
1347      *
1348      * Therefore, there's no need for the NCR chip to directly manipulate
1349      * this data, and we should put it wherever is most convenient for 
1350      * Linux.
1351      */
1352         if (track_events) 
1353     	hostdata->events = (struct NCR53c7x0_event *) (track_events ? 
1354     	    vmalloc (sizeof (struct NCR53c7x0_event) * track_events) : NULL);
1355         else
1356     	hostdata->events = NULL;
1357     
1358         if (hostdata->events) {
1359     	memset ((void *) hostdata->events, 0, sizeof(struct NCR53c7x0_event) *
1360     	    track_events);	
1361     	hostdata->event_size = track_events;
1362     	hostdata->event_index = 0;
1363         } else 
1364     	hostdata->event_size = 0;
1365     
1366         return NCR53c7x0_init(instance);
1367     }
1368     
1369     
1370     /* 
1371      * Function : static int ncr_pci_init(Scsi_Host_Template *tpnt, int board, 
1372      *	int chip, int bus, int device_fn, long long options)
1373      *
1374      * Purpose : initializes a NCR53c800 family based on the PCI
1375      *	bus, device, and function location of it.  Allows 
1376      * 	reprogramming of latency timer and determining addresses
1377      *	and whether bus mastering, etc. are OK.
1378      *	
1379      *	Useful where a new NCR chip is backwards compatible with
1380      *	a supported chip, but the DEVICE ID has changed so it 
1381      *	doesn't show up when the autoprobe does a pcibios_find_device.
1382      *
1383      * Inputs : tpnt - Template for this SCSI adapter, board - board level
1384      *	product, chip - 810, 820, or 825, bus - PCI bus, device_fn -
1385      *	device and function encoding as used by PCI BIOS calls.
1386      * 
1387      * Returns : 0 on success, -1 on failure.
1388      *
1389      */
1390     
1391     static int  __init 
1392     ncr_pci_init (Scsi_Host_Template *tpnt, int board, int chip, 
1393         unsigned char bus, unsigned char device_fn, long long options){
1394         unsigned short command;
1395         unsigned int base, io_port; 
1396         unsigned char revision;
1397         int error, expected_chip;
1398         int expected_id = -1, max_revision = -1, min_revision = -1;
1399         int i, irq;
1400         struct pci_dev *pdev = pci_find_slot(bus, device_fn);
1401     
1402         printk("scsi-ncr53c7,8xx : at PCI bus %d, device %d, function %d\n",
1403     	bus, (int) (device_fn & 0xf8) >> 3, 
1404         	(int) device_fn & 7);
1405     
1406         if (!pdev) {
1407     	printk("scsi-ncr53c7,8xx : not initializing -- PCI device not found,\n"
1408     	       "        try using memory, port, irq override instead.\n");
1409     	return -1;
1410         }
1411     
1412         if ((error = pci_read_config_word (pdev, PCI_COMMAND, &command)) ||
1413     	(error = pci_read_config_byte (pdev, PCI_CLASS_REVISION, &revision))) {
1414     	printk ("scsi-ncr53c7,8xx : error %d not initializing due to error reading configuration space\n"
1415     		"	 perhaps you specified an incorrect PCI bus, device, or function.\n", error);
1416     	return -1;
1417         }
1418         if (pci_enable_device(pdev))
1419     	return -1;
1420         io_port = pci_resource_start(pdev, 0);
1421         base = pci_resource_start(pdev, 1);
1422         irq = pdev->irq;
1423     
1424         /* If any one ever clones the NCR chips, this will have to change */
1425     
1426         if (pdev->vendor != PCI_VENDOR_ID_NCR) {
1427     	printk ("scsi-ncr53c7,8xx : not initializing, 0x%04x is not NCR vendor ID\n",
1428     	    (int) pdev->vendor);
1429     	return -1;
1430         }
1431     
1432     #ifdef __powerpc__
1433         if ( ! (command & PCI_COMMAND_MASTER)) {
1434           printk("SCSI: PCI Master Bit has not been set. Setting...\n");
1435           command |= PCI_COMMAND_MASTER|PCI_COMMAND_IO;
1436           pci_write_config_word(pdev, PCI_COMMAND, command);
1437     
1438           if (io_port >= 0x10000000 && is_prep ) {
1439     	      /* Mapping on PowerPC can't handle this! */
1440     	      unsigned long new_io_port;
1441     	      new_io_port = (io_port & 0x00FFFFFF) | 0x01000000;
1442     	      printk("SCSI: I/O moved from %08X to %08x\n", io_port, new_io_port);
1443     	      io_port = new_io_port;
1444     	      pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, io_port);
1445     	      pdev->base_address[0] = io_port;
1446           }
1447         }
1448     #endif
1449     
1450         /* 
1451          * Bit 0 is the address space indicator and must be one for I/O
1452          * space mappings, bit 1 is reserved, discard them after checking
1453          * that they have the correct value of 1.
1454          */
1455     
1456         if (command & PCI_COMMAND_IO) { 
1457     	if (!(pdev->resource[0].flags & IORESOURCE_IO)) {
1458     	    printk ("scsi-ncr53c7,8xx : disabling I/O mapping since base "
1459     		    "address 0\n        contains a non-IO mapping\n");
1460     	    io_port = 0;
1461     	}
1462         } else {
1463         	io_port = 0;
1464         }
1465     
1466         if (command & PCI_COMMAND_MEMORY) {
1467     	if (!(pdev->resource[1].flags & IORESOURCE_MEM)) {
1468     	    printk("scsi-ncr53c7,8xx : disabling memory mapping since base "
1469     		   "address 1\n        contains a non-memory mapping\n");
1470     	    base = 0;
1471     	}
1472         } else {
1473     	base = 0;
1474         }
1475     	
1476         if (!io_port && !base) {
1477     	printk ("scsi-ncr53c7,8xx : not initializing, both I/O and memory mappings disabled\n");
1478     	return -1;
1479         }
1480     	
1481         if (!(command & PCI_COMMAND_MASTER)) {
1482     	printk ("scsi-ncr53c7,8xx : not initializing, BUS MASTERING was disabled\n");
1483     	return -1;
1484         }
1485     
1486         for (i = 0; i < NPCI_CHIP_IDS; ++i) {
1487     	if (pdev->device == pci_chip_ids[i].pci_device_id) {
1488     	    max_revision = pci_chip_ids[i].max_revision;
1489     	    min_revision = pci_chip_ids[i].min_revision;
1490     	    expected_chip = pci_chip_ids[i].chip;
1491     	}
1492     	if (chip == pci_chip_ids[i].chip)
1493     	    expected_id = pci_chip_ids[i].pci_device_id;
1494         }
1495     
1496         if (chip && pdev->device != expected_id) 
1497     	printk ("scsi-ncr53c7,8xx : warning : device id of 0x%04x doesn't\n"
1498                     "                   match expected 0x%04x\n",
1499     	    (unsigned int) pdev->device, (unsigned int) expected_id );
1500         
1501         if (max_revision != -1 && revision > max_revision) 
1502     	printk ("scsi-ncr53c7,8xx : warning : revision of %d is greater than %d.\n",
1503     	    (int) revision, max_revision);
1504         else if (min_revision != -1 && revision < min_revision)
1505     	printk ("scsi-ncr53c7,8xx : warning : revision of %d is less than %d.\n",
1506     	    (int) revision, min_revision);
1507     
1508         if (io_port && check_region (io_port, 128)) {
1509     	printk ("scsi-ncr53c7,8xx : IO region 0x%x to 0x%x is in use\n",
1510     	    (unsigned) io_port, (unsigned) io_port + 127);
1511     	return -1;
1512         }
1513     
1514         return normal_init (tpnt, board, chip, (int) base, io_port, 
1515     	(int) irq, DMA_NONE, 1, bus, device_fn, pdev, options);
1516     }
1517     
1518     
1519     /* 
1520      * Function : int NCR53c7xx_detect(Scsi_Host_Template *tpnt)
1521      *
1522      * Purpose : detects and initializes NCR53c7,8x0 SCSI chips
1523      *	that were autoprobed, overridden on the LILO command line, 
1524      *	or specified at compile time.
1525      *
1526      * Inputs : tpnt - template for this SCSI adapter
1527      * 
1528      * Returns : number of host adapters detected
1529      *
1530      */
1531     
1532     int __init 
1533     NCR53c7xx_detect(Scsi_Host_Template *tpnt){
1534         int i;
1535         int current_override;
1536         int count;			/* Number of boards detected */
1537         unsigned char pci_bus, pci_device_fn;
1538         static short pci_index=0;	/* Device index to PCI BIOS calls */
1539     
1540         tpnt->proc_name = "ncr53c7xx";
1541     
1542         for (current_override = count = 0; current_override < OVERRIDE_LIMIT; 
1543     	 ++current_override) {
1544     	 if (overrides[current_override].pci ? 
1545     	    !ncr_pci_init (tpnt, overrides[current_override].board,
1546     		overrides[current_override].chip,
1547     		(unsigned char) overrides[current_override].data.pci.bus,
1548     		(((overrides[current_override].data.pci.device
1549     		<< 3) & 0xf8)|(overrides[current_override].data.pci.function & 
1550     		7)), overrides[current_override].options):
1551     	    !normal_init (tpnt, overrides[current_override].board, 
1552     		overrides[current_override].chip, 
1553     		overrides[current_override].data.normal.base, 
1554     		overrides[current_override].data.normal.io_port,
1555     		overrides[current_override].data.normal.irq,
1556     		overrides[current_override].data.normal.dma,
1557     		0 /* PCI data invalid */, 0 /* PCI bus place holder */,  
1558     		0 /* PCI device_function place holder */,
1559                     NULL /* PCI pci_dev place holder */,
1560         	    	overrides[current_override].options)) {
1561         	    ++count;
1562     	} 
1563         }
1564     
1565         if (pci_present()) {
1566     	for (i = 0; i < NPCI_CHIP_IDS; ++i) 
1567     	    for (pci_index = 0;
1568     		!pcibios_find_device (PCI_VENDOR_ID_NCR, 
1569     		    pci_chip_ids[i].pci_device_id, pci_index, &pci_bus, 
1570     		    &pci_device_fn); 
1571         		++pci_index)
1572     		if (!ncr_pci_init (tpnt, BOARD_GENERIC, pci_chip_ids[i].chip, 
1573     		    pci_bus, pci_device_fn, /* no options */ 0))
1574     		    ++count;
1575         }
1576         return count;
1577     }
1578     
1579     /* NCR53c810 and NCR53c820 script handling code */
1580     
1581     #include "53c8xx_d.h"
1582     #ifdef A_int_debug_sync
1583     #define DEBUG_SYNC_INTR A_int_debug_sync
1584     #endif
1585     static int NCR53c8xx_script_len = sizeof (SCRIPT);
1586     static int NCR53c8xx_dsa_len = A_dsa_end + Ent_dsa_zero - Ent_dsa_code_template;
1587     
1588     /* 
1589      * Function : static void NCR53c8x0_init_fixup (struct Scsi_Host *host)
1590      *
1591      * Purpose :  copy and fixup the SCSI SCRIPTS(tm) code for this device.
1592      *
1593      * Inputs : host - pointer to this host adapter's structure
1594      *
1595      */
1596     
1597     static void 
1598     NCR53c8x0_init_fixup (struct Scsi_Host *host) {
1599         NCR53c7x0_local_declare();
1600         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1601     	host->hostdata;
1602         unsigned char tmp;
1603         int i, ncr_to_memory, memory_to_ncr;
1604         u32 base;
1605     #ifdef __powerpc__
1606         unsigned long *script_ptr;
1607     #endif    
1608         NCR53c7x0_local_setup(host);
1609     
1610     
1611         /* XXX - NOTE : this code MUST be made endian aware */
1612         /*  Copy code into buffer that was allocated at detection time.  */
1613         memcpy ((void *) hostdata->script, (void *) SCRIPT, 
1614     	sizeof(SCRIPT));
1615         /* Fixup labels */
1616         for (i = 0; i < PATCHES; ++i) 
1617     	hostdata->script[LABELPATCHES[i]] += 
1618         	    virt_to_bus(hostdata->script);
1619         /* Fixup addresses of constants that used to be EXTERNAL */
1620     
1621         patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_abort, 
1622         	virt_to_bus(&(hostdata->NCR53c7xx_msg_abort)));
1623         patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_reject, 
1624         	virt_to_bus(&(hostdata->NCR53c7xx_msg_reject)));
1625         patch_abs_32 (hostdata->script, 0, NCR53c7xx_zero, 
1626         	virt_to_bus(&(hostdata->NCR53c7xx_zero)));
1627         patch_abs_32 (hostdata->script, 0, NCR53c7xx_sink, 
1628         	virt_to_bus(&(hostdata->NCR53c7xx_sink)));
1629         patch_abs_32 (hostdata->script, 0, NOP_insn,
1630     	virt_to_bus(&(hostdata->NOP_insn)));
1631         patch_abs_32 (hostdata->script, 0, schedule,
1632     	virt_to_bus((void *) hostdata->schedule));
1633     
1634         /* Fixup references to external variables: */
1635         for (i = 0; i < EXTERNAL_PATCHES_LEN; ++i)
1636            hostdata->script[EXTERNAL_PATCHES[i].offset] +=
1637              virt_to_bus(EXTERNAL_PATCHES[i].address);
1638     
1639         /* 
1640          * Fixup absolutes set at boot-time.
1641          * 
1642          * All non-code absolute variables suffixed with "dsa_" and "int_"
1643          * are constants, and need no fixup provided the assembler has done 
1644          * it for us (I don't know what the "real" NCR assembler does in 
1645          * this case, my assembler does the right magic).
1646          */
1647     
1648         patch_abs_rwri_data (hostdata->script, 0, dsa_save_data_pointer, 
1649         	Ent_dsa_code_save_data_pointer - Ent_dsa_zero);
1650         patch_abs_rwri_data (hostdata->script, 0, dsa_restore_pointers,
1651         	Ent_dsa_code_restore_pointers - Ent_dsa_zero);
1652         patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
1653         	Ent_dsa_code_check_reselect - Ent_dsa_zero);
1654     
1655         /*
1656          * Just for the hell of it, preserve the settings of 
1657          * Burst Length and Enable Read Line bits from the DMODE 
1658          * register.  Make sure SCRIPTS start automagically.
1659          */
1660     
1661         tmp = NCR53c7x0_read8(DMODE_REG_10);
1662         tmp &= (DMODE_800_ERL | DMODE_BL_MASK);
1663     
1664         if (!(hostdata->options & OPTION_MEMORY_MAPPED)) {
1665         	base = (u32) host->io_port;
1666         	memory_to_ncr = tmp|DMODE_800_DIOM;
1667         	ncr_to_memory = tmp|DMODE_800_SIOM;
1668         } else {
1669         	base = virt_to_bus((void *)host->base);
1670     	memory_to_ncr = ncr_to_memory = tmp;
1671         }
1672     
1673         patch_abs_32 (hostdata->script, 0, addr_scratch, base + SCRATCHA_REG_800);
1674         patch_abs_32 (hostdata->script, 0, addr_temp, base + TEMP_REG);
1675     
1676         /*
1677          * I needed some variables in the script to be accessible to 
1678          * both the NCR chip and the host processor. For these variables,
1679          * I made the arbitrary decision to store them directly in the 
1680          * hostdata structure rather than in the RELATIVE area of the 
1681          * SCRIPTS.
1682          */
1683         
1684     
1685         patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_memory, tmp);
1686         patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_ncr, memory_to_ncr);
1687         patch_abs_rwri_data (hostdata->script, 0, dmode_ncr_to_memory, ncr_to_memory);
1688     
1689         patch_abs_32 (hostdata->script, 0, msg_buf, 
1690     	virt_to_bus((void *)&(hostdata->msg_buf)));
1691         patch_abs_32 (hostdata->script, 0, reconnect_dsa_head, 
1692         	virt_to_bus((void *)&(hostdata->reconnect_dsa_head)));
1693         patch_abs_32 (hostdata->script, 0, addr_reconnect_dsa_head, 
1694     	virt_to_bus((void *)&(hostdata->addr_reconnect_dsa_head)));
1695         patch_abs_32 (hostdata->script, 0, reselected_identify, 
1696         	virt_to_bus((void *)&(hostdata->reselected_identify)));
1697     /* reselected_tag is currently unused */
1698     #if 0
1699         patch_abs_32 (hostdata->script, 0, reselected_tag, 
1700         	virt_to_bus((void *)&(hostdata->reselected_tag)));
1701     #endif
1702     
1703         patch_abs_32 (hostdata->script, 0, test_dest, 
1704     	virt_to_bus((void*)&hostdata->test_dest));
1705         patch_abs_32 (hostdata->script, 0, test_src, 
1706     	virt_to_bus(&hostdata->test_source));
1707     
1708         patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect, 
1709     	(unsigned char)(Ent_dsa_code_check_reselect - Ent_dsa_zero));
1710     
1711     /* These are for event logging; the ncr_event enum contains the 
1712        actual interrupt numbers. */
1713     #ifdef A_int_EVENT_SELECT
1714        patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT, (u32) EVENT_SELECT);
1715     #endif
1716     #ifdef A_int_EVENT_DISCONNECT
1717        patch_abs_32 (hostdata->script, 0, int_EVENT_DISCONNECT, (u32) EVENT_DISCONNECT);
1718     #endif
1719     #ifdef A_int_EVENT_RESELECT
1720        patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT, (u32) EVENT_RESELECT);
1721     #endif
1722     #ifdef A_int_EVENT_COMPLETE
1723        patch_abs_32 (hostdata->script, 0, int_EVENT_COMPLETE, (u32) EVENT_COMPLETE);
1724     #endif
1725     #ifdef A_int_EVENT_IDLE
1726        patch_abs_32 (hostdata->script, 0, int_EVENT_IDLE, (u32) EVENT_IDLE);
1727     #endif
1728     #ifdef A_int_EVENT_SELECT_FAILED
1729        patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT_FAILED, 
1730     	(u32) EVENT_SELECT_FAILED);
1731     #endif
1732     #ifdef A_int_EVENT_BEFORE_SELECT
1733        patch_abs_32 (hostdata->script, 0, int_EVENT_BEFORE_SELECT,
1734     	(u32) EVENT_BEFORE_SELECT);
1735     #endif
1736     #ifdef A_int_EVENT_RESELECT_FAILED
1737        patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT_FAILED, 
1738     	(u32) EVENT_RESELECT_FAILED);
1739     #endif
1740     
1741         /*
1742          * Make sure the NCR and Linux code agree on the location of 
1743          * certain fields.
1744          */
1745     
1746         hostdata->E_accept_message = Ent_accept_message;
1747         hostdata->E_command_complete = Ent_command_complete;		
1748         hostdata->E_cmdout_cmdout = Ent_cmdout_cmdout;
1749         hostdata->E_data_transfer = Ent_data_transfer;
1750         hostdata->E_debug_break = Ent_debug_break;	
1751         hostdata->E_dsa_code_template = Ent_dsa_code_template;
1752         hostdata->E_dsa_code_template_end = Ent_dsa_code_template_end;
1753         hostdata->E_end_data_transfer = Ent_end_data_transfer;
1754         hostdata->E_initiator_abort = Ent_initiator_abort;
1755         hostdata->E_msg_in = Ent_msg_in;
1756         hostdata->E_other_transfer = Ent_other_transfer;
1757         hostdata->E_other_in = Ent_other_in;
1758         hostdata->E_other_out = Ent_other_out;
1759         hostdata->E_reject_message = Ent_reject_message;
1760         hostdata->E_respond_message = Ent_respond_message;
1761         hostdata->E_select = Ent_select;
1762         hostdata->E_select_msgout = Ent_select_msgout;
1763         hostdata->E_target_abort = Ent_target_abort;
1764     #ifdef Ent_test_0
1765         hostdata->E_test_0 = Ent_test_0;
1766     #endif
1767         hostdata->E_test_1 = Ent_test_1;
1768         hostdata->E_test_2 = Ent_test_2;
1769     #ifdef Ent_test_3
1770         hostdata->E_test_3 = Ent_test_3;
1771     #endif
1772         hostdata->E_wait_reselect = Ent_wait_reselect;
1773         hostdata->E_dsa_code_begin = Ent_dsa_code_begin;
1774     
1775         hostdata->dsa_cmdout = A_dsa_cmdout;
1776         hostdata->dsa_cmnd = A_dsa_cmnd;
1777         hostdata->dsa_datain = A_dsa_datain;
1778         hostdata->dsa_dataout = A_dsa_dataout;
1779         hostdata->dsa_end = A_dsa_end;			
1780         hostdata->dsa_msgin = A_dsa_msgin;
1781         hostdata->dsa_msgout = A_dsa_msgout;
1782         hostdata->dsa_msgout_other = A_dsa_msgout_other;
1783         hostdata->dsa_next = A_dsa_next;
1784         hostdata->dsa_select = A_dsa_select;
1785         hostdata->dsa_start = Ent_dsa_code_template - Ent_dsa_zero;
1786         hostdata->dsa_status = A_dsa_status;
1787         hostdata->dsa_jump_dest = Ent_dsa_code_fix_jump - Ent_dsa_zero + 
1788     	8 /* destination operand */;
1789     
1790         /* sanity check */
1791         if (A_dsa_fields_start != Ent_dsa_code_template_end - 
1792         	Ent_dsa_zero) 
1793         	printk("scsi%d : NCR dsa_fields start is %d not %d\n",
1794         	    host->host_no, A_dsa_fields_start, Ent_dsa_code_template_end - 
1795         	    Ent_dsa_zero);
1796     #ifdef __powerpc__
1797     /* The PowerPC is Big Endian - adjust script appropriately */
1798         script_ptr = hostdata->script;
1799         for (i = 0;  i < sizeof(SCRIPT);  i += sizeof(long))
1800         {
1801             *script_ptr++ = le32_to_cpu(*script_ptr);
1802         }
1803     #endif    	    
1804     
1805         printk("scsi%d : NCR code relocated to 0x%lx (virt 0x%p)\n", host->host_no,
1806     	virt_to_bus(hostdata->script), hostdata->script);
1807     }
1808     
1809     /*
1810      * Function : static int NCR53c8xx_run_tests (struct Scsi_Host *host)
1811      *
1812      * Purpose : run various verification tests on the NCR chip, 
1813      *	including interrupt generation, and proper bus mastering
1814      * 	operation.
1815      * 
1816      * Inputs : host - a properly initialized Scsi_Host structure
1817      *
1818      * Preconditions : the NCR chip must be in a halted state.
1819      *
1820      * Returns : 0 if all tests were successful, -1 on error.
1821      * 
1822      */
1823     
1824     static int 
1825     NCR53c8xx_run_tests (struct Scsi_Host *host) {
1826         NCR53c7x0_local_declare();
1827         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
1828     	host->hostdata;
1829         unsigned long timeout;
1830         u32 start;
1831         int failed, i;
1832         unsigned long flags;
1833         NCR53c7x0_local_setup(host);
1834     
1835         /* The NCR chip _must_ be idle to run the test scripts */
1836     
1837         save_flags(flags);
1838         cli();
1839         if (!hostdata->idle) {
1840     	printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
1841     	restore_flags(flags);
1842     	return -1;
1843         }
1844     
1845         /* 
1846          * Check for functional interrupts, this could work as an
1847          * autoprobe routine.
1848          */
1849     
1850         if ((hostdata->options & OPTION_DEBUG_TEST1) && 
1851     	    hostdata->state != STATE_DISABLED) {
1852     	hostdata->idle = 0;
1853     	hostdata->test_running = 1;
1854     	hostdata->test_completed = -1;
1855     	hostdata->test_dest = 0;
1856     	hostdata->test_source = 0xdeadbeef;
1857     	start = virt_to_bus (hostdata->script) + hostdata->E_test_1;
1858         	hostdata->state = STATE_RUNNING;
1859     	printk ("scsi%d : test 1", host->host_no);
1860     	NCR53c7x0_write32 (DSP_REG, start);
1861     	printk (" started\n");
1862     	restore_flags(flags);
1863     
1864     	/* 
1865     	 * This is currently a .5 second timeout, since (in theory) no slow 
1866     	 * board will take that long.  In practice, we've seen one 
1867     	 * pentium which occasionally fails with this, but works with 
1868     	 * 10 times as much?
1869     	 */
1870     
1871     	timeout = jiffies + 5 * HZ / 10;
1872     	while ((hostdata->test_completed == -1) && jiffies < timeout)
1873     		barrier();
1874     
1875     	failed = 1;
1876     	if (hostdata->test_completed == -1)
1877     	    printk ("scsi%d : driver test 1 timed out%s\n",host->host_no ,
1878     		(hostdata->test_dest == 0xdeadbeef) ? 
1879     		    " due to lost interrupt.\n"
1880     		    "         Please verify that the correct IRQ is being used for your board,\n"
1881     		    "	      and that the motherboard IRQ jumpering matches the PCI setup on\n"
1882     		    "         PCI systems.\n"
1883     		    "         If you are using a NCR53c810 board in a PCI system, you should\n" 
1884     		    "         also verify that the board is jumpered to use PCI INTA, since\n"
1885     		    "         most PCI motherboards lack support for INTB, INTC, and INTD.\n"
1886     		    : "");
1887           else if (hostdata->test_completed != 1) 
1888     	    printk ("scsi%d : test 1 bad interrupt value (%d)\n", 
1889     		host->host_no, hostdata->test_completed);
1890     	else 
1891     	    failed = (hostdata->test_dest != 0xdeadbeef);
1892     
1893     	if (hostdata->test_dest != 0xdeadbeef) {
1894     	    printk ("scsi%d : driver test 1 read 0x%x instead of 0xdeadbeef indicating a\n"
1895                         "         probable cache invalidation problem.  Please configure caching\n"
1896     		    "         as write-through or disabled\n",
1897     		host->host_no, hostdata->test_dest);
1898     	}
1899     
1900     	if (failed) {
1901     	    printk ("scsi%d : DSP = 0x%p (script at 0x%p, start at 0x%x)\n",
1902     		host->host_no, bus_to_virt(NCR53c7x0_read32(DSP_REG)),
1903     		hostdata->script, start);
1904     	    printk ("scsi%d : DSPS = 0x%x\n", host->host_no,
1905     		NCR53c7x0_read32(DSPS_REG));
1906     	    return -1;
1907     	}
1908         	hostdata->test_running = 0;
1909         }
1910     
1911         if ((hostdata->options & OPTION_DEBUG_TEST2) && 
1912     	hostdata->state != STATE_DISABLED) {
1913     	u32 dsa[48];
1914         	unsigned char identify = IDENTIFY(0, 0);
1915     	unsigned char cmd[6];
1916     	unsigned char data[36];
1917         	unsigned char status = 0xff;
1918         	unsigned char msg = 0xff;
1919     
1920         	cmd[0] = INQUIRY;
1921         	cmd[1] = cmd[2] = cmd[3] = cmd[5] = 0;
1922         	cmd[4] = sizeof(data); 
1923     
1924     /* Need to adjust for endian-ness */    	
1925         	dsa[2] = le32_to_cpu(1);
1926         	dsa[3] = le32_to_cpu(virt_to_bus(&identify));
1927         	dsa[4] = le32_to_cpu(6);
1928         	dsa[5] = le32_to_cpu(virt_to_bus(&cmd));
1929         	dsa[6] = le32_to_cpu(sizeof(data));
1930         	dsa[7] = le32_to_cpu(virt_to_bus(&data));
1931         	dsa[8] = le32_to_cpu(1);
1932         	dsa[9] = le32_to_cpu(virt_to_bus(&status));
1933         	dsa[10] = le32_to_cpu(1);
1934         	dsa[11] = le32_to_cpu(virt_to_bus(&msg));
1935     
1936     	for (i = 0; i < 3; ++i) {
1937     	    cli();
1938     	    if (!hostdata->idle) {
1939     		printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
1940     		restore_flags(flags);
1941     		return -1;
1942     	    }
1943     
1944     	    /*	     SCNTL3         SDID	*/
1945     	    dsa[0] = le32_to_cpu((0x33 << 24) | (i << 16))  ;
1946     	    hostdata->idle = 0;
1947     	    hostdata->test_running = 2;
1948     	    hostdata->test_completed = -1;
1949     	    start = virt_to_bus(hostdata->script) + hostdata->E_test_2;
1950     	    hostdata->state = STATE_RUNNING;
1951     	    NCR53c7x0_write32 (DSA_REG, virt_to_bus(dsa));
1952     	    NCR53c7x0_write32 (DSP_REG, start);
1953     	    restore_flags(flags);
1954     
1955     	    timeout = jiffies + 5 * HZ;	/* arbitrary */
1956     	    while ((hostdata->test_completed == -1) && jiffies < timeout)
1957     	    	barrier();
1958     	    NCR53c7x0_write32 (DSA_REG, 0);
1959     
1960     	    if (hostdata->test_completed == 2) {
1961     		data[35] = 0;
1962     		printk ("scsi%d : test 2 INQUIRY to target %d, lun 0 : %s\n",
1963     		    host->host_no, i, data + 8);
1964     		printk ("scsi%d : status ", host->host_no);
1965     		print_status (status);
1966     		printk ("\nscsi%d : message ", host->host_no);
1967     		print_msg (&msg);
1968     		printk ("\n");
1969     	    } else if (hostdata->test_completed == 3) {
1970     		printk("scsi%d : test 2 no connection with target %d\n",
1971     		    host->host_no, i);
1972     		if (!hostdata->idle) {
1973     		    printk("scsi%d : not idle\n", host->host_no);
1974     		    restore_flags(flags);
1975     		    return -1;
1976     		}
1977     	    } else if (hostdata->test_completed == -1) {
1978     		printk ("scsi%d : test 2 timed out\n", host->host_no);
1979     		restore_flags(flags);
1980     		return -1;
1981     	    } 
1982     	    hostdata->test_running = 0;
1983     	}
1984         }
1985     
1986         restore_flags(flags);
1987         return 0;
1988     }
1989     
1990     /*
1991      * Function : static void NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd)
1992      *
1993      * Purpose : copy the NCR53c8xx dsa structure into cmd's dsa buffer,
1994      * 	performing all necessary relocation.
1995      *
1996      * Inputs : cmd, a NCR53c7x0_cmd structure with a dsa area large
1997      *	enough to hold the NCR53c8xx dsa.
1998      */
1999     
2000     static void 
2001     NCR53c8xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
2002         Scsi_Cmnd *c = cmd->cmd;
2003         struct Scsi_Host *host = c->host;
2004         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2005         	host->hostdata;
2006         int i;
2007     #ifdef __powerpc__
2008         int len;
2009         unsigned long *dsa_ptr;
2010     #endif    
2011     
2012         memcpy (cmd->dsa, hostdata->script + (hostdata->E_dsa_code_template / 4),
2013         	hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template);
2014     #ifdef __powerpc__
2015         /* Note: the script has already been 'endianized' */
2016         dsa_ptr = cmd->dsa;
2017         len = hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template;
2018         for (i = 0;  i < len;  i += sizeof(long))
2019         {
2020            *dsa_ptr++ = le32_to_cpu(*dsa_ptr);
2021         }
2022     #endif    	
2023     
2024         /* 
2025          * Note : within the NCR 'C' code, dsa points to the _start_
2026          * of the DSA structure, and _not_ the offset of dsa_zero within
2027          * that structure used to facilitate shorter signed offsets
2028          * for the 8 bit ALU.
2029          * 
2030          * The implications of this are that 
2031          * 
2032          * - 32 bit A_dsa_* absolute values require an additional 
2033          * 	 dsa_zero added to their value to be correct, since they are 
2034          *   relative to dsa_zero which is in essentially a separate
2035          *   space from the code symbols.
2036          *
2037          * - All other symbols require no special treatment.
2038          */
2039     
2040         patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2041         	dsa_temp_lun, c->lun);
2042         patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2043     	dsa_temp_addr_next, virt_to_bus(&cmd->dsa_next_addr));
2044         patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2045         	dsa_temp_next, virt_to_bus(cmd->dsa) + Ent_dsa_zero -
2046     	Ent_dsa_code_template + A_dsa_next);
2047         patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32), 
2048         	dsa_temp_sync, virt_to_bus((void *)hostdata->sync[c->target].script));
2049         patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2050         	dsa_temp_target, c->target);
2051         /* XXX - new pointer stuff */
2052         patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2053         	dsa_temp_addr_saved_pointer, virt_to_bus(&cmd->saved_data_pointer));
2054         patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2055         	dsa_temp_addr_saved_residual, virt_to_bus(&cmd->saved_residual));
2056         patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2057         	dsa_temp_addr_residual, virt_to_bus(&cmd->residual));
2058     
2059         /*  XXX - new start stuff */
2060         patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
2061     	dsa_temp_addr_dsa_value, virt_to_bus(&cmd->dsa_addr));
2062     #ifdef __powerpc__
2063         dsa_ptr = cmd->dsa;    
2064         len = hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template;
2065         for (i = 0;  i < len;  i += sizeof(long))
2066         {
2067            *dsa_ptr++ = le32_to_cpu(*dsa_ptr);
2068         }
2069     #endif    	
2070     
2071     }
2072     
2073     /* 
2074      * Function : run_process_issue_queue (void)
2075      * 
2076      * Purpose : insure that the coroutine is running and will process our 
2077      * 	request.  process_issue_queue_running is checked/set here (in an 
2078      *	inline function) rather than in process_issue_queue itself to reduce 
2079      * 	the chances of stack overflow.
2080      *
2081      */
2082     
2083     static volatile int process_issue_queue_running = 0;
2084     
2085     static __inline__ void 
2086     run_process_issue_queue(void) {
2087         unsigned long flags;
2088         save_flags (flags);
2089         cli();
2090         if (!process_issue_queue_running) {
2091     	process_issue_queue_running = 1;
2092             process_issue_queue(flags);
2093     	/* 
2094              * process_issue_queue_running is cleared in process_issue_queue 
2095     	 * once it can't do more work, and process_issue_queue exits with 
2096     	 * interrupts disabled.
2097     	 */
2098         }
2099         restore_flags (flags);
2100     }
2101     
2102     /*
2103      * Function : static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int
2104      *	result)
2105      *
2106      * Purpose : mark SCSI command as finished, OR'ing the host portion 
2107      *	of the result word into the result field of the corresponding
2108      *	Scsi_Cmnd structure, and removing it from the internal queues.
2109      *
2110      * Inputs : cmd - command, result - entire result field
2111      *
2112      * Preconditions : the 	NCR chip should be in a halted state when 
2113      *	abnormal_finished is run, since it modifies structures which
2114      *	the NCR expects to have exclusive access to.
2115      */
2116     
2117     static void 
2118     abnormal_finished (struct NCR53c7x0_cmd *cmd, int result) {
2119         Scsi_Cmnd *c = cmd->cmd;
2120         struct Scsi_Host *host = c->host;
2121         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2122         	host->hostdata;
2123         unsigned long flags;
2124         int left, found;
2125         volatile struct NCR53c7x0_cmd * linux_search;
2126         volatile struct NCR53c7x0_cmd * volatile *linux_prev;
2127         volatile u32 *ncr_prev, *curr, ncr_search;
2128     
2129     #if 0
2130         printk ("scsi%d: abnormal finished\n", host->host_no);
2131     #endif
2132     
2133         save_flags(flags);
2134         cli();
2135         found = 0;
2136         /* 
2137          * Traverse the NCR issue array until we find a match or run out 
2138          * of instructions.  Instructions in the NCR issue array are 
2139          * either JUMP or NOP instructions, which are 2 words in length.
2140          */
2141     
2142     
2143         for (found = 0, left = host->can_queue, curr = hostdata->schedule; 
2144     	left > 0; --left, curr += 2)
2145         {
2146     	if (issue_to_cmd (host, hostdata, (u32 *) curr) == cmd) 
2147     	{
2148     	    curr[0] = hostdata->NOP_insn;
2149     	    curr[1] = le32_to_cpu(0xdeadbeef);
2150     	    ++found;
2151     	    break;
2152     	}
2153         }
2154     	
2155         /* 
2156          * Traverse the NCR reconnect list of DSA structures until we find 
2157          * a pointer to this dsa or have found too many command structures.  
2158          * We let prev point at the next field of the previous element or 
2159          * head of the list, so we don't do anything different for removing 
2160          * the head element.  
2161          */
2162     
2163         for (left = host->can_queue,
2164     	    ncr_search = le32_to_cpu(hostdata->reconnect_dsa_head), 
2165     	    ncr_prev = &hostdata->reconnect_dsa_head;
2166     	left >= 0 && ncr_search && 
2167     	    ((char*)bus_to_virt(ncr_search) + hostdata->dsa_start) 
2168     		!= (char *) cmd->dsa;
2169     	ncr_prev = (u32*) ((char*)bus_to_virt(ncr_search) + 
2170     	    hostdata->dsa_next), ncr_search = le32_to_cpu(*ncr_prev), --left);
2171     
2172         if (left < 0) {
2173     	printk("scsi%d: loop detected in ncr reconnect list\n",
2174     	    host->host_no);
2175         } else if (ncr_search) {
2176     	if (found)
2177     	    printk("scsi%d: scsi %ld in ncr issue array and reconnect lists\n",
2178     		host->host_no, c->pid);
2179     	else {
2180     	    volatile u32 * next = (u32 *) 
2181     	    	((char *)bus_to_virt(ncr_search) + hostdata->dsa_next);
2182     	    *ncr_prev = *next;
2183     /* If we're at the tail end of the issue queue, update that pointer too. */
2184     	    found = 1;
2185     	}
2186         }
2187     
2188         /*
2189          * Traverse the host running list until we find this command or discover
2190          * we have too many elements, pointing linux_prev at the next field of the 
2191          * linux_previous element or head of the list, search at this element.
2192          */
2193     
2194         for (left = host->can_queue, linux_search = hostdata->running_list, 
2195     	    linux_prev = &hostdata->running_list;
2196     	left >= 0 && linux_search && linux_search != cmd;
2197     	linux_prev = &(linux_search->next), 
2198     	    linux_search = linux_search->next, --left);
2199         
2200         if (left < 0) 
2201     	printk ("scsi%d: loop detected in host running list for scsi pid %ld\n",
2202     	    host->host_no, c->pid);
2203         else if (linux_search) {
2204     	*linux_prev = linux_search->next;
2205     	--hostdata->busy[c->target][c->lun];
2206         }
2207     
2208         /* Return the NCR command structure to the free list */
2209         cmd->next = hostdata->free;
2210         hostdata->free = cmd;
2211         c->host_scribble = NULL;
2212     
2213         /* And return */
2214         c->result = result;
2215         c->scsi_done(c);
2216     
2217         restore_flags(flags);
2218         run_process_issue_queue();
2219     }
2220     
2221     /* 
2222      * Function : static void intr_break (struct Scsi_Host *host,
2223      * 	struct NCR53c7x0_cmd *cmd)
2224      *
2225      * Purpose :  Handler for breakpoint interrupts from a SCSI script
2226      *
2227      * Inputs : host - pointer to this host adapter's structure,
2228      * 	cmd - pointer to the command (if any) dsa was pointing 
2229      * 	to.
2230      *
2231      */
2232     
2233     static void 
2234     intr_break (struct Scsi_Host *host, struct 
2235         NCR53c7x0_cmd *cmd) {
2236         NCR53c7x0_local_declare();
2237         struct NCR53c7x0_break *bp;
2238     #if 0
2239         Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
2240     #endif
2241         u32 *dsp;
2242         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2243     	host->hostdata;		
2244         unsigned long flags;
2245         NCR53c7x0_local_setup(host);
2246     
2247         /*
2248          * Find the break point corresponding to this address, and 
2249          * dump the appropriate debugging information to standard 
2250          * output.  
2251          */
2252         save_flags(flags);
2253         cli();
2254         dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
2255         for (bp = hostdata->breakpoints; bp && bp->address != dsp; 
2256         	bp = bp->next);
2257         if (!bp) 
2258         	panic("scsi%d : break point interrupt from %p with no breakpoint!",
2259         	    host->host_no, dsp);
2260     
2261         /*
2262          * Configure the NCR chip for manual start mode, so that we can 
2263          * point the DSP register at the instruction that follows the 
2264          * INT int_debug_break instruction.
2265          */
2266     
2267         NCR53c7x0_write8 (hostdata->dmode, 
2268     	NCR53c7x0_read8(hostdata->dmode)|DMODE_MAN);
2269     
2270         /*
2271          * And update the DSP register, using the size of the old 
2272          * instruction in bytes.
2273          */
2274     
2275         restore_flags(flags);
2276     }
2277     /*
2278      * Function : static void print_synchronous (const char *prefix, 
2279      *	const unsigned char *msg)
2280      * 
2281      * Purpose : print a pretty, user and machine parsable representation
2282      *	of a SDTR message, including the "real" parameters, data
2283      *	clock so we can tell transfer rate at a glance.
2284      *
2285      * Inputs ; prefix - text to prepend, msg - SDTR message (5 bytes)
2286      */
2287     
2288     static void
2289     print_synchronous (const char *prefix, const unsigned char *msg) {
2290         if (msg[4]) {
2291     	int Hz = 1000000000 / (msg[3] * 4);
2292     	int integer = Hz / 1000000;
2293     	int fraction = (Hz - (integer * 1000000)) / 10000;
2294     	printk ("%speriod %dns offset %d %d.%02dMHz %s SCSI%s\n",
2295     	    prefix, (int) msg[3] * 4, (int) msg[4], integer, fraction,
2296     	    (((msg[3] * 4) < 200) ? "FAST" : "synchronous"),
2297     	    (((msg[3] * 4) < 200) ? "-II" : ""));
2298         } else 
2299     	printk ("%sasynchronous SCSI\n", prefix);
2300     }
2301     
2302     /*
2303      * Function : static void set_synchronous (struct Scsi_Host *host, 
2304      *	 	int target, int sxfer, int scntl3, int now_connected)
2305      *
2306      * Purpose : reprogram transfers between the selected SCSI initiator and 
2307      *	target with the given register values; in the indirect
2308      *	select operand, reselection script, and chip registers.
2309      *
2310      * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
2311      *	sxfer and scntl3 - NCR registers. now_connected - if non-zero, 
2312      *	we should reprogram the registers now too.
2313      */
2314     
2315     static void
2316     set_synchronous (struct Scsi_Host *host, int target, int sxfer, int scntl3,
2317         int now_connected) {
2318         NCR53c7x0_local_declare();
2319         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *) 
2320     	host->hostdata;
2321         u32 *script;
2322         NCR53c7x0_local_setup(host);
2323     
2324         /* These are eight bit registers */
2325         sxfer &= 0xff;
2326         scntl3 &= 0xff;
2327     
2328         hostdata->sync[target].sxfer_sanity = sxfer;
2329         hostdata->sync[target].scntl3_sanity = scntl3;
2330     
2331     /* 
2332      * HARD CODED : synchronous script is EIGHT words long.  This 
2333      * must agree with 53c7.8xx.h
2334      */
2335     
2336         if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
2337     	hostdata->sync[target].select_indirect = (scntl3 << 24) | 
2338     	    (target << 16) | (sxfer << 8);
2339     
2340     	script = (u32 *) hostdata->sync[target].script;
2341     
2342     	/* XXX - add NCR53c7x0 code to reprogram SCF bits if we want to */
2343     	if ((hostdata->chip / 100) == 8) {
2344     	    script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
2345     		DCMD_RWRI_OP_MOVE) << 24) |
2346     		(SCNTL3_REG_800 << 16) | (scntl3 << 8);
2347     	    script[1] = 0;
2348     	    script += 2;
2349     	}
2350     
2351     	script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
2352     	    DCMD_RWRI_OP_MOVE) << 24) |
2353     		(SXFER_REG << 16) | (sxfer << 8);
2354     	script[1] = 0;
2355     	script += 2;
2356     
2357     #ifdef DEBUG_SYNC_INTR
2358     	if (hostdata->options & OPTION_DEBUG_DISCONNECT) {
2359     	    script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_INT) << 24) | DBC_TCI_TRUE;
2360     	    script[1] = DEBUG_SYNC_INTR;
2361     	    script += 2;
2362     	}
2363     #endif
2364     
2365     	script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24) | DBC_TCI_TRUE;
2366     	script[1] = 0;
2367     	script += 2;
2368         }
2369     
2370         if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS) 
2371     	printk ("scsi%d : target %d sync parameters are sxfer=0x%x, scntl3=0x%x\n",
2372     	host->host_no, target, sxfer, scntl3);
2373     
2374         if (now_connected) {
2375     	if ((hostdata->chip / 100) == 8) 
2376     	    NCR53c7x0_write8(SCNTL3_REG_800, scntl3);
2377     	NCR53c7x0_write8(SXFER_REG, sxfer);
2378         }
2379     }
2380     
2381     
2382     /*
2383      * Function : static int asynchronous (struct Scsi_Host *host, int target)
2384      *
2385      * Purpose : reprogram between the selected SCSI Host adapter and target 
2386      *      (assumed to be currently connected) for asynchronous transfers.
2387      *
2388      * Inputs : host - SCSI host structure, target - numeric target ID.
2389      *
2390      * Preconditions : the NCR chip should be in one of the halted states
2391      */
2392         
2393     static void
2394     asynchronous (struct Scsi_Host *host, int target) {
2395         NCR53c7x0_local_declare();
2396         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2397     	host->hostdata;
2398         NCR53c7x0_local_setup(host);
2399         set_synchronous (host, target, /* no offset */ 0, hostdata->saved_scntl3,
2400     	1);
2401         printk ("scsi%d : setting target %d to asynchronous SCSI\n",
2402     	host->host_no, target);
2403     }
2404     
2405     /* 
2406      * XXX - do we want to go out of our way (ie, add extra code to selection
2407      * 	in the NCR53c710/NCR53c720 script) to reprogram the synchronous
2408      * 	conversion bits, or can we be content in just setting the 
2409      * 	sxfer bits?
2410      */
2411     
2412     /* Table for NCR53c8xx synchronous values */
2413     static const struct {
2414         int div;		/* Total clock divisor * 10 */
2415         unsigned char scf;	/* */
2416         unsigned char tp;	/* 4 + tp = xferp divisor */
2417     } syncs[] = {
2418     /*	div	scf	tp	div	scf	tp	div	scf	tp */
2419         {	40,	1,	0}, {	50,	1,	1}, {	60,	1,	2}, 
2420         {	70,	1,	3}, {	75,	2,	1}, {	80,	1,	4},
2421         {	90,	1,	5}, {	100,	1,	6}, {	105,	2,	3},
2422         {	110,	1,	7}, {	120,	2,	4}, {	135,	2,	5},
2423         {	140,	3,	3}, {	150,	2,	6}, {	160,	3,	4},
2424         {	165,	2,	7}, {	180,	3,	5}, {	200,	3,	6},
2425         {	210,	4,	3}, {	220,	3,	7}, {	240,	4,	4},
2426         {	270,	4,	5}, {	300,	4,	6}, {	330,	4,	7}
2427     };
2428     
2429     /*
2430      * Function : static void synchronous (struct Scsi_Host *host, int target, 
2431      *	char *msg)
2432      *
2433      * Purpose : reprogram transfers between the selected SCSI initiator and 
2434      *	target for synchronous SCSI transfers such that the synchronous 
2435      *	offset is less than that requested and period at least as long 
2436      *	as that requested.  Also modify *msg such that it contains 
2437      *	an appropriate response. 
2438      *
2439      * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
2440      *	msg - synchronous transfer request.
2441      */
2442     
2443     
2444     static void 
2445     synchronous (struct Scsi_Host *host, int target, char *msg) {
2446         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2447     	host->hostdata;
2448         int desire, divisor, i, limit;
2449         unsigned char scntl3, sxfer;
2450     /* The diagnostic message fits on one line, even with max. width integers */
2451         char buf[80];	
2452        
2453     /* Desired transfer clock in Hz */
2454         desire = 1000000000L / (msg[3] * 4);
2455     /* Scale the available SCSI clock by 10 so we get tenths */
2456         divisor = (hostdata->scsi_clock * 10) / desire;
2457     
2458     /* NCR chips can handle at most an offset of 8 */
2459         if (msg[4] > 8)
2460     	msg[4] = 8;
2461     
2462         if (hostdata->options & OPTION_DEBUG_SDTR)
2463         	printk("scsi%d : optimal synchronous divisor of %d.%01d\n", 
2464     	    host->host_no, divisor / 10, divisor % 10);
2465     
2466         limit = (sizeof(syncs) / sizeof(syncs[0]) -1);
2467         for (i = 0; (i < limit) && (divisor > syncs[i].div); ++i);
2468     
2469         if (hostdata->options & OPTION_DEBUG_SDTR)
2470         	printk("scsi%d : selected synchronous divisor of %d.%01d\n", 
2471     	    host->host_no, syncs[i].div / 10, syncs[i].div % 10);
2472     
2473         msg[3] = ((1000000000L / hostdata->scsi_clock) * syncs[i].div / 10 / 4);
2474     
2475         if (hostdata->options & OPTION_DEBUG_SDTR)
2476         	printk("scsi%d : selected synchronous period of %dns\n", host->host_no,
2477     	    msg[3] * 4);
2478     
2479         scntl3 = (hostdata->chip / 100 == 8) ? ((hostdata->saved_scntl3 & 
2480     	~SCNTL3_800_SCF_MASK) | (syncs[i].scf << SCNTL3_800_SCF_SHIFT)) : 0;
2481         sxfer = (msg[4] << SXFER_MO_SHIFT) | ((syncs[i].tp) << SXFER_TP_SHIFT);
2482         if (hostdata->options & OPTION_DEBUG_SDTR)
2483         	printk ("scsi%d : sxfer=0x%x scntl3=0x%x\n", 
2484     	    host->host_no, (int) sxfer, (int) scntl3);
2485         set_synchronous (host, target, sxfer, scntl3, 1);
2486         sprintf (buf, "scsi%d : setting target %d to ", host->host_no, target);
2487         print_synchronous (buf, msg);
2488     }
2489     
2490     /* 
2491      * Function : static int NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host,
2492      * 	struct NCR53c7x0_cmd *cmd)
2493      *
2494      * Purpose :  Handler for INT generated instructions for the 
2495      * 	NCR53c810/820 SCSI SCRIPT
2496      *
2497      * Inputs : host - pointer to this host adapter's structure,
2498      * 	cmd - pointer to the command (if any) dsa was pointing 
2499      * 	to.
2500      *
2501      */
2502     
2503     static int 
2504     NCR53c8x0_dstat_sir_intr (struct Scsi_Host *host, struct 
2505         NCR53c7x0_cmd *cmd) {
2506         NCR53c7x0_local_declare();
2507         int print;
2508         Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
2509         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
2510     	host->hostdata;		
2511         u32 dsps,*dsp;	/* Argument of the INT instruction */
2512         NCR53c7x0_local_setup(host);
2513         dsps = NCR53c7x0_read32(DSPS_REG);
2514         dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
2515     
2516         if (hostdata->options & OPTION_DEBUG_INTR)
2517     	printk ("scsi%d : DSPS = 0x%x\n", host->host_no, dsps);
2518     
2519         switch (dsps) {
2520         case A_int_msg_1:
2521     	print = 1;
2522     	switch (hostdata->msg_buf[0]) {
2523     	/* 
2524     	 * Unless we've initiated synchronous negotiation, I don't
2525     	 * think that this should happen.
2526     	 */
2527     	case MESSAGE_REJECT:
2528     	    hostdata->dsp = hostdata->script + hostdata->E_accept_message /
2529     		sizeof(u32);
2530     	    hostdata->dsp_changed = 1;
2531     	    if (cmd && (cmd->flags & CMD_FLAG_SDTR)) {
2532     		printk ("scsi%d : target %d rejected SDTR\n", host->host_no, 
2533     		    c->target);
2534     		cmd->flags &= ~CMD_FLAG_SDTR;
2535     		asynchronous (host, c->target);
2536     		print = 0;
2537     	    } 
2538     	    break;
2539     	case INITIATE_RECOVERY:
2540     	    printk ("scsi%d : extended contingent allegiance not supported yet, rejecting\n",
2541     		host->host_no);
2542     	    /* Fall through to default */
2543     	    hostdata->dsp = hostdata->script + hostdata->E_reject_message /
2544     		sizeof(u32);
2545     	    hostdata->dsp_changed = 1;
2546     	    break;
2547     	default:
2548     	    printk ("scsi%d : unsupported message, rejecting\n",
2549     		host->host_no);
2550     	    hostdata->dsp = hostdata->script + hostdata->E_reject_message /
2551     		sizeof(u32);
2552     	    hostdata->dsp_changed = 1;
2553     	}
2554     	if (print) {
2555     	    printk ("scsi%d : received message", host->host_no);
2556     	    if (c) 
2557     	    	printk (" from target %d lun %d ", c->target, c->lun);
2558     	    print_msg ((unsigned char *) hostdata->msg_buf);
2559     	    printk("\n");
2560     	}
2561     	
2562     	return SPECIFIC_INT_NOTHING;
2563     
2564     
2565         case A_int_msg_sdtr:
2566     /*
2567      * At this point, hostdata->msg_buf contains
2568      * 0 EXTENDED MESSAGE
2569      * 1 length 
2570      * 2 SDTR
2571      * 3 period * 4ns
2572      * 4 offset
2573      */
2574     
2575     	if (cmd) {
2576     	    char buf[80];
2577     	    sprintf (buf, "scsi%d : target %d %s ", host->host_no, c->target,
2578     		(cmd->flags & CMD_FLAG_SDTR) ? "accepting" : "requesting");
2579     	    print_synchronous (buf, (unsigned char *) hostdata->msg_buf);
2580     
2581     	/* 
2582     	 * Initiator initiated, won't happen unless synchronous 
2583     	 * 	transfers are enabled.  If we get a SDTR message in
2584     	 * 	response to our SDTR, we should program our parameters
2585     	 * 	such that 
2586     	 *		offset <= requested offset
2587     	 *		period >= requested period		 	
2588        	 */
2589     	    if (cmd->flags & CMD_FLAG_SDTR) {
2590     		cmd->flags &= ~CMD_FLAG_SDTR; 
2591     		if (hostdata->msg_buf[4]) 
2592     		    synchronous (host, c->target, (unsigned char *) 
2593     		    	hostdata->msg_buf);
2594     		else 
2595     		    asynchronous (host, c->target);
2596     		hostdata->dsp = hostdata->script + hostdata->E_accept_message /
2597     		    sizeof(u32);
2598     		hostdata->dsp_changed = 1;
2599     		return SPECIFIC_INT_NOTHING;
2600     	    } else {
2601     		if (hostdata->options & OPTION_SYNCHRONOUS)  {
2602     		    cmd->flags |= CMD_FLAG_DID_SDTR;
2603     		    synchronous (host, c->target, (unsigned char *) 
2604     			hostdata->msg_buf);
2605     		} else {
2606     		    hostdata->msg_buf[4] = 0;		/* 0 offset = async */
2607     		    asynchronous (host, c->target);
2608     		}
2609     		patch_dsa_32 (cmd->dsa, dsa_msgout_other, 0, le32_to_cpu(5));
2610     		patch_dsa_32 (cmd->dsa, dsa_msgout_other, 1, (u32) 
2611     		    le32_to_cpu(virt_to_bus ((void *)&hostdata->msg_buf)));
2612     		hostdata->dsp = hostdata->script + 
2613     		    hostdata->E_respond_message / sizeof(u32);
2614     		hostdata->dsp_changed = 1;
2615     	    }
2616     	    return SPECIFIC_INT_NOTHING;
2617     	}
2618     	/* Fall through to abort if we couldn't find a cmd, and 
2619     	   therefore a dsa structure to twiddle */
2620         case A_int_msg_wdtr:
2621     	hostdata->dsp = hostdata->script + hostdata->E_reject_message /
2622     	    sizeof(u32);
2623     	hostdata->dsp_changed = 1;
2624     	return SPECIFIC_INT_NOTHING;
2625         case A_int_err_unexpected_phase:
2626     	if (hostdata->options & OPTION_DEBUG_INTR) 
2627     	    printk ("scsi%d : unexpected phase\n", host->host_no);
2628     	return SPECIFIC_INT_ABORT;
2629         case A_int_err_selected:
2630     	printk ("scsi%d : selected by target %d\n", host->host_no,
2631     	    (int) NCR53c7x0_read8(SDID_REG_800) &7);
2632     	hostdata->dsp = hostdata->script + hostdata->E_target_abort / 
2633         	    sizeof(u32);
2634     	hostdata->dsp_changed = 1;
2635     	return SPECIFIC_INT_NOTHING;
2636         case A_int_err_unexpected_reselect:
2637     	printk ("scsi%d : unexpected reselect by target %d lun %d\n", 
2638     	    host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & 7,
2639     	    hostdata->reselected_identify & 7);
2640     	hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
2641         	    sizeof(u32);
2642     	hostdata->dsp_changed = 1;
2643     	return SPECIFIC_INT_NOTHING;
2644     /*
2645      * Since contingent allegiance conditions are cleared by the next 
2646      * command issued to a target, we must issue a REQUEST SENSE 
2647      * command after receiving a CHECK CONDITION status, before
2648      * another command is issued.
2649      * 
2650      * Since this NCR53c7x0_cmd will be freed after use, we don't 
2651      * care if we step on the various fields, so modify a few things.
2652      */
2653         case A_int_err_check_condition: 
2654     #if 0
2655     	if (hostdata->options & OPTION_DEBUG_INTR) 
2656     #endif
2657     	    printk ("scsi%d : CHECK CONDITION\n", host->host_no);
2658     	if (!c) {
2659     	    printk("scsi%d : CHECK CONDITION with no SCSI command\n",
2660     		host->host_no);
2661     	    return SPECIFIC_INT_PANIC;
2662     	}
2663     
2664     	/* 
2665     	 * FIXME : this uses the normal one-byte selection message.
2666     	 * 	We may want to renegotiate for synchronous & WIDE transfers
2667     	 * 	since these could be the crux of our problem.
2668     	 *
2669     	 hostdata->NOP_insn* FIXME : once SCSI-II tagged queuing is implemented, we'll
2670     	 * 	have to set this up so that the rest of the DSA
2671     	 *	agrees with this being an untagged queue'd command.
2672     	 */
2673     
2674         	patch_dsa_32 (cmd->dsa, dsa_msgout, 0, le32_to_cpu(1));
2675     
2676         	/* 
2677         	 * Modify the table indirect for COMMAND OUT phase, since 
2678         	 * Request Sense is a six byte command.
2679         	 */
2680     
2681         	patch_dsa_32 (cmd->dsa, dsa_cmdout, 0, le32_to_cpu(6));
2682     
2683     	c->cmnd[0] = REQUEST_SENSE;
2684     	c->cmnd[1] &= 0xe0;	/* Zero all but LUN */
2685     	c->cmnd[2] = 0;
2686     	c->cmnd[3] = 0;
2687     	c->cmnd[4] = sizeof(c->sense_buffer);
2688     	c->cmnd[5] = 0; 
2689     
2690     	/*
2691     	 * Disable dataout phase, and program datain to transfer to the 
2692     	 * sense buffer, and add a jump to other_transfer after the 
2693         	 * command so overflow/underrun conditions are detected.
2694     	 */
2695     
2696         	patch_dsa_32 (cmd->dsa, dsa_dataout, 0, 
2697     	    le32_to_cpu(virt_to_bus(hostdata->script) + hostdata->E_other_transfer));
2698         	patch_dsa_32 (cmd->dsa, dsa_datain, 0, 
2699     	    le32_to_cpu(virt_to_bus(cmd->data_transfer_start)));
2700         	cmd->data_transfer_start[0] = le32_to_cpu((((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | 
2701         	    DCMD_BMI_IO)) << 24) | sizeof(c->sense_buffer));
2702         	cmd->data_transfer_start[1] = (u32) le32_to_cpu(virt_to_bus(c->sense_buffer));
2703     
2704     	cmd->data_transfer_start[2] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) 
2705         	    << 24) | DBC_TCI_TRUE);
2706     	cmd->data_transfer_start[3] = (u32) le32_to_cpu(virt_to_bus(hostdata->script) + 
2707     	    hostdata->E_other_transfer);
2708     
2709         	/*
2710         	 * Currently, this command is flagged as completed, ie 
2711         	 * it has valid status and message data.  Reflag it as
2712         	 * incomplete.  Q - need to do something so that original
2713     	 * status, etc are used.
2714         	 */
2715     
2716     	cmd->cmd->result = le32_to_cpu(0xffff);		
2717     
2718     	/* 
2719     	 * Restart command as a REQUEST SENSE.
2720     	 */
2721     	hostdata->dsp = (u32 *) hostdata->script + hostdata->E_select /
2722     	    sizeof(u32);
2723     	hostdata->dsp_changed = 1;
2724     	return SPECIFIC_INT_NOTHING;
2725         case A_int_debug_break:
2726     	return SPECIFIC_INT_BREAK;
2727         case A_int_norm_aborted:
2728     	hostdata->dsp = (u32 *) hostdata->schedule;
2729     	hostdata->dsp_changed = 1;
2730     	if (cmd)
2731     	    abnormal_finished (cmd, DID_ERROR << 16);
2732     	return SPECIFIC_INT_NOTHING;
2733         case A_int_test_1:
2734         case A_int_test_2:
2735     	hostdata->idle = 1;
2736     	hostdata->test_completed = (dsps - A_int_test_1) / 0x00010000 + 1;
2737     	if (hostdata->options & OPTION_DEBUG_INTR)
2738     	    printk("scsi%d : test%d complete\n", host->host_no,
2739     		hostdata->test_completed);
2740     	return SPECIFIC_INT_NOTHING;
2741     #ifdef A_int_debug_reselected_ok
2742         case A_int_debug_reselected_ok:
2743     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2744         	    	OPTION_DEBUG_DISCONNECT)) {
2745     	    /* 
2746     	     * Note - this dsa is not based on location relative to 
2747     	     * the command structure, but to location relative to the 
2748     	     * DSA register 
2749     	     */	
2750     	    u32 *dsa;
2751     	    dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
2752     
2753     	    printk("scsi%d : reselected_ok (DSA = 0x%x (virt 0x%p)\n", 
2754     		host->host_no, NCR53c7x0_read32(DSA_REG), dsa);
2755     	    printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
2756     		    host->host_no, cmd->saved_data_pointer,
2757     		    bus_to_virt(le32_to_cpu(cmd->saved_data_pointer)));
2758     	    print_insn (host, hostdata->script + Ent_reselected_ok / 
2759         	    	    sizeof(u32), "", 1);
2760         	    printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
2761     		host->host_no, NCR53c7x0_read8(SXFER_REG),
2762     		NCR53c7x0_read8(SCNTL3_REG_800));
2763     	    if (c) {
2764     		print_insn (host, (u32 *) 
2765     		    hostdata->sync[c->target].script, "", 1);
2766     		print_insn (host, (u32 *) 
2767     		    hostdata->sync[c->target].script + 2, "", 1);
2768     	    }
2769     	}
2770         	return SPECIFIC_INT_RESTART;
2771     #endif
2772     #ifdef A_int_debug_reselect_check
2773         case A_int_debug_reselect_check:
2774     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2775     	    u32 *dsa;
2776     #if 0
2777     	    u32 *code;
2778     #endif
2779     	    /* 
2780     	     * Note - this dsa is not based on location relative to 
2781     	     * the command structure, but to location relative to the 
2782     	     * DSA register 
2783     	     */	
2784     	    dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
2785     	    printk("scsi%d : reselected_check_next (DSA = 0x%lx (virt 0x%p))\n",
2786     		host->host_no, virt_to_bus(dsa), dsa);
2787     	    if (dsa) {
2788     		printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
2789     		    host->host_no, cmd->saved_data_pointer,
2790     		    bus_to_virt (le32_to_cpu(cmd->saved_data_pointer)));
2791     #if 0
2792     		printk("scsi%d : template code :\n", host->host_no);
2793     		for (code = dsa + (Ent_dsa_code_check_reselect - Ent_dsa_zero) 
2794     		    / sizeof(u32); code < (dsa + Ent_dsa_zero / sizeof(u32)); 
2795     		    code += print_insn (host, code, "", 1));
2796     #endif
2797     	    }
2798     	    print_insn (host, hostdata->script + Ent_reselected_ok / 
2799         	    	    sizeof(u32), "", 1);
2800     	}
2801         	return SPECIFIC_INT_RESTART;
2802     #endif
2803     #ifdef A_int_debug_dsa_schedule
2804         case A_int_debug_dsa_schedule:
2805     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2806     	    u32 *dsa;
2807     	    /* 
2808     	     * Note - this dsa is not based on location relative to 
2809     	     * the command structure, but to location relative to the 
2810     	     * DSA register 
2811     	     */	
2812     	    dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
2813     	    printk("scsi%d : dsa_schedule (old DSA = 0x%lx (virt 0x%p))\n", 
2814     		host->host_no, virt_to_bus(dsa), dsa);
2815     	    if (dsa) 
2816     		printk("scsi%d : resume address is 0x%x (virt 0x%p)\n"
2817     		       "         (temp was 0x%x (virt 0x%p))\n",
2818     		    host->host_no, cmd->saved_data_pointer,
2819     		    bus_to_virt (le32_to_cpu(cmd->saved_data_pointer)),
2820     		    NCR53c7x0_read32 (TEMP_REG),
2821     		    bus_to_virt (NCR53c7x0_read32(TEMP_REG)));
2822     	}
2823         	return SPECIFIC_INT_RESTART;
2824     #endif
2825     #ifdef A_int_debug_scheduled
2826         case A_int_debug_scheduled:
2827     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2828     	    printk("scsi%d : new I/O 0x%x (virt 0x%p) scheduled\n", 
2829     		host->host_no, NCR53c7x0_read32(DSA_REG),
2830     	    	bus_to_virt(NCR53c7x0_read32(DSA_REG)));
2831     	}
2832     	return SPECIFIC_INT_RESTART;
2833     #endif
2834     #ifdef A_int_debug_idle
2835         case A_int_debug_idle:
2836     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2837     	    printk("scsi%d : idle\n", host->host_no);
2838     	}
2839     	return SPECIFIC_INT_RESTART;
2840     #endif
2841     #ifdef A_int_debug_cmd
2842         case A_int_debug_cmd:
2843     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2844     	    printk("scsi%d : command sent\n");
2845     	}
2846         	return SPECIFIC_INT_RESTART;
2847     #endif
2848     #ifdef A_int_debug_dsa_loaded
2849         case A_int_debug_dsa_loaded:
2850     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2851     	    printk("scsi%d : DSA loaded with 0x%x (virt 0x%p)\n", host->host_no,
2852     		NCR53c7x0_read32(DSA_REG), 
2853     		bus_to_virt(NCR53c7x0_read32(DSA_REG)));
2854     	}
2855     	return SPECIFIC_INT_RESTART; 
2856     #endif
2857     #ifdef A_int_debug_reselected
2858         case A_int_debug_reselected:
2859     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2860     	    OPTION_DEBUG_DISCONNECT)) {
2861     	    printk("scsi%d : reselected by target %d lun %d\n",
2862     		host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & ~0x80, 
2863     		(int) hostdata->reselected_identify & 7);
2864     	    print_queues(host);
2865     	}
2866         	return SPECIFIC_INT_RESTART;
2867     #endif
2868     #ifdef A_int_debug_disconnect_msg
2869         case A_int_debug_disconnect_msg:
2870     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
2871     	    if (c)
2872     		printk("scsi%d : target %d lun %d disconnecting\n", 
2873     		    host->host_no, c->target, c->lun);
2874     	    else
2875     		printk("scsi%d : unknown target disconnecting\n",
2876     		    host->host_no);
2877     	}
2878     	return SPECIFIC_INT_RESTART;
2879     #endif
2880     #ifdef A_int_debug_disconnected
2881         case A_int_debug_disconnected:
2882     	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2883     		OPTION_DEBUG_DISCONNECT)) {
2884     	    printk ("scsi%d : disconnected, new queues are\n", 
2885     		host->host_no);
2886     	    print_queues(host);
2887     #if 0
2888         	    printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
2889     		host->host_no, NCR53c7x0_read8(SXFER_REG),
2890     		NCR53c7x0_read8(SCNTL3_REG_800));
2891     #endif
2892     	    if (c) {
2893     		print_insn (host, (u32 *) 
2894     		    hostdata->sync[c->target].script, "", 1);
2895     		print_insn (host, (u32 *) 
2896     		    hostdata->sync[c->target].script + 2, "", 1);
2897     	    }
2898     	}
2899     	return SPECIFIC_INT_RESTART;
2900     #endif
2901     #ifdef A_int_debug_panic
2902         case A_int_debug_panic:
2903     	printk("scsi%d : int_debug_panic received\n", host->host_no);
2904     	print_lots (host);
2905     	return SPECIFIC_INT_PANIC;
2906     #endif
2907     #ifdef A_int_debug_saved
2908         case A_int_debug_saved:
2909         	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2910         	    OPTION_DEBUG_DISCONNECT)) {
2911         	    printk ("scsi%d : saved data pointer 0x%x (virt 0x%p)\n",
2912         	    	host->host_no, cmd->saved_data_pointer,
2913     		bus_to_virt (le32_to_cpu(cmd->saved_data_pointer)));
2914         	    print_progress (c);
2915         	}
2916         	return SPECIFIC_INT_RESTART;
2917     #endif
2918     #ifdef A_int_debug_restored
2919         case A_int_debug_restored:
2920         	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2921         	    OPTION_DEBUG_DISCONNECT)) {
2922         	    if (cmd) {
2923     		int size;
2924         	    	printk ("scsi%d : restored data pointer 0x%x (virt 0x%p)\n",
2925         	    	    host->host_no, cmd->saved_data_pointer, bus_to_virt (
2926     		    le32_to_cpu(cmd->saved_data_pointer)));
2927     		size = print_insn (host, (u32 *) 
2928     		    bus_to_virt(le32_to_cpu(cmd->saved_data_pointer)), "", 1);
2929     		size = print_insn (host, (u32 *) 
2930     		    bus_to_virt(le32_to_cpu(cmd->saved_data_pointer)) + size, "", 1);
2931         	    	print_progress (c);
2932     	    }
2933     #if 0
2934     	    printk ("scsi%d : datapath residual %d\n",
2935     		host->host_no, datapath_residual (host)) ;
2936     #endif
2937         	}
2938         	return SPECIFIC_INT_RESTART;
2939     #endif
2940     #ifdef A_int_debug_sync
2941         case A_int_debug_sync:
2942         	if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2943         	    OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
2944     	    unsigned char sxfer = NCR53c7x0_read8 (SXFER_REG),
2945     		scntl3 = NCR53c7x0_read8 (SCNTL3_REG_800);
2946     	    if (c) {
2947     		if (sxfer != hostdata->sync[c->target].sxfer_sanity ||
2948     		    scntl3 != hostdata->sync[c->target].scntl3_sanity) {
2949     		   	printk ("scsi%d :  sync sanity check failed sxfer=0x%x, scntl3=0x%x",
2950     			    host->host_no, sxfer, scntl3);
2951     			NCR53c7x0_write8 (SXFER_REG, sxfer);
2952     			NCR53c7x0_write8 (SCNTL3_REG_800, scntl3);
2953     		    }
2954     	    } else 
2955         	    	printk ("scsi%d : unknown command sxfer=0x%x, scntl3=0x%x\n",
2956     		    host->host_no, (int) sxfer, (int) scntl3);
2957     	}
2958         	return SPECIFIC_INT_RESTART;
2959     #endif
2960     #ifdef A_int_debug_datain
2961     	case A_int_debug_datain:
2962     	    if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
2963     		OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
2964     		int size;
2965     		printk ("scsi%d : In do_datain (%s) sxfer=0x%x, scntl3=0x%x\n"
2966     			"         datapath residual=%d\n",
2967     		    host->host_no, sbcl_to_phase (NCR53c7x0_read8 (SBCL_REG)),
2968     		    (int) NCR53c7x0_read8(SXFER_REG), 
2969     		    (int) NCR53c7x0_read8(SCNTL3_REG_800),
2970     		    datapath_residual (host)) ;
2971     		print_insn (host, dsp, "", 1);
2972     		size = print_insn (host, (u32 *) bus_to_virt(le32_to_cpu(dsp[1])), "", 1);
2973     		print_insn (host, (u32 *) bus_to_virt(le32_to_cpu(dsp[1])) + size, "", 1);
2974     	   } 
2975     	return SPECIFIC_INT_RESTART;
2976     #endif
2977     /* 
2978      * FIXME : for 7xx support, we need to read SDID_REG_700 and handle 
2979      *	the comparison as bitfielded,  not binary.
2980      */
2981     #ifdef A_int_debug_check_dsa
2982     	case A_int_debug_check_dsa:
2983     	    if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
2984     		int sdid = NCR53c7x0_read8 (SDID_REG_800) & 15;
2985     		char *where = dsp - NCR53c7x0_insn_size(NCR53c7x0_read8 
2986     			(DCMD_REG)) == hostdata->script + 
2987     		    	Ent_select_check_dsa / sizeof(u32) ?
2988     		    "selection" : "reselection";
2989     		if (c && sdid != c->target) {
2990     		    printk ("scsi%d : SDID target %d != DSA target %d at %s\n",
2991     			host->host_no, sdid, c->target, where);
2992     		    print_lots(host);
2993     		    dump_events (host, 20);
2994     		    return SPECIFIC_INT_PANIC;
2995     		}
2996     	    }
2997     	    return SPECIFIC_INT_RESTART;
2998     #endif
2999         default:
3000     	if ((dsps & 0xff000000) == 0x03000000) {
3001     	     printk ("scsi%d : misc debug interrupt 0x%x\n",
3002     		host->host_no, dsps);
3003     	    return SPECIFIC_INT_RESTART;
3004     	} else if ((dsps & 0xff000000) == 0x05000000) {
3005     	    if (hostdata->events) {
3006     		struct NCR53c7x0_event *event;
3007     		++hostdata->event_index;
3008     		if (hostdata->event_index >= hostdata->event_size)
3009     		    hostdata->event_index = 0;
3010     		event = (struct NCR53c7x0_event *) hostdata->events + 
3011     		    hostdata->event_index;
3012     		event->event = (enum ncr_event) dsps;
3013     		event->dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
3014     	    /* FIXME : this needs to change for the '7xx family */
3015     		if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) 
3016     			event->target = NCR53c7x0_read8(SSID_REG_800);
3017     		else 
3018     			event->target = 255;
3019     
3020     		if (event->event == EVENT_RESELECT)
3021     		    event->lun = hostdata->reselected_identify & 0xf;
3022     		else if (c)
3023     		    event->lun = c->lun;
3024     		else
3025     		    event->lun = 255;
3026     		do_gettimeofday(&(event->time));
3027     		if (c) {
3028     		    event->pid = c->pid;
3029     		    memcpy ((void *) event->cmnd, (void *) c->cmnd, 
3030     			sizeof (event->cmnd));
3031     		} else {
3032     		    event->pid = -1;
3033     		}
3034     	    }
3035     	    return SPECIFIC_INT_RESTART;
3036     	}
3037     
3038     	printk ("scsi%d : unknown user interrupt 0x%x\n", 
3039     	    host->host_no, (unsigned) dsps);
3040     	return SPECIFIC_INT_PANIC;
3041         }
3042     }
3043     
3044     /* 
3045      * XXX - the stock NCR assembler won't output the scriptu.h file,
3046      * which undefine's all #define'd CPP symbols from the script.h
3047      * file, which will create problems if you use multiple scripts
3048      * with the same  symbol names.
3049      *
3050      * If you insist on using NCR's assembler, you could generate
3051      * scriptu.h from script.h using something like 
3052      *
3053      * grep #define script.h | \
3054      * sed 's/#define[ 	][ 	]*\([_a-zA-Z][_a-zA-Z0-9]*\).*$/#undefine \1/' \
3055      * > scriptu.h
3056      */
3057     
3058     #include "53c8xx_u.h"
3059     
3060     /* XXX - add alternate script handling code here */
3061     
3062     
3063     #ifdef NCR_DEBUG
3064     /*
3065      * Debugging without a debugger is no fun. So, I've provided 
3066      * a debugging interface in the NCR53c7x0 driver.  To avoid
3067      * kernel cruft, there's just enough here to act as an interface
3068      * to a user level debugger (aka, GDB).
3069      *
3070      *
3071      * The following restrictions apply to debugger commands : 
3072      * 1.  The command must be terminated by a newline.
3073      * 2.  Command length must be less than 80 bytes including the 
3074      * 	newline.
3075      * 3.  The entire command must be written with one system call.
3076      */
3077     
3078     static const char debugger_help = 
3079     "bc <addr> 			- clear breakpoint\n"
3080     "bl				- list breakpoints\n"
3081     "bs <addr>			- set breakpoint\n"
3082     "g				- start\n" 				
3083     "h				- halt\n"
3084     "?				- this message\n"
3085     "i				- info\n"
3086     "mp <addr> <size> 		- print memory\n"
3087     "ms <addr> <size> <value>	- store memory\n"
3088     "rp <num> <size>		- print register\n"
3089     "rs <num> <size> <value> 	- store register\n"
3090     "s                              - single step\n"	
3091     "tb				- begin trace \n"
3092     "te				- end trace\n";
3093     
3094     /*
3095      * Whenever we change a break point, we should probably 
3096      * set the NCR up so that it is in a single step mode.
3097      */
3098     
3099     static int debugger_fn_bc (struct Scsi_Host *host, struct debugger_token *token,
3100         u32 args[]) {
3101         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3102     	instance->hostdata;
3103         struct NCR53c7x0_break *bp, **prev;
3104         unsigned long flags;
3105         save_flags(flags);
3106         cli();
3107         for (bp = (struct NCR53c7x0_break *) instance->breakpoints,
3108     	    prev = (struct NCR53c7x0_break **) &instance->breakpoints;
3109     	    bp; prev = (struct NCR53c7x0_break **) &(bp->next),
3110     	    bp = (struct NCR53c7x0_break *) bp->next);
3111     
3112         if (!bp) {
3113     	restore_flags(flags);
3114     	return -EIO;
3115         }
3116     
3117         /* 
3118          * XXX - we need to insure that the processor is halted 
3119          * here in order to prevent a race condition.
3120          */
3121         
3122         memcpy ((void *) bp->addr, (void *) bp->old, sizeof(bp->old));
3123         if (prev)
3124     	*prev = bp->next;
3125     
3126         restore_flags(flags);
3127         return 0;
3128     }
3129     
3130     
3131     static int 
3132     debugger_fn_bl (struct Scsi_Host *host, struct debugger_token *token,
3133         u32 args[]) {
3134         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3135     	host->hostdata;
3136         struct NCR53c7x0_break *bp;
3137         char buf[80];
3138         size_t len;
3139         unsigned long flags;
3140         /* 
3141          * XXX - we need to insure that the processor is halted 
3142          * here in order to prevent a race condition.  So, if the 
3143          * processor isn't halted, print an error message and continue.
3144          */
3145     
3146         sprintf (buf, "scsi%d : bp : warning : processor not halted\b",
3147     	host->host_no);
3148         debugger_kernel_write (host, buf, strlen(buf));
3149     
3150         save_flags(flags);
3151         cli();
3152         for (bp = (struct NCR53c7x0_break *) host->breakpoints;
3153     	    bp; bp = (struct NCR53c7x0_break *) bp->next) {
3154     	    sprintf (buf, "scsi%d : bp : success : at %08x, replaces %08x %08x",
3155     		bp->addr, bp->old[0], bp->old[1]);
3156     	    len = strlen(buf);
3157     	    if ((bp->old[0] & (DCMD_TYPE_MASK << 24)) ==
3158     		(DCMD_TYPE_MMI << 24)) {
3159     		sprintf(buf + len, "%08x\n", * (u32 *) bp->addr);
3160     	    } else {
3161     		sprintf(buf + len, "\n");
3162     	    }
3163     	    len = strlen(buf);
3164     	    debugger_kernel_write (host, buf, len);
3165         }
3166         restore_flags(flags);
3167         return 0;
3168     }
3169     
3170     static int 
3171     debugger_fn_bs (struct Scsi_Host *host, struct debugger_token *token,
3172         u32 args[]) {
3173         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3174     	host->hostdata;
3175         struct NCR53c7x0_break *bp;
3176         char buf[80];
3177         size_t len;
3178         unsigned long flags;
3179         save_flags(flags);
3180         cli();
3181     
3182         if (hostdata->state != STATE_HALTED) {
3183     	sprintf (buf, "scsi%d : bs : failure : NCR not halted\n", host->host_no);
3184     	debugger_kernel_write (host, buf, strlen(buf));
3185     	restore_flags(flags);
3186     	return -1;
3187         }
3188     
3189         if (!(bp = kmalloc (sizeof (struct NCR53c7x0_break)))) {
3190     	printk ("scsi%d : kmalloc(%d) of breakpoint structure failed, try again\n",
3191     	    host->host_no, sizeof(struct NCR53c7x0_break));
3192     	restore_flags(flags);
3193     	return -1;
3194         }
3195     
3196         bp->address = (u32 *) args[0];
3197         memcpy ((void *) bp->old_instruction, (void *) bp->address, 8);
3198         bp->old_size = (((bp->old_instruction[0] >> 24) & DCMD_TYPE_MASK) ==
3199     	DCMD_TYPE_MMI ? 3 : 2;
3200         bp->next = hostdata->breakpoints;
3201         hostdata->breakpoints = bp->next;
3202         memcpy ((void *) bp->address, (void *) hostdata->E_debug_break, 8);
3203         
3204         restore_flags(flags);
3205         return 0;
3206     }
3207     
3208     #define TOKEN(name,nargs) {#name, nargs, debugger_fn_##name}
3209     static const struct debugger_token {
3210         char *name;
3211         int numargs;
3212         int (*fn)(struct debugger_token *token, u32 args[]);
3213     } debugger_tokens[] = {
3214         TOKEN(bc,1), TOKEN(bl,0), TOKEN(bs,1), TOKEN(g,0), TOKEN(halt,0),
3215         {DT_help, "?", 0} , TOKEN(h,0), TOKEN(i,0), TOKEN(mp,2), 
3216         TOKEN(ms,3), TOKEN(rp,2), TOKEN(rs,2), TOKEN(s,0), TOKEN(tb,0), TOKEN(te,0)
3217     };
3218     
3219     #define NDT sizeof(debugger_tokens / sizeof(struct debugger_token))
3220     
3221     static struct Scsi_Host * inode_to_host (struct inode *inode) {
3222         int dev;
3223         struct Scsi_Host *tmp;
3224         for (dev = MINOR(inode->rdev), host = first_host;
3225     	(host->hostt == the_template); --dev, host = host->next)
3226     	if (!dev) return host;
3227         return NULL;
3228     }
3229     
3230     
3231     static int
3232     debugger_user_write (struct inode *inode,struct file *filp,
3233         char *buf,int count) {
3234         struct Scsi_Host *host;			/* This SCSI host */
3235         struct NCR53c7x0_hostadata *hostdata;	
3236         char input_buf[80], 			/* Kernel space copy of buf */
3237     	*ptr;					/* Pointer to argument list */
3238         u32 args[3];				/* Arguments */
3239         int i, j, error, len;
3240     
3241         if (!(host = inode_to_host(inode)))
3242     	return -ENXIO;
3243     
3244         hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
3245     
3246         if (error = verify_area(VERIFY_READ,buf,count))
3247     	return error;
3248     
3249         if (count > 80) 
3250     	return -EIO;
3251     
3252         memcpy_from_fs(input_buf, buf, count);
3253     
3254         if (input_buf[count - 1] != '\n')
3255     	return -EIO;
3256     
3257         input_buf[count - 1]=0;
3258     
3259         for (i = 0; i < NDT; ++i) {
3260     	len = strlen (debugger_tokens[i].name);
3261     	if (!strncmp(input_buf, debugger_tokens[i].name, len)) 
3262     	    break;
3263         };
3264     
3265         if (i == NDT) 
3266     	return -EIO;
3267     
3268         for (ptr = input_buf + len, j = 0; j < debugger_tokens[i].nargs && *ptr;) {
3269     	if (*ptr == ' ' || *ptr == '\t') {
3270     	    ++ptr; 
3271     	} else if (isdigit(*ptr)) {
3272     	    args[j++] = simple_strtoul (ptr, &ptr, 0);
3273     	} else {
3274     	    return -EIO;
3275     	} 
3276         }
3277     
3278         if (j != debugger_tokens[i].nargs)
3279     	return -EIO;
3280     
3281         return count;
3282     } 
3283     
3284     static int 
3285     debugger_user_read (struct inode *inode,struct file *filp,
3286         char *buf,int count) {
3287         struct Scsi_Host *instance;
3288         
3289     }
3290     
3291     static int 
3292     debugger_kernel_write (struct Scsi_Host *host, char *buf, size_t
3293         buflen) {
3294         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3295     	host->hostdata;
3296         int copy, left;
3297         unsigned long flags;
3298         save_flags(flags);
3299         cli();
3300         while (buflen) {
3301     	left = (hostdata->debug_buf + hostdata->debug_size - 1) -
3302     	    hostdata->debug_write;
3303     	copy = (buflen <= left) ? buflen : left;
3304     	memcpy (hostdata->debug_write, buf, copy);
3305     	buf += copy;
3306     	buflen -= copy;
3307     	hostdata->debug_count += copy;
3308     	if ((hostdata->debug_write += copy) == 
3309     	    (hostdata->debug_buf + hostdata->debug_size))
3310     	    hosdata->debug_write = hostdata->debug_buf;
3311         }
3312         restore_flags(flags);
3313     }
3314     
3315     #endif /* def NCRDEBUG */
3316     
3317     /* 
3318      * Function : static void NCR538xx_soft_reset (struct Scsi_Host *host)
3319      *
3320      * Purpose :  perform a soft reset of the NCR53c8xx chip
3321      *
3322      * Inputs : host - pointer to this host adapter's structure
3323      *
3324      * Preconditions : NCR53c7x0_init must have been called for this 
3325      *      host.
3326      * 
3327      */
3328     
3329     static void 
3330     NCR53c8x0_soft_reset (struct Scsi_Host *host) {
3331         NCR53c7x0_local_declare();
3332         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3333     	host->hostdata;
3334         NCR53c7x0_local_setup(host);
3335     
3336     
3337         /*
3338          * Do a soft reset of the chip so that everything is 
3339          * reinitialized to the power-on state.
3340          *
3341          * Basically follow the procedure outlined in the NCR53c700
3342          * data manual under Chapter Six, How to Use, Steps Necessary to
3343          * Start SCRIPTS, with the exception of actually starting the 
3344          * script and setting up the synchronous transfer gunk.
3345          */
3346     
3347         NCR53c7x0_write8(ISTAT_REG_800, ISTAT_10_SRST);
3348         NCR53c7x0_write8(ISTAT_REG_800, 0);
3349         NCR53c7x0_write8(hostdata->dmode, hostdata->saved_dmode & ~DMODE_MAN);
3350     
3351     
3352         /* 
3353          * Respond to reselection by targets and use our _initiator_ SCSI ID  
3354          * for arbitration. If notyet, also respond to SCSI selection.
3355          *
3356          * XXX - Note : we must reprogram this when reselecting as 
3357          *	a target.
3358          */
3359     
3360     #ifdef notyet
3361         NCR53c7x0_write8(SCID_REG, (host->this_id & 7)|SCID_800_RRE|SCID_800_SRE);
3362     #else
3363         NCR53c7x0_write8(SCID_REG, (host->this_id & 7)|SCID_800_RRE);
3364     #endif
3365         NCR53c7x0_write8(RESPID_REG_800, hostdata->this_id_mask);
3366     
3367         /*
3368          * Use a maximum (1.6) second handshake to handshake timeout,
3369          * and SCSI recommended .5s selection timeout.
3370          */
3371     
3372         /*
3373          * The new gcc won't recognize preprocessing directives
3374          * within macro args.
3375          */
3376     #if 0
3377         NCR53c7x0_write8(STIME0_REG_800, 
3378         	((selection_timeout << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK) 
3379     	| ((15 << STIME0_800_HTH_SHIFT) & STIME0_800_HTH_MASK));
3380     #else
3381     /* Disable HTH interrupt */
3382         NCR53c7x0_write8(STIME0_REG_800, 
3383         	((selection_timeout << STIME0_800_SEL_SHIFT) & STIME0_800_SEL_MASK));
3384     #endif
3385     
3386     
3387         /*
3388          * Enable active negation for happy synchronous transfers.
3389          */
3390     
3391         NCR53c7x0_write8(STEST3_REG_800, STEST3_800_TE);
3392     
3393         /*
3394          * Enable all interrupts, except parity which we only want when
3395          * the user requests it.
3396          */
3397     
3398         NCR53c7x0_write8(DIEN_REG, DIEN_800_MDPE | DIEN_800_BF |
3399     		DIEN_ABRT | DIEN_SSI | DIEN_SIR | DIEN_800_IID);
3400     
3401         
3402         NCR53c7x0_write8(SIEN0_REG_800, ((hostdata->options & OPTION_PARITY) ?
3403     	    SIEN_PAR : 0) | SIEN_RST | SIEN_UDC | SIEN_SGE | SIEN_MA);
3404         NCR53c7x0_write8(SIEN1_REG_800, SIEN1_800_STO | SIEN1_800_HTH);
3405     
3406         /* 
3407          * Use saved clock frequency divisor and scripts loaded in 16 bit
3408          * mode flags from the saved dcntl.
3409          */
3410     
3411         NCR53c7x0_write8(DCNTL_REG, hostdata->saved_dcntl);
3412         NCR53c7x0_write8(CTEST4_REG_800, hostdata->saved_ctest4);
3413     
3414         /* Enable active negation */
3415         NCR53c7x0_write8(STEST3_REG_800, STEST3_800_TE);
3416     }
3417     
3418     /*
3419      * Function static struct NCR53c7x0_cmd *allocate_cmd (Scsi_Cmnd *cmd)
3420      * 
3421      * Purpose : Return the first free NCR53c7x0_cmd structure (which are 
3422      * 	reused in a LIFO manner to minimize cache thrashing).
3423      *
3424      * Side effects : If we haven't yet scheduled allocation of NCR53c7x0_cmd
3425      *	structures for this device, do so.  Attempt to complete all scheduled
3426      *	allocations using kmalloc(), putting NCR53c7x0_cmd structures on 
3427      *	the free list.  Teach programmers not to drink and hack.
3428      *
3429      * Inputs : cmd - SCSI command
3430      *
3431      * Returns : NCR53c7x0_cmd structure allocated on behalf of cmd;
3432      *	NULL on failure.
3433      */
3434     
3435     static struct NCR53c7x0_cmd *
3436     allocate_cmd (Scsi_Cmnd *cmd) {
3437         struct Scsi_Host *host = cmd->host;
3438         struct NCR53c7x0_hostdata *hostdata = 
3439     	(struct NCR53c7x0_hostdata *) host->hostdata;
3440         void *real;			/* Real address */
3441         int size;			/* Size of *tmp */
3442         struct NCR53c7x0_cmd *tmp;
3443         unsigned long flags;
3444     
3445         if (hostdata->options & OPTION_DEBUG_ALLOCATION)
3446     	printk ("scsi%d : num_cmds = %d, can_queue = %d\n"
3447     		"         target = %d, lun = %d, %s\n",
3448     	    host->host_no, hostdata->num_cmds, host->can_queue,
3449     	    cmd->target, cmd->lun, (hostdata->cmd_allocated[cmd->target] &
3450     		(1 << cmd->lun)) ? "already allocated" : "not allocated");
3451     
3452     /*
3453      * If we have not yet reserved commands for this I_T_L nexus, and
3454      * the device exists (as indicated by permanent Scsi_Cmnd structures
3455      * being allocated under 1.3.x, or being outside of scan_scsis in 
3456      * 1.2.x), do so now.
3457      */
3458         if (!(hostdata->cmd_allocated[cmd->target] & (1 << cmd->lun)) &&
3459     				cmd->device && cmd->device->has_cmdblocks
3460     	) {
3461     	if ((hostdata->extra_allocate + hostdata->num_cmds) < host->can_queue) 
3462     	    hostdata->extra_allocate += host->cmd_per_lun;
3463     	hostdata->cmd_allocated[cmd->target] |= (1 << cmd->lun);
3464         }
3465     
3466         for (; hostdata->extra_allocate > 0 ; --hostdata->extra_allocate, 
3467         	++hostdata->num_cmds) {
3468         /* historically, kmalloc has returned unaligned addresses; pad so we 
3469            have enough room to ROUNDUP */
3470     	size = hostdata->max_cmd_size + sizeof (void *);
3471     /* FIXME: for ISA bus '7xx chips, we need to or GFP_DMA in here */
3472     	real = kmalloc (size, GFP_ATOMIC);
3473     	if (!real) {
3474     	    if (hostdata->options & OPTION_DEBUG_ALLOCATION)
3475     		printk ("scsi%d : kmalloc(%d) failed\n",
3476     		    host->host_no, size);
3477     	    break;
3478     	}
3479     	tmp = ROUNDUP(real, void *);
3480     	tmp->real = real;
3481     	tmp->size = size;			
3482     	tmp->free = ((void (*)(void *, int)) kfree);
3483     	save_flags (flags);
3484     	cli();
3485     	tmp->next = hostdata->free;
3486     	hostdata->free = tmp;
3487     	restore_flags (flags);
3488         }
3489         save_flags(flags);
3490         cli();
3491         tmp = (struct NCR53c7x0_cmd *) hostdata->free;
3492         if (tmp) {
3493     	hostdata->free = tmp->next;
3494         }
3495         restore_flags(flags);
3496         if (!tmp)
3497     	printk ("scsi%d : can't allocate command for target %d lun %d\n",
3498     	    host->host_no, cmd->target, cmd->lun);
3499         return tmp;
3500     }
3501     
3502     /*
3503      * Function static struct NCR53c7x0_cmd *create_cmd (Scsi_Cmnd *cmd) 
3504      *
3505      *
3506      * Purpose : allocate a NCR53c7x0_cmd structure, initialize it based on the 
3507      * 	Scsi_Cmnd structure passed in cmd, including dsa and Linux field 
3508      * 	initialization, and dsa code relocation.
3509      *
3510      * Inputs : cmd - SCSI command
3511      *
3512      * Returns : NCR53c7x0_cmd structure corresponding to cmd,
3513      *	NULL on failure.
3514      */
3515     
3516     static struct NCR53c7x0_cmd *
3517     create_cmd (Scsi_Cmnd *cmd) {
3518         NCR53c7x0_local_declare();
3519         struct Scsi_Host *host = cmd->host;
3520         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
3521             host->hostdata;	
3522         struct NCR53c7x0_cmd *tmp; 	/* NCR53c7x0_cmd structure for this command */
3523         int datain,  		/* Number of instructions per phase */
3524     	dataout;
3525         int data_transfer_instructions, /* Count of dynamic instructions */
3526         	i;			/* Counter */
3527         u32 *cmd_datain,		/* Address of datain/dataout code */
3528     	*cmd_dataout;		/* Incremented as we assemble */
3529     #ifdef notyet
3530         unsigned char *msgptr;	/* Current byte in select message */
3531         int msglen;			/* Length of whole select message */
3532     #endif
3533         unsigned long flags;
3534         NCR53c7x0_local_setup(cmd->host);
3535     
3536         if (!(tmp = allocate_cmd (cmd)))
3537     	return NULL;
3538     
3539     
3540         /*
3541          * Decide whether we need to generate commands for DATA IN,
3542          * DATA OUT, neither, or both based on the SCSI command 
3543          */
3544     
3545         switch (cmd->cmnd[0]) {
3546         /* These commands do DATA IN */
3547         case INQUIRY:
3548         case MODE_SENSE:
3549         case READ_6:
3550         case READ_10:
3551         case READ_CAPACITY:
3552         case REQUEST_SENSE:
3553     	datain = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
3554         	dataout = 0;
3555     	break;
3556         /* These commands do DATA OUT */
3557         case MODE_SELECT: 
3558         case WRITE_6:
3559         case WRITE_10:
3560     #if 0
3561     	printk("scsi%d : command is ", host->host_no);
3562     	print_command(cmd->cmnd);
3563     #endif
3564     #if 0
3565     	printk ("scsi%d : %d scatter/gather segments\n", host->host_no,
3566     	    cmd->use_sg);
3567     #endif
3568         	datain = 0;
3569     	dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
3570     #if 0
3571     	hostdata->options |= OPTION_DEBUG_INTR;
3572     #endif
3573     	break;
3574         /* 
3575          * These commands do no data transfer, we should force an
3576          * interrupt if a data phase is attempted on them.
3577          */
3578         case START_STOP: /* also SCAN, which may do DATA OUT */
3579         case TEST_UNIT_READY:
3580         	datain = dataout = 0;
3581     	break;
3582         /*
3583          * We don't know about these commands, so generate code to handle
3584          * both DATA IN and DATA OUT phases.
3585          */
3586         default:
3587     	datain = dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
3588         }
3589     
3590         /*
3591          * New code : so that active pointers work correctly regardless
3592          * 	of where the saved data pointer is at, we want to immediately
3593          * 	enter the dynamic code after selection, and on a non-data
3594          * 	phase perform a CALL to the non-data phase handler, with
3595          * 	returns back to this address.
3596          *
3597          * 	If a phase mismatch is encountered in the middle of a 
3598          * 	Block MOVE instruction, we want to _leave_ that instruction
3599          *	unchanged as the current case is, modify a temporary buffer,
3600          *	and point the active pointer (TEMP) at that.
3601          *
3602          * 	Furthermore, we want to implement a saved data pointer, 
3603          * 	set by the SAVE_DATA_POINTERs message.
3604          *
3605          * 	So, the data transfer segments will change to 
3606          *		CALL data_transfer, WHEN NOT data phase
3607          *		MOVE x, x, WHEN data phase
3608          *		( repeat )
3609          *		JUMP other_transfer
3610          */
3611     
3612         data_transfer_instructions = datain + dataout;
3613     
3614         /*
3615          * When we perform a request sense, we overwrite various things,
3616          * including the data transfer code.  Make sure we have enough
3617          * space to do that.
3618          */
3619     
3620         if (data_transfer_instructions < 2)
3621         	data_transfer_instructions = 2;
3622     
3623     
3624         /*
3625          * The saved data pointer is set up so that a RESTORE POINTERS message 
3626          * will start the data transfer over at the beginning.
3627          */
3628     
3629         tmp->saved_data_pointer = le32_to_cpu(virt_to_bus (hostdata->script) + 
3630     	hostdata->E_data_transfer);
3631     
3632         /*
3633          * Initialize Linux specific fields.
3634          */
3635     
3636         tmp->cmd = cmd;
3637         tmp->next = NULL;
3638         tmp->flags = 0;
3639         tmp->dsa_next_addr = le32_to_cpu(virt_to_bus(tmp->dsa) + hostdata->dsa_next - 
3640     	hostdata->dsa_start);
3641         tmp->dsa_addr = le32_to_cpu(virt_to_bus(tmp->dsa) - hostdata->dsa_start);
3642     
3643         /* 
3644          * Calculate addresses of dynamic code to fill in DSA
3645          */
3646     
3647         tmp->data_transfer_start = tmp->dsa + (hostdata->dsa_end - 
3648         	hostdata->dsa_start) / sizeof(u32);
3649         tmp->data_transfer_end = tmp->data_transfer_start + 
3650         	2 * data_transfer_instructions;
3651     
3652         cmd_datain = datain ? tmp->data_transfer_start : NULL;
3653         cmd_dataout = dataout ? (datain ? cmd_datain + 2 * datain : tmp->
3654         	data_transfer_start) : NULL;
3655     
3656         /*
3657          * Fill in the NCR53c7x0_cmd structure as follows
3658          * dsa, with fixed up DSA code
3659          * datain code
3660          * dataout code
3661          */
3662     
3663         /* Copy template code into dsa and perform all necessary fixups */
3664         if (hostdata->dsa_fixup)
3665         	hostdata->dsa_fixup(tmp);
3666     
3667         patch_dsa_32(tmp->dsa, dsa_next, 0, le32_to_cpu(0));
3668         patch_dsa_32(tmp->dsa, dsa_cmnd, 0, le32_to_cpu(virt_to_bus(cmd)));
3669     
3670         if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS) 
3671     	if (hostdata->sync[cmd->target].select_indirect != 
3672     	    ((hostdata->sync[cmd->target].scntl3_sanity << 24) | 
3673     		(cmd->target << 16) | 
3674     		(hostdata->sync[cmd->target].sxfer_sanity << 8))) {
3675     	    printk ("scsi%d :  sanity check failed select_indirect=0x%x\n",
3676     		host->host_no, hostdata->sync[cmd->target].select_indirect);
3677     	    FATAL(host);
3678     
3679     	}
3680     
3681         patch_dsa_32(tmp->dsa, dsa_select, 0, le32_to_cpu(hostdata->sync[cmd->target].
3682         	select_indirect));
3683         /*
3684          * Right now, we'll do the WIDE and SYNCHRONOUS negotiations on
3685          * different commands; although it should be trivial to do them
3686          * both at the same time.
3687          */
3688         if (hostdata->initiate_wdtr & (1 << cmd->target)) {
3689     	memcpy ((void *) (tmp->select + 1), (void *) wdtr_message,
3690     	    sizeof(wdtr_message));
3691         	patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1 + sizeof(wdtr_message)));
3692     	save_flags(flags);
3693     	cli();
3694     	hostdata->initiate_wdtr &= ~(1 << cmd->target);
3695     	restore_flags(flags);
3696         } else if (hostdata->initiate_sdtr & (1 << cmd->target)) {
3697     	memcpy ((void *) (tmp->select + 1), (void *) sdtr_message, 
3698     	    sizeof(sdtr_message));
3699         	patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1 + sizeof(sdtr_message)));
3700     	tmp->flags |= CMD_FLAG_SDTR;
3701     	save_flags(flags);
3702     	cli();
3703     	hostdata->initiate_sdtr &= ~(1 << cmd->target);
3704     	restore_flags(flags);
3705         
3706         }
3707     #if 1
3708         else if (!(hostdata->talked_to & (1 << cmd->target)) && 
3709     		!(hostdata->options & OPTION_NO_ASYNC)) {
3710     	memcpy ((void *) (tmp->select + 1), (void *) async_message, 
3711     	    sizeof(async_message));
3712         	patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1 + sizeof(async_message)));
3713     	tmp->flags |= CMD_FLAG_SDTR;
3714         } 
3715     #endif
3716         else 
3717         	patch_dsa_32(tmp->dsa, dsa_msgout, 0, le32_to_cpu(1));
3718         hostdata->talked_to |= (1 << cmd->target);
3719         tmp->select[0] = (hostdata->options & OPTION_DISCONNECT) ? 
3720     	IDENTIFY (1, cmd->lun) : IDENTIFY (0, cmd->lun);
3721         patch_dsa_32(tmp->dsa, dsa_msgout, 1, le32_to_cpu(virt_to_bus(tmp->select)));
3722         patch_dsa_32(tmp->dsa, dsa_cmdout, 0, le32_to_cpu(cmd->cmd_len));
3723         patch_dsa_32(tmp->dsa, dsa_cmdout, 1, le32_to_cpu(virt_to_bus(cmd->cmnd)));
3724         patch_dsa_32(tmp->dsa, dsa_dataout, 0, le32_to_cpu(cmd_dataout ? 
3725         	    virt_to_bus (cmd_dataout)
3726     	: virt_to_bus (hostdata->script) + hostdata->E_other_transfer));
3727         patch_dsa_32(tmp->dsa, dsa_datain, 0, le32_to_cpu(cmd_datain ? 
3728         	    virt_to_bus (cmd_datain) 
3729     	: virt_to_bus (hostdata->script) + hostdata->E_other_transfer));
3730         /* 
3731          * XXX - need to make endian aware, should use separate variables
3732          * for both status and message bytes.
3733          */
3734         patch_dsa_32(tmp->dsa, dsa_msgin, 0, le32_to_cpu(1));
3735     /* 
3736      * FIXME : these only works for little endian.  We probably want to 
3737      * 	provide message and status fields in the NCR53c7x0_cmd 
3738      *	structure, and assign them to cmd->result when we're done.
3739      */
3740         patch_dsa_32(tmp->dsa, dsa_msgin, 1, le32_to_cpu(virt_to_bus(&cmd->result) + 1));
3741         patch_dsa_32(tmp->dsa, dsa_status, 0, le32_to_cpu(1));
3742         patch_dsa_32(tmp->dsa, dsa_status, 1, le32_to_cpu(virt_to_bus(&cmd->result)));
3743         patch_dsa_32(tmp->dsa, dsa_msgout_other, 0, le32_to_cpu(1));
3744         patch_dsa_32(tmp->dsa, dsa_msgout_other, 1, 
3745     	le32_to_cpu(virt_to_bus(&(hostdata->NCR53c7xx_msg_nop))));
3746         
3747         /*
3748          * Generate code for zero or more of the DATA IN, DATA OUT phases 
3749          * in the format 
3750          *
3751          * CALL data_transfer, WHEN NOT phase
3752          * MOVE first buffer length, first buffer address, WHEN phase
3753          * ...
3754          * MOVE last buffer length, last buffer address, WHEN phase
3755          * JUMP other_transfer
3756          */
3757     
3758     /* 
3759      * See if we're getting to data transfer by generating an unconditional 
3760      * interrupt.
3761      */
3762     #if 0
3763         if (datain) {
3764     	cmd_datain[0] = 0x98080000;
3765     	cmd_datain[1] = 0x03ffd00d;
3766     	cmd_datain += 2;
3767         }
3768     #endif
3769     
3770     /* 
3771      * XXX - I'm undecided whether all of this nonsense is faster
3772      * in the long run, or whether I should just go and implement a loop
3773      * on the NCR chip using table indirect mode?
3774      *
3775      * In any case, this is how it _must_ be done for 53c700/700-66 chips,
3776      * so this stays even when we come up with something better.
3777      *
3778      * When we're limited to 1 simultaneous command, no overlapping processing,
3779      * we're seeing 630K/sec, with 7% CPU usage on a slow Syquest 45M
3780      * drive.
3781      *
3782      * Not bad, not good. We'll see.
3783      */
3784     
3785         for (i = 0; cmd->use_sg ? (i < cmd->use_sg) : !i; cmd_datain += 4, 
3786     	cmd_dataout += 4, ++i) {
3787     	u32 buf = cmd->use_sg ? 
3788     	    virt_to_bus(((struct scatterlist *)cmd->buffer)[i].address) :
3789     	    virt_to_bus(cmd->request_buffer);
3790     	u32 count = cmd->use_sg ?
3791     	    ((struct scatterlist *)cmd->buffer)[i].length :
3792     	    cmd->request_bufflen;
3793     
3794     	if (datain) {
3795     	    /* CALL other_in, WHEN NOT DATA_IN */  
3796     	    cmd_datain[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL | 
3797     		DCMD_TCI_IO) << 24) | 
3798     		DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE);
3799     	    cmd_datain[1] = le32_to_cpu(virt_to_bus (hostdata->script) + 
3800     		hostdata->E_other_in);
3801     	    /* MOVE count, buf, WHEN DATA_IN */
3802     	    cmd_datain[2] = le32_to_cpu(((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | DCMD_BMI_IO) 
3803         	    	<< 24) | count);
3804     	    cmd_datain[3] = le32_to_cpu(buf);
3805     #if 0
3806     	    print_insn (host, cmd_datain, "dynamic ", 1);
3807     	    print_insn (host, cmd_datain + 2, "dynamic ", 1);
3808     #endif
3809     	}
3810     	if (dataout) {
3811     	    /* CALL other_out, WHEN NOT DATA_OUT */
3812     	    cmd_dataout[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL) << 24) | 
3813     		DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE);
3814     	    cmd_dataout[1] = le32_to_cpu(virt_to_bus(hostdata->script) + 
3815         	    	hostdata->E_other_out);
3816     	    /* MOVE count, buf, WHEN DATA+OUT */
3817     	    cmd_dataout[2] = le32_to_cpu(((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I) << 24) 
3818     		| count);
3819     	    cmd_dataout[3] = le32_to_cpu(buf);
3820     #if 0
3821     	    print_insn (host, cmd_dataout, "dynamic ", 1);
3822     	    print_insn (host, cmd_dataout + 2, "dynamic ", 1);
3823     #endif
3824     	}
3825         }
3826     
3827         /*
3828          * Install JUMP instructions after the data transfer routines to return
3829          * control to the do_other_transfer routines.
3830          */
3831       
3832         
3833         if (datain) {
3834     	cmd_datain[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
3835         	    DBC_TCI_TRUE);
3836     	cmd_datain[1] = le32_to_cpu(virt_to_bus(hostdata->script) + 
3837         	    hostdata->E_other_transfer);
3838     #if 0
3839     	print_insn (host, cmd_datain, "dynamic jump ", 1);
3840     #endif
3841     	cmd_datain += 2; 
3842         }
3843     #if 0
3844         if (datain) {
3845     	cmd_datain[0] = 0x98080000;
3846     	cmd_datain[1] = 0x03ffdeed;
3847     	cmd_datain += 2;
3848         }
3849     #endif
3850         if (dataout) {
3851     	cmd_dataout[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
3852         	    DBC_TCI_TRUE);
3853     	cmd_dataout[1] = le32_to_cpu(virt_to_bus(hostdata->script) + 
3854         	    hostdata->E_other_transfer);
3855     #if 0
3856     	print_insn (host, cmd_dataout, "dynamic jump ", 1);
3857     #endif
3858     	cmd_dataout += 2;
3859         }
3860         return tmp;
3861     }
3862     
3863     /*
3864      * Function : int NCR53c7xx_queue_command (Scsi_Cmnd *cmd,
3865      *      void (*done)(Scsi_Cmnd *))
3866      *
3867      * Purpose :  enqueues a SCSI command
3868      *
3869      * Inputs : cmd - SCSI command, done - function called on completion, with
3870      *      a pointer to the command descriptor.
3871      *
3872      * Returns : 0
3873      *
3874      * Side effects :
3875      *      cmd is added to the per instance driver issue_queue, with major
3876      *      twiddling done to the host specific fields of cmd.  If the
3877      *      process_issue_queue coroutine isn't running, it is restarted.
3878      * 
3879      * NOTE : we use the host_scribble field of the Scsi_Cmnd structure to 
3880      *	hold our own data, and pervert the ptr field of the SCp field
3881      *	to create a linked list.
3882      */
3883     
3884     int
3885     NCR53c7xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *)) {
3886         struct Scsi_Host *host = cmd->host;
3887         struct NCR53c7x0_hostdata *hostdata = 
3888     	(struct NCR53c7x0_hostdata *) host->hostdata;
3889         unsigned long flags;
3890         Scsi_Cmnd *tmp;
3891     
3892         cmd->scsi_done = done;
3893         cmd->host_scribble = NULL;
3894         cmd->SCp.ptr = NULL;
3895         cmd->SCp.buffer = NULL;
3896     
3897         save_flags(flags);
3898         cli();
3899         if ((hostdata->options & (OPTION_DEBUG_INIT_ONLY|OPTION_DEBUG_PROBE_ONLY)) 
3900     	|| ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
3901     	    !(hostdata->debug_lun_limit[cmd->target] & (1 << cmd->lun))) 
3902     	|| cmd->target > host->max_id
3903     	|| cmd->target == host->this_id
3904     	|| hostdata->state == STATE_DISABLED) {
3905     	printk("scsi%d : disabled or bad target %d lun %d\n", host->host_no,
3906     	    cmd->target, cmd->lun);
3907     	cmd->result = DID_BAD_TARGET << 16;
3908         } else if ((hostdata->options & OPTION_DEBUG_NCOMMANDS_LIMIT) &&
3909     	(hostdata->debug_count_limit == 0)) {
3910     	printk("scsi%d : maximum commands exceeded\n", host->host_no);
3911     	cmd->result = DID_BAD_TARGET << 16;
3912         } else if (hostdata->options & OPTION_DEBUG_READ_ONLY) {
3913     	switch (cmd->cmnd[0]) {
3914     	case WRITE_6:
3915     	case WRITE_10:
3916     	    printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
3917     		host->host_no);
3918     	    cmd->result = DID_BAD_TARGET << 16;
3919     	}
3920         } else {
3921         	if ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
3922     	    hostdata->debug_count_limit != -1) 
3923     	    --hostdata->debug_count_limit;
3924     	restore_flags (flags);
3925     	cmd->result = le32_to_cpu(0xffff);	/* The NCR will overwrite message
3926     				       and status with valid data */
3927     	cmd->host_scribble = (unsigned char *) tmp = create_cmd (cmd);
3928         }
3929         cli();
3930         /*
3931          * REQUEST SENSE commands are inserted at the head of the queue 
3932          * so that we do not clear the contingent allegiance condition
3933          * they may be looking at.
3934          */
3935     
3936         if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
3937     	cmd->SCp.ptr = (unsigned char *) hostdata->issue_queue;
3938     	hostdata->issue_queue = cmd;
3939         } else {
3940     	for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->SCp.ptr; 
3941     		tmp = (Scsi_Cmnd *) tmp->SCp.ptr);
3942     	tmp->SCp.ptr = (unsigned char *) cmd;
3943         }
3944         restore_flags (flags);
3945         run_process_issue_queue();
3946         return 0;
3947     }
3948     
3949     /*
3950      * Function : void to_schedule_list (struct Scsi_Host *host,
3951      * 	struct NCR53c7x0_hostdata * hostdata, Scsi_Cmnd *cmd)
3952      *
3953      * Purpose : takes a SCSI command which was just removed from the 
3954      *	issue queue, and deals with it by inserting it in the first
3955      *	free slot in the schedule list or by terminating it immediately.
3956      *
3957      * Inputs : 
3958      *	host - SCSI host adapter; hostdata - hostdata structure for 
3959      *	this adapter; cmd - a pointer to the command; should have 
3960      *	the host_scribble field initialized to point to a valid 
3961      *	
3962      * Side effects : 
3963      *      cmd is added to the per instance schedule list, with minor 
3964      *      twiddling done to the host specific fields of cmd.
3965      *
3966      */
3967     
3968     static __inline__ void
3969     to_schedule_list (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
3970         struct NCR53c7x0_cmd *cmd) {
3971         NCR53c7x0_local_declare();
3972         Scsi_Cmnd *tmp = cmd->cmd;
3973         unsigned long flags;
3974         /* dsa start is negative, so subtraction is used */
3975         volatile u32 *curr;
3976     
3977         int i;
3978         NCR53c7x0_local_setup(host);
3979     #if 0
3980         printk("scsi%d : new dsa is 0x%lx (virt 0x%p)\n", host->host_no, 
3981     	virt_to_bus(dsa), dsa);
3982     #endif
3983     
3984         save_flags(flags);
3985         cli();
3986         
3987         /* 
3988          * Work around race condition : if an interrupt fired and we 
3989          * got disabled forget about this command.
3990          */
3991     
3992         if (hostdata->state == STATE_DISABLED) {
3993     	printk("scsi%d : driver disabled\n", host->host_no);
3994     	tmp->result = DID_BAD_TARGET << 16;
3995     	cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
3996     	hostdata->free = cmd;
3997     	tmp->scsi_done(tmp);
3998     	restore_flags (flags);
3999     	return;
4000         }
4001     
4002         for (i = host->can_queue, curr = hostdata->schedule; 
4003     	i > 0  && curr[0] != hostdata->NOP_insn;
4004     	--i, curr += 2 /* JUMP instructions are two words */);
4005     
4006         if (i > 0) {
4007     	++hostdata->busy[tmp->target][tmp->lun];
4008     	cmd->next = hostdata->running_list;
4009     	hostdata->running_list = cmd;
4010     
4011     	/* Restore this instruction to a NOP once the command starts */
4012     	cmd->dsa [(hostdata->dsa_jump_dest - hostdata->dsa_start) / 
4013     	    sizeof(u32)] = (u32) le32_to_cpu(virt_to_bus ((void *)curr));
4014     	/* Replace the current jump operand.  */
4015     	curr[1] =
4016     	    le32_to_cpu(virt_to_bus ((void *) cmd->dsa) + hostdata->E_dsa_code_begin -
4017     	    hostdata->E_dsa_code_template);
4018     	/* Replace the NOP instruction with a JUMP */
4019     	curr[0] = le32_to_cpu(((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) |
4020     	    DBC_TCI_TRUE);
4021         }  else {
4022     	printk ("scsi%d: no free slot\n", host->host_no);
4023     	disable(host);
4024     	tmp->result = DID_ERROR << 16;
4025     	cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
4026     	hostdata->free = cmd;
4027     	tmp->scsi_done(tmp);
4028     	restore_flags (flags);
4029     	return;
4030         }
4031     
4032         /* 
4033          * If the NCR chip is in an idle state, start it running the scheduler
4034          * immediately.  Otherwise, signal the chip to jump to schedule as 
4035          * soon as it is idle.
4036          */
4037         if (hostdata->idle) {
4038     	hostdata->idle = 0;
4039     	hostdata->state = STATE_RUNNING;
4040     	NCR53c7x0_write32 (DSP_REG,  virt_to_bus ((void *)hostdata->schedule));
4041         } else {
4042     	NCR53c7x0_write8(hostdata->istat, ISTAT_10_SIGP);
4043         }
4044     
4045         restore_flags(flags);
4046     }
4047     
4048     /*
4049      * Function : busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata 
4050      *	*hostdata, Scsi_Cmnd *cmd)
4051      *
4052      * Purpose : decide if we can pass the given SCSI command on to the 
4053      *	device in question or not.
4054      *  
4055      * Returns : non-zero when we're busy, 0 when we aren't.
4056      */
4057     
4058     static __inline__ int
4059     busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata, 
4060         Scsi_Cmnd *cmd) {
4061         /* FIXME : in the future, this needs to accommodate SCSI-II tagged
4062            queuing, and we may be able to play with fairness here a bit.
4063          */
4064         return hostdata->busy[cmd->target][cmd->lun];
4065     }
4066     
4067     /*
4068      * Function : process_issue_queue (void)
4069      *
4070      * Purpose : transfer commands from the issue queue to NCR start queue 
4071      *	of each NCR53c7/8xx in the system, avoiding kernel stack 
4072      *	overflows when the scsi_done() function is invoked recursively.
4073      * 
4074      * NOTE : process_issue_queue exits with interrupts *disabled*, so the 
4075      *	caller must reenable them if it desires.
4076      * 
4077      * NOTE : process_issue_queue should be called from both 
4078      *	NCR53c7x0_queue_command() and from the interrupt handler 
4079      *	after command completion in case NCR53c7x0_queue_command()
4080      * 	isn't invoked again but we've freed up resources that are
4081      *	needed.
4082      */
4083     
4084     static void 
4085     process_issue_queue (unsigned long flags) {
4086         Scsi_Cmnd *tmp, *prev;
4087         struct Scsi_Host *host;
4088         struct NCR53c7x0_hostdata *hostdata;
4089         int done;
4090     
4091         /*
4092          * We run (with interrupts disabled) until we're sure that none of 
4093          * the host adapters have anything that can be done, at which point 
4094          * we set process_issue_queue_running to 0 and exit.
4095          *
4096          * Interrupts are enabled before doing various other internal 
4097          * instructions, after we've decided that we need to run through
4098          * the loop again.
4099          *
4100          */
4101     
4102         do {
4103     	cli(); /* Freeze request queues */
4104     	done = 1;
4105     	for (host = first_host; host && host->hostt == the_template; 
4106     	    host = host->next) {
4107     	    hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
4108     	    cli();
4109     	    if (hostdata->issue_queue) {
4110     	    	if (hostdata->state == STATE_DISABLED) {
4111     		    tmp = (Scsi_Cmnd *) hostdata->issue_queue;
4112     		    hostdata->issue_queue = (Scsi_Cmnd *) tmp->SCp.ptr;
4113     		    tmp->result = DID_BAD_TARGET << 16;
4114     		    if (tmp->host_scribble) {
4115     			((struct NCR53c7x0_cmd *)tmp->host_scribble)->next = 
4116     			    hostdata->free;
4117     			hostdata->free = 
4118     			    (struct NCR53c7x0_cmd *)tmp->host_scribble;
4119     			tmp->host_scribble = NULL;
4120     		    }
4121     		    tmp->scsi_done (tmp);
4122     		    done = 0;
4123     		} else 
4124     		    for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, 
4125     			prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) 
4126     			tmp->SCp.ptr) 
4127     			if (!tmp->host_scribble || 
4128     			    !busyp (host, hostdata, tmp)) {
4129     				if (prev)
4130     				    prev->SCp.ptr = tmp->SCp.ptr;
4131     				else
4132     				    hostdata->issue_queue = (Scsi_Cmnd *) 
4133     					tmp->SCp.ptr;
4134     			    tmp->SCp.ptr = NULL;
4135     			    if (tmp->host_scribble) {
4136     				if (hostdata->options & OPTION_DEBUG_QUEUES) 
4137     				    printk ("scsi%d : moving command for target %d lun %d to start list\n",
4138     					host->host_no, tmp->target, tmp->lun);
4139     		
4140     
4141     			    	to_schedule_list (host, hostdata, 
4142     				    (struct NCR53c7x0_cmd *)
4143     				    tmp->host_scribble);
4144     			    } else {
4145     			    	tmp->result = le32_to_cpu(tmp->result);
4146     				if (((tmp->result & 0xff) == 0xff) ||
4147     			    	    ((tmp->result & 0xff00) == 0xff00)) {
4148     				    printk ("scsi%d : danger Will Robinson!\n",
4149     					host->host_no);
4150     				    tmp->result = DID_ERROR << 16;
4151     				    disable (host);
4152     				}
4153     				tmp->scsi_done(tmp);
4154     			    }
4155     			    done = 0;
4156     			} /* if target/lun is not busy */
4157     	    } /* if hostdata->issue_queue */
4158     	    if (!done)
4159     		restore_flags (flags);
4160         	} /* for host */
4161         } while (!done);
4162         process_issue_queue_running = 0;
4163     }
4164     
4165     /*
4166      * Function : static void intr_scsi (struct Scsi_Host *host, 
4167      * 	struct NCR53c7x0_cmd *cmd)
4168      *
4169      * Purpose : handle all SCSI interrupts, indicated by the setting 
4170      * 	of the SIP bit in the ISTAT register.
4171      *
4172      * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
4173      * 	may be NULL.
4174      */
4175     
4176     static void 
4177     intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
4178         NCR53c7x0_local_declare();
4179         struct NCR53c7x0_hostdata *hostdata = 
4180         	(struct NCR53c7x0_hostdata *) host->hostdata;
4181         unsigned char sstat0_sist0, sist1, 		/* Registers */
4182     	    fatal; 				/* Did a fatal interrupt 
4183     						   occur ? */
4184        
4185         int is_8xx_chip;		 
4186         NCR53c7x0_local_setup(host);
4187     
4188         fatal = 0;
4189     
4190         is_8xx_chip = ((unsigned) (hostdata->chip - 800)) < 100;
4191         if (is_8xx_chip) {
4192         	sstat0_sist0 = NCR53c7x0_read8(SIST0_REG_800);
4193     	udelay(1);
4194         	sist1 = NCR53c7x0_read8(SIST1_REG_800);
4195         } else {
4196         	sstat0_sist0 = NCR53c7x0_read8(SSTAT0_REG);
4197         	sist1 = 0;
4198         }
4199     
4200         if (hostdata->options & OPTION_DEBUG_INTR) 
4201     	printk ("scsi%d : SIST0 0x%0x, SIST1 0x%0x\n", host->host_no,
4202     	    sstat0_sist0, sist1);
4203     
4204         /* 250ms selection timeout */
4205         if ((is_8xx_chip && (sist1 & SIST1_800_STO)) || 
4206             (!is_8xx_chip && (sstat0_sist0 & SSTAT0_700_STO))) {
4207     	fatal = 1;
4208     	if (hostdata->options & OPTION_DEBUG_INTR) {
4209     	    printk ("scsi%d : Selection Timeout\n", host->host_no);
4210         	    if (cmd) {
4211         	    	printk("scsi%d : target %d, lun %d, command ",
4212         	    	    host->host_no, cmd->cmd->target, cmd->cmd->lun);
4213         	    	print_command (cmd->cmd->cmnd);
4214     		printk("scsi%d : dsp = 0x%x (virt 0x%p)\n", host->host_no,
4215     		    NCR53c7x0_read32(DSP_REG),
4216     		    bus_to_virt(NCR53c7x0_read32(DSP_REG)));
4217         	    } else {
4218         	    	printk("scsi%d : no command\n", host->host_no);
4219         	    }
4220         	}
4221     /*
4222      * XXX - question : how do we want to handle the Illegal Instruction
4223      * 	interrupt, which may occur before or after the Selection Timeout
4224      * 	interrupt?
4225      */
4226     
4227     	if (1) {
4228     	    hostdata->idle = 1;
4229     	    hostdata->expecting_sto = 0;
4230     
4231     	    if (hostdata->test_running) {
4232     		hostdata->test_running = 0;
4233     		hostdata->test_completed = 3;
4234     	    } else if (cmd) {
4235     		abnormal_finished(cmd, DID_BAD_TARGET << 16);
4236     	    }
4237     #if 0	    
4238     	    hostdata->intrs = 0;
4239     #endif
4240     	}
4241         } 
4242     
4243     /*
4244      * FIXME : in theory, we can also get a UDC when a STO occurs.
4245      */
4246         if (sstat0_sist0 & SSTAT0_UDC) {
4247     	fatal = 1;
4248     	if (cmd) {
4249     	    printk("scsi%d : target %d lun %d unexpected disconnect\n",
4250     		host->host_no, cmd->cmd->target, cmd->cmd->lun);
4251     	    print_lots (host);
4252     	    abnormal_finished(cmd, DID_ERROR << 16);
4253     	} else 
4254     	     printk("scsi%d : unexpected disconnect (no command)\n",
4255     		host->host_no);
4256     
4257     	hostdata->dsp = (u32 *) hostdata->schedule;
4258     	hostdata->dsp_changed = 1;
4259         }
4260     
4261         /* SCSI PARITY error */
4262         if (sstat0_sist0 & SSTAT0_PAR) {
4263     	fatal = 1;
4264     	if (cmd && cmd->cmd) {
4265     	    printk("scsi%d : target %d lun %d parity error.\n",
4266     		host->host_no, cmd->cmd->target, cmd->cmd->lun);
4267     	    abnormal_finished (cmd, DID_PARITY << 16); 
4268     	} else
4269     	    printk("scsi%d : parity error\n", host->host_no);
4270     	/* Should send message out, parity error */
4271     
4272     	/* XXX - Reduce synchronous transfer rate! */
4273     	hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
4274         	    sizeof(u32);
4275     	hostdata->dsp_changed = 1; 
4276         /* SCSI GROSS error */
4277         } 
4278     
4279         if (sstat0_sist0 & SSTAT0_SGE) {
4280     	fatal = 1;
4281     	printk("scsi%d : gross error\n", host->host_no);
4282     	/* Reset SCSI offset */
4283     	if ((hostdata->chip / 100) == 8) {
4284     	    NCR53c7x0_write8 (STEST2_REG_800, STEST2_800_ROF);
4285     	}
4286     	
4287     	/* 
4288              * A SCSI gross error may occur when we have 
4289     	 *
4290     	 * - A synchronous offset which causes the SCSI FIFO to be overwritten.
4291     	 *
4292     	 * - A REQ which causes the maximum synchronous offset programmed in 
4293     	 * 	the SXFER register to be exceeded.
4294     	 *
4295     	 * - A phase change with an outstanding synchronous offset.
4296     	 *
4297     	 * - Residual data in the synchronous data FIFO, with a transfer
4298     	 *	other than a synchronous receive is started.$#
4299     	 */
4300     		
4301     
4302     	/* XXX Should deduce synchronous transfer rate! */
4303     	hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
4304         	    sizeof(u32);
4305     	hostdata->dsp_changed = 1;
4306         /* Phase mismatch */
4307         } 
4308     
4309         if (sstat0_sist0 & SSTAT0_MA) {
4310     	fatal = 1;
4311     	if (hostdata->options & OPTION_DEBUG_INTR)
4312     	    printk ("scsi%d : SSTAT0_MA\n", host->host_no);
4313     	intr_phase_mismatch (host, cmd);
4314         }
4315     
4316     #if 0
4317         if (sstat0_sist0 & SIST0_800_RSL) 
4318     	printk ("scsi%d : Oh no Mr. Bill!\n", host->host_no);
4319     #endif
4320         
4321     /*
4322      * If a fatal SCSI interrupt occurs, we must insure that the DMA and
4323      * SCSI FIFOs were flushed.
4324      */
4325     
4326         if (fatal) {
4327     	if (!hostdata->dstat_valid) {
4328     	    hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
4329     	    hostdata->dstat_valid = 1;
4330     	}
4331     
4332     /* XXX - code check for 700/800 chips */
4333     	if (!(hostdata->dstat & DSTAT_DFE)) {
4334     	    printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
4335         	    if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4336     		printk ("scsi%d: Flushing DMA FIFO\n", 
4337     			host->host_no);
4338         	    	NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
4339         	    	while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
4340         	    	    DSTAT_DFE));
4341         	    } else {
4342         	    	NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
4343         	    	while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
4344         	    }
4345     	    hostdata->dstat |= DSTAT_DFE;
4346         	}
4347         }
4348     }
4349     
4350     /*
4351      * Function : do_NCR53c7x0_intr()
4352      *
4353      * Purpose : A quick wrapper function added to grab the io_request_lock
4354      *      spin lock prior to entering the real interrupt handler.  Needed
4355      *      for 2.1.95 and above.
4356      */
4357     static void
4358     do_NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs) {
4359         unsigned long flags;
4360     
4361         spin_lock_irqsave(&io_request_lock, flags);
4362         NCR53c7x0_intr(irq, dev_id, regs);
4363         spin_unlock_irqrestore(&io_request_lock, flags);
4364     }
4365     
4366     /*
4367      * Function : static void NCR53c7x0_intr (int irq, void *dev_id, struct pt_regs * regs)
4368      *
4369      * Purpose : handle NCR53c7x0 interrupts for all NCR devices sharing
4370      *	the same IRQ line.  
4371      * 
4372      * Inputs : Since we're using the SA_INTERRUPT interrupt handler
4373      *	semantics, irq indicates the interrupt which invoked 
4374      *	this handler.  
4375      */
4376     
4377     static void 
4378     NCR53c7x0_intr (int irq, void *dev_id, struct pt_regs * regs) {
4379         NCR53c7x0_local_declare();
4380         struct Scsi_Host *host;			/* Host we are looking at */
4381         unsigned char istat; 			/* Values of interrupt regs */
4382         struct NCR53c7x0_hostdata *hostdata;	/* host->hostdata */
4383         struct NCR53c7x0_cmd *cmd,			/* command which halted */
4384     	**cmd_prev_ptr;
4385         u32 *dsa;					/* DSA */
4386         int done = 1;				/* Indicates when handler 
4387     						   should terminate */
4388         int interrupted = 0;			/* This HA generated 
4389     						   an interrupt */
4390         int have_intfly;				/* Don't print warning 
4391     						   messages when we stack
4392     						   INTFLYs */
4393         unsigned long flags;				
4394     
4395     #ifdef NCR_DEBUG
4396         char buf[80];				/* Debugging sprintf buffer */
4397         size_t buflen;				/* Length of same */
4398     #endif
4399         do {
4400     	done = 1;
4401     	for (host = first_host; host; host = host->next) 
4402     	    if (host->hostt == the_template && host->irq == irq) {
4403         	    NCR53c7x0_local_setup(host);
4404     
4405     	    hostdata = (struct NCR53c7x0_hostdata *) host->hostdata;
4406     	    hostdata->dsp_changed = 0;
4407     	    interrupted = 0;
4408     	    have_intfly = 0;
4409     
4410     	    do {
4411     		int is_8xx_chip;
4412     
4413     		hostdata->dstat_valid = 0;
4414     		interrupted = 0;
4415     		/*
4416     		 * Only read istat once, since reading it again will unstack
4417     		 * interrupts?
4418     		 */
4419     		istat = NCR53c7x0_read8(hostdata->istat);
4420     
4421     		/*
4422     		 * INTFLY interrupts are used by the NCR53c720, NCR53c810,
4423     		 * and NCR53c820 to signify completion of a command.  Since 
4424     		 * the SCSI processor continues running, we can't just look
4425     		 * at the contents of the DSA register and continue running.
4426     		 */
4427     /* XXX - this is too big, offends my sense of aesthetics, and should 
4428        move to intr_intfly() */
4429     		is_8xx_chip = ((unsigned) (hostdata->chip - 800)) < 100;
4430     		if ((hostdata->options & OPTION_INTFLY) && 
4431     		    (is_8xx_chip && (istat & ISTAT_800_INTF))) {
4432     		    char search_found = 0;	/* Got at least one ? */
4433     		    done = 0;
4434     		    interrupted = 1;
4435     
4436     		    /* 
4437     		     * Clear the INTF bit by writing a one.  
4438     		     * This reset operation is self-clearing.
4439     		     */
4440     		    NCR53c7x0_write8(hostdata->istat, istat|ISTAT_800_INTF);
4441     
4442     		    if (hostdata->options & OPTION_DEBUG_INTR)
4443     			printk ("scsi%d : INTFLY\n", host->host_no); 
4444     
4445     		    /*
4446     		     * Traverse our list of running commands, and look
4447     		     * for those with valid (non-0xff ff) status and message
4448     		     * bytes encoded in the result which signify command
4449     		     * completion.
4450     		     */
4451     
4452     
4453     		    save_flags(flags);
4454     		    cli();
4455     restart:
4456     		    for (cmd_prev_ptr = (struct NCR53c7x0_cmd **) 
4457     			 &(hostdata->running_list), cmd = 
4458     			 (struct NCR53c7x0_cmd *) hostdata->running_list; cmd ;
4459     			 cmd_prev_ptr = (struct NCR53c7x0_cmd **) &(cmd->next), 
4460         	    	    	 cmd = (struct NCR53c7x0_cmd *) cmd->next) {
4461     			Scsi_Cmnd *tmp;
4462     
4463     			if (!cmd) {
4464     			    printk("scsi%d : very weird.\n", host->host_no);
4465     			    break;
4466     			}
4467     
4468     			if (!(tmp = cmd->cmd)) {
4469     			    printk("scsi%d : weird.  NCR53c7x0_cmd has no Scsi_Cmnd\n",
4470     				host->host_no);
4471     				continue;
4472     			}
4473     #if 0
4474     			printk ("scsi%d : looking at result of 0x%x\n",
4475     			    host->host_no, cmd->cmd->result);
4476     #endif
4477     
4478     #ifdef __powerpc__
4479     			if (tmp->result == le32_to_cpu(0xffff))
4480     			    continue;
4481     			tmp->result = le32_to_cpu(tmp->result);
4482     #else			
4483     			if (((tmp->result & 0xff) == 0xff) ||
4484     			    ((tmp->result & 0xff00) == 0xff00))
4485     			    continue;
4486     #endif			    
4487     
4488     			search_found = 1;
4489     
4490     			/* Important - remove from list _before_ done is called */
4491     			if (cmd_prev_ptr)
4492     			    *cmd_prev_ptr = (struct NCR53c7x0_cmd *) cmd->next;
4493     
4494     			--hostdata->busy[tmp->target][tmp->lun];
4495         	    	    	cmd->next = hostdata->free;
4496         	    	    	hostdata->free = cmd;
4497     
4498         	    	    	tmp->host_scribble = NULL;
4499     
4500     			if (hostdata->options & OPTION_DEBUG_INTR) {
4501     			    printk ("scsi%d : command complete : pid %lu, id %d,lun %d result 0x%x ", 
4502     				host->host_no, tmp->pid, tmp->target, tmp->lun, tmp->result);
4503     			    print_command (tmp->cmnd);
4504     			}
4505     			
4506     #if 0
4507     			hostdata->options &= ~OPTION_DEBUG_INTR;
4508     #endif
4509     			tmp->scsi_done(tmp);
4510     			goto restart;
4511     
4512     		    }
4513     		    restore_flags(flags);
4514     		   
4515         /*
4516          * I think that we're stacking INTFLY interrupts; taking care of 
4517          * all the finished commands on the first one, and then getting
4518          * worried when we see the next one.  The magic with have_intfly
4519          * should tell if this is the case..
4520          */
4521     		
4522     		    if (!search_found && !have_intfly)  {
4523     			printk ("scsi%d : WARNING : INTFLY with no completed commands.\n",
4524     			    host->host_no);
4525     		    } else if (!have_intfly)  {
4526     			have_intfly = 1; 
4527         		    	run_process_issue_queue();
4528     		    }
4529     		}
4530     
4531     		if (istat & (ISTAT_SIP|ISTAT_DIP)) {
4532     		    done = 0;
4533     		    interrupted = 1;
4534         	    	    hostdata->state = STATE_HALTED;
4535     
4536     		    if (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
4537     			SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) 
4538     			printk ("scsi%d : SCSI FIFO not empty\n", 
4539     			    host->host_no);
4540     
4541     		    /*
4542     		     * NCR53c700 and NCR53c700-66 change the current SCSI
4543     		     * process, hostdata->curr, in the Linux driver so
4544     		     * cmd = hostdata->curr.
4545     		     *
4546     		     * With other chips, we must look through the commands
4547     		     * executing and find the command structure which 
4548     		     * corresponds to the DSA register.
4549     		     */
4550     
4551     		    if (hostdata->options & OPTION_700) {
4552     			cmd = (struct NCR53c7x0_cmd *) hostdata->curr;
4553     		    } else {
4554     			dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
4555     			for (cmd = (struct NCR53c7x0_cmd *) 
4556     			    hostdata->running_list; cmd &&
4557         	    	    	    (dsa + (hostdata->dsa_start / sizeof(u32))) != 
4558         	    	    	    	cmd->dsa;
4559     			    cmd = (struct NCR53c7x0_cmd *)(cmd->next));
4560     		    }
4561     		    if (hostdata->options & OPTION_DEBUG_INTR) {
4562     			if (cmd) {
4563     			    printk("scsi%d : interrupt for pid %lu, id %d, lun %d ", 
4564     				host->host_no, cmd->cmd->pid, (int) cmd->cmd->target,
4565     				(int) cmd->cmd->lun);
4566     			    print_command (cmd->cmd->cmnd);
4567     			} else {
4568     			    printk("scsi%d : no active command\n", host->host_no);
4569     			}
4570     		    }
4571     		    if (istat & ISTAT_SIP) {
4572     			if (hostdata->options & OPTION_DEBUG_INTR) 
4573     			    printk ("scsi%d : ISTAT_SIP\n", host->host_no);
4574     			intr_scsi (host, cmd);
4575     		    }
4576     		
4577     		    if (istat & ISTAT_DIP) {
4578     			if (hostdata->options & OPTION_DEBUG_INTR) 
4579     			    printk ("scsi%d : ISTAT_DIP\n", host->host_no);
4580     			intr_dma (host, cmd);
4581     		    }
4582     
4583     		    if (!hostdata->dstat_valid) {
4584     			hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
4585     			hostdata->dstat_valid = 1;
4586     		    }
4587     
4588     	    /* XXX - code check for 700/800 chips */
4589     		    if (!(hostdata->dstat & DSTAT_DFE)) {
4590     			printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
4591     			if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4592     			    printk ("scsi%d: Flushing DMA FIFO\n", 
4593     				host->host_no);
4594     			    NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
4595     			    while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
4596     				DSTAT_DFE));
4597     			} else 
4598     			{
4599     			    NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
4600     			    while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
4601     			}
4602     			hostdata->dstat |= DSTAT_DFE;
4603     		    }
4604     		}
4605     	    } while (interrupted);
4606     
4607     
4608     
4609     	    if (hostdata->intrs != -1)
4610     		hostdata->intrs++;
4611     #if 0
4612     	    if (hostdata->intrs > 40) {
4613     		printk("scsi%d : too many interrupts, halting", host->host_no);
4614     		disable(host);
4615     	    }
4616     #endif
4617     
4618     	    if (!hostdata->idle && hostdata->state == STATE_HALTED) {
4619     		if (!hostdata->dsp_changed) {
4620     		    hostdata->dsp = (u32 *) 
4621     			bus_to_virt(NCR53c7x0_read32(DSP_REG));
4622     		}
4623     			
4624     #if 0
4625     		printk("scsi%d : new dsp is 0x%lx (virt 0x%p)\n",
4626     		    host->host_no,  virt_to_bus(hostdata->dsp), hostdata->dsp);
4627     #endif
4628     		
4629     		hostdata->state = STATE_RUNNING;
4630     		NCR53c7x0_write32 (DSP_REG, virt_to_bus(hostdata->dsp));
4631     	    }
4632     	}
4633         } while (!done);
4634     }
4635     
4636     
4637     /* 
4638      * Function : static int abort_connected (struct Scsi_Host *host)
4639      *
4640      * Purpose : Assuming that the NCR SCSI processor is currently 
4641      * 	halted, break the currently established nexus.  Clean
4642      *	up of the NCR53c7x0_cmd and Scsi_Cmnd structures should
4643      *	be done on receipt of the abort interrupt.
4644      *
4645      * Inputs : host - SCSI host
4646      *
4647      */
4648     
4649     static int 
4650     abort_connected (struct Scsi_Host *host) {
4651     #ifdef NEW_ABORT
4652         NCR53c7x0_local_declare();
4653     #endif
4654         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
4655     	host->hostdata;
4656     /* FIXME : this probably should change for production kernels; at the 
4657        least, counter should move to a per-host structure. */
4658         static int counter = 5;
4659     #ifdef NEW_ABORT
4660         int sstat, phase, offset;
4661         u32 *script;
4662         NCR53c7x0_local_setup(host);
4663     #endif
4664     
4665         if (--counter <= 0) {
4666     	disable(host);
4667     	return 0;
4668         }
4669     
4670         printk ("scsi%d : DANGER : abort_connected() called \n",
4671     	host->host_no);
4672     
4673     #ifdef NEW_ABORT
4674     
4675     /*
4676      * New strategy : Rather than using a generic abort routine,
4677      * we'll specifically try to source or sink the appropriate
4678      * amount of data for the phase we're currently in (taking into 
4679      * account the current synchronous offset) 
4680      */
4681     
4682         sstat = (NCR53c8x0_read8 ((chip / 100) == 8 ? SSTAT1_REG : SSTAT2_REG);
4683         offset = OFFSET (sstat & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
4684         phase = sstat & SSTAT2_PHASE_MASK;
4685     
4686     /*
4687      * SET ATN
4688      * MOVE source_or_sink, WHEN CURRENT PHASE 
4689      * < repeat for each outstanding byte >
4690      * JUMP send_abort_message
4691      */
4692     
4693         script = hostdata->abort_script = kmalloc (
4694     	8  /* instruction size */ * (
4695     	    1 /* set ATN */ +
4696     	    (!offset ? 1 : offset) /* One transfer per outstanding byte */ +
4697     	    1 /* send abort message */),
4698     	GFP_ATOMIC);
4699     
4700     
4701     #else /* def NEW_ABORT */
4702         hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
4703     	    sizeof(u32);
4704     #endif /* def NEW_ABORT */
4705         hostdata->dsp_changed = 1;
4706     
4707     /* XXX - need to flag the command as aborted after the abort_connected
4708      	 code runs 
4709      */
4710         return 0;
4711     }
4712     
4713     /*
4714      * Function : static int datapath_residual (Scsi_Host *host)
4715      *
4716      * Purpose : return residual data count of what's in the chip.
4717      *
4718      * Inputs : host - SCSI host
4719      */
4720     
4721     static int
4722     datapath_residual (struct Scsi_Host *host) {
4723         NCR53c7x0_local_declare();
4724         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
4725         	host->hostdata;
4726         int count, synchronous, sstat;
4727         NCR53c7x0_local_setup(host);
4728         /* COMPAT : the 700 and 700-66 need to use DFIFO_00_BO_MASK */
4729         count = ((NCR53c7x0_read8 (DFIFO_REG) & DFIFO_10_BO_MASK) -
4730     	(NCR53c7x0_read32 (DBC_REG) & DFIFO_10_BO_MASK)) & DFIFO_10_BO_MASK;
4731         synchronous = NCR53c7x0_read8 (SXFER_REG) & SXFER_MO_MASK;
4732         /* COMPAT : DDIR is elsewhere on non-'8xx chips. */
4733         if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4734         /* Receive */
4735     	if (synchronous) 
4736     	    count += (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ? 
4737     		SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
4738     	else
4739     	    if (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ? 
4740     		SSTAT0_REG : SSTAT1_REG) & SSTAT1_ILF)
4741     		++count;
4742         } else {
4743         /* Send */
4744     	sstat = ((hostdata->chip / 100) == 8) ?  NCR53c7x0_read8 (SSTAT0_REG) :
4745     	    NCR53c7x0_read8 (SSTAT1_REG);
4746     	if (sstat & SSTAT1_OLF)
4747     	    ++count;
4748     	if (synchronous && (sstat & SSTAT1_ORF))
4749     	    ++count;
4750         }
4751         return count;
4752     }
4753     
4754     /* 
4755      * Function : static const char * sbcl_to_phase (int sbcl)_
4756      *
4757      * Purpose : Convert SBCL register to user-parsable phase representation
4758      *
4759      * Inputs : sbcl - value of sbcl register
4760      */
4761     
4762     
4763     static const char *
4764     sbcl_to_phase (int sbcl) {
4765         switch (sbcl & SBCL_PHASE_MASK) {
4766         case SBCL_PHASE_DATAIN:
4767     	return "DATAIN";
4768         case SBCL_PHASE_DATAOUT:
4769     	return "DATAOUT";
4770         case SBCL_PHASE_MSGIN:
4771     	return "MSGIN";
4772         case SBCL_PHASE_MSGOUT:
4773     	return "MSGOUT";
4774         case SBCL_PHASE_CMDOUT:
4775     	return "CMDOUT";
4776         case SBCL_PHASE_STATIN:
4777     	return "STATUSIN";
4778         default:
4779     	return "unknown";
4780         }
4781     }
4782     
4783     /* 
4784      * Function : static const char * sstat2_to_phase (int sstat)_
4785      *
4786      * Purpose : Convert SSTAT2 register to user-parsable phase representation
4787      *
4788      * Inputs : sstat - value of sstat register
4789      */
4790     
4791     
4792     static const char *
4793     sstat2_to_phase (int sstat) {
4794         switch (sstat & SSTAT2_PHASE_MASK) {
4795         case SSTAT2_PHASE_DATAIN:
4796     	return "DATAIN";
4797         case SSTAT2_PHASE_DATAOUT:
4798     	return "DATAOUT";
4799         case SSTAT2_PHASE_MSGIN:
4800     	return "MSGIN";
4801         case SSTAT2_PHASE_MSGOUT:
4802     	return "MSGOUT";
4803         case SSTAT2_PHASE_CMDOUT:
4804     	return "CMDOUT";
4805         case SSTAT2_PHASE_STATIN:
4806     	return "STATUSIN";
4807         default:
4808     	return "unknown";
4809         }
4810     }
4811     
4812     /* 
4813      * Function : static void intr_phase_mismatch (struct Scsi_Host *host, 
4814      *	struct NCR53c7x0_cmd *cmd)
4815      *
4816      * Purpose : Handle phase mismatch interrupts
4817      *
4818      * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
4819      * 	may be NULL.
4820      *
4821      * Side effects : The abort_connected() routine is called or the NCR chip 
4822      *	is restarted, jumping to the command_complete entry point, or 
4823      *	patching the address and transfer count of the current instruction 
4824      *	and calling the msg_in entry point as appropriate.
4825      */
4826     
4827     static void 
4828     intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
4829         NCR53c7x0_local_declare();
4830         u32 dbc_dcmd, *dsp, *dsp_next;
4831         unsigned char dcmd, sbcl;
4832         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
4833         	host->hostdata;
4834         int residual;
4835         enum {ACTION_ABORT, ACTION_ABORT_PRINT, ACTION_CONTINUE} action = 
4836     	ACTION_ABORT_PRINT;
4837         const char *where = NULL;
4838         NCR53c7x0_local_setup(host);
4839     
4840         /*
4841          * Corrective action is based on where in the SCSI SCRIPT(tm) the error 
4842          * occurred, as well as which SCSI phase we are currently in.
4843          */
4844         dsp_next = bus_to_virt(NCR53c7x0_read32(DSP_REG));
4845     
4846         /* 
4847          * Fetch the current instruction, and remove the operands for easier 
4848          * interpretation.
4849          */
4850         dbc_dcmd = NCR53c7x0_read32(DBC_REG);
4851         dcmd = (dbc_dcmd & 0xff000000) >> 24;
4852         /*
4853          * Like other processors, the NCR adjusts the instruction pointer before
4854          * instruction decode.  Set the DSP address back to what it should
4855          * be for this instruction based on its size (2 or 3 32 bit words).
4856          */
4857         dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
4858     
4859     
4860         /*
4861          * Read new SCSI phase from the SBCL lines.  Since all of our code uses 
4862          * a WHEN conditional instead of an IF conditional, we don't need to 
4863          * wait for a new REQ.
4864          */
4865         sbcl = NCR53c7x0_read8(SBCL_REG) & SBCL_PHASE_MASK;
4866     
4867         if (!cmd) {
4868     	action = ACTION_ABORT_PRINT;
4869     	where = "no current command";
4870         /*
4871          * The way my SCSI SCRIPTS(tm) are architected, recoverable phase
4872          * mismatches should only occur where we're doing a multi-byte  
4873          * BMI instruction.  Specifically, this means 
4874          *
4875          *  - select messages (a SCSI-I target may ignore additional messages
4876          * 		after the IDENTIFY; any target may reject a SDTR or WDTR)
4877          *
4878          *  - command out (targets may send a message to signal an error 
4879          * 		condition, or go into STATUSIN after they've decided 
4880          *		they don't like the command.
4881          *
4882          *	- reply_message (targets may reject a multi-byte message in the 
4883          *		middle)
4884          *
4885          * 	- data transfer routines (command completion with buffer space
4886          *		left, disconnect message, or error message)
4887          */
4888         } else if (((dsp >= cmd->data_transfer_start && 
4889     	dsp < cmd->data_transfer_end)) || dsp == (cmd->residual + 2)) {
4890     	if ((dcmd & (DCMD_TYPE_MASK|DCMD_BMI_OP_MASK|DCMD_BMI_INDIRECT|
4891     		DCMD_BMI_MSG|DCMD_BMI_CD)) == (DCMD_TYPE_BMI|
4892     		DCMD_BMI_OP_MOVE_I)) {
4893     	    residual = datapath_residual (host);
4894     	    if (hostdata->options & OPTION_DEBUG_DISCONNECT)
4895     	    	printk ("scsi%d : handling residual transfer (+ %d bytes from DMA FIFO)\n", 
4896     		    host->host_no, residual);
4897     
4898     	    /*
4899     	     * The first instruction is a CALL to the alternate handler for 
4900     	     * this data transfer phase, so we can do calls to 
4901     	     * munge_msg_restart as we would if control were passed 
4902     	     * from normal dynamic code.
4903     	     */
4904     	    if (dsp != cmd->residual + 2) {
4905     		cmd->residual[0] = le32_to_cpu(((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
4906     			((dcmd & DCMD_BMI_IO) ? DCMD_TCI_IO : 0)) << 24) | 
4907     		    DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE);
4908     		cmd->residual[1] = le32_to_cpu(virt_to_bus(hostdata->script)
4909     		    + ((dcmd & DCMD_BMI_IO)
4910     		       ? hostdata->E_other_in : hostdata->E_other_out));
4911     	    }
4912     
4913     	    /*
4914     	     * The second instruction is the a data transfer block
4915     	     * move instruction, reflecting the pointer and count at the 
4916     	     * time of the phase mismatch.
4917     	     */
4918     	    cmd->residual[2] = le32_to_cpu(dbc_dcmd + residual);
4919     	    cmd->residual[3] = le32_to_cpu(NCR53c7x0_read32(DNAD_REG) - residual);
4920     
4921     	    /*
4922     	     * The third and final instruction is a jump to the instruction
4923     	     * which follows the instruction which had to be 'split'
4924     	     */
4925     	    if (dsp != cmd->residual + 2) {
4926     		cmd->residual[4] = le32_to_cpu(((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) 
4927     		    << 24) | DBC_TCI_TRUE);
4928     		cmd->residual[5] = le32_to_cpu(virt_to_bus(dsp_next));
4929     	    }
4930     
4931     	    /*
4932     	     * For the sake of simplicity, transfer control to the 
4933     	     * conditional CALL at the start of the residual buffer.
4934     	     */
4935     	    hostdata->dsp = cmd->residual;
4936     	    hostdata->dsp_changed = 1;
4937     	    action = ACTION_CONTINUE;
4938     	} else {
4939     	    where = "non-BMI dynamic DSA code";
4940     	    action = ACTION_ABORT_PRINT;
4941     	}
4942         } else if (dsp == (hostdata->script + hostdata->E_select_msgout / 4)) {
4943     	/* Release ATN */
4944     	NCR53c7x0_write8 (SOCL_REG, 0);
4945     	switch (sbcl) {
4946         /* 
4947          * Some devices (SQ555 come to mind) grab the IDENTIFY message
4948          * sent on selection, and decide to go into COMMAND OUT phase
4949          * rather than accepting the rest of the messages or rejecting
4950          * them.  Handle these devices gracefully.
4951          */
4952     	case SBCL_PHASE_CMDOUT:
4953     	    hostdata->dsp = dsp + 2 /* two _words_ */;
4954     	    hostdata->dsp_changed = 1;
4955     	    printk ("scsi%d : target %d ignored SDTR and went into COMMAND OUT\n", 
4956     		host->host_no, cmd->cmd->target);
4957     	    cmd->flags &= ~CMD_FLAG_SDTR;
4958     	    action = ACTION_CONTINUE;
4959     	    break;
4960     	case SBCL_PHASE_MSGIN:
4961     	    hostdata->dsp = hostdata->script + hostdata->E_msg_in / 
4962     		sizeof(u32);
4963     	    hostdata->dsp_changed = 1;
4964     	    action = ACTION_CONTINUE;
4965     	    break;
4966     	default:
4967     	    where="select message out";
4968     	    action = ACTION_ABORT_PRINT;
4969     	}
4970         /*
4971          * Some SCSI devices will interpret a command as they read the bytes
4972          * off the SCSI bus, and may decide that the command is Bogus before 
4973          * they've read the entire command off the bus.
4974          */
4975         } else if (dsp == hostdata->script + hostdata->E_cmdout_cmdout / sizeof 
4976     	(u32)) {
4977     	hostdata->dsp = hostdata->script + hostdata->E_data_transfer /
4978     	    sizeof (u32);
4979     	hostdata->dsp_changed = 1;
4980     	action = ACTION_CONTINUE;
4981         /* FIXME : we need to handle message reject, etc. within msg_respond. */
4982     #ifdef notyet
4983         } else if (dsp == hostdata->script + hostdata->E_reply_message) {
4984     	switch (sbcl) {
4985         /* Any other phase mismatches abort the currently executing command.  */
4986     #endif
4987         } else {
4988     	where = "unknown location";
4989     	action = ACTION_ABORT_PRINT;
4990         }
4991     
4992         /* Flush DMA FIFO */
4993         if (!hostdata->dstat_valid) {
4994     	hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
4995     	hostdata->dstat_valid = 1;
4996         }
4997         if (!(hostdata->dstat & DSTAT_DFE)) {
4998     	if (NCR53c7x0_read8 (CTEST2_REG_800) & CTEST2_800_DDIR) {
4999     	    printk ("scsi%d: Flushing DMA FIFO\n", 
5000     		    host->host_no);
5001     	    NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_FLF);
5002     	    /* FIXME : what about stacked DMA interrupts? */
5003     	    while (!((hostdata->dstat = NCR53c7x0_read8(DSTAT_REG)) &
5004     		DSTAT_DFE));
5005     	} else {
5006     	    NCR53c7x0_write8 (CTEST3_REG_800, CTEST3_800_CLF);
5007     	    while (NCR53c7x0_read8 (CTEST3_REG_800) & CTEST3_800_CLF);
5008     	}
5009     	hostdata->dstat |= DSTAT_DFE;
5010         }
5011     
5012         switch (action) {
5013         case ACTION_ABORT_PRINT:
5014     	printk("scsi%d : %s : unexpected phase %s.\n",
5015     	     host->host_no, where ? where : "unknown location", 
5016     	     sbcl_to_phase(sbcl));
5017     	print_lots (host);
5018         /* Fall through to ACTION_ABORT */
5019         case ACTION_ABORT:
5020     	abort_connected (host);
5021     	break;
5022         case ACTION_CONTINUE:
5023     	break;
5024         }
5025     
5026     #if 0
5027         if (hostdata->dsp_changed) {
5028     	printk("scsi%d: new dsp 0x%p\n", host->host_no, hostdata->dsp);
5029     	print_insn (host, hostdata->dsp, "", 1);
5030         }
5031     #endif
5032         
5033     }
5034     
5035     /*
5036      * Function : static void intr_bf (struct Scsi_Host *host, 
5037      * 	struct NCR53c7x0_cmd *cmd)
5038      *
5039      * Purpose : handle BUS FAULT interrupts 
5040      *
5041      * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
5042      * 	may be NULL.
5043      */
5044     
5045     static void
5046     intr_bf (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
5047         NCR53c7x0_local_declare();
5048         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5049     	    host->hostdata;
5050         u32 *dsp,
5051     	*next_dsp,		/* Current dsp */
5052         	*dsa,
5053     	dbc_dcmd;		/* DCMD (high eight bits) + DBC */
5054         unsigned short pci_status;
5055         int tmp;
5056         unsigned long flags;
5057         char *reason = NULL;
5058         /* Default behavior is for a silent error, with a retry until we've
5059            exhausted retries. */
5060         enum {MAYBE, ALWAYS, NEVER} retry = MAYBE;
5061         int report = 0;
5062         NCR53c7x0_local_setup(host);
5063     
5064         dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
5065         next_dsp = bus_to_virt (NCR53c7x0_read32(DSP_REG));
5066         dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
5067     /* FIXME - check chip type  */
5068         dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
5069     
5070         /*
5071          * Bus faults can be caused by either a Bad Address or 
5072          * Target Abort. We should check the Received Target Abort
5073          * bit of the PCI status register and Master Abort Bit.
5074          *
5075          * 	- Master Abort bit indicates that no device claimed
5076          *		the address with DEVSEL within five clocks
5077          *
5078          *	- Target Abort bit indicates that a target claimed it,
5079          *		but changed its mind once it saw the byte enables.
5080          *
5081          */
5082     
5083         if ((hostdata->chip / 100) == 8) {
5084     	save_flags (flags);
5085     	cli();
5086     	tmp = pcibios_read_config_word (hostdata->pci_bus, 
5087     	    hostdata->pci_device_fn, PCI_STATUS, &pci_status);
5088     	restore_flags (flags);
5089     	if (tmp == PCIBIOS_SUCCESSFUL) {
5090     	    if (pci_status & PCI_STATUS_REC_TARGET_ABORT) {
5091     		reason = "PCI target abort";
5092     		pci_status &= ~PCI_STATUS_REC_TARGET_ABORT;
5093     	    } else if (pci_status & PCI_STATUS_REC_MASTER_ABORT) {
5094     		reason = "No device asserted PCI DEVSEL within five bus clocks";
5095     		pci_status &= ~PCI_STATUS_REC_MASTER_ABORT;
5096     	    } else if (pci_status & PCI_STATUS_PARITY) {
5097     		report = 1;   
5098     		pci_status &= ~PCI_STATUS_PARITY;
5099     	    }
5100     	} else {
5101     	    printk ("scsi%d : couldn't read status register : error %d\n",
5102     		host->host_no, tmp);
5103     	    retry = NEVER;
5104     	}
5105         }
5106     
5107     #ifndef notyet
5108         report = 1;
5109     #endif
5110         if (report && reason) {
5111     	printk(KERN_ALERT "scsi%d : BUS FAULT reason = %s\n",
5112     	     host->host_no, reason ? reason : "unknown");
5113     	print_lots (host);
5114         }
5115     
5116     #ifndef notyet
5117         retry = NEVER;
5118     #endif
5119     
5120         /* 
5121          * TODO : we should attempt to recover from any spurious bus 
5122          * faults.  After X retries, we should figure that things are 
5123          * sufficiently wedged, and call NCR53c7xx_reset.
5124          *
5125          * This code should only get executed once we've decided that we 
5126          * cannot retry.
5127          */
5128     
5129         if (retry == NEVER) {
5130         	printk(KERN_ALERT "          mail drew@PoohSticks.ORG\n");
5131         	FATAL (host);
5132         }
5133     }
5134     
5135     /*
5136      * Function : static void intr_dma (struct Scsi_Host *host, 
5137      * 	struct NCR53c7x0_cmd *cmd)
5138      *
5139      * Purpose : handle all DMA interrupts, indicated by the setting 
5140      * 	of the DIP bit in the ISTAT register.
5141      *
5142      * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
5143      * 	may be NULL.
5144      */
5145     
5146     static void 
5147     intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
5148         NCR53c7x0_local_declare();
5149         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5150     	host->hostdata;
5151         unsigned char dstat;	/* DSTAT */	
5152         u32 *dsp,
5153     	*next_dsp,		/* Current dsp */
5154         	*dsa,
5155     	dbc_dcmd;		/* DCMD (high eight bits) + DBC */
5156         int tmp;
5157         unsigned long flags;
5158         NCR53c7x0_local_setup(host);
5159     
5160         if (!hostdata->dstat_valid) {
5161     	hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
5162     	hostdata->dstat_valid = 1;
5163         }
5164         
5165         dstat = hostdata->dstat;
5166         
5167         if (hostdata->options & OPTION_DEBUG_INTR)
5168     	printk("scsi%d : DSTAT=0x%x\n", host->host_no, (int) dstat);
5169     
5170         dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
5171         next_dsp = bus_to_virt(NCR53c7x0_read32(DSP_REG));
5172         dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
5173     /* XXX - check chip type */
5174         dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
5175     
5176         /*
5177          * DSTAT_ABRT is the aborted interrupt.  This is set whenever the 
5178          * SCSI chip is aborted.  
5179          * 
5180          * With NCR53c700 and NCR53c700-66 style chips, we should only 
5181          * get this when the chip is currently running the accept 
5182          * reselect/select code and we have set the abort bit in the 
5183          * ISTAT register.
5184          *
5185          */
5186         
5187         if (dstat & DSTAT_ABRT) {
5188     #if 0
5189     	/* XXX - add code here to deal with normal abort */
5190     	if ((hostdata->options & OPTION_700) && (hostdata->state ==
5191     	    STATE_ABORTING)) {
5192     	} else 
5193     #endif
5194     	{
5195     	    printk(KERN_ALERT "scsi%d : unexpected abort interrupt at\n" 
5196     		   "         ", host->host_no);
5197     	    print_insn (host, dsp, KERN_ALERT "s ", 1);
5198     	    FATAL (host);
5199     	}
5200         }
5201     
5202         /*
5203          * DSTAT_SSI is the single step interrupt.  Should be generated 
5204          * whenever we have single stepped or are tracing.
5205          */
5206     
5207         if (dstat & DSTAT_SSI) {
5208     	if (hostdata->options & OPTION_DEBUG_TRACE) {
5209     	} else if (hostdata->options & OPTION_DEBUG_SINGLE) {
5210     	    print_insn (host, dsp, "s ", 0);
5211     	    save_flags(flags);
5212     	    cli();
5213     /* XXX - should we do this, or can we get away with writing dsp? */
5214     
5215     	    NCR53c7x0_write8 (DCNTL_REG, (NCR53c7x0_read8(DCNTL_REG) & 
5216         	    	~DCNTL_SSM) | DCNTL_STD);
5217     	    restore_flags(flags);
5218     	} else {
5219     	    printk(KERN_ALERT "scsi%d : unexpected single step interrupt at\n"
5220     		   "         ", host->host_no);
5221     	    print_insn (host, dsp, KERN_ALERT "", 1);
5222     	    printk(KERN_ALERT "         mail drew@PoohSticks.ORG\n");
5223         	    FATAL (host);
5224         	}
5225         }
5226     
5227         /*
5228          * DSTAT_IID / DSTAT_OPC (same bit, same meaning, only the name 
5229          * is different) is generated whenever an illegal instruction is 
5230          * encountered.  
5231          * 
5232          * XXX - we may want to emulate INTFLY here, so we can use 
5233          *    the same SCSI SCRIPT (tm) for NCR53c710 through NCR53c810  
5234          *	  chips.
5235          */
5236     
5237         if (dstat & DSTAT_OPC) {
5238         /* 
5239          * Ascertain if this IID interrupts occurred before or after a STO 
5240          * interrupt.  Since the interrupt handling code now leaves 
5241          * DSP unmodified until _after_ all stacked interrupts have been
5242          * processed, reading the DSP returns the original DSP register.
5243          * This means that if dsp lies between the select code, and 
5244          * message out following the selection code (where the IID interrupt
5245          * would have to have occurred by due to the implicit wait for REQ),
5246          * we have an IID interrupt resulting from a STO condition and 
5247          * can ignore it.
5248          */
5249     
5250     	if (((dsp >= (hostdata->script + hostdata->E_select / sizeof(u32))) &&
5251     	    (dsp <= (hostdata->script + hostdata->E_select_msgout / 
5252         	    sizeof(u32) + 8))) || (hostdata->test_running == 2)) {
5253     	    if (hostdata->options & OPTION_DEBUG_INTR) 
5254     		printk ("scsi%d : ignoring DSTAT_IID for SSTAT_STO\n",
5255     		    host->host_no);
5256     	    if (hostdata->expecting_iid) {
5257     		hostdata->expecting_iid = 0;
5258     		hostdata->idle = 1;
5259     		if (hostdata->test_running == 2) {
5260     		    hostdata->test_running = 0;
5261     		    hostdata->test_completed = 3;
5262     		} else if (cmd) 
5263     			abnormal_finished (cmd, DID_BAD_TARGET << 16);
5264     	    } else {
5265     		hostdata->expecting_sto = 1;
5266     	    }
5267         /*
5268          * We can't guarantee we'll be able to execute the WAIT DISCONNECT
5269          * instruction within the 3.4us of bus free and arbitration delay
5270          * that a target can RESELECT in and assert REQ after we've dropped
5271          * ACK.  If this happens, we'll get an illegal instruction interrupt.
5272          * Doing away with the WAIT DISCONNECT instructions broke everything,
5273          * so instead I'll settle for moving one WAIT DISCONNECT a few 
5274          * instructions closer to the CLEAR ACK before it to minimize the
5275          * chances of this happening, and handle it if it occurs anyway.
5276          *
5277          * Simply continue with what we were doing, and control should
5278          * be transferred to the schedule routine which will ultimately
5279          * pass control onto the reselection or selection (not yet)
5280          * code.
5281          */
5282     	} else if (dbc_dcmd == 0x48000000 && (NCR53c7x0_read8 (SBCL_REG) &
5283     	    SBCL_REQ)) {
5284     	    if (!(hostdata->options & OPTION_NO_PRINT_RACE))
5285     	    {
5286     		printk("scsi%d: REQ before WAIT DISCONNECT IID\n", 
5287     		    host->host_no);
5288     		hostdata->options |= OPTION_NO_PRINT_RACE;
5289     	    }
5290     	} else {
5291     	    printk(KERN_ALERT "scsi%d : illegal instruction\n", host->host_no);
5292     	    print_lots (host);
5293     	    printk(KERN_ALERT "         mail drew@PoohSticks.ORG with ALL\n"
5294     		              "         boot messages and diagnostic output\n");
5295         	    FATAL (host);
5296     	}
5297         }
5298     
5299         /* 
5300          * DSTAT_BF are bus fault errors
5301          */
5302         
5303         if (dstat & DSTAT_800_BF) {
5304     	intr_bf (host, cmd);
5305         }
5306     	
5307     
5308         /* 
5309          * DSTAT_SIR interrupts are generated by the execution of 
5310          * the INT instruction.  Since the exact values available 
5311          * are determined entirely by the SCSI script running, 
5312          * and are local to a particular script, a unique handler
5313          * is called for each script.
5314          */
5315     
5316         if (dstat & DSTAT_SIR) {
5317     	if (hostdata->options & OPTION_DEBUG_INTR)
5318     	    printk ("scsi%d : DSTAT_SIR\n", host->host_no);
5319     	switch ((tmp = hostdata->dstat_sir_intr (host, cmd))) {
5320     	case SPECIFIC_INT_NOTHING:
5321     	case SPECIFIC_INT_RESTART:
5322     	    break;
5323     	case SPECIFIC_INT_ABORT:
5324     	    abort_connected(host);
5325     	    break;
5326     	case SPECIFIC_INT_PANIC:
5327     	    printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
5328     	    print_insn (host, dsp, KERN_ALERT "", 1);
5329     	    printk(KERN_ALERT "          dstat_sir_intr() returned SPECIFIC_INT_PANIC\n");
5330         	    FATAL (host);
5331     	    break;
5332     	case SPECIFIC_INT_BREAK:
5333     	    intr_break (host, cmd);
5334     	    break;
5335     	default:
5336     	    printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
5337     	    print_insn (host, dsp, KERN_ALERT "", 1);
5338     	    printk(KERN_ALERT"          dstat_sir_intr() returned unknown value %d\n", 
5339     		tmp);
5340         	    FATAL (host);
5341     	}
5342         } 
5343     
5344         if ((hostdata->chip / 100) == 8 && (dstat & DSTAT_800_MDPE)) {
5345     	printk(KERN_ALERT "scsi%d : Master Data Parity Error\n",
5346     	    host->host_no);
5347     	FATAL (host);
5348         }
5349     }
5350     
5351     /*
5352      * Function : static int print_insn (struct Scsi_Host *host, 
5353      * 	u32 *insn, int kernel)
5354      *
5355      * Purpose : print numeric representation of the instruction pointed
5356      * 	to by insn to the debugging or kernel message buffer
5357      *	as appropriate.  
5358      *
5359      * 	If desired, a user level program can interpret this 
5360      * 	information.
5361      *
5362      * Inputs : host, insn - host, pointer to instruction, prefix - 
5363      *	string to prepend, kernel - use printk instead of debugging buffer.
5364      *
5365      * Returns : size, in u32s, of instruction printed.
5366      */
5367     
5368     /*
5369      * FIXME: should change kernel parameter so that it takes an ENUM
5370      * 	specifying severity - either KERN_ALERT or KERN_PANIC so
5371      *	all panic messages are output with the same severity.
5372      */
5373     
5374     static int 
5375     print_insn (struct Scsi_Host *host, const u32 *insn, 
5376         const char *prefix, int kernel) {
5377         char buf[160], 		/* Temporary buffer and pointer.  ICKY 
5378     				   arbitrary length.  */
5379     
5380     		
5381     	*tmp;			
5382         unsigned char dcmd;		/* dcmd register for *insn */
5383         int size;
5384     
5385         /* 
5386          * Check to see if the instruction pointer is not bogus before 
5387          * indirecting through it; avoiding red-zone at start of 
5388          * memory.
5389          *
5390          * FIXME: icky magic needs to happen here on non-intel boxes which
5391          * don't have kernel memory mapped in like this.  Might be reasonable
5392          * to use vverify()?
5393          */
5394     
5395         if (virt_to_phys((void *)insn) < PAGE_SIZE || 
5396     	virt_to_phys((void *)(insn + 8)) > virt_to_phys(high_memory) ||
5397     	((((dcmd = (insn[0] >> 24) & 0xff) & DCMD_TYPE_MMI) == DCMD_TYPE_MMI) &&
5398     	virt_to_phys((void *)(insn + 12)) > virt_to_phys(high_memory))) {
5399     	size = 0;
5400     	sprintf (buf, "%s%p: address out of range\n",
5401     	    prefix, insn);
5402         } else {
5403     /* 
5404      * FIXME : (void *) cast in virt_to_bus should be unnecessary, because
5405      * 	it should take const void * as argument.
5406      */
5407     	sprintf(buf, "%s0x%lx (virt 0x%p) : 0x%08x 0x%08x (virt 0x%p)", 
5408     	    (prefix ? prefix : ""), virt_to_bus((void *) insn), insn,  
5409     	    insn[0], insn[1], bus_to_virt (le32_to_cpu(insn[1])));
5410     	tmp = buf + strlen(buf);
5411     	if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)  {
5412     	    sprintf (tmp, " 0x%08x (virt 0x%p)\n", insn[2], 
5413     		bus_to_virt(le32_to_cpu(insn[2])));
5414     	    size = 3;
5415     	} else {
5416     	    sprintf (tmp, "\n");
5417     	    size = 2;
5418     	}
5419         }
5420     
5421         if (kernel) 
5422     	printk ("%s", buf);
5423     #ifdef NCR_DEBUG
5424         else {
5425     	size_t len = strlen(buf);
5426     	debugger_kernel_write(host, buf, len);
5427         }
5428     #endif
5429         return size;
5430     }
5431     
5432     /*
5433      * Function : static const char *ncr_state (int state)
5434      *
5435      * Purpose : convert state (probably from hostdata->state) to a string
5436      *
5437      * Inputs : state
5438      *
5439      * Returns : char * representation of state, "unknown" on error.
5440      */
5441     
5442     #if 0
5443     static const char *
5444     ncr_state (int state) {
5445         switch (state) {
5446         case STATE_HALTED: return "halted";
5447         case STATE_WAITING: return "waiting";
5448         case STATE_RUNNING: return "running";
5449         case STATE_ABORTING: return "aborting";
5450         case STATE_DISABLED: return "disabled";
5451         default: return "unknown";
5452         }
5453     }
5454     #endif
5455     
5456     /*
5457      * Function : int NCR53c7xx_abort (Scsi_Cmnd *cmd)
5458      * 
5459      * Purpose : Abort an errant SCSI command, doing all necessary
5460      *	cleanup of the issue_queue, running_list, shared Linux/NCR
5461      *	dsa issue and reconnect queues.
5462      *
5463      * Inputs : cmd - command to abort, code - entire result field
5464      *
5465      * Returns : 0 on success, -1 on failure.
5466      */
5467     
5468     int 
5469     NCR53c7xx_abort (Scsi_Cmnd *cmd) {
5470         NCR53c7x0_local_declare();
5471         struct Scsi_Host *host = cmd->host;
5472         struct NCR53c7x0_hostdata *hostdata = host ? (struct NCR53c7x0_hostdata *) 
5473     	host->hostdata : NULL;
5474         unsigned long flags;
5475         unsigned long result;
5476         struct NCR53c7x0_cmd *curr, **prev;
5477         Scsi_Cmnd *me, **last;
5478     #if 0
5479         static long cache_pid = -1;
5480     #endif
5481     
5482     
5483         if (!host) {
5484     	printk ("Bogus SCSI command pid %ld; no host structure\n",
5485     	    cmd->pid);
5486     	return SCSI_ABORT_ERROR;
5487         } else if (!hostdata) {
5488     	printk ("Bogus SCSI host %d; no hostdata\n", host->host_no);
5489     	return SCSI_ABORT_ERROR;
5490         }
5491         NCR53c7x0_local_setup(host);
5492     
5493     /*
5494      * CHECK : I don't think that reading ISTAT will unstack any interrupts,
5495      *	since we need to write the INTF bit to clear it, and SCSI/DMA
5496      * 	interrupts don't clear until we read SSTAT/SIST and DSTAT registers.
5497      *	
5498      *	See that this is the case.
5499      *
5500      * I suspect that several of our failures may be coming from a new fatal
5501      * interrupt (possibly due to a phase mismatch) happening after we've left
5502      * the interrupt handler, but before the PIC has had the interrupt condition
5503      * cleared.
5504      */
5505     
5506         if (NCR53c7x0_read8(hostdata->istat) & 
5507     	(ISTAT_DIP|ISTAT_SIP|
5508     	    (hostdata->chip / 100 == 8 ? ISTAT_800_INTF : 0))) {
5509     	printk ("scsi%d : dropped interrupt for command %ld\n", host->host_no,
5510     	    cmd->pid);
5511     	NCR53c7x0_intr (host->irq, NULL, NULL);
5512     	return SCSI_ABORT_BUSY;
5513         }
5514     	
5515         save_flags(flags);
5516         cli();
5517     #if 0
5518         if (cache_pid == cmd->pid) 
5519     	panic ("scsi%d : bloody fetus %d\n", host->host_no, cmd->pid);
5520         else
5521     	cache_pid = cmd->pid;
5522     #endif
5523     	
5524     
5525     /*
5526      * The command could be hiding in the issue_queue.  This would be very
5527      * nice, as commands can't be moved from the high level driver's issue queue 
5528      * into the shared queue until an interrupt routine is serviced, and this
5529      * moving is atomic.  
5530      *
5531      * If this is the case, we don't have to worry about anything - we simply
5532      * pull the command out of the old queue, and call it aborted.
5533      */
5534     
5535         for (me = (Scsi_Cmnd *) hostdata->issue_queue, 
5536              last = (Scsi_Cmnd **) &(hostdata->issue_queue);
5537     	 me && me != cmd;  last = (Scsi_Cmnd **)&(me->SCp.ptr), 
5538     	 me = (Scsi_Cmnd *)me->SCp.ptr);
5539     
5540         if (me) {
5541     	*last = (Scsi_Cmnd *) me->SCp.ptr;
5542     	if (me->host_scribble) {
5543     	    ((struct NCR53c7x0_cmd *)me->host_scribble)->next = hostdata->free;
5544     	    hostdata->free = (struct NCR53c7x0_cmd *) me->host_scribble;
5545     	    me->host_scribble = NULL;
5546     	}
5547     	cmd->result = DID_ABORT << 16;
5548     	cmd->scsi_done(cmd);
5549     	printk ("scsi%d : found command %ld in Linux issue queue\n", 
5550     	    host->host_no, me->pid);
5551     	restore_flags(flags);
5552         	run_process_issue_queue();
5553     	return SCSI_ABORT_SUCCESS;
5554         }
5555     
5556     /* 
5557      * That failing, the command could be in our list of already executing 
5558      * commands.  If this is the case, drastic measures are called for.  
5559      */ 
5560     
5561         for (curr = (struct NCR53c7x0_cmd *) hostdata->running_list, 
5562         	 prev = (struct NCR53c7x0_cmd **) &(hostdata->running_list);
5563     	 curr && curr->cmd != cmd; prev = (struct NCR53c7x0_cmd **) 
5564              &(curr->next), curr = (struct NCR53c7x0_cmd *) curr->next);
5565     
5566         if (curr) {
5567     	result = le32_to_cpu(cmd->result);
5568     	if ((result & 0xff) != 0xff && (result & 0xff00) != 0xff00) {
5569     	    if (prev)
5570     		*prev = (struct NCR53c7x0_cmd *) curr->next;
5571     	    curr->next = (struct NCR53c7x0_cmd *) hostdata->free;
5572     	    cmd->host_scribble = NULL;
5573     	    hostdata->free = curr;
5574     	    cmd->scsi_done(cmd);
5575     	printk ("scsi%d : found finished command %ld in running list\n", 
5576     	    host->host_no, cmd->pid);
5577     	    restore_flags(flags);
5578     	    return SCSI_ABORT_NOT_RUNNING;
5579     	} else {
5580     	    printk ("scsi%d : DANGER : command running, can not abort.\n",
5581     		cmd->host->host_no);
5582     	    restore_flags(flags);
5583     	    return SCSI_ABORT_BUSY;
5584     	}
5585         }
5586     
5587     /* 
5588      * And if we couldn't find it in any of our queues, it must have been 
5589      * a dropped interrupt.
5590      */
5591     
5592         curr = (struct NCR53c7x0_cmd *) cmd->host_scribble;
5593         if (curr) {
5594     	curr->next = hostdata->free;
5595     	hostdata->free = curr;
5596     	cmd->host_scribble = NULL;
5597         }
5598     
5599         result = le32_to_cpu(cmd->result);
5600         if (((result & 0xff00) == 0xff00) ||
5601     	((result & 0xff) == 0xff)) {
5602     	printk ("scsi%d : did this command ever run?\n", host->host_no);
5603     	cmd->result = DID_ABORT << 16;
5604         } else {
5605     	printk ("scsi%d : probably lost INTFLY, normal completion\n", 
5606     	    host->host_no);
5607     /* 
5608      * FIXME : We need to add an additional flag which indicates if a 
5609      * command was ever counted as BUSY, so if we end up here we can
5610      * decrement the busy count if and only if it is necessary.
5611      */
5612             --hostdata->busy[cmd->target][cmd->lun];
5613         }
5614         restore_flags(flags);
5615         cmd->scsi_done(cmd);
5616     
5617     /* 
5618      * We need to run process_issue_queue since termination of this command 
5619      * may allow another queued command to execute first? 
5620      */
5621         return SCSI_ABORT_NOT_RUNNING;
5622     }
5623     
5624     /*
5625      * Function : int NCR53c7xx_reset (Scsi_Cmnd *cmd) 
5626      * 
5627      * Purpose : perform a hard reset of the SCSI bus and NCR
5628      * 	chip.
5629      *
5630      * Inputs : cmd - command which caused the SCSI RESET
5631      *
5632      * Returns : 0 on success.
5633      */
5634      
5635     int 
5636     NCR53c7xx_reset (Scsi_Cmnd *cmd, unsigned int reset_flags) {
5637         NCR53c7x0_local_declare();
5638         unsigned long flags;
5639         int found = 0;
5640         struct NCR53c7x0_cmd * c;
5641         Scsi_Cmnd *tmp;
5642         /*
5643          * When we call scsi_done(), it's going to wake up anything sleeping on the
5644          * resources which were in use by the aborted commands, and we'll start to 
5645          * get new commands.
5646          *
5647          * We can't let this happen until after we've re-initialized the driver
5648          * structures, and can't reinitialize those structures until after we've 
5649          * dealt with their contents.
5650          *
5651          * So, we need to find all of the commands which were running, stick
5652          * them on a linked list of completed commands (we'll use the host_scribble
5653          * pointer), do our reinitialization, and then call the done function for
5654          * each command.  
5655          */
5656         Scsi_Cmnd *nuke_list = NULL;
5657         struct Scsi_Host *host = cmd->host;
5658         struct NCR53c7x0_hostdata *hostdata = 
5659         	(struct NCR53c7x0_hostdata *) host->hostdata;
5660     
5661         NCR53c7x0_local_setup(host);
5662         save_flags(flags);
5663         cli();
5664         ncr_halt (host);
5665         print_lots (host);
5666         dump_events (host, 30);
5667         ncr_scsi_reset (host);
5668         for (tmp = nuke_list = return_outstanding_commands (host, 1 /* free */,
5669     	0 /* issue */ ); tmp; tmp = (Scsi_Cmnd *) tmp->SCp.buffer)
5670     	if (tmp == cmd) {
5671     	    found = 1;
5672     	    break;
5673     	}
5674     	    
5675         /* 
5676          * If we didn't find the command which caused this reset in our running
5677          * list, then we've lost it.  See that it terminates normally anyway.
5678          */
5679         if (!found) {
5680         	c = (struct NCR53c7x0_cmd *) cmd->host_scribble;
5681         	if (c) {
5682     	    cmd->host_scribble = NULL;
5683         	    c->next = hostdata->free;
5684         	    hostdata->free = c;
5685         	} else
5686     	    printk ("scsi%d: lost command %ld\n", host->host_no, cmd->pid);
5687     	cmd->SCp.buffer = (struct scatterlist *) nuke_list;
5688     	nuke_list = cmd;
5689         }
5690     
5691         NCR53c7x0_driver_init (host);
5692         hostdata->soft_reset (host);
5693         if (hostdata->resets == 0) 
5694     	disable(host);
5695         else if (hostdata->resets != -1)
5696     	--hostdata->resets;
5697         restore_flags(flags);
5698         for (; nuke_list; nuke_list = tmp) {
5699     	tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
5700         	nuke_list->result = DID_RESET << 16;
5701     	nuke_list->scsi_done (nuke_list);
5702         }
5703         restore_flags(flags);
5704         return SCSI_RESET_SUCCESS;
5705     }
5706     
5707     /*
5708      * The NCR SDMS bios follows Annex A of the SCSI-CAM draft, and 
5709      * therefore shares the scsicam_bios_param function.
5710      */
5711     
5712     /*
5713      * Function : int insn_to_offset (Scsi_Cmnd *cmd, u32 *insn)
5714      *
5715      * Purpose : convert instructions stored at NCR pointer into data 
5716      *	pointer offset.
5717      * 
5718      * Inputs : cmd - SCSI command; insn - pointer to instruction.  Either current
5719      *	DSP, or saved data pointer.
5720      *
5721      * Returns : offset on success, -1 on failure.
5722      */
5723     
5724     
5725     static int 
5726     insn_to_offset (Scsi_Cmnd *cmd, u32 *insn) {
5727         struct NCR53c7x0_hostdata *hostdata = 
5728     	(struct NCR53c7x0_hostdata *) cmd->host->hostdata;
5729         struct NCR53c7x0_cmd *ncmd = 
5730     	(struct NCR53c7x0_cmd *) cmd->host_scribble;
5731         int offset = 0, buffers;
5732         struct scatterlist *segment;
5733         char *ptr;
5734         int found = 0;
5735     
5736     /*
5737      * With the current code implementation, if the insn is inside dynamically 
5738      * generated code, the data pointer will be the instruction preceding 
5739      * the next transfer segment.
5740      */
5741     
5742         if (!check_address ((unsigned long) ncmd, sizeof (struct NCR53c7x0_cmd)) &&
5743     	((insn >= ncmd->data_transfer_start &&  
5744         	    insn < ncmd->data_transfer_end) ||
5745         	(insn >= ncmd->residual &&
5746         	    insn < (ncmd->residual + 
5747         	    	sizeof(ncmd->residual))))) {
5748     	    ptr = bus_to_virt(le32_to_cpu(insn[3]));
5749     
5750     	    if ((buffers = cmd->use_sg)) {
5751         	    	for (offset = 0, 
5752     		     	segment = (struct scatterlist *) cmd->buffer;
5753         	    	     buffers && !((found = ((ptr >= segment->address) && 
5754         	    	    	    (ptr < (segment->address + segment->length)))));
5755         	    	     --buffers, offset += segment->length, ++segment)
5756     #if 0
5757     		    printk("scsi%d: comparing 0x%p to 0x%p\n", 
5758     			cmd->host->host_no, saved, segment->address);
5759     #else
5760     		    ;
5761     #endif
5762         	    	    offset += ptr - segment->address;
5763         	    } else {
5764     		found = 1;
5765         	    	offset = ptr - (char *) (cmd->request_buffer);
5766         	    }
5767         } else if ((insn >= hostdata->script + 
5768     		hostdata->E_data_transfer / sizeof(u32)) &&
5769     	       (insn <= hostdata->script +
5770     		hostdata->E_end_data_transfer / sizeof(u32))) {
5771         	found = 1;
5772     	offset = 0;
5773         }
5774         return found ? offset : -1;
5775     }
5776     
5777     
5778     
5779     /*
5780      * Function : void print_progress (Scsi_Cmnd *cmd) 
5781      * 
5782      * Purpose : print the current location of the saved data pointer
5783      *
5784      * Inputs : cmd - command we are interested in
5785      *
5786      */
5787     
5788     static void 
5789     print_progress (Scsi_Cmnd *cmd) {
5790         NCR53c7x0_local_declare();
5791         struct NCR53c7x0_cmd *ncmd = 
5792     	(struct NCR53c7x0_cmd *) cmd->host_scribble;
5793         int offset, i;
5794         char *where;
5795         u32 *ptr;
5796         NCR53c7x0_local_setup (cmd->host);
5797         for (i = 0; i < 2; ++i) {
5798     	if (check_address ((unsigned long) ncmd, 
5799     	    sizeof (struct NCR53c7x0_cmd)) == -1) 
5800     	    continue;
5801     	if (!i) {
5802     	    where = "saved";
5803     	    ptr = bus_to_virt(le32_to_cpu(ncmd->saved_data_pointer));
5804     	} else {
5805     	    where = "active";
5806     	    ptr = bus_to_virt (NCR53c7x0_read32 (DSP_REG) -
5807     		NCR53c7x0_insn_size (NCR53c7x0_read8 (DCMD_REG)) *
5808     		sizeof(u32));
5809     	} 
5810     	offset = insn_to_offset (cmd, ptr);
5811     
5812     	if (offset != -1) 
5813     	    printk ("scsi%d : %s data pointer at offset %d\n",
5814     		cmd->host->host_no, where, offset);
5815     	else {
5816     	    int size;
5817     	    printk ("scsi%d : can't determine %s data pointer offset\n",
5818     		cmd->host->host_no, where);
5819     	    if (ncmd) {
5820     		size = print_insn (cmd->host, 
5821     		    bus_to_virt(le32_to_cpu(ncmd->saved_data_pointer)), "", 1);
5822     		print_insn (cmd->host, 
5823     		    bus_to_virt(le32_to_cpu(ncmd->saved_data_pointer)) + size * sizeof(u32),
5824     		    "", 1);
5825     	    }
5826     	}
5827         }
5828     }
5829     
5830     
5831     static void 
5832     print_dsa (struct Scsi_Host *host, u32 *dsa, const char *prefix) {
5833         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5834     	host->hostdata;
5835         int i, len;
5836         char *ptr;
5837         Scsi_Cmnd *cmd;
5838     
5839         if (check_address ((unsigned long) dsa, hostdata->dsa_end - 
5840     	hostdata->dsa_start) == -1) {
5841     	printk("scsi%d : bad dsa virt 0x%p\n", host->host_no, dsa);
5842     	return;
5843         }
5844         printk("%sscsi%d : dsa at phys 0x%lx (virt 0x%p)\n"
5845     	    "        + %d : dsa_msgout length = %u, data = 0x%x (virt 0x%p)\n" ,
5846         	    prefix ? prefix : "",
5847         	    host->host_no,  virt_to_bus (dsa), dsa, hostdata->dsa_msgout,
5848         	    le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32)]),
5849     	    le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32) + 1]),
5850     	    bus_to_virt (le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32) + 1])));
5851     
5852         /* 
5853          * Only print messages if they're sane in length so we don't
5854          * blow the kernel printk buffer on something which won't buy us
5855          * anything.
5856          */
5857     
5858         if (le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32)]) < 
5859     	    sizeof (hostdata->free->select)) 
5860     	for (i = le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32)]),
5861     	    ptr = bus_to_virt (le32_to_cpu(dsa[hostdata->dsa_msgout / sizeof(u32) + 1])); 
5862     	    i > 0 && !check_address ((unsigned long) ptr, 1);
5863     	    ptr += len, i -= len) {
5864     	    printk("               ");
5865     	    len = print_msg (ptr);
5866     	    printk("\n");
5867     	    if (!len)
5868     		break;
5869     	}
5870     
5871         printk("        + %d : select_indirect = 0x%x\n",
5872     	hostdata->dsa_select, le32_to_cpu(dsa[hostdata->dsa_select / sizeof(u32)]));
5873         cmd = (Scsi_Cmnd *) bus_to_virt(le32_to_cpu(dsa[hostdata->dsa_cmnd / sizeof(u32)]));
5874         printk("        + %d : dsa_cmnd = 0x%x ", hostdata->dsa_cmnd,
5875     	   (u32) virt_to_bus(cmd));
5876         if (cmd) {
5877     	printk("               result = 0x%x, target = %d, lun = %d, cmd = ",
5878     	    cmd->result, cmd->target, cmd->lun);
5879     	print_command(cmd->cmnd);
5880         } else
5881     	printk("\n");
5882         printk("        + %d : dsa_next = 0x%x\n", hostdata->dsa_next,
5883     	le32_to_cpu(dsa[hostdata->dsa_next / sizeof(u32)]));
5884         if (cmd) { 
5885     	printk("scsi%d target %d : sxfer_sanity = 0x%x, scntl3_sanity = 0x%x\n"
5886     	       "                   script : ",
5887     	    host->host_no, cmd->target,
5888     	    hostdata->sync[cmd->target].sxfer_sanity,
5889     	    hostdata->sync[cmd->target].scntl3_sanity);
5890     	for (i = 0; i < (sizeof(hostdata->sync[cmd->target].script) / 4); ++i)
5891     	    printk ("0x%x ", hostdata->sync[cmd->target].script[i]);
5892     	printk ("\n");
5893         	print_progress (cmd);
5894         }
5895     }
5896     /*
5897      * Function : void print_queues (Scsi_Host *host) 
5898      * 
5899      * Purpose : print the contents of the NCR issue and reconnect queues
5900      *
5901      * Inputs : host - SCSI host we are interested in
5902      *
5903      */
5904     
5905     static void 
5906     print_queues (struct Scsi_Host *host) {
5907         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
5908     	host->hostdata;
5909         u32 *dsa, *next_dsa;
5910         volatile u32 *curr;
5911         int left;
5912         Scsi_Cmnd *cmd, *next_cmd;
5913         unsigned long flags;
5914     
5915         printk ("scsi%d : issue queue\n", host->host_no);
5916     
5917         for (left = host->can_queue, cmd = (Scsi_Cmnd *) hostdata->issue_queue; 
5918     	    left >= 0 && cmd; 
5919     	    cmd = next_cmd) {
5920     	next_cmd = (Scsi_Cmnd *) cmd->SCp.ptr;
5921     	save_flags(flags);
5922     	cli();
5923     	if (cmd->host_scribble) {
5924     	    if (check_address ((unsigned long) (cmd->host_scribble), 
5925     		sizeof (cmd->host_scribble)) == -1)
5926     		printk ("scsi%d: scsi pid %ld bad pointer to NCR53c7x0_cmd\n",
5927     		    host->host_no, cmd->pid);
5928     	    /* print_dsa does sanity check on address, no need to check */
5929     	    else
5930     	    	print_dsa (host, bus_to_virt(le32_to_cpu(((struct NCR53c7x0_cmd *) cmd->host_scribble)-> dsa)), "");
5931     	} else 
5932     	    printk ("scsi%d : scsi pid %ld for target %d lun %d has no NCR53c7x0_cmd\n",
5933     		host->host_no, cmd->pid, cmd->target, cmd->lun);
5934     	restore_flags(flags);
5935         }
5936     
5937         if (left <= 0) {
5938     	printk ("scsi%d : loop detected in issue queue\n",
5939     	    host->host_no);
5940         }
5941     
5942         /*
5943          * Traverse the NCR reconnect and start DSA structures, printing out 
5944          * each element until we hit the end or detect a loop.  Currently,
5945          * the reconnect structure is a linked list; and the start structure
5946          * is an array.  Eventually, the reconnect structure will become a 
5947          * list as well, since this simplifies the code.
5948          */
5949     
5950         printk ("scsi%d : schedule dsa array :\n", host->host_no);
5951         for (left = host->can_queue, curr = hostdata->schedule;
5952     	    left > 0; curr += 2, --left)
5953     	if (curr[0] != hostdata->NOP_insn) 
5954     /* FIXME : convert pointer to dsa_begin to pointer to dsa. */
5955     	    print_dsa (host, bus_to_virt (le32_to_cpu(curr[1]) - 
5956     		(hostdata->E_dsa_code_begin - 
5957     		hostdata->E_dsa_code_template)), "");
5958         printk ("scsi%d : end schedule dsa array\n", host->host_no);
5959         
5960         printk ("scsi%d : reconnect_dsa_head :\n", host->host_no);
5961     	    
5962         for (left = host->can_queue, 
5963     	dsa = bus_to_virt (le32_to_cpu(hostdata->reconnect_dsa_head));
5964     	left >= 0 && dsa; 
5965     	dsa = next_dsa) {
5966     	save_flags (flags);
5967     	cli();
5968     	if (check_address ((unsigned long) dsa, sizeof(dsa)) == -1) {
5969     	    printk ("scsi%d: bad DSA pointer 0x%p", host->host_no,
5970     		dsa);
5971     	    next_dsa = NULL;
5972     	}
5973     	else 
5974     	{
5975     	    next_dsa = bus_to_virt(le32_to_cpu(dsa[hostdata->dsa_next / sizeof(u32)]));
5976     	    print_dsa (host, dsa, "");
5977     	}
5978     	restore_flags(flags);
5979         }
5980         printk ("scsi%d : end reconnect_dsa_head\n", host->host_no);
5981         if (left < 0)
5982     	printk("scsi%d: possible loop in ncr reconnect list\n",
5983     	    host->host_no);
5984     }
5985     
5986     static void
5987     print_lots (struct Scsi_Host *host) {
5988         NCR53c7x0_local_declare();
5989         struct NCR53c7x0_hostdata *hostdata = 
5990     	(struct NCR53c7x0_hostdata *) host->hostdata;
5991         u32 *dsp_next, *dsp, *dsa, dbc_dcmd;
5992         unsigned char dcmd, sbcl;
5993         int i, size;
5994         NCR53c7x0_local_setup(host);
5995     
5996         if ((dsp_next = bus_to_virt(NCR53c7x0_read32 (DSP_REG)))) {
5997         	dbc_dcmd = NCR53c7x0_read32(DBC_REG);
5998         	dcmd = (dbc_dcmd & 0xff000000) >> 24;
5999         	dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
6000     	dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
6001     	sbcl = NCR53c7x0_read8 (SBCL_REG);
6002     	    
6003     	
6004         	printk ("scsi%d : DCMD|DBC=0x%x, DNAD=0x%x (virt 0x%p)\n"
6005     		"         DSA=0x%lx (virt 0x%p)\n"
6006     	        "         DSPS=0x%x, TEMP=0x%x (virt 0x%p), DMODE=0x%x\n"
6007     		"         SXFER=0x%x, SCNTL3=0x%x\n"
6008     		"         %s%s%sphase=%s, %d bytes in SCSI FIFO\n"
6009     		"         STEST0=0x%x\n",
6010     	    host->host_no, dbc_dcmd, NCR53c7x0_read32(DNAD_REG), 
6011     		bus_to_virt(NCR53c7x0_read32(DNAD_REG)),
6012     	    virt_to_bus(dsa), dsa, 
6013     	    NCR53c7x0_read32(DSPS_REG), NCR53c7x0_read32(TEMP_REG), 
6014     	    bus_to_virt (NCR53c7x0_read32(TEMP_REG)),
6015     	    (int) NCR53c7x0_read8(hostdata->dmode),
6016     	    (int) NCR53c7x0_read8(SXFER_REG), 
6017     	    (int) NCR53c7x0_read8(SCNTL3_REG_800),
6018     	    (sbcl & SBCL_BSY) ? "BSY " : "",
6019     	    (sbcl & SBCL_SEL) ? "SEL " : "",
6020     	    (sbcl & SBCL_REQ) ? "REQ " : "",
6021     	    sstat2_to_phase(NCR53c7x0_read8 (((hostdata->chip / 100) == 8) ?
6022     	    	SSTAT1_REG : SSTAT2_REG)),
6023     	    (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ? 
6024     		SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT,
6025     	    NCR53c7x0_read8 (STEST0_REG_800));
6026     	printk ("scsi%d : DSP 0x%lx (virt 0x%p) ->\n", host->host_no, 
6027     	    virt_to_bus(dsp), dsp);
6028         	for (i = 6; i > 0; --i, dsp += size)
6029     	    size = print_insn (host, dsp, "", 1);
6030     	if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON)  {
6031     	    printk ("scsi%d : connected (SDID=0x%x, SSID=0x%x)\n",
6032     		host->host_no, NCR53c7x0_read8 (SDID_REG_800),
6033     		NCR53c7x0_read8 (SSID_REG_800));
6034     	    print_dsa (host, dsa, "");
6035     	}
6036     
6037     #if 1
6038     	print_queues (host);
6039     #endif
6040         }
6041     }
6042     
6043     /*
6044      * Function : static int shutdown (struct Scsi_Host *host)
6045      * 
6046      * Purpose : does a clean (we hope) shutdown of the NCR SCSI 
6047      *	chip.  Use prior to dumping core, unloading the NCR driver,
6048      * 
6049      * Returns : 0 on success
6050      */
6051     static int 
6052     shutdown (struct Scsi_Host *host) {
6053         NCR53c7x0_local_declare();
6054         unsigned long flags;
6055         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6056     	host->hostdata;
6057         NCR53c7x0_local_setup(host);
6058         save_flags (flags);
6059         cli();
6060     /* Get in a state where we can reset the SCSI bus */
6061         ncr_halt (host);
6062         ncr_scsi_reset (host);
6063         hostdata->soft_reset(host);
6064     
6065         disable (host);
6066         restore_flags (flags);
6067         return 0;
6068     }
6069     
6070     /*
6071      * Function : void ncr_scsi_reset (struct Scsi_Host *host)
6072      *
6073      * Purpose : reset the SCSI bus.
6074      */
6075     
6076     static void 
6077     ncr_scsi_reset (struct Scsi_Host *host) {
6078         NCR53c7x0_local_declare();
6079         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6080     	host->hostdata;
6081         unsigned long flags;
6082         int sien = 0;
6083         NCR53c7x0_local_setup(host);
6084         save_flags (flags);
6085         cli();
6086         if ((hostdata->chip / 100) == 8) {
6087     	sien = NCR53c7x0_read8(SIEN0_REG_800);
6088     	NCR53c7x0_write8(SIEN0_REG_800, sien & ~SIEN_RST);
6089         }
6090         NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
6091         udelay(25);	/* Minimum amount of time to assert RST */
6092         NCR53c7x0_write8(SCNTL1_REG, 0);
6093         if ((hostdata->chip / 100) == 8) {
6094     	NCR53c7x0_write8(SIEN0_REG_800, sien);
6095         }
6096         restore_flags (flags);
6097     }
6098     
6099     /* 
6100      * Function : void hard_reset (struct Scsi_Host *host)
6101      *
6102      */
6103     
6104     static void 
6105     hard_reset (struct Scsi_Host *host) {
6106         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6107     	host->hostdata;
6108         unsigned long flags;
6109         save_flags (flags);
6110         cli();
6111         ncr_scsi_reset(host);
6112         NCR53c7x0_driver_init (host);
6113         if (hostdata->soft_reset)
6114     	hostdata->soft_reset (host);
6115         restore_flags(flags);
6116     }
6117     
6118     
6119     /*
6120      * Function : Scsi_Cmnd *return_outstanding_commands (struct Scsi_Host *host,
6121      *	int free, int issue)
6122      *
6123      * Purpose : return a linked list (using the SCp.buffer field as next,
6124      *	so we don't perturb hostdata.  We don't use a field of the 
6125      *	NCR53c7x0_cmd structure since we may not have allocated one 
6126      *	for the command causing the reset.) of Scsi_Cmnd structures that 
6127      *  	had propagated below the Linux issue queue level.  If free is set, 
6128      *	free the NCR53c7x0_cmd structures which are associated with 
6129      *	the Scsi_Cmnd structures, and clean up any internal 
6130      *	NCR lists that the commands were on.  If issue is set,
6131      *	also return commands in the issue queue.
6132      *
6133      * Returns : linked list of commands
6134      *
6135      * NOTE : the caller should insure that the NCR chip is halted
6136      *	if the free flag is set. 
6137      */
6138     
6139     static Scsi_Cmnd *
6140     return_outstanding_commands (struct Scsi_Host *host, int free, int issue) {
6141         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6142     	host->hostdata;
6143         struct NCR53c7x0_cmd *c;
6144         int i;
6145         u32 *curr;
6146         Scsi_Cmnd *list = NULL, *tmp;
6147         for (c = (struct NCR53c7x0_cmd *) hostdata->running_list; c; 
6148         	c = (struct NCR53c7x0_cmd *) c->next)  {
6149     	if (c->cmd->SCp.buffer) {
6150     	    printk ("scsi%d : loop detected in running list!\n", host->host_no);
6151     	    break;
6152     	} else {
6153     	    printk ("Duh? Bad things happening in the NCR driver\n");
6154     	    break;
6155     	}
6156     
6157     	c->cmd->SCp.buffer = (struct scatterlist *) list;
6158     	list = c->cmd;
6159     	if (free) {
6160         	    c->next = hostdata->free;
6161         	    hostdata->free = c;
6162     	}
6163         }
6164     
6165         if (free) { 
6166     	for (i = 0, curr = (u32 *) hostdata->schedule; 
6167     	    i < host->can_queue; ++i, curr += 2) {
6168     	    curr[0] = hostdata->NOP_insn;
6169     	    curr[1] = le32_to_cpu(0xdeadbeef);
6170     	}
6171     	hostdata->curr = NULL;
6172         }
6173     
6174         if (issue) {
6175     	for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; tmp = tmp->next) {
6176     	    if (tmp->SCp.buffer) {
6177     		printk ("scsi%d : loop detected in issue queue!\n", 
6178     			host->host_no);
6179     		break;
6180     	    }
6181     	    tmp->SCp.buffer = (struct scatterlist *) list;
6182     	    list = tmp;
6183     	}
6184     	if (free)
6185     	    hostdata->issue_queue = NULL;
6186     		
6187         }
6188         return list;
6189     }
6190     
6191     /* 
6192      * Function : static int disable (struct Scsi_Host *host)
6193      *
6194      * Purpose : disables the given NCR host, causing all commands
6195      * 	to return a driver error.  Call this so we can unload the
6196      * 	module during development and try again.  Eventually, 
6197      * 	we should be able to find clean workarounds for these
6198      * 	problems.
6199      *
6200      * Inputs : host - hostadapter to twiddle
6201      *
6202      * Returns : 0 on success.
6203      */
6204     
6205     static int 
6206     disable (struct Scsi_Host *host) {
6207         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6208     	host->hostdata;
6209         unsigned long flags;
6210         Scsi_Cmnd *nuke_list, *tmp;
6211         save_flags(flags);
6212         cli();
6213         if (hostdata->state != STATE_HALTED)
6214     	ncr_halt (host);
6215         nuke_list = return_outstanding_commands (host, 1 /* free */, 1 /* issue */);
6216         hard_reset (host);
6217         hostdata->state = STATE_DISABLED;
6218         restore_flags(flags);
6219         printk ("scsi%d : nuking commands\n", host->host_no);
6220         for (; nuke_list; nuke_list = tmp) {
6221     	    tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
6222     	    nuke_list->result = DID_ERROR << 16;
6223     	    nuke_list->scsi_done(nuke_list);
6224         }
6225         printk ("scsi%d : done. \n", host->host_no);
6226         printk (KERN_ALERT "scsi%d : disabled.  Unload and reload\n",
6227         	host->host_no);
6228         return 0;
6229     }
6230     
6231     /*
6232      * Function : static int ncr_halt (struct Scsi_Host *host)
6233      * 
6234      * Purpose : halts the SCSI SCRIPTS(tm) processor on the NCR chip
6235      *
6236      * Inputs : host - SCSI chip to halt
6237      *
6238      * Returns : 0 on success
6239      */
6240     
6241     static int 
6242     ncr_halt (struct Scsi_Host *host) {
6243         NCR53c7x0_local_declare();
6244         unsigned long flags;
6245         unsigned char istat, tmp;
6246         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6247     	host->hostdata;
6248         int stage;
6249         NCR53c7x0_local_setup(host);
6250     
6251         save_flags(flags);
6252         cli();
6253         /* Stage 0 : eat all interrupts
6254            Stage 1 : set ABORT
6255            Stage 2 : eat all but abort interrupts
6256            Stage 3 : eat all interrupts
6257          */
6258         for (stage = 0;;) {
6259     	if (stage == 1) {
6260     	    NCR53c7x0_write8(hostdata->istat, ISTAT_ABRT);
6261     	    ++stage;
6262     	}
6263     	istat = NCR53c7x0_read8 (hostdata->istat);
6264     	if (istat & ISTAT_SIP) {
6265     	    if ((hostdata->chip / 100) == 8) {
6266     		tmp = NCR53c7x0_read8(SIST0_REG_800);
6267     		udelay(1);
6268     		tmp = NCR53c7x0_read8(SIST1_REG_800);
6269     	    } else {
6270     		tmp = NCR53c7x0_read8(SSTAT0_REG);
6271     	    }
6272     	} else if (istat & ISTAT_DIP) {
6273     	    tmp = NCR53c7x0_read8(DSTAT_REG);
6274     	    if (stage == 2) {
6275     		if (tmp & DSTAT_ABRT) {
6276     		    NCR53c7x0_write8(hostdata->istat, 0);
6277     		    ++stage;
6278     		} else {
6279     		    printk(KERN_ALERT "scsi%d : could not halt NCR chip\n", 
6280     			host->host_no);
6281     		    disable (host);
6282     	    	}
6283         	    }
6284     	}
6285     	if (!(istat & (ISTAT_SIP|ISTAT_DIP))) {
6286     	    if (stage == 0)
6287     	    	++stage;
6288     	    else if (stage == 3)
6289     		break;
6290     	}
6291         }
6292         hostdata->state = STATE_HALTED;
6293         restore_flags(flags);
6294     #if 0
6295         print_lots (host);
6296     #endif
6297         return 0;
6298     }
6299     
6300     /* 
6301      * Function: event_name (int event)
6302      * 
6303      * Purpose: map event enum into user-readable strings.
6304      */
6305     
6306     static const char *
6307     event_name (int event) {
6308         switch (event) {
6309         case EVENT_NONE:		return "none";
6310         case EVENT_ISSUE_QUEUE:	return "to issue queue";
6311         case EVENT_START_QUEUE:	return "to start queue";
6312         case EVENT_SELECT:		return "selected";
6313         case EVENT_DISCONNECT:	return "disconnected";
6314         case EVENT_RESELECT:	return "reselected";
6315         case EVENT_COMPLETE:	return "completed";
6316         case EVENT_IDLE:		return "idle";
6317         case EVENT_SELECT_FAILED:	return "select failed";
6318         case EVENT_BEFORE_SELECT:	return "before select";
6319         case EVENT_RESELECT_FAILED:	return "reselect failed";
6320         default:			return "unknown";
6321         }
6322     }
6323     
6324     /*
6325      * Function : void dump_events (struct Scsi_Host *host, count)
6326      *
6327      * Purpose : print last count events which have occurred.
6328      */ 
6329     static void
6330     dump_events (struct Scsi_Host *host, int count) {
6331         struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
6332     	host->hostdata;
6333         struct NCR53c7x0_event event;
6334         int i;
6335         unsigned long flags;
6336         if (hostdata->events) {
6337     	if (count > hostdata->event_size)
6338     	    count = hostdata->event_size;
6339     	for (i = hostdata->event_index; count > 0; 
6340     	    i = (i ? i - 1 : hostdata->event_size -1), --count) {
6341     	    save_flags(flags);
6342     /*
6343      * By copying the event we're currently examining with interrupts
6344      * disabled, we can do multiple printk(), etc. operations and 
6345      * still be guaranteed that they're happening on the same 
6346      * event structure.
6347      */
6348     	    cli();
6349     #if 0
6350     	    event = hostdata->events[i];
6351     #else
6352     	    memcpy ((void *) &event, (void *) &(hostdata->events[i]),
6353     		sizeof(event));
6354     #endif
6355     
6356     	    restore_flags(flags);
6357     	    printk ("scsi%d : %s event %d at %ld secs %ld usecs target %d lun %d\n",
6358     		host->host_no, event_name (event.event), count,
6359     		(long) event.time.tv_sec, (long) event.time.tv_usec,
6360     		event.target, event.lun);
6361     	    if (event.dsa) 
6362     		printk ("         event for dsa 0x%lx (virt 0x%p)\n", 
6363     		    virt_to_bus(event.dsa), event.dsa);
6364     	    if (event.pid != -1) {
6365     		printk ("         event for pid %ld ", event.pid);
6366     		print_command (event.cmnd);
6367     	    }
6368     	}
6369         }
6370     }
6371     
6372     /*
6373      * Function: check_address
6374      *
6375      * Purpose: Check to see if a possibly corrupt pointer will fault the 
6376      *	kernel.
6377      *
6378      * Inputs: addr - address; size - size of area
6379      *
6380      * Returns: 0 if area is OK, -1 on error.
6381      *
6382      * NOTES: should be implemented in terms of vverify on kernels 
6383      *	that have it.
6384      */
6385     
6386     static int 
6387     check_address (unsigned long addr, int size) {
6388         return (virt_to_phys((void *)addr) < PAGE_SIZE || virt_to_phys((void *)(addr + size)) > virt_to_phys(high_memory) ?  -1 : 0);
6389     }
6390     
6391     #ifdef MODULE
6392     int 
6393     NCR53c7x0_release(struct Scsi_Host *host) {
6394         struct NCR53c7x0_hostdata *hostdata = 
6395     	(struct NCR53c7x0_hostdata *) host->hostdata;
6396         struct NCR53c7x0_cmd *cmd, *tmp;
6397         shutdown (host);
6398         if (host->irq != IRQ_NONE)
6399     	{
6400     	    int irq_count;
6401     	    struct Scsi_Host *tmp;
6402     	    for (irq_count = 0, tmp = first_host; tmp; tmp = tmp->next)
6403     		if (tmp->hostt == the_template && tmp->irq == host->irq)
6404     		    ++irq_count;
6405     	    if (irq_count == 1)
6406     		free_irq(host->irq, NULL);
6407     	}
6408         if (host->dma_channel != DMA_NONE)
6409     	free_dma(host->dma_channel);
6410         if (host->io_port)
6411     	release_region(host->io_port, host->n_io_port);
6412         
6413         for (cmd = (struct NCR53c7x0_cmd *) hostdata->free; cmd; cmd = tmp, 
6414     	--hostdata->num_cmds) {
6415     	tmp = (struct NCR53c7x0_cmd *) cmd->next;
6416         /* 
6417          * If we're going to loop, try to stop it to get a more accurate
6418          * count of the leaked commands.
6419          */
6420     	cmd->next = NULL;
6421     	if (cmd->free)
6422     	    cmd->free ((void *) cmd->real, cmd->size);
6423         }
6424         if (hostdata->num_cmds)
6425     	printk ("scsi%d : leaked %d NCR53c7x0_cmd structures\n",
6426     	    host->host_no, hostdata->num_cmds);
6427         if (hostdata->events) 
6428     	vfree ((void *)hostdata->events);
6429         return 1;
6430     }
6431     #endif /* def MODULE */
6432     
6433     static Scsi_Host_Template driver_template = NCR53c7xx;
6434     #include "scsi_module.c"
6435