Google
 

Trailing-Edge - PDP-10 Archives - BB-X116A-BB_1984 - sclink.mac
There are 20 other files named sclink.mac in the archive. Click here to see a list.
;DNET:SCLINK.MAC[10,36,MON,NEW], 05-Oct-1983 04:04:28, Edit by TARL
;MCO 10994 - Fix loop node stuff.
;DNET:SCLINK.MAC[10,36,MON,NEW], 22-Sep-1983 20:55:00, Edit by TARL
;MCO 10971 - Grand Re-merge of the -10/-20 sources
;TITLE SCLINK - Session Control Layer Control for DECnet-36
	SUBTTL V.Brownell & W.Nichols/Tarl
	SEARCH D36PAR,SCPAR,MACSYM
	SALL

	ENTRY SCTL,SCTINI

;THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY ONLY BE USED
;  OR COPIED IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE.
;
.CPYRT<
COPYRIGHT (C) 1984 BY DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS.
>


IFN FTOPS20,<
	SEARCH PROLOG
	TTITLE SCLINK,,< - Session Control Layer for DECnet-36>
	>

IFN FTOPS10,<
	SEARCH F,S
	TITLE SCLINK - Session Control Layer Control for DECnet-36
	>

	D36SYM			;SET UP D36 SPECIFIC PARAMETERS

	$RELOC
	$HIGH			;START IN HIGHSEG
	SUBTTL	Table of Contents


;		Table of Contents for SCLINK
;
;
;			   Section			      Page
;   1. Table of Contents. . . . . . . . . . . . . . . . . . .    2
;   2. Definitions
;        2.1.   External References . . . . . . . . . . . . .    3
;        2.2.   Accumulators. . . . . . . . . . . . . . . . .    5
;   3. Macros
;        3.1.   Code-Generating
;             3.1.1.     IFxSTATE . . . . . . . . . . . . . .    6
;             3.1.2.     NEWSTATE . . . . . . . . . . . . . .    7
;   4. SCTINI - Session Control Initialization. . . . . . . .    8
;   5. SCTPSQ - Grant a PSI interrupt . . . . . . . . . . . .    9
;   6. SCTSTM - Check Active Connect Timers for Expiration. .   10
;   7. SCTJIF - Jiffy Service for Session Control . . . . . .   11
;   8. SCTRFJ - Request Jiffy Service for a Link. . . . . . .   14
;   9. SCTL's Interlock
;        9.1.   Queuing Version . . . . . . . . . . . . . . .   15
;        9.2.   Waiting Version . . . . . . . . . . . . . . .   16
;        9.3.   Flag Version. . . . . . . . . . . . . . . . .   17
;  10. SCTULK - SCTL's Interlock
;       10.1.   Unlock Checks . . . . . . . . . . . . . . . .   18
;  11. NSP.
;       11.1.   Function Dispatch Table . . . . . . . . . . .   19
;       11.2.   Wait Check Tables
;            11.2.1.     Definitons . . . . . . . . . . . . .   20
;       11.3.   Wait check tables
;            11.3.1.     "Before" NSP. table. . . . . . . . .   21
;            11.3.2.     "After" NSP. table . . . . . . . . .   22
;            11.3.3.     Routines . . . . . . . . . . . . . .   23
;       11.4.   SCTNSF - The Entry to SCLINK from SCMUUO. . .   24
;       11.5.   NSFRE - REset all links . . . . . . . . . . .   30
;       11.6.   NSFEA - Enter Active State. . . . . . . . . .   31
;       11.7.   NSFEP - Enter Passive State . . . . . . . . .   34
;       11.8.   NSFRI - Read Connect Information. . . . . . .   35
;       11.9.   NSFAC - Accept the Connect. . . . . . . . . .   36
;       11.10.  NSFRJ - Reject the Connect. . . . . . . . . .   38
;       11.11.  NSFRC - Read Connect Confirm Information. . .   39
;       11.12.  NSFSD - Synchronous Disconnect. . . . . . . .   40
;       11.13.  NSFAB - Abort and Release . . . . . . . . . .   42
;       11.14.  NSFRD - Read Disconnect Data. . . . . . . . .   43
;       11.15.  NSFRL - Release the Channel . . . . . . . . .   44
;       11.16.  NSFRS - Read the Channel Status . . . . . . .   45
;       11.17.  NSFIS - Send Interrupt Data . . . . . . . . .   46
;       11.18.  NSFIR - Receive Interrupt Data. . . . . . . .   48
;       11.19.  NSFDS - Send Normal Data. . . . . . . . . . .   50
;       11.20.  NSFDR - Receive Normal Data . . . . . . . . .   54
;       11.21.  NSFSQ - Set Quotas and Goals. . . . . . . . .   57
;       11.22.  NSFRQ - Read Quotas and Goals . . . . . . . .   58
;       11.23.  NSFJS - Set Job Quotas and Goals. . . . . . .   59
;       11.24.  NSFJR - Read Job Quotas and Goals . . . . . .   60
;       11.25.  NSFPI - Set PSI Reason Mask . . . . . . . . .   61
;  12. RETFLW - Return current flow controls. . . . . . . . .   62
;  13. SNDDRQ - Send Data Requests. . . . . . . . . . . . . .   63
;  14. RLSLNK - Release a Link. . . . . . . . . . . . . . . .   64
;  15. LLINKS Calls
;       15.1.   Entry Vector Table. . . . . . . . . . . . . .   65
;       15.2.   SCTRIB - Reserve Input Buffer . . . . . . . .   66
;       15.3.   SCTUCG - Uncongestion Call. . . . . . . . . .   67
;       15.4.   SCTLCI - Connect Initiate Call. . . . . . . .   68
;       15.5.   The Vectored Call Entry Point SCTL. . . . . .   71
;       15.6.   SCCCR - Connect Confirmed call from NSP . . .   72
;       15.7.   SCDIR - Disconnect Initiate received call . .   73
;       15.8.   SCDCR - Disconnect Confirm received call. . .   74
;       15.9.   SCODN - Output done call. . . . . . . . . . .   75
;       15.10.  SCSEG - Segment received call . . . . . . . .   76
;       15.11.  SCDRQ - Data request received call. . . . . .   77
;       15.12.  SCNCF - No confidence in port call. . . . . .   78
;       15.13.  SCNRS - No resources call . . . . . . . . . .   79
;       15.14.  SCCLS - Close Completed call. . . . . . . . .   80
;       15.15.  SCNLK - No link call. . . . . . . . . . . . .   81
;       15.16.  SCNCM - No communication call . . . . . . . .   82
;       15.17.  SCNRN - Not in Run State call . . . . . . . .   83
;       15.18.  SCCAK - Got a Connect ACK . . . . . . . . . .   84
;  16. Subroutines
;       16.1.   CHKABO - Check SLABO flag . . . . . . . . . .   85
;       16.2.   FREMSG - Free a message block . . . . . . . .   86
;       16.3.   CONBUF - Invoke Conservative Buffering. . . .   87
;       16.4.   FNDSLB - Find SLB given a channel number. . .   88
;       16.5.   FNDSBI - Find SLB from SLBid. . . . . . . . .   89
;       16.6.   FRESLB - Deallocate a SLB . . . . . . . . . .   90
;       16.7.   FRECBP - Free Connect Block . . . . . . . . .   91
;       16.8.   CDMCBP - Copy User Data from Message Block. .   92
;       16.9.   CPMSCB - Copy User Data from Message Block. .   93
;       16.10.  FRESJB - Deallocate a SJB . . . . . . . . . .   94
;       16.11.  MAKSLB - Create a SLB, filling in defaults. .   95
;       16.12.  MAKSJB - Create a SJB, filling in defaults. .   97
;       16.13.  SCSSTS/CHKSTS - Set Status & Inform PSISER. .   98
;  17. SCTWKQ - Queue a Link for later call to SCTPSQ . . . .  100
;  18. Subroutines
;       18.1.   SLBMAT - Pattern match connect to passive SLBs 101
;       18.2.   CDBMAT - Match Two Connect Blocks . . . . . .  102
;       18.3.   STRMAT - Pattern Matcher. . . . . . . . . . .  105
;       18.4.   STPTMR - Stop the Connect Initiate Timer. . .  106
;       18.5.   STRTMR - Start the Connect Initiate Timer . .  107
;       18.6.   TMRREJ - Send a Reject with Reason RSNNRO . .  108
;       18.7.   BLDCTX - Build connect message. . . . . . . .  109
;       18.8.   PRSCTX - Parse a connect message. . . . . . .  113
;       18.9.   SCTA2N - Do node address to node name mapping  117
;       18.10.  SCTN2A - Do node name to node address mapping  118
;       18.11.  SCTAND - Add a node to the node name table. .  119
;       18.12.  CPYS2M - Copy string block to message segment  120
;       18.13.  CPYM2S - Copy message data to string block. .  121
;  19. Network Management
;       19.1.   SCEIVM - Report Invalid Message Event . . . .  122
;  20. Local SCTL Storage . . . . . . . . . . . . . . . . . .  123
;  21. End of SCLINK. . . . . . . . . . . . . . . . . . . . .  124
	SUBTTL	Definitions -- External References

;These are the external references to the D36COM library of routines.

	EXT DNGNBF		;GET TOTAL NUMBER OF BUFFERS IN SYSTEM

	EXT DNGINI		;INITIALIZE FOR INPUT (DGXYBY)
	EXT DNPINI		;INITIALIZE FOR OUTPUT (DPXYBY)
	EXT DNPINR		;REINITIALIZE FOR OUTPUT (DPXYBY)

	EXT DNP1BY		;PUT ONE BYTE IN MESSAGE
	EXT DNP2BY		;PUT TWO BYTES IN MESSAGE
	EXT DNPEBY		;PUT AN EXTENSIBLE BYTE IN MESSAGE

	EXT DNG1BY		;GET ONE BYTE FROM MESSAGE
	EXT DNG2BY		;GET TWO BYTES FROM MESSAGE
	EXT DNGEBY		;GET EXTENSIBLE BYTE FROM MESSAGE

	EXT DNGMSG		;GET DECNET-36 MESSAGE BLOCK
	EXT DNFMSG		;FREE MESSAGE BLOCK
	EXT DNMINI		;REUSE A MESSAGE BLOCK

	EXT DNLENG		;GET THE MESSAGE LENGTH
	EXT DNSLNG		;FIND THE SEGMENT LENGTH

	EXT DNGWDS		;GET SOME WORDS
	EXT DNGWDZ		;GET SOME ZEROED WORDS
	EXT DNFWDS		;FREE SOME WORDS
	EXT DNSWDS		;SMEAR SOME WORDS

	EXT DNBKBY		;GO BACKWARDS SOME BYTES
	EXT DNSKBY		;SKIP SOME BYTES

	EXT DNSMRK		;SET MARK IN MESSAGE
	EXT DNGMRK		;POSITION AT MARK IN MESSAGE

	EXT DNLMSS		;LINK MESSAGE SEGMENT INTO MESSAGE BLOCK

	EXT DNCPYW		;COPY WORDS WITH BLT OR XBLT

	EXT DNCM2U		;COPY MESSAGE TO USER BUFFER
	EXT DNCU2M		;COPY USER BUFFER TO MESSAGE
	EXT DNCM2B		;COPY MESSAGE TO MONITOR BUFFER
	EXT DNCB2M		;COPY MONITOR BUFFER TO MESSAGE

	EXT DNGTIM		;GET CURRENT TIME IN MS

	EXT DCNTSB		;TOTAL SYSTEM BUFFERS, USED AND UNUSED
	EXT DCNCON		;NON-ZERO IF SYSTEM IS CONGESTED
	EXT DCNRSB		;RESERVED BUFFERS, MODIFIED UNDER D36OFF
				; TO PROTECT AGAINST SCTRIB CALL FROM LLINKS
	EXT DCNRHT		;HIGH TIDE FOR DCNRSB
	EXT DCNRIF		;INPUT RESERVATION FAILURES
	EXT DCNROF		;OUTPUT RESERVATION FAILURES

;Entries into NSP.

	EXT NSP			;THE MAIN ENTRY TO NSP
	EXT NSPINI		;INITIALIZATION ROUTINE FOR NSP
	EXT NSPEVT		;SIGNAL AN NSP-LEVEL EVENT, USING EVENT MACRO
				; FROM SCPAR.UNV

;Here are some external references to system things.

	EXT RTN			;RETURN
	EXT RSKP		;SKIP RETURN

;Externals for TOPS10 interlock management

IFN FTOPS10,<			;EXTERNAL FOR SMP INTERLOCK BREAKER
	EXT SCTLOK		;INTERLOCK WORD
	EXT SCTLKO		;INTERLOCK OWNER WORD
>

;External data.

	EXT %SCHDR		;MAX LENGTH OF HEADERS BELOW SCTL
	EXT RTRBSZ		;CELL CONTAINING ROUTER'S BUFFER SIZE
	EXT %SCP2Q		;"PHASE II" QUOTA
	EXT %SCINT		;DEFAULT INCOMING TIMER VALUE
	EXT %SCOTT		;DEFAULT OUTGOING TIMER VALUE
	EXT %RTMXN		;MAX-NODES
	SUBTTL	Definitions -- Accumulators

;These are some local AC defintions for Session Control.

	SL=FREE1		;SL POINTS TO THE CURRENT SC LINK BLOCK
	SA=FREE2		;SA POINTS TO THE ARGUMENT BLOCK
	PURGE FREE1,FREE2	;WE DON'T NEED THIS SYMBOL ANYMORE
	SUBTTL	Macros -- Code-Generating -- IFxSTATE

;Use:	AC/ One of the .NSSxx state codes
;
;a)	IFSTATE AC,<OP,CC,RJ>
;	  executed if match found
;b)	IFSTATE AC,<OP,CC,RJ>,LABEL    (Go there if match found)

DEFINE IFSTATE(AC,STATES,GLABEL),<IFST1(AC,<STATES>,<GLABEL>,N,GE,L)>


;Use:	AC/ One of the .NSSxx state codes
;
;a)	IFNSTATE AC,<OP,CC,RJ>
;	  executed if match NOT found
;b)	IFNSTATE AC,<OP,CC,RJ>,LABEL    (Go there if match NOT found)

DEFINE IFNSTATE(AC,STATES,GLABEL),<IFST1(AC,<STATES>,<GLABEL>,E,L,GE)>

DEFINE IFST1(AC,STATES,GLABEL,MOD1,MOD2,MOD3),<
	ZZ==0
	ZZCNT==0
	IRP STATES,<ZZ==ZZ ! 1B<.NSS'STATES>
		    ZZCNT==ZZCNT+1>
  IFE ZZCNT-1,<
	CAI'MOD1 AC,.NSS'STATES
	IFNB <GLABEL>,<
		IFIDN <GLABEL>,<RTN>,<RET>
		IFDIF <GLABEL>,<RTN>,<JRST GLABEL>
	>>
  IFG ZZCNT-1,<			;;IF MORE THAN ONE STATE TESTED
	MOVX CX,ZZ
	ROT CX,(AC)
	IFB  <GLABEL>,< SKIP'MOD2 CX >
	IFNB <GLABEL>,< JUMP'MOD3 CX,GLABEL >
	>
PURGE ZZCNT,ZZ
>
	SUBTTL	Macros -- Code-Generating -- NEWSTATE

;NEWSTATE will change the state of the link and call SCSSTS to tell
;PSISER to possibly let the user know about the change.

DEFINE NEWSTATE(suffix),<
	MOVX T1,.NSS'suffix	;;SET UP STATE
	CALL SCSSTS		;;LET PSISER KNOW ABOUT IT
	TRACE SC,New State: suffix
>
	SUBTTL	SCTINI - Session Control Initialization

;SCTINI - Session Control Initialization
;
; Call:
;	T1/ Our local node number
;	T2/ Flags:
;		XP%PH2 - This "NSP and SC" can handle Phase II
;
; Return:
;	RET			;COULDN'T INITIALIZE DUE TO RESOURCE FAILURE
;	RETSKP			;INITIALIZED
;
; Uses: T1-T6

SCTINI::SAVEAC <MB,MS,P1,P2,T5,T6>
	TRACE SC,<Initializing Session Control>

	MOVE T3,RTRBSZ		;ROUTER'S BUFFER SIZE IN CASE ITS BEEN POKED
	SUBI T3,%SCHDR		;LESS MAX HEADERS BELOW SCTL
	MOVEM T3,SCTSIZ		;STORE AS MAX MESSAGE SEGMENT SIZE

;Initialize NSP, which will then initialize Transport.

	SKIPL T1		;DO A QUICKIE RANGE
	CAMLE T1,SCTMXN		; CHECK ON THE NODE NUMBER
	BUG.(CHK,SCLBNS,SCLINK,SOFT,<Bad Node Number in SC Initialization>,,<

Cause:	The node number compiled into this monitor image is either
	negative or it is larger than the max node number compiled in
	too.

Cure:	Either change the node number or gen a larger net to include
	this monitor's node number.

>,RTN)
	MOVE T3,T2		;NSP WANTS FLAGS IN T3
	CALL NSPINI		;CALL NSP AND TRANSPORT
	 RET			;FAILED TO INITIALIZE

;Set up the node address to name mapping database.

	MOVE T1,SCTMXN		;GET MAX NODES
	ADDI T1,1		;ONE EXTRA (FOR ZEROTH NODE)
	CALL DNGWDZ		;GET THAT MANY WORDS
	 RET			;RESOURCE FAILURE
	MOVEM T1,SCTNDT		;SAVE THE POINTER TO THE NODE TABLE
	RETSKP			;ALL IS WELL
	SUBTTL SCTPSQ - Grant a PSI interrupt

;SCTPSQ - Grant a PSI interrupt for user
;
; Call:		T1/ SJB Pointer
;
; Return:
;		RET		;IF NOTHING IN QUEUE
;		RETSKP		;IF FOUND AN SLB QUEUED
;			T1+T2/ STRUCTURE PS, SEE SCPAR
;
; Uses:
;Note that this routine is not under the interlock

;Note that if we use T5 or T6 in this routine, we have to save
;them for caller.

SCTPSQ::D36OFF			;#AVOID PROBLEMS WITH SLPSI FLAG
	DEQUE T4,SJ.PSQ(T1),SL.NXP,SCTPQ1 ;#ANYTHING ON THE PSI Q?
	SETZRO SLPSI,(T4)	;#NOT ON THE PSI Q ANY MORE
	D36ON

IF1,IFN PS.MOR,<PRINTX SCTPSQ needs PSMOR to be in T1>

	MOVE T3,T1		;GET POINTER TO SJB
	LOAD T1,SLSST,(T4)	;NO INTERLOCK, DON'T CALL CHKSTS
	STOR T1,PSSTS,+T1	;STORE FOR CALLER (IN T2)
	TMNE QHBEG,+SJ.PSQ(T3)	;MORE SLBS ON PSI Q?
	TXO T1,PSMOR		;YES, SET THE MORE FLAG FOR CALLER
	LOAD T3,SLCHN,(T4)	;GET CHANNEL NUMBER
	STOR T3,PSCHN,+T1
	LOAD T3,SLPSM,(T4)	;GET PSI MASK
	STOR T3,PSPSM,+T1

	RETSKP			;SUCCESS RETURN


;Here if DEQUE found nothing

SCTPQ1:	D36ON
	RET
	SUBTTL	SCTSTM - Check Active Connect Timers for Expiration

;SCTSTM - Check active connect timers for expiration
;
; Call:
;	With nothing special
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCTSTM:	SAVEAC <SA,SL,P1,P2>	;SAVE THE JOB AND LINK BLOCK POINTERS
	LOAD SA,QHBEG,+SCTSJQ	;POINT TO THE FIRST SJB
	TRNA
SCTST1:	LOAD SA,SJNXT,(SA)	;GET THE NEXT SJB IN LIST
	JUMPE SA,RTN		;NO MORE, JUST RETURN
	JE SJCTA,(SA),SCTST1	;IF THERE ARE NO TIMERS ACTIVE FOR JOB
				; JUST LOOK AT THE NEXT GUY
	LOAD P1,SJCHC,(SA)	;GET THE COUNT OF ENTRIES IN SLB TABLE
	LOAD P2,SJCHT,(SA)	;POINT TO THE SLB TABLE

SCTST2:	SKIPN SL,(P2)		;DO WE HAVE AN SLB?
	JRST SCTST3		;NO, CHECK THE NEXT ONE
	JN <SLFSL,SLLBC>,(SL),SCTST3 ;YES, IGNORE IF ALREADY CLOSING
	LOAD T1,SLSTA,(SL)  	;GET THE STATE
	IFSTATE T1,CR,SCTST4	;CHECK THE INCOMING CONNECT TIMER
	IFSTATE T1,CS,SCTST5	;CHECK THE OUTGOING CONNECT TIMER

SCTST3:	ADDI P2,1		;INCREMENT SLB POINTER
	SOJG P1,SCTST2		;ANY MORE LEFT?
	JRST SCTST1		;NO, CHECK THE NEXT JOB

;Here with a SLB in CR state.  Check the incoming timer for expiration.

SCTST4:	CALL DNGTIM		;GET THE CURRENT TIME
	OPSTR <SUB T1,>,SLCTM,(SL) ;FIND TIME SINCE TIMER STARTED
	CAMGE T1,SCTINT		;HAS IT EXPIRED?
	JRST SCTST3		;NO, LOOK AT NEXT SLB
	CALL TMRREJ		;YES, SEND REJECT
	  JRST SCTST3		;ALLOCATION FAILURE, LEAVE TIMER RUNNING
	CALLRET SCTST6		;REJECT, STOP TIMERS AND CHECK THE NEXT ONE

;Here with a SLB in CS state.  Check the outgoing timer for expiration.

SCTST5:	CALL DNGTIM		;GET THE CURRENT TIME
	OPSTR <SUB T1,>,SLCTM,(SL) ;FIND TIME SINCE TIMER STARTED
	CAMGE T1,SCTOTT		;HAS OUTGOING TIMER EXPIRED?
	JRST SCTST3		;NO, LOOK AT NEXT SLB
	MOVX T1,RSNNRO		;YES, GET 'NO RESPONSE FROM OBJECT' REASON CODE
	STOR T1,SLRSN,(SL)	;STORE REASON FOR INTERESTED CALLER
	NEWSTATE RJ		;TELL USER LINK IS REJECTED
SCTST6:	CALL STPTMR		;STOP THE TIMERS
	JRST SCTST3		; AND CHECK THE NEXT ONE
	SUBTTL SCTJIF - Jiffy Service for Session Control

;SCTJIF - Jiffy Service for Session Control
;
; Call:
;	with nothing, every jiffy
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCTJIF::TMNN QHBEG,+SCTJFQ	;ANY SLB QUEUED FOR SERVICE?
	SOSLE SCTJLC		; OR HAS THE LONG INTERVAL EXPIRED?
	TRNA			;YES, GET THE INTERLOCK
	RET			;NO, JUST RETURN
				;YES,
	SEC1			;RUN ALL OUR JUNK IN SECTION 1
	SAVEAC <MB,MS,T5,T6,SL,SA>
	SETOM SCTJFF		;TELL SCTULK WE NEED JIFFY SERVICE
	CALLRET SCTLCF		;GET INTERLOCK, CALL SCTJIX


SCTJIX:	SAVEAC <P1,P2>
	SKIPG SCTJLC		;HAS THE LONG COUNTER EXPIRED?
	CALL [	MOVE T1,SCTJLI	;YES, GET INITIAL VALUE FOR LONG TIME
		MOVEM T1,SCTJLC	; AND RESET THE COUNTER
		CALLRET SCTSLJ]	;CALL THE LONG TIME PROCESSOR

	CALLRET SCTSSJ		;CALL THE SHORT TIME (JIFFY) PROCESSOR
;Here every long interval

SCTSLJ:	SKIPE SCTCTA		;ARE THERE ANY OUTSTANDING CONNECTS?
	CALLRET SCTSTM		;SCAN FOR RUNNING CONNECT TIMERS
	RET
;Here every short interval

;We must not continue processing after getting a no-resources return
;because the procedure which gave that return put the current port
;back on the jiffy-request queue.  If we were to continue along that
;queue, we would visit that port over and over forever.

SCTSSJ:
SCTSS1:!DEQUE SL,SCTJFQ,SL.JFQ,RTN ;RETURN WHEN Q EMPTY
	SETZRO SLJFR,(SL)	;NO LONGER ON THE JIFFY-REQ Q
	TMNN SLFSL,(SL)		;SKIP IF SLB NEEDS TO BE FREED
	JRST SCTSS2		;NO, SEE IF WE NEED DRQ CHECK
	CALL SCTSFR		;YES, GET RID OF IT
	  CALLRET SCTRFJ	;LEAVE, REQUESTING SERVICE AGAIN
	JRST SCTSS1		; AND CHECK THE NEXT CHANNEL

SCTSS2:	OPSTR <SKIPE T1,>,SLDRR,(SL) ;ARE THERE ANY DATA REQUESTS?
	CALL SCTJDR		;YES, RESEND THE DATA REQUESTS
	  CALLRET SCTRFJ	;LEAVE, REQUESTING SERVICE AGAIN
	JRST SCTSS1		;GET NEXT SLB THAT NEEDS SERVICE




;Here to free an SLB that we are finished with

SCTSFR:	JN SLBSY,(SL),RTN	;CAN'T FREE IT NOW, TRY LATER
	CALL FRESLB		;CLEAN UP THAT ONE.
	RETSKP			;SUCCESS

;Here with a SLB that needs to have DRQs resent.

SCTJDR:	TXZ T2,MBOTH		;ON THE NORMAL SUB-LINK
	CALL SNDDRQ		;SEND THOSE DATA REQUESTS
	  RET			;CAN'T, LET CALLER HANDLE THIS
	SETZRO SLDRR,(SL)	;WE SENT THE DATA REQUESTS
	RETSKP			; AND RETURN
	SUBTTL	SCTRFJ - Request Jiffy Service for a Link

;SCTRFJ - Request Jiffy Service for a Link
;
; Call:
;	SL/ The SLB for which service is requested
;
; Return:
;	RET			;ALWAYS WITH ALL TEMP ACS SMASHED
;
; Uses: T1

SCTRFJ:	MOVX T1,SLJFR		;GET QUEUED-FOR-SERVICE FLAG
	TDNE T1,SL.JFR(SL)	;ALREADY QUEUED?
	RET			;YES, DON'T Q AGAIN
	IORM T1,SL.JFR(SL)	;NO, WILL BE NOW THOUGH
	ENDQUE SL,SCTJFQ,SL.JFQ,T1
	RET
	SUBTTL	SCTL's Interlock -- Queuing Version

;SCTLCQ - Interlock routine for interrupt level callers
;
; Call:
;	T1/ Address of processor to call with the interlock
;	MB/ Pointer to Message Block with arguments stored
;		or zero if called from SCTJIF
;
; Return:
;	RET			;ALWAYS WITH ALL TEMP ACS SMASHED
;
; Uses: T1
;
;If it could not get the interlock, it queues the message block with its
;arguments.  When the interlock is given up, the queue is checked and
;if it has a message on it, it will service it.

SCTLCQ:	STOR T1,MBPRC,(MB)	;SAVE THE PROCESSING PROCEDURE

	D36OFF			;GET GLOBAL INTERLOCK FOR ENDQUE BELOW
	AOSN SCTLOK		;TEST AND SET THE INTERLOCK
	JRST SCTLQ1		;GO PROCESS, NOW THAT WE HAVE THE LOCK

;Here when we have to queue the message and dismiss.

	ENDQUE MB,SCTPRQ,MB.NXT,T1 ;QUEUE THE MESSAGE
	D36ON			;TURN INTERLOCK OFF
	RET

;Here to process the message now.

SCTLQ1:	APRID SCTLKO		;SET THE OWNER OF THE INTERLOCK
	D36ON			;TURN INTERLOCK OFF
IFN FTOPS20,<
	CONSO PI,1B<DLSCHN+^D20> ;AT DTE PI LEVEL?
	CSKED			;NO, ASSURE WE GET THROUGH THIS QUICKLY
>;END IFN FTOPS20		;MUSTN'T CALL CSKED AT INTERRUPT LEVEL
	LOAD T1,MBAR1,(MB)	;GET THE FIRST ARGUMENT
	LOAD T2,MBAR2,(MB)	; SECOND
	LOAD T3,MBAR3,(MB)	; THIRD
	OPSTR <CALL @>,MBPRC,(MB) ;GET THE PROCESSOR

	CALLRET SCTULK		;DO THE UNLOCK CHECKS
	SUBTTL	SCTL's Interlock -- Waiting Version

;SCTLCW - Interlock routine for process level callers
;
; Call:
;	T1/ Address of processor to call with the interlock
;	MB/ Pointer to Message Block with arguments stored
;
; Return:
;	RET			;ALWAYS WITH ALL TEMP ACS SMASHED
;
; Uses: T1
;
;This version of the interlock will spin on SCTLOK until it is freed.
;This is to be called only from process level, thus it will only spin
;if another process or interrupt level user has it on another processor.
;It will not fight with any other process level users on this processor
;since only one can be active on any one processor at any one time.
;
;Note that SCTLOK must be uncached if this is a multiprocessor system.

SCTLCW::
	SAVEAC <MB,SA>		;TEMPORARY UNTIL AC-TRASHING BUG FOUND.

;Even though we don't really have to store the routine address in
;the message block here, we do because its a convenient debugging tool.

	STOR T1,MBPRC,(MB)	;SAVE THE PROCESSING PROCEDURE

IFN FTOPS10,<
SCTLW1:	AOSE SCTLOK		;TEST AND SET THE INTERLOCK
	JRST SCTLW1		;ITS LOCKED, SPIN UNTIL ITS FREED
	APRID SCTLKO		;SET THE OWNER OF THE INTERLOCK
>;END IFN FTOPS10		; CAN ONLY BE COMPETING WITH ANOTHER
				; PROCESSOR ON TOPS10
IFN FTOPS20,<
SCTLW1:	AOSN SCTLOK		;TEST AND SET THE INTERLOCK
	JRST SCTLW3		;ITS OK, GO USE IT
	SKIPE INSKED		;IN USE, ARE WE AT SCHEDULAR LEVEL?
	JRST SCTLW2		;YES
	XMOVEI T1,SCTLWB	;NO, SET UP SCHEDULAR TEST
	MDISMS			;WAIT FOR COMPETING PROCESS TO FINISH
	JRST SCTLW1		;OK, LETS TRY AGAIN

SCTLWB:	SKIPL SCTLOK		;IS SESSION CONTROL LOCK FREE YET?
	JRST (T4)		;NO, SLEEP ON
	JRST 1(T4)		;YES, WAKE UP FORK

SCTLW2:	BUG.(CHK,SCTBWK,SCLINK,SOFT,<SCTNSF call from sched w/o lock>,,<

Cause:	The DECnet entry point SCTNSF has been called from schedular
	level when the Session Control interlock was locked.

Action:	All schedular level routines which call SCTNSF should first
	check SCTLOK.  If SCTLOK is not -1, then the caller should
	wait for the next schedular cycle before calling SCTNSF.
>)
	  RET			;LET CALLER WORRY ABOUT NO DECNET ACTION

SCTLW3:
>;END IFN FTOPS20

;Here to process the message now.

IFN FTOPS20,<
	CONSO PI,1B<DLSCHN+^D20> ;AT DTE PI LEVEL?
	CSKED			;NO, ASSURE WE GET THROUGH THIS QUICKLY
>;END IFN FTOPS20		;MUSTN'T CALL CSKED AT INTERRUPT LEVEL
	LOAD T1,MBAR1,(MB)	;GET THE FIRST ARGUMENT
	LOAD T2,MBAR2,(MB)	; SECOND
	LOAD T3,MBAR3,(MB)	; THIRD
	OPSTR <CALL @>,MBPRC,(MB) ;GET THE PROCESSOR

	CALLRET SCTULK		;DO THE UNLOCK CHECKS
	SUBTTL	SCTL's Interlock -- Flag Version

;SCTLCF - Interlock routine for Flagged Routines
;
; Call:
;	T1/ Address of processor to call with the interlock
;	SCTJFF or SCTUCF set non-zero for SCTULK
;
; Return:
;	RET			;ALWAYS WITH ALL TEMP ACS SMASHED
;
; Uses: T1
;

SCTLCF:

;We have no message block here, so we cannot store any processor
;address.  Caller has set a SCTxxF flag instead.
;
;All we have to do here is call the unlocker, for it will check
;the Service flags SCTJFF and SCTUCF.
;
	AOSE SCTLOK		;TEST AND SET THE INTERLOCK
	RET			;NOT FREE, LEAVE NOW
IFN FTOPS20,<
	CONSO PI,1B<DLSCHN+^D20> ;AT DTE PI LEVEL?
	CSKED			;NO, ASSURE WE GET THROUGH THIS QUICKLY
>;END IFN FTOPS20		;MUSTN'T CALL CSKED AT INTERRUPT LEVEL
	APRID SCTLKO		;SET THE OWNER OF THE INTERLOCK
	CALLRET SCTULK		;AND DO THE UNLOCK CHECKS.
	SUBTTL	SCTULK - SCTL's Interlock -- Unlock Checks

;SCTULK - All interlock routines call SCTULK to unlock
;
; Call:
;	No args in registers
;	Called with the Session Control Interlock still on
;
; Return:
;	RET			;ALWAYS WITH ALL TEMP ACS SMASHED
;
; Uses: T1
;

SCTULK:	SAVEAC <MB,MS,SL>	;SAVE SOME ACS

;It is OK to have the window between SKIPE SCTJFF and SETZM SCTJFF
;since the worst that can happen is that we lose a jiffy interrupt.
;If we are so close to the next jiffy when we respond to the last one
;that we fall into the window, its just as well not to introduce more
;overhead by closing the window!

SCTUL1:	SETZ T1,		;LOAD UP A ZERO TO TO TURN FLAG OFF
	EXCH T1,SCTJFF		;NEED TO DO
	SKIPE T1		; CLOCK LEVEL STUFF?
	CALL SCTJIX		;YES, GIVE JIFFY SERVICE

	SETZ T1,		;LOAD UP A ZERO TO TO TURN FLAG OFF
	EXCH T1,SCTUCF		;NEED TO DEAL WITH
	SKIPE T1		; CONGESTION RELIEF?
	CALL SCIUCG		;YES, DO SO

	D36OFF			;TURN OF THE INTERRUPTS
	DEQUE MB,SCTPRQ,MB.NXT,SCTUL2 ;DEQUEUE MSG FROM PROCESS Q
	D36ON			;UNDO THE D36OFF

	LOAD T1,MBAR1,(MB)	;GET THE FIRST ARGUMENT
	LOAD T2,MBAR2,(MB)	; SECOND
	LOAD T3,MBAR3,(MB)	; THIRD
	OPSTR <CALL @>,MBPRC,(MB) ;GET THE PROCESSOR
	JRST SCTUL1		;PROCESS THE NEXT ONE

SCTUL2:	SETOM SCTLKO		;CLEAR THE INTERLOCK OWNER
	SETOM SCTLOK		;TURN OFF THE INTERLOCK
	D36ON			;UNDO THE D36OFF
IFN FTOPS20,<
	CONSO PI,1B<DLSCHN+^D20> ;AT DTE PI LEVEL?
	ECSKED			;NO, UNDO THE CSKED WE DID BEFORE
>;END IFN FTOPS20		;MUSTN'T CALL ECSKED AT INTERRUPT LEVEL
	RET			; AND RETURN
	SUBTTL	NSP. -- Function Dispatch Table

;Define some macros to set up the dispatch table for the NSP. UUO functions.

DEFINE INIDSP(PREFIX,LABEL,START<0>,FSTNAM<MN>),<
	PREFIX''FSTNAM==START
	%%%CTR==START-1

DEFINE DSP(NAME),<
	%%%CTR==%%%CTR+1	;;INCREMENT THE COUNTER
	IFN <%%%CTR-PREFIX''NAME>,<
		PRINTX ?Function PREFIX''NAME in SCLINK not in
		PRINTX ? the same order as in SCPAR.UNV
		PASS2
		END>

	IFIW LABEL''NAME>	;;ENTRY POINT FOR INDIRECT CALL

DEFINE ENDDSP(MAXNAM<MX>),<
	PREFIX''MAXNAM==%%%CTR>
>

;This is the table which is used to find the entry vector to for a specific
;NSP. function.   Symbols are also defined in the for .NSFxx for each of the
;function types.

NSFNT:				;START OF THE TABLE

INIDSP .NSF,NSF,0
	DSP RE			;RESET ALL CHANNELS
	DSP EA			;ENTER ACTIVE STATE
	DSP EP			;ENTER PASSIVE STATE
	DSP RI			;READ CONNECT INFORMATION
	DSP AC			;ACCEPT THE CONNECT
	DSP RJ			;REJECT THE CONNECT
	DSP RC			;READ CONNECT CONFIRM INFORMATION
	DSP SD			;SYNCHRONOUS DISCONNECT
	DSP AB			;ABORT
	DSP RD			;READ DISCONNECT DATA
	DSP RL			;RELEASE THE CHANNEL
	DSP RS			;READ THE CHANNEL STATUS
	DSP IS			;SEND INTERRUPT DATA
	DSP IR			;RECEIVE INTERRUPT DATA
	DSP DS			;SEND NORMAL DATA
	DSP DR			;RECEIVE NORMAL DATA
	DSP SQ			;SET QUOTAS
	DSP RQ			;READ QUOTAS
	DSP JS			;SET JOB QUOTAS
	DSP JR			;READ JOB QUOTAS
	DSP PI			;SET PSI REASON MASK
ENDDSP
	SUBTTL	NSP. -- Wait Check Tables -- Definitons

;The following definitions are for the wait check tables.  The tables
;are used to find out if session control should wait for a condition
;when the user has set the NS.WAI bit.  Each table is indexed by
;function number and returns in T1 a value which means one of:

	XP CW.ERR,0		;GIVE THE USER A UNEXPECTED STATE ERROR
	XP CW.NWA,1		;DON'T BOTHER WAITING
	XP CW.WAI,2		;WAIT FOR SOME MORE TIME
	XP CW.SAT,3		;WAIT FOR SATISFYING COND BEFORE PROCEEDING
	XP CW.DAT,4		;WAIT FOR MORE DATA TO BE SENT

BEGSTR CF			;SEE CHKFOR, BELOW
	FIELD IFI,1		;THE SIGN BIT IS RESERVED FOR IFIW FLG
	FILLER 11		;ROOM FOR SOME FUNCTION FLAGS
	FIELD CHN,1		;SET IF A CHANNEL IS REQ'D FOR THIS FCN
	HWORD TST		;LOCAL ADDRESS OF TESTER FUNCTION
ENDSTR

	XP CHNREQ,1		;CHANNEL REQUIRED FOR FUNCTION
	XP NOCHAN,0		;NO CHANNEL REQ'D


;Define some macros to set up the table.

DEFINE INITAB(START<0>),<
	%%%CTR==START-1

DEFINE CHKFOR(func,reqchn,tester),<
	%%%CTR==%%%CTR+1

IFN <%%%CTR-.NSF'func>,<
	PRINTX ?NSP. Function .NSF'func in SCLINK not in
	PRINTX ? the same order as in SCPAR.UNV
	PASS2
	END>

;;The following doubtful format is designed to prevent MACRO from
;;going Polish and thus encountering its bugs.
	IFIW reqchn,tester	;SEE BEGSTR CF, ABOVE
>

DEFINE ENDTAB,<
	PURGE CHKFOR,...TST
>>

;CHKSTA checks for one of STATES, returning successfully if in WAISTA
;and the wait bit is off.

DEFINE CHKSTA(SUCSTA,WAISTA),<[IFSTATE T2,<SUCSTA>,NOWAIT
	IFNB <WAISTA>,<	       IFSTATE T2,<WAISTA>,WAITMORE >
		 	       JRST BLEWIT]
>

;CHKSTE checks for one of STATES, returning an error if in WAISTA
;and the wait bit is off.

DEFINE CHKSTE(SUCSTA,WAISTA),<[	IFSTATE T2,<SUCSTA>,NOWAIT
	IFNB <WAISTA>,<		JE SAWAI,(SA),BLEWIT
				IFSTATE T2,<WAISTA>,WAITMORE >
				JRST BLEWIT]
>

;CHKBIT checks for on of the .NSxxx status bits.
;CONDSTATES are conditional states, in which we will allow a data or
;interrupt read only if there is something queued now, else the state
;is illegal.  We don't want the poor user who sets the wait bit to hang
;in DR or DC state forever!

DEFINE CHKBIT(BIT,STATES,CONDSTATES),<[
	 IFB <STATES>,<PRINTX ?No STATE(s) declared for CHKBIT macro>
	 IFB <CONDSTATES>,IFNSTATE T2,<STATES>,BLEWIT
	 IFNB <CONDSTATES>,IFNSTATE T2,<STATES,CONDSTATES>,BLEWIT
		 	    TXNE T1,NS'BIT
	 	     	    JRST NOWAIT
	 IFNB <CONDSTATES>,IFNSTATE T2,<STATES>,BLEWIT
	 		    JRST WAITMORE]
>
	SUBTTL	NSP. -- Wait check tables -- "Before" NSP. table

;The following is the wait table which is checked before the NSP.
;function is called.  This table is indexed by function type and
;returns CW.xxx as explained above.

SCTWTB:

INITAB
	CHKFOR RE,NOCHAN,NOWAIT
	CHKFOR EA,NOCHAN,NOWAIT
	CHKFOR EP,NOCHAN,NOWAIT
	CHKFOR RI,CHNREQ,NOWAIT
	CHKFOR AC,CHNREQ,CHKSTA(CR)	;SUCCESS STATE,WAITMORE STATE
	CHKFOR RJ,CHNREQ,CHKSTA(CR)	;SUCCESS STATE,WAITMORE STATE
IFN FTOPS10,<CHKFOR RC,CHNREQ,CHKSTE(RN,CS)> ;ERROR IF CS STATE AND NS.WAI=0
IFN FTOPS20,<CHKFOR RC,CHNREQ,NOWAIT>
	CHKFOR SD,CHNREQ,CHKSTA(RN)	;SUCCESS STATE,WAITMORE STATE
	CHKFOR AB,CHNREQ,CHKSTA(RN)	;SUCCESS STATE,WAITMORE STATE
IFN FTOPS10,<CHKFOR RD,CHNREQ,CHKSTA(<DR,RJ>)> ;SUCCESS STATE,WAITMORE STATE
IFN FTOPS20,<CHKFOR RD,CHNREQ,NOWAIT>
	CHKFOR RL,CHNREQ,NOWAIT
	CHKFOR RS,CHNREQ,NOWAIT
	CHKFOR IS,CHNREQ,CHKBIT(IDR,<CS,RN>)
	CHKFOR IR,CHNREQ,CHKBIT(IDA,<CS,RN,DS>,<DR,DC>)
	CHKFOR DS,CHNREQ,CHKBIT(NDR,<CS,RN>)
	CHKFOR DR,CHNREQ,CHKBIT(NDA,<CS,RN,DS>,<DR,DC>)
	CHKFOR SQ,CHNREQ,NOWAIT
	CHKFOR RQ,CHNREQ,NOWAIT
	CHKFOR JS,CHNREQ,NOWAIT
	CHKFOR JR,CHNREQ,NOWAIT
	CHKFOR PI,CHNREQ,NOWAIT
ENDTAB
	SUBTTL	NSP. -- Wait check tables -- "After" NSP. table

;The following is the wait table which is checked after the NSP. function
;is called.  This table is indexed by function type and returns CW.xxx

SCTWTA:

INITAB
	CHKFOR RE,NOCHAN,NOWAIT
	CHKFOR EA,CHNREQ,CHKSTA(RN,CS)	;SUCCESS STATE,WAITMORE STATE
	CHKFOR EP,CHNREQ,CHKSTA(CR,CW)	;SUCCESS STATE,WAITMORE STATE
	CHKFOR RI,CHNREQ,NOWAIT
	CHKFOR AC,CHNREQ,CHKSTA(RN)	;SUCCESS STATE,WAITMORE STATE
	CHKFOR RJ,CHNREQ,NOWAIT
	CHKFOR RC,CHNREQ,CHKSTA(RN,CS)	;SUCCESS STATE,WAITMORE STATE
	CHKFOR SD,CHNREQ,CHKSTA(DC,DS)	;SUCCESS STATE,WAITMORE STATE
	CHKFOR AB,CHNREQ,CHKSTA(DC,DS)	;SUCCESS STATE,WAITMORE STATE
	CHKFOR RD,CHNREQ,NOWAIT
	CHKFOR RL,CHNREQ,NOWAIT
	CHKFOR RS,CHNREQ,NOWAIT
	CHKFOR IS,CHNREQ,NOWAIT
	CHKFOR IR,CHNREQ,NOWAIT
	CHKFOR DS,CHNREQ,WAITDAT
	CHKFOR DR,CHNREQ,WAITSAT
	CHKFOR SQ,CHNREQ,NOWAIT
	CHKFOR RQ,CHNREQ,NOWAIT
	CHKFOR JS,CHNREQ,NOWAIT
	CHKFOR JR,CHNREQ,NOWAIT
	CHKFOR PI,CHNREQ,NOWAIT
ENDTAB
	SUBTTL	NSP. -- Wait check tables -- Routines

;The following routines perform the correct action for the function.  They
;are dispatched to by the "before" and "after" tables.

;Don't wait any longer.

NOWAIT:	MOVX T1,CW.NWA
	RET

;Error (possibly expected) occured.

BLEWIT:	MOVX T1,CW.ERR
	RET

;Wait some more.

WAITMO:	MOVX T1,CW.WAI
	RET

;Loop to do it again.

WAITSAT:
	MOVX T1,CW.SAT
	RET

;Wait if we are to get more data.

WAITDAT:
	MOVX T1,CW.DAT
	RET
	SUBTTL	NSP. -- SCTNSF - The Entry to SCLINK from SCMUUO

;SCTNSF - Perform a NSP. UUO function
;
; Call:
;	T1/ Pointer to Message Block in SA format (See BEGSTR SA in D36PAR)
;
; Return:
;	RET			;ALWAYS WITH SJERR CONTAINING A ZERO FOR SUC.
;				; AND A NON-ZERO ERROR CODE FOR FAILURES
;
; Uses: T1-T6
;
;We return the updated user arguments in the SJB (or error code on a
;failure).
;
;Note:  It is assumed that SCJSYS, or whatever equivalent, has range
;and privilege checked all of the user arguments.  The arguments are
;all in either the SAB portion of the message block or in internal
;blocks pointed to by the SAB (SACBP and SASBP).

SCTNSF::SAVEAC <MB,MS,SL,SA,T5,T6,P1,P2> ;SAVE ALMOST ALL ACS
	SKIPG SA,T1		;DID HE SUPPLY A MESSAGE BLOCK?
	BUG.(CHK,SCLSNM,SCLINK,SOFT,<No Message Block supplied to SCTNSF>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	TMNN SASJB,(SA)		;HAVE WE GOT AN SJB?
	BUG.(CHK,SCLSBJ,SCLINK,SOFT,<Bad Job Block Pointer Passed to SCTNSF>,,<

Cause:	This BUG is not documented yet.

>,RTN)

	SETZRO SAERR,(SA)	;INITIALIZE THE ERROR LOCATION

	LOAD T1,SAAFN,(SA)	;GET THE FUNCTION
	CAIL T1,.NSFMN		;RANGE CHECK
	CAILE T1,.NSFMX		; THE FUNCTION CODE
	SCERR %NEILF,RTN,<Illegal function>
	;...
	;...

;The code on this page and the following page is a series of coroutines.  The
;Session Control interlock will only interlock a whole routine (to reduce the
;possibility of interlock screwups).  The routines on this page call
;coroutines on the following page (only) via the Session Control interlock.

;We start on this page, not owning the Session Control interlock.  The first
;interlocked coroutine called is SCTNIS (Setup).  SCTNIS will do what it can,
;then, when it needs to do something outstide of the Session Control
;interlock, it will put the address of one of the labels on this page into
;MBPRC,(SA) and then RET.  The RET takes us back to SCTULK (Session Control
;unlock) where we process any Session Control requests which may have been
;queued.  Then control returns to this page after the original coroutine call.
;Each of the coroutines on this page has OPSTR <CALLRET @>,MBPRC,(SA) after
;its call the the Session Control interlock.

;Control leaves this page either via a coroutine call to an interlocked
;routine on the next page or by a RET to SCTNSF's caller.

	MOVE MB,SA		;INTERLOCK ROUTINE NEEDS MB SET UP
	XMOVEI T1,SCTNIS	;INTERLOCKED SETUP ROUTINE
	CALL SCTLCW		;CALL INTERLOCKED SETUP ROUTINE
	OPSTR <CALLRET @>,MBPRC,(SA) ;CALL COROUTINE CHOSEN BY SCTNIS

;Here to HIBER without the interlock before calling the BEFORE-function
;coroutine (again).

SCTNS1:	LOAD T1,SASJB,(SA)	;PASS PTR TO SJB IN T1 TO @SAHBA(SA)
	LOAD T2,SAACH,(SA)	;PASS CHANNEL # IN T2
	MOVE T3,SA		;PASS SAB ADDR IN T3 (SIGH)
	OPSTR <CALL @>,SAHBA,(SA) ;CALL THE HIBER'ER
	MOVE MB,SA		;MB MUST BE SET UP FOR INTERLOCK
	XMOVEI T1,SCTNIB	;CALL THE "BEFORE" INTERLOCKED PROCESS
	CALL SCTLCW		; AFTER WE GET THE INTERLOCK
	OPSTR <CALLRET @>,MBPRC,(SA) ;CALL COROUTINE CHOSEN BY SCTNIB

;Here to HIBER without the interlock before calling the AFTER-function
;coroutine (again).

SCTNS2:	LOAD T1,SASJB,(SA)	;PASS PTR TO SJB IN T1 TO @SAHBA(SA)
	LOAD T2,SAACH,(SA)	;PASS CHANNEL # IN T2
	MOVE T3,SA		;PASS SAB ADDR IN T3 (SIGH)
	OPSTR <CALL @>,SAHBA,(SA) ;CALL THE HIBER'ER
	MOVE MB,SA		;MB MUST BE SET UP FOR INTERLOCK
	XMOVEI T1,SCTNIA	;CALL THE "AFTER" INTERLOCKED PROCESS
	CALL SCTLCW		; AFTER WE GET THE INTERLOCK
	OPSTR <CALLRET @>,MBPRC,(SA) ;CALL COROUTINE CHOSEN BY SCTNIA

;Here to give up the interlock and call the system schedular to give others
;a chance between segments of a long message.

SCTNS3:
IFN FTOPS10,<
	CALL INTLVL##		;ARE WE AT UUO LEVEL?
	  CALL SCDCHK##		;YES, LET OTHER USERS HAVE A CHANCE
>;END IFN FTOPS10
	MOVE MB,SA		;MB MUST BE SET UP FOR INTERLOCK
	XMOVEI T1,SCTNIB	;CALL THE "BEFORE" PROCESS AGAIN
	CALL SCTLCW		; AFTER WE GET THE INTERLOCK
	OPSTR <CALLRET @>,MBPRC,(SA) ;CALL COROUTINE CHOSEN BY SCTNIB
;All of these routines have the interlock.  These routines may only go
;back to non-interlocked code by the return address in MBPRC.

;Setup the SLB.

SCTNIS:	LOAD T1,SAAFN,(SA)	;GET THE FUNCTION CODE
	SETO SL,		;ASSUME WE HAVE NO SLB (-1 IS 'UNASSIGNED')
	JE CFCHN,+SCTWTB(T1),SCNIS1 ;IF NO CHANNEL IS REQ'D,
				    ; LEAVE SL AS 'UNASSIGNED' (-1)
	LOAD T1,SAACH,(SA)	;GET THE CHANNEL NUMBER
	LOAD T2,SASJB,(SA)	;POINT TO THE JOB BLOCK
	CALL FNDSLB		;FIND THE CORRESPONDING SLB
	  SCERR %NEBCN,SCTNIE,<Bad Channel Number>
	SETONE SLBSY,(SL)	;NO ONE MAY FREE THIS SLB
SCNIS1:	STOR SL,SASLB,(SA)	;STORE THE SLB POINTER
	JRST SCNIB1		;WE ALREADY HAVE THE INTERLOCK

;Do the BEFORE-function state checking and perform the function.

SCTNIB:	LOAD SL,SASLB,(SA)	;GET POINTER TO LINK BLOCK
SCNIB1:	JUMPL SL,[SETZB T1,T2	;NO STATUS IF NO SLB (EA & EP FUNCTS)
		  JRST SCNIB3]	;GO SEE IF WE CAN MAKE ONE
SCNIB2:	LOAD T1,SLSST,(SL)	;GET CURRENT LINK STATUS
	LOAD T2,NSSTA,+T1	;PUT JUST THE STATE IN T2
SCNIB3:	LOAD T3,SAAFN,(SA)	;GET THE FUNCTION BACK
	CALL @SCTWTB(T3)	;SEE IF WE SHOULD WAIT A WHILE
	CALLRET @.+1(T1)	;BRANCH ON SCTWTB'S RETURN CODE
	   IFIW SCTNIU		;CW.ERR - ERROR RETURN TO MONUSER
	   IFIW SCNIB4		;CW.NWA - NO WAIT, GO DO FUNCTION
	   IFIW SCNIB5		;CW.WAI - WAIT IF MONUSER SO REQUESTED

;Here to call the function processor

SCNIB4:	LOAD T1,SAAFN,(SA)	;GET THE FUNCTION TYPE
	CALL @NSFNT(T1)		;CALL FUNCTION PROCESSOR
	  JRST SCTNIE		;REPORT THE ERROR IF NEEDED
	STOR SL,SASLB,(SA)	;SAVE THE POSSIBLY NEW SLB FOR AFTER CHECKING
	JRST SCNIA1		;DON'T GET INTERLOCK, WE HAVE IT

;Here when function not yet complete to see if we should wait

SCNIB5:	TMNN SAWAI,(SA)		;MONUSER ASK US TO WAIT FOR FCN COMPLETION?
	JRST SCTNIX		;NO, RETURN TO MONUSER NOW
	XMOVEI T2,SCTNS1	;YES, DO OUR HIBER
	STOR T2,MBPRC,(SA)	;STORE COROUTINE ADDRESS
	RET			;BACK TO NON-INTERLOCKED CODE
;Here to do the AFTER-function checking.

SCTNIA:	LOAD SL,SASLB,(SA)	;GET POINTER TO LINK BLOCK
SCNIA1:	JUMPL SL,SCTNX1		;IF WE DON'T HAVE A SLB, JUST LEAVE

;Note that none of the NSFxx routines call CHKSTS when they change
;a status other than the link state, because they assume that we will
;call CHKSTS from here.

	CALL CHKSTS		;UPDATE SLB'S LINK STATUS WORD
	LOAD T1,SLSST,(SL)	;GET CURRENT LINK STATUS
	LOAD T2,NSSTA,+T1	;PUT JUST THE STATE IN T2
	LOAD T3,SAAFN,(SA)	;GET THE FUNCTION CODE BACK
	CALL @SCTWTA(T3)	;SEE IF WE SHOULD WAIT A WHILE
	CALLRET @.+1(T1)	;BRANCH ON ANSWER FROM SCTWTA
	   IFIW SCTNIU		;CW.ERR - ERROR RETURN REQUIRED
	   IFIW SCTNIX		;CW.NWA - FCN COMPLETE, RETURN TO MONUSER
	   IFIW SCNIA2		;CW.WAI - FCN NOT COMPLETE, WAIT
	   IFIW SCNIA3		;CW.SAT - CHECK FOR I/O SATISFIED
	   IFIW SCNIA4		;CW.DAT - BRIEF PAUSE BETWEEN MSG SEGMENTS

;Here if function not yet complete, see if we have to wait

SCNIA2:	TMNN SAWAI,(SA)		;DID MONUSER ASK TO WAIT FOR FCN TO COMPLETE?
	JRST SCTNIX		;NO, RETURN TO USER NOW
	XMOVEI T1,SCTNS2	;YES, POINT TO HIBER ROUTINE, OUT-OF-INTERLOCK
	STOR T1,MBPRC,(SA)	;TELL CO-ROUTINE WHERE TO GO TO HIBER
	RET			; AND RETURN TO TO HIBER

;Here to see if .NSFDR function is complete, wait if so requested

SCNIA3:	TMNN SAWAI,(SA)		;MONUSER WANT US TO WAIT FOR I/O COMPLETE?
	JRST SCTNIX		;NO, RETURN TO MONUSER NOW
	TMNN SASAT,(SA)		;YES, IS THE I/O SATISFIED YET?
	JRST SCNIB2		;NO, GO BACK AND TRY AGAIN
	JRST SCTNIX		;YES, RETURN TO MONUSER NOW

;Here to free the Session Control interlock briefly between message segments
;so that we don't keep the lock too long when sending long messages.

SCNIA4:	TMNE SASAT,(SA)		;I/O SATISFIED YET?
	JRST SCTNIX		;YES, RETURN TO CALLER NOW
	LOAD T1,SLSST,(SL)	;NO, CAN WE SEND ANOTHER SEGMENT
	TXNE T1,NSNDR		; NOW? (ONLY NORMAL DATA CAN HAVE MULT SEGS)
	JRST SCNIA5		;YES
	TMNN SAWAI,(SA)		;NO, USER WANT TO BLOCK?
	JRST SCTNIX		;NO, RETURN TO USER NOW
	JRST SCNIB2		;YES, GET BEFORE PROCESSOR TO BLOCK

SCNIA5:	XMOVEI T1,SCTNS3	;RELEASE INTERLOCK A BIT
	STOR T1,MBPRC,(SA)	; THEN DO THE NEXT SEGMENT
	RET			;RETURN FROM THE SCTL INTERLOCK

;Here to exit with the normal return.

SCTNIX:	SETZRO SAAST,(SA)	;START WITH NO STATUS SO THAT A RESET
				; FUNCTION WILL RETURN A ZERO AS STATUS
	JUMPL SL,SCTNX1		;IF WE DON'T HAVE AN SLB, PUNT
	LOAD T1,SLSST,(SL)	;GET CURRENT LINK STATUS
	STOR T1,SAAST,(SA)	;STORE STATUS FOR USER AS WE LEAVE
	SETZRO SLBSY,(SL)	;WE'RE DONE WITH SLB NOW, IF NEED BE
				; IT MAY BE FREED
SCTNX1:	XMOVEI T1,RTN		;JUST RETURN WHEN DONE
	STOR T1,MBPRC,(SA)	;STORE THAT AS THE PROCEDURE
	RET			;RETURN TO OTHER CO-ROUTINE
;Here to report the Unexpected State errors.  We will give the error
;based upon the state that was unexpected.

SCTNIU:	SKIPG SL		;IS SL KOSHER?
	BUG.(CHK,SCLISU,SCLINK,SOFT,<Illegal SL at SCTINU>,,<

Cause:	After a Session Control function was complete, register SL
	no longer held a value SLB pointer.

Cure:	Find what routine in SCLINK smashed SL.

>,SCTNIE)
	CALL CHKSTS		;UPDATE SLB'S LINK STATUS WORD
	LOAD T1,SLSST,(SL)	;GET CURRENT LINK STATUS
	LOAD T2,NSSTA,+T1	;GET THE STATE
	CAXG T2,.NSSMX		;STATE CODE TOO LARGE?
	SKIPG T2		;NO, TOO SMALL?
	BUG.(CHK,SCLZST,SCLINK,SOFT,<Illegal state value at SCTNIU>,,<

Cause:	The SCLINK error handler has found that the state code in this
	link's SLB is illegal.  This is just a gateway check to see if
	something somewhere in SCLINK is messed up.

Cure:	Hard to know; find the function code in the SAB and trace the
	probable flow from that function code.

>,SCTNUE)
	IFSTATE T2,<DR,RJ>,SCTNIR ;IF WE'RE IN DR OR RJ GIVE REASON ERROR
				; MESSAGE
	ADJBP T2,ERRSBP		;INDEX INTO UNEXPECTED STATE CODE TABLE
	LDB T1,T2		;GET THE VALUE OF THE ERROR MESSAGE
	JUMPE T1,SCTNUE		;JUMP IF UNEXPECTED STATE
	CALLRET SCTNIE		;REPORT THE ERROR TO USER

SCTNUE:	SCERR %NEUXS,SCTNIE,<Unexpected State: Unspecified>


;Here to give the error codes which correspond to the REASON
;codes given by NSP.

SCTNIR:	LOAD T3,SLRSN,(SL)	;GET THE REASON CODE NSP GAVE US
	JUMPE T3,SCTNR1		;IF REASON IS ZERO, WE HAVE TO FIGURE
				;OUT ERROR BASED ON STATE
	CAILE T3,RSNLEN		;IS THE REASON VALUE REASONABLE?
	JRST SCTNR2		;NO, CALL IT UNSPECIFIED
	MOVE T1,ERRRBP		;GET THE REASON ERROR TABLE BYTE POINTER
	ADJBP T3,T1		;INDEX TO THE CORRECT MESSAGE
	LDB T1,T3		;GET THE VALUE OF THE ERROR MESSAGE
	JUMPE T1,SCTNR2		;IF TABLE SAYS ZERO, IT UNSPECIFIED
	CALLRET SCTNIE		;RETURN THE FAILURE TO THE USER

SCTNR1:	IFSTATE T2,RJ
	  SCERR %NERBO,SCTNIE,<Rejected by Object>
	IFSTATE T2,DR
	  SCERR %NEDBO,SCTNIE,<Disconnected by Object>
SCTNR2:	SCERR %NEREJ,SCTNIE,<Unspecified Reject Reason>


;Here with an error code in T1.

SCTNIE:	SKIPN T1		;IS THERE AN ERROR CODE?
	BUG.(CHK,SCLNZE,SCLINK,SOFT,<Passing zero error code to SCMUUO>,,<

Cause:	Arrived at the routine which is supposed to store an error code
	for the user and we find that the error code is zero:  an illegal
	value.

Cure:	Find who called SCTNIE with T1/ 0 and correct the caller's behavior.

>)
	STOR T1,SAERR,(SA)	;STORE THE ERROR TYPE
	SKIPLE SL		;DON'T UPDATE STATUS IF WE HAVE NO SLB
	CALL CHKSTS		;UPDATE SLB'S LINK STATUS WORD
	CALLRET SCTNIX		;FREE THE MESSAGE
;The following is the table (indexed by SC state) which contains the
;error code to give (i.e., %NSUDR = Unexpected State:  Disconnect
;Received).

ERRSTB:	BYTE(6)	%NEUXS,	%NEUCW,	%NEUCR,	%NEUCS,	%NEURJ,	%NEURN
	BYTE(6) %NEUDS,	%NEUDC,	%NEUCF,	%NEULK,	%NEUCM,	%NEUNR

ERRSBP:	POINT 6,ERRSTB		;BYTE POINTER TO ERROR MESSAGES


;This table is indexed by reason code, with each cell corresponding to
;the correct error message to give for that reason.  A zero in the
;table means "return the Unspecified Reject Reason error code".
;This should only happen when someone gives us a reason that we
;never heard of.

ERRRSB:	BYTE(6)	%NERES,	%NEUNN,	%NERNS,	%NEURO,	%NEIOF,	%NEOTB
	BYTE(6)	0,	%NEABM,	%NEABO,	%NEINF,	%NELNS,	0
	BYTE(6)	0,	0,	0,	0,	0,	0
	BYTE(6)	0,	0,	0,	0,	0,	0
	BYTE(6)	0,	0,	0,	0,	0,	0
	BYTE(6)	0,	0,	0,	%NEACR,	0,	0
	BYTE(6)	0,	%NERNO,	%NENUR,	0,	%NENLK,	%NEDSC
	BYTE(6) %NEIMG

RSNLEN==<.-ERRRSB>*6

ERRRBP:	POINT 6,ERRRSB		;BYTE POINTER TO REASON ERROR MESSAGES
	SUBTTL	NSP. -- NSFRE - REset all links

;NSFRE - REset all links
;
; Call:
;	SA/ Pointer to Session Control Argument Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;This function is only done by the system when the user does a RESET
;UUO.  There are no arguments.

NSFRE:	SAVEAC SL		;PRESERVE SL FOR SCTNI*
	LOAD T1,SASJB,(SA)	;GET POINTER TO SJB
	LOAD P1,SJCHT,(T1)	;GET THE POINTER TO SLB TABLE
	LOAD P2,SJCHC,(T1)	;GET THE COUNT OF LINKS

NSFRE1:	SKIPN SL,(P1)		;IS THERE ANYTHING THERE?
	JRST NSFRE2		;NO, TRY NEXT LINK
	SETZRO SLBSY,(SL)	;THIS LINK IS NO LONGER BUSY
	SETZ MB,		;RESET PASSES NO MSG BLK, NO Q'D INTERLOCKS
	CALL RLSLNK		;(MB)DO A RELEASE ON THIS LINK
NSFRE2:	ADDI P1,1		;POINT TO THE NEXT SLB
	SOJG P2,NSFRE1		;DO THE NEXT ONE, IF THERE ARE ANY LEFT
	RETSKP			;RETURN SUCCESS
	SUBTTL	NSP. -- NSFEA - Enter Active State

;NSFEA - Enter Active State
;
; Call:
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFEA function, the function dependant arguments are:
;
;	SAACH/ Returned: zero on failure, channel # on success
;	SAAA1/ Ptr to connect-block
;	SAAA2/ Segment Size
;	SAAA3/ Flow control mode

NSFEA:	TRACE SC,<Performing Enter Active (.NSFEA)>
	SETZRO SAACH,(SA)	;ASSUME WE CANNOT ASSIGN A CHANNEL
	CALL MAKSLB		;MAKE A SLB
	 SCERR %NEALF,RTN,<Allocation failure>
	STOR T1,SAACH,(SA)	;STORE CHANNEL NUMBER FOR MONUSER
	NEWSTATE CS		;INITIAL STATE IS CONNECT SENT

; Get the Object type and if format 0, store both the source (SLSOB)
; and destination (ROB) object type into the SLB.

	LOAD T3,SACBP,(SA)	;GET POINTER TO CONNECT BLOCK
	XMOVEI T1,CB.SRC(T3)	;GET THE POINTER TO SOURCE PDB
	LOAD T2,PBFOR,(T1)	;GET THE FORMAT TYPE
	CAIE T2,FRM.2		;FORMAT TYPE WITH PPN?
	 IFSKP.
	  CALL CHKPPN		;AND VERIFY THE PPN
	   SCERR %NEPRV,NSFEAC,<No privs to use bad PPN>
	 ELSE.
	 IFN FRM.0,<PRINTX ?FRM.0 MUST BE EQUAL TO 0>
	 ANDE. T2		;IF OBJECT TYPE, STORE THE NUMBER
	  LOAD T3,SLSJB,(SL)	;GET POINTER TO SJB
	  LOAD T2,PBOBJ,(T1)	;GET OBJECT TYPE WE CLAIM TO BE
	  TMNN SJPRV,(T3)	;ARE WE PRIVED
	  CAIL T2,^D128		;IS OBJECT TYPE PRIVED?
	  TRNA			;WE ARE O.K.
	   SCERR %NEPRV,NSFEAC,<No privs for this object type>
	  STOR T2,SLSOB,(SL)	;SAVE IT FOR SYSDPY AND THE LIKE
	 ENDIF.
	LOAD T3,SACBP,(SA)	;GET POINTER TO CONNECT BLOCK
	XMOVEI T1,CB.DST(T3)	;GET THE POINTER TO THE DEST PDB
	LOAD T2,PBFOR,(T1)	;GET THE FORMAT TYPE
	CAIE T2,FRM.0		;FORM WITH JUST AN OBJECT TYPE
	 IFSKP.
	  LOAD T2,PBOBJ,(T1)	;GET THE OBJECT TYPE FROM PDB
	  STOR T2,SLROB,(SL)	;SAVE IT FOR SYSDPY AND THE LIKE
	 ENDIF.
	LOAD T1,SAKCB,(SA)      ;GET USER'S 'KEEP CONNECT BLOCK' FLAG
	STOR T1,SLKCB,(SL)      ;REMEMBER IT
	JUMPE T1,NSFEA3         ;JUMP IF WE'RE NOT TO KEEP CONNECT BLOCK
	MOVX T1,CB.LEN		;GET 'CONNECT BLOCK LENGTH' WORDS
	CALL DNGWDS		;GET NON-ZEROED WORDS
  	 SCERR %NEALF,NSFEAC,<Allocation failure>
	STOR T1,SLCBP,(SL)	;SAVE POINTER TO SLB'S NEW CB
				; FRESLB WILL FREE THIS CB FROM NOW ON
	MOVE T2,T1		;DESTINATION IS NEW CB
	LOAD T1,SACBP,(SA)	;SOURCE IS CONNECT BLOCK FROM SAB
	MOVX T3,CB.LEN		;LENGTH IS LENGTH OF A CONNECT BLOCK
	CALL DNCPYW		;COPY SAB'S CB TO SLB'S CB
NSFEA3:
	LOAD P1,SANAG,(SA)	;GET THE NUMBER OF ARGS USER SPECIFIED
	CAIG P1,.NSAA2		;DID THE USER SPECIFIY THE SEGMENT SIZE?
	JRST NSFEA4		;NO

	OPSTR <SKIPLE T1,>,SAAA2,(SA) ;GET THE SEGMENT SIZE
	CAMLE T1,SCTSIZ		;IS IT BIGGER THAN THE LEGAL MAX?
	MOVE T1,SCTSIZ		;NONE OR TOO BIG, USE DEFAULT
	STOR T1,SLSIZ,(SL)	;STORE THE SEGMENT SIZE IN SLB

	CAIG P1,.NSAA3		;DID USER SPECIFY THE FLOW CONTROL MODE?
	JRST NSFEA4		;NO

	LOAD T1,SAAA3,(SA)	;GET THE FLOW CONTROL MODE
	JUMPLE T1,NSFEA4	;DIDN'T SPECIFY, USE THE DEFAULT
	CAIL T1,NSF.C0		;RANGE CHECK THE
	CAILE T1,NSF.MX		; FLOW CONTROL MODE
	SCERR %NEIFM,NSFEAC,<Illegal flow control mode>
	SUBI T1,1		;TRANSLATE FLOW CONTROL MODE INTO THE
				; FCM.XX FORM THAT NSP USES
	STOR T1,SLRFL,(SL)	;STORE THE FLOW CONTROL MODE

NSFEA4:	SETZ T1,		;NO USER DATA NEEDED
	CALL DNGMSG		;GET MESSAGE BLOCK
	 SCERR %NEALF,NSFEAC,<Allocation failure>
	MOVE MB,T1		;PUT MESSAGE BLOCK POINTER IN MB

	MOVX T1,OA.LEN		;LENGTH OF OPEN ARGS
	CALL DNGWDZ		;GET BLOCK FOR OPEN ARGS
	 SCERR %NEALF,NSFEAD,<Allocation failure>
;Now fill in the Open block with the arguments.

	MOVE P1,T1		;MAKE P1 POINT TO THE ARGUMENT BLOCK
	LOAD T1,SLSLB,(SL)	;GET THE SLB ID (WHICH IS POINTER)
	CAME T1,SL		;ARE THEY THE SAME?
	BUG.(CHK,SCLSPM,SCLINK,SOFT,<Self pointers messed up in SLB>,,<

Cause:	This BUG is not documented yet.

>,NSFEAE)
	STOR T1,OASCB,(P1)	;PUT ID IN OPEN ARG BLOCK

	OPSTR <SKIPN T2,>,SACBP,(SA) ;GET THE CONNECT BLOCK POINTER
	BUG.(CHK,SCLNCP,SCLINK,SOFT,<No CBLOCK pointer in SJB>,,<

Cause:	This BUG is not documented yet.

>,NSFEAE)

	LOAD T1,CBNAM,(T2)	;GET NODE NAME
	CALL SCTN2A		;DO NAME TO NODE ADDRESS TRANSLATION
	 JRST [	CALL SCTN2L	;TRY LOOPBACK NODE NAME
		 SCERR %NEUKN,NSFEAE,<Unknown node name>
		STOR T1,OACIR,(P1) ;STORE CIRCUIT ID
		MOVE T1,RTRADR## ;AND GET OUR NODE ADDRESS
		JRST .+1]	;AND CONTINUE
	STOR T1,OANOD,(P1)	;PUT DESTINATION NODE ADDRESS IN ARG BLOCK
	STOR T1,SLDNA,(SL)	; AND STORE AS DESTINATION OF THIS LINK

	LOAD T1,SLRFL,(SL)	;GET THE FLOW CONTROL MODE BACK
	STOR T1,OAFLO,(P1)	;PUT IN OPEN ARG BLOCK
	LOAD T2,SLGOL,(SL)	;GET THE DATA REQUEST GOAL
	CAIN T1,FCM.MG		;ARE WE IN MESSAGE FLOW CONTROL?
	CAIL T2,1		;YES, IS OUR GOAL SET TO LESS THAN ONE?
	TRNA			;NO, DON'T WORRY ABOUT IT
	MOVEI T2,1		;IN MSG FLOW MODE WE GOAL OF AT LEAST 1
	STOR T2,SLGOL,(SL)	;REMEMBER THE GOAL WE DECIDED ON
	STOR T2,OAGOL,(P1)	;TELL LLINKS ABOUT THE GOAL TOO

	LOAD T1,SLSIZ,(SL)	;GET THE MAXIMUM SEGMENT SIZE
	STOR T1,OASIZ,(P1)	;THAT TOO

	XMOVEI T1,SCTL		;HE WANT'S OUR ENTRY VECTOR ADDRESS
	STOR T1,OASCV,(P1)	;GIVE IT TO HIM

	MOVE T1,P1		;PASS ARG POINTER IN T1
	MOVX T2,OA.LEN		;LENGTH OF THE OPEN ARG BLOCK
	MOVX T3,NV.OPN		;FUNCTION IS OPEN A PORT
	MOVE T4,MB		;POINT TO THE MESSAGE BLOCK WE ALLOCATED
	CALL NSP		;ASK NSP TO OPEN A PORT
	JUMPE T1,NSFEAD		;ZERO IN T1 MEANS WE FAILED, GO PUNT
;Now we have an open port to NSP, let's try to make it active.

	STOR T1,SLPID,(SL)	;STORE THE NSPpid

	MOVE T1,MB		;LET'S REUSE THE MESSAGE BLOCK
	MOVEI T2,^D200		;GET ENOUGH ROOM FOR THE CONNECT MESSAGE
	CALL DNMINI		;MAKE THE MESSAGE BLOCK LIKE NEW
	 SCERR %NEALF,NSFEAD,<Allocation failure>

	XMOVEI T1,UD.MSD(MB)	;POINT TO USER DATA PORTION OF MESSAGE
	CALL DNPINI		;INITIALIZE FOR PUTTING BYTES IN MESSAGE

	LOAD T1,SACBP,(SA)	;POINT TO THE CONNECT BLOCK
	CALL BLDCTX		;BUILD THE CONNECT TEXT IN THE MESSAGE
	 SCERR %NECFE,NSFEAD,<Connect Block format error>

	LOAD T1,SLPID,(SL)	;GET THE NSPpid BACK AGAIN
	MOVX T3,NV.ACT		;FUNCTION IS ENTER ACTIVE
	MOVE T4,MB		;POINT TO MB WITH CONNECT DATA
	CALL NSP		;CALL NSP TO DO THE FUNCTION

	CALL STRTMR		;START THE CONNECT TIMERS
	RETSKP			;GIVE A GOOD RETURN

;The following are the error returns that free up things that we have
;allocated before we got the error.

;Free an arg block, a message block and a SLB.

NSFEAE:	SAVEAC T1		;SAVE T1 WITH THE ERROR CODE
	MOVE T1,P1		;POINT TO THE ARGUMENT BLOCK
	CALL DNFWDS		; AND GO AND FREE UP THE WORDS
	JRST NSFED1		;GO AND FREE MESSAGE BLOCK

;Free a message block and a SLB.

NSFEAD:	SAVEAC T1		;SAVE THE ERROR CODE
NSFED1:	CALL FREMSG		;FREE THE MESSAGE BLOCK POINTED TO BY MB
	JRST NSFEC1		;GO AND FREE THE SLB

;Free an SLB.

NSFEAC:	SAVEAC T1		;SAVE THE ERROR CODE
NSFEC1:	SETONE SLFSL,(SL)	;FREE THE SLB AFTER THE "AFTER" PROCESSING
	SETZRO SAACH,(SA)	;TELL USER THERE'S NO CHANNEL OPEN
	CALLRET SCTRFJ		;REQUEST JIFFY SERVICE TO FREE SLB
	SUBTTL	NSP. -- NSFEP - Enter Passive State

;NSFEP - Enter Passive State
;
; Call:
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFEP function, the function dependant arguments are:
;
;	.NSAA1/ Ptr to connect-block

NSFEP:	TRACE SC,<Performing Enter Passive (.NSFEP)>
	CALL MAKSLB		;MAKE A SLB
	 SCERR %NEALF,RTN,<Allocation failure>
	STOR T1,SAACH,(SA)	;STORE THE CHANNEL FOR USER

; Get the Object type and if format 0, store both the source (SLSOB)
; and destination (SLROB) object type into the SLB.

	LOAD T3,SACBP,(SA)	;GET POINTER TO CONNECT BLOCK
	XMOVEI T1,CB.SRC(T3)	;GET THE POINTER TO THE SOURCE PDB
	LOAD T2,PBFOR,(T1)	;GET THE FORMAT TYPE
	 IFE. T2		;IF FRM.0, IT HAS AN OBJECT TYPE
	  LOAD T2,PBOBJ,(T1)	;GET THE OBJECT TYPE FROM THE PDB
	  STOR T2,SLSOB,(SL)	;STORE IT IN THE SLB
	 ENDIF.
	XMOVEI T1,CB.DST(T3)	;GET THE POINTER TO THE DEST PDB
	LOAD T2,PBFOR,(T1)	;GET THE FORMAT TYPE
	CAIE T2,FRM.2		;FORMAT TYPE WITH PPN?
	 IFSKP.
	  CALL CHKPPN		;AND VERIFY THE PPN
	   SCERR %NEPRV,NSFEPC,<No privs to use bad PPN>
	 ELSE.
	 IFN FRM.0,<PRINTX ?FRM.0 MUST BE EQUAL TO 0>
	 ANDE. T2		;IF OBJECT TYPE, STORE THE NUMBER
	  LOAD T3,SLSJB,(SL)	;GET POINTER TO SJB
	  LOAD T2,PBOBJ,(T1)	;GET OBJECT TYPE WE CLAIM TO BE
	  TMNN SJPRV,(T3)	;ARE WE PRIVED
	  CAIL T2,^D128		;IS OBJECT TYPE PRIVED?
	  TRNA			;WE ARE O.K.
	   SCERR %NEPRV,NSFEPC,<No privs for this object type>
	  STOR T2,SLROB,(SL)	;SAVE IT FOR SYSDPY AND THE LIKE
	 ENDIF.

;Now allocate another block for the internal connect block, so that the
;original goes back to SCLINK's caller.

NSFEP2:	LOAD T1,SAKCB,(SA)      ;GET USER'S 'KEEP CONNECT BLOCK' FLAG
	STOR T1,SLKCB,(SL)      ;REMEMBER IT

	MOVX T1,CB.LEN		;LENGTH OF A INTERNAL CONNECT BLOCK
	CALL DNGWDS		;GET THE WORDS
	 SCERR %NEALF,NSFEPC,<Allocation failure>
	STOR T1,SLCBP,(SL)	;STORE THE POINTER IN THE SLB
	MOVE T2,T1		;SET IT UP IN T2 ALSO FOR DNCPYW

	LOAD T1,SACBP,(SA)	;POINT TO THE CONNECT BLOCK
	MOVX T3,CB.LEN		;LENGTH OF THE CONNECT BLOCK
	CALL DNCPYW		;COPY THE WHOLE THING

	NEWSTATE CW		;NEW STATE IS CONNECT WAIT
	RETSKP			;RETURN WITH SUCCESS

;Here to free up the SLB after an error occured.

NSFEPC:	SAVEAC T1		;SAVE THE ERROR CODE
	SETONE SLFSL,(SL)	;SAY THAT WE SHOULD FREE THE SLB
	CALLRET SCTRFJ		;REQUEST JIFFY SERVICE TO FREE SLB
	SUBTTL	NSP. -- NSFRI - Read Connect Information

;NSFRI - Read Connect Information
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFRI function, the function dependant arguments are:
;
;	.NSAA1/ Ptr to connect-block
;	.NSAA2/ Maximum segment size
;	.NSAA3/ Flow control mode
;
;This function can be called in any state.  If we were asked to keep the
;connect in the Enter Active or Enter Passive, then .NSFRI will be able to
;return the connect block at any time, and the user data will have been
;updated with the most recently received user data from CC or DI messages.

NSFRI:	TRACE SC,<Performing Read Connect Info (.NSFRI)>
	SETZRO SASBP,(SA)	;ZERO THE ARGUMENTS IN
	SETZRO SAAA2,(SA)	; CASE WE CAN'T
	SETZRO SAAA3,(SA)	; GET THE RIGHT ANSWER
				;CDMCBP WILL BUILD A CONNECT BLOCK IF NEEDED
	CALL CDMCBP		;COPY ANY MESSAGE DATA TO CONNECT BLOCK
	  RET			;PROPOGATE ERROR RETURN, CODE IN T1
	TMNN SLCBP,(SL)		;ANY DIS/CONNECT DATA TO READ?
	  SCERR %NENCD,RTN,<No dis/connect data to read>


	JE SLKCB,(SL),NSFRI1    ;USER WANT US TO KEEP CONNECT BLOCK

;Here if we are to keep connect block, copy it for user

	MOVX T1,CB.LEN		;YES, LENGTH OF AN INTERNAL CONNECT BLOCK
	CALL DNGWDS		;GET NON-ZEROED WORDS
	  SCERR %NEALF,RTN,<Allocation Failure>
	STOR T1,SACBP,(SA)	;STORE THE POINTER FOR SCJSYS
	MOVE T2,T1		;SET UP DEST POINTER FOR DNCPYW
	LOAD T1,SLCBP,(SL)	;GET THE INTERNAL CB POINTED TO IN THE SLB
	MOVX T3,CB.LEN		;COUNT OF WORDS TO COPY
	CALL DNCPYW		;COPY CONNECT BLOCK FOR USER
	JRST NSFRI2             ;CONTINUE

;Here if we are not to keep the connect block, just hand it over

NSFRI1:	LOAD T1,SLCBP,(SL)	;GET THE INTERNAL CB POINTED TO IN THE SLB
	SETZRO SLCBP,(SL)	;SCLINK NO LONGER OWNS THE CONNECT BLOCK
	SKIPN T1		;DO WE STILL HAVE A CONNECT BLOCK?
	  SCERR %NEWRS,RTN,<Function called in wrong state>
	STOR T1,SACBP,(SA)	;STORE THE POINTER FOR SCMUUO

NSFRI2:	LOAD P1,SANAG,(SA)	;GET THE NUMBER OF ARGUMENTS
	CAIG P1,.NSAA2		;DID HE WANT SOME OTHER ARGUMENTS?
	RETSKP			;NO, JUST RETURN

	LOAD T1,SLSIZ,(SL)	;YES, GET IT FROM THE SLB
	STOR T1,SAAA2,(SA)	;STORE REASON CODE,,SEGMENT SIZE

	CAIG P1,.NSAA3		;HOW ABOUT THE FLOW CONTROL OPTION?
	RETSKP			;NO, JUST RETURN

	CALL RETFLW		;GET THE RFL,,XFL IN T1
	STOR T1,SAAA3,(SA)	; AND STORE IT IN THE ARG BLOCK
	RETSKP			;RETURN SUCCESS
	SUBTTL	NSP. -- NSFAC - Accept the Connect

;NSFAC - Accept the Connect
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFAC function, the function dependant arguments are:
;
;	.NSAA1/ Ptr to user-data (which is the DATA-CTL optional response)
;	.NSAA2/ Maximum segment size
;	.NSAA3/ Flow control mode

NSFAC:	TRACE SC,<Performing Accept Connect (.NSFAC)>

	CALL STPTMR		;STOP THE CONNECT INITIATE TIMER AND FIX COUNTS

	LOAD P1,SANAG,(SA)	;GET THE NUMBER OF ARGS USER SPECIFIED
	CAIG P1,.NSAA2		;DID THE USER SPECIFIY THE SEGMENT SIZE?
	JRST NSFAC1		;NO

	OPSTR <SKIPLE T1,>,SAAA2,(SA) ;GET THE SEGMENT SIZE
	CAMLE T1,SCTSIZ		;IS IT BIGGER THAN THE LEGAL MAX?
	MOVE T1,SCTSIZ		;NONE OR TOO BIG, USE DEFAULT MAX
	OPSTR <CAMGE T1,>,SLSIZ,(SL) ;THIS SMALLER THAN REMOTE WANTS?
	STOR T1,SLSIZ,(SL)	;YES, STORE OUR SEGMENT SIZE IN SLB

	CAIG P1,.NSAA3		;DID USER SPECIFY THE FLOW CONTROL MODE?
	JRST NSFAC1		;NO

	LOAD T1,SAAA3,(SA)	;GET THE FLOW CONTROL MODE
	JUMPLE T1,NSFAC1	;DIDN'T SPECIFY IT, USE THE DEFAULT
	CAIL T1,NSF.C0		;RANGE CHECK THE FLOW
	CAILE T1,NSF.MX		; CONTROL MODE
	SCERR %NEIFM,RTN,<Illegal flow control mode>
	SUBI T1,1		;TRANSLATE FROM NSP.'S VERSION OF FLOW CONTROL
				; VALUE TO NSP'S
	STOR T1,SLRFL,(SL)	;STORE THE FLOW CONTROL MODE
;Allocate two message blocks, one for the accept arguments and one to
;send out data requests with.

NSFAC1:	MOVX T1,^D20		;GET ENOUGH ROOM FOR DATA-CTL
	CALL DNGMSG		;GET A MESSAGE BLOCK
	 SCERR %NEALF,RTN,<Allocation failure>
	MOVE MB,T1		;MAKE MB POINT TO THE MESSAGE BLOCK

	SETZ T1,		;DON'T NEED ANY USER DATA FOR DRQ MB
	CALL DNGMSG		;GET THE MESSAGE BLOCK
	 JRST [CALL FREMSG	;FREE THE MESSAGE BLOCK WE JUST ALLOCATED
	       SCERR %NEALF,RTN,<Allocation failure>] ; AND GIVE A FAILURE TO USER
	MOVE P2,T1		;SAVE THE POINTER TO THE DRQ MB

;Copy the user's DATA-CTL (in the user data area) from the user area to
;the message block.

	XMOVEI T1,UD.MSD(MB)	;WE WANT TO WRITE DATA-CTL INTO USER DATA
	CALL DNPINI		;INITIALIZE THE MSD

	LOAD T1,SASBP,(SA)	;GET THE POINTER TO SBLOCK
	CALL CPYS2M		;COPY THE DATA-CTL TO THE MESSAGE

;Now set up the accept argument block for NSP.

	MOVX T1,AA.LEN		;LENGTH OF THE ACCEPT ARGUMENT BLOCK
	CALL DNGWDZ		;GET THAT MANY WORDS
	 SCERR %NEALF,NSFACD,<Allocation failure>

	LOAD T2,SLSLB,(SL)	;THE SCB ID FOR NEW PORT
	STOR T2,AASCB,(T1)

	LOAD T2,SLPID,(SL)	;NSP'S PORT IDENTIFIER
	STOR T2,AAPID,(T1)

	LOAD T3,SLRFL,(SL)	;RECEIVE FLOW CONTROL TYPE
	STOR T3,AAFLO,(T1)
	LOAD T2,SLGOL,(SL)	;GET THE DATA REQUEST GOAL
	CAIN T3,FCM.MG		;ARE WE IN MESSAGE FLOW CONTROL?
	CAIL T2,1		;YES, IS OUR GOAL SET TO LESS THAN ONE?
	TRNA			;NO, DON'T WORRY ABOUT IT
	MOVEI T2,1		;IN MSG FLOW MODE WE GOAL OF AT LEAST 1
	STOR T2,SLGOL,(SL)	;REMEMBER THE GOAL WE DECIDED ON
	STOR T2,AAGOL,(T1)	;TELL LLINKS ABOUT THE GOAL TOO

	LOAD T2,SLSIZ,(SL)	;MAX BYTES ALLOWED IN A SEGMENT
	STOR T2,AASIZ,(T1)

	XMOVEI T2,SCTL		;OUT ENTRY VECTOR ADDRESS
	STOR T2,AASCV,(T1)	; SO HE KNOWS WHERE TO CALL US

	MOVX T2,AA.LEN		;LENGTH OF THE ARG BLOCK
	MOVX T3,NV.ACC		;FUNCTION IS ACCEPT THE CONNECT
	MOVE T4,MB		;SET UP T4 WITH MB POINTER
				;T1 IS SET UP ALREADY WITH ARG BLOCK ADDRESS
	CALL NSP		;PERFORM THE FUNCTION

	NEWSTATE RN		;NEWSTATE IS CONNECT INITIATE
	SETONE SLCCB,(SL)	;HAVE TO CHECK CONNECT BLOCK NOW

;Now let's send the job's input quota worth of DRQs.

	MOVE MB,P2		;POINT TO THE MB WE ALLOCATED BEFORE

	TMNE SLPH2,(SL)		;SHOULD WE BE CONSERVATIVE WITH THIS FELLOW?
	RETSKP			;YES, WE BETTER LEAVE NICELY

	LOAD T1,SLINQ,(SL)	;GET THE INPUT QUOTA
	STOR T1,QACNT,+T2	; USE THAT AS THE DATA REQUEST COUNT
	STOR T1,SSRDO,+SL.NSL(SL) ;WE KEEP COUNT OF OUTGOING DATA REQUESTS

	TXZ T2,MBOTH		;ON THE NORMAL SUBLINK
	CALL SNDDRM		;SEND THE DATA REQUESTS
	 BUG.(CHK,SCLMIF,SCLINK,SOFT,<DNMINI failed to init msg blk>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	RETSKP			;RETURN SUCCESS

;Here on a error.

NSFACD:	SAVEAC T1		;SAVE THE ERROR CODE
	CALLRET FREMSG		;FREE THE MESSAGE BLOCK POINTED TO BY MB
	SUBTTL	NSP. -- NSFRJ - Reject the Connect

;NSFRJ - Reject the Connect
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFRJ function, the function dependant arguments are:
;
;	.NSAA1/ Ptr to user-data (which is the DATA-CTL optional response)
;	.NSAA2/ Reason code or zero to take default 'rejected by object'

NSFRJ:	TRACE SC,<Performing Reject the Connect (.NSFRJ)>

	CALL STPTMR		;STOP THE CONNECT INITIATE TIMER AND FIX COUNTS

	MOVX T1,^D20		;GET ENOUGH ROOM FOR DATA-CTL
	CALL DNGMSG		;GET A MESSAGE BLOCK
	 SCERR %NEALF,RTN,<Allocation failure>
	MOVE MB,T1		;MAKE MB POINT TO THE MESSAGE BLOCK

	XMOVEI T1,UD.MSD(MB)	;WE WANT TO WRITE DATA-CTL INTO USER DATA
	CALL DNPINI		;INITIALIZE THE MSD

;Put in the reason code (RSNRBO - Rejected by object).

	LOAD T2,SANAG,(SA)	;GET NUMBER OF ARGUMENTS USER OFFERED
	OPSTR <SKIPLE T1,>,SAAA2,(SA) ;USER SPECIFY A REASON?
	CAIGE T2,4		;YES, SANAG INCLUDE SAAA2?
	MOVX T1,RSNRBO		;NO, REJECTED BY THE OBJECT
	CALL DNP2BY		;PUT REASON IN THE MESSAGE

;Copy the user's DATA-CTL (in the user data area) from the user area to the
;message block.

	LOAD T1,SASBP,(SA)	;GET THE POINTER TO SBLOCK
	CALL CPYS2M		;COPY THE DATA-CTL TO THE MESSAGE

	LOAD T1,SLPID,(SL)	;GET THE NSPpid FOR NSP
	MOVX T3,NV.REJ		;FUNCTION IS REJECT THE CONNECT
	MOVE T4,MB		;SET UP T4 WITH MB POINTER
				;T1 IS SET UP ALREADY WITH ARG BLOCK ADDRESS
	CALL NSP		;PERFORM THE FUNCTION

;There is no need to change the state of the link here, as we are
;about to forget about the link.  The user may possibly clean out
;his database about this link and then get the state change interrupt,
;confusing him.

	SETONE SLFSL,(SL)	;SAY THAT WE SHOULD FREE THE SLB LATER
	CALL SCTRFJ		;REQUEST JIFFY SERVICE TO FREE SLB
	RETSKP			;RETURN NICELY
	SUBTTL	NSP. -- NSFRC - Read Connect Confirm Information

;NSFRC - Read Connect Confirm Information
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFRC function, the function dependant arguments are:
;
;	.NSAA1/ Ptr to string block to receive data
;	.NSAA2/ Segment size
;	.NSAA3/ Flow control mode

NSFRC:	TRACE SC,<Performing Read Connect Confirm Data (.NSFRC)>
	SETZRO SASBP,(SA)	;ZERO THE ARGUMENTS IN
	SETZRO SAAA2,(SA)	; CASE WE CAN'T
	SETZRO SAAA3,(SA)	; GET THE RIGHT ANSWER
				;CDMCBP WILL BUILD A CONNECT BLOCK IF NEEDED
	CALL CDMCBP		;COPY ANY MESSAGE DATA TO CONNECT BLOCK
	  RET			;PROPOGATE ERROR RETURN, CODE IN T1
	TMNN SLCBP,(SL)		;DO WE HAVE ANY CONNECT DATA
	SCERR %NENCD,RTN,<No dis/connect data to read>

	MOVX T1,SB.LEN		;GET SOME WORDS FOR THE DATA-CTL FIELD
	CALL DNGWDS		;GET THEM FROM MEMORY MANAGER
	 SCERR %NEALF,RTN,<Allocation failure>
	MOVE P2,T1		;POINTER TO STRING BLOCK
	STOR P2,SASBP,(SA)	;PUT POINTER IN ARG BLOCK FOR SCMUUO

	MOVEI T1,SB.LEN		;LENGTH OF STRING BLOCK IN WORDS
	STOR T1,SBWDS,(P2)	;STORE LENGTH IN WORDS
	LOAD P1,SLCBP,(SL)	;GET PTR TO CONNECT BLOCK
	LOAD T3,CBCCT,(P1)	;LENGTH OF USER DATA
	STOR T3,SBCNT,(P2)	;STORE IN STRING BLOCK
	ADDI T3,3		;ROUND UP
	ASH T3,-2		;CONVERT BYTES TO WORDS
	XMOVEI T1,CB.UDA(P1)	;POINTER TO CONNECT BLOCK'S USER DATA
	XMOVEI T2,SB.DAT(P2)	;POINTER TO STRING BLOCK'S DATA SECTION
	CALL DNCPYW		;(T1,T2,T3)COPY USER DATA TO STRING BLOCK

	JN SLKCB,(SL),NSFRC1	;JUMP IF WE'RE TO KEEP CONNECT BLOCK
	MOVE T1,P1		;POINTER TO CONNECT BLOCK
	CALL DNFWDS		;DEALLOCATE MSG BLK WHICH HAD CONNECT DATA
	SETZRO SLCBP,(SL)	; AND REMEMBER WE'VE USED IT

NSFRC1:	LOAD T1,SLSIZ,(SL)	;GET THE SEGMENT SIZE
	STOR T1,SAAA2,(SA)	;STORE IT IN THE ARGUMENT WORD

	CALL RETFLW		;GET THE FLOW CONTROL TYPES
	STOR T1,SAAA3,(SA)	;STORE IN THE ARG WORD
	RETSKP			;RETURN SUCCESS
	SUBTTL	NSP. -- NSFSD - Synchronous Disconnect

;NSFSD - Synchronous Disconnect
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFSD function, the function dependant arguments are:
;
;	.NSAA1/ Ptr to user-data (which is the optional disconnect data)
;	.NSAA2/ Reason code or zero for 'Disconnected by Object'

NSFSD:	TRACE SC,<Performing Synchronous Disconnect (.NSFSD)>
	TMNE SLOTM,(SL)		;IS THERE A PARTIALLY FILLED OUTPUT MESSAGE?
	  SCERR %NEWRS,RTN,<Function called in wrong state>

	MOVX T1,^D20		;GET ENOUGH ROOM FOR DATA-CTL
	CALL DNGMSG		;GET A MESSAGE BLOCK
	 SCERR %NEALF,RTN,<Allocation failure>
	MOVE MB,T1		;MAKE MB POINT TO THE MESSAGE BLOCK

;Copy the user's DATA-CTL (in the user data area) from the user area to the
;message block.

	XMOVEI T1,UD.MSD(MB)	;WE WANT TO WRITE DATA-CTL INTO USER DATA
	CALL DNPINI		;INITIALIZE THE MSD

	LOAD T2,SANAG,(SA)	;GET NUMBER OF ARGUMENTS USER OFFERED
	OPSTR <SKIPLE T1,>,SAAA2,(SA) ;USER SPECIFY A REASON?
	CAIGE T2,4		;YES, SANAG INCLUDE SAAA2?
	MOVX T1,RSNDBO		;NO, REASON IS DISCONNECTED BY OBJECT
	CALL DNP2BY		;PUT REASON (2 BYTES) IN MESSAGE

	LOAD T1,SASBP,(SA)	;GET THE POINTER TO SBLOCK
	CALL CPYS2M		;COPY THE DATA-CTL TO THE MESSAGE
;Now call NSP to do the disconnect.

	LOAD T1,SLPID,(SL)	;GET THE NSPpid FOR NSP
	MOVX T3,NV.DSC		;FUNCTION IS DISCONNECT
	MOVE T4,MB		;SET UP T4 WITH MB POINTER
	CALL NSP		;PERFORM THE FUNCTION

	NEWSTATE DS		;NEWSTATE IS DISCONNECT SENT
	RETSKP			;DID OK
	SUBTTL	NSP. -- NSFAB - Abort and Release

;NSFAB - Abort and Release
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFAB function, the function dependant arguments are:
;
;	.NSAA1/ Ptr to user-data (which is the optional disconnect data)
;	.NSAA2/ Reason code or zero for 'Aborted by Object'

NSFAB:	TRACE SC,<Performing Abort and Release (.NSFAB)>

	MOVX T1,^D20		;GET ENOUGH ROOM FOR DATA-CTL
	CALL DNGMSG		;GET A MESSAGE BLOCK
	 SCERR %NEALF,RTN,<Allocation failure>
	MOVE MB,T1		;MAKE MB POINT TO THE MESSAGE BLOCK

;Copy the user's DATA-CTL (in the user data area) from the user area to the
;message block.

	XMOVEI T1,UD.MSD(MB)	;WE WANT TO WRITE DATA-CTL INTO USER DATA
	CALL DNPINI		;INITIALIZE THE MSD

	LOAD T2,SANAG,(SA)	;GET NUMBER OF ARGUMENTS USER OFFERED
	OPSTR <SKIPLE T1,>,SAAA2,(SA) ;USER SPECIFY A REASON?
	CAIGE T2,4		;YES, SANAG INCLUDE SAAA2?
	MOVX T1,RSNABO		;NO, REASON IS ABORTED BY OBJECT
	CALL DNP2BY		;PUT THE TWO BYTES OF REASON CODE IN MSG

	LOAD T1,SASBP,(SA)	;GET THE POINTER TO SBLOCK
	CALL CPYS2M		;COPY THE DATA-CTL TO THE MESSAGE

	LOAD T1,SLPID,(SL)	;GET THE NSPpid FOR NSP
	MOVX T3,NV.ABO		;FUNCTION IS DISCONNECT
	MOVE T4,MB		;SET UP T4 WITH MB POINTER
	CALL NSP		;PERFORM THE FUNCTION

	NEWSTATE DS		;NEWSTATE IS DISCONNECT SENT
	CALL STPTMR		;STOP THE CI TIMER IN CASE IT'S GOING
	SETONE SLABO,(SL)	;WE HAVE TO CLOSE WHEN LINK IS FINISHED
	RETSKP			;RETURN WITH GOODNESS
	SUBTTL	NSP. -- NSFRD - Read Disconnect Data

;NSFRD - Read Disconnect Data
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFRD function, the function dependant arguments are:
;
;	.NSAA1/ Ptr to Connect-Block
;	.NSAA2/ The Disconnect Reason

NSFRD:	TRACE SC,<Performing Read Disconnect Data (.NSFRD)>
	SETZRO SASBP,(SA)	;ZERO THE ARGUMENTS IN
	SETZRO SAAA2,(SA)	; CASE WE CAN'T GET THE RIGHT ANSWER
				;CDMCBP WILL BUILD A CONNECT BLOCK IF NEEDED
	CALL CDMCBP		;COPY ANY MESSAGE DATA TO CONNECT BLOCK
	  RET			;PROPOGATE ERROR RETURN, CODE IN T1
	OPSTR <SKIPN P1,>,SLCBP,(SL) ;GET PTR TO DIS/CONNECT BLOCK
	  SCERR %NENCD,RTN,<No dis/connect data to read>

	MOVX T1,SB.LEN		;GET SOME WORDS FOR THE DATA-CTL FIELD
	CALL DNGWDS		;GET THEM FROM MEMORY MANAGER
	  SCERR %NEALF,RTN,<Allocation failure>
	MOVE P2,T1		;POINTER TO STRING BLOCK
	STOR P2,SASBP,(SA)	;PUT POINTER IN ARG BLOCK FOR SCMUUO

	MOVEI T1,SB.LEN		;LENGTH OF STRING BLOCK IN WORDS
	STOR T1,SBWDS,(P2)	;STORE LENGTH IN WORDS
	LOAD T3,CBCCT,(P1)	;LENGTH OF USER DATA IN CONNECT BLOCK
	STOR T3,SBCNT,(P2)	;STORE IN STRING BLOCK
	ADDI T3,3		;ROUND UP TO NEAREST WORD
	ASH T3,-3		;CONVERT BYTES TO WORDS
	XMOVEI T1,CB.UDA(P1)	;POINTER TO CONNECT BLOCK'S USER DATA
	XMOVEI T2,SB.DAT(P2)	;POINTER TO STRING BLOCK'S DATA SECTION
	CALL DNCPYW		;(T1,T2,T3)COPY USER DATA TO STRING BLOCK

	LOAD T1,SLRSN,(SL)	;GET THE DISCONNECT REASON CODE
	STOR T1,SAAA2,(SA)	;RETURN THE REASON CODE
	RETSKP			; AND GIVE THE GOOD RETURN
	SUBTTL	NSP. -- NSFRL - Release the Channel

;NSFRL - Release the Channel
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFRL function, the function dependant arguments are:
;
;	.NSAA1/ Ptr to user-data (which is the optional disconnect data)

NSFRL:	TRACE SC,<Performing Release (.NSFRL)>
	SETZ MB,		;WE HAVE NO MESSAGE BLOCK, NO Q'D INTERLOCKS
	CALL RLSLNK		;(MB)RESET THE LINK (DO THE CLOSE)
	RETSKP			;RETURN SUCCESS
	SUBTTL	NSP. -- NSFRS - Read the Channel Status

;NSFRS - Read the Channel Status
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFRS function, the function dependant arguments are:
;
;	.NSAA1/ State
;	.NSAA2/ Segment Size
;	.NSAA3/ Flow control type

NSFRS:	TRACE SC,<Performing Read Status (.NSFRS)>
	LOAD P1,SANAG,(SA)	;GET NUMBER OF ARGUMENTS USER WANTED

	CAIG P1,.NSAA1		;DOES HE WANT TO KNOW SEGMENT SIZE?
	RETSKP			;NO, JUST RETURN

	LOAD T1,SLSIZ,(SL)	;GET THE MAX SEG SIZE WE'RE USING
	STOR T1,SAAA1,(SA)	;STORE IT IN SAB ARGUMENT BLOCK

	CAIG P1,.NSAA2		;DOES HE WANT TO KNOW FLOW CONTROL TYPE?
	RETSKP			;NO, JUST RETURN

	CALL RETFLW		;RETURN THE FLOW CONTROL TYPES
	STOR T1,SAAA2,(SA)	;STORE IT IN THE ARGUEMENT BLOCK
	RETSKP			; AND RETURN SUCCESSFULLY
	SUBTTL	NSP. -- NSFIS - Send Interrupt Data

;NSFIS - Send Interrupt Data
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block with user arguments stored
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;	.NSAA1/ Ptr to user-data (which is the interrupt data to be sent)

NSFIS:	TRACE SC,<Sending Interrupt Data (.NSFIS)>

	TMNE SLCCB,(SL)		;SHOULD WE CHECK CONNECT BLOCK?
	CALL FRECBP		;YES, GET RID OF CONNECT DATA IF WE CAN

NSFIS1:	OPSTR <SKIPN P1,>,SASBP,(SA) ;POINT TO THE SBLOCK POINTER
	BUG.(CHK,SCLSNS,SCLINK,SOFT,<Sblock not supplied to NSFIS>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	LOAD T1,SBCNT,(P1)	;GET THE COUNT OF BYTES
	SKIPL T1		;RANGE CHECK THE LENGTH
	CAILE T1,^D16		;IS IT WITHIN THE SPEC?
	 SCERR %NEIDL,RTN,<Interrupt data too long>

;...
;...
;Allocate the message block.

	MOVX T1,^D16		;GET 16 BYTES (MIGHT AS WELL GO ALL THE WAY
				; SINCE THEY'RE IN THE MB ALREADY)
	CALL DNGMSG		;GET THE MESSAGE BLOCK
	 SCERR %NEALF,NSFIS2,<Allocation failure>
	MOVE MB,T1		;SET UP MB
	XMOVEI T1,UD.MSD(MB)	;SET UP TO PUT IN BYTES
	CALL DNPINI		; IN THE USER SEGMENT

	D36OFF			;INTERLOCK AGAINST SCTRIB CALL FROM LLINKS
	JE SLOTU,(SL),NSFISO	;JUMP IF THIS WILL BE ONLY OUTPUT BUFFER NOW
	MOVE T1,DCNRSB		;GET # NOW RESERVED
	SKIPN DCNCON		;IS SYSTEM CONGESTED?
	CAML T1,DCNTSB		;NO, IS THERE ROOM FOR ONE MORE?
	JRST NSFISF		;NO, RETURN FAILURE
	AOS T1,DCNRSB		;ONE MORE BUFFER RESERVED
	CAMLE T1,DCNRHT		;NEW HIGH TIDE?
	MOVEM T1,DCNRHT		;YES, RECORD IT
	JRST NSFISA		;GOT ROOM, GO ALLOCATE

NSFISF:				;NO ROOM FOR THIS LOGICAL LINK
	SETOM DCNCON		;CALL FOR CHKSTS WHEN CONGESTION FREES
	D36ON			;END CRITICAL SECTION
	AOS DCNROF		;INCREMENT OUTPUT RESERVATION FAILURES
	CALL FREMSG		;RETURN THE MESSAGE BLOCK
	JRST NSFIS2		;SUCCEEDED IN SENDING ZERO BYTES (CONGESTION)

NSFISO:	JN SLINU,(SL),NSFISA	;JUMP IF INPUT BUFF ALLOC, THAT RESERVES 1 OUT
	MOVE T1,DCNRSB		;GET # ALREADY RESERVED
	ADDI T1,2		;WE INTEND TO RESERVE 2 MORE (1 IN + 1 OUT)
	CAMLE T1,DCNTSB		;WILL THAT FIT IN TOTAL?
	JRST NSFISF		;NO, RETURN FAILURE
	MOVEM T1,DCNRSB		;YES, D36OFF MAKES THIS STORE OK
	CAMLE T1,DCNRHT		;NEW HIGH TIDE?
	MOVEM T1,DCNRHT		;YES, RECORD IT
NSFISA:	INCR SLOTU,(SL)		;INCREMENT NUMBER OF OUTPUT BUFFERS IN USE
	D36ON			;END OF CRITICAL SECTION

;Here to copy the bytes from the user's buffer into the message.

	MOVE T1,P1		;POINT TO THE SBLOCK
	CALL CPYS2I		;(T1)COPY INTERRUPT DATA FROM STRING BLOCK
				; TO MESSAGE
	SETONE <MBBOM,MBEOM>,(MB) ;INTERRUPT MESSAGES ARE ONE SEGMENT

;Now send the message to NSP.

	LOAD T1,SLPID,(SL)	;GET THE NSPpid
	SETZ T2,		;T2 HAS THE DA STRUCTURE
	SETONE MBOTH,(MB)	;SEND ON THE OTHER SUB-LINK
	MOVX T3,NV.SEG		;FUNCTION IS SEND A SEGMENT
	MOVE T4,MB		;POINT TO THE MESSAGE BLOCK
	CALL NSP		;SEND INTERRUPT MESSAGE

	OPSTRM <SOS>,SSXDO,+SL.OSL(SL) ;DECR COUNT OF XMT DATA REQUESTS
	SETZRO SBCNT,(P1)	;TELL ASYNCHRONOUS USER WE SENT THE DATA
	RETSKP			; AND RETURN SUCCESSFULLY


;Here when DNGMSG or DCNRSB fails due to congestion.

NSFIS2:	LOAD T1,SLSST,(SL)	;GET LINK STATUS HALF-WORD
	TXZ T1,NSNDR ! NSIDR	;NEITHER TYPE OF OUTPUT IS OK NOW
	STOR T1,SLSST,(SL)	;STORE BACK SO WHEN CONGESTION IS FREED
	RET			; CHKSTS WILL WAKE USER
	SUBTTL	NSP. -- NSFIR - Receive Interrupt Data

;NSFIR - Receive Interrupt Data
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block with user arguments stored
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;	.NSAA1/ Ptr to interrupt data

NSFIR:	TRACE SC,<Receiving Interrupt Data (.NSFIR)>

	TMNE SLCCB,(SL)		;SHOULD WE CHECK CONNECT BLOCK?
	CALL FRECBP		;YES, GET RID OF CONNECT DATA IF WE CAN

	MOVX T1,SB.LEN		;GET SB TO STORE THE INTERRUPT DATA
	CALL DNGWDZ		; FROM MEMORY MANAGER
	  SCERR %NEALF,RTN,<Allocation failure>
	STOR T1,SASBP,(SA)	;STORE THE POINTER TO STRING BLOCK
	MOVX T2,SB.LEN		;LENGTH OF STRING BLOCK IN WORDS
	STOR T2,SBWDS,(T1)	;STORE LENGTH IN WORDS IN STRING BLOCK

	XMOVEI T1,SL.OSL(SL)	;POINT TO THE OTHER SUB-LINK'S AREA

	DEQUE MB,SS.INQ(T1),MB.NXT,NSFIR2 ;TRY TO DEQUE A MESSAGE ON INPUT Q

	MOVE T1,MB		;SET UP FOR DNLENG
	CALL DNLENG		;FIGURE OUT LENGTH OF MESSAGE
	CAILE T1,^D16		;IS IT TOO LONG?
	CALLRET SCEIVM		;--INVALID MESSAGE EVENT
	MOVE P1,T1		;SAVE THE LENGTH

	LOAD T1,MBFLG,(MB)	;GET THE PUBLIC FLAGS IN MB
	TXC T1,MB%BOM!MB%EOM	;SEE IF BOTH FLAGS ARE ON
	TXCE T1,MB%BOM!MB%EOM	; ARE AT THE SAME TIME
	BUG.(CHK,SCLSIM,SCLINK,SOFT,<Got a segmented interrupt message from NSP>,,<

Cause:	This BUG is not documented yet.

>,FREMSG)
;Now copy the data into the intermediate buffer.

	MOVE T1,MB		;POINT TO THE MESSAGE BLOCK
	CALL DNGINI		;SET UP TO GET IT

	LOAD T1,SASBP,(SA)	;POINT TO THE INTERNAL STRING BLOCK
	CALL CPYI2S		;COPY INTERRUPT DATA TO INTERNAL SB
	 CALLRET SCEIVM		;--INVALID MESSAGE EVENT

	OPSTRM <SOS T1,>,SSRDO,+SL.OSL(SL) ;DECREMENT DATA REQUEST COUNT
	OPSTR <CAML T1,>,SLINQ,(SL) ;IS NEW COUNT WITHIN QUOTA?
	JRST [CALL FREMSG	;NO, CAN'T SEND ANOTHER DRQ, FREE THE MB
	      RETSKP]		;AND RETURN TO SCMUUO

	OPSTRM <AOS>,SSRDO,+SL.OSL(SL) ;WE'RE SENDING OUT ONE DATA REQUEST
	TXO T2,MBOTH		;PUT IT ON THE OTHER SUB-LINK
	MOVX T1,1		;LET'S SEND OUT ONE DATA REQUEST
	CALL SNDDRM		;SEND THE DATA REQUESTS
	  TRNA			;ERROR
	RETSKP			;RETURN SUCCESSFULLY

	BUG.(CHK,SCLMF1,SCLINK,SOFT,<SNDDRM failed to send DRQs>,,<

Cause:	We should never fail to send data requests when we already have
	a message block!

>)
	SCERR %NEALF,RTN,<Allocation Failure>


;Here when there was nothing on the input queue.

NSFIR2:	RETSKP			;SCTNA? WILL CALL CHKSTS FOR US
	SUBTTL	NSP. -- NSFDS - Send Normal Data

;NSFDS - Send Normal Data
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block with user arguments stored
;
; Return:
;	RET		;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP		;ALL IS WELL
;
; Uses: T1-T6
;
;For .NSFDS function, the function dependant arguments are:
;
;	SAAA1/ Byte count
;	SAAA2/ Byte pointer to data
;	SAAA3/ Optional second word of byte pointer

NSFDS:	TRACE SC,<Sending Normal Data (.NSFDS)>

	SETZRO SASAT,(SA)	;NOT YET SATISFIED THIS REQUEST.
	TMNE SLCCB,(SL)		;SHOULD WE CHECK CONNECT BLOCK?
	CALL FRECBP		;YES, GET RID OF CONNECT DATA IF WE CAN

;We only check the data requests here, not the quotas as well, because
;we may have a msg blk already allocated on the output queue.

	LOAD T1,SLXFL,(SL)	;GET FLOW CONTROL TYPE
	TMNN SSXDO,+SL.NSL(SL)	;ARE THERE ANY DRQS LEFT?
	JUMPN T1,RSKP		;NO, RETURN NICELY UNLESS NO FLOW CTL
IF1,<IFN FCM.NO,<PRINTX ?NSFDS expects FCM.NO to be zero; it isn't>>
;Fall through to next page
;Fall through from previous page

;Here we get a message block in which to put user's data and calculate
;how much of the user's data will fit.

	OPSTR <SKIPGE P1,>,SAAA1,(SA) ;GET USER'S BYTE COUNT
	  SCERR %NEADE,RTN,<Address Error> ;DON'T ALLOW NEGATIVE COUNT
	SETZ T1,		;ASSUME NO BYTES IN SAVED MESSAGE
	LOAD MB,SLOTM,(SL)	;ALREADY HAVE A MESSAGE?
	JUMPE MB,NSFDS0		;JUMP IF NOT
	SETZRO SLOTM,(SL)       ;YES, ITS NOT THERE ANY MORE
	XMOVEI T1,UD.MSD(MB)	;REINITIALIZE FOR PUTTING BYTES INTO
	CALL DNPINR		; A PARTIALLY FILLED MESSAGE BLOCK
	MOVE T1,MB		;SEE HOW MANY ARE BYTES ALREADY
	CALL DNLENG		; IN IT BY GETTING LENGTH
	ADD P1,T1		;ADD IN BYTES ALREADY IN MESSAGE
NSFDS0:	OPSTR <SUB P1,>,SLSIZ,(SL) ;SUBTRACT SEGMENT SIZE WE'RE USING

;P1 is now:
;	Negative if user data fits in segment with room to spare
;	Zero     if user data fits in segment exactly
;	Positive if user data is too big to fit in segment
;This fact is used throughout the rest of this routine.

	LOAD P2,SAAA1,(SA)	;ASSUME USER'S BUFFER WILL FIT IN SEGMENT
	JUMPLE P1,NSFDS1	;WILL IT FIT?
	LOAD P2,SLSIZ,(SL)	;NO, SEND ONE SEGMENT'S WORTH
	SUB P2,T1		;P2 NOW HAS NUMBER OF BYTES WE'LL COPY
NSFDS1:	JUMPN MB,NSFDS2		;JUMP IF WE JUST DEQUEUED A MESSAGE BLOCK

;Get a message block

	MOVE T1,P2		;NUMBER OF BYTES WE'LL COPY INTO FIRST SEGMENT
	TMNN SAEOM,(SA)		;IS THIS SEGMENT AN END OF MESSAGE?
	LOAD T1,SLSIZ,(SL)	;NO, ALLOCATE A WHOLE SEGMENT'S WORTH SINCE
				; USER WILL BE SENDING MORE IN THIS SEG
	CALL DNGMSG		;(T1)GET MESSAGE BLOCK
	  JRST NSFDS6		;SUCCEEDED IN SENDING ZERO BYTES (CONGESTION)
	MOVE MB,T1		;SET UP MB WITH MESSAGE BLOCK POINTER
	XMOVEI T1,UD.MSD(MB)	;INITIALIZE FOR PUTTING BYTES
	CALL DNPINI		; IN USER DATA PART OF MESSAGE BLOCK

	D36OFF			;INTERLOCK AGAINST SCTRIB CALL FROM LLINKS
	JE SLOTU,(SL),NSFDSO	;JUMP IF THIS WILL BE ONLY OUTPUT BUFFER NOW
	MOVE T1,DCNRSB		;GET # NOW RESERVED
	SKIPN DCNCON		;IS SYSTEM CONGESTED?
	CAML T1,DCNTSB		;NO, IS THERE ROOM FOR ONE MORE?
	JRST NSFDSF		;NO, RETURN FAILURE
	AOS T1,DCNRSB		;ONE MORE BUFFER RESERVED
	CAMLE T1,DCNRHT		;NEW HIGH TIDE?
	MOVEM T1,DCNRHT		;YES, RECORD IT
	JRST NSFDSA		;GOT ROOM, GO ALLOCATE

NSFDSF:				;NO ROOM FOR THIS LOGICAL LINK
	SETOM DCNCON		;CALL FOR CHKSTS WHEN CONGESTION FREES
	D36ON			;END CRITICAL SECTION
	AOS DCNROF		;INCREMENT OUTPUT RESERVATION FAILURES
	CALL FREMSG		;RETURN THE MESSAGE BLOCK
	JRST NSFDS6		;SUCCEEDED IN SENDING ZERO BYTES (CONGESTION)

NSFDSO:	JN SLINU,(SL),NSFDSA	;JUMP IF INPUT BUFF ALLOC, THAT RESERVES 1 OUT
	MOVE T1,DCNRSB		;GET # ALREADY RESERVED
	ADDI T1,2		;WE INTEND TO RESERVE 2 MORE (1 IN + 1 OUT)
	CAMLE T1,DCNTSB		;WILL THAT FIT IN TOTAL?
	JRST NSFDSF		;NO, RETURN FAILURE
	MOVEM T1,DCNRSB		;YES, D36OFF MAKES THIS STORE OK
	CAMLE T1,DCNRHT		;NEW HIGH TIDE?
	MOVEM T1,DCNRHT		;YES, RECORD IT
NSFDSA:	INCR SLOTU,(SL)		;INCREMENT NUMBER OF OUTPUT BUFFERS IN USE
	D36ON			;END OF CRITICAL SECTION
NSFDS2:

;Fall through to next page
;Fall through from previous page

;Message block is now ready to receive data and P2 has the count of
;data to copy into it.

	MOVE T1,P2		;PASS # OF BYTES TO COPY TO COPIER
	LOAD T2,SAAA2,(SA)	;GET BYTE POINTER
	LOAD T3,SAAA3,(SA)	;GET OPTIONAL SECOND WORD OF BYTE PTR

	XMOVEI T4,DNCU2M	;ASSUME BUFFER IN USER SPACE
	TMNE SAEVA,(SA)		;IS THAT TRUE?
	XMOVEI T4,DNCB2M	;NO COPY FROM MONITOR BUFFER
	CALL 0(T4)		;COPY BUFFER TO THE MESSAGE BLOCK
	STOR T2,SAAA2,(SA)	;STORE UPDATED BYTE POINTER
	STOR T3,SAAA3,(SA)	;OPTIONAL SECOND WORD OF BYTE PTR

	MOVN T1,P2		;GET # OF BYTES WE COPIED
	OPSTRM <ADDM T1,>,SAAA1,(SA) ;FIX USER'S COUNT OF BYTES TO SEND

	TMNN SAEOM,(SA)		;IS THIS END OF MESSAGE
	SKIPL P1		; OR DID WE GET A WHOLE SEGMENTS WORTH?
	JRST NSFDS3		;YES, SEND SEGMENT TO NSP

;Here to save pointer to message so that the user can add data to it
;later.  Note that in this case we allocated a whole segment when we
;allocated the message block.

	SETONE SASAT,(SA)	;WE HAVE SATISFIED THIS NSFDS REQUEST
	STOR MB,SLOTM,(SL)	;SAVE PTR TO PARTIALLY FILLED OUTPUT MSG
	RETSKP			;RETURN SUCCESSFULLY
;Send segment to NSP.

;Set the BoM and EoM flags in the message block and remember whether
;or not this segment had EoM for figuring the next segment's BoM.

NSFDS3:	MOVE T1,SL.EOM(SL)	;GET "LAST SEGMENT'S EOM" WORD
	MOVX T2,MBBOM		;GET MESSAGE BLOCK'S BOM FLAG
	TXOE T1,SLEOM		;LAST SEGMENT HAVE EOM? (ASSUME THIS WILL)
	 IORM T2,MB.BOM(MB)	;YES, THIS MUST BE FIRST SEGMENT OF NEXT MSG
	JUMPG P1,NSFDS4		;NEVER EOM IF WE'RE SEGMENTING
	MOVX T2,MBEOM		;GET MESSAGE BLOCK'S EOM FLAG
	TMNN SAEOM,(SA)		;IS THIS EOM?
NSFDS4:	 TXZA T1,SLEOM		;NO, REMEMBER THAT FOR NEXT SEGMENT
	  IORM T2,MB.EOM(MB)	;YES, MARK MESSAGE BLOCK WITH EOM
	MOVEM T1,SL.EOM(SL)	;REMEMBER STATE OF EOM FLAG FOR NEXT TIME

;Send the message

	LOAD T1,SLPID,(SL)	;GET NSPpid
	SETZ T2,		;DAOTH IS ZERO
	MOVX T3,NV.SEG		;FUNCTION IS SEND A SEGMENT
 	MOVE T4,MB		;POINT TO MESSAGE BLOCK
	CALL NSP		;DO SEND

;Determine whether or not to claim that we have satisfied the request

	MOVX T1,SASAT		;PICK UP SASAT FLAG
	SKIPG P1		;MORE DATA TO SEND FOR THIS REQUEST?
	IORM T1,SA.SAT(SA)	;NO, WE'VE SATISFIED THE REQUEST

;Update DRQs.

	LOAD T1,SLXFL,(SL)	;GET FLOW CONTROL TYPE WE ARE USING
	CAIN T1,FCM.SG		;IS IT SEGMENT?
	JRST NSFDS5		;YES, DECREMENT DATA REQUEST COUNT

	SKIPG P1		;DO WE HAVE ANY MORE SEGMENTS TO SEND LATER?
	CAIE T1,FCM.MG		;NO, SAEOM MUST BE SET IF WE'RE HERE
	RETSKP			;MORE SEGS TO GO OR NO FLOW CONTROL

NSFDS5:	OPSTRM <SOS>,SSXDO,+SL.NSL(SL) ;DECR COUNT OF XMT DATA REQUESTS
	RETSKP			; AND RETURN SUCCESS


;Here when DNGMSG fails due to congestion.  We have not yet changed
;SAAA1, so the user will see that we succeeded in sending zero bytes.

NSFDS6:	LOAD T1,SLSST,(SL)	;GET LINK STATUS HALF-WORD
	TXZ T1,NSNDR ! NSIDR	;NEITHER TYPE OF OUTPUT IS OK NOW
	STOR T1,SLSST,(SL)	;STORE BACK SO WHEN CONGESTION IS FREED
	RETSKP			; CHKSTS WILL WAKE USER
	SUBTTL	NSP. -- NSFDR - Receive Normal Data

;NSFDR - Receive Normal Data
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block with user arguments stored
;
; Return:
;	RET		;ON ERROR WITH T1 CONTAINING THE ERROR CODE
;	RETSKP		;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFDR function, the function dependant arguments are:
;
;	SAAA1/ Byte count
;	SAAA2/ Byte pointer
;	SAAA3/ Optional second word of byte pointer

NSFDR:	TRACE SC,<Receiving Normal Data (.NSFDR)>

	SETZRO SASAT,(SA)	;NOT YET SATISFIED THIS REQUEST.
	TMNE SLCCB,(SL)		;SHOULD WE CHECK CONNECT BLOCK?
	CALL FRECBP		;YES, GET RID OF CONNECT DATA IF WE CAN

	LOAD T1,SAFLG,(SA)	;GET THE FLAGS
	TXNN T1,SA%WAI		;IS WAIT OFF
	TXNN T1,SA%EOM		; AND EOM ON?
	TRNA			;NO, NO DEADLY EMBRACE PROBLEM
	SCERR %NEBCF,RTN,<Bad Combination of NS.xxx Flags>

	XMOVEI P2,SL.NSL(SL)	;POINT TO NORMAL SUBLINK AREA

NSFDR1:	LOAD MB,QHBEG,+SS.INQ(P2) ;GET THE FIRST THING ON THE INPUT QUEUE
	JUMPE MB,RSKP		;THERE'S NOTHING THERE,
				; SCTNI? WILL CALL CHKSTS
;Here with MB/ The Input Message
;	   P2/ SL.NSL(SL)

;The SASAT flag is set when a read is satisfied, that is, when we have
;read a message segment containing an End of Message flag. SASAT is
;used to tell whether to wake a user who has requested NS.WAIt. SASAT
;is for internal use only: it has no meaning to the user. We cannot use
;SAEOM for this purpose because SAEOM has meaning when passed from the
;user to us: if it is set, then we must put the next message in one
;buffer, possibly truncating data, if SAEOM is cleared by the user, we
;must keep the overflow and present it to the next read.  Also, we
;consider a read satisfied if the user's buffer is filled and the
;user has not asserted SAEOM.

	MOVE T1,MB		;SET UP FOR CALL TO DNLENG
	CALL DNLENG		;GET THE LENGTH OF THE MESSAGE
	MOVE P1,T1		;P1 HAS LENGTH OF USER'S DATA IN MSG

	MOVE T1,MB		;GET READY TO READ REST OF BYTES
	CALL DNGINI		; IN THE MESSAGE

;Note that the following code does not depend on SASAT being initialized

	MOVE T4,SA.FLG(SA)	;GET USER'S FLAGS BEFORE THEY'RE MODIFIED
	LOAD T5,MBEOM,(MB)	;GET EOM FLAG FROM MSG BLK, T5 USED BELOW
	STOR T5,SASAT,(SA)	;READ IS SATISFIED IF THIS IS EOM
				; IOR IF BUFFER IS FULL, SEE BELOW
	OPSTR <SKIPGE T1,>,SAAA1,(SA) ;GET THE LENGTH OF THE USER'S BUFFER
	  SCERR %NEADE,RTN,<Address Error> ;DON'T ALLOW NEGATIVE COUNT
	SUB T1,P1		;WILL THIS MESSAGE FIT?
	JUMPE T1,NSFDR2		;MSG FITS IN USER BUFFER EXACTLY
	JUMPG T1,NSFDR3		;MSG FITS IN USER BUFFER WITH ROOM TO SPARE

;Here if message overfills user buffer
	LOAD P1,SAAA1,(SA)	;COPY ONLY WHAT FITS IN USER BUFFER
	SETZ T5,		;BUFFERFUL HAS NO EOM, SINCE IT OVERFLOWED
	TXNN T4,SAEOM		;USER WANT WHOLE MSG IN 1 READ?
	SETZB P2,T1		;NO, LEAVE MSG BLK FOR NEXT READ, COUNT := 0
				;YES, TOSS REST OF MSG, P2 STILL PTS TO NSL
;Here if message fills or overfills user buffer
NSFDR2:	MOVX T2,SASAT		;SATISFACTION FLAG
	TXNN T4,SAEOM		;USER WANT WHOLE MSG IN 1 READ?
	IORM T2,SA.SAT(SA)	;NO, READ IS SATISFIED SINCE BUFFER IS FULL
				; WHETHER OR NOT MBEOM IS TRUE
;Here for all messages, note that count is negative if we toss overflow

NSFDR3:	TXNN T4,SAEOM		;USER WANT WHOLE MSG IN 1 READ?
	STOR T5,SAEOM,(SA)	;NO, STORE EOM FLAG WE'VE CALC'D HERE

	STOR T1,SAAA1,(SA)	;STORE # BYTES REMAINING IN USER BFR

;Fall through to next page
;Copy the message data into the user's buffer.
;We now have:	P1/ Length of data to copy
;		P2/ Pointer to NSL if we're to free msg blk, else 0

	MOVE T1,P1		;NUMBER OF BYTES TO COPY FROM MSG TO USER
	LOAD T2,SAAA2,(SA)	;GET THE BYTE POINTER
	LOAD T3,SAAA3,(SA)	; AND OPTIONAL SECOND WORD OF BPTR
	XMOVEI T4,DNCM2U	;ASSUME BUFFER IN USER SPACE
	TMNE SAEVA,(SA)		;IS THAT TRUE?
	XMOVEI T4,DNCM2B	;NO COPY TO MONITOR BUFFER
	CALL 0(T4)		;COPY MESSAGE BLOCK TO BUFFER
	  CALLRET SCEIVM	;--INVALID MESSAGE EVENT
	STOR T2,SAAA2,(SA)	;STORE THE UPDATED BYTE POINTER
	STOR T3,SAAA3,(SA)	; AND OPTIONAL SECOND WORD OF BPTR

	JUMPE P2,RSKP		;IF THERE'S NO PTR, WE HAVE MORE TO READ
				; FROM THIS MSG, SO DON'T DEQUE MSG
				; OR SEND OUT DATA REQUESTS YET

	DEQUE MB,SS.INQ(P2),MB.NXT,NSFDRB ;DEQUE MSG WE WERE PEEKING AT

;Here we unreserve an appropriate number of buffers, but we don't
;reserve any again to correspond to the data requests we send, that is
;done by a call to SCTRIB from LLINKS when a message arrives.

NSFDRB:	D36OFF			;PROTECT THIS CODE FROM LLINKS CALL TO SCTRIB
	OPSTRM <SOS T1,>,SLINU,(SL) ;ONE LESS INPUT BUFFER USED
	MOVX T2,-1		;ASSUME WE'RE NOT FREEING LAST INPUT BUFFER
	JUMPG T1,NSFDR4		;JUMP IF THAT'S TRUE
	MOVX T2,-2		;WE ARE FREEING LAST INPUT, ASSUME NO OUTPUT
	TMNE SLOTU,(SL)		;ANY OUTPUT BUFFERS RESERVED?
	MOVX T2,0		;YES, LEAVE AN INPUT BUFFER RESERVED
NSFDR4:	ADDB T2,DCNRSB		;ADJUST THE RESERVED BUFFER COUNT
	D36ON			;END OF CRITICAL SECTION
	SKIPGE T2		;CHECK THAT THE COUNT DIDN'T GO NEGATIVE
	BUG.(CHK,SCLICR,SCLINK,SOFT,<Negative buffers reserved, from input>,,<

Cause:	LLINKS has sent a buffer to SCLINK's NSFDR routine, which
	has decremented the number of buffers reserved for the appropriate
	logical link and that count has gone negative.

	Either LLINKS has offered too many buffers or the count DCNRSB
	has been decremented or zeroed prematurely.
>)
	LOAD T1,SLINQ,(SL)	;GET THE INPUT QUOTA
	OPSTRM <SOS T2,>,SSRDO,+SL.NSL(SL) ;ONE LESS REC DATA REQUEST
	SUB T1,T2		;HOW MANY REQUESTS SHOULD WE SEND NOW?
	JUMPL T1,[CALL FREMSG	;IF WE DON'T HAVE ENOUGH QUOTA, CAN'T SEND
				; ANOTHER DRQ, SO FREE MESSAGE BLOCK
	          RETSKP]	;AND RETURN TO MONITOR USER
	OPSTRM <ADDM T1,>,SSRDO,+SL.NSL(SL) ;UPDATE RECEIVE DRQ COUNT

;Send the data requests to NSP.

	TXZ T2,MBOTH		;(T1,T2)ON THE NORMAL SUB-LINK
	CALL SNDDRM		;SEND DATA REQUESTS USING MESSAGE BLOCK
				; WE CURRENTLY HAVE
	  TRNA			;FAILED TO SEND DRQS
	RETSKP			;RETURN SUCCESSFULLY

	BUG.(CHK,SCLMF2,SCLINK,SOFT,<SNDDRM failed to send DRQs>,,<

Cause:	This BUG is not documented yet.

>)
	SCERR %NEALF,RTN,<Allocation Failure>
	SUBTTL	NSP. -- NSFSQ - Set Quotas and Goals

;NSFSQ - Set Quotas and Goals
;Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block with user arguments stored
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6, P1 & MB
;
;For the .NSFSQ function, the function dependant arguments are:
;	.NSAA1/ Link Quota, input + output
;	.NSAA2/ % Input
;	.NSAA3/ Input Goal

NSFSQ:	TRACE SC,<Performing Set Link Quota (.NSFSQ)>
	LOAD T1,SLSTA,(SL)	;GET LINK STATE
	IFSTATE T1,<DC,LK,CM,CF,NR>,RSKP ;LEAVE QUIETLY IF IN TERMINAL STATE
	LOAD P1,SANAG,(SA)	;GET NUMBER OF ARGUMENTS USER SUPPLIED
	CAIGE P1,.NSAA1		;DID HE GIVE THE TOTAL QUOTA?
	SCERR %NEWNA,RTN,<Wrong number of arguments>

	LOAD T1,SLINQ,(SL)	;GET THE OLD QUOTA
	OPSTR <ADD T1,>,SLOTQ,(SL) ;TOTAL OF INPUT + OUTPUT QUOTA
	OPSTR <SKIPG>,SAAA1,(SA) ;USER SUPPLY A NEW QUOTA?
	STOR T1,SAAA1,(SA)	;NO, STORE OLD QUOTA TOTAL AS DEFAULT

	MOVEI T2,0		;NO VALUE OFFERED YET
	CAIL P1,.NSAA2		;DID HE GIVE THE % INPUT?
	LOAD T2,SAAA2,(SA)	;GET HIS VALUE
	CAILE T2,^D100		; AND 100
	  SCERR %NEPIO,RTN,<Percentage input out of bounds>
	JUMPG T2,NSFSQ1		;USER OFFER A VALUE?
	MOVEI T2,^D50		;NO, ASSUME HALF INPUT & HALF OUTPUT
	STOR T2,SAAA2,(SA)	;REMEMBER PERCENTAGE WE DECIDED ON
NSFSQ1:
	SETZB T1,MB		;DON'T NEED ANY USER DATA
	CAIGE P1,.NSAA2		;USER SPECIFY AN INPUT GOAL?
	JRST NSFSQ2		;NO, DON'T GET A MSG BLK
	CALL DNGMSG		;GET MESSAGE BLOCK IN CASE WE NEED IT
	  SCERR %NEALF,RTN,<Allocation failure>
	MOVE MB,T1		;SAVE POINTER TO MSG BLK
NSFSQ2:

;No more error returns now

	LOAD T1,SAAA1,(SA)	;GET THE LINK QUOTA
	LOAD T2,SAAA2,(SA)	;GET PERCENTAGE VERIFIED ABOVE
	IMUL T1,T2		;MULTIPY LINK QUOTA BY % INPUT
	IDIVI T1,^D100		;DIVIDE BY 100. TO FIND INPUT QUOTA
	STOR T1,SLINQ,(SL)	;STORE THE INPUT QUOTA IN THE SLB
	LOAD T2,SAAA1,(SA)	;GET THE LINK QUOTA BACK
	SUB T2,T1		;SUBTRACT TO FIND THE OUTPUT QUOTA
	STOR T2,SLOTQ,(SL)	;STORE THE OUTPUT QUOTA IN THE SLB

	CAIGE P1,.NSAA2		;WAS THE GOAL SPECIFIED?
	RETSKP			;USE DEFAULT THAT'S THERE ALREADY
				;WE ONLY GOT MSG BLK IF WE HAVE A GOAL
	LOAD T1,SLRFL,(SL)	;GET THE FLOW CONTROL TYPE
	LOAD T2,SAAA3,(SA)	;GET THE INPUT GOAL
	CAIN T1,FCM.MG		;ARE WE IN MESSAGE FLOW CONTROL?
	CAIL T2,1		;YES, IS OUR GOAL SET TO LESS THAN ONE?
	TRNA			;NO, DON'T WORRY ABOUT IT
	MOVEI T2,1		;IN MSG FLOW MODE WE GOAL OF AT LEAST 1
	STOR T2,SLGOL,(SL)	;REMEMBER THE GOAL WE DECIDED ON

;Notify NSP of the change in the goal.

	LOAD T1,SLPID,(SL)	;GET THE NSPpid
				;T2 CARRIED DOWN FROM ABOVE
	MOVX T3,NV.GOL		;FUNCTION IS SET GOAL
	MOVE T4,MB		;NSP TAKES MB POINTER IN T4
	CALL NSP		;(T1,T2,T3,T4)PERFORM THE SET GOAL
	RETSKP			;RETURN SUCCESS
	SUBTTL	NSP. -- NSFRQ - Read Quotas and Goals

;NSFRQ - Read Quotas and Goals
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block with user arguments stored
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFRQ function, the function dependant arguments are:
;
;	.NSAA1/ Link Quota
;	.NSAA2/ % Input
;	.NSAA3/ Input Goal

NSFRQ:	TRACE SC,<Performing Read Link Quota (.NSFRQ)>

	LOAD T1,SLINQ,(SL)	;GET THE INPUT QUOTA
	LOAD T2,SLOTQ,(SL)	; AND THE OUTPUT QUOTA
	ADD T1,T2		;TOTAL THEM UP

	STOR T1,SAAA1,(SA)	;STORE IN ARG BLOCK WERE THE LINK QUOTA GOES

	LOAD P1,SANAG,(SA)	;GET THE NUMBER OF ARGUMENTS USER SUPPLIED
	CAIG P1,.NSAA2		;DOES HE WANT THE %INPUT?
	RETSKP			;NO, JUST GIVE GOOD RETURN

	LOAD T2,SLOTQ,(SL)	;GET THE OUTPUT QUOTA
	IMUL T2,^D100		;MULT BY 100. SO WE GET THE %INPUT
	IDIV T2,T1		; AS A INT BETWEEN 0 AND 100.
	STOR T1,SAAA2,(SA)	;STORE IT IN THE ARG BLOCK

	CAIG P1,.NSAA3		;DOES HE WANT THE INPUT GOAL?
	RETSKP			;NO, JUST RETURN

	LOAD T1,SLGOL,(SL)	;GET THE GOAL
	STOR T1,SAAA3,(SA)	;PUT IT IN THE ARG BLOCK
	RETSKP			; AND RETURN
	SUBTTL	NSP. -- NSFJS - Set Job Quotas and Goals

;NSFJS - Set Job Quotas and Goals
;
; Call:
;	Never Happens: serviced in SCMUUO
;
; Return:
;
; Uses: Nothing
;
;This function is performed entirely by SCMUUO or equivalent.

NSFJS:	BUG.(CHK,SCLSJS,SCLINK,SOFT,<SCMUUO called NSFJS in SCLINK>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	SUBTTL	NSP. -- NSFJR - Read Job Quotas and Goals

;NSFJR - Read Job Quotas and Goals
;
; Call:
;	Never Happens: serviced in SCMUUO
;
; Return:
;
; Uses: Nothing
;
;This function is performed entirely by SCMUUO or equivalent.

NSFJR:	BUG.(CHK,SCLSJR,SCLINK,SOFT,<SCMUUO called NSFJR in SCLINK>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	SUBTTL	NSP. -- NSFPI - Set PSI Reason Mask

;NSFPI - Set PSI Reason Mask
;
; Call:
;	SL/ Pointer to Session Control Link Block
;	SA/ Pointer to Session Control Arg Block with user arguments stored
;
; Return:
;	RET			;ON ERROR WITH T1 CONTAINING ERROR CODE
;	RETSKP			;ALL IS WELL
;
; Uses: T1-T6
;
;For the .NSFPI function, the function dependent arguments are:
;
;	.NSAA1/ XWD 0,reason mask

NSFPI:	TRACE SC,<Performing Set PSI Mask (.NSFPI)>

	LOAD T1,SAAA1,(SA)	;GET .NSAA1 ARGUMENT
	STOR T1,SLPSM,(SL)	;STORE THE PSI MASK FOR SCTPSI

	TMNE <SLPSI,SLLBC,SLFSL>,(SL) ;PSI ALREADY PENDING OR LINK CLOSING?
	RETSKP			;YES, AVOID RACE

				;T1 HOLDS OLD STATUS (0),,NEW MASK
	LOAD T2,SLCHN,(SL)	;THE LINK NUMBER
	LOAD T5,SLSST,(SL)	;T5 PASSES STATUS CHANGES (CURRENT STATUS)
	HRL T2,T5		;T2 HOLDS NEW STATUS,,CHN NUMBER
	LOAD T3,SLSJB,(SL)	;@SLWKA NEEDS T3 SET UP WITH PTR TO SJB
	MOVE T4,SL		;'LINK IDENTIFIER' TO PASS TO SCTWKQ
	OPSTR <SKIPE T6,>,SLWKA,(SL) ;IS THERE A WAKE ROUTINE?
	CALL 0(T6)		;(T1,T2,T3,T4,T5)YES, CALL WAKE ROUTINE

	RETSKP			;RETURN SUCCESSFULLY
	SUBTTL RETFLW - Return current flow controls

;RETFLW - Return Receive Flow Mode,,Transmit Flow Control
;
; Call:
;	SL/ Pointer to Session Control Line Block
;
; Return:
;	RET			;ALWAYS, WITH T1 CONTAINING RFL,,XFL
;
; Uses: T1,T2

RETFLW:	LOAD T1,SLRFL,(SL)	;GET THE RECEIVE FLOW CONTROL
	LOAD T2,SLXFL,(SL)	;GET THE TRANSMIT FLOW CONTROL TYPE
	ADDI T1,1		;TRANSLATE TO USER'S VALUE
	HRLI T1,1(T2)		;BUILD RFL,XFL
				;TRANSLATE THAT ALSO
	RET			; AND RETURN
	SUBTTL SNDDRQ - Send Data Requests

;SNDDRQ -  Send some data requests to NSP
;
; Call:
;	T1/ Number of Data Requests to Send
;	T2/ MBOTH set to the correct value
;	(for SNDDRM:
;	MB/ Pointer to Message Block)
;
; Return:
;	RET			;ON ALLOCATION FAILURE
;	RETSKP			;ON SUCCESS
;
; Uses: T1-T6

SNDDRQ:	SAVEAC <MB,P1,P2>	;GET OUR OWN COPY OF MB AND SAVE SOME PEAS
	DMOVE P1,T1		;PRESERVE THE ARGUMENTS
	SETZ T1,		;DON'T NEED ANY USER DATA
	CALL DNGMSG		;GET A MESSAGE BLOCK
	 RET			;COULDN'T ALLOCATE, JUST RETURN
	MOVE MB,T1		;POINT TO THE NEW MESSAGE BLOCK
	CALLRET SNDDRX		;JOIN COMMON CODE

;This is the alternate entry for those guys which already have a message
;block.

SNDDRM:	DMOVE P1,T1		;SAVE THE COUNT AND MBOTH
	MOVE T1,MB		;POINT TO THE MESSAGE BLOCK
	MOVEI T2,0		;NO USER DATA NEEDED
	CALL DNMINI		;RE-INITIALIZE THE MESSAGE BLOCK
	 RET			;COULDN'T DO IT

SNDDRX:	SETZ T2,		;BUILD THE DATA REQUEST ARG BLOCK (QA) IN T2
	STOR P1,QACNT,+T2	; USE THAT AS THE DATA REQUEST COUNT

	MOVX T1,MBOTH		;GET THE "OTHER" SUBLINK FLAG
	TXNE P2,MBOTH		;CALLER WANT IT SET IN MESSAGE BLOCK?
	IORM T1,MB.OTH(MB)	;YES, IT WAS CLEARED BY DNMINI

	LOAD T1,SLPID,(SL)	;GET THE NSPpid FOR NSP
	MOVX T3,NV.DRQ		;FUNCTION IS SEND DATA REQUESTS
	MOVE T4,MB		;POINT TO MESSAGE BLOCK
	CALL NSP		;CALL NSP AND RETURN EVENTUALLY
	RETSKP			;RETURN SUCCESS
	SUBTTL RLSLNK - Release a Link

;RLSLNK - Release a Link
;
; Call:
;	MB/ Ptr to msg blk or zero if LLINKS will not have to queue NV.CLS call
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6,MB,MS

RLSLNK:	JN <SLFSL,SLLBC>,(SL),RLSLN1 ;IGNORE IF LINK ALREADY BEING CLOSED
	CALL STPTMR		;STOP THE CI TIMER, IF IT'S GOING
	JN SLPID,(SL),RLSLN2	;JUMP IF WE HAVE A PID FROM NSP
	SETONE SLFSL,(SL)	;WE HAVEN'T TOLD NSP ABOUT
				; THIS LINK YET, DON'T CALL HIM NOW
	CALL SCTRFJ		;REQUEST JIFFY SERVICE, SLFSL WILL
				; TELL IT TO FREE THE SLB
RLSLN1:	SKIPN MB		;DID WE HAVE A MSG BLK?
	RET			;NO, ONLY RETURN
	CALLRET FREMSG		;YES, FREE IT AND RETURN

RLSLN2:	LOAD T1,SLPID,(SL)	;GET THE NSPpid FROM SLB
	LOAD T2,SLSLB,(SL)	;GET THIS PORT'S SLBid
	MOVX T3,NV.CLS		;CLOSE FUNCTION FOR NSP
	MOVE T4,MB		;NSP WANTS MSG BLK PTR IN T4 (ZERO IF RESET)
	CALL NSP		;CALL NSP TO CLOSE THE PORT
	SETONE SLLBC,(SL)	;LINK IS BEING CLOSED
	RET			;ONLY RETURN
	SUBTTL	LLINKS Calls -- Entry Vector Table

;See procedure SCTL for a description of the calling sequence used to
;call SCTL.

;The offsets SV.xxx into this table are defined in D36PAR.

DEFINE SV(NAM),<
IFN .-SCFNT-SV.'NAM,<	PRINTX ?SCFNT table (NAM) not in same order
			PRINTX ? as SV.'NAM in D36PAR.UNV
			PASS2
			END>
	IFIW SC'NAM>

;These are the function codes for the calls to Session Control from
;NSP.

SCFNT:	SV CCR			;CONNECT CONFIRMED CALL
	SV DIR			;DISCONNECT INITIATE RECEIVED CALL
	SV DCR			;DISCONNECT CONFIRM RECEIVED CALL
	SV OND			;OUTPUT NOT DONE CALL
	SV ODN			;OUTPUT DONE CALL
	SV SEG			;SEGMENT RECEIVED CALL
	SV DRQ			;DATA REQUEST RECEIVED CALL
	SV NCF			;NO CONFIDENCE IN PORT CALL
	SV NRS			;NO RESOURCES CALL
	SV CLS			;CLOSE COMPLETED CALL
	SV NLK			;NO LINK CALL
	SV NCM			;NO COMMUNICATION CALL
	SV NRN			;NOT IN RUN STATE
	SV CAK			;GOT A CONNECT ACK
SV.MAX==.-SCFNT-1		;THE HIGHEST RECOGNIZED ENTRY OFFSET

				;The Connect Initiate call does not go
				;through the vectored interface, since
				;it must go to a single routine
				;capable of sorting out which Session
				;Control will handle the incoming
				;message.  the connect initiate call
				;goes to the global label SCTLCI.

PURGE SV
	SUBTTL	LLINKS Calls -- SCTRIB - Reserve Input Buffer

;SCTRIB - Reserve Input Buffer
;
;Call:	T1/ Pointer to SLB
;	CALL	SCTRIB
;	  Error Return: no input buffer has been reserved
;	Normal Return: one input buffer has been reserved
;Changes T1,T2,T3,T4

;This routine and all others which reference DCNRSB must run under
;D36OFF because this routine is called from LLINKS without the SCLINK
;interlock.  This routine is only called from LLINKS.  It is here in
;SCLINK so that all references to the reserved buffer counter will be
;together in one module.

;The scheme here is that we want to count the buffers which are
;actually in use, plus those which are required to be reserved in
;order to prevent a deadlock.  We must be sure that no logical link is
;put in the position of being having a allowed to read a message while
;being stuck not allowed to send a message.  If both ends of a link
;were to be in that state, they would be in a deadly embrace.
;
;So whenever we want to commit a buffer for output (by sending it) or
;for input (by ACKing it), we must check first if we have room in the
;system pool to reserve it.  If we reserve a buffer for one direction,
;then we must assure that at least one is reserved for the other
;direction.
;
;The handling of DCNRSB in SCLINK preserves the above guarantees.
;This routine is called from LLINKS when it receives a message and
;there are Session Control data requests for that message.  If this
;routine returns failure, LLINKS will keep the message, unACKed, and
;will try to send it to Session Control again when the memory manager
;tells LLINKS that congestion has been relieved.

SCTRIB::D36OFF			;INTERLOCK AGAINST SCTRIB CALL FROM LLINKS
	JE SLINU,(T1),SCTRB1	;JUMP IF THIS WILL BE ONLY INPUT BUFFER NOW
	MOVE T2,DCNRSB		;GET # NOW RESERVED
	SKIPN DCNCON		;IS THE SYSTEM CONGESTED?
	CAML T2,DCNTSB		;NO, CAN WE FIT ONE MORE?
	JRST SCTRB3		;NO ROOM FOR THIS LINK
	AOS T2,DCNRSB		;ONE MORE BUFFER RESERVED
	JRST SCTRB2		;SUCCESS

SCTRB1:	JN SLOTU,(T1),SCTRB4	;JUMP IF OUTPUT BUFF ALLOC, THAT RESERVES 1 IN
	MOVE T2,DCNRSB		;GET # ALREADY RESERVED
	ADDI T2,2		;WE INTEND TO RESERVE 2 MORE (1 IN + 1 OUT)
	CAMLE T2,DCNTSB		;ENOUGH ROOM IN TOTAL SYSTEM BUFFERS?
	JRST SCTRB3		;NO, NO ROOM FOR THIS LINK
	MOVEM T2,DCNRSB		;YES, D36OFF MAKES THIS STORE OK
SCTRB2:	CAMLE T2,DCNRHT		;NEW HIGH TIDE?
	MOVEM T2,DCNRHT		;YES, RECORD IT
SCTRB4:	INCR SLINU,(T1)		;UPDATE INPUT_BUFFERS_IN_USE COUNT
	D36ON			;END OF CRITICAL SECTION
	RETSKP			;GOT ROOM, SUCCESS RETURN

SCTRB3:	SETOM DCNCON		;CALL FOR CHKSTS WHEN CONGESTION FREES
	D36ON			;END OF CRITICAL SECTION
	AOS DCNRIF		;INCREMENT INPUT RESERVATION FAILURES
	RET			;FAIL RETURN
	SUBTTL	LLINKS Calls -- SCTUCG - Uncongestion Call

;SCTUCG - LLINKS calls this routine when memory manager announces
;	  that congestion is relieved.
;
; Call:
;	No arguments
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCTUCG::SAVEAC <MB,SL,SA,MS,P1,P2>
	TRACE SC,Session Control told of congestion relief

	SETOM SCTUCF		;NO MESSAGE BLOCK TO QUEUE HERE
	CALLRET SCTLCF		;GET THE INTERLOCK, FLAG IF FAIL

;When NSFDS (data send) and NSFIS (interrupt send) fail to get a
;message block, they will both turn off both NSNDR (normal data
;requests available) and NSIDR (interrupt data requests available).
;Presumably this will only happen if the system is congested.  Later,
;when the system congestion is relieved, we must tell the stalled
;links that we again have message blocks available.  Since the NSxDR
;flags in the status half-word have been turned off, this call to
;CHKSTS will turn them back on again if appropriate and this will
;trigger CHKSTS to call the wake routine.

SCIUCG:	LOAD SL,QHBEG,+SCTASQ	;PTR TO FIRST SLB ON ALL SLBs Q
SCIUC1:	JUMPE SL,RTN		;RETURN WHEN NO MORE SLBs TO VISIT
	CALL CHKSTS		;SEE IF THIS SLB NEEDS WAKING
	LOAD SL,SLASQ,(SL)	;STEP TO NEXT SLB ON ALL SLBs Q
	JRST SCIUC1		;LOOP OVER ALL SLBs IN SYSTEM
	SUBTTL	LLINKS Calls -- SCTLCI - Connect Initiate Call

;SCTLCI - Connect Initiate Call
;
; Call:
;	T1/ NSPpid for port just created
;	T2/ Connect Call arguments (see BEGSTR IA in D36PAR)
;	T4/ Pointer to Message Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCTLCI::SAVEAC <MB,SL,SA,MS,P1,P2>
	TRACE SC,Connect Initiate call
	SKIPG MB,T4		;SET UP THE MESSAGE BLOCK POINTER
	BUG.(CHK,SCLCIM,SCLINK,SOFT,<NSP called SCTLCI without a msg blk>,,<

Cause:	This BUG is not documented yet.

>,RTN)

	STOR T1,MBAR1,(MB)	;STORE THE FIRST ARGUMENT
	STOR T2,MBAR2,(MB)	; AND THE SECOND ONE
	XMOVEI T1,SCTLC1	;PROCESSOR TO CALL WHEN WE
	CALLRET SCTLCQ		; GET THE INTERLOCK

;Now try to find a matching SLB for the incoming connect.

SCTLC1:	TRACE SC,Processing Connect Initiate

	MOVE T1,MB		;POINT TO THE INCOMING MESSAGE BLOCK
	CALL DNGINI		;SET UP MS FOR DNGxBY ROUTINES

	MOVX T1,CB.LEN		;WE HAVE TO GET A CONNECT BLOCK
	CALL DNGWDZ		; TO FILL IN AND COMPARE WITH PASSIVE SLBS
	  JRST SCTCIN		;RESOURCE FAILURE
	MOVE P1,T1		;SAVE POINTER TO INCOMING CONNECT BLOCK

	CALL PRSCTX		;PARSE THE INCOMING CONNECT DATA IN CB
	 JRST [	MOVE T1,P1	;PTR TO NEW CONNECT BLK
		CALL DNFWDS	;FREE NEW CONNECT BLK
		CALLRET SCEIVM]	;--INVALID MESSAGE EVENT

	MOVE T1,P1		;GET THE INCOMING CONNECT BLOCK POINTER BACK
	CALL SLBMAT		;FIND PASSIVE SLB TO MATCH NEW SLB
	 JRST [	MOVE T1,P1	;PTR TO NEW CONNECT BLK
		CALL DNFWDS	;FREE NEW CONNECT BLK
		CALLRET SCTCIN]	;UNRECOGNIZED OBJECT ERROR
;We have found matching SLB, lets set some parameters in the SLB and
;go into CR state.

	LOAD T1,SLCBP,(SL)	;POINT TO PASSIVE CB
	CALL DNFWDS		;FREE THAT UP
	SETZRO SLCBP,(SL)	;LOSE PTR TO PASSIVE CB WE JUST FREED

	LOAD T1,MBSRC,(MB)	;GET SOURCE NODE ADDRESS
	STOR T1,SLDNA,(SL)	; AND STORE AS DESTINATION OF THIS LINK
	CALL SCTA2N		;(T1)GET THAT NODE'S NAME
	 SETZ T1,		;DON'T HAVE A NAME, LEAVE IT ZERO LENGTH
	STOR T1,CBNAM,(P1)	;SAVE THE NODE NAME.

SCTLC2:	STOR P1,SLCBP,(SL)	;PUT PTR TO ACTIVE CONNECT BLK IN SLB
				; FOR .NSFRI FUNCTION
	LOAD T1,MBAR1,(MB)	;GET THE NSPpid
	STOR T1,SLPID,(SL)	;STORE IT IN THE SLB

	LOAD T2,MBAR2,(MB)	;GET THE CONNECT CALL ARGUMENTS
	LOAD T1,IAFLO,+T2	;GET FLOW CONTROL TYPE FROM ARGUMENTS
	STOR T1,SLXFL,(SL)	; AND STORE IT IN SLB

	LOAD T1,IASIZ,+T2	;GET MAXIMUM SEGMENT SIZE SPECIFIED
				; BY THE REMOTE
	OPSTR <CAMGE T1,>,SLSIZ,(SL) ;IS OURS BIGGER?
	STOR T1,SLSIZ,(SL)	;YES, MAKE REMOTE'S SIZE ONE TO USE

	NEWSTATE CR		;CALL SCSSTS TO PUT US IN .NSSCR STATE
				; AND WAKE THE USER
	TMNE MBPH2,(MB)		;IS THIS CONNECT FROM A PHASE II GUY?
	CALL CONBUF		;YES, SET UP FOR CONSERVATIVE BUFFERING

	CALL STRTMR		;START CONNECT TIMERS & FIX TIMER COUNTS
	CALLRET FREMSG		;DEALLOCATE CI MESSAGE, & RETURN TO NSP
;Here when we could not get enough resources to accept the Connect.

SCTCIR:	MOVX P2,RSNRES		;NO RESOURCES ERROR CODE
	JRST SCTCFN		;TO COMMON REJECT CODE

SCTCIN:	MOVX P2,RSNURO		;UNRECOGNIZED OBJECT REASON CODE
SCTCFN:	TRACE SC,Rejecting Connect Initiate call
	LOAD P1,MBAR1,(MB)	;SAVE THE NSPpid
	MOVE T1,MB		;SET UP FOR DNMINI CALL
	MOVEI T2,3		;LENGTH OF USER DATA NEEDED
	CALL DNMINI		;RE-INITIALIZE THE MESSAGE BLOCK WE'RE USING
	  BUG.(CHK,SCLMI1,SCLINK,SOFT,<DNMINI failed to re-init an MB>,,<

Cause:	This BUG is not documented yet.

>,RTN)

	XMOVEI T1,UD.MSD(MB)	;POINT TO THE USER MSD
	CALL DNPINI		;INITIALIZE FOR PUTTING BYTES

	MOVE T1,P2		;GET REASON CODE
	CALL DNP2BY		;PLACE IT IN THE MESSAGE

	SETZ T1,		;NO DATA-CTL NEEDED
	CALL DNP1BY		;PLACE IT IN

	MOVE T1,P1		;SET UP THE NSPpid
	MOVX T3,NV.REJ		;REJECT THE CONNECT
	MOVE T4,MB		;SET UP POINTER TO MESSAGE BLOCK
	CALLRET NSP		;INFORM NSP OF FAILURE
	SUBTTL	LLINKS Calls -- The Vectored Call Entry Point SCTL

;SCTL - Here for all calls from NSP except Connect Initiate
;
; Call:
;	T1/ SLBid
;	T2/ Function specific argument
;	T3/ Function Code (SV.xxx)
;	T4/ Pointer to message block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCTL::	SAVEAC <MB,MS,SA,SL>	;SAVE SOME ACS
	SKIPL T3		;RANGE CHECK
	CAILE T3,SV.MAX		; THE FUNCTION TYPE
	BUG.(CHK,SCLFCT,SCLINK,SOFT,<Illegal function in call from NSP>,,<

Cause:	This BUG is not documented yet.

>,RTN)

	SKIPG MB,T4		;SET UP MESSAGE BLOCK
	BUG.(CHK,SCLNMB,SCLINK,SOFT,<NSP called SCTL without a msg blk>,,<

Cause:	This BUG is not documented yet.

>,RTN)

	STOR T1,MBAR1,(MB)	;STORE THE SLBid
	STOR T2,MBAR2,(MB)	;STORE THE FUNCTION SPECIFIC ARGUMENT
	STOR T3,MBAR3,(MB)	;STORE THE FUNCTION CODE
	XMOVEI T1,SCTL1		;ADDRESS OF PROCESSOR TO CALL AFTER WE
	CALLRET SCTLCQ		; GET THE INTERLOCK

SCTL1:	CALL FNDSBI		;FIND THE SLB FROM THE SLBid
	  CALLRET FREMSG	;NO SUCH SLBid, IGNORE THIS MSG
	CALLRET @SCFNT(T3)	;DISPATCH BY FUNCTION TYPE
	SUBTTL	LLINKS Calls -- SCCCR - Connect Confirmed call from NSP

;SCCCR - Connect Confirmed call from NSP
;
; Call:
;	T2/ Connect Initiate arguments (see BEGSTR IA in D36PAR)
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCCCR:	TRACE SC,Connect confirmed call
	SAVEAC P1		;SAVE A PEA
	MOVE P1,T2		;SAVE ARGUMENT FROM NSP

	CALL STPTMR		;STOP CI TIMER & DECREMENT TIMER COUNTS

	LOAD T1,IAFLO,+P1	;GET FLOW CONTROL TYPE
	STOR T1,SLXFL,(SL)	;STORE TRANSMIT FLOW TYPE IN SLB

	LOAD T1,IASIZ,+P1	;GET MAX BYTES ALLOWED IN MESSAGE SEGMENT
	STOR T1,SLSIZ,(SL)	;STORE THAT

	TMNE MBPH2,(MB)		;WAS CC FROM A PHASE II NODE?
	CALL CONBUF		;YES, USE CONSERVATIVE BUFFERING

	STOR MB,SLCDM,(SL)	;SAVE CONNECT MSG FOR "NSFRC"

	NEWSTATE RN		;SET NEW STATE TO CONNECT RECEIVED
	SETONE SLCCB,(SL)	;HAVE TO CHECK CONNECT BLOCK NOW

;Now let's send the job's input quota worth of DRQs.

	TMNE SLPH2,(SL)		;SHOULD WE BE CONSERVATIVE WITH THIS FELLOW?
	RET			;LEAVE QUIETLY

	MOVEI T1,0		;DON'T NEED ANY USER DATA
	CALL DNGMSG		;GET A NEW MESSAGE BLOCK
	  BUG.(CHK,SCLNRD,SCLINK,SOFT,<No resources to send DRQs in SCCCR>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	MOVE MB,T1		;POINT TO THE NEW MSG BLK

	SETZRO MBOTH,(MB)	;SEND OUT ON THE NORMAL SUBLINK
	SETZ T2,		;BUILD THE DATA REQUEST ARG BLOCK (QA) IN T2
	LOAD T1,SLINQ,(SL)	;GET THE INPUT QUOTA
	STOR T1,QACNT,+T2	; USE THAT AS THE DATA REQUEST COUNT
	STOR T1,SSRDO,+SL.NSL(SL) ;WE KEEP COUNT OF OUTGOING DATA REQUESTS

	LOAD T1,SLPID,(SL)	;GET THE NSPpid FOR NSP
	MOVX T3,NV.DRQ		;FUNCTION IS SEND DATA REQUESTS
	MOVE T4,MB		;POINT TO MESSAGE BLOCK
	CALLRET NSP		;CALL NSP AND RETURN EVENTUALLY
	SUBTTL	LLINKS Calls -- SCDIR - Disconnect Initiate received call

;SCDIR - Disconnect Initiate received call from NSP
;
; Call:
;	T2/ Reason Code
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T2

SCDIR:	TRACE SC,Disconnect Initiate received call
	SAVEAC P1		;SAVE A PEA

	STOR T2,SLRSN,(SL)	;ALSO STORE THE REASON FOR ERROR CODE
				; PROCESSING
	LOAD P1,SLSTA,(SL)	;GET THE STATE OF LINK
	IFSTATE P1,CS		;IF WE WERE IN CONNECT SENT STATE,
	  CALL STPTMR		; STOP CI TIMERS & ADJUST TIMER CNTRS

	OPSTR <SKIPE T1,>,SLCDM,(SL) ;ANY CONNECT DATA STILL THERE?
	  CALL DNFMSG		     ;YES, FREE IT NOW
	STOR MB,SLCDM,(SL)	;STORE DISCONNECT MESSAGE
	SETONE SLCCB,(SL)	;HAVE TO CHECK CONNECT BLOCK NOW

	IFNSTATE P1,CS,SCDIR1	;IF WE WERE IN CONNECT SENT,
	NEWSTATE RJ		; SET THE STATE TO RJ
	RET			; AND RETURN

;We differentiate between RJ and DR states here so that the user can
;tell from the state whether or not the link has ever been open.

SCDIR1:	NEWSTATE DR		;WE GOT INTO RN STATE, SO GO INTO DR STATE
	RET			;RETURN
	SUBTTL	LLINKS Calls -- SCDCR - Disconnect Confirm received call

;SCDCR - Disconnect Confirm received call from NSP
;
; Call:
;	T2/ Reason Code
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T2

SCDCR:	TRACE SC,Disconnect Confirm received call

	NEWSTATE DC		;GO INTO DISCONNECT CONFIRM STATE
	CALLRET CHKABO		;(MB)CLOSE PORT IF NECESSARY
	SUBTTL	LLINKS Calls -- SCODN - Output done call

;SCODN - Output done call from NSP
;SCOND - Output not done call from NSP
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T2

SCOND:!
SCODN:	TRACE SC,Output done call
	D36OFF			;PROTECT THIS CODE FROM LLINKS CALL TO SCTRIB
	OPSTRM <SOS T1,>,SLOTU,(SL) ;DECREMENT OUTPUT BUFFERS IN USE
	MOVX T2,-1		;ASSUME WE'RE NOT FREEING LAST OUTPUT BUFFER
	JUMPG T1,SCODN1		;JUMP IF THAT'S TRUE
	MOVX T2,-2		;WE ARE FREEING LAST OUTPUT, ASSUME NO INPUT
	TMNE SLINU,(SL)		;ANY INPUT BUFFERS ALLOCATED?
	MOVX T2,0		;YES, LEAVE AN OUTPUT BUFFER RESERVED
SCODN1:	ADDB T2,DCNRSB		;ADJUST THE RESERVED BUFFER COUNT
	D36ON			;END OF CRITICAL SECTION
	SKIPGE T2		;CHECK THAT THE COUNT DIDN'T GO NEGATIVE
	BUG.(CHK,SCLOCR,SCLINK,SOFT,<Negative buffers reserved, from output>,,<

Cause:	LLINKS has sent a buffer to SCLINK's output done routine, which
	has decremented the number of buffers reserved for the appropriate
	logical link and that count has gone negative.

	Either LLINKS has returned too many buffers or the count DCNRSB
	has been decremented or zeroed prematurely.
>)
	CALL FREMSG		;FREE THE MESSAGE BLOCK
	CALLRET CHKSTS		;GENERATE PSI INTERRUPT "OUTPUT-OK"
	SUBTTL	LLINKS Calls -- SCSEG - Segment received call

;SCSEG - Segment received call from NSP
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCSEG:	TRACE SC,Segment Received Call

	XMOVEI T4,SL.NSL(SL)	;ASSUME IT'S ON THE NORMAL SUBLINK
	TMNE MBOTH,(MB)		;IS IT THE OTHER SUBLINK?
	XMOVEI T4,SL.OSL(SL)	;YES, POINT TO OTHER AREA

	ENDQUE MB,SS.INQ(T4),MB.NXT,T1 ;QUEUE THE MESSAGE
	CALLRET CHKSTS		;SEE IF THE USER NEEDS WAKING

;Note:  The count of input buffers used is incremented when the data
;requests are sent out in NSFxR, so we don't have to incr now.
	SUBTTL	LLINKS Calls -- SCDRQ - Data request received call

;SCDRQ - Data request received call from NSP
;
; Call:
;	T2/ DATA REQUEST call's arguments (see BEGSTR QA in D36PAR)
;	SL/ Pointer to Session Control Link Block
;	MB/ Pointer to the Message Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCDRQ:	TRACE SC,Data Request Received Call

	XMOVEI T4,SL.NSL(SL)	;ASSUME IT CAME FROM NORMAL SUBLINK
	TMNE MBOTH,(MB)		;DID IT?
	XMOVEI T4,SL.OSL(SL)	;NO, MUST BE FROM INTERRUPT SUBLINK

	LOADE T1,QACNT,+T2	;GET THE DATA REQUEST COUNT
	LOADE T3,SSXDO,(T4)	;GET # OF DRQS WE HAVE NOW OUTSTANDING
	ADD T3,T1		;NEW COUNT OF DATA REQUESTS
	STOR T3,SSXDO,(T4)	;STORE THE NEW DATA REQUEST COUNT

	CALL CHKSTS		;SET THE STATUS, POSSIBLY GENERATING PSI
	CALLRET FREMSG		;FREE THE MESSAGE BLOCK
	SUBTTL	LLINKS Calls -- SCNCF - No confidence in port call

;SCNCF - No confidence in port call from NSP
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCNCF:	TRACE SC,No confidence in port call

	NEWSTATE CF		;NEW STATE IS NO CONFIDENCE
	CALLRET CHKABO		;(MB)CLOSE PORT IF NECESSARY
	SUBTTL	LLINKS Calls -- SCNRS - No resources call

;SCNRS - No resources call from NSP
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCNRS:	TRACE SC,No resources call

	NEWSTATE NR		;SET THE NEW STATE TO "NO RESOURCES"
	CALLRET CHKABO		;(MB)CLOSE PORT IF NECESSARY
	SUBTTL	LLINKS Calls -- SCCLS - Close Completed call

;SCCLS - Close Completed call from NSP
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCCLS:	TRACE SC,Close Completed call

	SETONE SLFSL,(SL)	;FREE THE SLB ON NEXT TICK
	CALL SCTRFJ		;REQUEST JIFFY SERVICE FOR THIS LINK
	CALLRET FREMSG		;FREE THE MESSAGE BLOCK
	SUBTTL	LLINKS Calls -- SCNLK - No link call

;SCNLK - No link call from NSP
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCNLK:	TRACE SC,No Link call

	NEWSTATE LK		;NEW STATE IS NO-LINK
	CALLRET CHKABO		;(MB)CLOSE PORT IF NECESSARY
	SUBTTL	LLINKS Calls -- SCNCM - No communication call

;SCNCM - No communication call from NSP
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCNCM:	TRACE SC,No communication call

	NEWSTATE CM		;NEW STATE IS NO COMMUNICATION
	CALLRET CHKABO		;(MB)CLOSE PORT IF NECESSARY
	SUBTTL	LLINKS Calls -- SCNRN - Not in Run State call

;SCNRN - Not in Run State call from NSP
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

SCNRN:	TRACE SC,Not in Run State call
				;NO NEW STATE TO GO INTO HERE
	CALLRET CHKABO		;(MB)CLOSE PORT IF NECESSARY
	SUBTTL	LLINKS Calls -- SCCAK - Got a Connect ACK

;SCCAK - Got a Connect ACK from NSP
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1
;
;NSP will call us here to report a connect ACK.  Using this call, we
;can determine that the CI has made it to the remote, while the remote
;user hasn't yet accepted the connect.  Maybe someday we'll figure out
;what to do with this.

SCCAK:	TRACE SC,Got a Connect ACK
	CALLRET FREMSG
	SUBTTL	Subroutines -- CHKABO - Check SLABO flag

;CHKABO - Check SLABO flag, close port if need be
;
; Call:
;	MB/ Pointer to a message block
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS, MESSAGE BLOCK FREED
;
; Uses: T1-T6
;

CHKABO:	TMNN SLABO,(SL)		;ARE WE TRYING TO CLOSE AFTER AN ABORT?
	CALLRET FREMSG		;NO, FREE MSG BLK AND LEAVE
	TMNE <SLFSL,SLLBC>,(SL)	;YES, HAVE WE ALREADY CLOSED LINK?
	CALLRET FREMSG		;YES, FREE MSG BLK AND LEAVE

	CALLRET RLSLNK		;(MB)RELEASE THE LINK AND RETURN
	SUBTTL	Subroutines -- CHKPPN - Check self PPN for validity

;CHKPPN - Check PPN in self port block
;
; Call:
;	T1/ Pointer to Port Block
;	SL/ Pointer to SLB
; Return
;	RET			;INVALID PPN AND NO PRIVS TO FAKE IT
;	RETSKP			;VALID


CHKPPN:
IFN FTOPS10,<
	LOAD T2,SLSJB,(SL)	;GET POINTER TO SJB
	JN SJPRV,(T2),RSKP	;IF PRIVED, ALWAYS VALID
IFN <PB.USR-PB.GRP>,<PRINTX PBUSR and PBGRP must be in same word>
	MOVE T1,PB.USR(T1)	;GET PPN WE CLAIM TO BE
	LOAD T2,SJJOB,(T2)	;GET JOB OUR JOB NUMBER
	CAME T1,JBTPPN##(T2)	;COMPARE OUR PPN WITH PPN WE CLAIM TO BE
	RET			;THAT'S NOT YOU. FAIL.
	RETSKP			;THAT'S YOU ALRIGHT. SUCCEED.
> ;END IFN FTOPS10
IFN FTOPS20,<
	RETSKP			;SUCCEED UNTIL WE DECIDE TO CHECK
> ;END IFN FTOPS20
	SUBTTL	Subroutines -- FREMSG - Free a message block

;FREMSG - Free a message block
;
; Call:
;	MB/ Pointer to Message Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

FREMSG:	SKIPN T1,MB		;POINT TO THE MESSAGE BLOCK
	BUG.(CHK,SCLFMZ,SCLINK,SOFT,<Tried to free message with MB zero>,,<

Cause:	This BUG is not documented yet.

>)
	TRASH MB,		;CATCH A BUG
	CALLRET DNFMSG		;FREE THE MESSAGE BLOCK
	SUBTTL	Subroutines -- CONBUF - Invoke Conservative Buffering

;CONBUF - Invoke Conservative Buffering
;
; Call:
;	SL/ Pointer to SLB
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1,T2

;If we do support Phase II, then this routine will have to be updated
;to reserve a small number of buffers (eg, 1 in + 1 out) at open time,
;and then the rest of the 'reserve' routines will have to test for
;Phase II and promise never to release this link's reservations, and
;always to use the existing reservation when reallocating.

CONBUF:	BUG.(INF,SCLCBN,SCLINK,SOFT,<Phase-II buffering not implemented>,,<

Cause:	Conservative buffering is not yet implemented.

>)
	SETONE SLPH2,(SL)	;MARK LINK AS A PHASE II LINK
	MOVX T1,FCM.SG		;DO SEGMENT FLOW CONTROL ONLY
	STOR T1,SLRFL,(SL)	;SET THE RECEIVE FLOW CONTROL TYPE
	SETZRO SLGOL,(SL)	;SET THE GOAL TO ZERO
	MOVX T1,SCTP2Q		;GET THE PHASE II QUOTA
	STOR T1,SLINQ,(SL)	;STORE THAT AS THE INPUT QUOTA
	STOR T1,SLOTQ,(SL)	; AND AS THE OUTPUT QUOTA
	RET			;ONLY RETURN
	SUBTTL	Subroutines -- FNDSLB - Find SLB given a channel number

;FNDSLB - Find a SLB from a channel number
;
; Call:
;	T1/ Channel #
;	T2/ Pointer to Session Control Job Block
;
; Return:
;	RET			;COUDLN'T GET IT
;	RETSKP			;FOUND IT OK, SL POINTS TO IT
;
; Uses: T1,T2,T4,SL

FNDSLB:	JUMPLE T1,RTN		;IF NEGATIVE OR ZERO, RETURN FAILURE
	OPSTR <CAMLE T1,>,SJCHC,(T2) ;DO A QUICKIE RANGE CHECK
	RET			;OUT OF BOUNDS
	SOJ T1,			;OUR OFFSET IS ZERO, WHILE USER'S IS ONE
	LOAD T4,SJCHT,(T2)	;GET POINTER TO SLB TABLE
	ADD T4,T1		;FIND ENTRY FOR THIS CHANNEL
	SKIPN SL,(T4)		;GET THE POINTER TO IT
	RET			;NO THERE, GIVE BAD RETURN
	OPSTR <CAME SL,>,SLSLB,(SL) ;MAKE SURE IT POINTS TO AN SLB
	BUG.(CHK,SCLSPF,SCLINK,SOFT,<SLB self pointers messed up in FNDSLB>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	TMNE <SLFSL,SLLBC>,(SL)	;IS IT SCHEDULED FOR DESTRUCTION?
	RET			;YES, DON'T LET ON THAT ITS HERE
	RETSKP			;RETURN WITH SUCCESS
	SUBTTL	Subroutines -- FNDSBI - Find SLB from SLBid

;FNDSBI - Find SLB from a SLBid
;
; Call:
;	T1/ SLBid
;
; Return:
;	RET			;FAILED
;	RETSKP			;SUCCESS WITH SL POINTING TO SLB
;
; Uses: T1

FNDSBI:	TMNE SLFSL,(T1)		;SLB SCHEDULED FOR DESTRUCTION?
	RET			;YES, TELL CALLER IT DOESN'T EXIST
	SKIPE SL,T1		;EASY ISN'T IT?
	AOS (P)			;WE'RE OK
	RET			;BLEW IT
	SUBTTL	Subroutines -- FRESLB - Deallocate a SLB

;FRESLB - Deallocate a SLB
;
; Call:
;	SL/ Pointer to SLB
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6

FRESLB:	SAVEAC <P1,P2>
	OPSTR <SKIPN P2,>,SLSJB,(SL) ;LOAD UP THE JOB BLK POINTER
	BUG.(CHK,SCLSLB,SCLINK,SOFT,<SLB bad at FRESLB>,,<

Cause:	There is no Session Control Job Block (SJB) for this Session
	Control Link Block (SLB).  This error could have happened at
	any time during the life of the link after it was actively
	transferring data.

>,RTN)
	LOAD T1,SLCHN,(SL)	;GET THE CHANNEL NUMBER OF LINK
	LOAD T4,SJCHT,(P2)	;GET POINTER TO SLB TABLE IN THE SJB
	ADDI T4,-1(T1)		;FIND THE ENTRY FOR THIS CHANNEL
	CAME SL,(T4)		;MAKE SURE THERE'S ONE THERE
	BUG.(CHK,SCLTFS,SCLINK,SOFT,<Tried to free wrong SLB>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	SETZM (T4)		;ZERO CHANNEL TABLE ENTRY

	XMOVEI P1,SS.INQ+SL.NSL(SL) ;POINT TO NORMAL SUBLINK'S INPUT Q
	CALL CLNQUE		    ;GET RID OF ALL MESSAGES ON THE QUEUE
	XMOVEI P1,SS.INQ+SL.OSL(SL) ;SAME FOR OTHER SUBLINK'S INPUT Q
	CALL CLNQUE
	OPSTR <SKIPE T1,>,SLOTM,(SL) ;IF WE HAVE A PART FILLED OUTPUT MSG
	CALL DNFMSG		     ; THEN FREE IT
	OPSTR <SKIPE T1,>,SLCBP,(SL) ;IF WE HAVE AN INTERNAL CONNECT BLK
	CALL DNFWDS		     ; THEN FREE IT
	OPSTR <SKIPE T1,>,SLCDM,(SL) ;IF WE HAVE A DISCONNECT MSG BLK
	CALL DNFMSG		     ; THEN FREE IT

;Here we remove any buffer reservations attached to this SLB.

	D36OFF			;#KEEP LLINKS (SCTRIB CALL) OUT OF THIS
	LOAD T1,SLOTU,(SL)	;#GET OUTPUT BUFFERS IN USE
	LOAD T2,SLINU,(SL)	;#GET INPUT BUFFERS IN USE
	SKIPG T3,T1		;#ANY INPUT BUFFERS RESERVED?
	JUMPE T2,FRESL1		;#NO, NO RESERVATIONS IF NEITHER IN USE
	ADD T3,T2		;#GET TOTAL BUFFERS WHICH WERE IN USE
	SKIPE T1		;#WERE THERE ANY INPUTS IN USE?
	SKIPN T2		;#YES, ANY OUTPUTS?
	AOS T3			;#NO OR NO, ONE MORE WAS RESERVED FOR
	MOVNS T3		;# OTHER DIRECTION
	ADDM T3,DCNRSB		;#UPDATE RESERVED BUFFER COUNT
FRESL1:	D36ON			;#END CRITICAL SECTION

;Here we remove this SLB from the PSQ if it had a PSI waiting.

	TMNN SLPSI,(SL)		;ON THE PSI Q?
	JRST FRESL2		;NO, NO WORRY NOW
	D36OFF			;#PSI Q DOESN'T RESPECT SCT INTERLOCK
	RMVQUE SL,SJ.PSQ(P2),SL.NXP,T1 ;#REMOVE SLB FROM PSI Q
	D36ON			;#FINISHED WITH THE CRITICAL SECTION
FRESL2:

;Here we remove the SLB from the jiffy request Q if need be

	RMVQUE SL,SCTJFQ,SL.JFQ,T1 ;THIS IS INTERLOCKED

;Here we remove the SLB from the All SBLs Queue

	RMVQUE SL,SCTASQ,SL.ASQ,T1 ;THIS IS INTERLOCKED

;Here we free up the SLB's memory

	MOVE T1,SL		;SET UP FOR CALL TO DNFWDS
	SETZ SL,		;JUST FOR KICKS
	CALLRET DNFWDS		;FREE THE WORDS AND RETURN


;Routine to clean out a queue of message blks pointed to by P1.

CLNQUE:	DEQUE T1,(P1),MB.NXT,RTN ;GET NEXT MB, RETURN IF Q IS EMPTY
	CALL DNFMSG		;FREE MESSAGE POINTED TO BY T1
	JRST CLNQUE		;LOOP FOR NEXT ONE
	SUBTTL	Subroutines -- FRECBP - Free Connect Block

;FRECBP - Free Connect Block
;
; Call:
;	SL/ Pointer to SLB
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T2

;We try to free up connect data blocks in decending order of importance.
;First, we try to get rid of a message blocks which interrupt level may have
;left for process level by copying them to connect blocks.  (Interrupt level
;can't copy for fear of failing to get a connect block from free memory.)
;Whether or not we can get rid of the resulting connect block, we have freed a
;quota-controlled message block.
;Then we try to free any connect block which may be waiting, subject to the
;user's request that we keep it.

FRECBP:	SETZRO SLCCB,(SL)	;DON'T NEED TO CHECK CONNECT BLK ANY MORE
	TMNN SLCDM,(SL)		;ANY WAITING DIS/CONNECT DATA MESSAGE?
	JRST FRECB1		;NO
	CALL CDMCBP		;YES, FREE UP MESSAGE BY COPYING IT TO CBP
	  JFCL			;COULDN'T WE'LL TRY AGAIN NEXT TIME
FRECB1:	TMNN SLCBP,(SL)		;IS THERE A CONNECT BLOCK?
	RET			;NONE THERE, LEAVE NOW
	TMNE SLKCB,(SL)		;USER WANT US TO KEEP CONNECT BLOCK?
	RET			;YES, DON'T RELEASE IT.
	LOAD T2,SLSTA,(SL)	;GET LINK'S STATE
	IFNSTATE T2,RN,RTN	;IF NOT RUN STATE, MIGHT BE DISCON DATA
	LOAD T1,SLCBP,(SL)	;GET POINTER TO CONNECT BLOCK
	CALL DNFWDS		;(T1) FREE THE BLOCK
	SETZRO SLCBP,(SL)	;REMEMBER WE'VE LOST IT
	RET			;THAT'S ALL
	SUBTTL	Subroutines -- CDMCBP - Copy User Data from Message Block

;CDMCBP - Copy User Data from @SLCDM to @SLCBP
;
; Call:
;	SL/ Pointer to SLB
;
; Return:
;	RET			;IF INVALID MESSAGE, EVENT ALREADY CALLED
;	RETSKP			;ON SUCCESS
;
; Uses: T1-T2

CDMCBP:	SAVEAC <MB,MS>
	OPSTR <SKIPN MB,>,SLCDM,(SL) ;ANY DISCONNECT MESSAGE?
	RETSKP			;NO, NOTHING TO COPY
	TMNE SLCBP,(SL)		;YES, IS THERE A CONNECT BLOCK
	JRST CDMCB1		;YES, ONE OF THEM TOO, COPY THE GOODS NOW
	MOVEI T1,CB.LEN		;NO, ALLOCATE ONE FIRST
	CALL DNGWDZ		;GET A ZEROED BLOCK
	  SCERR %NEALF,RTN,<Allocation failure>
	STOR T1,SLCBP,(SL)	;STORE CONNECT BLOCK POINTER IN SLB

CDMCB1:	MOVE T1,MB		;POINT TO THE MESSAGE BLOCK
	CALL DNGINI		;SET UP TO GET SOME BYTES OUT

	SETZRO SLCDM,(SL)	;REMEMBER WE FREED IT
	CALL CPMSCB		;COPY USER DATA TO CONNECT BLOCK
	  RETSKP		;INVALID MESSAGE, WE'VE DONE BEST WE CAN
	CALL FREMSG		;DONE WITH THE DISCONNECT MESSAGE NOW
	RETSKP			;SUCCESS RETURN
	SUBTTL	Subroutines -- CPMSCB - Copy User Data from Message Block

;CPMSCB - Copy User Data from Message Block to Existing Connect Block
;
; Call:
;	SL/ Pointer to SLB, SLCBP points to Connect Block
;	MB/ Pointer to Message Block
;	MS/ Set up for D36COM message block routines to copy user data
;
; Return:
;	RET			;IF INVALID MESSAGE, EVENT ALREADY CALLED
;	RETSKP			;ON SUCCESS
;
; Uses: T1-T2

CPMSCB:	CALL DNG1BY		;GET FIRST BYTE = LENGTH OF USER DATA
	  SETZ T1,		;ZERO LENGTH IF NO LENGTH BYTE PRESENT
	CAIG T1,^D16		;LONGER THAN ARCHITECTURAL LIMIT OF 16?
	JRST CPMSC1		;NO, ITS OK
	MOVEI T1,^D16		;YES, TOO LONG, TRUNCATE TO 16 BYTES
	CALL CPMSC1		; COPY WHAT WE CAN BEFORE SCEIVM FREES MSG
	  RET			; OOPS, INVALID MESSAGE EVENT ALREADY GIVEN
	CALLRET SCEIVM		; GIVE INVALID MESSAGE EVENT

CPMSC1:	LOAD T4,SLCBP,(SL)	;T4 POINTS TO CONNECT BLOCK
	STOR T1,CBCCT,(T4)	;STORE LENGTH OF USER DIS/CONNECT DATA
	JUMPE T1,RSKP		;SUCCESS NOW IF NO USER DATA
	MOVX T2,<<POINT 8,>!1B12> ;2-WORD BYTE POINT PTR TO CONNECT BLOCK'S
	XMOVEI T3,CB.UDA(T4)	  ; USER-CONNECT-DATA BLOCK
	CALL DNCM2B		;(T1,T2,T3,MS)COPY MSG BLK TO MONITOR BUFFER
	  CALLRET SCEIVM	;TOO SHORT, GIVE INVALID MESSAGE EVENT
	RETSKP			;JUST RIGHT, SUCCESS
	SUBTTL	Subroutines -- FRESJB - Deallocate a SJB

;FRESJB - Deallocate a SJB
;
; Call:
;	T1/ Pointer to SJB
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T2

FRESJB::SAVEAC <P1,T5,T6>
	SKIPN P1,T1		;POINTER TO SJB
	RET			;JUST CHECKING
	LOAD T1,SJCHT,(P1)	;GET POINTER TO THE SLB TABLE
	LOAD T2,SJCHC,(P1)	; AND THE COUNT OF ENTRIES
	JUMPLE T2,FRESJ2	;DON'T CHECK IF COUNT IS ZERO
FRESJ1:	SKIPE (T1)		;IS THERE AN ENTRY THERE?
	BUG.(CHK,SCLTFJ,SCLINK,SOFT,<Freeing SJB with SLB entries existing>,,<

Cause:	This BUG is not documented yet.

>)
	SOSE T2			;SEE IF THERE ARE ANY MORE ENTRIES
	AOJA T1,FRESJ1		;THERE ARE, TEST THEM

FRESJ2:	D36OFF			   ;#PROTECT THE SMP
	RMVQUE P1,SCTSJQ,SJ.NXT,T1 ;#REMOVE SJB FROM LIST OF SJBS
	D36ON			   ;#END OF CRITICAL SECTION
	MOVE T1,P1		;SET UP TO FREE SOME WORDS
	CALLRET DNFWDS		;FREE THE WORDS THE SJB IS USING
	SUBTTL	Subroutines -- MAKSLB - Create a SLB, filling in defaults

;MAKSLB - Create a SLB, filling in defaults
;
; Call:
;	T1/ Channel #
;	SA/ Pointer to argument block from SCTNSF caller
;
; Return:
;	RET			;ON FAILURE DUE TO ALLOCATION
;	RETSKP			;SUCCESS WITH SL SET UP WITH THE SLB POINTER
;				; AND T1 CONTAINING THE CHANNEL NUMBER
;
; Uses: T1-T6

	CHTFAC==2		;FACTOR BY WHICH NEW CHT IS LARGER THAN OLD

MAKSLB:	SAVEAC <P1,P2>		;CHT POINTER, SJB POINTER
MAKSL1:	LOAD P2,SASJB,(SA)	;GET POINTER TO SJB
	LOAD P1,SJCHT,(P2)	;GET POINTER TO THE SLB TABLE
	LOAD T3,SJCHC,(P2)	; AND THE COUNT OF ENTRIES IN THE TABLE

MAKSL2:	SKIPN (P1)		;IS THERE A FREE ENTRY?
	JRST MAKSL3		;YES, GO USE IT
	SOSE T3			;ARE ANY MORE ENTRIES LEFT?
	AOJA P1,MAKSL2		;YES, LOOK AT THE NEXT ONE

;Here when we have to expand the SLB table in the SJB.  We will try to make
;it twice as large as it is currently.

	LOAD T1,SJCHC,(P2)	;GET THE CURRENT SIZE OF SLB TABLE
	IMULI T1,CHTFAC		;CALC NEW, LARGER SIZE
				;WE'LL STORE SIZE LATER, AFTER COPY
	CALL DNGWDZ		;GET THAT MANY WORDS
	  RET                   ;ALLOCATION FAILURE

	MOVE T2,T1		;SET UP DEST POINTER FOR DNCPYW
	OPSTRM <EXCH T1,>,SJCHT,(P2) ;STORE NEW CHT PTR, LOAD OLD CHT PTR
	LOAD T3,SJCHC,(P2)	;GET THE OLD COUNT
	PUSH P,T1		;SAVE POINTER TO OLD CHT
	CALL DNCPYW		;COPY THE OLD WORDS
	POP P,T4		;RESTORE POINTER TO OLD CHT FOR DEALLOCATION

	LOAD T2,SJCHC,(P2)	;GET OLD CHT LENGTH
	IMULI T2,CHTFAC		;CALC NEW LENGTH AGAIN
	STOR T2,SJCHC,(P2)	;STORE NEW CHT LENGTH

;Deallocate the old block if it was outside of the SJB.

	XMOVEI T2,SJ.SLT(P2)	;POINT TO THE SLB TABLE IN THE SJB
	CAME T2,T4		;IS THE SLB TABLE IN THE SLB?
	JRST [MOVE T1,T4	;NO, JUST FREE THE BLOCK
	      CALL DNFWDS	;FREE THE WORDS
	      JRST MAKSL1]	;MAKE THE NEW ENTRY UP
	MOVE T1,T2		;ZERO THE
	ADDI T2,SLT.LN-1	; ENTRIES IN THE SJB
	SETZ T3,		; JUST TO BE SAFE
	CALL DNSWDS		;SMEAR THEM
	JRST MAKSL1		; AND GO INTO THE OTHER CODE
;Here to make the new SLB.

MAKSL3:	MOVX T1,SL.LEN		;LENGTH OF A SLB
	CALL DNGWDZ		;GET THAT MANY WORDS
	  RET			;ALLOCATION FAILURE
	MOVE SL,T1		;SET UP SL WITH THE POINTER
	STOR SL,SLSLB,(SL)	;STORE POINTER TO OURSELVES FOR CHECKING
	STOR P2,SLSJB,(SL)	; AND POINTER TO SJB

;Initialize some of the variables.

	SETONE SSOTH,+SL.OSL(SL) ;SET "OTHER SUBLINK" BIT IN OTHER SUBLINK

	MOVX T1,FCM.SG		;DEFAULT FLOW CONTROL MODE IS SEGMENT
	STOR T1,SLXFL,(SL)	;SET THE TRANSMIT FLOW CONTROL TO SEGMENT
	STOR T1,SLRFL,(SL)	; AND THE RECEIVE FLOW TYPE

	SETZRO SLGOL,(SL)	;START THE GOAL AT ZERO
	MOVX T1,1
	STOR T1,SSXDO,+SL.OSL(SL) ;SET XMT DRQS ON OTHER SUBLINK TO ONE
	STOR T1,SSRDO,+SL.OSL(SL) ; ALONG WITH THE RCV DRQS

	LOAD T1,SJGOL,(P2)	;GET THE JOB'S GOAL
	STOR T1,SLGOL,(SL)	;STORE IT

	LOAD T1,SJINQ,(P2)	;GET THE JOB'S INPUT QUOTA
	STOR T1,SLINQ,(SL)	;STORE IT IN THE SLB
	LOAD T1,SJOTQ,(P2)	;SAME FOR THE OUTPUT QUOTA
	STOR T1,SLOTQ,(SL)	;STORE THAT IN THE SLB ALSO

	MOVE T1,SCTSIZ		;MAXIMUM NSP SEG SIZE
	STOR T1,SLSIZ,(SL)	;SET THAT UP ALSO

	SETZRO SLRSN,(SL)	;ZERO THE REASON FIELD

	LOAD T1,SAWKA,(SA)	;GET ADDRESS OF WAKEUP ROUTINE
	STOR T1,SLWKA,(SL)	;STORE IN THE SLB

	SETZRO SLPSM,(SL)	;START WITH PSI MASK DISABLED

	SETONE <SLBSY,SLEOM>,(SL) ;INITIALLY THE SLB IS BUSY, SEE SCTNIA
				  ; SLEOM INITIALLY TRUE FOR FIRST BOM
;After all errors have been avoided, store pointer in SLB table

	ENDQUE SL,SCTASQ,SL.ASQ,T1 ;PUT NEW SLB ON ALL SLB QUEUE

	MOVEM SL,(P1)		;SAVE POINTER TO US IN SLB TABLE
	OPSTR <SUB P1,>,SJCHT,(P2) ;CALC NEW CHANNEL NUMBER
	AOS T1,P1		;MAKE CHANNEL NUMBERS START AT ONE
	STOR P1,SLCHN,(SL)	;STORE IT IN THE SLB
	RETSKP			; AND RETURN CHANNEL # TO USER IN T1
	SUBTTL	Subroutines -- MAKSJB - Create a SJB, filling in defaults

;MAKSJB - Create a SJB, filling in defaults
;
; Call, PC in section 1 if KL paging:
;	T1/ DECnet "PDB" word (See PD Structure in D36PAR)
;
; Return:
;	RET			;ON ALLOCATION FAILURE
;	RETSKP			;OK, WITH T1 SET UP WITH SJB POINTER
;
; Uses: T1-T6

MAKSJB::SAVEAC <P1,P2,T5,T6>
	MOVE P1,T1		;SAVE THE ARGUMENT
	MOVX T1,SJ.LEN		;GET THE LENGTH OF A SJB
	CALL DNGWDZ		;GET THAT MANY WORDS
	 RET			;ALLOCATION FAILURE
	MOVE P2,T1		;SET UP THE SJB POINTER IN P2

	D36OFF			;INTERLOCK FOR SMP
	ENDQUE P2,SCTSJQ,SJ.NXT,T1 ;PUT IT ON THE QUEUE OF SJB'S
	D36ON			;END CRITICAL SECTION
	MOVX T1,SLT.LN		;LENGTH OF VIRGINAL SLB TABLE
	STOR T1,SJCHC,(P2)	;STORE THAT IN THE COUNT
	XMOVEI T1,SJ.SLT(P2)	;MAKE THE SLB TABLE POINTER POINT
	STOR T1,SJCHT,(P2)	; TO THE VIRGIN TABLE IN THE SJB

	LOAD T1,PDGOL,+P1	;GET THE JOB'S GOAL
	STOR T1,SJGOL,(P2)	;STORE IN THE SJB
	LOAD T1,PDDQT,+P1	;GET THE DEFAULT QUOTA
	OPSTR <IMUL T1,>,PDIPR,+P1 ;MULT BY INPUT % = INPUT QUOTA * 100
	IDIVI T1,^D100		; AND DIVIDE BY 100 TO FIND INPUT QUOTA
	SKIPG T1		;ASSURE INPUT
	MOVEI T1,1		; GETS AT LEAST ONE
	STOR T1,SJINQ,(P2)	;STORE THE INPUT QUOTA IN SJB
	LOAD T2,PDDQT,+P1	;GET THE TOTAL QUOTA BACK
	SUB T2,T1		;SUBTRACT INPUT QUOTA
	SKIPG T2		;ASSURE OUTPUT
	MOVEI T2,1		; GETS AT LEAST ONE
	STOR T2,SJOTQ,(P2)	;TO GET THE OUTPUT QUOTA

	MOVE T1,P2		;RETURN SJB POINTER IN T1
	RETSKP			;RETURN SUCCESS
	SUBTTL	Subroutines -- SCSSTS/CHKSTS - Set Status & Inform PSISER

;SCSSTS/CHKSTS - Set Status & Inform PSISER
;
; Call:
;	T1/ New State		;FOR SCSSTS ONLY, NO ARGS FOR CHKSTS
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6
;
;NSFDS and NSFIS use the NSNDR and NSIDR bits to determine whether they
;should send a msg, so CHKSTS is the repository of quota information.

SCSSTS:	SKIPG T1		;MAKE SURE ITS A LEGAL STATE
	BUG.(CHK,SCLILS,SCLINK,SOFT,<Illegal State at SCSSTS>,,<

Cause:	Session Control's set status routine has been called with an
	illegal status code.  SCSSTS should only be called via
	the NEWSTATE macro, which should be incapable of generating
	an illegal state code, so this call is probably spurious.

Cure:	Look on the stack to see who called SCSSTS and fix it.

>,RTN)
	STOR T1,SLSTA,(SL)	;STORE NEW STATE (NOT PART OF SLSST)
CHKSTS:				;ALTERNATE ENTRY TO CHECK BITS ONLY

;Now build a new status word from the data contained in the SLB.  The
;new status gets built in T6.

	LOAD T6,SLSTA,(SL)	;GET STATE FOR STATUS HALF-WORD
	MOVE T4,T6		;SAVE THE STATE FOR LATER

;Check for input available
	TMNE QHBEG,+SL.NSL+SS.INQ(SL) ;ANY NORMAL INPUT DATA AVAILABLE?
	TXO T6,NSNDA		;YES, SET NORMAL DATA AVAILABLE
	TMNE QHBEG,+SL.OSL+SS.INQ(SL) ;ANY OTHER INPUT DATA AVAILABLE?
	TXO T6,NSIDA		;YES, SET INTERRUPT DATA AVAILABLE

;Check for output quota
	IFNSTATE T4,<RN>,CHKST4 ;IF WE'RE NOT RUNNING, DON'T ALLOW SENDS
	D36OFF			;PROTECT FROM SCTRIB CALL FROM LLINKS
	LOAD T2,SLOTU,(SL)	;GET # OUTPUT BUFFERS CURRENTLY IN USE
	SKIPE DCNCON		;WE ARE CONGESTED,
	JUMPG T2,CHKST3		; ALLOW ONLY 1 OUTPUT/LINK
	OPSTR <CAML T2,>,SLOTQ,(SL) ;ARE WE WITHIN THE QUOTA?
	JRST CHKST3		;NO, DON'T ALLOW SENDS
	MOVEI T1,1		;ASSUME WE'RE ALREADY USING AN OUTPUT BUFFER
	JUMPG T2,CHKST1		;JUMP IF THAT'S TRUE
	MOVEI T1,2		;WE HAVE NO OUTPUT NOW, ASSUME NO INPUT
	TMNE SLINU,(SL)		;ANY INPUT BUFFERS ALLOCATED?
	JRST CHKST2		;YES, AN OUTPUT BUFFER ALREADY RESERVED
CHKST1:	MOVE T2,DCNTSB		;GET TOTAL SYSTEM BUFFERS
	SUB T2,DCNRSB		;SUBTRACT THOSE ALREADY USED
	CAMGE T2,T1		;ENOUGH ROOM FOR A NEW REQUEST?
	JRST CHKST3		;NO, DON'T ALLOW OUTPUT

;Check for output data requests
CHKST2:	LOAD T1,SLXFL,(SL)	;GET THE TRANSMIT FLOW CONTROL OPTION
	TMNN SSXDO,+SL.NSL(SL)	;NORMAL DATA REQUESTS AVAILABLE?
	CAIN T1,FCM.NO		;NO, PUNT UNLESS NO FLOW CONTROL
	TXO T6,NSNDR		;YES, SET NORMAL DATA REQUESTED
	TMNE SSXDO,+SL.OSL(SL)	;OTHER DATA REQUESTS AVAILABLE?
	TXO T6,NSIDR		;YES, SET INTERRUPT DATA REQUESTED
CHKST3:	D36ON			;END CRITICAL SECTION
CHKST4:
;Fall through with new status in T6
;From previous page with new status in T6

;Note that NSFPI calls the wake routine also, and any argument changes
;must be reflected there too.

	LOAD T5,SLSST,(SL)	;GET OLD STATUS
	STOR T6,SLSST,(SL)	;STORE NEW STATUS
	SKIPN T2,T5		;COPY OLD STATUS
	RET			;FIRST CHANGE IS NEVER "INTERESTING"
	TXZ T5,^-NSSTS		;LEAVE ONLY THE STATUS BITS
	IOR T5,T6		;BITS (NEW ! OLD) + NEW STATE
	XOR T5,T2		;REVEAL INTERESTING CHANGES AS ONES IN T5
	JUMPE T5,RTN		;LEAVE NOW IF NO INTERESTING CHANGES

;SLPSI is no longer in the following test incase we are e blocked at PSI level.
;SCTWKQ won't signal twice.

	TMNE <SLLBC,SLFSL>,(SL) ;PSI PENDING OR LINK CLOSING?
	RET			;YES, AVOID RACE

;Here if the status has changed in an interesting way
;Pick up the args for the caller's wake routine

	LOAD T1,SLPSM,(SL)	;THE PSI MASK FOR THIS LINK
	HRL T1,T2		;T1 HOLDS OLD STATUS,,MASK
	LOAD T2,SLCHN,(SL)	;THE LINK NUMBER
	HRL T2,T6		;T2 HOLDS NEW STATUS,,CHN NUMBER
	LOAD T3,SLSJB,(SL)	;@SLWKA NEEDS T3 SET UP WITH PTR TO SJB
	MOVE T4,SL		;'LINK IDENTIFIER' TO PASS TO SCTWKQ
	OPSTR <SKIPE T6,>,SLWKA,(SL) ;IS THERE A WAKE ROUTINE?
	CALLRET 0(T6)		;(T1,T2,T3,T4,T5)YES, CALL WAKE ROUTINE
	RET			;NO, JUST RETURN
	SUBTTL SCTWKQ - Queue a Link for later call to SCTPSQ

;SCTWKQ - Queue a Link for later call to SCTPSQ
;
; Call:		T1/ Link Identifier (SLB ptr) from @SLWKA call above
;
; Return:
;		RET		;Only return
;T1 = 0 on return if SLB already queued for PSI, non-zero altered
;This routine is only called by the routine whose address is in SAWKA.

SCTWKQ::LOAD T3,SLSJB,(T1)	;LOAD UP SJB POINTER FOR ENDQUE BELOW
	MOVX T2,SLPSI		;GET THE "QUEUED ON PSQ" FLAG
	D36OFF			;#THE DEQUE ROUTINE IS NOT INTERLOCKED
	TDNE T2,SL.PSI(T1)	;#IS THIS SLB ALREADY QUEUED?
	JRST [SETZ T1,
	      JRST SCTWKX]
	IORM T2,SL.PSI(T1)	;#WELL, IT IS QUEUED NOW
	ENDQUE T1,SJ.PSQ(T3),SL.NXP,T2 ;#QUEUE SLB FOR SCTPSQ
SCTWKX:	D36ON			;#INTERRUPTS BACK ON AGAIN
	RET			;ALL DONE
	SUBTTL	Subroutines -- SLBMAT - Pattern match connect to passive SLBs

;SLBMAT - Pattern match a connect over all passive SLBs
;
; Call:
;	T1/ Pointer to internal connect block (see BEGSTR CB in SCPAR)
;
; Return:
;	RET			;COULND'T FIND MATCHING PASSIVE SLB
;	RETSKP			;FOUND ONE, POINTER TO IT IN SL
;
; Uses: T1-T6

SLBMAT:	SAVEAC <P1>
	MOVE P1,T1		;SAVE THE POINTER TO THE CONNECT BLOCK
	LOAD SL,QHBEG,+SCTASQ	;GET PTR TO FIRST SLB ON ALL SLBs QUEUE
SLBMA1:	JUMPE SL,RTN		;NO MATCH IF NO MORE SLBs
	OPSTR <CAME SL,>,SLSLB,(SL) ;CHECK THE SELF POINTER
	BUG.(CHK,SCLSPS,SCLINK,SOFT,<SLB Self pointers messed up in SLBMAT>,,<

Cause:	This BUG is not documented yet.

>,SLBMA2)
	JN <SLFSL,SLLBC>,(SL),SLBMA2 ;IF LINK IS BEING CLOSED, IGNORE IT
	LOAD T1,SLSTA,(SL)	;GET THE SLB'S STATE
	IFNSTATE T1,CW,SLBMA2	;IF IT'S IN CONNECT WAIT, CHECK IT OUT
	CALL SLBMAS		;COMPARE SLB WITH RECEIVED CONNECT BLK
	  JRST SLBMA2		;NO MATCH
	RETSKP			;MATCH

SLBMA2:	LOAD SL,SLASQ,(SL)	;STEP TO NEXT SLB ON ALL SLBs Q
	JRST SLBMA1		;AND TRY IT

;We found a passive SLB.

SLBMAS:	SAVEAC <T3,T4>		;SAVE T3 AND T4 FOR A WHILE
	LOAD T1,SLCBP,(SL)	;POINT TO THE SLB'S CONNECT BLOCK
	MOVE T2,P1		;POINT TO THE INCOMING CONNECT BLOCK
	CALL CDBMAT		;PERFORM THE MATCH
	  RET			;RETURN
	RETSKP			;FOUND A MATCH, GIVE GOOD RETURN
	SUBTTL	Subroutines -- CDBMAT - Match Two Connect Blocks

;CDBMAT - Match two internal connect blocks
;
; Call:
;	T1/ Pointer to passive SLB's connect block
;	T2/ Pointer to incoming connect block
;
; Return:
;	RET			;THEY DON'T MATCH
;	RETSKP			;FOUND A MATCH
;
; Uses: T1-T6

CDBMAT:	SAVEAC <P1,P2>		;SAVE SOME PEAS
	DMOVE P1,T1		;SAVE THE POINTERS TO THE CONNECT BLOCKS

;Try to match the destination end names with each other.

	XMOVEI T5,CB.DST(P1)	;POINT TO THE DEST PB PATTERN (PASSIVE)
	XMOVEI T6,CB.DST(P2)	;POINT TO THE DEST PB SOURCE
	CALL CDBMTP		;COMPARE THE DEST END NAME DESCRIPTOR
	RET			;NOW GIVE ERROR RETURN

;Both of the end descriptors have matched.  Now let's try to match the
;remainder of the fields in the connect data.  First:

;RQSTRID

	LOAD T1,CBUCT,(P1)	;GET THE PASSIVE RQSTRID COUNT
	XMOVEI T2,CB.UID(P1)	;POINT TO THE WORD ALIGNED RQSTRID STRING
	LOAD T3,CBUCT,(P2)	;GET THE SOURCE RQSTRID COUNT
	XMOVEI T4,CB.UID(P2)	;POINT TO THE WORD ALIGNED STRING
	CALL STRMAT		;TRY TO MATCH THE STRING
	RET			;A MISMATCH

;PASSWRD

	LOAD T1,CBPCT,(P1)	;GET THE PASSIVE PASSWRD COUNT
	XMOVEI T2,CB.PSW(P1)	;POINT TO THE WORD ALIGNED PASSWRD STRING
	LOAD T3,CBPCT,(P2)	;GET THE SOURCE STRING COUNT
	XMOVEI T4,CB.PSW(P2)	;POINT TO THE WORD ALIGNED STRING
	CALL STRMAT		;TRY TO MATCH THEM
	RET			;MISMATCH

;ACCOUNT

	LOAD T1,CBACT,(P1)	;GET THE PASSIVE ACCOUNT COUNT
	XMOVEI T2,CB.ACC(P1)	;POINT TO THE WORD ALIGNED ACCOUNT STRING
	LOAD T3,CBACT,(P2)	;GET THE SOURCE STRING COUNT
	XMOVEI T4,CB.ACC(P2)	;POINT TO THE WORD ALIGNED STRING
	CALL STRMAT		;TRY TO MATCH THEM
	RET			;MISMATCH

;USRDATA

	LOAD T1,CBCCT,(P1)	;GET THE PASSIVE USER CONNECT DATA COUNT
	XMOVEI T2,CB.UDA(P1)	;POINT TO THE WORD ALIGNED USER DATA STRING
	LOAD T3,CBCCT,(P2)	;GET THE SOURCE STRING COUNT
	XMOVEI T4,CB.UDA(P2)	;POINT TO THE WORD ALIGNED STRING
	CALLRET STRMAT		;TRY TO MATCH THE STRINGS AND RETURN
;CDBMTP - Subroutine to match process descriptor blocks
;
;Call:
;	T5/ Pointer to the passive (or pattern) connect block
;	T6/ Pointer to the source (or incoming) connect block

CDBMTP:	LOAD T1,PBOBJ,(T5)	;GET THE PASSIVE GUY'S OBJECT TYPE
	OPSTR <CAME T1,>,PBOBJ,(T6) ;MUST BE THE SAME AS THE SOURCE'S
	RET			;MISMATCH

;Get the format type, do a range check and dispatch.

	LOAD T1,PBFOR,(T5)	;GET THE PASSIVE FORMAT TYPE
	OPSTR <CAME T1,>,PBFOR,(T6) ;DOES IT MATCH?
	RET			;NO, WE HAVE A MISMATCH
	CAIL T1,FRM.0		;RANGE CHECK THE
	CAILE T1,FRM.MX		; FORMAT TYPE
	BUG.(CHK,SCLBFS,SCLINK,SOFT,<bad format type not caught>,,<

Cause:	This BUG is not documented yet.

>,RSKP)
	CALLRET @.+1(T1)	;DISPATCH ON FORMAT TYPE
	   IFIW RSKP		;FORMAT TYPE 0
	   IFIW CDBMP1		;FORMAT TYPE 1
	   IFIW CDBMP2		;FORMAT TYPE 2

;Here to match format 1s. We already matched the object type, so just match
;the NAME field.

CDBMP1:	LOAD T1,PBNCT,(T5)	;GET THE PASSIVE COUNT
	XMOVEI T2,PB.NAM(T5)	;POINT TO THE WORD ALIGNED STRING
	LOAD T3,PBNCT,(T6)	;GET THE SOURCE STRING COUNT
	XMOVEI T4,PB.NAM(T6)	;POINT TO THE WORD ALIGNED STRING
	CALLRET STRMAT		;TRY TO MATCH THE STRING

;Here to match format 2s. We have already matched the object type. Compare
;the GRPCODE and USRCODE fields, allowing zeroes in either field to act as
;wildcards.

CDBMP2:

IFN <PB.USR-PB.GRP>,<
	IF2,<PRINTX ?GRPCODE and USRCODE must be in the same word>>

	MOVE T1,PB.USR(T5)	;GET THE PASSIVE USRCODE AND GRPCODE
	MOVE T2,PB.USR(T6)	; AND THE SOURCE ONE
	TLNE T1,-1		;A ZERO IN THE LEFT OF PASSIVE
	TLNN T2,-1		; OR LEFT OF SOURCE?
	HLL T1,T2		;YES, MAKE BOTH THE SAME
	TRNE T1,-1		;A ZERO IN THE RIGHT OF PASSIVE
	TRNN T2,-1		; OR RIGHT OF SOURCE?
	HRR T1,T2		;YES, MAKE BOTH THE SAME
	CAME T1,T2		;ARE THEY THE SAME?
	RET			;MISMATCH
;Now try to match the DESCRPT fields.

	LOAD T1,PBNCT,(T5)	;GET THE PASSIVE COUNT
	XMOVEI T2,PB.NAM(T5)	;POINT TO THE WORD ALIGNED STRING
	LOAD T3,PBNCT,(T6)	;GET THE SOURCE STRING COUNT
	XMOVEI T4,PB.NAM(T6)	;POINT TO THE WORD ALIGNED STRING
	CALLRET STRMAT		;TRY TO MATCH THE STRING
	SUBTTL	Subroutines -- STRMAT - Pattern Matcher

;STRMAT - Routine to perform a pattern match
;
; Call:
;	T1/ Length of pattern
;	T2/ Pointer to start of word aligned pattern string (not a BP)
;	T3/ Length of source string
;	T4/ Pointer to start of word aligned source string (not a BP)
;
; Return:
;	RET			;FAILURE TO MATCH OR TOO MANY "*"S IN PATTERN
;	RETSKP			;PATTERN MATCHED STRING
;
; Uses: T1-T6
;
;The special "pattern-matching" characters are:
;
;	?	Match the next source character.
;	*	Match zero or more next source characters.
;	^V	(Control-V) Treat the next pattern character as
;		a normal character even if it is a "?" or a "*".
;
;Note: This routine uses four words of stack for every "*" contained
;within the pattern.  Also note that we are assuming one word byte
;pointers.  This is the same algorithm that TSKSER (in TOPS-10) uses.
;Also note that a null pattern will match anything.

	P0==SL			;WE NEED ONE MORE AC
	STRMAX==^D4		;MAXIMUM LEVEL OF RECURSION

STRMAT:	JUMPE T1,RSKP		;NULL PATTERN MATCHES EVERYTHING
	SAVEAC <P0,P1,P2,P3>
	MOVE P1,T2		;KEEP WORD ALIGNED POINTER TO PATTERN IN P1
	MOVE P2,T4		; TO SOURCE STRING IN P2
	SETZ P3,		;USE P3 AS A RECURSION COUNTER
	MOVX T2,<POINT 8,(P1)>	;SET UP INDEXED BYTE POINTER TO PATTERN
	MOVX T4,<POINT 8,(P2)>	; TO SOURCE STRING ALSO
	MOVE P0,P		;SAVE THE STACK POINTER
	ADJSP P0,4		;THIS IS ZERO LEVEL

STRMT1:	ADJSP P,4		;SAVE THE
	DMOVEM T1,-3(P)		; SOURCE AND
	DMOVEM T3,-1(P)		; PATTERN DESCRIPTORS
	AOJ P3,			;INCREMENT THE RECURSION COUNTER
	CAILE P3,STRMAX		;ARE WE TOO FAR DOWN?
	JRST STRMTF		;WE FAILED

STRMT2:	SOJL T1,[SOJL T3,STRMTS	;IF BOTH STRINGS ARE NULL, RETURN SUCCESS
		 JRST STRMT3]	;IF SOURCE STRING TOO LONG, BACK UP
	ILDB T5,T2		;GET THE NEXT PATTERN CHARACTER
	CAIN T5,"*"		;SEE IF IT'S A STAR
	JRST STRMT1		;IT IS, GO RECURSE
	CAIN T5,"?"		;SEE IF IT'S A WILDCARD CHARACTER
	JRST [SOJL T3,STRMTF	;IF SO, COUNT ONE SOURCE CHAR, FAIL IF NONE
	      IBP T4		;SKIP OVER NEXT SOURCE CHAR
	      JRST STRMT2]	; AND CONTINUE MATCHING WITH NEXT CHARACTER
	CAIN T5,"V"-100		;IS IT A ^V?
	JRST [SOJL T1,STRMTF	;IF SO, COUNT OFF ON PATTERN CHARACTER
	      ILDB T5,T2	;GET THE NEXT "QUOTED" CHARACTER
	      JRST .+1]		; AND CONTINUE WITH THE MATCH
	SOJL T3,STRMTF		;COUNT OFF SOURCE CHARACTER, FAIL IF NONE
	ILDB T6,T4		;GET THE NEXT SOURCE CHARACTER
	CAIL T6,"A"+40		;IS IT
	CAILE T6,"Z"+40		; LOWER CASE?
	CAIA			;NO
	SUBI T6,40		;YES, MAKE UPPER CASE
	CAIL T5,"A"+40		;IS IT
	CAILE T5,"Z"+40		; LOWER CASE?
	CAIA			;NO
	SUBI T5,40		;YES, MAKE UPPER CASE
	XORI T5,(T6)		;COMPARE THE CHARACTERS
	TRNN T5,177		; BUT ONLY LOOK AT LOW 7 BITS
	JRST STRMT2		;IF THEY MATCH, COMPARE NEXT CHARACTERS

;Here when characters don't match.

STRMT3:	CAMN P,P0		;ARE WE AT LEVEL ZERO?
	JRST STRMTF		;NO, MATCH FAILED
	DMOVE T1,-3(P)		; PATTERN AND THE
	DMOVE T3,-1(P)		; SOURCE
	SOJL T3,STRMTF		;WE ARE PROCESSING A "*", EAT ONE MORE
	IBP T4			; SOURCE CHARACTER
	DMOVEM T3,-1(P)		;REMEMBER THE UPDATED SOURCE
	JRST STRMT2		;NO TRY TO COMPARE AGAIN

;STRMTF on failure, STRMTS on success

STRMTF:	SETO T6,		;WHEN T6 IS NON-ZERO, WE FAILED
	TRNA			;MERGE WITH OTHER SUCCESS
STRMTS:	 SETZ T6,		;SIGNAL SUCCESS
	ASH P3,2		;MULTIPLY RECURSION COUNTER BY FOUR
	MOVNS P3		;NEGATE IT
	ADJSP P,(P3)		;GO BACK TO TOP LEVEL
	ADJSP P0,-4		;BACK TO TOP LEVEL FROM ZERO LEVEL
	CAME P,P0		;I GET PARANOID ABOUT STACKS
	BUG.(CHK,SCLSMS,SCLINK,SOFT,<STRMAT messed up the stack pointer>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	JUMPN T6,RTN		;GIVE BAD RETURN ON FAILURE
	RETSKP			; AND GIVE SUCCESS RETURN
	SUBTTL	Subroutines -- STPTMR - Stop the Connect Initiate Timer

;STPTMR - Stop the Connect Initiate Timer, adjusting timer counts
;
; Call:
;	SL/ Pointer to the SLB
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1,T2
;
;This is called even when the CI timer isn't running.

STPTMR: JE SLCTM,(SL),RTN	;IF TIMER ISN'T GOING, JUST RETURN
	SETZRO SLCTM,(SL)	;ZERO THE CONNECT TIMER
	SOSGE SCTCTA		;DECREMENT THE GLOBAL TIMER COUNT
	BUG.(CHK,SCLGTN,SCLINK,SOFT,<Global connect timer count went negative>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	LOAD T2,SLSJB,(SL)	;POINT TO THE SJB
	OPSTRM <SOS T1,>,SJCTA,(T2) ;DECREMENT THE JOB'S TIMER COUNT
	JUMPGE T1,RTN		;RETURN IF COUNT IS OK
	BUG.(CHK,SCLJTN,SCLINK,SOFT,<Job connect timer count went negative>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	SUBTTL	Subroutines -- STRTMR - Start the Connect Initiate Timer

;STRTMR - Start the Connect Initiate Timer, adjusting all timer counts
;
; Call:
;	SL/ Pointer to SLB
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1,T2
;
;It is illegal to start a CI timer that has already been started.

STRTMR:	TMNE SLCTM,(SL)		;MAKE SURE TIMER IS GOING
	BUG.(CHK,SCLSTM,SCLINK,SOFT,<Trying to start a CI timer that's already going>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	CALL DNGTIM		;GET THE CURRENT TIME
	STOR T1,SLCTM,(SL)	;STORE THE CURRENT TIME
	AOS SCTCTA		;INCREMENT THE GLOBAL TIMER COUNT
	LOAD T1,SLSJB,(SL)	;GET POINTER TO SJB
	INCR SJCTA,(T1)		;INCREMENT THE JOB'S TIMER COUNT
	RET			;RETURN
	SUBTTL	Subroutines -- TMRREJ - Send a Reject with Reason RSNNRO

;TMRREJ - Reject a Connect with reason RSNNRO
;
; Call:
;	SL/ Pointer to Session Control Link Block
;
; Return:
;	RET			;ON ALLOCATION FAILURE - TRY AGAIN LATER
;	RETSKP			;ON SUCCESS - STOP THE TIMER
;
; Uses:	T1-T6

TMRREJ:	SAVEAC <MB,MS>		;SAVE SOME ACS
	TRACE SC,<Rejecting connect due to lack of response from object>
	MOVX T1,2		;WE NEED ONLY TWO BYTES
	CALL DNGMSG		;GET A MESSAGE BLOCK
	  RET			;COULDN'T GET IT, TRY AGAIN LATER
	MOVE MB,T1		;POINT TO THE MESSAGE BLOCK

	XMOVEI T1,UD.MSD(MB)	;POINT TO THE USER DATA AREA
	CALL DNPINI		;INITIALIZE TO PUT BYTES IN MESSAGE

	MOVX T1,RSNNRO		;REASON IS "NO RESPONSE FROM OBJECT"
	STOR T1,SLRSN,(SL)	;TELL USER THAT REASON AND
	CALL DNP2BY		; PUT IT IN MESSAGE TOO

	LOAD T1,SLPID,(SL)	;GET THE NSPpid
	MOVX T3,NV.REJ		;FUNCTION IS REJECT
	MOVE T4,MB		;POINT TO THE MESSAGE BLOCK
	CALL NSP		;CALL NSP TO REJECT THE CONNECT
	NEWSTATE LK		;GO INTO NO LINK STATE
	SETZRO SLPID,(SL)	;NSP HAS FORGOTTEN THIS LINK DUE TO REJECT
	RETSKP			;RETURN SUCCESS
	SUBTTL	Subroutines -- BLDCTX - Build connect message

;BLDCTX - Build the connect message from an internal connect block
;
; Call:
;	T1/ Pointer to the internal connect block (See BEGSTR CB in SCPRM)
;
; Return:
;	RET			;RANGE CHECKING FOUND ILLEGAL CONNECT INFO
;	RETSKP			;EVERYTHING'S ALRIGHT
;
; Uses: T1-T6
;
;Note: We are assuming that DNPINI has already been called and that we can
;just do DNPxBYs to dump bytes into the message.

BLDCTX:	SAVEAC <P1,P2>		;SAVE TWO PEAS
	MOVE P1,T1		;SAVE THE POINTER TO THE CONNECT BLOCK
	XMOVEI P2,CB.DST(P1)	;BUILD THE
	CALL BLDCTP		; DESTINATION END USER NAME
	 RET			;BAD FORMAT
	XMOVEI P2,CB.SRC(P1)	;BUILD THE
	CALL BLDCTP		; SOURCE END USER NAME
	 RET			;BAD FORMAT

;Here to build the MENUVER word.

	SETZB T3,T4		;BUILD MENUVER IN T4, FLAG FOR MNRPA IN T3
	OPSTR <IOR T3,>,CBUCT,(P1) ;ANY RQSTRID?
	OPSTR <IOR T3,>,CBPCT,(P1) ;ANY PASSWRD?
	OPSTR <IOR T3,>,CBACT,(P1) ;ANY ACCOUNT?
	SKIPE T3		;SHOULD WE SAY THOSE FIELDS ARE INCLUDED?
	TXO T4,MNRPA		;YES, SET BIT
	TMNE CBCCT,(P1)		;ANY USRDATA?
	TXO T4,MNUSR		;YES, SET THE BIT TO INDICATE THIS

	MOVX T1,SC.VER		;GET THE VERSION OF SESSION CONTROL
	STOR T1,MNVER,+T4	;STORE IT IN THE VERSION FIELD
	MOVE T1,T4		;PUT IT IN T1 FOR DNPEBY
	CALL DNPEBY		;PLACE THE MENUVER EXTENSIBLE BYTE IN MESSAGE

	TXNN T4,MNRPA		;WAS RQSTRID, PASSWRD OR ACCOUNT SET?
	JRST BLDCT1		;NO, DON'T INCLUDE THEM
;Now put in the User ID, Password, etc.

	LOAD T1,CBUCT,(P1)	;GET THE RQSTRID BYTE COUNT
	XMOVEI T2,CB.UID(P1)	;POINT TO THE STRING
	CALL PUTSTR		;PUT THE STRING IN THE MESSAGE

	LOAD T1,CBPCT,(P1)	;GET THE PASSWRD BYTE COUNT
	XMOVEI T2,CB.PSW(P1)	;POINT TO THE PASSWORD
	CALL PUTSTR		;PUT IT IN THE MESSAGE

	LOAD T1,CBACT,(P1)	;GET THE ACCOUNT BYTE COUNT
	XMOVEI T2,CB.ACC(P1)	;POINT TO THE ACCOUNT STRING
	CALL PUTSTR		;PUT IT IN THE MESSAGE

BLDCT1:	TXNN T4,MNUSR		;IS THERE ANY USRDATA?
	RETSKP			;NO, JUST RETURN

	LOAD T1,CBCCT,(P1)	;GET THE USRDATA BYTE COUNT
	XMOVEI T2,CB.UDA(P1)	;POINT TO THE ACCOUNT STRING
	CALL PUTSTR		;PUT IT IN THE MESSAGE
	RETSKP			; AND RETURN
;BLDCTP - Subroutine for BLDCTX to build end user name from process block
;
; Call:
;	P2/ Pointing to the process descriptor block

BLDCTP:	LOAD T1,PBFOR,(P2)	;GET THE FORMAT TYPE
	CAIL T1,FRM.0		;DO SOME
	CAILE T1,FRM.MX		; RANGE CHECKING
	RET			;BAD CONNECT BLOCK
	MOVE T4,T1		;SEMI-PRESERVE THE FORMAT
	CALL DNP1BY		;PLACE THE BYTE IN THE MESSAGE
	CALL @[IFIW BLDCP1	;DISPATCH BY THE FORMAT TYPE
	       IFIW BLDCP2
	       IFIW BLDCP4](T4)
	  RET			;ERROR RETURN
	RETSKP			;SUCCESS RETURN

;Here to process a end user name format 0.  This just consists
;of a non-zero one byte object type.

BLDCP1:	OPSTR <SKIPN T1,>,PBOBJ,(P2) ;MUST HAVE AN OBJECT TYPE
	RET			;DIDN'T - ILLEGAL CONNECT BLOCK
	CALL DNP1BY		;PLACE THE OBJECT TYPE IN
	RETSKP			; AND WE'RE DONE

;Here to process a end user name format 1.  This format is a
;zero object type followed by a I-16 name field.

BLDCP2:	JN PBOBJ,(P2),RTN	;OBJECT TYPE MUST BE ZERO
	SETZ T1,		;GET A ZERO FOR THE OBJECT TYPE
	CALL DNP1BY		;STORE OBJECT TYPE IN MESSAGE
	MOVX T3,^D16		;MAX LENGTH NAME IS 16 BYTES

BLDCP3:	LOAD T1,PBNCT,(P2)	;GET THE COUNT OF NAME BYTES
	CAMLE T1,T3		;RANGE CHECK THE LENGTH
	MOVE T1,T3		;OOPS, OVERFLOWED, USE MAXIMUM ALLOWED LENGTH
	XMOVEI T2,PB.NAM(P2)	;POINT TO THE STRING
	CALL PUTSTR		;PLACE THE STRING IN THE MESSAGE
	RETSKP			;RETURN SUCCESS

;Here to process a end user name format 2.  This format is a zero
;object type followed by GRPCODE (1B), USRCODE (1B) and DESCRPT (I-12).

BLDCP4:	JN PBOBJ,(P2),RTN	;OBJECT TYPE MUST BE ZERO
	SETZ T1,		;GET A ZERO FOR THE OBJECT TYPE
	CALL DNP1BY		;STORE OBJECT TYPE IN MESSAGE
	LOAD T1,PBGRP,(P2)	;GET THE GROUP CODE
	CALL DNP2BY		;PLACE IT IN THE MESSAGE
	LOAD T1,PBUSR,(P2)	;GET THE USER CODE
	CALL DNP2BY		;PLACE IT IN THE MESSAGE
	MOVX T3,^D12		;MAXIMUM LENGTH FOR THIS GUY IS 12 BYTES
	JRST BLDCP3		;MERGE WITH THE COMMON CODE
;PUTSTR - Subroutine to put a string from an internal block into a message
;
; Call:
;	T1/ Length of the string
;	T2/ Pointer to word aligned start of string

PUTSTR:	SAVEAC <P1,P2,T4>	;THIS IS A REALLY LOCAL ROUTINE
	MOVE P1,T2		;PUT THE POINTER IN P1
	MOVE T4,T1		;PRESERVE THE LENGTH FOR A WHILE
	CALL DNP1BY		;PLACE THE IMAGE COUNT IN THE STRING
	MOVX P2,<POINT 8,(P1)>	;BYTE POINTER TO 8 BIT STRING
	SOJGE T4,[ILDB T1,P2	;GET THE BYTE FROM CONNECT BLOCK
		  CALL DNP1BY	;PLACE THE BYTE IN THE MESSAGE
		  JRST .]	;KEEP GOING UNTIL DONE
	RET			; AND THEN RETURN
	SUBTTL	Subroutines -- PRSCTX - Parse a connect message

;PRSCTX - Parse a connect message into an internal connect block
;
; Call:
;	T1/ Pointer to internal connect block (See BEGSTR CB in SCPRM)
;	MB/ Pointer to the Message Block
;	MS/ Pointer to the current MSD
;
; Return:
;	RET			;ON MESSAGE FORMAT ERROR AFTER REPORTING EVENT
;	RETSKP			;SUCESS, INTERNAL CONNECT BLOCK IS FILLED
;
; Uses: T1-T6
;
;Note: We are assuming that DNGINI has already been called and that we can
;just do DNGxBYs to get bytes from the message.

PRSCTX:	SAVEAC <P1,P2>		;SAVE A COUPLE OF PEAS
	MOVE P1,T1		;PRESERVE THE POINTER TO THE CONNECT BLOCK
	XMOVEI P2,CB.DST(P1)	;POINT TO THE DESTINATION PROCESS BLOCK
	CALL PRSCTP		;PARSE DSTNAME PORTION OF MESSAGE
	  RET			;OOPS
	XMOVEI P2,CB.SRC(P1)	;POINT TO THE SOURCE PROCESS BLOCK
	CALL PRSCTP		;PARSE SRCNAME PORTION OF MESSAGE
	  RET			;COULDN'T DO IT

;Here to look at the MENUVER field and see what to parse.

	CALL DNG1BY		;GET MENUVER
	  RET			;ERROR, CALLER WILL CALLRET SCEIVM
	MOVE P2,T1		;PRESERVE IT
	LOAD T1,MNVER,+P2	;GET THE VERSION NUMBER
	CAIE T1,SC.VER		;SAME AS OURS?
	BUG.(CHK,SCLWVS,SCLINK,SOFT,<Incoming connect with wrong Session Control version>,,<

Cause:	This BUG is not documented yet.

>,RTN)
	TXNN P2,MNRPA		;ARE RQSTRID, PASSWRD AND ACCOUNT COMING?
	JRST PRSCT1		;NO, CHECK FOR USRDATA

;Now read the RQSTRID, PASSWRD and the ACCOUNT.

	CALL DNG1BY		;GET THE LENGTH OF THE RQSTRID
	  RET			;ERROR, CALLER WILL CALLRET SCEIVM
	CAILE T1,^D39		;IS LENGTH OK?
	RET			;ERROR, CALLER WILL CALLRET SCEIVM
	STOR T1,CBUCT,(P1)	;STORE THE BYTE COUNT
	XMOVEI T2,CB.UID(P1)	;PUT THE TEXT HERE
	CALL GETSTR		;DO IT

	CALL DNG1BY		;GET THE LENGTH OF THE PASSWRD
	  RET			;ERROR, CALLER WILL CALLRET SCEIVM
	CAILE T1,^D39		;IS LENGTH OK?
	RET			;ERROR, CALLER WILL CALLRET SCEIVM
	STOR T1,CBPCT,(P1)	;STORE THE BYTE COUNT
	XMOVEI T2,CB.PSW(P1)	;STORE THE TEXT IN
	CALL GETSTR		; THE CONNECT BLOCK

	CALL DNG1BY		;GET THE LENGTH OF THE ACCOUNT STRING
	  RET			;ERROR, CALLER WILL CALLRET SCEIVM
	CAILE T1,^D39		;IS LENGTH OK?
	RET			;ERROR, CALLER WILL CALLRET SCEIVM
	STOR T1,CBACT,(P1)	;STORE THE BYTE COUNT
	XMOVEI T2,CB.ACC(P1)	;STORE THE ACCOUNT STRING
	CALL GETSTR		; IN THE CONNECT BLOCK

;Now check for the USRDATA field.

PRSCT1:	TXNN P2,MNUSR		;IS THERE ANY USER TEXT?
	RETSKP			;NO, WE'RE DONE

	CALL DNG1BY		;GET THE LENGTH OF THE USRDATA
	  RET			;ERROR, CALLER WILL CALLRET SCEIVM
	CAILE T1,^D16		;IS LENGTH OK?
	RET			;ERROR, CALLER WILL CALLRET SCEIVM
	STOR T1,CBCCT,(P1)	;STORE THE BYTE COUNT
	XMOVEI T2,CB.UDA(P1)	;PUT THE TEXT HERE
	CALL GETSTR		;DO IT
	RETSKP			;RETURN SUCCESS
;PRSCTP - Subroutine for PRSCTX to parse end user name portion of message
;
; Call:
;	P2/ Pointing to the process descriptor block

PRSCTP:	CALL DNG1BY		;GET THE FORMAT TYPE
	  RET			;ERROR, CALLER WILL RET TO CALLRET SCEIVM
	CAIL T1,FRM.0		;RANGE CHECK
	CAILE T1,FRM.MX		;  THE FORMAT
	RET			;ERROR, CALLER WILL RET TO CALLRET SCEIVM
	STOR T1,PBFOR,(P2)	;STORE IT IN THE PD BLOCK
	MOVE T4,T1		;SORT OF PRESERVE THE FORMAT
	CALL DNG1BY		;GET THE OBJECT TYPE
	  RET			;ERROR, CALLER WILL RET TO CALLRET SCEIVM
	JRST @.+1(T4)		;DISPATCH BY FORMAT TYPE
	   IFIW PRSCP1		;FORMAT TYPE 0
	   IFIW PRSCP2		;FORMAT TYPE 1
	   IFIW PRSCP3		;FORMAT TYPE 2

;Here to parse the end user name format 0.

PRSCP1:	SKIPN T1		;IS THERE A NON-ZERO OBJECT TYPE?
	RET			;ERROR, CALLER WILL RET TO CALLRET SCEIVM
	STOR T1,PBOBJ,(P2)	;YES, STORE IT
	RETSKP			; AND WE'RE DONE

;Here to parse the end user name format 1.

PRSCP2:	CALL DNG1BY		;GET THE IMAGE COUNT
	  SETZ T1,		;ASSUME NO TEXT IF NO COUNT FIELD
	CAILE T1,^D16		;IS IT OF ACCEPTABLE LENGTH?
	RET			;ERROR, CALLER WILL RET TO CALLRET SCEIVM
	STOR T1,PBNCT,(P2)	;STORE THE COUNT OF BYTES IN NAME
	JUMPE T1,RSKP		;SUCCESS NOW IF NO STRING TO GET
	XMOVEI T2,PB.NAM(P2)	;POINT TO WERE TO PUT THE STRING
	CALL GETSTR		;GET THE STRING INTO THE PB
	RETSKP			; AND WE'RE DONE
;Here to parse the end user name format 2.

PRSCP3:	CALL DNG2BY		;GET THE GRPCODE
	  RET			;ERROR, CALLER RETS TO CALLRET SCEIVM
	STOR T1,PBGRP,(P2)	;STORE IT IN THE PB
	CALL DNG2BY		;GET THE USRCODE
	  RET			;ERROR, CALLER RETS TO CALLRET SCEIVM
	STOR T1,PBUSR,(P2)	;STORE IT IN THE PB
	CALL DNG1BY		;GET THE IMAGE LENGTH
	  SETZ T1,		;NO LENGTH FIELD, ASSUME ZERO LENGTH
	CAILE T1,^D14		;IS LENGTH OK?
	RET			;ERROR, CALLER RETS TO CALLRET SCEIVM
	STOR T1,PBNCT,(P2)	;STORE THE BYTE COUNT
	JUMPE T1,RSKP		;SUCCESS NOW IF NO MORE STRING
	XMOVEI T2,PB.NAM(P2)	;POINT TO THE NAME AREA IN THE PB
	CALL GETSTR		;PLACE THE STRING IN THE PB
	RETSKP			; AND RETURN SUCCESSFULLY
;GETSTR - Get a string from the message and place it in the connect block
;
;	T1/ Byte count
;	T2/ Pointer to the string area in the internal connect block

GETSTR:	SAVEAC <P1,P2,FREE0>
	DMOVE P1,T1		;SAVE THE BYTE COUNT AND POINTER
	MOVX FREE0,<POINT 8,(P2)> ;BYTE POINTER USING FULLWORD IN P2
	SOJGE P1,[CALL DNG1BY	;GET THE BYTE
		    CALLRET SCEIVM ;--INVALID MESSAGE EVENT
		  IDPB T1,FREE0	;STORE IN CONNECT BLOCK
		  JRST .]	; AND CONTINUE UNTIL WE RUN OUT
	RET			;RETURN

;GETSTP performs the same function but takes the data from a string.
;
; Call:
;	T1/ Count
;	T2/ Fullword Pointer
;	T3/ String Block Address (word aligned)
;
; Return:
;	RET			;WITH STRING IN BLOCK

GETSTP:	SAVEAC <P1,P2>		;SAVE FOR BYTE POINTERS
	DMOVE P1,[POINT 8,0(T2)
		  POINT 8,0(T3)];BYTE POINTERS FOR FULL WORD POINTERS
	SOJGE T1,[ILDB T4,P1	;GET A BYTE FROM STRING
		  IDPB T4,P2	;PLACE IT IN THE STRING BLOCK
		  JRST .]	; AND CONTINUE UNTIL DONE
	RET			;RETURN TO SENDER
	SUBTTL	Subroutines -- SCTA2N - Do node address to node name mapping

;SCTA2N - Do node address to node name mapping
;
; Call:
;	T1/ Node address
;
; Return:
;	RET			;THAT ADDRESS DOESN'T HAVE A NAME SET
;	RETSKP			;NODE NAME IN SIXBIT IN T1
;
;Called by NTMAN also
;
;Note that this routine must save T5 & T6 if it decides to use them.
;

SCTA2N::CAIL T1,1		;DO SOME RANGE CHECKING ON
	CAMLE T1,SCTMXN		; THE NODE ADDRESS.
	RET			;ERROR.

	ADD T1,SCTNDT		;MAKE POINTER TO THE NODE NAME
	SKIPN T1,(T1)		;GET THE NAME IF SET
	RET			;NO NAME
	RETSKP			;NAME SET, SUCCESS
	SUBTTL	Subroutines -- SCTN2A - Do node name to node address mapping

;SCTN2A - Look through the node table to find a node name
;
; Call:
;	T1/ Node name in SIXBIT
;
; Return:
;	RET			;NO SUCH NAME
;	RETSKP			;T1 CONTAINS THE NODE ADDRESS
;
;Note that if this routine uses T5 & T6 it must save them for a caller.
;

SCTN2A::JUMPE T1,RTN		;IF NO NODE NAME, DON'T BOTHER LOOKING.
	MOVE T2,SCTMXN		;MAXIMUM NODE NUMBER (SIZE OF NAME VECTOR)
	MOVE T3,SCTNDT		;POINTER TO THE NAME VECTOR
	AOJA T3,[CAMN T1,(T3)	;IS THIS NODE THE ONE WE ARE LOOKING FOR?
		JRST SCTN22	;YES, RETURN IT.
		SOJGE T2,.	;COUNT DOWN NODE NUMBER AND TRY AGAIN.
		RET]		;COUNTED ALL THE WAY DOWN, RETURN

SCTN22:	SUB T3,SCTNDT		;SUBTRACT POINTER TO VECTOR, TO GET OFFSET
	MOVE T1,T3		;AND RETURN THE ADDRESS TO USER
	RETSKP

	SUBTTL	Subroutines -- SCTAND - Add a node to the node name table

;SCTAND - Add a node to the node name table
;
; Call:
;	T1/ Node name (in sixbit). Zero to clear.
;	T2/ Node address
;
; Return:
;	RET			;NAME ALREADY TAKEN OR OUT OF RANGE
;	RETSKP			;NAME SET FOR THAT ADDRESS
;

SCTAND::SAVEAC <P1,P2>		;STORAGE FOR NODE ADDRESS AND NAME
	CAIL T2,1		;RANGE CHECK THE NODE ADDRESS
	CAMLE T2,SCTMXN		; SO WE DON'T FALL OFF
	RET			;BAD ADDRESS, BLOW HIM AWAY

SCTAN0:	DMOVE P1,T1		;SAVE FOR LATER USER
	CALL SCTN2A		;MAKE SURE THIS NAME ISN'T ALREADY IN USE
	JRST SCTAN1		;NOT NORMAL NAME, CHECK LOOPBACK NAME
	CAME T1,P2		;DID WE TRY TO RE-SET THE SAME NODE'S NAME?
	RET			;NOPE, NAME ALREADY TAKEN, FAIL
	RETSKP			;YEP, LET HIM GET AWAY WITH SETTING IT AGAIN

SCTAN1:	CALL SCTCKL		;CHECK LOOPBACK
	RET			;FAILURE, NAME ALREADY TAKEN
	ADD P2,SCTNDT		;POINT TO THE WORD IN THE VECTOR
	SKIPN (P2)		;IS THERE A CURRENT NAME FOR THIS ADDRESS ?
	AOS SCTNDC		;NO. MAYBE WE'RE ADDING ONE.
	SKIPN P1		;ARE WE CLEARING A NODE NAME ?
	SOS SCTNDC		;YES.
	MOVEM P1,(P2)		;AND STORE THE NEW NAME IN THAT WORD
	RETSKP			;RETURN SUCCESS
	SUBTTL	Subroutines -- SCTANL - Add a loopback node name

;SCTANL - Add a loopback node name to table
;
; Call
;	T1/ Node name (in sixbit). Zero to clear.
;	T2/ Circuit id
;
; Return:
;	RET			;NAME ALREADY TAKEN
;	RETSKP			;LOOPBACK NODE CREATED

SCTANL::SAVEAC <P1,P2>		;STORAGE SPACE
	DMOVE P1,T1		;SAVE ARGUMENTS
	JUMPE T2,SCTAL5		;CLEARING?
	CALL SCTN2A		;FIND OUT IF THIS NAME ALREADY EXISTS
	  CALL SCTCKL		;CHECK LOOPBACK
	  RET			;NAME ALREADY TAKEN
	MOVE T1,SCTLNL		;GET POINTER TO LIST OF LOOPBACK NODES
SCTAL1:	JUMPE T1,SCTAL2		;IF END OF LIST, ADD IT IN
	OPSTR <CAMN P2,>,LNCIR,(T1) ;MAKE SURE CIRCUIT ID DOESN'T MATCH
	RET			;CIRCUIT ALREADY HAS A LOOPBACK NODE
	LOAD T1,LNNXT,(T1)	;GET POINTER TO NEXT LOOPBACK NODE
	JRST SCTAL1		;AND LOOP

SCTAL2:	MOVX T1,LN.LST		;GET SIZE OF BLOCK WE NEED
	CALL DNGWDS		;ALLOCATE IT
	  RET			;OUT OF RESOURCES
	STOR P1,LNNAM,(T1)	;SAVE LOOPBACK NAME
	STOR P2,LNCIR,(T1)	;SAVE CIRCUIT ID
	D36OFF			;#TURN OFF DECNET
	MOVE T2,SCTLNL		;#GET POINTER TO HEAD OF LIST
	STOR T2,LNNXT,(T1)	;#STORE AS NEXT
	MOVEM T1,SCTLNL		;#AND STORE POINTER TO THIS AS HEAD
	D36ON			;#TURN DECNET BACK ON
	RETSKP			;AND RETURN SUCCESS

SCTAL5:	MOVE T2,SCTLNL		;GET POINTER TO LOOPBACK NODE LIST
	XMOVEI T3,SCTLNL	;PLACE TO BACK UP TO
SCTAL6:	JUMPE T2,RTN		;NO SUCH NODE, ERROR
	OPSTR <CAMN T1,>,LNNAM,(T2) ;IS THIS THE NODE WE ARE INTERESTED IN?
	JRST SCTAL7		;YES
	MOVE T3,T2		;SAVE POINTER TO THIS BLOCK
	LOAD T2,LNNXT,(T2)	;LOOK AT NEXT NODE
	JRST SCTAL6		;AND LOOP

SCTAL7:	LOAD T1,LNNXT,(T2)	;GET POINTER TO NEXT BLOCK
	STOR T1,LNNXT,(T3)	;SAVE IN PREDECESSOR, DELINKING THIS BLOCK
	MOVE T1,T2		;POINTER TO THIS BLOCK
	CALL DNFWDS		;RETURN THE CORE
	RETSKP			;AND DONE

	SUBTTL	Subroutines -- SCTN2L - Convert Name to loopback circuit

;SCTN2L - Convert name to loopback circuit
;
; Call
;	T1/ Node name (in sixbit).
; Return:
;	RET			;NO SUCH LOOPBACK NODE
;	RETSKP, T1/ Circuit ID

SCTN2L::MOVE T2,SCTLNL		;GET POINTER TO HEAD OF LOOPBACK NODE LIST
SCTNL4:	JUMPE T2,RTN		;END OF LIST, NO SUCH NAME
	OPSTR <CAMN T1,>,LNNAM,(T2) ;IS THIS THE NODE WE WANT?
	 JRST [	LOAD T1,LNCIR,(T2) ;YES
		RETSKP]
	LOAD T2,LNNXT,(T2)	;NOPE, TRY FOR THE NEXT
	JRST SCTNL4		;AND LOOP

	SUBTTL	Subroutines -- SCTCKL - Check for existance of loopback node

;SCTCKL - Check for existance of loopback node. Meant to be used with SCTN2A,
;	and thus has a reversed return sequence to indicate existance.
;
;Call
;	T1/ Node name (in sixbit).
;Return
;	RET			;NODE EXISTS
;	RETSKP			;NODE DOESN'T EXIST

SCTCKL::MOVE T2,SCTLNL		;GET POINTER TO HEAD OF LOOPBACK NODE LIST
SCTNL5:	JUMPE T2,RSKP		;END OF LIST, NODE DOESN'T EXIST
	OPSTR <CAMN T1,>,LNNAM,(T2) ;IS THIS THE NODE WE WANT?
	RET			;YES, DECLARE THAT IT EXISTS
	LOAD T2,LNNXT,(T2)	;GET POINTER TO NEXT
	JRST SCTNL5		;AND TRY AGAIN

	SUBTTL	Subroutines -- CPYS2M - Copy string block to message segment

;CPYS2M - Copy string block to message segment
;
; Call:
;	T1/ Pointer to SBLOCK to copy into message
;	MS/ Pointer to current message segment
;
; Return:
;	RET			;ALWAYS
;
; Uses: T1-T6
;
;Note: CPYS2I is identical except that it doesn't put a image count into
;the message (used for interrupt data).

CPYS2M:	SAVEAC P1		;SAVE A PEA
	SKIPE P1,T1		;SAVE STRING BLOCK POINTER
	LOAD T1,SBCNT,(P1)	;GET COUNT OF BYTES IN STRING
	CALL DNP1BY		;PUT IMAGE COUNT IN THE MESSAGE
	JUMPE P1,RTN		;IF NO STRING BLOCK, RETURN NOW

CPYS21:	LOAD T1,SBCNT,(P1)	;GET THE BYTE COUNT AGAIN
	MOVX T2,<<POINT 8,>!1B12> ;MAKE UP 2-WORD BYTE POINTER
	XMOVEI T3,SB.DAT(P1)	;POINT TO THE DATA
	CALLRET DNCB2M		;COPY STRING TO THE MESSAGE

;Here to copy interrupt data into the message block.

CPYS2I:	SAVEAC P1		;SAVE A PEA
	JUMPE T1,RTN		;IF THERE'S NO DATA TO COPY, JUST RETURN
	MOVE P1,T1		;PRESERVE POINTER TO THE SBLOCK
	CALLRET CPYS21		;MERGE WITH OTHER CODE
	SUBTTL	Subroutines -- CPYM2S - Copy message data to string block

;CPYM2S - Copy message data to string block
;
; Call:
;	T1/ Pointer to String Block to copy into
;	MS/ Pointer to current message segment
;	MB/ Pointer to current message block
;
; Return:
;	RET			;ON A FAILURE
;	RETSKP			;SUCCESSFUL
;
; Uses: T1-T6
;
;Note: CPYI2S performs the same function, expcept it gets the data
;length from the remaining length of the message (used for interrupt
;data).

CPYM2S:	SAVEAC <P1,P2>		;SAVE A PEA
	MOVE P1,T1		;SAVE STRING BLOCK POINTER
	MOVE T1,MB		;POINT TO MESSAGE BLOCK
	CALL DNLENG		;GET LENGTH OF THE MESSAGE
	SOS P2,T1		;SAVE LENGTH OF DATA IN MSG
	CALL DNG1BY		;GET THE IMAGE COUNT
	  JRST [SETZ T1,	   ;IF THERE WAS NO COUNT
	        STOR T1,SBCNT,(P1) ; WE SHOULD RETURN A ZERO AS
	        RETSKP]		   ; BYTE COUNT IN SBLOCK
	CAMLE T1,P2		;THAT LENGTH LOOK OK?
	RET			;NO, CALLER WILL CALLRET SCEIVM

CPYM21:	STOR T1,SBCNT,(P1)	;STORE THE REAL COUNT
	JUMPE T1,RSKP		;IF NOTHING THERE, JUST RETURN
	MOVX T2,<<POINT 8,>!1B12> ;MAKE UP 2-WORD BYTE POINTER
	XMOVEI T3,SB.DAT(P1)	;POINT TO THE TEXT PORTION
	CALLRET DNCM2B		;COPY IT TO BUFFER AND RETURN

;Here to copy interrupt data from a message block to a string block.

CPYI2S:	SAVEAC P1		;SAVE A PEA
	MOVE P1,T1		;SAVE SBLOCK POINTER
	MOVE T1,MB		;POINT TO MESSAGE BLOCK
	CALL DNLENG		;GET LENGTH OF THE MESSAGE
	CAILE T1,^D16		;LEGAL INTERRUPT MSG?
	RET			;NO, CALLER WILL CALLRET SCEIVM
	CALLRET CPYM21		;MERGE WITH OTHER CODE
	SUBTTL	Network Management -- SCEIVM - Report Invalid Message Event

;SCEIVM - Report Invalid Message Event
;
; Call:
;	MB/ Pointer to Message Block (which will be freed)
;
; Return:
;	RET			;ALWAYS (WITH AN ERROR CODE IN T1)
;
; Uses: T1-T6

SCEIVM:	EVENT(MSG,Invalid Msg in SCTL) ;BAD MESSAGE EVENT TYPE
	CALL FREMSG		;MUSTN'T FREE UNTIL AFTER EVENT MACRO
	SCERR %NEIMF,RTN,<Illegal message format detected>

;T1 set up with error code by SCERR macro, which also returns via RTN

	SUBTTL	Local SCTL Storage

	$LOW			;IMPURE STORAGE

IFN FTOPS20,<			;TOPS10 HAS THESE IN COMMON
;Schedular level routines do "SKIPL SCTLOK/RET" before proceeding
SCTLOK::EXP -1			;SCTL INTERLOCK, USED BY SCTLCQ & SCTLCW
SCTLKO:	EXP 0			;INTERLOCK OWNER
>
SCTJFF:	BLOCK 1			;NON-ZERO: SCTULK SHOULD CALL JIFFY SERVICE
SCTUCF:	BLOCK 1			;NON-ZREO: SCTULK SHOULD CALL SCIUCG
IFN FTOPS10,%%JLI==JIFSEC##	;A SECOND'S WORTH OF JIFFIES
IFN FTOPS20,%%JLI==DEC 50	;A SECOND'S WORTH OF JIFFIES
SCTJLI:	EXP %%JLI		;LONG INTERVAL IS THIS MANY JIFFIES/TICKS
	PURGE %%%JLI
SCTJLC:	BLOCK 1			;LONG INTERVAL COUNTER

SCTCTA:	BLOCK 1			;GLOBAL COUNT OF CI TIMERS ACTIVE
SCTSJQ::BLOCK QH.LEN		;QUEUE OF ALL SJBS
SCTMXN::%RTMXN			;MAXIMUM NODE NUMBER ALLOWED
SCTNDT::BLOCK 1			;POINTER TO NODE NAME TABLE
SCTNDC::BLOCK 1			;COUNT OF NODE NAMES DEFINED
SCTLNL::BLOCK 1			;LOOPBACK NODE LIST
SCTSIZ::BLOCK 1			;SCTL SEGMENT SIZE (INIT'D BY SCTINI)
SCTP2Q::%SCP2Q			;PHASE II QUOTA
SCTINT::%SCINT			;INCOMING TIMER EXPIRATION VALUE
SCTOTT::%SCOTT			;OUTGOING TIMER EXPIRATION VALUE
SCTJFQ:	BLOCK QH.LEN		;QUEUE OF SLBS NEEDING JIFFY SERVICE
SCTPRQ:	BLOCK QH.LEN		;SCTL TRANSACTION QUEUE HEADER

STRCNT:	BLOCK 1			;RECURSION COUNT FOR PATTERN MATCHER

SCTASQ:	BLOCK QH.LEN		;ALL-SLBs QUEUE HEADER

	$HIGH			;BACK TO PURE STORAGE
	SUBTTL	End of SCLINK

IFN FTOPS10, .XCMSY             ;XCREF THE MACSYM SYMBOLS
IFN FTOPS20, TNXEND		;TOPS-20 ENDING

	END