1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019 Realtek Corporation
10 void rtw_rx_stats(struct rtw_dev
*rtwdev
, struct ieee80211_vif
*vif
,
13 struct ieee80211_hdr
*hdr
;
14 struct rtw_vif
*rtwvif
;
16 hdr
= (struct ieee80211_hdr
*)skb
->data
;
18 if (!ieee80211_is_data(hdr
->frame_control
))
21 if (!is_broadcast_ether_addr(hdr
->addr1
) &&
22 !is_multicast_ether_addr(hdr
->addr1
)) {
23 rtwdev
->stats
.rx_unicast
+= skb
->len
;
24 rtwdev
->stats
.rx_cnt
++;
26 rtwvif
= (struct rtw_vif
*)vif
->drv_priv
;
27 rtwvif
->stats
.rx_unicast
+= skb
->len
;
28 rtwvif
->stats
.rx_cnt
++;
32 EXPORT_SYMBOL(rtw_rx_stats
);
34 struct rtw_rx_addr_match_data
{
35 struct rtw_dev
*rtwdev
;
36 struct ieee80211_hdr
*hdr
;
37 struct rtw_rx_pkt_stat
*pkt_stat
;
41 static void rtw_rx_phy_stat(struct rtw_dev
*rtwdev
,
42 struct rtw_rx_pkt_stat
*pkt_stat
,
43 struct ieee80211_hdr
*hdr
)
45 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
46 struct rtw_pkt_count
*cur_pkt_cnt
= &dm_info
->cur_pkt_count
;
47 u8 rate_ss
, rate_ss_evm
, evm_id
;
50 dm_info
->curr_rx_rate
= pkt_stat
->rate
;
52 if (ieee80211_is_beacon(hdr
->frame_control
))
53 cur_pkt_cnt
->num_bcn_pkt
++;
55 switch (pkt_stat
->rate
) {
56 case DESC_RATE1M
...DESC_RATE11M
:
58 case DESC_RATE6M
...DESC_RATE54M
:
61 evm_id
= RTW_EVM_OFDM
;
63 case DESC_RATEMCS0
...DESC_RATEMCS7
:
64 case DESC_RATEVHT1SS_MCS0
...DESC_RATEVHT1SS_MCS9
:
69 case DESC_RATEMCS8
...DESC_RATEMCS15
:
70 case DESC_RATEVHT2SS_MCS0
...DESC_RATEVHT2SS_MCS9
:
73 evm_id
= RTW_EVM_2SS_A
;
76 rtw_warn(rtwdev
, "unknown pkt rate = %d\n", pkt_stat
->rate
);
80 for (i
= 0; i
< rate_ss_evm
; i
++) {
82 ewma_evm_add(&dm_info
->ewma_evm
[idx
],
83 dm_info
->rx_evm_dbm
[i
]);
86 for (i
= 0; i
< rtwdev
->hal
.rf_path_num
; i
++) {
87 idx
= RTW_SNR_OFDM_A
+ 4 * rate_ss
+ i
;
88 ewma_snr_add(&dm_info
->ewma_snr
[idx
],
92 cur_pkt_cnt
->num_qry_pkt
[pkt_stat
->rate
]++;
95 static void rtw_rx_addr_match_iter(void *data
, u8
*mac
,
96 struct ieee80211_vif
*vif
)
98 struct rtw_rx_addr_match_data
*iter_data
= data
;
99 struct ieee80211_sta
*sta
;
100 struct ieee80211_hdr
*hdr
= iter_data
->hdr
;
101 struct rtw_dev
*rtwdev
= iter_data
->rtwdev
;
102 struct rtw_sta_info
*si
;
103 struct rtw_rx_pkt_stat
*pkt_stat
= iter_data
->pkt_stat
;
104 u8
*bssid
= iter_data
->bssid
;
106 if (!ether_addr_equal(vif
->bss_conf
.bssid
, bssid
))
109 if (!(ether_addr_equal(vif
->addr
, hdr
->addr1
) ||
110 ieee80211_is_beacon(hdr
->frame_control
)))
113 rtw_rx_phy_stat(rtwdev
, pkt_stat
, hdr
);
114 sta
= ieee80211_find_sta_by_ifaddr(rtwdev
->hw
, hdr
->addr2
,
119 si
= (struct rtw_sta_info
*)sta
->drv_priv
;
120 ewma_rssi_add(&si
->avg_rssi
, pkt_stat
->rssi
);
123 static void rtw_rx_addr_match(struct rtw_dev
*rtwdev
,
124 struct rtw_rx_pkt_stat
*pkt_stat
,
125 struct ieee80211_hdr
*hdr
)
127 struct rtw_rx_addr_match_data data
= {};
129 if (pkt_stat
->crc_err
|| pkt_stat
->icv_err
|| !pkt_stat
->phy_status
||
130 ieee80211_is_ctl(hdr
->frame_control
))
133 data
.rtwdev
= rtwdev
;
135 data
.pkt_stat
= pkt_stat
;
136 data
.bssid
= get_hdr_bssid(hdr
);
138 rtw_iterate_vifs_atomic(rtwdev
, rtw_rx_addr_match_iter
, &data
);
141 void rtw_rx_fill_rx_status(struct rtw_dev
*rtwdev
,
142 struct rtw_rx_pkt_stat
*pkt_stat
,
143 struct ieee80211_hdr
*hdr
,
144 struct ieee80211_rx_status
*rx_status
,
147 struct ieee80211_hw
*hw
= rtwdev
->hw
;
150 memset(rx_status
, 0, sizeof(*rx_status
));
151 rx_status
->freq
= hw
->conf
.chandef
.chan
->center_freq
;
152 rx_status
->band
= hw
->conf
.chandef
.chan
->band
;
153 if (pkt_stat
->crc_err
)
154 rx_status
->flag
|= RX_FLAG_FAILED_FCS_CRC
;
155 if (pkt_stat
->decrypted
)
156 rx_status
->flag
|= RX_FLAG_DECRYPTED
;
158 if (pkt_stat
->rate
>= DESC_RATEVHT1SS_MCS0
)
159 rx_status
->encoding
= RX_ENC_VHT
;
160 else if (pkt_stat
->rate
>= DESC_RATEMCS0
)
161 rx_status
->encoding
= RX_ENC_HT
;
163 if (rx_status
->band
== NL80211_BAND_5GHZ
&&
164 pkt_stat
->rate
>= DESC_RATE6M
&&
165 pkt_stat
->rate
<= DESC_RATE54M
) {
166 rx_status
->rate_idx
= pkt_stat
->rate
- DESC_RATE6M
;
167 } else if (rx_status
->band
== NL80211_BAND_2GHZ
&&
168 pkt_stat
->rate
>= DESC_RATE1M
&&
169 pkt_stat
->rate
<= DESC_RATE54M
) {
170 rx_status
->rate_idx
= pkt_stat
->rate
- DESC_RATE1M
;
171 } else if (pkt_stat
->rate
>= DESC_RATEMCS0
) {
172 rtw_desc_to_mcsrate(pkt_stat
->rate
, &rx_status
->rate_idx
,
176 rx_status
->flag
|= RX_FLAG_MACTIME_START
;
177 rx_status
->mactime
= pkt_stat
->tsf_low
;
179 if (pkt_stat
->bw
== RTW_CHANNEL_WIDTH_80
)
180 rx_status
->bw
= RATE_INFO_BW_80
;
181 else if (pkt_stat
->bw
== RTW_CHANNEL_WIDTH_40
)
182 rx_status
->bw
= RATE_INFO_BW_40
;
184 rx_status
->bw
= RATE_INFO_BW_20
;
186 rx_status
->signal
= pkt_stat
->signal_power
;
187 for (path
= 0; path
< rtwdev
->hal
.rf_path_num
; path
++) {
188 rx_status
->chains
|= BIT(path
);
189 rx_status
->chain_signal
[path
] = pkt_stat
->rx_power
[path
];
192 rtw_rx_addr_match(rtwdev
, pkt_stat
, hdr
);