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 
9 void rtw_rx_stats(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
10 		  struct sk_buff *skb)
11 {
12 	struct ieee80211_hdr *hdr;
13 	struct rtw_vif *rtwvif;
14 
15 	hdr = (struct ieee80211_hdr *)skb->data;
16 
17 	if (!ieee80211_is_data(hdr->frame_control))
18 		return;
19 
20 	if (!is_broadcast_ether_addr(hdr->addr1) &&
21 	    !is_multicast_ether_addr(hdr->addr1)) {
22 		rtwdev->stats.rx_unicast += skb->len;
23 		rtwdev->stats.rx_cnt++;
24 		if (vif) {
25 			rtwvif = (struct rtw_vif *)vif->drv_priv;
26 			rtwvif->stats.rx_unicast += skb->len;
27 			rtwvif->stats.rx_cnt++;
28 			if (rtwvif->stats.rx_cnt > RTW_LPS_THRESHOLD)
29 				rtw_leave_lps_irqsafe(rtwdev, rtwvif);
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_addr_match_iter(void *data, u8 *mac,
43 				   struct ieee80211_vif *vif)
44 {
45 	struct rtw_rx_addr_match_data *iter_data = data;
46 	struct ieee80211_sta *sta;
47 	struct ieee80211_hdr *hdr = iter_data->hdr;
48 	struct rtw_dev *rtwdev = iter_data->rtwdev;
49 	struct rtw_sta_info *si;
50 	struct rtw_rx_pkt_stat *pkt_stat = iter_data->pkt_stat;
51 	u8 *bssid = iter_data->bssid;
52 
53 	if (ether_addr_equal(vif->bss_conf.bssid, bssid) &&
54 	    (ether_addr_equal(vif->addr, hdr->addr1) ||
55 	     ieee80211_is_beacon(hdr->frame_control)))
56 		sta = ieee80211_find_sta_by_ifaddr(rtwdev->hw, hdr->addr2,
57 						   vif->addr);
58 	else
59 		return;
60 
61 	if (!sta)
62 		return;
63 
64 	si = (struct rtw_sta_info *)sta->drv_priv;
65 	ewma_rssi_add(&si->avg_rssi, pkt_stat->rssi);
66 }
67 
68 static void rtw_rx_addr_match(struct rtw_dev *rtwdev,
69 			      struct rtw_rx_pkt_stat *pkt_stat,
70 			      struct ieee80211_hdr *hdr)
71 {
72 	struct rtw_rx_addr_match_data data = {};
73 
74 	if (pkt_stat->crc_err || pkt_stat->icv_err || !pkt_stat->phy_status ||
75 	    ieee80211_is_ctl(hdr->frame_control))
76 		return;
77 
78 	data.rtwdev = rtwdev;
79 	data.hdr = hdr;
80 	data.pkt_stat = pkt_stat;
81 	data.bssid = get_hdr_bssid(hdr);
82 
83 	rtw_iterate_vifs_atomic(rtwdev, rtw_rx_addr_match_iter, &data);
84 }
85 
86 void rtw_rx_fill_rx_status(struct rtw_dev *rtwdev,
87 			   struct rtw_rx_pkt_stat *pkt_stat,
88 			   struct ieee80211_hdr *hdr,
89 			   struct ieee80211_rx_status *rx_status,
90 			   u8 *phy_status)
91 {
92 	struct ieee80211_hw *hw = rtwdev->hw;
93 	u8 path;
94 
95 	memset(rx_status, 0, sizeof(*rx_status));
96 	rx_status->freq = hw->conf.chandef.chan->center_freq;
97 	rx_status->band = hw->conf.chandef.chan->band;
98 	if (pkt_stat->crc_err)
99 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
100 	if (pkt_stat->decrypted)
101 		rx_status->flag |= RX_FLAG_DECRYPTED;
102 
103 	if (pkt_stat->rate >= DESC_RATEVHT1SS_MCS0)
104 		rx_status->encoding = RX_ENC_VHT;
105 	else if (pkt_stat->rate >= DESC_RATEMCS0)
106 		rx_status->encoding = RX_ENC_HT;
107 
108 	if (pkt_stat->rate >= DESC_RATEVHT1SS_MCS0 &&
109 	    pkt_stat->rate <= DESC_RATEVHT1SS_MCS9) {
110 		rx_status->nss = 1;
111 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEVHT1SS_MCS0;
112 	} else if (pkt_stat->rate >= DESC_RATEVHT2SS_MCS0 &&
113 		   pkt_stat->rate <= DESC_RATEVHT2SS_MCS9) {
114 		rx_status->nss = 2;
115 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEVHT2SS_MCS0;
116 	} else if (pkt_stat->rate >= DESC_RATEVHT3SS_MCS0 &&
117 		   pkt_stat->rate <= DESC_RATEVHT3SS_MCS9) {
118 		rx_status->nss = 3;
119 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEVHT3SS_MCS0;
120 	} else if (pkt_stat->rate >= DESC_RATEVHT4SS_MCS0 &&
121 		   pkt_stat->rate <= DESC_RATEVHT4SS_MCS9) {
122 		rx_status->nss = 4;
123 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEVHT4SS_MCS0;
124 	} else if (pkt_stat->rate >= DESC_RATEMCS0 &&
125 		   pkt_stat->rate <= DESC_RATEMCS15) {
126 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEMCS0;
127 	} else if (rx_status->band == NL80211_BAND_5GHZ &&
128 		   pkt_stat->rate >= DESC_RATE6M &&
129 		   pkt_stat->rate <= DESC_RATE54M) {
130 		rx_status->rate_idx = pkt_stat->rate - DESC_RATE6M;
131 	} else if (rx_status->band == NL80211_BAND_2GHZ &&
132 		   pkt_stat->rate >= DESC_RATE1M &&
133 		   pkt_stat->rate <= DESC_RATE54M) {
134 		rx_status->rate_idx = pkt_stat->rate - DESC_RATE1M;
135 	} else {
136 		rx_status->rate_idx = 0;
137 	}
138 
139 	rx_status->flag |= RX_FLAG_MACTIME_START;
140 	rx_status->mactime = pkt_stat->tsf_low;
141 
142 	if (pkt_stat->bw == RTW_CHANNEL_WIDTH_80)
143 		rx_status->bw = RATE_INFO_BW_80;
144 	else if (pkt_stat->bw == RTW_CHANNEL_WIDTH_40)
145 		rx_status->bw = RATE_INFO_BW_40;
146 	else
147 		rx_status->bw = RATE_INFO_BW_20;
148 
149 	rx_status->signal = pkt_stat->signal_power;
150 	for (path = 0; path < rtwdev->hal.rf_path_num; path++) {
151 		rx_status->chains |= BIT(path);
152 		rx_status->chain_signal[path] = pkt_stat->rx_power[path];
153 	}
154 
155 	rtw_rx_addr_match(rtwdev, pkt_stat, hdr);
156 }
157