1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3 
4 #define _RTL8188E_REDESC_C_
5 
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/rtl8188e_hal.h"
9 
process_rssi(struct adapter * padapter,struct recv_frame * prframe)10 static void process_rssi(struct adapter *padapter, struct recv_frame *prframe)
11 {
12 	struct rx_pkt_attrib *pattrib = &prframe->attrib;
13 	struct signal_stat *signal_stat = &padapter->recvpriv.signal_strength_data;
14 
15 	if (signal_stat->update_req) {
16 		signal_stat->total_num = 0;
17 		signal_stat->total_val = 0;
18 		signal_stat->update_req = 0;
19 	}
20 
21 	signal_stat->total_num++;
22 	signal_stat->total_val  += pattrib->phy_info.SignalStrength;
23 	signal_stat->avg_val = signal_stat->total_val / signal_stat->total_num;
24 } /*  Process_UI_RSSI_8192C */
25 
process_link_qual(struct adapter * padapter,struct recv_frame * prframe)26 static void process_link_qual(struct adapter *padapter, struct recv_frame *prframe)
27 {
28 	struct rx_pkt_attrib *pattrib;
29 	struct signal_stat *signal_stat;
30 
31 	if (!prframe || !padapter)
32 		return;
33 
34 	pattrib = &prframe->attrib;
35 	signal_stat = &padapter->recvpriv.signal_qual_data;
36 
37 	if (signal_stat->update_req) {
38 		signal_stat->total_num = 0;
39 		signal_stat->total_val = 0;
40 		signal_stat->update_req = 0;
41 	}
42 
43 	signal_stat->total_num++;
44 	signal_stat->total_val  += pattrib->phy_info.SignalQuality;
45 	signal_stat->avg_val = signal_stat->total_val / signal_stat->total_num;
46 }
47 
rtl8188e_process_phy_info(struct adapter * padapter,void * prframe)48 static void rtl8188e_process_phy_info(struct adapter *padapter, void *prframe)
49 {
50 	struct recv_frame *precvframe = (struct recv_frame *)prframe;
51 
52 	/*  Check RSSI */
53 	process_rssi(padapter, precvframe);
54 	/*  Check EVM */
55 	process_link_qual(padapter,  precvframe);
56 }
57 
update_recvframe_attrib_88e(struct recv_frame * precvframe,struct recv_stat * prxstat)58 void update_recvframe_attrib_88e(struct recv_frame *precvframe, struct recv_stat *prxstat)
59 {
60 	struct rx_pkt_attrib *pattrib = &precvframe->attrib;
61 	memset(pattrib, 0, sizeof(struct rx_pkt_attrib));
62 
63 	pattrib->crc_err = (le32_to_cpu(prxstat->rxdw0) >> 14) & 0x1;
64 
65 	pattrib->pkt_rpt_type = (le32_to_cpu(prxstat->rxdw3) >> 14) & 0x3;
66 
67 	if (pattrib->pkt_rpt_type == NORMAL_RX) {
68 		pattrib->pkt_len = le32_to_cpu(prxstat->rxdw0) & 0x00003fff;
69 		pattrib->drvinfo_sz = ((le32_to_cpu(prxstat->rxdw0) >> 16) & 0xf) * 8;
70 
71 		pattrib->physt = (le32_to_cpu(prxstat->rxdw0) >> 26) & 0x1;
72 
73 		pattrib->bdecrypted = (le32_to_cpu(prxstat->rxdw0) & BIT(27)) ? 0 : 1;
74 		pattrib->encrypt = (le32_to_cpu(prxstat->rxdw0) >> 20) & 0x7;
75 
76 		pattrib->qos = (le32_to_cpu(prxstat->rxdw0) >> 23) & 0x1;
77 		pattrib->priority = (le32_to_cpu(prxstat->rxdw1) >> 8) & 0xf;
78 
79 		pattrib->amsdu = (le32_to_cpu(prxstat->rxdw1) >> 13) & 0x1;
80 
81 		pattrib->seq_num = le32_to_cpu(prxstat->rxdw2) & 0x00000fff;
82 		pattrib->frag_num = (le32_to_cpu(prxstat->rxdw2) >> 12) & 0xf;
83 		pattrib->mfrag = (le32_to_cpu(prxstat->rxdw1) >> 27) & 0x1;
84 		pattrib->mdata = (le32_to_cpu(prxstat->rxdw1) >> 26) & 0x1;
85 
86 		pattrib->mcs_rate = le32_to_cpu(prxstat->rxdw3) & 0x3f;
87 		pattrib->rxht = (le32_to_cpu(prxstat->rxdw3) >> 6) & 0x1;
88 
89 		pattrib->icv_err = (le32_to_cpu(prxstat->rxdw0) >> 15) & 0x1;
90 		pattrib->shift_sz = (le32_to_cpu(prxstat->rxdw0) >> 24) & 0x3;
91 	} else if (pattrib->pkt_rpt_type == TX_REPORT1) { /* CCX */
92 		pattrib->pkt_len = TX_RPT1_PKT_LEN;
93 	} else if (pattrib->pkt_rpt_type == TX_REPORT2) {
94 		pattrib->pkt_len = le32_to_cpu(prxstat->rxdw0) & 0x3FF;
95 
96 		pattrib->MacIDValidEntry[0] = le32_to_cpu(prxstat->rxdw4);
97 		pattrib->MacIDValidEntry[1] = le32_to_cpu(prxstat->rxdw5);
98 
99 	} else if (pattrib->pkt_rpt_type == HIS_REPORT) {
100 		pattrib->pkt_len = le32_to_cpu(prxstat->rxdw0) & 0x00003fff;
101 	}
102 }
103 
104 /*
105  * Notice:
106  *	Before calling this function,
107  *	precvframe->rx_data should be ready!
108  */
update_recvframe_phyinfo_88e(struct recv_frame * precvframe,struct phy_stat * pphy_status)109 void update_recvframe_phyinfo_88e(struct recv_frame *precvframe, struct phy_stat *pphy_status)
110 {
111 	struct adapter *padapter = precvframe->adapter;
112 	struct rx_pkt_attrib *pattrib = &precvframe->attrib;
113 	struct hal_data_8188e *pHalData = &padapter->haldata;
114 	struct phy_info *pPHYInfo  = &pattrib->phy_info;
115 	u8 *wlanhdr = precvframe->rx_data;
116 	__le16 fc = *(__le16 *)wlanhdr;
117 	struct odm_per_pkt_info	pkt_info;
118 	u8 *sa = NULL;
119 	struct sta_priv *pstapriv;
120 	struct sta_info *psta;
121 
122 	pkt_info.bPacketMatchBSSID = ((!ieee80211_is_ctl(fc)) &&
123 		!pattrib->icv_err && !pattrib->crc_err &&
124 		!memcmp(get_hdr_bssid(wlanhdr),
125 		 get_bssid(&padapter->mlmepriv), ETH_ALEN));
126 
127 	pkt_info.bPacketToSelf = pkt_info.bPacketMatchBSSID &&
128 				 (!memcmp(get_da(wlanhdr),
129 				  myid(&padapter->eeprompriv), ETH_ALEN));
130 
131 	pkt_info.bPacketBeacon = pkt_info.bPacketMatchBSSID && ieee80211_is_beacon(fc);
132 	if (pkt_info.bPacketBeacon) {
133 		if (check_fwstate(&padapter->mlmepriv, WIFI_STATION_STATE))
134 			sa = padapter->mlmepriv.cur_network.network.MacAddress;
135 		/* to do Ad-hoc */
136 	} else {
137 		sa = get_sa(wlanhdr);
138 	}
139 
140 	pstapriv = &padapter->stapriv;
141 	pkt_info.StationID = 0xFF;
142 	psta = rtw_get_stainfo(pstapriv, sa);
143 	if (psta)
144 		pkt_info.StationID = psta->mac_id;
145 	pkt_info.Rate = pattrib->mcs_rate;
146 
147 	ODM_PhyStatusQuery(&pHalData->odmpriv, pPHYInfo, (u8 *)pphy_status, &(pkt_info), padapter);
148 
149 	precvframe->psta = NULL;
150 	if (pkt_info.bPacketMatchBSSID &&
151 	    (check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE))) {
152 		if (psta) {
153 			precvframe->psta = psta;
154 			rtl8188e_process_phy_info(padapter, precvframe);
155 		}
156 	} else if (pkt_info.bPacketToSelf || pkt_info.bPacketBeacon) {
157 		if (check_fwstate(&padapter->mlmepriv, WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE)) {
158 			if (psta)
159 				precvframe->psta = psta;
160 		}
161 		rtl8188e_process_phy_info(padapter, precvframe);
162 	}
163 }
164