/* =========================================================================
 *  Copyright 2019-2022 NXP
 *
 *  NXP Confidential. This software is owned or controlled by NXP and may only
 *  be used strictly in accordance with the applicable license terms. By
 *  expressly accepting such terms or by downloading, installing, activating
 *  and/or otherwise using the software, you are agreeing that you have read,
 *  and that you agree to comply with and are bound by, such license terms. If
 *  you do not agree to be bound by the applicable license terms, then you may
 *  not retain, install, activate or otherwise use the software.
 *  <<< PFE Restricted Software >>>
 *
 * ========================================================================= */

/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/
#define PFE_SRC_VERSION_CHECK 43100440

#include "pfe_cfg.h"
#include "Mcal.h"
#include "SchM_Eth_43_PFE.h"
#include "oal_mutex_autosar.h"
#include "oal.h"

#define PFE_MAC_DB_MUTEX_SIZE                            50U
#define PFE_LOG_IF_MUTEX_SIZE                            50U
#define PFE_BLALLOC_MUTEX_SIZE                           8U
#define PFE_TS_QUEUE_WRITE_MUTEX_SIZE                    24U
#define PFE_CHNL_MUTEX_SIZE                              15U
#define PFE_PE_MUTEX_SIZE                                9U
#define PFE_HIF_PTP_TS_DB_MUTEX_SIZE                     5U

#define PFE_MAC_DB_MUTEX_START                           0U
#define PFE_MAC_DB_MUTEX_STOP                            (PFE_MAC_DB_MUTEX_START + PFE_MAC_DB_MUTEX_SIZE)
#define PFE_LOG_IF_MUTEX_START                           PFE_MAC_DB_MUTEX_STOP
#define PFE_LOG_IF_MUTEX_STOP                            (PFE_LOG_IF_MUTEX_START + PFE_LOG_IF_MUTEX_SIZE)
#define PFE_BLALLOC_MUTEX_START                          PFE_LOG_IF_MUTEX_STOP
#define PFE_BLALLOC_MUTEX_STOP                           (PFE_BLALLOC_MUTEX_START + PFE_BLALLOC_MUTEX_SIZE)
#define PFE_TS_QUEUE_WRITE_MUTEX_START                   PFE_BLALLOC_MUTEX_STOP
#define PFE_TS_QUEUE_WRITE_MUTEX_STOP                    (PFE_TS_QUEUE_WRITE_MUTEX_START + PFE_TS_QUEUE_WRITE_MUTEX_SIZE)
#define PFE_CHNL_MUTEX_START                             PFE_TS_QUEUE_WRITE_MUTEX_STOP
#define PFE_CHNL_MUTEX_STOP                              (PFE_CHNL_MUTEX_START + PFE_CHNL_MUTEX_SIZE)
#define PFE_PE_MUTEX_START                               PFE_CHNL_MUTEX_STOP
#define PFE_PE_MUTEX_STOP                                (PFE_PE_MUTEX_START + PFE_PE_MUTEX_SIZE)
#define PFE_HIF_PTP_TS_DB_MUTEX_START                    PFE_PE_MUTEX_STOP
#define PFE_HIF_PTP_TS_DB_MUTEX_STOP                     (PFE_HIF_PTP_TS_DB_MUTEX_START + PFE_HIF_PTP_TS_DB_MUTEX_SIZE)

#define ETH_43_PFE_START_SEC_VAR_INIT_BOOLEAN
#include "Eth_43_PFE_MemMap.h"

bool_t mutex_alloc[MUTEX_POOL_SIZE] = {FALSE};

#define ETH_43_PFE_STOP_SEC_VAR_INIT_BOOLEAN
#include "Eth_43_PFE_MemMap.h"

#define ETH_43_PFE_START_SEC_CODE
#include "Eth_43_PFE_MemMap.h"

errno_t oal_mutex_init_internal(oal_mutex_t *mutex)
{
    uint16_t index;
    errno_t retval = -1;

    switch (*mutex)
    {
        case PFE_MAC_DB_MUTEX:
        {
            for (index = PFE_MAC_DB_MUTEX_START; index < PFE_MAC_DB_MUTEX_STOP; index++)
            {
                if (FALSE == mutex_alloc[index])
                {
                    mutex_alloc[index] = TRUE;
                    *mutex = index;
                    retval = EOK;
                    break;
                }
            }
            break;
        }
        case PFE_LOG_IF_MUTEX:
        {
            for (index = PFE_LOG_IF_MUTEX_START; index < PFE_LOG_IF_MUTEX_STOP; index++)
            {
                if (FALSE == mutex_alloc[index])
                {
                    mutex_alloc[index] = TRUE;
                    *mutex = index;
                    retval = EOK;
                    break;
                }
            }
            break;
        }
        case PFE_BLALLOC_MUTEX:
        {
            for (index = PFE_BLALLOC_MUTEX_START; index < PFE_BLALLOC_MUTEX_STOP; index++)
            {
                if (FALSE == mutex_alloc[index])
                {
                    mutex_alloc[index] = TRUE;
                    *mutex = index;
                    retval = EOK;
                    break;
                }
            }
            break;
        }
        case PFE_TS_QUEUE_WRITE_MUTEX:
        {
            for (index = PFE_TS_QUEUE_WRITE_MUTEX_START; index < PFE_TS_QUEUE_WRITE_MUTEX_STOP; index++)
            {
                if (FALSE == mutex_alloc[index])
                {
                    mutex_alloc[index] = TRUE;
                    *mutex = index;
                    retval = EOK;
                    break;
                }
            }
            break;
        }
        case PFE_CHNL_A_LOCK_MUTEX:
        case PFE_CHNL_LOCK_MUTEX:
        case PFE_CHNL_RX_LOCK_MUTEX:
        {
            for (index = PFE_CHNL_MUTEX_START; index < PFE_CHNL_MUTEX_STOP; index++)
            {
                if (FALSE == mutex_alloc[index])
                {
                    mutex_alloc[index] = TRUE;
                    *mutex = index;
                    retval = EOK;
                    break;
                }
            }
            break;
        }
        case PFE_PE_MUTEX:
        {
            for (index = PFE_PE_MUTEX_START; index < PFE_PE_MUTEX_STOP; index++)
            {
                if (FALSE == mutex_alloc[index])
                {
                    mutex_alloc[index] = TRUE;
                    *mutex = index;
                    retval = EOK;
                    break;
                }
            }
            break;
        }
        case PFE_HIF_PTP_TS_DB_MUTEX:
        {
            for (index = PFE_HIF_PTP_TS_DB_MUTEX_START; index < PFE_HIF_PTP_TS_DB_MUTEX_STOP; index++)
            {
                if (FALSE == mutex_alloc[index])
                {
                    mutex_alloc[index] = TRUE;
                    *mutex = index;
                    retval = EOK;
                    break;
                }
            }
            break;
        }
        case PFE_EMAC1_MUTEX:
        case PFE_EMAC2_MUTEX:
        case PFE_EMAC3_MUTEX:
        case PFE_EMAC1_TS_MUTEX:
        case PFE_EMAC2_TS_MUTEX:
        case PFE_EMAC3_TS_MUTEX:
        case PFE_L2BR_TABLE_MUTEX:
        case PFE_BRIDGE_MUTEX:
        case PFE_RTABLE_LOCK_MUTEX:
        case PFE_IF_DB_CONTEXT_MUTEX:
        case PFE_PHY_IF_ID_EMAC0_MUTEX:
        case PFE_PHY_IF_ID_EMAC1_MUTEX:
        case PFE_PHY_IF_ID_EMAC2_MUTEX:
        case PFE_PHY_IF_ID_HIF_NOCPY_MUTEX:
        case PFE_PHY_IF_ID_UTIL_MUTEX:
        case PFE_PHY_IF_ID_HIF0_MUTEX:
        case PFE_PHY_IF_ID_HIF1_MUTEX:
        case PFE_PHY_IF_ID_HIF2_MUTEX:
        case PFE_PHY_IF_ID_HIF3_MUTEX:
        case PFE_HIF_DRV_MUTEX:
        case PFE_CL_API_LOCK_MUTEX:
        case PFE_IDEX_MUTEX:
        case PFE_OAL_UTIL_AUTOSAR_MUTEX:
        case PFE_HIF_RX_JOB_MUTEX:
        case PFE_HIF_TX_JOB_MUTEX:
        case PFE_FCI_FIFO_MUTEX:
        case PFE_FCI_CONTEXT_DB_MUTEX:
        case PFE_FCI_OWNER_MUTEX:
        {
            if (FALSE == mutex_alloc[*mutex])
            {
                mutex_alloc[*mutex] = TRUE;
                retval = EOK;
            }
            else
            {
                NXP_LOG_ERROR("Only one critical area is available for current component\n");
            }
            break;
        }
        default:
            NXP_LOG_ERROR("Invalid resource request mutexes\n");
            break;
    }

    return retval;
}

errno_t oal_mutex_destroy_internal(oal_mutex_t *mutex)
{
    errno_t retval = EINVAL;

    if (MUTEX_POOL_SIZE > *mutex)
    {
        if(TRUE == mutex_alloc[*mutex])
        {
            mutex_alloc[*mutex] = FALSE;
            *mutex = MUTEX_POOL_SIZE; /* Invalid value */
            retval = EOK;
        }
    }
    else
    {
        NXP_LOG_ERROR("Invalid mutex id passed to oal_mutex_destroy\n");
    }
    return retval;
}

#define ETH_43_PFE_STOP_SEC_CODE
#include "Eth_43_PFE_MemMap.h"

