File: /usr/src/linux/drivers/scsi/ncr53c8xx.c

1     /******************************************************************************
2     **  Device driver for the PCI-SCSI NCR538XX controller family.
3     **
4     **  Copyright (C) 1994  Wolfgang Stanglmeier
5     **
6     **  This program is free software; you can redistribute it and/or modify
7     **  it under the terms of the GNU General Public License as published by
8     **  the Free Software Foundation; either version 2 of the License, or
9     **  (at your option) any later version.
10     **
11     **  This program is distributed in the hope that it will be useful,
12     **  but WITHOUT ANY WARRANTY; without even the implied warranty of
13     **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     **  GNU General Public License for more details.
15     **
16     **  You should have received a copy of the GNU General Public License
17     **  along with this program; if not, write to the Free Software
18     **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19     **
20     **-----------------------------------------------------------------------------
21     **
22     **  This driver has been ported to Linux from the FreeBSD NCR53C8XX driver
23     **  and is currently maintained by
24     **
25     **          Gerard Roudier              <groudier@club-internet.fr>
26     **
27     **  Being given that this driver originates from the FreeBSD version, and
28     **  in order to keep synergy on both, any suggested enhancements and corrections
29     **  received on Linux are automatically a potential candidate for the FreeBSD 
30     **  version.
31     **
32     **  The original driver has been written for 386bsd and FreeBSD by
33     **          Wolfgang Stanglmeier        <wolf@cologne.de>
34     **          Stefan Esser                <se@mi.Uni-Koeln.de>
35     **
36     **  And has been ported to NetBSD by
37     **          Charles M. Hannum           <mycroft@gnu.ai.mit.edu>
38     **
39     **-----------------------------------------------------------------------------
40     **
41     **                     Brief history
42     **
43     **  December 10 1995 by Gerard Roudier:
44     **     Initial port to Linux.
45     **
46     **  June 23 1996 by Gerard Roudier:
47     **     Support for 64 bits architectures (Alpha).
48     **
49     **  November 30 1996 by Gerard Roudier:
50     **     Support for Fast-20 scsi.
51     **     Support for large DMA fifo and 128 dwords bursting.
52     **
53     **  February 27 1997 by Gerard Roudier:
54     **     Support for Fast-40 scsi.
55     **     Support for on-Board RAM.
56     **
57     **  May 3 1997 by Gerard Roudier:
58     **     Full support for scsi scripts instructions pre-fetching.
59     **
60     **  May 19 1997 by Richard Waltham <dormouse@farsrobt.demon.co.uk>:
61     **     Support for NvRAM detection and reading.
62     **
63     **  August 18 1997 by Cort <cort@cs.nmt.edu>:
64     **     Support for Power/PC (Big Endian).
65     **
66     **  June 20 1998 by Gerard Roudier <groudier@club-internet.fr>:
67     **     Support for up to 64 tags per lun.
68     **     O(1) everywhere (C and SCRIPTS) for normal cases.
69     **     Low PCI traffic for command handling when on-chip RAM is present.
70     **     Aggressive SCSI SCRIPTS optimizations.
71     **
72     *******************************************************************************
73     */
74     
75     /*
76     **	Supported SCSI-II features:
77     **	    Synchronous negotiation
78     **	    Wide negotiation        (depends on the NCR Chip)
79     **	    Enable disconnection
80     **	    Tagged command queuing
81     **	    Parity checking
82     **	    Etc...
83     **
84     **	Supported NCR/SYMBIOS chips:
85     **		53C810		(8 bits, Fast SCSI-2, no rom BIOS) 
86     **		53C815		(8 bits, Fast SCSI-2, on board rom BIOS)
87     **		53C820		(Wide,   Fast SCSI-2, no rom BIOS)
88     **		53C825		(Wide,   Fast SCSI-2, on board rom BIOS)
89     **		53C860		(8 bits, Fast 20,     no rom BIOS)
90     **		53C875		(Wide,   Fast 20,     on board rom BIOS)
91     **		53C895		(Wide,   Fast 40,     on board rom BIOS)
92     **		53C895A		(Wide,   Fast 40,     on board rom BIOS)
93     **		53C896		(Wide,   Fast 40,     on board rom BIOS)
94     **		53C897		(Wide,   Fast 40,     on board rom BIOS)
95     **		53C1510D	(Wide,   Fast 40,     on board rom BIOS)
96     **
97     **	Other features:
98     **		Memory mapped IO (linux-1.3.X and above only)
99     **		Module
100     **		Shared IRQ (since linux-1.3.72)
101     */
102     
103     /*
104     **	Name and version of the driver
105     */
106     #define SCSI_NCR_DRIVER_NAME	"ncr53c8xx-3.4.3b-20010512"
107     
108     #define SCSI_NCR_DEBUG_FLAGS	(0)
109     
110     /*==========================================================
111     **
112     **      Include files
113     **
114     **==========================================================
115     */
116     
117     #define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
118     
119     #ifdef MODULE
120     #include <linux/module.h>
121     #endif
122     
123     #include <asm/dma.h>
124     #include <asm/io.h>
125     #include <asm/system.h>
126     #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,17)
127     #include <linux/spinlock.h>
128     #elif LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
129     #include <asm/spinlock.h>
130     #endif
131     #include <linux/delay.h>
132     #include <linux/signal.h>
133     #include <linux/sched.h>
134     #include <linux/errno.h>
135     #include <linux/pci.h>
136     #include <linux/string.h>
137     #include <linux/mm.h>
138     #include <linux/ioport.h>
139     #include <linux/time.h>
140     #include <linux/timer.h>
141     #include <linux/stat.h>
142     
143     #include <linux/version.h>
144     #include <linux/blk.h>
145     
146     #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,35)
147     #include <linux/init.h>
148     #endif
149     
150     #ifndef	__init
151     #define	__init
152     #endif
153     #ifndef	__initdata
154     #define	__initdata
155     #endif
156     
157     #if LINUX_VERSION_CODE <= LinuxVersionCode(2,1,92)
158     #include <linux/bios32.h>
159     #endif
160     
161     #include "scsi.h"
162     #include "hosts.h"
163     #include "constants.h"
164     #include "sd.h"
165     
166     #include <linux/types.h>
167     
168     /*
169     **	Define BITS_PER_LONG for earlier linux versions.
170     */
171     #ifndef	BITS_PER_LONG
172     #if (~0UL) == 0xffffffffUL
173     #define	BITS_PER_LONG	32
174     #else
175     #define	BITS_PER_LONG	64
176     #endif
177     #endif
178     
179     /*
180     **	Define the BSD style u_int32 and u_int64 type.
181     **	Are in fact u_int32_t and u_int64_t :-)
182     */
183     typedef u32 u_int32;
184     typedef u64 u_int64;
185     typedef	u_long		vm_offset_t;
186     #include "ncr53c8xx.h"
187     
188     /*
189     **	Donnot compile integrity checking code for Linux-2.3.0 
190     **	and above since SCSI data structures are not ready yet.
191     */
192     /* #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,0) */
193     #if 0
194     #define	SCSI_NCR_INTEGRITY_CHECKING
195     #endif
196     
197     #define NAME53C			"ncr53c"
198     #define NAME53C8XX		"ncr53c8xx"
199     #define DRIVER_SMP_LOCK		ncr53c8xx_lock
200     
201     #include "sym53c8xx_comm.h"
202     
203     /*==========================================================
204     **
205     **	The CCB done queue uses an array of CCB virtual 
206     **	addresses. Empty entries are flagged using the bogus 
207     **	virtual address 0xffffffff.
208     **
209     **	Since PCI ensures that only aligned DWORDs are accessed 
210     **	atomically, 64 bit little-endian architecture requires 
211     **	to test the high order DWORD of the entry to determine 
212     **	if it is empty or valid.
213     **
214     **	BTW, I will make things differently as soon as I will 
215     **	have a better idea, but this is simple and should work.
216     **
217     **==========================================================
218     */
219      
220     #define SCSI_NCR_CCB_DONE_SUPPORT
221     #ifdef  SCSI_NCR_CCB_DONE_SUPPORT
222     
223     #define MAX_DONE 24
224     #define CCB_DONE_EMPTY 0xffffffffUL
225     
226     /* All 32 bit architectures */
227     #if BITS_PER_LONG == 32
228     #define CCB_DONE_VALID(cp)  (((u_long) cp) != CCB_DONE_EMPTY)
229     
230     /* All > 32 bit (64 bit) architectures regardless endian-ness */
231     #else
232     #define CCB_DONE_VALID(cp)  \
233     	((((u_long) cp) & 0xffffffff00000000ul) && 	\
234     	 (((u_long) cp) & 0xfffffffful) != CCB_DONE_EMPTY)
235     #endif
236     
237     #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
238     
239     /*==========================================================
240     **
241     **	Configuration and Debugging
242     **
243     **==========================================================
244     */
245     
246     /*
247     **    SCSI address of this device.
248     **    The boot routines should have set it.
249     **    If not, use this.
250     */
251     
252     #ifndef SCSI_NCR_MYADDR
253     #define SCSI_NCR_MYADDR      (7)
254     #endif
255     
256     /*
257     **    The maximum number of tags per logic unit.
258     **    Used only for disk devices that support tags.
259     */
260     
261     #ifndef SCSI_NCR_MAX_TAGS
262     #define SCSI_NCR_MAX_TAGS    (8)
263     #endif
264     
265     /*
266     **    TAGS are actually limited to 64 tags/lun.
267     **    We need to deal with power of 2, for alignment constraints.
268     */
269     #if	SCSI_NCR_MAX_TAGS > 64
270     #define	MAX_TAGS (64)
271     #else
272     #define	MAX_TAGS SCSI_NCR_MAX_TAGS
273     #endif
274     
275     #define NO_TAG	(255)
276     
277     /*
278     **	Choose appropriate type for tag bitmap.
279     */
280     #if	MAX_TAGS > 32
281     typedef u_int64 tagmap_t;
282     #else
283     typedef u_int32 tagmap_t;
284     #endif
285     
286     /*
287     **    Number of targets supported by the driver.
288     **    n permits target numbers 0..n-1.
289     **    Default is 16, meaning targets #0..#15.
290     **    #7 .. is myself.
291     */
292     
293     #ifdef SCSI_NCR_MAX_TARGET
294     #define MAX_TARGET  (SCSI_NCR_MAX_TARGET)
295     #else
296     #define MAX_TARGET  (16)
297     #endif
298     
299     /*
300     **    Number of logic units supported by the driver.
301     **    n enables logic unit numbers 0..n-1.
302     **    The common SCSI devices require only
303     **    one lun, so take 1 as the default.
304     */
305     
306     #ifdef SCSI_NCR_MAX_LUN
307     #define MAX_LUN    SCSI_NCR_MAX_LUN
308     #else
309     #define MAX_LUN    (1)
310     #endif
311     
312     /*
313     **    Asynchronous pre-scaler (ns). Shall be 40
314     */
315      
316     #ifndef SCSI_NCR_MIN_ASYNC
317     #define SCSI_NCR_MIN_ASYNC (40)
318     #endif
319     
320     /*
321     **    The maximum number of jobs scheduled for starting.
322     **    There should be one slot per target, and one slot
323     **    for each tag of each target in use.
324     **    The calculation below is actually quite silly ...
325     */
326     
327     #ifdef SCSI_NCR_CAN_QUEUE
328     #define MAX_START   (SCSI_NCR_CAN_QUEUE + 4)
329     #else
330     #define MAX_START   (MAX_TARGET + 7 * MAX_TAGS)
331     #endif
332     
333     /*
334     **   We limit the max number of pending IO to 250.
335     **   since we donnot want to allocate more than 1 
336     **   PAGE for 'scripth'.
337     */
338     #if	MAX_START > 250
339     #undef	MAX_START
340     #define	MAX_START 250
341     #endif
342     
343     /*
344     **    The maximum number of segments a transfer is split into.
345     **    We support up to 127 segments for both read and write.
346     **    The data scripts are broken into 2 sub-scripts.
347     **    80 (MAX_SCATTERL) segments are moved from a sub-script 
348     **    in on-chip RAM. This makes data transfers shorter than 
349     **    80k (assuming 1k fs) as fast as possible.
350     */
351     
352     #define MAX_SCATTER (SCSI_NCR_MAX_SCATTER)
353     
354     #if (MAX_SCATTER > 80)
355     #define MAX_SCATTERL	80
356     #define	MAX_SCATTERH	(MAX_SCATTER - MAX_SCATTERL)
357     #else
358     #define MAX_SCATTERL	(MAX_SCATTER-1)
359     #define	MAX_SCATTERH	1
360     #endif
361     
362     /*
363     **	other
364     */
365     
366     #define NCR_SNOOP_TIMEOUT (1000000)
367     
368     /*
369     **	Head of list of NCR boards
370     **
371     **	For kernel version < 1.3.70, host is retrieved by its irq level.
372     **	For later kernels, the internal host control block address 
373     **	(struct ncb) is used as device id parameter of the irq stuff.
374     */
375     
376     static struct Scsi_Host		*first_host	= NULL;
377     static Scsi_Host_Template	*the_template	= NULL;	
378     
379     /*
380     **	Other definitions
381     */
382     
383     #define ScsiResult(host_code, scsi_code) (((host_code) << 16) + ((scsi_code) & 0x7f))
384     
385     static void ncr53c8xx_select_queue_depths(
386     	struct Scsi_Host *host, struct scsi_device *devlist);
387     static void ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs);
388     static void ncr53c8xx_timeout(unsigned long np);
389     
390     #define initverbose (driver_setup.verbose)
391     #define bootverbose (np->verbose)
392     
393     #ifdef SCSI_NCR_NVRAM_SUPPORT
394     static u_char Tekram_sync[16] __initdata =
395     	{25,31,37,43, 50,62,75,125, 12,15,18,21, 6,7,9,10};
396     #endif /* SCSI_NCR_NVRAM_SUPPORT */
397     
398     /*==========================================================
399     **
400     **	Command control block states.
401     **
402     **==========================================================
403     */
404     
405     #define HS_IDLE		(0)
406     #define HS_BUSY		(1)
407     #define HS_NEGOTIATE	(2)	/* sync/wide data transfer*/
408     #define HS_DISCONNECT	(3)	/* Disconnected by target */
409     
410     #define HS_DONEMASK	(0x80)
411     #define HS_COMPLETE	(4|HS_DONEMASK)
412     #define HS_SEL_TIMEOUT	(5|HS_DONEMASK)	/* Selection timeout      */
413     #define HS_RESET	(6|HS_DONEMASK)	/* SCSI reset	          */
414     #define HS_ABORTED	(7|HS_DONEMASK)	/* Transfer aborted       */
415     #define HS_TIMEOUT	(8|HS_DONEMASK)	/* Software timeout       */
416     #define HS_FAIL		(9|HS_DONEMASK)	/* SCSI or PCI bus errors */
417     #define HS_UNEXPECTED	(10|HS_DONEMASK)/* Unexpected disconnect  */
418     
419     /*
420     **	Invalid host status values used by the SCRIPTS processor 
421     **	when the nexus is not fully identified.
422     **	Shall never appear in a CCB.
423     */
424     
425     #define HS_INVALMASK	(0x40)
426     #define	HS_SELECTING	(0|HS_INVALMASK)
427     #define	HS_IN_RESELECT	(1|HS_INVALMASK)
428     #define	HS_STARTING	(2|HS_INVALMASK)
429     
430     /*
431     **	Flags set by the SCRIPT processor for commands 
432     **	that have been skipped.
433     */
434     #define HS_SKIPMASK	(0x20)
435     
436     /*==========================================================
437     **
438     **	Software Interrupt Codes
439     **
440     **==========================================================
441     */
442     
443     #define	SIR_BAD_STATUS		(1)
444     #define	SIR_XXXXXXXXXX		(2)
445     #define	SIR_NEGO_SYNC		(3)
446     #define	SIR_NEGO_WIDE		(4)
447     #define	SIR_NEGO_FAILED		(5)
448     #define	SIR_NEGO_PROTO		(6)
449     #define	SIR_REJECT_RECEIVED	(7)
450     #define	SIR_REJECT_SENT		(8)
451     #define	SIR_IGN_RESIDUE		(9)
452     #define	SIR_MISSING_SAVE	(10)
453     #define	SIR_RESEL_NO_MSG_IN	(11)
454     #define	SIR_RESEL_NO_IDENTIFY	(12)
455     #define	SIR_RESEL_BAD_LUN	(13)
456     #define	SIR_RESEL_BAD_TARGET	(14)
457     #define	SIR_RESEL_BAD_I_T_L	(15)
458     #define	SIR_RESEL_BAD_I_T_L_Q	(16)
459     #define	SIR_DONE_OVERFLOW	(17)
460     #define	SIR_MAX			(17)
461     
462     /*==========================================================
463     **
464     **	Extended error codes.
465     **	xerr_status field of struct ccb.
466     **
467     **==========================================================
468     */
469     
470     #define	XE_OK		(0)
471     #define	XE_EXTRA_DATA	(1)	/* unexpected data phase */
472     #define	XE_BAD_PHASE	(2)	/* illegal phase (4/5)   */
473     
474     /*==========================================================
475     **
476     **	Negotiation status.
477     **	nego_status field	of struct ccb.
478     **
479     **==========================================================
480     */
481     
482     #define NS_NOCHANGE	(0)
483     #define NS_SYNC		(1)
484     #define NS_WIDE		(2)
485     #define NS_PPR		(4)
486     
487     /*==========================================================
488     **
489     **	"Special features" of targets.
490     **	quirks field		of struct tcb.
491     **	actualquirks field	of struct ccb.
492     **
493     **==========================================================
494     */
495     
496     #define	QUIRK_AUTOSAVE	(0x01)
497     #define	QUIRK_NOMSG	(0x02)
498     #define QUIRK_NOSYNC	(0x10)
499     #define QUIRK_NOWIDE16	(0x20)
500     
501     /*==========================================================
502     **
503     **	Capability bits in Inquire response byte 7.
504     **
505     **==========================================================
506     */
507     
508     #define	INQ7_QUEUE	(0x02)
509     #define	INQ7_SYNC	(0x10)
510     #define	INQ7_WIDE16	(0x20)
511     
512     /*==========================================================
513     **
514     **	Misc.
515     **
516     **==========================================================
517     */
518     
519     #define CCB_MAGIC	(0xf2691ad2)
520     
521     /*==========================================================
522     **
523     **	Declaration of structs.
524     **
525     **==========================================================
526     */
527     
528     struct tcb;
529     struct lcb;
530     struct ccb;
531     struct ncb;
532     struct script;
533     
534     typedef struct ncb * ncb_p;
535     typedef struct tcb * tcb_p;
536     typedef struct lcb * lcb_p;
537     typedef struct ccb * ccb_p;
538     
539     struct link {
540     	ncrcmd	l_cmd;
541     	ncrcmd	l_paddr;
542     };
543     
544     struct	usrcmd {
545     	u_long	target;
546     	u_long	lun;
547     	u_long	data;
548     	u_long	cmd;
549     };
550     
551     #define UC_SETSYNC      10
552     #define UC_SETTAGS	11
553     #define UC_SETDEBUG	12
554     #define UC_SETORDER	13
555     #define UC_SETWIDE	14
556     #define UC_SETFLAG	15
557     #define UC_SETVERBOSE	17
558     
559     #define	UF_TRACE	(0x01)
560     #define	UF_NODISC	(0x02)
561     #define	UF_NOSCAN	(0x04)
562     
563     /*========================================================================
564     **
565     **	Declaration of structs:		target control block
566     **
567     **========================================================================
568     */
569     struct tcb {
570     	/*----------------------------------------------------------------
571     	**	During reselection the ncr jumps to this point with SFBR 
572     	**	set to the encoded target number with bit 7 set.
573     	**	if it's not this target, jump to the next.
574     	**
575     	**	JUMP  IF (SFBR != #target#), @(next tcb)
576     	**----------------------------------------------------------------
577     	*/
578     	struct link   jump_tcb;
579     
580     	/*----------------------------------------------------------------
581     	**	Load the actual values for the sxfer and the scntl3
582     	**	register (sync/wide mode).
583     	**
584     	**	SCR_COPY (1), @(sval field of this tcb), @(sxfer  register)
585     	**	SCR_COPY (1), @(wval field of this tcb), @(scntl3 register)
586     	**----------------------------------------------------------------
587     	*/
588     	ncrcmd	getscr[6];
589     
590     	/*----------------------------------------------------------------
591     	**	Get the IDENTIFY message and load the LUN to SFBR.
592     	**
593     	**	CALL, <RESEL_LUN>
594     	**----------------------------------------------------------------
595     	*/
596     	struct link   call_lun;
597     
598     	/*----------------------------------------------------------------
599     	**	Now look for the right lun.
600     	**
601     	**	For i = 0 to 3
602     	**		SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(first lcb mod. i)
603     	**
604     	**	Recent chips will prefetch the 4 JUMPS using only 1 burst.
605     	**	It is kind of hashcoding.
606     	**----------------------------------------------------------------
607     	*/
608     	struct link     jump_lcb[4];	/* JUMPs for reselection	*/
609     	lcb_p		lp[MAX_LUN];	/* The lcb's of this tcb	*/
610     	u_char		inq_done;	/* Target capabilities received	*/
611     	u_char		inq_byte7;	/* Contains these capabilities	*/
612     
613     	/*----------------------------------------------------------------
614     	**	Pointer to the ccb used for negotiation.
615     	**	Prevent from starting a negotiation for all queued commands 
616     	**	when tagged command queuing is enabled.
617     	**----------------------------------------------------------------
618     	*/
619     	ccb_p   nego_cp;
620     
621     	/*----------------------------------------------------------------
622     	**	statistical data
623     	**----------------------------------------------------------------
624     	*/
625     	u_long	transfers;
626     	u_long	bytes;
627     
628     	/*----------------------------------------------------------------
629     	**	negotiation of wide and synch transfer and device quirks.
630     	**----------------------------------------------------------------
631     	*/
632     /*0*/	u_char	minsync;
633     /*1*/	u_char	sval;
634     /*2*/	u_short	period;
635     /*0*/	u_char	maxoffs;
636     /*1*/	u_char	quirks;
637     /*2*/	u_char	widedone;
638     /*3*/	u_char	wval;
639     
640     #ifdef SCSI_NCR_INTEGRITY_CHECKING
641     	u_char 	ic_min_sync;
642     	u_char 	ic_max_width;
643     	u_char 	ic_maximums_set;
644     	u_char 	ic_done;
645     #endif
646     
647     	/*----------------------------------------------------------------
648     	**	User settable limits and options.
649     	**	These limits are read from the NVRAM if present.
650     	**----------------------------------------------------------------
651     	*/
652     	u_char	usrsync;
653     	u_char	usrwide;
654     	u_char	usrtags;
655     	u_char	usrflag;
656     };
657     
658     /*========================================================================
659     **
660     **	Declaration of structs:		lun control block
661     **
662     **========================================================================
663     */
664     struct lcb {
665     	/*----------------------------------------------------------------
666     	**	During reselection the ncr jumps to this point
667     	**	with SFBR set to the "Identify" message.
668     	**	if it's not this lun, jump to the next.
669     	**
670     	**	JUMP  IF (SFBR != #lun#), @(next lcb of this target)
671     	**
672     	**	It is this lun. Load TEMP with the nexus jumps table 
673     	**	address and jump to RESEL_TAG (or RESEL_NOTAG).
674     	**
675     	**		SCR_COPY (4), p_jump_ccb, TEMP,
676     	**		SCR_JUMP, <RESEL_TAG>
677     	**----------------------------------------------------------------
678     	*/
679     	struct link	jump_lcb;
680     	ncrcmd		load_jump_ccb[3];
681     	struct link	jump_tag;
682     	ncrcmd		p_jump_ccb;	/* Jump table bus address	*/
683     
684     	/*----------------------------------------------------------------
685     	**	Jump table used by the script processor to directly jump 
686     	**	to the CCB corresponding to the reselected nexus.
687     	**	Address is allocated on 256 bytes boundary in order to 
688     	**	allow 8 bit calculation of the tag jump entry for up to 
689     	**	64 possible tags.
690     	**----------------------------------------------------------------
691     	*/
692     	u_int32		jump_ccb_0;	/* Default table if no tags	*/
693     	u_int32		*jump_ccb;	/* Virtual address		*/
694     
695     	/*----------------------------------------------------------------
696     	**	CCB queue management.
697     	**----------------------------------------------------------------
698     	*/
699     	XPT_QUEHEAD	free_ccbq;	/* Queue of available CCBs	*/
700     	XPT_QUEHEAD	busy_ccbq;	/* Queue of busy CCBs		*/
701     	XPT_QUEHEAD	wait_ccbq;	/* Queue of waiting for IO CCBs	*/
702     	XPT_QUEHEAD	skip_ccbq;	/* Queue of skipped CCBs	*/
703     	u_char		actccbs;	/* Number of allocated CCBs	*/
704     	u_char		busyccbs;	/* CCBs busy for this lun	*/
705     	u_char		queuedccbs;	/* CCBs queued to the controller*/
706     	u_char		queuedepth;	/* Queue depth for this lun	*/
707     	u_char		scdev_depth;	/* SCSI device queue depth	*/
708     	u_char		maxnxs;		/* Max possible nexuses		*/
709     
710     	/*----------------------------------------------------------------
711     	**	Control of tagged command queuing.
712     	**	Tags allocation is performed using a circular buffer.
713     	**	This avoids using a loop for tag allocation.
714     	**----------------------------------------------------------------
715     	*/
716     	u_char		ia_tag;		/* Allocation index		*/
717     	u_char		if_tag;		/* Freeing index		*/
718     	u_char cb_tags[MAX_TAGS];	/* Circular tags buffer	*/
719     	u_char		usetags;	/* Command queuing is active	*/
720     	u_char		maxtags;	/* Max nr of tags asked by user	*/
721     	u_char		numtags;	/* Current number of tags	*/
722     	u_char		inq_byte7;	/* Store unit CmdQ capabitility	*/
723     
724     	/*----------------------------------------------------------------
725     	**	QUEUE FULL control and ORDERED tag control.
726     	**----------------------------------------------------------------
727     	*/
728     	/*----------------------------------------------------------------
729     	**	QUEUE FULL and ORDERED tag control.
730     	**----------------------------------------------------------------
731     	*/
732     	u_short		num_good;	/* Nr of GOOD since QUEUE FULL	*/
733     	tagmap_t	tags_umap;	/* Used tags bitmap		*/
734     	tagmap_t	tags_smap;	/* Tags in use at 'tag_stime'	*/
735     	u_long		tags_stime;	/* Last time we set smap=umap	*/
736     	ccb_p		held_ccb;	/* CCB held for QUEUE FULL	*/
737     };
738     
739     /*========================================================================
740     **
741     **      Declaration of structs:     the launch script.
742     **
743     **========================================================================
744     **
745     **	It is part of the CCB and is called by the scripts processor to 
746     **	start or restart the data structure (nexus).
747     **	This 6 DWORDs mini script makes use of prefetching.
748     **
749     **------------------------------------------------------------------------
750     */
751     struct launch {
752     	/*----------------------------------------------------------------
753     	**	SCR_COPY(4),	@(p_phys), @(dsa register)
754     	**	SCR_JUMP,	@(scheduler_point)
755     	**----------------------------------------------------------------
756     	*/
757     	ncrcmd		setup_dsa[3];	/* Copy 'phys' address to dsa	*/
758     	struct link	schedule;	/* Jump to scheduler point	*/
759     	ncrcmd		p_phys;		/* 'phys' header bus address	*/
760     };
761     
762     /*========================================================================
763     **
764     **      Declaration of structs:     global HEADER.
765     **
766     **========================================================================
767     **
768     **	This substructure is copied from the ccb to a global address after 
769     **	selection (or reselection) and copied back before disconnect.
770     **
771     **	These fields are accessible to the script processor.
772     **
773     **------------------------------------------------------------------------
774     */
775     
776     struct head {
777     	/*----------------------------------------------------------------
778     	**	Saved data pointer.
779     	**	Points to the position in the script responsible for the
780     	**	actual transfer transfer of data.
781     	**	It's written after reception of a SAVE_DATA_POINTER message.
782     	**	The goalpointer points after the last transfer command.
783     	**----------------------------------------------------------------
784     	*/
785     	u_int32		savep;
786     	u_int32		lastp;
787     	u_int32		goalp;
788     
789     	/*----------------------------------------------------------------
790     	**	Alternate data pointer.
791     	**	They are copied back to savep/lastp/goalp by the SCRIPTS 
792     	**	when the direction is unknown and the device claims data out.
793     	**----------------------------------------------------------------
794     	*/
795     	u_int32		wlastp;
796     	u_int32		wgoalp;
797     
798     	/*----------------------------------------------------------------
799     	**	The virtual address of the ccb containing this header.
800     	**----------------------------------------------------------------
801     	*/
802     	ccb_p	cp;
803     
804     	/*----------------------------------------------------------------
805     	**	Status fields.
806     	**----------------------------------------------------------------
807     	*/
808     	u_char		scr_st[4];	/* script status		*/
809     	u_char		status[4];	/* host status. must be the 	*/
810     					/*  last DWORD of the header.	*/
811     };
812     
813     /*
814     **	The status bytes are used by the host and the script processor.
815     **
816     **	The byte corresponding to the host_status must be stored in the 
817     **	last DWORD of the CCB header since it is used for command 
818     **	completion (ncr_wakeup()). Doing so, we are sure that the header 
819     **	has been entirely copied back to the CCB when the host_status is 
820     **	seen complete by the CPU.
821     **
822     **	The last four bytes (status[4]) are copied to the scratchb register
823     **	(declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
824     **	and copied back just after disconnecting.
825     **	Inside the script the XX_REG are used.
826     **
827     **	The first four bytes (scr_st[4]) are used inside the script by 
828     **	"COPY" commands.
829     **	Because source and destination must have the same alignment
830     **	in a DWORD, the fields HAVE to be at the choosen offsets.
831     **		xerr_st		0	(0x34)	scratcha
832     **		sync_st		1	(0x05)	sxfer
833     **		wide_st		3	(0x03)	scntl3
834     */
835     
836     /*
837     **	Last four bytes (script)
838     */
839     #define  QU_REG	scr0
840     #define  HS_REG	scr1
841     #define  HS_PRT	nc_scr1
842     #define  SS_REG	scr2
843     #define  SS_PRT	nc_scr2
844     #define  PS_REG	scr3
845     
846     /*
847     **	Last four bytes (host)
848     */
849     #define  actualquirks  phys.header.status[0]
850     #define  host_status   phys.header.status[1]
851     #define  scsi_status   phys.header.status[2]
852     #define  parity_status phys.header.status[3]
853     
854     /*
855     **	First four bytes (script)
856     */
857     #define  xerr_st       header.scr_st[0]
858     #define  sync_st       header.scr_st[1]
859     #define  nego_st       header.scr_st[2]
860     #define  wide_st       header.scr_st[3]
861     
862     /*
863     **	First four bytes (host)
864     */
865     #define  xerr_status   phys.xerr_st
866     #define  nego_status   phys.nego_st
867     
868     #if 0
869     #define  sync_status   phys.sync_st
870     #define  wide_status   phys.wide_st
871     #endif
872     
873     /*==========================================================
874     **
875     **      Declaration of structs:     Data structure block
876     **
877     **==========================================================
878     **
879     **	During execution of a ccb by the script processor,
880     **	the DSA (data structure address) register points
881     **	to this substructure of the ccb.
882     **	This substructure contains the header with
883     **	the script-processor-changable data and
884     **	data blocks for the indirect move commands.
885     **
886     **----------------------------------------------------------
887     */
888     
889     struct dsb {
890     
891     	/*
892     	**	Header.
893     	*/
894     
895     	struct head	header;
896     
897     	/*
898     	**	Table data for Script
899     	*/
900     
901     	struct scr_tblsel  select;
902     	struct scr_tblmove smsg  ;
903     	struct scr_tblmove cmd   ;
904     	struct scr_tblmove sense ;
905     	struct scr_tblmove data [MAX_SCATTER];
906     };
907     
908     
909     /*========================================================================
910     **
911     **      Declaration of structs:     Command control block.
912     **
913     **========================================================================
914     */
915     struct ccb {
916     	/*----------------------------------------------------------------
917     	**	This is the data structure which is pointed by the DSA 
918     	**	register when it is executed by the script processor.
919     	**	It must be the first entry because it contains the header 
920     	**	as first entry that must be cache line aligned.
921     	**----------------------------------------------------------------
922     	*/
923     	struct dsb	phys;
924     
925     	/*----------------------------------------------------------------
926     	**	Mini-script used at CCB execution start-up.
927     	**	Load the DSA with the data structure address (phys) and 
928     	**	jump to SELECT. Jump to CANCEL if CCB is to be canceled.
929     	**----------------------------------------------------------------
930     	*/
931     	struct launch	start;
932     
933     	/*----------------------------------------------------------------
934     	**	Mini-script used at CCB relection to restart the nexus.
935     	**	Load the DSA with the data structure address (phys) and 
936     	**	jump to RESEL_DSA. Jump to ABORT if CCB is to be aborted.
937     	**----------------------------------------------------------------
938     	*/
939     	struct launch	restart;
940     
941     	/*----------------------------------------------------------------
942     	**	If a data transfer phase is terminated too early
943     	**	(after reception of a message (i.e. DISCONNECT)),
944     	**	we have to prepare a mini script to transfer
945     	**	the rest of the data.
946     	**----------------------------------------------------------------
947     	*/
948     	ncrcmd		patch[8];
949     
950     	/*----------------------------------------------------------------
951     	**	The general SCSI driver provides a
952     	**	pointer to a control block.
953     	**----------------------------------------------------------------
954     	*/
955     	Scsi_Cmnd	*cmd;		/* SCSI command 		*/
956     	u_char		cdb_buf[16];	/* Copy of CDB			*/
957     	u_char		sense_buf[64];
958     	int		data_len;	/* Total data length		*/
959     
960     	/*----------------------------------------------------------------
961     	**	Message areas.
962     	**	We prepare a message to be sent after selection.
963     	**	We may use a second one if the command is rescheduled 
964     	**	due to GETCC or QFULL.
965     	**      Contents are IDENTIFY and SIMPLE_TAG.
966     	**	While negotiating sync or wide transfer,
967     	**	a SDTR or WDTR message is appended.
968     	**----------------------------------------------------------------
969     	*/
970     	u_char		scsi_smsg [8];
971     	u_char		scsi_smsg2[8];
972     
973     	/*----------------------------------------------------------------
974     	**	Other fields.
975     	**----------------------------------------------------------------
976     	*/
977     	u_long		p_ccb;		/* BUS address of this CCB	*/
978     	u_char		sensecmd[6];	/* Sense command		*/
979     	u_char		tag;		/* Tag for this transfer	*/
980     					/*  255 means no tag		*/
981     	u_char		target;
982     	u_char		lun;
983     	u_char		queued;
984     	u_char		auto_sense;
985     	ccb_p		link_ccb;	/* Host adapter CCB chain	*/
986     	XPT_QUEHEAD	link_ccbq;	/* Link to unit CCB queue	*/
987     	u_int32		startp;		/* Initial data pointer		*/
988     	u_long		magic;		/* Free / busy  CCB flag	*/
989     };
990     
991     #define CCB_PHYS(cp,lbl)	(cp->p_ccb + offsetof(struct ccb, lbl))
992     
993     
994     /*========================================================================
995     **
996     **      Declaration of structs:     NCR device descriptor
997     **
998     **========================================================================
999     */
1000     struct ncb {
1001     	/*----------------------------------------------------------------
1002     	**	The global header.
1003     	**	It is accessible to both the host and the script processor.
1004     	**	Must be cache line size aligned (32 for x86) in order to 
1005     	**	allow cache line bursting when it is copied to/from CCB.
1006     	**----------------------------------------------------------------
1007     	*/
1008     	struct head     header;
1009     
1010     	/*----------------------------------------------------------------
1011     	**	CCBs management queues.
1012     	**----------------------------------------------------------------
1013     	*/
1014     	Scsi_Cmnd	*waiting_list;	/* Commands waiting for a CCB	*/
1015     					/*  when lcb is not allocated.	*/
1016     	Scsi_Cmnd	*done_list;	/* Commands waiting for done()  */
1017     					/* callback to be invoked.      */ 
1018     #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
1019     	spinlock_t	smp_lock;	/* Lock for SMP threading       */
1020     #endif
1021     
1022     	/*----------------------------------------------------------------
1023     	**	Chip and controller indentification.
1024     	**----------------------------------------------------------------
1025     	*/
1026     	int		unit;		/* Unit number			*/
1027     	char		chip_name[8];	/* Chip name			*/
1028     	char		inst_name[16];	/* ncb instance name		*/
1029     
1030     	/*----------------------------------------------------------------
1031     	**	Initial value of some IO register bits.
1032     	**	These values are assumed to have been set by BIOS, and may 
1033     	**	be used for probing adapter implementation differences.
1034     	**----------------------------------------------------------------
1035     	*/
1036     	u_char	sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest3, sv_ctest4,
1037     		sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4;
1038     
1039     	/*----------------------------------------------------------------
1040     	**	Actual initial value of IO register bits used by the 
1041     	**	driver. They are loaded at initialisation according to  
1042     	**	features that are to be enabled.
1043     	**----------------------------------------------------------------
1044     	*/
1045     	u_char	rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest3, rv_ctest4, 
1046     		rv_ctest5, rv_stest2;
1047     
1048     	/*----------------------------------------------------------------
1049     	**	Targets management.
1050     	**	During reselection the ncr jumps to jump_tcb.
1051     	**	The SFBR register is loaded with the encoded target id.
1052     	**	For i = 0 to 3
1053     	**		SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(next tcb mod. i)
1054     	**
1055     	**	Recent chips will prefetch the 4 JUMPS using only 1 burst.
1056     	**	It is kind of hashcoding.
1057     	**----------------------------------------------------------------
1058     	*/
1059     	struct link     jump_tcb[4];	/* JUMPs for reselection	*/
1060     	struct tcb  target[MAX_TARGET];	/* Target data			*/
1061     
1062     	/*----------------------------------------------------------------
1063     	**	Virtual and physical bus addresses of the chip.
1064     	**----------------------------------------------------------------
1065     	*/
1066     	vm_offset_t	vaddr;		/* Virtual and bus address of	*/
1067     	vm_offset_t     paddr;		/*  chip's IO registers.	*/
1068     	vm_offset_t     paddr2;		/* On-chip RAM bus address.	*/
1069     	volatile			/* Pointer to volatile for 	*/
1070     	struct ncr_reg	*reg;		/*  memory mapped IO.		*/
1071     
1072     	/*----------------------------------------------------------------
1073     	**	SCRIPTS virtual and physical bus addresses.
1074     	**	'script'  is loaded in the on-chip RAM if present.
1075     	**	'scripth' stays in main memory.
1076     	**----------------------------------------------------------------
1077     	*/
1078     	struct script	*script0;	/* Copies of script and scripth	*/
1079     	struct scripth	*scripth0;	/*  relocated for this ncb.	*/
1080     	struct scripth	*scripth;	/* Actual scripth virt. address	*/
1081     	u_long		p_script;	/* Actual script and scripth	*/
1082     	u_long		p_scripth;	/*  bus addresses.		*/
1083     
1084     	/*----------------------------------------------------------------
1085     	**	General controller parameters and configuration.
1086     	**----------------------------------------------------------------
1087     	*/
1088     	pcidev_t	pdev;
1089     	u_short		device_id;	/* PCI device id		*/
1090     	u_char		revision_id;	/* PCI device revision id	*/
1091     	u_char		bus;		/* PCI BUS number		*/
1092     	u_char		device_fn;	/* PCI BUS device and function	*/
1093     	u_long		base_io;	/* IO space base address	*/
1094     	u_int		irq;		/* IRQ level			*/
1095     	u_int		features;	/* Chip features map		*/
1096     	u_char		myaddr;		/* SCSI id of the adapter	*/
1097     	u_char		maxburst;	/* log base 2 of dwords burst	*/
1098     	u_char		maxwide;	/* Maximum transfer width	*/
1099     	u_char		minsync;	/* Minimum sync period factor	*/
1100     	u_char		maxsync;	/* Maximum sync period factor	*/
1101     	u_char		maxoffs;	/* Max scsi offset		*/
1102     	u_char		multiplier;	/* Clock multiplier (1,2,4)	*/
1103     	u_char		clock_divn;	/* Number of clock divisors	*/
1104     	u_long		clock_khz;	/* SCSI clock frequency in KHz	*/
1105     
1106     	/*----------------------------------------------------------------
1107     	**	Start queue management.
1108     	**	It is filled up by the host processor and accessed by the 
1109     	**	SCRIPTS processor in order to start SCSI commands.
1110     	**----------------------------------------------------------------
1111     	*/
1112     	u_short		squeueput;	/* Next free slot of the queue	*/
1113     	u_short		actccbs;	/* Number of allocated CCBs	*/
1114     	u_short		queuedccbs;	/* Number of CCBs in start queue*/
1115     	u_short		queuedepth;	/* Start queue depth		*/
1116     
1117     	/*----------------------------------------------------------------
1118     	**	Timeout handler.
1119     	**----------------------------------------------------------------
1120     	*/
1121     	struct timer_list timer;	/* Timer handler link header	*/
1122     	u_long		lasttime;
1123     	u_long		settle_time;	/* Resetting the SCSI BUS	*/
1124     
1125     	/*----------------------------------------------------------------
1126     	**	Debugging and profiling.
1127     	**----------------------------------------------------------------
1128     	*/
1129     	struct ncr_reg	regdump;	/* Register dump		*/
1130     	u_long		regtime;	/* Time it has been done	*/
1131     
1132     	/*----------------------------------------------------------------
1133     	**	Miscellaneous buffers accessed by the scripts-processor.
1134     	**	They shall be DWORD aligned, because they may be read or 
1135     	**	written with a SCR_COPY script command.
1136     	**----------------------------------------------------------------
1137     	*/
1138     	u_char		msgout[8];	/* Buffer for MESSAGE OUT 	*/
1139     	u_char		msgin [8];	/* Buffer for MESSAGE IN	*/
1140     	u_int32		lastmsg;	/* Last SCSI message sent	*/
1141     	u_char		scratch;	/* Scratch for SCSI receive	*/
1142     
1143     	/*----------------------------------------------------------------
1144     	**	Miscellaneous configuration and status parameters.
1145     	**----------------------------------------------------------------
1146     	*/
1147     	u_char		disc;		/* Diconnection allowed		*/
1148     	u_char		scsi_mode;	/* Current SCSI BUS mode	*/
1149     	u_char		order;		/* Tag order to use		*/
1150     	u_char		verbose;	/* Verbosity for this controller*/
1151     	int		ncr_cache;	/* Used for cache test at init.	*/
1152     	u_long		p_ncb;		/* BUS address of this NCB	*/
1153     
1154     	/*----------------------------------------------------------------
1155     	**	Command completion handling.
1156     	**----------------------------------------------------------------
1157     	*/
1158     #ifdef SCSI_NCR_CCB_DONE_SUPPORT
1159     	struct ccb	*(ccb_done[MAX_DONE]);
1160     	int		ccb_done_ic;
1161     #endif
1162     	/*----------------------------------------------------------------
1163     	**	Fields that should be removed or changed.
1164     	**----------------------------------------------------------------
1165     	*/
1166     	struct ccb	*ccb;		/* Global CCB			*/
1167     	struct usrcmd	user;		/* Command from user		*/
1168     	u_char		release_stage;	/* Synchronisation stage on release  */
1169     
1170     #ifdef SCSI_NCR_INTEGRITY_CHECKING
1171     	/*----------------------------------------------------------------
1172     	**	Fields that are used for integrity check
1173     	**----------------------------------------------------------------
1174     	*/
1175     	unsigned char check_integrity; /* Enable midlayer integ.check on
1176     					* bus scan. */
1177     	unsigned char check_integ_par;  /* Set if par or Init. Det. error
1178     					 * used only during integ check */
1179     #endif
1180     };
1181     
1182     #define NCB_SCRIPT_PHYS(np,lbl)	 (np->p_script  + offsetof (struct script, lbl))
1183     #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
1184     
1185     /*==========================================================
1186     **
1187     **
1188     **      Script for NCR-Processor.
1189     **
1190     **	Use ncr_script_fill() to create the variable parts.
1191     **	Use ncr_script_copy_and_bind() to make a copy and
1192     **	bind to physical addresses.
1193     **
1194     **
1195     **==========================================================
1196     **
1197     **	We have to know the offsets of all labels before
1198     **	we reach them (for forward jumps).
1199     **	Therefore we declare a struct here.
1200     **	If you make changes inside the script,
1201     **	DONT FORGET TO CHANGE THE LENGTHS HERE!
1202     **
1203     **----------------------------------------------------------
1204     */
1205     
1206     /*
1207     **	Script fragments which are loaded into the on-chip RAM 
1208     **	of 825A, 875 and 895 chips.
1209     */
1210     struct script {
1211     	ncrcmd	start		[  5];
1212     	ncrcmd  startpos	[  1];
1213     	ncrcmd	select		[  6];
1214     	ncrcmd	select2		[  9];
1215     	ncrcmd	loadpos		[  4];
1216     	ncrcmd	send_ident	[  9];
1217     	ncrcmd	prepare		[  6];
1218     	ncrcmd	prepare2	[  7];
1219     	ncrcmd  command		[  6];
1220     	ncrcmd  dispatch	[ 32];
1221     	ncrcmd  clrack		[  4];
1222     	ncrcmd	no_data		[ 17];
1223     	ncrcmd  status		[  8];
1224     	ncrcmd  msg_in		[  2];
1225     	ncrcmd  msg_in2		[ 16];
1226     	ncrcmd  msg_bad		[  4];
1227     	ncrcmd	setmsg		[  7];
1228     	ncrcmd	cleanup		[  6];
1229     	ncrcmd  complete	[  9];
1230     	ncrcmd	cleanup_ok	[  8];
1231     	ncrcmd	cleanup0	[  1];
1232     #ifndef SCSI_NCR_CCB_DONE_SUPPORT
1233     	ncrcmd	signal		[ 12];
1234     #else
1235     	ncrcmd	signal		[  9];
1236     	ncrcmd	done_pos	[  1];
1237     	ncrcmd	done_plug	[  2];
1238     	ncrcmd	done_end	[  7];
1239     #endif
1240     	ncrcmd  save_dp		[  7];
1241     	ncrcmd  restore_dp	[  5];
1242     	ncrcmd  disconnect	[ 17];
1243     	ncrcmd	msg_out		[  9];
1244     	ncrcmd	msg_out_done	[  7];
1245     	ncrcmd  idle		[  2];
1246     	ncrcmd	reselect	[  8];
1247     	ncrcmd	reselected	[  8];
1248     	ncrcmd	resel_dsa	[  6];
1249     	ncrcmd	loadpos1	[  4];
1250     	ncrcmd  resel_lun	[  6];
1251     	ncrcmd	resel_tag	[  6];
1252     	ncrcmd	jump_to_nexus	[  4];
1253     	ncrcmd	nexus_indirect	[  4];
1254     	ncrcmd	resel_notag	[  4];
1255     	ncrcmd  data_in		[MAX_SCATTERL * 4];
1256     	ncrcmd  data_in2	[  4];
1257     	ncrcmd  data_out	[MAX_SCATTERL * 4];
1258     	ncrcmd  data_out2	[  4];
1259     };
1260     
1261     /*
1262     **	Script fragments which stay in main memory for all chips.
1263     */
1264     struct scripth {
1265     	ncrcmd  tryloop		[MAX_START*2];
1266     	ncrcmd  tryloop2	[  2];
1267     #ifdef SCSI_NCR_CCB_DONE_SUPPORT
1268     	ncrcmd  done_queue	[MAX_DONE*5];
1269     	ncrcmd  done_queue2	[  2];
1270     #endif
1271     	ncrcmd	select_no_atn	[  8];
1272     	ncrcmd	cancel		[  4];
1273     	ncrcmd	skip		[  9];
1274     	ncrcmd	skip2		[ 19];
1275     	ncrcmd	par_err_data_in	[  6];
1276     	ncrcmd	par_err_other	[  4];
1277     	ncrcmd	msg_reject	[  8];
1278     	ncrcmd	msg_ign_residue	[ 24];
1279     	ncrcmd  msg_extended	[ 10];
1280     	ncrcmd  msg_ext_2	[ 10];
1281     	ncrcmd	msg_wdtr	[ 14];
1282     	ncrcmd	send_wdtr	[  7];
1283     	ncrcmd  msg_ext_3	[ 10];
1284     	ncrcmd	msg_sdtr	[ 14];
1285     	ncrcmd	send_sdtr	[  7];
1286     	ncrcmd	nego_bad_phase	[  4];
1287     	ncrcmd	msg_out_abort	[ 10];
1288     	ncrcmd  hdata_in	[MAX_SCATTERH * 4];
1289     	ncrcmd  hdata_in2	[  2];
1290     	ncrcmd  hdata_out	[MAX_SCATTERH * 4];
1291     	ncrcmd  hdata_out2	[  2];
1292     	ncrcmd	reset		[  4];
1293     	ncrcmd	aborttag	[  4];
1294     	ncrcmd	abort		[  2];
1295     	ncrcmd	abort_resel	[ 20];
1296     	ncrcmd	resend_ident	[  4];
1297     	ncrcmd	clratn_go_on	[  3];
1298     	ncrcmd	nxtdsp_go_on	[  1];
1299     	ncrcmd	sdata_in	[  8];
1300     	ncrcmd  data_io		[ 18];
1301     	ncrcmd	bad_identify	[ 12];
1302     	ncrcmd	bad_i_t_l	[  4];
1303     	ncrcmd	bad_i_t_l_q	[  4];
1304     	ncrcmd	bad_target	[  8];
1305     	ncrcmd	bad_status	[  8];
1306     	ncrcmd	start_ram	[  4];
1307     	ncrcmd	start_ram0	[  4];
1308     	ncrcmd	sto_restart	[  5];
1309     	ncrcmd	snooptest	[  9];
1310     	ncrcmd	snoopend	[  2];
1311     };
1312     
1313     /*==========================================================
1314     **
1315     **
1316     **      Function headers.
1317     **
1318     **
1319     **==========================================================
1320     */
1321     
1322     static	void	ncr_alloc_ccb	(ncb_p np, u_char tn, u_char ln);
1323     static	void	ncr_complete	(ncb_p np, ccb_p cp);
1324     static	void	ncr_exception	(ncb_p np);
1325     static	void	ncr_free_ccb	(ncb_p np, ccb_p cp);
1326     static	void	ncr_init_ccb	(ncb_p np, ccb_p cp);
1327     static	void	ncr_init_tcb	(ncb_p np, u_char tn);
1328     static	lcb_p	ncr_alloc_lcb	(ncb_p np, u_char tn, u_char ln);
1329     static	lcb_p	ncr_setup_lcb	(ncb_p np, u_char tn, u_char ln,
1330     				 u_char *inq_data);
1331     static	void	ncr_getclock	(ncb_p np, int mult);
1332     static	void	ncr_selectclock	(ncb_p np, u_char scntl3);
1333     static	ccb_p	ncr_get_ccb	(ncb_p np, u_char tn, u_char ln);
1334     static	void	ncr_init	(ncb_p np, int reset, char * msg, u_long code);
1335     static	int	ncr_int_sbmc	(ncb_p np);
1336     static	int	ncr_int_par	(ncb_p np);
1337     static	void	ncr_int_ma	(ncb_p np);
1338     static	void	ncr_int_sir	(ncb_p np);
1339     static  void    ncr_int_sto     (ncb_p np);
1340     static	u_long	ncr_lookup	(char* id);
1341     static	void	ncr_negotiate	(struct ncb* np, struct tcb* tp);
1342     static	int	ncr_prepare_nego(ncb_p np, ccb_p cp, u_char *msgptr);
1343     #ifdef SCSI_NCR_INTEGRITY_CHECKING
1344     static	int	ncr_ic_nego(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd, u_char *msgptr);
1345     #endif
1346     
1347     static	void	ncr_script_copy_and_bind
1348     				(ncb_p np, ncrcmd *src, ncrcmd *dst, int len);
1349     static  void    ncr_script_fill (struct script * scr, struct scripth * scripth);
1350     static	int	ncr_scatter	(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd);
1351     static	void	ncr_getsync	(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p);
1352     static	void	ncr_setsync	(ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer);
1353     static	void	ncr_setup_tags	(ncb_p np, u_char tn, u_char ln);
1354     static	void	ncr_setwide	(ncb_p np, ccb_p cp, u_char wide, u_char ack);
1355     static	int	ncr_show_msg	(u_char * msg);
1356     static  void    ncr_print_msg   (ccb_p cp, char *label, u_char *msg);
1357     static	int	ncr_snooptest	(ncb_p np);
1358     static	void	ncr_timeout	(ncb_p np);
1359     static  void    ncr_wakeup      (ncb_p np, u_long code);
1360     static  void    ncr_wakeup_done (ncb_p np);
1361     static	void	ncr_start_next_ccb (ncb_p np, lcb_p lp, int maxn);
1362     static	void	ncr_put_start_queue(ncb_p np, ccb_p cp);
1363     static	void	ncr_start_reset	(ncb_p np);
1364     static	int	ncr_reset_scsi_bus (ncb_p np, int enab_int, int settle_delay);
1365     
1366     #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
1367     static	void	ncr_usercmd	(ncb_p np);
1368     #endif
1369     
1370     static int ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device);
1371     
1372     static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd);
1373     static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd);
1374     static void process_waiting_list(ncb_p np, int sts);
1375     
1376     #define remove_from_waiting_list(np, cmd) \
1377     		retrieve_from_waiting_list(1, (np), (cmd))
1378     #define requeue_waiting_list(np) process_waiting_list((np), DID_OK)
1379     #define reset_waiting_list(np) process_waiting_list((np), DID_RESET)
1380     
1381     static inline char *ncr_name (ncb_p np)
1382     {
1383     	return np->inst_name;
1384     }
1385     
1386     
1387     /*==========================================================
1388     **
1389     **
1390     **      Scripts for NCR-Processor.
1391     **
1392     **      Use ncr_script_bind for binding to physical addresses.
1393     **
1394     **
1395     **==========================================================
1396     **
1397     **	NADDR generates a reference to a field of the controller data.
1398     **	PADDR generates a reference to another part of the script.
1399     **	RADDR generates a reference to a script processor register.
1400     **	FADDR generates a reference to a script processor register
1401     **		with offset.
1402     **
1403     **----------------------------------------------------------
1404     */
1405     
1406     #define	RELOC_SOFTC	0x40000000
1407     #define	RELOC_LABEL	0x50000000
1408     #define	RELOC_REGISTER	0x60000000
1409     #if 0
1410     #define	RELOC_KVAR	0x70000000
1411     #endif
1412     #define	RELOC_LABELH	0x80000000
1413     #define	RELOC_MASK	0xf0000000
1414     
1415     #define	NADDR(label)	(RELOC_SOFTC | offsetof(struct ncb, label))
1416     #define PADDR(label)    (RELOC_LABEL | offsetof(struct script, label))
1417     #define PADDRH(label)   (RELOC_LABELH | offsetof(struct scripth, label))
1418     #define	RADDR(label)	(RELOC_REGISTER | REG(label))
1419     #define	FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
1420     #if 0
1421     #define	KVAR(which)	(RELOC_KVAR | (which))
1422     #endif
1423     
1424     #if 0
1425     #define	SCRIPT_KVAR_JIFFIES	(0)
1426     #define	SCRIPT_KVAR_FIRST		SCRIPT_KVAR_JIFFIES
1427     #define	SCRIPT_KVAR_LAST		SCRIPT_KVAR_JIFFIES
1428     /*
1429      * Kernel variables referenced in the scripts.
1430      * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
1431      */
1432     static void *script_kvars[] __initdata =
1433     	{ (void *)&jiffies };
1434     #endif
1435     
1436     static	struct script script0 __initdata = {
1437     /*--------------------------< START >-----------------------*/ {
1438     	/*
1439     	**	This NOP will be patched with LED ON
1440     	**	SCR_REG_REG (gpreg, SCR_AND, 0xfe)
1441     	*/
1442     	SCR_NO_OP,
1443     		0,
1444     	/*
1445     	**      Clear SIGP.
1446     	*/
1447     	SCR_FROM_REG (ctest2),
1448     		0,
1449     	/*
1450     	**	Then jump to a certain point in tryloop.
1451     	**	Due to the lack of indirect addressing the code
1452     	**	is self modifying here.
1453     	*/
1454     	SCR_JUMP,
1455     }/*-------------------------< STARTPOS >--------------------*/,{
1456     		PADDRH(tryloop),
1457     
1458     }/*-------------------------< SELECT >----------------------*/,{
1459     	/*
1460     	**	DSA	contains the address of a scheduled
1461     	**		data structure.
1462     	**
1463     	**	SCRATCHA contains the address of the script,
1464     	**		which starts the next entry.
1465     	**
1466     	**	Set Initiator mode.
1467     	**
1468     	**	(Target mode is left as an exercise for the reader)
1469     	*/
1470     
1471     	SCR_CLR (SCR_TRG),
1472     		0,
1473     	SCR_LOAD_REG (HS_REG, HS_SELECTING),
1474     		0,
1475     
1476     	/*
1477     	**      And try to select this target.
1478     	*/
1479     	SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
1480     		PADDR (reselect),
1481     
1482     }/*-------------------------< SELECT2 >----------------------*/,{
1483     	/*
1484     	**	Now there are 4 possibilities:
1485     	**
1486     	**	(1) The ncr looses arbitration.
1487     	**	This is ok, because it will try again,
1488     	**	when the bus becomes idle.
1489     	**	(But beware of the timeout function!)
1490     	**
1491     	**	(2) The ncr is reselected.
1492     	**	Then the script processor takes the jump
1493     	**	to the RESELECT label.
1494     	**
1495     	**	(3) The ncr wins arbitration.
1496     	**	Then it will execute SCRIPTS instruction until 
1497     	**	the next instruction that checks SCSI phase.
1498     	**	Then will stop and wait for selection to be 
1499     	**	complete or selection time-out to occur.
1500     	**	As a result the SCRIPTS instructions until 
1501     	**	LOADPOS + 2 should be executed in parallel with 
1502     	**	the SCSI core performing selection.
1503     	*/
1504     
1505     	/*
1506     	**	The M_REJECT problem seems to be due to a selection 
1507     	**	timing problem.
1508     	**	Wait immediately for the selection to complete. 
1509     	**	(2.5x behaves so)
1510     	*/
1511     	SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
1512     		0,
1513     
1514     	/*
1515     	**	Next time use the next slot.
1516     	*/
1517     	SCR_COPY (4),
1518     		RADDR (temp),
1519     		PADDR (startpos),
1520     	/*
1521     	**      The ncr doesn't have an indirect load
1522     	**	or store command. So we have to
1523     	**	copy part of the control block to a
1524     	**	fixed place, where we can access it.
1525     	**
1526     	**	We patch the address part of a
1527     	**	COPY command with the DSA-register.
1528     	*/
1529     	SCR_COPY_F (4),
1530     		RADDR (dsa),
1531     		PADDR (loadpos),
1532     	/*
1533     	**	then we do the actual copy.
1534     	*/
1535     	SCR_COPY (sizeof (struct head)),
1536     	/*
1537     	**	continued after the next label ...
1538     	*/
1539     }/*-------------------------< LOADPOS >---------------------*/,{
1540     		0,
1541     		NADDR (header),
1542     	/*
1543     	**	Wait for the next phase or the selection
1544     	**	to complete or time-out.
1545     	*/
1546     	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
1547     		PADDR (prepare),
1548     
1549     }/*-------------------------< SEND_IDENT >----------------------*/,{
1550     	/*
1551     	**	Selection complete.
1552     	**	Send the IDENTIFY and SIMPLE_TAG messages
1553     	**	(and the M_X_SYNC_REQ message)
1554     	*/
1555     	SCR_MOVE_TBL ^ SCR_MSG_OUT,
1556     		offsetof (struct dsb, smsg),
1557     	SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
1558     		PADDRH (resend_ident),
1559     	SCR_LOAD_REG (scratcha, 0x80),
1560     		0,
1561     	SCR_COPY (1),
1562     		RADDR (scratcha),
1563     		NADDR (lastmsg),
1564     }/*-------------------------< PREPARE >----------------------*/,{
1565     	/*
1566     	**      load the savep (saved pointer) into
1567     	**      the TEMP register (actual pointer)
1568     	*/
1569     	SCR_COPY (4),
1570     		NADDR (header.savep),
1571     		RADDR (temp),
1572     	/*
1573     	**      Initialize the status registers
1574     	*/
1575     	SCR_COPY (4),
1576     		NADDR (header.status),
1577     		RADDR (scr0),
1578     }/*-------------------------< PREPARE2 >---------------------*/,{
1579     	/*
1580     	**	Initialize the msgout buffer with a NOOP message.
1581     	*/
1582     	SCR_LOAD_REG (scratcha, M_NOOP),
1583     		0,
1584     	SCR_COPY (1),
1585     		RADDR (scratcha),
1586     		NADDR (msgout),
1587     #if 0
1588     	SCR_COPY (1),
1589     		RADDR (scratcha),
1590     		NADDR (msgin),
1591     #endif
1592     	/*
1593     	**	Anticipate the COMMAND phase.
1594     	**	This is the normal case for initial selection.
1595     	*/
1596     	SCR_JUMP ^ IFFALSE (WHEN (SCR_COMMAND)),
1597     		PADDR (dispatch),
1598     
1599     }/*-------------------------< COMMAND >--------------------*/,{
1600     	/*
1601     	**	... and send the command
1602     	*/
1603     	SCR_MOVE_TBL ^ SCR_COMMAND,
1604     		offsetof (struct dsb, cmd),
1605     	/*
1606     	**	If status is still HS_NEGOTIATE, negotiation failed.
1607     	**	We check this here, since we want to do that 
1608     	**	only once.
1609     	*/
1610     	SCR_FROM_REG (HS_REG),
1611     		0,
1612     	SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
1613     		SIR_NEGO_FAILED,
1614     
1615     }/*-----------------------< DISPATCH >----------------------*/,{
1616     	/*
1617     	**	MSG_IN is the only phase that shall be 
1618     	**	entered at least once for each (re)selection.
1619     	**	So we test it first.
1620     	*/
1621     	SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
1622     		PADDR (msg_in),
1623     
1624     	SCR_RETURN ^ IFTRUE (IF (SCR_DATA_OUT)),
1625     		0,
1626     	/*
1627     	**	DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 4.
1628     	**	Possible data corruption during Memory Write and Invalidate.
1629     	**	This work-around resets the addressing logic prior to the 
1630     	**	start of the first MOVE of a DATA IN phase.
1631     	**	(See README.ncr53c8xx for more information)
1632     	*/
1633     	SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
1634     		20,
1635     	SCR_COPY (4),
1636     		RADDR (scratcha),
1637     		RADDR (scratcha),
1638     	SCR_RETURN,
1639      		0,
1640     	SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
1641     		PADDR (status),
1642     	SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
1643     		PADDR (command),
1644     	SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
1645     		PADDR (msg_out),
1646     	/*
1647     	**      Discard one illegal phase byte, if required.
1648     	*/
1649     	SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
1650     		0,
1651     	SCR_COPY (1),
1652     		RADDR (scratcha),
1653     		NADDR (xerr_st),
1654     	SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
1655     		8,
1656     	SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
1657     		NADDR (scratch),
1658     	SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
1659     		8,
1660     	SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
1661     		NADDR (scratch),
1662     	SCR_JUMP,
1663     		PADDR (dispatch),
1664     
1665     }/*-------------------------< CLRACK >----------------------*/,{
1666     	/*
1667     	**	Terminate possible pending message phase.
1668     	*/
1669     	SCR_CLR (SCR_ACK),
1670     		0,
1671     	SCR_JUMP,
1672     		PADDR (dispatch),
1673     
1674     }/*-------------------------< NO_DATA >--------------------*/,{
1675     	/*
1676     	**	The target wants to tranfer too much data
1677     	**	or in the wrong direction.
1678     	**      Remember that in extended error.
1679     	*/
1680     	SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
1681     		0,
1682     	SCR_COPY (1),
1683     		RADDR (scratcha),
1684     		NADDR (xerr_st),
1685     	/*
1686     	**      Discard one data byte, if required.
1687     	*/
1688     	SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
1689     		8,
1690     	SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
1691     		NADDR (scratch),
1692     	SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
1693     		8,
1694     	SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
1695     		NADDR (scratch),
1696     	/*
1697     	**      .. and repeat as required.
1698     	*/
1699     	SCR_CALL,
1700     		PADDR (dispatch),
1701     	SCR_JUMP,
1702     		PADDR (no_data),
1703     
1704     }/*-------------------------< STATUS >--------------------*/,{
1705     	/*
1706     	**	get the status
1707     	*/
1708     	SCR_MOVE_ABS (1) ^ SCR_STATUS,
1709     		NADDR (scratch),
1710     	/*
1711     	**	save status to scsi_status.
1712     	**	mark as complete.
1713     	*/
1714     	SCR_TO_REG (SS_REG),
1715     		0,
1716     	SCR_LOAD_REG (HS_REG, HS_COMPLETE),
1717     		0,
1718     	SCR_JUMP,
1719     		PADDR (dispatch),
1720     }/*-------------------------< MSG_IN >--------------------*/,{
1721     	/*
1722     	**	Get the first byte of the message
1723     	**	and save it to SCRATCHA.
1724     	**
1725     	**	The script processor doesn't negate the
1726     	**	ACK signal after this transfer.
1727     	*/
1728     	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
1729     		NADDR (msgin[0]),
1730     }/*-------------------------< MSG_IN2 >--------------------*/,{
1731     	/*
1732     	**	Handle this message.
1733     	*/
1734     	SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)),
1735     		PADDR (complete),
1736     	SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)),
1737     		PADDR (disconnect),
1738     	SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)),
1739     		PADDR (save_dp),
1740     	SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)),
1741     		PADDR (restore_dp),
1742     	SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
1743     		PADDRH (msg_extended),
1744     	SCR_JUMP ^ IFTRUE (DATA (M_NOOP)),
1745     		PADDR (clrack),
1746     	SCR_JUMP ^ IFTRUE (DATA (M_REJECT)),
1747     		PADDRH (msg_reject),
1748     	SCR_JUMP ^ IFTRUE (DATA (M_IGN_RESIDUE)),
1749     		PADDRH (msg_ign_residue),
1750     	/*
1751     	**	Rest of the messages left as
1752     	**	an exercise ...
1753     	**
1754     	**	Unimplemented messages:
1755     	**	fall through to MSG_BAD.
1756     	*/
1757     }/*-------------------------< MSG_BAD >------------------*/,{
1758     	/*
1759     	**	unimplemented message - reject it.
1760     	*/
1761     	SCR_INT,
1762     		SIR_REJECT_SENT,
1763     	SCR_LOAD_REG (scratcha, M_REJECT),
1764     		0,
1765     }/*-------------------------< SETMSG >----------------------*/,{
1766     	SCR_COPY (1),
1767     		RADDR (scratcha),
1768     		NADDR (msgout),
1769     	SCR_SET (SCR_ATN),
1770     		0,
1771     	SCR_JUMP,
1772     		PADDR (clrack),
1773     }/*-------------------------< CLEANUP >-------------------*/,{
1774     	/*
1775     	**      dsa:    Pointer to ccb
1776     	**	      or xxxxxxFF (no ccb)
1777     	**
1778     	**      HS_REG:   Host-Status (<>0!)
1779     	*/
1780     	SCR_FROM_REG (dsa),
1781     		0,
1782     	SCR_JUMP ^ IFTRUE (DATA (0xff)),
1783     		PADDR (start),
1784     	/*
1785     	**      dsa is valid.
1786     	**	complete the cleanup.
1787     	*/
1788     	SCR_JUMP,
1789     		PADDR (cleanup_ok),
1790     
1791     }/*-------------------------< COMPLETE >-----------------*/,{
1792     	/*
1793     	**	Complete message.
1794     	**
1795     	**	Copy TEMP register to LASTP in header.
1796     	*/
1797     	SCR_COPY (4),
1798     		RADDR (temp),
1799     		NADDR (header.lastp),
1800     	/*
1801     	**	When we terminate the cycle by clearing ACK,
1802     	**	the target may disconnect immediately.
1803     	**
1804     	**	We don't want to be told of an
1805     	**	"unexpected disconnect",
1806     	**	so we disable this feature.
1807     	*/
1808     	SCR_REG_REG (scntl2, SCR_AND, 0x7f),
1809     		0,
1810     	/*
1811     	**	Terminate cycle ...
1812     	*/
1813     	SCR_CLR (SCR_ACK|SCR_ATN),
1814     		0,
1815     	/*
1816     	**	... and wait for the disconnect.
1817     	*/
1818     	SCR_WAIT_DISC,
1819     		0,
1820     }/*-------------------------< CLEANUP_OK >----------------*/,{
1821     	/*
1822     	**	Save host status to header.
1823     	*/
1824     	SCR_COPY (4),
1825     		RADDR (scr0),
1826     		NADDR (header.status),
1827     	/*
1828     	**	and copy back the header to the ccb.
1829     	*/
1830     	SCR_COPY_F (4),
1831     		RADDR (dsa),
1832     		PADDR (cleanup0),
1833     	SCR_COPY (sizeof (struct head)),
1834     		NADDR (header),
1835     }/*-------------------------< CLEANUP0 >--------------------*/,{
1836     		0,
1837     }/*-------------------------< SIGNAL >----------------------*/,{
1838     	/*
1839     	**	if job not completed ...
1840     	*/
1841     	SCR_FROM_REG (HS_REG),
1842     		0,
1843     	/*
1844     	**	... start the next command.
1845     	*/
1846     	SCR_JUMP ^ IFTRUE (MASK (0, (HS_DONEMASK|HS_SKIPMASK))),
1847     		PADDR(start),
1848     	/*
1849     	**	If command resulted in not GOOD status,
1850     	**	call the C code if needed.
1851     	*/
1852     	SCR_FROM_REG (SS_REG),
1853     		0,
1854     	SCR_CALL ^ IFFALSE (DATA (S_GOOD)),
1855     		PADDRH (bad_status),
1856     
1857     #ifndef	SCSI_NCR_CCB_DONE_SUPPORT
1858     
1859     	/*
1860     	**	... signal completion to the host
1861     	*/
1862     	SCR_INT_FLY,
1863     		0,
1864     	/*
1865     	**	Auf zu neuen Schandtaten!
1866     	*/
1867     	SCR_JUMP,
1868     		PADDR(start),
1869     
1870     #else	/* defined SCSI_NCR_CCB_DONE_SUPPORT */
1871     
1872     	/*
1873     	**	... signal completion to the host
1874     	*/
1875     	SCR_JUMP,
1876     }/*------------------------< DONE_POS >---------------------*/,{
1877     		PADDRH (done_queue),
1878     }/*------------------------< DONE_PLUG >--------------------*/,{
1879     	SCR_INT,
1880     		SIR_DONE_OVERFLOW,
1881     }/*------------------------< DONE_END >---------------------*/,{
1882     	SCR_INT_FLY,
1883     		0,
1884     	SCR_COPY (4),
1885     		RADDR (temp),
1886     		PADDR (done_pos),
1887     	SCR_JUMP,
1888     		PADDR (start),
1889     
1890     #endif	/* SCSI_NCR_CCB_DONE_SUPPORT */
1891     
1892     }/*-------------------------< SAVE_DP >------------------*/,{
1893     	/*
1894     	**	SAVE_DP message:
1895     	**	Copy TEMP register to SAVEP in header.
1896     	*/
1897     	SCR_COPY (4),
1898     		RADDR (temp),
1899     		NADDR (header.savep),
1900     	SCR_CLR (SCR_ACK),
1901     		0,
1902     	SCR_JUMP,
1903     		PADDR (dispatch),
1904     }/*-------------------------< RESTORE_DP >---------------*/,{
1905     	/*
1906     	**	RESTORE_DP message:
1907     	**	Copy SAVEP in header to TEMP register.
1908     	*/
1909     	SCR_COPY (4),
1910     		NADDR (header.savep),
1911     		RADDR (temp),
1912     	SCR_JUMP,
1913     		PADDR (clrack),
1914     
1915     }/*-------------------------< DISCONNECT >---------------*/,{
1916     	/*
1917     	**	DISCONNECTing  ...
1918     	**
1919     	**	disable the "unexpected disconnect" feature,
1920     	**	and remove the ACK signal.
1921     	*/
1922     	SCR_REG_REG (scntl2, SCR_AND, 0x7f),
1923     		0,
1924     	SCR_CLR (SCR_ACK|SCR_ATN),
1925     		0,
1926     	/*
1927     	**	Wait for the disconnect.
1928     	*/
1929     	SCR_WAIT_DISC,
1930     		0,
1931     	/*
1932     	**	Status is: DISCONNECTED.
1933     	*/
1934     	SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
1935     		0,
1936     	/*
1937     	**	If QUIRK_AUTOSAVE is set,
1938     	**	do an "save pointer" operation.
1939     	*/
1940     	SCR_FROM_REG (QU_REG),
1941     		0,
1942     	SCR_JUMP ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
1943     		PADDR (cleanup_ok),
1944     	/*
1945     	**	like SAVE_DP message:
1946     	**	Copy TEMP register to SAVEP in header.
1947     	*/
1948     	SCR_COPY (4),
1949     		RADDR (temp),
1950     		NADDR (header.savep),
1951     	SCR_JUMP,
1952     		PADDR (cleanup_ok),
1953     
1954     }/*-------------------------< MSG_OUT >-------------------*/,{
1955     	/*
1956     	**	The target requests a message.
1957     	*/
1958     	SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
1959     		NADDR (msgout),
1960     	SCR_COPY (1),
1961     		NADDR (msgout),
1962     		NADDR (lastmsg),
1963     	/*
1964     	**	If it was no ABORT message ...
1965     	*/
1966     	SCR_JUMP ^ IFTRUE (DATA (M_ABORT)),
1967     		PADDRH (msg_out_abort),
1968     	/*
1969     	**	... wait for the next phase
1970     	**	if it's a message out, send it again, ...
1971     	*/
1972     	SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
1973     		PADDR (msg_out),
1974     }/*-------------------------< MSG_OUT_DONE >--------------*/,{
1975     	/*
1976     	**	... else clear the message ...
1977     	*/
1978     	SCR_LOAD_REG (scratcha, M_NOOP),
1979     		0,
1980     	SCR_COPY (4),
1981     		RADDR (scratcha),
1982     		NADDR (msgout),
1983     	/*
1984     	**	... and process the next phase
1985     	*/
1986     	SCR_JUMP,
1987     		PADDR (dispatch),
1988     }/*-------------------------< IDLE >------------------------*/,{
1989     	/*
1990     	**	Nothing to do?
1991     	**	Wait for reselect.
1992     	**	This NOP will be patched with LED OFF
1993     	**	SCR_REG_REG (gpreg, SCR_OR, 0x01)
1994     	*/
1995     	SCR_NO_OP,
1996     		0,
1997     }/*-------------------------< RESELECT >--------------------*/,{
1998     	/*
1999     	**	make the DSA invalid.
2000     	*/
2001     	SCR_LOAD_REG (dsa, 0xff),
2002     		0,
2003     	SCR_CLR (SCR_TRG),
2004     		0,
2005     	SCR_LOAD_REG (HS_REG, HS_IN_RESELECT),
2006     		0,
2007     	/*
2008     	**	Sleep waiting for a reselection.
2009     	**	If SIGP is set, special treatment.
2010     	**
2011     	**	Zu allem bereit ..
2012     	*/
2013     	SCR_WAIT_RESEL,
2014     		PADDR(start),
2015     }/*-------------------------< RESELECTED >------------------*/,{
2016     	/*
2017     	**	This NOP will be patched with LED ON
2018     	**	SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2019     	*/
2020     	SCR_NO_OP,
2021     		0,
2022     	/*
2023     	**	... zu nichts zu gebrauchen ?
2024     	**
2025     	**      load the target id into the SFBR
2026     	**	and jump to the control block.
2027     	**
2028     	**	Look at the declarations of
2029     	**	- struct ncb
2030     	**	- struct tcb
2031     	**	- struct lcb
2032     	**	- struct ccb
2033     	**	to understand what's going on.
2034     	*/
2035     	SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
2036     		0,
2037     	SCR_TO_REG (sdid),
2038     		0,
2039     	SCR_JUMP,
2040     		NADDR (jump_tcb),
2041     
2042     }/*-------------------------< RESEL_DSA >-------------------*/,{
2043     	/*
2044     	**	Ack the IDENTIFY or TAG previously received.
2045     	*/
2046     	SCR_CLR (SCR_ACK),
2047     		0,
2048     	/*
2049     	**      The ncr doesn't have an indirect load
2050     	**	or store command. So we have to
2051     	**	copy part of the control block to a
2052     	**	fixed place, where we can access it.
2053     	**
2054     	**	We patch the address part of a
2055     	**	COPY command with the DSA-register.
2056     	*/
2057     	SCR_COPY_F (4),
2058     		RADDR (dsa),
2059     		PADDR (loadpos1),
2060     	/*
2061     	**	then we do the actual copy.
2062     	*/
2063     	SCR_COPY (sizeof (struct head)),
2064     	/*
2065     	**	continued after the next label ...
2066     	*/
2067     
2068     }/*-------------------------< LOADPOS1 >-------------------*/,{
2069     		0,
2070     		NADDR (header),
2071     	/*
2072     	**	The DSA contains the data structure address.
2073     	*/
2074     	SCR_JUMP,
2075     		PADDR (prepare),
2076     
2077     }/*-------------------------< RESEL_LUN >-------------------*/,{
2078     	/*
2079     	**	come back to this point
2080     	**	to get an IDENTIFY message
2081     	**	Wait for a msg_in phase.
2082     	*/
2083     	SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
2084     		SIR_RESEL_NO_MSG_IN,
2085     	/*
2086     	**	message phase.
2087     	**	Read the data directly from the BUS DATA lines.
2088     	**	This helps to support very old SCSI devices that 
2089     	**	may reselect without sending an IDENTIFY.
2090     	*/
2091     	SCR_FROM_REG (sbdl),
2092     		0,
2093     	/*
2094     	**	It should be an Identify message.
2095     	*/
2096     	SCR_RETURN,
2097     		0,
2098     }/*-------------------------< RESEL_TAG >-------------------*/,{
2099     	/*
2100     	**	Read IDENTIFY + SIMPLE + TAG using a single MOVE.
2101     	**	Agressive optimization, is'nt it?
2102     	**	No need to test the SIMPLE TAG message, since the 
2103     	**	driver only supports conformant devices for tags. ;-)
2104     	*/
2105     	SCR_MOVE_ABS (3) ^ SCR_MSG_IN,
2106     		NADDR (msgin),
2107     	/*
2108     	**	Read the TAG from the SIDL.
2109     	**	Still an aggressive optimization. ;-)
2110     	**	Compute the CCB indirect jump address which 
2111     	**	is (#TAG*2 & 0xfc) due to tag numbering using 
2112     	**	1,3,5..MAXTAGS*2+1 actual values.
2113     	*/
2114     	SCR_REG_SFBR (sidl, SCR_SHL, 0),
2115     		0,
2116     	SCR_SFBR_REG (temp, SCR_AND, 0xfc),
2117     		0,
2118     }/*-------------------------< JUMP_TO_NEXUS >-------------------*/,{
2119     	SCR_COPY_F (4),
2120     		RADDR (temp),
2121     		PADDR (nexus_indirect),
2122     	SCR_COPY (4),
2123     }/*-------------------------< NEXUS_INDIRECT >-------------------*/,{
2124     		0,
2125     		RADDR (temp),
2126     	SCR_RETURN,
2127     		0,
2128     }/*-------------------------< RESEL_NOTAG >-------------------*/,{
2129     	/*
2130     	**	No tag expected.
2131     	**	Read an throw away the IDENTIFY.
2132     	*/
2133     	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2134     		NADDR (msgin),
2135     	SCR_JUMP,
2136     		PADDR (jump_to_nexus),
2137     }/*-------------------------< DATA_IN >--------------------*/,{
2138     /*
2139     **	Because the size depends on the
2140     **	#define MAX_SCATTERL parameter,
2141     **	it is filled in at runtime.
2142     **
2143     **  ##===========< i=0; i<MAX_SCATTERL >=========
2144     **  ||	SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2145     **  ||		PADDR (dispatch),
2146     **  ||	SCR_MOVE_TBL ^ SCR_DATA_IN,
2147     **  ||		offsetof (struct dsb, data[ i]),
2148     **  ##==========================================
2149     **
2150     **---------------------------------------------------------
2151     */
2152     0
2153     }/*-------------------------< DATA_IN2 >-------------------*/,{
2154     	SCR_CALL,
2155     		PADDR (dispatch),
2156     	SCR_JUMP,
2157     		PADDR (no_data),
2158     }/*-------------------------< DATA_OUT >--------------------*/,{
2159     /*
2160     **	Because the size depends on the
2161     **	#define MAX_SCATTERL parameter,
2162     **	it is filled in at runtime.
2163     **
2164     **  ##===========< i=0; i<MAX_SCATTERL >=========
2165     **  ||	SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2166     **  ||		PADDR (dispatch),
2167     **  ||	SCR_MOVE_TBL ^ SCR_DATA_OUT,
2168     **  ||		offsetof (struct dsb, data[ i]),
2169     **  ##==========================================
2170     **
2171     **---------------------------------------------------------
2172     */
2173     0
2174     }/*-------------------------< DATA_OUT2 >-------------------*/,{
2175     	SCR_CALL,
2176     		PADDR (dispatch),
2177     	SCR_JUMP,
2178     		PADDR (no_data),
2179     }/*--------------------------------------------------------*/
2180     };
2181     
2182     static	struct scripth scripth0 __initdata = {
2183     /*-------------------------< TRYLOOP >---------------------*/{
2184     /*
2185     **	Start the next entry.
2186     **	Called addresses point to the launch script in the CCB.
2187     **	They are patched by the main processor.
2188     **
2189     **	Because the size depends on the
2190     **	#define MAX_START parameter, it is filled
2191     **	in at runtime.
2192     **
2193     **-----------------------------------------------------------
2194     **
2195     **  ##===========< I=0; i<MAX_START >===========
2196     **  ||	SCR_CALL,
2197     **  ||		PADDR (idle),
2198     **  ##==========================================
2199     **
2200     **-----------------------------------------------------------
2201     */
2202     0
2203     }/*------------------------< TRYLOOP2 >---------------------*/,{
2204     	SCR_JUMP,
2205     		PADDRH(tryloop),
2206     
2207     #ifdef SCSI_NCR_CCB_DONE_SUPPORT
2208     
2209     }/*------------------------< DONE_QUEUE >-------------------*/,{
2210     /*
2211     **	Copy the CCB address to the next done entry.
2212     **	Because the size depends on the
2213     **	#define MAX_DONE parameter, it is filled
2214     **	in at runtime.
2215     **
2216     **-----------------------------------------------------------
2217     **
2218     **  ##===========< I=0; i<MAX_DONE >===========
2219     **  ||	SCR_COPY (sizeof(ccb_p)),
2220     **  ||		NADDR (header.cp),
2221     **  ||		NADDR (ccb_done[i]),
2222     **  ||	SCR_CALL,
2223     **  ||		PADDR (done_end),
2224     **  ##==========================================
2225     **
2226     **-----------------------------------------------------------
2227     */
2228     0
2229     }/*------------------------< DONE_QUEUE2 >------------------*/,{
2230     	SCR_JUMP,
2231     		PADDRH (done_queue),
2232     
2233     #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
2234     }/*------------------------< SELECT_NO_ATN >-----------------*/,{
2235     	/*
2236     	**	Set Initiator mode.
2237     	**      And try to select this target without ATN.
2238     	*/
2239     
2240     	SCR_CLR (SCR_TRG),
2241     		0,
2242     	SCR_LOAD_REG (HS_REG, HS_SELECTING),
2243     		0,
2244     	SCR_SEL_TBL ^ offsetof (struct dsb, select),
2245     		PADDR (reselect),
2246     	SCR_JUMP,
2247     		PADDR (select2),
2248     
2249     }/*-------------------------< CANCEL >------------------------*/,{
2250     
2251     	SCR_LOAD_REG (scratcha, HS_ABORTED),
2252     		0,
2253     	SCR_JUMPR,
2254     		8,
2255     }/*-------------------------< SKIP >------------------------*/,{
2256     	SCR_LOAD_REG (scratcha, 0),
2257     		0,
2258     	/*
2259     	**	This entry has been canceled.
2260     	**	Next time use the next slot.
2261     	*/
2262     	SCR_COPY (4),
2263     		RADDR (temp),
2264     		PADDR (startpos),
2265     	/*
2266     	**      The ncr doesn't have an indirect load
2267     	**	or store command. So we have to
2268     	**	copy part of the control block to a
2269     	**	fixed place, where we can access it.
2270     	**
2271     	**	We patch the address part of a
2272     	**	COPY command with the DSA-register.
2273     	*/
2274     	SCR_COPY_F (4),
2275     		RADDR (dsa),
2276     		PADDRH (skip2),
2277     	/*
2278     	**	then we do the actual copy.
2279     	*/
2280     	SCR_COPY (sizeof (struct head)),
2281     	/*
2282     	**	continued after the next label ...
2283     	*/
2284     }/*-------------------------< SKIP2 >---------------------*/,{
2285     		0,
2286     		NADDR (header),
2287     	/*
2288     	**      Initialize the status registers
2289     	*/
2290     	SCR_COPY (4),
2291     		NADDR (header.status),
2292     		RADDR (scr0),
2293     	/*
2294     	**	Force host status.
2295     	*/
2296     	SCR_FROM_REG (scratcha),
2297     		0,
2298     	SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
2299     		16,
2300     	SCR_REG_REG (HS_REG, SCR_OR, HS_SKIPMASK),
2301     		0,
2302     	SCR_JUMPR,
2303     		8,
2304     	SCR_TO_REG (HS_REG),
2305     		0,
2306     	SCR_LOAD_REG (SS_REG, S_GOOD),
2307     		0,
2308     	SCR_JUMP,
2309     		PADDR (cleanup_ok),
2310     
2311     },/*-------------------------< PAR_ERR_DATA_IN >---------------*/{
2312     	/*
2313     	**	Ignore all data in byte, until next phase
2314     	*/
2315     	SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
2316     		PADDRH (par_err_other),
2317     	SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
2318     		NADDR (scratch),
2319     	SCR_JUMPR,
2320     		-24,
2321     },/*-------------------------< PAR_ERR_OTHER >------------------*/{
2322     	/*
2323     	**	count it.
2324     	*/
2325     	SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
2326     		0,
2327     	/*
2328     	**	jump to dispatcher.
2329     	*/
2330     	SCR_JUMP,
2331     		PADDR (dispatch),
2332     }/*-------------------------< MSG_REJECT >---------------*/,{
2333     	/*
2334     	**	If a negotiation was in progress,
2335     	**	negotiation failed.
2336     	**	Otherwise, let the C code print 
2337     	**	some message.
2338     	*/
2339     	SCR_FROM_REG (HS_REG),
2340     		0,
2341     	SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
2342     		SIR_REJECT_RECEIVED,
2343     	SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2344     		SIR_NEGO_FAILED,
2345     	SCR_JUMP,
2346     		PADDR (clrack),
2347     
2348     }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
2349     	/*
2350     	**	Terminate cycle
2351     	*/
2352     	SCR_CLR (SCR_ACK),
2353     		0,
2354     	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2355     		PADDR (dispatch),
2356     	/*
2357     	**	get residue size.
2358     	*/
2359     	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2360     		NADDR (msgin[1]),
2361     	/*
2362     	**	Size is 0 .. ignore message.
2363     	*/
2364     	SCR_JUMP ^ IFTRUE (DATA (0)),
2365     		PADDR (clrack),
2366     	/*
2367     	**	Size is not 1 .. have to interrupt.
2368     	*/
2369     	SCR_JUMPR ^ IFFALSE (DATA (1)),
2370     		40,
2371     	/*
2372     	**	Check for residue byte in swide register
2373     	*/
2374     	SCR_FROM_REG (scntl2),
2375     		0,
2376     	SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
2377     		16,
2378     	/*
2379     	**	There IS data in the swide register.
2380     	**	Discard it.
2381     	*/
2382     	SCR_REG_REG (scntl2, SCR_OR, WSR),
2383     		0,
2384     	SCR_JUMP,
2385     		PADDR (clrack),
2386     	/*
2387     	**	Load again the size to the sfbr register.
2388     	*/
2389     	SCR_FROM_REG (scratcha),
2390     		0,
2391     	SCR_INT,
2392     		SIR_IGN_RESIDUE,
2393     	SCR_JUMP,
2394     		PADDR (clrack),
2395     
2396     }/*-------------------------< MSG_EXTENDED >-------------*/,{
2397     	/*
2398     	**	Terminate cycle
2399     	*/
2400     	SCR_CLR (SCR_ACK),
2401     		0,
2402     	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2403     		PADDR (dispatch),
2404     	/*
2405     	**	get length.
2406     	*/
2407     	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2408     		NADDR (msgin[1]),
2409     	/*
2410     	*/
2411     	SCR_JUMP ^ IFTRUE (DATA (3)),
2412     		PADDRH (msg_ext_3),
2413     	SCR_JUMP ^ IFFALSE (DATA (2)),
2414     		PADDR (msg_bad),
2415     }/*-------------------------< MSG_EXT_2 >----------------*/,{
2416     	SCR_CLR (SCR_ACK),
2417     		0,
2418     	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2419     		PADDR (dispatch),
2420     	/*
2421     	**	get extended message code.
2422     	*/
2423     	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2424     		NADDR (msgin[2]),
2425     	SCR_JUMP ^ IFTRUE (DATA (M_X_WIDE_REQ)),
2426     		PADDRH (msg_wdtr),
2427     	/*
2428     	**	unknown extended message
2429     	*/
2430     	SCR_JUMP,
2431     		PADDR (msg_bad)
2432     }/*-------------------------< MSG_WDTR >-----------------*/,{
2433     	SCR_CLR (SCR_ACK),
2434     		0,
2435     	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2436     		PADDR (dispatch),
2437     	/*
2438     	**	get data bus width
2439     	*/
2440     	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2441     		NADDR (msgin[3]),
2442     	/*
2443     	**	let the host do the real work.
2444     	*/
2445     	SCR_INT,
2446     		SIR_NEGO_WIDE,
2447     	/*
2448     	**	let the target fetch our answer.
2449     	*/
2450     	SCR_SET (SCR_ATN),
2451     		0,
2452     	SCR_CLR (SCR_ACK),
2453     		0,
2454     	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2455     		PADDRH (nego_bad_phase),
2456     
2457     }/*-------------------------< SEND_WDTR >----------------*/,{
2458     	/*
2459     	**	Send the M_X_WIDE_REQ
2460     	*/
2461     	SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
2462     		NADDR (msgout),
2463     	SCR_COPY (1),
2464     		NADDR (msgout),
2465     		NADDR (lastmsg),
2466     	SCR_JUMP,
2467     		PADDR (msg_out_done),
2468     
2469     }/*-------------------------< MSG_EXT_3 >----------------*/,{
2470     	SCR_CLR (SCR_ACK),
2471     		0,
2472     	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2473     		PADDR (dispatch),
2474     	/*
2475     	**	get extended message code.
2476     	*/
2477     	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2478     		NADDR (msgin[2]),
2479     	SCR_JUMP ^ IFTRUE (DATA (M_X_SYNC_REQ)),
2480     		PADDRH (msg_sdtr),
2481     	/*
2482     	**	unknown extended message
2483     	*/
2484     	SCR_JUMP,
2485     		PADDR (msg_bad)
2486     
2487     }/*-------------------------< MSG_SDTR >-----------------*/,{
2488     	SCR_CLR (SCR_ACK),
2489     		0,
2490     	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2491     		PADDR (dispatch),
2492     	/*
2493     	**	get period and offset
2494     	*/
2495     	SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
2496     		NADDR (msgin[3]),
2497     	/*
2498     	**	let the host do the real work.
2499     	*/
2500     	SCR_INT,
2501     		SIR_NEGO_SYNC,
2502     	/*
2503     	**	let the target fetch our answer.
2504     	*/
2505     	SCR_SET (SCR_ATN),
2506     		0,
2507     	SCR_CLR (SCR_ACK),
2508     		0,
2509     	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2510     		PADDRH (nego_bad_phase),
2511     
2512     }/*-------------------------< SEND_SDTR >-------------*/,{
2513     	/*
2514     	**	Send the M_X_SYNC_REQ
2515     	*/
2516     	SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
2517     		NADDR (msgout),
2518     	SCR_COPY (1),
2519     		NADDR (msgout),
2520     		NADDR (lastmsg),
2521     	SCR_JUMP,
2522     		PADDR (msg_out_done),
2523     
2524     }/*-------------------------< NEGO_BAD_PHASE >------------*/,{
2525     	SCR_INT,
2526     		SIR_NEGO_PROTO,
2527     	SCR_JUMP,
2528     		PADDR (dispatch),
2529     
2530     }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
2531     	/*
2532     	**	After ABORT message,
2533     	**
2534     	**	expect an immediate disconnect, ...
2535     	*/
2536     	SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2537     		0,
2538     	SCR_CLR (SCR_ACK|SCR_ATN),
2539     		0,
2540     	SCR_WAIT_DISC,
2541     		0,
2542     	/*
2543     	**	... and set the status to "ABORTED"
2544     	*/
2545     	SCR_LOAD_REG (HS_REG, HS_ABORTED),
2546     		0,
2547     	SCR_JUMP,
2548     		PADDR (cleanup),
2549     
2550     }/*-------------------------< HDATA_IN >-------------------*/,{
2551     /*
2552     **	Because the size depends on the
2553     **	#define MAX_SCATTERH parameter,
2554     **	it is filled in at runtime.
2555     **
2556     **  ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
2557     **  ||	SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2558     **  ||		PADDR (dispatch),
2559     **  ||	SCR_MOVE_TBL ^ SCR_DATA_IN,
2560     **  ||		offsetof (struct dsb, data[ i]),
2561     **  ##===================================================
2562     **
2563     **---------------------------------------------------------
2564     */
2565     0
2566     }/*-------------------------< HDATA_IN2 >------------------*/,{
2567     	SCR_JUMP,
2568     		PADDR (data_in),
2569     
2570     }/*-------------------------< HDATA_OUT >-------------------*/,{
2571     /*
2572     **	Because the size depends on the
2573     **	#define MAX_SCATTERH parameter,
2574     **	it is filled in at runtime.
2575     **
2576     **  ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
2577     **  ||	SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2578     **  ||		PADDR (dispatch),
2579     **  ||	SCR_MOVE_TBL ^ SCR_DATA_OUT,
2580     **  ||		offsetof (struct dsb, data[ i]),
2581     **  ##===================================================
2582     **
2583     **---------------------------------------------------------
2584     */
2585     0
2586     }/*-------------------------< HDATA_OUT2 >------------------*/,{
2587     	SCR_JUMP,
2588     		PADDR (data_out),
2589     
2590     }/*-------------------------< RESET >----------------------*/,{
2591     	/*
2592     	**      Send a M_RESET message if bad IDENTIFY 
2593     	**	received on reselection.
2594     	*/
2595     	SCR_LOAD_REG (scratcha, M_ABORT_TAG),
2596     		0,
2597     	SCR_JUMP,
2598     		PADDRH (abort_resel),
2599     }/*-------------------------< ABORTTAG >-------------------*/,{
2600     	/*
2601     	**      Abort a wrong tag received on reselection.
2602     	*/
2603     	SCR_LOAD_REG (scratcha, M_ABORT_TAG),
2604     		0,
2605     	SCR_JUMP,
2606     		PADDRH (abort_resel),
2607     }/*-------------------------< ABORT >----------------------*/,{
2608     	/*
2609     	**      Abort a reselection when no active CCB.
2610     	*/
2611     	SCR_LOAD_REG (scratcha, M_ABORT),
2612     		0,
2613     }/*-------------------------< ABORT_RESEL >----------------*/,{
2614     	SCR_COPY (1),
2615     		RADDR (scratcha),
2616     		NADDR (msgout),
2617     	SCR_SET (SCR_ATN),
2618     		0,
2619     	SCR_CLR (SCR_ACK),
2620     		0,
2621     	/*
2622     	**	and send it.
2623     	**	we expect an immediate disconnect
2624     	*/
2625     	SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2626     		0,
2627     	SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2628     		NADDR (msgout),
2629     	SCR_COPY (1),
2630     		NADDR (msgout),
2631     		NADDR (lastmsg),
2632     	SCR_CLR (SCR_ACK|SCR_ATN),
2633     		0,
2634     	SCR_WAIT_DISC,
2635     		0,
2636     	SCR_JUMP,
2637     		PADDR (start),
2638     }/*-------------------------< RESEND_IDENT >-------------------*/,{
2639     	/*
2640     	**	The target stays in MSG OUT phase after having acked 
2641     	**	Identify [+ Tag [+ Extended message ]]. Targets shall
2642     	**	behave this way on parity error.
2643     	**	We must send it again all the messages.
2644     	*/
2645     	SCR_SET (SCR_ATN), /* Shall be asserted 2 deskew delays before the  */
2646     		0,         /* 1rst ACK = 90 ns. Hope the NCR is'nt too fast */
2647     	SCR_JUMP,
2648     		PADDR (send_ident),
2649     }/*-------------------------< CLRATN_GO_ON >-------------------*/,{
2650     	SCR_CLR (SCR_ATN),
2651     		0,
2652     	SCR_JUMP,
2653     }/*-------------------------< NXTDSP_GO_ON >-------------------*/,{
2654     		0,
2655     }/*-------------------------< SDATA_IN >-------------------*/,{
2656     	SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2657     		PADDR (dispatch),
2658     	SCR_MOVE_TBL ^ SCR_DATA_IN,
2659     		offsetof (struct dsb, sense),
2660     	SCR_CALL,
2661     		PADDR (dispatch),
2662     	SCR_JUMP,
2663     		PADDR (no_data),
2664     }/*-------------------------< DATA_IO >--------------------*/,{
2665     	/*
2666     	**	We jump here if the data direction was unknown at the 
2667     	**	time we had to queue the command to the scripts processor.
2668     	**	Pointers had been set as follow in this situation:
2669     	**	  savep   -->   DATA_IO
2670     	**	  lastp   -->   start pointer when DATA_IN
2671     	**	  goalp   -->   goal  pointer when DATA_IN
2672     	**	  wlastp  -->   start pointer when DATA_OUT
2673     	**	  wgoalp  -->   goal  pointer when DATA_OUT
2674     	**	This script sets savep/lastp/goalp according to the 
2675     	**	direction chosen by the target.
2676     	*/
2677     	SCR_JUMPR ^ IFTRUE (WHEN (SCR_DATA_OUT)),
2678     		32,
2679     	/*
2680     	**	Direction is DATA IN.
2681     	**	Warning: we jump here, even when phase is DATA OUT.
2682     	*/
2683     	SCR_COPY (4),
2684     		NADDR (header.lastp),
2685     		NADDR (header.savep),
2686     
2687     	/*
2688     	**	Jump to the SCRIPTS according to actual direction.
2689     	*/
2690     	SCR_COPY (4),
2691     		NADDR (header.savep),
2692     		RADDR (temp),
2693     	SCR_RETURN,
2694     		0,
2695     	/*
2696     	**	Direction is DATA OUT.
2697     	*/
2698     	SCR_COPY (4),
2699     		NADDR (header.wlastp),
2700     		NADDR (header.lastp),
2701     	SCR_COPY (4),
2702     		NADDR (header.wgoalp),
2703     		NADDR (header.goalp),
2704     	SCR_JUMPR,
2705     		-64,
2706     }/*-------------------------< BAD_IDENTIFY >---------------*/,{
2707     	/*
2708     	**	If message phase but not an IDENTIFY,
2709     	**	get some help from the C code.
2710     	**	Old SCSI device may behave so.
2711     	*/
2712     	SCR_JUMPR ^ IFTRUE (MASK (0x80, 0x80)),
2713     		16,
2714     	SCR_INT,
2715     		SIR_RESEL_NO_IDENTIFY,
2716     	SCR_JUMP,
2717     		PADDRH (reset),
2718     	/*
2719     	**	Message is an IDENTIFY, but lun is unknown.
2720     	**	Read the message, since we got it directly 
2721     	**	from the SCSI BUS data lines.
2722     	**	Signal problem to C code for logging the event.
2723     	**	Send a M_ABORT to clear all pending tasks.
2724     	*/
2725     	SCR_INT,
2726     		SIR_RESEL_BAD_LUN,
2727     	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2728     		NADDR (msgin),
2729     	SCR_JUMP,
2730     		PADDRH (abort),
2731     }/*-------------------------< BAD_I_T_L >------------------*/,{
2732     	/*
2733     	**	We donnot have a task for that I_T_L.
2734     	**	Signal problem to C code for logging the event.
2735     	**	Send a M_ABORT message.
2736     	*/
2737     	SCR_INT,
2738     		SIR_RESEL_BAD_I_T_L,
2739     	SCR_JUMP,
2740     		PADDRH (abort),
2741     }/*-------------------------< BAD_I_T_L_Q >----------------*/,{
2742     	/*
2743     	**	We donnot have a task that matches the tag.
2744     	**	Signal problem to C code for logging the event.
2745     	**	Send a M_ABORTTAG message.
2746     	*/
2747     	SCR_INT,
2748     		SIR_RESEL_BAD_I_T_L_Q,
2749     	SCR_JUMP,
2750     		PADDRH (aborttag),
2751     }/*-------------------------< BAD_TARGET >-----------------*/,{
2752     	/*
2753     	**	We donnot know the target that reselected us.
2754     	**	Grab the first message if any (IDENTIFY).
2755     	**	Signal problem to C code for logging the event.
2756     	**	M_RESET message.
2757     	*/
2758     	SCR_INT,
2759     		SIR_RESEL_BAD_TARGET,
2760     	SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2761     		8,
2762     	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2763     		NADDR (msgin),
2764     	SCR_JUMP,
2765     		PADDRH (reset),
2766     }/*-------------------------< BAD_STATUS >-----------------*/,{
2767     	/*
2768     	**	If command resulted in either QUEUE FULL,
2769     	**	CHECK CONDITION or COMMAND TERMINATED,
2770     	**	call the C code.
2771     	*/
2772     	SCR_INT ^ IFTRUE (DATA (S_QUEUE_FULL)),
2773     		SIR_BAD_STATUS,
2774     	SCR_INT ^ IFTRUE (DATA (S_CHECK_COND)),
2775     		SIR_BAD_STATUS,
2776     	SCR_INT ^ IFTRUE (DATA (S_TERMINATED)),
2777     		SIR_BAD_STATUS,
2778     	SCR_RETURN,
2779     		0,
2780     }/*-------------------------< START_RAM >-------------------*/,{
2781     	/*
2782     	**	Load the script into on-chip RAM, 
2783     	**	and jump to start point.
2784     	*/
2785     	SCR_COPY_F (4),
2786     		RADDR (scratcha),
2787     		PADDRH (start_ram0),
2788     	SCR_COPY (sizeof (struct script)),
2789     }/*-------------------------< START_RAM0 >--------------------*/,{
2790     		0,
2791     		PADDR (start),
2792     	SCR_JUMP,
2793     		PADDR (start),
2794     }/*-------------------------< STO_RESTART >-------------------*/,{
2795     	/*
2796     	**
2797     	**	Repair start queue (e.g. next time use the next slot) 
2798     	**	and jump to start point.
2799     	*/
2800     	SCR_COPY (4),
2801     		RADDR (temp),
2802     		PADDR (startpos),
2803     	SCR_JUMP,
2804     		PADDR (start),
2805     }/*-------------------------< SNOOPTEST >-------------------*/,{
2806     	/*
2807     	**	Read the variable.
2808     	*/
2809     	SCR_COPY (4),
2810     		NADDR(ncr_cache),
2811     		RADDR (scratcha),
2812     	/*
2813     	**	Write the variable.
2814     	*/
2815     	SCR_COPY (4),
2816     		RADDR (temp),
2817     		NADDR(ncr_cache),
2818     	/*
2819     	**	Read back the variable.
2820     	*/
2821     	SCR_COPY (4),
2822     		NADDR(ncr_cache),
2823     		RADDR (temp),
2824     }/*-------------------------< SNOOPEND >-------------------*/,{
2825     	/*
2826     	**	And stop.
2827     	*/
2828     	SCR_INT,
2829     		99,
2830     }/*--------------------------------------------------------*/
2831     };
2832     
2833     /*==========================================================
2834     **
2835     **
2836     **	Fill in #define dependent parts of the script
2837     **
2838     **
2839     **==========================================================
2840     */
2841     
2842     void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
2843     {
2844     	int	i;
2845     	ncrcmd	*p;
2846     
2847     	p = scrh->tryloop;
2848     	for (i=0; i<MAX_START; i++) {
2849     		*p++ =SCR_CALL;
2850     		*p++ =PADDR (idle);
2851     	};
2852     
2853     	assert ((u_long)p == (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
2854     
2855     #ifdef SCSI_NCR_CCB_DONE_SUPPORT
2856     
2857     	p = scrh->done_queue;
2858     	for (i = 0; i<MAX_DONE; i++) {
2859     		*p++ =SCR_COPY (sizeof(ccb_p));
2860     		*p++ =NADDR (header.cp);
2861     		*p++ =NADDR (ccb_done[i]);
2862     		*p++ =SCR_CALL;
2863     		*p++ =PADDR (done_end);
2864     	}
2865     
2866     	assert ((u_long)p ==(u_long)&scrh->done_queue+sizeof(scrh->done_queue));
2867     
2868     #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
2869     
2870     	p = scrh->hdata_in;
2871     	for (i=0; i<MAX_SCATTERH; i++) {
2872     		*p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
2873     		*p++ =PADDR (dispatch);
2874     		*p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
2875     		*p++ =offsetof (struct dsb, data[i]);
2876     	};
2877     	assert ((u_long)p == (u_long)&scrh->hdata_in + sizeof (scrh->hdata_in));
2878     
2879     	p = scr->data_in;
2880     	for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
2881     		*p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
2882     		*p++ =PADDR (dispatch);
2883     		*p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
2884     		*p++ =offsetof (struct dsb, data[i]);
2885     	};
2886     	assert ((u_long)p == (u_long)&scr->data_in + sizeof (scr->data_in));
2887     
2888     	p = scrh->hdata_out;
2889     	for (i=0; i<MAX_SCATTERH; i++) {
2890     		*p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
2891     		*p++ =PADDR (dispatch);
2892     		*p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
2893     		*p++ =offsetof (struct dsb, data[i]);
2894     	};
2895     	assert ((u_long)p==(u_long)&scrh->hdata_out + sizeof (scrh->hdata_out));
2896     
2897     	p = scr->data_out;
2898     	for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
2899     		*p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
2900     		*p++ =PADDR (dispatch);
2901     		*p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
2902     		*p++ =offsetof (struct dsb, data[i]);
2903     	};
2904     
2905     	assert ((u_long)p == (u_long)&scr->data_out + sizeof (scr->data_out));
2906     }
2907     
2908     /*==========================================================
2909     **
2910     **
2911     **	Copy and rebind a script.
2912     **
2913     **
2914     **==========================================================
2915     */
2916     
2917     static void __init 
2918     ncr_script_copy_and_bind (ncb_p np, ncrcmd *src, ncrcmd *dst, int len)
2919     {
2920     	ncrcmd  opcode, new, old, tmp1, tmp2;
2921     	ncrcmd	*start, *end;
2922     	int relocs;
2923     	int opchanged = 0;
2924     
2925     	start = src;
2926     	end = src + len/4;
2927     
2928     	while (src < end) {
2929     
2930     		opcode = *src++;
2931     		*dst++ = cpu_to_scr(opcode);
2932     
2933     		/*
2934     		**	If we forget to change the length
2935     		**	in struct script, a field will be
2936     		**	padded with 0. This is an illegal
2937     		**	command.
2938     		*/
2939     
2940     		if (opcode == 0) {
2941     			printk (KERN_ERR "%s: ERROR0 IN SCRIPT at %d.\n",
2942     				ncr_name(np), (int) (src-start-1));
2943     			MDELAY (1000);
2944     		};
2945     
2946     		if (DEBUG_FLAGS & DEBUG_SCRIPT)
2947     			printk (KERN_DEBUG "%p:  <%x>\n",
2948     				(src-1), (unsigned)opcode);
2949     
2950     		/*
2951     		**	We don't have to decode ALL commands
2952     		*/
2953     		switch (opcode >> 28) {
2954     
2955     		case 0xc:
2956     			/*
2957     			**	COPY has TWO arguments.
2958     			*/
2959     			relocs = 2;
2960     			tmp1 = src[0];
2961     #ifdef	RELOC_KVAR
2962     			if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
2963     				tmp1 = 0;
2964     #endif
2965     			tmp2 = src[1];
2966     #ifdef	RELOC_KVAR
2967     			if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
2968     				tmp2 = 0;
2969     #endif
2970     			if ((tmp1 ^ tmp2) & 3) {
2971     				printk (KERN_ERR"%s: ERROR1 IN SCRIPT at %d.\n",
2972     					ncr_name(np), (int) (src-start-1));
2973     				MDELAY (1000);
2974     			}
2975     			/*
2976     			**	If PREFETCH feature not enabled, remove 
2977     			**	the NO FLUSH bit if present.
2978     			*/
2979     			if ((opcode & SCR_NO_FLUSH) && !(np->features & FE_PFEN)) {
2980     				dst[-1] = cpu_to_scr(opcode & ~SCR_NO_FLUSH);
2981     				++opchanged;
2982     			}
2983     			break;
2984     
2985     		case 0x0:
2986     			/*
2987     			**	MOVE (absolute address)
2988     			*/
2989     			relocs = 1;
2990     			break;
2991     
2992     		case 0x8:
2993     			/*
2994     			**	JUMP / CALL
2995     			**	dont't relocate if relative :-)
2996     			*/
2997     			if (opcode & 0x00800000)
2998     				relocs = 0;
2999     			else
3000     				relocs = 1;
3001     			break;
3002     
3003     		case 0x4:
3004     		case 0x5:
3005     		case 0x6:
3006     		case 0x7:
3007     			relocs = 1;
3008     			break;
3009     
3010     		default:
3011     			relocs = 0;
3012     			break;
3013     		};
3014     
3015     		if (relocs) {
3016     			while (relocs--) {
3017     				old = *src++;
3018     
3019     				switch (old & RELOC_MASK) {
3020     				case RELOC_REGISTER:
3021     					new = (old & ~RELOC_MASK) + np->paddr;
3022     					break;
3023     				case RELOC_LABEL:
3024     					new = (old & ~RELOC_MASK) + np->p_script;
3025     					break;
3026     				case RELOC_LABELH:
3027     					new = (old & ~RELOC_MASK) + np->p_scripth;
3028     					break;
3029     				case RELOC_SOFTC:
3030     					new = (old & ~RELOC_MASK) + np->p_ncb;
3031     					break;
3032     #ifdef	RELOC_KVAR
3033     				case RELOC_KVAR:
3034     					if (((old & ~RELOC_MASK) <
3035     					     SCRIPT_KVAR_FIRST) ||
3036     					    ((old & ~RELOC_MASK) >
3037     					     SCRIPT_KVAR_LAST))
3038     						panic("ncr KVAR out of range");
3039     					new = vtophys(script_kvars[old &
3040     					    ~RELOC_MASK]);
3041     					break;
3042     #endif
3043     				case 0:
3044     					/* Don't relocate a 0 address. */
3045     					if (old == 0) {
3046     						new = old;
3047     						break;
3048     					}
3049     					/* fall through */
3050     				default:
3051     					panic("ncr_script_copy_and_bind: weird relocation %x\n", old);
3052     					break;
3053     				}
3054     
3055     				*dst++ = cpu_to_scr(new);
3056     			}
3057     		} else
3058     			*dst++ = cpu_to_scr(*src++);
3059     
3060     	};
3061     }
3062     
3063     /*==========================================================
3064     **
3065     **
3066     **      Auto configuration:  attach and init a host adapter.
3067     **
3068     **
3069     **==========================================================
3070     */
3071     
3072     /*
3073     **	Linux host data structure
3074     **
3075     **	The script area is allocated in the host data structure
3076     **	because kmalloc() returns NULL during scsi initialisations
3077     **	with Linux 1.2.X
3078     */
3079     
3080     struct host_data {
3081          struct ncb *ncb;
3082     };
3083     
3084     /*
3085     **	Print something which allows to retrieve the controller type, unit,
3086     **	target, lun concerned by a kernel message.
3087     */
3088     
3089     static void PRINT_TARGET(ncb_p np, int target)
3090     {
3091     	printk(KERN_INFO "%s-<%d,*>: ", ncr_name(np), target);
3092     }
3093     
3094     static void PRINT_LUN(ncb_p np, int target, int lun)
3095     {
3096     	printk(KERN_INFO "%s-<%d,%d>: ", ncr_name(np), target, lun);
3097     }
3098     
3099     static void PRINT_ADDR(Scsi_Cmnd *cmd)
3100     {
3101     	struct host_data *host_data = (struct host_data *) cmd->host->hostdata;
3102     	PRINT_LUN(host_data->ncb, cmd->target, cmd->lun);
3103     }
3104     
3105     /*==========================================================
3106     **
3107     **	NCR chip clock divisor table.
3108     **	Divisors are multiplied by 10,000,000 in order to make 
3109     **	calculations more simple.
3110     **
3111     **==========================================================
3112     */
3113     
3114     #define _5M 5000000
3115     static u_long div_10M[] =
3116     	{2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
3117     
3118     
3119     /*===============================================================
3120     **
3121     **	Prepare io register values used by ncr_init() according 
3122     **	to selected and supported features.
3123     **
3124     **	NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128 
3125     **	transfers. 32,64,128 are only supported by 875 and 895 chips.
3126     **	We use log base 2 (burst length) as internal code, with 
3127     **	value 0 meaning "burst disabled".
3128     **
3129     **===============================================================
3130     */
3131     
3132     /*
3133      *	Burst length from burst code.
3134      */
3135     #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
3136     
3137     /*
3138      *	Burst code from io register bits.
3139      */
3140     #define burst_code(dmode, ctest4, ctest5) \
3141     	(ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
3142     
3143     /*
3144      *	Set initial io register bits from burst code.
3145      */
3146     static inline void ncr_init_burst(ncb_p np, u_char bc)
3147     {
3148     	np->rv_ctest4	&= ~0x80;
3149     	np->rv_dmode	&= ~(0x3 << 6);
3150     	np->rv_ctest5	&= ~0x4;
3151     
3152     	if (!bc) {
3153     		np->rv_ctest4	|= 0x80;
3154     	}
3155     	else {
3156     		--bc;
3157     		np->rv_dmode	|= ((bc & 0x3) << 6);
3158     		np->rv_ctest5	|= (bc & 0x4);
3159     	}
3160     }
3161     
3162     #ifdef SCSI_NCR_NVRAM_SUPPORT
3163     
3164     /*
3165     **	Get target set-up from Symbios format NVRAM.
3166     */
3167     
3168     static void __init 
3169     ncr_Symbios_setup_target(ncb_p np, int target, Symbios_nvram *nvram)
3170     {
3171     	tcb_p tp = &np->target[target];
3172     	Symbios_target *tn = &nvram->target[target];
3173     
3174     	tp->usrsync = tn->sync_period ? (tn->sync_period + 3) / 4 : 255;
3175     	tp->usrwide = tn->bus_width == 0x10 ? 1 : 0;
3176     	tp->usrtags =
3177     		(tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? MAX_TAGS : 0;
3178     
3179     	if (!(tn->flags & SYMBIOS_DISCONNECT_ENABLE))
3180     		tp->usrflag |= UF_NODISC;
3181     	if (!(tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME))
3182     		tp->usrflag |= UF_NOSCAN;
3183     }
3184     
3185     /*
3186     **	Get target set-up from Tekram format NVRAM.
3187     */
3188     
3189     static void __init 
3190     ncr_Tekram_setup_target(ncb_p np, int target, Tekram_nvram *nvram)
3191     {
3192     	tcb_p tp = &np->target[target];
3193     	struct Tekram_target *tn = &nvram->target[target];
3194     	int i;
3195     
3196     	if (tn->flags & TEKRAM_SYNC_NEGO) {
3197     		i = tn->sync_index & 0xf;
3198     		tp->usrsync = Tekram_sync[i];
3199     	}
3200     
3201     	tp->usrwide = (tn->flags & TEKRAM_WIDE_NEGO) ? 1 : 0;
3202     
3203     	if (tn->flags & TEKRAM_TAGGED_COMMANDS) {
3204     		tp->usrtags = 2 << nvram->max_tags_index;
3205     	}
3206     
3207     	if (!(tn->flags & TEKRAM_DISCONNECT_ENABLE))
3208     		tp->usrflag = UF_NODISC;
3209      
3210     	/* If any device does not support parity, we will not use this option */
3211     	if (!(tn->flags & TEKRAM_PARITY_CHECK))
3212     		np->rv_scntl0  &= ~0x0a; /* SCSI parity checking disabled */
3213     }
3214     #endif /* SCSI_NCR_NVRAM_SUPPORT */
3215     
3216     static int __init ncr_prepare_setting(ncb_p np, ncr_nvram *nvram)
3217     {
3218     	u_char	burst_max;
3219     	u_long	period;
3220     	int i;
3221     
3222     	/*
3223     	**	Save assumed BIOS setting
3224     	*/
3225     
3226     	np->sv_scntl0	= INB(nc_scntl0) & 0x0a;
3227     	np->sv_scntl3	= INB(nc_scntl3) & 0x07;
3228     	np->sv_dmode	= INB(nc_dmode)  & 0xce;
3229     	np->sv_dcntl	= INB(nc_dcntl)  & 0xa8;
3230     	np->sv_ctest3	= INB(nc_ctest3) & 0x01;
3231     	np->sv_ctest4	= INB(nc_ctest4) & 0x80;
3232     	np->sv_ctest5	= INB(nc_ctest5) & 0x24;
3233     	np->sv_gpcntl	= INB(nc_gpcntl);
3234     	np->sv_stest2	= INB(nc_stest2) & 0x20;
3235     	np->sv_stest4	= INB(nc_stest4);
3236     
3237     	/*
3238     	**	Wide ?
3239     	*/
3240     
3241     	np->maxwide	= (np->features & FE_WIDE)? 1 : 0;
3242     
3243      	/*
3244     	 *  Guess the frequency of the chip's clock.
3245     	 */
3246     	if	(np->features & (FE_ULTRA3 | FE_ULTRA2))
3247     		np->clock_khz = 160000;
3248     	else if	(np->features & FE_ULTRA)
3249     		np->clock_khz = 80000;
3250     	else
3251     		np->clock_khz = 40000;
3252     
3253     	/*
3254     	 *  Get the clock multiplier factor.
3255      	 */
3256     	if	(np->features & FE_QUAD)
3257     		np->multiplier	= 4;
3258     	else if	(np->features & FE_DBLR)
3259     		np->multiplier	= 2;
3260     	else
3261     		np->multiplier	= 1;
3262     
3263     	/*
3264     	 *  Measure SCSI clock frequency for chips 
3265     	 *  it may vary from assumed one.
3266     	 */
3267     	if (np->features & FE_VARCLK)
3268     		ncr_getclock(np, np->multiplier);
3269     
3270     	/*
3271     	 * Divisor to be used for async (timer pre-scaler).
3272     	 */
3273     	i = np->clock_divn - 1;
3274     	while (--i >= 0) {
3275     		if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
3276     			++i;
3277     			break;
3278     		}
3279     	}
3280     	np->rv_scntl3 = i+1;
3281     
3282     	/*
3283     	 * Minimum synchronous period factor supported by the chip.
3284     	 * Btw, 'period' is in tenths of nanoseconds.
3285     	 */
3286     
3287     	period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
3288     	if	(period <= 250)		np->minsync = 10;
3289     	else if	(period <= 303)		np->minsync = 11;
3290     	else if	(period <= 500)		np->minsync = 12;
3291     	else				np->minsync = (period + 40 - 1) / 40;
3292     
3293     	/*
3294     	 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
3295     	 */
3296     
3297     	if	(np->minsync < 25 && !(np->features & (FE_ULTRA|FE_ULTRA2)))
3298     		np->minsync = 25;
3299     	else if	(np->minsync < 12 && !(np->features & FE_ULTRA2))
3300     		np->minsync = 12;
3301     
3302     	/*
3303     	 * Maximum synchronous period factor supported by the chip.
3304     	 */
3305     
3306     	period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
3307     	np->maxsync = period > 2540 ? 254 : period / 10;
3308     
3309     	/*
3310     	**	Prepare initial value of other IO registers
3311     	*/
3312     #if defined SCSI_NCR_TRUST_BIOS_SETTING
3313     	np->rv_scntl0	= np->sv_scntl0;
3314     	np->rv_dmode	= np->sv_dmode;
3315     	np->rv_dcntl	= np->sv_dcntl;
3316     	np->rv_ctest3	= np->sv_ctest3;
3317     	np->rv_ctest4	= np->sv_ctest4;
3318     	np->rv_ctest5	= np->sv_ctest5;
3319     	burst_max	= burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
3320     #else
3321     
3322     	/*
3323     	**	Select burst length (dwords)
3324     	*/
3325     	burst_max	= driver_setup.burst_max;
3326     	if (burst_max == 255)
3327     		burst_max = burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
3328     	if (burst_max > 7)
3329     		burst_max = 7;
3330     	if (burst_max > np->maxburst)
3331     		burst_max = np->maxburst;
3332     
3333     	/*
3334     	**	Select all supported special features
3335     	*/
3336     	if (np->features & FE_ERL)
3337     		np->rv_dmode	|= ERL;		/* Enable Read Line */
3338     	if (np->features & FE_BOF)
3339     		np->rv_dmode	|= BOF;		/* Burst Opcode Fetch */
3340     	if (np->features & FE_ERMP)
3341     		np->rv_dmode	|= ERMP;	/* Enable Read Multiple */
3342     	if (np->features & FE_PFEN)
3343     		np->rv_dcntl	|= PFEN;	/* Prefetch Enable */
3344     	if (np->features & FE_CLSE)
3345     		np->rv_dcntl	|= CLSE;	/* Cache Line Size Enable */
3346     	if (np->features & FE_WRIE)
3347     		np->rv_ctest3	|= WRIE;	/* Write and Invalidate */
3348     	if (np->features & FE_DFS)
3349     		np->rv_ctest5	|= DFS;		/* Dma Fifo Size */
3350     
3351     	/*
3352     	**	Select some other
3353     	*/
3354     	if (driver_setup.master_parity)
3355     		np->rv_ctest4	|= MPEE;	/* Master parity checking */
3356     	if (driver_setup.scsi_parity)
3357     		np->rv_scntl0	|= 0x0a;	/*  full arb., ena parity, par->ATN  */
3358     
3359     #ifdef SCSI_NCR_NVRAM_SUPPORT
3360     	/*
3361     	**	Get parity checking, host ID and verbose mode from NVRAM
3362     	**/
3363     	if (nvram) {
3364     		switch(nvram->type) {
3365     		case SCSI_NCR_TEKRAM_NVRAM:
3366     			np->myaddr = nvram->data.Tekram.host_id & 0x0f;
3367     			break;
3368     		case SCSI_NCR_SYMBIOS_NVRAM:
3369     			if (!(nvram->data.Symbios.flags & SYMBIOS_PARITY_ENABLE))
3370     				np->rv_scntl0  &= ~0x0a;
3371     			np->myaddr = nvram->data.Symbios.host_id & 0x0f;
3372     			if (nvram->data.Symbios.flags & SYMBIOS_VERBOSE_MSGS)
3373     				np->verbose += 1;
3374     			break;
3375     		}
3376     	}
3377     #endif
3378     	/*
3379     	**  Get SCSI addr of host adapter (set by bios?).
3380     	*/
3381     	if (np->myaddr == 255) {
3382     		np->myaddr = INB(nc_scid) & 0x07;
3383     		if (!np->myaddr)
3384     			np->myaddr = SCSI_NCR_MYADDR;
3385     	}
3386     
3387     #endif /* SCSI_NCR_TRUST_BIOS_SETTING */
3388     
3389     	/*
3390     	 *	Prepare initial io register bits for burst length
3391     	 */
3392     	ncr_init_burst(np, burst_max);
3393     
3394     	/*
3395     	**	Set SCSI BUS mode.
3396     	**
3397     	**	- ULTRA2 chips (895/895A/896) report the current 
3398     	**	  BUS mode through the STEST4 IO register.
3399     	**	- For previous generation chips (825/825A/875), 
3400     	**	  user has to tell us how to check against HVD, 
3401     	**	  since a 100% safe algorithm is not possible.
3402     	*/
3403     	np->scsi_mode = SMODE_SE;
3404     	if	(np->features & FE_ULTRA2)
3405     		np->scsi_mode = (np->sv_stest4 & SMODE);
3406     	else if	(np->features & FE_DIFF) {
3407     		switch(driver_setup.diff_support) {
3408     		case 4:	/* Trust previous settings if present, then GPIO3 */
3409     			if (np->sv_scntl3) {
3410     				if (np->sv_stest2 & 0x20)
3411     					np->scsi_mode = SMODE_HVD;
3412     				break;
3413     			}
3414     		case 3:	/* SYMBIOS controllers report HVD through GPIO3 */
3415     			if (nvram && nvram->type != SCSI_NCR_SYMBIOS_NVRAM)
3416     				break;
3417     			if (INB(nc_gpreg) & 0x08)
3418     				break;
3419     		case 2:	/* Set HVD unconditionally */
3420     			np->scsi_mode = SMODE_HVD;
3421     		case 1:	/* Trust previous settings for HVD */
3422     			if (np->sv_stest2 & 0x20)
3423     				np->scsi_mode = SMODE_HVD;
3424     			break;
3425     		default:/* Don't care about HVD */	
3426     			break;
3427     		}
3428     	}
3429     	if (np->scsi_mode == SMODE_HVD)
3430     		np->rv_stest2 |= 0x20;
3431     
3432     	/*
3433     	**	Set LED support from SCRIPTS.
3434     	**	Ignore this feature for boards known to use a 
3435     	**	specific GPIO wiring and for the 895A or 896 
3436     	**	that drive the LED directly.
3437     	**	Also probe initial setting of GPIO0 as output.
3438     	*/
3439     	if ((driver_setup.led_pin ||
3440     	     (nvram && nvram->type == SCSI_NCR_SYMBIOS_NVRAM)) &&
3441     	    !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
3442     		np->features |= FE_LED0;
3443     
3444     	/*
3445     	**	Set irq mode.
3446     	*/
3447     	switch(driver_setup.irqm & 3) {
3448     	case 2:
3449     		np->rv_dcntl	|= IRQM;
3450     		break;
3451     	case 1:
3452     		np->rv_dcntl	|= (np->sv_dcntl & IRQM);
3453     		break;
3454     	default:
3455     		break;
3456     	}
3457     
3458     	/*
3459     	**	Configure targets according to driver setup.
3460     	**	If NVRAM present get targets setup from NVRAM.
3461     	**	Allow to override sync, wide and NOSCAN from 
3462     	**	boot command line.
3463     	*/
3464     	for (i = 0 ; i < MAX_TARGET ; i++) {
3465     		tcb_p tp = &np->target[i];
3466     
3467     		tp->usrsync = 255;
3468     #ifdef SCSI_NCR_NVRAM_SUPPORT
3469     		if (nvram) {
3470     			switch(nvram->type) {
3471     			case SCSI_NCR_TEKRAM_NVRAM:
3472     				ncr_Tekram_setup_target(np, i, &nvram->data.Tekram);
3473     				break;
3474     			case SCSI_NCR_SYMBIOS_NVRAM:
3475     				ncr_Symbios_setup_target(np, i, &nvram->data.Symbios);
3476     				break;
3477     			}
3478     			if (driver_setup.use_nvram & 0x2)
3479     				tp->usrsync = driver_setup.default_sync;
3480     			if (driver_setup.use_nvram & 0x4)
3481     				tp->usrwide = driver_setup.max_wide;
3482     			if (driver_setup.use_nvram & 0x8)
3483     				tp->usrflag &= ~UF_NOSCAN;
3484     		}
3485     		else {
3486     #else
3487     		if (1) {
3488     #endif
3489     			tp->usrsync = driver_setup.default_sync;
3490     			tp->usrwide = driver_setup.max_wide;
3491     			tp->usrtags = MAX_TAGS;
3492     			if (!driver_setup.disconnection)
3493     				np->target[i].usrflag = UF_NODISC;
3494     		}
3495     	}
3496     
3497     	/*
3498     	**	Announce all that stuff to user.
3499     	*/
3500     
3501     	i = nvram ? nvram->type : 0;
3502     	printk(KERN_INFO "%s: %sID %d, Fast-%d%s%s\n", ncr_name(np),
3503     		i  == SCSI_NCR_SYMBIOS_NVRAM ? "Symbios format NVRAM, " :
3504     		(i == SCSI_NCR_TEKRAM_NVRAM  ? "Tekram format NVRAM, " : ""),
3505     		np->myaddr,
3506     		np->minsync < 12 ? 40 : (np->minsync < 25 ? 20 : 10),
3507     		(np->rv_scntl0 & 0xa)	? ", Parity Checking"	: ", NO Parity",
3508     		(np->rv_stest2 & 0x20)	? ", Differential"	: "");
3509     
3510     	if (bootverbose > 1) {
3511     		printk (KERN_INFO "%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
3512     			"(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
3513     			ncr_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
3514     			np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
3515     
3516     		printk (KERN_INFO "%s: final   SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
3517     			"(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
3518     			ncr_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
3519     			np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
3520     	}
3521     
3522     	if (bootverbose && np->paddr2)
3523     		printk (KERN_INFO "%s: on-chip RAM at 0x%lx\n",
3524     			ncr_name(np), np->paddr2);
3525     
3526     	return 0;
3527     }
3528     
3529     /*
3530     **	Host attach and initialisations.
3531     **
3532     **	Allocate host data and ncb structure.
3533     **	Request IO region and remap MMIO region.
3534     **	Do chip initialization.
3535     **	If all is OK, install interrupt handling and
3536     **	start the timer daemon.
3537     */
3538     
3539     static int __init 
3540     ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device)
3541     {
3542             struct host_data *host_data;
3543     	ncb_p np = 0;
3544             struct Scsi_Host *instance = 0;
3545     	u_long flags = 0;
3546     	ncr_nvram *nvram = device->nvram;
3547     	int i;
3548     
3549     	printk(KERN_INFO "ncr53c%s-%d: rev 0x%x on pci bus %d device %d function %d "
3550     #ifdef __sparc__
3551     		"irq %s\n",
3552     #else
3553     		"irq %d\n",
3554     #endif
3555     		device->chip.name, unit, device->chip.revision_id,
3556     		device->slot.bus, (device->slot.device_fn & 0xf8) >> 3,
3557     		device->slot.device_fn & 7,
3558     #ifdef __sparc__
3559     		__irq_itoa(device->slot.irq));
3560     #else
3561     		device->slot.irq);
3562     #endif
3563     
3564     	/*
3565     	**	Allocate host_data structure
3566     	*/
3567             if (!(instance = scsi_register(tpnt, sizeof(*host_data))))
3568     	        goto attach_error;
3569     	host_data = (struct host_data *) instance->hostdata;
3570     
3571     	/*
3572     	**	Allocate the host control block.
3573     	*/
3574     	np = __m_calloc_dma(device->pdev, sizeof(struct ncb), "NCB");
3575     	if (!np)
3576     		goto attach_error;
3577     	NCR_INIT_LOCK_NCB(np);
3578     	np->pdev  = device->pdev;
3579     	np->p_ncb = vtobus(np);
3580     	host_data->ncb = np;
3581     
3582     	/*
3583     	**	Allocate the default CCB.
3584     	*/
3585     	np->ccb = (ccb_p) m_calloc_dma(sizeof(struct ccb), "CCB");
3586     	if (!np->ccb)
3587     		goto attach_error;
3588     
3589     	/*
3590     	**	Store input informations in the host data structure.
3591     	*/
3592     	strncpy(np->chip_name, device->chip.name, sizeof(np->chip_name) - 1);
3593     	np->unit	= unit;
3594     	np->verbose	= driver_setup.verbose;
3595     	sprintf(np->inst_name, "ncr53c%s-%d", np->chip_name, np->unit);
3596     	np->device_id	= device->chip.device_id;
3597     	np->revision_id	= device->chip.revision_id;
3598     	np->bus		= device->slot.bus;
3599     	np->device_fn	= device->slot.device_fn;
3600     	np->features	= device->chip.features;
3601     	np->clock_divn	= device->chip.nr_divisor;
3602     	np->maxoffs	= device->chip.offset_max;
3603     	np->maxburst	= device->chip.burst_max;
3604     	np->myaddr	= device->host_id;
3605     
3606     	/*
3607     	**	Allocate SCRIPTS areas.
3608     	*/
3609     	np->script0  = (struct script *)
3610     			m_calloc_dma(sizeof(struct script), "SCRIPT");
3611     	if (!np->script0)
3612     		goto attach_error;
3613     	np->scripth0  = (struct scripth *)
3614     			m_calloc_dma(sizeof(struct scripth), "SCRIPTH");
3615     	if (!np->scripth0)
3616     		goto attach_error;
3617     
3618     	/*
3619     	**    Initialize timer structure
3620             **
3621             */
3622     	init_timer(&np->timer);
3623     	np->timer.data     = (unsigned long) np;
3624     	np->timer.function = ncr53c8xx_timeout;
3625     
3626     	/*
3627     	**	Try to map the controller chip to
3628     	**	virtual and physical memory.
3629     	*/
3630     
3631     	np->paddr	= device->slot.base;
3632     	np->paddr2	= (np->features & FE_RAM)? device->slot.base_2 : 0;
3633     
3634     #ifndef SCSI_NCR_IOMAPPED
3635     	np->vaddr = remap_pci_mem(device->slot.base_c, (u_long) 128);
3636     	if (!np->vaddr) {
3637     		printk(KERN_ERR
3638     			"%s: can't map memory mapped IO region\n",ncr_name(np));
3639     		goto attach_error;
3640     	}
3641     	else
3642     		if (bootverbose > 1)
3643     			printk(KERN_INFO
3644     				"%s: using memory mapped IO at virtual address 0x%lx\n", ncr_name(np), (u_long) np->vaddr);
3645     
3646     	/*
3647     	**	Make the controller's registers available.
3648     	**	Now the INB INW INL OUTB OUTW OUTL macros
3649     	**	can be used safely.
3650     	*/
3651     
3652     	np->reg = (struct ncr_reg*) np->vaddr;
3653     
3654     #endif /* !defined SCSI_NCR_IOMAPPED */
3655     
3656     	/*
3657     	**	Try to map the controller chip into iospace.
3658     	*/
3659     
3660     	request_region(device->slot.io_port, 128, "ncr53c8xx");
3661     	np->base_io = device->slot.io_port;
3662     
3663     #ifdef SCSI_NCR_NVRAM_SUPPORT
3664     	if (nvram) {
3665     		switch(nvram->type) {
3666     		case SCSI_NCR_SYMBIOS_NVRAM:
3667     #ifdef SCSI_NCR_DEBUG_NVRAM
3668     			ncr_display_Symbios_nvram(&nvram->data.Symbios);
3669     #endif
3670     			break;
3671     		case SCSI_NCR_TEKRAM_NVRAM:
3672     #ifdef SCSI_NCR_DEBUG_NVRAM
3673     			ncr_display_Tekram_nvram(&nvram->data.Tekram);
3674     #endif
3675     			break;
3676     		default:
3677     			nvram = 0;
3678     #ifdef SCSI_NCR_DEBUG_NVRAM
3679     			printk(KERN_DEBUG "%s: NVRAM: None or invalid data.\n", ncr_name(np));
3680     #endif
3681     		}
3682     	}
3683     #endif
3684     
3685     	/*
3686     	**	Do chip dependent initialization.
3687     	*/
3688     	(void)ncr_prepare_setting(np, nvram);
3689     
3690     	if (np->paddr2 && sizeof(struct script) > 4096) {
3691     		np->paddr2 = 0;
3692     		printk(KERN_WARNING "%s: script too large, NOT using on chip RAM.\n",
3693     			ncr_name(np));
3694     	}
3695     
3696     	/*
3697     	**	Fill Linux host instance structure
3698     	*/
3699     	instance->max_channel	= 0;
3700     	instance->this_id       = np->myaddr;
3701     	instance->max_id	= np->maxwide ? 16 : 8;
3702     	instance->max_lun	= SCSI_NCR_MAX_LUN;
3703     #ifndef SCSI_NCR_IOMAPPED
3704     #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,29)
3705     	instance->base		= (unsigned long) np->reg;
3706     #else
3707     	instance->base		= (char *) np->reg;
3708     #endif
3709     #endif
3710     	instance->irq		= device->slot.irq;
3711     	instance->unique_id	= device->slot.io_port;
3712     	instance->io_port	= device->slot.io_port;
3713     	instance->n_io_port	= 128;
3714     	instance->dma_channel	= 0;
3715     	instance->cmd_per_lun	= MAX_TAGS;
3716     	instance->can_queue	= (MAX_START-4);
3717     	instance->select_queue_depths = ncr53c8xx_select_queue_depths;
3718     	scsi_set_pci_device(instance, device->pdev);
3719     
3720     #ifdef SCSI_NCR_INTEGRITY_CHECKING
3721     	np->check_integrity	  = 0;
3722     	instance->check_integrity = 0;
3723     
3724     #ifdef SCSI_NCR_ENABLE_INTEGRITY_CHECK
3725     	if ( !(driver_setup.bus_check & 0x04) ) {
3726     		np->check_integrity	  = 1;
3727     		instance->check_integrity = 1;
3728     	}
3729     #endif
3730     #endif
3731     	/*
3732     	**	Patch script to physical addresses
3733     	*/
3734     	ncr_script_fill (&script0, &scripth0);
3735     
3736     	np->scripth	= np->scripth0;
3737     	np->p_scripth	= vtobus(np->scripth);
3738     
3739     	np->p_script	= (np->paddr2) ?  np->paddr2 : vtobus(np->script0);
3740     
3741     	ncr_script_copy_and_bind (np, (ncrcmd *) &script0, (ncrcmd *) np->script0, sizeof(struct script));
3742     	ncr_script_copy_and_bind (np, (ncrcmd *) &scripth0, (ncrcmd *) np->scripth0, sizeof(struct scripth));
3743     	np->ccb->p_ccb		= vtobus (np->ccb);
3744     
3745     	/*
3746     	**    Patch the script for LED support.
3747     	*/
3748     
3749     	if (np->features & FE_LED0) {
3750     		np->script0->idle[0]  =
3751     				cpu_to_scr(SCR_REG_REG(gpreg, SCR_OR,  0x01));
3752     		np->script0->reselected[0] =
3753     				cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
3754     		np->script0->start[0] =
3755     				cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
3756     	}
3757     
3758     	/*
3759     	**	Look for the target control block of this nexus.
3760     	**	For i = 0 to 3
3761     	**		JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
3762     	*/
3763     	for (i = 0 ; i < 4 ; i++) {
3764     		np->jump_tcb[i].l_cmd   =
3765     				cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
3766     		np->jump_tcb[i].l_paddr =
3767     				cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_target));
3768     	}
3769     
3770     	/*
3771     	**	Reset chip.
3772     	*/
3773     
3774     	OUTB (nc_istat,  SRST);
3775     	UDELAY (100);
3776     	OUTB (nc_istat,  0   );
3777     
3778     	/*
3779     	**	Now check the cache handling of the pci chipset.
3780     	*/
3781     
3782     	if (ncr_snooptest (np)) {
3783     		printk (KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
3784     		goto attach_error;
3785     	};
3786     
3787     	/*
3788     	**	Install the interrupt handler.
3789     	*/
3790     
3791     	if (request_irq(device->slot.irq, ncr53c8xx_intr,
3792     			((driver_setup.irqm & 0x10) ? 0 : SA_SHIRQ) |
3793     #if LINUX_VERSION_CODE < LinuxVersionCode(2,2,0)
3794     			((driver_setup.irqm & 0x20) ? 0 : SA_INTERRUPT),
3795     #else
3796     			0,
3797     #endif
3798     			"ncr53c8xx", np)) {
3799     #ifdef __sparc__
3800     		printk(KERN_ERR "%s: request irq %s failure\n",
3801     			ncr_name(np), __irq_itoa(device->slot.irq));
3802     #else
3803     		printk(KERN_ERR "%s: request irq %d failure\n",
3804     			ncr_name(np), device->slot.irq);
3805     #endif
3806     		goto attach_error;
3807     	}
3808     
3809     	np->irq = device->slot.irq;
3810     
3811     	/*
3812     	**	Initialize the fixed part of the default ccb.
3813     	*/
3814     	ncr_init_ccb(np, np->ccb);
3815     
3816     	/*
3817     	**	After SCSI devices have been opened, we cannot
3818     	**	reset the bus safely, so we do it here.
3819     	**	Interrupt handler does the real work.
3820     	**	Process the reset exception,
3821     	**	if interrupts are not enabled yet.
3822     	**	Then enable disconnects.
3823     	*/
3824     	NCR_LOCK_NCB(np, flags);
3825     	if (ncr_reset_scsi_bus(np, 0, driver_setup.settle_delay) != 0) {
3826     		printk(KERN_ERR "%s: FATAL ERROR: CHECK SCSI BUS - CABLES, TERMINATION, DEVICE POWER etc.!\n", ncr_name(np));
3827     
3828     		NCR_UNLOCK_NCB(np, flags);
3829     		goto attach_error;
3830     	}
3831     	ncr_exception (np);
3832     
3833     	np->disc = 1;
3834     
3835     	/*
3836     	**	The middle-level SCSI driver does not
3837     	**	wait for devices to settle.
3838     	**	Wait synchronously if more than 2 seconds.
3839     	*/
3840     	if (driver_setup.settle_delay > 2) {
3841     		printk(KERN_INFO "%s: waiting %d seconds for scsi devices to settle...\n",
3842     			ncr_name(np), driver_setup.settle_delay);
3843     		MDELAY (1000 * driver_setup.settle_delay);
3844     	}
3845     
3846     	/*
3847     	**	Now let the generic SCSI driver
3848     	**	look for the SCSI devices on the bus ..
3849     	*/
3850     
3851     	/*
3852     	**	start the timeout daemon
3853     	*/
3854     	np->lasttime=0;
3855     	ncr_timeout (np);
3856     
3857     	/*
3858     	**  use SIMPLE TAG messages by default
3859     	*/
3860     #ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
3861     	np->order = M_SIMPLE_TAG;
3862     #endif
3863     
3864     	/*
3865     	**  Done.
3866     	*/
3867             if (!the_template) {
3868             	the_template = instance->hostt;
3869             	first_host = instance;
3870     	}
3871     
3872     	NCR_UNLOCK_NCB(np, flags);
3873     
3874     	return 0;
3875     
3876     attach_error:
3877     	if (!instance) return -1;
3878     	printk(KERN_INFO "%s: detaching...\n", ncr_name(np));
3879     	if (!np)
3880     		goto unregister;
3881     #ifndef SCSI_NCR_IOMAPPED
3882     	if (np->vaddr) {
3883     #ifdef DEBUG_NCR53C8XX
3884     		printk(KERN_DEBUG "%s: releasing memory mapped IO region %lx[%d]\n", ncr_name(np), (u_long) np->vaddr, 128);
3885     #endif
3886     		unmap_pci_mem((vm_offset_t) np->vaddr, (u_long) 128);
3887     	}
3888     #endif /* !SCSI_NCR_IOMAPPED */
3889     	if (np->base_io) {
3890     #ifdef DEBUG_NCR53C8XX
3891     		printk(KERN_DEBUG "%s: releasing IO region %x[%d]\n", ncr_name(np), np->base_io, 128);
3892     #endif
3893     		release_region(np->base_io, 128);
3894     	}
3895     	if (np->irq) {
3896     #ifdef DEBUG_NCR53C8XX
3897     #ifdef __sparc__
3898     	printk(KERN_INFO "%s: freeing irq %s\n", ncr_name(np),
3899     	       __irq_itoa(np->irq));
3900     #else
3901     	printk(KERN_INFO "%s: freeing irq %d\n", ncr_name(np), np->irq);
3902     #endif
3903     #endif
3904     		free_irq(np->irq, np);
3905     	}
3906     	if (np->scripth0)
3907     		m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
3908     	if (np->script0)
3909     		m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
3910     	if (np->ccb)
3911     		m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
3912     	m_free_dma(np, sizeof(struct ncb), "NCB");
3913     
3914     unregister:
3915     	scsi_unregister(instance);
3916     
3917             return -1;
3918      }
3919     
3920     
3921     /*==========================================================
3922     **
3923     **
3924     **	Done SCSI commands list management.
3925     **
3926     **	We donnot enter the scsi_done() callback immediately 
3927     **	after a command has been seen as completed but we 
3928     **	insert it into a list which is flushed outside any kind 
3929     **	of driver critical section.
3930     **	This allows to do minimal stuff under interrupt and 
3931     **	inside critical sections and to also avoid locking up 
3932     **	on recursive calls to driver entry points under SMP.
3933     **	In fact, the only kernel point which is entered by the 
3934     **	driver with a driver lock set is kmalloc(GFP_ATOMIC) 
3935     **	that shall not reenter the driver under any circumstances,
3936     **	AFAIK.
3937     **
3938     **==========================================================
3939     */
3940     static inline void ncr_queue_done_cmd(ncb_p np, Scsi_Cmnd *cmd)
3941     {
3942     	unmap_scsi_data(np, cmd);
3943     	cmd->host_scribble = (char *) np->done_list;
3944     	np->done_list = cmd;
3945     }
3946     
3947     static inline void ncr_flush_done_cmds(Scsi_Cmnd *lcmd)
3948     {
3949     	Scsi_Cmnd *cmd;
3950     
3951     	while (lcmd) {
3952     		cmd = lcmd;
3953     		lcmd = (Scsi_Cmnd *) cmd->host_scribble;
3954     		cmd->scsi_done(cmd);
3955     	}
3956     }
3957     
3958     /*==========================================================
3959     **
3960     **
3961     **	Prepare the next negotiation message for integrity check,
3962     **	if needed.
3963     **
3964     **	Fill in the part of message buffer that contains the 
3965     **	negotiation and the nego_status field of the CCB.
3966     **	Returns the size of the message in bytes.
3967     **
3968     **
3969     **==========================================================
3970     */
3971     
3972     #ifdef SCSI_NCR_INTEGRITY_CHECKING
3973     static int ncr_ic_nego(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd, u_char *msgptr)
3974     {
3975     	tcb_p tp = &np->target[cp->target];
3976     	int msglen = 0;
3977     	int nego = 0;
3978     	u_char no_increase;
3979     
3980     	if (tp->inq_done) {
3981     
3982     		if (!tp->ic_maximums_set) {
3983     			tp->ic_maximums_set = 1;
3984     
3985     			/* check target and host adapter capabilities */
3986     			if ( (tp->inq_byte7 & INQ7_WIDE16) && 
3987     					np->maxwide && tp->usrwide ) 
3988     				tp->ic_max_width = 1;
3989     			else
3990     				tp->ic_max_width = 0;
3991     
3992     			if ((tp->inq_byte7 & INQ7_SYNC) && tp->maxoffs) {
3993     				tp->ic_min_sync   = (tp->minsync < np->minsync) ?
3994     							np->minsync : tp->minsync;
3995     			}
3996     			else 
3997     				tp->ic_min_sync   = 255;
3998     			
3999     			tp->period   = 1;
4000     			tp->widedone = 1;
4001     		}
4002     
4003     		if (DEBUG_FLAGS & DEBUG_IC) {
4004     			printk("%s: cmd->ic_nego %d, 1st byte 0x%2X\n",
4005     				ncr_name(np), cmd->ic_nego, cmd->cmnd[0]);
4006     		}
4007     
4008     		/* First command from integrity check routine will request
4009     		 * a PPR message.  Disable.
4010     		 */
4011     		if ((cmd->ic_nego & NS_PPR) == NS_PPR)
4012     			cmd->ic_nego &= ~NS_PPR;
4013     		/* Previous command recorded a parity or an initiator
4014     		 * detected error condition. Force bus to narrow for this
4015     		 * target. Clear flag. Negotation on request sense.
4016     		 * Note: kernel forces 2 bus resets :o( but clears itself out.
4017     		 * Minor bug? in scsi_obsolete.c (ugly)
4018     		 */
4019     		if (np->check_integ_par) {
4020     			printk("%s: Parity Error. Target set to narrow.\n",
4021     				ncr_name(np));
4022     			tp->ic_max_width = 0;
4023     			tp->widedone = tp->period = 0;
4024     		}
4025     		
4026     		/* In case of a bus reset, ncr_negotiate will reset 
4027                      * the flags tp->widedone and tp->period to 0, forcing
4028     		 * a new negotiation. 
4029     		 */
4030     		no_increase = 0;
4031     		if (tp->widedone == 0) {
4032     			cmd->ic_nego = NS_WIDE;
4033     			tp->widedone = 1;
4034     			no_increase = 1;
4035     		}
4036     		else if (tp->period == 0) {
4037     			cmd->ic_nego = NS_SYNC;
4038     			tp->period = 1;
4039     			no_increase = 1;
4040     		}
4041     			
4042     		switch (cmd->ic_nego) {
4043     		case NS_WIDE:
4044     			/*
4045     			**	negotiate wide transfers ?
4046     			**	Do NOT negotiate if device only supports
4047     			**	narrow.	
4048     			*/
4049     			if (tp->ic_max_width | np->check_integ_par) {
4050     				nego = NS_WIDE;
4051     
4052     				msgptr[msglen++] = M_EXTENDED;
4053     				msgptr[msglen++] = 2;
4054     				msgptr[msglen++] = M_X_WIDE_REQ;
4055     				msgptr[msglen++] = cmd->ic_nego_width & tp->ic_max_width;
4056     			}
4057     			else
4058     				cmd->ic_nego_width &= tp->ic_max_width;
4059                   
4060     			break;
4061     
4062     		case NS_SYNC:
4063     			/*
4064     			**	negotiate synchronous transfers?
4065     			**	Target must support sync transfers.
4066     			**
4067     			**	If period becomes longer than max, reset to async
4068     			*/
4069     
4070     			if (tp->inq_byte7 & INQ7_SYNC) {
4071     
4072     				nego = NS_SYNC;
4073     
4074     				msgptr[msglen++] = M_EXTENDED;
4075     				msgptr[msglen++] = 3;
4076     				msgptr[msglen++] = M_X_SYNC_REQ;
4077     
4078     				switch (cmd->ic_nego_sync) {
4079     				case 2: /* increase the period */
4080     					if (!no_increase) {
4081     					  if (tp->ic_min_sync <= 0x0A)
4082     					      tp->ic_min_sync = 0x0C;
4083     					  else if (tp->ic_min_sync <= 0x0C)
4084     					      tp->ic_min_sync = 0x19;
4085     					  else if (tp->ic_min_sync <= 0x19)
4086     					      tp->ic_min_sync *= 2;
4087     					  else {
4088     						tp->ic_min_sync = 255;
4089     						cmd->ic_nego_sync = 0;
4090     						tp->maxoffs = 0;
4091     					   }
4092     					}
4093     					msgptr[msglen++] = tp->maxoffs?tp->ic_min_sync:0;
4094     					msgptr[msglen++] = tp->maxoffs;
4095     					break;
4096     
4097     				case 1: /* nego. to maximum */
4098     					msgptr[msglen++] = tp->maxoffs?tp->ic_min_sync:0;
4099     					msgptr[msglen++] = tp->maxoffs;
4100     					break;
4101     
4102     				case 0:	/* nego to async */
4103     				default:
4104     					msgptr[msglen++] = 0;
4105     					msgptr[msglen++] = 0;
4106     					break;
4107     				};
4108     			}
4109     			else
4110     				cmd->ic_nego_sync = 0;
4111     			break;
4112     
4113     		case NS_NOCHANGE:
4114     		default:
4115     			break;
4116     		};
4117     	};
4118     
4119     	cp->nego_status = nego;
4120     	np->check_integ_par = 0;
4121     
4122     	if (nego) {
4123     		tp->nego_cp = cp;
4124     		if (DEBUG_FLAGS & DEBUG_NEGO) {
4125     			ncr_print_msg(cp, nego == NS_WIDE ?
4126     			  "wide/narrow msgout": "sync/async msgout", msgptr);
4127     		};
4128     	};
4129     
4130     	return msglen;
4131     }
4132     #endif /* SCSI_NCR_INTEGRITY_CHECKING */
4133     
4134     /*==========================================================
4135     **
4136     **
4137     **	Prepare the next negotiation message if needed.
4138     **
4139     **	Fill in the part of message buffer that contains the 
4140     **	negotiation and the nego_status field of the CCB.
4141     **	Returns the size of the message in bytes.
4142     **
4143     **
4144     **==========================================================
4145     */
4146     
4147     
4148     static int ncr_prepare_nego(ncb_p np, ccb_p cp, u_char *msgptr)
4149     {
4150     	tcb_p tp = &np->target[cp->target];
4151     	int msglen = 0;
4152     	int nego = 0;
4153     
4154     	if (tp->inq_done) {
4155     
4156     		/*
4157     		**	negotiate wide transfers ?
4158     		*/
4159     
4160     		if (!tp->widedone) {
4161     			if (tp->inq_byte7 & INQ7_WIDE16) {
4162     				nego = NS_WIDE;
4163     #ifdef SCSI_NCR_INTEGRITY_CHECKING
4164     				if (tp->ic_done)
4165     		       			 tp->usrwide &= tp->ic_max_width;
4166     #endif
4167     			} else
4168     				tp->widedone=1;
4169     
4170     		};
4171     
4172     		/*
4173     		**	negotiate synchronous transfers?
4174     		*/
4175     
4176     		if (!nego && !tp->period) {
4177     			if (tp->inq_byte7 & INQ7_SYNC) {
4178     				nego = NS_SYNC;
4179     #ifdef SCSI_NCR_INTEGRITY_CHECKING
4180     				if ((tp->ic_done) &&
4181     				 	      (tp->minsync < tp->ic_min_sync))
4182     		       			 tp->minsync = tp->ic_min_sync;
4183     #endif
4184     			} else {
4185     				tp->period  =0xffff;
4186     				PRINT_TARGET(np, cp->target);
4187     				printk ("target did not report SYNC.\n");
4188     			};
4189     		};
4190     	};
4191     
4192     	switch (nego) {
4193     	case NS_SYNC:
4194     		msgptr[msglen++] = M_EXTENDED;
4195     		msgptr[msglen++] = 3;
4196     		msgptr[msglen++] = M_X_SYNC_REQ;
4197     		msgptr[msglen++] = tp->maxoffs ? tp->minsync : 0;
4198     		msgptr[msglen++] = tp->maxoffs;
4199     		break;
4200     	case NS_WIDE:
4201     		msgptr[msglen++] = M_EXTENDED;
4202     		msgptr[msglen++] = 2;
4203     		msgptr[msglen++] = M_X_WIDE_REQ;
4204     		msgptr[msglen++] = tp->usrwide;
4205     		break;
4206     	};
4207     
4208     	cp->nego_status = nego;
4209     
4210     	if (nego) {
4211     		tp->nego_cp = cp;
4212     		if (DEBUG_FLAGS & DEBUG_NEGO) {
4213     			ncr_print_msg(cp, nego == NS_WIDE ?
4214     					  "wide msgout":"sync_msgout", msgptr);
4215     		};
4216     	};
4217     
4218     	return msglen;
4219     }
4220     
4221     
4222     
4223     /*==========================================================
4224     **
4225     **
4226     **	Start execution of a SCSI command.
4227     **	This is called from the generic SCSI driver.
4228     **
4229     **
4230     **==========================================================
4231     */
4232     static int ncr_queue_command (ncb_p np, Scsi_Cmnd *cmd)
4233     {
4234     /*	Scsi_Device        *device    = cmd->device; */
4235     	tcb_p tp                      = &np->target[cmd->target];
4236     	lcb_p lp		      = tp->lp[cmd->lun];
4237     	ccb_p cp;
4238     
4239     	int	segments;
4240     	u_char	idmsg, *msgptr;
4241     	u_int  msglen;
4242     	int	direction;
4243     	u_int32	lastp, goalp;
4244     
4245     	/*---------------------------------------------
4246     	**
4247     	**      Some shortcuts ...
4248     	**
4249     	**---------------------------------------------
4250     	*/
4251     	if ((cmd->target == np->myaddr	  ) ||
4252     		(cmd->target >= MAX_TARGET) ||
4253     		(cmd->lun    >= MAX_LUN   )) {
4254     		return(DID_BAD_TARGET);
4255             }
4256     
4257     	/*---------------------------------------------
4258     	**
4259     	**	Complete the 1st TEST UNIT READY command
4260     	**	with error condition if the device is 
4261     	**	flagged NOSCAN, in order to speed up 
4262     	**	the boot.
4263     	**
4264     	**---------------------------------------------
4265     	*/
4266     	if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12) && 
4267     	    (tp->usrflag & UF_NOSCAN)) {
4268     		tp->usrflag &= ~UF_NOSCAN;
4269     		return DID_BAD_TARGET;
4270     	}
4271     
4272     	if (DEBUG_FLAGS & DEBUG_TINY) {
4273     		PRINT_ADDR(cmd);
4274     		printk ("CMD=%x ", cmd->cmnd[0]);
4275     	}
4276     
4277     	/*---------------------------------------------------
4278     	**
4279     	**	Assign a ccb / bind cmd.
4280     	**	If resetting, shorten settle_time if necessary
4281     	**	in order to avoid spurious timeouts.
4282     	**	If resetting or no free ccb,
4283     	**	insert cmd into the waiting list.
4284     	**
4285     	**----------------------------------------------------
4286     	*/
4287     	if (np->settle_time && cmd->timeout_per_command >= HZ) {
4288     		u_long tlimit = ktime_get(cmd->timeout_per_command - HZ);
4289     		if (ktime_dif(np->settle_time, tlimit) > 0)
4290     			np->settle_time = tlimit;
4291     	}
4292     
4293             if (np->settle_time || !(cp=ncr_get_ccb (np, cmd->target, cmd->lun))) {
4294     		insert_into_waiting_list(np, cmd);
4295     		return(DID_OK);
4296     	}
4297     	cp->cmd = cmd;
4298     
4299     	/*---------------------------------------------------
4300     	**
4301     	**	Enable tagged queue if asked by scsi ioctl
4302     	**
4303     	**----------------------------------------------------
4304     	*/
4305     #if 0	/* This stuff was only useful for linux-1.2.13 */
4306     	if (lp && !lp->numtags && cmd->device && cmd->device->tagged_queue) {
4307     		lp->numtags = tp->usrtags;
4308     		ncr_setup_tags (np, cmd->target, cmd->lun);
4309     	}
4310     #endif
4311     
4312     	/*----------------------------------------------------
4313     	**
4314     	**	Build the identify / tag / sdtr message
4315     	**
4316     	**----------------------------------------------------
4317     	*/
4318     
4319     	idmsg = M_IDENTIFY | cmd->lun;
4320     
4321     	if (cp ->tag != NO_TAG ||
4322     		(cp != np->ccb && np->disc && !(tp->usrflag & UF_NODISC)))
4323     		idmsg |= 0x40;
4324     
4325     	msgptr = cp->scsi_smsg;
4326     	msglen = 0;
4327     	msgptr[msglen++] = idmsg;
4328     
4329     	if (cp->tag != NO_TAG) {
4330     		char order = np->order;
4331     
4332     		/*
4333     		**	Force ordered tag if necessary to avoid timeouts 
4334     		**	and to preserve interactivity.
4335     		*/
4336     		if (lp && ktime_exp(lp->tags_stime)) {
4337     			if (lp->tags_smap) {
4338     				order = M_ORDERED_TAG;
4339     				if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){ 
4340     					PRINT_ADDR(cmd);
4341     					printk("ordered tag forced.\n");
4342     				}
4343     			}
4344     			lp->tags_stime = ktime_get(3*HZ);
4345     			lp->tags_smap = lp->tags_umap;
4346     		}
4347     
4348     		if (order == 0) {
4349     			/*
4350     			**	Ordered write ops, unordered read ops.
4351     			*/
4352     			switch (cmd->cmnd[0]) {
4353     			case 0x08:  /* READ_SMALL (6) */
4354     			case 0x28:  /* READ_BIG  (10) */
4355     			case 0xa8:  /* READ_HUGE (12) */
4356     				order = M_SIMPLE_TAG;
4357     				break;
4358     			default:
4359     				order = M_ORDERED_TAG;
4360     			}
4361     		}
4362     		msgptr[msglen++] = order;
4363     		/*
4364     		**	Actual tags are numbered 1,3,5,..2*MAXTAGS+1,
4365     		**	since we may have to deal with devices that have 
4366     		**	problems with #TAG 0 or too great #TAG numbers.
4367     		*/
4368     		msgptr[msglen++] = (cp->tag << 1) + 1;
4369     	}
4370     
4371     	/*----------------------------------------------------
4372     	**
4373     	**	Build the data descriptors
4374     	**
4375     	**----------------------------------------------------
4376     	*/
4377     
4378     	direction = scsi_data_direction(cmd);
4379     	if (direction != SCSI_DATA_NONE) {
4380     		segments = ncr_scatter (np, cp, cp->cmd);
4381     		if (segments < 0) {
4382     			ncr_free_ccb(np, cp);
4383     			return(DID_ERROR);
4384     		}
4385     	}
4386     	else {
4387     		cp->data_len = 0;
4388     		segments = 0;
4389     	}
4390     
4391     	/*---------------------------------------------------
4392     	**
4393     	**	negotiation required?
4394     	**
4395     	**	(nego_status is filled by ncr_prepare_nego())
4396     	**
4397     	**---------------------------------------------------
4398     	*/
4399     
4400     	cp->nego_status = 0;
4401     
4402     #ifdef SCSI_NCR_INTEGRITY_CHECKING
4403     	if ((np->check_integrity && tp->ic_done) || !np->check_integrity) {
4404     		 if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
4405     			msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
4406     		 }
4407     	}
4408     	else if (np->check_integrity && (cmd->ic_in_progress)) { 
4409     		msglen += ncr_ic_nego (np, cp, cmd, msgptr + msglen);
4410             }
4411     	else if (np->check_integrity && cmd->ic_complete) {
4412                     /*
4413                      * Midlayer signal to the driver that all of the scsi commands
4414                      * for the integrity check have completed. Save the negotiated
4415                      * parameters (extracted from sval and wval). 
4416                      */ 
4417     
4418     		{
4419     			u_char idiv;
4420     			idiv = (tp->wval>>4) & 0x07;
4421     			if ((tp->sval&0x1f) && idiv )
4422     				tp->period = (((tp->sval>>5)+4)  
4423     						*div_10M[idiv-1])/np->clock_khz;
4424     			else
4425     				tp->period = 0xffff;
4426     		}
4427     		/*
4428     		 * tp->period contains 10 times the transfer period, 
4429     		 * which itself is 4 * the requested negotiation rate.
4430     		 */
4431     		if	(tp->period <= 250)	tp->ic_min_sync = 10;
4432     		else if	(tp->period <= 303)	tp->ic_min_sync = 11;
4433     		else if	(tp->period <= 500)	tp->ic_min_sync = 12;
4434     		else				
4435     				tp->ic_min_sync = (tp->period + 40 - 1) / 40;
4436     
4437     
4438     		/*
4439                      * Negotiation for this target it complete.
4440                      */
4441     		tp->ic_max_width =  (tp->wval & EWS) ? 1: 0;
4442     		tp->ic_done = 1;
4443     		tp->widedone = 1;
4444     
4445     		printk("%s: Integrity Check Complete: \n", ncr_name(np)); 
4446     
4447     		printk("%s: %s %s SCSI", ncr_name(np), 
4448     				(tp->sval&0x1f)?"SYNC":"ASYNC",
4449     				tp->ic_max_width?"WIDE":"NARROW");
4450     
4451     		if (tp->sval&0x1f) {
4452                             u_long mbs = 10000 * (tp->ic_max_width + 1);
4453     
4454                             printk(" %d.%d  MB/s", 
4455                                     (int) (mbs / tp->period), (int) (mbs % tp->period));
4456     
4457     			printk(" (%d ns, %d offset)\n", 
4458     				  tp->period/10, tp->sval&0x1f);
4459     		}
4460     		else
4461                             printk(" %d MB/s. \n ", (tp->ic_max_width+1)*5);
4462             }
4463     #else
4464     	if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
4465     		msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
4466     	}
4467     #endif /* SCSI_NCR_INTEGRITY_CHECKING */
4468     
4469     	/*----------------------------------------------------
4470     	**
4471     	**	Determine xfer direction.
4472     	**
4473     	**----------------------------------------------------
4474     	*/
4475     	if (!cp->data_len)
4476     		direction = SCSI_DATA_NONE;
4477     
4478     	/*
4479     	**	If data direction is UNKNOWN, speculate DATA_READ 
4480     	**	but prepare alternate pointers for WRITE in case 
4481     	**	of our speculation will be just wrong.
4482     	**	SCRIPTS will swap values if needed.
4483     	*/
4484     	switch(direction) {
4485     	case SCSI_DATA_UNKNOWN:
4486     	case SCSI_DATA_WRITE:
4487     		goalp = NCB_SCRIPT_PHYS (np, data_out2) + 8;
4488     		if (segments <= MAX_SCATTERL)
4489     			lastp = goalp - 8 - (segments * 16);
4490     		else {
4491     			lastp = NCB_SCRIPTH_PHYS (np, hdata_out2);
4492     			lastp -= (segments - MAX_SCATTERL) * 16;
4493     		}
4494     		if (direction != SCSI_DATA_UNKNOWN)
4495     			break;
4496     		cp->phys.header.wgoalp	= cpu_to_scr(goalp);
4497     		cp->phys.header.wlastp	= cpu_to_scr(lastp);
4498     		/* fall through */
4499     	case SCSI_DATA_READ:
4500     		goalp = NCB_SCRIPT_PHYS (np, data_in2) + 8;
4501     		if (segments <= MAX_SCATTERL)
4502     			lastp = goalp - 8 - (segments * 16);
4503     		else {
4504     			lastp = NCB_SCRIPTH_PHYS (np, hdata_in2);
4505     			lastp -= (segments - MAX_SCATTERL) * 16;
4506     		}
4507     		break;
4508     	default:
4509     	case SCSI_DATA_NONE:
4510     		lastp = goalp = NCB_SCRIPT_PHYS (np, no_data);
4511     		break;
4512     	}
4513     
4514     	/*
4515     	**	Set all pointers values needed by SCRIPTS.
4516     	**	If direction is unknown, start at data_io.
4517     	*/
4518     	cp->phys.header.lastp = cpu_to_scr(lastp);
4519     	cp->phys.header.goalp = cpu_to_scr(goalp);
4520     
4521     	if (direction == SCSI_DATA_UNKNOWN)
4522     		cp->phys.header.savep = 
4523     			cpu_to_scr(NCB_SCRIPTH_PHYS (np, data_io));
4524     	else
4525     		cp->phys.header.savep= cpu_to_scr(lastp);
4526     
4527     	/*
4528     	**	Save the initial data pointer in order to be able 
4529     	**	to redo the command.
4530     	*/
4531     	cp->startp = cp->phys.header.savep;
4532     
4533     	/*----------------------------------------------------
4534     	**
4535     	**	fill in ccb
4536     	**
4537     	**----------------------------------------------------
4538     	**
4539     	**
4540     	**	physical -> virtual backlink
4541     	**	Generic SCSI command
4542     	*/
4543     
4544     	/*
4545     	**	Startqueue
4546     	*/
4547     	cp->start.schedule.l_paddr   = cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
4548     	cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_dsa));
4549     	/*
4550     	**	select
4551     	*/
4552     	cp->phys.select.sel_id		= cmd->target;
4553     	cp->phys.select.sel_scntl3	= tp->wval;
4554     	cp->phys.select.sel_sxfer	= tp->sval;
4555     	/*
4556     	**	message
4557     	*/
4558     	cp->phys.smsg.addr		= cpu_to_scr(CCB_PHYS (cp, scsi_smsg));
4559     	cp->phys.smsg.size		= cpu_to_scr(msglen);
4560     
4561     	/*
4562     	**	command
4563     	*/
4564     	memcpy(cp->cdb_buf, cmd->cmnd, MIN(cmd->cmd_len, sizeof(cp->cdb_buf)));
4565     	cp->phys.cmd.addr		= cpu_to_scr(CCB_PHYS (cp, cdb_buf[0]));
4566     	cp->phys.cmd.size		= cpu_to_scr(cmd->cmd_len);
4567     
4568     	/*
4569     	**	status
4570     	*/
4571     	cp->actualquirks		= tp->quirks;
4572     	cp->host_status			= cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
4573     	cp->scsi_status			= S_ILLEGAL;
4574     	cp->parity_status		= 0;
4575     
4576     	cp->xerr_status			= XE_OK;
4577     #if 0
4578     	cp->sync_status			= tp->sval;
4579     	cp->wide_status			= tp->wval;
4580     #endif
4581     
4582     	/*----------------------------------------------------
4583     	**
4584     	**	Critical region: start this job.
4585     	**
4586     	**----------------------------------------------------
4587     	*/
4588     
4589     	/*
4590     	**	activate this job.
4591     	*/
4592     	cp->magic		= CCB_MAGIC;
4593     
4594     	/*
4595     	**	insert next CCBs into start queue.
4596     	**	2 max at a time is enough to flush the CCB wait queue.
4597     	*/
4598     	cp->auto_sense = 0;
4599     	if (lp)
4600     		ncr_start_next_ccb(np, lp, 2);
4601     	else
4602     		ncr_put_start_queue(np, cp);
4603     
4604     	/*
4605     	**	Command is successfully queued.
4606     	*/
4607     
4608     	return(DID_OK);
4609     }
4610     
4611     
4612     /*==========================================================
4613     **
4614     **
4615     **	Insert a CCB into the start queue and wake up the 
4616     **	SCRIPTS processor.
4617     **
4618     **
4619     **==========================================================
4620     */
4621     
4622     static void ncr_start_next_ccb(ncb_p np, lcb_p lp, int maxn)
4623     {
4624     	XPT_QUEHEAD *qp;
4625     	ccb_p cp;
4626     
4627     	if (lp->held_ccb)
4628     		return;
4629     
4630     	while (maxn-- && lp->queuedccbs < lp->queuedepth) {
4631     		qp = xpt_remque_head(&lp->wait_ccbq);
4632     		if (!qp)
4633     			break;
4634     		++lp->queuedccbs;
4635     		cp = xpt_que_entry(qp, struct ccb, link_ccbq);
4636     		xpt_insque_tail(qp, &lp->busy_ccbq);
4637     		lp->jump_ccb[cp->tag == NO_TAG ? 0 : cp->tag] =
4638     			cpu_to_scr(CCB_PHYS (cp, restart));
4639     		ncr_put_start_queue(np, cp);
4640     	}
4641     }
4642     
4643     static void ncr_put_start_queue(ncb_p np, ccb_p cp)
4644     {
4645     	u_short	qidx;
4646     
4647     	/*
4648     	**	insert into start queue.
4649     	*/
4650     	if (!np->squeueput) np->squeueput = 1;
4651     	qidx = np->squeueput + 2;
4652     	if (qidx >= MAX_START + MAX_START) qidx = 1;
4653     
4654     	np->scripth->tryloop [qidx] = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
4655     	MEMORY_BARRIER();
4656     	np->scripth->tryloop [np->squeueput] = cpu_to_scr(CCB_PHYS (cp, start));
4657     
4658     	np->squeueput = qidx;
4659     	++np->queuedccbs;
4660     	cp->queued = 1;
4661     
4662     	if (DEBUG_FLAGS & DEBUG_QUEUE)
4663     		printk ("%s: queuepos=%d.\n", ncr_name (np), np->squeueput);
4664     
4665     	/*
4666     	**	Script processor may be waiting for reselect.
4667     	**	Wake it up.
4668     	*/
4669     	MEMORY_BARRIER();
4670     	OUTB (nc_istat, SIGP);
4671     }
4672     
4673     
4674     /*==========================================================
4675     **
4676     **
4677     **	Start reset process.
4678     **	If reset in progress do nothing.
4679     **	The interrupt handler will reinitialize the chip.
4680     **	The timeout handler will wait for settle_time before 
4681     **	clearing it and so resuming command processing.
4682     **
4683     **
4684     **==========================================================
4685     */
4686     static void ncr_start_reset(ncb_p np)
4687     {
4688     	if (!np->settle_time) {
4689     		(void) ncr_reset_scsi_bus(np, 1, driver_setup.settle_delay);
4690      	}
4691      }
4692      
4693     static int ncr_reset_scsi_bus(ncb_p np, int enab_int, int settle_delay)
4694     {
4695     	u_int32 term;
4696     	int retv = 0;
4697     
4698     	np->settle_time	= ktime_get(settle_delay * HZ);
4699     
4700     	if (bootverbose > 1)
4701     		printk("%s: resetting, "
4702     			"command processing suspended for %d seconds\n",
4703     			ncr_name(np), settle_delay);
4704     
4705     	OUTB (nc_istat, SRST);
4706     	UDELAY (100);
4707     	OUTB (nc_istat, 0);
4708     	UDELAY (2000);	/* The 895 needs time for the bus mode to settle */
4709     	if (enab_int)
4710     		OUTW (nc_sien, RST);
4711     	/*
4712     	**	Enable Tolerant, reset IRQD if present and 
4713     	**	properly set IRQ mode, prior to resetting the bus.
4714     	*/
4715     	OUTB (nc_stest3, TE);
4716     	OUTB (nc_dcntl, (np->rv_dcntl & IRQM));
4717     	OUTB (nc_scntl1, CRST);
4718     	UDELAY (200);
4719     
4720     	if (!driver_setup.bus_check)
4721     		goto out;
4722     	/*
4723     	**	Check for no terminators or SCSI bus shorts to ground.
4724     	**	Read SCSI data bus, data parity bits and control signals.
4725     	**	We are expecting RESET to be TRUE and other signals to be 
4726     	**	FALSE.
4727     	*/
4728     
4729     	term =	INB(nc_sstat0);
4730     	term =	((term & 2) << 7) + ((term & 1) << 17);	/* rst sdp0 */
4731     	term |= ((INB(nc_sstat2) & 0x01) << 26) |	/* sdp1     */
4732     		((INW(nc_sbdl) & 0xff)   << 9)  |	/* d7-0     */
4733     		((INW(nc_sbdl) & 0xff00) << 10) |	/* d15-8    */
4734     		INB(nc_sbcl);	/* req ack bsy sel atn msg cd io    */
4735     
4736     	if (!(np->features & FE_WIDE))
4737     		term &= 0x3ffff;
4738     
4739     	if (term != (2<<7)) {
4740     		printk("%s: suspicious SCSI data while resetting the BUS.\n",
4741     			ncr_name(np));
4742     		printk("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
4743     			"0x%lx, expecting 0x%lx\n",
4744     			ncr_name(np),
4745     			(np->features & FE_WIDE) ? "dp1,d15-8," : "",
4746     			(u_long)term, (u_long)(2<<7));
4747     		if (driver_setup.bus_check == 1)
4748     			retv = 1;
4749     	}
4750     out:
4751     	OUTB (nc_scntl1, 0);
4752     	return retv;
4753     }
4754     
4755     /*==========================================================
4756     **
4757     **
4758     **	Reset the SCSI BUS.
4759     **	This is called from the generic SCSI driver.
4760     **
4761     **
4762     **==========================================================
4763     */
4764     static int ncr_reset_bus (ncb_p np, Scsi_Cmnd *cmd, int sync_reset)
4765     {
4766     /*	Scsi_Device        *device    = cmd->device; */
4767     	ccb_p cp;
4768     	int found;
4769     
4770     /*
4771      * Return immediately if reset is in progress.
4772      */
4773     	if (np->settle_time) {
4774     		return SCSI_RESET_PUNT;
4775     	}
4776     /*
4777      * Start the reset process.
4778      * The script processor is then assumed to be stopped.
4779      * Commands will now be queued in the waiting list until a settle 
4780      * delay of 2 seconds will be completed.
4781      */
4782     	ncr_start_reset(np);
4783     /*
4784      * First, look in the wakeup list
4785      */
4786     	for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
4787     		/*
4788     		**	look for the ccb of this command.
4789     		*/
4790     		if (cp->host_status == HS_IDLE) continue;
4791     		if (cp->cmd == cmd) {
4792     			found = 1;
4793     			break;
4794     		}
4795     	}
4796     /*
4797      * Then, look in the waiting list
4798      */
4799     	if (!found && retrieve_from_waiting_list(0, np, cmd))
4800     		found = 1;
4801     /*
4802      * Wake-up all awaiting commands with DID_RESET.
4803      */
4804     	reset_waiting_list(np);
4805     /*
4806      * Wake-up all pending commands with HS_RESET -> DID_RESET.
4807      */
4808     	ncr_wakeup(np, HS_RESET);
4809     /*
4810      * If the involved command was not in a driver queue, and the 
4811      * scsi driver told us reset is synchronous, and the command is not 
4812      * currently in the waiting list, complete it with DID_RESET status,
4813      * in order to keep it alive.
4814      */
4815     	if (!found && sync_reset && !retrieve_from_waiting_list(0, np, cmd)) {
4816     		cmd->result = ScsiResult(DID_RESET, 0);
4817     		ncr_queue_done_cmd(np, cmd);
4818     	}
4819     
4820     	return SCSI_RESET_SUCCESS;
4821     }
4822     
4823     /*==========================================================
4824     **
4825     **
4826     **	Abort an SCSI command.
4827     **	This is called from the generic SCSI driver.
4828     **
4829     **
4830     **==========================================================
4831     */
4832     static int ncr_abort_command (ncb_p np, Scsi_Cmnd *cmd)
4833     {
4834     /*	Scsi_Device        *device    = cmd->device; */
4835     	ccb_p cp;
4836     	int found;
4837     	int retv;
4838     
4839     /*
4840      * First, look for the scsi command in the waiting list
4841      */
4842     	if (remove_from_waiting_list(np, cmd)) {
4843     		cmd->result = ScsiResult(DID_ABORT, 0);
4844     		ncr_queue_done_cmd(np, cmd);
4845     		return SCSI_ABORT_SUCCESS;
4846     	}
4847     
4848     /*
4849      * Then, look in the wakeup list
4850      */
4851     	for (found=0, cp=np->ccb; cp; cp=cp->link_ccb) {
4852     		/*
4853     		**	look for the ccb of this command.
4854     		*/
4855     		if (cp->host_status == HS_IDLE) continue;
4856     		if (cp->cmd == cmd) {
4857     			found = 1;
4858     			break;
4859     		}
4860     	}
4861     
4862     	if (!found) {
4863     		return SCSI_ABORT_NOT_RUNNING;
4864     	}
4865     
4866     	if (np->settle_time) {
4867     		return SCSI_ABORT_SNOOZE;
4868     	}
4869     
4870     	/*
4871     	**	If the CCB is active, patch schedule jumps for the 
4872     	**	script to abort the command.
4873     	*/
4874     
4875     	switch(cp->host_status) {
4876     	case HS_BUSY:
4877     	case HS_NEGOTIATE:
4878     		printk ("%s: abort ccb=%p (cancel)\n", ncr_name (np), cp);
4879     			cp->start.schedule.l_paddr =
4880     				cpu_to_scr(NCB_SCRIPTH_PHYS (np, cancel));
4881     		retv = SCSI_ABORT_PENDING;
4882     		break;
4883     	case HS_DISCONNECT:
4884     		cp->restart.schedule.l_paddr =
4885     				cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
4886     		retv = SCSI_ABORT_PENDING;
4887     		break;
4888     	default:
4889     		retv = SCSI_ABORT_NOT_RUNNING;
4890     		break;
4891     
4892     	}
4893     
4894     	/*
4895     	**      If there are no requests, the script
4896     	**      processor will sleep on SEL_WAIT_RESEL.
4897     	**      Let's wake it up, since it may have to work.
4898     	*/
4899     	OUTB (nc_istat, SIGP);
4900     
4901     	return retv;
4902     }
4903     
4904     /*==========================================================
4905     **
4906     **	Linux release module stuff.
4907     **
4908     **	Called before unloading the module
4909     **	Detach the host.
4910     **	We have to free resources and halt the NCR chip
4911     **
4912     **==========================================================
4913     */
4914     
4915     #ifdef MODULE
4916     static int ncr_detach(ncb_p np)
4917     {
4918     	ccb_p cp;
4919     	tcb_p tp;
4920     	lcb_p lp;
4921     	int target, lun;
4922     	int i;
4923     
4924     	printk("%s: releasing host resources\n", ncr_name(np));
4925     
4926     /*
4927     **	Stop the ncr_timeout process
4928     **	Set release_stage to 1 and wait that ncr_timeout() set it to 2.
4929     */
4930     
4931     #ifdef DEBUG_NCR53C8XX
4932     	printk("%s: stopping the timer\n", ncr_name(np));
4933     #endif
4934     	np->release_stage = 1;
4935     	for (i = 50 ; i && np->release_stage != 2 ; i--) MDELAY (100);
4936     	if (np->release_stage != 2)
4937     		printk("%s: the timer seems to be already stopped\n", ncr_name(np));
4938     	else np->release_stage = 2;
4939     
4940     /*
4941     **	Disable chip interrupts
4942     */
4943     
4944     #ifdef DEBUG_NCR53C8XX
4945     	printk("%s: disabling chip interrupts\n", ncr_name(np));
4946     #endif
4947     	OUTW (nc_sien , 0);
4948     	OUTB (nc_dien , 0);
4949     
4950     /*
4951     **	Free irq
4952     */
4953     
4954     #ifdef DEBUG_NCR53C8XX
4955     #ifdef __sparc__
4956     	printk("%s: freeing irq %s\n", ncr_name(np), __irq_itoa(np->irq));
4957     #else
4958     	printk("%s: freeing irq %d\n", ncr_name(np), np->irq);
4959     #endif
4960     #endif
4961     	free_irq(np->irq, np);
4962     
4963     	/*
4964     	**	Reset NCR chip
4965     	**	Restore bios setting for automatic clock detection.
4966     	*/
4967     
4968     	printk("%s: resetting chip\n", ncr_name(np));
4969     	OUTB (nc_istat,  SRST);
4970     	UDELAY (100);
4971     	OUTB (nc_istat,  0   );
4972     
4973     	OUTB(nc_dmode,	np->sv_dmode);
4974     	OUTB(nc_dcntl,	np->sv_dcntl);
4975     	OUTB(nc_ctest3,	np->sv_ctest3);
4976     	OUTB(nc_ctest4,	np->sv_ctest4);
4977     	OUTB(nc_ctest5,	np->sv_ctest5);
4978     	OUTB(nc_gpcntl,	np->sv_gpcntl);
4979     	OUTB(nc_stest2,	np->sv_stest2);
4980     
4981     	ncr_selectclock(np, np->sv_scntl3);
4982     
4983     	/*
4984     	**	Release Memory mapped IO region and IO mapped region
4985     	*/
4986     
4987     #ifndef SCSI_NCR_IOMAPPED
4988     #ifdef DEBUG_NCR53C8XX
4989     	printk("%s: releasing memory mapped IO region %lx[%d]\n", ncr_name(np), (u_long) np->vaddr, 128);
4990     #endif
4991     	unmap_pci_mem((vm_offset_t) np->vaddr, (u_long) 128);
4992     #endif /* !SCSI_NCR_IOMAPPED */
4993     
4994     #ifdef DEBUG_NCR53C8XX
4995     	printk("%s: releasing IO region %x[%d]\n", ncr_name(np), np->base_io, 128);
4996     #endif
4997     	release_region(np->base_io, 128);
4998     
4999     	/*
5000     	**	Free allocated ccb(s)
5001     	*/
5002     
5003     	while ((cp=np->ccb->link_ccb) != NULL) {
5004     		np->ccb->link_ccb = cp->link_ccb;
5005     		if (cp->host_status) {
5006     		printk("%s: shall free an active ccb (host_status=%d)\n",
5007     			ncr_name(np), cp->host_status);
5008     		}
5009     #ifdef DEBUG_NCR53C8XX
5010     	printk("%s: freeing ccb (%lx)\n", ncr_name(np), (u_long) cp);
5011     #endif
5012     		m_free_dma(cp, sizeof(*cp), "CCB");
5013     	}
5014     
5015     	/*
5016     	**	Free allocated tp(s)
5017     	*/
5018     
5019     	for (target = 0; target < MAX_TARGET ; target++) {
5020     		tp=&np->target[target];
5021     		for (lun = 0 ; lun < MAX_LUN ; lun++) {
5022     			lp = tp->lp[lun];
5023     			if (lp) {
5024     #ifdef DEBUG_NCR53C8XX
5025     	printk("%s: freeing lp (%lx)\n", ncr_name(np), (u_long) lp);
5026     #endif
5027     				if (lp->jump_ccb != &lp->jump_ccb_0)
5028     					m_free_dma(lp->jump_ccb,256,"JUMP_CCB");
5029     				m_free_dma(lp, sizeof(*lp), "LCB");
5030     			}
5031     		}
5032     	}
5033     
5034     	if (np->scripth0)
5035     		m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
5036     	if (np->script0)
5037     		m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
5038     	if (np->ccb)
5039     		m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
5040     	m_free_dma(np, sizeof(struct ncb), "NCB");
5041     
5042     	printk("%s: host resources successfully released\n", ncr_name(np));
5043     
5044     	return 1;
5045     }
5046     #endif
5047     
5048     /*==========================================================
5049     **
5050     **
5051     **	Complete execution of a SCSI command.
5052     **	Signal completion to the generic SCSI driver.
5053     **
5054     **
5055     **==========================================================
5056     */
5057     
5058     void ncr_complete (ncb_p np, ccb_p cp)
5059     {
5060     	Scsi_Cmnd *cmd;
5061     	tcb_p tp;
5062     	lcb_p lp;
5063     
5064     	/*
5065     	**	Sanity check
5066     	*/
5067     
5068     	if (!cp || cp->magic != CCB_MAGIC || !cp->cmd)
5069     		return;
5070     
5071     	/*
5072     	**	Print minimal debug information.
5073     	*/
5074     
5075     	if (DEBUG_FLAGS & DEBUG_TINY)
5076     		printk ("CCB=%lx STAT=%x/%x\n", (unsigned long)cp,
5077     			cp->host_status,cp->scsi_status);
5078     
5079     	/*
5080     	**	Get command, target and lun pointers.
5081     	*/
5082     
5083     	cmd = cp->cmd;
5084     	cp->cmd = NULL;
5085     	tp = &np->target[cmd->target];
5086     	lp = tp->lp[cmd->lun];
5087     
5088     	/*
5089     	**	We donnot queue more than 1 ccb per target 
5090     	**	with negotiation at any time. If this ccb was 
5091     	**	used for negotiation, clear this info in the tcb.
5092     	*/
5093     
5094     	if (cp == tp->nego_cp)
5095     		tp->nego_cp = 0;
5096     
5097     	/*
5098     	**	If auto-sense performed, change scsi status.
5099     	*/
5100     	if (cp->auto_sense) {
5101     		cp->scsi_status = cp->auto_sense;
5102     	}
5103     
5104     	/*
5105     	**	If we were recovering from queue full or performing 
5106     	**	auto-sense, requeue skipped CCBs to the wait queue.
5107     	*/
5108     
5109     	if (lp && lp->held_ccb) {
5110     		if (cp == lp->held_ccb) {
5111     			xpt_que_splice(&lp->skip_ccbq, &lp->wait_ccbq);
5112     			xpt_que_init(&lp->skip_ccbq);
5113     			lp->held_ccb = 0;
5114     		}
5115     	}
5116     
5117     	/*
5118     	**	Check for parity errors.
5119     	*/
5120     
5121     	if (cp->parity_status > 1) {
5122     		PRINT_ADDR(cmd);
5123     		printk ("%d parity error(s).\n",cp->parity_status);
5124     	}
5125     
5126     	/*
5127     	**	Check for extended errors.
5128     	*/
5129     
5130     	if (cp->xerr_status != XE_OK) {
5131     		PRINT_ADDR(cmd);
5132     		switch (cp->xerr_status) {
5133     		case XE_EXTRA_DATA:
5134     			printk ("extraneous data discarded.\n");
5135     			break;
5136     		case XE_BAD_PHASE:
5137     			printk ("illegal scsi phase (4/5).\n");
5138     			break;
5139     		default:
5140     			printk ("extended error %d.\n", cp->xerr_status);
5141     			break;
5142     		}
5143     		if (cp->host_status==HS_COMPLETE)
5144     			cp->host_status = HS_FAIL;
5145     	}
5146     
5147     	/*
5148     	**	Print out any error for debugging purpose.
5149     	*/
5150     	if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
5151     		if (cp->host_status!=HS_COMPLETE || cp->scsi_status!=S_GOOD) {
5152     			PRINT_ADDR(cmd);
5153     			printk ("ERROR: cmd=%x host_status=%x scsi_status=%x\n",
5154     				cmd->cmnd[0], cp->host_status, cp->scsi_status);
5155     		}
5156     	}
5157     
5158     	/*
5159     	**	Check the status.
5160     	*/
5161     	if (   (cp->host_status == HS_COMPLETE)
5162     		&& (cp->scsi_status == S_GOOD ||
5163     		    cp->scsi_status == S_COND_MET)) {
5164                     /*
5165     		**	All went well (GOOD status).
5166     		**	CONDITION MET status is returned on 
5167                     **	`Pre-Fetch' or `Search data' success.
5168                     */
5169     		cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5170     
5171     		/*
5172     		**	@RESID@
5173     		**	Could dig out the correct value for resid,
5174     		**	but it would be quite complicated.
5175     		*/
5176     		/* if (cp->phys.header.lastp != cp->phys.header.goalp) */
5177     
5178     		/*
5179     		**	Allocate the lcb if not yet.
5180     		*/
5181     		if (!lp)
5182     			ncr_alloc_lcb (np, cmd->target, cmd->lun);
5183     
5184     		/*
5185     		**	On standard INQUIRY response (EVPD and CmDt 
5186     		**	not set), setup logical unit according to 
5187     		**	announced capabilities (we need the 1rst 7 bytes).
5188     		*/
5189     		if (cmd->cmnd[0] == 0x12 && !(cmd->cmnd[1] & 0x3) &&
5190     		    cmd->cmnd[4] >= 7 && !cmd->use_sg) {
5191     			sync_scsi_data(np, cmd);	/* SYNC the data */
5192     			ncr_setup_lcb (np, cmd->target, cmd->lun,
5193     				       (char *) cmd->request_buffer);
5194     		}
5195     
5196     		tp->bytes     += cp->data_len;
5197     		tp->transfers ++;
5198     
5199     		/*
5200     		**	If tags was reduced due to queue full,
5201     		**	increase tags if 1000 good status received.
5202     		*/
5203     		if (lp && lp->usetags && lp->numtags < lp->maxtags) {
5204     			++lp->num_good;
5205     			if (lp->num_good >= 1000) {
5206     				lp->num_good = 0;
5207     				++lp->numtags;
5208     				ncr_setup_tags (np, cmd->target, cmd->lun);
5209     			}
5210     		}
5211     	} else if ((cp->host_status == HS_COMPLETE)
5212     		&& (cp->scsi_status == S_CHECK_COND)) {
5213     		/*
5214     		**   Check condition code
5215     		*/
5216     		cmd->result = ScsiResult(DID_OK, S_CHECK_COND);
5217     
5218     		/*
5219     		**	Copy back sense data to caller's buffer.
5220     		*/
5221     		memcpy(cmd->sense_buffer, cp->sense_buf,
5222     		       MIN(sizeof(cmd->sense_buffer), sizeof(cp->sense_buf)));
5223     
5224     		if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
5225     			u_char * p = (u_char*) & cmd->sense_buffer;
5226     			int i;
5227     			PRINT_ADDR(cmd);
5228     			printk ("sense data:");
5229     			for (i=0; i<14; i++) printk (" %x", *p++);
5230     			printk (".\n");
5231     		}
5232     	} else if ((cp->host_status == HS_COMPLETE)
5233     		&& (cp->scsi_status == S_CONFLICT)) {
5234     		/*
5235     		**   Reservation Conflict condition code
5236     		*/
5237     		cmd->result = ScsiResult(DID_OK, S_CONFLICT);
5238     	
5239     	} else if ((cp->host_status == HS_COMPLETE)
5240     		&& (cp->scsi_status == S_BUSY ||
5241     		    cp->scsi_status == S_QUEUE_FULL)) {
5242     
5243     		/*
5244     		**   Target is busy.
5245     		*/
5246     		cmd->result = ScsiResult(DID_OK, cp->scsi_status);
5247     
5248     	} else if ((cp->host_status == HS_SEL_TIMEOUT)
5249     		|| (cp->host_status == HS_TIMEOUT)) {
5250     
5251     		/*
5252     		**   No response
5253     		*/
5254     		cmd->result = ScsiResult(DID_TIME_OUT, cp->scsi_status);
5255     
5256     	} else if (cp->host_status == HS_RESET) {
5257     
5258     		/*
5259     		**   SCSI bus reset
5260     		*/
5261     		cmd->result = ScsiResult(DID_RESET, cp->scsi_status);
5262     
5263     	} else if (cp->host_status == HS_ABORTED) {
5264     
5265     		/*
5266     		**   Transfer aborted
5267     		*/
5268     		cmd->result = ScsiResult(DID_ABORT, cp->scsi_status);
5269     
5270     	} else {
5271     
5272     		/*
5273     		**  Other protocol messes
5274     		*/
5275     		PRINT_ADDR(cmd);
5276     		printk ("COMMAND FAILED (%x %x) @%p.\n",
5277     			cp->host_status, cp->scsi_status, cp);
5278     
5279     		cmd->result = ScsiResult(DID_ERROR, cp->scsi_status);
5280     	}
5281     
5282     	/*
5283     	**	trace output
5284     	*/
5285     
5286     	if (tp->usrflag & UF_TRACE) {
5287     		u_char * p;
5288     		int i;
5289     		PRINT_ADDR(cmd);
5290     		printk (" CMD:");
5291     		p = (u_char*) &cmd->cmnd[0];
5292     		for (i=0; i<cmd->cmd_len; i++) printk (" %x", *p++);
5293     
5294     		if (cp->host_status==HS_COMPLETE) {
5295     			switch (cp->scsi_status) {
5296     			case S_GOOD:
5297     				printk ("  GOOD");
5298     				break;
5299     			case S_CHECK_COND:
5300     				printk ("  SENSE:");
5301     				p = (u_char*) &cmd->sense_buffer;
5302     				for (i=0; i<14; i++)
5303     					printk (" %x", *p++);
5304     				break;
5305     			default:
5306     				printk ("  STAT: %x\n", cp->scsi_status);
5307     				break;
5308     			}
5309     		} else printk ("  HOSTERROR: %x", cp->host_status);
5310     		printk ("\n");
5311     	}
5312     
5313     	/*
5314     	**	Free this ccb
5315     	*/
5316     	ncr_free_ccb (np, cp);
5317     
5318     	/*
5319     	**	requeue awaiting scsi commands for this lun.
5320     	*/
5321     	if (lp && lp->queuedccbs < lp->queuedepth &&
5322     	    !xpt_que_empty(&lp->wait_ccbq))
5323     		ncr_start_next_ccb(np, lp, 2);
5324     
5325     	/*
5326     	**	requeue awaiting scsi commands for this controller.
5327     	*/
5328     	if (np->waiting_list)
5329     		requeue_waiting_list(np);
5330     
5331     	/*
5332     	**	signal completion to generic driver.
5333     	*/
5334     	ncr_queue_done_cmd(np, cmd);
5335     }
5336     
5337     /*==========================================================
5338     **
5339     **
5340     **	Signal all (or one) control block done.
5341     **
5342     **
5343     **==========================================================
5344     */
5345     
5346     /*
5347     **	This CCB has been skipped by the NCR.
5348     **	Queue it in the correponding unit queue.
5349     */
5350     static void ncr_ccb_skipped(ncb_p np, ccb_p cp)
5351     {
5352     	tcb_p tp = &np->target[cp->target];
5353     	lcb_p lp = tp->lp[cp->lun];
5354     
5355     	if (lp && cp != np->ccb) {
5356     		cp->host_status &= ~HS_SKIPMASK;
5357     		cp->start.schedule.l_paddr = 
5358     			cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
5359     		xpt_remque(&cp->link_ccbq);
5360     		xpt_insque_tail(&cp->link_ccbq, &lp->skip_ccbq);
5361     		if (cp->queued) {
5362     			--lp->queuedccbs;
5363     		}
5364     	}
5365     	if (cp->queued) {
5366     		--np->queuedccbs;
5367     		cp->queued = 0;
5368     	}
5369     }
5370     
5371     /*
5372     **	The NCR has completed CCBs.
5373     **	Look at the DONE QUEUE if enabled, otherwise scan all CCBs
5374     */
5375     void ncr_wakeup_done (ncb_p np)
5376     {
5377     	ccb_p cp;
5378     #ifdef SCSI_NCR_CCB_DONE_SUPPORT
5379     	int i, j;
5380     
5381     	i = np->ccb_done_ic;
5382     	while (1) {
5383     		j = i+1;
5384     		if (j >= MAX_DONE)
5385     			j = 0;
5386     
5387     		cp = np->ccb_done[j];
5388     		if (!CCB_DONE_VALID(cp))
5389     			break;
5390     
5391     		np->ccb_done[j] = (ccb_p) CCB_DONE_EMPTY;
5392     		np->scripth->done_queue[5*j + 4] =
5393     				cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
5394     		MEMORY_BARRIER();
5395     		np->scripth->done_queue[5*i + 4] =
5396     				cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
5397     
5398     		if (cp->host_status & HS_DONEMASK)
5399     			ncr_complete (np, cp);
5400     		else if (cp->host_status & HS_SKIPMASK)
5401     			ncr_ccb_skipped (np, cp);
5402     
5403     		i = j;
5404     	}
5405     	np->ccb_done_ic = i;
5406     #else
5407     	cp = np->ccb;
5408     	while (cp) {
5409     		if (cp->host_status & HS_DONEMASK)
5410     			ncr_complete (np, cp);
5411     		else if (cp->host_status & HS_SKIPMASK)
5412     			ncr_ccb_skipped (np, cp);
5413     		cp = cp->link_ccb;
5414     	}
5415     #endif
5416     }
5417     
5418     /*
5419     **	Complete all active CCBs.
5420     */
5421     void ncr_wakeup (ncb_p np, u_long code)
5422     {
5423     	ccb_p cp = np->ccb;
5424     
5425     	while (cp) {
5426     		if (cp->host_status != HS_IDLE) {
5427     			cp->host_status = code;
5428     			ncr_complete (np, cp);
5429     		}
5430     		cp = cp->link_ccb;
5431     	}
5432     }
5433     
5434     /*==========================================================
5435     **
5436     **
5437     **	Start NCR chip.
5438     **
5439     **
5440     **==========================================================
5441     */
5442     
5443     void ncr_init (ncb_p np, int reset, char * msg, u_long code)
5444     {
5445      	int	i;
5446     
5447      	/*
5448     	**	Reset chip if asked, otherwise just clear fifos.
5449      	*/
5450     
5451     	if (reset) {
5452     		OUTB (nc_istat,  SRST);
5453     		UDELAY (100);
5454     	}
5455     	else {
5456     		OUTB (nc_stest3, TE|CSF);
5457     		OUTONB (nc_ctest3, CLF);
5458     	}
5459      
5460     	/*
5461     	**	Message.
5462     	*/
5463     
5464     	if (msg) printk (KERN_INFO "%s: restart (%s).\n", ncr_name (np), msg);
5465     
5466     	/*
5467     	**	Clear Start Queue
5468     	*/
5469     	np->queuedepth = MAX_START - 1;	/* 1 entry needed as end marker */
5470     	for (i = 1; i < MAX_START + MAX_START; i += 2)
5471     		np->scripth0->tryloop[i] =
5472     				cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5473     
5474     	/*
5475     	**	Start at first entry.
5476     	*/
5477     	np->squeueput = 0;
5478     	np->script0->startpos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np, tryloop));
5479     
5480     	/*
5481     	**	Clear Done Queue
5482     	*/
5483     	for (i = 0; i < MAX_DONE; i++) {
5484     		np->ccb_done[i] = (ccb_p) CCB_DONE_EMPTY;
5485     		np->scripth0->done_queue[5*i + 4] =
5486     			cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
5487     	}
5488     
5489     	/*
5490     	**	Start at first entry.
5491     	*/
5492     	np->script0->done_pos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np,done_queue));
5493     	np->ccb_done_ic = MAX_DONE-1;
5494     	np->scripth0->done_queue[5*(MAX_DONE-1) + 4] =
5495     			cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
5496     
5497     	/*
5498     	**	Wakeup all pending jobs.
5499     	*/
5500     	ncr_wakeup (np, code);
5501     
5502     	/*
5503     	**	Init chip.
5504     	*/
5505     
5506     	OUTB (nc_istat,  0x00   );	/*  Remove Reset, abort */
5507     	UDELAY (2000);	/* The 895 needs time for the bus mode to settle */
5508     
5509     	OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
5510     					/*  full arb., ena parity, par->ATN  */
5511     	OUTB (nc_scntl1, 0x00);		/*  odd parity, and remove CRST!! */
5512     
5513     	ncr_selectclock(np, np->rv_scntl3);	/* Select SCSI clock */
5514     
5515     	OUTB (nc_scid  , RRE|np->myaddr);	/* Adapter SCSI address */
5516     	OUTW (nc_respid, 1ul<<np->myaddr);	/* Id to respond to */
5517     	OUTB (nc_istat , SIGP	);		/*  Signal Process */
5518     	OUTB (nc_dmode , np->rv_dmode);		/* Burst length, dma mode */
5519     	OUTB (nc_ctest5, np->rv_ctest5);	/* Large fifo + large burst */
5520     
5521     	OUTB (nc_dcntl , NOCOM|np->rv_dcntl);	/* Protect SFBR */
5522     	OUTB (nc_ctest3, np->rv_ctest3);	/* Write and invalidate */
5523     	OUTB (nc_ctest4, np->rv_ctest4);	/* Master parity checking */
5524     
5525     	OUTB (nc_stest2, EXT|np->rv_stest2);	/* Extended Sreq/Sack filtering */
5526     	OUTB (nc_stest3, TE);			/* TolerANT enable */
5527     	OUTB (nc_stime0, 0x0c	);		/* HTH disabled  STO 0.25 sec */
5528     
5529     	/*
5530     	**	Disable disconnects.
5531     	*/
5532     
5533     	np->disc = 0;
5534     
5535     	/*
5536     	**    Enable GPIO0 pin for writing if LED support.
5537     	*/
5538     
5539     	if (np->features & FE_LED0) {
5540     		OUTOFFB (nc_gpcntl, 0x01);
5541     	}
5542     
5543     	/*
5544     	**      enable ints
5545     	*/
5546     
5547     	OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
5548     	OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
5549     
5550     	/*
5551     	**	For 895/6 enable SBMC interrupt and save current SCSI bus mode.
5552     	*/
5553     	if (np->features & FE_ULTRA2) {
5554     		OUTONW (nc_sien, SBMC);
5555     		np->scsi_mode = INB (nc_stest4) & SMODE;
5556     	}
5557     
5558     	/*
5559     	**	DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
5560     	**	Disable overlapped arbitration.
5561     	**	All 896 chips are also affected by this errata.
5562     	*/
5563     	if (np->device_id == PCI_DEVICE_ID_NCR_53C875)
5564     		OUTB (nc_ctest0, (1<<5));
5565     	else if (np->device_id == PCI_DEVICE_ID_NCR_53C896)
5566     		OUTB (nc_ccntl0, DPR);
5567     
5568     	/*
5569     	**	Fill in target structure.
5570     	**	Reinitialize usrsync.
5571     	**	Reinitialize usrwide.
5572     	**	Prepare sync negotiation according to actual SCSI bus mode.
5573     	*/
5574     
5575     	for (i=0;i<MAX_TARGET;i++) {
5576     		tcb_p tp = &np->target[i];
5577     
5578     		tp->sval    = 0;
5579     		tp->wval    = np->rv_scntl3;
5580     
5581     		if (tp->usrsync != 255) {
5582     			if (tp->usrsync <= np->maxsync) {
5583     				if (tp->usrsync < np->minsync) {
5584     					tp->usrsync = np->minsync;
5585     				}
5586     			}
5587     			else
5588     				tp->usrsync = 255;
5589     		};
5590     
5591     		if (tp->usrwide > np->maxwide)
5592     			tp->usrwide = np->maxwide;
5593     
5594     		ncr_negotiate (np, tp);
5595     	}
5596     
5597     	/*
5598     	**    Start script processor.
5599     	*/
5600     	if (np->paddr2) {
5601     		if (bootverbose)
5602     			printk ("%s: Downloading SCSI SCRIPTS.\n",
5603     				ncr_name(np));
5604     		OUTL (nc_scratcha, vtobus(np->script0));
5605     		OUTL_DSP (NCB_SCRIPTH_PHYS (np, start_ram));
5606     	}
5607     	else
5608     		OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
5609     }
5610     
5611     /*==========================================================
5612     **
5613     **	Prepare the negotiation values for wide and
5614     **	synchronous transfers.
5615     **
5616     **==========================================================
5617     */
5618     
5619     static void ncr_negotiate (struct ncb* np, struct tcb* tp)
5620     {
5621     	/*
5622     	**	minsync unit is 4ns !
5623     	*/
5624     
5625     	u_long minsync = tp->usrsync;
5626     
5627     	/*
5628     	**	SCSI bus mode limit
5629     	*/
5630     
5631     	if (np->scsi_mode && np->scsi_mode == SMODE_SE) {
5632     		if (minsync < 12) minsync = 12;
5633     	}
5634     
5635     	/*
5636     	**	our limit ..
5637     	*/
5638     
5639     	if (minsync < np->minsync)
5640     		minsync = np->minsync;
5641     
5642     	/*
5643     	**	divider limit
5644     	*/
5645     
5646     	if (minsync > np->maxsync)
5647     		minsync = 255;
5648     
5649     	tp->minsync = minsync;
5650     	tp->maxoffs = (minsync<255 ? np->maxoffs : 0);
5651     
5652     	/*
5653     	**	period=0: has to negotiate sync transfer
5654     	*/
5655     
5656     	tp->period=0;
5657     
5658     	/*
5659     	**	widedone=0: has to negotiate wide transfer
5660     	*/
5661     	tp->widedone=0;
5662     }
5663     
5664     /*==========================================================
5665     **
5666     **	Get clock factor and sync divisor for a given 
5667     **	synchronous factor period.
5668     **	Returns the clock factor (in sxfer) and scntl3 
5669     **	synchronous divisor field.
5670     **
5671     **==========================================================
5672     */
5673     
5674     static void ncr_getsync(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p)
5675     {
5676     	u_long	clk = np->clock_khz;	/* SCSI clock frequency in kHz	*/
5677     	int	div = np->clock_divn;	/* Number of divisors supported	*/
5678     	u_long	fak;			/* Sync factor in sxfer		*/
5679     	u_long	per;			/* Period in tenths of ns	*/
5680     	u_long	kpc;			/* (per * clk)			*/
5681     
5682     	/*
5683     	**	Compute the synchronous period in tenths of nano-seconds
5684     	*/
5685     	if	(sfac <= 10)	per = 250;
5686     	else if	(sfac == 11)	per = 303;
5687     	else if	(sfac == 12)	per = 500;
5688     	else			per = 40 * sfac;
5689     
5690     	/*
5691     	**	Look for the greatest clock divisor that allows an 
5692     	**	input speed faster than the period.
5693     	*/
5694     	kpc = per * clk;
5695     	while (--div >= 0)
5696     		if (kpc >= (div_10M[div] << 2)) break;
5697     
5698     	/*
5699     	**	Calculate the lowest clock factor that allows an output 
5700     	**	speed not faster than the period.
5701     	*/
5702     	fak = (kpc - 1) / div_10M[div] + 1;
5703     
5704     #if 0	/* This optimization does not seem very useful */
5705     
5706     	per = (fak * div_10M[div]) / clk;
5707     
5708     	/*
5709     	**	Why not to try the immediate lower divisor and to choose 
5710     	**	the one that allows the fastest output speed ?
5711     	**	We dont want input speed too much greater than output speed.
5712     	*/
5713     	if (div >= 1 && fak < 8) {
5714     		u_long fak2, per2;
5715     		fak2 = (kpc - 1) / div_10M[div-1] + 1;
5716     		per2 = (fak2 * div_10M[div-1]) / clk;
5717     		if (per2 < per && fak2 <= 8) {
5718     			fak = fak2;
5719     			per = per2;
5720     			--div;
5721     		}
5722     	}
5723     #endif
5724     
5725     	if (fak < 4) fak = 4;	/* Should never happen, too bad ... */
5726     
5727     	/*
5728     	**	Compute and return sync parameters for the ncr
5729     	*/
5730     	*fakp		= fak - 4;
5731     	*scntl3p	= ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
5732     }
5733     
5734     
5735     /*==========================================================
5736     **
5737     **	Set actual values, sync status and patch all ccbs of 
5738     **	a target according to new sync/wide agreement.
5739     **
5740     **==========================================================
5741     */
5742     
5743     static void ncr_set_sync_wide_status (ncb_p np, u_char target)
5744     {
5745     	ccb_p cp;
5746     	tcb_p tp = &np->target[target];
5747     
5748     	/*
5749     	**	set actual value and sync_status
5750     	*/
5751     	OUTB (nc_sxfer, tp->sval);
5752     	np->sync_st = tp->sval;
5753     	OUTB (nc_scntl3, tp->wval);
5754     	np->wide_st = tp->wval;
5755     
5756     	/*
5757     	**	patch ALL ccbs of this target.
5758     	*/
5759     	for (cp = np->ccb; cp; cp = cp->link_ccb) {
5760     		if (!cp->cmd) continue;
5761     		if (cp->cmd->target != target) continue;
5762     #if 0
5763     		cp->sync_status = tp->sval;
5764     		cp->wide_status = tp->wval;
5765     #endif
5766     		cp->phys.select.sel_scntl3 = tp->wval;
5767     		cp->phys.select.sel_sxfer  = tp->sval;
5768     	};
5769     }
5770     
5771     /*==========================================================
5772     **
5773     **	Switch sync mode for current job and it's target
5774     **
5775     **==========================================================
5776     */
5777     
5778     static void ncr_setsync (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer)
5779     {
5780     	Scsi_Cmnd *cmd;
5781     	tcb_p tp;
5782     	u_char target = INB (nc_sdid) & 0x0f;
5783     	u_char idiv;
5784     
5785     	assert (cp && cp->cmd);
5786     	if (!cp) return;
5787     
5788     	cmd = cp->cmd;
5789     	if (!cmd) return;
5790     
5791     	assert (target == (cmd->target & 0xf));
5792     
5793     	tp = &np->target[target];
5794     
5795     	if (!scntl3 || !(sxfer & 0x1f))
5796     		scntl3 = np->rv_scntl3;
5797     	scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) | (np->rv_scntl3 & 0x07);
5798     
5799     	/*
5800     	**	Deduce the value of controller sync period from scntl3.
5801     	**	period is in tenths of nano-seconds.
5802     	*/
5803     
5804     	idiv = ((scntl3 >> 4) & 0x7);
5805     	if ((sxfer & 0x1f) && idiv)
5806     		tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
5807     	else
5808     		tp->period = 0xffff;
5809     
5810     	/*
5811     	**	 Stop there if sync parameters are unchanged
5812     	*/
5813     	if (tp->sval == sxfer && tp->wval == scntl3) return;
5814     	tp->sval = sxfer;
5815     	tp->wval = scntl3;
5816     
5817     	/*
5818     	**	Bells and whistles   ;-)
5819     	*/
5820     	PRINT_TARGET(np, target);
5821     	if (sxfer & 0x01f) {
5822     		unsigned f10 = 100000 << (tp->widedone ? tp->widedone -1 : 0);
5823     		unsigned mb10 = (f10 + tp->period/2) / tp->period;
5824     		char *scsi;
5825     
5826     		/*
5827     		**  Disable extended Sreq/Sack filtering
5828     		*/
5829     		if (tp->period <= 2000) OUTOFFB (nc_stest2, EXT);
5830     
5831     		/*
5832     		**	Bells and whistles   ;-)
5833     		*/
5834     		if	(tp->period < 500)	scsi = "FAST-40";
5835     		else if	(tp->period < 1000)	scsi = "FAST-20";
5836     		else if	(tp->period < 2000)	scsi = "FAST-10";
5837     		else				scsi = "FAST-5";
5838     
5839     		printk ("%s %sSCSI %d.%d MB/s (%d ns, offset %d)\n", scsi,
5840     			tp->widedone > 1 ? "WIDE " : "",
5841     			mb10 / 10, mb10 % 10, tp->period / 10, sxfer & 0x1f);
5842     	} else
5843     		printk ("%sasynchronous.\n", tp->widedone > 1 ? "wide " : "");
5844     
5845     	/*
5846     	**	set actual value and sync_status
5847     	**	patch ALL ccbs of this target.
5848     	*/
5849     	ncr_set_sync_wide_status(np, target);
5850     }
5851     
5852     /*==========================================================
5853     **
5854     **	Switch wide mode for current job and it's target
5855     **	SCSI specs say: a SCSI device that accepts a WDTR 
5856     **	message shall reset the synchronous agreement to 
5857     **	asynchronous mode.
5858     **
5859     **==========================================================
5860     */
5861     
5862     static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide, u_char ack)
5863     {
5864     	Scsi_Cmnd *cmd;
5865     	u_short target = INB (nc_sdid) & 0x0f;
5866     	tcb_p tp;
5867     	u_char	scntl3;
5868     	u_char	sxfer;
5869     
5870     	assert (cp && cp->cmd);
5871     	if (!cp) return;
5872     
5873     	cmd = cp->cmd;
5874     	if (!cmd) return;
5875     
5876     	assert (target == (cmd->target & 0xf));
5877     
5878     	tp = &np->target[target];
5879     	tp->widedone  =  wide+1;
5880     	scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
5881     
5882     	sxfer = ack ? 0 : tp->sval;
5883     
5884     	/*
5885     	**	 Stop there if sync/wide parameters are unchanged
5886     	*/
5887     	if (tp->sval == sxfer && tp->wval == scntl3) return;
5888     	tp->sval = sxfer;
5889     	tp->wval = scntl3;
5890     
5891     	/*
5892     	**	Bells and whistles   ;-)
5893     	*/
5894     	if (bootverbose >= 2) {
5895     		PRINT_TARGET(np, target);
5896     		if (scntl3 & EWS)
5897     			printk ("WIDE SCSI (16 bit) enabled.\n");
5898     		else
5899     			printk ("WIDE SCSI disabled.\n");
5900     	}
5901     
5902     	/*
5903     	**	set actual value and sync_status
5904     	**	patch ALL ccbs of this target.
5905     	*/
5906     	ncr_set_sync_wide_status(np, target);
5907     }
5908     
5909     /*==========================================================
5910     **
5911     **	Switch tagged mode for a target.
5912     **
5913     **==========================================================
5914     */
5915     
5916     static void ncr_setup_tags (ncb_p np, u_char tn, u_char ln)
5917     {
5918     	tcb_p tp = &np->target[tn];
5919     	lcb_p lp = tp->lp[ln];
5920     	u_char   reqtags, maxdepth;
5921     
5922     	/*
5923     	**	Just in case ...
5924     	*/
5925     	if ((!tp) || (!lp))
5926     		return;
5927     
5928     	/*
5929     	**	If SCSI device queue depth is not yet set, leave here.
5930     	*/
5931     	if (!lp->scdev_depth)
5932     		return;
5933     
5934     	/*
5935     	**	Donnot allow more tags than the SCSI driver can queue 
5936     	**	for this device.
5937     	**	Donnot allow more tags than we can handle.
5938     	*/
5939     	maxdepth = lp->scdev_depth;
5940     	if (maxdepth > lp->maxnxs)	maxdepth    = lp->maxnxs;
5941     	if (lp->maxtags > maxdepth)	lp->maxtags = maxdepth;
5942     	if (lp->numtags > maxdepth)	lp->numtags = maxdepth;
5943     
5944     	/*
5945     	**	only devices conformant to ANSI Version >= 2
5946     	**	only devices capable of tagged commands
5947     	**	only if enabled by user ..
5948     	*/
5949     	if ((lp->inq_byte7 & INQ7_QUEUE) && lp->numtags > 1) {
5950     		reqtags = lp->numtags;
5951     	} else {
5952     		reqtags = 1;
5953     	};
5954     
5955     	/*
5956     	**	Update max number of tags
5957     	*/
5958     	lp->numtags = reqtags;
5959     	if (lp->numtags > lp->maxtags)
5960     		lp->maxtags = lp->numtags;
5961     
5962     	/*
5963     	**	If we want to switch tag mode, we must wait 
5964     	**	for no CCB to be active.
5965     	*/
5966     	if	(reqtags > 1 && lp->usetags) {	 /* Stay in tagged mode    */
5967     		if (lp->queuedepth == reqtags)	 /* Already announced	   */
5968     			return;
5969     		lp->queuedepth	= reqtags;
5970     	}
5971     	else if	(reqtags <= 1 && !lp->usetags) { /* Stay in untagged mode  */
5972     		lp->queuedepth	= reqtags;
5973     		return;
5974     	}
5975     	else {					 /* Want to switch tag mode */
5976     		if (lp->busyccbs)		 /* If not yet safe, return */
5977     			return;
5978     		lp->queuedepth	= reqtags;
5979     		lp->usetags	= reqtags > 1 ? 1 : 0;
5980     	}
5981     
5982     	/*
5983     	**	Patch the lun mini-script, according to tag mode.
5984     	*/
5985     	lp->jump_tag.l_paddr = lp->usetags?
5986     			cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_tag)) :
5987     			cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
5988     
5989     	/*
5990     	**	Announce change to user.
5991     	*/
5992     	if (bootverbose) {
5993     		PRINT_LUN(np, tn, ln);
5994     		if (lp->usetags) {
5995     			printk("tagged command queue depth set to %d\n", reqtags);
5996     		}
5997     		else {
5998     			printk("tagged command queueing disabled\n");
5999     		}
6000     	}
6001     }
6002     
6003     /*----------------------------------------------------
6004     **
6005     **	handle user commands
6006     **
6007     **----------------------------------------------------
6008     */
6009     
6010     #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
6011     
6012     static void ncr_usercmd (ncb_p np)
6013     {
6014     	u_char t;
6015     	tcb_p tp;
6016     
6017     	switch (np->user.cmd) {
6018     
6019     	case 0: return;
6020     
6021     	case UC_SETSYNC:
6022     		for (t=0; t<MAX_TARGET; t++) {
6023     			if (!((np->user.target>>t)&1)) continue;
6024     			tp = &np->target[t];
6025     			tp->usrsync = np->user.data;
6026     			ncr_negotiate (np, tp);
6027     		};
6028     		break;
6029     
6030     	case UC_SETTAGS:
6031     		for (t=0; t<MAX_TARGET; t++) {
6032     			int ln;
6033     			if (!((np->user.target>>t)&1)) continue;
6034     			np->target[t].usrtags = np->user.data;
6035     			for (ln = 0; ln < MAX_LUN; ln++) {
6036     				lcb_p lp = np->target[t].lp[ln];
6037     				if (!lp)
6038     					continue;
6039     				lp->maxtags = lp->numtags = np->user.data;
6040     				ncr_setup_tags (np, t, ln);
6041     			}
6042      		};
6043     		break;
6044     
6045     	case UC_SETDEBUG:
6046     #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
6047     		ncr_debug = np->user.data;
6048     #endif
6049     		break;
6050     
6051     	case UC_SETORDER:
6052     		np->order = np->user.data;
6053     		break;
6054     
6055     	case UC_SETVERBOSE:
6056     		np->verbose = np->user.data;
6057     		break;
6058     
6059     	case UC_SETWIDE:
6060     		for (t=0; t<MAX_TARGET; t++) {
6061     			u_long size;
6062     			if (!((np->user.target>>t)&1)) continue;
6063     			tp = &np->target[t];
6064     			size = np->user.data;
6065     			if (size > np->maxwide) size=np->maxwide;
6066     			tp->usrwide = size;
6067     			ncr_negotiate (np, tp);
6068     		};
6069     		break;
6070     
6071     	case UC_SETFLAG:
6072     		for (t=0; t<MAX_TARGET; t++) {
6073     			if (!((np->user.target>>t)&1)) continue;
6074     			tp = &np->target[t];
6075     			tp->usrflag = np->user.data;
6076     		};
6077     		break;
6078     	}
6079     	np->user.cmd=0;
6080     }
6081     #endif
6082     
6083     /*==========================================================
6084     **
6085     **
6086     **	ncr timeout handler.
6087     **
6088     **
6089     **==========================================================
6090     **
6091     **	Misused to keep the driver running when
6092     **	interrupts are not configured correctly.
6093     **
6094     **----------------------------------------------------------
6095     */
6096     
6097     static void ncr_timeout (ncb_p np)
6098     {
6099     	u_long	thistime = ktime_get(0);
6100     
6101     	/*
6102     	**	If release process in progress, let's go
6103     	**	Set the release stage from 1 to 2 to synchronize
6104     	**	with the release process.
6105     	*/
6106     
6107     	if (np->release_stage) {
6108     		if (np->release_stage == 1) np->release_stage = 2;
6109     		return;
6110     	}
6111     
6112     	np->timer.expires = ktime_get(SCSI_NCR_TIMER_INTERVAL);
6113     	add_timer(&np->timer);
6114     
6115     	/*
6116     	**	If we are resetting the ncr, wait for settle_time before 
6117     	**	clearing it. Then command processing will be resumed.
6118     	*/
6119     	if (np->settle_time) {
6120     		if (np->settle_time <= thistime) {
6121     			if (bootverbose > 1)
6122     				printk("%s: command processing resumed\n", ncr_name(np));
6123     			np->settle_time	= 0;
6124     			np->disc	= 1;
6125     			requeue_waiting_list(np);
6126     		}
6127     		return;
6128     	}
6129     
6130     	/*
6131     	**	Since the generic scsi driver only allows us 0.5 second 
6132     	**	to perform abort of a command, we must look at ccbs about 
6133     	**	every 0.25 second.
6134     	*/
6135     	if (np->lasttime + 4*HZ < thistime) {
6136     		/*
6137     		**	block ncr interrupts
6138     		*/
6139     		np->lasttime = thistime;
6140     	}
6141     
6142     #ifdef SCSI_NCR_BROKEN_INTR
6143     	if (INB(nc_istat) & (INTF|SIP|DIP)) {
6144     
6145     		/*
6146     		**	Process pending interrupts.
6147     		*/
6148     		if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
6149     		ncr_exception (np);
6150     		if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
6151     	}
6152     #endif /* SCSI_NCR_BROKEN_INTR */
6153     }
6154     
6155     /*==========================================================
6156     **
6157     **	log message for real hard errors
6158     **
6159     **	"ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
6160     **	"	      reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
6161     **
6162     **	exception register:
6163     **		ds:	dstat
6164     **		si:	sist
6165     **
6166     **	SCSI bus lines:
6167     **		so:	control lines as driver by NCR.
6168     **		si:	control lines as seen by NCR.
6169     **		sd:	scsi data lines as seen by NCR.
6170     **
6171     **	wide/fastmode:
6172     **		sxfer:	(see the manual)
6173     **		scntl3:	(see the manual)
6174     **
6175     **	current script command:
6176     **		dsp:	script address (relative to start of script).
6177     **		dbc:	first word of script command.
6178     **
6179     **	First 16 register of the chip:
6180     **		r0..rf
6181     **
6182     **==========================================================
6183     */
6184     
6185     static void ncr_log_hard_error(ncb_p np, u_short sist, u_char dstat)
6186     {
6187     	u_int32	dsp;
6188     	int	script_ofs;
6189     	int	script_size;
6190     	char	*script_name;
6191     	u_char	*script_base;
6192     	int	i;
6193     
6194     	dsp	= INL (nc_dsp);
6195     
6196     	if (dsp > np->p_script && dsp <= np->p_script + sizeof(struct script)) {
6197     		script_ofs	= dsp - np->p_script;
6198     		script_size	= sizeof(struct script);
6199     		script_base	= (u_char *) np->script0;
6200     		script_name	= "script";
6201     	}
6202     	else if (np->p_scripth < dsp && 
6203     		 dsp <= np->p_scripth + sizeof(struct scripth)) {
6204     		script_ofs	= dsp - np->p_scripth;
6205     		script_size	= sizeof(struct scripth);
6206     		script_base	= (u_char *) np->scripth0;
6207     		script_name	= "scripth";
6208     	} else {
6209     		script_ofs	= dsp;
6210     		script_size	= 0;
6211     		script_base	= 0;
6212     		script_name	= "mem";
6213     	}
6214     
6215     	printk ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
6216     		ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
6217     		(unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
6218     		(unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
6219     		(unsigned)INL (nc_dbc));
6220     
6221     	if (((script_ofs & 3) == 0) &&
6222     	    (unsigned)script_ofs < script_size) {
6223     		printk ("%s: script cmd = %08x\n", ncr_name(np),
6224     			scr_to_cpu((int) *(ncrcmd *)(script_base + script_ofs)));
6225     	}
6226     
6227             printk ("%s: regdump:", ncr_name(np));
6228             for (i=0; i<16;i++)
6229                 printk (" %02x", (unsigned)INB_OFF(i));
6230             printk (".\n");
6231     }
6232     
6233     /*============================================================
6234     **
6235     **	ncr chip exception handler.
6236     **
6237     **============================================================
6238     **
6239     **	In normal cases, interrupt conditions occur one at a 
6240     **	time. The ncr is able to stack in some extra registers 
6241     **	other interrupts that will occurs after the first one.
6242     **	But severall interrupts may occur at the same time.
6243     **
6244     **	We probably should only try to deal with the normal 
6245     **	case, but it seems that multiple interrupts occur in 
6246     **	some cases that are not abnormal at all.
6247     **
6248     **	The most frequent interrupt condition is Phase Mismatch.
6249     **	We should want to service this interrupt quickly.
6250     **	A SCSI parity error may be delivered at the same time.
6251     **	The SIR interrupt is not very frequent in this driver, 
6252     **	since the INTFLY is likely used for command completion 
6253     **	signaling.
6254     **	The Selection Timeout interrupt may be triggered with 
6255     **	IID and/or UDC.
6256     **	The SBMC interrupt (SCSI Bus Mode Change) may probably 
6257     **	occur at any time.
6258     **
6259     **	This handler try to deal as cleverly as possible with all
6260     **	the above.
6261     **
6262     **============================================================
6263     */
6264     
6265     void ncr_exception (ncb_p np)
6266     {
6267     	u_char	istat, dstat;
6268     	u_short	sist;
6269     	int	i;
6270     
6271     	/*
6272     	**	interrupt on the fly ?
6273     	**	Since the global header may be copied back to a CCB 
6274     	**	using a posted PCI memory write, the last operation on 
6275     	**	the istat register is a READ in order to flush posted 
6276     	**	PCI write commands.
6277     	*/
6278     	istat = INB (nc_istat);
6279     	if (istat & INTF) {
6280     		OUTB (nc_istat, (istat & SIGP) | INTF);
6281     		istat = INB (nc_istat);
6282     		if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
6283     		ncr_wakeup_done (np);
6284     	};
6285     
6286     	if (!(istat & (SIP|DIP)))
6287     		return;
6288     
6289     	if (istat & CABRT)
6290     		OUTB (nc_istat, CABRT);
6291     
6292     	/*
6293     	**	Steinbach's Guideline for Systems Programming:
6294     	**	Never test for an error condition you don't know how to handle.
6295     	*/
6296     
6297     	sist  = (istat & SIP) ? INW (nc_sist)  : 0;
6298     	dstat = (istat & DIP) ? INB (nc_dstat) : 0;
6299     
6300     	if (DEBUG_FLAGS & DEBUG_TINY)
6301     		printk ("<%d|%x:%x|%x:%x>",
6302     			(int)INB(nc_scr0),
6303     			dstat,sist,
6304     			(unsigned)INL(nc_dsp),
6305     			(unsigned)INL(nc_dbc));
6306     
6307     	/*========================================================
6308     	**	First, interrupts we want to service cleanly.
6309     	**
6310     	**	Phase mismatch is the most frequent interrupt, and 
6311     	**	so we have to service it as quickly and as cleanly 
6312     	**	as possible.
6313     	**	Programmed interrupts are rarely used in this driver,
6314     	**	but we must handle them cleanly anyway.
6315     	**	We try to deal with PAR and SBMC combined with 
6316     	**	some other interrupt(s).
6317     	**=========================================================
6318     	*/
6319     
6320     	if (!(sist  & (STO|GEN|HTH|SGE|UDC|RST)) &&
6321     	    !(dstat & (MDPE|BF|ABRT|IID))) {
6322     		if ((sist & SBMC) && ncr_int_sbmc (np))
6323     			return;
6324     		if ((sist & PAR)  && ncr_int_par  (np))
6325     			return;
6326     		if (sist & MA) {
6327     			ncr_int_ma (np);
6328     			return;
6329     		}
6330     		if (dstat & SIR) {
6331     			ncr_int_sir (np);
6332     			return;
6333     		}
6334     		/*
6335     		**  DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 2.
6336     		*/
6337     		if (!(sist & (SBMC|PAR)) && !(dstat & SSI)) {
6338     			printk(	"%s: unknown interrupt(s) ignored, "
6339     				"ISTAT=%x DSTAT=%x SIST=%x\n",
6340     				ncr_name(np), istat, dstat, sist);
6341     			return;
6342     		}
6343     		OUTONB_STD ();
6344     		return;
6345     	};
6346     
6347     	/*========================================================
6348     	**	Now, interrupts that need some fixing up.
6349     	**	Order and multiple interrupts is so less important.
6350     	**
6351     	**	If SRST has been asserted, we just reset the chip.
6352     	**
6353     	**	Selection is intirely handled by the chip. If the 
6354     	**	chip says STO, we trust it. Seems some other 
6355     	**	interrupts may occur at the same time (UDC, IID), so 
6356     	**	we ignore them. In any case we do enough fix-up 
6357     	**	in the service routine.
6358     	**	We just exclude some fatal dma errors.
6359     	**=========================================================
6360     	*/
6361     
6362     	if (sist & RST) {
6363     		ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
6364     		return;
6365     	};
6366     
6367     	if ((sist & STO) &&
6368     		!(dstat & (MDPE|BF|ABRT))) {
6369     	/*
6370     	**	DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 1.
6371     	*/
6372     		OUTONB (nc_ctest3, CLF);
6373     
6374     		ncr_int_sto (np);
6375     		return;
6376     	};
6377     
6378     	/*=========================================================
6379     	**	Now, interrupts we are not able to recover cleanly.
6380     	**	(At least for the moment).
6381     	**
6382     	**	Do the register dump.
6383     	**	Log message for real hard errors.
6384     	**	Clear all fifos.
6385     	**	For MDPE, BF, ABORT, IID, SGE and HTH we reset the 
6386     	**	BUS and the chip.
6387     	**	We are more soft for UDC.
6388     	**=========================================================
6389     	*/
6390     
6391     	if (ktime_exp(np->regtime)) {
6392     		np->regtime = ktime_get(10*HZ);
6393     		for (i = 0; i<sizeof(np->regdump); i++)
6394     			((char*)&np->regdump)[i] = INB_OFF(i);
6395     		np->regdump.nc_dstat = dstat;
6396     		np->regdump.nc_sist  = sist;
6397     	};
6398     
6399     	ncr_log_hard_error(np, sist, dstat);
6400     
6401     	printk ("%s: have to clear fifos.\n", ncr_name (np));
6402     	OUTB (nc_stest3, TE|CSF);
6403     	OUTONB (nc_ctest3, CLF);
6404     
6405     	if ((sist & (SGE)) ||
6406     		(dstat & (MDPE|BF|ABRT|IID))) {
6407     		ncr_start_reset(np);
6408     		return;
6409     	};
6410     
6411     	if (sist & HTH) {
6412     		printk ("%s: handshake timeout\n", ncr_name(np));
6413     		ncr_start_reset(np);
6414     		return;
6415     	};
6416     
6417     	if (sist & UDC) {
6418     		printk ("%s: unexpected disconnect\n", ncr_name(np));
6419     		OUTB (HS_PRT, HS_UNEXPECTED);
6420     		OUTL_DSP (NCB_SCRIPT_PHYS (np, cleanup));
6421     		return;
6422     	};
6423     
6424     	/*=========================================================
6425     	**	We just miss the cause of the interrupt. :(
6426     	**	Print a message. The timeout will do the real work.
6427     	**=========================================================
6428     	*/
6429     	printk ("%s: unknown interrupt\n", ncr_name(np));
6430     }
6431     
6432     /*==========================================================
6433     **
6434     **	ncr chip exception handler for selection timeout
6435     **
6436     **==========================================================
6437     **
6438     **	There seems to be a bug in the 53c810.
6439     **	Although a STO-Interrupt is pending,
6440     **	it continues executing script commands.
6441     **	But it will fail and interrupt (IID) on
6442     **	the next instruction where it's looking
6443     **	for a valid phase.
6444     **
6445     **----------------------------------------------------------
6446     */
6447     
6448     void ncr_int_sto (ncb_p np)
6449     {
6450     	u_long dsa;
6451     	ccb_p cp;
6452     	if (DEBUG_FLAGS & DEBUG_TINY) printk ("T");
6453     
6454     	/*
6455     	**	look for ccb and set the status.
6456     	*/
6457     
6458     	dsa = INL (nc_dsa);
6459     	cp = np->ccb;
6460     	while (cp && (CCB_PHYS (cp, phys) != dsa))
6461     		cp = cp->link_ccb;
6462     
6463     	if (cp) {
6464     		cp-> host_status = HS_SEL_TIMEOUT;
6465     		ncr_complete (np, cp);
6466     	};
6467     
6468     	/*
6469     	**	repair start queue and jump to start point.
6470     	*/
6471     
6472     	OUTL_DSP (NCB_SCRIPTH_PHYS (np, sto_restart));
6473     	return;
6474     }
6475     
6476     /*==========================================================
6477     **
6478     **	ncr chip exception handler for SCSI bus mode change
6479     **
6480     **==========================================================
6481     **
6482     **	spi2-r12 11.2.3 says a transceiver mode change must 
6483     **	generate a reset event and a device that detects a reset 
6484     **	event shall initiate a hard reset. It says also that a
6485     **	device that detects a mode change shall set data transfer 
6486     **	mode to eight bit asynchronous, etc...
6487     **	So, just resetting should be enough.
6488     **	 
6489     **
6490     **----------------------------------------------------------
6491     */
6492     
6493     static int ncr_int_sbmc (ncb_p np)
6494     {
6495     	u_char scsi_mode = INB (nc_stest4) & SMODE;
6496     
6497     	if (scsi_mode != np->scsi_mode) {
6498     		printk("%s: SCSI bus mode change from %x to %x.\n",
6499     			ncr_name(np), np->scsi_mode, scsi_mode);
6500     
6501     		np->scsi_mode = scsi_mode;
6502     
6503     
6504     		/*
6505     		**	Suspend command processing for 1 second and 
6506     		**	reinitialize all except the chip.
6507     		*/
6508     		np->settle_time	= ktime_get(1*HZ);
6509     		ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
6510     		return 1;
6511     	}
6512     	return 0;
6513     }
6514     
6515     /*==========================================================
6516     **
6517     **	ncr chip exception handler for SCSI parity error.
6518     **
6519     **==========================================================
6520     **
6521     **
6522     **----------------------------------------------------------
6523     */
6524     
6525     static int ncr_int_par (ncb_p np)
6526     {
6527     	u_char	hsts	= INB (HS_PRT);
6528     	u_int32	dbc	= INL (nc_dbc);
6529     	u_char	sstat1	= INB (nc_sstat1);
6530     	int phase	= -1;
6531     	int msg		= -1;
6532     	u_int32 jmp;
6533     
6534     	printk("%s: SCSI parity error detected: SCR1=%d DBC=%x SSTAT1=%x\n",
6535     		ncr_name(np), hsts, dbc, sstat1);
6536     
6537     	/*
6538     	 *	Ignore the interrupt if the NCR is not connected 
6539     	 *	to the SCSI bus, since the right work should have  
6540     	 *	been done on unexpected disconnection handling.
6541     	 */
6542     	if (!(INB (nc_scntl1) & ISCON))
6543     		return 0;
6544     
6545     	/*
6546     	 *	If the nexus is not clearly identified, reset the bus.
6547     	 *	We will try to do better later.
6548     	 */
6549     	if (hsts & HS_INVALMASK)
6550     		goto reset_all;
6551     
6552     	/*
6553     	 *	If the SCSI parity error occurs in MSG IN phase, prepare a 
6554     	 *	MSG PARITY message. Otherwise, prepare a INITIATOR DETECTED 
6555     	 *	ERROR message and let the device decide to retry the command 
6556     	 *	or to terminate with check condition. If we were in MSG IN 
6557     	 *	phase waiting for the response of a negotiation, we will 
6558     	 *	get SIR_NEGO_FAILED at dispatch.
6559     	 */
6560     	if (!(dbc & 0xc0000000))
6561     		phase = (dbc >> 24) & 7;
6562     	if (phase == 7)
6563     		msg = M_PARITY;
6564     	else
6565     		msg = M_ID_ERROR;
6566     
6567     #ifdef SCSI_NCR_INTEGRITY_CHECKING
6568     	/*
6569     	**      Save error message. For integrity check use only.
6570     	*/
6571     	if (np->check_integrity)
6572     		np->check_integ_par = msg;
6573     #endif
6574     
6575     	/*
6576     	 *	If the NCR stopped on a MOVE ^ DATA_IN, we jump to a 
6577     	 *	script that will ignore all data in bytes until phase 
6578     	 *	change, since we are not sure the chip will wait the phase 
6579     	 *	change prior to delivering the interrupt.
6580     	 */
6581     	if (phase == 1)
6582     		jmp = NCB_SCRIPTH_PHYS (np, par_err_data_in);
6583     	else
6584     		jmp = NCB_SCRIPTH_PHYS (np, par_err_other);
6585     
6586     	OUTONB (nc_ctest3, CLF );	/* clear dma fifo  */
6587     	OUTB (nc_stest3, TE|CSF);	/* clear scsi fifo */
6588     
6589     	np->msgout[0] = msg;
6590     	OUTL_DSP (jmp);
6591     	return 1;
6592     
6593     reset_all:
6594     	ncr_start_reset(np);
6595     	return 1;
6596     }
6597     
6598     /*==========================================================
6599     **
6600     **
6601     **	ncr chip exception handler for phase errors.
6602     **
6603     **
6604     **==========================================================
6605     **
6606     **	We have to construct a new transfer descriptor,
6607     **	to transfer the rest of the current block.
6608     **
6609     **----------------------------------------------------------
6610     */
6611     
6612     static void ncr_int_ma (ncb_p np)
6613     {
6614     	u_int32	dbc;
6615     	u_int32	rest;
6616     	u_int32	dsp;
6617     	u_int32	dsa;
6618     	u_int32	nxtdsp;
6619     	u_int32	newtmp;
6620     	u_int32	*vdsp;
6621     	u_int32	oadr, olen;
6622     	u_int32	*tblp;
6623             ncrcmd *newcmd;
6624     	u_char	cmd, sbcl;
6625     	ccb_p	cp;
6626     
6627     	dsp	= INL (nc_dsp);
6628     	dbc	= INL (nc_dbc);
6629     	sbcl	= INB (nc_sbcl);
6630     
6631     	cmd	= dbc >> 24;
6632     	rest	= dbc & 0xffffff;
6633     
6634     	/*
6635     	**	Take into account dma fifo and various buffers and latches,
6636     	**	only if the interrupted phase is an OUTPUT phase.
6637     	*/
6638     
6639     	if ((cmd & 1) == 0) {
6640     		u_char	ctest5, ss0, ss2;
6641     		u_short	delta;
6642     
6643     		ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
6644     		if (ctest5 & DFS)
6645     			delta=(((ctest5 << 8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
6646     		else
6647     			delta=(INB (nc_dfifo) - rest) & 0x7f;
6648     
6649     		/*
6650     		**	The data in the dma fifo has not been transferred to
6651     		**	the target -> add the amount to the rest
6652     		**	and clear the data.
6653     		**	Check the sstat2 register in case of wide transfer.
6654     		*/
6655     
6656     		rest += delta;
6657     		ss0  = INB (nc_sstat0);
6658     		if (ss0 & OLF) rest++;
6659     		if (ss0 & ORF) rest++;
6660     		if (INB(nc_scntl3) & EWS) {
6661     			ss2 = INB (nc_sstat2);
6662     			if (ss2 & OLF1) rest++;
6663     			if (ss2 & ORF1) rest++;
6664     		};
6665     
6666     		if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
6667     			printk ("P%x%x RL=%d D=%d SS0=%x ", cmd&7, sbcl&7,
6668     				(unsigned) rest, (unsigned) delta, ss0);
6669     
6670     	} else	{
6671     		if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
6672     			printk ("P%x%x RL=%d ", cmd&7, sbcl&7, rest);
6673     	}
6674     
6675     	/*
6676     	**	Clear fifos.
6677     	*/
6678     	OUTONB (nc_ctest3, CLF );	/* clear dma fifo  */
6679     	OUTB (nc_stest3, TE|CSF);	/* clear scsi fifo */
6680     
6681     	/*
6682     	**	locate matching cp.
6683     	**	if the interrupted phase is DATA IN or DATA OUT,
6684     	**	trust the global header.
6685     	*/
6686     	dsa = INL (nc_dsa);
6687     	if (!(cmd & 6)) {
6688     		cp = np->header.cp;
6689     		if (CCB_PHYS(cp, phys) != dsa)
6690     			cp = 0;
6691     	} else {
6692     		cp  = np->ccb;
6693     		while (cp && (CCB_PHYS (cp, phys) != dsa))
6694     			cp = cp->link_ccb;
6695     	}
6696     
6697     	/*
6698     	**	try to find the interrupted script command,
6699     	**	and the address at which to continue.
6700     	*/
6701     	vdsp	= 0;
6702     	nxtdsp	= 0;
6703     	if	(dsp >  np->p_script &&
6704     		 dsp <= np->p_script + sizeof(struct script)) {
6705     		vdsp = (u_int32 *)((char*)np->script0 + (dsp-np->p_script-8));
6706     		nxtdsp = dsp;
6707     	}
6708     	else if	(dsp >  np->p_scripth &&
6709     		 dsp <= np->p_scripth + sizeof(struct scripth)) {
6710     		vdsp = (u_int32 *)((char*)np->scripth0 + (dsp-np->p_scripth-8));
6711     		nxtdsp = dsp;
6712     	}
6713     	else if (cp) {
6714     		if	(dsp == CCB_PHYS (cp, patch[2])) {
6715     			vdsp = &cp->patch[0];
6716     			nxtdsp = scr_to_cpu(vdsp[3]);
6717     		}
6718     		else if (dsp == CCB_PHYS (cp, patch[6])) {
6719     			vdsp = &cp->patch[4];
6720     			nxtdsp = scr_to_cpu(vdsp[3]);
6721     		}
6722     	}
6723     
6724     	/*
6725     	**	log the information
6726     	*/
6727     
6728     	if (DEBUG_FLAGS & DEBUG_PHASE) {
6729     		printk ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
6730     			cp, np->header.cp,
6731     			(unsigned)dsp,
6732     			(unsigned)nxtdsp, vdsp, cmd);
6733     	};
6734     
6735     	/*
6736     	**	cp=0 means that the DSA does not point to a valid control 
6737     	**	block. This should not happen since we donnot use multi-byte 
6738     	**	move while we are being reselected ot after command complete.
6739     	**	We are not able to recover from such a phase error.
6740     	*/
6741     	if (!cp) {
6742     		printk ("%s: SCSI phase error fixup: "
6743     			"CCB already dequeued (0x%08lx)\n", 
6744     			ncr_name (np), (u_long) np->header.cp);
6745     		goto reset_all;
6746     	}
6747     
6748     	/*
6749     	**	get old startaddress and old length.
6750     	*/
6751     
6752     	oadr = scr_to_cpu(vdsp[1]);
6753     
6754     	if (cmd & 0x10) {	/* Table indirect */
6755     		tblp = (u_int32 *) ((char*) &cp->phys + oadr);
6756     		olen = scr_to_cpu(tblp[0]);
6757     		oadr = scr_to_cpu(tblp[1]);
6758     	} else {
6759     		tblp = (u_int32 *) 0;
6760     		olen = scr_to_cpu(vdsp[0]) & 0xffffff;
6761     	};
6762     
6763     	if (DEBUG_FLAGS & DEBUG_PHASE) {
6764     		printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
6765     			(unsigned) (scr_to_cpu(vdsp[0]) >> 24),
6766     			tblp,
6767     			(unsigned) olen,
6768     			(unsigned) oadr);
6769     	};
6770     
6771     	/*
6772     	**	check cmd against assumed interrupted script command.
6773     	*/
6774     
6775     	if (cmd != (scr_to_cpu(vdsp[0]) >> 24)) {
6776     		PRINT_ADDR(cp->cmd);
6777     		printk ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
6778     			(unsigned)cmd, (unsigned)scr_to_cpu(vdsp[0]) >> 24);
6779     
6780     		goto reset_all;
6781     	}
6782     
6783     	/*
6784     	**	cp != np->header.cp means that the header of the CCB 
6785     	**	currently being processed has not yet been copied to 
6786     	**	the global header area. That may happen if the device did 
6787     	**	not accept all our messages after having been selected.
6788     	*/
6789     	if (cp != np->header.cp) {
6790     		printk ("%s: SCSI phase error fixup: "
6791     			"CCB address mismatch (0x%08lx != 0x%08lx)\n", 
6792     			ncr_name (np), (u_long) cp, (u_long) np->header.cp);
6793     	}
6794     
6795     	/*
6796     	**	if old phase not dataphase, leave here.
6797     	*/
6798     
6799     	if (cmd & 0x06) {
6800     		PRINT_ADDR(cp->cmd);
6801     		printk ("phase change %x-%x %d@%08x resid=%d.\n",
6802     			cmd&7, sbcl&7, (unsigned)olen,
6803     			(unsigned)oadr, (unsigned)rest);
6804     		goto unexpected_phase;
6805     	};
6806     
6807     	/*
6808     	**	choose the correct patch area.
6809     	**	if savep points to one, choose the other.
6810     	*/
6811     
6812     	newcmd = cp->patch;
6813     	newtmp = CCB_PHYS (cp, patch);
6814     	if (newtmp == scr_to_cpu(cp->phys.header.savep)) {
6815     		newcmd = &cp->patch[4];
6816     		newtmp = CCB_PHYS (cp, patch[4]);
6817     	}
6818     
6819     	/*
6820     	**	fillin the commands
6821     	*/
6822     
6823     	newcmd[0] = cpu_to_scr(((cmd & 0x0f) << 24) | rest);
6824     	newcmd[1] = cpu_to_scr(oadr + olen - rest);
6825     	newcmd[2] = cpu_to_scr(SCR_JUMP);
6826     	newcmd[3] = cpu_to_scr(nxtdsp);
6827     
6828     	if (DEBUG_FLAGS & DEBUG_PHASE) {
6829     		PRINT_ADDR(cp->cmd);
6830     		printk ("newcmd[%d] %x %x %x %x.\n",
6831     			(int) (newcmd - cp->patch),
6832     			(unsigned)scr_to_cpu(newcmd[0]),
6833     			(unsigned)scr_to_cpu(newcmd[1]),
6834     			(unsigned)scr_to_cpu(newcmd[2]),
6835     			(unsigned)scr_to_cpu(newcmd[3]));
6836     	}
6837     	/*
6838     	**	fake the return address (to the patch).
6839     	**	and restart script processor at dispatcher.
6840     	*/
6841     	OUTL (nc_temp, newtmp);
6842     	OUTL_DSP (NCB_SCRIPT_PHYS (np, dispatch));
6843     	return;
6844     
6845     	/*
6846     	**	Unexpected phase changes that occurs when the current phase 
6847     	**	is not a DATA IN or DATA OUT phase are due to error conditions.
6848     	**	Such event may only happen when the SCRIPTS is using a 
6849     	**	multibyte SCSI MOVE.
6850     	**
6851     	**	Phase change		Some possible cause
6852     	**
6853     	**	COMMAND  --> MSG IN	SCSI parity error detected by target.
6854     	**	COMMAND  --> STATUS	Bad command or refused by target.
6855     	**	MSG OUT  --> MSG IN     Message rejected by target.
6856     	**	MSG OUT  --> COMMAND    Bogus target that discards extended
6857     	**				negotiation messages.
6858     	**
6859     	**	The code below does not care of the new phase and so 
6860     	**	trusts the target. Why to annoy it ?
6861     	**	If the interrupted phase is COMMAND phase, we restart at
6862     	**	dispatcher.
6863     	**	If a target does not get all the messages after selection, 
6864     	**	the code assumes blindly that the target discards extended 
6865     	**	messages and clears the negotiation status.
6866     	**	If the target does not want all our response to negotiation,
6867     	**	we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids 
6868     	**	bloat for such a should_not_happen situation).
6869     	**	In all other situation, we reset the BUS.
6870     	**	Are these assumptions reasonnable ? (Wait and see ...)
6871     	*/
6872     unexpected_phase:
6873     	dsp -= 8;
6874     	nxtdsp = 0;
6875     
6876     	switch (cmd & 7) {
6877     	case 2:	/* COMMAND phase */
6878     		nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
6879     		break;
6880     #if 0
6881     	case 3:	/* STATUS  phase */
6882     		nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
6883     		break;
6884     #endif
6885     	case 6:	/* MSG OUT phase */
6886     		np->scripth->nxtdsp_go_on[0] = cpu_to_scr(dsp + 8);
6887     		if	(dsp == NCB_SCRIPT_PHYS (np, send_ident)) {
6888     			cp->host_status = HS_BUSY;
6889     			nxtdsp = NCB_SCRIPTH_PHYS (np, clratn_go_on);
6890     		}
6891     		else if	(dsp == NCB_SCRIPTH_PHYS (np, send_wdtr) ||
6892     			 dsp == NCB_SCRIPTH_PHYS (np, send_sdtr)) {
6893     			nxtdsp = NCB_SCRIPTH_PHYS (np, nego_bad_phase);
6894     		}
6895     		break;
6896     #if 0
6897     	case 7:	/* MSG IN  phase */
6898     		nxtdsp = NCB_SCRIPT_PHYS (np, clrack);
6899     		break;
6900     #endif
6901     	}
6902     
6903     	if (nxtdsp) {
6904     		OUTL_DSP (nxtdsp);
6905     		return;
6906     	}
6907     
6908     reset_all:
6909     	ncr_start_reset(np);
6910     }
6911     
6912     
6913     static void ncr_sir_to_redo(ncb_p np, int num, ccb_p cp)
6914     {
6915     	Scsi_Cmnd *cmd	= cp->cmd;
6916     	tcb_p tp	= &np->target[cmd->target];
6917     	lcb_p lp	= tp->lp[cmd->lun];
6918     	XPT_QUEHEAD	*qp;
6919     	ccb_p		cp2;
6920     	int		disc_cnt = 0;
6921     	int		busy_cnt = 0;
6922     	u_int32		startp;
6923     	u_char		s_status = INB (SS_PRT);
6924     
6925     	/*
6926     	**	Let the SCRIPTS processor skip all not yet started CCBs,
6927     	**	and count disconnected CCBs. Since the busy queue is in 
6928     	**	the same order as the chip start queue, disconnected CCBs 
6929     	**	are before cp and busy ones after.
6930     	*/
6931     	if (lp) {
6932     		qp = lp->busy_ccbq.blink;
6933     		while (qp != &lp->busy_ccbq) {
6934     			cp2 = xpt_que_entry(qp, struct ccb, link_ccbq);
6935     			qp  = qp->blink;
6936     			++busy_cnt;
6937     			if (cp2 == cp)
6938     				break;
6939     			cp2->start.schedule.l_paddr =
6940     			cpu_to_scr(NCB_SCRIPTH_PHYS (np, skip));
6941     		}
6942     		lp->held_ccb = cp;	/* Requeue when this one completes */
6943     		disc_cnt = lp->queuedccbs - busy_cnt;
6944     	}
6945     
6946     	switch(s_status) {
6947     	default:	/* Just for safety, should never happen */
6948     	case S_QUEUE_FULL:
6949     		/*
6950     		**	Decrease number of tags to the number of 
6951     		**	disconnected commands.
6952     		*/
6953     		if (!lp)
6954     			goto out;
6955     		if (bootverbose >= 1) {
6956     			PRINT_ADDR(cmd);
6957     			printk ("QUEUE FULL! %d busy, %d disconnected CCBs\n",
6958     				busy_cnt, disc_cnt);
6959     		}
6960     		if (disc_cnt < lp->numtags) {
6961     			lp->numtags	= disc_cnt > 2 ? disc_cnt : 2;
6962     			lp->num_good	= 0;
6963     			ncr_setup_tags (np, cmd->target, cmd->lun);
6964     		}
6965     		/*
6966     		**	Requeue the command to the start queue.
6967     		**	If any disconnected commands,
6968     		**		Clear SIGP.
6969     		**		Jump to reselect.
6970     		*/
6971     		cp->phys.header.savep = cp->startp;
6972     		cp->host_status = HS_BUSY;
6973     		cp->scsi_status = S_ILLEGAL;
6974     
6975     		ncr_put_start_queue(np, cp);
6976     		if (disc_cnt)
6977     			INB (nc_ctest2);		/* Clear SIGP */
6978     		OUTL_DSP (NCB_SCRIPT_PHYS (np, reselect));
6979     		return;
6980     	case S_TERMINATED:
6981     	case S_CHECK_COND:
6982     		/*
6983     		**	If we were requesting sense, give up.
6984     		*/
6985     		if (cp->auto_sense)
6986     			goto out;
6987     
6988     		/*
6989     		**	Device returned CHECK CONDITION status.
6990     		**	Prepare all needed data strutures for getting 
6991     		**	sense data.
6992     		**
6993     		**	identify message
6994     		*/
6995     		cp->scsi_smsg2[0]	= M_IDENTIFY | cmd->lun;
6996     		cp->phys.smsg.addr	= cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
6997     		cp->phys.smsg.size	= cpu_to_scr(1);
6998     
6999     		/*
7000     		**	sense command
7001     		*/
7002     		cp->phys.cmd.addr	= cpu_to_scr(CCB_PHYS (cp, sensecmd));
7003     		cp->phys.cmd.size	= cpu_to_scr(6);
7004     
7005     		/*
7006     		**	patch requested size into sense command
7007     		*/
7008     		cp->sensecmd[0]		= 0x03;
7009     		cp->sensecmd[1]		= cmd->lun << 5;
7010     		cp->sensecmd[4]		= sizeof(cp->sense_buf);
7011     
7012     		/*
7013     		**	sense data
7014     		*/
7015     		bzero(cp->sense_buf, sizeof(cp->sense_buf));
7016     		cp->phys.sense.addr	= cpu_to_scr(CCB_PHYS(cp,sense_buf[0]));
7017     		cp->phys.sense.size	= cpu_to_scr(sizeof(cp->sense_buf));
7018     
7019     		/*
7020     		**	requeue the command.
7021     		*/
7022     		startp = cpu_to_scr(NCB_SCRIPTH_PHYS (np, sdata_in));
7023     
7024     		cp->phys.header.savep	= startp;
7025     		cp->phys.header.goalp	= startp + 24;
7026     		cp->phys.header.lastp	= startp;
7027     		cp->phys.header.wgoalp	= startp + 24;
7028     		cp->phys.header.wlastp	= startp;
7029     
7030     		cp->host_status = HS_BUSY;
7031     		cp->scsi_status = S_ILLEGAL;
7032     		cp->auto_sense	= s_status;
7033     
7034     		cp->start.schedule.l_paddr =
7035     			cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
7036     
7037     		/*
7038     		**	Select without ATN for quirky devices.
7039     		*/
7040     		if (tp->quirks & QUIRK_NOMSG)
7041     			cp->start.schedule.l_paddr =
7042     			cpu_to_scr(NCB_SCRIPTH_PHYS (np, select_no_atn));
7043     
7044     		ncr_put_start_queue(np, cp);
7045     
7046     		OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
7047     		return;
7048     	}
7049     
7050     out:
7051     	OUTONB_STD ();
7052     	return;
7053     }
7054     
7055     
7056     /*==========================================================
7057     **
7058     **
7059     **      ncr chip exception handler for programmed interrupts.
7060     **
7061     **
7062     **==========================================================
7063     */
7064     
7065     static int ncr_show_msg (u_char * msg)
7066     {
7067     	u_char i;
7068     	printk ("%x",*msg);
7069     	if (*msg==M_EXTENDED) {
7070     		for (i=1;i<8;i++) {
7071     			if (i-1>msg[1]) break;
7072     			printk ("-%x",msg[i]);
7073     		};
7074     		return (i+1);
7075     	} else if ((*msg & 0xf0) == 0x20) {
7076     		printk ("-%x",msg[1]);
7077     		return (2);
7078     	};
7079     	return (1);
7080     }
7081     
7082     static void ncr_print_msg ( ccb_p cp, char *label, u_char *msg)
7083     {
7084     	if (cp)
7085     		PRINT_ADDR(cp->cmd);
7086     	if (label)
7087     		printk("%s: ", label);
7088     	
7089     	(void) ncr_show_msg (msg);
7090     	printk(".\n");
7091     }
7092     
7093     void ncr_int_sir (ncb_p np)
7094     {
7095     	u_char scntl3;
7096     	u_char chg, ofs, per, fak, wide;
7097     	u_char num = INB (nc_dsps);
7098     	ccb_p	cp=0;
7099     	u_long	dsa    = INL (nc_dsa);
7100     	u_char	target = INB (nc_sdid) & 0x0f;
7101     	tcb_p	tp     = &np->target[target];
7102     
7103     	if (DEBUG_FLAGS & DEBUG_TINY) printk ("I#%d", num);
7104     
7105     	switch (num) {
7106     	case SIR_RESEL_NO_MSG_IN:
7107     	case SIR_RESEL_NO_IDENTIFY:
7108     		/*
7109     		**	If devices reselecting without sending an IDENTIFY 
7110     		**	message still exist, this should help.
7111     		**	We just assume lun=0, 1 CCB, no tag.
7112     		*/
7113     		if (tp->lp[0]) { 
7114     			OUTL_DSP (scr_to_cpu(tp->lp[0]->jump_ccb[0]));
7115     			return;
7116     		}
7117     	case SIR_RESEL_BAD_TARGET:	/* Will send a TARGET RESET message */
7118     	case SIR_RESEL_BAD_LUN:		/* Will send a TARGET RESET message */
7119     	case SIR_RESEL_BAD_I_T_L_Q:	/* Will send an ABORT TAG message   */
7120     	case SIR_RESEL_BAD_I_T_L:	/* Will send an ABORT message	    */
7121     		printk ("%s:%d: SIR %d, "
7122     			"incorrect nexus identification on reselection\n",
7123     			ncr_name (np), target, num);
7124     		goto out;
7125     	case SIR_DONE_OVERFLOW:
7126     		printk ("%s:%d: SIR %d, "
7127     			"CCB done queue overflow\n",
7128     			ncr_name (np), target, num);
7129     		goto out;
7130     	case SIR_BAD_STATUS:
7131     		cp = np->header.cp;
7132     		if (!cp || CCB_PHYS (cp, phys) != dsa)
7133     			goto out;
7134     		ncr_sir_to_redo(np, num, cp);
7135     		return;
7136     	default:
7137     		/*
7138     		**	lookup the ccb
7139     		*/
7140     		cp = np->ccb;
7141     		while (cp && (CCB_PHYS (cp, phys) != dsa))
7142     			cp = cp->link_ccb;
7143     
7144     		assert (cp && cp == np->header.cp);
7145     
7146     		if (!cp || cp != np->header.cp)
7147     			goto out;
7148     	}
7149     
7150     	switch (num) {
7151     /*-----------------------------------------------------------------------------
7152     **
7153     **	Was Sie schon immer ueber transfermode negotiation wissen wollten ...
7154     **
7155     **	We try to negotiate sync and wide transfer only after
7156     **	a successful inquire command. We look at byte 7 of the
7157     **	inquire data to determine the capabilities of the target.
7158     **
7159     **	When we try to negotiate, we append the negotiation message
7160     **	to the identify and (maybe) simple tag message.
7161     **	The host status field is set to HS_NEGOTIATE to mark this
7162     **	situation.
7163     **
7164     **	If the target doesn't answer this message immidiately
7165     **	(as required by the standard), the SIR_NEGO_FAIL interrupt
7166     **	will be raised eventually.
7167     **	The handler removes the HS_NEGOTIATE status, and sets the
7168     **	negotiated value to the default (async / nowide).
7169     **
7170     **	If we receive a matching answer immediately, we check it
7171     **	for validity, and set the values.
7172     **
7173     **	If we receive a Reject message immediately, we assume the
7174     **	negotiation has failed, and fall back to standard values.
7175     **
7176     **	If we receive a negotiation message while not in HS_NEGOTIATE
7177     **	state, it's a target initiated negotiation. We prepare a
7178     **	(hopefully) valid answer, set our parameters, and send back 
7179     **	this answer to the target.
7180     **
7181     **	If the target doesn't fetch the answer (no message out phase),
7182     **	we assume the negotiation has failed, and fall back to default
7183     **	settings.
7184     **
7185     **	When we set the values, we adjust them in all ccbs belonging 
7186     **	to this target, in the controller's register, and in the "phys"
7187     **	field of the controller's struct ncb.
7188     **
7189     **	Possible cases:		   hs  sir   msg_in value  send   goto
7190     **	We try to negotiate:
7191     **	-> target doesnt't msgin   NEG FAIL  noop   defa.  -      dispatch
7192     **	-> target rejected our msg NEG FAIL  reject defa.  -      dispatch
7193     **	-> target answered  (ok)   NEG SYNC  sdtr   set    -      clrack
7194     **	-> target answered (!ok)   NEG SYNC  sdtr   defa.  REJ--->msg_bad
7195     **	-> target answered  (ok)   NEG WIDE  wdtr   set    -      clrack
7196     **	-> target answered (!ok)   NEG WIDE  wdtr   defa.  REJ--->msg_bad
7197     **	-> any other msgin	   NEG FAIL  noop   defa.  -      dispatch
7198     **
7199     **	Target tries to negotiate:
7200     **	-> incoming message	   --- SYNC  sdtr   set    SDTR   -
7201     **	-> incoming message	   --- WIDE  wdtr   set    WDTR   -
7202     **      We sent our answer:
7203     **	-> target doesn't msgout   --- PROTO ?      defa.  -      dispatch
7204     **
7205     **-----------------------------------------------------------------------------
7206     */
7207     
7208     	case SIR_NEGO_FAILED:
7209     		/*-------------------------------------------------------
7210     		**
7211     		**	Negotiation failed.
7212     		**	Target doesn't send an answer message,
7213     		**	or target rejected our message.
7214     		**
7215     		**      Remove negotiation request.
7216     		**
7217     		**-------------------------------------------------------
7218     		*/
7219     		OUTB (HS_PRT, HS_BUSY);
7220     
7221     		/* fall through */
7222     
7223     	case SIR_NEGO_PROTO:
7224     		/*-------------------------------------------------------
7225     		**
7226     		**	Negotiation failed.
7227     		**	Target doesn't fetch the answer message.
7228     		**
7229     		**-------------------------------------------------------
7230     		*/
7231     
7232     		if (DEBUG_FLAGS & DEBUG_NEGO) {
7233     			PRINT_ADDR(cp->cmd);
7234     			printk ("negotiation failed sir=%x status=%x.\n",
7235     				num, cp->nego_status);
7236     		};
7237     
7238     		/*
7239     		**	any error in negotiation:
7240     		**	fall back to default mode.
7241     		*/
7242     		switch (cp->nego_status) {
7243     
7244     		case NS_SYNC:
7245     			ncr_setsync (np, cp, 0, 0xe0);
7246     			break;
7247     
7248     		case NS_WIDE:
7249     			ncr_setwide (np, cp, 0, 0);
7250     			break;
7251     
7252     		};
7253     		np->msgin [0] = M_NOOP;
7254     		np->msgout[0] = M_NOOP;
7255     		cp->nego_status = 0;
7256     		break;
7257     
7258     	case SIR_NEGO_SYNC:
7259     		/*
7260     		**	Synchronous request message received.
7261     		*/
7262     
7263     		if (DEBUG_FLAGS & DEBUG_NEGO) {
7264     			PRINT_ADDR(cp->cmd);
7265     			printk ("sync msgin: ");
7266     			(void) ncr_show_msg (np->msgin);
7267     			printk (".\n");
7268     		};
7269     
7270     		/*
7271     		**	get requested values.
7272     		*/
7273     
7274     		chg = 0;
7275     		per = np->msgin[3];
7276     		ofs = np->msgin[4];
7277     		if (ofs==0) per=255;
7278     
7279     		/*
7280     		**      if target sends SDTR message,
7281     		**	      it CAN transfer synch.
7282     		*/
7283     
7284     		if (ofs)
7285     			tp->inq_byte7 |= INQ7_SYNC;
7286     
7287     		/*
7288     		**	check values against driver limits.
7289     		*/
7290     
7291     		if (per < np->minsync)
7292     			{chg = 1; per = np->minsync;}
7293     		if (per < tp->minsync)
7294     			{chg = 1; per = tp->minsync;}
7295     		if (ofs > tp->maxoffs)
7296     			{chg = 1; ofs = tp->maxoffs;}
7297     
7298     		/*
7299     		**	Check against controller limits.
7300     		*/
7301     		fak	= 7;
7302     		scntl3	= 0;
7303     		if (ofs != 0) {
7304     			ncr_getsync(np, per, &fak, &scntl3);
7305     			if (fak > 7) {
7306     				chg = 1;
7307     				ofs = 0;
7308     			}
7309     		}
7310     		if (ofs == 0) {
7311     			fak	= 7;
7312     			per	= 0;
7313     			scntl3	= 0;
7314     			tp->minsync = 0;
7315     		}
7316     
7317     		if (DEBUG_FLAGS & DEBUG_NEGO) {
7318     			PRINT_ADDR(cp->cmd);
7319     			printk ("sync: per=%d scntl3=0x%x ofs=%d fak=%d chg=%d.\n",
7320     				per, scntl3, ofs, fak, chg);
7321     		}
7322     
7323     		if (INB (HS_PRT) == HS_NEGOTIATE) {
7324     			OUTB (HS_PRT, HS_BUSY);
7325     			switch (cp->nego_status) {
7326     
7327     			case NS_SYNC:
7328     				/*
7329     				**      This was an answer message
7330     				*/
7331     				if (chg) {
7332     					/*
7333     					**	Answer wasn't acceptable.
7334     					*/
7335     					ncr_setsync (np, cp, 0, 0xe0);
7336     					OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
7337     				} else {
7338     					/*
7339     					**	Answer is ok.
7340     					*/
7341     					ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
7342     					OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
7343     				};
7344     				return;
7345     
7346     			case NS_WIDE:
7347     				ncr_setwide (np, cp, 0, 0);
7348     				break;
7349     			};
7350     		};
7351     
7352     		/*
7353     		**	It was a request. Set value and
7354     		**      prepare an answer message
7355     		*/
7356     
7357     		ncr_setsync (np, cp, scntl3, (fak<<5)|ofs);
7358     
7359     		np->msgout[0] = M_EXTENDED;
7360     		np->msgout[1] = 3;
7361     		np->msgout[2] = M_X_SYNC_REQ;
7362     		np->msgout[3] = per;
7363     		np->msgout[4] = ofs;
7364     
7365     		cp->nego_status = NS_SYNC;
7366     
7367     		if (DEBUG_FLAGS & DEBUG_NEGO) {
7368     			PRINT_ADDR(cp->cmd);
7369     			printk ("sync msgout: ");
7370     			(void) ncr_show_msg (np->msgout);
7371     			printk (".\n");
7372     		}
7373     
7374     		if (!ofs) {
7375     			OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
7376     			return;
7377     		}
7378     		np->msgin [0] = M_NOOP;
7379     
7380     		break;
7381     
7382     	case SIR_NEGO_WIDE:
7383     		/*
7384     		**	Wide request message received.
7385     		*/
7386     		if (DEBUG_FLAGS & DEBUG_NEGO) {
7387     			PRINT_ADDR(cp->cmd);
7388     			printk ("wide msgin: ");
7389     			(void) ncr_show_msg (np->msgin);
7390     			printk (".\n");
7391     		};
7392     
7393     		/*
7394     		**	get requested values.
7395     		*/
7396     
7397     		chg  = 0;
7398     		wide = np->msgin[3];
7399     
7400     		/*
7401     		**      if target sends WDTR message,
7402     		**	      it CAN transfer wide.
7403     		*/
7404     
7405     		if (wide)
7406     			tp->inq_byte7 |= INQ7_WIDE16;
7407     
7408     		/*
7409     		**	check values against driver limits.
7410     		*/
7411     
7412     		if (wide > tp->usrwide)
7413     			{chg = 1; wide = tp->usrwide;}
7414     
7415     		if (DEBUG_FLAGS & DEBUG_NEGO) {
7416     			PRINT_ADDR(cp->cmd);
7417     			printk ("wide: wide=%d chg=%d.\n", wide, chg);
7418     		}
7419     
7420     		if (INB (HS_PRT) == HS_NEGOTIATE) {
7421     			OUTB (HS_PRT, HS_BUSY);
7422     			switch (cp->nego_status) {
7423     
7424     			case NS_WIDE:
7425     				/*
7426     				**      This was an answer message
7427     				*/
7428     				if (chg) {
7429     					/*
7430     					**	Answer wasn't acceptable.
7431     					*/
7432     					ncr_setwide (np, cp, 0, 1);
7433     					OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
7434     				} else {
7435     					/*
7436     					**	Answer is ok.
7437     					*/
7438     					ncr_setwide (np, cp, wide, 1);
7439     					OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
7440     				};
7441     				return;
7442     
7443     			case NS_SYNC:
7444     				ncr_setsync (np, cp, 0, 0xe0);
7445     				break;
7446     			};
7447     		};
7448     
7449     		/*
7450     		**	It was a request, set value and
7451     		**      prepare an answer message
7452     		*/
7453     
7454     		ncr_setwide (np, cp, wide, 1);
7455     
7456     		np->msgout[0] = M_EXTENDED;
7457     		np->msgout[1] = 2;
7458     		np->msgout[2] = M_X_WIDE_REQ;
7459     		np->msgout[3] = wide;
7460     
7461     		np->msgin [0] = M_NOOP;
7462     
7463     		cp->nego_status = NS_WIDE;
7464     
7465     		if (DEBUG_FLAGS & DEBUG_NEGO) {
7466     			PRINT_ADDR(cp->cmd);
7467     			printk ("wide msgout: ");
7468     			(void) ncr_show_msg (np->msgin);
7469     			printk (".\n");
7470     		}
7471     		break;
7472     
7473     /*--------------------------------------------------------------------
7474     **
7475     **	Processing of special messages
7476     **
7477     **--------------------------------------------------------------------
7478     */
7479     
7480     	case SIR_REJECT_RECEIVED:
7481     		/*-----------------------------------------------
7482     		**
7483     		**	We received a M_REJECT message.
7484     		**
7485     		**-----------------------------------------------
7486     		*/
7487     
7488     		PRINT_ADDR(cp->cmd);
7489     		printk ("M_REJECT received (%x:%x).\n",
7490     			(unsigned)scr_to_cpu(np->lastmsg), np->msgout[0]);
7491     		break;
7492     
7493     	case SIR_REJECT_SENT:
7494     		/*-----------------------------------------------
7495     		**
7496     		**	We received an unknown message
7497     		**
7498     		**-----------------------------------------------
7499     		*/
7500     
7501     		PRINT_ADDR(cp->cmd);
7502     		printk ("M_REJECT sent for ");
7503     		(void) ncr_show_msg (np->msgin);
7504     		printk (".\n");
7505     		break;
7506     
7507     /*--------------------------------------------------------------------
7508     **
7509     **	Processing of special messages
7510     **
7511     **--------------------------------------------------------------------
7512     */
7513     
7514     	case SIR_IGN_RESIDUE:
7515     		/*-----------------------------------------------
7516     		**
7517     		**	We received an IGNORE RESIDUE message,
7518     		**	which couldn't be handled by the script.
7519     		**
7520     		**-----------------------------------------------
7521     		*/
7522     
7523     		PRINT_ADDR(cp->cmd);
7524     		printk ("M_IGN_RESIDUE received, but not yet implemented.\n");
7525     		break;
7526     #if 0
7527     	case SIR_MISSING_SAVE:
7528     		/*-----------------------------------------------
7529     		**
7530     		**	We received an DISCONNECT message,
7531     		**	but the datapointer wasn't saved before.
7532     		**
7533     		**-----------------------------------------------
7534     		*/
7535     
7536     		PRINT_ADDR(cp->cmd);
7537     		printk ("M_DISCONNECT received, but datapointer not saved: "
7538     			"data=%x save=%x goal=%x.\n",
7539     			(unsigned) INL (nc_temp),
7540     			(unsigned) scr_to_cpu(np->header.savep),
7541     			(unsigned) scr_to_cpu(np->header.goalp));
7542     		break;
7543     #endif
7544     	};
7545     
7546     out:
7547     	OUTONB_STD ();
7548     }
7549     
7550     /*==========================================================
7551     **
7552     **
7553     **	Acquire a control block
7554     **
7555     **
7556     **==========================================================
7557     */
7558     
7559     static	ccb_p ncr_get_ccb (ncb_p np, u_char tn, u_char ln)
7560     {
7561     	tcb_p tp = &np->target[tn];
7562     	lcb_p lp = tp->lp[ln];
7563     	u_char tag = NO_TAG;
7564     	ccb_p cp = (ccb_p) 0;
7565     
7566     	/*
7567     	**	Lun structure available ?
7568     	*/
7569     	if (lp) {
7570     		XPT_QUEHEAD *qp;
7571     		/*
7572     		**	Keep from using more tags than we can handle.
7573     		*/
7574     		if (lp->usetags && lp->busyccbs >= lp->maxnxs)
7575     			return (ccb_p) 0;
7576     
7577     		/*
7578     		**	Allocate a new CCB if needed.
7579     		*/
7580     		if (xpt_que_empty(&lp->free_ccbq))
7581     			ncr_alloc_ccb(np, tn, ln);
7582     
7583     		/*
7584     		**	Tune tag mode if asked by user.
7585     		*/
7586     		if (lp->queuedepth != lp->numtags) {
7587     			ncr_setup_tags(np, tn, ln);
7588     		}
7589     			
7590     		/*
7591     		**	Look for free CCB
7592     		*/
7593     		qp = xpt_remque_head(&lp->free_ccbq);
7594     		if (qp) {
7595     			cp = xpt_que_entry(qp, struct ccb, link_ccbq);
7596     			if (cp->magic) {
7597     				PRINT_LUN(np, tn, ln);
7598     				printk ("ccb free list corrupted (@%p)\n", cp);
7599     				cp = 0;
7600     			}
7601     			else {
7602     				xpt_insque_tail(qp, &lp->wait_ccbq);
7603     				++lp->busyccbs;
7604     			}
7605     		}
7606     
7607     		/*
7608     		**	If a CCB is available,
7609     		**	Get a tag for this nexus if required.
7610     		*/
7611     		if (cp) {
7612     			if (lp->usetags)
7613     				tag = lp->cb_tags[lp->ia_tag];
7614     		}
7615     		else if (lp->actccbs > 0)
7616     			return (ccb_p) 0;
7617     	}
7618     
7619     	/*
7620     	**	if nothing available, take the default.
7621     	*/
7622     	if (!cp)
7623     		cp = np->ccb;
7624     
7625     	/*
7626     	**	Wait until available.
7627     	*/
7628     #if 0
7629     	while (cp->magic) {
7630     		if (flags & SCSI_NOSLEEP) break;
7631     		if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
7632     			break;
7633     	};
7634     #endif
7635     
7636     	if (cp->magic)
7637     		return ((ccb_p) 0);
7638     
7639     	cp->magic = 1;
7640     
7641     	/*
7642     	**	Move to next available tag if tag used.
7643     	*/
7644     	if (lp) {
7645     		if (tag != NO_TAG) {
7646     			++lp->ia_tag;
7647     			if (lp->ia_tag == MAX_TAGS)
7648     				lp->ia_tag = 0;
7649     			lp->tags_umap |= (((tagmap_t) 1) << tag);
7650     		}
7651     	}
7652     
7653     	/*
7654     	**	Remember all informations needed to free this CCB.
7655     	*/
7656     	cp->tag	   = tag;
7657     	cp->target = tn;
7658     	cp->lun    = ln;
7659     
7660     	if (DEBUG_FLAGS & DEBUG_TAGS) {
7661     		PRINT_LUN(np, tn, ln);
7662     		printk ("ccb @%p using tag %d.\n", cp, tag);
7663     	}
7664     
7665     	return cp;
7666     }
7667     
7668     /*==========================================================
7669     **
7670     **
7671     **	Release one control block
7672     **
7673     **
7674     **==========================================================
7675     */
7676     
7677     static void ncr_free_ccb (ncb_p np, ccb_p cp)
7678     {
7679     	tcb_p tp = &np->target[cp->target];
7680     	lcb_p lp = tp->lp[cp->lun];
7681     
7682     	if (DEBUG_FLAGS & DEBUG_TAGS) {
7683     		PRINT_LUN(np, cp->target, cp->lun);
7684     		printk ("ccb @%p freeing tag %d.\n", cp, cp->tag);
7685     	}
7686     
7687     	/*
7688     	**	If lun control block available,
7689     	**	decrement active commands and increment credit, 
7690     	**	free the tag if any and remove the JUMP for reselect.
7691     	*/
7692     	if (lp) {
7693     		if (cp->tag != NO_TAG) {
7694     			lp->cb_tags[lp->if_tag++] = cp->tag;
7695     			if (lp->if_tag == MAX_TAGS)
7696     				lp->if_tag = 0;
7697     			lp->tags_umap &= ~(((tagmap_t) 1) << cp->tag);
7698     			lp->tags_smap &= lp->tags_umap;
7699     			lp->jump_ccb[cp->tag] =
7700     				cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l_q));
7701     		} else {
7702     			lp->jump_ccb[0] =
7703     				cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l));
7704     		}
7705     	}
7706     
7707     	/*
7708     	**	Make this CCB available.
7709     	*/
7710     
7711     	if (lp) {
7712     		if (cp != np->ccb) {
7713     			xpt_remque(&cp->link_ccbq);
7714     			xpt_insque_head(&cp->link_ccbq, &lp->free_ccbq);
7715     		}
7716     		--lp->busyccbs;
7717     		if (cp->queued) {
7718     			--lp->queuedccbs;
7719     		}
7720     	}
7721     	cp -> host_status = HS_IDLE;
7722     	cp -> magic = 0;
7723     	if (cp->queued) {
7724     		--np->queuedccbs;
7725     		cp->queued = 0;
7726     	}
7727     
7728     #if 0
7729     	if (cp == np->ccb)
7730     		wakeup ((caddr_t) cp);
7731     #endif
7732     }
7733     
7734     
7735     #define ncr_reg_bus_addr(r) (np->paddr + offsetof (struct ncr_reg, r))
7736     
7737     /*------------------------------------------------------------------------
7738     **	Initialize the fixed part of a CCB structure.
7739     **------------------------------------------------------------------------
7740     **------------------------------------------------------------------------
7741     */
7742     static void ncr_init_ccb(ncb_p np, ccb_p cp)
7743     {
7744     	ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
7745     
7746     	/*
7747     	**	Remember virtual and bus address of this ccb.
7748     	*/
7749     	cp->p_ccb 	   = vtobus(cp);
7750     	cp->phys.header.cp = cp;
7751     
7752     	/*
7753     	**	This allows xpt_remque to work for the default ccb.
7754     	*/
7755     	xpt_que_init(&cp->link_ccbq);
7756     
7757     	/*
7758     	**	Initialyze the start and restart launch script.
7759     	**
7760     	**	COPY(4) @(...p_phys), @(dsa)
7761     	**	JUMP @(sched_point)
7762     	*/
7763     	cp->start.setup_dsa[0]	 = cpu_to_scr(copy_4);
7764     	cp->start.setup_dsa[1]	 = cpu_to_scr(CCB_PHYS(cp, start.p_phys));
7765     	cp->start.setup_dsa[2]	 = cpu_to_scr(ncr_reg_bus_addr(nc_dsa));
7766     	cp->start.schedule.l_cmd = cpu_to_scr(SCR_JUMP);
7767     	cp->start.p_phys	 = cpu_to_scr(CCB_PHYS(cp, phys));
7768     
7769     	bcopy(&cp->start, &cp->restart, sizeof(cp->restart));
7770     
7771     	cp->start.schedule.l_paddr   = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
7772     	cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
7773     }
7774     
7775     
7776     /*------------------------------------------------------------------------
7777     **	Allocate a CCB and initialize its fixed part.
7778     **------------------------------------------------------------------------
7779     **------------------------------------------------------------------------
7780     */
7781     static void ncr_alloc_ccb(ncb_p np, u_char tn, u_char ln)
7782     {
7783     	tcb_p tp = &np->target[tn];
7784     	lcb_p lp = tp->lp[ln];
7785     	ccb_p cp = 0;
7786     
7787     	/*
7788     	**	Allocate memory for this CCB.
7789     	*/
7790     	cp = m_calloc_dma(sizeof(struct ccb), "CCB");
7791     	if (!cp)
7792     		return;
7793     
7794     	/*
7795     	**	Count it and initialyze it.
7796     	*/
7797     	lp->actccbs++;
7798     	np->actccbs++;
7799     	bzero (cp, sizeof (*cp));
7800     	ncr_init_ccb(np, cp);
7801     
7802     	/*
7803     	**	Chain into wakeup list and free ccb queue and take it 
7804     	**	into account for tagged commands.
7805     	*/
7806     	cp->link_ccb      = np->ccb->link_ccb;
7807     	np->ccb->link_ccb = cp;
7808     
7809     	xpt_insque_head(&cp->link_ccbq, &lp->free_ccbq);
7810     	ncr_setup_tags (np, tn, ln);
7811     }
7812     
7813     /*==========================================================
7814     **
7815     **
7816     **      Allocation of resources for Targets/Luns/Tags.
7817     **
7818     **
7819     **==========================================================
7820     */
7821     
7822     
7823     /*------------------------------------------------------------------------
7824     **	Target control block initialisation.
7825     **------------------------------------------------------------------------
7826     **	This data structure is fully initialized after a SCSI command 
7827     **	has been successfully completed for this target.
7828     **	It contains a SCRIPT that is called on target reselection.
7829     **------------------------------------------------------------------------
7830     */
7831     static void ncr_init_tcb (ncb_p np, u_char tn)
7832     {
7833     	tcb_p tp = &np->target[tn];
7834     	ncrcmd copy_1 = np->features & FE_PFEN ? SCR_COPY(1) : SCR_COPY_F(1);
7835     	int th = tn & 3;
7836     	int i;
7837     
7838     	/*
7839     	**	Jump to next tcb if SFBR does not match this target.
7840     	**	JUMP  IF (SFBR != #target#), @(next tcb)
7841     	*/
7842     	tp->jump_tcb.l_cmd   =
7843     		cpu_to_scr((SCR_JUMP ^ IFFALSE (DATA (0x80 + tn))));
7844     	tp->jump_tcb.l_paddr = np->jump_tcb[th].l_paddr;
7845     
7846     	/*
7847     	**	Load the synchronous transfer register.
7848     	**	COPY @(tp->sval), @(sxfer)
7849     	*/
7850     	tp->getscr[0] =	cpu_to_scr(copy_1);
7851     	tp->getscr[1] = cpu_to_scr(vtobus (&tp->sval));
7852     	tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer));
7853       
7854     	/*
7855     	**	Load the timing register.
7856     	**	COPY @(tp->wval), @(scntl3)
7857     	*/
7858     	tp->getscr[3] =	cpu_to_scr(copy_1);
7859     	tp->getscr[4] = cpu_to_scr(vtobus (&tp->wval));
7860     	tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3));
7861     
7862     	/*
7863     	**	Get the IDENTIFY message and the lun.
7864     	**	CALL @script(resel_lun)
7865     	*/
7866     	tp->call_lun.l_cmd   = cpu_to_scr(SCR_CALL);
7867     	tp->call_lun.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_lun));
7868     
7869     	/*
7870     	**	Look for the lun control block of this nexus.
7871     	**	For i = 0 to 3
7872     	**		JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
7873     	*/
7874     	for (i = 0 ; i < 4 ; i++) {
7875     		tp->jump_lcb[i].l_cmd   =
7876     				cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
7877     		tp->jump_lcb[i].l_paddr =
7878     				cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_identify));
7879     	}
7880     
7881     	/*
7882     	**	Link this target control block to the JUMP chain.
7883     	*/
7884     	np->jump_tcb[th].l_paddr = cpu_to_scr(vtobus (&tp->jump_tcb));
7885     
7886     	/*
7887     	**	These assert's should be moved at driver initialisations.
7888     	*/
7889     	assert (( (offsetof(struct ncr_reg, nc_sxfer) ^
7890     		offsetof(struct tcb    , sval    )) &3) == 0);
7891     	assert (( (offsetof(struct ncr_reg, nc_scntl3) ^
7892     		offsetof(struct tcb    , wval    )) &3) == 0);
7893     }
7894     
7895     
7896     /*------------------------------------------------------------------------
7897     **	Lun control block allocation and initialization.
7898     **------------------------------------------------------------------------
7899     **	This data structure is allocated and initialized after a SCSI 
7900     **	command has been successfully completed for this target/lun.
7901     **------------------------------------------------------------------------
7902     */
7903     static lcb_p ncr_alloc_lcb (ncb_p np, u_char tn, u_char ln)
7904     {
7905     	tcb_p tp = &np->target[tn];
7906     	lcb_p lp = tp->lp[ln];
7907     	ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
7908     	int lh = ln & 3;
7909     
7910     	/*
7911     	**	Already done, return.
7912     	*/
7913     	if (lp)
7914     		return lp;
7915     
7916     	/*
7917     	**	Allocate the lcb.
7918     	*/
7919     	lp = m_calloc_dma(sizeof(struct lcb), "LCB");
7920     	if (!lp)
7921     		goto fail;
7922     	bzero(lp, sizeof(*lp));
7923     	tp->lp[ln] = lp;
7924     
7925     	/*
7926     	**	Initialize the target control block if not yet.
7927     	*/
7928     	if (!tp->jump_tcb.l_cmd)
7929     		ncr_init_tcb(np, tn);
7930     
7931     	/*
7932     	**	Initialize the CCB queue headers.
7933     	*/
7934     	xpt_que_init(&lp->free_ccbq);
7935     	xpt_que_init(&lp->busy_ccbq);
7936     	xpt_que_init(&lp->wait_ccbq);
7937     	xpt_que_init(&lp->skip_ccbq);
7938     
7939     	/*
7940     	**	Set max CCBs to 1 and use the default 1 entry 
7941     	**	jump table by default.
7942     	*/
7943     	lp->maxnxs	= 1;
7944     	lp->jump_ccb	= &lp->jump_ccb_0;
7945     	lp->p_jump_ccb	= cpu_to_scr(vtobus(lp->jump_ccb));
7946     
7947     	/*
7948     	**	Initilialyze the reselect script:
7949     	**
7950     	**	Jump to next lcb if SFBR does not match this lun.
7951     	**	Load TEMP with the CCB direct jump table bus address.
7952     	**	Get the SIMPLE TAG message and the tag.
7953     	**
7954     	**	JUMP  IF (SFBR != #lun#), @(next lcb)
7955     	**	COPY @(lp->p_jump_ccb),	  @(temp)
7956     	**	JUMP @script(resel_notag)
7957     	*/
7958     	lp->jump_lcb.l_cmd   =
7959     		cpu_to_scr((SCR_JUMP ^ IFFALSE (MASK (0x80+ln, 0xff))));
7960     	lp->jump_lcb.l_paddr = tp->jump_lcb[lh].l_paddr;
7961     
7962     	lp->load_jump_ccb[0] = cpu_to_scr(copy_4);
7963     	lp->load_jump_ccb[1] = cpu_to_scr(vtobus (&lp->p_jump_ccb));
7964     	lp->load_jump_ccb[2] = cpu_to_scr(ncr_reg_bus_addr(nc_temp));
7965     
7966     	lp->jump_tag.l_cmd   = cpu_to_scr(SCR_JUMP);
7967     	lp->jump_tag.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_notag));
7968     
7969     	/*
7970     	**	Link this lun control block to the JUMP chain.
7971     	*/
7972     	tp->jump_lcb[lh].l_paddr = cpu_to_scr(vtobus (&lp->jump_lcb));
7973     
7974     	/*
7975     	**	Initialize command queuing control.
7976     	*/
7977     	lp->busyccbs	= 1;
7978     	lp->queuedccbs	= 1;
7979     	lp->queuedepth	= 1;
7980     fail:
7981     	return lp;
7982     }
7983     
7984     
7985     /*------------------------------------------------------------------------
7986     **	Lun control block setup on INQUIRY data received.
7987     **------------------------------------------------------------------------
7988     **	We only support WIDE, SYNC for targets and CMDQ for logical units.
7989     **	This setup is done on each INQUIRY since we are expecting user 
7990     **	will play with CHANGE DEFINITION commands. :-)
7991     **------------------------------------------------------------------------
7992     */
7993     static lcb_p ncr_setup_lcb (ncb_p np, u_char tn, u_char ln, u_char *inq_data)
7994     {
7995     	tcb_p tp = &np->target[tn];
7996     	lcb_p lp = tp->lp[ln];
7997     	u_char inq_byte7;
7998     
7999     	/*
8000     	**	If no lcb, try to allocate it.
8001     	*/
8002     	if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
8003     		goto fail;
8004     
8005     	/*
8006     	**	Get device quirks from a speciality table.
8007     	*/
8008     	tp->quirks = ncr_lookup (inq_data);
8009     	if (tp->quirks && bootverbose) {
8010     		PRINT_LUN(np, tn, ln);
8011     		printk ("quirks=%x.\n", tp->quirks);
8012     	}
8013     
8014     	/*
8015     	**	Evaluate trustable target/unit capabilities.
8016     	**	We only believe device version >= SCSI-2 that 
8017     	**	use appropriate response data format (2).
8018     	**	But it seems that some CCS devices also 
8019     	**	support SYNC and I donnot want to frustrate 
8020     	**	anybody. ;-)
8021     	*/
8022     	inq_byte7 = 0;
8023     	if	((inq_data[2] & 0x7) >= 2 && (inq_data[3] & 0xf) == 2)
8024     		inq_byte7 = inq_data[7];
8025     	else if ((inq_data[2] & 0x7) == 1 && (inq_data[3] & 0xf) == 1)
8026     		inq_byte7 = INQ7_SYNC;
8027     
8028     	/*
8029     	**	Throw away announced LUN capabilities if we are told 
8030     	**	that there is no real device supported by the logical unit.
8031     	*/
8032     	if ((inq_data[0] & 0xe0) > 0x20 || (inq_data[0] & 0x1f) == 0x1f)
8033     		inq_byte7 &= (INQ7_SYNC | INQ7_WIDE16);
8034     
8035     	/*
8036     	**	If user is wanting SYNC, force this feature.
8037     	*/
8038     	if (driver_setup.force_sync_nego)
8039     		inq_byte7 |= INQ7_SYNC;
8040     
8041     	/*
8042     	**	Prepare negotiation if SIP capabilities have changed.
8043     	*/
8044     	tp->inq_done = 1;
8045     	if ((inq_byte7 ^ tp->inq_byte7) & (INQ7_SYNC | INQ7_WIDE16)) {
8046     		tp->inq_byte7 = inq_byte7;
8047     		ncr_negotiate(np, tp);
8048     	}
8049     
8050     	/*
8051     	**	If unit supports tagged commands, allocate the 
8052     	**	CCB JUMP table if not yet.
8053     	*/
8054     	if ((inq_byte7 & INQ7_QUEUE) && lp->jump_ccb == &lp->jump_ccb_0) {
8055     		int i;
8056     		lp->jump_ccb = m_calloc_dma(256, "JUMP_CCB");
8057     		if (!lp->jump_ccb) {
8058     			lp->jump_ccb = &lp->jump_ccb_0;
8059     			goto fail;
8060     		}
8061     		lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
8062     		for (i = 0 ; i < 64 ; i++)
8063     			lp->jump_ccb[i] =
8064     				cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l_q));
8065     		for (i = 0 ; i < MAX_TAGS ; i++)
8066     			lp->cb_tags[i] = i;
8067     		lp->maxnxs = MAX_TAGS;
8068     		lp->tags_stime = ktime_get(3*HZ);
8069     	}
8070     
8071     	/*
8072     	**	Adjust tagged queueing status if needed.
8073     	*/
8074     	if ((inq_byte7 ^ lp->inq_byte7) & INQ7_QUEUE) {
8075     		lp->inq_byte7 = inq_byte7;
8076     		lp->numtags   = lp->maxtags;
8077     		ncr_setup_tags (np, tn, ln);
8078     	}
8079     
8080     fail:
8081     	return lp;
8082     }
8083     
8084     /*==========================================================
8085     **
8086     **
8087     **	Build Scatter Gather Block
8088     **
8089     **
8090     **==========================================================
8091     **
8092     **	The transfer area may be scattered among
8093     **	several non adjacent physical pages.
8094     **
8095     **	We may use MAX_SCATTER blocks.
8096     **
8097     **----------------------------------------------------------
8098     */
8099     
8100     /*
8101     **	We try to reduce the number of interrupts caused
8102     **	by unexpected phase changes due to disconnects.
8103     **	A typical harddisk may disconnect before ANY block.
8104     **	If we wanted to avoid unexpected phase changes at all
8105     **	we had to use a break point every 512 bytes.
8106     **	Of course the number of scatter/gather blocks is
8107     **	limited.
8108     **	Under Linux, the scatter/gatter blocks are provided by 
8109     **	the generic driver. We just have to copy addresses and 
8110     **	sizes to the data segment array.
8111     */
8112     
8113     static	int	ncr_scatter(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd)
8114     {
8115     	struct scr_tblmove *data;
8116     	int segment	= 0;
8117     	int use_sg	= (int) cmd->use_sg;
8118     
8119     	data		= cp->phys.data;
8120     	cp->data_len	= 0;
8121     
8122     	if (!use_sg) {
8123     		if (cmd->request_bufflen) {
8124     			u_long baddr = map_scsi_single_data(np, cmd);
8125     
8126     			data = &data[MAX_SCATTER - 1];
8127     			data[0].addr = cpu_to_scr(baddr);
8128     			data[0].size = cpu_to_scr(cmd->request_bufflen);
8129     			cp->data_len = cmd->request_bufflen;
8130     			segment = 1;
8131     		}
8132     	}
8133     	else if (use_sg <= MAX_SCATTER) {
8134     		struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
8135     
8136     		use_sg = map_scsi_sg_data(np, cmd);
8137     		data = &data[MAX_SCATTER - use_sg];
8138     
8139     		while (segment < use_sg) {
8140     			u_long baddr = scsi_sg_dma_address(&scatter[segment]);
8141     			unsigned int len = scsi_sg_dma_len(&scatter[segment]);
8142     
8143     			data[segment].addr = cpu_to_scr(baddr);
8144     			data[segment].size = cpu_to_scr(len);
8145     			cp->data_len	  += len;
8146     			++segment;
8147     		}
8148     	}
8149     	else {
8150     		return -1;
8151     	}
8152     
8153     	return segment;
8154     }
8155     
8156     /*==========================================================
8157     **
8158     **
8159     **	Test the pci bus snoop logic :-(
8160     **
8161     **	Has to be called with interrupts disabled.
8162     **
8163     **
8164     **==========================================================
8165     */
8166     
8167     #ifndef SCSI_NCR_IOMAPPED
8168     static int __init ncr_regtest (struct ncb* np)
8169     {
8170     	register volatile u_int32 data;
8171     	/*
8172     	**	ncr registers may NOT be cached.
8173     	**	write 0xffffffff to a read only register area,
8174     	**	and try to read it back.
8175     	*/
8176     	data = 0xffffffff;
8177     	OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
8178     	data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
8179     #if 1
8180     	if (data == 0xffffffff) {
8181     #else
8182     	if ((data & 0xe2f0fffd) != 0x02000080) {
8183     #endif
8184     		printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
8185     			(unsigned) data);
8186     		return (0x10);
8187     	};
8188     	return (0);
8189     }
8190     #endif
8191     
8192     static int __init ncr_snooptest (struct ncb* np)
8193     {
8194     	u_int32	ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
8195     	int	i, err=0;
8196     #ifndef SCSI_NCR_IOMAPPED
8197     	if (np->reg) {
8198                 err |= ncr_regtest (np);
8199                 if (err) return (err);
8200     	}
8201     #endif
8202     	/*
8203     	**	init
8204     	*/
8205     	pc  = NCB_SCRIPTH_PHYS (np, snooptest);
8206     	host_wr = 1;
8207     	ncr_wr  = 2;
8208     	/*
8209     	**	Set memory and register.
8210     	*/
8211     	np->ncr_cache = cpu_to_scr(host_wr);
8212     	OUTL (nc_temp, ncr_wr);
8213     	/*
8214     	**	Start script (exchange values)
8215     	*/
8216     	OUTL_DSP (pc);
8217     	/*
8218     	**	Wait 'til done (with timeout)
8219     	*/
8220     	for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
8221     		if (INB(nc_istat) & (INTF|SIP|DIP))
8222     			break;
8223     	/*
8224     	**	Save termination position.
8225     	*/
8226     	pc = INL (nc_dsp);
8227     	/*
8228     	**	Read memory and register.
8229     	*/
8230     	host_rd = scr_to_cpu(np->ncr_cache);
8231     	ncr_rd  = INL (nc_scratcha);
8232     	ncr_bk  = INL (nc_temp);
8233     	/*
8234     	**	Reset ncr chip
8235     	*/
8236     	OUTB (nc_istat,  SRST);
8237     	UDELAY (100);
8238     	OUTB (nc_istat,  0   );
8239     	/*
8240     	**	check for timeout
8241     	*/
8242     	if (i>=NCR_SNOOP_TIMEOUT) {
8243     		printk ("CACHE TEST FAILED: timeout.\n");
8244     		return (0x20);
8245     	};
8246     	/*
8247     	**	Check termination position.
8248     	*/
8249     	if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
8250     		printk ("CACHE TEST FAILED: script execution failed.\n");
8251     		printk ("start=%08lx, pc=%08lx, end=%08lx\n", 
8252     			(u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
8253     			(u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
8254     		return (0x40);
8255     	};
8256     	/*
8257     	**	Show results.
8258     	*/
8259     	if (host_wr != ncr_rd) {
8260     		printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
8261     			(int) host_wr, (int) ncr_rd);
8262     		err |= 1;
8263     	};
8264     	if (host_rd != ncr_wr) {
8265     		printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
8266     			(int) ncr_wr, (int) host_rd);
8267     		err |= 2;
8268     	};
8269     	if (ncr_bk != ncr_wr) {
8270     		printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
8271     			(int) ncr_wr, (int) ncr_bk);
8272     		err |= 4;
8273     	};
8274     	return (err);
8275     }
8276     
8277     /*==========================================================
8278     **
8279     **
8280     **	Device lookup.
8281     **
8282     **	@GENSCSI@ should be integrated to scsiconf.c
8283     **
8284     **
8285     **==========================================================
8286     */
8287     
8288     struct table_entry {
8289     	char *	manufacturer;
8290     	char *	model;
8291     	char *	version;
8292     	u_long	info;
8293     };
8294     
8295     static struct table_entry device_tab[] =
8296     {
8297     #if 0
8298     	{"", "", "", QUIRK_NOMSG},
8299     #endif
8300     	{"SONY", "SDT-5000", "3.17", QUIRK_NOMSG},
8301     	{"WangDAT", "Model 2600", "01.7", QUIRK_NOMSG},
8302     	{"WangDAT", "Model 3200", "02.2", QUIRK_NOMSG},
8303     	{"WangDAT", "Model 1300", "02.4", QUIRK_NOMSG},
8304     	{"", "", "", 0} /* catch all: must be last entry. */
8305     };
8306     
8307     static u_long ncr_lookup(char * id)
8308     {
8309     	struct table_entry * p = device_tab;
8310     	char *d, *r, c;
8311     
8312     	for (;;p++) {
8313     
8314     		d = id+8;
8315     		r = p->manufacturer;
8316     		while ((c=*r++)) if (c!=*d++) break;
8317     		if (c) continue;
8318     
8319     		d = id+16;
8320     		r = p->model;
8321     		while ((c=*r++)) if (c!=*d++) break;
8322     		if (c) continue;
8323     
8324     		d = id+32;
8325     		r = p->version;
8326     		while ((c=*r++)) if (c!=*d++) break;
8327     		if (c) continue;
8328     
8329     		return (p->info);
8330     	}
8331     }
8332     
8333     /*==========================================================
8334     **
8335     **	Determine the ncr's clock frequency.
8336     **	This is essential for the negotiation
8337     **	of the synchronous transfer rate.
8338     **
8339     **==========================================================
8340     **
8341     **	Note: we have to return the correct value.
8342     **	THERE IS NO SAVE DEFAULT VALUE.
8343     **
8344     **	Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
8345     **	53C860 and 53C875 rev. 1 support fast20 transfers but 
8346     **	do not have a clock doubler and so are provided with a 
8347     **	80 MHz clock. All other fast20 boards incorporate a doubler 
8348     **	and so should be delivered with a 40 MHz clock.
8349     **	The future fast40 chips (895/895) use a 40 Mhz base clock 
8350     **	and provide a clock quadrupler (160 Mhz). The code below 
8351     **	tries to deal as cleverly as possible with all this stuff.
8352     **
8353     **----------------------------------------------------------
8354     */
8355     
8356     /*
8357      *	Select NCR SCSI clock frequency
8358      */
8359     static void ncr_selectclock(ncb_p np, u_char scntl3)
8360     {
8361     	if (np->multiplier < 2) {
8362     		OUTB(nc_scntl3,	scntl3);
8363     		return;
8364     	}
8365     
8366     	if (bootverbose >= 2)
8367     		printk ("%s: enabling clock multiplier\n", ncr_name(np));
8368     
8369     	OUTB(nc_stest1, DBLEN);	   /* Enable clock multiplier		  */
8370     	if (np->multiplier > 2) {  /* Poll bit 5 of stest4 for quadrupler */
8371     		int i = 20;
8372     		while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
8373     			UDELAY (20);
8374     		if (!i)
8375     			printk("%s: the chip cannot lock the frequency\n", ncr_name(np));
8376     	} else			/* Wait 20 micro-seconds for doubler	*/
8377     		UDELAY (20);
8378     	OUTB(nc_stest3, HSC);		/* Halt the scsi clock		*/
8379     	OUTB(nc_scntl3,	scntl3);
8380     	OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier	*/
8381     	OUTB(nc_stest3, 0x00);		/* Restart scsi clock 		*/
8382     }
8383     
8384     
8385     /*
8386      *	calculate NCR SCSI clock frequency (in KHz)
8387      */
8388     static unsigned __init ncrgetfreq (ncb_p np, int gen)
8389     {
8390     	unsigned ms = 0;
8391     	char count = 0;
8392     
8393     	/*
8394     	 * Measure GEN timer delay in order 
8395     	 * to calculate SCSI clock frequency
8396     	 *
8397     	 * This code will never execute too
8398     	 * many loop iterations (if DELAY is 
8399     	 * reasonably correct). It could get
8400     	 * too low a delay (too high a freq.)
8401     	 * if the CPU is slow executing the 
8402     	 * loop for some reason (an NMI, for
8403     	 * example). For this reason we will
8404     	 * if multiple measurements are to be 
8405     	 * performed trust the higher delay 
8406     	 * (lower frequency returned).
8407     	 */
8408     	OUTB (nc_stest1, 0);	/* make sure clock doubler is OFF */
8409     	OUTW (nc_sien , 0);	/* mask all scsi interrupts */
8410     	(void) INW (nc_sist);	/* clear pending scsi interrupt */
8411     	OUTB (nc_dien , 0);	/* mask all dma interrupts */
8412     	(void) INW (nc_sist);	/* another one, just to be sure :) */
8413     	OUTB (nc_scntl3, 4);	/* set pre-scaler to divide by 3 */
8414     	OUTB (nc_stime1, 0);	/* disable general purpose timer */
8415     	OUTB (nc_stime1, gen);	/* set to nominal delay of 1<<gen * 125us */
8416     	while (!(INW(nc_sist) & GEN) && ms++ < 100000) {
8417     		for (count = 0; count < 10; count ++)
8418     			UDELAY (100);	/* count ms */
8419     	}
8420     	OUTB (nc_stime1, 0);	/* disable general purpose timer */
8421      	/*
8422      	 * set prescaler to divide by whatever 0 means
8423      	 * 0 ought to choose divide by 2, but appears
8424      	 * to set divide by 3.5 mode in my 53c810 ...
8425      	 */
8426      	OUTB (nc_scntl3, 0);
8427     
8428     	if (bootverbose >= 2)
8429     		printk ("%s: Delay (GEN=%d): %u msec\n", ncr_name(np), gen, ms);
8430       	/*
8431      	 * adjust for prescaler, and convert into KHz 
8432       	 */
8433     	return ms ? ((1 << gen) * 4340) / ms : 0;
8434     }
8435     
8436     /*
8437      *	Get/probe NCR SCSI clock frequency
8438      */
8439     static void __init ncr_getclock (ncb_p np, int mult)
8440     {
8441     	unsigned char scntl3 = INB(nc_scntl3);
8442     	unsigned char stest1 = INB(nc_stest1);
8443     	unsigned f1;
8444     
8445     	np->multiplier = 1;
8446     	f1 = 40000;
8447     
8448     	/*
8449     	**	True with 875 or 895 with clock multiplier selected
8450     	*/
8451     	if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
8452     		if (bootverbose >= 2)
8453     			printk ("%s: clock multiplier found\n", ncr_name(np));
8454     		np->multiplier = mult;
8455     	}
8456     
8457     	/*
8458     	**	If multiplier not found or scntl3 not 7,5,3,
8459     	**	reset chip and get frequency from general purpose timer.
8460     	**	Otherwise trust scntl3 BIOS setting.
8461     	*/
8462     	if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
8463     		unsigned f2;
8464     
8465     		OUTB(nc_istat, SRST); UDELAY (5); OUTB(nc_istat, 0);
8466     
8467     		(void) ncrgetfreq (np, 11);	/* throw away first result */
8468     		f1 = ncrgetfreq (np, 11);
8469     		f2 = ncrgetfreq (np, 11);
8470     
8471     		if (bootverbose)
8472     			printk ("%s: NCR clock is %uKHz, %uKHz\n", ncr_name(np), f1, f2);
8473     
8474     		if (f1 > f2) f1 = f2;		/* trust lower result	*/
8475     
8476     		if	(f1 <	45000)		f1 =  40000;
8477     		else if (f1 <	55000)		f1 =  50000;
8478     		else				f1 =  80000;
8479     
8480     		if (f1 < 80000 && mult > 1) {
8481     			if (bootverbose >= 2)
8482     				printk ("%s: clock multiplier assumed\n", ncr_name(np));
8483     			np->multiplier	= mult;
8484     		}
8485     	} else {
8486     		if	((scntl3 & 7) == 3)	f1 =  40000;
8487     		else if	((scntl3 & 7) == 5)	f1 =  80000;
8488     		else 				f1 = 160000;
8489     
8490     		f1 /= np->multiplier;
8491     	}
8492     
8493     	/*
8494     	**	Compute controller synchronous parameters.
8495     	*/
8496     	f1		*= np->multiplier;
8497     	np->clock_khz	= f1;
8498     }
8499     
8500     /*===================== LINUX ENTRY POINTS SECTION ==========================*/
8501     
8502     /*
8503     **   Linux select queue depths function
8504     */
8505     
8506     static void ncr53c8xx_select_queue_depths(struct Scsi_Host *host, struct scsi_device *devlist)
8507     {
8508     	struct scsi_device *device;
8509     
8510     	for (device = devlist; device; device = device->next) {
8511     		ncb_p np;
8512     		tcb_p tp;
8513     		lcb_p lp;
8514     		int numtags;
8515     
8516     		if (device->host != host)
8517     			continue;
8518     
8519     		np = ((struct host_data *) host->hostdata)->ncb;
8520     		tp = &np->target[device->id];
8521     		lp = tp->lp[device->lun];
8522     
8523     		/*
8524     		**	Select queue depth from driver setup.
8525     		**	Donnot use more than configured by user.
8526     		**	Use at least 2.
8527     		**	Donnot use more than our maximum.
8528     		*/
8529     		numtags = device_queue_depth(np->unit, device->id, device->lun);
8530     		if (numtags > tp->usrtags)
8531     			numtags = tp->usrtags;
8532     		if (!device->tagged_supported)
8533     			numtags = 1;
8534     		device->queue_depth = numtags;
8535     		if (device->queue_depth < 2)
8536     			device->queue_depth = 2;
8537     		if (device->queue_depth > MAX_TAGS)
8538     			device->queue_depth = MAX_TAGS;
8539     
8540     		/*
8541     		**	Since the queue depth is not tunable under Linux,
8542     		**	we need to know this value in order not to 
8543     		**	announce stupid things to user.
8544     		*/
8545     		if (lp) {
8546     			lp->numtags = lp->maxtags = numtags;
8547     			lp->scdev_depth = device->queue_depth;
8548     		}
8549     		ncr_setup_tags (np, device->id, device->lun);
8550     
8551     #ifdef DEBUG_NCR53C8XX
8552     printk("ncr53c8xx_select_queue_depth: host=%d, id=%d, lun=%d, depth=%d\n",
8553     	np->unit, device->id, device->lun, device->queue_depth);
8554     #endif
8555     	}
8556     }
8557     
8558     /*
8559     **   Linux entry point of queuecommand() function
8560     */
8561     
8562     int ncr53c8xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *))
8563     {
8564          ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
8565          unsigned long flags;
8566          int sts;
8567     
8568     #ifdef DEBUG_NCR53C8XX
8569     printk("ncr53c8xx_queue_command\n");
8570     #endif
8571     
8572          cmd->scsi_done     = done;
8573          cmd->host_scribble = NULL;
8574     #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
8575          cmd->__data_mapped = 0;
8576          cmd->__data_mapping = 0;
8577     #endif
8578     
8579          NCR_LOCK_NCB(np, flags);
8580     
8581          if ((sts = ncr_queue_command(np, cmd)) != DID_OK) {
8582     	  cmd->result = ScsiResult(sts, 0);
8583     #ifdef DEBUG_NCR53C8XX
8584     printk("ncr53c8xx : command not queued - result=%d\n", sts);
8585     #endif
8586          }
8587     #ifdef DEBUG_NCR53C8XX
8588          else
8589     printk("ncr53c8xx : command successfully queued\n");
8590     #endif
8591     
8592          NCR_UNLOCK_NCB(np, flags);
8593     
8594          if (sts != DID_OK) {
8595               unmap_scsi_data(np, cmd);
8596               done(cmd);
8597          }
8598     
8599          return sts;
8600     }
8601     
8602     /*
8603     **   Linux entry point of the interrupt handler.
8604     **   Since linux versions > 1.3.70, we trust the kernel for 
8605     **   passing the internal host descriptor as 'dev_id'.
8606     **   Otherwise, we scan the host list and call the interrupt 
8607     **   routine for each host that uses this IRQ.
8608     */
8609     
8610     static void ncr53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
8611     {
8612          unsigned long flags;
8613          ncb_p np = (ncb_p) dev_id;
8614          Scsi_Cmnd *done_list;
8615     
8616     #ifdef DEBUG_NCR53C8XX
8617          printk("ncr53c8xx : interrupt received\n");
8618     #endif
8619     
8620          if (DEBUG_FLAGS & DEBUG_TINY) printk ("[");
8621     
8622          NCR_LOCK_NCB(np, flags);
8623          ncr_exception(np);
8624          done_list     = np->done_list;
8625          np->done_list = 0;
8626          NCR_UNLOCK_NCB(np, flags);
8627     
8628          if (DEBUG_FLAGS & DEBUG_TINY) printk ("]\n");
8629     
8630          if (done_list) {
8631               NCR_LOCK_SCSI_DONE(np, flags);
8632               ncr_flush_done_cmds(done_list);
8633               NCR_UNLOCK_SCSI_DONE(np, flags);
8634          }
8635     }
8636     
8637     /*
8638     **   Linux entry point of the timer handler
8639     */
8640     
8641     static void ncr53c8xx_timeout(unsigned long npref)
8642     {
8643          ncb_p np = (ncb_p) npref;
8644          unsigned long flags;
8645          Scsi_Cmnd *done_list;
8646     
8647          NCR_LOCK_NCB(np, flags);
8648          ncr_timeout((ncb_p) np);
8649          done_list     = np->done_list;
8650          np->done_list = 0;
8651          NCR_UNLOCK_NCB(np, flags);
8652     
8653          if (done_list) {
8654               NCR_LOCK_SCSI_DONE(np, flags);
8655               ncr_flush_done_cmds(done_list);
8656               NCR_UNLOCK_SCSI_DONE(np, flags);
8657          }
8658     }
8659     
8660     /*
8661     **   Linux entry point of reset() function
8662     */
8663     
8664     #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8665     int ncr53c8xx_reset(Scsi_Cmnd *cmd, unsigned int reset_flags)
8666     #else
8667     int ncr53c8xx_reset(Scsi_Cmnd *cmd)
8668     #endif
8669     {
8670     	ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
8671     	int sts;
8672     	unsigned long flags;
8673     	Scsi_Cmnd *done_list;
8674     
8675     #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8676     	printk("ncr53c8xx_reset: pid=%lu reset_flags=%x serial_number=%ld serial_number_at_timeout=%ld\n",
8677     		cmd->pid, reset_flags, cmd->serial_number, cmd->serial_number_at_timeout);
8678     #else
8679     	printk("ncr53c8xx_reset: command pid %lu\n", cmd->pid);
8680     #endif
8681     
8682     	NCR_LOCK_NCB(np, flags);
8683     
8684     	/*
8685     	 * We have to just ignore reset requests in some situations.
8686     	 */
8687     #if defined SCSI_RESET_NOT_RUNNING
8688     	if (cmd->serial_number != cmd->serial_number_at_timeout) {
8689     		sts = SCSI_RESET_NOT_RUNNING;
8690     		goto out;
8691     	}
8692     #endif
8693     	/*
8694     	 * If the mid-level driver told us reset is synchronous, it seems 
8695     	 * that we must call the done() callback for the involved command, 
8696     	 * even if this command was not queued to the low-level driver, 
8697     	 * before returning SCSI_RESET_SUCCESS.
8698     	 */
8699     
8700     #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8701     	sts = ncr_reset_bus(np, cmd,
8702     	(reset_flags & (SCSI_RESET_SYNCHRONOUS | SCSI_RESET_ASYNCHRONOUS)) == SCSI_RESET_SYNCHRONOUS);
8703     #else
8704     	sts = ncr_reset_bus(np, cmd, 0);
8705     #endif
8706     
8707     	/*
8708     	 * Since we always reset the controller, when we return success, 
8709     	 * we add this information to the return code.
8710     	 */
8711     #if defined SCSI_RESET_HOST_RESET
8712     	if (sts == SCSI_RESET_SUCCESS)
8713     		sts |= SCSI_RESET_HOST_RESET;
8714     #endif
8715     
8716     out:
8717     	done_list     = np->done_list;
8718     	np->done_list = 0;
8719     	NCR_UNLOCK_NCB(np, flags);
8720     
8721     	ncr_flush_done_cmds(done_list);
8722     
8723     	return sts;
8724     }
8725     
8726     /*
8727     **   Linux entry point of abort() function
8728     */
8729     
8730     int ncr53c8xx_abort(Scsi_Cmnd *cmd)
8731     {
8732     	ncb_p np = ((struct host_data *) cmd->host->hostdata)->ncb;
8733     	int sts;
8734     	unsigned long flags;
8735     	Scsi_Cmnd *done_list;
8736     
8737     #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8738     	printk("ncr53c8xx_abort: pid=%lu serial_number=%ld serial_number_at_timeout=%ld\n",
8739     		cmd->pid, cmd->serial_number, cmd->serial_number_at_timeout);
8740     #else
8741     	printk("ncr53c8xx_abort: command pid %lu\n", cmd->pid);
8742     #endif
8743     
8744     	NCR_LOCK_NCB(np, flags);
8745     
8746     #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
8747     	/*
8748     	 * We have to just ignore abort requests in some situations.
8749     	 */
8750     	if (cmd->serial_number != cmd->serial_number_at_timeout) {
8751     		sts = SCSI_ABORT_NOT_RUNNING;
8752     		goto out;
8753     	}
8754     #endif
8755     
8756     	sts = ncr_abort_command(np, cmd);
8757     out:
8758     	done_list     = np->done_list;
8759     	np->done_list = 0;
8760     	NCR_UNLOCK_NCB(np, flags);
8761     
8762     	ncr_flush_done_cmds(done_list);
8763     
8764     	return sts;
8765     }
8766     
8767     
8768     #ifdef MODULE
8769     int ncr53c8xx_release(struct Scsi_Host *host)
8770     {
8771     #ifdef DEBUG_NCR53C8XX
8772     printk("ncr53c8xx : release\n");
8773     #endif
8774          ncr_detach(((struct host_data *) host->hostdata)->ncb);
8775     
8776          return 1;
8777     }
8778     #endif
8779     
8780     
8781     /*
8782     **	Scsi command waiting list management.
8783     **
8784     **	It may happen that we cannot insert a scsi command into the start queue,
8785     **	in the following circumstances.
8786     ** 		Too few preallocated ccb(s), 
8787     **		maxtags < cmd_per_lun of the Linux host control block,
8788     **		etc...
8789     **	Such scsi commands are inserted into a waiting list.
8790     **	When a scsi command complete, we try to requeue the commands of the
8791     **	waiting list.
8792     */
8793     
8794     #define next_wcmd host_scribble
8795     
8796     static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd)
8797     {
8798     	Scsi_Cmnd *wcmd;
8799     
8800     #ifdef DEBUG_WAITING_LIST
8801     	printk("%s: cmd %lx inserted into waiting list\n", ncr_name(np), (u_long) cmd);
8802     #endif
8803     	cmd->next_wcmd = 0;
8804     	if (!(wcmd = np->waiting_list)) np->waiting_list = cmd;
8805     	else {
8806     		while ((wcmd->next_wcmd) != 0)
8807     			wcmd = (Scsi_Cmnd *) wcmd->next_wcmd;
8808     		wcmd->next_wcmd = (char *) cmd;
8809     	}
8810     }
8811     
8812     static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd)
8813     {
8814     	Scsi_Cmnd **pcmd = &np->waiting_list;
8815     
8816     	while (*pcmd) {
8817     		if (cmd == *pcmd) {
8818     			if (to_remove) {
8819     				*pcmd = (Scsi_Cmnd *) cmd->next_wcmd;
8820     				cmd->next_wcmd = 0;
8821     			}
8822     #ifdef DEBUG_WAITING_LIST
8823     	printk("%s: cmd %lx retrieved from waiting list\n", ncr_name(np), (u_long) cmd);
8824     #endif
8825     			return cmd;
8826     		}
8827     		pcmd = (Scsi_Cmnd **) &(*pcmd)->next_wcmd;
8828     	}
8829     	return 0;
8830     }
8831     
8832     static void process_waiting_list(ncb_p np, int sts)
8833     {
8834     	Scsi_Cmnd *waiting_list, *wcmd;
8835     
8836     	waiting_list = np->waiting_list;
8837     	np->waiting_list = 0;
8838     
8839     #ifdef DEBUG_WAITING_LIST
8840     	if (waiting_list) printk("%s: waiting_list=%lx processing sts=%d\n", ncr_name(np), (u_long) waiting_list, sts);
8841     #endif
8842     	while ((wcmd = waiting_list) != 0) {
8843     		waiting_list = (Scsi_Cmnd *) wcmd->next_wcmd;
8844     		wcmd->next_wcmd = 0;
8845     		if (sts == DID_OK) {
8846     #ifdef DEBUG_WAITING_LIST
8847     	printk("%s: cmd %lx trying to requeue\n", ncr_name(np), (u_long) wcmd);
8848     #endif
8849     			sts = ncr_queue_command(np, wcmd);
8850     		}
8851     		if (sts != DID_OK) {
8852     #ifdef DEBUG_WAITING_LIST
8853     	printk("%s: cmd %lx done forced sts=%d\n", ncr_name(np), (u_long) wcmd, sts);
8854     #endif
8855     			wcmd->result = ScsiResult(sts, 0);
8856     			ncr_queue_done_cmd(np, wcmd);
8857     		}
8858     	}
8859     }
8860     
8861     #undef next_wcmd
8862     
8863     #ifdef SCSI_NCR_PROC_INFO_SUPPORT
8864     
8865     /*=========================================================================
8866     **	Proc file system stuff
8867     **
8868     **	A read operation returns profile information.
8869     **	A write operation is a control command.
8870     **	The string is parsed in the driver code and the command is passed 
8871     **	to the ncr_usercmd() function.
8872     **=========================================================================
8873     */
8874     
8875     #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
8876     
8877     #define is_digit(c)	((c) >= '0' && (c) <= '9')
8878     #define digit_to_bin(c)	((c) - '0')
8879     #define is_space(c)	((c) == ' ' || (c) == '\t')
8880     
8881     static int skip_spaces(char *ptr, int len)
8882     {
8883     	int cnt, c;
8884     
8885     	for (cnt = len; cnt > 0 && (c = *ptr++) && is_space(c); cnt--);
8886     
8887     	return (len - cnt);
8888     }
8889     
8890     static int get_int_arg(char *ptr, int len, u_long *pv)
8891     {
8892     	int	cnt, c;
8893     	u_long	v;
8894     
8895     	for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && is_digit(c); cnt--) {
8896     		v = (v * 10) + digit_to_bin(c);
8897     	}
8898     
8899     	if (pv)
8900     		*pv = v;
8901     
8902     	return (len - cnt);
8903     }
8904     
8905     static int is_keyword(char *ptr, int len, char *verb)
8906     {
8907     	int verb_len = strlen(verb);
8908     
8909     	if (len >= strlen(verb) && !memcmp(verb, ptr, verb_len))
8910     		return verb_len;
8911     	else
8912     		return 0;
8913     
8914     }
8915     
8916     #define SKIP_SPACES(min_spaces)						\
8917     	if ((arg_len = skip_spaces(ptr, len)) < (min_spaces))		\
8918     		return -EINVAL;						\
8919     	ptr += arg_len; len -= arg_len;
8920     
8921     #define GET_INT_ARG(v)							\
8922     	if (!(arg_len = get_int_arg(ptr, len, &(v))))			\
8923     		return -EINVAL;						\
8924     	ptr += arg_len; len -= arg_len;
8925     
8926     
8927     /*
8928     **	Parse a control command
8929     */
8930     
8931     static int ncr_user_command(ncb_p np, char *buffer, int length)
8932     {
8933     	char *ptr	= buffer;
8934     	int len		= length;
8935     	struct usrcmd	 *uc = &np->user;
8936     	int		arg_len;
8937     	u_long 		target;
8938     
8939     	bzero(uc, sizeof(*uc));
8940     
8941     	if (len > 0 && ptr[len-1] == '\n')
8942     		--len;
8943     
8944     	if	((arg_len = is_keyword(ptr, len, "setsync")) != 0)
8945     		uc->cmd = UC_SETSYNC;
8946     	else if	((arg_len = is_keyword(ptr, len, "settags")) != 0)
8947     		uc->cmd = UC_SETTAGS;
8948     	else if	((arg_len = is_keyword(ptr, len, "setorder")) != 0)
8949     		uc->cmd = UC_SETORDER;
8950     	else if	((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
8951     		uc->cmd = UC_SETVERBOSE;
8952     	else if	((arg_len = is_keyword(ptr, len, "setwide")) != 0)
8953     		uc->cmd = UC_SETWIDE;
8954     	else if	((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
8955     		uc->cmd = UC_SETDEBUG;
8956     	else if	((arg_len = is_keyword(ptr, len, "setflag")) != 0)
8957     		uc->cmd = UC_SETFLAG;
8958     	else
8959     		arg_len = 0;
8960     
8961     #ifdef DEBUG_PROC_INFO
8962     printk("ncr_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
8963     #endif
8964     
8965     	if (!arg_len)
8966     		return -EINVAL;
8967     	ptr += arg_len; len -= arg_len;
8968     
8969     	switch(uc->cmd) {
8970     	case UC_SETSYNC:
8971     	case UC_SETTAGS:
8972     	case UC_SETWIDE:
8973     	case UC_SETFLAG:
8974     		SKIP_SPACES(1);
8975     		if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
8976     			ptr += arg_len; len -= arg_len;
8977     			uc->target = ~0;
8978     		} else {
8979     			GET_INT_ARG(target);
8980     			uc->target = (1<<target);
8981     #ifdef DEBUG_PROC_INFO
8982     printk("ncr_user_command: target=%ld\n", target);
8983     #endif
8984     		}
8985     		break;
8986     	}
8987     
8988     	switch(uc->cmd) {
8989     	case UC_SETVERBOSE:
8990     	case UC_SETSYNC:
8991     	case UC_SETTAGS:
8992     	case UC_SETWIDE:
8993     		SKIP_SPACES(1);
8994     		GET_INT_ARG(uc->data);
8995     #ifdef DEBUG_PROC_INFO
8996     printk("ncr_user_command: data=%ld\n", uc->data);
8997     #endif
8998     		break;
8999     	case UC_SETORDER:
9000     		SKIP_SPACES(1);
9001     		if	((arg_len = is_keyword(ptr, len, "simple")))
9002     			uc->data = M_SIMPLE_TAG;
9003     		else if	((arg_len = is_keyword(ptr, len, "ordered")))
9004     			uc->data = M_ORDERED_TAG;
9005     		else if	((arg_len = is_keyword(ptr, len, "default")))
9006     			uc->data = 0;
9007     		else
9008     			return -EINVAL;
9009     		break;
9010     	case UC_SETDEBUG:
9011     		while (len > 0) {
9012     			SKIP_SPACES(1);
9013     			if	((arg_len = is_keyword(ptr, len, "alloc")))
9014     				uc->data |= DEBUG_ALLOC;
9015     			else if	((arg_len = is_keyword(ptr, len, "phase")))
9016     				uc->data |= DEBUG_PHASE;
9017     			else if	((arg_len = is_keyword(ptr, len, "queue")))
9018     				uc->data |= DEBUG_QUEUE;
9019     			else if	((arg_len = is_keyword(ptr, len, "result")))
9020     				uc->data |= DEBUG_RESULT;
9021     			else if	((arg_len = is_keyword(ptr, len, "scatter")))
9022     				uc->data |= DEBUG_SCATTER;
9023     			else if	((arg_len = is_keyword(ptr, len, "script")))
9024     				uc->data |= DEBUG_SCRIPT;
9025     			else if	((arg_len = is_keyword(ptr, len, "tiny")))
9026     				uc->data |= DEBUG_TINY;
9027     			else if	((arg_len = is_keyword(ptr, len, "timing")))
9028     				uc->data |= DEBUG_TIMING;
9029     			else if	((arg_len = is_keyword(ptr, len, "nego")))
9030     				uc->data |= DEBUG_NEGO;
9031     			else if	((arg_len = is_keyword(ptr, len, "tags")))
9032     				uc->data |= DEBUG_TAGS;
9033     			else
9034     				return -EINVAL;
9035     			ptr += arg_len; len -= arg_len;
9036     		}
9037     #ifdef DEBUG_PROC_INFO
9038     printk("ncr_user_command: data=%ld\n", uc->data);
9039     #endif
9040     		break;
9041     	case UC_SETFLAG:
9042     		while (len > 0) {
9043     			SKIP_SPACES(1);
9044     			if	((arg_len = is_keyword(ptr, len, "trace")))
9045     				uc->data |= UF_TRACE;
9046     			else if	((arg_len = is_keyword(ptr, len, "no_disc")))
9047     				uc->data |= UF_NODISC;
9048     			else
9049     				return -EINVAL;
9050     			ptr += arg_len; len -= arg_len;
9051     		}
9052     		break;
9053     	default:
9054     		break;
9055     	}
9056     
9057     	if (len)
9058     		return -EINVAL;
9059     	else {
9060     		long flags;
9061     
9062     		NCR_LOCK_NCB(np, flags);
9063     		ncr_usercmd (np);
9064     		NCR_UNLOCK_NCB(np, flags);
9065     	}
9066     	return length;
9067     }
9068     
9069     #endif	/* SCSI_NCR_USER_COMMAND_SUPPORT */
9070     
9071     
9072     #ifdef SCSI_NCR_USER_INFO_SUPPORT
9073     /*
9074     **	Copy formatted information into the input buffer.
9075     */
9076     
9077     static int ncr_host_info(ncb_p np, char *ptr, off_t offset, int len)
9078     {
9079     	struct info_str info;
9080     
9081     	info.buffer	= ptr;
9082     	info.length	= len;
9083     	info.offset	= offset;
9084     	info.pos	= 0;
9085     
9086     	copy_info(&info, "  Chip NCR53C%s, device id 0x%x, "
9087     			 "revision id 0x%x\n",
9088     			 np->chip_name, np->device_id,	np->revision_id);
9089     	copy_info(&info, "  On PCI bus %d, device %d, function %d, "
9090     #ifdef __sparc__
9091     		"IRQ %s\n",
9092     #else
9093     		"IRQ %d\n",
9094     #endif
9095     		np->bus, (np->device_fn & 0xf8) >> 3, np->device_fn & 7,
9096     #ifdef __sparc__
9097     		__irq_itoa(np->irq));
9098     #else
9099     		(int) np->irq);
9100     #endif
9101     	copy_info(&info, "  Synchronous period factor %d, "
9102     			 "max commands per lun %d\n",
9103     			 (int) np->minsync, MAX_TAGS);
9104     
9105     	if (driver_setup.debug || driver_setup.verbose > 1) {
9106     		copy_info(&info, "  Debug flags 0x%x, verbosity level %d\n",
9107     			  driver_setup.debug, driver_setup.verbose);
9108     	}
9109     
9110     	return info.pos > info.offset? info.pos - info.offset : 0;
9111     }
9112     
9113     #endif /* SCSI_NCR_USER_INFO_SUPPORT */
9114     
9115     /*
9116     **	Entry point of the scsi proc fs of the driver.
9117     **	- func = 0 means read  (returns profile data)
9118     **	- func = 1 means write (parse user control command)
9119     */
9120     
9121     static int ncr53c8xx_proc_info(char *buffer, char **start, off_t offset,
9122     			int length, int hostno, int func)
9123     {
9124     	struct Scsi_Host *host;
9125     	struct host_data *host_data;
9126     	ncb_p ncb = 0;
9127     	int retv;
9128     
9129     #ifdef DEBUG_PROC_INFO
9130     printk("ncr53c8xx_proc_info: hostno=%d, func=%d\n", hostno, func);
9131     #endif
9132     
9133     	for (host = first_host; host; host = host->next) {
9134     		if (host->hostt == the_template && host->host_no == hostno) {
9135     			host_data = (struct host_data *) host->hostdata;
9136     			ncb = host_data->ncb;
9137     			break;
9138     		}
9139     	}
9140     
9141     	if (!ncb)
9142     		return -EINVAL;
9143     
9144     	if (func) {
9145     #ifdef	SCSI_NCR_USER_COMMAND_SUPPORT
9146     		retv = ncr_user_command(ncb, buffer, length);
9147     #else
9148     		retv = -EINVAL;
9149     #endif
9150     	}
9151     	else {
9152     		if (start)
9153     			*start = buffer;
9154     #ifdef SCSI_NCR_USER_INFO_SUPPORT
9155     		retv = ncr_host_info(ncb, buffer, offset, length);
9156     #else
9157     		retv = -EINVAL;
9158     #endif
9159     	}
9160     
9161     	return retv;
9162     }
9163     
9164     /*=========================================================================
9165     **	End of proc file system stuff
9166     **=========================================================================
9167     */
9168     #endif
9169     
9170     
9171     /*==========================================================
9172     **
9173     **	/proc directory entry.
9174     **
9175     **==========================================================
9176     */
9177     #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
9178     static struct proc_dir_entry proc_scsi_ncr53c8xx = {
9179         PROC_SCSI_NCR53C8XX, 9, NAME53C8XX,
9180         S_IFDIR | S_IRUGO | S_IXUGO, 2
9181     };
9182     #endif
9183     
9184     /*==========================================================
9185     **
9186     **	Boot command line.
9187     **
9188     **==========================================================
9189     */
9190     #ifdef	MODULE
9191     char *ncr53c8xx = 0;	/* command line passed by insmod */
9192     # if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,30)
9193     MODULE_PARM(ncr53c8xx, "s");
9194     # endif
9195     #endif
9196     
9197     int __init ncr53c8xx_setup(char *str)
9198     {
9199     	return sym53c8xx__setup(str);
9200     }
9201     
9202     #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,13)
9203     #ifndef MODULE
9204     __setup("ncr53c8xx=", ncr53c8xx_setup);
9205     #endif
9206     #endif
9207     
9208     /*===================================================================
9209     **
9210     **   SYM53C8XX supported device list
9211     **
9212     **===================================================================
9213     */
9214     
9215     static u_short	ncr_chip_ids[]   __initdata = {
9216     	PCI_DEVICE_ID_NCR_53C810,
9217     	PCI_DEVICE_ID_NCR_53C815,
9218     	PCI_DEVICE_ID_NCR_53C820,
9219     	PCI_DEVICE_ID_NCR_53C825,
9220     	PCI_DEVICE_ID_NCR_53C860,
9221     	PCI_DEVICE_ID_NCR_53C875,
9222     	PCI_DEVICE_ID_NCR_53C875J,
9223     	PCI_DEVICE_ID_NCR_53C885,
9224     	PCI_DEVICE_ID_NCR_53C895,
9225     	PCI_DEVICE_ID_NCR_53C896,
9226     	PCI_DEVICE_ID_NCR_53C895A,
9227     	PCI_DEVICE_ID_NCR_53C1510D
9228     };
9229     
9230     /*==========================================================
9231     **
9232     **	Chip detection entry point.
9233     **
9234     **==========================================================
9235     */
9236     int __init ncr53c8xx_detect(Scsi_Host_Template *tpnt)
9237     {
9238     	/*
9239     	**    Initialize driver general stuff.
9240     	*/
9241     #ifdef SCSI_NCR_PROC_INFO_SUPPORT
9242     #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
9243          tpnt->proc_dir  = &proc_scsi_ncr53c8xx;
9244     #else
9245          tpnt->proc_name = NAME53C8XX;
9246     #endif
9247          tpnt->proc_info = ncr53c8xx_proc_info;
9248     #endif
9249     
9250     #if	defined(SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT) && defined(MODULE)
9251     if (ncr53c8xx)
9252     	ncr53c8xx_setup(ncr53c8xx);
9253     #endif
9254     
9255     	return sym53c8xx__detect(tpnt, ncr_chip_ids,
9256     				 sizeof(ncr_chip_ids)/sizeof(ncr_chip_ids[0]));
9257     }
9258     
9259     /*==========================================================
9260     **
9261     **   Entry point for info() function
9262     **
9263     **==========================================================
9264     */
9265     const char *ncr53c8xx_info (struct Scsi_Host *host)
9266     {
9267     	return SCSI_NCR_DRIVER_NAME;
9268     }
9269     
9270     /*
9271     **	Module stuff
9272     */
9273     
9274     #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
9275     static
9276     #endif
9277     #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0) || defined(MODULE)
9278     Scsi_Host_Template driver_template = NCR53C8XX;
9279     #include "scsi_module.c"
9280     #endif
9281