// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 SmartLogic.
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : main.c
// Author        : xianfeng.du
// Created On    : 2022-06-25
// Last Modified :
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------
#include "typedef.h"
#include "osp_task.h"
#include "osp_timer.h"
#include "ucp_printf.h"
#include "spu_log.h"

#include "ucp_drv_common.h"
//#include "pet_rfm_spu0_top.h"
//#include "msg_transfer_host.h"
#include "msg_transfer_layer.h"
//#include "ucp_pcie_traffic.h"
#include "ucp_printf.h"
#include "msg_transfer_mem.h"
#include "msg_transfer_mbuffer.h"
#include "ucp_drv_common.h"

#define RC_OAM_MSG 0

typedef struct SPU_SW_MSG_INFO1
{
    uint32_t u32DataLen;
    uint32_t u32PktType;
    uint32_t u32CoreId;    
    uint32_t u32MsgType;
}spu1_info_t;

typedef struct tOamMsgTransferHeader {
	uint8_t numMsg;
	uint8_t cellIndex;
	uint16_t rsv;
	uint32_t msgType;
	uint32_t msgLen;
	uint32_t msgData[0];
} OamMsgTransferHeader_t;

typedef struct tSpuOamBaseDelayQryRsp {
	uint8_t  u8fiber_port;
    uint8_t  u8result;
    uint8_t  u8rsv[2];
	uint32_t u32dl_frame_offset;
	uint32_t u32ul_frame_offset;
} SpuOamBaseDelayQryRsp_t;


static uint32_t g_ape0 = 0;
static uint32_t g_ape1 = 0;
static OamMsgTransferHeader_t *g_oam_msg_ptr = NULL;

uint32_t pcie_rx_callback_data(const char* buf,uint32_t payloadSize)
{
	char tmplogbuf[1024] = {0};
	if(payloadSize < 1024)
	{
		memcpy_ucp(tmplogbuf, buf, payloadSize);
	}
	else
	{
		memcpy_ucp(tmplogbuf, buf, 1024);
	}
	UCP_PRINT_LOG("received buf : %s\n", tmplogbuf);

    return payloadSize;
}

uint32_t pcie_rx_callback_ctrl(const char* buf,uint32_t payloadSize)
{
	char tmplogbuf[1024] = {0};
	if(payloadSize < 1024)
	{
		memcpy_ucp(tmplogbuf, buf, payloadSize);
	}
	else
	{
		memcpy_ucp(tmplogbuf, buf, 1024);
	}
	UCP_PRINT_LOG("received buf : %s\n", tmplogbuf);

    return payloadSize;
}

uint32_t pcie_rx_callback_oam(const char* buf,uint32_t payloadSize)
{
	char tmplogbuf[1024] = {0};
	if(payloadSize < 1024)
	{
		memcpy_ucp(tmplogbuf, buf, payloadSize);
	}
	else
	{
		memcpy_ucp(tmplogbuf, buf, 1024);
	}
	UCP_PRINT_LOG("received buf : %s\n", tmplogbuf);

    return payloadSize;
}

void test_case_cfgpar_pcie()
{
	int i = 0;

	for(i = 0; i < MAX_INSTANCE_NUM; i++)
	{
		msg_transfer_callback_register(CU_SPLIT, i, C_PLANE, pcie_rx_callback_ctrl);
		msg_transfer_callback_register(CU_SPLIT, i, U_PLANE, pcie_rx_callback_data);
		msg_transfer_callback_register(OAM, i, C_PLANE, pcie_rx_callback_oam);
	}
}

int8_t get_id(uint16_t type_id, uint16_t cu_flag)
{
	int8_t que_id = -1;

	switch (type_id) {
		case CU_SPLIT:
			if (cu_flag == U_PLANE) {
				que_id = UCP4008_TRAFFIC_NR_eMBB_DATA;
			} else {
				que_id = UCP4008_TRAFFIC_NR_eMBB_CTRL;
			}
			break;
		case OAM:
			que_id = UCP4008_TRAFFIC_OAM;
			break;
		default:
			UCP_PRINT_ERROR("get_queue_id doesn't support transfer_type[%d] .",type_id);
			break;
	}

	return que_id;
}

//static uint32_t count = 0;

void test_case_sendmsg_pcie()
{
    uint32_t size = 512-16;
    char* buf = NULL;
    uint32_t availableSize;

	uint16_t cu_flag = C_PLANE;
	HandleId_t handler;
	handler.port_id = 0;
	handler.inst_id = 0;
	handler.type_id = CU_SPLIT;
	uint32_t slot_ind_flag = 1;
	int ret = 0;
	
	uint32_t offset = 0;
	uint32_t i = 0;
	char templogbuf[256] = {0};
	
	int8_t que_id = 0;
	MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
	MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t *)&pMsgQueueLocalMgt->localDlQueue[0][1];	
	//uint32_t core_id = get_core_id();
	//uint32_t bufsize =  ch->bufSize - MSG_MBUF_HEAD_SIZE;
	/****************************test code************************************************/
	for(i = 0; i < MAX_INSTANCE_NUM; i++)
	{
		//debug_write(DBG_DDR_COMMON_IDX(core_id, 2), ++count);
		handler.port_id = 0;
		handler.inst_id = i;
		handler.type_id = CU_SPLIT; 	
		if(1 == slot_ind_flag)
		{
		   // slot_ind_flag = 0;
			ret = msg_transfer_send_start(handler.value);
			/************C_PLANE***************/
			cu_flag = C_PLANE;
			que_id = get_id(handler.type_id, cu_flag);
			ch = (MsgQueueLocalInfo_t *)&pMsgQueueLocalMgt->localDlQueue[i][que_id];
			size =  ch->bufSize-MSG_MBUF_HEAD_SIZE;				
			ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
			if ( SUCCESS != ret) {
				UCP_PRINT_ERROR("alloc msg failed!\n");
				continue;
			}
			//size = availableSize;
			//buf = *bufAddr;
			UCP_PRINT_ERROR("msg transfer alloced ul msg C_PLANE buf = 0x%x!\n", buf);
			memset(templogbuf, 0x00, sizeof(templogbuf));				
			sprintf(templogbuf, "%s", "this is test for ul CU_SPLIT ctrl transfer!\n");
			memcpy_ucp(buf, templogbuf, strlen(templogbuf)+1);
			/*
			*(uint32_t*)(ptr + 0) = 0x12345678;
			*(uint32_t*)(ptr + 4) = 0x5a5a5a5a;
			*(uint32_t*)(ptr + 8) = 0x12345678;
			*(uint32_t*)(ptr + 12) = 0xa5a5a5a5;
			*/
			//PCIE_DEBUG_LOG("start enter msg_transfer_send_msg\n");
			ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);
			if(ret != SUCCESS)
			{
				UCP_PRINT_ERROR("ul msg_transfer_send_msg CU_SPLIT ctrl  failed! ret = %d\n", ret);
			}	
	
			/************U_PLANE***************/
			cu_flag = U_PLANE;
			que_id = get_id(handler.type_id, cu_flag);
			ch = (MsgQueueLocalInfo_t *)&pMsgQueueLocalMgt->localDlQueue[i][que_id];
			size =  ch->bufSize-MSG_MBUF_HEAD_SIZE;				
			
			ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
			if ( SUCCESS != ret) {
				UCP_PRINT_ERROR("alloc msg failed!\n");
				continue;
			}
			//size = availableSize;
			//buf = *bufAddr;
			UCP_PRINT_ERROR("msg transfer alloced ul U_PLANE msg buf = 0x%x!\n", buf);
			memset(templogbuf, 0x00, sizeof(templogbuf));
			sprintf(templogbuf, "%s", "this is test for ul CU_SPLIT data transfer!\n");
			memcpy_ucp(buf, templogbuf, strlen(templogbuf)+1);
			
			/*
			ptr = (uint8_t *)buf;
			*(uint32_t*)(ptr + 0) = 0x12345678;
			*(uint32_t*)(ptr + 4) = 0x5a5a5a5a;
			*(uint32_t*)(ptr + 8) = 0x12345678;
			*(uint32_t*)(ptr + 12) = 0xa5a5a5a5;
			*/
			ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);
			if(ret != SUCCESS)
			{
				UCP_PRINT_ERROR("ul msg_transfer_send_msg CU_SPLIT data failed! ret = %d\n", ret);
			}
	
			ret = msg_transfer_send_end(handler.value);
			
		}
	
		handler.type_id = OAM;	
		if(1 == slot_ind_flag)
		{
		   // slot_ind_flag = 0;
		
			ret = msg_transfer_send_start(handler.value);
		
			/************C_PLANE***************/
			cu_flag = C_PLANE;
			que_id = get_id(handler.type_id, cu_flag);
			ch = (MsgQueueLocalInfo_t *)&pMsgQueueLocalMgt->localDlQueue[i][que_id];
			size =  ch->bufSize-MSG_MBUF_HEAD_SIZE;				
			
			ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
			if ( SUCCESS != ret) {
				UCP_PRINT_ERROR("alloc msg failed!\n");
				continue;
			}
			//size = availableSize;
			//buf = *bufAddr;
			UCP_PRINT_ERROR("msg transfer alloced msg OAM buf = 0x%x!\n", buf);
			memset(templogbuf, 0x00, sizeof(templogbuf));				
			sprintf(templogbuf, "%s", "this is test for ul OAM ctrl transfer!\n");
			memcpy_ucp(buf, templogbuf, strlen(templogbuf)+1);
			
			/*
			*(uint32_t*)(ptr + 0) = 0x12345678;
			*(uint32_t*)(ptr + 4) = 0x5a5a5a5a;
			*(uint32_t*)(ptr + 8) = 0x12345678;
			*(uint32_t*)(ptr + 12) = 0xa5a5a5a5;
			*/
			ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);
			if(ret != SUCCESS)
			{
				UCP_PRINT_ERROR("ul msg_transfer_send_msg failed! CU_SPLIT oam !	ret = %d\n", ret);
				continue;
			}
			
			UCP_PRINT_ERROR("msg_transfer_send_msg end!\n");
			ret = msg_transfer_send_end(handler.value);
			
		}			
	}
	/*********************************************************************************/

}

void test_case_recv_msg_pcie()
{
	uint16_t cu_flag = C_PLANE;
	HandleId_t handler;
	handler.port_id = 0;
	handler.inst_id = 0;
	handler.type_id = CU_SPLIT;
	
	uint8_t port_id = 0;
	uint32_t offset = 0;
	uint32_t len = 0;
	uint8_t* msg_ptr;	
	uint32_t i = 0;	
	
	/****************************test code************************************************/
	for ( i = 0; i < MAX_INSTANCE_NUM; i++) {
		handler.port_id = port_id;
		handler.inst_id = i;
		handler.type_id = CU_SPLIT;
		cu_flag = C_PLANE;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);
	
		cu_flag = U_PLANE;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);
	
		handler.type_id = OAM;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);		
	}
	/*********************************************************************************/ 	
}

#if 0

void test_speed_sendmsg_pcie()
{
    uint32_t size = 0;
    char* buf = NULL;
    uint32_t availableSize;

	uint16_t cu_flag = C_PLANE;
	HandleId_t handler;
	handler.port_id = 0;
	handler.inst_id = 0;
	handler.type_id = CU_SPLIT;
	uint32_t slot_ind_flag = 1;
	int ret = 0;
	
	uint32_t offset = 0;
	uint32_t i = 0;
	char templogbuf[1024] = {0};
	
    MsgQueueLocalMgt_t* pMsgQueueLocalMgt = get_msg_queue_local_mgt();
	MsgQueueLocalInfo_t* ch = (MsgQueueLocalInfo_t*)&pMsgQueueLocalMgt->localUlQueue[0][1];	
	size = ch->bufSize - MSG_MBUF_HEAD_SIZE;
	/****************************test code************************************************/
	for(i = 0; i < 2; i++)
	{
		handler.port_id = 0;
		handler.inst_id = i;
		handler.type_id = CU_SPLIT; 	
		if(1 == slot_ind_flag)
		{
		   // slot_ind_flag = 0;
			ret = msg_transfer_send_start(handler.value);
	
			/************C_PLANE***************/
			cu_flag = C_PLANE;
			ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
			if ( SUCCESS != ret) {
				UCP_PRINT_LOG("alloc msg failed!\n");
				continue;
			}
			//size = availableSize;
			//buf = *bufAddr;
			UCP_PRINT_LOG("msg transfer alloced ul msg C_PLANE buf = 0x%x!\n", buf);
			memset(templogbuf, 0x00, sizeof(templogbuf));				
			sprintf(templogbuf, "%s", "this is test for ul CU_SPLIT ctrl transfer!\n");
			memcpy_ucp(buf, templogbuf, strlen(templogbuf)+1);
			/*
			*(uint32_t*)(ptr + 0) = 0x12345678;
			*(uint32_t*)(ptr + 4) = 0x5a5a5a5a;
			*(uint32_t*)(ptr + 8) = 0x12345678;
			*(uint32_t*)(ptr + 12) = 0xa5a5a5a5;
			*/
			//PCIE_DEBUG_LOG("start enter msg_transfer_send_msg\n");
			ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);
			if(ret != SUCCESS)
			{
				UCP_PRINT_LOG("ul msg_transfer_send_msg CU_SPLIT ctrl  failed! ret = %d\n", ret);
			}
			
			ret = msg_transfer_send_end(handler.value);
		}

	}
	/*********************************************************************************/

}
#endif

void test_speed_recv_msg_pcie()
{
	uint16_t cu_flag = C_PLANE;
	HandleId_t handler;
	handler.port_id = 0;
	handler.inst_id = 0;
	handler.type_id = CU_SPLIT;
	
	uint8_t port_id = 0;
	uint32_t offset = 0;
	uint32_t len = 0;
	uint8_t* msg_ptr;	
	uint32_t i = 0;	
	
	/****************************test code************************************************/
	for ( i = 0; i < 2; i++) {
		handler.port_id = port_id;
		handler.inst_id = i;
		handler.type_id = CU_SPLIT;
		cu_flag = C_PLANE;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);
		
		cu_flag = U_PLANE;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);
	
		handler.type_id = OAM;
		msg_transfer_receive(handler.value, cu_flag, offset, len, &msg_ptr);		
	}
	/*********************************************************************************/ 	
}


uint32_t oam_spu_fill_rc_msg_header(uint8_t inst_id, char* buf, uint32_t size, uint32_t msgType)
{
    OamMsgTransferHeader_t header;
    header.numMsg = 1;
    header.cellIndex = inst_id;
    header.msgType = msgType;
    header.msgLen  = size + sizeof(OamMsgTransferHeader_t);
    uint8_t u8core_id = (uint8_t)get_core_id();

    memcpy_ucp(buf, &header, sizeof(OamMsgTransferHeader_t));
     __ucps2_synch(0);
    debug_write(DBG_DDR_COMMON_IDX(u8core_id, 57), buf);/*0xb7e016e4*/

    uint32_t dst_addr = (uint32_t)(buf + sizeof(OamMsgTransferHeader_t));
    
    return dst_addr;
}


int32_t oam_spu_send_rc_msg(uint8_t inst_id, uint32_t src_addr, uint32_t size, uint32_t msgType)
{
    uint8_t u8core_id = (uint8_t)get_core_id();

    if (0 == size) {
        return SUCCESS;
    }
    uint32_t len = size+sizeof(OamMsgTransferHeader_t);

    char* buf;
    uint32_t availableSize,offset;

    uint16_t cu_flag = C_PLANE;
    HandleId_t handler;
    handler.port_id = u8core_id;
    handler.inst_id = inst_id;
    handler.type_id = OAM;
    
    int32_t ret = msg_transfer_send_start(handler.value);
    ret = msg_transfer_alloc_msg(handler.value, cu_flag, len, &buf, &availableSize, &offset);
    if (SUCCESS != ret)
    {
        UCP_PRINT_ERROR("oam_spu_send_msg ret[0x%08x]", ret);
        return ret;
    }

    uint32_t dst_addr = oam_spu_fill_rc_msg_header(inst_id, buf, size, msgType);

    //ape_csu_dma_1D_G2L_ch2ch3_transfer((uint64_t)((uint32_t)(src_addr-0x100000)), dst_addr, size, inst_id, 1);
    //ape_csu_dma_1D_L2G_ch2ch3_transfer((uint64_t)((uint32_t)(src_addr-0x100000)), dst_addr, size, inst_id, 1);
    memcpy_ucp(dst_addr, src_addr, size);

    debug_write(DBG_DDR_COMMON_IDX(u8core_id, 53), dst_addr);/*0xb7e002d4*/
    debug_write(DBG_DDR_COMMON_IDX(u8core_id, 54), src_addr);
    debug_write(DBG_DDR_COMMON_IDX(u8core_id, 55), buf);

    ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, len);
    ret = msg_transfer_send_end(handler.value);

    UCP_PRINT_LOG("oam_spu_send_msg buf:%p len:%d\n", buf, len);

    return ret;
}



/* 收到rc侧发来的oam消息（通过RFM转发） */
static uint32_t g_u32_case20_ape1_event1_rev_num      = 0; // 30 0xB7E00278
static uint32_t g_u32_case20_ape1_event1_rev_ok       = 0; // 31 0xB7E0027C
static uint32_t g_u32_case20_ape1_event1_addr_err     = 0; // 32 0xB7E00280

void ape1_oam_event1_task(uint32_t addr, uint32_t size)
{
    uint32_t ape_id    = get_core_id();
    uint32_t msg_addr = 0;
    int32_t ret        = 0;
    SpuOamBaseDelayQryRsp_t  *oam_base_delay_qry_rsp_ptr     = NULL;

    g_u32_case20_ape1_event1_rev_num++;
    if (0 == addr)
    {
        g_u32_case20_ape1_event1_addr_err++;
        debug_write(DBG_DDR_COMMON_IDX(ape_id, 32), g_u32_case20_ape1_event1_addr_err);
        return ;
    }

    msg_addr = do_read(addr);

    g_oam_msg_ptr = (OamMsgTransferHeader_t*)msg_addr;
    g_u32_case20_ape1_event1_rev_ok++;

    debug_write(DBG_DDR_COMMON_IDX(ape_id, 30), g_oam_msg_ptr->cellIndex);
    debug_write(DBG_DDR_COMMON_IDX(ape_id, 31), g_oam_msg_ptr->msgType);
    debug_write(DBG_DDR_COMMON_IDX(ape_id, 32), g_oam_msg_ptr->numMsg);
    oam_base_delay_qry_rsp_ptr = (SpuOamBaseDelayQryRsp_t  *)(msg_addr+sizeof(OamMsgTransferHeader_t));
    oam_base_delay_qry_rsp_ptr->u8result = 1;

    ret = oam_spu_send_rc_msg(g_oam_msg_ptr->cellIndex, (uint32_t)oam_base_delay_qry_rsp_ptr, sizeof(SpuOamBaseDelayQryRsp_t), 0x203);

    return ;
}


void pcie_ape0_task_func(void)
{
    uint32_t core_id = get_core_id();

    char  abuf2[256]   = "ape0:ape0_log_send_task test12test67\n";
    spu1_info_t sttest;
    
    sttest.u32DataLen  = 0xaabbccdd;
    sttest.u32PktType  = 0x01020304;
    sttest.u32CoreId   = 0x05060708;
    sttest.u32MsgType  = 0x090a0b0c;

    spu_log_output(0, abuf2);
    osp_sendLog(0, abuf2, strlen(abuf2), 0);
    osp_sendLog_print(0, (char *)&sttest, sizeof(spu1_info_t), 0);

    g_ape0++;
    debug_write(OSP_DEBUG_POT(core_id, 38), g_ape0);//0xb7e02098

    return;
}

void pcie_ape1_task_func(void)
{
    uint32_t core_id = get_core_id();
    char  abuf2[256]   = "ape1:ape1_log_send_task test12test67\n";
    spu1_info_t sttest;
    
    sttest.u32DataLen  = 0xaabbccdd;
    sttest.u32PktType  = 0x01020304;
    sttest.u32CoreId   = 0x05060708;
    sttest.u32MsgType  = 0x090a0b0c;

    spu_log_output(0, abuf2);
    osp_sendLog(0, abuf2, strlen(abuf2), 0);
    osp_sendLog_print(0, (char *)&sttest, sizeof(spu1_info_t), 0);

    g_ape1++;
    debug_write(OSP_DEBUG_POT(core_id, 39), g_ape1);//0xb7e0245c

    return;
}

void ape0_test_task_reg(void)
{
    osp_task_info_ex pcie_timer_task_stc  = {58, (int8_t*)"pcie_test_taskc", 58, 4096, OSP_NORMAL_TYPE, 3, 0, 0, NULL, (OSP_TASKENTRY_FUNC)pcie_ape0_task_func};
    osp_task_create(&pcie_timer_task_stc);
    //osp_timer_sync(1);

    return;
}

void ape1_test_task_reg(void)
{
    osp_task_info_ex ape1_event_task_recv = {40, (int8_t*)"ape1_test_taskd", 40, 2048, OSP_EVENT_TYPE, 0, 0    , 0,   NULL, (OSP_TASKENTRY_FUNC)ape1_oam_event1_task};
    osp_task_info_ex pcie_timer_task_stc1  = {61, (int8_t*)"pcie_test_taskc1", 61, 4096, OSP_NORMAL_TYPE, 5, 0, 0, NULL, (OSP_TASKENTRY_FUNC)pcie_ape1_task_func};
    osp_task_create(&ape1_event_task_recv);
    osp_task_create(&pcie_timer_task_stc1);
    //osp_timer_sync(1);

    return;
}

void ape2_test_task_reg(void)
{
    return ;
}

void ape3_test_task_reg(void)
{
    return ;
}

void ape4_test_task_reg(void)
{
    return ;
}

void ape5_test_task_reg(void)
{
    return ;
}

void ape6_test_task_reg(void)
{
    return ;
}

void ape7_test_task_reg(void)
{
    return ;
}

