ncr.c revision 12662
1139804Simp/**************************************************************************
21541Srgrimes**
31541Srgrimes**  $Id: ncr.c,v 1.49 1995/09/21 17:27:28 se Exp $
41541Srgrimes**
51541Srgrimes**  Device driver for the   NCR 53C810   PCI-SCSI-Controller.
61541Srgrimes**
71541Srgrimes**  FreeBSD / NetBSD
81541Srgrimes**
91541Srgrimes**-------------------------------------------------------------------------
101541Srgrimes**
111541Srgrimes**  Written for 386bsd and FreeBSD by
121541Srgrimes**	Wolfgang Stanglmeier	<wolf@cologne.de>
131541Srgrimes**	Stefan Esser		<se@mi.Uni-Koeln.de>
141541Srgrimes**
151541Srgrimes**  Ported to NetBSD by
161541Srgrimes**	Charles M. Hannum	<mycroft@gnu.ai.mit.edu>
171541Srgrimes**
181541Srgrimes**-------------------------------------------------------------------------
191541Srgrimes**
201541Srgrimes** Copyright (c) 1994 Wolfgang Stanglmeier.  All rights reserved.
211541Srgrimes**
221541Srgrimes** Redistribution and use in source and binary forms, with or without
231541Srgrimes** modification, are permitted provided that the following conditions
241541Srgrimes** are met:
251541Srgrimes** 1. Redistributions of source code must retain the above copyright
261541Srgrimes**    notice, this list of conditions and the following disclaimer.
271541Srgrimes** 2. Redistributions in binary form must reproduce the above copyright
281541Srgrimes**    notice, this list of conditions and the following disclaimer in the
291541Srgrimes**    documentation and/or other materials provided with the distribution.
301541Srgrimes** 3. The name of the author may not be used to endorse or promote products
311541Srgrimes**    derived from this software without specific prior written permission.
32116182Sobrien**
33116182Sobrien** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34116182Sobrien** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
351541Srgrimes** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
361541Srgrimes** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
371541Srgrimes** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38108524Salfred** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39143417Srwatson** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
401541Srgrimes** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41108524Salfred** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42143417Srwatson** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43143417Srwatson**
441541Srgrimes***************************************************************************
451541Srgrimes*/
4624206Sbde
4724206Sbde#define NCR_DATE "pl23 95/09/07"
481541Srgrimes
4934924Sbde#define NCR_VERSION	(2)
5060405Schris#define	MAX_UNITS	(16)
51191816Szec
521541Srgrimes#define NCR_GETCC_WITHMSG
531541Srgrimes
541541Srgrimes/*==========================================================
551541Srgrimes**
56163606Srwatson**	Configuration and Debugging
57163606Srwatson**
58167206Srwatson**	May be overwritten in <arch/conf/xxxx>
59116546Sphk**
60116546Sphk**==========================================================
61175140Sjhb*/
62116546Sphk
63116546Sphk/*
64116546Sphk**    SCSI address of this device.
65116546Sphk**    The boot routines should have set it.
66116546Sphk**    If not, use this.
67116546Sphk*/
6872521Sjlemon
691541Srgrimes#ifndef SCSI_NCR_MYADDR
701541Srgrimes#define SCSI_NCR_MYADDR      (7)
7143408Snewton#endif /* SCSI_NCR_MYADDR */
72167206Srwatson
73167206Srwatson/*
741541Srgrimes**    The maximal synchronous frequency in kHz.
75109153Sdillon**    (0=asynchronous)
7692310Salfred*/
7792310Salfred
78191816Szec#ifndef SCSI_NCR_MAX_SYNC
79172930Srwatson#define SCSI_NCR_MAX_SYNC   (10000)
80171744Srwatson#endif /* SCSI_NCR_MAX_SYNC */
81104571Srwatson
82104571Srwatson/*
83191816Szec**    The maximal bus with (in log2 byte)
84191816Szec**    (0=8 bit, 1=16 bit)
85191816Szec*/
86191816Szec
871541Srgrimes#ifndef SCSI_NCR_MAX_WIDE
881541Srgrimes#define SCSI_NCR_MAX_WIDE   (1)
891541Srgrimes#endif /* SCSI_NCR_MAX_WIDE */
9043408Snewton
91167206Srwatson/*
92167206Srwatson**    The maximum number of tags per logic unit.
931541Srgrimes**    Used only for disk devices that support tags.
94109153Sdillon*/
9592310Salfred
9692310Salfred#ifndef SCSI_NCR_MAX_TAGS
97104571Srwatson#define SCSI_NCR_MAX_TAGS    (4)
98172930Srwatson#endif /* SCSI_NCR_MAX_TAGS */
99171744Srwatson
100104571Srwatson/*==========================================================
101104571Srwatson**
102160619Srwatson**      Configuration and Debugging
103143417Srwatson**
104143417Srwatson**==========================================================
105143417Srwatson*/
106143417Srwatson
107143417Srwatson/*
10892310Salfred**    Number of targets supported by the driver.
1091541Srgrimes**    n permits target numbers 0..n-1.
1101541Srgrimes**    Default is 7, meaning targets #0..#6.
1111549Srgrimes**    #7 .. is myself.
112175140Sjhb*/
113175140Sjhb
114175140Sjhb#define MAX_TARGET  (16)
115175140Sjhb
116175140Sjhb/*
117175140Sjhb**    Number of logic units supported by the driver.
118175140Sjhb**    n enables logic unit numbers 0..n-1.
119175140Sjhb**    The common SCSI devices require only
120167206Srwatson**    one lun, so take 1 as the default.
121167206Srwatson*/
1221541Srgrimes
123137671Sphk#define MAX_LUN     (1)
124137671Sphk
1251541Srgrimes/*
126191816Szec**    The maximum number of jobs scheduled for starting.
1271541Srgrimes**    There should be one slot per target, and one slot
1281541Srgrimes**    for each tag of each target in use.
129130653Srwatson**    The calculation below is actually quite silly ...
1301541Srgrimes*/
1311541Srgrimes
1321541Srgrimes#define MAX_START   (MAX_TARGET + 7 * SCSI_NCR_MAX_TAGS)
1331541Srgrimes
134130653Srwatson/*
135137671Sphk**    The maximum number of segments a transfer is split into.
1361541Srgrimes*/
1371541Srgrimes
138130653Srwatson#define MAX_SCATTER (33)
139167206Srwatson
140167206Srwatson/*
141167206Srwatson**    The maximum transfer length (should be >= 64k).
142167206Srwatson**    MUST NOT be greater than (MAX_SCATTER-1) * NBPG.
143130653Srwatson*/
1441541Srgrimes
145130653Srwatson#define MAX_SIZE  ((MAX_SCATTER-1) * (long) NBPG)
1461541Srgrimes
147130653Srwatson/*
148130653Srwatson**	other
1491541Srgrimes*/
150130653Srwatson
151130653Srwatson#define NCR_SNOOP_TIMEOUT (1000000)
1521541Srgrimes
153130653Srwatson/*==========================================================
1541541Srgrimes**
155130653Srwatson**      Include files
1561541Srgrimes**
157130653Srwatson**==========================================================
158130653Srwatson*/
1591541Srgrimes
160130653Srwatson#ifdef __NetBSD__
161130653Srwatson#ifdef _KERNEL
1621541Srgrimes#define KERNEL
163130653Srwatson#endif
1641541Srgrimes#endif
165137671Sphk#include <stddef.h>
1661541Srgrimes
1671541Srgrimes#include <sys/types.h>
168130796Srwatson#include <sys/param.h>
1691541Srgrimes#include <sys/time.h>
170137671Sphk#include <sys/proc.h>
1711541Srgrimes
172195134Sphk#ifdef KERNEL
173195134Sphk#include <sys/systm.h>
174195134Sphk#include <sys/malloc.h>
175195134Sphk#include <sys/buf.h>
176195134Sphk#include <sys/kernel.h>
177195191Semaste#ifndef __NetBSD__
178195191Semaste#include <machine/clock.h>
179195191Semaste#include <machine/cpu.h> /* bootverbose */
180195191Semaste#else
181195191Semaste#define bootverbose	1
182195191Semaste#endif
183195191Semaste#include <vm/vm.h>
184195191Semaste#include <vm/vm_param.h>
18541086Struckman#include <vm/pmap.h>
186137671Sphk#include <vm/vm_extern.h>
187137671Sphk#endif /* KERNEL */
18841086Struckman
18941086Struckman
190104393Struckman#ifndef __NetBSD__
191137671Sphk#include <sys/devconf.h>
1921541Srgrimes#include <pci/pcivar.h>
19341086Struckman#include <pci/pcireg.h>
194137671Sphk#include <pci/ncrreg.h>
195137671Sphkextern PRINT_ADDR();
19641086Struckman#else
1971541Srgrimes#include <sys/device.h>
198104393Struckman#include <dev/pci/ncr_reg.h>
199137671Sphk#include <dev/pci/pcivar.h>
2001541Srgrimes#include <dev/pci/pcireg.h>
2011541Srgrimes#define DELAY(x)	delay(x)
202130796Srwatson#endif /* __NetBSD */
203130480Srwatson
204137671Sphk#include <scsi/scsi_all.h>
205137671Sphk#include <scsi/scsiconf.h>
206137671Sphk#ifndef __NetBSD__
207167206Srwatson#include <machine/clock.h>
208167206Srwatson#endif /* __NetBSD */
209167206Srwatson
210137671Sphk
211137671Sphk/*==========================================================
212137671Sphk**
213137671Sphk**	Debugging tags
214178888Sjulian**
215137671Sphk**==========================================================
216137671Sphk*/
217137671Sphk
218137671Sphk#define DEBUG_ALLOC    (0x0001)
2191541Srgrimes#define DEBUG_PHASE    (0x0002)
220191816Szec#define DEBUG_POLL     (0x0004)
221171744Srwatson#define DEBUG_QUEUE    (0x0008)
2221541Srgrimes#define DEBUG_RESULT   (0x0010)
2231541Srgrimes#define DEBUG_SCATTER  (0x0020)
2241549Srgrimes#define DEBUG_SCRIPT   (0x0040)
225167206Srwatson#define DEBUG_TINY     (0x0080)
226167206Srwatson#define DEBUG_TIMING   (0x0100)
2271541Srgrimes#define DEBUG_NEGO     (0x0200)
228109153Sdillon#define DEBUG_TAGS     (0x0400)
229171744Srwatson#define DEBUG_FREEZE   (0x0800)
230137671Sphk#define DEBUG_RESTART  (0x1000)
231137671Sphk
232172930Srwatson/*
233171744Srwatson**    Enable/Disable debug messages.
234145167Srwatson**    Can be changed at runtime too.
235145167Srwatson*/
236171744Srwatson
2371541Srgrimes#ifdef SCSI_DEBUG_FLAGS
2381541Srgrimes	#define DEBUG_FLAGS ncr_debug
2391549Srgrimes#else /* SCSI_DEBUG_FLAGS */
240167206Srwatson	#define SCSI_DEBUG_FLAGS	0
241167206Srwatson	#define DEBUG_FLAGS	0
2421541Srgrimes#endif /* SCSI_DEBUG_FLAGS */
243109153Sdillon
244171744Srwatson
245132554Srwatson
246171744Srwatson/*==========================================================
2471541Srgrimes**
2481541Srgrimes**	assert ()
24962425Schris**
250145167Srwatson**==========================================================
251172930Srwatson**
252171744Srwatson**	modified copy from 386bsd:/usr/include/sys/assert.h
253145167Srwatson**
254145167Srwatson**----------------------------------------------------------
25562425Schris*/
256130480Srwatson
25762425Schris#define	assert(expression) { \
25862425Schris	if (!(expression)) { \
259183661Srwatson		(void)printf(\
260130480Srwatson			"assertion \"%s\" failed: file \"%s\", line %d\n", \
26162425Schris			#expression, \
26262425Schris			__FILE__, __LINE__); \
263183661Srwatson	} \
264183661Srwatson}
265183661Srwatson
266130480Srwatson/*==========================================================
26762425Schris**
26860405Schris**	Access to the controller chip.
26960405Schris**
270171744Srwatson**==========================================================
2711541Srgrimes*/
2721541Srgrimes
27386487Sdillon#ifdef NCR_IOMAPPED
274167206Srwatson
275167206Srwatson#define	INB(r) inb (np->port + offsetof(struct ncr_reg, r))
276167206Srwatson#define	INW(r) inw (np->port + offsetof(struct ncr_reg, r))
277167206Srwatson#define	INL(r) inl (np->port + offsetof(struct ncr_reg, r))
27886487Sdillon
2791541Srgrimes#define	OUTB(r, val) outb (np->port+offsetof(struct ncr_reg,r),(val))
28043408Snewton#define	OUTW(r, val) outw (np->port+offsetof(struct ncr_reg,r),(val))
281167206Srwatson#define	OUTL(r, val) outl (np->port+offsetof(struct ncr_reg,r),(val))
2821541Srgrimes
2831541Srgrimes#else
28486487Sdillon
2851541Srgrimes#define INB(r) (np->reg->r)
286109153Sdillon#define INW(r) (np->reg->r)
28749413Sgreen#define INL(r) (np->reg->r)
288109153Sdillon
28989306Salfred#define OUTB(r, val) np->reg->r = val
29089306Salfred#define OUTW(r, val) np->reg->r = val
29186487Sdillon#define OUTL(r, val) np->reg->r = val
2921541Srgrimes
2931541Srgrimes#endif
294
295/*==========================================================
296**
297**	Command control block states.
298**
299**==========================================================
300*/
301
302#define HS_IDLE		(0)
303#define HS_BUSY		(1)
304#define HS_NEGOTIATE	(2)	/* sync/wide data transfer*/
305#define HS_DISCONNECT	(3)	/* Disconnected by target */
306
307#define HS_COMPLETE	(4)
308#define HS_SEL_TIMEOUT	(5)	/* Selection timeout      */
309#define HS_RESET	(6)	/* SCSI reset	     */
310#define HS_ABORTED	(7)	/* Transfer aborted       */
311#define HS_TIMEOUT	(8)	/* Software timeout       */
312#define HS_FAIL		(9)	/* SCSI or PCI bus errors */
313#define HS_UNEXPECTED	(10)	/* Unexpected disconnect  */
314
315#define HS_DONEMASK	(0xfc)
316
317/*==========================================================
318**
319**	Software Interrupt Codes
320**
321**==========================================================
322*/
323
324#define	SIR_SENSE_RESTART	(1)
325#define	SIR_SENSE_FAILED	(2)
326#define	SIR_STALL_RESTART	(3)
327#define	SIR_STALL_QUEUE		(4)
328#define	SIR_NEGO_SYNC		(5)
329#define	SIR_NEGO_WIDE		(6)
330#define	SIR_NEGO_FAILED		(7)
331#define	SIR_NEGO_PROTO		(8)
332#define	SIR_REJECT_RECEIVED	(9)
333#define	SIR_REJECT_SENT		(10)
334#define	SIR_IGN_RESIDUE		(11)
335#define	SIR_MISSING_SAVE	(12)
336#define	SIR_MAX			(12)
337
338/*==========================================================
339**
340**	Extended error codes.
341**	xerr_status field of struct ccb.
342**
343**==========================================================
344*/
345
346#define	XE_OK		(0)
347#define	XE_EXTRA_DATA	(1)	/* unexpected data phase */
348#define	XE_BAD_PHASE	(2)	/* illegal phase (4/5)   */
349
350/*==========================================================
351**
352**	Negotiation status.
353**	nego_status field	of struct ccb.
354**
355**==========================================================
356*/
357
358#define NS_SYNC		(1)
359#define NS_WIDE		(2)
360
361/*==========================================================
362**
363**	"Special features" of targets.
364**	quirks field		of struct tcb.
365**	actualquirks field	of struct ccb.
366**
367**==========================================================
368*/
369
370#define	QUIRK_AUTOSAVE	(0x01)
371#define	QUIRK_NOMSG	(0x02)
372#define QUIRK_NOSYNC	(0x10)
373#define QUIRK_NOWIDE16	(0x20)
374#define	QUIRK_UPDATE	(0x80)
375
376/*==========================================================
377**
378**	Capability bits in Inquire response byte 7.
379**
380**==========================================================
381*/
382
383#define	INQ7_QUEUE	(0x02)
384#define	INQ7_SYNC	(0x10)
385#define	INQ7_WIDE16	(0x20)
386
387/*==========================================================
388**
389**	Misc.
390**
391**==========================================================
392*/
393
394#define CCB_MAGIC	(0xf2691ad2)
395#define	MAX_TAGS	(16)		/* hard limit */
396
397/*==========================================================
398**
399**	OS dependencies.
400**
401**==========================================================
402*/
403
404#ifdef __NetBSD__
405	#define INT32     int
406	#define U_INT32   u_int
407	#define TIMEOUT   (void*)
408#else  /*__NetBSD__*/
409	#define INT32     int32
410	#define U_INT32   u_int32
411	#define TIMEOUT   (timeout_func_t)
412#endif /*__NetBSD__*/
413#define PRINT_ADDR(xp) sc_print_addr(xp->sc_link)
414
415/*==========================================================
416**
417**	Declaration of structs.
418**
419**==========================================================
420*/
421
422struct tcb;
423struct lcb;
424struct ccb;
425struct ncb;
426struct script;
427
428typedef struct ncb * ncb_p;
429typedef struct tcb * tcb_p;
430typedef struct lcb * lcb_p;
431typedef struct ccb * ccb_p;
432
433struct link {
434	u_long	l_cmd;
435	u_long	l_paddr;
436};
437
438struct	usrcmd {
439	u_long	target;
440	u_long	lun;
441	u_long	data;
442	u_long	cmd;
443};
444
445#define UC_SETSYNC      10
446#define UC_SETTAGS	11
447#define UC_SETDEBUG	12
448#define UC_SETORDER	13
449#define UC_SETWIDE	14
450#define UC_SETFLAG	15
451
452#define	UF_TRACE	(0x01)
453
454/*---------------------------------------
455**
456**	Timestamps for profiling
457**
458**---------------------------------------
459*/
460
461struct tstamp {
462	struct timeval	start;
463	struct timeval	end;
464	struct timeval	select;
465	struct timeval	command;
466	struct timeval	data;
467	struct timeval	status;
468	struct timeval	disconnect;
469	struct timeval	reselect;
470};
471
472/*
473**	profiling data (per device)
474*/
475
476struct profile {
477	u_long	num_trans;
478	u_long	num_bytes;
479	u_long	num_disc;
480	u_long	num_break;
481	u_long	num_int;
482	u_long	num_fly;
483	u_long	ms_setup;
484	u_long	ms_data;
485	u_long	ms_disc;
486	u_long	ms_post;
487};
488
489/*==========================================================
490**
491**	Declaration of structs:		target control block
492**
493**==========================================================
494*/
495
496struct tcb {
497	/*
498	**	during reselection the ncr jumps to this point
499	**	with SFBR set to the encoded target number
500	**	with bit 7 set.
501	**	if it's not this target, jump to the next.
502	**
503	**	JUMP  IF (SFBR != #target#)
504	**	@(next tcb)
505	*/
506
507	struct link   jump_tcb;
508
509	/*
510	**	load the actual values for the sxfer and the scntl3
511	**	register (sync/wide mode).
512	**
513	**	SCR_COPY (1);
514	**	@(sval field of this tcb)
515	**	@(sxfer register)
516	**	SCR_COPY (1);
517	**	@(wval field of this tcb)
518	**	@(scntl3 register)
519	*/
520
521	ncrcmd	getscr[6];
522
523	/*
524	**	if next message is "identify"
525	**	then load the message to SFBR,
526	**	else load 0 to SFBR.
527	**
528	**	CALL
529	**	<RESEL_LUN>
530	*/
531
532	struct link   call_lun;
533
534	/*
535	**	now look for the right lun.
536	**
537	**	JUMP
538	**	@(first ccb of this lun)
539	*/
540
541	struct link   jump_lcb;
542
543	/*
544	**	pointer to interrupted getcc ccb
545	*/
546
547	ccb_p   hold_cp;
548
549	/*
550	**	statistical data
551	*/
552
553	u_long	transfers;
554	u_long	bytes;
555
556	/*
557	**	user settable limits for sync transfer
558	**	and tagged commands.
559	*/
560
561	u_char	usrsync;
562	u_char	usrtags;
563	u_char	usrwide;
564	u_char	usrflag;
565
566	/*
567	**	negotiation of wide and synch transfer.
568	**	device quirks.
569	*/
570
571/*0*/	u_char	minsync;
572/*1*/	u_char	sval;
573/*2*/	u_short	period;
574/*0*/	u_char	maxoffs;
575
576/*1*/	u_char	quirks;
577
578/*2*/	u_char	widedone;
579/*3*/	u_char	wval;
580	/*
581	**	inquire data
582	*/
583#define MAX_INQUIRE 36
584	u_char	inqdata[MAX_INQUIRE];
585
586	/*
587	**	the lcb's of this tcb
588	*/
589
590	lcb_p   lp[MAX_LUN];
591};
592
593/*==========================================================
594**
595**	Declaration of structs:		lun control block
596**
597**==========================================================
598*/
599
600struct lcb {
601	/*
602	**	during reselection the ncr jumps to this point
603	**	with SFBR set to the "Identify" message.
604	**	if it's not this lun, jump to the next.
605	**
606	**	JUMP  IF (SFBR != #lun#)
607	**	@(next lcb of this target)
608	*/
609
610	struct link	jump_lcb;
611
612	/*
613	**	if next message is "simple tag",
614	**	then load the tag to SFBR,
615	**	else load 0 to SFBR.
616	**
617	**	CALL
618	**	<RESEL_TAG>
619	*/
620
621	struct link	call_tag;
622
623	/*
624	**	now look for the right ccb.
625	**
626	**	JUMP
627	**	@(first ccb of this lun)
628	*/
629
630	struct link	jump_ccb;
631
632	/*
633	**	start of the ccb chain
634	*/
635
636	ccb_p	next_ccb;
637
638	/*
639	**	Control of tagged queueing
640	*/
641
642	u_char		reqccbs;
643	u_char		actccbs;
644	u_char		reqlink;
645	u_char		actlink;
646	u_char		usetags;
647	u_char		lasttag;
648};
649
650/*==========================================================
651**
652**      Declaration of structs:     COMMAND control block
653**
654**==========================================================
655**
656**	This substructure is copied from the ccb to a
657**	global address after selection (or reselection)
658**	and copied back before disconnect.
659**
660**	These fields are accessible to the script processor.
661**
662**----------------------------------------------------------
663*/
664
665struct head {
666	/*
667	**	Execution of a ccb starts at this point.
668	**	It's a jump to the "SELECT" label
669	**	of the script.
670	**
671	**	After successful selection the script
672	**	processor overwrites it with a jump to
673	**	the IDLE label of the script.
674	*/
675
676	struct link	launch;
677
678	/*
679	**	Saved data pointer.
680	**	Points to the position in the script
681	**	responsible for the actual transfer
682	**	of data.
683	**	It's written after reception of a
684	**	"SAVE_DATA_POINTER" message.
685	**	The goalpointer points after
686	**	the last transfer command.
687	*/
688
689	u_long		savep;
690	u_long		lastp;
691	u_long		goalp;
692
693	/*
694	**	The virtual address of the ccb
695	**	containing this header.
696	*/
697
698	ccb_p	cp;
699
700	/*
701	**	space for some timestamps to gather
702	**	profiling data about devices and this driver.
703	*/
704
705	struct tstamp	stamp;
706
707	/*
708	**	status fields.
709	*/
710
711	u_char		status[8];
712};
713
714/*
715**	The status bytes are used by the host and the script processor.
716**
717**	The first four byte are copied to the scratchb register
718**	(declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
719**	and copied back just after disconnecting.
720**	Inside the script the XX_REG are used.
721**
722**	The last four bytes are used inside the script by "COPY" commands.
723**	Because source and destination must have the same alignment
724**	in a longword, the fields HAVE to be at the choosen offsets.
725**		xerr_st	(4)	0	(0x34)	scratcha
726**		sync_st	(5)	1	(0x05)	sxfer
727**		wide_st	(7)	3	(0x03)	scntl3
728*/
729
730/*
731**	First four bytes (script)
732*/
733#define  QU_REG	scr0
734#define  HS_REG	scr1
735#define  HS_PRT	nc_scr1
736#define  SS_REG	scr2
737#define  PS_REG	scr3
738
739/*
740**	First four bytes (host)
741*/
742#define  actualquirks  phys.header.status[0]
743#define  host_status   phys.header.status[1]
744#define  scsi_status   phys.header.status[2]
745#define  parity_status phys.header.status[3]
746
747/*
748**	Last four bytes (script)
749*/
750#define  xerr_st       header.status[4]	/* MUST be ==0 mod 4 */
751#define  sync_st       header.status[5]	/* MUST be ==1 mod 4 */
752#define  nego_st       header.status[6]
753#define  wide_st       header.status[7]	/* MUST be ==3 mod 4 */
754
755/*
756**	Last four bytes (host)
757*/
758#define  xerr_status   phys.xerr_st
759#define  sync_status   phys.sync_st
760#define  nego_status   phys.nego_st
761#define  wide_status   phys.wide_st
762
763/*==========================================================
764**
765**      Declaration of structs:     Data structure block
766**
767**==========================================================
768**
769**	During execution of a ccb by the script processor,
770**	the DSA (data structure address) register points
771**	to this substructure of the ccb.
772**	This substructure contains the header with
773**	the script-processor-changable data and
774**	data blocks for the indirect move commands.
775**
776**----------------------------------------------------------
777*/
778
779struct dsb {
780
781	/*
782	**	Header.
783	**	Has to be the first entry,
784	**	because it's jumped to by the
785	**	script processor
786	*/
787
788	struct head	header;
789
790	/*
791	**	Table data for Script
792	*/
793
794	struct scr_tblsel  select;
795	struct scr_tblmove smsg  ;
796	struct scr_tblmove smsg2 ;
797	struct scr_tblmove cmd   ;
798	struct scr_tblmove scmd  ;
799	struct scr_tblmove sense ;
800	struct scr_tblmove data [MAX_SCATTER];
801};
802
803/*==========================================================
804**
805**      Declaration of structs:     Command control block.
806**
807**==========================================================
808**
809**	During execution of a ccb by the script processor,
810**	the DSA (data structure address) register points
811**	to this substructure of the ccb.
812**	This substructure contains the header with
813**	the script-processor-changable data and then
814**	data blocks for the indirect move commands.
815**
816**----------------------------------------------------------
817*/
818
819
820struct ccb {
821	/*
822	**	during reselection the ncr jumps to this point.
823	**	If a "SIMPLE_TAG" message was received,
824	**	then SFBR is set to the tag.
825	**	else SFBR is set to 0
826	**	If looking for another tag, jump to the next ccb.
827	**
828	**	JUMP  IF (SFBR != #TAG#)
829	**	@(next ccb of this lun)
830	*/
831
832	struct link		jump_ccb;
833
834	/*
835	**	After execution of this call, the return address
836	**	(in  the TEMP register) points to the following
837	**	data structure block.
838	**	So copy it to the DSA register, and start
839	**	processing of this data structure.
840	**
841	**	CALL
842	**	<RESEL_TMP>
843	*/
844
845	struct link		call_tmp;
846
847	/*
848	**	This is the data structure which is
849	**	to be executed by the script processor.
850	*/
851
852	struct dsb		phys;
853
854	/*
855	**	If a data transfer phase is terminated too early
856	**	(after reception of a message (i.e. DISCONNECT)),
857	**	we have to prepare a mini script to transfer
858	**	the rest of the data.
859	*/
860
861	u_long			patch[8];
862
863	/*
864	**	The general SCSI driver provides a
865	**	pointer to a control block.
866	*/
867
868	struct scsi_xfer	*xfer;
869
870	/*
871	**	We prepare a message to be sent after selection,
872	**	and a second one to be sent after getcc selection.
873	**      Contents are IDENTIFY and SIMPLE_TAG.
874	**	While negotiating sync or wide transfer,
875	**	a SDTM or WDTM message is appended.
876	*/
877
878	u_char			scsi_smsg [8];
879	u_char			scsi_smsg2[8];
880
881	/*
882	**	Lock this ccb.
883	**	Flag is used while looking for a free ccb.
884	*/
885
886	u_long		magic;
887
888	/*
889	**	Physical address of this instance of ccb
890	*/
891
892	u_long		p_ccb;
893
894	/*
895	**	Completion time out for this job.
896	**	It's set to time of start + allowed number of seconds.
897	*/
898
899	u_long		tlimit;
900
901	/*
902	**	All ccbs of one hostadapter are chained.
903	*/
904
905	ccb_p		link_ccb;
906
907	/*
908	**	All ccbs of one target/lun are chained.
909	*/
910
911	ccb_p		next_ccb;
912
913	/*
914	**	Sense command
915	*/
916
917	u_char		sensecmd[6];
918
919	/*
920	**	Tag for this transfer.
921	**	It's patched into jump_ccb.
922	**	If it's not zero, a SIMPLE_TAG
923	**	message is included in smsg.
924	*/
925
926	u_char			tag;
927};
928
929#define CCB_PHYS(cp,lbl)	(cp->p_ccb + offsetof(struct ccb, lbl))
930
931/*==========================================================
932**
933**      Declaration of structs:     NCR device descriptor
934**
935**==========================================================
936*/
937
938struct ncb {
939#ifdef __NetBSD__
940	struct device sc_dev;
941	void *sc_ih;
942#else /* !__NetBSD__ */
943	int	unit;
944#endif /* __NetBSD__ */
945
946	/*-----------------------------------------------
947	**	Scripts ..
948	**-----------------------------------------------
949	**
950	**	During reselection the ncr jumps to this point.
951	**	The SFBR register is loaded with the encoded target id.
952	**
953	**	Jump to the first target.
954	**
955	**	JUMP
956	**	@(next tcb)
957	*/
958	struct link     jump_tcb;
959
960	/*-----------------------------------------------
961	**	Configuration ..
962	**-----------------------------------------------
963	**
964	**	virtual and physical addresses
965	**	of the 53c810 chip.
966	*/
967	vm_offset_t     vaddr;
968	vm_offset_t     paddr;
969
970	/*
971	**	pointer to the chip's registers.
972	*/
973	volatile
974	struct ncr_reg* reg;
975
976	/*
977	**	A copy of the script, relocated for this ncb.
978	*/
979	struct script	*script;
980
981	/*
982	**	Physical address of this instance of ncb->script
983	*/
984	u_long		p_script;
985
986	/*
987	**	The SCSI address of the host adapter.
988	*/
989	u_char	  myaddr;
990
991	/*
992	**	timing parameters
993	*/
994	u_char		ns_async;
995	u_char		ns_sync;
996	u_char		rv_scntl3;
997
998	/*-----------------------------------------------
999	**	Link to the generic SCSI driver
1000	**-----------------------------------------------
1001	*/
1002
1003	struct scsi_link	sc_link;
1004
1005	/*-----------------------------------------------
1006	**	Job control
1007	**-----------------------------------------------
1008	**
1009	**	Commands from user
1010	*/
1011	struct usrcmd	user;
1012	u_char		order;
1013
1014	/*
1015	**	Target data
1016	*/
1017	struct tcb	target[MAX_TARGET];
1018
1019	/*
1020	**	Start queue.
1021	*/
1022	u_long		squeue [MAX_START];
1023	u_short		squeueput;
1024	u_short		actccbs;
1025
1026	/*
1027	**	Timeout handler
1028	*/
1029	u_long		heartbeat;
1030	u_short		ticks;
1031	u_short		latetime;
1032	u_long		lasttime;
1033
1034	/*-----------------------------------------------
1035	**	Debug and profiling
1036	**-----------------------------------------------
1037	**
1038	**	register dump
1039	*/
1040	struct ncr_reg	regdump;
1041	struct timeval	regtime;
1042
1043	/*
1044	**	Profiling data
1045	*/
1046	struct profile	profile;
1047	u_long		disc_phys;
1048	u_long		disc_ref;
1049
1050	/*
1051	**	The global header.
1052	**	Accessible to both the host and the
1053	**	script-processor.
1054	*/
1055	struct head     header;
1056
1057	/*
1058	**	The global control block.
1059	**	It's used only during the configuration phase.
1060	**	A target control block will be created
1061	**	after the first successful transfer.
1062	*/
1063	struct ccb      ccb;
1064
1065	/*
1066	**	message buffers.
1067	**	Should be longword aligned,
1068	**	because they're written with a
1069	**	COPY script command.
1070	*/
1071	u_char		msgout[8];
1072	u_char		msgin [8];
1073	u_long		lastmsg;
1074
1075	/*
1076	**	Buffer for STATUS_IN phase.
1077	*/
1078	u_char		scratch;
1079
1080	/*
1081	**	controller chip dependent maximal transfer width.
1082	*/
1083	u_char		maxwide;
1084
1085	/*
1086	**	option for M_IDENTIFY message: enables disconnecting
1087	*/
1088	u_char		disc;
1089
1090#ifdef NCR_IOMAPPED
1091	/*
1092	**	address of the ncr control registers in io space
1093	*/
1094	u_short		port;
1095#endif
1096};
1097
1098#define NCB_SCRIPT_PHYS(np,lbl)	(np->p_script + offsetof (struct script, lbl))
1099
1100/*==========================================================
1101**
1102**
1103**      Script for NCR-Processor.
1104**
1105**	Use ncr_script_fill() to create the variable parts.
1106**	Use ncr_script_copy_and_bind() to make a copy and
1107**	bind to physical addresses.
1108**
1109**
1110**==========================================================
1111**
1112**	We have to know the offsets of all labels before
1113**	we reach them (for forward jumps).
1114**	Therefore we declare a struct here.
1115**	If you make changes inside the script,
1116**	DONT FORGET TO CHANGE THE LENGTHS HERE!
1117**
1118**----------------------------------------------------------
1119*/
1120
1121struct script {
1122	ncrcmd	start		[  7];
1123	ncrcmd	start0		[  2];
1124	ncrcmd	start1		[  3];
1125	ncrcmd  startpos	[  1];
1126	ncrcmd  tryloop		[MAX_START*5+2];
1127	ncrcmd  trysel		[  8];
1128	ncrcmd	skip		[  8];
1129	ncrcmd	skip2		[  3];
1130	ncrcmd  idle		[  2];
1131	ncrcmd	select		[ 24];
1132	ncrcmd	prepare		[  4];
1133	ncrcmd	loadpos		[ 14];
1134	ncrcmd	prepare2	[ 24];
1135	ncrcmd	setmsg		[  5];
1136	ncrcmd  clrack		[  2];
1137	ncrcmd  dispatch	[ 33];
1138	ncrcmd	no_data		[ 17];
1139	ncrcmd  checkatn	[ 10];
1140	ncrcmd  command		[ 15];
1141	ncrcmd  status		[ 27];
1142	ncrcmd  msg_in		[ 26];
1143	ncrcmd  msg_bad		[  6];
1144	ncrcmd  msg_parity	[  6];
1145	ncrcmd	msg_reject	[  8];
1146	ncrcmd	msg_ign_residue	[ 32];
1147	ncrcmd  msg_extended	[ 18];
1148	ncrcmd  msg_ext_2	[ 18];
1149	ncrcmd	msg_wdtr	[ 27];
1150	ncrcmd  msg_ext_3	[ 18];
1151	ncrcmd	msg_sdtr	[ 27];
1152	ncrcmd  complete	[ 13];
1153	ncrcmd	cleanup		[ 12];
1154	ncrcmd	cleanup0	[ 11];
1155	ncrcmd	signal		[ 10];
1156	ncrcmd  save_dp		[  5];
1157	ncrcmd  restore_dp	[  5];
1158	ncrcmd  disconnect	[ 12];
1159	ncrcmd  disconnect0	[  5];
1160	ncrcmd  disconnect1	[ 23];
1161	ncrcmd	msg_out		[  9];
1162	ncrcmd	msg_out_done	[  7];
1163	ncrcmd	msg_out_abort	[ 10];
1164	ncrcmd  getcc		[  4];
1165	ncrcmd  getcc1		[  5];
1166#ifdef NCR_GETCC_WITHMSG
1167	ncrcmd	getcc2		[ 33];
1168#else
1169	ncrcmd	getcc2		[ 14];
1170#endif
1171	ncrcmd	getcc3		[ 10];
1172	ncrcmd  badgetcc	[  6];
1173	ncrcmd	reselect	[ 12];
1174	ncrcmd	reselect2	[  6];
1175	ncrcmd	resel_tmp	[  5];
1176	ncrcmd  resel_lun	[ 18];
1177	ncrcmd	resel_tag	[ 24];
1178	ncrcmd  data_in		[MAX_SCATTER * 4 + 7];
1179	ncrcmd  data_out	[MAX_SCATTER * 4 + 7];
1180	ncrcmd	aborttag	[  4];
1181	ncrcmd	abort		[ 22];
1182	ncrcmd	snooptest	[  9];
1183	ncrcmd	snoopend	[  2];
1184};
1185
1186/*==========================================================
1187**
1188**
1189**      Function headers.
1190**
1191**
1192**==========================================================
1193*/
1194
1195#ifdef KERNEL
1196static	void	ncr_alloc_ccb	(ncb_p np, struct scsi_xfer * xp);
1197static	void	ncr_complete	(ncb_p np, ccb_p cp);
1198static	int	ncr_delta	(struct timeval * from, struct timeval * to);
1199static	void	ncr_exception	(ncb_p np);
1200static	void	ncr_free_ccb	(ncb_p np, ccb_p cp, int flags);
1201static	void	ncr_getclock	(ncb_p np);
1202static	ccb_p	ncr_get_ccb	(ncb_p np, u_long flags, u_long t,u_long l);
1203static  U_INT32 ncr_info	(int unit);
1204static	void	ncr_init	(ncb_p np, char * msg, u_long code);
1205#ifdef __NetBSD__
1206static	int     ncr_intr        (void *);
1207#else	/* !__NetBSD__ */
1208static	int	ncr_intr	(ncb_p np);
1209#endif	/* __NetBSD__ */
1210static	void	ncr_int_ma	(ncb_p np);
1211static	void	ncr_int_sir	(ncb_p np);
1212static  void    ncr_int_sto     (ncb_p np);
1213#ifndef NEW_SCSICONF
1214static	u_long	ncr_lookup	(char* id);
1215#endif /* NEW_SCSICONF */
1216static	void	ncr_min_phys	(struct buf *bp);
1217static	void	ncr_negotiate	(struct ncb* np, struct tcb* tp);
1218static	void	ncr_opennings	(ncb_p np, lcb_p lp, struct scsi_xfer * xp);
1219static	void	ncb_profile	(ncb_p np, ccb_p cp);
1220static	void	ncr_script_copy_and_bind
1221				(struct script * script, ncb_p np);
1222static  void    ncr_script_fill (struct script * scr);
1223static	int	ncr_scatter	(struct dsb* phys,u_long vaddr,u_long datalen);
1224static	void	ncr_setmaxtags	(tcb_p tp, u_long usrtags);
1225static	void	ncr_setsync	(ncb_p np, ccb_p cp, u_char sxfer);
1226static	void	ncr_settags     (tcb_p tp, lcb_p lp);
1227static	void	ncr_setwide	(ncb_p np, ccb_p cp, u_char wide);
1228static	int	ncr_show_msg	(u_char * msg);
1229static	int	ncr_snooptest	(ncb_p np);
1230static	INT32	ncr_start       (struct scsi_xfer *xp);
1231static	void	ncr_timeout	(ncb_p np);
1232static	void	ncr_usercmd	(ncb_p np);
1233static  void    ncr_wakeup      (ncb_p np, u_long code);
1234
1235#ifdef __NetBSD__
1236static	int	ncr_probe	(struct device *, void *, void *);
1237static	void	ncr_attach	(struct device *, struct device *, void *);
1238#else /* !__NetBSD */
1239static  char*	ncr_probe       (pcici_t tag, pcidi_t type);
1240static	void	ncr_attach	(pcici_t tag, int unit);
1241#endif /* __NetBSD__ */
1242
1243#endif /* KERNEL */
1244
1245/*==========================================================
1246**
1247**
1248**      Global static data.
1249**
1250**
1251**==========================================================
1252*/
1253
1254
1255static char ident[] =
1256	"\n$Id: ncr.c,v 1.49 1995/09/21 17:27:28 se Exp $\n";
1257
1258u_long	ncr_version = NCR_VERSION	* 11
1259	+ (u_long) sizeof (struct ncb)	*  7
1260	+ (u_long) sizeof (struct ccb)	*  5
1261	+ (u_long) sizeof (struct lcb)	*  3
1262	+ (u_long) sizeof (struct tcb)	*  2;
1263
1264#ifdef KERNEL
1265
1266#ifndef __NetBSD__
1267u_long		nncr=MAX_UNITS;
1268ncb_p		ncrp [MAX_UNITS];
1269#endif /* !__NetBSD__ */
1270
1271static int ncr_debug = SCSI_DEBUG_FLAGS;
1272
1273int ncr_cache; /* to be aligned _NOT_ static */
1274
1275/*==========================================================
1276**
1277**
1278**      Global static data:	auto configure
1279**
1280**
1281**==========================================================
1282*/
1283
1284#define	NCR_810_ID	(0x00011000ul)
1285#define	NCR_810AP_ID	(0x00051000ul)
1286#define	NCR_815_ID	(0x00041000ul)
1287#define	NCR_825_ID	(0x00031000ul)
1288#define	NCR_860_ID	(0x00061000ul)
1289#define	NCR_875_ID	(0x000f1000ul)
1290
1291#ifdef __NetBSD__
1292
1293struct	cfdriver ncrcd = {
1294	NULL, "ncr", ncr_probe, ncr_attach, DV_DISK, sizeof(struct ncb)
1295};
1296
1297#else /* !__NetBSD__ */
1298
1299static u_long ncr_count;
1300
1301struct	pci_device ncr_device = {
1302	"ncr",
1303	ncr_probe,
1304	ncr_attach,
1305	&ncr_count,
1306	NULL
1307};
1308
1309DATA_SET (pcidevice_set, ncr_device);
1310
1311#endif /* !__NetBSD__ */
1312
1313struct scsi_adapter ncr_switch =
1314{
1315	ncr_start,
1316	ncr_min_phys,
1317	0,
1318	0,
1319#ifndef __NetBSD__
1320	ncr_info,
1321	"ncr",
1322#endif /* !__NetBSD__ */
1323};
1324
1325struct scsi_device ncr_dev =
1326{
1327	NULL,			/* Use default error handler */
1328	NULL,			/* have a queue, served by this */
1329	NULL,			/* have no async handler */
1330	NULL,			/* Use default 'done' routine */
1331#ifndef __NetBSD__
1332	"ncr",
1333#endif /* !__NetBSD__ */
1334};
1335
1336#ifdef __NetBSD__
1337
1338#define	ncr_name(np)	(np->sc_dev.dv_xname)
1339
1340#else /* !__NetBSD__ */
1341
1342static char *ncr_name (ncb_p np)
1343{
1344	static char name[10];
1345	sprintf(name, "ncr%d", np->unit);
1346	return (name);
1347}
1348#endif
1349
1350/*==========================================================
1351**
1352**
1353**      Scripts for NCR-Processor.
1354**
1355**      Use ncr_script_bind for binding to physical addresses.
1356**
1357**
1358**==========================================================
1359**
1360**	NADDR generates a reference to a field of the controller data.
1361**	PADDR generates a reference to another part of the script.
1362**	RADDR generates a reference to a script processor register.
1363**	FADDR generates a reference to a script processor register
1364**		with offset.
1365**
1366**----------------------------------------------------------
1367*/
1368
1369#define	RELOC_SOFTC	0x40000000
1370#define	RELOC_LABEL	0x50000000
1371#define	RELOC_REGISTER	0x60000000
1372#define	RELOC_MASK	0xf0000000
1373
1374#define	NADDR(label)	(RELOC_SOFTC | offsetof(struct ncb, label))
1375#define PADDR(label)    (RELOC_LABEL | offsetof(struct script, label))
1376#define	RADDR(label)	(RELOC_REGISTER | REG(label))
1377#define	FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
1378
1379static	struct script script0 = {
1380/*--------------------------< START >-----------------------*/ {
1381	/*
1382	**	Claim to be still alive ...
1383	*/
1384	SCR_COPY (sizeof (((struct ncb *)0)->heartbeat)),
1385		(ncrcmd) &time.tv_sec,
1386		NADDR (heartbeat),
1387	/*
1388	**      Make data structure address invalid.
1389	**      clear SIGP.
1390	*/
1391	SCR_LOAD_REG (dsa, 0xff),
1392		0,
1393	SCR_FROM_REG (ctest2),
1394		0,
1395}/*-------------------------< START0 >----------------------*/,{
1396	/*
1397	**	Hook for interrupted GetConditionCode.
1398	**	Will be patched to ... IFTRUE by
1399	**	the interrupt handler.
1400	*/
1401	SCR_INT ^ IFFALSE (0),
1402		SIR_SENSE_RESTART,
1403
1404}/*-------------------------< START1 >----------------------*/,{
1405	/*
1406	**	Hook for stalled start queue.
1407	**	Will be patched to IFTRUE by the interrupt handler.
1408	*/
1409	SCR_INT ^ IFFALSE (0),
1410		SIR_STALL_RESTART,
1411	/*
1412	**	Then jump to a certain point in tryloop.
1413	**	Due to the lack of indirect addressing the code
1414	**	is self modifying here.
1415	*/
1416	SCR_JUMP,
1417}/*-------------------------< STARTPOS >--------------------*/,{
1418		PADDR(tryloop),
1419}/*-------------------------< TRYLOOP >---------------------*/,{
1420/*
1421**	Load an entry of the start queue into dsa
1422**	and try to start it by jumping to TRYSEL.
1423**
1424**	Because the size depends on the
1425**	#define MAX_START parameter, it is filled
1426**	in at runtime.
1427**
1428**-----------------------------------------------------------
1429**
1430**  ##===========< I=0; i<MAX_START >===========
1431**  ||	SCR_COPY (4),
1432**  ||		NADDR (squeue[i]),
1433**  ||		RADDR (dsa),
1434**  ||	SCR_CALL,
1435**  ||		PADDR (trysel),
1436**  ##==========================================
1437**
1438**	SCR_JUMP,
1439**		PADDR(tryloop),
1440**
1441**-----------------------------------------------------------
1442*/
14430
1444
1445}/*-------------------------< TRYSEL >----------------------*/,{
1446	/*
1447	**	Now:
1448	**	DSA: Address of a Data Structure
1449	**	or   Address of the IDLE-Label.
1450	**
1451	**	TEMP:	Address of a script, which tries to
1452	**		start the NEXT entry.
1453	**
1454	**	Save the TEMP register into the SCRATCHA register.
1455	**	Then copy the DSA to TEMP and RETURN.
1456	**	This is kind of an indirect jump.
1457	**	(The script processor has NO stack, so the
1458	**	CALL is actually a jump and link, and the
1459	**	RETURN is an indirect jump.)
1460	**
1461	**	If the slot was empty, DSA contains the address
1462	**	of the IDLE part of this script. The processor
1463	**	jumps to IDLE and waits for a reselect.
1464	**	It will wake up and try the same slot again
1465	**	after the SIGP bit becomes set by the host.
1466	**
1467	**	If the slot was not empty, DSA contains
1468	**	the address of the phys-part of a ccb.
1469	**	The processor jumps to this address.
1470	**	phys starts with head,
1471	**	head starts with launch,
1472	**	so actually the processor jumps to
1473	**	the lauch part.
1474	**	If the entry is scheduled for execution,
1475	**	then launch contains a jump to SELECT.
1476	**	If it's not scheduled, it contains a jump to IDLE.
1477	*/
1478	SCR_COPY (4),
1479		RADDR (temp),
1480		RADDR (scratcha),
1481	SCR_COPY (4),
1482		RADDR (dsa),
1483		RADDR (temp),
1484	SCR_RETURN,
1485		0
1486
1487}/*-------------------------< SKIP >------------------------*/,{
1488	/*
1489	**	This entry has been canceled.
1490	**	Next time use the next slot.
1491	*/
1492	SCR_COPY (4),
1493		RADDR (scratcha),
1494		PADDR (startpos),
1495	/*
1496	**	patch the launch field.
1497	**	should look like an idle process.
1498	*/
1499	SCR_COPY (4),
1500		RADDR (dsa),
1501		PADDR (skip2),
1502	SCR_COPY (8),
1503		PADDR (idle),
1504}/*-------------------------< SKIP2 >-----------------------*/,{
1505		0,
1506	SCR_JUMP,
1507		PADDR(start),
1508}/*-------------------------< IDLE >------------------------*/,{
1509	/*
1510	**	Nothing to do?
1511	**	Wait for reselect.
1512	*/
1513	SCR_JUMP,
1514		PADDR(reselect),
1515
1516}/*-------------------------< SELECT >----------------------*/,{
1517	/*
1518	**	DSA	contains the address of a scheduled
1519	**		data structure.
1520	**
1521	**	SCRATCHA contains the address of the script,
1522	**		which starts the next entry.
1523	**
1524	**	Set Initiator mode.
1525	**
1526	**	(Target mode is left as an exercise for the reader)
1527	*/
1528
1529	SCR_CLR (SCR_TRG),
1530		0,
1531	SCR_LOAD_REG (HS_REG, 0xff),
1532		0,
1533
1534	/*
1535	**      And try to select this target.
1536	*/
1537	SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
1538		PADDR (reselect),
1539
1540	/*
1541	**	Now there are 4 possibilities:
1542	**
1543	**	(1) The ncr looses arbitration.
1544	**	This is ok, because it will try again,
1545	**	when the bus becomes idle.
1546	**	(But beware of the timeout function!)
1547	**
1548	**	(2) The ncr is reselected.
1549	**	Then the script processor takes the jump
1550	**	to the RESELECT label.
1551	**
1552	**	(3) The ncr completes the selection.
1553	**	Then it will execute the next statement.
1554	**
1555	**	(4) There is a selection timeout.
1556	**	Then the ncr should interrupt the host and stop.
1557	**	Unfortunately, it seems to continue execution
1558	**	of the script. But it will fail with an
1559	**	IID-interrupt on the next WHEN.
1560	*/
1561
1562	SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
1563		0,
1564
1565	/*
1566	**	Save target id to ctest0 register
1567	*/
1568
1569	SCR_FROM_REG (sdid),
1570		0,
1571	SCR_TO_REG (ctest0),
1572		0,
1573	/*
1574	**	Send the IDENTIFY and SIMPLE_TAG messages
1575	**	(and the M_X_SYNC_REQ message)
1576	*/
1577	SCR_MOVE_TBL ^ SCR_MSG_OUT,
1578		offsetof (struct dsb, smsg),
1579	SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_OUT)),
1580		-16,
1581	SCR_CLR (SCR_ATN),
1582		0,
1583	SCR_COPY (1),
1584		RADDR (sfbr),
1585		NADDR (lastmsg),
1586	/*
1587	**	Selection complete.
1588	**	Next time use the next slot.
1589	*/
1590	SCR_COPY (4),
1591		RADDR (scratcha),
1592		PADDR (startpos),
1593}/*-------------------------< PREPARE >----------------------*/,{
1594	/*
1595	**      The ncr doesn't have an indirect load
1596	**	or store command. So we have to
1597	**	copy part of the control block to a
1598	**	fixed place, where we can access it.
1599	**
1600	**	We patch the address part of a
1601	**	COPY command with the DSA-register.
1602	*/
1603	SCR_COPY (4),
1604		RADDR (dsa),
1605		PADDR (loadpos),
1606	/*
1607	**	then we do the actual copy.
1608	*/
1609	SCR_COPY (sizeof (struct head)),
1610	/*
1611	**	continued after the next label ...
1612	*/
1613
1614}/*-------------------------< LOADPOS >---------------------*/,{
1615		0,
1616		NADDR (header),
1617	/*
1618	**      Mark this ccb as not scheduled.
1619	*/
1620	SCR_COPY (8),
1621		PADDR (idle),
1622		NADDR (header.launch),
1623	/*
1624	**      Set a time stamp for this selection
1625	*/
1626	SCR_COPY (sizeof (struct timeval)),
1627		(ncrcmd) &time,
1628		NADDR (header.stamp.select),
1629	/*
1630	**      load the savep (saved pointer) into
1631	**      the TEMP register (actual pointer)
1632	*/
1633	SCR_COPY (4),
1634		NADDR (header.savep),
1635		RADDR (temp),
1636	/*
1637	**      Initialize the status registers
1638	*/
1639	SCR_COPY (4),
1640		NADDR (header.status),
1641		RADDR (scr0),
1642
1643}/*-------------------------< PREPARE2 >---------------------*/,{
1644	/*
1645	**      Load the synchronous mode register
1646	*/
1647	SCR_COPY (1),
1648		NADDR (sync_st),
1649		RADDR (sxfer),
1650	/*
1651	**      Load the wide mode and timing register
1652	*/
1653	SCR_COPY (1),
1654		NADDR (wide_st),
1655		RADDR (scntl3),
1656	/*
1657	**	Initialize the msgout buffer with a NOOP message.
1658	*/
1659	SCR_LOAD_REG (scratcha, M_NOOP),
1660		0,
1661	SCR_COPY (1),
1662		RADDR (scratcha),
1663		NADDR (msgout),
1664	SCR_COPY (1),
1665		RADDR (scratcha),
1666		NADDR (msgin),
1667	/*
1668	**	Message in phase ?
1669	*/
1670	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
1671		PADDR (dispatch),
1672	/*
1673	**	Extended or reject message ?
1674	*/
1675	SCR_FROM_REG (sbdl),
1676		0,
1677	SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
1678		PADDR (msg_in),
1679	SCR_JUMP ^ IFTRUE (DATA (M_REJECT)),
1680		PADDR (msg_reject),
1681	/*
1682	**	normal processing
1683	*/
1684	SCR_JUMP,
1685		PADDR (dispatch),
1686}/*-------------------------< SETMSG >----------------------*/,{
1687	SCR_COPY (1),
1688		RADDR (scratcha),
1689		NADDR (msgout),
1690	SCR_SET (SCR_ATN),
1691		0,
1692}/*-------------------------< CLRACK >----------------------*/,{
1693	/*
1694	**	Terminate possible pending message phase.
1695	*/
1696	SCR_CLR (SCR_ACK),
1697		0,
1698
1699}/*-----------------------< DISPATCH >----------------------*/,{
1700	SCR_FROM_REG (HS_REG),
1701		0,
1702	SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
1703		SIR_NEGO_FAILED,
1704	/*
1705	**	remove bogus output signals
1706	*/
1707	SCR_REG_REG (socl, SCR_AND, CACK|CATN),
1708		0,
1709	SCR_RETURN ^ IFTRUE (WHEN (SCR_DATA_OUT)),
1710		0,
1711	SCR_RETURN ^ IFTRUE (IF (SCR_DATA_IN)),
1712		0,
1713	SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
1714		PADDR (msg_out),
1715	SCR_JUMP ^ IFTRUE (IF (SCR_MSG_IN)),
1716		PADDR (msg_in),
1717	SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
1718		PADDR (command),
1719	SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
1720		PADDR (status),
1721	/*
1722	**      Discard one illegal phase byte, if required.
1723	*/
1724	SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
1725		0,
1726	SCR_COPY (1),
1727		RADDR (scratcha),
1728		NADDR (xerr_st),
1729	SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
1730		8,
1731	SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
1732		NADDR (scratch),
1733	SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
1734		8,
1735	SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
1736		NADDR (scratch),
1737	SCR_JUMP,
1738		PADDR (dispatch),
1739
1740}/*-------------------------< NO_DATA >--------------------*/,{
1741	/*
1742	**	The target wants to tranfer too much data
1743	**	or in the wrong direction.
1744	**      Remember that in extended error.
1745	*/
1746	SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
1747		0,
1748	SCR_COPY (1),
1749		RADDR (scratcha),
1750		NADDR (xerr_st),
1751	/*
1752	**      Discard one data byte, if required.
1753	*/
1754	SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
1755		8,
1756	SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
1757		NADDR (scratch),
1758	SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
1759		8,
1760	SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
1761		NADDR (scratch),
1762	/*
1763	**      .. and repeat as required.
1764	*/
1765	SCR_CALL,
1766		PADDR (dispatch),
1767	SCR_JUMP,
1768		PADDR (no_data),
1769}/*-------------------------< CHECKATN >--------------------*/,{
1770	/*
1771	**	If AAP (bit 1 of scntl0 register) is set
1772	**	and a parity error is detected,
1773	**	the script processor asserts ATN.
1774	**
1775	**	The target should switch to a MSG_OUT phase
1776	**	to get the message.
1777	*/
1778	SCR_FROM_REG (socl),
1779		0,
1780	SCR_JUMP ^ IFFALSE (MASK (CATN, CATN)),
1781		PADDR (dispatch),
1782	/*
1783	**	count it
1784	*/
1785	SCR_REG_REG (PS_REG, SCR_ADD, 1),
1786		0,
1787	/*
1788	**	Prepare a M_ID_ERROR message
1789	**	(initiator detected error).
1790	**	The target should retry the transfer.
1791	*/
1792	SCR_LOAD_REG (scratcha, M_ID_ERROR),
1793		0,
1794	SCR_JUMP,
1795		PADDR (setmsg),
1796
1797}/*-------------------------< COMMAND >--------------------*/,{
1798	/*
1799	**	If this is not a GETCC transfer ...
1800	*/
1801	SCR_FROM_REG (SS_REG),
1802		0,
1803/*<<<*/	SCR_JUMPR ^ IFTRUE (DATA (S_CHECK_COND)),
1804		28,
1805	/*
1806	**	... set a timestamp ...
1807	*/
1808	SCR_COPY (sizeof (struct timeval)),
1809		(ncrcmd) &time,
1810		NADDR (header.stamp.command),
1811	/*
1812	**	... and send the command
1813	*/
1814	SCR_MOVE_TBL ^ SCR_COMMAND,
1815		offsetof (struct dsb, cmd),
1816	SCR_JUMP,
1817		PADDR (dispatch),
1818	/*
1819	**	Send the GETCC command
1820	*/
1821/*>>>*/	SCR_MOVE_TBL ^ SCR_COMMAND,
1822		offsetof (struct dsb, scmd),
1823	SCR_JUMP,
1824		PADDR (dispatch),
1825
1826}/*-------------------------< STATUS >--------------------*/,{
1827	/*
1828	**	set the timestamp.
1829	*/
1830	SCR_COPY (sizeof (struct timeval)),
1831		(ncrcmd) &time,
1832		NADDR (header.stamp.status),
1833	/*
1834	**	If this is a GETCC transfer,
1835	*/
1836	SCR_FROM_REG (SS_REG),
1837		0,
1838/*<<<*/	SCR_JUMPR ^ IFFALSE (DATA (S_CHECK_COND)),
1839		40,
1840	/*
1841	**	get the status
1842	*/
1843	SCR_MOVE_ABS (1) ^ SCR_STATUS,
1844		NADDR (scratch),
1845	/*
1846	**	Save status to scsi_status.
1847	**	Mark as complete.
1848	**	And wait for disconnect.
1849	*/
1850	SCR_TO_REG (SS_REG),
1851		0,
1852	SCR_REG_REG (SS_REG, SCR_OR, S_SENSE),
1853		0,
1854	SCR_LOAD_REG (HS_REG, HS_COMPLETE),
1855		0,
1856	SCR_JUMP,
1857		PADDR (checkatn),
1858	/*
1859	**	If it was no GETCC transfer,
1860	**	save the status to scsi_status.
1861	*/
1862/*>>>*/	SCR_MOVE_ABS (1) ^ SCR_STATUS,
1863		NADDR (scratch),
1864	SCR_TO_REG (SS_REG),
1865		0,
1866	/*
1867	**	if it was no check condition ...
1868	*/
1869	SCR_JUMP ^ IFTRUE (DATA (S_CHECK_COND)),
1870		PADDR (checkatn),
1871	/*
1872	**	... mark as complete.
1873	*/
1874	SCR_LOAD_REG (HS_REG, HS_COMPLETE),
1875		0,
1876	SCR_JUMP,
1877		PADDR (checkatn),
1878
1879}/*-------------------------< MSG_IN >--------------------*/,{
1880	/*
1881	**	Get the first byte of the message
1882	**	and save it to SCRATCHA.
1883	**
1884	**	The script processor doesn't negate the
1885	**	ACK signal after this transfer.
1886	*/
1887	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
1888		NADDR (msgin[0]),
1889	/*
1890	**	Check for message parity error.
1891	*/
1892	SCR_TO_REG (scratcha),
1893		0,
1894	SCR_FROM_REG (socl),
1895		0,
1896	SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
1897		PADDR (msg_parity),
1898	SCR_FROM_REG (scratcha),
1899		0,
1900	/*
1901	**	Parity was ok, handle this message.
1902	*/
1903	SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)),
1904		PADDR (complete),
1905	SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)),
1906		PADDR (save_dp),
1907	SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)),
1908		PADDR (restore_dp),
1909	SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)),
1910		PADDR (disconnect),
1911	SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
1912		PADDR (msg_extended),
1913	SCR_JUMP ^ IFTRUE (DATA (M_NOOP)),
1914		PADDR (clrack),
1915	SCR_JUMP ^ IFTRUE (DATA (M_REJECT)),
1916		PADDR (msg_reject),
1917	SCR_JUMP ^ IFTRUE (DATA (M_IGN_RESIDUE)),
1918		PADDR (msg_ign_residue),
1919	/*
1920	**	Rest of the messages left as
1921	**	an exercise ...
1922	**
1923	**	Unimplemented messages:
1924	**	fall through to MSG_BAD.
1925	*/
1926}/*-------------------------< MSG_BAD >------------------*/,{
1927	/*
1928	**	unimplemented message - reject it.
1929	*/
1930	SCR_INT,
1931		SIR_REJECT_SENT,
1932	SCR_LOAD_REG (scratcha, M_REJECT),
1933		0,
1934	SCR_JUMP,
1935		PADDR (setmsg),
1936
1937}/*-------------------------< MSG_PARITY >---------------*/,{
1938	/*
1939	**	count it
1940	*/
1941	SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
1942		0,
1943	/*
1944	**	send a "message parity error" message.
1945	*/
1946	SCR_LOAD_REG (scratcha, M_PARITY),
1947		0,
1948	SCR_JUMP,
1949		PADDR (setmsg),
1950}/*-------------------------< MSG_REJECT >---------------*/,{
1951	/*
1952	**	If a negotiation was in progress,
1953	**	negotiation failed.
1954	*/
1955	SCR_FROM_REG (HS_REG),
1956		0,
1957	SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
1958		SIR_NEGO_FAILED,
1959	/*
1960	**	else make host log this message
1961	*/
1962	SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
1963		SIR_REJECT_RECEIVED,
1964	SCR_JUMP,
1965		PADDR (clrack),
1966
1967}/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
1968	/*
1969	**	Terminate cycle
1970	*/
1971	SCR_CLR (SCR_ACK),
1972		0,
1973	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
1974		PADDR (dispatch),
1975	/*
1976	**	get residue size.
1977	*/
1978	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
1979		NADDR (msgin[1]),
1980	/*
1981	**	Check for message parity error.
1982	*/
1983	SCR_TO_REG (scratcha),
1984		0,
1985	SCR_FROM_REG (socl),
1986		0,
1987	SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
1988		PADDR (msg_parity),
1989	SCR_FROM_REG (scratcha),
1990		0,
1991	/*
1992	**	Size is 0 .. ignore message.
1993	*/
1994	SCR_JUMP ^ IFTRUE (DATA (0)),
1995		PADDR (clrack),
1996	/*
1997	**	Size is not 1 .. have to interrupt.
1998	*/
1999/*<<<*/	SCR_JUMPR ^ IFFALSE (DATA (1)),
2000		40,
2001	/*
2002	**	Check for residue byte in swide register
2003	*/
2004	SCR_FROM_REG (scntl2),
2005		0,
2006/*<<<*/	SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
2007		16,
2008	/*
2009	**	There IS data in the swide register.
2010	**	Discard it.
2011	*/
2012	SCR_REG_REG (scntl2, SCR_OR, WSR),
2013		0,
2014	SCR_JUMP,
2015		PADDR (clrack),
2016	/*
2017	**	Load again the size to the sfbr register.
2018	*/
2019/*>>>*/	SCR_FROM_REG (scratcha),
2020		0,
2021/*>>>*/	SCR_INT,
2022		SIR_IGN_RESIDUE,
2023	SCR_JUMP,
2024		PADDR (clrack),
2025
2026}/*-------------------------< MSG_EXTENDED >-------------*/,{
2027	/*
2028	**	Terminate cycle
2029	*/
2030	SCR_CLR (SCR_ACK),
2031		0,
2032	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2033		PADDR (dispatch),
2034	/*
2035	**	get length.
2036	*/
2037	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2038		NADDR (msgin[1]),
2039	/*
2040	**	Check for message parity error.
2041	*/
2042	SCR_TO_REG (scratcha),
2043		0,
2044	SCR_FROM_REG (socl),
2045		0,
2046	SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2047		PADDR (msg_parity),
2048	SCR_FROM_REG (scratcha),
2049		0,
2050	/*
2051	*/
2052	SCR_JUMP ^ IFTRUE (DATA (3)),
2053		PADDR (msg_ext_3),
2054	SCR_JUMP ^ IFFALSE (DATA (2)),
2055		PADDR (msg_bad),
2056}/*-------------------------< MSG_EXT_2 >----------------*/,{
2057	SCR_CLR (SCR_ACK),
2058		0,
2059	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2060		PADDR (dispatch),
2061	/*
2062	**	get extended message code.
2063	*/
2064	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2065		NADDR (msgin[2]),
2066	/*
2067	**	Check for message parity error.
2068	*/
2069	SCR_TO_REG (scratcha),
2070		0,
2071	SCR_FROM_REG (socl),
2072		0,
2073	SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2074		PADDR (msg_parity),
2075	SCR_FROM_REG (scratcha),
2076		0,
2077	SCR_JUMP ^ IFTRUE (DATA (M_X_WIDE_REQ)),
2078		PADDR (msg_wdtr),
2079	/*
2080	**	unknown extended message
2081	*/
2082	SCR_JUMP,
2083		PADDR (msg_bad)
2084}/*-------------------------< MSG_WDTR >-----------------*/,{
2085	SCR_CLR (SCR_ACK),
2086		0,
2087	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2088		PADDR (dispatch),
2089	/*
2090	**	get data bus width
2091	*/
2092	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2093		NADDR (msgin[3]),
2094	SCR_FROM_REG (socl),
2095		0,
2096	SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2097		PADDR (msg_parity),
2098	/*
2099	**	let the host do the real work.
2100	*/
2101	SCR_INT,
2102		SIR_NEGO_WIDE,
2103	/*
2104	**	let the target fetch our answer.
2105	*/
2106	SCR_SET (SCR_ATN),
2107		0,
2108	SCR_CLR (SCR_ACK),
2109		0,
2110
2111	SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2112		SIR_NEGO_PROTO,
2113	/*
2114	**	Send the M_X_WIDE_REQ
2115	*/
2116	SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
2117		NADDR (msgout),
2118	SCR_CLR (SCR_ATN),
2119		0,
2120	SCR_COPY (1),
2121		RADDR (sfbr),
2122		NADDR (lastmsg),
2123	SCR_JUMP,
2124		PADDR (msg_out_done),
2125
2126}/*-------------------------< MSG_EXT_3 >----------------*/,{
2127	SCR_CLR (SCR_ACK),
2128		0,
2129	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2130		PADDR (dispatch),
2131	/*
2132	**	get extended message code.
2133	*/
2134	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2135		NADDR (msgin[2]),
2136	/*
2137	**	Check for message parity error.
2138	*/
2139	SCR_TO_REG (scratcha),
2140		0,
2141	SCR_FROM_REG (socl),
2142		0,
2143	SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2144		PADDR (msg_parity),
2145	SCR_FROM_REG (scratcha),
2146		0,
2147	SCR_JUMP ^ IFTRUE (DATA (M_X_SYNC_REQ)),
2148		PADDR (msg_sdtr),
2149	/*
2150	**	unknown extended message
2151	*/
2152	SCR_JUMP,
2153		PADDR (msg_bad)
2154
2155}/*-------------------------< MSG_SDTR >-----------------*/,{
2156	SCR_CLR (SCR_ACK),
2157		0,
2158	SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2159		PADDR (dispatch),
2160	/*
2161	**	get period and offset
2162	*/
2163	SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
2164		NADDR (msgin[3]),
2165	SCR_FROM_REG (socl),
2166		0,
2167	SCR_JUMP ^ IFTRUE (MASK (CATN, CATN)),
2168		PADDR (msg_parity),
2169	/*
2170	**	let the host do the real work.
2171	*/
2172	SCR_INT,
2173		SIR_NEGO_SYNC,
2174	/*
2175	**	let the target fetch our answer.
2176	*/
2177	SCR_SET (SCR_ATN),
2178		0,
2179	SCR_CLR (SCR_ACK),
2180		0,
2181
2182	SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2183		SIR_NEGO_PROTO,
2184	/*
2185	**	Send the M_X_SYNC_REQ
2186	*/
2187	SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
2188		NADDR (msgout),
2189	SCR_CLR (SCR_ATN),
2190		0,
2191	SCR_COPY (1),
2192		RADDR (sfbr),
2193		NADDR (lastmsg),
2194	SCR_JUMP,
2195		PADDR (msg_out_done),
2196
2197}/*-------------------------< COMPLETE >-----------------*/,{
2198	/*
2199	**	Complete message.
2200	**
2201	**	If it's not the get condition code,
2202	**	copy TEMP register to LASTP in header.
2203	*/
2204	SCR_FROM_REG (SS_REG),
2205		0,
2206/*<<<*/	SCR_JUMPR ^ IFTRUE (MASK (S_SENSE, S_SENSE)),
2207		12,
2208	SCR_COPY (4),
2209		RADDR (temp),
2210		NADDR (header.lastp),
2211/*>>>*/	/*
2212	**	When we terminate the cycle by clearing ACK,
2213	**	the target may disconnect immediately.
2214	**
2215	**	We don't want to be told of an
2216	**	"unexpected disconnect",
2217	**	so we disable this feature.
2218	*/
2219	SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2220		0,
2221	/*
2222	**	Terminate cycle ...
2223	*/
2224	SCR_CLR (SCR_ACK|SCR_ATN),
2225		0,
2226	/*
2227	**	... and wait for the disconnect.
2228	*/
2229	SCR_WAIT_DISC,
2230		0,
2231}/*-------------------------< CLEANUP >-------------------*/,{
2232	/*
2233	**      dsa:    Pointer to ccb
2234	**	      or xxxxxxFF (no ccb)
2235	**
2236	**      HS_REG:   Host-Status (<>0!)
2237	*/
2238	SCR_FROM_REG (dsa),
2239		0,
2240	SCR_JUMP ^ IFTRUE (DATA (0xff)),
2241		PADDR (signal),
2242	/*
2243	**      dsa is valid.
2244	**	save the status registers
2245	*/
2246	SCR_COPY (4),
2247		RADDR (scr0),
2248		NADDR (header.status),
2249	/*
2250	**	and copy back the header to the ccb.
2251	*/
2252	SCR_COPY (4),
2253		RADDR (dsa),
2254		PADDR (cleanup0),
2255	SCR_COPY (sizeof (struct head)),
2256		NADDR (header),
2257}/*-------------------------< CLEANUP0 >--------------------*/,{
2258		0,
2259
2260	/*
2261	**	If command resulted in "check condition"
2262	**	status and is not yet completed,
2263	**	try to get the condition code.
2264	*/
2265	SCR_FROM_REG (HS_REG),
2266		0,
2267/*<<<*/	SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
2268		16,
2269	SCR_FROM_REG (SS_REG),
2270		0,
2271	SCR_JUMP ^ IFTRUE (DATA (S_CHECK_COND)),
2272		PADDR(getcc2),
2273	/*
2274	**	And make the DSA register invalid.
2275	*/
2276/*>>>*/	SCR_LOAD_REG (dsa, 0xff), /* invalid */
2277		0,
2278}/*-------------------------< SIGNAL >----------------------*/,{
2279	/*
2280	**	if status = queue full,
2281	**	reinsert in startqueue and stall queue.
2282	*/
2283	SCR_FROM_REG (SS_REG),
2284		0,
2285	SCR_INT ^ IFTRUE (DATA (S_QUEUE_FULL)),
2286		SIR_STALL_QUEUE,
2287	/*
2288	**	if job completed ...
2289	*/
2290	SCR_FROM_REG (HS_REG),
2291		0,
2292	/*
2293	**	... signal completion to the host
2294	*/
2295	SCR_INT_FLY ^ IFFALSE (MASK (0, HS_DONEMASK)),
2296		0,
2297	/*
2298	**	Auf zu neuen Schandtaten!
2299	*/
2300	SCR_JUMP,
2301		PADDR(start),
2302
2303}/*-------------------------< SAVE_DP >------------------*/,{
2304	/*
2305	**	SAVE_DP message:
2306	**	Copy TEMP register to SAVEP in header.
2307	*/
2308	SCR_COPY (4),
2309		RADDR (temp),
2310		NADDR (header.savep),
2311	SCR_JUMP,
2312		PADDR (clrack),
2313}/*-------------------------< RESTORE_DP >---------------*/,{
2314	/*
2315	**	RESTORE_DP message:
2316	**	Copy SAVEP in header to TEMP register.
2317	*/
2318	SCR_COPY (4),
2319		NADDR (header.savep),
2320		RADDR (temp),
2321	SCR_JUMP,
2322		PADDR (clrack),
2323
2324}/*-------------------------< DISCONNECT >---------------*/,{
2325	/*
2326	**	If QUIRK_AUTOSAVE is set,
2327	**	do an "save pointer" operation.
2328	*/
2329	SCR_FROM_REG (QU_REG),
2330		0,
2331/*<<<*/	SCR_JUMPR ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
2332		12,
2333	/*
2334	**	like SAVE_DP message:
2335	**	Copy TEMP register to SAVEP in header.
2336	*/
2337	SCR_COPY (4),
2338		RADDR (temp),
2339		NADDR (header.savep),
2340/*>>>*/	/*
2341	**	Check if temp==savep or temp==goalp:
2342	**	if not, log a missing save pointer message.
2343	**	In fact, it's a comparison mod 256.
2344	**
2345	**	Hmmm, I hadn't thought that I would be urged to
2346	**	write this kind of ugly self modifying code.
2347	**
2348	**	It's unbelievable, but the ncr53c8xx isn't able
2349	**	to subtract one register from another.
2350	*/
2351	SCR_FROM_REG (temp),
2352		0,
2353	/*
2354	**	You are not expected to understand this ..
2355	**
2356	**	CAUTION: only little endian architectures supported! XXX
2357	*/
2358	SCR_COPY (1),
2359		NADDR (header.savep),
2360		PADDR (disconnect0),
2361}/*-------------------------< DISCONNECT0 >--------------*/,{
2362/*<<<*/	SCR_JUMPR ^ IFTRUE (DATA (1)),
2363		20,
2364	/*
2365	**	neither this
2366	*/
2367	SCR_COPY (1),
2368		NADDR (header.goalp),
2369		PADDR (disconnect1),
2370}/*-------------------------< DISCONNECT1 >--------------*/,{
2371	SCR_INT ^ IFFALSE (DATA (1)),
2372		SIR_MISSING_SAVE,
2373/*>>>*/
2374
2375	/*
2376	**	DISCONNECTing  ...
2377	**
2378	**	disable the "unexpected disconnect" feature,
2379	**	and remove the ACK signal.
2380	*/
2381	SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2382		0,
2383	SCR_CLR (SCR_ACK|SCR_ATN),
2384		0,
2385	/*
2386	**	Wait for the disconnect.
2387	*/
2388	SCR_WAIT_DISC,
2389		0,
2390	/*
2391	**	Profiling:
2392	**	Set a time stamp,
2393	**	and count the disconnects.
2394	*/
2395	SCR_COPY (sizeof (struct timeval)),
2396		(ncrcmd) &time,
2397		NADDR (header.stamp.disconnect),
2398	SCR_COPY (4),
2399		NADDR (disc_phys),
2400		RADDR (temp),
2401	SCR_REG_REG (temp, SCR_ADD, 0x01),
2402		0,
2403	SCR_COPY (4),
2404		RADDR (temp),
2405		NADDR (disc_phys),
2406	/*
2407	**	Status is: DISCONNECTED.
2408	*/
2409	SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
2410		0,
2411	SCR_JUMP,
2412		PADDR (cleanup),
2413
2414}/*-------------------------< MSG_OUT >-------------------*/,{
2415	/*
2416	**	The target requests a message.
2417	*/
2418	SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2419		NADDR (msgout),
2420	SCR_COPY (1),
2421		RADDR (sfbr),
2422		NADDR (lastmsg),
2423	/*
2424	**	If it was no ABORT message ...
2425	*/
2426	SCR_JUMP ^ IFTRUE (DATA (M_ABORT)),
2427		PADDR (msg_out_abort),
2428	/*
2429	**	... wait for the next phase
2430	**	if it's a message out, send it again, ...
2431	*/
2432	SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2433		PADDR (msg_out),
2434}/*-------------------------< MSG_OUT_DONE >--------------*/,{
2435	/*
2436	**	... else clear the message ...
2437	*/
2438	SCR_LOAD_REG (scratcha, M_NOOP),
2439		0,
2440	SCR_COPY (4),
2441		RADDR (scratcha),
2442		NADDR (msgout),
2443	/*
2444	**	... and process the next phase
2445	*/
2446	SCR_JUMP,
2447		PADDR (dispatch),
2448}/*-------------------------< MSG_OUT_ABORT >-------------*/,{
2449	/*
2450	**	After ABORT message,
2451	**
2452	**	expect an immediate disconnect, ...
2453	*/
2454	SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2455		0,
2456	SCR_CLR (SCR_ACK|SCR_ATN),
2457		0,
2458	SCR_WAIT_DISC,
2459		0,
2460	/*
2461	**	... and set the status to "ABORTED"
2462	*/
2463	SCR_LOAD_REG (HS_REG, HS_ABORTED),
2464		0,
2465	SCR_JUMP,
2466		PADDR (cleanup),
2467
2468}/*-------------------------< GETCC >-----------------------*/,{
2469	/*
2470	**	The ncr doesn't have an indirect load
2471	**	or store command. So we have to
2472	**	copy part of the control block to a
2473	**	fixed place, where we can modify it.
2474	**
2475	**	We patch the address part of a COPY command
2476	**	with the address of the dsa register ...
2477	*/
2478	SCR_COPY (4),
2479		RADDR (dsa),
2480		PADDR (getcc1),
2481	/*
2482	**	... then we do the actual copy.
2483	*/
2484	SCR_COPY (sizeof (struct head)),
2485}/*-------------------------< GETCC1 >----------------------*/,{
2486		0,
2487		NADDR (header),
2488	/*
2489	**	Initialize the status registers
2490	*/
2491	SCR_COPY (4),
2492		NADDR (header.status),
2493		RADDR (scr0),
2494}/*-------------------------< GETCC2 >----------------------*/,{
2495	/*
2496	**	Get the condition code from a target.
2497	**
2498	**	DSA points to a data structure.
2499	**	Set TEMP to the script location
2500	**	that receives the condition code.
2501	**
2502	**	Because there is no script command
2503	**	to load a longword into a register,
2504	**	we use a CALL command.
2505	*/
2506/*<<<*/	SCR_CALLR,
2507		24,
2508	/*
2509	**	Get the condition code.
2510	*/
2511	SCR_MOVE_TBL ^ SCR_DATA_IN,
2512		offsetof (struct dsb, sense),
2513	/*
2514	**	No data phase may follow!
2515	*/
2516	SCR_CALL,
2517		PADDR (checkatn),
2518	SCR_JUMP,
2519		PADDR (no_data),
2520/*>>>*/
2521
2522	/*
2523	**	The CALL jumps to this point.
2524	**	Prepare for a RESTORE_POINTER message.
2525	**	Save the TEMP register into the saved pointer.
2526	*/
2527	SCR_COPY (4),
2528		RADDR (temp),
2529		NADDR (header.savep),
2530	/*
2531	**	Load scratcha, because in case of a selection timeout,
2532	**	the host will expect a new value for startpos in
2533	**	the scratcha register.
2534	*/
2535	SCR_COPY (4),
2536		PADDR (startpos),
2537		RADDR (scratcha),
2538#ifdef NCR_GETCC_WITHMSG
2539	/*
2540	**	If QUIRK_NOMSG is set, select without ATN.
2541	**	and don't send a message.
2542	*/
2543	SCR_FROM_REG (QU_REG),
2544		0,
2545	SCR_JUMP ^ IFTRUE (MASK (QUIRK_NOMSG, QUIRK_NOMSG)),
2546		PADDR(getcc3),
2547	/*
2548	**	Then try to connect to the target.
2549	**	If we are reselected, special treatment
2550	**	of the current job is required before
2551	**	accepting the reselection.
2552	*/
2553	SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
2554		PADDR(badgetcc),
2555	/*
2556	**	save target id.
2557	*/
2558	SCR_FROM_REG (sdid),
2559		0,
2560	SCR_TO_REG (ctest0),
2561		0,
2562	/*
2563	**	Send the IDENTIFY message.
2564	**	In case of short transfer, remove ATN.
2565	*/
2566	SCR_MOVE_TBL ^ SCR_MSG_OUT,
2567		offsetof (struct dsb, smsg2),
2568	SCR_CLR (SCR_ATN),
2569		0,
2570	/*
2571	**	save the first byte of the message.
2572	*/
2573	SCR_COPY (1),
2574		RADDR (sfbr),
2575		NADDR (lastmsg),
2576	SCR_JUMP,
2577		PADDR (prepare2),
2578
2579#endif
2580}/*-------------------------< GETCC3 >----------------------*/,{
2581	/*
2582	**	Try to connect to the target.
2583	**	If we are reselected, special treatment
2584	**	of the current job is required before
2585	**	accepting the reselection.
2586	**
2587	**	Silly target won't accept a message.
2588	**	Select without ATN.
2589	*/
2590	SCR_SEL_TBL ^ offsetof (struct dsb, select),
2591		PADDR(badgetcc),
2592	/*
2593	**	save target id.
2594	*/
2595	SCR_FROM_REG (sdid),
2596		0,
2597	SCR_TO_REG (ctest0),
2598		0,
2599	/*
2600	**	Force error if selection timeout
2601	*/
2602	SCR_JUMPR ^ IFTRUE (WHEN (SCR_MSG_IN)),
2603		0,
2604	/*
2605	**	don't negotiate.
2606	*/
2607	SCR_JUMP,
2608		PADDR (prepare2),
2609
2610}/*------------------------< BADGETCC >---------------------*/,{
2611	/*
2612	**	If SIGP was set, clear it and try again.
2613	*/
2614	SCR_FROM_REG (ctest2),
2615		0,
2616	SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
2617		PADDR (getcc2),
2618	SCR_INT,
2619		SIR_SENSE_FAILED,
2620}/*-------------------------< RESELECT >--------------------*/,{
2621	/*
2622	**	make the DSA invalid.
2623	*/
2624	SCR_LOAD_REG (dsa, 0xff),
2625		0,
2626	SCR_CLR (SCR_TRG),
2627		0,
2628	/*
2629	**	Sleep waiting for a reselection.
2630	**	If SIGP is set, special treatment.
2631	**
2632	**	Zu allem bereit ..
2633	*/
2634	SCR_WAIT_RESEL,
2635		PADDR(reselect2),
2636	/*
2637	**	... zu nichts zu gebrauchen ?
2638	**
2639	**      load the target id into the SFBR
2640	**	and jump to the control block.
2641	**
2642	**	Look at the declarations of
2643	**	- struct ncb
2644	**	- struct tcb
2645	**	- struct lcb
2646	**	- struct ccb
2647	**	to understand what's going on.
2648	*/
2649	SCR_REG_SFBR (ssid, SCR_AND, 0x87),
2650		0,
2651	SCR_TO_REG (ctest0),
2652		0,
2653	SCR_JUMP,
2654		NADDR (jump_tcb),
2655}/*-------------------------< RESELECT2 >-------------------*/,{
2656	/*
2657	**	If it's not connected :(
2658	**	-> interrupted by SIGP bit.
2659	**	Jump to start.
2660	*/
2661	SCR_FROM_REG (ctest2),
2662		0,
2663	SCR_JUMP ^ IFTRUE (MASK (CSIGP,CSIGP)),
2664		PADDR (start),
2665	SCR_JUMP,
2666		PADDR (reselect),
2667
2668}/*-------------------------< RESEL_TMP >-------------------*/,{
2669	/*
2670	**	The return address in TEMP
2671	**	is in fact the data structure address,
2672	**	so copy it to the DSA register.
2673	*/
2674	SCR_COPY (4),
2675		RADDR (temp),
2676		RADDR (dsa),
2677	SCR_JUMP,
2678		PADDR (prepare),
2679
2680}/*-------------------------< RESEL_LUN >-------------------*/,{
2681	/*
2682	**	come back to this point
2683	**	to get an IDENTIFY message
2684	**	Wait for a msg_in phase.
2685	*/
2686/*<<<*/	SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2687		48,
2688	/*
2689	**	message phase
2690	**	It's not a sony, it's a trick:
2691	**	read the data without acknowledging it.
2692	*/
2693	SCR_FROM_REG (sbdl),
2694		0,
2695/*<<<*/	SCR_JUMPR ^ IFFALSE (MASK (M_IDENTIFY, 0x98)),
2696		32,
2697	/*
2698	**	It WAS an Identify message.
2699	**	get it and ack it!
2700	*/
2701	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2702		NADDR (msgin),
2703	SCR_CLR (SCR_ACK),
2704		0,
2705	/*
2706	**	Mask out the lun.
2707	*/
2708	SCR_REG_REG (sfbr, SCR_AND, 0x07),
2709		0,
2710	SCR_RETURN,
2711		0,
2712	/*
2713	**	No message phase or no IDENTIFY message:
2714	**	return 0.
2715	*/
2716/*>>>*/	SCR_LOAD_SFBR (0),
2717		0,
2718	SCR_RETURN,
2719		0,
2720
2721}/*-------------------------< RESEL_TAG >-------------------*/,{
2722	/*
2723	**	come back to this point
2724	**	to get a SIMPLE_TAG message
2725	**	Wait for a MSG_IN phase.
2726	*/
2727/*<<<*/	SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2728		64,
2729	/*
2730	**	message phase
2731	**	It's a trick - read the data
2732	**	without acknowledging it.
2733	*/
2734	SCR_FROM_REG (sbdl),
2735		0,
2736/*<<<*/	SCR_JUMPR ^ IFFALSE (DATA (M_SIMPLE_TAG)),
2737		48,
2738	/*
2739	**	It WAS a SIMPLE_TAG message.
2740	**	get it and ack it!
2741	*/
2742	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2743		NADDR (msgin),
2744	SCR_CLR (SCR_ACK),
2745		0,
2746	/*
2747	**	Wait for the second byte (the tag)
2748	*/
2749/*<<<*/	SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
2750		24,
2751	/*
2752	**	Get it and ack it!
2753	*/
2754	SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2755		NADDR (msgin),
2756	SCR_CLR (SCR_ACK|SCR_CARRY),
2757		0,
2758	SCR_RETURN,
2759		0,
2760	/*
2761	**	No message phase or no SIMPLE_TAG message
2762	**	or no second byte: return 0.
2763	*/
2764/*>>>*/	SCR_LOAD_SFBR (0),
2765		0,
2766	SCR_SET (SCR_CARRY),
2767		0,
2768	SCR_RETURN,
2769		0,
2770
2771}/*-------------------------< DATA_IN >--------------------*/,{
2772/*
2773**	Because the size depends on the
2774**	#define MAX_SCATTER parameter,
2775**	it is filled in at runtime.
2776**
2777**	SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
2778**		PADDR (no_data),
2779**	SCR_COPY (sizeof (struct timeval)),
2780**		(ncrcmd) &time,
2781**		NADDR (header.stamp.data),
2782**	SCR_MOVE_TBL ^ SCR_DATA_IN,
2783**		offsetof (struct dsb, data[ 0]),
2784**
2785**  ##===========< i=1; i<MAX_SCATTER >=========
2786**  ||	SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
2787**  ||		PADDR (checkatn),
2788**  ||	SCR_MOVE_TBL ^ SCR_DATA_IN,
2789**  ||		offsetof (struct dsb, data[ i]),
2790**  ##==========================================
2791**
2792**	SCR_CALL,
2793**		PADDR (checkatn),
2794**	SCR_JUMP,
2795**		PADDR (no_data),
2796*/
27970
2798}/*-------------------------< DATA_OUT >-------------------*/,{
2799/*
2800**	Because the size depends on the
2801**	#define MAX_SCATTER parameter,
2802**	it is filled in at runtime.
2803**
2804**	SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
2805**		PADDR (no_data),
2806**	SCR_COPY (sizeof (struct timeval)),
2807**		(ncrcmd) &time,
2808**		NADDR (header.stamp.data),
2809**	SCR_MOVE_TBL ^ SCR_DATA_OUT,
2810**		offsetof (struct dsb, data[ 0]),
2811**
2812**  ##===========< i=1; i<MAX_SCATTER >=========
2813**  ||	SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
2814**  ||		PADDR (dispatch),
2815**  ||	SCR_MOVE_TBL ^ SCR_DATA_OUT,
2816**  ||		offsetof (struct dsb, data[ i]),
2817**  ##==========================================
2818**
2819**	SCR_CALL,
2820**		PADDR (dispatch),
2821**	SCR_JUMP,
2822**		PADDR (no_data),
2823**
2824**---------------------------------------------------------
2825*/
2826(u_long)&ident
2827
2828}/*-------------------------< ABORTTAG >-------------------*/,{
2829	/*
2830	**      Abort a bad reselection.
2831	**	Set the message to ABORT vs. ABORT_TAG
2832	*/
2833	SCR_LOAD_REG (scratcha, M_ABORT_TAG),
2834		0,
2835	SCR_JUMPR ^ IFFALSE (CARRYSET),
2836		8,
2837}/*-------------------------< ABORT >----------------------*/,{
2838	SCR_LOAD_REG (scratcha, M_ABORT),
2839		0,
2840	SCR_COPY (1),
2841		RADDR (scratcha),
2842		NADDR (msgout),
2843	SCR_SET (SCR_ATN),
2844		0,
2845	SCR_CLR (SCR_ACK),
2846		0,
2847	/*
2848	**	and send it.
2849	**	we expect an immediate disconnect
2850	*/
2851	SCR_REG_REG (scntl2, SCR_AND, 0x7f),
2852		0,
2853	SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
2854		NADDR (msgout),
2855	SCR_COPY (1),
2856		RADDR (sfbr),
2857		NADDR (lastmsg),
2858	SCR_CLR (SCR_ACK|SCR_ATN),
2859		0,
2860	SCR_WAIT_DISC,
2861		0,
2862	SCR_JUMP,
2863		PADDR (start),
2864}/*-------------------------< SNOOPTEST >-------------------*/,{
2865	/*
2866	**	Read the variable.
2867	*/
2868	SCR_COPY (4),
2869		(ncrcmd) &ncr_cache,
2870		RADDR (scratcha),
2871	/*
2872	**	Write the variable.
2873	*/
2874	SCR_COPY (4),
2875		RADDR (temp),
2876		(ncrcmd) &ncr_cache,
2877	/*
2878	**	Read back the variable.
2879	*/
2880	SCR_COPY (4),
2881		(ncrcmd) &ncr_cache,
2882		RADDR (temp),
2883}/*-------------------------< SNOOPEND >-------------------*/,{
2884	/*
2885	**	And stop.
2886	*/
2887	SCR_INT,
2888		99,
2889}/*--------------------------------------------------------*/
2890};
2891
2892/*==========================================================
2893**
2894**
2895**	Fill in #define dependent parts of the script
2896**
2897**
2898**==========================================================
2899*/
2900
2901void ncr_script_fill (struct script * scr)
2902{
2903	int	i;
2904	ncrcmd	*p;
2905
2906	p = scr->tryloop;
2907	for (i=0; i<MAX_START; i++) {
2908		*p++ =SCR_COPY (4);
2909		*p++ =NADDR (squeue[i]);
2910		*p++ =RADDR (dsa);
2911		*p++ =SCR_CALL;
2912		*p++ =PADDR (trysel);
2913	};
2914	*p++ =SCR_JUMP;
2915	*p++ =PADDR(tryloop);
2916
2917	assert ((u_long)p == (u_long)&scr->tryloop + sizeof (scr->tryloop));
2918
2919	p = scr->data_in;
2920
2921	*p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN));
2922	*p++ =PADDR (no_data);
2923	*p++ =SCR_COPY (sizeof (struct timeval));
2924	*p++ =(ncrcmd) &time;
2925	*p++ =NADDR (header.stamp.data);
2926	*p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
2927	*p++ =offsetof (struct dsb, data[ 0]);
2928
2929	for (i=1; i<MAX_SCATTER; i++) {
2930		*p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
2931		*p++ =PADDR (checkatn);
2932		*p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
2933		*p++ =offsetof (struct dsb, data[i]);
2934	};
2935
2936	*p++ =SCR_CALL;
2937	*p++ =PADDR (checkatn);
2938	*p++ =SCR_JUMP;
2939	*p++ =PADDR (no_data);
2940
2941	assert ((u_long)p == (u_long)&scr->data_in + sizeof (scr->data_in));
2942
2943	p = scr->data_out;
2944
2945	*p++ =SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_OUT));
2946	*p++ =PADDR (no_data);
2947	*p++ =SCR_COPY (sizeof (struct timeval));
2948	*p++ =(ncrcmd) &time;
2949	*p++ =NADDR (header.stamp.data);
2950	*p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
2951	*p++ =offsetof (struct dsb, data[ 0]);
2952
2953	for (i=1; i<MAX_SCATTER; i++) {
2954		*p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
2955		*p++ =PADDR (dispatch);
2956		*p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
2957		*p++ =offsetof (struct dsb, data[i]);
2958	};
2959
2960	*p++ =SCR_CALL;
2961	*p++ =PADDR (dispatch);
2962	*p++ =SCR_JUMP;
2963	*p++ =PADDR (no_data);
2964
2965	assert ((u_long)p == (u_long)&scr->data_out + sizeof (scr->data_out));
2966}
2967
2968/*==========================================================
2969**
2970**
2971**	Copy and rebind a script.
2972**
2973**
2974**==========================================================
2975*/
2976
2977static void ncr_script_copy_and_bind (struct script *script, ncb_p np)
2978{
2979	ncrcmd  opcode, new, old;
2980	ncrcmd	*src, *dst, *start, *end;
2981	int relocs;
2982
2983#ifndef __NetBSD__
2984	np->script = (struct script*) vm_page_alloc_contig
2985	(round_page(sizeof (struct script)), 0x100000, 0xffffffff, PAGE_SIZE);
2986#else  /* !__NetBSD___ */
2987	np->script = (struct script *)
2988		malloc (sizeof (struct script), M_DEVBUF, M_WAITOK);
2989#endif /* __NetBSD__ */
2990
2991	np->p_script = vtophys(np->script);
2992
2993	src = script->start;
2994	dst = np->script->start;
2995
2996	start = src;
2997	end = src + (sizeof (struct script) / 4);
2998
2999	while (src < end) {
3000
3001		*dst++ = opcode = *src++;
3002
3003		/*
3004		**	If we forget to change the length
3005		**	in struct script, a field will be
3006		**	padded with 0. This is an illegal
3007		**	command.
3008		*/
3009
3010		if (opcode == 0) {
3011			printf ("%s: ERROR0 IN SCRIPT at %d.\n",
3012				ncr_name(np), src-start-1);
3013			DELAY (1000000);
3014		};
3015
3016		if (DEBUG_FLAGS & DEBUG_SCRIPT)
3017			printf ("%x:  <%x>\n",
3018				(unsigned)(src-1), (unsigned)opcode);
3019
3020		/*
3021		**	We don't have to decode ALL commands
3022		*/
3023		switch (opcode >> 28) {
3024
3025		case 0xc:
3026			/*
3027			**	COPY has TWO arguments.
3028			*/
3029			relocs = 2;
3030			if ((src[0] ^ src[1]) & 3) {
3031				printf ("%s: ERROR1 IN SCRIPT at %d.\n",
3032					ncr_name(np), src-start-1);
3033				DELAY (1000000);
3034			};
3035			break;
3036
3037		case 0x0:
3038			/*
3039			**	MOVE (absolute address)
3040			*/
3041			relocs = 1;
3042			break;
3043
3044		case 0x8:
3045			/*
3046			**	JUMP / CALL
3047			**	dont't relocate if relative :-)
3048			*/
3049			if (opcode & 0x00800000)
3050				relocs = 0;
3051			else
3052				relocs = 1;
3053			break;
3054
3055		case 0x4:
3056		case 0x5:
3057		case 0x6:
3058		case 0x7:
3059			relocs = 1;
3060			break;
3061
3062		default:
3063			relocs = 0;
3064			break;
3065		};
3066
3067		if (relocs) {
3068			while (relocs--) {
3069				old = *src++;
3070
3071				switch (old & RELOC_MASK) {
3072				case RELOC_REGISTER:
3073					new = (old & ~RELOC_MASK) + np->paddr;
3074					break;
3075				case RELOC_LABEL:
3076					new = (old & ~RELOC_MASK) + np->p_script;
3077					break;
3078				case RELOC_SOFTC:
3079					new = (old & ~RELOC_MASK) + vtophys(np);
3080					break;
3081				case 0:
3082					/* Don't relocate a 0 address. */
3083					if (old == 0) {
3084						new = old;
3085						break;
3086					}
3087					/* fall through */
3088				default:
3089					new = vtophys(old);
3090					break;
3091				}
3092
3093				*dst++ = new;
3094			}
3095		} else
3096			*dst++ = *src++;
3097
3098	};
3099}
3100
3101/*==========================================================
3102**
3103**
3104**      Auto configuration.
3105**
3106**
3107**==========================================================
3108*/
3109
3110/*----------------------------------------------------------
3111**
3112**	Reduce the transfer length to the max value
3113**	we can transfer safely.
3114**
3115**      Reading a block greater then MAX_SIZE from the
3116**	raw (character) device exercises a memory leak
3117**	in the vm subsystem. This is common to ALL devices.
3118**	We have submitted a description of this bug to
3119**	<FreeBSD-bugs@freefall.cdrom.com>.
3120**	It should be fixed in the current release.
3121**
3122**----------------------------------------------------------
3123*/
3124
3125void ncr_min_phys (struct  buf *bp)
3126{
3127	if ((unsigned long)bp->b_bcount > MAX_SIZE) bp->b_bcount = MAX_SIZE;
3128}
3129
3130/*----------------------------------------------------------
3131**
3132**	Maximal number of outstanding requests per target.
3133**
3134**----------------------------------------------------------
3135*/
3136
3137U_INT32 ncr_info (int unit)
3138{
3139	return (1);   /* may be changed later */
3140}
3141
3142/*----------------------------------------------------------
3143**
3144**	Probe the hostadapter.
3145**
3146**----------------------------------------------------------
3147*/
3148
3149#ifdef __NetBSD__
3150
3151int
3152ncr_probe(parent, match, aux)
3153	struct device *parent;
3154	void *match, *aux;
3155{
3156	struct cfdata *cf = match;
3157	struct pci_attach_args *pa = aux;
3158
3159#ifdef 0
3160	if (!pci_targmatch(cf, pa))
3161		return 0;
3162#endif
3163	if (pa->pa_id != NCR_810_ID &&
3164	    pa->pa_id != NCR_810AP_ID &&
3165	    pa->pa_id != NCR_815_ID &&
3166	    pa->pa_id != NCR_825_ID &&
3167	    pa->pa_id != NCR_860_ID &&
3168	    pa->pa_id != NCR_875_ID)
3169		return 0;
3170
3171	return 1;
3172}
3173
3174#else /* !__NetBSD__ */
3175
3176
3177static	char* ncr_probe (pcici_t tag, pcidi_t type)
3178{
3179	switch (type) {
3180
3181	case NCR_810_ID:
3182		return ("ncr 53c810 scsi");
3183
3184	case NCR_810AP_ID:
3185		return ("ncr 53c810ap scsi");
3186
3187	case NCR_815_ID:
3188		return ("ncr 53c815 scsi");
3189
3190	case NCR_825_ID:
3191		return ("ncr 53c825 wide scsi");
3192
3193	case NCR_860_ID:
3194		return ("ncr 53c860 scsi");
3195
3196	case NCR_875_ID:
3197		return ("ncr 53c875 wide scsi");
3198	}
3199	return (NULL);
3200}
3201
3202#endif /* !__NetBSD__ */
3203
3204
3205/*==========================================================
3206**
3207**
3208**      Auto configuration:  attach and init a host adapter.
3209**
3210**
3211**==========================================================
3212*/
3213
3214#define	MIN_ASYNC_PD	40
3215#define	MIN_SYNC_PD	20
3216
3217#ifdef __NetBSD__
3218
3219int
3220ncr_print()
3221{
3222}
3223
3224void
3225ncr_attach(parent, self, aux)
3226	struct device *parent, *self;
3227	void *aux;
3228{
3229	struct pci_attach_args *pa = aux;
3230	int retval;
3231	ncb_p np = (void *)self;
3232
3233	/*
3234	** XXX NetBSD
3235	** Perhaps try to figure what which model chip it is and print that
3236	** out.
3237	*/
3238	printf("\n");
3239
3240	/*
3241	**	Try to map the controller chip to
3242	**	virtual and physical memory.
3243	*/
3244
3245	retval = pci_map_mem(pa->pa_tag, 0x14, &np->vaddr, &np->paddr);
3246	if (retval)
3247		return;
3248
3249	np->sc_ih = pci_map_int(pa->pa_tag, PCI_IPL_BIO, ncr_intr, np);
3250	if (np->sc_ih == NULL)
3251		return;
3252
3253
3254#else /* !__NetBSD__ */
3255
3256static	void ncr_attach (pcici_t config_id, int unit)
3257{
3258	ncb_p np = (struct ncb*) 0;
3259#if ! (__FreeBSD__ >= 2)
3260	extern unsigned bio_imask;
3261#endif
3262
3263#if (__FreeBSD__ >= 2)
3264	struct scsibus_data *scbus;
3265#endif
3266
3267	/*
3268	**	allocate structure
3269	*/
3270
3271	if (!np) {
3272		np = (ncb_p) malloc (sizeof (struct ncb), M_DEVBUF, M_WAITOK);
3273		if (!np) return;
3274		ncrp[unit]=np;
3275	}
3276
3277	/*
3278	**	initialize structure.
3279	*/
3280
3281	bzero (np, sizeof (*np));
3282	np->unit = unit;
3283
3284	/*
3285	**	Try to map the controller chip to
3286	**	virtual and physical memory.
3287	*/
3288
3289	if (!pci_map_mem (config_id, 0x14, &np->vaddr, &np->paddr))
3290		return;
3291
3292#ifdef NCR_IOMAPPED
3293	/*
3294	**	Try to map the controller chip into iospace.
3295	*/
3296
3297	if (!pci_map_port (config_id, 0x10, &np->port))
3298		return;
3299#endif
3300
3301#endif /* !__NetBSD__ */
3302
3303	/*
3304	**	Do chip dependent initialization.
3305	*/
3306
3307#ifdef __NetBSD__
3308	switch (pa->pa_id) {
3309#else /* !__NetBSD__ */
3310	switch (pci_conf_read (config_id, PCI_ID_REG)) {
3311#endif /* __NetBSD__ */
3312	case NCR_825_ID:
3313	case NCR_875_ID:
3314		np->maxwide = 1;
3315		break;
3316	default:
3317		np->maxwide = 0;
3318		break;
3319	}
3320
3321	/*
3322	**	Patch script to physical addresses
3323	*/
3324
3325	ncr_script_fill (&script0);
3326	ncr_script_copy_and_bind (&script0, np);
3327	np->ccb.p_ccb		= vtophys (&np->ccb);
3328
3329	/*
3330	**	init data structure
3331	*/
3332
3333	np->jump_tcb.l_cmd	= SCR_JUMP;
3334	np->jump_tcb.l_paddr	= NCB_SCRIPT_PHYS (np, abort);
3335
3336	/*
3337	**	Make the controller's registers available.
3338	**	Now the INB INW INL OUTB OUTW OUTL macros
3339	**	can be used safely.
3340	*/
3341
3342	np->reg = (struct ncr_reg*) np->vaddr;
3343
3344	/*
3345	**  Get SCSI addr of host adapter (set by bios?).
3346	*/
3347
3348	np->myaddr = INB(nc_scid) & 0x07;
3349	if (!np->myaddr) np->myaddr = SCSI_NCR_MYADDR;
3350
3351	/*
3352	**	Get the value of the chip's clock.
3353	**	Find the right value for scntl3.
3354	*/
3355
3356	ncr_getclock (np);
3357
3358	/*
3359	**	Reset chip.
3360	*/
3361
3362	OUTB (nc_istat,  SRST);
3363	DELAY (1000);
3364	OUTB (nc_istat,  0   );
3365
3366#ifdef NCR_DUMP_REG
3367	/*
3368	**	Log the initial register contents
3369	*/
3370	{
3371		int reg;
3372#ifdef __NetBSD__
3373		u_long config_id = pa->pa_tag;
3374#endif /* __NetBSD__ */
3375		for (reg=0; reg<256; reg+=4) {
3376			if (reg%16==0) printf ("reg[%2x]", reg);
3377			printf (" %08x", (int)pci_conf_read (config_id, reg));
3378			if (reg%16==12) printf ("\n");
3379		}
3380	}
3381
3382	/*
3383	**	Reset chip, once again.
3384	*/
3385
3386	OUTB (nc_istat,  SRST);
3387	DELAY (1000);
3388	OUTB (nc_istat,  0   );
3389
3390#endif /* NCR_DUMP_REG */
3391
3392	/*
3393	**	Now check the cache handling of the pci chipset.
3394	*/
3395
3396	if (ncr_snooptest (np)) {
3397		printf ("CACHE INCORRECTLY CONFIGURED.\n");
3398		return;
3399	};
3400
3401#ifndef __NetBSD__
3402	/*
3403	**	Install the interrupt handler.
3404	*/
3405
3406	if (!pci_map_int (config_id, ncr_intr, np, &bio_imask))
3407		printf ("\tinterruptless mode: reduced performance.\n");
3408#endif /* __NetBSD__ */
3409
3410	/*
3411	**	After SCSI devices have been opened, we cannot
3412	**	reset the bus safely, so we do it here.
3413	**	Interrupt handler does the real work.
3414	*/
3415
3416	OUTB (nc_scntl1, CRST);
3417	DELAY (1000);
3418
3419	/*
3420	**	Process the reset exception,
3421	**	if interrupts are not enabled yet.
3422	**	Then enable disconnects.
3423	*/
3424	ncr_exception (np);
3425	np->disc = 1;
3426
3427	/*
3428	**	Now let the generic SCSI driver
3429	**	look for the SCSI devices on the bus ..
3430	*/
3431
3432#ifdef __NetBSD__
3433	np->sc_link.adapter_softc = np;
3434	np->sc_link.adapter_target = np->myaddr;
3435	np->sc_link.openings = 1;
3436#else /* !__NetBSD__ */
3437	np->sc_link.adapter_unit = unit;
3438	np->sc_link.adapter_targ = np->myaddr;
3439	np->sc_link.fordriver	 = 0;
3440#endif /* !__NetBSD__ */
3441	np->sc_link.adapter      = &ncr_switch;
3442	np->sc_link.device       = &ncr_dev;
3443	np->sc_link.flags	 = 0;
3444
3445#ifdef __NetBSD__
3446	config_found(self, &np->sc_link, ncr_print);
3447#else /* !__NetBSD__ */
3448#if (__FreeBSD__ >= 2)
3449	scbus = scsi_alloc_bus();
3450	if(!scbus)
3451		return;
3452	scbus->adapter_link = &np->sc_link;
3453
3454	if(np->maxwide)
3455		scbus->maxtarg = 15;
3456
3457	if (bootverbose) {
3458		unsigned t_from = 0;
3459		unsigned t_to   = scbus->maxtarg;
3460		unsigned myaddr = np->myaddr;
3461
3462		char *txt_and = "";
3463		printf ("%s scanning for targets ", ncr_name (np));
3464		if (t_from < myaddr) {
3465			printf ("%d..%d ", t_from, myaddr -1);
3466			txt_and = "and ";
3467		}
3468		if (myaddr < t_to)
3469			printf ("%s%d..%d ", txt_and, myaddr +1, t_to);
3470		printf ("(V%d " NCR_DATE ")\n", NCR_VERSION);
3471	}
3472
3473	scsi_attachdevs (scbus);
3474	scbus = NULL;   /* Upper-level SCSI code owns this now */
3475#else
3476	scsi_attachdevs (&np->sc_link);
3477#endif /* !__FreeBSD__ >= 2 */
3478#endif /* !__NetBSD__ */
3479
3480	/*
3481	**	start the timeout daemon
3482	*/
3483	ncr_timeout (np);
3484	np->lasttime=0;
3485
3486	/*
3487	**  Done.
3488	*/
3489
3490	return;
3491}
3492
3493/*==========================================================
3494**
3495**
3496**	Process pending device interrupts.
3497**
3498**
3499**==========================================================
3500*/
3501
3502#ifdef __NetBSD__
3503int
3504ncr_intr(arg)
3505        void *arg;
3506{
3507        ncb_p np = arg;
3508#else /* !__NetBSD__ */
3509int
3510ncr_intr(np)
3511	ncb_p np;
3512{
3513#endif /* __NetBSD__ */
3514	int n = 0;
3515	int oldspl = splbio();
3516
3517	if (DEBUG_FLAGS & DEBUG_TINY) printf ("[");
3518
3519	if (INB(nc_istat) & (INTF|SIP|DIP)) {
3520		/*
3521		**	Repeat until no outstanding ints
3522		*/
3523		do {
3524			ncr_exception (np);
3525		} while (INB(nc_istat) & (INTF|SIP|DIP));
3526
3527		n=1;
3528		np->ticks = 100;
3529	};
3530
3531	if (DEBUG_FLAGS & DEBUG_TINY) printf ("]\n");
3532
3533	splx (oldspl);
3534	return (n);
3535}
3536
3537/*==========================================================
3538**
3539**
3540**	Start execution of a SCSI command.
3541**	This is called from the generic SCSI driver.
3542**
3543**
3544**==========================================================
3545*/
3546
3547static INT32 ncr_start (struct scsi_xfer * xp)
3548{
3549#ifdef __NetBSD__
3550	ncb_p np  = xp->sc_link->adapter_softc;
3551#else /*__NetBSD__*/
3552	ncb_p np  = ncrp[xp->sc_link->adapter_unit];
3553#endif/*__NetBSD__*/
3554
3555	struct scsi_generic * cmd = xp->cmd;
3556	ccb_p cp;
3557	lcb_p lp;
3558	tcb_p tp = &np->target[xp->sc_link->target];
3559
3560	int	i, oldspl, segments, flags = xp->flags;
3561	u_char	ptr, nego, idmsg;
3562	u_long  msglen, msglen2;
3563
3564
3565
3566	/*---------------------------------------------
3567	**
3568	**   Reset SCSI bus
3569	**
3570	**	Interrupt handler does the real work.
3571	**
3572	**---------------------------------------------
3573	*/
3574
3575	if (flags & SCSI_RESET) {
3576		OUTB (nc_scntl1, CRST);
3577		DELAY (1000);
3578		return(COMPLETE);
3579	};
3580
3581	/*---------------------------------------------
3582	**
3583	**      Some shortcuts ...
3584	**
3585	**---------------------------------------------
3586	*/
3587
3588	if ((xp->sc_link->target == np->myaddr	  ) ||
3589		(xp->sc_link->target >= MAX_TARGET) ||
3590		(xp->sc_link->lun    >= MAX_LUN   ) ||
3591		(flags    & SCSI_DATA_UIO)) {
3592		xp->error = XS_DRIVER_STUFFUP;
3593		return(COMPLETE);
3594	};
3595
3596	/*---------------------------------------------
3597	**
3598	**      Diskaccess to partial blocks?
3599	**
3600	**---------------------------------------------
3601	*/
3602
3603	if ((xp->datalen & 0x1ff) && !(tp->inqdata[0] & 0x1f)) {
3604		switch (cmd->opcode) {
3605		case 0x28:  /* READ_BIG  (10) */
3606		case 0xa8:  /* READ_HUGE (12) */
3607		case 0x2a:  /* WRITE_BIG (10) */
3608		case 0xaa:  /* WRITE_HUGE(12) */
3609			PRINT_ADDR(xp);
3610			printf ("access to partial disk block refused.\n");
3611			xp->error = XS_DRIVER_STUFFUP;
3612			return(COMPLETE);
3613		};
3614	};
3615
3616	if (DEBUG_FLAGS & DEBUG_TINY) {
3617		PRINT_ADDR(xp);
3618		printf ("CMD=%x F=%x L=%x ", cmd->opcode,
3619			(unsigned)xp->flags, (unsigned) xp->datalen);
3620	}
3621
3622	/*--------------------------------------------
3623	**
3624	**   Sanity checks ...
3625	**	copied from Elischer's Adaptec driver.
3626	**
3627	**--------------------------------------------
3628	*/
3629
3630	flags = xp->flags;
3631	if (!(flags & INUSE)) {
3632		printf("%s: ?INUSE?\n", ncr_name (np));
3633		xp->flags |= INUSE;
3634	};
3635
3636	if(flags & ITSDONE) {
3637		printf("%s: ?ITSDONE?\n", ncr_name (np));
3638		xp->flags &= ~ITSDONE;
3639	};
3640
3641	if (xp->bp)
3642		flags |= (SCSI_NOSLEEP); /* just to be sure */
3643
3644	/*---------------------------------------------------
3645	**
3646	**	Assign a ccb / bind xp
3647	**
3648	**----------------------------------------------------
3649	*/
3650
3651	oldspl = splbio();
3652
3653	if (!(cp=ncr_get_ccb (np, flags, xp->sc_link->target, xp->sc_link->lun))) {
3654		printf ("%s: no ccb.\n", ncr_name (np));
3655		xp->error = XS_DRIVER_STUFFUP;
3656		splx(oldspl);
3657		return(TRY_AGAIN_LATER);
3658	};
3659	cp->xfer = xp;
3660
3661	/*---------------------------------------------------
3662	**
3663	**	timestamp
3664	**
3665	**----------------------------------------------------
3666	*/
3667
3668	bzero (&cp->phys.header.stamp, sizeof (struct tstamp));
3669	cp->phys.header.stamp.start = time;
3670
3671	/*----------------------------------------------------
3672	**
3673	**	Get device quirks from a speciality table.
3674	**
3675	**	@GENSCSI@
3676	**	This should be a part of the device table
3677	**	in "scsi_conf.c".
3678	**
3679	**----------------------------------------------------
3680	*/
3681
3682	if (tp->quirks & QUIRK_UPDATE) {
3683#ifdef NEW_SCSICONF
3684		tp->quirks = xp->sc_link->quirks;
3685#else
3686		tp->quirks = ncr_lookup ((char*) &tp->inqdata[0]);
3687#endif
3688#ifndef NCR_GETCC_WITHMSG
3689		if (tp->quirks) {
3690			PRINT_ADDR(xp);
3691			printf ("quirks=%x.\n", tp->quirks);
3692		};
3693#endif
3694	};
3695
3696	/*---------------------------------------------------
3697	**
3698	**	negotiation required?
3699	**
3700	**----------------------------------------------------
3701	*/
3702
3703	nego = 0;
3704
3705	if (tp->inqdata[7]) {
3706		/*
3707		**	negotiate synchronous transfers?
3708		*/
3709
3710		if (!tp->period) {
3711			if (SCSI_NCR_MAX_SYNC
3712#if defined (CDROM_ASYNC) || defined (GENERIC)
3713			    && ((tp->inqdata[0] & 0x1f) != 5)
3714#endif
3715			    && (tp->inqdata[7] & INQ7_SYNC)) {
3716				nego = NS_SYNC;
3717			} else {
3718				tp->period  =0xffff;
3719				tp->sval = 0xe0;
3720				PRINT_ADDR(xp);
3721				printf ("asynchronous.\n");
3722			};
3723		};
3724
3725		/*
3726		**	negotiate wide transfers ?
3727		*/
3728
3729		if (!tp->widedone) {
3730			if (tp->inqdata[7] & INQ7_WIDE16) {
3731				if (!nego) nego = NS_WIDE;
3732			} else
3733				tp->widedone=1;
3734		};
3735	};
3736
3737	/*---------------------------------------------------
3738	**
3739	**	choose a new tag ...
3740	**
3741	**----------------------------------------------------
3742	*/
3743
3744	if ((lp = tp->lp[xp->sc_link->lun]) && (lp->usetags)) {
3745		/*
3746		**	assign a tag to this ccb!
3747		*/
3748		while (!cp->tag) {
3749			ccb_p cp2 = lp->next_ccb;
3750			lp->lasttag = lp->lasttag % 255 + 1;
3751			while (cp2 && cp2->tag != lp->lasttag)
3752				cp2 = cp2->next_ccb;
3753			if (cp2) continue;
3754			cp->tag=lp->lasttag;
3755			if (DEBUG_FLAGS & DEBUG_TAGS) {
3756				PRINT_ADDR(xp);
3757				printf ("using tag #%d.\n", cp->tag);
3758			};
3759		};
3760	} else {
3761		cp->tag=0;
3762	};
3763
3764	/*----------------------------------------------------
3765	**
3766	**	Build the identify / tag / sdtr message
3767	**
3768	**----------------------------------------------------
3769	*/
3770
3771	idmsg = M_IDENTIFY | xp->sc_link->lun;
3772#ifndef NCR_NO_DISCONNECT
3773	/*---------------------------------------------------------------------
3774	** Some users have problems with this driver.
3775	** I assume that the current problems relate to a conflict between
3776	** a disconnect and an immediately following reconnect operation.
3777	** With this option one can prevent the driver from using disconnects.
3778	** Without disconnects the performance will be severely degraded.
3779	** But it may help to trace down the core problem.
3780	**---------------------------------------------------------------------
3781	*/
3782	if ((cp!=&np->ccb) && (np->disc))
3783		idmsg |= 0x40;
3784#endif
3785
3786	cp -> scsi_smsg [0] = idmsg;
3787	msglen=1;
3788
3789	if (cp->tag) {
3790
3791		/*
3792		**	Ordered write ops, unordered read ops.
3793		*/
3794		switch (cmd->opcode) {
3795		case 0x08:  /* READ_SMALL (6) */
3796		case 0x28:  /* READ_BIG  (10) */
3797		case 0xa8:  /* READ_HUGE (12) */
3798			cp -> scsi_smsg [msglen] = M_SIMPLE_TAG;
3799			break;
3800		default:
3801			cp -> scsi_smsg [msglen] = M_ORDERED_TAG;
3802		}
3803
3804		/*
3805		**	can be overwritten by ncrcontrol
3806		*/
3807		switch (np->order) {
3808		case M_SIMPLE_TAG:
3809		case M_ORDERED_TAG:
3810			cp -> scsi_smsg [msglen] = np->order;
3811		};
3812		msglen++;
3813		cp -> scsi_smsg [msglen++] = cp -> tag;
3814	}
3815
3816	switch (nego) {
3817	case NS_SYNC:
3818		cp -> scsi_smsg [msglen++] = M_EXTENDED;
3819		cp -> scsi_smsg [msglen++] = 3;
3820		cp -> scsi_smsg [msglen++] = M_X_SYNC_REQ;
3821		cp -> scsi_smsg [msglen++] = tp->minsync;
3822		cp -> scsi_smsg [msglen++] = tp->maxoffs;
3823		if (DEBUG_FLAGS & DEBUG_NEGO) {
3824			PRINT_ADDR(cp->xfer);
3825			printf ("sync msgout: ");
3826			ncr_show_msg (&cp->scsi_smsg [msglen-5]);
3827			printf (".\n");
3828		};
3829		break;
3830	case NS_WIDE:
3831		cp -> scsi_smsg [msglen++] = M_EXTENDED;
3832		cp -> scsi_smsg [msglen++] = 2;
3833		cp -> scsi_smsg [msglen++] = M_X_WIDE_REQ;
3834		cp -> scsi_smsg [msglen++] = tp->usrwide;
3835		if (DEBUG_FLAGS & DEBUG_NEGO) {
3836			PRINT_ADDR(cp->xfer);
3837			printf ("wide msgout: ");
3838			ncr_show_msg (&cp->scsi_smsg [msglen-4]);
3839			printf (".\n");
3840		};
3841		break;
3842	};
3843
3844	/*----------------------------------------------------
3845	**
3846	**	Build the identify message for getcc.
3847	**
3848	**----------------------------------------------------
3849	*/
3850
3851	cp -> scsi_smsg2 [0] = idmsg;
3852	msglen2 = 1;
3853
3854	/*----------------------------------------------------
3855	**
3856	**	Build the data descriptors
3857	**
3858	**----------------------------------------------------
3859	*/
3860
3861	segments = ncr_scatter (&cp->phys, (vm_offset_t) xp->data,
3862					(vm_size_t) xp->datalen);
3863
3864	if (segments < 0) {
3865		xp->error = XS_DRIVER_STUFFUP;
3866		ncr_free_ccb(np, cp, flags);
3867		splx(oldspl);
3868		return(COMPLETE);
3869	};
3870
3871	/*----------------------------------------------------
3872	**
3873	**	Set the SAVED_POINTER.
3874	**
3875	**----------------------------------------------------
3876	*/
3877
3878	if (flags & SCSI_DATA_IN) {
3879		cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_in);
3880		cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
3881	} else if (flags & SCSI_DATA_OUT) {
3882		cp->phys.header.savep = NCB_SCRIPT_PHYS (np, data_out);
3883		cp->phys.header.goalp = cp->phys.header.savep +20 +segments*16;
3884	} else {
3885		cp->phys.header.savep = NCB_SCRIPT_PHYS (np, no_data);
3886		cp->phys.header.goalp = cp->phys.header.savep;
3887	};
3888	cp->phys.header.lastp = cp->phys.header.savep;
3889
3890
3891	/*----------------------------------------------------
3892	**
3893	**	fill in ccb
3894	**
3895	**----------------------------------------------------
3896	**
3897	**
3898	**	physical -> virtual backlink
3899	**	Generic SCSI command
3900	*/
3901	cp->phys.header.cp		= cp;
3902	/*
3903	**	Startqueue
3904	*/
3905	cp->phys.header.launch.l_paddr	= NCB_SCRIPT_PHYS (np, select);
3906	cp->phys.header.launch.l_cmd	= SCR_JUMP;
3907	/*
3908	**	select
3909	*/
3910	cp->phys.select.sel_id		= xp->sc_link->target;
3911	cp->phys.select.sel_scntl3	= tp->wval;
3912	cp->phys.select.sel_sxfer	= tp->sval;
3913	/*
3914	**	message
3915	*/
3916/*	cp->phys.smsg.addr		= cp->p_scsi_smsg;*/
3917	cp->phys.smsg.addr		= CCB_PHYS (cp, scsi_smsg);
3918	cp->phys.smsg.size		= msglen;
3919
3920/*	cp->phys.smsg2.addr		= cp->p_scsi_smsg2;*/
3921	cp->phys.smsg2.addr		= CCB_PHYS (cp, scsi_smsg2);
3922	cp->phys.smsg2.size		= msglen2;
3923	/*
3924	**	command
3925	*/
3926	cp->phys.cmd.addr		= vtophys (cmd);
3927	cp->phys.cmd.size		= xp->cmdlen;
3928	/*
3929	**	sense command
3930	*/
3931/*	cp->phys.scmd.addr		= cp->p_sensecmd;*/
3932	cp->phys.scmd.addr		= CCB_PHYS (cp, sensecmd);
3933	cp->phys.scmd.size		= 6;
3934	/*
3935	**	patch requested size into sense command
3936	*/
3937	cp->sensecmd[0]			= 0x03;
3938	cp->sensecmd[1]			= xp->sc_link->lun << 5;
3939	cp->sensecmd[4]			= sizeof(struct scsi_sense_data);
3940	if (xp->req_sense_length)
3941		cp->sensecmd[4]		= xp->req_sense_length;
3942	/*
3943	**	sense data
3944	*/
3945	cp->phys.sense.addr		= vtophys (&cp->xfer->sense);
3946	cp->phys.sense.size		= sizeof(struct scsi_sense_data);
3947	/*
3948	**	status
3949	*/
3950	cp->actualquirks		= tp->quirks;
3951	cp->host_status			= nego ? HS_NEGOTIATE : HS_BUSY;
3952	cp->scsi_status			= S_ILLEGAL;
3953	cp->parity_status		= 0;
3954
3955	cp->xerr_status			= XE_OK;
3956	cp->sync_status			= tp->sval;
3957	cp->nego_status			= nego;
3958	cp->wide_status			= tp->wval;
3959
3960	/*----------------------------------------------------
3961	**
3962	**	Critical region: start this job.
3963	**
3964	**----------------------------------------------------
3965	*/
3966
3967	/*
3968	**	reselect pattern and activate this job.
3969	*/
3970
3971	cp->jump_ccb.l_cmd	= (SCR_JUMP ^ IFFALSE (DATA (cp->tag)));
3972	cp->tlimit		= time.tv_sec + xp->timeout / 1000 + 2;
3973	cp->magic		= CCB_MAGIC;
3974
3975	/*
3976	**	insert into start queue.
3977	*/
3978
3979	ptr = np->squeueput + 1;
3980	if (ptr >= MAX_START) ptr=0;
3981	np->squeue [ptr	  ] = NCB_SCRIPT_PHYS (np, idle);
3982	np->squeue [np->squeueput] = CCB_PHYS (cp, phys);
3983	np->squeueput = ptr;
3984
3985	if(DEBUG_FLAGS & DEBUG_QUEUE)
3986		printf ("%s: queuepos=%d tryoffset=%d.\n", ncr_name (np),
3987		np->squeueput,
3988		(unsigned)(np->script->startpos[0]-
3989			   (NCB_SCRIPT_PHYS (np, tryloop))));
3990
3991	/*
3992	**	Script processor may be waiting for reselect.
3993	**	Wake it up.
3994	*/
3995	OUTB (nc_istat, SIGP);
3996
3997	/*
3998	**	and reenable interrupts
3999	*/
4000	splx (oldspl);
4001
4002	/*
4003	**	If interrupts are enabled, return now.
4004	**	Command is successfully queued.
4005	*/
4006
4007#ifdef __NetBSD__
4008        if (!(flags & SCSI_POLL)) {
4009#else /* !__NetBSD__ */
4010	if (!(flags & SCSI_NOMASK)) {
4011#endif /* __NetBSD__ */
4012		if (np->lasttime) {
4013			if(DEBUG_FLAGS & DEBUG_TINY) printf ("Q");
4014			return(SUCCESSFULLY_QUEUED);
4015		};
4016	};
4017
4018	/*----------------------------------------------------
4019	**
4020	**	Interrupts not yet enabled - have to poll.
4021	**
4022	**----------------------------------------------------
4023	*/
4024
4025	if (DEBUG_FLAGS & DEBUG_POLL) printf("P");
4026
4027	for (i=xp->timeout; i && !(xp->flags & ITSDONE);i--) {
4028		if ((DEBUG_FLAGS & DEBUG_POLL) && (cp->host_status))
4029			printf ("%c", (cp->host_status & 0xf) + '0');
4030		DELAY (1000);
4031		ncr_exception (np);
4032	};
4033
4034	/*
4035	**	Abort if command not done.
4036	*/
4037	if (!(xp->flags & ITSDONE)) {
4038		printf ("%s: aborting job ...\n", ncr_name (np));
4039		OUTB (nc_istat, CABRT);
4040		DELAY (100000);
4041		OUTB (nc_istat, SIGP);
4042		ncr_exception (np);
4043	};
4044
4045	if (!(xp->flags & ITSDONE)) {
4046		printf ("%s: abortion failed at %x.\n",
4047			ncr_name (np), (unsigned) INL(nc_dsp));
4048		ncr_init (np, "timeout", HS_TIMEOUT);
4049	};
4050
4051	if (!(xp->flags & ITSDONE)) {
4052		cp-> host_status = HS_SEL_TIMEOUT;
4053		ncr_complete (np, cp);
4054	};
4055
4056	if (DEBUG_FLAGS & DEBUG_RESULT) {
4057		printf ("%s: result: %x %x.\n",
4058			ncr_name (np), cp->host_status, cp->scsi_status);
4059	};
4060#ifdef __NetBSD__
4061        if (!(flags & SCSI_POLL))
4062#else /* !__NetBSD__ */
4063	if (!(flags & SCSI_NOMASK))
4064#endif /* __NetBSD__ */
4065		return (SUCCESSFULLY_QUEUED);
4066	switch (xp->error) {
4067	case  0     : return (COMPLETE);
4068	case XS_BUSY: return (TRY_AGAIN_LATER);
4069	};
4070	return (COMPLETE);
4071}
4072
4073/*==========================================================
4074**
4075**
4076**	Complete execution of a SCSI command.
4077**	Signal completion to the generic SCSI driver.
4078**
4079**
4080**==========================================================
4081*/
4082
4083void ncr_complete (ncb_p np, ccb_p cp)
4084{
4085	struct scsi_xfer * xp;
4086	tcb_p tp;
4087	lcb_p lp;
4088
4089	/*
4090	**	Sanity check
4091	*/
4092
4093	if (!cp || (cp->magic!=CCB_MAGIC) || !cp->xfer) return;
4094	cp->magic = 1;
4095	cp->tlimit= 0;
4096
4097	/*
4098	**	No Reselect anymore.
4099	*/
4100	cp->jump_ccb.l_cmd = (SCR_JUMP);
4101
4102	/*
4103	**	No starting.
4104	*/
4105	cp->phys.header.launch.l_paddr= NCB_SCRIPT_PHYS (np, idle);
4106
4107	/*
4108	**	timestamp
4109	*/
4110	ncb_profile (np, cp);
4111
4112	if (DEBUG_FLAGS & DEBUG_TINY)
4113		printf ("CCB=%x STAT=%x/%x\n", (unsigned)cp & 0xfff,
4114			cp->host_status,cp->scsi_status);
4115
4116	xp = cp->xfer;
4117	cp->xfer = NULL;
4118	tp = &np->target[xp->sc_link->target];
4119	lp = tp->lp[xp->sc_link->lun];
4120
4121	/*
4122	**	Check for parity errors.
4123	*/
4124
4125	if (cp->parity_status) {
4126		PRINT_ADDR(xp);
4127		printf ("%d parity error(s), fallback.\n", cp->parity_status);
4128		/*
4129		**	fallback to asynch transfer.
4130		*/
4131		tp->usrsync=255;
4132		tp->period =  0;
4133	};
4134
4135	/*
4136	**	Check for extended errors.
4137	*/
4138
4139	if (cp->xerr_status != XE_OK) {
4140		PRINT_ADDR(xp);
4141		switch (cp->xerr_status) {
4142		case XE_EXTRA_DATA:
4143			printf ("extraneous data discarded.\n");
4144			break;
4145		case XE_BAD_PHASE:
4146			printf ("illegal scsi phase (4/5).\n");
4147			break;
4148		default:
4149			printf ("extended error %d.\n", cp->xerr_status);
4150			break;
4151		};
4152		if (cp->host_status==HS_COMPLETE)
4153			cp->host_status = HS_FAIL;
4154	};
4155
4156	/*
4157	**	Check the status.
4158	*/
4159#ifdef __NetBSD__
4160	if (xp->error != XS_NOERROR) {
4161
4162                /*
4163                **      Don't override the error value.
4164                */
4165	} else
4166#endif /* __NetBSD__ */
4167	if (   (cp->host_status == HS_COMPLETE)
4168		&& (cp->scsi_status == S_GOOD)) {
4169
4170		/*
4171		**	All went well.
4172		*/
4173
4174		xp->resid = 0;
4175
4176		/*
4177		** if (cp->phys.header.lastp != cp->phys.header.goalp)...
4178		**
4179		**	@RESID@
4180		**	Could dig out the correct value for resid,
4181		**	but it would be quite complicated.
4182		**
4183		**	The ah1542.c driver sets it to 0 too ...
4184		*/
4185
4186		/*
4187		**	Try to assign a ccb to this nexus
4188		*/
4189		ncr_alloc_ccb (np, xp);
4190
4191		/*
4192		**	On inquire cmd (0x12) save some data.
4193		*/
4194		if (xp->cmd->opcode == 0x12) {
4195			bcopy (	xp->data,
4196				&tp->inqdata,
4197				sizeof (tp->inqdata));
4198
4199			/*
4200			**	set number of tags
4201			*/
4202			ncr_setmaxtags (tp, tp->usrtags);
4203
4204			/*
4205			**	prepare negotiation of synch and wide.
4206			*/
4207			ncr_negotiate (np, tp);
4208
4209			/*
4210			**	force quirks update before next command start
4211			*/
4212			tp->quirks |= QUIRK_UPDATE;
4213		};
4214
4215		/*
4216		**	Announce changes to the generic driver
4217		*/
4218		if (lp) {
4219			ncr_settags (tp, lp);
4220			if (lp->reqlink != lp->actlink)
4221				ncr_opennings (np, lp, xp);
4222		};
4223
4224		tp->bytes     += xp->datalen;
4225		tp->transfers ++;
4226#ifndef __NetBSD__
4227	} else if (xp->flags & SCSI_ERR_OK) {
4228
4229		/*
4230		**   Not correct, but errors expected.
4231		*/
4232		xp->resid = 0;
4233#endif /* !__NetBSD__ */
4234	} else if ((cp->host_status == HS_COMPLETE)
4235		&& (cp->scsi_status == (S_SENSE|S_GOOD))) {
4236
4237		/*
4238		**   Check condition code
4239		*/
4240		xp->error = XS_SENSE;
4241
4242		if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
4243			u_char * p = (u_char*) & xp->sense;
4244			int i;
4245			printf ("\n%s: sense data:", ncr_name (np));
4246			for (i=0; i<14; i++) printf (" %x", *p++);
4247			printf (".\n");
4248		};
4249
4250	} else if ((cp->host_status == HS_COMPLETE)
4251		&& (cp->scsi_status == S_BUSY)) {
4252
4253		/*
4254		**   Target is busy.
4255		*/
4256		xp->error = XS_BUSY;
4257
4258	} else if ((cp->host_status == HS_SEL_TIMEOUT)
4259		|| (cp->host_status == HS_TIMEOUT)) {
4260
4261		/*
4262		**   No response
4263		*/
4264		xp->error = XS_TIMEOUT;
4265
4266	} else {
4267
4268		/*
4269		**  Other protocol messes
4270		*/
4271		PRINT_ADDR(xp);
4272		printf ("COMMAND FAILED (%x %x) @%x.\n",
4273			cp->host_status, cp->scsi_status, (unsigned)cp);
4274
4275		xp->error = XS_TIMEOUT;
4276	}
4277
4278	xp->flags |= ITSDONE;
4279
4280	/*
4281	**	trace output
4282	*/
4283
4284	if (tp->usrflag & UF_TRACE) {
4285		u_char * p;
4286		int i;
4287		PRINT_ADDR(xp);
4288		printf (" CMD:");
4289		p = (u_char*) &xp->cmd->opcode;
4290		for (i=0; i<xp->cmdlen; i++) printf (" %x", *p++);
4291
4292		if (cp->host_status==HS_COMPLETE) {
4293			switch (cp->scsi_status) {
4294			case S_GOOD:
4295				printf ("  GOOD");
4296				break;
4297			case S_CHECK_COND:
4298				printf ("  SENSE:");
4299				p = (u_char*) &xp->sense;
4300				for (i=0; i<xp->req_sense_length; i++)
4301					printf (" %x", *p++);
4302				break;
4303			default:
4304				printf ("  STAT: %x\n", cp->scsi_status);
4305				break;
4306			};
4307		} else printf ("  HOSTERROR: %x", cp->host_status);
4308		printf ("\n");
4309	};
4310
4311	/*
4312	**	Free this ccb
4313	*/
4314	ncr_free_ccb (np, cp, xp->flags);
4315
4316	/*
4317	**	signal completion to generic driver.
4318	*/
4319	scsi_done (xp);
4320}
4321
4322/*==========================================================
4323**
4324**
4325**	Signal all (or one) control block done.
4326**
4327**
4328**==========================================================
4329*/
4330
4331void ncr_wakeup (ncb_p np, u_long code)
4332{
4333	/*
4334	**	Starting at the default ccb and following
4335	**	the links, complete all jobs with a
4336	**	host_status greater than "disconnect".
4337	**
4338	**	If the "code" parameter is not zero,
4339	**	complete all jobs that are not IDLE.
4340	*/
4341
4342	ccb_p cp = &np->ccb;
4343	while (cp) {
4344		switch (cp->host_status) {
4345
4346		case HS_IDLE:
4347			break;
4348
4349		case HS_DISCONNECT:
4350			if(DEBUG_FLAGS & DEBUG_TINY) printf ("D");
4351			/* fall through */
4352
4353		case HS_BUSY:
4354		case HS_NEGOTIATE:
4355			if (!code) break;
4356			cp->host_status = code;
4357
4358			/* fall through */
4359
4360		default:
4361			ncr_complete (np, cp);
4362			break;
4363		};
4364		cp = cp -> link_ccb;
4365	};
4366}
4367
4368/*==========================================================
4369**
4370**
4371**	Start NCR chip.
4372**
4373**
4374**==========================================================
4375*/
4376
4377void ncr_init (ncb_p np, char * msg, u_long code)
4378{
4379	int	i;
4380	u_long	usrsync;
4381	u_char	usrwide;
4382	u_char	burstlen;
4383
4384	/*
4385	**	Reset chip.
4386	*/
4387
4388	OUTB (nc_istat,  SRST);
4389	DELAY (1000);
4390
4391	/*
4392	**	Message.
4393	*/
4394
4395	if (msg) printf ("%s: restart (%s).\n", ncr_name (np), msg);
4396
4397	/*
4398	**	Clear Start Queue
4399	*/
4400
4401	for (i=0;i<MAX_START;i++)
4402		np -> squeue [i] = NCB_SCRIPT_PHYS (np, idle);
4403
4404	/*
4405	**	Start at first entry.
4406	*/
4407
4408	np->squeueput = 0;
4409	np->script->startpos[0] = NCB_SCRIPT_PHYS (np, tryloop);
4410	np->script->start0  [0] = SCR_INT ^ IFFALSE (0);
4411
4412	/*
4413	**	Wakeup all pending jobs.
4414	*/
4415
4416	ncr_wakeup (np, code);
4417
4418	/*
4419	**	Init chip.
4420	*/
4421
4422#ifndef __NetBSD__
4423	if (pci_max_burst_len < 4) {
4424		static u_char tbl[4]={0,0,0x40,0x80};
4425		burstlen = tbl[pci_max_burst_len];
4426	} else burstlen = 0xc0;
4427#else /* !__NetBSD__ */
4428	burstlen = 0xc0;
4429#endif /* __NetBSD__ */
4430
4431	OUTB (nc_istat,  0      );      /*  Remove Reset, abort ...	     */
4432	OUTB (nc_scntl0, 0xca   );      /*  full arb., ena parity, par->ATN  */
4433	OUTB (nc_scntl1, 0x00	);	/*  odd parity, and remove CRST!!    */
4434	OUTB (nc_scntl3, np->rv_scntl3);/*  timing prescaler		     */
4435	OUTB (nc_scid  , RRE|np->myaddr);/*  host adapter SCSI address       */
4436	OUTW (nc_respid, 1ul<<np->myaddr);/*  id to respond to		     */
4437	OUTB (nc_istat , SIGP	);	/*  Signal Process		     */
4438	OUTB (nc_dmode , burstlen);	/*  Burst length = 2 .. 16 transfers */
4439	OUTB (nc_dcntl , NOCOM  );      /*  no single step mode, protect SFBR*/
4440	OUTB (nc_ctest4, 0x08	);	/*  enable master parity checking    */
4441	OUTB (nc_stest2, EXT    );	/*  Extended Sreq/Sack filtering     */
4442	OUTB (nc_stest3, TE     );	/*  TolerANT enable		     */
4443	OUTB (nc_stime0, 0xfb	);	/*  HTH = 1.6sec  STO = 0.1 sec.     */
4444
4445	/*
4446	**	Reinitialize usrsync.
4447	**	Have to renegotiate synch mode.
4448	*/
4449
4450	usrsync = 255;
4451	if (SCSI_NCR_MAX_SYNC) {
4452		u_long period;
4453		period =1000000/SCSI_NCR_MAX_SYNC; /* ns = 10e6 / kHz */
4454		if (period <= 11 * np->ns_sync) {
4455			if (period < 4 * np->ns_sync)
4456				usrsync = np->ns_sync;
4457			else
4458				usrsync = period / 4;
4459		};
4460	};
4461
4462	/*
4463	**	Reinitialize usrwide.
4464	**	Have to renegotiate wide mode.
4465	*/
4466
4467	usrwide = (SCSI_NCR_MAX_WIDE);
4468	if (usrwide > np->maxwide) usrwide=np->maxwide;
4469
4470	/*
4471	**	Disable disconnects.
4472	*/
4473
4474	np->disc = 0;
4475
4476	/*
4477	**	Fill in target structure.
4478	*/
4479
4480	for (i=0;i<MAX_TARGET;i++) {
4481		tcb_p tp = &np->target[i];
4482
4483		tp->sval    = 0;
4484		tp->wval    = np->rv_scntl3;
4485
4486		tp->usrsync = usrsync;
4487		tp->usrwide = usrwide;
4488
4489		ncr_negotiate (np, tp);
4490	}
4491
4492	/*
4493	**      enable ints
4494	*/
4495
4496	OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST);
4497	OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
4498
4499	/*
4500	**    Start script processor.
4501	*/
4502
4503	OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
4504}
4505
4506/*==========================================================
4507**
4508**	Prepare the negotiation values for wide and
4509**	synchronous transfers.
4510**
4511**==========================================================
4512*/
4513
4514static void ncr_negotiate (struct ncb* np, struct tcb* tp)
4515{
4516	/*
4517	**	minsync unit is 4ns !
4518	*/
4519
4520	u_long minsync = tp->usrsync;
4521
4522	if (minsync < 25) minsync=25;
4523
4524	/*
4525	**	if not scsi 2
4526	**	don't believe FAST!
4527	*/
4528
4529	if ((minsync < 50) && (tp->inqdata[2] & 0x0f) < 2)
4530		minsync=50;
4531
4532	/*
4533	**	our limit ..
4534	*/
4535
4536	if (minsync < np->ns_sync)
4537		minsync = np->ns_sync;
4538
4539	/*
4540	**	divider limit
4541	*/
4542
4543	if (minsync > (np->ns_sync * 11) / 4)
4544		minsync = 255;
4545
4546	tp->minsync = minsync;
4547	tp->maxoffs = (minsync<255 ? 8 : 0);
4548
4549	/*
4550	**	period=0: has to negotiate sync transfer
4551	*/
4552
4553	tp->period=0;
4554
4555	/*
4556	**	widedone=0: has to negotiate wide transfer
4557	*/
4558	tp->widedone=0;
4559}
4560
4561/*==========================================================
4562**
4563**	Switch sync mode for current job and it's target
4564**
4565**==========================================================
4566*/
4567
4568static void ncr_setsync (ncb_p np, ccb_p cp, u_char sxfer)
4569{
4570	struct scsi_xfer *xp;
4571	tcb_p tp;
4572	u_char target = INB (nc_ctest0)&7;
4573
4574	assert (cp);
4575	if (!cp) return;
4576
4577	xp = cp->xfer;
4578	assert (xp);
4579	if (!xp) return;
4580	assert (target == xp->sc_link->target & 7);
4581
4582	tp = &np->target[target];
4583	tp->period= sxfer&0xf ? ((sxfer>>5)+4) * np->ns_sync : 0xffff;
4584
4585	if (tp->sval == sxfer) return;
4586	tp->sval = sxfer;
4587
4588	/*
4589	**	Bells and whistles   ;-)
4590	*/
4591	PRINT_ADDR(xp);
4592	if (sxfer & 0x0f) {
4593		/*
4594		**  Disable extended Sreq/Sack filtering
4595		*/
4596		if (tp->period <= 200) OUTB (nc_stest2, 0);
4597		printf ("%s%dns (%d Mb/sec) offset %d.\n",
4598			tp->period<200 ? "FAST SCSI-2 ":"",
4599			tp->period, (1000+tp->period/2)/tp->period,
4600			sxfer & 0x0f);
4601	} else  printf ("asynchronous.\n");
4602
4603	/*
4604	**	set actual value and sync_status
4605	*/
4606	OUTB (nc_sxfer, sxfer);
4607	np->sync_st = sxfer;
4608
4609	/*
4610	**	patch ALL ccbs of this target.
4611	*/
4612	for (cp = &np->ccb; cp; cp = cp->link_ccb) {
4613		if (!cp->xfer) continue;
4614		if (cp->xfer->sc_link->target != target) continue;
4615		cp->sync_status = sxfer;
4616	};
4617}
4618
4619/*==========================================================
4620**
4621**	Switch wide mode for current job and it's target
4622**
4623**==========================================================
4624*/
4625
4626static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide)
4627{
4628	struct scsi_xfer *xp;
4629	u_short target = INB (nc_ctest0)&7;
4630	tcb_p tp;
4631	u_char	scntl3 = np->rv_scntl3 | (wide ? EWS : 0);
4632
4633	assert (cp);
4634	if (!cp) return;
4635
4636	xp = cp->xfer;
4637	assert (xp);
4638	if (!xp) return;
4639	assert (target == xp->sc_link->target & 7);
4640
4641	tp = &np->target[target];
4642	tp->widedone  =  wide+1;
4643	if (tp->wval == scntl3) return;
4644	tp->wval = scntl3;
4645
4646	/*
4647	**	Bells and whistles   ;-)
4648	*/
4649	PRINT_ADDR(xp);
4650	if (scntl3 & EWS)
4651		printf ("WIDE SCSI (16 bit) enabled.\n");
4652	else
4653		printf ("WIDE SCSI disabled.\n");
4654
4655	/*
4656	**	set actual value and sync_status
4657	*/
4658	OUTB (nc_scntl3, scntl3);
4659	np->wide_st = scntl3;
4660
4661	/*
4662	**	patch ALL ccbs of this target.
4663	*/
4664	for (cp = &np->ccb; cp; cp = cp->link_ccb) {
4665		if (!cp->xfer) continue;
4666		if (cp->xfer->sc_link->target != target) continue;
4667		cp->wide_status = scntl3;
4668	};
4669}
4670
4671/*==========================================================
4672**
4673**	Switch tagged mode for a target.
4674**
4675**==========================================================
4676*/
4677
4678static void ncr_setmaxtags (tcb_p tp, u_long usrtags)
4679{
4680	int l;
4681	tp->usrtags = usrtags;
4682	for (l=0; l<MAX_LUN; l++) {
4683		lcb_p lp;
4684		if (!tp) break;
4685		lp=tp->lp[l];
4686		if (!lp) continue;
4687		ncr_settags (tp, lp);
4688	};
4689}
4690
4691static void ncr_settags (tcb_p tp, lcb_p lp)
4692{
4693	u_char reqtags, tmp;
4694
4695	if ((!tp) || (!lp)) return;
4696
4697	/*
4698	**	only devices capable of tagges commands
4699	**	only disk devices
4700	**	only if enabled by user ..
4701	*/
4702	if ((  tp->inqdata[7] & INQ7_QUEUE) && ((tp->inqdata[0] & 0x1f)==0x00)
4703		&& tp->usrtags) {
4704		reqtags = tp->usrtags;
4705		if (lp->actlink <= 1)
4706			lp->usetags=reqtags;
4707	} else {
4708		reqtags = 1;
4709		if (lp->actlink <= 1)
4710			lp->usetags=0;
4711	};
4712
4713	/*
4714	**	don't announce more than available.
4715	*/
4716	tmp = lp->actccbs;
4717	if (tmp > reqtags) tmp = reqtags;
4718	lp->reqlink = tmp;
4719
4720	/*
4721	**	don't discard if announced.
4722	*/
4723	tmp = lp->actlink;
4724	if (tmp < reqtags) tmp = reqtags;
4725	lp->reqccbs = tmp;
4726}
4727
4728/*----------------------------------------------------
4729**
4730**	handle user commands
4731**
4732**----------------------------------------------------
4733*/
4734
4735static void ncr_usercmd (ncb_p np)
4736{
4737	u_char t;
4738	tcb_p tp;
4739
4740	switch (np->user.cmd) {
4741
4742	case 0: return;
4743
4744	case UC_SETSYNC:
4745		for (t=0; t<MAX_TARGET; t++) {
4746			if (!((np->user.target>>t)&1)) continue;
4747			tp = &np->target[t];
4748			tp->usrsync = np->user.data;
4749			ncr_negotiate (np, tp);
4750		};
4751		break;
4752
4753	case UC_SETTAGS:
4754		if (np->user.data > MAX_TAGS)
4755			break;
4756		for (t=0; t<MAX_TARGET; t++) {
4757			if (!((np->user.target>>t)&1)) continue;
4758			ncr_setmaxtags (&np->target[t], np->user.data);
4759		};
4760		break;
4761
4762	case UC_SETDEBUG:
4763		ncr_debug = np->user.data;
4764		break;
4765
4766	case UC_SETORDER:
4767		np->order = np->user.data;
4768		break;
4769
4770	case UC_SETWIDE:
4771		for (t=0; t<MAX_TARGET; t++) {
4772			u_long size;
4773			if (!((np->user.target>>t)&1)) continue;
4774			tp = &np->target[t];
4775			size = np->user.data;
4776			if (size > np->maxwide) size=np->maxwide;
4777			tp->usrwide = size;
4778			ncr_negotiate (np, tp);
4779		};
4780		break;
4781
4782	case UC_SETFLAG:
4783		for (t=0; t<MAX_TARGET; t++) {
4784			if (!((np->user.target>>t)&1)) continue;
4785			tp = &np->target[t];
4786			tp->usrflag = np->user.data;
4787		};
4788		break;
4789	}
4790	np->user.cmd=0;
4791}
4792
4793
4794
4795
4796/*==========================================================
4797**
4798**
4799**	ncr timeout handler.
4800**
4801**
4802**==========================================================
4803**
4804**	Misused to keep the driver running when
4805**	interrupts are not configured correctly.
4806**
4807**----------------------------------------------------------
4808*/
4809
4810static void ncr_timeout (ncb_p np)
4811{
4812	u_long	thistime = time.tv_sec;
4813	u_long	step  = np->ticks;
4814	u_long	count = 0;
4815	long signed   t;
4816	ccb_p cp;
4817
4818	if (np->lasttime != thistime) {
4819		/*
4820		**	block ncr interrupts
4821		*/
4822		int oldspl = splbio();
4823		np->lasttime = thistime;
4824
4825		ncr_usercmd (np);
4826
4827		/*----------------------------------------------------
4828		**
4829		**	handle ncr chip timeouts
4830		**
4831		**	Assumption:
4832		**	We have a chance to arbitrate for the
4833		**	SCSI bus at least every 10 seconds.
4834		**
4835		**----------------------------------------------------
4836		*/
4837
4838		t = thistime - np->heartbeat;
4839
4840		if (t<2) np->latetime=0; else np->latetime++;
4841
4842		if (np->latetime>2) {
4843			/*
4844			**      If there are no requests, the script
4845			**      processor will sleep on SEL_WAIT_RESEL.
4846			**      But we have to check whether it died.
4847			**      Let's wake it up.
4848			*/
4849			OUTB (nc_istat, SIGP);
4850		};
4851
4852		if (np->latetime>4) {
4853			/*
4854			**	Although we tried to wake it up,
4855			**	the script processor didn't respond.
4856			**
4857			**	May be a target is hanging,
4858			**	or another initator lets a tape device
4859			**	rewind with disconnect disabled :-(
4860			**
4861			**	We won't accept that.
4862			*/
4863			if (INB (nc_sbcl) & CBSY)
4864				OUTB (nc_scntl1, CRST);
4865			DELAY (1000);
4866			ncr_init (np, "ncr dead ?", HS_TIMEOUT);
4867			np->heartbeat = thistime;
4868		};
4869
4870		/*----------------------------------------------------
4871		**
4872		**	handle ccb timeouts
4873		**
4874		**----------------------------------------------------
4875		*/
4876
4877		for (cp=&np->ccb; cp; cp=cp->link_ccb) {
4878			/*
4879			**	look for timed out ccbs.
4880			*/
4881			if (!cp->host_status) continue;
4882			count++;
4883			if (cp->tlimit > thistime) continue;
4884
4885			/*
4886			**	Disable reselect.
4887			**      Remove it from startqueue.
4888			*/
4889			cp->jump_ccb.l_cmd = (SCR_JUMP);
4890			if (cp->phys.header.launch.l_paddr ==
4891				NCB_SCRIPT_PHYS (np, select)) {
4892				printf ("%s: timeout ccb=%x (skip)\n",
4893					ncr_name (np), (unsigned)cp);
4894				cp->phys.header.launch.l_paddr
4895				= NCB_SCRIPT_PHYS (np, skip);
4896			};
4897
4898			switch (cp->host_status) {
4899
4900			case HS_BUSY:
4901			case HS_NEGOTIATE:
4902				/*
4903				** still in start queue ?
4904				*/
4905				if (cp->phys.header.launch.l_paddr ==
4906					NCB_SCRIPT_PHYS (np, skip))
4907					continue;
4908
4909				/* fall through */
4910			case HS_DISCONNECT:
4911				cp->host_status=HS_TIMEOUT;
4912			};
4913			cp->tag = 0;
4914
4915			/*
4916			**	wakeup this ccb.
4917			*/
4918			ncr_complete (np, cp);
4919		};
4920		splx (oldspl);
4921	}
4922
4923	timeout (TIMEOUT ncr_timeout, (caddr_t) np, step ? step : 1);
4924
4925	if (INB(nc_istat) & (INTF|SIP|DIP)) {
4926
4927		/*
4928		**	Process pending interrupts.
4929		*/
4930
4931		int	oldspl	= splbio ();
4932		if (DEBUG_FLAGS & DEBUG_TINY) printf ("{");
4933		ncr_exception (np);
4934		if (DEBUG_FLAGS & DEBUG_TINY) printf ("}");
4935		splx (oldspl);
4936	};
4937}
4938
4939/*==========================================================
4940**
4941**
4942**	ncr chip exception handler.
4943**
4944**
4945**==========================================================
4946*/
4947
4948void ncr_exception (ncb_p np)
4949{
4950	u_char	istat, dstat;
4951	u_short	sist;
4952	u_long	dsp, dsa;
4953	int	i, script_ofs;
4954
4955	/*
4956	**	interrupt on the fly ?
4957	*/
4958	while ((istat = INB (nc_istat)) & INTF) {
4959		if (DEBUG_FLAGS & DEBUG_TINY) printf ("F");
4960		OUTB (nc_istat, INTF);
4961		np->profile.num_fly++;
4962		ncr_wakeup (np, 0);
4963	};
4964
4965	if (!(istat & (SIP|DIP))) return;
4966
4967	/*
4968	**	Steinbach's Guideline for Systems Programming:
4969	**	Never test for an error condition you don't know how to handle.
4970	*/
4971
4972	dstat = INB (nc_dstat);
4973	sist  = INW (nc_sist) ;
4974	np->profile.num_int++;
4975
4976	if (DEBUG_FLAGS & DEBUG_TINY)
4977		printf ("<%d|%x:%x|%x:%x>",
4978			INB(nc_scr0),
4979			dstat,sist,
4980			(unsigned)INL(nc_dsp),
4981			(unsigned)INL(nc_dbc));
4982	if ((dstat==DFE) && (sist==PAR)) return;
4983
4984/*==========================================================
4985**
4986**	First the normal cases.
4987**
4988**==========================================================
4989*/
4990	/*-------------------------------------------
4991	**	SCSI reset
4992	**-------------------------------------------
4993	*/
4994
4995	if (sist & RST) {
4996		ncr_init (np, bootverbose ? "scsi reset" : NULL, HS_RESET);
4997		return;
4998	};
4999
5000	/*-------------------------------------------
5001	**	selection timeout
5002	**
5003	**	IID excluded from dstat mask!
5004	**	(chip bug)
5005	**-------------------------------------------
5006	*/
5007
5008	if ((sist  & STO) &&
5009		!(sist  & (GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5010		!(dstat & (MDPE|BF|ABRT|SIR))) {
5011		ncr_int_sto (np);
5012		return;
5013	};
5014
5015	/*-------------------------------------------
5016	**      Phase mismatch.
5017	**-------------------------------------------
5018	*/
5019
5020	if ((sist  & MA) &&
5021		!(sist  & (STO|GEN|HTH|SGE|UDC|RST|PAR)) &&
5022		!(dstat & (MDPE|BF|ABRT|SIR|IID))) {
5023		ncr_int_ma (np);
5024		return;
5025	};
5026
5027	/*----------------------------------------
5028	**	move command with length 0
5029	**----------------------------------------
5030	*/
5031
5032	if ((dstat & IID) &&
5033		!(sist  & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5034		!(dstat & (MDPE|BF|ABRT|SIR)) &&
5035		((INL(nc_dbc) & 0xf8000000) == SCR_MOVE_TBL)) {
5036		/*
5037		**      Target wants more data than available.
5038		**	The "no_data" script will do it.
5039		*/
5040		OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, no_data));
5041		return;
5042	};
5043
5044	/*-------------------------------------------
5045	**	Programmed interrupt
5046	**-------------------------------------------
5047	*/
5048
5049	if ((dstat & SIR) &&
5050		!(sist  & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5051		!(dstat & (MDPE|BF|ABRT|IID)) &&
5052		(INB(nc_dsps) <= SIR_MAX)) {
5053		ncr_int_sir (np);
5054		return;
5055	};
5056
5057	/*========================================
5058	**	do the register dump
5059	**========================================
5060	*/
5061
5062	if (time.tv_sec - np->regtime.tv_sec>10) {
5063		int i;
5064		np->regtime = time;
5065		for (i=0; i<sizeof(np->regdump); i++)
5066			((char*)&np->regdump)[i] = ((char*)np->reg)[i];
5067		np->regdump.nc_dstat = dstat;
5068		np->regdump.nc_sist  = sist;
5069	};
5070
5071	/*=========================================
5072	**	log message for real hard errors
5073	**=========================================
5074
5075	"ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ (dsp:dbc)."
5076	"	      reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
5077
5078	exception register:
5079		ds:	dstat
5080		si:	sist
5081
5082	SCSI bus lines:
5083		so:	control lines as driver by NCR.
5084		si:	control lines as seen by NCR.
5085		sd:	scsi data lines as seen by NCR.
5086
5087	wide/fastmode:
5088		sxfer:	(see the manual)
5089		scntl3:	(see the manual)
5090
5091	current script command:
5092		dsp:	script adress (relative to start of script).
5093		dbc:	first word of script command.
5094
5095	First 16 register of the chip:
5096		r0..rf
5097
5098	=============================================
5099	*/
5100
5101	dsp = (unsigned) INL (nc_dsp);
5102	dsa = (unsigned) INL (nc_dsa);
5103
5104	script_ofs = dsp - np->p_script;
5105
5106	printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%x:%08x).\n",
5107		ncr_name (np), INB (nc_ctest0)&0x0f, dstat, sist,
5108		INB (nc_socl), INB (nc_sbcl), INB (nc_sbdl),
5109		INB (nc_sxfer),INB (nc_scntl3), script_ofs,
5110		(unsigned) INL (nc_dbc));
5111
5112	if (((script_ofs & 3) == 0) &&
5113	    (unsigned)script_ofs < sizeof(struct script)) {
5114		printf ("\tscript cmd = %08x\n",
5115			*(ncrcmd *)((char*)np->script +script_ofs));
5116	}
5117
5118	printf ("\treg:\t");
5119	for (i=0; i<16;i++)
5120		printf (" %02x", ((u_char*)np->reg)[i]);
5121	printf (".\n");
5122
5123	/*----------------------------------------
5124	**	clean up the dma fifo
5125	**----------------------------------------
5126	*/
5127
5128	if ( (INB(nc_sstat0) & (ILF|ORF|OLF)   ) ||
5129	     (INB(nc_sstat1) & (FF3210)	) ||
5130	     (INB(nc_sstat2) & (ILF1|ORF1|OLF1)) ||	/* wide .. */
5131	     !(dstat & DFE)) {
5132		printf ("%s: have to clear fifos.\n", ncr_name (np));
5133		OUTB (nc_stest3, TE|CSF);	/* clear scsi fifo */
5134		OUTB (nc_ctest3, CLF);		/* clear dma fifo  */
5135	}
5136
5137	/*----------------------------------------
5138	**	handshake timeout
5139	**----------------------------------------
5140	*/
5141
5142	if (sist & HTH) {
5143		printf ("%s: handshake timeout\n", ncr_name(np));
5144		OUTB (nc_scntl1, CRST);
5145		DELAY (1000);
5146		OUTB (nc_scntl1, 0x00);
5147		OUTB (nc_scr0, HS_FAIL);
5148		OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
5149		return;
5150	}
5151
5152	/*----------------------------------------
5153	**	unexpected disconnect
5154	**----------------------------------------
5155	*/
5156
5157	if ((sist  & UDC) &&
5158		!(sist  & (STO|GEN|HTH|MA|SGE|RST|PAR)) &&
5159		!(dstat & (MDPE|BF|ABRT|SIR|IID))) {
5160		OUTB (nc_scr0, HS_UNEXPECTED);
5161		OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, cleanup));
5162		return;
5163	};
5164
5165	/*----------------------------------------
5166	**	cannot disconnect
5167	**----------------------------------------
5168	*/
5169
5170	if ((dstat & IID) &&
5171		!(sist  & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5172		!(dstat & (MDPE|BF|ABRT|SIR)) &&
5173		((INL(nc_dbc) & 0xf8000000) == SCR_WAIT_DISC)) {
5174		/*
5175		**      Unexpected data cycle while waiting for disconnect.
5176		*/
5177		if (INB(nc_sstat2) & LDSC) {
5178			/*
5179			**	It's an early reconnect.
5180			**	Let's continue ...
5181			*/
5182			OUTB (nc_dcntl, (STD|NOCOM));
5183			/*
5184			**	info message
5185			*/
5186			printf ("%s: INFO: LDSC while IID.\n",
5187				ncr_name (np));
5188			return;
5189		};
5190		printf ("%s: target %d doesn't release the bus.\n",
5191			ncr_name (np), INB (nc_ctest0)&0x0f);
5192		/*
5193		**	return without restarting the NCR.
5194		**	timeout will do the real work.
5195		*/
5196		return;
5197	};
5198
5199	/*----------------------------------------
5200	**	single step
5201	**----------------------------------------
5202	*/
5203
5204	if ((dstat & SSI) &&
5205		!(sist  & (STO|GEN|HTH|MA|SGE|UDC|RST|PAR)) &&
5206		!(dstat & (MDPE|BF|ABRT|SIR|IID))) {
5207		OUTB (nc_dcntl, (STD|NOCOM));
5208		return;
5209	};
5210
5211/*
5212**	@RECOVER@ HTH, SGE, ABRT.
5213**
5214**	We should try to recover from these interrupts.
5215**	They may occur if there are problems with synch transfers, or
5216**	if targets are switched on or off while the driver is running.
5217*/
5218
5219	if (sist & SGE) {
5220		OUTB (nc_ctest3, CLF);		/* clear scsi offsets */
5221	}
5222
5223	/*
5224	**	Freeze controller to be able to read the messages.
5225	*/
5226
5227	if (DEBUG_FLAGS & DEBUG_FREEZE) {
5228		int i;
5229		unsigned char val;
5230		for (i=0; i<0x60; i++) {
5231			switch (i%16) {
5232
5233			case 0:
5234				printf ("%s: reg[%d0]: ",
5235					ncr_name(np),i/16);
5236				break;
5237			case 4:
5238			case 8:
5239			case 12:
5240				printf (" ");
5241				break;
5242			};
5243			val = ((unsigned char*) np->vaddr) [i];
5244			printf (" %x%x", val/16, val%16);
5245			if (i%16==15) printf (".\n");
5246		};
5247
5248		untimeout (TIMEOUT ncr_timeout, (caddr_t) np);
5249
5250		printf ("%s: halted!\n", ncr_name(np));
5251		/*
5252		**	don't restart controller ...
5253		*/
5254		OUTB (nc_istat,  SRST);
5255		return;
5256	};
5257
5258#ifdef NCR_FREEZE
5259	/*
5260	**	Freeze system to be able to read the messages.
5261	*/
5262	printf ("ncr: fatal error: system halted - press reset to reboot ...");
5263	(void) splhigh();
5264	for (;;);
5265#endif
5266
5267	/*
5268	**	sorry, have to kill ALL jobs ...
5269	*/
5270
5271	ncr_init (np, "fatal error", HS_FAIL);
5272}
5273
5274/*==========================================================
5275**
5276**	ncr chip exception handler for selection timeout
5277**
5278**==========================================================
5279**
5280**	There seems to be a bug in the 53c810.
5281**	Although a STO-Interrupt is pending,
5282**	it continues executing script commands.
5283**	But it will fail and interrupt (IID) on
5284**	the next instruction where it's looking
5285**	for a valid phase.
5286**
5287**----------------------------------------------------------
5288*/
5289
5290void ncr_int_sto (ncb_p np)
5291{
5292	u_long dsa, scratcha, diff;
5293	ccb_p cp;
5294	if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
5295
5296	/*
5297	**	look for ccb and set the status.
5298	*/
5299
5300	dsa = INL (nc_dsa);
5301	cp = &np->ccb;
5302	while (cp && (CCB_PHYS (cp, phys) != dsa))
5303		cp = cp->link_ccb;
5304
5305	if (cp) {
5306		cp-> host_status = HS_SEL_TIMEOUT;
5307		ncr_complete (np, cp);
5308	};
5309
5310	/*
5311	**	repair start queue
5312	*/
5313
5314	scratcha = INL (nc_scratcha);
5315	diff = scratcha - NCB_SCRIPT_PHYS (np, tryloop);
5316
5317/*	assert ((diff <= MAX_START * 20) && !(diff % 20));*/
5318
5319	if ((diff <= MAX_START * 20) && !(diff % 20)) {
5320		np->script->startpos[0] = scratcha;
5321		OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, start));
5322		return;
5323	};
5324	ncr_init (np, "selection timeout", HS_FAIL);
5325}
5326
5327/*==========================================================
5328**
5329**
5330**	ncr chip exception handler for phase errors.
5331**
5332**
5333**==========================================================
5334**
5335**	We have to construct a new transfer descriptor,
5336**	to transfer the rest of the current block.
5337**
5338**----------------------------------------------------------
5339*/
5340
5341static void ncr_int_ma (ncb_p np)
5342{
5343	u_long	dbc;
5344	u_long	rest;
5345	u_long	dsa;
5346	u_long	dsp;
5347	u_long	nxtdsp;
5348	u_long	*vdsp;
5349	u_long	oadr, olen;
5350	u_long	*tblp, *newcmd;
5351	u_char	cmd, sbcl, delta, ss0, ss2;
5352	ccb_p	cp;
5353
5354	dsp = INL (nc_dsp);
5355	dsa = INL (nc_dsa);
5356	dbc = INL (nc_dbc);
5357	ss0 = INB (nc_sstat0);
5358	ss2 = INB (nc_sstat2);
5359	sbcl= INB (nc_sbcl);
5360
5361	cmd = dbc >> 24;
5362	rest= dbc & 0xffffff;
5363	delta=(INB (nc_dfifo) - rest) & 0x7f;
5364
5365	/*
5366	**	The data in the dma fifo has not been transfered to
5367	**	the target -> add the amount to the rest
5368	**	and clear the data.
5369	**	Check the sstat2 register in case of wide transfer.
5370	*/
5371
5372	if (! (INB(nc_dstat) & DFE)) rest += delta;
5373	if (ss0 & OLF) rest++;
5374	if (ss0 & ORF) rest++;
5375	if (INB(nc_scntl3) & EWS) {
5376		if (ss2 & OLF1) rest++;
5377		if (ss2 & ORF1) rest++;
5378	};
5379	OUTB (nc_ctest3, CLF   );	/* clear dma fifo  */
5380	OUTB (nc_stest3, TE|CSF);	/* clear scsi fifo */
5381
5382	/*
5383	**	locate matching cp
5384	*/
5385	dsa = INL (nc_dsa);
5386	cp = &np->ccb;
5387	while (cp && (CCB_PHYS (cp, phys) != dsa))
5388		cp = cp->link_ccb;
5389
5390	if (!cp) {
5391	    printf ("%s: SCSI phase error fixup: CCB already dequeued (0x%08lx)\n",
5392		    ncr_name (np), (u_long) np->header.cp);
5393	    return;
5394	}
5395	if (cp != np->header.cp) {
5396	    printf ("%s: SCSI phase error fixup: CCB address mismatch (0x%08lx != 0x%08lx)\n",
5397		    ncr_name (np), (u_long) cp, (u_long) np->header.cp);
5398	    return;
5399	}
5400
5401	/*
5402	**	find the interrupted script command,
5403	**	and the address at which to continue.
5404	*/
5405
5406	if (dsp == vtophys (&cp->patch[2])) {
5407		vdsp = &cp->patch[0];
5408		nxtdsp = vdsp[3];
5409	} else if (dsp == vtophys (&cp->patch[6])) {
5410		vdsp = &cp->patch[4];
5411		nxtdsp = vdsp[3];
5412	} else {
5413		vdsp = (u_long*) ((char*)np->script - np->p_script + dsp -8);
5414		nxtdsp = dsp;
5415	};
5416
5417	/*
5418	**	log the information
5419	*/
5420	if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE)) {
5421		printf ("P%x%x ",cmd&7, sbcl&7);
5422		printf ("RL=%d D=%d SS0=%x ",
5423			(unsigned) rest, (unsigned) delta, ss0);
5424	};
5425	if (DEBUG_FLAGS & DEBUG_PHASE) {
5426		printf ("\nCP=%x CP2=%x DSP=%x NXT=%x VDSP=%x CMD=%x ",
5427			(unsigned)cp, (unsigned)np->header.cp,
5428			(unsigned)dsp,
5429			(unsigned)nxtdsp, (unsigned)vdsp, cmd);
5430	};
5431
5432	/*
5433	**	get old startaddress and old length.
5434	*/
5435
5436	oadr = vdsp[1];
5437
5438	if (cmd & 0x10) {	/* Table indirect */
5439		tblp = (u_long*) ((char*) &cp->phys + oadr);
5440		olen = tblp[0];
5441		oadr = tblp[1];
5442	} else {
5443		tblp = (u_long*) 0;
5444		olen = vdsp[0] & 0xffffff;
5445	};
5446
5447	if (DEBUG_FLAGS & DEBUG_PHASE) {
5448		printf ("OCMD=%x\nTBLP=%x OLEN=%x OADR=%x\n",
5449			(unsigned) (vdsp[0] >> 24),
5450			(unsigned) tblp,
5451			(unsigned) olen,
5452			(unsigned) oadr);
5453	};
5454
5455	/*
5456	**	if old phase not dataphase, leave here.
5457	*/
5458
5459	if (cmd != (vdsp[0] >> 24)) {
5460		PRINT_ADDR(cp->xfer);
5461		printf ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
5462			(unsigned)cmd, (unsigned)vdsp[0] >> 24);
5463
5464		return;
5465	}
5466	if (cmd & 0x06) {
5467		PRINT_ADDR(cp->xfer);
5468		printf ("phase change %x-%x %d@%08x resid=%d.\n",
5469			cmd&7, sbcl&7, (unsigned)olen,
5470			(unsigned)oadr, (unsigned)rest);
5471
5472		OUTB (nc_dcntl, (STD|NOCOM));
5473		return;
5474	};
5475
5476	/*
5477	**	choose the correct patch area.
5478	**	if savep points to one, choose the other.
5479	*/
5480
5481	newcmd = cp->patch;
5482	if (cp->phys.header.savep == vtophys (newcmd)) newcmd+=4;
5483
5484	/*
5485	**	fillin the commands
5486	*/
5487
5488	newcmd[0] = ((cmd & 0x0f) << 24) | rest;
5489	newcmd[1] = oadr + olen - rest;
5490	newcmd[2] = SCR_JUMP;
5491	newcmd[3] = nxtdsp;
5492
5493	if (DEBUG_FLAGS & DEBUG_PHASE) {
5494		PRINT_ADDR(cp->xfer);
5495		printf ("newcmd[%d] %x %x %x %x.\n",
5496			newcmd - cp->patch,
5497			(unsigned)newcmd[0],
5498			(unsigned)newcmd[1],
5499			(unsigned)newcmd[2],
5500			(unsigned)newcmd[3]);
5501	}
5502	/*
5503	**	fake the return address (to the patch).
5504	**	and restart script processor at dispatcher.
5505	*/
5506	np->profile.num_break++;
5507	OUTL (nc_temp, vtophys (newcmd));
5508	OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
5509}
5510
5511/*==========================================================
5512**
5513**
5514**      ncr chip exception handler for programmed interrupts.
5515**
5516**
5517**==========================================================
5518*/
5519
5520static int ncr_show_msg (u_char * msg)
5521{
5522	u_char i;
5523	printf ("%x",*msg);
5524	if (*msg==M_EXTENDED) {
5525		for (i=1;i<8;i++) {
5526			if (i-1>msg[1]) break;
5527			printf ("-%x",msg[i]);
5528		};
5529		return (i+1);
5530	} else if ((*msg & 0xf0) == 0x20) {
5531		printf ("-%x",msg[1]);
5532		return (2);
5533	};
5534	return (1);
5535}
5536
5537void ncr_int_sir (ncb_p np)
5538{
5539	u_char chg, ofs, per, fak, wide;
5540	u_char num = INB (nc_dsps);
5541	ccb_p	cp=0;
5542	u_long	dsa;
5543	u_char	target = INB (nc_ctest0) & 7;
5544	tcb_p	tp     = &np->target[target];
5545	int     i;
5546	if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
5547
5548	switch (num) {
5549	case SIR_SENSE_RESTART:
5550	case SIR_STALL_RESTART:
5551		break;
5552
5553	default:
5554		/*
5555		**	lookup the ccb
5556		*/
5557		dsa = INL (nc_dsa);
5558		cp = &np->ccb;
5559		while (cp && (CCB_PHYS (cp, phys) != dsa))
5560			cp = cp->link_ccb;
5561
5562		assert (cp);
5563		if (!cp)
5564			goto out;
5565		assert (cp == np->header.cp);
5566		if (cp != np->header.cp)
5567			goto out;
5568	}
5569
5570	switch (num) {
5571
5572/*--------------------------------------------------------------------
5573**
5574**	Processing of interrupted getcc selects
5575**
5576**--------------------------------------------------------------------
5577*/
5578
5579	case SIR_SENSE_RESTART:
5580		/*------------------------------------------
5581		**	Script processor is idle.
5582		**	Look for interrupted "check cond"
5583		**------------------------------------------
5584		*/
5585
5586		if (DEBUG_FLAGS & DEBUG_RESTART)
5587			printf ("%s: int#%d",ncr_name (np),num);
5588		cp = (ccb_p) 0;
5589		for (i=0; i<MAX_TARGET; i++) {
5590			if (DEBUG_FLAGS & DEBUG_RESTART) printf (" t%d", i);
5591			tp = &np->target[i];
5592			if (DEBUG_FLAGS & DEBUG_RESTART) printf ("+");
5593			cp = tp->hold_cp;
5594			if (!cp) continue;
5595			if (DEBUG_FLAGS & DEBUG_RESTART) printf ("+");
5596			if ((cp->host_status==HS_BUSY) &&
5597				(cp->scsi_status==S_CHECK_COND))
5598				break;
5599			if (DEBUG_FLAGS & DEBUG_RESTART) printf ("- (remove)");
5600			tp->hold_cp = cp = (ccb_p) 0;
5601		};
5602
5603		if (cp) {
5604			if (DEBUG_FLAGS & DEBUG_RESTART)
5605				printf ("+ restart job ..\n");
5606			OUTL (nc_dsa, CCB_PHYS (cp, phys));
5607			OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, getcc));
5608			return;
5609		};
5610
5611		/*
5612		**	no job, resume normal processing
5613		*/
5614		if (DEBUG_FLAGS & DEBUG_RESTART) printf (" -- remove trap\n");
5615		np->script->start0[0] =  SCR_INT ^ IFFALSE (0);
5616		break;
5617
5618	case SIR_SENSE_FAILED:
5619		/*-------------------------------------------
5620		**	While trying to select for
5621		**	getting the condition code,
5622		**	a target reselected us.
5623		**-------------------------------------------
5624		*/
5625		if (DEBUG_FLAGS & DEBUG_RESTART) {
5626			PRINT_ADDR(cp->xfer);
5627			printf ("in getcc reselect by t%d.\n",
5628				INB(nc_ssid) & 0x0f);
5629		}
5630
5631		/*
5632		**	Mark this job
5633		*/
5634		cp->host_status = HS_BUSY;
5635		cp->scsi_status = S_CHECK_COND;
5636		np->target[cp->xfer->sc_link->target].hold_cp = cp;
5637
5638		/*
5639		**	And patch code to restart it.
5640		*/
5641		np->script->start0[0] =  SCR_INT;
5642		break;
5643
5644/*-----------------------------------------------------------------------------
5645**
5646**	Was Sie schon immer ueber transfermode negotiation wissen wollten ...
5647**
5648**	We try to negotiate sync and wide transfer only after
5649**	a successfull inquire command. We look at byte 7 of the
5650**	inquire data to determine the capabilities if the target.
5651**
5652**	When we try to negotiate, we append the negotiation message
5653**	to the identify and (maybe) simple tag message.
5654**	The host status field is set to HS_NEGOTIATE to mark this
5655**	situation.
5656**
5657**	If the target doesn't answer this message immidiately
5658**	(as required by the standard), the SIR_NEGO_FAIL interrupt
5659**	will be raised eventually.
5660**	The handler removes the HS_NEGOTIATE status, and sets the
5661**	negotiated value to the default (async / nowide).
5662**
5663**	If we receive a matching answer immediately, we check it
5664**	for validity, and set the values.
5665**
5666**	If we receive a Reject message immediately, we assume the
5667**	negotiation has failed, and fall back to standard values.
5668**
5669**	If we receive a negotiation message while not in HS_NEGOTIATE
5670**	state, it's a target initiated negotiation. We prepare a
5671**	(hopefully) valid answer, set our parameters, and send back
5672**	this answer to the target.
5673**
5674**	If the target doesn't fetch the answer (no message out phase),
5675**	we assume the negotiation has failed, and fall back to default
5676**	settings.
5677**
5678**	When we set the values, we adjust them in all ccbs belonging
5679**	to this target, in the controller's register, and in the "phys"
5680**	field of the controller's struct ncb.
5681**
5682**	Possible cases:		   hs  sir   msg_in value  send   goto
5683**	We try try to negotiate:
5684**	-> target doesnt't msgin   NEG FAIL  noop   defa.  -      dispatch
5685**	-> target rejected our msg NEG FAIL  reject defa.  -      dispatch
5686**	-> target answered  (ok)   NEG SYNC  sdtr   set    -      clrack
5687**	-> target answered (!ok)   NEG SYNC  sdtr   defa.  REJ--->msg_bad
5688**	-> target answered  (ok)   NEG WIDE  wdtr   set    -      clrack
5689**	-> target answered (!ok)   NEG WIDE  wdtr   defa.  REJ--->msg_bad
5690**	-> any other msgin	   NEG FAIL  noop   defa.  -      dispatch
5691**
5692**	Target tries to negotiate:
5693**	-> incoming message	   --- SYNC  sdtr   set    SDTR   -
5694**	-> incoming message	   --- WIDE  wdtr   set    WDTR   -
5695**      We sent our answer:
5696**	-> target doesn't msgout   --- PROTO ?      defa.  -      dispatch
5697**
5698**-----------------------------------------------------------------------------
5699*/
5700
5701	case SIR_NEGO_FAILED:
5702		/*-------------------------------------------------------
5703		**
5704		**	Negotiation failed.
5705		**	Target doesn't send an answer message,
5706		**	or target rejected our message.
5707		**
5708		**      Remove negotiation request.
5709		**
5710		**-------------------------------------------------------
5711		*/
5712		OUTB (HS_PRT, HS_BUSY);
5713
5714		/* fall through */
5715
5716	case SIR_NEGO_PROTO:
5717		/*-------------------------------------------------------
5718		**
5719		**	Negotiation failed.
5720		**	Target doesn't fetch the answer message.
5721		**
5722		**-------------------------------------------------------
5723		*/
5724
5725		if (DEBUG_FLAGS & DEBUG_NEGO) {
5726			PRINT_ADDR(cp->xfer);
5727			printf ("negotiation failed sir=%x status=%x.\n",
5728				num, cp->nego_status);
5729		};
5730
5731		/*
5732		**	any error in negotiation:
5733		**	fall back to default mode.
5734		*/
5735		switch (cp->nego_status) {
5736
5737		case NS_SYNC:
5738			ncr_setsync (np, cp, 0xe0);
5739			break;
5740
5741		case NS_WIDE:
5742			ncr_setwide (np, cp, 0);
5743			break;
5744
5745		};
5746		np->msgin [0] = M_NOOP;
5747		np->msgout[0] = M_NOOP;
5748		cp->nego_status = 0;
5749		OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, dispatch));
5750		break;
5751
5752	case SIR_NEGO_SYNC:
5753		/*
5754		**	Synchronous request message received.
5755		*/
5756
5757		if (DEBUG_FLAGS & DEBUG_NEGO) {
5758			PRINT_ADDR(cp->xfer);
5759			printf ("sync msgin: ");
5760			(void) ncr_show_msg (np->msgin);
5761			printf (".\n");
5762		};
5763
5764		/*
5765		**	get requested values.
5766		*/
5767
5768		chg = 0;
5769		per = np->msgin[3];
5770		ofs = np->msgin[4];
5771		if (ofs==0) per=255;
5772
5773		/*
5774		**      if target sends SDTR message,
5775		**	      it CAN transfer synch.
5776		*/
5777
5778		if (ofs)
5779			tp->inqdata[7] |= INQ7_SYNC;
5780
5781		/*
5782		**	check values against driver limits.
5783		*/
5784
5785		if (per < np->ns_sync)
5786			{chg = 1; per = np->ns_sync;}
5787		if (per < tp->minsync)
5788			{chg = 1; per = tp->minsync;}
5789		if (ofs > tp->maxoffs)
5790			{chg = 1; ofs = tp->maxoffs;}
5791
5792		/*
5793		**	Check against controller limits.
5794		*/
5795		fak = (4ul * per - 1) / np->ns_sync - 3;
5796		if (ofs && (fak>7))   {chg = 1; ofs = 0;}
5797		if (!ofs) fak=7;
5798
5799		if (DEBUG_FLAGS & DEBUG_NEGO) {
5800			PRINT_ADDR(cp->xfer);
5801			printf ("sync: per=%d ofs=%d fak=%d chg=%d.\n",
5802				per, ofs, fak, chg);
5803		}
5804
5805		if (INB (HS_PRT) == HS_NEGOTIATE) {
5806			OUTB (HS_PRT, HS_BUSY);
5807			switch (cp->nego_status) {
5808
5809			case NS_SYNC:
5810				/*
5811				**      This was an answer message
5812				*/
5813				if (chg) {
5814					/*
5815					**	Answer wasn't acceptable.
5816					*/
5817					ncr_setsync (np, cp, 0xe0);
5818					OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
5819				} else {
5820					/*
5821					**	Answer is ok.
5822					*/
5823					ncr_setsync (np, cp, (fak<<5)|ofs);
5824					OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
5825				};
5826				return;
5827
5828			case NS_WIDE:
5829				ncr_setwide (np, cp, 0);
5830				break;
5831			};
5832		};
5833
5834		/*
5835		**	It was a request. Set value and
5836		**      prepare an answer message
5837		*/
5838
5839		ncr_setsync (np, cp, (fak<<5)|ofs);
5840
5841		np->msgout[0] = M_EXTENDED;
5842		np->msgout[1] = 3;
5843		np->msgout[2] = M_X_SYNC_REQ;
5844		np->msgout[3] = per;
5845		np->msgout[4] = ofs;
5846
5847		cp->nego_status = NS_SYNC;
5848
5849		if (DEBUG_FLAGS & DEBUG_NEGO) {
5850			PRINT_ADDR(cp->xfer);
5851			printf ("sync msgout: ");
5852			(void) ncr_show_msg (np->msgin);
5853			printf (".\n");
5854		}
5855
5856		if (!ofs) {
5857			OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
5858			return;
5859		}
5860		np->msgin [0] = M_NOOP;
5861
5862		break;
5863
5864	case SIR_NEGO_WIDE:
5865		/*
5866		**	Wide request message received.
5867		*/
5868		if (DEBUG_FLAGS & DEBUG_NEGO) {
5869			PRINT_ADDR(cp->xfer);
5870			printf ("wide msgin: ");
5871			(void) ncr_show_msg (np->msgin);
5872			printf (".\n");
5873		};
5874
5875		/*
5876		**	get requested values.
5877		*/
5878
5879		chg  = 0;
5880		wide = np->msgin[3];
5881
5882		/*
5883		**      if target sends WDTR message,
5884		**	      it CAN transfer wide.
5885		*/
5886
5887		if (wide)
5888			tp->inqdata[7] |= INQ7_WIDE16;
5889
5890		/*
5891		**	check values against driver limits.
5892		*/
5893
5894		if (wide > tp->usrwide)
5895			{chg = 1; wide = tp->usrwide;}
5896
5897		if (DEBUG_FLAGS & DEBUG_NEGO) {
5898			PRINT_ADDR(cp->xfer);
5899			printf ("wide: wide=%d chg=%d.\n", wide, chg);
5900		}
5901
5902		if (INB (HS_PRT) == HS_NEGOTIATE) {
5903			OUTB (HS_PRT, HS_BUSY);
5904			switch (cp->nego_status) {
5905
5906			case NS_WIDE:
5907				/*
5908				**      This was an answer message
5909				*/
5910				if (chg) {
5911					/*
5912					**	Answer wasn't acceptable.
5913					*/
5914					ncr_setwide (np, cp, 0);
5915					OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, msg_bad));
5916				} else {
5917					/*
5918					**	Answer is ok.
5919					*/
5920					ncr_setwide (np, cp, wide);
5921					OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, clrack));
5922				};
5923				return;
5924
5925			case NS_SYNC:
5926				ncr_setsync (np, cp, 0xe0);
5927				break;
5928			};
5929		};
5930
5931		/*
5932		**	It was a request, set value and
5933		**      prepare an answer message
5934		*/
5935
5936		ncr_setwide (np, cp, wide);
5937
5938		np->msgout[0] = M_EXTENDED;
5939		np->msgout[1] = 2;
5940		np->msgout[2] = M_X_WIDE_REQ;
5941		np->msgout[3] = wide;
5942
5943		np->msgin [0] = M_NOOP;
5944
5945		cp->nego_status = NS_WIDE;
5946
5947		if (DEBUG_FLAGS & DEBUG_NEGO) {
5948			PRINT_ADDR(cp->xfer);
5949			printf ("wide msgout: ");
5950			(void) ncr_show_msg (np->msgin);
5951			printf (".\n");
5952		}
5953		break;
5954
5955/*--------------------------------------------------------------------
5956**
5957**	Processing of special messages
5958**
5959**--------------------------------------------------------------------
5960*/
5961
5962	case SIR_REJECT_RECEIVED:
5963		/*-----------------------------------------------
5964		**
5965		**	We received a M_REJECT message.
5966		**
5967		**-----------------------------------------------
5968		*/
5969
5970		PRINT_ADDR(cp->xfer);
5971		printf ("M_REJECT received (%x:%x).\n",
5972			(unsigned)np->lastmsg, np->msgout[0]);
5973		break;
5974
5975	case SIR_REJECT_SENT:
5976		/*-----------------------------------------------
5977		**
5978		**	We received an unknown message
5979		**
5980		**-----------------------------------------------
5981		*/
5982
5983		PRINT_ADDR(cp->xfer);
5984		printf ("M_REJECT sent for ");
5985		(void) ncr_show_msg (np->msgin);
5986		printf (".\n");
5987		break;
5988
5989/*--------------------------------------------------------------------
5990**
5991**	Processing of special messages
5992**
5993**--------------------------------------------------------------------
5994*/
5995
5996	case SIR_IGN_RESIDUE:
5997		/*-----------------------------------------------
5998		**
5999		**	We received an IGNORE RESIDUE message,
6000		**	which couldn't be handled by the script.
6001		**
6002		**-----------------------------------------------
6003		*/
6004
6005		PRINT_ADDR(cp->xfer);
6006		printf ("M_IGN_RESIDUE received, but not yet implemented.\n");
6007		break;
6008
6009	case SIR_MISSING_SAVE:
6010		/*-----------------------------------------------
6011		**
6012		**	We received an DISCONNECT message,
6013		**	but the datapointer wasn't saved before.
6014		**
6015		**-----------------------------------------------
6016		*/
6017
6018		PRINT_ADDR(cp->xfer);
6019		printf ("M_DISCONNECT received, but datapointer not saved:\n"
6020			"\tdata=%x save=%x goal=%x.\n",
6021			(unsigned) INL (nc_temp),
6022			(unsigned) np->header.savep,
6023			(unsigned) np->header.goalp);
6024		break;
6025
6026/*--------------------------------------------------------------------
6027**
6028**	Processing of a "S_QUEUE_FULL" status.
6029**
6030**	The current command has been rejected,
6031**	because there are too many in the command queue.
6032**	We have started too many commands for that target.
6033**
6034**	If possible, reinsert at head of queue.
6035**	Stall queue until there are no disconnected jobs
6036**	(ncr is REALLY idle). Then restart processing.
6037**
6038**	We should restart the current job after the controller
6039**	has become idle. But this is not yet implemented.
6040**
6041**--------------------------------------------------------------------
6042*/
6043	case SIR_STALL_QUEUE:
6044		/*-----------------------------------------------
6045		**
6046		**	Stall the start queue.
6047		**
6048		**-----------------------------------------------
6049		*/
6050		PRINT_ADDR(cp->xfer);
6051		printf ("queue full.\n");
6052
6053		np->script->start1[0] =  SCR_INT;
6054
6055		/*
6056		**	Try to disable tagged transfers.
6057		*/
6058		ncr_setmaxtags (&np->target[target], 0);
6059
6060		/*
6061		** @QUEUE@
6062		**
6063		**	Should update the launch field of the
6064		**	current job to be able to restart it.
6065		**	Then prepend it to the start queue.
6066		*/
6067
6068		/* fall through */
6069
6070	case SIR_STALL_RESTART:
6071		/*-----------------------------------------------
6072		**
6073		**	Enable selecting again,
6074		**	if NO disconnected jobs.
6075		**
6076		**-----------------------------------------------
6077		*/
6078		/*
6079		**	Look for a disconnected job.
6080		*/
6081		cp = &np->ccb;
6082		while (cp && cp->host_status != HS_DISCONNECT)
6083			cp = cp->link_ccb;
6084
6085		/*
6086		**	if there is one, ...
6087		*/
6088		if (cp) {
6089			/*
6090			**	wait for reselection
6091			*/
6092			OUTL (nc_dsp, NCB_SCRIPT_PHYS (np, reselect));
6093			return;
6094		};
6095
6096		/*
6097		**	else remove the interrupt.
6098		*/
6099
6100		printf ("%s: queue empty.\n", ncr_name (np));
6101		np->script->start1[0] =  SCR_INT ^ IFFALSE (0);
6102		break;
6103	};
6104
6105out:
6106	OUTB (nc_dcntl, (STD|NOCOM));
6107}
6108
6109/*==========================================================
6110**
6111**
6112**	Aquire a control block
6113**
6114**
6115**==========================================================
6116*/
6117
6118static	ccb_p ncr_get_ccb
6119	(ncb_p np, u_long flags, u_long target, u_long lun)
6120{
6121	lcb_p lp;
6122	ccb_p cp = (ccb_p) 0;
6123
6124	/*
6125	**	Lun structure available ?
6126	*/
6127
6128	lp = np->target[target].lp[lun];
6129	if (lp) {
6130		cp = lp->next_ccb;
6131
6132		/*
6133		**	Look for free CCB
6134		*/
6135
6136		while (cp && cp->magic) cp = cp->next_ccb;
6137	}
6138
6139	/*
6140	**	if nothing available, take the default.
6141	*/
6142
6143	if (!cp) cp = &np->ccb;
6144
6145	/*
6146	**	Wait until available.
6147	*/
6148
6149	while (cp->magic) {
6150		if (flags & SCSI_NOSLEEP) break;
6151		if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
6152			break;
6153	};
6154
6155	if (cp->magic)
6156		return ((ccb_p) 0);
6157
6158	cp->magic = 1;
6159	return (cp);
6160}
6161
6162/*==========================================================
6163**
6164**
6165**	Release one control block
6166**
6167**
6168**==========================================================
6169*/
6170
6171void ncr_free_ccb (ncb_p np, ccb_p cp, int flags)
6172{
6173	/*
6174	**    sanity
6175	*/
6176
6177	assert (cp != NULL);
6178
6179	cp -> host_status = HS_IDLE;
6180	cp -> magic = 0;
6181	if (cp == &np->ccb)
6182		wakeup ((caddr_t) cp);
6183}
6184
6185/*==========================================================
6186**
6187**
6188**      Allocation of resources for Targets/Luns/Tags.
6189**
6190**
6191**==========================================================
6192*/
6193
6194static	void ncr_alloc_ccb (ncb_p np, struct scsi_xfer * xp)
6195{
6196	tcb_p tp;
6197	lcb_p lp;
6198	ccb_p cp;
6199
6200	u_long	target;
6201	u_long	lun;
6202
6203	assert (np != NULL);
6204	assert (xp != NULL);
6205
6206	target = xp->sc_link->target;
6207	lun    = xp->sc_link->lun;
6208
6209	if (target>=MAX_TARGET) return;
6210	if (lun   >=MAX_LUN   ) return;
6211
6212	tp=&np->target[target];
6213
6214	if (!tp->jump_tcb.l_cmd) {
6215
6216		/*
6217		**	initialize it.
6218		*/
6219		tp->jump_tcb.l_cmd   = (SCR_JUMP^IFFALSE (DATA (0x80 + target)));
6220		tp->jump_tcb.l_paddr = np->jump_tcb.l_paddr;
6221
6222		tp->getscr[0] = SCR_COPY (1);
6223		tp->getscr[1] = vtophys (&tp->sval);
6224		tp->getscr[2] = np->paddr + offsetof (struct ncr_reg, nc_sxfer);
6225		tp->getscr[3] = SCR_COPY (1);
6226		tp->getscr[4] = vtophys (&tp->wval);
6227		tp->getscr[5] = np->paddr + offsetof (struct ncr_reg, nc_scntl3);
6228
6229		assert (( (offsetof(struct ncr_reg, nc_sxfer) ^
6230			offsetof(struct tcb    , sval    )) &3) == 0);
6231		assert (( (offsetof(struct ncr_reg, nc_scntl3) ^
6232			offsetof(struct tcb    , wval    )) &3) == 0);
6233
6234		tp->call_lun.l_cmd   = (SCR_CALL);
6235		tp->call_lun.l_paddr = NCB_SCRIPT_PHYS (np, resel_lun);
6236
6237		tp->jump_lcb.l_cmd   = (SCR_JUMP);
6238		tp->jump_lcb.l_paddr = NCB_SCRIPT_PHYS (np, abort);
6239		np->jump_tcb.l_paddr = vtophys (&tp->jump_tcb);
6240
6241		ncr_setmaxtags (tp, SCSI_NCR_MAX_TAGS);
6242	}
6243
6244	/*
6245	**	Logic unit control block
6246	*/
6247	lp = tp->lp[lun];
6248	if (!lp) {
6249		/*
6250		**	Allocate a lcb
6251		*/
6252		lp = (lcb_p) malloc (sizeof (struct lcb), M_DEVBUF, M_NOWAIT);
6253		if (!lp) return;
6254
6255		/*
6256		**	Initialize it
6257		*/
6258		bzero (lp, sizeof (*lp));
6259		lp->jump_lcb.l_cmd   = (SCR_JUMP ^ IFFALSE (DATA (lun)));
6260		lp->jump_lcb.l_paddr = tp->jump_lcb.l_paddr;
6261
6262		lp->call_tag.l_cmd   = (SCR_CALL);
6263		lp->call_tag.l_paddr = NCB_SCRIPT_PHYS (np, resel_tag);
6264
6265		lp->jump_ccb.l_cmd   = (SCR_JUMP);
6266		lp->jump_ccb.l_paddr = NCB_SCRIPT_PHYS (np, aborttag);
6267
6268		lp->actlink = 1;
6269
6270		/*
6271		**   Chain into LUN list
6272		*/
6273		tp->jump_lcb.l_paddr = vtophys (&lp->jump_lcb);
6274		tp->lp[lun] = lp;
6275
6276	}
6277
6278	/*
6279	**	Limit possible number of ccbs.
6280	**
6281	**	If tagged command queueing is enabled,
6282	**	can use more than one ccb.
6283	*/
6284
6285	if (np->actccbs >= MAX_START-2) return;
6286	if (lp->actccbs && (lp->actccbs >= lp->reqccbs))
6287		return;
6288
6289	/*
6290	**	Allocate a ccb
6291	*/
6292	cp = (ccb_p) malloc (sizeof (struct ccb), M_DEVBUF, M_NOWAIT);
6293
6294	if (!cp)
6295		return;
6296
6297	if (DEBUG_FLAGS & DEBUG_ALLOC) {
6298		PRINT_ADDR(xp);
6299		printf ("new ccb @%x.\n", (unsigned) cp);
6300	}
6301
6302	/*
6303	**	Count it
6304	*/
6305	lp->actccbs++;
6306	np->actccbs++;
6307
6308	/*
6309	**	Initialize it
6310	*/
6311	bzero (cp, sizeof (*cp));
6312
6313	/*
6314	**	Fill in physical addresses
6315	*/
6316
6317	cp->p_ccb	     = vtophys (cp);
6318
6319	/*
6320	**	Chain into reselect list
6321	*/
6322	cp->jump_ccb.l_cmd   = SCR_JUMP;
6323	cp->jump_ccb.l_paddr = lp->jump_ccb.l_paddr;
6324	lp->jump_ccb.l_paddr = CCB_PHYS (cp, jump_ccb);
6325	cp->call_tmp.l_cmd   = SCR_CALL;
6326	cp->call_tmp.l_paddr = NCB_SCRIPT_PHYS (np, resel_tmp);
6327
6328	/*
6329	**	Chain into wakeup list
6330	*/
6331	cp->link_ccb      = np->ccb.link_ccb;
6332	np->ccb.link_ccb  = cp;
6333
6334	/*
6335	**	Chain into CCB list
6336	*/
6337	cp->next_ccb	= lp->next_ccb;
6338	lp->next_ccb	= cp;
6339}
6340
6341/*==========================================================
6342**
6343**
6344**	Announce the number of ccbs/tags to the scsi driver.
6345**
6346**
6347**==========================================================
6348*/
6349
6350static void ncr_opennings (ncb_p np, lcb_p lp, struct scsi_xfer * xp)
6351{
6352	/*
6353	**	want to reduce the number ...
6354	*/
6355	if (lp->actlink > lp->reqlink) {
6356
6357		/*
6358		**	Try to  reduce the count.
6359		**	We assume to run at splbio ..
6360		*/
6361		u_char diff = lp->actlink - lp->reqlink;
6362
6363		if (!diff) return;
6364
6365#ifdef __NetBSD__
6366		if (diff > xp->sc_link->openings)
6367			diff = xp->sc_link->openings;
6368
6369		xp->sc_link->openings	-= diff;
6370#else /* !__NetBSD__ */
6371		if (diff > xp->sc_link->opennings)
6372			diff = xp->sc_link->opennings;
6373
6374		xp->sc_link->opennings	-= diff;
6375#endif /* __NetBSD__ */
6376		lp->actlink		-= diff;
6377		if (DEBUG_FLAGS & DEBUG_TAGS)
6378			printf ("%s: actlink: diff=%d, new=%d, req=%d\n",
6379				ncr_name(np), diff, lp->actlink, lp->reqlink);
6380		return;
6381	};
6382
6383	/*
6384	**	want to increase the number ?
6385	*/
6386	if (lp->reqlink > lp->actlink) {
6387		u_char diff = lp->reqlink - lp->actlink;
6388
6389#ifdef __NetBSD__
6390		xp->sc_link->openings	+= diff;
6391#else /* !__NetBSD__ */
6392		xp->sc_link->opennings	+= diff;
6393#endif /* __NetBSD__ */
6394		lp->actlink		+= diff;
6395		wakeup ((caddr_t) xp->sc_link);
6396		if (DEBUG_FLAGS & DEBUG_TAGS)
6397			printf ("%s: actlink: diff=%d, new=%d, req=%d\n",
6398				ncr_name(np), diff, lp->actlink, lp->reqlink);
6399	};
6400}
6401
6402/*==========================================================
6403**
6404**
6405**	Build Scatter Gather Block
6406**
6407**
6408**==========================================================
6409**
6410**	The transfer area may be scattered among
6411**	several non adjacent physical pages.
6412**
6413**	We may use MAX_SCATTER blocks.
6414**
6415**----------------------------------------------------------
6416*/
6417
6418static	int	ncr_scatter
6419	(struct dsb* phys, vm_offset_t vaddr, vm_size_t datalen)
6420{
6421	u_long	paddr, pnext;
6422
6423	u_short	segment  = 0;
6424	u_long	segsize, segaddr;
6425	u_long	size, csize    = 0;
6426	u_long	chunk = MAX_SIZE;
6427	int	free;
6428
6429	bzero (&phys->data, sizeof (phys->data));
6430	if (!datalen) return (0);
6431
6432	paddr = vtophys (vaddr);
6433
6434	/*
6435	**	insert extra break points at a distance of chunk.
6436	**	We try to reduce the number of interrupts caused
6437	**	by unexpected phase changes due to disconnects.
6438	**	A typical harddisk may disconnect before ANY block.
6439	**	If we wanted to avoid unexpected phase changes at all
6440	**	we had to use a break point every 512 bytes.
6441	**	Of course the number of scatter/gather blocks is
6442	**	limited.
6443	*/
6444
6445	free = MAX_SCATTER - 1;
6446
6447	if (vaddr & (NBPG-1)) free -= datalen / NBPG;
6448
6449	if (free>1)
6450		while ((chunk * free >= 2 * datalen) && (chunk>=1024))
6451			chunk /= 2;
6452
6453	if(DEBUG_FLAGS & DEBUG_SCATTER)
6454		printf("ncr?:\tscattering virtual=0x%x size=%d chunk=%d.\n",
6455			(unsigned) vaddr, (unsigned) datalen, (unsigned) chunk);
6456
6457	/*
6458	**   Build data descriptors.
6459	*/
6460	while (datalen && (segment < MAX_SCATTER)) {
6461
6462		/*
6463		**	this segment is empty
6464		*/
6465		segsize = 0;
6466		segaddr = paddr;
6467		pnext   = paddr;
6468
6469		if (!csize) csize = chunk;
6470
6471		while ((datalen) && (paddr == pnext) && (csize)) {
6472
6473			/*
6474			**	continue this segment
6475			*/
6476			pnext = (paddr & (~(NBPG - 1))) + NBPG;
6477
6478			/*
6479			**	Compute max size
6480			*/
6481
6482			size = pnext - paddr;		/* page size */
6483			if (size > datalen) size = datalen;  /* data size */
6484			if (size > csize  ) size = csize  ;  /* chunksize */
6485
6486			segsize += size;
6487			vaddr   += size;
6488			csize   -= size;
6489			datalen -= size;
6490			paddr    = vtophys (vaddr);
6491		};
6492
6493		if(DEBUG_FLAGS & DEBUG_SCATTER)
6494			printf ("\tseg #%d  addr=%x  size=%d  (rest=%d).\n",
6495			segment,
6496			(unsigned) segaddr,
6497			(unsigned) segsize,
6498			(unsigned) datalen);
6499
6500		phys->data[segment].addr = segaddr;
6501		phys->data[segment].size = segsize;
6502		segment++;
6503	}
6504
6505	if (datalen) {
6506		printf("ncr?: scatter/gather failed (residue=%d).\n",
6507			(unsigned) datalen);
6508		return (-1);
6509	};
6510
6511	return (segment);
6512}
6513
6514/*==========================================================
6515**
6516**
6517**	Test the pci bus snoop logic :-(
6518**
6519**	Has to be called with interrupts disabled.
6520**
6521**
6522**==========================================================
6523*/
6524
6525#ifndef NCR_IOMAPPED
6526static int ncr_regtest (struct ncb* np)
6527{
6528	register volatile u_long data, *addr;
6529	/*
6530	**	ncr registers may NOT be cached.
6531	**	write 0xffffffff to a read only register area,
6532	**	and try to read it back.
6533	*/
6534	addr = (u_long*) &np->reg->nc_dstat;
6535	data = 0xffffffff;
6536	*addr= data;
6537	data = *addr;
6538#if 1
6539	if (data == 0xffffffff) {
6540#else
6541	if ((data & 0xe2f0fffd) != 0x02000080) {
6542#endif
6543		printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
6544			(unsigned) data);
6545		return (0x10);
6546	};
6547	return (0);
6548}
6549#endif
6550
6551static int ncr_snooptest (struct ncb* np)
6552{
6553	u_long	ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc, err=0;
6554	int	i;
6555#ifndef NCR_IOMAPPED
6556	err |= ncr_regtest (np);
6557	if (err) return (err);
6558#endif
6559	/*
6560	**	init
6561	*/
6562	pc  = NCB_SCRIPT_PHYS (np, snooptest);
6563	host_wr = 1;
6564	ncr_wr  = 2;
6565	/*
6566	**	Set memory and register.
6567	*/
6568	ncr_cache = host_wr;
6569	OUTL (nc_temp, ncr_wr);
6570	/*
6571	**	Start script (exchange values)
6572	*/
6573	OUTL (nc_dsp, pc);
6574	/*
6575	**	Wait 'til done (with timeout)
6576	*/
6577	for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
6578		if (INB(nc_istat) & (INTF|SIP|DIP))
6579			break;
6580	/*
6581	**	Save termination position.
6582	*/
6583	pc = INL (nc_dsp);
6584	/*
6585	**	Read memory and register.
6586	*/
6587	host_rd = ncr_cache;
6588	ncr_rd  = INL (nc_scratcha);
6589	ncr_bk  = INL (nc_temp);
6590	/*
6591	**	Reset ncr chip
6592	*/
6593	OUTB (nc_istat,  SRST);
6594	DELAY (1000);
6595	OUTB (nc_istat,  0   );
6596	/*
6597	**	check for timeout
6598	*/
6599	if (i>=NCR_SNOOP_TIMEOUT) {
6600		printf ("CACHE TEST FAILED: timeout.\n");
6601		return (0x20);
6602	};
6603	/*
6604	**	Check termination position.
6605	*/
6606	if (pc != NCB_SCRIPT_PHYS (np, snoopend)+8) {
6607		printf ("CACHE TEST FAILED: script execution failed.\n");
6608		return (0x40);
6609	};
6610	/*
6611	**	Show results.
6612	*/
6613	if (host_wr != ncr_rd) {
6614		printf ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
6615			(int) host_wr, (int) ncr_rd);
6616		err |= 1;
6617	};
6618	if (host_rd != ncr_wr) {
6619		printf ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
6620			(int) ncr_wr, (int) host_rd);
6621		err |= 2;
6622	};
6623	if (ncr_bk != ncr_wr) {
6624		printf ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
6625			(int) ncr_wr, (int) ncr_bk);
6626		err |= 4;
6627	};
6628	return (err);
6629}
6630
6631/*==========================================================
6632**
6633**
6634**	Profiling the drivers and targets performance.
6635**
6636**
6637**==========================================================
6638*/
6639
6640/*
6641**	Compute the difference in milliseconds.
6642**/
6643
6644static	int ncr_delta (struct timeval * from, struct timeval * to)
6645{
6646	if (!from->tv_sec) return (-1);
6647	if (!to  ->tv_sec) return (-2);
6648	return ( (to->tv_sec  - from->tv_sec  -       2)*1000+
6649		+(to->tv_usec - from->tv_usec + 2000000)/1000);
6650}
6651
6652#define PROFILE  cp->phys.header.stamp
6653static	void ncb_profile (ncb_p np, ccb_p cp)
6654{
6655	int co, da, st, en, di, se, post,work,disc;
6656	u_long diff;
6657
6658	PROFILE.end = time;
6659
6660	st = ncr_delta (&PROFILE.start,&PROFILE.status);
6661	if (st<0) return;	/* status  not reached  */
6662
6663	da = ncr_delta (&PROFILE.start,&PROFILE.data);
6664	if (da<0) return;	/* No data transfer phase */
6665
6666	co = ncr_delta (&PROFILE.start,&PROFILE.command);
6667	if (co<0) return;	/* command not executed */
6668
6669	en = ncr_delta (&PROFILE.start,&PROFILE.end),
6670	di = ncr_delta (&PROFILE.start,&PROFILE.disconnect),
6671	se = ncr_delta (&PROFILE.start,&PROFILE.select);
6672	post = en - st;
6673
6674	/*
6675	**	@PROFILE@  Disconnect time invalid if multiple disconnects
6676	*/
6677
6678	if (di>=0) disc = se-di; else  disc = 0;
6679
6680	work = (st - co) - disc;
6681
6682	diff = (np->disc_phys - np->disc_ref) & 0xff;
6683	np->disc_ref += diff;
6684
6685	np->profile.num_trans	+= 1;
6686	if (cp->xfer)
6687	np->profile.num_bytes	+= cp->xfer->datalen;
6688	np->profile.num_disc	+= diff;
6689	np->profile.ms_setup	+= co;
6690	np->profile.ms_data	+= work;
6691	np->profile.ms_disc	+= disc;
6692	np->profile.ms_post	+= post;
6693}
6694#undef PROFILE
6695
6696/*==========================================================
6697**
6698**
6699**	Device lookup.
6700**
6701**	@GENSCSI@ should be integrated to scsiconf.c
6702**
6703**
6704**==========================================================
6705*/
6706
6707#ifndef NEW_SCSICONF
6708
6709struct table_entry {
6710	char *	manufacturer;
6711	char *	model;
6712	char *	version;
6713	u_long	info;
6714};
6715
6716static struct table_entry device_tab[] =
6717{
6718#ifdef NCR_GETCC_WITHMSG
6719	{"", "", "", QUIRK_NOMSG},
6720	{"SONY", "SDT-5000", "3.17", QUIRK_NOMSG},
6721	{"WangDAT", "Model 2600", "01.7", QUIRK_NOMSG},
6722	{"WangDAT", "Model 3200", "02.2", QUIRK_NOMSG},
6723	{"WangDAT", "Model 1300", "02.4", QUIRK_NOMSG},
6724#endif
6725	{"", "", "", 0} /* catch all: must be last entry. */
6726};
6727
6728static u_long ncr_lookup(char * id)
6729{
6730	struct table_entry * p = device_tab;
6731	char *d, *r, c;
6732
6733	for (;;p++) {
6734
6735		d = id+8;
6736		r = p->manufacturer;
6737		while ((c=*r++)) if (c!=*d++) break;
6738		if (c) continue;
6739
6740		d = id+16;
6741		r = p->model;
6742		while ((c=*r++)) if (c!=*d++) break;
6743		if (c) continue;
6744
6745		d = id+32;
6746		r = p->version;
6747		while ((c=*r++)) if (c!=*d++) break;
6748		if (c) continue;
6749
6750		return (p->info);
6751	}
6752}
6753#endif
6754
6755/*==========================================================
6756**
6757**	Determine the ncr's clock frequency.
6758**	This is important for the negotiation
6759**	of the synchronous transfer rate.
6760**
6761**==========================================================
6762**
6763**	Note: we have to return the correct value.
6764**	THERE IS NO SAVE DEFAULT VALUE.
6765**
6766**	We assume that all NCR based boards are delivered
6767**	with a 40Mhz clock. Because we have to divide
6768**	by an integer value greater than 3, only clock
6769**	frequencies of 40Mhz (/4) or 50MHz (/5) permit
6770**	the FAST-SCSI rate of 10MHz.
6771**
6772**----------------------------------------------------------
6773*/
6774
6775#ifndef NCR_CLOCK
6776#	define NCR_CLOCK 40
6777#endif /* NCR_CLOCK */
6778
6779
6780static void ncr_getclock (ncb_p np)
6781{
6782	u_char	tbl[5] = {6,2,3,4,6};
6783	u_char	f;
6784	u_char	ns_clock = (1000/NCR_CLOCK);
6785
6786	/*
6787	**	Compute the best value for scntl3.
6788	*/
6789
6790	f = (2 * MIN_SYNC_PD - 1) / ns_clock;
6791	if (!f ) f=1;
6792	if (f>4) f=4;
6793	np -> ns_sync = (ns_clock * tbl[f]) / 2;
6794	np -> rv_scntl3 = f<<4;
6795
6796	f = (2 * MIN_ASYNC_PD - 1) / ns_clock;
6797	if (!f ) f=1;
6798	if (f>4) f=4;
6799	np -> ns_async = (ns_clock * tbl[f]) / 2;
6800	np -> rv_scntl3 |= f;
6801	if (DEBUG_FLAGS & DEBUG_TIMING)
6802		printf ("%s: sclk=%d async=%d sync=%d (ns) scntl3=0x%x\n",
6803		ncr_name (np), ns_clock, np->ns_async, np->ns_sync, np->rv_scntl3);
6804}
6805
6806/*=========================================================================*/
6807#endif /* KERNEL */
6808
6809
6810