/**************************************************************************** Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT be copied by any method or incorporated into another program without the express written consent of Aerospace C.Power. This Information or any portion thereof remains the property of Aerospace C.Power. The Information contained herein is believed to be accurate and Aerospace C.Power assumes no responsibility or liability for its use in any way and conveys no license or title under any patent or copyright and makes no representation or warranty that this Information is free from patent or copyright infringement. ****************************************************************************/ #include "mac_rx_hw.h" #include "mac_pdev.h" #include "mpdu_frame.h" #include "iot_errno.h" #include "iot_pkt.h" #include "iot_io.h" #include "iot_system.h" #include "plc_protocol.h" #include "hw_tonemap.h" #include "mac_vdev.h" #include "mac_stream.h" #include "mac_peer.h" #include "mac_isr.h" #include "mac_sched_hw.h" #include "plc_const.h" #include "phy_chn.h" #include "phy_phase.h" #include "mpdu_header.h" #include "mac_rx_reg.h" #include "mac_msg.h" /* for msg send */ #include "mac.h" /* for msg id */ #include "plc_mpdu_header.h" /* for frame_control_t */ #include "mac_sched.h" #include "mac_cert_test.h" #include "mac_rawdata_hw.h" #include "iot_dbglog_api.h" #include "iot_dbglog_parser.h" #include "iot_plc_led.h" #include "mac_ppm_scan.h" #include "chip_reg_base.h" #include "mac_reset.h" #include "iot_system_api.h" #include "mac_crc.h" #include "mac_channel.h" #include "hw_war.h" #if MAC_TIMESTAMPING #include "mac_sys_reg.h" #include "hw_reg_api.h" #endif #include "multi_nid_sync.h" /* init rx interface, register the call back */ uint32_t mac_rx_hw_init(uint32_t fw_mode) { #if HW_PLATFORM >= HW_PLATFORM_FPGA /* enable bcn miss int */ mac_isr_enable(MAC_ISR_BC_MISS_ID); /* enable mpdu rx int */ mac_isr_enable(MAC_ISR_MPDU_RX_ID); /* overflow */ mac_isr_enable(MAC_ISR_RX_DESC_OVERFLOW_ID); //mac_isr_enable(MAC_ISR_RX_LOAD_OVERFLOW_ID); /* low water mark */ //mac_isr_enable(MAC_ISR_RX_LOW_WATERMARK_ID); /* rx timeout reset 0 - reset; 1 - not reset */ mac_rx_timeout_act_hang(0); /* rx abort reset */ mac_rx_abort_act_hang(0); /* WAR for RX CRC error issue */ mac_set_trans_done_selection(0); /* rx pb timeout */ mac_rx_pb_timeout(1000000); /** * pb framelength for gp * no good methods to caculate FL_ppb * for GP protocol,so write the registers * to restore the FL value **/ uint32_t proto = PHY_PROTO_TYPE_GET(); if (PLC_PROTO_TYPE_GP == proto) { mac_rx_gp_proto_band_set(); } else if (PLC_PROTO_TYPE_SG == proto \ || PLC_PROTO_TYPE_SPG == proto) { /* filter unknown mpdu delimiter type */ mac_rx_set_sg_dt_filter(4, 5, 6, 7); /* TODO: move to phy module */ mac_rx_set_delim(phy_band_id_get(), 0); #if ENA_WAR_244 /* WAR:bugid 244,set debug register */ mac_set_debug_reg(MAC_RX_DEBUG_CFG); /* WAR:bugid 244,sack tx isr config */ mac_rawdata_sack_isr_cfg(PHY_PROTO_TYPE_GET()); #endif } #endif /* enable or disable rx filter * 0:enable 1:disable **/ if (MM_MODE == fw_mode) { /* disable bcn phase filter */ mac_rx_set_filter(CFG_BEACON_PHASE_FILTER_DIS_MASK, \ (CFG_BEACON_PHASE_FILTER_DIS_MASK & MAC_RX_FILTER_DISABLE_BITMAP)); /* enable dtei filter */ mac_rx_set_filter(CFG_MPDU_DTEI_FILTER_DIS_MASK, \ (CFG_MPDU_DTEI_FILTER_DIS_MASK & MAC_RX_FILTER_DISABLE_BITMAP)); /* disable nid filter */ mac_rx_set_filter(CFG_NID_FILTER_DIS_MASK, \ (CFG_NID_FILTER_DIS_MASK & MAC_RX_FILTER_DISABLE_BITMAP)); /* enable fc CRC error filter */ mac_rx_set_filter(CFG_FC_CRCERR_FILTER_DIS_MASK, \ (CFG_FC_CRCERR_FILTER_DIS_MASK & MAC_RX_FILTER_DISABLE_BITMAP)); } else { /* FTM mode rx filter set */ /* disable bcn phase filter */ mac_rx_set_filter(CFG_BEACON_PHASE_FILTER_DIS_MASK, 1); /* disable dtei filter */ mac_rx_set_filter(CFG_MPDU_DTEI_FILTER_DIS_MASK, 1); /* disable nid filter */ mac_rx_set_filter(CFG_NID_FILTER_DIS_MASK, 1); /* enable fc CRC error filter */ mac_rx_set_filter(CFG_FC_CRCERR_FILTER_DIS_MASK, 0); } return 0; } void mac_rx_led_control(rx_mpdu_start *mpdu_st) { if (mpdu_st) { iot_plc_led_request(IOT_PLC_LED_REQ_PLC_RX); } return; } uint32_t mac_rx_debug_log_ringid_and_detype(rx_mpdu_start *mpdu_st) { #if (PLC_MAC_RX_DEBUG_LOG >= PLC_MAC_LOG_LEVEL_2) uint8_t ring_id = mac_rx_mpdu_st_get_ring_id(mpdu_st); frame_control_t *fc_debug = \ (frame_control_t *)mac_rx_mpdu_st_get_fc_addr(mpdu_st); uint8_t delimiter_debug = fc_debug->delimiter_type; iot_printf("ring_id = %d delimiter_type = %d\n",\ ring_id, delimiter_debug); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_INFO_LVL_1, IOT_MAC_RX_RINGID_DELI_ID, 2,ring_id, delimiter_debug); #else (void)mpdu_st; #endif return 0; } uint32_t mac_rx_debug_log_info(iot_pkt_t *pkt) { #if (PLC_MAC_RX_DEBUG_LOG > PLC_MAC_LOG_LEVEL_3) rx_attention *att; rx_mpdu_start *mpdu_st; rx_mpdu_end *mpdu_ed; rx_pb_start *pb_st; rx_pb_end *pb_ed; uint8_t *payload; uint8_t *tmp = iot_pkt_block_ptr(pkt, IOT_PKT_BLOCK_DATA); att = (rx_attention*)(tmp + RX_ATTENTION_OFFSET); mpdu_st = (rx_mpdu_start *)(tmp + MPDU_START_OFFSET); mpdu_ed = (rx_mpdu_end *)(tmp + MPDU_END_OFFSET); pb_st = (rx_pb_start *)(tmp + PB_START_OFFSET); pb_ed = (rx_pb_end *)(tmp + PB_END_OFFSET); payload = tmp + PB_PAYLOAD_OFFSET; iot_printf( \ "proto1 = 0x%x,proto2 = 0x%x,pb_num=0x%x\n", \ mac_rx_att_get_rx_proto(att), mac_rx_mpdu_st_get_rx_proto(mpdu_st), mac_rx_mpdu_st_get_rx_pb_num(mpdu_st)); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_INFO, IOT_MAC_RX_PORT_PBNUM_ID, 3,\ mac_rx_att_get_rx_proto(att), mac_rx_mpdu_st_get_rx_proto(mpdu_st), mac_rx_mpdu_st_get_rx_pb_num(mpdu_st)); iot_printf("ssn = %d, msdu_start = %d, msdu_end = %d\n",\ pb_st->ssn, pb_st->msdu_start, pb_st->msdu_end); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_INFO, IOT_MAC_RX_PB_HEADER_ID, 3,\ pb_st->ssn, pb_st->msdu_start, pb_st->msdu_end); iot_printf("fcserr = %d, pb_crc_err = %d\n",\ mac_rx_att_get_is_fcserr(att), \ mac_rx_pb_end_get_rx_pb_crc_err(pb_ed)); iot_dbglog_input(PLC_MAC_CRC_MID, DBGLOG_INFO, IOT_MAC_PB_CRC_ERE_ID, 2,\ mac_rx_att_get_is_fcserr(att), \ mac_rx_pb_end_get_rx_pb_crc_err(pb_ed)); mem_dump((uint32_t *)payload, 0x50); #else (void)pkt; #endif return 0; } uint32_t mac_rx_debug_log_check_phy_err(rx_pb_start *pb_st, \ rx_mpdu_end *mpdu_ed) { #if (PLC_MAC_RX_DEBUG_LOG >= PLC_MAC_LOG_LEVEL_1) /* if rx desc valid */ if (pb_st->last_pb) { /* check rx phy err */ if (mpdu_ed->err_fc_parser_timeout) { iot_printf("rxerr:fcp;"); } if (mpdu_ed->err_fd_overflow) { iot_printf("rxerr:fdo;"); } if (mpdu_ed->err_power_drop) { iot_printf("rxerr:fpd;"); } if (mpdu_ed->err_power_sta) { iot_printf("rxerr:fps;"); } if (mpdu_ed->err_power_up) { iot_printf("rxerr:fpu;"); } iot_printf("\n"); } #else (void)pb_st; (void)mpdu_ed; #endif return 0; } uint32_t mac_rx_debug_log_rxabort(iot_pkt_t *pkt) { #if (PLC_MAC_RX_DEBUG_LOG >= PLC_MAC_LOG_LEVEL_1) rx_fc_msg_t rx_fc_msg = { 0 }; uint32_t proto = 0; rx_attention *att; rx_mpdu_start *mpdu_st; rx_mpdu_end *mpdu_ed; rx_pb_start *pb_st; uint8_t *tmp = iot_pkt_block_ptr(pkt, IOT_PKT_BLOCK_DATA); att = (rx_attention*)(tmp + RX_ATTENTION_OFFSET); mpdu_st = (rx_mpdu_start *)(tmp + MPDU_START_OFFSET); mpdu_ed = (rx_mpdu_end *)(tmp + MPDU_END_OFFSET); pb_st = (rx_pb_start *)(tmp + PB_START_OFFSET); if (pb_st->last_pb) { iot_printf("rx abort:reason-%d," "rx_ntb=0x%x, fcrx=0x%x, txfd=0x%x, txtd=0x%x," "force_en=%d,force_phase=0x%x" "phy_force_0:0x%x\n", mpdu_ed->rx_abnormal_id, mpdu_ed->ntb_ts, g_phy_cpu_share_ctxt._1_rx_fd_fc_ts, g_phy_cpu_share_ctxt._2_tx_fd_ts, g_phy_cpu_share_ctxt._2_tx_td_st_ts, phy_get_rx_phase_force_en(), phy_get_rx_force_phase_val(), g_phy_cpu_share_ctxt.phy_force_0); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_WARN, IOT_MAC_RX_ABORT_LAST_PB_ID, 2, \ mpdu_ed->rx_abnormal_id, mpdu_ed->ntb_ts); } if (pb_st->first_pb) { proto = PHY_PROTO_TYPE_GET(); mac_get_rx_frm_msg_from_fc(proto, &mpdu_st->fc, &rx_fc_msg); iot_printf("rx_abort:nid=0x%x,stei=%d,delim=%d, " "rx_phase = %d, reg_phase = %d\n", \ rx_fc_msg.nid, rx_fc_msg.src_tei, \ mac_get_rx_delimiter_from_fc(proto, &mpdu_st->fc), \ att->rx_phase, g_phy_cpu_share_ctxt.reg_phase); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_WARN, IOT_MAC_RX_ABORT_FIRST_PB_ID, 5, \ rx_fc_msg.nid, rx_fc_msg.src_tei, \ mac_get_rx_delimiter_from_fc(proto, &mpdu_st->fc), \ att->rx_phase, g_phy_cpu_share_ctxt.reg_phase); } #else (void)pkt; #endif return 0; } uint32_t mac_rx_debug_log_fc(rx_mpdu_start *mpdu_st) { #if (PLC_MAC_RX_DEBUG_LOG > PLC_MAC_LOG_LEVEL_2) uint32_t proto = 0; proto = PHY_PROTO_TYPE_GET(); if (PLC_PROTO_TYPE_SG == proto) { frame_control_t *fc = \ (frame_control_t *)mac_rx_mpdu_st_get_fc_addr(mpdu_st); iot_printf( "delimiter = 0x%x," "network = 0x%x," "nid = 0x%x\n", fc->delimiter_type, fc->network_type, (uint32_t)fc->nid); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_INFO_LVL_2, IOT_MAC_SG_FC_INFO_ID, 3, fc->delimiter_type, fc->network_type, (uint32_t)fc->nid); } #if SUPPORT_SOUTHERN_POWER_GRID else if (PLC_PROTO_TYPE_SPG == proto) { spg_frame_control_t *fc = \ (spg_frame_control_t *)mac_rx_mpdu_st_get_fc_addr(mpdu_st); iot_printf( "delimiter = 0x%x," "access_ind = 0x%x," "snid = 0x%x\n", fc->delimiter_type, fc->access_ind, fc->snid); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_INFO_LVL_2, IOT_MAC_SPG_FC_INFO_ID, 3, fc->delimiter_type, fc->access_ind, fc->snid); } #endif else if (PLC_PROTO_TYPE_GP == proto){ hpav_frame_control *fc = \ (hpav_frame_control *)mac_rx_mpdu_st_get_fc_addr(mpdu_st); iot_printf( "delimiter = 0x%x," "access_ind = 0x%x," "snid = 0x%x\n", fc->delimiter_type, fc->access, fc->snid); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_INFO_LVL_2, IOT_MAC_GP_FC_INFO_ID, 3, fc->delimiter_type, fc->access, fc->snid); }else{ //todo } #else (void)mpdu_st; #endif return 0; } uint32_t mac_rx_debug_log_sof_uicast_info(rx_fc_msg_t rx_fc_msg, rx_mpdu_start *mpdu_st, rx_mpdu_end *mpdu_end, rx_pb_start *pb_st, int8_t raw_snr) { #if (PLC_MAC_RX_DEBUG_LOG >= PLC_MAC_LOG_LEVEL_1) /* if multi ppm, ppm is invalid */ #if CERT_TEST_DEBUG frame_control_t *fc = &mpdu_st->fc.sg_fc; #endif iot_printf("-----rx stei %d, dtei %d," \ "nid=0x%x, " #if CERT_TEST_DEBUG "delimiter_type= %d,network_type=%d,"\ "lid=%d,fream_len=%d, sym_num = %d,"\ "bcast=%d, force_phase_en:%d, force_phase=%d," #endif "ssn=%d,msdu_s=%d,msdu_e=%d," \ "1st_pb=%d,last_pb=%d," \ "retry=%d,pb_num=%d," \ "snr:%d->%d,tmi:%d-%d," \ "rx_phase=%d," \ "rssi=%d,nf=%d," \ "agc=%d," \ "adc=%d,adc_power=%d," \ "rx_sof_ts=%u,tx_sack_ts=%u" \ "hwppm=%d\n", \ rx_fc_msg.src_tei, rx_fc_msg.dst_tei, \ rx_fc_msg.nid, #if CERT_TEST_DEBUG fc->delimiter_type, fc->network_type,\ rx_fc_msg.lid, fc->vf.sof.frame_len, rx_fc_msg.numsym,\ fc->vf.sof.bcast, phy_get_rx_phase_force_en(), phy_get_rx_force_phase_val(), #endif pb_st->ssn, pb_st->msdu_start, pb_st->msdu_end, \ pb_st->first_pb, pb_st->last_pb, \ rx_fc_msg.retry, mac_rx_mpdu_st_get_rx_pb_num(mpdu_st),\ raw_snr, mac_rx_mpdu_st_get_avg_snr(mpdu_st), \ rx_fc_msg.tmi, \ rx_fc_msg.tmi_ext, \ mac_rx_mpdu_st_get_rx_phase(mpdu_st), \ PHY_RSSI_FROM_RMI_GAIN(mac_rx_mpdu_st_get_adc_power(mpdu_st), \ mac_rx_mpdu_st_get_agc_gain_entry(mpdu_st)), \ PHY_GET_NF(), \ PHY_GET_AGC_POWER(mac_rx_mpdu_st_get_agc_gain_entry(mpdu_st)), \ mac_rx_mpdu_st_get_estimated_dc(mpdu_st), \ mac_rx_mpdu_st_get_adc_power(mpdu_st), mac_rx_mpdu_end_get_ntb_timestamp(mpdu_end), mac_get_unicast_sack_ts(), mac_rx_mpdu_st_get_estimated_ppm(mpdu_st) ); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_INFO_LVL_2, IOT_MAC_RX_DSTEI_SNR_TMI_ID, 6, \ rx_fc_msg.src_tei, rx_fc_msg.dst_tei, \ mac_rx_mpdu_st_get_avg_snr(mpdu_st), \ rx_fc_msg.tmi, \ rx_fc_msg.tmi_ext, \ mac_rx_mpdu_st_get_rx_phase(mpdu_st) \ ); #else (void)rx_fc_msg; (void)mpdu_st; (void)mpdu_end; (void)pb_st; (void)raw_snr; #endif return 0; } uint32_t mac_rx_debug_log_sof_bcast_info(rx_fc_msg_t rx_fc_msg, rx_mpdu_start *mpdu_st, rx_mpdu_end *mpdu_end, rx_pb_start *pb_st, int8_t raw_snr) { #if (PLC_MAC_RX_DEBUG_LOG >= PLC_MAC_LOG_LEVEL_1) /* if multi ppm, ppm is invalid */ iot_printf("-----bcast rx stei %d, dtei %d," \ "nid=0x%x,ssn=%d,msdu_s=%d,msdu_e=%d," \ "1st_pb=%d,last_pb=%d," \ "retry=%d,pb_num=%d," \ "snr:%d->%d,tmi:%d-%d," \ "rx_phase=%d," \ "rssi=%d,nf=%d," \ "agc=%d," \ "adc=%d,adc_power=%d," \ "hwppm:%d, rx_ts:%lu\n", \ rx_fc_msg.src_tei, rx_fc_msg.dst_tei, \ rx_fc_msg.nid, \ pb_st->ssn, pb_st->msdu_start, pb_st->msdu_end, \ pb_st->first_pb, pb_st->last_pb, \ rx_fc_msg.retry, mac_rx_mpdu_st_get_rx_pb_num(mpdu_st),\ raw_snr, mac_rx_mpdu_st_get_avg_snr(mpdu_st), \ rx_fc_msg.tmi, \ rx_fc_msg.tmi_ext, \ mac_rx_mpdu_st_get_rx_phase(mpdu_st), \ PHY_RSSI_FROM_RMI_GAIN(mac_rx_mpdu_st_get_adc_power(mpdu_st), \ mac_rx_mpdu_st_get_agc_gain_entry(mpdu_st)), \ PHY_GET_NF(),\ PHY_GET_AGC_POWER(mac_rx_mpdu_st_get_agc_gain_entry(mpdu_st)), \ mac_rx_mpdu_st_get_estimated_dc(mpdu_st), \ mac_rx_mpdu_st_get_adc_power(mpdu_st), mac_rx_mpdu_st_get_estimated_ppm(mpdu_st), mac_rx_mpdu_end_get_ntb_timestamp(mpdu_end) ); #else (void)rx_fc_msg; (void)mpdu_end; (void)mpdu_st; (void)pb_st; (void)raw_snr; #endif return 0; } uint32_t mac_rx_debug_log_bcn_info(rx_fc_msg_t *rx_fc_msg,\ rx_mpdu_start *mpdu_st, rx_mpdu_end *mpdu_ed, rx_pb_end *pb_ed,\ int8_t raw_snr) { #if (PLC_MAC_RX_DEBUG_LOG >= PLC_MAC_LOG_LEVEL_1) uint32_t nid = 0; nid = mac_get_nid_from_fc(PHY_PROTO_TYPE_GET(), \ mac_rx_mpdu_st_get_fc_addr(mpdu_st)); if (mpdu_ed == NULL) { return ERR_INVAL; } int32_t delta = mac_rx_mpdu_end_get_ntb_timestamp(mpdu_ed) \ - rx_fc_msg->time_stamp; iot_printf("rx bcn:" \ "nid=0x%x," \ "stei=%d," \ "rx_phase=%d," \ "fc_phase=%d," \ "reg_phase = %d," "snr=%d->%d," \ "hwppm=%d," \ "mbandid=%d," \ "rssi=%d,nf=%d," \ "agc=%d," \ "adc=%d,adc_power=%d," \ "pb crc=%d,pld crc=%d," \ "d_ts=%d," \ "tmi%d-%d,"\ "sym_num=%d, "\ "ts:%d, rx_ts:%lu, lts:%u\n", nid, \ rx_fc_msg->src_tei, \ mac_rx_mpdu_st_get_rx_phase(mpdu_st), \ rx_fc_msg->phase, \ g_phy_cpu_share_ctxt.reg_phase,\ raw_snr, mpdu_st->phy.avg_snr, \ mpdu_st->phy.est_ppm, \ mpdu_st->rx_band_sel, \ PHY_RSSI_FROM_RMI_GAIN(mpdu_st->phy.adc_power, \ mpdu_st->phy.agc_tbl_entry), \ PHY_GET_NF(), \ PHY_GET_AGC_POWER(mac_rx_mpdu_st_get_agc_gain_entry(mpdu_st)), \ mac_rx_mpdu_st_get_estimated_dc, \ mac_rx_mpdu_st_get_adc_power(mpdu_st), \ pb_ed->rx_pb_crc_err, \ (mac_tx_hw_get_bcn_swcrc_cfg() ? \ 0:pb_ed->rx_beacon_pld_crc_err), \ delta, \ rx_fc_msg->tmi, rx_fc_msg->tmi_ext,\ rx_fc_msg->numsym,\ rx_fc_msg->time_stamp, mac_rx_mpdu_end_get_ntb_timestamp(mpdu_ed), mac_rx_mpdu_end_get_local_timestamp(mpdu_ed)); iot_dbglog_input(PLC_MAC_RX_HW_MID, \ DBGLOG_INFO_LVL_2, IOT_MAC_RX_BC_PB_PLD_CRC_ID, 4, \ nid, \ rx_fc_msg->src_tei, \ pb_ed->rx_pb_crc_err, \ (mac_tx_hw_get_bcn_swcrc_cfg() ? \ 0:pb_ed->rx_beacon_pld_crc_err)); #else (void)rx_fc_msg; (void)mpdu_st; (void)mpdu_ed; (void)pb_ed; (void)raw_snr; #endif return 0; } uint32_t mac_rx_debug_log_for_gp(uint32_t rtsf) { #if SUPPORT_GREEN_PHY #if (PLC_MAC_RX_DEBUG_LOG > PLC_MAC_LOG_LEVEL_1) if (1 == rtsf) { iot_printf("%s, RTS rx\n", __FUNCTION__); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_INFO, IOT_MAC_RX_RTS_ID, 0); } else { iot_printf("%s, CTS rx\n", __FUNCTION__); iot_dbglog_input(PLC_MAC_RX_HW_MID, DBGLOG_INFO, IOT_MAC_RX_CTS_ID, 0); } #endif #endif (void)rtsf; return 0; }