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 
94 	memset(rx_status, 0, sizeof(*rx_status));
95 	rx_status->freq = hw->conf.chandef.chan->center_freq;
96 	rx_status->band = hw->conf.chandef.chan->band;
97 	if (pkt_stat->crc_err)
98 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
99 	if (pkt_stat->decrypted)
100 		rx_status->flag |= RX_FLAG_DECRYPTED;
101 
102 	if (pkt_stat->rate >= DESC_RATEVHT1SS_MCS0)
103 		rx_status->encoding = RX_ENC_VHT;
104 	else if (pkt_stat->rate >= DESC_RATEMCS0)
105 		rx_status->encoding = RX_ENC_HT;
106 
107 	if (pkt_stat->rate >= DESC_RATEVHT1SS_MCS0 &&
108 	    pkt_stat->rate <= DESC_RATEVHT1SS_MCS9) {
109 		rx_status->nss = 1;
110 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEVHT1SS_MCS0;
111 	} else if (pkt_stat->rate >= DESC_RATEVHT2SS_MCS0 &&
112 		   pkt_stat->rate <= DESC_RATEVHT2SS_MCS9) {
113 		rx_status->nss = 2;
114 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEVHT2SS_MCS0;
115 	} else if (pkt_stat->rate >= DESC_RATEVHT3SS_MCS0 &&
116 		   pkt_stat->rate <= DESC_RATEVHT3SS_MCS9) {
117 		rx_status->nss = 3;
118 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEVHT3SS_MCS0;
119 	} else if (pkt_stat->rate >= DESC_RATEVHT4SS_MCS0 &&
120 		   pkt_stat->rate <= DESC_RATEVHT4SS_MCS9) {
121 		rx_status->nss = 4;
122 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEVHT4SS_MCS0;
123 	} else if (pkt_stat->rate >= DESC_RATEMCS0 &&
124 		   pkt_stat->rate <= DESC_RATEMCS15) {
125 		rx_status->rate_idx = pkt_stat->rate - DESC_RATEMCS0;
126 	} else if (rx_status->band == NL80211_BAND_5GHZ &&
127 		   pkt_stat->rate >= DESC_RATE6M &&
128 		   pkt_stat->rate <= DESC_RATE54M) {
129 		rx_status->rate_idx = pkt_stat->rate - DESC_RATE6M;
130 	} else if (rx_status->band == NL80211_BAND_2GHZ &&
131 		   pkt_stat->rate >= DESC_RATE1M &&
132 		   pkt_stat->rate <= DESC_RATE54M) {
133 		rx_status->rate_idx = pkt_stat->rate - DESC_RATE1M;
134 	} else {
135 		rx_status->rate_idx = 0;
136 	}
137 
138 	rx_status->flag |= RX_FLAG_MACTIME_START;
139 	rx_status->mactime = pkt_stat->tsf_low;
140 
141 	if (pkt_stat->bw == RTW_CHANNEL_WIDTH_80)
142 		rx_status->bw = RATE_INFO_BW_80;
143 	else if (pkt_stat->bw == RTW_CHANNEL_WIDTH_40)
144 		rx_status->bw = RATE_INFO_BW_40;
145 	else
146 		rx_status->bw = RATE_INFO_BW_20;
147 
148 	rx_status->signal = pkt_stat->signal_power;
149 
150 	rtw_rx_addr_match(rtwdev, pkt_stat, hdr);
151 }
152