#ident "$Id: frsfr.c,v 1.8 1997/10/21 20:48:09 gary Exp $"
/*****************************************************************************
 * Copyright (c) 1988-95 Digi International Inc., All Rights Reserved
 * 
 * This  software  contains  proprietary  and  confidential   information   of
 * Digi International  Inc.   By  accepting  transfer  of this copy, Recipient
 * agrees to retain this software in  confidence,  to  prevent  disclosure  to
 * others,  and  to make no  use of this software other than that for which it
 * was delivered.  This is an unpublished copyright work  Digi   International
 * Inc.    Except   as   permitted   by  federal  law,  17 USC 117, copying is
 * strictly prohibited.
 * 
 * Digi  International  Inc.  CONFIDENTIAL  -   (Digi    International    Inc.
 * Confidential  when   combined   with   the   aggregated  modules  for  this
 * product) OBJECT CODE ONLY SOURCE MATERIALS
 * 
 * (C) COPYRIGHT Digi International Inc. 1995
 * 
 * All Rights Reserved
 * 
 * US   Government   Users   Restricted   Rights   -   Use,   duplication   or
 * disclosure  restricted by GSA ADP Schedule Contract with Digi International
 * Inc.
 * 
 *****************************************************************************/

/*****************************************************************************
 *                                                                           *
 * (c) COPYRIGHT 1991, 1992 HARRIS & JEFFRIES, INC.                          *
 *                          888 WASHINGTON STREET                            *
 *                          SUITE 130                                        *
 *                          DEDHAM, MA  02026                                *
 * ALL RIGHTS RESERVED                                                       *
 *                                                                           *
 * This software is the property of Harris & Jeffries, Inc. and is furnished *
 * under license by Harris & Jeffries, Inc. and this software may be used    *
 * only in accordance with the terms of said license.  This copyright notice *
 * may not be removed, modified or obliterated without the prior written     *
 * permission of Harris & Jeffries, Inc.                                     *
 *                                                                           *
 * This software may not be copied, transmitted, provided to or otherwise    *
 * made available to any other person, company, corporation or other entiry  *
 * except as specified in the terms of said license.                         *
 *                                                                           *
 * No right, title, ownership or other interest in the software is hereby    *
 * granted or transferred.                                                   *
 *                                                                           *
 * The information contained herein is subject to change without notice and  *
 * should not be construed as a commitment by Harris & Jeffries, Inc.        *
 *                                                                           *
 *****************************************************************************

  MODULE NAME:


           @@@@@@  @@@@@    @@@@   @@@@@@  @@@@@            @@@@
           @       @    @  @       @       @    @          @    @
           @@@@@   @    @   @@@@   @@@@@   @    @          @
           @       @@@@@        @  @       @@@@@    @@@    @
           @       @   @   @    @  @       @   @    @@@    @    @
           @       @    @   @@@@   @       @    @   @@@     @@@@


  ORIGINAL AUTHOR:  David Jeffries
  SUBJECT MATTER 
      SPECIALIST:   Deepak Shahane

  REVISION HISTORY:

  Date     Version  Name          Description
  -------- -------  ------------  --------------------------------------------
  01-16-92 1.10A    H&J (DJ)      Original
  06-05-92 1.20A    H&J (DS)      Use CCBNULL and LCBNULL
  11-20-92 2.00E    H&J (DJ)      Remove unreferenced local variable from
                                  FRSFR_ClosePLL()

  $Log: frsfr.c,v $
  Revision 1.8  1997/10/21 20:48:09  gary
  Modifications made to accomodate the SOH (Streams-on-host) folks.

  Revision 1.7  1996/10/22 21:18:55  gary
  A bit of rearranging was required in order to move the FRF9_Stats_t
  structure over to fr_cont.h. (needed by host drivers)

  Revision 1.6  1996/06/21 21:37:32  gary
  Backflowing now implemented on the Frame Relay stack!
  This minimizes the amount of loss encountered under
  Forced and Buffered injection.

  Revision 1.5  1996/06/19 16:33:24  gary
  General cleanup and modifications so that the source files may be
  compiled using makefile v2.x.  References to the compiler flag
  "JUPITER" were replaced with "PCI".  #include <xxx> lines were
  replaced with #includ "xxx".

  Revision 1.4  1996/04/26 19:57:40  gary
  First rev of changes: Frame Relay on PRI card.
  Bug fix: sequence number on segmented frames was wrong.

  Revision 1.3  1995/04/10 16:32:02  garyp
  remove strm.h

 * Revision 1.3  1994/07/27  20:27:41  stavrosm
 * Release 2.2
 *
 * Revision 1.2  1993/11/02  11:13:39  stavrosm
 * Release 2.1
 *
..............................................................................

  DESCRIPTION:

  This module is the H&J FRS Frame Relay API.

..............................................................................

  REF DOCUMENTS:

  (a) U.S Sprint Frame Relay Service Interface Specification Doc #: 5136.02
  (b) Frame Relay Interface Specification, Stratacom Inc., Doc #: 001-207460
  (c) Frame Relay Stack Functional Specification Rel 1.2 Harris & Jeffries Inc.
  (d) Frame Relay Stack External Specification Rel 1.2 Harris & Jeffries Inc.
  (e) MIB for Frame Relay DTEs - RFC Draft from Internet Engr Task Force (IETF).
  (f) Multiprotocol Interconnect over Frame Relay Networks - T. Bradley, C.Brown
      Wellfleet Communications, Inc. A. Malis BBN Communications Oct 14, 1991.

............................................................................

  SPECIAL COMPILE/LINK INSTRUCTIONS:

        see notes/instructions in frs.h

..............................................................................

  FUNCTIONS INCLUDED in this module:

  FRSFR_ClosePLL ......... Close a Permanent Logical Link (PLL)
  FRSFR_OpenPLL .......... Open a Permanent Logical Link (PLL) for Asynch IO
  FRSFR_OpenPLLBlkIO ..... Open a Permanent Logical Link (PLL) for Synch IO
  FRSFR_XoffPLL .......... Inbound flow ctrl on a PLL
  FRSFR_XonPLL ........... Reset inbound flow ctrl on a PLL

 **************************************************************************/


 /****************************************************************************
 * This software contains proprietary and confidential information of Digi  *
 * International Inc.  By accepting transfer of this copy, Recipient agrees *
 * to retain this software in confidence, to prevent disclosure to others,  *
 * and to make no use of this software other than that for which it was     *
 * delivered.  This is an unpublished copyright work of Digi International  *
 * Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly*
 * prohibited.                                                              *
 * Copyright 1994, Digi International Inc.                                  *
 * All rights reserved.                                                     *
 *       $Id: frsfr.c,v 1.8 1997/10/21 20:48:09 gary Exp $I
 ****************************************************************************/


#define STATIC static
#define BUFP_T 	bufp_t
#define DBDIAG(MP, BYTE)    (MP)->b_datap->db_diag=BYTE;

#include "types.h"
#include "errno.h"
#include "stream.h"
#include "strlog.h"
#include "system.h"
#include "timer.h"
#include "x25trace.h"
#include "fr_cont.h"
#include "fr.h"
#include "frs.h"


/**/
/*****************************************************************************

  PROCEDURE NAME:    FRSFR_ClosePLL

  DESCRIPTION:

  This API function will close a PLL effectively releasing all resources used
  by this PLL. Further communication over the PLL will not be allowed.

  PARAMETERS:

  u_short --- PLL handle

  RETURNS:

  FRS_SUCCESS if successful.
  Else, refer API codes in frs.h.

  NOTES:

*****************************************************************************/
u_short 
FRSFR_ClosePLL(h)
u_long   h;
{
    CCB *pCCB;
printf("FRSFR_ClosePLL: ");
    if(!m_FRS_CHECK) return FRS_DISABLED;
printf("FRS_CHECK ");

    if ((h < gGlobalCfg.frsTotalPLLs) && (pCCB = frsCCBtable[h]) != CCBNULL)
    {
printf("GOT_CCB ");

	/* Nail rcvd_q field
	 */
	pCCB->rcvd_q = (queue_t *) 0 ;

        /* If PLL is indeed open...
         */
        if (m_IS_FRS_PLL_OPEN( pCCB))
        {
printf("PLL_OPEN ");
            /* Delete the CCB...
             */
            FRSUnlinkCCB( pCCB);
printf("FRS_UNLINK_CALLED ");
            frsCCBtable[h] = (CCB *)0;
        }
        else
            return FRS_CLOSED;
    }
    else
        return FRS_INVALID;

    return FRS_SUCCESS;
}

/**/
/*****************************************************************************

  PROCEDURE NAME:    FRSFR_OpenPLL

  DESCRIPTION:

  This API function will open a PLL for i/o operations. After the PLL is
  appropriately initialised, the channel will start transmiting/receiving data.

  PARAMETERS:

  struct lcb * --- link ctrl blk
  u_short --- pvc number
  LFN_PTR --- ptr to the TxDoneFunc callback function
  u_short * --- ptr to the PLL handle (return value)
  queue_t * --- ptr to the Stream Write Side Queue Owner 
  RETURNS:

  FRS_SUCCESS if successful.
  Else, refer API codes in frs.h.

  NOTES:

*****************************************************************************/

u_long
FRSFR_OpenPLL(pLCB, dlci, fTxDone, hPLL, q)
LCB *pLCB;
u_long     dlci;
BFN_PTR    fTxDone;
u_long     *hPLL;
queue_t	   *q ;
{
    CCB *pCCB;

    if(!m_FRS_CHECK) return FRS_DISABLED;

    if (pLCB == LCBNULL)
        return FRS_INVALID;

    /* If PLL is already open...
     */
    if (FRSLocateCCBbyDLCI(pLCB, dlci) != CCBNULL)
    {
	/* This is now illegal */
        return FRS_OPEN;
    }
    else
        {
        if ((pCCB = FRSCreateCCB( pLCB, dlci, TRUE)) == CCBNULL)
            return FRS_NO_SPACE;
        }
	
    pCCB->rcvd_q = RD(q) ;
    pCCB->TxDoneFunc = fTxDone;
    return (FRSFetchCCBHandle( pCCB, hPLL));
}

/**/

#if USE_BLOCKED_IO
/*****************************************************************************

  PROCEDURE NAME:    FRSFR_OpenPLLBlkIO

  DESCRIPTION:

  This API function will open a PLL for i/o operations. After the PLL is
  appropriately initialised, the channel will start transmiting/receiving data.

  When opened with this function, the PLL will support FRSFR_SendDataWait()
  and FRSFR_RcvDataWait() calls.

  PARAMETERS:

  struct lcb *              --- ptr link ctrl blk
  u_short                   --- pvc number
  LFN_PTR                   --- ptr to the TxDoneFunc callback function
  u_short *                 --- ptr to the PLL handle (return value)
  BLK_FN_PTR                --- ptr to Rcv process blocking function
  BLK_FN_PTR                --- ptr to Rcv process un-blocking function
  BLK_FN_PTR                --- ptr to Send process blocking function
  BLK_FN_PTR                --- ptr to Send process un-blocking function
  u_short                   --- user argument to pass to blockin/un-blocking
                                functions.
  
  RETURNS:

  FRS_SUCCESS if successful.
  Else, refer API codes in frs.h.

  NOTES:

*****************************************************************************/
u_long
FRSFR_OpenPLLBlkIO(pLCB,dlci,fTxDone,hPLL,fRcvBlk,fRcvUnb,fSndBlk,fSndUnb,uarg)
struct lcb *pLCB;
u_long    dlci;
BFN_PTR    fTxDone;
u_long    *hPLL;
BLK_FN_PTR fRcvBlk;
BLK_FN_PTR fRcvUnb;
BLK_FN_PTR fSndBlk;
BLK_FN_PTR fSndUnb;
u_int      uarg;
{
    u_long rcode = FRSFR_OpenPLL(pLCB, dlci, fTxDone, hPLL);
    CCB *pCCB;

    if (rcode == FRS_SUCCESS)
    {
        pCCB = FRSLocateCCBbyDLCI(pLCB, dlci);
        pCCB->fReadBlock = fRcvBlk;
        pCCB->fReadUnblock = fRcvUnb;
        pCCB->fWriteBlock = fSndBlk;
        pCCB->fWriteUnblock = fSndUnb;
        pCCB->uarg = uarg;
    }

    return(rcode);
}
#endif

/* if USE_BLOCKED_IO */

/**/
/*****************************************************************************

  PROCEDURE NAME:    FRSFR_XoffPLL

  DESCRIPTION:

  This API function will cause flow control to be actuated on a PLL. All
  outbound frames will carry the BECN flag set until flow control conditions
  are reset using the FRSFR_XonPLL call.

  PARAMETERS:

  struct lcb * --- Ptr to a link ctrl blk

  RETURNS:

  FRS_SUCCESS if successful.
  Else, refer API codes in frs.h.

  NOTES:

  It is intended that this function be used by a user application (process)
  to signal the network of a congestion problem being faced by the
  application. Since the application itself is the best judge of a congestion
  state, FRS delegates this decision to the application and provides this
  function as a means of activating/deactivating flow control.

*****************************************************************************/
u_long
FRSFR_XoffPLL(pLCB,dlci)
LCB *pLCB;
u_long    dlci;
{
    CCB *pCCB;

    if(!m_FRS_CHECK) return FRS_DISABLED;

    /* Locate CCB and mark BECN...
     */
    if (pLCB != LCBNULL)
        if ((pCCB = FRSLocateCCBbyDLCI(pLCB, dlci)) != CCBNULL)
        {
            mSetBECN(pCCB);
            return FRS_SUCCESS;
        }

    return FRS_INVALID;
}

/**/
/*****************************************************************************

  PROCEDURE NAME:    FRSFR_XonPLL

  DESCRIPTION:

  This procedure will deactivate flow control on a PLL.

  PARAMETERS:

  struct lcb * --- Ptr to a link ctrl blk

  RETURNS:

  FRS_SUCCESS if successful.
  Else, refer API codes in frs.h.

  NOTES:

  It is intended that this function be used by a user application (process)
  to signal the network of a congestion problem being faced by the
  application. Since the application itself is the best judge of a congestion
  state, FRS delegates this decision to the application and provides this
  function as a means of activating/deactivating flow control.

*****************************************************************************/
u_long
FRSFR_XonPLL(pLCB,dlci)
LCB *pLCB;
u_long    dlci;
{
    CCB *pCCB;

    if(!m_FRS_CHECK) return FRS_DISABLED;

    /* Locate CCB and mark BECN...
     */
    if (pLCB != LCBNULL)
        if ((pCCB = FRSLocateCCBbyDLCI(pLCB, dlci)) != CCBNULL)
        {
            mResetBECN(pCCB);
            return FRS_SUCCESS;
        }

    return FRS_INVALID;
}
