xref: /linux/drivers/net/wireless/realtek/rtw88/rx.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019  Realtek Corporation
3  */
4 
5 #include "main.h"
6 #include "rx.h"
7 #include "ps.h"
8 #include "debug.h"
9 #include "fw.h"
10 
11 void rtw_rx_stats(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
12 		  struct sk_buff *skb)
13 {
14 	struct ieee80211_hdr *hdr;
15 	struct rtw_vif *rtwvif;
16 
17 	hdr = (struct ieee80211_hdr *)skb->data;
18 
19 	if (!ieee80211_is_data(hdr->frame_control))
20 		return;
21 
22 	if (!is_broadcast_ether_addr(hdr->addr1) &&
23 	    !is_multicast_ether_addr(hdr->addr1)) {
24 		rtwdev->stats.rx_unicast += skb->len;
25 		rtwdev->stats.rx_cnt++;
26 		if (vif) {
27 			rtwvif = (struct rtw_vif *)vif->drv_priv;
28 			rtwvif->stats.rx_unicast += skb->len;
29 			rtwvif->stats.rx_cnt++;
30 		}
31 	}
32 }
33 EXPORT_SYMBOL(rtw_rx_stats);
34 
35 struct rtw_rx_addr_match_data {
36 	struct rtw_dev *rtwdev;
37 	struct ieee80211_hdr *hdr;
38 	struct rtw_rx_pkt_stat *pkt_stat;
39 	u8 *bssid;
40 };
41 
42 static void rtw_rx_phy_stat(struct rtw_dev *rtwdev,
43 			    struct rtw_rx_pkt_stat *pkt_stat,
44 			    struct ieee80211_hdr *hdr)
45 {
46 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
47 	struct rtw_pkt_count *cur_pkt_cnt = &dm_info->cur_pkt_count;
48 	u8 rate_ss, rate_ss_evm, evm_id;
49 	u8 i, idx;
50 
51 	dm_info->curr_rx_rate = pkt_stat->rate;
52 
53 	if (ieee80211_is_beacon(hdr->frame_control))
54 		cur_pkt_cnt->num_bcn_pkt++;
55 
56 	switch (pkt_stat->rate) {
57 	case DESC_RATE1M...DESC_RATE11M:
58 		goto pkt_num;
59 	case DESC_RATE6M...DESC_RATE54M:
60 		rate_ss = 0;
61 		rate_ss_evm = 1;
62 		evm_id = RTW_EVM_OFDM;
63 		break;
64 	case DESC_RATEMCS0...DESC_RATEMCS7:
65 	case DESC_RATEVHT1SS_MCS0...DESC_RATEVHT1SS_MCS9:
66 		rate_ss = 1;
67 		rate_ss_evm = 1;
68 		evm_id = RTW_EVM_1SS;
69 		break;
70 	case DESC_RATEMCS8...DESC_RATEMCS15:
71 	case DESC_RATEVHT2SS_MCS0...DESC_RATEVHT2SS_MCS9:
72 		rate_ss = 2;
73 		rate_ss_evm = 2;
74 		evm_id = RTW_EVM_2SS_A;
75 		break;
76 	default:
77 		rtw_warn(rtwdev, "unknown pkt rate = %d\n", pkt_stat->rate);
78 		return;
79 	}
80 
81 	for (i = 0; i < rate_ss_evm; i++) {
82 		idx = evm_id + i;
83 		ewma_evm_add(&dm_info->ewma_evm[idx],
84 			     dm_info->rx_evm_dbm[i]);
85 	}
86 
87 	for (i = 0; i < rtwdev->hal.rf_path_num; i++) {
88 		idx = RTW_SNR_OFDM_A + 4 * rate_ss + i;
89 		ewma_snr_add(&dm_info->ewma_snr[idx],
90 			     dm_info->rx_snr[i]);
91 	}
92 pkt_num:
93 	cur_pkt_cnt->num_qry_pkt[pkt_stat->rate]++;
94 }
95 
96 static void rtw_rx_addr_match_iter(void *data, u8 *mac,
97 				   struct ieee80211_vif *vif)
98 {
99 	struct rtw_rx_addr_match_data *iter_data = data;
100 	struct ieee80211_sta *sta;
101 	struct ieee80211_hdr *hdr = iter_data->hdr;
102 	struct rtw_dev *rtwdev = iter_data->rtwdev;
103 	struct rtw_sta_info *si;
104 	struct rtw_rx_pkt_stat *pkt_stat = iter_data->pkt_stat;
105 	u8 *bssid = iter_data->bssid;
106 
107 	if (!ether_addr_equal(vif->bss_conf.bssid, bssid))
108 		return;
109 
110 	if (!(ether_addr_equal(vif->addr, hdr->addr1) ||
111 	      ieee80211_is_beacon(hdr->frame_control)))
112 		return;
113 
114 	rtw_rx_phy_stat(rtwdev, pkt_stat, hdr);
115 	sta = ieee80211_find_sta_by_ifaddr(rtwdev->hw, hdr->addr2,
116 					   vif->addr);
117 	if (!sta)
118 		return;
119 
120 	si = (struct rtw_sta_info *)sta->drv_priv;
121 	ewma_rssi_add(&si->avg_rssi, pkt_stat->rssi);
122 }
123 
124 static void rtw_rx_addr_match(struct rtw_dev *rtwdev,
125 			      struct rtw_rx_pkt_stat *pkt_stat,
126 			      struct ieee80211_hdr *hdr)
127 {
128 	struct rtw_rx_addr_match_data data = {};
129 
130 	if (pkt_stat->crc_err || pkt_stat->icv_err || !pkt_stat->phy_status ||
131 	    ieee80211_is_ctl(hdr->frame_control))
132 		return;
133 
134 	data.rtwdev = rtwdev;
135 	data.hdr = hdr;
136 	data.pkt_stat = pkt_stat;
137 	data.bssid = get_hdr_bssid(hdr);
138 
139 	rtw_iterate_vifs_atomic(rtwdev, rtw_rx_addr_match_iter, &data);
140 }
141 
142 static void rtw_set_rx_freq_by_pktstat(struct rtw_rx_pkt_stat *pkt_stat,
143 				       struct ieee80211_rx_status *rx_status)
144 {
145 	rx_status->freq = pkt_stat->freq;
146 	rx_status->band = pkt_stat->band;
147 }
148 
149 void rtw_update_rx_freq_from_ie(struct rtw_dev *rtwdev, struct sk_buff *skb,
150 				struct ieee80211_rx_status *rx_status,
151 				struct rtw_rx_pkt_stat *pkt_stat)
152 {
153 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
154 	int channel = rtwdev->hal.current_channel;
155 	size_t hdr_len, ielen;
156 	int channel_number;
157 	u8 *variable;
158 
159 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
160 		goto fill_rx_status;
161 
162 	if (ieee80211_is_beacon(mgmt->frame_control)) {
163 		variable = mgmt->u.beacon.variable;
164 		hdr_len = offsetof(struct ieee80211_mgmt,
165 				   u.beacon.variable);
166 	} else if (ieee80211_is_probe_resp(mgmt->frame_control)) {
167 		variable = mgmt->u.probe_resp.variable;
168 		hdr_len = offsetof(struct ieee80211_mgmt,
169 				   u.probe_resp.variable);
170 	} else {
171 		goto fill_rx_status;
172 	}
173 
174 	if (skb->len > hdr_len)
175 		ielen = skb->len - hdr_len;
176 	else
177 		goto fill_rx_status;
178 
179 	channel_number = cfg80211_get_ies_channel_number(variable, ielen,
180 							 NL80211_BAND_2GHZ);
181 	if (channel_number != -1)
182 		channel = channel_number;
183 
184 fill_rx_status:
185 	rtw_set_rx_freq_band(pkt_stat, channel);
186 	rtw_set_rx_freq_by_pktstat(pkt_stat, rx_status);
187 }
188 EXPORT_SYMBOL(rtw_update_rx_freq_from_ie);
189 
190 static void rtw_rx_fill_rx_status(struct rtw_dev *rtwdev,
191 				  struct rtw_rx_pkt_stat *pkt_stat,
192 				  struct ieee80211_hdr *hdr,
193 				  struct ieee80211_rx_status *rx_status)
194 {
195 	struct ieee80211_hw *hw = rtwdev->hw;
196 	u8 path;
197 
198 	memset(rx_status, 0, sizeof(*rx_status));
199 	rx_status->freq = hw->conf.chandef.chan->center_freq;
200 	rx_status->band = hw->conf.chandef.chan->band;
201 	if (rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD) &&
202 	    test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
203 		rtw_set_rx_freq_by_pktstat(pkt_stat, rx_status);
204 	if (pkt_stat->crc_err)
205 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
206 	if (pkt_stat->decrypted)
207 		rx_status->flag |= RX_FLAG_DECRYPTED;
208 
209 	if (pkt_stat->rate >= DESC_RATEVHT1SS_MCS0)
210 		rx_status->encoding = RX_ENC_VHT;
211 	else if (pkt_stat->rate >= DESC_RATEMCS0)
212 		rx_status->encoding = RX_ENC_HT;
213 
214 	if (rx_status->band == NL80211_BAND_5GHZ &&
215 	    pkt_stat->rate >= DESC_RATE6M &&
216 	    pkt_stat->rate <= DESC_RATE54M) {
217 		rx_status->rate_idx = pkt_stat->rate - DESC_RATE6M;
218 	} else if (rx_status->band == NL80211_BAND_2GHZ &&
219 		   pkt_stat->rate >= DESC_RATE1M &&
220 		   pkt_stat->rate <= DESC_RATE54M) {
221 		rx_status->rate_idx = pkt_stat->rate - DESC_RATE1M;
222 	} else if (pkt_stat->rate >= DESC_RATEMCS0) {
223 		rtw_desc_to_mcsrate(pkt_stat->rate, &rx_status->rate_idx,
224 				    &rx_status->nss);
225 	}
226 
227 	rx_status->flag |= RX_FLAG_MACTIME_START;
228 	rx_status->mactime = pkt_stat->tsf_low;
229 
230 	if (pkt_stat->bw == RTW_CHANNEL_WIDTH_80)
231 		rx_status->bw = RATE_INFO_BW_80;
232 	else if (pkt_stat->bw == RTW_CHANNEL_WIDTH_40)
233 		rx_status->bw = RATE_INFO_BW_40;
234 	else
235 		rx_status->bw = RATE_INFO_BW_20;
236 
237 	if (pkt_stat->phy_status) {
238 		rx_status->signal = pkt_stat->signal_power;
239 		for (path = 0; path < rtwdev->hal.rf_path_num; path++) {
240 			rx_status->chains |= BIT(path);
241 			rx_status->chain_signal[path] = pkt_stat->rx_power[path];
242 		}
243 	} else {
244 		rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
245 	}
246 
247 	rtw_rx_addr_match(rtwdev, pkt_stat, hdr);
248 
249 	/* Rtl8723cs driver checks for size < 14 or size > 8192 and
250 	 * simply drops the packet.
251 	 */
252 	if (rtwdev->chip->id == RTW_CHIP_TYPE_8703B && pkt_stat->pkt_len == 0) {
253 		rx_status->flag |= RX_FLAG_NO_PSDU;
254 		rtw_dbg(rtwdev, RTW_DBG_RX, "zero length packet");
255 	}
256 }
257 
258 void rtw_rx_query_rx_desc(struct rtw_dev *rtwdev, void *rx_desc8,
259 			  struct rtw_rx_pkt_stat *pkt_stat,
260 			  struct ieee80211_rx_status *rx_status)
261 {
262 	u32 desc_sz = rtwdev->chip->rx_pkt_desc_sz;
263 	struct rtw_rx_desc *rx_desc = rx_desc8;
264 	struct ieee80211_hdr *hdr;
265 	u32 enc_type, swdec;
266 	void *phy_status;
267 
268 	memset(pkt_stat, 0, sizeof(*pkt_stat));
269 
270 	pkt_stat->pkt_len = le32_get_bits(rx_desc->w0, RTW_RX_DESC_W0_PKT_LEN);
271 	pkt_stat->crc_err = le32_get_bits(rx_desc->w0, RTW_RX_DESC_W0_CRC32);
272 	pkt_stat->icv_err = le32_get_bits(rx_desc->w0, RTW_RX_DESC_W0_ICV_ERR);
273 	pkt_stat->drv_info_sz = le32_get_bits(rx_desc->w0,
274 					      RTW_RX_DESC_W0_DRV_INFO_SIZE);
275 	enc_type = le32_get_bits(rx_desc->w0, RTW_RX_DESC_W0_ENC_TYPE);
276 	pkt_stat->shift = le32_get_bits(rx_desc->w0, RTW_RX_DESC_W0_SHIFT);
277 	pkt_stat->phy_status = le32_get_bits(rx_desc->w0, RTW_RX_DESC_W0_PHYST);
278 	swdec = le32_get_bits(rx_desc->w0, RTW_RX_DESC_W0_SWDEC);
279 	pkt_stat->decrypted = !swdec && enc_type != RX_DESC_ENC_NONE;
280 
281 	pkt_stat->cam_id = le32_get_bits(rx_desc->w1, RTW_RX_DESC_W1_MACID);
282 
283 	pkt_stat->is_c2h = le32_get_bits(rx_desc->w2, RTW_RX_DESC_W2_C2H);
284 	pkt_stat->ppdu_cnt = le32_get_bits(rx_desc->w2, RTW_RX_DESC_W2_PPDU_CNT);
285 
286 	pkt_stat->rate = le32_get_bits(rx_desc->w3, RTW_RX_DESC_W3_RX_RATE);
287 
288 	pkt_stat->bw = le32_get_bits(rx_desc->w4, RTW_RX_DESC_W4_BW);
289 
290 	pkt_stat->tsf_low = le32_get_bits(rx_desc->w5, RTW_RX_DESC_W5_TSFL);
291 
292 	/* drv_info_sz is in unit of 8-bytes */
293 	pkt_stat->drv_info_sz *= 8;
294 
295 	/* c2h cmd pkt's rx/phy status is not interested */
296 	if (pkt_stat->is_c2h)
297 		return;
298 
299 	phy_status = rx_desc8 + desc_sz + pkt_stat->shift;
300 	hdr = phy_status + pkt_stat->drv_info_sz;
301 	pkt_stat->hdr = hdr;
302 
303 	if (pkt_stat->phy_status)
304 		rtwdev->chip->ops->query_phy_status(rtwdev, phy_status, pkt_stat);
305 
306 	rtw_rx_fill_rx_status(rtwdev, pkt_stat, hdr, rx_status);
307 }
308 EXPORT_SYMBOL(rtw_rx_query_rx_desc);
309