xref: /linux/drivers/net/wireless/realtek/rtw88/rx.c (revision 170aafe35cb98e0f3fbacb446ea86389fbce22ea)
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 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 			   u8 *phy_status)
195 {
196 	struct ieee80211_hw *hw = rtwdev->hw;
197 	u8 path;
198 
199 	memset(rx_status, 0, sizeof(*rx_status));
200 	rx_status->freq = hw->conf.chandef.chan->center_freq;
201 	rx_status->band = hw->conf.chandef.chan->band;
202 	if (rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD) &&
203 	    test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
204 		rtw_set_rx_freq_by_pktstat(pkt_stat, rx_status);
205 	if (pkt_stat->crc_err)
206 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
207 	if (pkt_stat->decrypted)
208 		rx_status->flag |= RX_FLAG_DECRYPTED;
209 
210 	if (pkt_stat->rate >= DESC_RATEVHT1SS_MCS0)
211 		rx_status->encoding = RX_ENC_VHT;
212 	else if (pkt_stat->rate >= DESC_RATEMCS0)
213 		rx_status->encoding = RX_ENC_HT;
214 
215 	if (rx_status->band == NL80211_BAND_5GHZ &&
216 	    pkt_stat->rate >= DESC_RATE6M &&
217 	    pkt_stat->rate <= DESC_RATE54M) {
218 		rx_status->rate_idx = pkt_stat->rate - DESC_RATE6M;
219 	} else if (rx_status->band == NL80211_BAND_2GHZ &&
220 		   pkt_stat->rate >= DESC_RATE1M &&
221 		   pkt_stat->rate <= DESC_RATE54M) {
222 		rx_status->rate_idx = pkt_stat->rate - DESC_RATE1M;
223 	} else if (pkt_stat->rate >= DESC_RATEMCS0) {
224 		rtw_desc_to_mcsrate(pkt_stat->rate, &rx_status->rate_idx,
225 				    &rx_status->nss);
226 	}
227 
228 	rx_status->flag |= RX_FLAG_MACTIME_START;
229 	rx_status->mactime = pkt_stat->tsf_low;
230 
231 	if (pkt_stat->bw == RTW_CHANNEL_WIDTH_80)
232 		rx_status->bw = RATE_INFO_BW_80;
233 	else if (pkt_stat->bw == RTW_CHANNEL_WIDTH_40)
234 		rx_status->bw = RATE_INFO_BW_40;
235 	else
236 		rx_status->bw = RATE_INFO_BW_20;
237 
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 
244 	rtw_rx_addr_match(rtwdev, pkt_stat, hdr);
245 }
246 EXPORT_SYMBOL(rtw_rx_fill_rx_status);
247