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