#include <drv_types.h>
#include <rtw_debug.h>
#include <rtl8723b_hal.h>
static void initrecvbuf(struct recv_buf *precvbuf, struct adapter *padapter)
{
INIT_LIST_HEAD(&precvbuf->list);
spin_lock_init(&precvbuf->recvbuf_lock);
precvbuf->adapter = padapter;
}
static void update_recvframe_attrib(struct adapter *padapter,
union recv_frame *precvframe,
struct recv_stat *prxstat)
{
struct rx_pkt_attrib *pattrib;
struct recv_stat report;
struct rxreport_8723b *prxreport = (struct rxreport_8723b *)&report;
report.rxdw0 = prxstat->rxdw0;
report.rxdw1 = prxstat->rxdw1;
report.rxdw2 = prxstat->rxdw2;
report.rxdw3 = prxstat->rxdw3;
report.rxdw4 = prxstat->rxdw4;
report.rxdw5 = prxstat->rxdw5;
pattrib = &precvframe->u.hdr.attrib;
memset(pattrib, 0, sizeof(struct rx_pkt_attrib));
pattrib->pkt_rpt_type = prxreport->c2h_ind ? C2H_PACKET : NORMAL_RX;
if (pattrib->pkt_rpt_type == NORMAL_RX) {
pattrib->pkt_len = (u16)prxreport->pktlen;
pattrib->drvinfo_sz = (u8)(prxreport->drvinfosize << 3);
pattrib->physt = (u8)prxreport->physt;
pattrib->crc_err = (u8)prxreport->crc32;
pattrib->icv_err = (u8)prxreport->icverr;
pattrib->bdecrypted = (u8)(prxreport->swdec ? 0 : 1);
pattrib->encrypt = (u8)prxreport->security;
pattrib->qos = (u8)prxreport->qos;
pattrib->priority = (u8)prxreport->tid;
pattrib->amsdu = (u8)prxreport->amsdu;
pattrib->seq_num = (u16)prxreport->seq;
pattrib->frag_num = (u8)prxreport->frag;
pattrib->mfrag = (u8)prxreport->mf;
pattrib->mdata = (u8)prxreport->md;
pattrib->data_rate = (u8)prxreport->rx_rate;
} else {
pattrib->pkt_len = (u16)prxreport->pktlen;
}
}
static void update_recvframe_phyinfo(union recv_frame *precvframe,
struct phy_stat *pphy_status)
{
struct adapter *padapter = precvframe->u.hdr.adapter;
struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
struct hal_com_data *p_hal_data = GET_HAL_DATA(padapter);
struct odm_phy_info *p_phy_info =
(struct odm_phy_info *)(&pattrib->phy_info);
u8 *wlanhdr = precvframe->u.hdr.rx_data;
u8 *my_bssid;
u8 *rx_bssid;
u8 *rx_ra;
u8 *my_hwaddr;
u8 *sa = NULL;
struct odm_packet_info pkt_info = {
.data_rate = 0x00,
.station_id = 0x00,
.bssid_match = false,
.to_self = false,
.is_beacon = false,
};
struct sta_priv *pstapriv;
struct sta_info *psta;
my_bssid = get_bssid(&padapter->mlmepriv);
rx_bssid = get_hdr_bssid(wlanhdr);
pkt_info.bssid_match = ((!IsFrameTypeCtrl(wlanhdr)) &&
!pattrib->icv_err && !pattrib->crc_err &&
ether_addr_equal(rx_bssid, my_bssid));
rx_ra = rtl8723bs_get_ra(wlanhdr);
my_hwaddr = myid(&padapter->eeprompriv);
pkt_info.to_self = pkt_info.bssid_match &&
ether_addr_equal(rx_ra, my_hwaddr);
pkt_info.is_beacon = pkt_info.bssid_match &&
(GetFrameSubType(wlanhdr) == WIFI_BEACON);
sa = get_ta(wlanhdr);
pkt_info.station_id = 0xFF;
pstapriv = &padapter->stapriv;
psta = rtw_get_stainfo(pstapriv, sa);
if (psta)
pkt_info.station_id = psta->mac_id;
pkt_info.data_rate = pattrib->data_rate;
odm_phy_status_query(&p_hal_data->odmpriv, p_phy_info,
(u8 *)pphy_status, &(pkt_info));
if (psta)
psta->rssi = pattrib->phy_info.RecvSignalPower;
precvframe->u.hdr.psta = NULL;
if (
pkt_info.bssid_match &&
(check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE) == true)
) {
if (psta) {
precvframe->u.hdr.psta = psta;
rtl8723b_process_phy_info(padapter, precvframe);
}
} else if (pkt_info.to_self || pkt_info.is_beacon) {
u32 adhoc_state = WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE;
if (check_fwstate(&padapter->mlmepriv, adhoc_state))
if (psta)
precvframe->u.hdr.psta = psta;
rtl8723b_process_phy_info(padapter, precvframe);
}
}
static void rtl8723bs_c2h_packet_handler(struct adapter *padapter,
u8 *pbuf, u16 length)
{
u8 *tmp = NULL;
u8 res = false;
if (length == 0)
return;
tmp = rtw_zmalloc(length);
if (!tmp)
return;
memcpy(tmp, pbuf, length);
res = rtw_c2h_packet_wk_cmd(padapter, tmp, length);
if (!res)
kfree(tmp);
}
static inline union recv_frame *try_alloc_recvframe(struct recv_priv *precvpriv,
struct recv_buf *precvbuf)
{
union recv_frame *precvframe;
precvframe = rtw_alloc_recvframe(&precvpriv->free_recv_queue);
if (!precvframe) {
rtw_enqueue_recvbuf_to_head(precvbuf,
&precvpriv->recv_buf_pending_queue);
tasklet_schedule(&precvpriv->recv_tasklet);
}
return precvframe;
}
static inline bool rx_crc_err(struct recv_priv *precvpriv,
struct hal_com_data *p_hal_data,
struct rx_pkt_attrib *pattrib,
union recv_frame *precvframe)
{
if ((!(p_hal_data->ReceiveConfig & RCR_ACRC32)) && pattrib->crc_err) {
rtw_free_recvframe(precvframe, &precvpriv->free_recv_queue);
return true;
}
return false;
}
static inline bool pkt_exceeds_tail(struct recv_priv *precvpriv,
u8 *end, u8 *tail,
union recv_frame *precvframe)
{
if (end > tail) {
rtw_free_recvframe(precvframe, &precvpriv->free_recv_queue);
return true;
}
return false;
}
static void rtl8723bs_recv_tasklet(struct tasklet_struct *t)
{
struct adapter *padapter = from_tasklet(padapter, t,
recvpriv.recv_tasklet);
struct hal_com_data *p_hal_data;
struct recv_priv *precvpriv;
struct recv_buf *precvbuf;
union recv_frame *precvframe;
struct rx_pkt_attrib *pattrib;
struct __queue *recv_buf_queue;
u8 *ptr;
u32 pkt_offset, skb_len, alloc_sz;
struct sk_buff *pkt_copy = NULL;
u8 shift_sz = 0, rx_report_sz = 0;
p_hal_data = GET_HAL_DATA(padapter);
precvpriv = &padapter->recvpriv;
recv_buf_queue = &precvpriv->recv_buf_pending_queue;
do {
precvbuf = rtw_dequeue_recvbuf(recv_buf_queue);
if (!precvbuf)
break;
ptr = precvbuf->pdata;
while (ptr < precvbuf->ptail) {
precvframe = try_alloc_recvframe(precvpriv, precvbuf);
if (!precvframe)
return;
update_recvframe_attrib(padapter, precvframe,
(struct recv_stat *)ptr);
pattrib = &precvframe->u.hdr.attrib;
if (rx_crc_err(precvpriv, p_hal_data,
pattrib, precvframe))
break;
rx_report_sz = RXDESC_SIZE + pattrib->drvinfo_sz;
pkt_offset = rx_report_sz +
pattrib->shift_sz +
pattrib->pkt_len;
if (pkt_exceeds_tail(precvpriv, ptr + pkt_offset,
precvbuf->ptail, precvframe))
break;
if ((pattrib->crc_err) || (pattrib->icv_err)) {
rtw_free_recvframe(precvframe,
&precvpriv->free_recv_queue);
} else {
if (pattrib->qos)
shift_sz = 6;
else
shift_sz = 0;
skb_len = pattrib->pkt_len;
if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
if (skb_len <= 1650)
alloc_sz = 1664;
else
alloc_sz = skb_len + 14;
} else {
alloc_sz = skb_len;
alloc_sz += 14;
}
pkt_copy = rtw_skb_alloc(alloc_sz);
if (!pkt_copy) {
rtw_free_recvframe(precvframe, &precvpriv->free_recv_queue);
break;
}
pkt_copy->dev = padapter->pnetdev;
precvframe->u.hdr.pkt = pkt_copy;
skb_reserve(pkt_copy, 8 - ((SIZE_PTR)(pkt_copy->data) & 7));
skb_reserve(pkt_copy, shift_sz);
memcpy(pkt_copy->data, (ptr + rx_report_sz + pattrib->shift_sz), skb_len);
precvframe->u.hdr.rx_head = pkt_copy->head;
precvframe->u.hdr.rx_data = precvframe->u.hdr.rx_tail = pkt_copy->data;
precvframe->u.hdr.rx_end = skb_end_pointer(pkt_copy);
recvframe_put(precvframe, skb_len);
if (p_hal_data->ReceiveConfig & RCR_APPFCS)
recvframe_pull_tail(precvframe, IEEE80211_FCS_LEN);
ptr += RXDESC_SIZE;
if (p_hal_data->ReceiveConfig & RCR_APP_BA_SSN) {
ptr += 4;
}
if (pattrib->pkt_rpt_type == NORMAL_RX) {
if (pattrib->physt)
update_recvframe_phyinfo(precvframe, (struct phy_stat *)ptr);
rtw_recv_entry(precvframe);
} else if (pattrib->pkt_rpt_type == C2H_PACKET) {
struct c2h_evt_hdr_t C2hEvent;
u16 len_c2h = pattrib->pkt_len;
u8 *pbuf_c2h = precvframe->u.hdr.rx_data;
u8 *pdata_c2h;
C2hEvent.CmdID = pbuf_c2h[0];
C2hEvent.CmdSeq = pbuf_c2h[1];
C2hEvent.CmdLen = (len_c2h-2);
pdata_c2h = pbuf_c2h+2;
if (C2hEvent.CmdID == C2H_CCX_TX_RPT)
CCX_FwC2HTxRpt_8723b(padapter, pdata_c2h, C2hEvent.CmdLen);
else
rtl8723bs_c2h_packet_handler(padapter, precvframe->u.hdr.rx_data, pattrib->pkt_len);
rtw_free_recvframe(precvframe, &precvpriv->free_recv_queue);
}
}
pkt_offset = round_up(pkt_offset, 8);
precvbuf->pdata += pkt_offset;
ptr = precvbuf->pdata;
precvframe = NULL;
pkt_copy = NULL;
}
rtw_enqueue_recvbuf(precvbuf, &precvpriv->free_recv_buf_queue);
} while (1);
}
s32 rtl8723bs_init_recv_priv(struct adapter *padapter)
{
s32 res;
u32 i, n;
struct recv_priv *precvpriv;
struct recv_buf *precvbuf;
res = _SUCCESS;
precvpriv = &padapter->recvpriv;
INIT_LIST_HEAD(&precvpriv->free_recv_buf_queue.queue);
spin_lock_init(&precvpriv->free_recv_buf_queue.lock);
INIT_LIST_HEAD(&precvpriv->recv_buf_pending_queue.queue);
spin_lock_init(&precvpriv->recv_buf_pending_queue.lock);
n = NR_RECVBUFF * sizeof(struct recv_buf) + 4;
precvpriv->pallocated_recv_buf = rtw_zmalloc(n);
if (!precvpriv->pallocated_recv_buf) {
res = _FAIL;
goto exit;
}
precvpriv->precv_buf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(precvpriv->pallocated_recv_buf), 4);
precvbuf = (struct recv_buf *)precvpriv->precv_buf;
for (i = 0; i < NR_RECVBUFF; i++) {
initrecvbuf(precvbuf, padapter);
if (!precvbuf->pskb) {
SIZE_PTR tmpaddr = 0;
SIZE_PTR alignment = 0;
precvbuf->pskb = rtw_skb_alloc(MAX_RECVBUF_SZ + RECVBUFF_ALIGN_SZ);
if (precvbuf->pskb) {
precvbuf->pskb->dev = padapter->pnetdev;
tmpaddr = (SIZE_PTR)precvbuf->pskb->data;
alignment = tmpaddr & (RECVBUFF_ALIGN_SZ-1);
skb_reserve(precvbuf->pskb, (RECVBUFF_ALIGN_SZ - alignment));
}
}
list_add_tail(&precvbuf->list, &precvpriv->free_recv_buf_queue.queue);
precvbuf++;
}
precvpriv->free_recv_buf_queue_cnt = i;
if (res == _FAIL)
goto initbuferror;
tasklet_setup(&precvpriv->recv_tasklet, rtl8723bs_recv_tasklet);
goto exit;
initbuferror:
precvbuf = (struct recv_buf *)precvpriv->precv_buf;
if (precvbuf) {
n = precvpriv->free_recv_buf_queue_cnt;
precvpriv->free_recv_buf_queue_cnt = 0;
for (i = 0; i < n ; i++) {
list_del_init(&precvbuf->list);
rtw_os_recvbuf_resource_free(padapter, precvbuf);
precvbuf++;
}
precvpriv->precv_buf = NULL;
}
kfree(precvpriv->pallocated_recv_buf);
precvpriv->pallocated_recv_buf = NULL;
exit:
return res;
}
void rtl8723bs_free_recv_priv(struct adapter *padapter)
{
u32 i;
struct recv_priv *precvpriv;
struct recv_buf *precvbuf;
precvpriv = &padapter->recvpriv;
tasklet_kill(&precvpriv->recv_tasklet);
precvbuf = (struct recv_buf *)precvpriv->precv_buf;
if (precvbuf) {
precvpriv->free_recv_buf_queue_cnt = 0;
for (i = 0; i < NR_RECVBUFF; i++) {
list_del_init(&precvbuf->list);
rtw_os_recvbuf_resource_free(padapter, precvbuf);
precvbuf++;
}
precvpriv->precv_buf = NULL;
}
kfree(precvpriv->pallocated_recv_buf);
precvpriv->pallocated_recv_buf = NULL;
}