// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 SmartLogic.
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : msg_transfer_host.c
// Author        : xianfeng.du
// Created On    : 2022-06-25
// Last Modified :
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------

#include <assert.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include "pet_sm_mgt.h"
#include "mem_sections.h"
#include "ucp_utility.h"
#include "ucp_printf.h"
#include "ucp_tick.h"
#include "ucp_port.h"
#include "ucp_handshake.h"
#include "ucp_pcie_traffic.h"
#include "ucp_pcie_mem.h"
#include "msg_transfer_mem.h"
#include "msg_transfer_mbuffer.h"
#include "msg_transfer_host.h"
#include "ucp_tick.h"


static inline void msg_queue_ul_setup(uint32_t instId, uint32_t qNo, uint8_t* bufBase, uint32_t bufSize, uint32_t numOfBufs)
{
    assert(isPowerOf2(numOfBufs));

    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t *)&pMsgQueueLocalMgt->localUlQueue[instId][qNo];
    //memset((void *)ch->bufIdxBase, 0, (numOfBufs*sizeof(uint16_t)));
    //memset((void *)ch, 0, sizeof(MsgQueueLocalInfo_t));
    ch->ringSize = (uint16_t)numOfBufs;
    ch->ringMask = (uint16_t)(numOfBufs - 1);
    ch->bufBase = (uint32_t)bufBase;
    ch->bufSize = bufSize;

    MsgMemBufAttr_t attr;
    attr.qNo = (uint8_t)qNo;
    attr.state = 1;
    //attr.eop = 0;
    //attr.nSegs = 0;

    uint8_t *m = bufBase;
    MsgMemBufAttr_t *mbuf;
    for (uint32_t k = 0; k < numOfBufs; k++) {
        mbuf = (MsgMemBufAttr_t *)m;
        attr.idx = k;
        do_write(&mbuf->ele[1], attr.ele[1]);
        m += bufSize;
    }

    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueCommonInfo_t* chCommon = pPetSmLocalMgt->pUlQueue[instId][qNo];
    memcpy_ucp(chCommon, ch, sizeof(MsgQueueCommonInfo_t));

    PCIE_DEBUG_LOG("Setup UL Queue[%d] ch = 0x%x\n", qNo, (uint64_t)chCommon);
   // PCIE_DEBUG_LOG("qNo = 0x%d bufBase = 0x%x bufSize = %d numOfBufs = %d\n",qNo, (uint32_t)bufBase, bufSize, numOfBufs);

    return;
}

void msg_queue_ul_update_in(uint32_t inst_id, uint32_t que_id)
{
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t*)&pMsgQueueLocalMgt->localUlQueue[inst_id][que_id];

    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueCommonInfo_t* chCommon = pPetSmLocalMgt->pUlQueue[inst_id][que_id];

    ch->in = do_read(&chCommon->in);

    return;
}

uint8_t* msg_queue_ul_get_buf(uint32_t inst_id, uint32_t que_id)
{
#ifdef UCP_TICK_ENABLE
    uint32_t clockBegin,clockEnd;
    int32_t  clockCnt;
    rdmcycle(&clockBegin);
#endif

    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t*)&pMsgQueueLocalMgt->localUlQueue[inst_id][que_id];

    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueCommonInfo_t* chCommon = pPetSmLocalMgt->pUlQueue[inst_id][que_id];

    uint16_t idx = ch->out & ch->ringMask;
    uint16_t* pIdxBase = (uint16_t *)ch->bufIdxBase;
    uint16_t bufIdx = do_read_short(&pIdxBase[idx]);
    MsgMemBufAttr_t *mbuf = (MsgMemBufAttr_t *)get_mbuf_ptr(ch->bufBase, ch->bufSize, bufIdx);
    uint8_t *buf = (uint8_t *)&mbuf[1];
    ch->out++;
    do_write(&chCommon->out, ch->out);//only for debug

#ifdef UCP_TICK_ENABLE
    rdmcycle(&clockEnd);
    clockCnt = clockEnd - clockBegin;
    //debug_write(DBG_DDR_MSG_IDX(qNo,3),clockCnt);
    UCP_PRINT_TICK("ucp_queue_ul_get_buf,cycle:[0x%08x]",clockCnt);
#endif

    return buf;
}

void msg_queue_ul_get_done(uint32_t inst_id, uint32_t que_id, uint32_t N)
{
#ifdef UCP_TICK_ENABLE
    uint32_t clockBegin,clockEnd;
    int32_t  clockCnt;
    rdmcycle(&clockBegin);
#endif

    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t*)&pMsgQueueLocalMgt->localUlQueue[inst_id][que_id];
    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueCommonInfo_t* chCommon = pPetSmLocalMgt->pUlQueue[inst_id][que_id];

    ch->free += N;

    do_write(&chCommon->free, ch->free);//only for debug

#ifdef UCP_TICK_ENABLE
    rdmcycle(&clockEnd);
    clockCnt = clockEnd - clockBegin;
    //debug_write(DBG_DDR_MSG_IDX(qNo,4),clockCnt);
    UCP_PRINT_TICK("ucp_queue_ul_get_done,cycle:[0x%08x]",clockCnt);
#endif

    return;
}

static inline void msg_queue_dl_setup(uint32_t instId, uint32_t qNo, uint8_t* bufBase, uint32_t bufSize, uint32_t numOfBufs)
{
    assert(isPowerOf2(numOfBufs));

    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t *)&pMsgQueueLocalMgt->localDlQueue[instId][qNo];
    //memset((void *)ch, 0, sizeof(MsgQueueLocalInfo_t));
    ch->ringSize = (uint16_t)numOfBufs;
    ch->ringMask = (uint16_t)(numOfBufs - 1);
    ch->bufBase = (uint32_t)bufBase;
    ch->bufSize = bufSize;

    MsgMemBufAttr_t attr;
    attr.qNo = (uint8_t)qNo;
    attr.state = 1;
    //attr.eop = 0;
    //attr.nSegs = 0;

    uint8_t *m = bufBase;
    MsgMemBufAttr_t *mbuf;
    for (uint32_t k = 0; k < numOfBufs; k++) {
        mbuf = (MsgMemBufAttr_t *)m;
        attr.idx = k;
        do_write(&mbuf->ele[1], attr.ele[1]);
        m += bufSize;
    }

    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueCommonInfo_t* chCommon = pPetSmLocalMgt->pDlQueue[instId][qNo];
    memcpy_ucp(chCommon, ch, sizeof(MsgQueueCommonInfo_t));

    PCIE_DEBUG_LOG("Setup DL Queue[%d] ch = 0x%x\n", qNo, (uint32_t)chCommon);
    PCIE_DEBUG_LOG("ch->bufBase = 0x%x bufBase = 0x%x bufSize = %d numOfBufs = %d\n",
		(uint32_t)ch->bufBase, (uint32_t)bufBase, bufSize, numOfBufs);

    return;
}

uint8_t* msg_queue_dl_put_buf(uint32_t inst_id, uint32_t que_id)
{
#ifdef UCP_TICK_ENABLE
    uint32_t clockBegin,clockEnd;
    int32_t  clockCnt;
    rdmcycle(&clockBegin);
#endif
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t*)&pMsgQueueLocalMgt->localDlQueue[inst_id][que_id];

    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueCommonInfo_t* chCommon = pPetSmLocalMgt->pDlQueue[inst_id][que_id];

    uint32_t idx = ch->alloc & ch->ringMask;
    MsgMemBufAttr_t *mbuf = (MsgMemBufAttr_t *)get_mbuf_ptr(ch->bufBase, ch->bufSize, idx);
    uint8_t *buf =  (uint8_t *)&mbuf[1];
    ch->alloc++;
    do_write(&chCommon->alloc, ch->alloc);//only for debug

#ifdef UCP_TICK_ENABLE
    rdmcycle(&clockEnd);
    clockCnt = clockEnd - clockBegin;
    //debug_write(DBG_DDR_MSG_IDX(qNo,5),clockCnt);
    UCP_PRINT_TICK("ucp_queue_dl_put_buf,cycle:[0x%08x]",clockCnt);
#endif

    return buf;
}

void msg_queue_dl_put_done(uint32_t inst_id, uint32_t que_id, uint32_t N)
{
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t*)&pMsgQueueLocalMgt->localDlQueue[inst_id][que_id];

    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueCommonInfo_t* chCommon = pPetSmLocalMgt->pDlQueue[inst_id][que_id];

    ch->in += N;


    do_write(&chCommon->in, ch->in);
#if 0
	uint32_t comonin = do_read(&chCommon->in);
	uint32_t comonout = do_read(&chCommon->out);

    PCIE_DEBUG_LOG("msg_queue_dl_put_done,qNo[%d][%d], chCommon = 0x%x comonin[%d],comonout[%d]\n", inst_id, que_id, (uint32_t)chCommon, comonin, comonout);
    PCIE_DEBUG_LOG("msg_queue_dl_put_done,qNo[%d][%d],in[%d],out[%d]\n", inst_id, que_id, ch->in, ch->out);
#endif
    return;
}

void msg_transfer_queue_setup(uint8_t inst_id, uint8_t que_id)
{
    MEM_SECTION_INFO* pMemSection = GetMsgDdrSection();
    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();

    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    MsgQueueCfg_t* pQueueCfg = (MsgQueueCfg_t *)&pMsgQueueLocalMgt->localQueueCfg[inst_id][que_id];
    memset((void *)&pMsgQueueLocalMgt->localDlQueue[inst_id][que_id],0,sizeof(MsgQueueLocalInfo_t));
    memset((void *)&pMsgQueueLocalMgt->localUlQueue[inst_id][que_id],0,sizeof(MsgQueueLocalInfo_t));

    uint8_t* ptr;
    uint16_t block_num;
    uint32_t block_size;

    block_num = pQueueCfg->rx_queue.block_num;
    block_size = pQueueCfg->rx_queue.block_size;
    ptr = (uint8_t *)memSectionAlloc(pMemSection, block_num*block_size, MEM_ALIGNED_4BYTES, "dlAddr");
	PCIE_DEBUG_LOG("dl setup malloc bufbase = 0x%x!\n", (uint32_t)ptr);
    msg_queue_dl_setup(inst_id, que_id, ptr, block_size, block_num);

    block_num = pQueueCfg->tx_queue.block_num;
    block_size = pQueueCfg->tx_queue.block_size;

	PCIE_DEBUG_LOG("pPetSmLocalMgt->pBufIdxBase = 0x%x!\n", (uint32_t)pPetSmLocalMgt->pBufIdxBase);
    memset_ucp((void *)pPetSmLocalMgt->pBufIdxBase, 0, (block_num*sizeof(uint16_t)));
    pMsgQueueLocalMgt->localUlQueue[inst_id][que_id].bufIdxBase = (uint32_t)pPetSmLocalMgt->pBufIdxBase;
    //PCIE_DEBUG_LOG("pMsgQueueLocalMgt->localUlQueue[%d][%d]", inst_id,que_id);
   // PCIE_DEBUG_LOG(".bufIdxBase = 0x%x, pEcsSmLocalMgt->pBufIdxBase = 0x%x\n", pMsgQueueLocalMgt->localUlQueue[inst_id][que_id].bufIdxBase, (uint32_t)pEcsSmLocalMgt->pBufIdxBase);
    pPetSmLocalMgt->pBufIdxBase += block_num;
    ptr = (uint8_t *)memSectionAlloc(pMemSection, block_num*block_size, MEM_ALIGNED_4BYTES, "ulAddr");
    msg_queue_ul_setup(inst_id, que_id, ptr, block_size, block_num);

    return;
}

static inline void get_queue_info(uint16_t inst_id, uint16_t que_id, uint32_t handle_id, queue_info_s* queue_info)
{
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    MsgQueueCfg_t* pLocalQueueCfg = (MsgQueueCfg_t *)&pMsgQueueLocalMgt->localQueueCfg[inst_id][que_id];

    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueCfg_t* pCommonQueueCfg = pPetSmLocalMgt->pQueueCfg[inst_id][que_id];

    pLocalQueueCfg->handler.value = handle_id;
    pLocalQueueCfg->tx_queue.block_num = queue_info->rx_block_num;
    pLocalQueueCfg->tx_queue.block_size = queue_info->rx_block_size;
    pLocalQueueCfg->rx_queue.block_num = queue_info->tx_block_num;
    pLocalQueueCfg->rx_queue.block_size = queue_info->tx_block_size;
    //pLocalQueueCfg->rx_queue.rx_callback = queue_info->tx_callback;
    memcpy_ucp(pCommonQueueCfg, pLocalQueueCfg, sizeof(MsgQueueCfg_t));

    msg_transfer_queue_setup(inst_id, que_id);

    return;
}

static inline int32_t msg_transfer_init(uint16_t port_index, uint16_t transfer_type, uint16_t inst_id, const transfer_type_info_s* transfer_type_info)
{
    if (port_index >= MAX_PORT_NUM) {
        PCIE_DEBUG_LOG("msg_transfer_init port_index[%d] error.\n",port_index);
        return FAILURE;
    }

    if (transfer_type > OAM) {
        PCIE_DEBUG_LOG("msg_transfer_init transfer_type[%d] error.\n",transfer_type);
        return FAILURE;
    }

    if (inst_id >= MAX_INSTANCE_NUM) {
            PCIE_DEBUG_LOG("msg_transfer_init inst_id[%d] error.\n",inst_id);
            return FAILURE;
    }

    uint16_t que_id;
    HandleId_t handler;

    handler.port_id = (uint8_t)port_index;
    handler.inst_id = (uint8_t)inst_id;
    handler.type_id = (uint8_t)transfer_type;
    handler.rsv = 0;

    switch (transfer_type) {
        case CU_SPLIT:
            que_id = UCP4008_TRAFFIC_NR_eMBB_DATA;
            get_queue_info(inst_id, que_id, handler.value, (queue_info_s *)&transfer_type_info->queue_uplane_info);

            que_id = UCP4008_TRAFFIC_NR_eMBB_CTRL;
            get_queue_info(inst_id, que_id, handler.value, (queue_info_s *)&transfer_type_info->queue_cplane_info);
            break;
        case OAM:
            que_id = UCP4008_TRAFFIC_OAM;
            get_queue_info(inst_id, que_id, handler.value, (queue_info_s *)&transfer_type_info->queue_cplane_info);
            break;
        default:
            PCIE_DEBUG_LOG("msg_transfer_init doesn't support transfer_type[%d] .\n",transfer_type);
            //break;
            return FAILURE;
    }

    return (int32_t)handler.value;
}

static inline void get_msg_transfer_info(uint16_t port_index, uint16_t inst_id, uint16_t transfer_type, transfer_type_info_s* transfer_type_info_ptr)
{
    UcpPcieEpMemInfo_t* pUcpPcieMemInfo = get_ucp_pcie_mem_info();
    PcieEpMemBarStruct_t* pUcpMemBar = pUcpPcieMemInfo->pUcpMemBar;
    transfer_type_info_s* pTraffic = (transfer_type_info_s *)&pUcpMemBar->traffic[inst_id][transfer_type];
    //memcpy((void *)transfer_type_info_ptr,(void *)pTraffic,sizeof(transfer_type_info_s));

    queue_info_s c_plane = {0};
    queue_info_s u_plane = {0};

    switch (transfer_type) {
        case CU_SPLIT:
            c_plane.rx_block_size = pTraffic->queue_cplane_info.rx_block_size;
            c_plane.rx_block_num = pTraffic->queue_cplane_info.rx_block_num;
            c_plane.rx_callback = NULL;
            c_plane.rx_desc_num = pTraffic->queue_cplane_info.rx_desc_num;

            c_plane.tx_block_size = pTraffic->queue_cplane_info.tx_block_size;
            c_plane.tx_block_num = pTraffic->queue_cplane_info.tx_block_num;
            c_plane.tx_callback = NULL;
            c_plane.tx_desc_num = pTraffic->queue_cplane_info.tx_desc_num;


            u_plane.rx_block_size = pTraffic->queue_uplane_info.rx_block_size;
            u_plane.rx_block_num = pTraffic->queue_uplane_info.rx_block_num;
            u_plane.rx_callback = NULL;
            u_plane.rx_desc_num = pTraffic->queue_uplane_info.rx_desc_num;

            u_plane.tx_block_size = pTraffic->queue_uplane_info.tx_block_size;
            u_plane.tx_block_num = pTraffic->queue_uplane_info.tx_block_num;
            u_plane.tx_callback = NULL;
            u_plane.tx_desc_num = pTraffic->queue_uplane_info.tx_desc_num;
            transfer_type_info_ptr->queue_cplane_info = c_plane;
            transfer_type_info_ptr->queue_uplane_info = u_plane;
            break;
        case OAM:
            c_plane.rx_block_size = pTraffic->queue_cplane_info.rx_block_size;
            c_plane.rx_block_num = pTraffic->queue_cplane_info.rx_block_num;
            c_plane.rx_callback = NULL;
            c_plane.rx_desc_num =  pTraffic->queue_cplane_info.rx_desc_num;

            c_plane.tx_block_size = pTraffic->queue_cplane_info.tx_block_size;
            c_plane.tx_block_num = pTraffic->queue_cplane_info.tx_block_num;
            c_plane.tx_callback = NULL;
            c_plane.tx_desc_num = pTraffic->queue_cplane_info.tx_desc_num;
            transfer_type_info_ptr->queue_cplane_info = c_plane;
            break;
        default:
            PCIE_DEBUG_LOG("get_msg_queue_cfg doesn't support transfer_type[%d] .\n",transfer_type);
            break;
    }


    PCIE_DEBUG_LOG("c_plane.rx_block_size =  %d\n", c_plane.rx_block_size);
    PCIE_DEBUG_LOG("c_plane.rx_block_num =  %d\n", c_plane.rx_block_num);
    PCIE_DEBUG_LOG("c_plane.rx_desc_num =  %d\n", c_plane.rx_desc_num);

    PCIE_DEBUG_LOG("c_plane.tx_block_size =  %d\n", c_plane.tx_block_size);
    PCIE_DEBUG_LOG("c_plane.tx_block_num =  %d\n", c_plane.tx_block_num);
    PCIE_DEBUG_LOG("c_plane.tx_desc_num =  %d\n", c_plane.tx_desc_num);

    PCIE_DEBUG_LOG("u_plane.rx_block_size =  %d\n", u_plane.rx_block_size);
    PCIE_DEBUG_LOG("u_plane.rx_block_num =  %d\n", u_plane.rx_block_num);
    PCIE_DEBUG_LOG("u_plane.rx_desc_num =  %d\n", u_plane.rx_desc_num);

    PCIE_DEBUG_LOG("u_plane.tx_block_size =  %d\n", u_plane.tx_block_size);
    PCIE_DEBUG_LOG("u_plane.tx_block_num =  %d\n", u_plane.tx_block_num);
    PCIE_DEBUG_LOG("u_plane.tx_desc_num =  %d\n", u_plane.tx_desc_num);

    return;
}

static inline void checkMsgTransferCfgFlag(void)
{
#if 1
    UcpPcieEpMemInfo_t* pUcpPcieMemInfo = get_ucp_pcie_mem_info();
    PcieEpMemBarStruct_t* pUcpMemBar = pUcpPcieMemInfo->pUcpMemBar;
    SyncInfo_t* pSyncInfo = (SyncInfo_t *)&pUcpMemBar->trafficSyncInfo;
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();

    volatile uint8_t flag;
    do {
        flag = pSyncInfo->queueCfgFlag;
        if(!flag)//首次配置的时候，此标识为0，后续配置由rc进行加1操作
        {
            PCIE_DEBUG_LOG("transfer wait for rc set queueCfgFlag!  flag = %d\n", flag);
            //msleep(500);
        }
        else
        {
            break;
        }
    }while(1);//如果是首次配置，则需要等待RC配置完成
    pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag = pSyncInfo->queueCfgFlag;//将配置标志保存到本地
    //pSyncInfo->queueCfgFlag = false;
#endif
    return;
}

void msg_transfer_cfg(void)
{
    uint8_t port_id = get_ucp_port_id();
    uint16_t inst_id = 0;
    uint16_t transfer_type = 0;
    int32_t handle_id = 0;
    transfer_type_info_s transfer_type_info;

    checkMsgTransferCfgFlag();

    for (inst_id=0; inst_id<MAX_INSTANCE_NUM; inst_id++) {
        /*********************************transfer_type = CU_SPLIT**********************************/
        transfer_type = CU_SPLIT;
        get_msg_transfer_info(port_id,inst_id,transfer_type, &transfer_type_info);
        handle_id = msg_transfer_init(port_id, transfer_type, inst_id, &transfer_type_info);
        if (handle_id < 0) {
            PCIE_ERROR_LOG("phy_cfg_init transfer_type:CU_SPLIT, handle_id[0x%08x] error.\n",handle_id);
        }

        /*********************************transfer_type = OAM**********************************/
        transfer_type = OAM;
        get_msg_transfer_info(port_id,inst_id,transfer_type, &transfer_type_info);
        handle_id = msg_transfer_init(port_id, transfer_type, inst_id, &transfer_type_info);
        if (handle_id < 0) {
            PCIE_ERROR_LOG("phy_cfg_init transfer_type:OAM, handle_id[0x%08x] error.\n",handle_id);
        }
    }

    PetSmLocalMgt_t* pPetSmLocalMgt = get_pet_sm_local_mgt();
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    //pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag++;
    pPetSmLocalMgt->pSyncInfo->queueCfgFlag= pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag;

    //配置pcie bar空间的counter和traffic的初始化值
    ucp_pcie_reset_bar();

    msg_transfer_cfg_finished();
    return;
}

void msg_transfer_recfg(void)
{
    UcpPcieEpMemInfo_t* pUcpPcieMemInfo = get_ucp_pcie_mem_info();
    PcieEpMemBarStruct_t* pUcpMemBar = pUcpPcieMemInfo->pUcpMemBar;
    SyncInfo_t* pSyncInfo = (SyncInfo_t *)&pUcpMemBar->trafficSyncInfo;
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
    if (pSyncInfo->queueCfgFlag == pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag)
	{
        return;
    }
	else
	{
        pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag = pSyncInfo->queueCfgFlag;
    }

    msg_transfer_cfg();
    //重置pcie bar空间的变量值
    ucp_pcie_reset_bar();
    return;
}

//通知RC，EP配置完成，可以开始传输数据
void msg_transfer_cfg_finished(void)
{
    UcpPcieEpMemInfo_t* pUcpPcieMemInfo = get_ucp_pcie_mem_info();
    pUcpPcieMemInfo->pUcpMemBar->command[PCI_ENDPOINT_UCPEP_COMMAND] = 1;
}
//app重启配置
int msg_transfer_reset_recfg(void)
{
    UcpPcieEpMemInfo_t* pUcpPcieMemInfo = get_ucp_pcie_mem_info();
    PcieEpMemBarStruct_t* pUcpMemBar = pUcpPcieMemInfo->pUcpMemBar;
    SyncInfo_t* pSyncInfo = (SyncInfo_t *)&pUcpMemBar->trafficSyncInfo;
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
	uint32_t count = 0;
	int32_t ret = FAILURE;
	while(1)
	{
		if (pSyncInfo->queueCfgFlag == pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag) 
		{
			PCIE_ERROR_LOG("wait msg_transfer_recfg  pSyncInfo->queueCfgFlag = %d  pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag = %d\n", 
				pSyncInfo->queueCfgFlag,
				pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag); 
			count++;
			//return;
		} 
		else 
		{
			pMsgQueueLocalMgt->localSyncInfo.queueCfgFlag = pSyncInfo->queueCfgFlag;
			ret = SUCCESS;
			break;
		}
	#if 1
		if(count > 50000)
		{
			ret = FAILURE;
			break;
		}
	#endif
		ucp_nop(1000);		
	}

	if(pUcpPcieMemInfo->pUcpMemBar->command[PCI_ENDPOINT_UCPEP_RC_STATUS])
	{
		PCIE_ERROR_LOG("rc had reboot need recfg dma!\n");
		pcie_ep_dma_recfg();
		pUcpPcieMemInfo->pUcpMemBar->command[PCI_ENDPOINT_UCPEP_RC_STATUS] = RC_NG;
	}
		
	if(SUCCESS == ret)
	{
	    msg_transfer_cfg();
	    //重置pcie bar空间的变量值
	    //ucp_pcie_reset_bar();
	}
    return ret;
}


