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