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