xref: /linux/drivers/net/wireless/realtek/rtw89/core.c (revision 9410645520e9b820069761f3450ef6661418e279)
1e3ec7017SPing-Ke Shih // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2e3ec7017SPing-Ke Shih /* Copyright(c) 2019-2020  Realtek Corporation
3e3ec7017SPing-Ke Shih  */
415fcb103SJakub Kicinski #include <linux/ip.h>
515fcb103SJakub Kicinski #include <linux/udp.h>
6e3ec7017SPing-Ke Shih 
72ab856ccSPing-Ke Shih #include "cam.h"
8967439c7SZong-Zhe Yang #include "chan.h"
9e3ec7017SPing-Ke Shih #include "coex.h"
10e3ec7017SPing-Ke Shih #include "core.h"
11e3ec7017SPing-Ke Shih #include "efuse.h"
12e3ec7017SPing-Ke Shih #include "fw.h"
13e3ec7017SPing-Ke Shih #include "mac.h"
14e3ec7017SPing-Ke Shih #include "phy.h"
15e3ec7017SPing-Ke Shih #include "ps.h"
16e3ec7017SPing-Ke Shih #include "reg.h"
17e3ec7017SPing-Ke Shih #include "sar.h"
18e3ec7017SPing-Ke Shih #include "ser.h"
19e3ec7017SPing-Ke Shih #include "txrx.h"
20e3ec7017SPing-Ke Shih #include "util.h"
2192790c4eSChih-Kang Chang #include "wow.h"
22e3ec7017SPing-Ke Shih 
23e3ec7017SPing-Ke Shih static bool rtw89_disable_ps_mode;
24e3ec7017SPing-Ke Shih module_param_named(disable_ps_mode, rtw89_disable_ps_mode, bool, 0644);
25e3ec7017SPing-Ke Shih MODULE_PARM_DESC(disable_ps_mode, "Set Y to disable low power mode");
26e3ec7017SPing-Ke Shih 
270237f65aSZong-Zhe Yang #define RTW89_DEF_CHAN(_freq, _hw_val, _flags, _band)	\
280237f65aSZong-Zhe Yang 	{ .center_freq = _freq, .hw_value = _hw_val, .flags = _flags, .band = _band, }
290237f65aSZong-Zhe Yang #define RTW89_DEF_CHAN_2G(_freq, _hw_val)	\
300237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN(_freq, _hw_val, 0, NL80211_BAND_2GHZ)
310237f65aSZong-Zhe Yang #define RTW89_DEF_CHAN_5G(_freq, _hw_val)	\
320237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN(_freq, _hw_val, 0, NL80211_BAND_5GHZ)
330237f65aSZong-Zhe Yang #define RTW89_DEF_CHAN_5G_NO_HT40MINUS(_freq, _hw_val)	\
340237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN(_freq, _hw_val, IEEE80211_CHAN_NO_HT40MINUS, NL80211_BAND_5GHZ)
350237f65aSZong-Zhe Yang #define RTW89_DEF_CHAN_6G(_freq, _hw_val)	\
360237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN(_freq, _hw_val, 0, NL80211_BAND_6GHZ)
370237f65aSZong-Zhe Yang 
38e3ec7017SPing-Ke Shih static struct ieee80211_channel rtw89_channels_2ghz[] = {
390237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2412, 1),
400237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2417, 2),
410237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2422, 3),
420237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2427, 4),
430237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2432, 5),
440237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2437, 6),
450237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2442, 7),
460237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2447, 8),
470237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2452, 9),
480237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2457, 10),
490237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2462, 11),
500237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2467, 12),
510237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2472, 13),
520237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_2G(2484, 14),
53e3ec7017SPing-Ke Shih };
54e3ec7017SPing-Ke Shih 
55e3ec7017SPing-Ke Shih static struct ieee80211_channel rtw89_channels_5ghz[] = {
560237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5180, 36),
570237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5200, 40),
580237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5220, 44),
590237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5240, 48),
600237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5260, 52),
610237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5280, 56),
620237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5300, 60),
630237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5320, 64),
640237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5500, 100),
650237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5520, 104),
660237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5540, 108),
670237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5560, 112),
680237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5580, 116),
690237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5600, 120),
700237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5620, 124),
710237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5640, 128),
720237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5660, 132),
730237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5680, 136),
740237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5700, 140),
750237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5720, 144),
760237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5745, 149),
770237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5765, 153),
780237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5785, 157),
790237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5805, 161),
800237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_5G_NO_HT40MINUS(5825, 165),
81e3b77c06SZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5845, 169),
82e3b77c06SZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5865, 173),
83e3b77c06SZong-Zhe Yang 	RTW89_DEF_CHAN_5G(5885, 177),
840237f65aSZong-Zhe Yang };
850237f65aSZong-Zhe Yang 
86d3c84636SZong-Zhe Yang static_assert(RTW89_5GHZ_UNII4_START_INDEX + RTW89_5GHZ_UNII4_CHANNEL_NUM ==
87d3c84636SZong-Zhe Yang 	      ARRAY_SIZE(rtw89_channels_5ghz));
88d3c84636SZong-Zhe Yang 
890237f65aSZong-Zhe Yang static struct ieee80211_channel rtw89_channels_6ghz[] = {
900237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(5955, 1),
910237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(5975, 5),
920237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(5995, 9),
930237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6015, 13),
940237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6035, 17),
950237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6055, 21),
960237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6075, 25),
970237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6095, 29),
980237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6115, 33),
990237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6135, 37),
1000237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6155, 41),
1010237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6175, 45),
1020237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6195, 49),
1030237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6215, 53),
1040237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6235, 57),
1050237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6255, 61),
1060237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6275, 65),
1070237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6295, 69),
1080237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6315, 73),
1090237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6335, 77),
1100237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6355, 81),
1110237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6375, 85),
1120237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6395, 89),
1130237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6415, 93),
1140237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6435, 97),
1150237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6455, 101),
1160237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6475, 105),
1170237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6495, 109),
1180237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6515, 113),
1190237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6535, 117),
1200237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6555, 121),
1210237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6575, 125),
1220237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6595, 129),
1230237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6615, 133),
1240237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6635, 137),
1250237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6655, 141),
1260237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6675, 145),
1270237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6695, 149),
1280237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6715, 153),
1290237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6735, 157),
1300237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6755, 161),
1310237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6775, 165),
1320237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6795, 169),
1330237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6815, 173),
1340237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6835, 177),
1350237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6855, 181),
1360237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6875, 185),
1370237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6895, 189),
1380237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6915, 193),
1390237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6935, 197),
1400237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6955, 201),
1410237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6975, 205),
1420237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(6995, 209),
1430237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(7015, 213),
1440237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(7035, 217),
1450237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(7055, 221),
1460237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(7075, 225),
1470237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(7095, 229),
1480237f65aSZong-Zhe Yang 	RTW89_DEF_CHAN_6G(7115, 233),
149e3ec7017SPing-Ke Shih };
150e3ec7017SPing-Ke Shih 
151e3ec7017SPing-Ke Shih static struct ieee80211_rate rtw89_bitrates[] = {
152e3ec7017SPing-Ke Shih 	{ .bitrate = 10,  .hw_value = 0x00, },
153e3ec7017SPing-Ke Shih 	{ .bitrate = 20,  .hw_value = 0x01, },
154e3ec7017SPing-Ke Shih 	{ .bitrate = 55,  .hw_value = 0x02, },
155e3ec7017SPing-Ke Shih 	{ .bitrate = 110, .hw_value = 0x03, },
156e3ec7017SPing-Ke Shih 	{ .bitrate = 60,  .hw_value = 0x04, },
157e3ec7017SPing-Ke Shih 	{ .bitrate = 90,  .hw_value = 0x05, },
158e3ec7017SPing-Ke Shih 	{ .bitrate = 120, .hw_value = 0x06, },
159e3ec7017SPing-Ke Shih 	{ .bitrate = 180, .hw_value = 0x07, },
160e3ec7017SPing-Ke Shih 	{ .bitrate = 240, .hw_value = 0x08, },
161e3ec7017SPing-Ke Shih 	{ .bitrate = 360, .hw_value = 0x09, },
162e3ec7017SPing-Ke Shih 	{ .bitrate = 480, .hw_value = 0x0a, },
163e3ec7017SPing-Ke Shih 	{ .bitrate = 540, .hw_value = 0x0b, },
164e3ec7017SPing-Ke Shih };
165e3ec7017SPing-Ke Shih 
166f22c0bffSPo-Hao Huang static const struct ieee80211_iface_limit rtw89_iface_limits[] = {
167f22c0bffSPo-Hao Huang 	{
168f22c0bffSPo-Hao Huang 		.max = 1,
169f22c0bffSPo-Hao Huang 		.types = BIT(NL80211_IFTYPE_STATION),
170f22c0bffSPo-Hao Huang 	},
171f22c0bffSPo-Hao Huang 	{
172f22c0bffSPo-Hao Huang 		.max = 1,
173f22c0bffSPo-Hao Huang 		.types = BIT(NL80211_IFTYPE_P2P_CLIENT) |
174f22c0bffSPo-Hao Huang 			 BIT(NL80211_IFTYPE_P2P_GO) |
175f22c0bffSPo-Hao Huang 			 BIT(NL80211_IFTYPE_AP),
176f22c0bffSPo-Hao Huang 	},
177f22c0bffSPo-Hao Huang };
178f22c0bffSPo-Hao Huang 
1798e73c045SZong-Zhe Yang static const struct ieee80211_iface_limit rtw89_iface_limits_mcc[] = {
1808e73c045SZong-Zhe Yang 	{
1818e73c045SZong-Zhe Yang 		.max = 1,
1828e73c045SZong-Zhe Yang 		.types = BIT(NL80211_IFTYPE_STATION),
1838e73c045SZong-Zhe Yang 	},
1848e73c045SZong-Zhe Yang 	{
1858e73c045SZong-Zhe Yang 		.max = 1,
1868e73c045SZong-Zhe Yang 		.types = BIT(NL80211_IFTYPE_P2P_CLIENT) |
1878e73c045SZong-Zhe Yang 			 BIT(NL80211_IFTYPE_P2P_GO),
1888e73c045SZong-Zhe Yang 	},
1898e73c045SZong-Zhe Yang };
1908e73c045SZong-Zhe Yang 
191f22c0bffSPo-Hao Huang static const struct ieee80211_iface_combination rtw89_iface_combs[] = {
192f22c0bffSPo-Hao Huang 	{
193f22c0bffSPo-Hao Huang 		.limits = rtw89_iface_limits,
194f22c0bffSPo-Hao Huang 		.n_limits = ARRAY_SIZE(rtw89_iface_limits),
195f22c0bffSPo-Hao Huang 		.max_interfaces = 2,
196f22c0bffSPo-Hao Huang 		.num_different_channels = 1,
1978e73c045SZong-Zhe Yang 	},
1988e73c045SZong-Zhe Yang 	{
1998e73c045SZong-Zhe Yang 		.limits = rtw89_iface_limits_mcc,
2008e73c045SZong-Zhe Yang 		.n_limits = ARRAY_SIZE(rtw89_iface_limits_mcc),
2018e73c045SZong-Zhe Yang 		.max_interfaces = 2,
2028e73c045SZong-Zhe Yang 		.num_different_channels = 2,
2038e73c045SZong-Zhe Yang 	},
204f22c0bffSPo-Hao Huang };
205f22c0bffSPo-Hao Huang 
rtw89_ra_report_to_bitrate(struct rtw89_dev * rtwdev,u8 rpt_rate,u16 * bitrate)2069a3a593cSPing-Ke Shih bool rtw89_ra_report_to_bitrate(struct rtw89_dev *rtwdev, u8 rpt_rate, u16 *bitrate)
207e3ec7017SPing-Ke Shih {
208e3ec7017SPing-Ke Shih 	struct ieee80211_rate rate;
209e3ec7017SPing-Ke Shih 
210e3ec7017SPing-Ke Shih 	if (unlikely(rpt_rate >= ARRAY_SIZE(rtw89_bitrates))) {
2119a3a593cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_UNEXP, "invalid rpt rate %d\n", rpt_rate);
2129a3a593cSPing-Ke Shih 		return false;
213e3ec7017SPing-Ke Shih 	}
214e3ec7017SPing-Ke Shih 
215e3ec7017SPing-Ke Shih 	rate = rtw89_bitrates[rpt_rate];
2169a3a593cSPing-Ke Shih 	*bitrate = rate.bitrate;
217e3ec7017SPing-Ke Shih 
2189a3a593cSPing-Ke Shih 	return true;
219e3ec7017SPing-Ke Shih }
220e3ec7017SPing-Ke Shih 
221a29dba47SZong-Zhe Yang static const struct ieee80211_supported_band rtw89_sband_2ghz = {
222e3ec7017SPing-Ke Shih 	.band		= NL80211_BAND_2GHZ,
223e3ec7017SPing-Ke Shih 	.channels	= rtw89_channels_2ghz,
224e3ec7017SPing-Ke Shih 	.n_channels	= ARRAY_SIZE(rtw89_channels_2ghz),
225e3ec7017SPing-Ke Shih 	.bitrates	= rtw89_bitrates,
226e3ec7017SPing-Ke Shih 	.n_bitrates	= ARRAY_SIZE(rtw89_bitrates),
227e3ec7017SPing-Ke Shih 	.ht_cap		= {0},
228e3ec7017SPing-Ke Shih 	.vht_cap	= {0},
229e3ec7017SPing-Ke Shih };
230e3ec7017SPing-Ke Shih 
231a29dba47SZong-Zhe Yang static const struct ieee80211_supported_band rtw89_sband_5ghz = {
232e3ec7017SPing-Ke Shih 	.band		= NL80211_BAND_5GHZ,
233e3ec7017SPing-Ke Shih 	.channels	= rtw89_channels_5ghz,
234e3ec7017SPing-Ke Shih 	.n_channels	= ARRAY_SIZE(rtw89_channels_5ghz),
235e3ec7017SPing-Ke Shih 
236e3ec7017SPing-Ke Shih 	/* 5G has no CCK rates, 1M/2M/5.5M/11M */
237e3ec7017SPing-Ke Shih 	.bitrates	= rtw89_bitrates + 4,
238e3ec7017SPing-Ke Shih 	.n_bitrates	= ARRAY_SIZE(rtw89_bitrates) - 4,
239e3ec7017SPing-Ke Shih 	.ht_cap		= {0},
240e3ec7017SPing-Ke Shih 	.vht_cap	= {0},
241e3ec7017SPing-Ke Shih };
242e3ec7017SPing-Ke Shih 
243a29dba47SZong-Zhe Yang static const struct ieee80211_supported_band rtw89_sband_6ghz = {
2440237f65aSZong-Zhe Yang 	.band		= NL80211_BAND_6GHZ,
2450237f65aSZong-Zhe Yang 	.channels	= rtw89_channels_6ghz,
2460237f65aSZong-Zhe Yang 	.n_channels	= ARRAY_SIZE(rtw89_channels_6ghz),
2470237f65aSZong-Zhe Yang 
2480237f65aSZong-Zhe Yang 	/* 6G has no CCK rates, 1M/2M/5.5M/11M */
2490237f65aSZong-Zhe Yang 	.bitrates	= rtw89_bitrates + 4,
2500237f65aSZong-Zhe Yang 	.n_bitrates	= ARRAY_SIZE(rtw89_bitrates) - 4,
2510237f65aSZong-Zhe Yang };
2520237f65aSZong-Zhe Yang 
rtw89_traffic_stats_accu(struct rtw89_dev * rtwdev,struct rtw89_traffic_stats * stats,struct sk_buff * skb,bool tx)253e3ec7017SPing-Ke Shih static void rtw89_traffic_stats_accu(struct rtw89_dev *rtwdev,
254e3ec7017SPing-Ke Shih 				     struct rtw89_traffic_stats *stats,
255e3ec7017SPing-Ke Shih 				     struct sk_buff *skb, bool tx)
256e3ec7017SPing-Ke Shih {
257e3ec7017SPing-Ke Shih 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
258e3ec7017SPing-Ke Shih 
25992790c4eSChih-Kang Chang 	if (tx && ieee80211_is_assoc_req(hdr->frame_control))
26092790c4eSChih-Kang Chang 		rtw89_wow_parse_akm(rtwdev, skb);
26192790c4eSChih-Kang Chang 
262e3ec7017SPing-Ke Shih 	if (!ieee80211_is_data(hdr->frame_control))
263e3ec7017SPing-Ke Shih 		return;
264e3ec7017SPing-Ke Shih 
265e3ec7017SPing-Ke Shih 	if (is_broadcast_ether_addr(hdr->addr1) ||
266e3ec7017SPing-Ke Shih 	    is_multicast_ether_addr(hdr->addr1))
267e3ec7017SPing-Ke Shih 		return;
268e3ec7017SPing-Ke Shih 
269e3ec7017SPing-Ke Shih 	if (tx) {
270e3ec7017SPing-Ke Shih 		stats->tx_cnt++;
271e3ec7017SPing-Ke Shih 		stats->tx_unicast += skb->len;
272e3ec7017SPing-Ke Shih 	} else {
273e3ec7017SPing-Ke Shih 		stats->rx_cnt++;
274e3ec7017SPing-Ke Shih 		stats->rx_unicast += skb->len;
275e3ec7017SPing-Ke Shih 	}
276e3ec7017SPing-Ke Shih }
277e3ec7017SPing-Ke Shih 
rtw89_get_default_chandef(struct cfg80211_chan_def * chandef)278a88b6cc4SZong-Zhe Yang void rtw89_get_default_chandef(struct cfg80211_chan_def *chandef)
279a88b6cc4SZong-Zhe Yang {
280a88b6cc4SZong-Zhe Yang 	cfg80211_chandef_create(chandef, &rtw89_channels_2ghz[0],
281a88b6cc4SZong-Zhe Yang 				NL80211_CHAN_NO_HT);
282a88b6cc4SZong-Zhe Yang }
283a88b6cc4SZong-Zhe Yang 
rtw89_get_channel_params(const struct cfg80211_chan_def * chandef,struct rtw89_chan * chan)2844843aa37SZong-Zhe Yang void rtw89_get_channel_params(const struct cfg80211_chan_def *chandef,
2853e5831caSZong-Zhe Yang 			      struct rtw89_chan *chan)
286e3ec7017SPing-Ke Shih {
287e3ec7017SPing-Ke Shih 	struct ieee80211_channel *channel = chandef->chan;
288e3ec7017SPing-Ke Shih 	enum nl80211_chan_width width = chandef->width;
289e3ec7017SPing-Ke Shih 	u32 primary_freq, center_freq;
290e3ec7017SPing-Ke Shih 	u8 center_chan;
291e3ec7017SPing-Ke Shih 	u8 bandwidth = RTW89_CHANNEL_WIDTH_20;
292e715f10fSPing-Ke Shih 	u32 offset;
2930237f65aSZong-Zhe Yang 	u8 band;
294e3ec7017SPing-Ke Shih 
295e3ec7017SPing-Ke Shih 	center_chan = channel->hw_value;
296e3ec7017SPing-Ke Shih 	primary_freq = channel->center_freq;
297e3ec7017SPing-Ke Shih 	center_freq = chandef->center_freq1;
298e3ec7017SPing-Ke Shih 
299e3ec7017SPing-Ke Shih 	switch (width) {
300e3ec7017SPing-Ke Shih 	case NL80211_CHAN_WIDTH_20_NOHT:
301e3ec7017SPing-Ke Shih 	case NL80211_CHAN_WIDTH_20:
302e3ec7017SPing-Ke Shih 		bandwidth = RTW89_CHANNEL_WIDTH_20;
303e3ec7017SPing-Ke Shih 		break;
304e3ec7017SPing-Ke Shih 	case NL80211_CHAN_WIDTH_40:
305e3ec7017SPing-Ke Shih 		bandwidth = RTW89_CHANNEL_WIDTH_40;
306e3ec7017SPing-Ke Shih 		if (primary_freq > center_freq) {
307e3ec7017SPing-Ke Shih 			center_chan -= 2;
308e3ec7017SPing-Ke Shih 		} else {
309e3ec7017SPing-Ke Shih 			center_chan += 2;
310e3ec7017SPing-Ke Shih 		}
311e3ec7017SPing-Ke Shih 		break;
312e3ec7017SPing-Ke Shih 	case NL80211_CHAN_WIDTH_80:
313e715f10fSPing-Ke Shih 	case NL80211_CHAN_WIDTH_160:
314e715f10fSPing-Ke Shih 		bandwidth = nl_to_rtw89_bandwidth(width);
315e3ec7017SPing-Ke Shih 		if (primary_freq > center_freq) {
316e715f10fSPing-Ke Shih 			offset = (primary_freq - center_freq - 10) / 20;
317e715f10fSPing-Ke Shih 			center_chan -= 2 + offset * 4;
318e3ec7017SPing-Ke Shih 		} else {
319e715f10fSPing-Ke Shih 			offset = (center_freq - primary_freq - 10) / 20;
320e715f10fSPing-Ke Shih 			center_chan += 2 + offset * 4;
321e3ec7017SPing-Ke Shih 		}
322e3ec7017SPing-Ke Shih 		break;
323e3ec7017SPing-Ke Shih 	default:
324e3ec7017SPing-Ke Shih 		center_chan = 0;
325e3ec7017SPing-Ke Shih 		break;
326e3ec7017SPing-Ke Shih 	}
327e3ec7017SPing-Ke Shih 
3280237f65aSZong-Zhe Yang 	switch (channel->band) {
3290237f65aSZong-Zhe Yang 	default:
3300237f65aSZong-Zhe Yang 	case NL80211_BAND_2GHZ:
3310237f65aSZong-Zhe Yang 		band = RTW89_BAND_2G;
3320237f65aSZong-Zhe Yang 		break;
3330237f65aSZong-Zhe Yang 	case NL80211_BAND_5GHZ:
3340237f65aSZong-Zhe Yang 		band = RTW89_BAND_5G;
3350237f65aSZong-Zhe Yang 		break;
3360237f65aSZong-Zhe Yang 	case NL80211_BAND_6GHZ:
3370237f65aSZong-Zhe Yang 		band = RTW89_BAND_6G;
3380237f65aSZong-Zhe Yang 		break;
3390237f65aSZong-Zhe Yang 	}
3400237f65aSZong-Zhe Yang 
341bb8152b3SZong-Zhe Yang 	rtw89_chan_create(chan, center_chan, channel->hw_value, band, bandwidth);
342e3ec7017SPing-Ke Shih }
343e3ec7017SPing-Ke Shih 
rtw89_core_set_chip_txpwr(struct rtw89_dev * rtwdev)344967439c7SZong-Zhe Yang void rtw89_core_set_chip_txpwr(struct rtw89_dev *rtwdev)
345967439c7SZong-Zhe Yang {
3464843aa37SZong-Zhe Yang 	struct rtw89_hal *hal = &rtwdev->hal;
347967439c7SZong-Zhe Yang 	const struct rtw89_chip_info *chip = rtwdev->chip;
34807ef5f2fSZong-Zhe Yang 	const struct rtw89_chan *chan;
349583e998eSZong-Zhe Yang 	enum rtw89_chanctx_idx chanctx_idx;
350583e998eSZong-Zhe Yang 	enum rtw89_chanctx_idx roc_idx;
3517cf674ffSZong-Zhe Yang 	enum rtw89_phy_idx phy_idx;
3527cf674ffSZong-Zhe Yang 	enum rtw89_entity_mode mode;
353967439c7SZong-Zhe Yang 	bool entity_active;
354967439c7SZong-Zhe Yang 
355967439c7SZong-Zhe Yang 	entity_active = rtw89_get_entity_state(rtwdev);
356967439c7SZong-Zhe Yang 	if (!entity_active)
357967439c7SZong-Zhe Yang 		return;
358967439c7SZong-Zhe Yang 
3597cf674ffSZong-Zhe Yang 	mode = rtw89_get_entity_mode(rtwdev);
3604843aa37SZong-Zhe Yang 	switch (mode) {
3614843aa37SZong-Zhe Yang 	case RTW89_ENTITY_MODE_SCC:
3624843aa37SZong-Zhe Yang 	case RTW89_ENTITY_MODE_MCC:
363583e998eSZong-Zhe Yang 		chanctx_idx = RTW89_CHANCTX_0;
3644843aa37SZong-Zhe Yang 		break;
3654843aa37SZong-Zhe Yang 	case RTW89_ENTITY_MODE_MCC_PREPARE:
366583e998eSZong-Zhe Yang 		chanctx_idx = RTW89_CHANCTX_1;
3674843aa37SZong-Zhe Yang 		break;
3684843aa37SZong-Zhe Yang 	default:
3694843aa37SZong-Zhe Yang 		WARN(1, "Invalid ent mode: %d\n", mode);
3704843aa37SZong-Zhe Yang 		return;
3714843aa37SZong-Zhe Yang 	}
3724843aa37SZong-Zhe Yang 
373d03b3d74SZong-Zhe Yang 	roc_idx = atomic_read(&hal->roc_chanctx_idx);
374583e998eSZong-Zhe Yang 	if (roc_idx != RTW89_CHANCTX_IDLE)
375583e998eSZong-Zhe Yang 		chanctx_idx = roc_idx;
3764843aa37SZong-Zhe Yang 
3777cf674ffSZong-Zhe Yang 	phy_idx = RTW89_PHY_0;
378583e998eSZong-Zhe Yang 	chan = rtw89_chan_get(rtwdev, chanctx_idx);
3797cf674ffSZong-Zhe Yang 	chip->ops->set_txpwr(rtwdev, chan, phy_idx);
380967439c7SZong-Zhe Yang }
381967439c7SZong-Zhe Yang 
rtw89_set_channel(struct rtw89_dev * rtwdev)3821ae9fbafSZong-Zhe Yang int rtw89_set_channel(struct rtw89_dev *rtwdev)
383e3ec7017SPing-Ke Shih {
3844843aa37SZong-Zhe Yang 	struct rtw89_hal *hal = &rtwdev->hal;
385e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
3864843aa37SZong-Zhe Yang 	const struct rtw89_chan_rcd *chan_rcd;
3874843aa37SZong-Zhe Yang 	const struct rtw89_chan *chan;
388583e998eSZong-Zhe Yang 	enum rtw89_chanctx_idx chanctx_idx;
389583e998eSZong-Zhe Yang 	enum rtw89_chanctx_idx roc_idx;
3907cf674ffSZong-Zhe Yang 	enum rtw89_mac_idx mac_idx;
3917cf674ffSZong-Zhe Yang 	enum rtw89_phy_idx phy_idx;
392e3ec7017SPing-Ke Shih 	struct rtw89_channel_help_params bak;
3937cf674ffSZong-Zhe Yang 	enum rtw89_entity_mode mode;
394967439c7SZong-Zhe Yang 	bool entity_active;
395967439c7SZong-Zhe Yang 
396967439c7SZong-Zhe Yang 	entity_active = rtw89_get_entity_state(rtwdev);
397e3ec7017SPing-Ke Shih 
3987cf674ffSZong-Zhe Yang 	mode = rtw89_entity_recalc(rtwdev);
3994843aa37SZong-Zhe Yang 	switch (mode) {
4004843aa37SZong-Zhe Yang 	case RTW89_ENTITY_MODE_SCC:
4014843aa37SZong-Zhe Yang 	case RTW89_ENTITY_MODE_MCC:
402583e998eSZong-Zhe Yang 		chanctx_idx = RTW89_CHANCTX_0;
4034843aa37SZong-Zhe Yang 		break;
4044843aa37SZong-Zhe Yang 	case RTW89_ENTITY_MODE_MCC_PREPARE:
405583e998eSZong-Zhe Yang 		chanctx_idx = RTW89_CHANCTX_1;
4064843aa37SZong-Zhe Yang 		break;
4074843aa37SZong-Zhe Yang 	default:
4084843aa37SZong-Zhe Yang 		WARN(1, "Invalid ent mode: %d\n", mode);
4091ae9fbafSZong-Zhe Yang 		return -EINVAL;
4104843aa37SZong-Zhe Yang 	}
4114843aa37SZong-Zhe Yang 
412d03b3d74SZong-Zhe Yang 	roc_idx = atomic_read(&hal->roc_chanctx_idx);
413583e998eSZong-Zhe Yang 	if (roc_idx != RTW89_CHANCTX_IDLE)
414583e998eSZong-Zhe Yang 		chanctx_idx = roc_idx;
4154843aa37SZong-Zhe Yang 
4167cf674ffSZong-Zhe Yang 	mac_idx = RTW89_MAC_0;
4177cf674ffSZong-Zhe Yang 	phy_idx = RTW89_PHY_0;
418e3ec7017SPing-Ke Shih 
419583e998eSZong-Zhe Yang 	chan = rtw89_chan_get(rtwdev, chanctx_idx);
420583e998eSZong-Zhe Yang 	chan_rcd = rtw89_chan_rcd_get(rtwdev, chanctx_idx);
421e3ec7017SPing-Ke Shih 
4224843aa37SZong-Zhe Yang 	rtw89_chip_set_channel_prepare(rtwdev, &bak, chan, mac_idx, phy_idx);
423967439c7SZong-Zhe Yang 
4244843aa37SZong-Zhe Yang 	chip->ops->set_channel(rtwdev, chan, mac_idx, phy_idx);
425e3ec7017SPing-Ke Shih 
4264843aa37SZong-Zhe Yang 	chip->ops->set_txpwr(rtwdev, chan, phy_idx);
427e3ec7017SPing-Ke Shih 
4284843aa37SZong-Zhe Yang 	rtw89_chip_set_channel_done(rtwdev, &bak, chan, mac_idx, phy_idx);
429e3ec7017SPing-Ke Shih 
4304843aa37SZong-Zhe Yang 	if (!entity_active || chan_rcd->band_changed) {
4314843aa37SZong-Zhe Yang 		rtw89_btc_ntfy_switch_band(rtwdev, phy_idx, chan->band_type);
4327c31f102SZong-Zhe Yang 		rtw89_chip_rfk_band_changed(rtwdev, phy_idx, chan);
433e3ec7017SPing-Ke Shih 	}
4347cf674ffSZong-Zhe Yang 
4357cf674ffSZong-Zhe Yang 	rtw89_set_entity_state(rtwdev, true);
4361ae9fbafSZong-Zhe Yang 	return 0;
437e3ec7017SPing-Ke Shih }
438e3ec7017SPing-Ke Shih 
rtw89_get_channel(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,struct rtw89_chan * chan)439e7399db2SPo-Hao Huang void rtw89_get_channel(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
440e7399db2SPo-Hao Huang 		       struct rtw89_chan *chan)
441e7399db2SPo-Hao Huang {
442e7399db2SPo-Hao Huang 	const struct cfg80211_chan_def *chandef;
443e7399db2SPo-Hao Huang 
444583e998eSZong-Zhe Yang 	chandef = rtw89_chandef_get(rtwdev, rtwvif->chanctx_idx);
445e7399db2SPo-Hao Huang 	rtw89_get_channel_params(chandef, chan);
446e7399db2SPo-Hao Huang }
447e7399db2SPo-Hao Huang 
448e3ec7017SPing-Ke Shih static enum rtw89_core_tx_type
rtw89_core_get_tx_type(struct rtw89_dev * rtwdev,struct sk_buff * skb)449e3ec7017SPing-Ke Shih rtw89_core_get_tx_type(struct rtw89_dev *rtwdev,
450e3ec7017SPing-Ke Shih 		       struct sk_buff *skb)
451e3ec7017SPing-Ke Shih {
452e3ec7017SPing-Ke Shih 	struct ieee80211_hdr *hdr = (void *)skb->data;
453e3ec7017SPing-Ke Shih 	__le16 fc = hdr->frame_control;
454e3ec7017SPing-Ke Shih 
455e3ec7017SPing-Ke Shih 	if (ieee80211_is_mgmt(fc) || ieee80211_is_nullfunc(fc))
456e3ec7017SPing-Ke Shih 		return RTW89_CORE_TX_TYPE_MGMT;
457e3ec7017SPing-Ke Shih 
458e3ec7017SPing-Ke Shih 	return RTW89_CORE_TX_TYPE_DATA;
459e3ec7017SPing-Ke Shih }
460e3ec7017SPing-Ke Shih 
461e3ec7017SPing-Ke Shih static void
rtw89_core_tx_update_ampdu_info(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req,enum btc_pkt_type pkt_type)462e3ec7017SPing-Ke Shih rtw89_core_tx_update_ampdu_info(struct rtw89_dev *rtwdev,
463679955d5SKuan-Chung Chen 				struct rtw89_core_tx_request *tx_req,
464679955d5SKuan-Chung Chen 				enum btc_pkt_type pkt_type)
465e3ec7017SPing-Ke Shih {
466e3ec7017SPing-Ke Shih 	struct ieee80211_sta *sta = tx_req->sta;
467e3ec7017SPing-Ke Shih 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
468679955d5SKuan-Chung Chen 	struct sk_buff *skb = tx_req->skb;
469e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta;
470e3ec7017SPing-Ke Shih 	u8 ampdu_num;
471679955d5SKuan-Chung Chen 	u8 tid;
472679955d5SKuan-Chung Chen 
473679955d5SKuan-Chung Chen 	if (pkt_type == PACKET_EAPOL) {
474679955d5SKuan-Chung Chen 		desc_info->bk = true;
475679955d5SKuan-Chung Chen 		return;
476679955d5SKuan-Chung Chen 	}
477679955d5SKuan-Chung Chen 
478679955d5SKuan-Chung Chen 	if (!(IEEE80211_SKB_CB(skb)->flags & IEEE80211_TX_CTL_AMPDU))
479679955d5SKuan-Chung Chen 		return;
480e3ec7017SPing-Ke Shih 
481e3ec7017SPing-Ke Shih 	if (!sta) {
482e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "cannot set ampdu info without sta\n");
483e3ec7017SPing-Ke Shih 		return;
484e3ec7017SPing-Ke Shih 	}
485e3ec7017SPing-Ke Shih 
486679955d5SKuan-Chung Chen 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
487e3ec7017SPing-Ke Shih 	rtwsta = (struct rtw89_sta *)sta->drv_priv;
488e3ec7017SPing-Ke Shih 
489e3ec7017SPing-Ke Shih 	ampdu_num = (u8)((rtwsta->ampdu_params[tid].agg_num ?
490e3ec7017SPing-Ke Shih 			  rtwsta->ampdu_params[tid].agg_num :
491046d2e7cSSriram R 			  4 << sta->deflink.ht_cap.ampdu_factor) - 1);
492e3ec7017SPing-Ke Shih 
493e3ec7017SPing-Ke Shih 	desc_info->agg_en = true;
494046d2e7cSSriram R 	desc_info->ampdu_density = sta->deflink.ht_cap.ampdu_density;
495e3ec7017SPing-Ke Shih 	desc_info->ampdu_num = ampdu_num;
496e3ec7017SPing-Ke Shih }
497e3ec7017SPing-Ke Shih 
498e3ec7017SPing-Ke Shih static void
rtw89_core_tx_update_sec_key(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)499e3ec7017SPing-Ke Shih rtw89_core_tx_update_sec_key(struct rtw89_dev *rtwdev,
500e3ec7017SPing-Ke Shih 			     struct rtw89_core_tx_request *tx_req)
501e3ec7017SPing-Ke Shih {
5020448c65aSZong-Zhe Yang 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
50379a6c9a4SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
5040448c65aSZong-Zhe Yang 	const struct rtw89_sec_cam_entry *sec_cam;
505e3ec7017SPing-Ke Shih 	struct ieee80211_tx_info *info;
506e3ec7017SPing-Ke Shih 	struct ieee80211_key_conf *key;
507e3ec7017SPing-Ke Shih 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
508e3ec7017SPing-Ke Shih 	struct sk_buff *skb = tx_req->skb;
509e3ec7017SPing-Ke Shih 	u8 sec_type = RTW89_SEC_KEY_TYPE_NONE;
5100448c65aSZong-Zhe Yang 	u8 sec_cam_idx;
51179a6c9a4SPing-Ke Shih 	u64 pn64;
512e3ec7017SPing-Ke Shih 
513e3ec7017SPing-Ke Shih 	info = IEEE80211_SKB_CB(skb);
514e3ec7017SPing-Ke Shih 	key = info->control.hw_key;
5150448c65aSZong-Zhe Yang 	sec_cam_idx = key->hw_key_idx;
5160448c65aSZong-Zhe Yang 	sec_cam = cam_info->sec_entries[sec_cam_idx];
517e3ec7017SPing-Ke Shih 	if (!sec_cam) {
518e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "sec cam entry is empty\n");
519e3ec7017SPing-Ke Shih 		return;
520e3ec7017SPing-Ke Shih 	}
521e3ec7017SPing-Ke Shih 
522e3ec7017SPing-Ke Shih 	switch (key->cipher) {
523e3ec7017SPing-Ke Shih 	case WLAN_CIPHER_SUITE_WEP40:
524e3ec7017SPing-Ke Shih 		sec_type = RTW89_SEC_KEY_TYPE_WEP40;
525e3ec7017SPing-Ke Shih 		break;
526e3ec7017SPing-Ke Shih 	case WLAN_CIPHER_SUITE_WEP104:
527e3ec7017SPing-Ke Shih 		sec_type = RTW89_SEC_KEY_TYPE_WEP104;
528e3ec7017SPing-Ke Shih 		break;
529e3ec7017SPing-Ke Shih 	case WLAN_CIPHER_SUITE_TKIP:
530e3ec7017SPing-Ke Shih 		sec_type = RTW89_SEC_KEY_TYPE_TKIP;
531e3ec7017SPing-Ke Shih 		break;
532e3ec7017SPing-Ke Shih 	case WLAN_CIPHER_SUITE_CCMP:
533e3ec7017SPing-Ke Shih 		sec_type = RTW89_SEC_KEY_TYPE_CCMP128;
534e3ec7017SPing-Ke Shih 		break;
535e3ec7017SPing-Ke Shih 	case WLAN_CIPHER_SUITE_CCMP_256:
536e3ec7017SPing-Ke Shih 		sec_type = RTW89_SEC_KEY_TYPE_CCMP256;
537e3ec7017SPing-Ke Shih 		break;
538e3ec7017SPing-Ke Shih 	case WLAN_CIPHER_SUITE_GCMP:
539e3ec7017SPing-Ke Shih 		sec_type = RTW89_SEC_KEY_TYPE_GCMP128;
540e3ec7017SPing-Ke Shih 		break;
541e3ec7017SPing-Ke Shih 	case WLAN_CIPHER_SUITE_GCMP_256:
542e3ec7017SPing-Ke Shih 		sec_type = RTW89_SEC_KEY_TYPE_GCMP256;
543e3ec7017SPing-Ke Shih 		break;
544e3ec7017SPing-Ke Shih 	default:
545e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "key cipher not supported %d\n", key->cipher);
546e3ec7017SPing-Ke Shih 		return;
547e3ec7017SPing-Ke Shih 	}
548e3ec7017SPing-Ke Shih 
549e3ec7017SPing-Ke Shih 	desc_info->sec_en = true;
55079a6c9a4SPing-Ke Shih 	desc_info->sec_keyid = key->keyidx;
551e3ec7017SPing-Ke Shih 	desc_info->sec_type = sec_type;
552e3ec7017SPing-Ke Shih 	desc_info->sec_cam_idx = sec_cam->sec_cam_idx;
55379a6c9a4SPing-Ke Shih 
55479a6c9a4SPing-Ke Shih 	if (!chip->hw_sec_hdr)
55579a6c9a4SPing-Ke Shih 		return;
55679a6c9a4SPing-Ke Shih 
55779a6c9a4SPing-Ke Shih 	pn64 = atomic64_inc_return(&key->tx_pn);
55879a6c9a4SPing-Ke Shih 	desc_info->sec_seq[0] = pn64;
55979a6c9a4SPing-Ke Shih 	desc_info->sec_seq[1] = pn64 >> 8;
56079a6c9a4SPing-Ke Shih 	desc_info->sec_seq[2] = pn64 >> 16;
56179a6c9a4SPing-Ke Shih 	desc_info->sec_seq[3] = pn64 >> 24;
56279a6c9a4SPing-Ke Shih 	desc_info->sec_seq[4] = pn64 >> 32;
56379a6c9a4SPing-Ke Shih 	desc_info->sec_seq[5] = pn64 >> 40;
56479a6c9a4SPing-Ke Shih 	desc_info->wp_offset = 1; /* in unit of 8 bytes for security header */
565e3ec7017SPing-Ke Shih }
566e3ec7017SPing-Ke Shih 
rtw89_core_get_mgmt_rate(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req,const struct rtw89_chan * chan)567e3ec7017SPing-Ke Shih static u16 rtw89_core_get_mgmt_rate(struct rtw89_dev *rtwdev,
568ad3dc722SZong-Zhe Yang 				    struct rtw89_core_tx_request *tx_req,
569ad3dc722SZong-Zhe Yang 				    const struct rtw89_chan *chan)
570e3ec7017SPing-Ke Shih {
571e3ec7017SPing-Ke Shih 	struct sk_buff *skb = tx_req->skb;
572e3ec7017SPing-Ke Shih 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
573e3ec7017SPing-Ke Shih 	struct ieee80211_vif *vif = tx_info->control.vif;
5743788c599SDian-Syuan Yang 	u16 lowest_rate;
5753788c599SDian-Syuan Yang 
57695dafeabSKuan-Chung Chen 	if (tx_info->flags & IEEE80211_TX_CTL_NO_CCK_RATE ||
57795dafeabSKuan-Chung Chen 	    (vif && vif->p2p))
5783788c599SDian-Syuan Yang 		lowest_rate = RTW89_HW_RATE_OFDM6;
5793788c599SDian-Syuan Yang 	else if (chan->band_type == RTW89_BAND_2G)
5803788c599SDian-Syuan Yang 		lowest_rate = RTW89_HW_RATE_CCK1;
5813788c599SDian-Syuan Yang 	else
5823788c599SDian-Syuan Yang 		lowest_rate = RTW89_HW_RATE_OFDM6;
583e3ec7017SPing-Ke Shih 
584e3ec7017SPing-Ke Shih 	if (!vif || !vif->bss_conf.basic_rates || !tx_req->sta)
585e3ec7017SPing-Ke Shih 		return lowest_rate;
586e3ec7017SPing-Ke Shih 
587e3ec7017SPing-Ke Shih 	return __ffs(vif->bss_conf.basic_rates) + lowest_rate;
588e3ec7017SPing-Ke Shih }
589e3ec7017SPing-Ke Shih 
rtw89_core_tx_get_mac_id(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)590877287f9SKuan-Chung Chen static u8 rtw89_core_tx_get_mac_id(struct rtw89_dev *rtwdev,
591877287f9SKuan-Chung Chen 				   struct rtw89_core_tx_request *tx_req)
592877287f9SKuan-Chung Chen {
593877287f9SKuan-Chung Chen 	struct ieee80211_vif *vif = tx_req->vif;
594877287f9SKuan-Chung Chen 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
595877287f9SKuan-Chung Chen 	struct ieee80211_sta *sta = tx_req->sta;
596877287f9SKuan-Chung Chen 	struct rtw89_sta *rtwsta;
597877287f9SKuan-Chung Chen 
598877287f9SKuan-Chung Chen 	if (!sta)
599877287f9SKuan-Chung Chen 		return rtwvif->mac_id;
600877287f9SKuan-Chung Chen 
601877287f9SKuan-Chung Chen 	rtwsta = (struct rtw89_sta *)sta->drv_priv;
602877287f9SKuan-Chung Chen 	return rtwsta->mac_id;
603877287f9SKuan-Chung Chen }
604877287f9SKuan-Chung Chen 
rtw89_core_tx_update_llc_hdr(struct rtw89_dev * rtwdev,struct rtw89_tx_desc_info * desc_info,struct sk_buff * skb)60550961f88SKuan-Chung Chen static void rtw89_core_tx_update_llc_hdr(struct rtw89_dev *rtwdev,
60650961f88SKuan-Chung Chen 					 struct rtw89_tx_desc_info *desc_info,
60750961f88SKuan-Chung Chen 					 struct sk_buff *skb)
60850961f88SKuan-Chung Chen {
60950961f88SKuan-Chung Chen 	struct ieee80211_hdr *hdr = (void *)skb->data;
61050961f88SKuan-Chung Chen 	__le16 fc = hdr->frame_control;
61150961f88SKuan-Chung Chen 
61250961f88SKuan-Chung Chen 	desc_info->hdr_llc_len = ieee80211_hdrlen(fc);
61350961f88SKuan-Chung Chen 	desc_info->hdr_llc_len >>= 1; /* in unit of 2 bytes */
61450961f88SKuan-Chung Chen }
61550961f88SKuan-Chung Chen 
616e3ec7017SPing-Ke Shih static void
rtw89_core_tx_update_mgmt_info(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)617e3ec7017SPing-Ke Shih rtw89_core_tx_update_mgmt_info(struct rtw89_dev *rtwdev,
618e3ec7017SPing-Ke Shih 			       struct rtw89_core_tx_request *tx_req)
619e3ec7017SPing-Ke Shih {
62050961f88SKuan-Chung Chen 	const struct rtw89_chip_info *chip = rtwdev->chip;
6219eecaec2SPing-Ke Shih 	struct ieee80211_vif *vif = tx_req->vif;
6229eecaec2SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
623e3ec7017SPing-Ke Shih 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
624ad3dc722SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
625583e998eSZong-Zhe Yang 						       rtwvif->chanctx_idx);
62650961f88SKuan-Chung Chen 	struct sk_buff *skb = tx_req->skb;
627e3ec7017SPing-Ke Shih 	u8 qsel, ch_dma;
628e3ec7017SPing-Ke Shih 
62911d261f2SPing-Ke Shih 	qsel = desc_info->hiq ? RTW89_TX_QSEL_B0_HI : RTW89_TX_QSEL_B0_MGMT;
630e3ec7017SPing-Ke Shih 	ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
631e3ec7017SPing-Ke Shih 
63211d261f2SPing-Ke Shih 	desc_info->qsel = qsel;
633e3ec7017SPing-Ke Shih 	desc_info->ch_dma = ch_dma;
6349eecaec2SPing-Ke Shih 	desc_info->port = desc_info->hiq ? rtwvif->port : 0;
635877287f9SKuan-Chung Chen 	desc_info->mac_id = rtw89_core_tx_get_mac_id(rtwdev, tx_req);
63691644020SPing-Ke Shih 	desc_info->hw_ssn_sel = RTW89_MGMT_HW_SSN_SEL;
63791644020SPing-Ke Shih 	desc_info->hw_seq_mode = RTW89_MGMT_HW_SEQ_MODE;
638e3ec7017SPing-Ke Shih 
639e3ec7017SPing-Ke Shih 	/* fixed data rate for mgmt frames */
640e3ec7017SPing-Ke Shih 	desc_info->en_wd_info = true;
641e3ec7017SPing-Ke Shih 	desc_info->use_rate = true;
642e3ec7017SPing-Ke Shih 	desc_info->dis_data_fb = true;
643ad3dc722SZong-Zhe Yang 	desc_info->data_rate = rtw89_core_get_mgmt_rate(rtwdev, tx_req, chan);
644e3ec7017SPing-Ke Shih 
64550961f88SKuan-Chung Chen 	if (chip->hw_mgmt_tx_encrypt && IEEE80211_SKB_CB(skb)->control.hw_key) {
64650961f88SKuan-Chung Chen 		rtw89_core_tx_update_sec_key(rtwdev, tx_req);
64750961f88SKuan-Chung Chen 		rtw89_core_tx_update_llc_hdr(rtwdev, desc_info, skb);
64850961f88SKuan-Chung Chen 	}
64950961f88SKuan-Chung Chen 
650e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_TXRX,
651cbb145b9SZong-Zhe Yang 		    "tx mgmt frame with rate 0x%x on channel %d (band %d, bw %d)\n",
652cbb145b9SZong-Zhe Yang 		    desc_info->data_rate, chan->channel, chan->band_type,
653cbb145b9SZong-Zhe Yang 		    chan->band_width);
654e3ec7017SPing-Ke Shih }
655e3ec7017SPing-Ke Shih 
656e3ec7017SPing-Ke Shih static void
rtw89_core_tx_update_h2c_info(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)657e3ec7017SPing-Ke Shih rtw89_core_tx_update_h2c_info(struct rtw89_dev *rtwdev,
658e3ec7017SPing-Ke Shih 			      struct rtw89_core_tx_request *tx_req)
659e3ec7017SPing-Ke Shih {
660e3ec7017SPing-Ke Shih 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
661e3ec7017SPing-Ke Shih 
662e3ec7017SPing-Ke Shih 	desc_info->is_bmc = false;
663e3ec7017SPing-Ke Shih 	desc_info->wd_page = false;
664e3ec7017SPing-Ke Shih 	desc_info->ch_dma = RTW89_DMA_H2C;
665e3ec7017SPing-Ke Shih }
666e3ec7017SPing-Ke Shih 
rtw89_core_get_no_ul_ofdma_htc(struct rtw89_dev * rtwdev,__le32 * htc,const struct rtw89_chan * chan)667ad3dc722SZong-Zhe Yang static void rtw89_core_get_no_ul_ofdma_htc(struct rtw89_dev *rtwdev, __le32 *htc,
668ad3dc722SZong-Zhe Yang 					   const struct rtw89_chan *chan)
669e3ec7017SPing-Ke Shih {
670e3ec7017SPing-Ke Shih 	static const u8 rtw89_bandwidth_to_om[] = {
671e3ec7017SPing-Ke Shih 		[RTW89_CHANNEL_WIDTH_20] = HTC_OM_CHANNEL_WIDTH_20,
672e3ec7017SPing-Ke Shih 		[RTW89_CHANNEL_WIDTH_40] = HTC_OM_CHANNEL_WIDTH_40,
673e3ec7017SPing-Ke Shih 		[RTW89_CHANNEL_WIDTH_80] = HTC_OM_CHANNEL_WIDTH_80,
674e3ec7017SPing-Ke Shih 		[RTW89_CHANNEL_WIDTH_160] = HTC_OM_CHANNEL_WIDTH_160_OR_80_80,
675e3ec7017SPing-Ke Shih 		[RTW89_CHANNEL_WIDTH_80_80] = HTC_OM_CHANNEL_WIDTH_160_OR_80_80,
676e3ec7017SPing-Ke Shih 	};
677e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
678e3ec7017SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
679e3ec7017SPing-Ke Shih 	u8 om_bandwidth;
680e3ec7017SPing-Ke Shih 
681e3ec7017SPing-Ke Shih 	if (!chip->dis_2g_40m_ul_ofdma ||
682cbb145b9SZong-Zhe Yang 	    chan->band_type != RTW89_BAND_2G ||
683cbb145b9SZong-Zhe Yang 	    chan->band_width != RTW89_CHANNEL_WIDTH_40)
684e3ec7017SPing-Ke Shih 		return;
685e3ec7017SPing-Ke Shih 
686cbb145b9SZong-Zhe Yang 	om_bandwidth = chan->band_width < ARRAY_SIZE(rtw89_bandwidth_to_om) ?
687cbb145b9SZong-Zhe Yang 		       rtw89_bandwidth_to_om[chan->band_width] : 0;
688e3ec7017SPing-Ke Shih 	*htc = le32_encode_bits(RTW89_HTC_VARIANT_HE, RTW89_HTC_MASK_VARIANT) |
689e3ec7017SPing-Ke Shih 	       le32_encode_bits(RTW89_HTC_VARIANT_HE_CID_OM, RTW89_HTC_MASK_CTL_ID) |
690e3ec7017SPing-Ke Shih 	       le32_encode_bits(hal->rx_nss - 1, RTW89_HTC_MASK_HTC_OM_RX_NSS) |
691e3ec7017SPing-Ke Shih 	       le32_encode_bits(om_bandwidth, RTW89_HTC_MASK_HTC_OM_CH_WIDTH) |
692e3ec7017SPing-Ke Shih 	       le32_encode_bits(1, RTW89_HTC_MASK_HTC_OM_UL_MU_DIS) |
693e3ec7017SPing-Ke Shih 	       le32_encode_bits(hal->tx_nss - 1, RTW89_HTC_MASK_HTC_OM_TX_NSTS) |
694e3ec7017SPing-Ke Shih 	       le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_ER_SU_DIS) |
695e3ec7017SPing-Ke Shih 	       le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_DL_MU_MIMO_RR) |
696e3ec7017SPing-Ke Shih 	       le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_UL_MU_DATA_DIS);
697e3ec7017SPing-Ke Shih }
698e3ec7017SPing-Ke Shih 
699e3ec7017SPing-Ke Shih static bool
__rtw89_core_tx_check_he_qos_htc(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req,enum btc_pkt_type pkt_type)700e3ec7017SPing-Ke Shih __rtw89_core_tx_check_he_qos_htc(struct rtw89_dev *rtwdev,
701e3ec7017SPing-Ke Shih 				 struct rtw89_core_tx_request *tx_req,
702e3ec7017SPing-Ke Shih 				 enum btc_pkt_type pkt_type)
703e3ec7017SPing-Ke Shih {
704e3ec7017SPing-Ke Shih 	struct ieee80211_sta *sta = tx_req->sta;
7050d466f05SPing-Ke Shih 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
706e3ec7017SPing-Ke Shih 	struct sk_buff *skb = tx_req->skb;
707e3ec7017SPing-Ke Shih 	struct ieee80211_hdr *hdr = (void *)skb->data;
708e3ec7017SPing-Ke Shih 	__le16 fc = hdr->frame_control;
709e3ec7017SPing-Ke Shih 
710e3ec7017SPing-Ke Shih 	/* AP IOT issue with EAPoL, ARP and DHCP */
711e3ec7017SPing-Ke Shih 	if (pkt_type < PACKET_MAX)
712e3ec7017SPing-Ke Shih 		return false;
713e3ec7017SPing-Ke Shih 
714046d2e7cSSriram R 	if (!sta || !sta->deflink.he_cap.has_he)
715e3ec7017SPing-Ke Shih 		return false;
716e3ec7017SPing-Ke Shih 
717e3ec7017SPing-Ke Shih 	if (!ieee80211_is_data_qos(fc))
718e3ec7017SPing-Ke Shih 		return false;
719e3ec7017SPing-Ke Shih 
720e3ec7017SPing-Ke Shih 	if (skb_headroom(skb) < IEEE80211_HT_CTL_LEN)
721e3ec7017SPing-Ke Shih 		return false;
722e3ec7017SPing-Ke Shih 
7230d466f05SPing-Ke Shih 	if (rtwsta && rtwsta->ra_report.might_fallback_legacy)
7240d466f05SPing-Ke Shih 		return false;
7250d466f05SPing-Ke Shih 
726e3ec7017SPing-Ke Shih 	return true;
727e3ec7017SPing-Ke Shih }
728e3ec7017SPing-Ke Shih 
729e3ec7017SPing-Ke Shih static void
__rtw89_core_tx_adjust_he_qos_htc(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)730e3ec7017SPing-Ke Shih __rtw89_core_tx_adjust_he_qos_htc(struct rtw89_dev *rtwdev,
731e3ec7017SPing-Ke Shih 				  struct rtw89_core_tx_request *tx_req)
732e3ec7017SPing-Ke Shih {
733e3ec7017SPing-Ke Shih 	struct ieee80211_sta *sta = tx_req->sta;
734e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
735e3ec7017SPing-Ke Shih 	struct sk_buff *skb = tx_req->skb;
736e3ec7017SPing-Ke Shih 	struct ieee80211_hdr *hdr = (void *)skb->data;
737e3ec7017SPing-Ke Shih 	__le16 fc = hdr->frame_control;
738e3ec7017SPing-Ke Shih 	void *data;
739e3ec7017SPing-Ke Shih 	__le32 *htc;
740e3ec7017SPing-Ke Shih 	u8 *qc;
741e3ec7017SPing-Ke Shih 	int hdr_len;
742e3ec7017SPing-Ke Shih 
743e3ec7017SPing-Ke Shih 	hdr_len = ieee80211_has_a4(fc) ? 32 : 26;
744e3ec7017SPing-Ke Shih 	data = skb_push(skb, IEEE80211_HT_CTL_LEN);
745e3ec7017SPing-Ke Shih 	memmove(data, data + IEEE80211_HT_CTL_LEN, hdr_len);
746e3ec7017SPing-Ke Shih 
747e3ec7017SPing-Ke Shih 	hdr = data;
748e3ec7017SPing-Ke Shih 	htc = data + hdr_len;
749e3ec7017SPing-Ke Shih 	hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_ORDER);
750e3ec7017SPing-Ke Shih 	*htc = rtwsta->htc_template ? rtwsta->htc_template :
751e3ec7017SPing-Ke Shih 	       le32_encode_bits(RTW89_HTC_VARIANT_HE, RTW89_HTC_MASK_VARIANT) |
752e3ec7017SPing-Ke Shih 	       le32_encode_bits(RTW89_HTC_VARIANT_HE_CID_CAS, RTW89_HTC_MASK_CTL_ID);
753e3ec7017SPing-Ke Shih 
754e3ec7017SPing-Ke Shih 	qc = data + hdr_len - IEEE80211_QOS_CTL_LEN;
755e3ec7017SPing-Ke Shih 	qc[0] |= IEEE80211_QOS_CTL_EOSP;
756e3ec7017SPing-Ke Shih }
757e3ec7017SPing-Ke Shih 
758e3ec7017SPing-Ke Shih static void
rtw89_core_tx_update_he_qos_htc(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req,enum btc_pkt_type pkt_type)759e3ec7017SPing-Ke Shih rtw89_core_tx_update_he_qos_htc(struct rtw89_dev *rtwdev,
760e3ec7017SPing-Ke Shih 				struct rtw89_core_tx_request *tx_req,
761e3ec7017SPing-Ke Shih 				enum btc_pkt_type pkt_type)
762e3ec7017SPing-Ke Shih {
763e3ec7017SPing-Ke Shih 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
764e3ec7017SPing-Ke Shih 	struct ieee80211_vif *vif = tx_req->vif;
765e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
766e3ec7017SPing-Ke Shih 
767e3ec7017SPing-Ke Shih 	if (!__rtw89_core_tx_check_he_qos_htc(rtwdev, tx_req, pkt_type))
768e3ec7017SPing-Ke Shih 		goto desc_bk;
769e3ec7017SPing-Ke Shih 
770e3ec7017SPing-Ke Shih 	__rtw89_core_tx_adjust_he_qos_htc(rtwdev, tx_req);
771e3ec7017SPing-Ke Shih 
772e3ec7017SPing-Ke Shih 	desc_info->pkt_size += IEEE80211_HT_CTL_LEN;
773e3ec7017SPing-Ke Shih 	desc_info->a_ctrl_bsr = true;
774e3ec7017SPing-Ke Shih 
775e3ec7017SPing-Ke Shih desc_bk:
776e3ec7017SPing-Ke Shih 	if (!rtwvif || rtwvif->last_a_ctrl == desc_info->a_ctrl_bsr)
777e3ec7017SPing-Ke Shih 		return;
778e3ec7017SPing-Ke Shih 
779e3ec7017SPing-Ke Shih 	rtwvif->last_a_ctrl = desc_info->a_ctrl_bsr;
780e3ec7017SPing-Ke Shih 	desc_info->bk = true;
781e3ec7017SPing-Ke Shih }
782e3ec7017SPing-Ke Shih 
rtw89_core_get_data_rate(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)783e5307c9cSChih-Kang Chang static u16 rtw89_core_get_data_rate(struct rtw89_dev *rtwdev,
784e5307c9cSChih-Kang Chang 				    struct rtw89_core_tx_request *tx_req)
785e5307c9cSChih-Kang Chang {
786e5307c9cSChih-Kang Chang 	struct ieee80211_vif *vif = tx_req->vif;
787e5307c9cSChih-Kang Chang 	struct ieee80211_sta *sta = tx_req->sta;
788e5307c9cSChih-Kang Chang 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
789e5307c9cSChih-Kang Chang 	struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern;
790583e998eSZong-Zhe Yang 	enum rtw89_chanctx_idx idx = rtwvif->chanctx_idx;
791e5307c9cSChih-Kang Chang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, idx);
792e5307c9cSChih-Kang Chang 	u16 lowest_rate;
793e5307c9cSChih-Kang Chang 
794e5307c9cSChih-Kang Chang 	if (rate_pattern->enable)
795e5307c9cSChih-Kang Chang 		return rate_pattern->rate;
796e5307c9cSChih-Kang Chang 
797e5307c9cSChih-Kang Chang 	if (vif->p2p)
798e5307c9cSChih-Kang Chang 		lowest_rate = RTW89_HW_RATE_OFDM6;
799e5307c9cSChih-Kang Chang 	else if (chan->band_type == RTW89_BAND_2G)
800e5307c9cSChih-Kang Chang 		lowest_rate = RTW89_HW_RATE_CCK1;
801e5307c9cSChih-Kang Chang 	else
802e5307c9cSChih-Kang Chang 		lowest_rate = RTW89_HW_RATE_OFDM6;
803e5307c9cSChih-Kang Chang 
8048551844dSPing-Ke Shih 	if (!sta || !sta->deflink.supp_rates[chan->band_type])
805e5307c9cSChih-Kang Chang 		return lowest_rate;
806e5307c9cSChih-Kang Chang 
807e5307c9cSChih-Kang Chang 	return __ffs(sta->deflink.supp_rates[chan->band_type]) + lowest_rate;
808e5307c9cSChih-Kang Chang }
809e5307c9cSChih-Kang Chang 
810e3ec7017SPing-Ke Shih static void
rtw89_core_tx_update_data_info(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)811e3ec7017SPing-Ke Shih rtw89_core_tx_update_data_info(struct rtw89_dev *rtwdev,
812e3ec7017SPing-Ke Shih 			       struct rtw89_core_tx_request *tx_req)
813e3ec7017SPing-Ke Shih {
814e3ec7017SPing-Ke Shih 	struct ieee80211_vif *vif = tx_req->vif;
81525ed1a17SPing-Ke Shih 	struct ieee80211_sta *sta = tx_req->sta;
816e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
81725ed1a17SPing-Ke Shih 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
818e3ec7017SPing-Ke Shih 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
819e3ec7017SPing-Ke Shih 	struct sk_buff *skb = tx_req->skb;
820e3ec7017SPing-Ke Shih 	u8 tid, tid_indicate;
821e3ec7017SPing-Ke Shih 	u8 qsel, ch_dma;
822e3ec7017SPing-Ke Shih 
823e3ec7017SPing-Ke Shih 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
824e3ec7017SPing-Ke Shih 	tid_indicate = rtw89_core_get_tid_indicate(rtwdev, tid);
82511d261f2SPing-Ke Shih 	qsel = desc_info->hiq ? RTW89_TX_QSEL_B0_HI : rtw89_core_get_qsel(rtwdev, tid);
826e3ec7017SPing-Ke Shih 	ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
827e3ec7017SPing-Ke Shih 
828e3ec7017SPing-Ke Shih 	desc_info->ch_dma = ch_dma;
829e3ec7017SPing-Ke Shih 	desc_info->tid_indicate = tid_indicate;
830e3ec7017SPing-Ke Shih 	desc_info->qsel = qsel;
8319eecaec2SPing-Ke Shih 	desc_info->mac_id = rtw89_core_tx_get_mac_id(rtwdev, tx_req);
8329eecaec2SPing-Ke Shih 	desc_info->port = desc_info->hiq ? rtwvif->port : 0;
83325ed1a17SPing-Ke Shih 	desc_info->er_cap = rtwsta ? rtwsta->er_cap : false;
834f12fc206SPing-Ke Shih 	desc_info->stbc = rtwsta ? rtwsta->ra.stbc_cap : false;
835f12fc206SPing-Ke Shih 	desc_info->ldpc = rtwsta ? rtwsta->ra.ldpc_cap : false;
836e3ec7017SPing-Ke Shih 
837e3ec7017SPing-Ke Shih 	/* enable wd_info for AMPDU */
838e3ec7017SPing-Ke Shih 	desc_info->en_wd_info = true;
839e3ec7017SPing-Ke Shih 
840e3ec7017SPing-Ke Shih 	if (IEEE80211_SKB_CB(skb)->control.hw_key)
841e3ec7017SPing-Ke Shih 		rtw89_core_tx_update_sec_key(rtwdev, tx_req);
842e3ec7017SPing-Ke Shih 
843e5307c9cSChih-Kang Chang 	desc_info->data_retry_lowest_rate = rtw89_core_get_data_rate(rtwdev, tx_req);
844e3ec7017SPing-Ke Shih }
845e3ec7017SPing-Ke Shih 
846e3ec7017SPing-Ke Shih static enum btc_pkt_type
rtw89_core_tx_btc_spec_pkt_notify(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)847e3ec7017SPing-Ke Shih rtw89_core_tx_btc_spec_pkt_notify(struct rtw89_dev *rtwdev,
848e3ec7017SPing-Ke Shih 				  struct rtw89_core_tx_request *tx_req)
849e3ec7017SPing-Ke Shih {
850e3ec7017SPing-Ke Shih 	struct sk_buff *skb = tx_req->skb;
851e3ec7017SPing-Ke Shih 	struct udphdr *udphdr;
852e3ec7017SPing-Ke Shih 
853e3ec7017SPing-Ke Shih 	if (IEEE80211_SKB_CB(skb)->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) {
854e3ec7017SPing-Ke Shih 		ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.eapol_notify_work);
855e3ec7017SPing-Ke Shih 		return PACKET_EAPOL;
856e3ec7017SPing-Ke Shih 	}
857e3ec7017SPing-Ke Shih 
858e3ec7017SPing-Ke Shih 	if (skb->protocol == htons(ETH_P_ARP)) {
859e3ec7017SPing-Ke Shih 		ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.arp_notify_work);
860e3ec7017SPing-Ke Shih 		return PACKET_ARP;
861e3ec7017SPing-Ke Shih 	}
862e3ec7017SPing-Ke Shih 
863e3ec7017SPing-Ke Shih 	if (skb->protocol == htons(ETH_P_IP) &&
864e3ec7017SPing-Ke Shih 	    ip_hdr(skb)->protocol == IPPROTO_UDP) {
865e3ec7017SPing-Ke Shih 		udphdr = udp_hdr(skb);
866e3ec7017SPing-Ke Shih 		if (((udphdr->source == htons(67) && udphdr->dest == htons(68)) ||
867e3ec7017SPing-Ke Shih 		     (udphdr->source == htons(68) && udphdr->dest == htons(67))) &&
868e3ec7017SPing-Ke Shih 		    skb->len > 282) {
869e3ec7017SPing-Ke Shih 			ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.dhcp_notify_work);
870e3ec7017SPing-Ke Shih 			return PACKET_DHCP;
871e3ec7017SPing-Ke Shih 		}
872e3ec7017SPing-Ke Shih 	}
873e3ec7017SPing-Ke Shih 
874e3ec7017SPing-Ke Shih 	if (skb->protocol == htons(ETH_P_IP) &&
875e3ec7017SPing-Ke Shih 	    ip_hdr(skb)->protocol == IPPROTO_ICMP) {
876e3ec7017SPing-Ke Shih 		ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.icmp_notify_work);
877e3ec7017SPing-Ke Shih 		return PACKET_ICMP;
878e3ec7017SPing-Ke Shih 	}
879e3ec7017SPing-Ke Shih 
880e3ec7017SPing-Ke Shih 	return PACKET_MAX;
881e3ec7017SPing-Ke Shih }
882e3ec7017SPing-Ke Shih 
883e3ec7017SPing-Ke Shih static void
rtw89_core_tx_wake(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)8847bfd05ffSChin-Yen Lee rtw89_core_tx_wake(struct rtw89_dev *rtwdev,
8857bfd05ffSChin-Yen Lee 		   struct rtw89_core_tx_request *tx_req)
8867bfd05ffSChin-Yen Lee {
8875abbb68aSChin-Yen Lee 	const struct rtw89_chip_info *chip = rtwdev->chip;
8885abbb68aSChin-Yen Lee 
88911fe4ccdSZong-Zhe Yang 	if (!RTW89_CHK_FW_FEATURE(TX_WAKE, &rtwdev->fw))
8907bfd05ffSChin-Yen Lee 		return;
8917bfd05ffSChin-Yen Lee 
8927bfd05ffSChin-Yen Lee 	if (!test_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags))
8937bfd05ffSChin-Yen Lee 		return;
8947bfd05ffSChin-Yen Lee 
8955abbb68aSChin-Yen Lee 	if (chip->chip_id != RTL8852C &&
8965abbb68aSChin-Yen Lee 	    tx_req->tx_type != RTW89_CORE_TX_TYPE_MGMT)
8977bfd05ffSChin-Yen Lee 		return;
8987bfd05ffSChin-Yen Lee 
8997bfd05ffSChin-Yen Lee 	rtw89_mac_notify_wake(rtwdev);
9007bfd05ffSChin-Yen Lee }
9017bfd05ffSChin-Yen Lee 
9027bfd05ffSChin-Yen Lee static void
rtw89_core_tx_update_desc_info(struct rtw89_dev * rtwdev,struct rtw89_core_tx_request * tx_req)903e3ec7017SPing-Ke Shih rtw89_core_tx_update_desc_info(struct rtw89_dev *rtwdev,
904e3ec7017SPing-Ke Shih 			       struct rtw89_core_tx_request *tx_req)
905e3ec7017SPing-Ke Shih {
906e3ec7017SPing-Ke Shih 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
907e3ec7017SPing-Ke Shih 	struct sk_buff *skb = tx_req->skb;
90811d261f2SPing-Ke Shih 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
909e3ec7017SPing-Ke Shih 	struct ieee80211_hdr *hdr = (void *)skb->data;
910e3ec7017SPing-Ke Shih 	enum rtw89_core_tx_type tx_type;
911e3ec7017SPing-Ke Shih 	enum btc_pkt_type pkt_type;
912e3ec7017SPing-Ke Shih 	bool is_bmc;
913e3ec7017SPing-Ke Shih 	u16 seq;
914e3ec7017SPing-Ke Shih 
915e3ec7017SPing-Ke Shih 	seq = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
916e3ec7017SPing-Ke Shih 	if (tx_req->tx_type != RTW89_CORE_TX_TYPE_FWCMD) {
917e3ec7017SPing-Ke Shih 		tx_type = rtw89_core_get_tx_type(rtwdev, skb);
918e3ec7017SPing-Ke Shih 		tx_req->tx_type = tx_type;
919e3ec7017SPing-Ke Shih 	}
920e3ec7017SPing-Ke Shih 	is_bmc = (is_broadcast_ether_addr(hdr->addr1) ||
921e3ec7017SPing-Ke Shih 		  is_multicast_ether_addr(hdr->addr1));
922e3ec7017SPing-Ke Shih 
923e3ec7017SPing-Ke Shih 	desc_info->seq = seq;
924e3ec7017SPing-Ke Shih 	desc_info->pkt_size = skb->len;
925e3ec7017SPing-Ke Shih 	desc_info->is_bmc = is_bmc;
926e3ec7017SPing-Ke Shih 	desc_info->wd_page = true;
92711d261f2SPing-Ke Shih 	desc_info->hiq = info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM;
928e3ec7017SPing-Ke Shih 
929e3ec7017SPing-Ke Shih 	switch (tx_req->tx_type) {
930e3ec7017SPing-Ke Shih 	case RTW89_CORE_TX_TYPE_MGMT:
931e3ec7017SPing-Ke Shih 		rtw89_core_tx_update_mgmt_info(rtwdev, tx_req);
932e3ec7017SPing-Ke Shih 		break;
933e3ec7017SPing-Ke Shih 	case RTW89_CORE_TX_TYPE_DATA:
934e3ec7017SPing-Ke Shih 		rtw89_core_tx_update_data_info(rtwdev, tx_req);
935e3ec7017SPing-Ke Shih 		pkt_type = rtw89_core_tx_btc_spec_pkt_notify(rtwdev, tx_req);
936e3ec7017SPing-Ke Shih 		rtw89_core_tx_update_he_qos_htc(rtwdev, tx_req, pkt_type);
937679955d5SKuan-Chung Chen 		rtw89_core_tx_update_ampdu_info(rtwdev, tx_req, pkt_type);
93879a6c9a4SPing-Ke Shih 		rtw89_core_tx_update_llc_hdr(rtwdev, desc_info, skb);
939e3ec7017SPing-Ke Shih 		break;
940e3ec7017SPing-Ke Shih 	case RTW89_CORE_TX_TYPE_FWCMD:
941e3ec7017SPing-Ke Shih 		rtw89_core_tx_update_h2c_info(rtwdev, tx_req);
942e3ec7017SPing-Ke Shih 		break;
943e3ec7017SPing-Ke Shih 	}
944e3ec7017SPing-Ke Shih }
945e3ec7017SPing-Ke Shih 
rtw89_core_tx_kick_off(struct rtw89_dev * rtwdev,u8 qsel)946e3ec7017SPing-Ke Shih void rtw89_core_tx_kick_off(struct rtw89_dev *rtwdev, u8 qsel)
947e3ec7017SPing-Ke Shih {
948e3ec7017SPing-Ke Shih 	u8 ch_dma;
949e3ec7017SPing-Ke Shih 
950e3ec7017SPing-Ke Shih 	ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
951e3ec7017SPing-Ke Shih 
952e3ec7017SPing-Ke Shih 	rtw89_hci_tx_kick_off(rtwdev, ch_dma);
953e3ec7017SPing-Ke Shih }
954e3ec7017SPing-Ke Shih 
rtw89_core_tx_kick_off_and_wait(struct rtw89_dev * rtwdev,struct sk_buff * skb,int qsel,unsigned int timeout)9551ae5ca61SPo-Hao Huang int rtw89_core_tx_kick_off_and_wait(struct rtw89_dev *rtwdev, struct sk_buff *skb,
9561ae5ca61SPo-Hao Huang 				    int qsel, unsigned int timeout)
9571ae5ca61SPo-Hao Huang {
9581ae5ca61SPo-Hao Huang 	struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
9591ae5ca61SPo-Hao Huang 	struct rtw89_tx_wait_info *wait;
9601ae5ca61SPo-Hao Huang 	unsigned long time_left;
9611ae5ca61SPo-Hao Huang 	int ret = 0;
9621ae5ca61SPo-Hao Huang 
9631ae5ca61SPo-Hao Huang 	wait = kzalloc(sizeof(*wait), GFP_KERNEL);
9641ae5ca61SPo-Hao Huang 	if (!wait) {
9651ae5ca61SPo-Hao Huang 		rtw89_core_tx_kick_off(rtwdev, qsel);
9661ae5ca61SPo-Hao Huang 		return 0;
9671ae5ca61SPo-Hao Huang 	}
9681ae5ca61SPo-Hao Huang 
9691ae5ca61SPo-Hao Huang 	init_completion(&wait->completion);
9701ae5ca61SPo-Hao Huang 	rcu_assign_pointer(skb_data->wait, wait);
9711ae5ca61SPo-Hao Huang 
9721ae5ca61SPo-Hao Huang 	rtw89_core_tx_kick_off(rtwdev, qsel);
9731ae5ca61SPo-Hao Huang 	time_left = wait_for_completion_timeout(&wait->completion,
9741ae5ca61SPo-Hao Huang 						msecs_to_jiffies(timeout));
9751ae5ca61SPo-Hao Huang 	if (time_left == 0)
9761ae5ca61SPo-Hao Huang 		ret = -ETIMEDOUT;
9771ae5ca61SPo-Hao Huang 	else if (!wait->tx_done)
9781ae5ca61SPo-Hao Huang 		ret = -EAGAIN;
9791ae5ca61SPo-Hao Huang 
9801ae5ca61SPo-Hao Huang 	rcu_assign_pointer(skb_data->wait, NULL);
9811ae5ca61SPo-Hao Huang 	kfree_rcu(wait, rcu_head);
9821ae5ca61SPo-Hao Huang 
9831ae5ca61SPo-Hao Huang 	return ret;
9841ae5ca61SPo-Hao Huang }
9851ae5ca61SPo-Hao Huang 
rtw89_h2c_tx(struct rtw89_dev * rtwdev,struct sk_buff * skb,bool fwdl)986e3ec7017SPing-Ke Shih int rtw89_h2c_tx(struct rtw89_dev *rtwdev,
987e3ec7017SPing-Ke Shih 		 struct sk_buff *skb, bool fwdl)
988e3ec7017SPing-Ke Shih {
989e3ec7017SPing-Ke Shih 	struct rtw89_core_tx_request tx_req = {0};
990e3ec7017SPing-Ke Shih 	u32 cnt;
991e3ec7017SPing-Ke Shih 	int ret;
992e3ec7017SPing-Ke Shih 
993fc5f311fSPing-Ke Shih 	if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) {
994fc5f311fSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_FW,
995fc5f311fSPing-Ke Shih 			    "ignore h2c due to power is off with firmware state=%d\n",
996fc5f311fSPing-Ke Shih 			    test_bit(RTW89_FLAG_FW_RDY, rtwdev->flags));
997eae672f3SPo-Hao Huang 		dev_kfree_skb(skb);
998fc5f311fSPing-Ke Shih 		return 0;
999fc5f311fSPing-Ke Shih 	}
1000fc5f311fSPing-Ke Shih 
1001e3ec7017SPing-Ke Shih 	tx_req.skb = skb;
1002e3ec7017SPing-Ke Shih 	tx_req.tx_type = RTW89_CORE_TX_TYPE_FWCMD;
1003e3ec7017SPing-Ke Shih 	if (fwdl)
1004e3ec7017SPing-Ke Shih 		tx_req.desc_info.fw_dl = true;
1005e3ec7017SPing-Ke Shih 
1006e3ec7017SPing-Ke Shih 	rtw89_core_tx_update_desc_info(rtwdev, &tx_req);
1007e3ec7017SPing-Ke Shih 
1008e3ec7017SPing-Ke Shih 	if (!fwdl)
1009e3ec7017SPing-Ke Shih 		rtw89_hex_dump(rtwdev, RTW89_DBG_FW, "H2C: ", skb->data, skb->len);
1010e3ec7017SPing-Ke Shih 
1011e3ec7017SPing-Ke Shih 	cnt = rtw89_hci_check_and_reclaim_tx_resource(rtwdev, RTW89_TXCH_CH12);
1012e3ec7017SPing-Ke Shih 	if (cnt == 0) {
1013e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "no tx fwcmd resource\n");
1014e3ec7017SPing-Ke Shih 		return -ENOSPC;
1015e3ec7017SPing-Ke Shih 	}
1016e3ec7017SPing-Ke Shih 
1017e3ec7017SPing-Ke Shih 	ret = rtw89_hci_tx_write(rtwdev, &tx_req);
1018e3ec7017SPing-Ke Shih 	if (ret) {
1019e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to transmit skb to HCI\n");
1020e3ec7017SPing-Ke Shih 		return ret;
1021e3ec7017SPing-Ke Shih 	}
1022e3ec7017SPing-Ke Shih 	rtw89_hci_tx_kick_off(rtwdev, RTW89_TXCH_CH12);
1023e3ec7017SPing-Ke Shih 
1024e3ec7017SPing-Ke Shih 	return 0;
1025e3ec7017SPing-Ke Shih }
1026e3ec7017SPing-Ke Shih 
rtw89_core_tx_write(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct sk_buff * skb,int * qsel)1027e3ec7017SPing-Ke Shih int rtw89_core_tx_write(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif,
1028e3ec7017SPing-Ke Shih 			struct ieee80211_sta *sta, struct sk_buff *skb, int *qsel)
1029e3ec7017SPing-Ke Shih {
1030e3ec7017SPing-Ke Shih 	struct rtw89_core_tx_request tx_req = {0};
1031e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
1032e3ec7017SPing-Ke Shih 	int ret;
1033e3ec7017SPing-Ke Shih 
1034e3ec7017SPing-Ke Shih 	tx_req.skb = skb;
1035e3ec7017SPing-Ke Shih 	tx_req.sta = sta;
1036e3ec7017SPing-Ke Shih 	tx_req.vif = vif;
1037e3ec7017SPing-Ke Shih 
1038e3ec7017SPing-Ke Shih 	rtw89_traffic_stats_accu(rtwdev, &rtwdev->stats, skb, true);
1039e3ec7017SPing-Ke Shih 	rtw89_traffic_stats_accu(rtwdev, &rtwvif->stats, skb, true);
1040e3ec7017SPing-Ke Shih 	rtw89_core_tx_update_desc_info(rtwdev, &tx_req);
10417bfd05ffSChin-Yen Lee 	rtw89_core_tx_wake(rtwdev, &tx_req);
10427bfd05ffSChin-Yen Lee 
1043e3ec7017SPing-Ke Shih 	ret = rtw89_hci_tx_write(rtwdev, &tx_req);
1044e3ec7017SPing-Ke Shih 	if (ret) {
1045e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to transmit skb to HCI\n");
1046e3ec7017SPing-Ke Shih 		return ret;
1047e3ec7017SPing-Ke Shih 	}
1048e3ec7017SPing-Ke Shih 
1049e3ec7017SPing-Ke Shih 	if (qsel)
1050e3ec7017SPing-Ke Shih 		*qsel = tx_req.desc_info.qsel;
1051e3ec7017SPing-Ke Shih 
1052e3ec7017SPing-Ke Shih 	return 0;
1053e3ec7017SPing-Ke Shih }
1054e3ec7017SPing-Ke Shih 
rtw89_build_txwd_body0(struct rtw89_tx_desc_info * desc_info)1055e3ec7017SPing-Ke Shih static __le32 rtw89_build_txwd_body0(struct rtw89_tx_desc_info *desc_info)
1056e3ec7017SPing-Ke Shih {
1057e3ec7017SPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY0_WP_OFFSET, desc_info->wp_offset) |
1058e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_WD_INFO_EN, desc_info->en_wd_info) |
1059e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_CHANNEL_DMA, desc_info->ch_dma) |
1060e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_HDR_LLC_LEN, desc_info->hdr_llc_len) |
1061e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_WD_PAGE, desc_info->wd_page) |
106291644020SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_FW_DL, desc_info->fw_dl) |
106391644020SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_HW_SSN_SEL, desc_info->hw_ssn_sel) |
106491644020SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_HW_SSN_MODE, desc_info->hw_seq_mode);
1065e3ec7017SPing-Ke Shih 
1066e3ec7017SPing-Ke Shih 	return cpu_to_le32(dword);
1067e3ec7017SPing-Ke Shih }
1068e3ec7017SPing-Ke Shih 
rtw89_build_txwd_body0_v1(struct rtw89_tx_desc_info * desc_info)1069f59acddeSPing-Ke Shih static __le32 rtw89_build_txwd_body0_v1(struct rtw89_tx_desc_info *desc_info)
1070f59acddeSPing-Ke Shih {
1071f59acddeSPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY0_WP_OFFSET_V1, desc_info->wp_offset) |
1072f59acddeSPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_WD_INFO_EN, desc_info->en_wd_info) |
1073f59acddeSPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_CHANNEL_DMA, desc_info->ch_dma) |
1074f59acddeSPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_HDR_LLC_LEN, desc_info->hdr_llc_len) |
1075f59acddeSPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_WD_PAGE, desc_info->wd_page) |
1076f59acddeSPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY0_FW_DL, desc_info->fw_dl);
1077f59acddeSPing-Ke Shih 
1078f59acddeSPing-Ke Shih 	return cpu_to_le32(dword);
1079f59acddeSPing-Ke Shih }
1080f59acddeSPing-Ke Shih 
rtw89_build_txwd_body1_v1(struct rtw89_tx_desc_info * desc_info)1081f59acddeSPing-Ke Shih static __le32 rtw89_build_txwd_body1_v1(struct rtw89_tx_desc_info *desc_info)
1082f59acddeSPing-Ke Shih {
1083f59acddeSPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY1_ADDR_INFO_NUM, desc_info->addr_info_nr) |
108479a6c9a4SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY1_SEC_KEYID, desc_info->sec_keyid) |
1085f59acddeSPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY1_SEC_TYPE, desc_info->sec_type);
1086f59acddeSPing-Ke Shih 
1087f59acddeSPing-Ke Shih 	return cpu_to_le32(dword);
1088f59acddeSPing-Ke Shih }
1089f59acddeSPing-Ke Shih 
rtw89_build_txwd_body2(struct rtw89_tx_desc_info * desc_info)1090e3ec7017SPing-Ke Shih static __le32 rtw89_build_txwd_body2(struct rtw89_tx_desc_info *desc_info)
1091e3ec7017SPing-Ke Shih {
1092e3ec7017SPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY2_TID_INDICATE, desc_info->tid_indicate) |
1093e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY2_QSEL, desc_info->qsel) |
10949eecaec2SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY2_TXPKT_SIZE, desc_info->pkt_size) |
10959eecaec2SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY2_MACID, desc_info->mac_id);
1096e3ec7017SPing-Ke Shih 
1097e3ec7017SPing-Ke Shih 	return cpu_to_le32(dword);
1098e3ec7017SPing-Ke Shih }
1099e3ec7017SPing-Ke Shih 
rtw89_build_txwd_body3(struct rtw89_tx_desc_info * desc_info)1100e3ec7017SPing-Ke Shih static __le32 rtw89_build_txwd_body3(struct rtw89_tx_desc_info *desc_info)
1101e3ec7017SPing-Ke Shih {
1102e3ec7017SPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY3_SW_SEQ, desc_info->seq) |
1103e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY3_AGG_EN, desc_info->agg_en) |
1104e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY3_BK, desc_info->bk);
1105e3ec7017SPing-Ke Shih 
1106e3ec7017SPing-Ke Shih 	return cpu_to_le32(dword);
1107e3ec7017SPing-Ke Shih }
1108e3ec7017SPing-Ke Shih 
rtw89_build_txwd_body4(struct rtw89_tx_desc_info * desc_info)110979a6c9a4SPing-Ke Shih static __le32 rtw89_build_txwd_body4(struct rtw89_tx_desc_info *desc_info)
111079a6c9a4SPing-Ke Shih {
111179a6c9a4SPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY4_SEC_IV_L0, desc_info->sec_seq[0]) |
111279a6c9a4SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY4_SEC_IV_L1, desc_info->sec_seq[1]);
111379a6c9a4SPing-Ke Shih 
111479a6c9a4SPing-Ke Shih 	return cpu_to_le32(dword);
111579a6c9a4SPing-Ke Shih }
111679a6c9a4SPing-Ke Shih 
rtw89_build_txwd_body5(struct rtw89_tx_desc_info * desc_info)111779a6c9a4SPing-Ke Shih static __le32 rtw89_build_txwd_body5(struct rtw89_tx_desc_info *desc_info)
111879a6c9a4SPing-Ke Shih {
111979a6c9a4SPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H2, desc_info->sec_seq[2]) |
112079a6c9a4SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H3, desc_info->sec_seq[3]) |
112179a6c9a4SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H4, desc_info->sec_seq[4]) |
112279a6c9a4SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H5, desc_info->sec_seq[5]);
112379a6c9a4SPing-Ke Shih 
112479a6c9a4SPing-Ke Shih 	return cpu_to_le32(dword);
112579a6c9a4SPing-Ke Shih }
112679a6c9a4SPing-Ke Shih 
rtw89_build_txwd_body7_v1(struct rtw89_tx_desc_info * desc_info)1127f59acddeSPing-Ke Shih static __le32 rtw89_build_txwd_body7_v1(struct rtw89_tx_desc_info *desc_info)
1128f59acddeSPing-Ke Shih {
1129f59acddeSPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY7_USE_RATE_V1, desc_info->use_rate) |
1130f59acddeSPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_BODY7_DATA_RATE, desc_info->data_rate);
1131f59acddeSPing-Ke Shih 
1132f59acddeSPing-Ke Shih 	return cpu_to_le32(dword);
1133f59acddeSPing-Ke Shih }
1134f59acddeSPing-Ke Shih 
rtw89_build_txwd_info0(struct rtw89_tx_desc_info * desc_info)1135e3ec7017SPing-Ke Shih static __le32 rtw89_build_txwd_info0(struct rtw89_tx_desc_info *desc_info)
1136e3ec7017SPing-Ke Shih {
1137e3ec7017SPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO0_USE_RATE, desc_info->use_rate) |
1138e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_DATA_RATE, desc_info->data_rate) |
1139f12fc206SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_DATA_STBC, desc_info->stbc) |
1140f12fc206SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_DATA_LDPC, desc_info->ldpc) |
11419eecaec2SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_DISDATAFB, desc_info->dis_data_fb) |
11429eecaec2SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_MULTIPORT_ID, desc_info->port);
1143e3ec7017SPing-Ke Shih 
1144e3ec7017SPing-Ke Shih 	return cpu_to_le32(dword);
1145e3ec7017SPing-Ke Shih }
1146e3ec7017SPing-Ke Shih 
rtw89_build_txwd_info0_v1(struct rtw89_tx_desc_info * desc_info)1147f59acddeSPing-Ke Shih static __le32 rtw89_build_txwd_info0_v1(struct rtw89_tx_desc_info *desc_info)
1148f59acddeSPing-Ke Shih {
1149f12fc206SPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO0_DATA_STBC, desc_info->stbc) |
1150f12fc206SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_DATA_LDPC, desc_info->ldpc) |
1151f12fc206SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_DISDATAFB, desc_info->dis_data_fb) |
115225ed1a17SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_MULTIPORT_ID, desc_info->port) |
115325ed1a17SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_DATA_ER, desc_info->er_cap) |
115425ed1a17SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO0_DATA_BW_ER, 0);
1155f59acddeSPing-Ke Shih 
1156f59acddeSPing-Ke Shih 	return cpu_to_le32(dword);
1157f59acddeSPing-Ke Shih }
1158f59acddeSPing-Ke Shih 
rtw89_build_txwd_info1(struct rtw89_tx_desc_info * desc_info)1159e3ec7017SPing-Ke Shih static __le32 rtw89_build_txwd_info1(struct rtw89_tx_desc_info *desc_info)
1160e3ec7017SPing-Ke Shih {
1161e3ec7017SPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO1_MAX_AGGNUM, desc_info->ampdu_num) |
1162e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO1_A_CTRL_BSR, desc_info->a_ctrl_bsr) |
1163e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO1_DATA_RTY_LOWEST_RATE,
1164e3ec7017SPing-Ke Shih 			       desc_info->data_retry_lowest_rate);
1165e3ec7017SPing-Ke Shih 
1166e3ec7017SPing-Ke Shih 	return cpu_to_le32(dword);
1167e3ec7017SPing-Ke Shih }
1168e3ec7017SPing-Ke Shih 
rtw89_build_txwd_info2(struct rtw89_tx_desc_info * desc_info)1169e3ec7017SPing-Ke Shih static __le32 rtw89_build_txwd_info2(struct rtw89_tx_desc_info *desc_info)
1170e3ec7017SPing-Ke Shih {
1171e3ec7017SPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO2_AMPDU_DENSITY, desc_info->ampdu_density) |
1172e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO2_SEC_TYPE, desc_info->sec_type) |
1173e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO2_SEC_HW_ENC, desc_info->sec_en) |
1174e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO2_SEC_CAM_IDX, desc_info->sec_cam_idx);
1175e3ec7017SPing-Ke Shih 
1176e3ec7017SPing-Ke Shih 	return cpu_to_le32(dword);
1177e3ec7017SPing-Ke Shih }
1178e3ec7017SPing-Ke Shih 
rtw89_build_txwd_info2_v1(struct rtw89_tx_desc_info * desc_info)1179f59acddeSPing-Ke Shih static __le32 rtw89_build_txwd_info2_v1(struct rtw89_tx_desc_info *desc_info)
1180f59acddeSPing-Ke Shih {
1181f59acddeSPing-Ke Shih 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO2_AMPDU_DENSITY, desc_info->ampdu_density) |
1182f59acddeSPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO2_FORCE_KEY_EN, desc_info->sec_en) |
1183f59acddeSPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO2_SEC_CAM_IDX, desc_info->sec_cam_idx);
1184f59acddeSPing-Ke Shih 
1185f59acddeSPing-Ke Shih 	return cpu_to_le32(dword);
1186f59acddeSPing-Ke Shih }
1187f59acddeSPing-Ke Shih 
rtw89_build_txwd_info4(struct rtw89_tx_desc_info * desc_info)1188e3ec7017SPing-Ke Shih static __le32 rtw89_build_txwd_info4(struct rtw89_tx_desc_info *desc_info)
1189e3ec7017SPing-Ke Shih {
1190dab2b8c4SPo-Hao Huang 	bool rts_en = !desc_info->is_bmc;
1191dab2b8c4SPo-Hao Huang 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO4_RTS_EN, rts_en) |
1192e3ec7017SPing-Ke Shih 		    FIELD_PREP(RTW89_TXWD_INFO4_HW_RTS_EN, 1);
1193e3ec7017SPing-Ke Shih 
1194e3ec7017SPing-Ke Shih 	return cpu_to_le32(dword);
1195e3ec7017SPing-Ke Shih }
1196e3ec7017SPing-Ke Shih 
rtw89_core_fill_txdesc(struct rtw89_dev * rtwdev,struct rtw89_tx_desc_info * desc_info,void * txdesc)1197e3ec7017SPing-Ke Shih void rtw89_core_fill_txdesc(struct rtw89_dev *rtwdev,
1198e3ec7017SPing-Ke Shih 			    struct rtw89_tx_desc_info *desc_info,
1199e3ec7017SPing-Ke Shih 			    void *txdesc)
1200e3ec7017SPing-Ke Shih {
1201e3ec7017SPing-Ke Shih 	struct rtw89_txwd_body *txwd_body = (struct rtw89_txwd_body *)txdesc;
1202e3ec7017SPing-Ke Shih 	struct rtw89_txwd_info *txwd_info;
1203e3ec7017SPing-Ke Shih 
1204e3ec7017SPing-Ke Shih 	txwd_body->dword0 = rtw89_build_txwd_body0(desc_info);
1205e3ec7017SPing-Ke Shih 	txwd_body->dword2 = rtw89_build_txwd_body2(desc_info);
1206e3ec7017SPing-Ke Shih 	txwd_body->dword3 = rtw89_build_txwd_body3(desc_info);
1207e3ec7017SPing-Ke Shih 
1208e3ec7017SPing-Ke Shih 	if (!desc_info->en_wd_info)
1209e3ec7017SPing-Ke Shih 		return;
1210e3ec7017SPing-Ke Shih 
1211e3ec7017SPing-Ke Shih 	txwd_info = (struct rtw89_txwd_info *)(txwd_body + 1);
1212e3ec7017SPing-Ke Shih 	txwd_info->dword0 = rtw89_build_txwd_info0(desc_info);
1213e3ec7017SPing-Ke Shih 	txwd_info->dword1 = rtw89_build_txwd_info1(desc_info);
1214e3ec7017SPing-Ke Shih 	txwd_info->dword2 = rtw89_build_txwd_info2(desc_info);
1215e3ec7017SPing-Ke Shih 	txwd_info->dword4 = rtw89_build_txwd_info4(desc_info);
1216e3ec7017SPing-Ke Shih 
1217e3ec7017SPing-Ke Shih }
1218e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_fill_txdesc);
1219e3ec7017SPing-Ke Shih 
rtw89_core_fill_txdesc_v1(struct rtw89_dev * rtwdev,struct rtw89_tx_desc_info * desc_info,void * txdesc)1220f59acddeSPing-Ke Shih void rtw89_core_fill_txdesc_v1(struct rtw89_dev *rtwdev,
1221f59acddeSPing-Ke Shih 			       struct rtw89_tx_desc_info *desc_info,
1222f59acddeSPing-Ke Shih 			       void *txdesc)
1223f59acddeSPing-Ke Shih {
1224f59acddeSPing-Ke Shih 	struct rtw89_txwd_body_v1 *txwd_body = (struct rtw89_txwd_body_v1 *)txdesc;
1225f59acddeSPing-Ke Shih 	struct rtw89_txwd_info *txwd_info;
1226f59acddeSPing-Ke Shih 
1227f59acddeSPing-Ke Shih 	txwd_body->dword0 = rtw89_build_txwd_body0_v1(desc_info);
1228f59acddeSPing-Ke Shih 	txwd_body->dword1 = rtw89_build_txwd_body1_v1(desc_info);
1229f59acddeSPing-Ke Shih 	txwd_body->dword2 = rtw89_build_txwd_body2(desc_info);
1230f59acddeSPing-Ke Shih 	txwd_body->dword3 = rtw89_build_txwd_body3(desc_info);
123179a6c9a4SPing-Ke Shih 	if (desc_info->sec_en) {
123279a6c9a4SPing-Ke Shih 		txwd_body->dword4 = rtw89_build_txwd_body4(desc_info);
123379a6c9a4SPing-Ke Shih 		txwd_body->dword5 = rtw89_build_txwd_body5(desc_info);
123479a6c9a4SPing-Ke Shih 	}
1235f59acddeSPing-Ke Shih 	txwd_body->dword7 = rtw89_build_txwd_body7_v1(desc_info);
1236f59acddeSPing-Ke Shih 
1237f59acddeSPing-Ke Shih 	if (!desc_info->en_wd_info)
1238f59acddeSPing-Ke Shih 		return;
1239f59acddeSPing-Ke Shih 
1240f59acddeSPing-Ke Shih 	txwd_info = (struct rtw89_txwd_info *)(txwd_body + 1);
1241f59acddeSPing-Ke Shih 	txwd_info->dword0 = rtw89_build_txwd_info0_v1(desc_info);
1242f59acddeSPing-Ke Shih 	txwd_info->dword1 = rtw89_build_txwd_info1(desc_info);
1243f59acddeSPing-Ke Shih 	txwd_info->dword2 = rtw89_build_txwd_info2_v1(desc_info);
1244f59acddeSPing-Ke Shih 	txwd_info->dword4 = rtw89_build_txwd_info4(desc_info);
1245f59acddeSPing-Ke Shih }
1246f59acddeSPing-Ke Shih EXPORT_SYMBOL(rtw89_core_fill_txdesc_v1);
1247f59acddeSPing-Ke Shih 
rtw89_build_txwd_body0_v2(struct rtw89_tx_desc_info * desc_info)1248d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_body0_v2(struct rtw89_tx_desc_info *desc_info)
1249d542ee74SPing-Ke Shih {
1250d542ee74SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_BODY0_WP_OFFSET_V1, desc_info->wp_offset) |
1251d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY0_WDINFO_EN, desc_info->en_wd_info) |
1252d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY0_CH_DMA, desc_info->ch_dma) |
1253d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY0_HDR_LLC_LEN, desc_info->hdr_llc_len) |
1254d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY0_WD_PAGE, desc_info->wd_page);
1255d542ee74SPing-Ke Shih 
1256d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1257d542ee74SPing-Ke Shih }
1258d542ee74SPing-Ke Shih 
rtw89_build_txwd_body1_v2(struct rtw89_tx_desc_info * desc_info)1259d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_body1_v2(struct rtw89_tx_desc_info *desc_info)
1260d542ee74SPing-Ke Shih {
1261d542ee74SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_BODY1_ADDR_INFO_NUM, desc_info->addr_info_nr) |
1262d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY1_SEC_KEYID, desc_info->sec_keyid) |
1263d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY1_SEC_TYPE, desc_info->sec_type);
1264d542ee74SPing-Ke Shih 
1265d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1266d542ee74SPing-Ke Shih }
1267d542ee74SPing-Ke Shih 
rtw89_build_txwd_body2_v2(struct rtw89_tx_desc_info * desc_info)1268d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_body2_v2(struct rtw89_tx_desc_info *desc_info)
1269d542ee74SPing-Ke Shih {
1270d542ee74SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_BODY2_TID_IND, desc_info->tid_indicate) |
1271d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY2_QSEL, desc_info->qsel) |
1272d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY2_TXPKTSIZE, desc_info->pkt_size) |
1273d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY2_AGG_EN, desc_info->agg_en) |
1274d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY2_BK, desc_info->bk) |
1275d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY2_MACID, desc_info->mac_id);
1276d542ee74SPing-Ke Shih 
1277d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1278d542ee74SPing-Ke Shih }
1279d542ee74SPing-Ke Shih 
rtw89_build_txwd_body3_v2(struct rtw89_tx_desc_info * desc_info)1280d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_body3_v2(struct rtw89_tx_desc_info *desc_info)
1281d542ee74SPing-Ke Shih {
1282d542ee74SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_BODY3_WIFI_SEQ, desc_info->seq);
1283d542ee74SPing-Ke Shih 
1284d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1285d542ee74SPing-Ke Shih }
1286d542ee74SPing-Ke Shih 
rtw89_build_txwd_body4_v2(struct rtw89_tx_desc_info * desc_info)1287d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_body4_v2(struct rtw89_tx_desc_info *desc_info)
1288d542ee74SPing-Ke Shih {
1289d542ee74SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_BODY4_SEC_IV_L0, desc_info->sec_seq[0]) |
1290d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY4_SEC_IV_L1, desc_info->sec_seq[1]);
1291d542ee74SPing-Ke Shih 
1292d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1293d542ee74SPing-Ke Shih }
1294d542ee74SPing-Ke Shih 
rtw89_build_txwd_body5_v2(struct rtw89_tx_desc_info * desc_info)1295d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_body5_v2(struct rtw89_tx_desc_info *desc_info)
1296d542ee74SPing-Ke Shih {
1297d542ee74SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_BODY5_SEC_IV_H2, desc_info->sec_seq[2]) |
1298d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY5_SEC_IV_H3, desc_info->sec_seq[3]) |
1299d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY5_SEC_IV_H4, desc_info->sec_seq[4]) |
1300d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY5_SEC_IV_H5, desc_info->sec_seq[5]);
1301d542ee74SPing-Ke Shih 
1302d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1303d542ee74SPing-Ke Shih }
1304d542ee74SPing-Ke Shih 
rtw89_build_txwd_body7_v2(struct rtw89_tx_desc_info * desc_info)1305d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_body7_v2(struct rtw89_tx_desc_info *desc_info)
1306d542ee74SPing-Ke Shih {
1307d542ee74SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_BODY7_USERATE_SEL, desc_info->use_rate) |
1308d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY7_DATA_ER, desc_info->er_cap) |
1309d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY7_DATA_BW_ER, 0) |
1310d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_BODY7_DATARATE, desc_info->data_rate);
1311d542ee74SPing-Ke Shih 
1312d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1313d542ee74SPing-Ke Shih }
1314d542ee74SPing-Ke Shih 
rtw89_build_txwd_info0_v2(struct rtw89_tx_desc_info * desc_info)1315d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_info0_v2(struct rtw89_tx_desc_info *desc_info)
1316d542ee74SPing-Ke Shih {
1317f12fc206SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_INFO0_DATA_STBC, desc_info->stbc) |
1318f12fc206SPing-Ke Shih 		    FIELD_PREP(BE_TXD_INFO0_DATA_LDPC, desc_info->ldpc) |
1319f12fc206SPing-Ke Shih 		    FIELD_PREP(BE_TXD_INFO0_DISDATAFB, desc_info->dis_data_fb) |
1320d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_INFO0_MULTIPORT_ID, desc_info->port);
1321d542ee74SPing-Ke Shih 
1322d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1323d542ee74SPing-Ke Shih }
1324d542ee74SPing-Ke Shih 
rtw89_build_txwd_info1_v2(struct rtw89_tx_desc_info * desc_info)1325d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_info1_v2(struct rtw89_tx_desc_info *desc_info)
1326d542ee74SPing-Ke Shih {
1327d542ee74SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_INFO1_MAX_AGG_NUM, desc_info->ampdu_num) |
1328d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_INFO1_A_CTRL_BSR, desc_info->a_ctrl_bsr) |
1329d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_INFO1_DATA_RTY_LOWEST_RATE,
1330d542ee74SPing-Ke Shih 			       desc_info->data_retry_lowest_rate);
1331d542ee74SPing-Ke Shih 
1332d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1333d542ee74SPing-Ke Shih }
1334d542ee74SPing-Ke Shih 
rtw89_build_txwd_info2_v2(struct rtw89_tx_desc_info * desc_info)1335d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_info2_v2(struct rtw89_tx_desc_info *desc_info)
1336d542ee74SPing-Ke Shih {
1337d542ee74SPing-Ke Shih 	u32 dword = FIELD_PREP(BE_TXD_INFO2_AMPDU_DENSITY, desc_info->ampdu_density) |
1338d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_INFO2_FORCE_KEY_EN, desc_info->sec_en) |
1339d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_INFO2_SEC_CAM_IDX, desc_info->sec_cam_idx);
1340d542ee74SPing-Ke Shih 
1341d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1342d542ee74SPing-Ke Shih }
1343d542ee74SPing-Ke Shih 
rtw89_build_txwd_info4_v2(struct rtw89_tx_desc_info * desc_info)1344d542ee74SPing-Ke Shih static __le32 rtw89_build_txwd_info4_v2(struct rtw89_tx_desc_info *desc_info)
1345d542ee74SPing-Ke Shih {
1346dab2b8c4SPo-Hao Huang 	bool rts_en = !desc_info->is_bmc;
1347dab2b8c4SPo-Hao Huang 	u32 dword = FIELD_PREP(BE_TXD_INFO4_RTS_EN, rts_en) |
1348d542ee74SPing-Ke Shih 		    FIELD_PREP(BE_TXD_INFO4_HW_RTS_EN, 1);
1349d542ee74SPing-Ke Shih 
1350d542ee74SPing-Ke Shih 	return cpu_to_le32(dword);
1351d542ee74SPing-Ke Shih }
1352d542ee74SPing-Ke Shih 
rtw89_core_fill_txdesc_v2(struct rtw89_dev * rtwdev,struct rtw89_tx_desc_info * desc_info,void * txdesc)1353d542ee74SPing-Ke Shih void rtw89_core_fill_txdesc_v2(struct rtw89_dev *rtwdev,
1354d542ee74SPing-Ke Shih 			       struct rtw89_tx_desc_info *desc_info,
1355d542ee74SPing-Ke Shih 			       void *txdesc)
1356d542ee74SPing-Ke Shih {
1357d542ee74SPing-Ke Shih 	struct rtw89_txwd_body_v2 *txwd_body = txdesc;
1358d542ee74SPing-Ke Shih 	struct rtw89_txwd_info_v2 *txwd_info;
1359d542ee74SPing-Ke Shih 
1360d542ee74SPing-Ke Shih 	txwd_body->dword0 = rtw89_build_txwd_body0_v2(desc_info);
1361d542ee74SPing-Ke Shih 	txwd_body->dword1 = rtw89_build_txwd_body1_v2(desc_info);
1362d542ee74SPing-Ke Shih 	txwd_body->dword2 = rtw89_build_txwd_body2_v2(desc_info);
1363d542ee74SPing-Ke Shih 	txwd_body->dword3 = rtw89_build_txwd_body3_v2(desc_info);
1364d542ee74SPing-Ke Shih 	if (desc_info->sec_en) {
1365d542ee74SPing-Ke Shih 		txwd_body->dword4 = rtw89_build_txwd_body4_v2(desc_info);
1366d542ee74SPing-Ke Shih 		txwd_body->dword5 = rtw89_build_txwd_body5_v2(desc_info);
1367d542ee74SPing-Ke Shih 	}
1368d542ee74SPing-Ke Shih 	txwd_body->dword7 = rtw89_build_txwd_body7_v2(desc_info);
1369d542ee74SPing-Ke Shih 
1370d542ee74SPing-Ke Shih 	if (!desc_info->en_wd_info)
1371d542ee74SPing-Ke Shih 		return;
1372d542ee74SPing-Ke Shih 
1373d542ee74SPing-Ke Shih 	txwd_info = (struct rtw89_txwd_info_v2 *)(txwd_body + 1);
1374d542ee74SPing-Ke Shih 	txwd_info->dword0 = rtw89_build_txwd_info0_v2(desc_info);
1375d542ee74SPing-Ke Shih 	txwd_info->dword1 = rtw89_build_txwd_info1_v2(desc_info);
1376d542ee74SPing-Ke Shih 	txwd_info->dword2 = rtw89_build_txwd_info2_v2(desc_info);
1377d542ee74SPing-Ke Shih 	txwd_info->dword4 = rtw89_build_txwd_info4_v2(desc_info);
1378d542ee74SPing-Ke Shih }
1379d542ee74SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_fill_txdesc_v2);
1380d542ee74SPing-Ke Shih 
rtw89_build_txwd_fwcmd0_v1(struct rtw89_tx_desc_info * desc_info)1381a95bd62eSPing-Ke Shih static __le32 rtw89_build_txwd_fwcmd0_v1(struct rtw89_tx_desc_info *desc_info)
1382a95bd62eSPing-Ke Shih {
1383a95bd62eSPing-Ke Shih 	u32 dword = FIELD_PREP(AX_RXD_RPKT_LEN_MASK, desc_info->pkt_size) |
1384a95bd62eSPing-Ke Shih 		    FIELD_PREP(AX_RXD_RPKT_TYPE_MASK, desc_info->fw_dl ?
1385a95bd62eSPing-Ke Shih 						      RTW89_CORE_RX_TYPE_FWDL :
1386a95bd62eSPing-Ke Shih 						      RTW89_CORE_RX_TYPE_H2C);
1387a95bd62eSPing-Ke Shih 
1388a95bd62eSPing-Ke Shih 	return cpu_to_le32(dword);
1389a95bd62eSPing-Ke Shih }
1390a95bd62eSPing-Ke Shih 
rtw89_core_fill_txdesc_fwcmd_v1(struct rtw89_dev * rtwdev,struct rtw89_tx_desc_info * desc_info,void * txdesc)1391a95bd62eSPing-Ke Shih void rtw89_core_fill_txdesc_fwcmd_v1(struct rtw89_dev *rtwdev,
1392a95bd62eSPing-Ke Shih 				     struct rtw89_tx_desc_info *desc_info,
1393a95bd62eSPing-Ke Shih 				     void *txdesc)
1394a95bd62eSPing-Ke Shih {
1395a95bd62eSPing-Ke Shih 	struct rtw89_rxdesc_short *txwd_v1 = (struct rtw89_rxdesc_short *)txdesc;
1396a95bd62eSPing-Ke Shih 
1397a95bd62eSPing-Ke Shih 	txwd_v1->dword0 = rtw89_build_txwd_fwcmd0_v1(desc_info);
1398a95bd62eSPing-Ke Shih }
1399a95bd62eSPing-Ke Shih EXPORT_SYMBOL(rtw89_core_fill_txdesc_fwcmd_v1);
1400a95bd62eSPing-Ke Shih 
rtw89_build_txwd_fwcmd0_v2(struct rtw89_tx_desc_info * desc_info)14016f09ff0aSPing-Ke Shih static __le32 rtw89_build_txwd_fwcmd0_v2(struct rtw89_tx_desc_info *desc_info)
14026f09ff0aSPing-Ke Shih {
14036f09ff0aSPing-Ke Shih 	u32 dword = FIELD_PREP(BE_RXD_RPKT_LEN_MASK, desc_info->pkt_size) |
14046f09ff0aSPing-Ke Shih 		    FIELD_PREP(BE_RXD_RPKT_TYPE_MASK, desc_info->fw_dl ?
14056f09ff0aSPing-Ke Shih 						      RTW89_CORE_RX_TYPE_FWDL :
14066f09ff0aSPing-Ke Shih 						      RTW89_CORE_RX_TYPE_H2C);
14076f09ff0aSPing-Ke Shih 
14086f09ff0aSPing-Ke Shih 	return cpu_to_le32(dword);
14096f09ff0aSPing-Ke Shih }
14106f09ff0aSPing-Ke Shih 
rtw89_core_fill_txdesc_fwcmd_v2(struct rtw89_dev * rtwdev,struct rtw89_tx_desc_info * desc_info,void * txdesc)14116f09ff0aSPing-Ke Shih void rtw89_core_fill_txdesc_fwcmd_v2(struct rtw89_dev *rtwdev,
14126f09ff0aSPing-Ke Shih 				     struct rtw89_tx_desc_info *desc_info,
14136f09ff0aSPing-Ke Shih 				     void *txdesc)
14146f09ff0aSPing-Ke Shih {
14156f09ff0aSPing-Ke Shih 	struct rtw89_rxdesc_short_v2 *txwd_v2 = (struct rtw89_rxdesc_short_v2 *)txdesc;
14166f09ff0aSPing-Ke Shih 
14176f09ff0aSPing-Ke Shih 	txwd_v2->dword0 = rtw89_build_txwd_fwcmd0_v2(desc_info);
14186f09ff0aSPing-Ke Shih }
14196f09ff0aSPing-Ke Shih EXPORT_SYMBOL(rtw89_core_fill_txdesc_fwcmd_v2);
14206f09ff0aSPing-Ke Shih 
rtw89_core_rx_process_mac_ppdu(struct rtw89_dev * rtwdev,struct sk_buff * skb,struct rtw89_rx_phy_ppdu * phy_ppdu)1421e3ec7017SPing-Ke Shih static int rtw89_core_rx_process_mac_ppdu(struct rtw89_dev *rtwdev,
1422e3ec7017SPing-Ke Shih 					  struct sk_buff *skb,
1423e3ec7017SPing-Ke Shih 					  struct rtw89_rx_phy_ppdu *phy_ppdu)
1424e3ec7017SPing-Ke Shih {
142576d45f48SZong-Zhe Yang 	const struct rtw89_chip_info *chip = rtwdev->chip;
142688bdc3ffSPing-Ke Shih 	const struct rtw89_rxinfo *rxinfo = (const struct rtw89_rxinfo *)skb->data;
1427e343faceSPing-Ke Shih 	const struct rtw89_rxinfo_user *user;
1428e343faceSPing-Ke Shih 	enum rtw89_chip_gen chip_gen = rtwdev->chip->chip_gen;
1429e343faceSPing-Ke Shih 	int rx_cnt_size = RTW89_PPDU_MAC_RX_CNT_SIZE;
1430e3ec7017SPing-Ke Shih 	bool rx_cnt_valid = false;
1431e343faceSPing-Ke Shih 	bool invalid = false;
1432e3ec7017SPing-Ke Shih 	u8 plcp_size = 0;
1433e3ec7017SPing-Ke Shih 	u8 *phy_sts;
1434e343faceSPing-Ke Shih 	u8 usr_num;
1435e343faceSPing-Ke Shih 	int i;
1436e343faceSPing-Ke Shih 
1437e343faceSPing-Ke Shih 	if (chip_gen == RTW89_CHIP_BE) {
1438e343faceSPing-Ke Shih 		invalid = le32_get_bits(rxinfo->w0, RTW89_RXINFO_W0_INVALID_V1);
1439e343faceSPing-Ke Shih 		rx_cnt_size = RTW89_PPDU_MAC_RX_CNT_SIZE_V1;
1440e343faceSPing-Ke Shih 	}
1441e343faceSPing-Ke Shih 
1442e343faceSPing-Ke Shih 	if (invalid)
1443e343faceSPing-Ke Shih 		return -EINVAL;
1444e3ec7017SPing-Ke Shih 
144588bdc3ffSPing-Ke Shih 	rx_cnt_valid = le32_get_bits(rxinfo->w0, RTW89_RXINFO_W0_RX_CNT_VLD);
1446e343faceSPing-Ke Shih 	if (chip_gen == RTW89_CHIP_BE) {
1447e343faceSPing-Ke Shih 		plcp_size = le32_get_bits(rxinfo->w0, RTW89_RXINFO_W0_PLCP_LEN_V1) << 3;
1448e343faceSPing-Ke Shih 		usr_num = le32_get_bits(rxinfo->w0, RTW89_RXINFO_W0_USR_NUM_V1);
1449e343faceSPing-Ke Shih 	} else {
145088bdc3ffSPing-Ke Shih 		plcp_size = le32_get_bits(rxinfo->w1, RTW89_RXINFO_W1_PLCP_LEN) << 3;
145188bdc3ffSPing-Ke Shih 		usr_num = le32_get_bits(rxinfo->w0, RTW89_RXINFO_W0_USR_NUM);
1452e343faceSPing-Ke Shih 	}
145376d45f48SZong-Zhe Yang 	if (usr_num > chip->ppdu_max_usr) {
145476d45f48SZong-Zhe Yang 		rtw89_warn(rtwdev, "Invalid user number (%d) in mac info\n",
145576d45f48SZong-Zhe Yang 			   usr_num);
1456e3ec7017SPing-Ke Shih 		return -EINVAL;
1457e3ec7017SPing-Ke Shih 	}
1458e3ec7017SPing-Ke Shih 
1459bd4a3b10SKuan-Chung Chen 	for (i = 0; i < usr_num; i++) {
1460e343faceSPing-Ke Shih 		user = &rxinfo->user[i];
1461e343faceSPing-Ke Shih 		if (!le32_get_bits(user->w0, RTW89_RXINFO_USER_MAC_ID_VALID))
1462e343faceSPing-Ke Shih 			continue;
1463bd4a3b10SKuan-Chung Chen 		/* For WiFi 7 chips, RXWD.mac_id of PPDU status is not set
1464bd4a3b10SKuan-Chung Chen 		 * by hardware, so update mac_id by rxinfo_user[].mac_id.
1465bd4a3b10SKuan-Chung Chen 		 */
1466bd4a3b10SKuan-Chung Chen 		if (chip_gen == RTW89_CHIP_BE)
1467e343faceSPing-Ke Shih 			phy_ppdu->mac_id =
1468e343faceSPing-Ke Shih 				le32_get_bits(user->w0, RTW89_RXINFO_USER_MACID);
1469bd4a3b10SKuan-Chung Chen 		phy_ppdu->has_data =
1470bd4a3b10SKuan-Chung Chen 			le32_get_bits(user->w0, RTW89_RXINFO_USER_DATA);
1471bd4a3b10SKuan-Chung Chen 		phy_ppdu->has_bcn =
1472bd4a3b10SKuan-Chung Chen 			le32_get_bits(user->w0, RTW89_RXINFO_USER_BCN);
1473e343faceSPing-Ke Shih 		break;
1474e343faceSPing-Ke Shih 	}
1475e343faceSPing-Ke Shih 
1476e3ec7017SPing-Ke Shih 	phy_sts = skb->data + RTW89_PPDU_MAC_INFO_SIZE;
1477e3ec7017SPing-Ke Shih 	phy_sts += usr_num * RTW89_PPDU_MAC_INFO_USR_SIZE;
1478e3ec7017SPing-Ke Shih 	/* 8-byte alignment */
1479e3ec7017SPing-Ke Shih 	if (usr_num & BIT(0))
1480e3ec7017SPing-Ke Shih 		phy_sts += RTW89_PPDU_MAC_INFO_USR_SIZE;
1481e3ec7017SPing-Ke Shih 	if (rx_cnt_valid)
1482e343faceSPing-Ke Shih 		phy_sts += rx_cnt_size;
1483e3ec7017SPing-Ke Shih 	phy_sts += plcp_size;
1484e3ec7017SPing-Ke Shih 
1485e343faceSPing-Ke Shih 	if (phy_sts > skb->data + skb->len)
1486e343faceSPing-Ke Shih 		return -EINVAL;
1487e343faceSPing-Ke Shih 
1488e3ec7017SPing-Ke Shih 	phy_ppdu->buf = phy_sts;
1489e3ec7017SPing-Ke Shih 	phy_ppdu->len = skb->data + skb->len - phy_sts;
1490e3ec7017SPing-Ke Shih 
1491e3ec7017SPing-Ke Shih 	return 0;
1492e3ec7017SPing-Ke Shih }
1493e3ec7017SPing-Ke Shih 
rtw89_get_data_rate_nss(struct rtw89_dev * rtwdev,u16 data_rate)1494bd4a3b10SKuan-Chung Chen static u8 rtw89_get_data_rate_nss(struct rtw89_dev *rtwdev, u16 data_rate)
1495bd4a3b10SKuan-Chung Chen {
1496bd4a3b10SKuan-Chung Chen 	u8 data_rate_mode;
1497bd4a3b10SKuan-Chung Chen 
1498bd4a3b10SKuan-Chung Chen 	data_rate_mode = rtw89_get_data_rate_mode(rtwdev, data_rate);
1499bd4a3b10SKuan-Chung Chen 	switch (data_rate_mode) {
1500bd4a3b10SKuan-Chung Chen 	case DATA_RATE_MODE_NON_HT:
1501bd4a3b10SKuan-Chung Chen 		return 1;
1502bd4a3b10SKuan-Chung Chen 	case DATA_RATE_MODE_HT:
1503bd4a3b10SKuan-Chung Chen 		return rtw89_get_data_ht_nss(rtwdev, data_rate) + 1;
1504bd4a3b10SKuan-Chung Chen 	case DATA_RATE_MODE_VHT:
1505bd4a3b10SKuan-Chung Chen 	case DATA_RATE_MODE_HE:
1506bd4a3b10SKuan-Chung Chen 	case DATA_RATE_MODE_EHT:
1507bd4a3b10SKuan-Chung Chen 		return rtw89_get_data_nss(rtwdev, data_rate) + 1;
1508bd4a3b10SKuan-Chung Chen 	default:
1509bd4a3b10SKuan-Chung Chen 		rtw89_warn(rtwdev, "invalid RX rate mode %d\n", data_rate_mode);
1510bd4a3b10SKuan-Chung Chen 		return 0;
1511bd4a3b10SKuan-Chung Chen 	}
1512bd4a3b10SKuan-Chung Chen }
1513bd4a3b10SKuan-Chung Chen 
rtw89_core_rx_process_phy_ppdu_iter(void * data,struct ieee80211_sta * sta)1514e3ec7017SPing-Ke Shih static void rtw89_core_rx_process_phy_ppdu_iter(void *data,
1515e3ec7017SPing-Ke Shih 						struct ieee80211_sta *sta)
1516e3ec7017SPing-Ke Shih {
1517e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
1518e3ec7017SPing-Ke Shih 	struct rtw89_rx_phy_ppdu *phy_ppdu = (struct rtw89_rx_phy_ppdu *)data;
15196ce472d6SPing-Ke Shih 	struct rtw89_dev *rtwdev = rtwsta->rtwdev;
1520f6b24241SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
1521f6b24241SPing-Ke Shih 	u8 ant_num = hal->ant_diversity ? 2 : rtwdev->chip->rf_path_num;
1522f6b24241SPing-Ke Shih 	u8 ant_pos = U8_MAX;
15234bb223a1SPing-Ke Shih 	u8 evm_pos = 0;
15246ce472d6SPing-Ke Shih 	int i;
1525e3ec7017SPing-Ke Shih 
1526f6b24241SPing-Ke Shih 	if (rtwsta->mac_id != phy_ppdu->mac_id || !phy_ppdu->to_self)
1527f6b24241SPing-Ke Shih 		return;
1528f6b24241SPing-Ke Shih 
15294bb223a1SPing-Ke Shih 	if (hal->ant_diversity && hal->antenna_rx) {
1530f6b24241SPing-Ke Shih 		ant_pos = __ffs(hal->antenna_rx);
15314bb223a1SPing-Ke Shih 		evm_pos = ant_pos;
15324bb223a1SPing-Ke Shih 	}
1533f6b24241SPing-Ke Shih 
1534e3ec7017SPing-Ke Shih 	ewma_rssi_add(&rtwsta->avg_rssi, phy_ppdu->rssi_avg);
1535f6b24241SPing-Ke Shih 
1536f6b24241SPing-Ke Shih 	if (ant_pos < ant_num) {
1537f6b24241SPing-Ke Shih 		ewma_rssi_add(&rtwsta->rssi[ant_pos], phy_ppdu->rssi[0]);
1538f6b24241SPing-Ke Shih 	} else {
15396ce472d6SPing-Ke Shih 		for (i = 0; i < rtwdev->chip->rf_path_num; i++)
15406ce472d6SPing-Ke Shih 			ewma_rssi_add(&rtwsta->rssi[i], phy_ppdu->rssi[i]);
15416ce472d6SPing-Ke Shih 	}
15424bb223a1SPing-Ke Shih 
1543bd4a3b10SKuan-Chung Chen 	if (phy_ppdu->ofdm.has && (phy_ppdu->has_data || phy_ppdu->has_bcn)) {
15444bb223a1SPing-Ke Shih 		ewma_snr_add(&rtwsta->avg_snr, phy_ppdu->ofdm.avg_snr);
1545bd4a3b10SKuan-Chung Chen 		if (rtw89_get_data_rate_nss(rtwdev, phy_ppdu->rate) == 1) {
1546bd4a3b10SKuan-Chung Chen 			ewma_evm_add(&rtwsta->evm_1ss, phy_ppdu->ofdm.evm_min);
1547bd4a3b10SKuan-Chung Chen 		} else {
15484bb223a1SPing-Ke Shih 			ewma_evm_add(&rtwsta->evm_min[evm_pos], phy_ppdu->ofdm.evm_min);
15494bb223a1SPing-Ke Shih 			ewma_evm_add(&rtwsta->evm_max[evm_pos], phy_ppdu->ofdm.evm_max);
15504bb223a1SPing-Ke Shih 		}
1551e3ec7017SPing-Ke Shih 	}
1552bd4a3b10SKuan-Chung Chen }
1553e3ec7017SPing-Ke Shih 
1554e3ec7017SPing-Ke Shih #define VAR_LEN 0xff
1555e3ec7017SPing-Ke Shih #define VAR_LEN_UNIT 8
rtw89_core_get_phy_status_ie_len(struct rtw89_dev * rtwdev,const struct rtw89_phy_sts_iehdr * iehdr)1556332debb8SPing-Ke Shih static u16 rtw89_core_get_phy_status_ie_len(struct rtw89_dev *rtwdev,
1557332debb8SPing-Ke Shih 					    const struct rtw89_phy_sts_iehdr *iehdr)
1558e3ec7017SPing-Ke Shih {
1559944496baSPing-Ke Shih 	static const u8 physts_ie_len_tabs[RTW89_CHIP_GEN_NUM][32] = {
1560944496baSPing-Ke Shih 		[RTW89_CHIP_AX] = {
1561e3ec7017SPing-Ke Shih 			16, 32, 24, 24, 8, 8, 8, 8, VAR_LEN, 8, VAR_LEN, 176, VAR_LEN,
1562e3ec7017SPing-Ke Shih 			VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, 16, 24, VAR_LEN,
1563e3ec7017SPing-Ke Shih 			VAR_LEN, VAR_LEN, 0, 24, 24, 24, 24, 32, 32, 32, 32
1564944496baSPing-Ke Shih 		},
1565944496baSPing-Ke Shih 		[RTW89_CHIP_BE] = {
1566944496baSPing-Ke Shih 			32, 40, 24, 24, 8, 8, 8, 8, VAR_LEN, 8, VAR_LEN, 176, VAR_LEN,
1567944496baSPing-Ke Shih 			VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, 16, 24, VAR_LEN,
1568944496baSPing-Ke Shih 			VAR_LEN, VAR_LEN, 0, 24, 24, 24, 24, 32, 32, 32, 32
1569944496baSPing-Ke Shih 		},
1570e3ec7017SPing-Ke Shih 	};
1571944496baSPing-Ke Shih 	const u8 *physts_ie_len_tab;
1572e3ec7017SPing-Ke Shih 	u16 ie_len;
1573e3ec7017SPing-Ke Shih 	u8 ie;
1574e3ec7017SPing-Ke Shih 
1575944496baSPing-Ke Shih 	physts_ie_len_tab = physts_ie_len_tabs[rtwdev->chip->chip_gen];
1576944496baSPing-Ke Shih 
1577332debb8SPing-Ke Shih 	ie = le32_get_bits(iehdr->w0, RTW89_PHY_STS_IEHDR_TYPE);
1578e3ec7017SPing-Ke Shih 	if (physts_ie_len_tab[ie] != VAR_LEN)
1579e3ec7017SPing-Ke Shih 		ie_len = physts_ie_len_tab[ie];
1580e3ec7017SPing-Ke Shih 	else
1581332debb8SPing-Ke Shih 		ie_len = le32_get_bits(iehdr->w0, RTW89_PHY_STS_IEHDR_LEN) * VAR_LEN_UNIT;
1582e3ec7017SPing-Ke Shih 
1583e3ec7017SPing-Ke Shih 	return ie_len;
1584e3ec7017SPing-Ke Shih }
1585e3ec7017SPing-Ke Shih 
rtw89_core_parse_phy_status_ie01_v2(struct rtw89_dev * rtwdev,const struct rtw89_phy_sts_iehdr * iehdr,struct rtw89_rx_phy_ppdu * phy_ppdu)1586*c9ac071eSEric Huang static void rtw89_core_parse_phy_status_ie01_v2(struct rtw89_dev *rtwdev,
1587*c9ac071eSEric Huang 						const struct rtw89_phy_sts_iehdr *iehdr,
1588*c9ac071eSEric Huang 						struct rtw89_rx_phy_ppdu *phy_ppdu)
1589*c9ac071eSEric Huang {
1590*c9ac071eSEric Huang 	const struct rtw89_phy_sts_ie01_v2 *ie;
1591*c9ac071eSEric Huang 	u8 *rpl_fd = phy_ppdu->rpl_fd;
1592*c9ac071eSEric Huang 
1593*c9ac071eSEric Huang 	ie = (const struct rtw89_phy_sts_ie01_v2 *)iehdr;
1594*c9ac071eSEric Huang 	rpl_fd[RF_PATH_A] = le32_get_bits(ie->w8, RTW89_PHY_STS_IE01_V2_W8_RPL_FD_A);
1595*c9ac071eSEric Huang 	rpl_fd[RF_PATH_B] = le32_get_bits(ie->w8, RTW89_PHY_STS_IE01_V2_W8_RPL_FD_B);
1596*c9ac071eSEric Huang 	rpl_fd[RF_PATH_C] = le32_get_bits(ie->w9, RTW89_PHY_STS_IE01_V2_W9_RPL_FD_C);
1597*c9ac071eSEric Huang 	rpl_fd[RF_PATH_D] = le32_get_bits(ie->w9, RTW89_PHY_STS_IE01_V2_W9_RPL_FD_D);
1598*c9ac071eSEric Huang 
1599*c9ac071eSEric Huang 	phy_ppdu->bw_idx = le32_get_bits(ie->w5, RTW89_PHY_STS_IE01_V2_W5_BW_IDX);
1600*c9ac071eSEric Huang }
1601*c9ac071eSEric Huang 
rtw89_core_parse_phy_status_ie01(struct rtw89_dev * rtwdev,const struct rtw89_phy_sts_iehdr * iehdr,struct rtw89_rx_phy_ppdu * phy_ppdu)1602332debb8SPing-Ke Shih static void rtw89_core_parse_phy_status_ie01(struct rtw89_dev *rtwdev,
1603332debb8SPing-Ke Shih 					     const struct rtw89_phy_sts_iehdr *iehdr,
1604e3ec7017SPing-Ke Shih 					     struct rtw89_rx_phy_ppdu *phy_ppdu)
1605e3ec7017SPing-Ke Shih {
1606*c9ac071eSEric Huang 	const struct rtw89_phy_sts_ie01 *ie = (const struct rtw89_phy_sts_ie01 *)iehdr;
1607e3ec7017SPing-Ke Shih 	s16 cfo;
160898055006SPing-Ke Shih 	u32 t;
1609e3ec7017SPing-Ke Shih 
161098055006SPing-Ke Shih 	phy_ppdu->chan_idx = le32_get_bits(ie->w0, RTW89_PHY_STS_IE01_W0_CH_IDX);
161185e02a0aSPing-Ke Shih 
161285e02a0aSPing-Ke Shih 	if (rtwdev->hw->conf.flags & IEEE80211_CONF_MONITOR) {
161385e02a0aSPing-Ke Shih 		phy_ppdu->ldpc = le32_get_bits(ie->w2, RTW89_PHY_STS_IE01_W2_LDPC);
161485e02a0aSPing-Ke Shih 		phy_ppdu->stbc = le32_get_bits(ie->w2, RTW89_PHY_STS_IE01_W2_STBC);
161585e02a0aSPing-Ke Shih 	}
161685e02a0aSPing-Ke Shih 
1617*c9ac071eSEric Huang 	if (!phy_ppdu->hdr_2_en)
1618*c9ac071eSEric Huang 		phy_ppdu->rx_path_en =
1619*c9ac071eSEric Huang 			le32_get_bits(ie->w0, RTW89_PHY_STS_IE01_W0_RX_PATH_EN);
1620*c9ac071eSEric Huang 
1621eb4e52b3SPo Hao Huang 	if (phy_ppdu->rate < RTW89_HW_RATE_OFDM6)
1622eb4e52b3SPo Hao Huang 		return;
162340711486SChih-Kang Chang 
162440711486SChih-Kang Chang 	if (!phy_ppdu->to_self)
162540711486SChih-Kang Chang 		return;
162640711486SChih-Kang Chang 
1627*c9ac071eSEric Huang 	phy_ppdu->rpl_avg = le32_get_bits(ie->w0, RTW89_PHY_STS_IE01_W0_RSSI_AVG_FD);
16284bb223a1SPing-Ke Shih 	phy_ppdu->ofdm.avg_snr = le32_get_bits(ie->w2, RTW89_PHY_STS_IE01_W2_AVG_SNR);
16294bb223a1SPing-Ke Shih 	phy_ppdu->ofdm.evm_max = le32_get_bits(ie->w2, RTW89_PHY_STS_IE01_W2_EVM_MAX);
16304bb223a1SPing-Ke Shih 	phy_ppdu->ofdm.evm_min = le32_get_bits(ie->w2, RTW89_PHY_STS_IE01_W2_EVM_MIN);
16314bb223a1SPing-Ke Shih 	phy_ppdu->ofdm.has = true;
16324bb223a1SPing-Ke Shih 
1633e3ec7017SPing-Ke Shih 	/* sign conversion for S(12,2) */
163498055006SPing-Ke Shih 	if (rtwdev->chip->cfo_src_fd) {
163598055006SPing-Ke Shih 		t = le32_get_bits(ie->w1, RTW89_PHY_STS_IE01_W1_FD_CFO);
163698055006SPing-Ke Shih 		cfo = sign_extend32(t, 11);
163798055006SPing-Ke Shih 	} else {
163898055006SPing-Ke Shih 		t = le32_get_bits(ie->w1, RTW89_PHY_STS_IE01_W1_PREMB_CFO);
163998055006SPing-Ke Shih 		cfo = sign_extend32(t, 11);
164098055006SPing-Ke Shih 	}
164110cd4092SEric Huang 
1642e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_parse(rtwdev, cfo, phy_ppdu);
1643*c9ac071eSEric Huang 
1644*c9ac071eSEric Huang 	if (rtwdev->chip->chip_gen == RTW89_CHIP_BE)
1645*c9ac071eSEric Huang 		rtw89_core_parse_phy_status_ie01_v2(rtwdev, iehdr, phy_ppdu);
1646*c9ac071eSEric Huang }
1647*c9ac071eSEric Huang 
rtw89_core_parse_phy_status_ie00(struct rtw89_dev * rtwdev,const struct rtw89_phy_sts_iehdr * iehdr,struct rtw89_rx_phy_ppdu * phy_ppdu)1648*c9ac071eSEric Huang static void rtw89_core_parse_phy_status_ie00(struct rtw89_dev *rtwdev,
1649*c9ac071eSEric Huang 					     const struct rtw89_phy_sts_iehdr *iehdr,
1650*c9ac071eSEric Huang 					     struct rtw89_rx_phy_ppdu *phy_ppdu)
1651*c9ac071eSEric Huang {
1652*c9ac071eSEric Huang 	const struct rtw89_phy_sts_ie00 *ie = (const struct rtw89_phy_sts_ie00 *)iehdr;
1653*c9ac071eSEric Huang 	u16 tmp_rpl;
1654*c9ac071eSEric Huang 
1655*c9ac071eSEric Huang 	tmp_rpl = le32_get_bits(ie->w0, RTW89_PHY_STS_IE00_W0_RPL);
1656*c9ac071eSEric Huang 	phy_ppdu->rpl_avg = tmp_rpl >> 1;
1657*c9ac071eSEric Huang }
1658*c9ac071eSEric Huang 
rtw89_core_parse_phy_status_ie00_v2(struct rtw89_dev * rtwdev,const struct rtw89_phy_sts_iehdr * iehdr,struct rtw89_rx_phy_ppdu * phy_ppdu)1659*c9ac071eSEric Huang static void rtw89_core_parse_phy_status_ie00_v2(struct rtw89_dev *rtwdev,
1660*c9ac071eSEric Huang 						const struct rtw89_phy_sts_iehdr *iehdr,
1661*c9ac071eSEric Huang 						struct rtw89_rx_phy_ppdu *phy_ppdu)
1662*c9ac071eSEric Huang {
1663*c9ac071eSEric Huang 	const struct rtw89_phy_sts_ie00_v2 *ie;
1664*c9ac071eSEric Huang 	u8 *rpl_path = phy_ppdu->rpl_path;
1665*c9ac071eSEric Huang 	u16 tmp_rpl[RF_PATH_MAX];
1666*c9ac071eSEric Huang 	u8 i;
1667*c9ac071eSEric Huang 
1668*c9ac071eSEric Huang 	ie = (const struct rtw89_phy_sts_ie00_v2 *)iehdr;
1669*c9ac071eSEric Huang 	tmp_rpl[RF_PATH_A] = le32_get_bits(ie->w4, RTW89_PHY_STS_IE00_V2_W4_RPL_TD_A);
1670*c9ac071eSEric Huang 	tmp_rpl[RF_PATH_B] = le32_get_bits(ie->w4, RTW89_PHY_STS_IE00_V2_W4_RPL_TD_B);
1671*c9ac071eSEric Huang 	tmp_rpl[RF_PATH_C] = le32_get_bits(ie->w4, RTW89_PHY_STS_IE00_V2_W4_RPL_TD_C);
1672*c9ac071eSEric Huang 	tmp_rpl[RF_PATH_D] = le32_get_bits(ie->w5, RTW89_PHY_STS_IE00_V2_W5_RPL_TD_D);
1673*c9ac071eSEric Huang 
1674*c9ac071eSEric Huang 	for (i = 0; i < RF_PATH_MAX; i++)
1675*c9ac071eSEric Huang 		rpl_path[i] = tmp_rpl[i] >> 1;
1676e3ec7017SPing-Ke Shih }
1677e3ec7017SPing-Ke Shih 
rtw89_core_process_phy_status_ie(struct rtw89_dev * rtwdev,const struct rtw89_phy_sts_iehdr * iehdr,struct rtw89_rx_phy_ppdu * phy_ppdu)1678332debb8SPing-Ke Shih static int rtw89_core_process_phy_status_ie(struct rtw89_dev *rtwdev,
1679332debb8SPing-Ke Shih 					    const struct rtw89_phy_sts_iehdr *iehdr,
1680e3ec7017SPing-Ke Shih 					    struct rtw89_rx_phy_ppdu *phy_ppdu)
1681e3ec7017SPing-Ke Shih {
1682e3ec7017SPing-Ke Shih 	u8 ie;
1683e3ec7017SPing-Ke Shih 
1684332debb8SPing-Ke Shih 	ie = le32_get_bits(iehdr->w0, RTW89_PHY_STS_IEHDR_TYPE);
1685332debb8SPing-Ke Shih 
1686e3ec7017SPing-Ke Shih 	switch (ie) {
1687*c9ac071eSEric Huang 	case RTW89_PHYSTS_IE00_CMN_CCK:
1688*c9ac071eSEric Huang 		rtw89_core_parse_phy_status_ie00(rtwdev, iehdr, phy_ppdu);
1689*c9ac071eSEric Huang 		if (rtwdev->chip->chip_gen == RTW89_CHIP_BE)
1690*c9ac071eSEric Huang 			rtw89_core_parse_phy_status_ie00_v2(rtwdev, iehdr, phy_ppdu);
1691*c9ac071eSEric Huang 		break;
1692e3ec7017SPing-Ke Shih 	case RTW89_PHYSTS_IE01_CMN_OFDM:
1693332debb8SPing-Ke Shih 		rtw89_core_parse_phy_status_ie01(rtwdev, iehdr, phy_ppdu);
1694e3ec7017SPing-Ke Shih 		break;
1695e3ec7017SPing-Ke Shih 	default:
1696e3ec7017SPing-Ke Shih 		break;
1697e3ec7017SPing-Ke Shih 	}
1698e3ec7017SPing-Ke Shih 
1699e3ec7017SPing-Ke Shih 	return 0;
1700e3ec7017SPing-Ke Shih }
1701e3ec7017SPing-Ke Shih 
rtw89_core_update_phy_ppdu_hdr_v2(struct rtw89_rx_phy_ppdu * phy_ppdu)1702*c9ac071eSEric Huang static void rtw89_core_update_phy_ppdu_hdr_v2(struct rtw89_rx_phy_ppdu *phy_ppdu)
1703*c9ac071eSEric Huang {
1704*c9ac071eSEric Huang 	const struct rtw89_phy_sts_hdr_v2 *hdr = phy_ppdu->buf + PHY_STS_HDR_LEN;
1705*c9ac071eSEric Huang 
1706*c9ac071eSEric Huang 	phy_ppdu->rx_path_en = le32_get_bits(hdr->w0, RTW89_PHY_STS_HDR_V2_W0_PATH_EN);
1707*c9ac071eSEric Huang }
1708*c9ac071eSEric Huang 
rtw89_core_update_phy_ppdu(struct rtw89_rx_phy_ppdu * phy_ppdu)1709e3ec7017SPing-Ke Shih static void rtw89_core_update_phy_ppdu(struct rtw89_rx_phy_ppdu *phy_ppdu)
1710e3ec7017SPing-Ke Shih {
1711332debb8SPing-Ke Shih 	const struct rtw89_phy_sts_hdr *hdr = phy_ppdu->buf;
17126ce472d6SPing-Ke Shih 	u8 *rssi = phy_ppdu->rssi;
1713e3ec7017SPing-Ke Shih 
1714332debb8SPing-Ke Shih 	phy_ppdu->ie = le32_get_bits(hdr->w0, RTW89_PHY_STS_HDR_W0_IE_MAP);
1715332debb8SPing-Ke Shih 	phy_ppdu->rssi_avg = le32_get_bits(hdr->w0, RTW89_PHY_STS_HDR_W0_RSSI_AVG);
1716332debb8SPing-Ke Shih 	rssi[RF_PATH_A] = le32_get_bits(hdr->w1, RTW89_PHY_STS_HDR_W1_RSSI_A);
1717332debb8SPing-Ke Shih 	rssi[RF_PATH_B] = le32_get_bits(hdr->w1, RTW89_PHY_STS_HDR_W1_RSSI_B);
1718332debb8SPing-Ke Shih 	rssi[RF_PATH_C] = le32_get_bits(hdr->w1, RTW89_PHY_STS_HDR_W1_RSSI_C);
1719332debb8SPing-Ke Shih 	rssi[RF_PATH_D] = le32_get_bits(hdr->w1, RTW89_PHY_STS_HDR_W1_RSSI_D);
1720*c9ac071eSEric Huang 
1721*c9ac071eSEric Huang 	phy_ppdu->hdr_2_en = le32_get_bits(hdr->w0, RTW89_PHY_STS_HDR_W0_HDR_2_EN);
1722*c9ac071eSEric Huang 	if (phy_ppdu->hdr_2_en)
1723*c9ac071eSEric Huang 		rtw89_core_update_phy_ppdu_hdr_v2(phy_ppdu);
1724e3ec7017SPing-Ke Shih }
1725e3ec7017SPing-Ke Shih 
rtw89_core_rx_process_phy_ppdu(struct rtw89_dev * rtwdev,struct rtw89_rx_phy_ppdu * phy_ppdu)1726e3ec7017SPing-Ke Shih static int rtw89_core_rx_process_phy_ppdu(struct rtw89_dev *rtwdev,
1727e3ec7017SPing-Ke Shih 					  struct rtw89_rx_phy_ppdu *phy_ppdu)
1728e3ec7017SPing-Ke Shih {
1729332debb8SPing-Ke Shih 	const struct rtw89_phy_sts_hdr *hdr = phy_ppdu->buf;
1730332debb8SPing-Ke Shih 	u32 len_from_header;
1731e343faceSPing-Ke Shih 	bool physts_valid;
1732e343faceSPing-Ke Shih 
1733e343faceSPing-Ke Shih 	physts_valid = le32_get_bits(hdr->w0, RTW89_PHY_STS_HDR_W0_VALID);
1734e343faceSPing-Ke Shih 	if (!physts_valid)
1735e343faceSPing-Ke Shih 		return -EINVAL;
1736332debb8SPing-Ke Shih 
1737332debb8SPing-Ke Shih 	len_from_header = le32_get_bits(hdr->w0, RTW89_PHY_STS_HDR_W0_LEN) << 3;
1738332debb8SPing-Ke Shih 
1739944496baSPing-Ke Shih 	if (rtwdev->chip->chip_gen == RTW89_CHIP_BE)
1740944496baSPing-Ke Shih 		len_from_header += PHY_STS_HDR_LEN;
1741944496baSPing-Ke Shih 
1742332debb8SPing-Ke Shih 	if (len_from_header != phy_ppdu->len) {
1743ad663693SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_UNEXP, "phy ppdu len mismatch\n");
1744e3ec7017SPing-Ke Shih 		return -EINVAL;
1745e3ec7017SPing-Ke Shih 	}
1746e3ec7017SPing-Ke Shih 	rtw89_core_update_phy_ppdu(phy_ppdu);
1747e3ec7017SPing-Ke Shih 
1748e3ec7017SPing-Ke Shih 	return 0;
1749e3ec7017SPing-Ke Shih }
1750e3ec7017SPing-Ke Shih 
rtw89_core_rx_parse_phy_sts(struct rtw89_dev * rtwdev,struct rtw89_rx_phy_ppdu * phy_ppdu)1751e3ec7017SPing-Ke Shih static int rtw89_core_rx_parse_phy_sts(struct rtw89_dev *rtwdev,
1752e3ec7017SPing-Ke Shih 				       struct rtw89_rx_phy_ppdu *phy_ppdu)
1753e3ec7017SPing-Ke Shih {
1754e3ec7017SPing-Ke Shih 	u16 ie_len;
1755332debb8SPing-Ke Shih 	void *pos, *end;
1756e3ec7017SPing-Ke Shih 
1757eb4e52b3SPo Hao Huang 	/* mark invalid reports and bypass them */
1758eb4e52b3SPo Hao Huang 	if (phy_ppdu->ie < RTW89_CCK_PKT)
1759eb4e52b3SPo Hao Huang 		return -EINVAL;
1760e3ec7017SPing-Ke Shih 
1761332debb8SPing-Ke Shih 	pos = phy_ppdu->buf + PHY_STS_HDR_LEN;
1762332debb8SPing-Ke Shih 	end = phy_ppdu->buf + phy_ppdu->len;
1763e3ec7017SPing-Ke Shih 	while (pos < end) {
1764332debb8SPing-Ke Shih 		const struct rtw89_phy_sts_iehdr *iehdr = pos;
1765332debb8SPing-Ke Shih 
1766332debb8SPing-Ke Shih 		ie_len = rtw89_core_get_phy_status_ie_len(rtwdev, iehdr);
1767332debb8SPing-Ke Shih 		rtw89_core_process_phy_status_ie(rtwdev, iehdr, phy_ppdu);
1768e3ec7017SPing-Ke Shih 		pos += ie_len;
1769e3ec7017SPing-Ke Shih 		if (pos > end || ie_len == 0) {
1770e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TXRX,
1771e3ec7017SPing-Ke Shih 				    "phy status parse failed\n");
1772e3ec7017SPing-Ke Shih 			return -EINVAL;
1773e3ec7017SPing-Ke Shih 		}
1774e3ec7017SPing-Ke Shih 	}
1775e3ec7017SPing-Ke Shih 
1776*c9ac071eSEric Huang 	rtw89_chip_convert_rpl_to_rssi(rtwdev, phy_ppdu);
1777e3715859SEric Huang 	rtw89_phy_antdiv_parse(rtwdev, phy_ppdu);
1778e3715859SEric Huang 
1779e3ec7017SPing-Ke Shih 	return 0;
1780e3ec7017SPing-Ke Shih }
1781e3ec7017SPing-Ke Shih 
rtw89_core_rx_process_phy_sts(struct rtw89_dev * rtwdev,struct rtw89_rx_phy_ppdu * phy_ppdu)1782e3ec7017SPing-Ke Shih static void rtw89_core_rx_process_phy_sts(struct rtw89_dev *rtwdev,
1783e3ec7017SPing-Ke Shih 					  struct rtw89_rx_phy_ppdu *phy_ppdu)
1784e3ec7017SPing-Ke Shih {
1785e3ec7017SPing-Ke Shih 	int ret;
1786e3ec7017SPing-Ke Shih 
1787e3ec7017SPing-Ke Shih 	ret = rtw89_core_rx_parse_phy_sts(rtwdev, phy_ppdu);
1788e3ec7017SPing-Ke Shih 	if (ret)
1789e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TXRX, "parse phy sts failed\n");
1790e3ec7017SPing-Ke Shih 	else
1791e3ec7017SPing-Ke Shih 		phy_ppdu->valid = true;
17924bb223a1SPing-Ke Shih 
17934bb223a1SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
17944bb223a1SPing-Ke Shih 					  rtw89_core_rx_process_phy_ppdu_iter,
17954bb223a1SPing-Ke Shih 					  phy_ppdu);
1796e3ec7017SPing-Ke Shih }
1797e3ec7017SPing-Ke Shih 
rtw89_rxdesc_to_nl_he_eht_gi(struct rtw89_dev * rtwdev,u8 desc_info_gi,bool rx_status,bool eht)1798786a93c9SPing-Ke Shih static u8 rtw89_rxdesc_to_nl_he_eht_gi(struct rtw89_dev *rtwdev,
1799786a93c9SPing-Ke Shih 				       u8 desc_info_gi,
1800786a93c9SPing-Ke Shih 				       bool rx_status, bool eht)
1801e3ec7017SPing-Ke Shih {
1802786a93c9SPing-Ke Shih 	switch (desc_info_gi) {
1803e3ec7017SPing-Ke Shih 	case RTW89_GILTF_SGI_4XHE08:
1804e3ec7017SPing-Ke Shih 	case RTW89_GILTF_2XHE08:
1805e3ec7017SPing-Ke Shih 	case RTW89_GILTF_1XHE08:
1806786a93c9SPing-Ke Shih 		return eht ? NL80211_RATE_INFO_EHT_GI_0_8 :
1807786a93c9SPing-Ke Shih 			     NL80211_RATE_INFO_HE_GI_0_8;
1808e3ec7017SPing-Ke Shih 	case RTW89_GILTF_2XHE16:
1809e3ec7017SPing-Ke Shih 	case RTW89_GILTF_1XHE16:
1810786a93c9SPing-Ke Shih 		return eht ? NL80211_RATE_INFO_EHT_GI_1_6 :
1811786a93c9SPing-Ke Shih 			     NL80211_RATE_INFO_HE_GI_1_6;
1812e3ec7017SPing-Ke Shih 	case RTW89_GILTF_LGI_4XHE32:
1813786a93c9SPing-Ke Shih 		return eht ? NL80211_RATE_INFO_EHT_GI_3_2 :
1814786a93c9SPing-Ke Shih 			     NL80211_RATE_INFO_HE_GI_3_2;
1815e3ec7017SPing-Ke Shih 	default:
1816786a93c9SPing-Ke Shih 		rtw89_warn(rtwdev, "invalid gi_ltf=%d", desc_info_gi);
1817786a93c9SPing-Ke Shih 		if (rx_status)
1818786a93c9SPing-Ke Shih 			return eht ? NL80211_RATE_INFO_EHT_GI_3_2 :
1819786a93c9SPing-Ke Shih 				     NL80211_RATE_INFO_HE_GI_3_2;
1820786a93c9SPing-Ke Shih 		return U8_MAX;
1821e3ec7017SPing-Ke Shih 	}
1822e3ec7017SPing-Ke Shih }
1823e3ec7017SPing-Ke Shih 
1824786a93c9SPing-Ke Shih static
rtw89_check_rx_statu_gi_match(struct ieee80211_rx_status * status,u8 gi_ltf,bool eht)1825786a93c9SPing-Ke Shih bool rtw89_check_rx_statu_gi_match(struct ieee80211_rx_status *status, u8 gi_ltf,
1826786a93c9SPing-Ke Shih 				   bool eht)
1827786a93c9SPing-Ke Shih {
1828786a93c9SPing-Ke Shih 	if (eht)
1829786a93c9SPing-Ke Shih 		return status->eht.gi == gi_ltf;
1830786a93c9SPing-Ke Shih 
1831786a93c9SPing-Ke Shih 	return status->he_gi == gi_ltf;
1832786a93c9SPing-Ke Shih }
1833786a93c9SPing-Ke Shih 
rtw89_core_rx_ppdu_match(struct rtw89_dev * rtwdev,struct rtw89_rx_desc_info * desc_info,struct ieee80211_rx_status * status)1834e3ec7017SPing-Ke Shih static bool rtw89_core_rx_ppdu_match(struct rtw89_dev *rtwdev,
1835e3ec7017SPing-Ke Shih 				     struct rtw89_rx_desc_info *desc_info,
1836e3ec7017SPing-Ke Shih 				     struct ieee80211_rx_status *status)
1837e3ec7017SPing-Ke Shih {
1838e3ec7017SPing-Ke Shih 	u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
1839e3ec7017SPing-Ke Shih 	u8 data_rate_mode, bw, rate_idx = MASKBYTE0, gi_ltf;
1840786a93c9SPing-Ke Shih 	bool eht = false;
1841e3ec7017SPing-Ke Shih 	u16 data_rate;
1842e3ec7017SPing-Ke Shih 	bool ret;
1843e3ec7017SPing-Ke Shih 
1844e3ec7017SPing-Ke Shih 	data_rate = desc_info->data_rate;
1845023d2f14SPing-Ke Shih 	data_rate_mode = rtw89_get_data_rate_mode(rtwdev, data_rate);
1846e3ec7017SPing-Ke Shih 	if (data_rate_mode == DATA_RATE_MODE_NON_HT) {
1847023d2f14SPing-Ke Shih 		rate_idx = rtw89_get_data_not_ht_idx(rtwdev, data_rate);
1848eb4e52b3SPo Hao Huang 		/* rate_idx is still hardware value here */
1849e3ec7017SPing-Ke Shih 	} else if (data_rate_mode == DATA_RATE_MODE_HT) {
1850023d2f14SPing-Ke Shih 		rate_idx = rtw89_get_data_ht_mcs(rtwdev, data_rate);
1851786a93c9SPing-Ke Shih 	} else if (data_rate_mode == DATA_RATE_MODE_VHT ||
1852786a93c9SPing-Ke Shih 		   data_rate_mode == DATA_RATE_MODE_HE ||
1853786a93c9SPing-Ke Shih 		   data_rate_mode == DATA_RATE_MODE_EHT) {
1854023d2f14SPing-Ke Shih 		rate_idx = rtw89_get_data_mcs(rtwdev, data_rate);
1855e3ec7017SPing-Ke Shih 	} else {
1856e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "invalid RX rate mode %d\n", data_rate_mode);
1857e3ec7017SPing-Ke Shih 	}
1858e3ec7017SPing-Ke Shih 
1859786a93c9SPing-Ke Shih 	eht = data_rate_mode == DATA_RATE_MODE_EHT;
1860167044afSPing-Ke Shih 	bw = rtw89_hw_to_rate_info_bw(desc_info->bw);
1861786a93c9SPing-Ke Shih 	gi_ltf = rtw89_rxdesc_to_nl_he_eht_gi(rtwdev, desc_info->gi_ltf, false, eht);
1862e3ec7017SPing-Ke Shih 	ret = rtwdev->ppdu_sts.curr_rx_ppdu_cnt[band] == desc_info->ppdu_cnt &&
1863e3ec7017SPing-Ke Shih 	      status->rate_idx == rate_idx &&
1864786a93c9SPing-Ke Shih 	      rtw89_check_rx_statu_gi_match(status, gi_ltf, eht) &&
1865e3ec7017SPing-Ke Shih 	      status->bw == bw;
1866e3ec7017SPing-Ke Shih 
1867e3ec7017SPing-Ke Shih 	return ret;
1868e3ec7017SPing-Ke Shih }
1869e3ec7017SPing-Ke Shih 
1870e3ec7017SPing-Ke Shih struct rtw89_vif_rx_stats_iter_data {
1871e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
1872e3ec7017SPing-Ke Shih 	struct rtw89_rx_phy_ppdu *phy_ppdu;
1873e3ec7017SPing-Ke Shih 	struct rtw89_rx_desc_info *desc_info;
1874e3ec7017SPing-Ke Shih 	struct sk_buff *skb;
1875e3ec7017SPing-Ke Shih 	const u8 *bssid;
1876e3ec7017SPing-Ke Shih };
1877e3ec7017SPing-Ke Shih 
rtw89_stats_trigger_frame(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct sk_buff * skb)18785165f168SPo Hao Huang static void rtw89_stats_trigger_frame(struct rtw89_dev *rtwdev,
18795165f168SPo Hao Huang 				      struct ieee80211_vif *vif,
18805165f168SPo Hao Huang 				      struct sk_buff *skb)
18815165f168SPo Hao Huang {
18825165f168SPo Hao Huang 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
18835165f168SPo Hao Huang 	struct ieee80211_trigger *tf = (struct ieee80211_trigger *)skb->data;
1884fc158f91SPo-Hao Huang 	u8 *pos, *end, type, tf_bw;
1885fc158f91SPo-Hao Huang 	u16 aid, tf_rua;
18865165f168SPo Hao Huang 
18875165f168SPo Hao Huang 	if (!ether_addr_equal(vif->bss_conf.bssid, tf->ta) ||
18885165f168SPo Hao Huang 	    rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION ||
18895165f168SPo Hao Huang 	    rtwvif->net_type == RTW89_NET_TYPE_NO_LINK)
18905165f168SPo Hao Huang 		return;
18915165f168SPo Hao Huang 
18925165f168SPo Hao Huang 	type = le64_get_bits(tf->common_info, IEEE80211_TRIGGER_TYPE_MASK);
1893fc158f91SPo-Hao Huang 	if (type != IEEE80211_TRIGGER_TYPE_BASIC && type != IEEE80211_TRIGGER_TYPE_MU_BAR)
18945165f168SPo Hao Huang 		return;
18955165f168SPo Hao Huang 
18965165f168SPo Hao Huang 	end = (u8 *)tf + skb->len;
18975165f168SPo Hao Huang 	pos = tf->variable;
18985165f168SPo Hao Huang 
18995165f168SPo Hao Huang 	while (end - pos >= RTW89_TF_BASIC_USER_INFO_SZ) {
19005165f168SPo Hao Huang 		aid = RTW89_GET_TF_USER_INFO_AID12(pos);
1901fc158f91SPo-Hao Huang 		tf_rua = RTW89_GET_TF_USER_INFO_RUA(pos);
1902fc158f91SPo-Hao Huang 		tf_bw = le64_get_bits(tf->common_info, IEEE80211_TRIGGER_ULBW_MASK);
19035165f168SPo Hao Huang 		rtw89_debug(rtwdev, RTW89_DBG_TXRX,
1904fc158f91SPo-Hao Huang 			    "[TF] aid: %d, ul_mcs: %d, rua: %d, bw: %d\n",
19055165f168SPo Hao Huang 			    aid, RTW89_GET_TF_USER_INFO_UL_MCS(pos),
1906fc158f91SPo-Hao Huang 			    tf_rua, tf_bw);
19075165f168SPo Hao Huang 
19085165f168SPo Hao Huang 		if (aid == RTW89_TF_PAD)
19095165f168SPo Hao Huang 			break;
19105165f168SPo Hao Huang 
1911f276e20bSJohannes Berg 		if (aid == vif->cfg.aid) {
1912fc158f91SPo-Hao Huang 			enum nl80211_he_ru_alloc rua = rtw89_he_rua_to_ru_alloc(tf_rua >> 1);
1913fc158f91SPo-Hao Huang 
19145165f168SPo Hao Huang 			rtwvif->stats.rx_tf_acc++;
19155165f168SPo Hao Huang 			rtwdev->stats.rx_tf_acc++;
1916fc158f91SPo-Hao Huang 			if (tf_bw == IEEE80211_TRIGGER_ULBW_160_80P80MHZ &&
1917fc158f91SPo-Hao Huang 			    rua <= NL80211_RATE_INFO_HE_RU_ALLOC_106)
1918fc158f91SPo-Hao Huang 				rtwvif->pwr_diff_en = true;
19195165f168SPo Hao Huang 			break;
19205165f168SPo Hao Huang 		}
19215165f168SPo Hao Huang 
19225165f168SPo Hao Huang 		pos += RTW89_TF_BASIC_USER_INFO_SZ;
19235165f168SPo Hao Huang 	}
19245165f168SPo Hao Huang }
19255165f168SPo Hao Huang 
rtw89_cancel_6ghz_probe_work(struct work_struct * work)19263ea1cd8dSZong-Zhe Yang static void rtw89_cancel_6ghz_probe_work(struct work_struct *work)
19273ea1cd8dSZong-Zhe Yang {
19283ea1cd8dSZong-Zhe Yang 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
19293ea1cd8dSZong-Zhe Yang 						cancel_6ghz_probe_work);
19303ea1cd8dSZong-Zhe Yang 	struct list_head *pkt_list = rtwdev->scan_info.pkt_list;
19313ea1cd8dSZong-Zhe Yang 	struct rtw89_pktofld_info *info;
19323ea1cd8dSZong-Zhe Yang 
19333ea1cd8dSZong-Zhe Yang 	mutex_lock(&rtwdev->mutex);
19343ea1cd8dSZong-Zhe Yang 
19353ea1cd8dSZong-Zhe Yang 	if (!rtwdev->scanning)
19363ea1cd8dSZong-Zhe Yang 		goto out;
19373ea1cd8dSZong-Zhe Yang 
19383ea1cd8dSZong-Zhe Yang 	list_for_each_entry(info, &pkt_list[NL80211_BAND_6GHZ], list) {
19393ea1cd8dSZong-Zhe Yang 		if (!info->cancel || !test_bit(info->id, rtwdev->pkt_offload))
19403ea1cd8dSZong-Zhe Yang 			continue;
19413ea1cd8dSZong-Zhe Yang 
19423ea1cd8dSZong-Zhe Yang 		rtw89_fw_h2c_del_pkt_offload(rtwdev, info->id);
19433ea1cd8dSZong-Zhe Yang 
19443ea1cd8dSZong-Zhe Yang 		/* Don't delete/free info from pkt_list at this moment. Let it
19453ea1cd8dSZong-Zhe Yang 		 * be deleted/freed in rtw89_release_pkt_list() after scanning,
19463ea1cd8dSZong-Zhe Yang 		 * since if during scanning, pkt_list is accessed in bottom half.
19473ea1cd8dSZong-Zhe Yang 		 */
19483ea1cd8dSZong-Zhe Yang 	}
19493ea1cd8dSZong-Zhe Yang 
19503ea1cd8dSZong-Zhe Yang out:
19513ea1cd8dSZong-Zhe Yang 	mutex_unlock(&rtwdev->mutex);
19523ea1cd8dSZong-Zhe Yang }
19533ea1cd8dSZong-Zhe Yang 
rtw89_core_cancel_6ghz_probe_tx(struct rtw89_dev * rtwdev,struct sk_buff * skb)1954c6aa9a9cSPo-Hao Huang static void rtw89_core_cancel_6ghz_probe_tx(struct rtw89_dev *rtwdev,
1955c6aa9a9cSPo-Hao Huang 					    struct sk_buff *skb)
1956c6aa9a9cSPo-Hao Huang {
1957c6aa9a9cSPo-Hao Huang 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1958c6aa9a9cSPo-Hao Huang 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1959c6aa9a9cSPo-Hao Huang 	struct list_head *pkt_list = rtwdev->scan_info.pkt_list;
1960c6aa9a9cSPo-Hao Huang 	struct rtw89_pktofld_info *info;
1961c6aa9a9cSPo-Hao Huang 	const u8 *ies = mgmt->u.beacon.variable, *ssid_ie;
19623ea1cd8dSZong-Zhe Yang 	bool queue_work = false;
1963c6aa9a9cSPo-Hao Huang 
1964c6aa9a9cSPo-Hao Huang 	if (rx_status->band != NL80211_BAND_6GHZ)
1965c6aa9a9cSPo-Hao Huang 		return;
1966c6aa9a9cSPo-Hao Huang 
1967c6aa9a9cSPo-Hao Huang 	ssid_ie = cfg80211_find_ie(WLAN_EID_SSID, ies, skb->len);
1968c6aa9a9cSPo-Hao Huang 
1969c6aa9a9cSPo-Hao Huang 	list_for_each_entry(info, &pkt_list[NL80211_BAND_6GHZ], list) {
1970c6aa9a9cSPo-Hao Huang 		if (ether_addr_equal(info->bssid, mgmt->bssid)) {
19713ea1cd8dSZong-Zhe Yang 			info->cancel = true;
19723ea1cd8dSZong-Zhe Yang 			queue_work = true;
1973c6aa9a9cSPo-Hao Huang 			continue;
1974c6aa9a9cSPo-Hao Huang 		}
1975c6aa9a9cSPo-Hao Huang 
1976c6aa9a9cSPo-Hao Huang 		if (!ssid_ie || ssid_ie[1] != info->ssid_len || info->ssid_len == 0)
1977c6aa9a9cSPo-Hao Huang 			continue;
1978c6aa9a9cSPo-Hao Huang 
19793ea1cd8dSZong-Zhe Yang 		if (memcmp(&ssid_ie[2], info->ssid, info->ssid_len) == 0) {
19803ea1cd8dSZong-Zhe Yang 			info->cancel = true;
19813ea1cd8dSZong-Zhe Yang 			queue_work = true;
1982c6aa9a9cSPo-Hao Huang 		}
1983c6aa9a9cSPo-Hao Huang 	}
1984c6aa9a9cSPo-Hao Huang 
19853ea1cd8dSZong-Zhe Yang 	if (queue_work)
19863ea1cd8dSZong-Zhe Yang 		ieee80211_queue_work(rtwdev->hw, &rtwdev->cancel_6ghz_probe_work);
19873ea1cd8dSZong-Zhe Yang }
19883ea1cd8dSZong-Zhe Yang 
rtw89_vif_sync_bcn_tsf(struct rtw89_vif * rtwvif,struct ieee80211_hdr * hdr,size_t len)1989188045a8SZong-Zhe Yang static void rtw89_vif_sync_bcn_tsf(struct rtw89_vif *rtwvif,
1990188045a8SZong-Zhe Yang 				   struct ieee80211_hdr *hdr, size_t len)
1991188045a8SZong-Zhe Yang {
1992188045a8SZong-Zhe Yang 	struct ieee80211_mgmt *mgmt = (typeof(mgmt))hdr;
1993188045a8SZong-Zhe Yang 
1994188045a8SZong-Zhe Yang 	if (len < offsetof(typeof(*mgmt), u.beacon.variable))
1995188045a8SZong-Zhe Yang 		return;
1996188045a8SZong-Zhe Yang 
1997188045a8SZong-Zhe Yang 	WRITE_ONCE(rtwvif->sync_bcn_tsf, le64_to_cpu(mgmt->u.beacon.timestamp));
1998188045a8SZong-Zhe Yang }
1999188045a8SZong-Zhe Yang 
rtw89_vif_rx_stats_iter(void * data,u8 * mac,struct ieee80211_vif * vif)2000e3ec7017SPing-Ke Shih static void rtw89_vif_rx_stats_iter(void *data, u8 *mac,
2001e3ec7017SPing-Ke Shih 				    struct ieee80211_vif *vif)
2002e3ec7017SPing-Ke Shih {
2003e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2004e3ec7017SPing-Ke Shih 	struct rtw89_vif_rx_stats_iter_data *iter_data = data;
2005e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = iter_data->rtwdev;
2006e3ec7017SPing-Ke Shih 	struct rtw89_pkt_stat *pkt_stat = &rtwdev->phystat.cur_pkt_stat;
2007e3ec7017SPing-Ke Shih 	struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
2008e3ec7017SPing-Ke Shih 	struct sk_buff *skb = iter_data->skb;
2009e3ec7017SPing-Ke Shih 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2010d2b6da24SPo-Hao Huang 	struct rtw89_rx_phy_ppdu *phy_ppdu = iter_data->phy_ppdu;
2011e3ec7017SPing-Ke Shih 	const u8 *bssid = iter_data->bssid;
2012e3ec7017SPing-Ke Shih 
2013c6aa9a9cSPo-Hao Huang 	if (rtwdev->scanning &&
2014c6aa9a9cSPo-Hao Huang 	    (ieee80211_is_beacon(hdr->frame_control) ||
2015c6aa9a9cSPo-Hao Huang 	     ieee80211_is_probe_resp(hdr->frame_control)))
2016c6aa9a9cSPo-Hao Huang 		rtw89_core_cancel_6ghz_probe_tx(rtwdev, skb);
2017c6aa9a9cSPo-Hao Huang 
2018cd9b6b3bSZong-Zhe Yang 	if (!vif->bss_conf.bssid)
2019cd9b6b3bSZong-Zhe Yang 		return;
2020cd9b6b3bSZong-Zhe Yang 
20215165f168SPo Hao Huang 	if (ieee80211_is_trigger(hdr->frame_control)) {
20225165f168SPo Hao Huang 		rtw89_stats_trigger_frame(rtwdev, vif, skb);
20235165f168SPo Hao Huang 		return;
20245165f168SPo Hao Huang 	}
20255165f168SPo Hao Huang 
2026e3ec7017SPing-Ke Shih 	if (!ether_addr_equal(vif->bss_conf.bssid, bssid))
2027e3ec7017SPing-Ke Shih 		return;
2028e3ec7017SPing-Ke Shih 
2029d2b6da24SPo-Hao Huang 	if (ieee80211_is_beacon(hdr->frame_control)) {
203060757f28SChih-Kang Chang 		if (vif->type == NL80211_IFTYPE_STATION &&
203160757f28SChih-Kang Chang 		    !test_bit(RTW89_FLAG_WOWLAN, rtwdev->flags)) {
2032188045a8SZong-Zhe Yang 			rtw89_vif_sync_bcn_tsf(rtwvif, hdr, skb->len);
2033d2b6da24SPo-Hao Huang 			rtw89_fw_h2c_rssi_offload(rtwdev, phy_ppdu);
2034188045a8SZong-Zhe Yang 		}
2035e3ec7017SPing-Ke Shih 		pkt_stat->beacon_nr++;
2036d2b6da24SPo-Hao Huang 	}
2037e3ec7017SPing-Ke Shih 
2038e3ec7017SPing-Ke Shih 	if (!ether_addr_equal(vif->addr, hdr->addr1))
2039e3ec7017SPing-Ke Shih 		return;
2040e3ec7017SPing-Ke Shih 
2041e3ec7017SPing-Ke Shih 	if (desc_info->data_rate < RTW89_HW_RATE_NR)
2042e3ec7017SPing-Ke Shih 		pkt_stat->rx_rate_cnt[desc_info->data_rate]++;
2043e3ec7017SPing-Ke Shih 
2044e3ec7017SPing-Ke Shih 	rtw89_traffic_stats_accu(rtwdev, &rtwvif->stats, skb, false);
2045e3ec7017SPing-Ke Shih }
2046e3ec7017SPing-Ke Shih 
rtw89_core_rx_stats(struct rtw89_dev * rtwdev,struct rtw89_rx_phy_ppdu * phy_ppdu,struct rtw89_rx_desc_info * desc_info,struct sk_buff * skb)2047e3ec7017SPing-Ke Shih static void rtw89_core_rx_stats(struct rtw89_dev *rtwdev,
2048e3ec7017SPing-Ke Shih 				struct rtw89_rx_phy_ppdu *phy_ppdu,
2049e3ec7017SPing-Ke Shih 				struct rtw89_rx_desc_info *desc_info,
2050e3ec7017SPing-Ke Shih 				struct sk_buff *skb)
2051e3ec7017SPing-Ke Shih {
2052e3ec7017SPing-Ke Shih 	struct rtw89_vif_rx_stats_iter_data iter_data;
2053e3ec7017SPing-Ke Shih 
2054e3ec7017SPing-Ke Shih 	rtw89_traffic_stats_accu(rtwdev, &rtwdev->stats, skb, false);
2055e3ec7017SPing-Ke Shih 
2056e3ec7017SPing-Ke Shih 	iter_data.rtwdev = rtwdev;
2057e3ec7017SPing-Ke Shih 	iter_data.phy_ppdu = phy_ppdu;
2058e3ec7017SPing-Ke Shih 	iter_data.desc_info = desc_info;
2059e3ec7017SPing-Ke Shih 	iter_data.skb = skb;
2060e3ec7017SPing-Ke Shih 	iter_data.bssid = get_hdr_bssid((struct ieee80211_hdr *)skb->data);
2061e3ec7017SPing-Ke Shih 	rtw89_iterate_vifs_bh(rtwdev, rtw89_vif_rx_stats_iter, &iter_data);
2062e3ec7017SPing-Ke Shih }
2063e3ec7017SPing-Ke Shih 
rtw89_correct_cck_chan(struct rtw89_dev * rtwdev,struct ieee80211_rx_status * status)2064eb4e52b3SPo Hao Huang static void rtw89_correct_cck_chan(struct rtw89_dev *rtwdev,
2065eb4e52b3SPo Hao Huang 				   struct ieee80211_rx_status *status)
2066eb4e52b3SPo Hao Huang {
2067cbb145b9SZong-Zhe Yang 	const struct rtw89_chan_rcd *rcd =
2068583e998eSZong-Zhe Yang 		rtw89_chan_rcd_get(rtwdev, RTW89_CHANCTX_0);
2069cbb145b9SZong-Zhe Yang 	u16 chan = rcd->prev_primary_channel;
2070b05fdc46SZong-Zhe Yang 	u8 band = rtw89_hw_to_nl80211_band(rcd->prev_band_type);
2071eb4e52b3SPo Hao Huang 
2072eb4e52b3SPo Hao Huang 	if (status->band != NL80211_BAND_2GHZ &&
2073eb4e52b3SPo Hao Huang 	    status->encoding == RX_ENC_LEGACY &&
2074eb4e52b3SPo Hao Huang 	    status->rate_idx < RTW89_HW_RATE_OFDM6) {
2075eb4e52b3SPo Hao Huang 		status->freq = ieee80211_channel_to_frequency(chan, band);
2076eb4e52b3SPo Hao Huang 		status->band = band;
2077eb4e52b3SPo Hao Huang 	}
2078eb4e52b3SPo Hao Huang }
2079eb4e52b3SPo Hao Huang 
rtw89_core_hw_to_sband_rate(struct ieee80211_rx_status * rx_status)2080eb4e52b3SPo Hao Huang static void rtw89_core_hw_to_sband_rate(struct ieee80211_rx_status *rx_status)
2081eb4e52b3SPo Hao Huang {
2082eb4e52b3SPo Hao Huang 	if (rx_status->band == NL80211_BAND_2GHZ ||
2083eb4e52b3SPo Hao Huang 	    rx_status->encoding != RX_ENC_LEGACY)
2084eb4e52b3SPo Hao Huang 		return;
208589590777SPo Hao Huang 
208689590777SPo Hao Huang 	/* Some control frames' freq(ACKs in this case) are reported wrong due
208789590777SPo Hao Huang 	 * to FW notify timing, set to lowest rate to prevent overflow.
208889590777SPo Hao Huang 	 */
208989590777SPo Hao Huang 	if (rx_status->rate_idx < RTW89_HW_RATE_OFDM6) {
209089590777SPo Hao Huang 		rx_status->rate_idx = 0;
209189590777SPo Hao Huang 		return;
209289590777SPo Hao Huang 	}
209389590777SPo Hao Huang 
2094eb4e52b3SPo Hao Huang 	/* No 4 CCK rates for non-2G */
2095eb4e52b3SPo Hao Huang 	rx_status->rate_idx -= 4;
2096eb4e52b3SPo Hao Huang }
2097eb4e52b3SPo Hao Huang 
209885e02a0aSPing-Ke Shih static
rtw89_core_update_rx_status_by_ppdu(struct rtw89_dev * rtwdev,struct ieee80211_rx_status * rx_status,struct rtw89_rx_phy_ppdu * phy_ppdu)209985e02a0aSPing-Ke Shih void rtw89_core_update_rx_status_by_ppdu(struct rtw89_dev *rtwdev,
210085e02a0aSPing-Ke Shih 					 struct ieee80211_rx_status *rx_status,
210185e02a0aSPing-Ke Shih 					 struct rtw89_rx_phy_ppdu *phy_ppdu)
210285e02a0aSPing-Ke Shih {
210385e02a0aSPing-Ke Shih 	if (!(rtwdev->hw->conf.flags & IEEE80211_CONF_MONITOR))
210485e02a0aSPing-Ke Shih 		return;
210585e02a0aSPing-Ke Shih 
210685e02a0aSPing-Ke Shih 	if (!phy_ppdu)
210785e02a0aSPing-Ke Shih 		return;
210885e02a0aSPing-Ke Shih 
210985e02a0aSPing-Ke Shih 	if (phy_ppdu->ldpc)
211085e02a0aSPing-Ke Shih 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
211185e02a0aSPing-Ke Shih 	if (phy_ppdu->stbc)
211285e02a0aSPing-Ke Shih 		rx_status->enc_flags |= u8_encode_bits(1, RX_ENC_FLAG_STBC_MASK);
211385e02a0aSPing-Ke Shih }
211485e02a0aSPing-Ke Shih 
2115f1f74dffSPing-Ke Shih static const u8 rx_status_bw_to_radiotap_eht_usig[] = {
2116f1f74dffSPing-Ke Shih 	[RATE_INFO_BW_20] = IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW_20MHZ,
2117f1f74dffSPing-Ke Shih 	[RATE_INFO_BW_5] = U8_MAX,
2118f1f74dffSPing-Ke Shih 	[RATE_INFO_BW_10] = U8_MAX,
2119f1f74dffSPing-Ke Shih 	[RATE_INFO_BW_40] = IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW_40MHZ,
2120f1f74dffSPing-Ke Shih 	[RATE_INFO_BW_80] = IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW_80MHZ,
2121f1f74dffSPing-Ke Shih 	[RATE_INFO_BW_160] = IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW_160MHZ,
2122f1f74dffSPing-Ke Shih 	[RATE_INFO_BW_HE_RU] = U8_MAX,
2123f1f74dffSPing-Ke Shih 	[RATE_INFO_BW_320] = IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW_320MHZ_1,
2124f1f74dffSPing-Ke Shih 	[RATE_INFO_BW_EHT_RU] = U8_MAX,
2125f1f74dffSPing-Ke Shih };
2126f1f74dffSPing-Ke Shih 
rtw89_core_update_radiotap_eht(struct rtw89_dev * rtwdev,struct sk_buff * skb,struct ieee80211_rx_status * rx_status)2127f1f74dffSPing-Ke Shih static void rtw89_core_update_radiotap_eht(struct rtw89_dev *rtwdev,
2128f1f74dffSPing-Ke Shih 					   struct sk_buff *skb,
2129f1f74dffSPing-Ke Shih 					   struct ieee80211_rx_status *rx_status)
2130f1f74dffSPing-Ke Shih {
2131f1f74dffSPing-Ke Shih 	struct ieee80211_radiotap_eht_usig *usig;
2132f1f74dffSPing-Ke Shih 	struct ieee80211_radiotap_eht *eht;
2133f1f74dffSPing-Ke Shih 	struct ieee80211_radiotap_tlv *tlv;
2134f1f74dffSPing-Ke Shih 	int eht_len = struct_size(eht, user_info, 1);
2135f1f74dffSPing-Ke Shih 	int usig_len = sizeof(*usig);
2136f1f74dffSPing-Ke Shih 	int len;
2137f1f74dffSPing-Ke Shih 	u8 bw;
2138f1f74dffSPing-Ke Shih 
2139f1f74dffSPing-Ke Shih 	len = sizeof(*tlv) + ALIGN(eht_len, 4) +
2140f1f74dffSPing-Ke Shih 	      sizeof(*tlv) + ALIGN(usig_len, 4);
2141f1f74dffSPing-Ke Shih 
2142f1f74dffSPing-Ke Shih 	rx_status->flag |= RX_FLAG_RADIOTAP_TLV_AT_END;
2143f1f74dffSPing-Ke Shih 	skb_reset_mac_header(skb);
2144f1f74dffSPing-Ke Shih 
2145f1f74dffSPing-Ke Shih 	/* EHT */
2146f1f74dffSPing-Ke Shih 	tlv = skb_push(skb, len);
2147f1f74dffSPing-Ke Shih 	memset(tlv, 0, len);
2148f1f74dffSPing-Ke Shih 	tlv->type = cpu_to_le16(IEEE80211_RADIOTAP_EHT);
2149f1f74dffSPing-Ke Shih 	tlv->len = cpu_to_le16(eht_len);
2150f1f74dffSPing-Ke Shih 
2151f1f74dffSPing-Ke Shih 	eht = (struct ieee80211_radiotap_eht *)tlv->data;
2152f1f74dffSPing-Ke Shih 	eht->known = cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_GI);
2153f1f74dffSPing-Ke Shih 	eht->data[0] =
2154f1f74dffSPing-Ke Shih 		le32_encode_bits(rx_status->eht.gi, IEEE80211_RADIOTAP_EHT_DATA0_GI);
2155f1f74dffSPing-Ke Shih 
2156f1f74dffSPing-Ke Shih 	eht->user_info[0] =
2157f1f74dffSPing-Ke Shih 		cpu_to_le32(IEEE80211_RADIOTAP_EHT_USER_INFO_MCS_KNOWN |
215885e02a0aSPing-Ke Shih 			    IEEE80211_RADIOTAP_EHT_USER_INFO_NSS_KNOWN_O |
215985e02a0aSPing-Ke Shih 			    IEEE80211_RADIOTAP_EHT_USER_INFO_CODING_KNOWN);
2160f1f74dffSPing-Ke Shih 	eht->user_info[0] |=
2161f1f74dffSPing-Ke Shih 		le32_encode_bits(rx_status->rate_idx, IEEE80211_RADIOTAP_EHT_USER_INFO_MCS) |
2162f1f74dffSPing-Ke Shih 		le32_encode_bits(rx_status->nss, IEEE80211_RADIOTAP_EHT_USER_INFO_NSS_O);
216385e02a0aSPing-Ke Shih 	if (rx_status->enc_flags & RX_ENC_FLAG_LDPC)
216485e02a0aSPing-Ke Shih 		eht->user_info[0] |=
216585e02a0aSPing-Ke Shih 			cpu_to_le32(IEEE80211_RADIOTAP_EHT_USER_INFO_CODING);
2166f1f74dffSPing-Ke Shih 
2167f1f74dffSPing-Ke Shih 	/* U-SIG */
2168f1f74dffSPing-Ke Shih 	tlv = (void *)tlv + sizeof(*tlv) + ALIGN(eht_len, 4);
2169f1f74dffSPing-Ke Shih 	tlv->type = cpu_to_le16(IEEE80211_RADIOTAP_EHT_USIG);
2170f1f74dffSPing-Ke Shih 	tlv->len = cpu_to_le16(usig_len);
2171f1f74dffSPing-Ke Shih 
2172f1f74dffSPing-Ke Shih 	if (rx_status->bw >= ARRAY_SIZE(rx_status_bw_to_radiotap_eht_usig))
2173f1f74dffSPing-Ke Shih 		return;
2174f1f74dffSPing-Ke Shih 
2175f1f74dffSPing-Ke Shih 	bw = rx_status_bw_to_radiotap_eht_usig[rx_status->bw];
2176f1f74dffSPing-Ke Shih 	if (bw == U8_MAX)
2177f1f74dffSPing-Ke Shih 		return;
2178f1f74dffSPing-Ke Shih 
2179f1f74dffSPing-Ke Shih 	usig = (struct ieee80211_radiotap_eht_usig *)tlv->data;
2180f1f74dffSPing-Ke Shih 	usig->common =
2181f1f74dffSPing-Ke Shih 		le32_encode_bits(1, IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW_KNOWN) |
2182f1f74dffSPing-Ke Shih 		le32_encode_bits(bw, IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW);
2183f1f74dffSPing-Ke Shih }
2184f1f74dffSPing-Ke Shih 
rtw89_core_update_radiotap(struct rtw89_dev * rtwdev,struct sk_buff * skb,struct ieee80211_rx_status * rx_status)218551e8ed4eSPing-Ke Shih static void rtw89_core_update_radiotap(struct rtw89_dev *rtwdev,
218651e8ed4eSPing-Ke Shih 				       struct sk_buff *skb,
218751e8ed4eSPing-Ke Shih 				       struct ieee80211_rx_status *rx_status)
218851e8ed4eSPing-Ke Shih {
218951e8ed4eSPing-Ke Shih 	static const struct ieee80211_radiotap_he known_he = {
219051e8ed4eSPing-Ke Shih 		.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
219185e02a0aSPing-Ke Shih 				     IEEE80211_RADIOTAP_HE_DATA1_CODING_KNOWN |
219285e02a0aSPing-Ke Shih 				     IEEE80211_RADIOTAP_HE_DATA1_STBC_KNOWN |
219351e8ed4eSPing-Ke Shih 				     IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN),
219451e8ed4eSPing-Ke Shih 		.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN),
219551e8ed4eSPing-Ke Shih 	};
219651e8ed4eSPing-Ke Shih 	struct ieee80211_radiotap_he *he;
219751e8ed4eSPing-Ke Shih 
219851e8ed4eSPing-Ke Shih 	if (!(rtwdev->hw->conf.flags & IEEE80211_CONF_MONITOR))
219951e8ed4eSPing-Ke Shih 		return;
220051e8ed4eSPing-Ke Shih 
220151e8ed4eSPing-Ke Shih 	if (rx_status->encoding == RX_ENC_HE) {
220251e8ed4eSPing-Ke Shih 		rx_status->flag |= RX_FLAG_RADIOTAP_HE;
220351e8ed4eSPing-Ke Shih 		he = skb_push(skb, sizeof(*he));
220451e8ed4eSPing-Ke Shih 		*he = known_he;
2205f1f74dffSPing-Ke Shih 	} else if (rx_status->encoding == RX_ENC_EHT) {
2206f1f74dffSPing-Ke Shih 		rtw89_core_update_radiotap_eht(rtwdev, skb, rx_status);
220751e8ed4eSPing-Ke Shih 	}
220851e8ed4eSPing-Ke Shih }
220951e8ed4eSPing-Ke Shih 
rtw89_core_rx_to_mac80211(struct rtw89_dev * rtwdev,struct rtw89_rx_phy_ppdu * phy_ppdu,struct rtw89_rx_desc_info * desc_info,struct sk_buff * skb_ppdu,struct ieee80211_rx_status * rx_status)2210c1ea345dSPing-Ke Shih static void rtw89_core_rx_to_mac80211(struct rtw89_dev *rtwdev,
2211c1ea345dSPing-Ke Shih 				      struct rtw89_rx_phy_ppdu *phy_ppdu,
2212c1ea345dSPing-Ke Shih 				      struct rtw89_rx_desc_info *desc_info,
2213c1ea345dSPing-Ke Shih 				      struct sk_buff *skb_ppdu,
2214c1ea345dSPing-Ke Shih 				      struct ieee80211_rx_status *rx_status)
2215c1ea345dSPing-Ke Shih {
2216c4756d5dSPing-Ke Shih 	struct napi_struct *napi = &rtwdev->napi;
2217c4756d5dSPing-Ke Shih 
2218c4756d5dSPing-Ke Shih 	/* In low power mode, napi isn't scheduled. Receive it to netif. */
22197f3eb217SChristian Marangi 	if (unlikely(!napi_is_scheduled(napi)))
2220c4756d5dSPing-Ke Shih 		napi = NULL;
2221c4756d5dSPing-Ke Shih 
2222c1ea345dSPing-Ke Shih 	rtw89_core_hw_to_sband_rate(rx_status);
2223c1ea345dSPing-Ke Shih 	rtw89_core_rx_stats(rtwdev, phy_ppdu, desc_info, skb_ppdu);
222485e02a0aSPing-Ke Shih 	rtw89_core_update_rx_status_by_ppdu(rtwdev, rx_status, phy_ppdu);
222551e8ed4eSPing-Ke Shih 	rtw89_core_update_radiotap(rtwdev, skb_ppdu, rx_status);
2226c83dcd05SPing-Ke Shih 	/* In low power mode, it does RX in thread context. */
2227c83dcd05SPing-Ke Shih 	local_bh_disable();
2228c4756d5dSPing-Ke Shih 	ieee80211_rx_napi(rtwdev->hw, NULL, skb_ppdu, napi);
2229c83dcd05SPing-Ke Shih 	local_bh_enable();
2230c1ea345dSPing-Ke Shih 	rtwdev->napi_budget_countdown--;
2231c1ea345dSPing-Ke Shih }
2232c1ea345dSPing-Ke Shih 
rtw89_core_rx_pending_skb(struct rtw89_dev * rtwdev,struct rtw89_rx_phy_ppdu * phy_ppdu,struct rtw89_rx_desc_info * desc_info,struct sk_buff * skb)2233e3ec7017SPing-Ke Shih static void rtw89_core_rx_pending_skb(struct rtw89_dev *rtwdev,
2234e3ec7017SPing-Ke Shih 				      struct rtw89_rx_phy_ppdu *phy_ppdu,
2235e3ec7017SPing-Ke Shih 				      struct rtw89_rx_desc_info *desc_info,
2236e3ec7017SPing-Ke Shih 				      struct sk_buff *skb)
2237e3ec7017SPing-Ke Shih {
2238e3ec7017SPing-Ke Shih 	u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
2239e3ec7017SPing-Ke Shih 	int curr = rtwdev->ppdu_sts.curr_rx_ppdu_cnt[band];
2240e3ec7017SPing-Ke Shih 	struct sk_buff *skb_ppdu = NULL, *tmp;
2241e3ec7017SPing-Ke Shih 	struct ieee80211_rx_status *rx_status;
2242e3ec7017SPing-Ke Shih 
2243e3ec7017SPing-Ke Shih 	if (curr > RTW89_MAX_PPDU_CNT)
2244e3ec7017SPing-Ke Shih 		return;
2245e3ec7017SPing-Ke Shih 
2246e3ec7017SPing-Ke Shih 	skb_queue_walk_safe(&rtwdev->ppdu_sts.rx_queue[band], skb_ppdu, tmp) {
2247e3ec7017SPing-Ke Shih 		skb_unlink(skb_ppdu, &rtwdev->ppdu_sts.rx_queue[band]);
2248e3ec7017SPing-Ke Shih 		rx_status = IEEE80211_SKB_RXCB(skb_ppdu);
2249e3ec7017SPing-Ke Shih 		if (rtw89_core_rx_ppdu_match(rtwdev, desc_info, rx_status))
2250e3ec7017SPing-Ke Shih 			rtw89_chip_query_ppdu(rtwdev, phy_ppdu, rx_status);
2251eb4e52b3SPo Hao Huang 		rtw89_correct_cck_chan(rtwdev, rx_status);
2252c1ea345dSPing-Ke Shih 		rtw89_core_rx_to_mac80211(rtwdev, phy_ppdu, desc_info, skb_ppdu, rx_status);
2253e3ec7017SPing-Ke Shih 	}
2254e3ec7017SPing-Ke Shih }
2255e3ec7017SPing-Ke Shih 
rtw89_core_rx_process_ppdu_sts(struct rtw89_dev * rtwdev,struct rtw89_rx_desc_info * desc_info,struct sk_buff * skb)2256e3ec7017SPing-Ke Shih static void rtw89_core_rx_process_ppdu_sts(struct rtw89_dev *rtwdev,
2257e3ec7017SPing-Ke Shih 					   struct rtw89_rx_desc_info *desc_info,
2258e3ec7017SPing-Ke Shih 					   struct sk_buff *skb)
2259e3ec7017SPing-Ke Shih {
2260e3ec7017SPing-Ke Shih 	struct rtw89_rx_phy_ppdu phy_ppdu = {.buf = skb->data, .valid = false,
2261e3ec7017SPing-Ke Shih 					     .len = skb->len,
2262e3ec7017SPing-Ke Shih 					     .to_self = desc_info->addr1_match,
2263eb4e52b3SPo Hao Huang 					     .rate = desc_info->data_rate,
2264e3ec7017SPing-Ke Shih 					     .mac_id = desc_info->mac_id};
2265e3ec7017SPing-Ke Shih 	int ret;
2266e3ec7017SPing-Ke Shih 
2267e343faceSPing-Ke Shih 	if (desc_info->mac_info_valid) {
2268e343faceSPing-Ke Shih 		ret = rtw89_core_rx_process_mac_ppdu(rtwdev, skb, &phy_ppdu);
2269e343faceSPing-Ke Shih 		if (ret)
2270e343faceSPing-Ke Shih 			goto out;
2271e343faceSPing-Ke Shih 	}
2272e343faceSPing-Ke Shih 
2273e3ec7017SPing-Ke Shih 	ret = rtw89_core_rx_process_phy_ppdu(rtwdev, &phy_ppdu);
2274e3ec7017SPing-Ke Shih 	if (ret)
2275e343faceSPing-Ke Shih 		goto out;
2276e3ec7017SPing-Ke Shih 
2277e3ec7017SPing-Ke Shih 	rtw89_core_rx_process_phy_sts(rtwdev, &phy_ppdu);
2278e343faceSPing-Ke Shih 
2279e343faceSPing-Ke Shih out:
2280e3ec7017SPing-Ke Shih 	rtw89_core_rx_pending_skb(rtwdev, &phy_ppdu, desc_info, skb);
2281e3ec7017SPing-Ke Shih 	dev_kfree_skb_any(skb);
2282e3ec7017SPing-Ke Shih }
2283e3ec7017SPing-Ke Shih 
rtw89_core_rx_process_report(struct rtw89_dev * rtwdev,struct rtw89_rx_desc_info * desc_info,struct sk_buff * skb)2284e3ec7017SPing-Ke Shih static void rtw89_core_rx_process_report(struct rtw89_dev *rtwdev,
2285e3ec7017SPing-Ke Shih 					 struct rtw89_rx_desc_info *desc_info,
2286e3ec7017SPing-Ke Shih 					 struct sk_buff *skb)
2287e3ec7017SPing-Ke Shih {
2288e3ec7017SPing-Ke Shih 	switch (desc_info->pkt_type) {
2289e3ec7017SPing-Ke Shih 	case RTW89_CORE_RX_TYPE_C2H:
2290e3ec7017SPing-Ke Shih 		rtw89_fw_c2h_irqsafe(rtwdev, skb);
2291e3ec7017SPing-Ke Shih 		break;
2292e3ec7017SPing-Ke Shih 	case RTW89_CORE_RX_TYPE_PPDU_STAT:
2293e3ec7017SPing-Ke Shih 		rtw89_core_rx_process_ppdu_sts(rtwdev, desc_info, skb);
2294e3ec7017SPing-Ke Shih 		break;
2295e3ec7017SPing-Ke Shih 	default:
2296e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TXRX, "unhandled pkt_type=%d\n",
2297e3ec7017SPing-Ke Shih 			    desc_info->pkt_type);
2298e3ec7017SPing-Ke Shih 		dev_kfree_skb_any(skb);
2299e3ec7017SPing-Ke Shih 		break;
2300e3ec7017SPing-Ke Shih 	}
2301e3ec7017SPing-Ke Shih }
2302e3ec7017SPing-Ke Shih 
rtw89_core_query_rxdesc(struct rtw89_dev * rtwdev,struct rtw89_rx_desc_info * desc_info,u8 * data,u32 data_offset)2303e3ec7017SPing-Ke Shih void rtw89_core_query_rxdesc(struct rtw89_dev *rtwdev,
2304e3ec7017SPing-Ke Shih 			     struct rtw89_rx_desc_info *desc_info,
2305e3ec7017SPing-Ke Shih 			     u8 *data, u32 data_offset)
2306e3ec7017SPing-Ke Shih {
230784fc6999SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
2308e3ec7017SPing-Ke Shih 	struct rtw89_rxdesc_short *rxd_s;
2309e3ec7017SPing-Ke Shih 	struct rtw89_rxdesc_long *rxd_l;
2310e3ec7017SPing-Ke Shih 	u8 shift_len, drv_info_len;
2311e3ec7017SPing-Ke Shih 
2312e3ec7017SPing-Ke Shih 	rxd_s = (struct rtw89_rxdesc_short *)(data + data_offset);
2313c26700d2SPing-Ke Shih 	desc_info->pkt_size = le32_get_bits(rxd_s->dword0, AX_RXD_RPKT_LEN_MASK);
2314c26700d2SPing-Ke Shih 	desc_info->drv_info_size = le32_get_bits(rxd_s->dword0, AX_RXD_DRV_INFO_SIZE_MASK);
2315c26700d2SPing-Ke Shih 	desc_info->long_rxdesc = le32_get_bits(rxd_s->dword0,  AX_RXD_LONG_RXD);
2316c26700d2SPing-Ke Shih 	desc_info->pkt_type = le32_get_bits(rxd_s->dword0,  AX_RXD_RPKT_TYPE_MASK);
2317c26700d2SPing-Ke Shih 	desc_info->mac_info_valid = le32_get_bits(rxd_s->dword0, AX_RXD_MAC_INFO_VLD);
231884fc6999SPing-Ke Shih 	if (chip->chip_id == RTL8852C)
2319c26700d2SPing-Ke Shih 		desc_info->bw = le32_get_bits(rxd_s->dword1, AX_RXD_BW_v1_MASK);
232084fc6999SPing-Ke Shih 	else
2321c26700d2SPing-Ke Shih 		desc_info->bw = le32_get_bits(rxd_s->dword1, AX_RXD_BW_MASK);
2322c26700d2SPing-Ke Shih 	desc_info->data_rate = le32_get_bits(rxd_s->dword1, AX_RXD_RX_DATARATE_MASK);
2323c26700d2SPing-Ke Shih 	desc_info->gi_ltf = le32_get_bits(rxd_s->dword1, AX_RXD_RX_GI_LTF_MASK);
2324c26700d2SPing-Ke Shih 	desc_info->user_id = le32_get_bits(rxd_s->dword1, AX_RXD_USER_ID_MASK);
2325c26700d2SPing-Ke Shih 	desc_info->sr_en = le32_get_bits(rxd_s->dword1, AX_RXD_SR_EN);
2326c26700d2SPing-Ke Shih 	desc_info->ppdu_cnt = le32_get_bits(rxd_s->dword1, AX_RXD_PPDU_CNT_MASK);
2327c26700d2SPing-Ke Shih 	desc_info->ppdu_type = le32_get_bits(rxd_s->dword1, AX_RXD_PPDU_TYPE_MASK);
2328c26700d2SPing-Ke Shih 	desc_info->free_run_cnt = le32_get_bits(rxd_s->dword2, AX_RXD_FREERUN_CNT_MASK);
2329c26700d2SPing-Ke Shih 	desc_info->icv_err = le32_get_bits(rxd_s->dword3, AX_RXD_ICV_ERR);
2330c26700d2SPing-Ke Shih 	desc_info->crc32_err = le32_get_bits(rxd_s->dword3, AX_RXD_CRC32_ERR);
2331c26700d2SPing-Ke Shih 	desc_info->hw_dec = le32_get_bits(rxd_s->dword3, AX_RXD_HW_DEC);
2332c26700d2SPing-Ke Shih 	desc_info->sw_dec = le32_get_bits(rxd_s->dword3, AX_RXD_SW_DEC);
2333c26700d2SPing-Ke Shih 	desc_info->addr1_match = le32_get_bits(rxd_s->dword3, AX_RXD_A1_MATCH);
2334e3ec7017SPing-Ke Shih 
2335e3ec7017SPing-Ke Shih 	shift_len = desc_info->shift << 1; /* 2-byte unit */
2336e3ec7017SPing-Ke Shih 	drv_info_len = desc_info->drv_info_size << 3; /* 8-byte unit */
2337e3ec7017SPing-Ke Shih 	desc_info->offset = data_offset + shift_len + drv_info_len;
2338de9f9338SPing-Ke Shih 	if (desc_info->long_rxdesc)
2339de9f9338SPing-Ke Shih 		desc_info->rxd_len = sizeof(struct rtw89_rxdesc_long);
2340de9f9338SPing-Ke Shih 	else
2341de9f9338SPing-Ke Shih 		desc_info->rxd_len = sizeof(struct rtw89_rxdesc_short);
2342e3ec7017SPing-Ke Shih 	desc_info->ready = true;
2343e3ec7017SPing-Ke Shih 
2344e3ec7017SPing-Ke Shih 	if (!desc_info->long_rxdesc)
2345e3ec7017SPing-Ke Shih 		return;
2346e3ec7017SPing-Ke Shih 
2347e3ec7017SPing-Ke Shih 	rxd_l = (struct rtw89_rxdesc_long *)(data + data_offset);
2348c26700d2SPing-Ke Shih 	desc_info->frame_type = le32_get_bits(rxd_l->dword4, AX_RXD_TYPE_MASK);
2349c26700d2SPing-Ke Shih 	desc_info->addr_cam_valid = le32_get_bits(rxd_l->dword5, AX_RXD_ADDR_CAM_VLD);
2350c26700d2SPing-Ke Shih 	desc_info->addr_cam_id = le32_get_bits(rxd_l->dword5, AX_RXD_ADDR_CAM_MASK);
2351c26700d2SPing-Ke Shih 	desc_info->sec_cam_id = le32_get_bits(rxd_l->dword5, AX_RXD_SEC_CAM_IDX_MASK);
2352c26700d2SPing-Ke Shih 	desc_info->mac_id = le32_get_bits(rxd_l->dword5, AX_RXD_MAC_ID_MASK);
2353c26700d2SPing-Ke Shih 	desc_info->rx_pl_id = le32_get_bits(rxd_l->dword5, AX_RXD_RX_PL_ID_MASK);
2354e3ec7017SPing-Ke Shih }
2355e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_query_rxdesc);
2356e3ec7017SPing-Ke Shih 
rtw89_core_query_rxdesc_v2(struct rtw89_dev * rtwdev,struct rtw89_rx_desc_info * desc_info,u8 * data,u32 data_offset)2357a1cb73f2SPing-Ke Shih void rtw89_core_query_rxdesc_v2(struct rtw89_dev *rtwdev,
2358a1cb73f2SPing-Ke Shih 				struct rtw89_rx_desc_info *desc_info,
2359a1cb73f2SPing-Ke Shih 				u8 *data, u32 data_offset)
2360a1cb73f2SPing-Ke Shih {
2361a1cb73f2SPing-Ke Shih 	struct rtw89_rxdesc_short_v2 *rxd_s;
2362a1cb73f2SPing-Ke Shih 	struct rtw89_rxdesc_long_v2 *rxd_l;
2363a1cb73f2SPing-Ke Shih 	u16 shift_len, drv_info_len, phy_rtp_len, hdr_cnv_len;
2364a1cb73f2SPing-Ke Shih 
2365a1cb73f2SPing-Ke Shih 	rxd_s = (struct rtw89_rxdesc_short_v2 *)(data + data_offset);
2366a1cb73f2SPing-Ke Shih 
2367a1cb73f2SPing-Ke Shih 	desc_info->pkt_size = le32_get_bits(rxd_s->dword0, BE_RXD_RPKT_LEN_MASK);
2368a1cb73f2SPing-Ke Shih 	desc_info->drv_info_size = le32_get_bits(rxd_s->dword0, BE_RXD_DRV_INFO_SZ_MASK);
2369a1cb73f2SPing-Ke Shih 	desc_info->phy_rpt_size = le32_get_bits(rxd_s->dword0, BE_RXD_PHY_RPT_SZ_MASK);
2370a1cb73f2SPing-Ke Shih 	desc_info->hdr_cnv_size = le32_get_bits(rxd_s->dword0, BE_RXD_HDR_CNV_SZ_MASK);
2371a1cb73f2SPing-Ke Shih 	desc_info->shift = le32_get_bits(rxd_s->dword0, BE_RXD_SHIFT_MASK);
2372a1cb73f2SPing-Ke Shih 	desc_info->long_rxdesc = le32_get_bits(rxd_s->dword0, BE_RXD_LONG_RXD);
2373a1cb73f2SPing-Ke Shih 	desc_info->pkt_type = le32_get_bits(rxd_s->dword0, BE_RXD_RPKT_TYPE_MASK);
2374a1cb73f2SPing-Ke Shih 	if (desc_info->pkt_type == RTW89_CORE_RX_TYPE_PPDU_STAT)
2375a1cb73f2SPing-Ke Shih 		desc_info->mac_info_valid = true;
2376a1cb73f2SPing-Ke Shih 
2377a1cb73f2SPing-Ke Shih 	desc_info->frame_type = le32_get_bits(rxd_s->dword2, BE_RXD_TYPE_MASK);
2378a1cb73f2SPing-Ke Shih 	desc_info->mac_id = le32_get_bits(rxd_s->dword2, BE_RXD_MAC_ID_MASK);
2379a1cb73f2SPing-Ke Shih 	desc_info->addr_cam_valid = le32_get_bits(rxd_s->dword2, BE_RXD_ADDR_CAM_VLD);
2380a1cb73f2SPing-Ke Shih 
2381a1cb73f2SPing-Ke Shih 	desc_info->icv_err = le32_get_bits(rxd_s->dword3, BE_RXD_ICV_ERR);
2382a1cb73f2SPing-Ke Shih 	desc_info->crc32_err = le32_get_bits(rxd_s->dword3, BE_RXD_CRC32_ERR);
2383a1cb73f2SPing-Ke Shih 	desc_info->hw_dec = le32_get_bits(rxd_s->dword3, BE_RXD_HW_DEC);
2384a1cb73f2SPing-Ke Shih 	desc_info->sw_dec = le32_get_bits(rxd_s->dword3, BE_RXD_SW_DEC);
2385a1cb73f2SPing-Ke Shih 	desc_info->addr1_match = le32_get_bits(rxd_s->dword3, BE_RXD_A1_MATCH);
2386a1cb73f2SPing-Ke Shih 
2387a1cb73f2SPing-Ke Shih 	desc_info->bw = le32_get_bits(rxd_s->dword4, BE_RXD_BW_MASK);
2388a1cb73f2SPing-Ke Shih 	desc_info->data_rate = le32_get_bits(rxd_s->dword4, BE_RXD_RX_DATARATE_MASK);
2389a1cb73f2SPing-Ke Shih 	desc_info->gi_ltf = le32_get_bits(rxd_s->dword4, BE_RXD_RX_GI_LTF_MASK);
2390a1cb73f2SPing-Ke Shih 	desc_info->ppdu_cnt = le32_get_bits(rxd_s->dword4, BE_RXD_PPDU_CNT_MASK);
2391a1cb73f2SPing-Ke Shih 	desc_info->ppdu_type = le32_get_bits(rxd_s->dword4, BE_RXD_PPDU_TYPE_MASK);
2392a1cb73f2SPing-Ke Shih 
2393a1cb73f2SPing-Ke Shih 	desc_info->free_run_cnt = le32_to_cpu(rxd_s->dword5);
2394a1cb73f2SPing-Ke Shih 
2395a1cb73f2SPing-Ke Shih 	shift_len = desc_info->shift << 1; /* 2-byte unit */
2396a1cb73f2SPing-Ke Shih 	drv_info_len = desc_info->drv_info_size << 3; /* 8-byte unit */
2397a1cb73f2SPing-Ke Shih 	phy_rtp_len = desc_info->phy_rpt_size << 3; /* 8-byte unit */
2398a1cb73f2SPing-Ke Shih 	hdr_cnv_len = desc_info->hdr_cnv_size << 4; /* 16-byte unit */
2399a1cb73f2SPing-Ke Shih 	desc_info->offset = data_offset + shift_len + drv_info_len +
2400a1cb73f2SPing-Ke Shih 			    phy_rtp_len + hdr_cnv_len;
2401a1cb73f2SPing-Ke Shih 
2402a1cb73f2SPing-Ke Shih 	if (desc_info->long_rxdesc)
2403a1cb73f2SPing-Ke Shih 		desc_info->rxd_len = sizeof(struct rtw89_rxdesc_long_v2);
2404a1cb73f2SPing-Ke Shih 	else
2405a1cb73f2SPing-Ke Shih 		desc_info->rxd_len = sizeof(struct rtw89_rxdesc_short_v2);
2406a1cb73f2SPing-Ke Shih 	desc_info->ready = true;
2407a1cb73f2SPing-Ke Shih 
2408a1cb73f2SPing-Ke Shih 	if (!desc_info->long_rxdesc)
2409a1cb73f2SPing-Ke Shih 		return;
2410a1cb73f2SPing-Ke Shih 
2411a1cb73f2SPing-Ke Shih 	rxd_l = (struct rtw89_rxdesc_long_v2 *)(data + data_offset);
2412a1cb73f2SPing-Ke Shih 
2413a1cb73f2SPing-Ke Shih 	desc_info->sr_en = le32_get_bits(rxd_l->dword6, BE_RXD_SR_EN);
2414a1cb73f2SPing-Ke Shih 	desc_info->user_id = le32_get_bits(rxd_l->dword6, BE_RXD_USER_ID_MASK);
2415a1cb73f2SPing-Ke Shih 	desc_info->addr_cam_id = le32_get_bits(rxd_l->dword6, BE_RXD_ADDR_CAM_MASK);
2416a1cb73f2SPing-Ke Shih 	desc_info->sec_cam_id = le32_get_bits(rxd_l->dword6, BE_RXD_SEC_CAM_IDX_MASK);
2417a1cb73f2SPing-Ke Shih 
2418a1cb73f2SPing-Ke Shih 	desc_info->rx_pl_id = le32_get_bits(rxd_l->dword7, BE_RXD_RX_PL_ID_MASK);
2419a1cb73f2SPing-Ke Shih }
2420a1cb73f2SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_query_rxdesc_v2);
2421a1cb73f2SPing-Ke Shih 
2422e3ec7017SPing-Ke Shih struct rtw89_core_iter_rx_status {
2423e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
2424e3ec7017SPing-Ke Shih 	struct ieee80211_rx_status *rx_status;
2425e3ec7017SPing-Ke Shih 	struct rtw89_rx_desc_info *desc_info;
2426e3ec7017SPing-Ke Shih 	u8 mac_id;
2427e3ec7017SPing-Ke Shih };
2428e3ec7017SPing-Ke Shih 
2429e3ec7017SPing-Ke Shih static
rtw89_core_stats_sta_rx_status_iter(void * data,struct ieee80211_sta * sta)2430e3ec7017SPing-Ke Shih void rtw89_core_stats_sta_rx_status_iter(void *data, struct ieee80211_sta *sta)
2431e3ec7017SPing-Ke Shih {
2432e3ec7017SPing-Ke Shih 	struct rtw89_core_iter_rx_status *iter_data =
2433e3ec7017SPing-Ke Shih 				(struct rtw89_core_iter_rx_status *)data;
2434e3ec7017SPing-Ke Shih 	struct ieee80211_rx_status *rx_status = iter_data->rx_status;
2435e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2436e3ec7017SPing-Ke Shih 	struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
2437e3ec7017SPing-Ke Shih 	u8 mac_id = iter_data->mac_id;
2438e3ec7017SPing-Ke Shih 
2439e3ec7017SPing-Ke Shih 	if (mac_id != rtwsta->mac_id)
2440e3ec7017SPing-Ke Shih 		return;
2441e3ec7017SPing-Ke Shih 
2442e3ec7017SPing-Ke Shih 	rtwsta->rx_status = *rx_status;
2443e3ec7017SPing-Ke Shih 	rtwsta->rx_hw_rate = desc_info->data_rate;
2444e3ec7017SPing-Ke Shih }
2445e3ec7017SPing-Ke Shih 
rtw89_core_stats_sta_rx_status(struct rtw89_dev * rtwdev,struct rtw89_rx_desc_info * desc_info,struct ieee80211_rx_status * rx_status)2446e3ec7017SPing-Ke Shih static void rtw89_core_stats_sta_rx_status(struct rtw89_dev *rtwdev,
2447e3ec7017SPing-Ke Shih 					   struct rtw89_rx_desc_info *desc_info,
2448e3ec7017SPing-Ke Shih 					   struct ieee80211_rx_status *rx_status)
2449e3ec7017SPing-Ke Shih {
2450e3ec7017SPing-Ke Shih 	struct rtw89_core_iter_rx_status iter_data;
2451e3ec7017SPing-Ke Shih 
2452e3ec7017SPing-Ke Shih 	if (!desc_info->addr1_match || !desc_info->long_rxdesc)
2453e3ec7017SPing-Ke Shih 		return;
2454e3ec7017SPing-Ke Shih 
2455e3ec7017SPing-Ke Shih 	if (desc_info->frame_type != RTW89_RX_TYPE_DATA)
2456e3ec7017SPing-Ke Shih 		return;
2457e3ec7017SPing-Ke Shih 
2458e3ec7017SPing-Ke Shih 	iter_data.rtwdev = rtwdev;
2459e3ec7017SPing-Ke Shih 	iter_data.rx_status = rx_status;
2460e3ec7017SPing-Ke Shih 	iter_data.desc_info = desc_info;
2461e3ec7017SPing-Ke Shih 	iter_data.mac_id = desc_info->mac_id;
2462e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
2463e3ec7017SPing-Ke Shih 					  rtw89_core_stats_sta_rx_status_iter,
2464e3ec7017SPing-Ke Shih 					  &iter_data);
2465e3ec7017SPing-Ke Shih }
2466e3ec7017SPing-Ke Shih 
rtw89_core_update_rx_status(struct rtw89_dev * rtwdev,struct rtw89_rx_desc_info * desc_info,struct ieee80211_rx_status * rx_status)2467e3ec7017SPing-Ke Shih static void rtw89_core_update_rx_status(struct rtw89_dev *rtwdev,
2468e3ec7017SPing-Ke Shih 					struct rtw89_rx_desc_info *desc_info,
2469e3ec7017SPing-Ke Shih 					struct ieee80211_rx_status *rx_status)
2470e3ec7017SPing-Ke Shih {
2471494399b2SZong-Zhe Yang 	const struct cfg80211_chan_def *chandef =
2472583e998eSZong-Zhe Yang 		rtw89_chandef_get(rtwdev, RTW89_CHANCTX_0);
2473e3ec7017SPing-Ke Shih 	u16 data_rate;
2474e3ec7017SPing-Ke Shih 	u8 data_rate_mode;
2475786a93c9SPing-Ke Shih 	bool eht = false;
2476786a93c9SPing-Ke Shih 	u8 gi;
2477e3ec7017SPing-Ke Shih 
2478e3ec7017SPing-Ke Shih 	/* currently using single PHY */
2479494399b2SZong-Zhe Yang 	rx_status->freq = chandef->chan->center_freq;
2480494399b2SZong-Zhe Yang 	rx_status->band = chandef->chan->band;
2481e3ec7017SPing-Ke Shih 
248211fe4ccdSZong-Zhe Yang 	if (rtwdev->scanning &&
248311fe4ccdSZong-Zhe Yang 	    RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw)) {
2484ad3dc722SZong-Zhe Yang 		const struct rtw89_chan *cur = rtw89_scan_chan_get(rtwdev);
2485cbb145b9SZong-Zhe Yang 		u8 chan = cur->primary_channel;
2486cbb145b9SZong-Zhe Yang 		u8 band = cur->band_type;
2487a06d2dd7SZong-Zhe Yang 		enum nl80211_band nl_band;
2488a06d2dd7SZong-Zhe Yang 
2489a06d2dd7SZong-Zhe Yang 		nl_band = rtw89_hw_to_nl80211_band(band);
2490a06d2dd7SZong-Zhe Yang 		rx_status->freq = ieee80211_channel_to_frequency(chan, nl_band);
2491a06d2dd7SZong-Zhe Yang 		rx_status->band = nl_band;
249289590777SPo Hao Huang 	}
249389590777SPo Hao Huang 
2494e3ec7017SPing-Ke Shih 	if (desc_info->icv_err || desc_info->crc32_err)
2495e3ec7017SPing-Ke Shih 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
2496e3ec7017SPing-Ke Shih 
2497e3ec7017SPing-Ke Shih 	if (desc_info->hw_dec &&
2498e3ec7017SPing-Ke Shih 	    !(desc_info->sw_dec || desc_info->icv_err))
2499e3ec7017SPing-Ke Shih 		rx_status->flag |= RX_FLAG_DECRYPTED;
2500e3ec7017SPing-Ke Shih 
2501167044afSPing-Ke Shih 	rx_status->bw = rtw89_hw_to_rate_info_bw(desc_info->bw);
2502e3ec7017SPing-Ke Shih 
2503e3ec7017SPing-Ke Shih 	data_rate = desc_info->data_rate;
2504023d2f14SPing-Ke Shih 	data_rate_mode = rtw89_get_data_rate_mode(rtwdev, data_rate);
2505e3ec7017SPing-Ke Shih 	if (data_rate_mode == DATA_RATE_MODE_NON_HT) {
2506e3ec7017SPing-Ke Shih 		rx_status->encoding = RX_ENC_LEGACY;
2507023d2f14SPing-Ke Shih 		rx_status->rate_idx = rtw89_get_data_not_ht_idx(rtwdev, data_rate);
2508eb4e52b3SPo Hao Huang 		/* convert rate_idx after we get the correct band */
2509e3ec7017SPing-Ke Shih 	} else if (data_rate_mode == DATA_RATE_MODE_HT) {
2510e3ec7017SPing-Ke Shih 		rx_status->encoding = RX_ENC_HT;
2511023d2f14SPing-Ke Shih 		rx_status->rate_idx = rtw89_get_data_ht_mcs(rtwdev, data_rate);
2512e3ec7017SPing-Ke Shih 		if (desc_info->gi_ltf)
2513e3ec7017SPing-Ke Shih 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2514e3ec7017SPing-Ke Shih 	} else if (data_rate_mode == DATA_RATE_MODE_VHT) {
2515e3ec7017SPing-Ke Shih 		rx_status->encoding = RX_ENC_VHT;
2516023d2f14SPing-Ke Shih 		rx_status->rate_idx = rtw89_get_data_mcs(rtwdev, data_rate);
2517023d2f14SPing-Ke Shih 		rx_status->nss = rtw89_get_data_nss(rtwdev, data_rate) + 1;
2518e3ec7017SPing-Ke Shih 		if (desc_info->gi_ltf)
2519e3ec7017SPing-Ke Shih 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2520e3ec7017SPing-Ke Shih 	} else if (data_rate_mode == DATA_RATE_MODE_HE) {
2521e3ec7017SPing-Ke Shih 		rx_status->encoding = RX_ENC_HE;
2522023d2f14SPing-Ke Shih 		rx_status->rate_idx = rtw89_get_data_mcs(rtwdev, data_rate);
2523023d2f14SPing-Ke Shih 		rx_status->nss = rtw89_get_data_nss(rtwdev, data_rate) + 1;
2524786a93c9SPing-Ke Shih 	} else if (data_rate_mode == DATA_RATE_MODE_EHT) {
2525786a93c9SPing-Ke Shih 		rx_status->encoding = RX_ENC_EHT;
2526786a93c9SPing-Ke Shih 		rx_status->rate_idx = rtw89_get_data_mcs(rtwdev, data_rate);
2527786a93c9SPing-Ke Shih 		rx_status->nss = rtw89_get_data_nss(rtwdev, data_rate) + 1;
2528786a93c9SPing-Ke Shih 		eht = true;
2529e3ec7017SPing-Ke Shih 	} else {
2530e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "invalid RX rate mode %d\n", data_rate_mode);
2531e3ec7017SPing-Ke Shih 	}
2532e3ec7017SPing-Ke Shih 
2533e3ec7017SPing-Ke Shih 	/* he_gi is used to match ppdu, so we always fill it. */
2534786a93c9SPing-Ke Shih 	gi = rtw89_rxdesc_to_nl_he_eht_gi(rtwdev, desc_info->gi_ltf, true, eht);
2535786a93c9SPing-Ke Shih 	if (eht)
2536786a93c9SPing-Ke Shih 		rx_status->eht.gi = gi;
2537786a93c9SPing-Ke Shih 	else
2538786a93c9SPing-Ke Shih 		rx_status->he_gi = gi;
2539e3ec7017SPing-Ke Shih 	rx_status->flag |= RX_FLAG_MACTIME_START;
2540e3ec7017SPing-Ke Shih 	rx_status->mactime = desc_info->free_run_cnt;
2541e3ec7017SPing-Ke Shih 
2542e3ec7017SPing-Ke Shih 	rtw89_core_stats_sta_rx_status(rtwdev, desc_info, rx_status);
2543e3ec7017SPing-Ke Shih }
2544e3ec7017SPing-Ke Shih 
rtw89_update_ps_mode(struct rtw89_dev * rtwdev)2545e3ec7017SPing-Ke Shih static enum rtw89_ps_mode rtw89_update_ps_mode(struct rtw89_dev *rtwdev)
2546e3ec7017SPing-Ke Shih {
2547e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
2548e3ec7017SPing-Ke Shih 
2549183c8effSChin-Yen Lee 	if (rtw89_disable_ps_mode || !chip->ps_mode_supported ||
2550183c8effSChin-Yen Lee 	    RTW89_CHK_FW_FEATURE(NO_DEEP_PS, &rtwdev->fw))
2551e3ec7017SPing-Ke Shih 		return RTW89_PS_MODE_NONE;
2552e3ec7017SPing-Ke Shih 
2553e5624482SPing-Ke Shih 	if ((chip->ps_mode_supported & BIT(RTW89_PS_MODE_PWR_GATED)) &&
2554e5624482SPing-Ke Shih 	    !RTW89_CHK_FW_FEATURE(NO_LPS_PG, &rtwdev->fw))
2555e3ec7017SPing-Ke Shih 		return RTW89_PS_MODE_PWR_GATED;
2556e3ec7017SPing-Ke Shih 
2557e3ec7017SPing-Ke Shih 	if (chip->ps_mode_supported & BIT(RTW89_PS_MODE_CLK_GATED))
2558e3ec7017SPing-Ke Shih 		return RTW89_PS_MODE_CLK_GATED;
2559e3ec7017SPing-Ke Shih 
2560e3ec7017SPing-Ke Shih 	if (chip->ps_mode_supported & BIT(RTW89_PS_MODE_RFOFF))
2561e3ec7017SPing-Ke Shih 		return RTW89_PS_MODE_RFOFF;
2562e3ec7017SPing-Ke Shih 
2563e3ec7017SPing-Ke Shih 	return RTW89_PS_MODE_NONE;
2564e3ec7017SPing-Ke Shih }
2565e3ec7017SPing-Ke Shih 
rtw89_core_flush_ppdu_rx_queue(struct rtw89_dev * rtwdev,struct rtw89_rx_desc_info * desc_info)2566e3ec7017SPing-Ke Shih static void rtw89_core_flush_ppdu_rx_queue(struct rtw89_dev *rtwdev,
2567e3ec7017SPing-Ke Shih 					   struct rtw89_rx_desc_info *desc_info)
2568e3ec7017SPing-Ke Shih {
2569e3ec7017SPing-Ke Shih 	struct rtw89_ppdu_sts_info *ppdu_sts = &rtwdev->ppdu_sts;
2570e3ec7017SPing-Ke Shih 	u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
2571eb4e52b3SPo Hao Huang 	struct ieee80211_rx_status *rx_status;
2572e3ec7017SPing-Ke Shih 	struct sk_buff *skb_ppdu, *tmp;
2573e3ec7017SPing-Ke Shih 
2574e3ec7017SPing-Ke Shih 	skb_queue_walk_safe(&ppdu_sts->rx_queue[band], skb_ppdu, tmp) {
2575e3ec7017SPing-Ke Shih 		skb_unlink(skb_ppdu, &ppdu_sts->rx_queue[band]);
2576eb4e52b3SPo Hao Huang 		rx_status = IEEE80211_SKB_RXCB(skb_ppdu);
2577c1ea345dSPing-Ke Shih 		rtw89_core_rx_to_mac80211(rtwdev, NULL, desc_info, skb_ppdu, rx_status);
2578e3ec7017SPing-Ke Shih 	}
2579e3ec7017SPing-Ke Shih }
2580e3ec7017SPing-Ke Shih 
rtw89_core_rx(struct rtw89_dev * rtwdev,struct rtw89_rx_desc_info * desc_info,struct sk_buff * skb)2581e3ec7017SPing-Ke Shih void rtw89_core_rx(struct rtw89_dev *rtwdev,
2582e3ec7017SPing-Ke Shih 		   struct rtw89_rx_desc_info *desc_info,
2583e3ec7017SPing-Ke Shih 		   struct sk_buff *skb)
2584e3ec7017SPing-Ke Shih {
2585e3ec7017SPing-Ke Shih 	struct ieee80211_rx_status *rx_status;
2586e3ec7017SPing-Ke Shih 	struct rtw89_ppdu_sts_info *ppdu_sts = &rtwdev->ppdu_sts;
2587e3ec7017SPing-Ke Shih 	u8 ppdu_cnt = desc_info->ppdu_cnt;
2588e3ec7017SPing-Ke Shih 	u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
2589e3ec7017SPing-Ke Shih 
2590e3ec7017SPing-Ke Shih 	if (desc_info->pkt_type != RTW89_CORE_RX_TYPE_WIFI) {
2591e3ec7017SPing-Ke Shih 		rtw89_core_rx_process_report(rtwdev, desc_info, skb);
2592e3ec7017SPing-Ke Shih 		return;
2593e3ec7017SPing-Ke Shih 	}
2594e3ec7017SPing-Ke Shih 
2595e3ec7017SPing-Ke Shih 	if (ppdu_sts->curr_rx_ppdu_cnt[band] != ppdu_cnt) {
2596e3ec7017SPing-Ke Shih 		rtw89_core_flush_ppdu_rx_queue(rtwdev, desc_info);
2597e3ec7017SPing-Ke Shih 		ppdu_sts->curr_rx_ppdu_cnt[band] = ppdu_cnt;
2598e3ec7017SPing-Ke Shih 	}
2599e3ec7017SPing-Ke Shih 
2600e3ec7017SPing-Ke Shih 	rx_status = IEEE80211_SKB_RXCB(skb);
2601e3ec7017SPing-Ke Shih 	memset(rx_status, 0, sizeof(*rx_status));
2602e3ec7017SPing-Ke Shih 	rtw89_core_update_rx_status(rtwdev, desc_info, rx_status);
2603e3ec7017SPing-Ke Shih 	if (desc_info->long_rxdesc &&
2604c1ea345dSPing-Ke Shih 	    BIT(desc_info->frame_type) & PPDU_FILTER_BITMAP)
2605e3ec7017SPing-Ke Shih 		skb_queue_tail(&ppdu_sts->rx_queue[band], skb);
2606c1ea345dSPing-Ke Shih 	else
2607c1ea345dSPing-Ke Shih 		rtw89_core_rx_to_mac80211(rtwdev, NULL, desc_info, skb, rx_status);
2608e3ec7017SPing-Ke Shih }
2609e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_rx);
2610e3ec7017SPing-Ke Shih 
rtw89_core_napi_start(struct rtw89_dev * rtwdev)2611e3ec7017SPing-Ke Shih void rtw89_core_napi_start(struct rtw89_dev *rtwdev)
2612e3ec7017SPing-Ke Shih {
2613e3ec7017SPing-Ke Shih 	if (test_and_set_bit(RTW89_FLAG_NAPI_RUNNING, rtwdev->flags))
2614e3ec7017SPing-Ke Shih 		return;
2615e3ec7017SPing-Ke Shih 
2616e3ec7017SPing-Ke Shih 	napi_enable(&rtwdev->napi);
2617e3ec7017SPing-Ke Shih }
2618e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_napi_start);
2619e3ec7017SPing-Ke Shih 
rtw89_core_napi_stop(struct rtw89_dev * rtwdev)2620e3ec7017SPing-Ke Shih void rtw89_core_napi_stop(struct rtw89_dev *rtwdev)
2621e3ec7017SPing-Ke Shih {
2622e3ec7017SPing-Ke Shih 	if (!test_and_clear_bit(RTW89_FLAG_NAPI_RUNNING, rtwdev->flags))
2623e3ec7017SPing-Ke Shih 		return;
2624e3ec7017SPing-Ke Shih 
2625e3ec7017SPing-Ke Shih 	napi_synchronize(&rtwdev->napi);
2626e3ec7017SPing-Ke Shih 	napi_disable(&rtwdev->napi);
2627e3ec7017SPing-Ke Shih }
2628e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_napi_stop);
2629e3ec7017SPing-Ke Shih 
rtw89_core_napi_init(struct rtw89_dev * rtwdev)26304c9aa94aSBreno Leitao int rtw89_core_napi_init(struct rtw89_dev *rtwdev)
2631e3ec7017SPing-Ke Shih {
26324c9aa94aSBreno Leitao 	rtwdev->netdev = alloc_netdev_dummy(0);
26334c9aa94aSBreno Leitao 	if (!rtwdev->netdev)
26344c9aa94aSBreno Leitao 		return -ENOMEM;
26354c9aa94aSBreno Leitao 
26364c9aa94aSBreno Leitao 	netif_napi_add(rtwdev->netdev, &rtwdev->napi,
2637b48b89f9SJakub Kicinski 		       rtwdev->hci.ops->napi_poll);
26384c9aa94aSBreno Leitao 	return 0;
2639e3ec7017SPing-Ke Shih }
2640e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_napi_init);
2641e3ec7017SPing-Ke Shih 
rtw89_core_napi_deinit(struct rtw89_dev * rtwdev)2642e3ec7017SPing-Ke Shih void rtw89_core_napi_deinit(struct rtw89_dev *rtwdev)
2643e3ec7017SPing-Ke Shih {
2644e3ec7017SPing-Ke Shih 	rtw89_core_napi_stop(rtwdev);
2645e3ec7017SPing-Ke Shih 	netif_napi_del(&rtwdev->napi);
26464c9aa94aSBreno Leitao 	free_netdev(rtwdev->netdev);
2647e3ec7017SPing-Ke Shih }
2648e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_napi_deinit);
2649e3ec7017SPing-Ke Shih 
rtw89_core_ba_work(struct work_struct * work)2650e3ec7017SPing-Ke Shih static void rtw89_core_ba_work(struct work_struct *work)
2651e3ec7017SPing-Ke Shih {
2652e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev =
2653e3ec7017SPing-Ke Shih 		container_of(work, struct rtw89_dev, ba_work);
2654e3ec7017SPing-Ke Shih 	struct rtw89_txq *rtwtxq, *tmp;
2655e3ec7017SPing-Ke Shih 	int ret;
2656e3ec7017SPing-Ke Shih 
2657e3ec7017SPing-Ke Shih 	spin_lock_bh(&rtwdev->ba_lock);
2658e3ec7017SPing-Ke Shih 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->ba_list, list) {
2659e3ec7017SPing-Ke Shih 		struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2660e3ec7017SPing-Ke Shih 		struct ieee80211_sta *sta = txq->sta;
26615d44f067SPing-Ke Shih 		struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL;
2662e3ec7017SPing-Ke Shih 		u8 tid = txq->tid;
2663e3ec7017SPing-Ke Shih 
2664e3ec7017SPing-Ke Shih 		if (!sta) {
2665e3ec7017SPing-Ke Shih 			rtw89_warn(rtwdev, "cannot start BA without sta\n");
2666e3ec7017SPing-Ke Shih 			goto skip_ba_work;
2667e3ec7017SPing-Ke Shih 		}
2668e3ec7017SPing-Ke Shih 
2669e3ec7017SPing-Ke Shih 		if (rtwsta->disassoc) {
2670e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TXRX,
2671e3ec7017SPing-Ke Shih 				    "cannot start BA with disassoc sta\n");
2672e3ec7017SPing-Ke Shih 			goto skip_ba_work;
2673e3ec7017SPing-Ke Shih 		}
2674e3ec7017SPing-Ke Shih 
2675e3ec7017SPing-Ke Shih 		ret = ieee80211_start_tx_ba_session(sta, tid, 0);
2676e3ec7017SPing-Ke Shih 		if (ret) {
2677e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TXRX,
2678e3ec7017SPing-Ke Shih 				    "failed to setup BA session for %pM:%2d: %d\n",
2679e3ec7017SPing-Ke Shih 				    sta->addr, tid, ret);
2680e3ec7017SPing-Ke Shih 			if (ret == -EINVAL)
2681e3ec7017SPing-Ke Shih 				set_bit(RTW89_TXQ_F_BLOCK_BA, &rtwtxq->flags);
2682e3ec7017SPing-Ke Shih 		}
2683e3ec7017SPing-Ke Shih skip_ba_work:
2684e3ec7017SPing-Ke Shih 		list_del_init(&rtwtxq->list);
2685e3ec7017SPing-Ke Shih 	}
2686e3ec7017SPing-Ke Shih 	spin_unlock_bh(&rtwdev->ba_lock);
2687e3ec7017SPing-Ke Shih }
2688e3ec7017SPing-Ke Shih 
rtw89_core_free_sta_pending_ba(struct rtw89_dev * rtwdev,struct ieee80211_sta * sta)2689e3ec7017SPing-Ke Shih static void rtw89_core_free_sta_pending_ba(struct rtw89_dev *rtwdev,
2690e3ec7017SPing-Ke Shih 					   struct ieee80211_sta *sta)
2691e3ec7017SPing-Ke Shih {
2692e3ec7017SPing-Ke Shih 	struct rtw89_txq *rtwtxq, *tmp;
2693e3ec7017SPing-Ke Shih 
2694e3ec7017SPing-Ke Shih 	spin_lock_bh(&rtwdev->ba_lock);
2695e3ec7017SPing-Ke Shih 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->ba_list, list) {
2696e3ec7017SPing-Ke Shih 		struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2697e3ec7017SPing-Ke Shih 
2698e3ec7017SPing-Ke Shih 		if (sta == txq->sta)
2699e3ec7017SPing-Ke Shih 			list_del_init(&rtwtxq->list);
2700e3ec7017SPing-Ke Shih 	}
2701e3ec7017SPing-Ke Shih 	spin_unlock_bh(&rtwdev->ba_lock);
2702e3ec7017SPing-Ke Shih }
2703e3ec7017SPing-Ke Shih 
rtw89_core_free_sta_pending_forbid_ba(struct rtw89_dev * rtwdev,struct ieee80211_sta * sta)2704679955d5SKuan-Chung Chen static void rtw89_core_free_sta_pending_forbid_ba(struct rtw89_dev *rtwdev,
2705679955d5SKuan-Chung Chen 						  struct ieee80211_sta *sta)
2706679955d5SKuan-Chung Chen {
2707679955d5SKuan-Chung Chen 	struct rtw89_txq *rtwtxq, *tmp;
2708679955d5SKuan-Chung Chen 
2709679955d5SKuan-Chung Chen 	spin_lock_bh(&rtwdev->ba_lock);
2710679955d5SKuan-Chung Chen 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->forbid_ba_list, list) {
2711679955d5SKuan-Chung Chen 		struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2712679955d5SKuan-Chung Chen 
2713679955d5SKuan-Chung Chen 		if (sta == txq->sta) {
2714679955d5SKuan-Chung Chen 			clear_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
2715679955d5SKuan-Chung Chen 			list_del_init(&rtwtxq->list);
2716679955d5SKuan-Chung Chen 		}
2717679955d5SKuan-Chung Chen 	}
2718679955d5SKuan-Chung Chen 	spin_unlock_bh(&rtwdev->ba_lock);
2719679955d5SKuan-Chung Chen }
2720679955d5SKuan-Chung Chen 
rtw89_core_free_sta_pending_roc_tx(struct rtw89_dev * rtwdev,struct ieee80211_sta * sta)2721a0e97ae3SPo-Hao Huang static void rtw89_core_free_sta_pending_roc_tx(struct rtw89_dev *rtwdev,
2722a0e97ae3SPo-Hao Huang 					       struct ieee80211_sta *sta)
2723a0e97ae3SPo-Hao Huang {
2724a0e97ae3SPo-Hao Huang 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2725a0e97ae3SPo-Hao Huang 	struct sk_buff *skb, *tmp;
2726a0e97ae3SPo-Hao Huang 
2727a0e97ae3SPo-Hao Huang 	skb_queue_walk_safe(&rtwsta->roc_queue, skb, tmp) {
2728a0e97ae3SPo-Hao Huang 		skb_unlink(skb, &rtwsta->roc_queue);
2729a0e97ae3SPo-Hao Huang 		dev_kfree_skb_any(skb);
2730a0e97ae3SPo-Hao Huang 	}
2731a0e97ae3SPo-Hao Huang }
2732a0e97ae3SPo-Hao Huang 
rtw89_core_stop_tx_ba_session(struct rtw89_dev * rtwdev,struct rtw89_txq * rtwtxq)2733679955d5SKuan-Chung Chen static void rtw89_core_stop_tx_ba_session(struct rtw89_dev *rtwdev,
2734679955d5SKuan-Chung Chen 					  struct rtw89_txq *rtwtxq)
2735679955d5SKuan-Chung Chen {
2736679955d5SKuan-Chung Chen 	struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2737679955d5SKuan-Chung Chen 	struct ieee80211_sta *sta = txq->sta;
2738679955d5SKuan-Chung Chen 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
2739679955d5SKuan-Chung Chen 
2740679955d5SKuan-Chung Chen 	if (unlikely(!rtwsta) || unlikely(rtwsta->disassoc))
2741679955d5SKuan-Chung Chen 		return;
2742679955d5SKuan-Chung Chen 
2743679955d5SKuan-Chung Chen 	if (!test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags) ||
2744679955d5SKuan-Chung Chen 	    test_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags))
2745679955d5SKuan-Chung Chen 		return;
2746679955d5SKuan-Chung Chen 
2747679955d5SKuan-Chung Chen 	spin_lock_bh(&rtwdev->ba_lock);
27483004a0a4SKuan-Chung Chen 	if (!test_and_set_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags))
2749679955d5SKuan-Chung Chen 		list_add_tail(&rtwtxq->list, &rtwdev->forbid_ba_list);
27503004a0a4SKuan-Chung Chen 	spin_unlock_bh(&rtwdev->ba_lock);
27513004a0a4SKuan-Chung Chen 
2752679955d5SKuan-Chung Chen 	ieee80211_stop_tx_ba_session(sta, txq->tid);
2753679955d5SKuan-Chung Chen 	cancel_delayed_work(&rtwdev->forbid_ba_work);
2754679955d5SKuan-Chung Chen 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->forbid_ba_work,
2755679955d5SKuan-Chung Chen 				     RTW89_FORBID_BA_TIMER);
2756679955d5SKuan-Chung Chen }
2757679955d5SKuan-Chung Chen 
rtw89_core_txq_check_agg(struct rtw89_dev * rtwdev,struct rtw89_txq * rtwtxq,struct sk_buff * skb)2758e3ec7017SPing-Ke Shih static void rtw89_core_txq_check_agg(struct rtw89_dev *rtwdev,
2759e3ec7017SPing-Ke Shih 				     struct rtw89_txq *rtwtxq,
2760e3ec7017SPing-Ke Shih 				     struct sk_buff *skb)
2761e3ec7017SPing-Ke Shih {
2762e3ec7017SPing-Ke Shih 	struct ieee80211_hw *hw = rtwdev->hw;
2763e3ec7017SPing-Ke Shih 	struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2764e3ec7017SPing-Ke Shih 	struct ieee80211_sta *sta = txq->sta;
27655d44f067SPing-Ke Shih 	struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL;
2766e3ec7017SPing-Ke Shih 
27673004a0a4SKuan-Chung Chen 	if (test_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags))
27683004a0a4SKuan-Chung Chen 		return;
27693004a0a4SKuan-Chung Chen 
2770679955d5SKuan-Chung Chen 	if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
2771679955d5SKuan-Chung Chen 		rtw89_core_stop_tx_ba_session(rtwdev, rtwtxq);
2772e3ec7017SPing-Ke Shih 		return;
2773679955d5SKuan-Chung Chen 	}
2774e3ec7017SPing-Ke Shih 
2775e3ec7017SPing-Ke Shih 	if (unlikely(!sta))
2776e3ec7017SPing-Ke Shih 		return;
2777e3ec7017SPing-Ke Shih 
2778e3ec7017SPing-Ke Shih 	if (unlikely(test_bit(RTW89_TXQ_F_BLOCK_BA, &rtwtxq->flags)))
2779e3ec7017SPing-Ke Shih 		return;
2780e3ec7017SPing-Ke Shih 
2781e3ec7017SPing-Ke Shih 	if (test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags)) {
2782e3ec7017SPing-Ke Shih 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_AMPDU;
2783e3ec7017SPing-Ke Shih 		return;
2784e3ec7017SPing-Ke Shih 	}
2785e3ec7017SPing-Ke Shih 
2786e3ec7017SPing-Ke Shih 	spin_lock_bh(&rtwdev->ba_lock);
2787e3ec7017SPing-Ke Shih 	if (!rtwsta->disassoc && list_empty(&rtwtxq->list)) {
2788e3ec7017SPing-Ke Shih 		list_add_tail(&rtwtxq->list, &rtwdev->ba_list);
2789e3ec7017SPing-Ke Shih 		ieee80211_queue_work(hw, &rtwdev->ba_work);
2790e3ec7017SPing-Ke Shih 	}
2791e3ec7017SPing-Ke Shih 	spin_unlock_bh(&rtwdev->ba_lock);
2792e3ec7017SPing-Ke Shih }
2793e3ec7017SPing-Ke Shih 
rtw89_core_txq_push(struct rtw89_dev * rtwdev,struct rtw89_txq * rtwtxq,unsigned long frame_cnt,unsigned long byte_cnt)2794e3ec7017SPing-Ke Shih static void rtw89_core_txq_push(struct rtw89_dev *rtwdev,
2795e3ec7017SPing-Ke Shih 				struct rtw89_txq *rtwtxq,
2796e3ec7017SPing-Ke Shih 				unsigned long frame_cnt,
2797e3ec7017SPing-Ke Shih 				unsigned long byte_cnt)
2798e3ec7017SPing-Ke Shih {
2799e3ec7017SPing-Ke Shih 	struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2800e3ec7017SPing-Ke Shih 	struct ieee80211_vif *vif = txq->vif;
2801e3ec7017SPing-Ke Shih 	struct ieee80211_sta *sta = txq->sta;
2802e3ec7017SPing-Ke Shih 	struct sk_buff *skb;
2803e3ec7017SPing-Ke Shih 	unsigned long i;
2804e3ec7017SPing-Ke Shih 	int ret;
2805e3ec7017SPing-Ke Shih 
2806f3d825a3SJiri Kosina 	rcu_read_lock();
2807e3ec7017SPing-Ke Shih 	for (i = 0; i < frame_cnt; i++) {
2808e3ec7017SPing-Ke Shih 		skb = ieee80211_tx_dequeue_ni(rtwdev->hw, txq);
2809e3ec7017SPing-Ke Shih 		if (!skb) {
2810e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TXRX, "dequeue a NULL skb\n");
2811f3d825a3SJiri Kosina 			goto out;
2812e3ec7017SPing-Ke Shih 		}
2813e3ec7017SPing-Ke Shih 		rtw89_core_txq_check_agg(rtwdev, rtwtxq, skb);
2814e3ec7017SPing-Ke Shih 		ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, NULL);
2815e3ec7017SPing-Ke Shih 		if (ret) {
2816e3ec7017SPing-Ke Shih 			rtw89_err(rtwdev, "failed to push txq: %d\n", ret);
2817e3ec7017SPing-Ke Shih 			ieee80211_free_txskb(rtwdev->hw, skb);
2818e3ec7017SPing-Ke Shih 			break;
2819e3ec7017SPing-Ke Shih 		}
2820e3ec7017SPing-Ke Shih 	}
2821f3d825a3SJiri Kosina out:
2822f3d825a3SJiri Kosina 	rcu_read_unlock();
2823e3ec7017SPing-Ke Shih }
2824e3ec7017SPing-Ke Shih 
rtw89_check_and_reclaim_tx_resource(struct rtw89_dev * rtwdev,u8 tid)2825e3ec7017SPing-Ke Shih static u32 rtw89_check_and_reclaim_tx_resource(struct rtw89_dev *rtwdev, u8 tid)
2826e3ec7017SPing-Ke Shih {
2827e3ec7017SPing-Ke Shih 	u8 qsel, ch_dma;
2828e3ec7017SPing-Ke Shih 
2829e3ec7017SPing-Ke Shih 	qsel = rtw89_core_get_qsel(rtwdev, tid);
2830e3ec7017SPing-Ke Shih 	ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
2831e3ec7017SPing-Ke Shih 
2832e3ec7017SPing-Ke Shih 	return rtw89_hci_check_and_reclaim_tx_resource(rtwdev, ch_dma);
2833e3ec7017SPing-Ke Shih }
2834e3ec7017SPing-Ke Shih 
rtw89_core_txq_agg_wait(struct rtw89_dev * rtwdev,struct ieee80211_txq * txq,unsigned long * frame_cnt,bool * sched_txq,bool * reinvoke)2835e3ec7017SPing-Ke Shih static bool rtw89_core_txq_agg_wait(struct rtw89_dev *rtwdev,
2836e3ec7017SPing-Ke Shih 				    struct ieee80211_txq *txq,
2837e3ec7017SPing-Ke Shih 				    unsigned long *frame_cnt,
2838e3ec7017SPing-Ke Shih 				    bool *sched_txq, bool *reinvoke)
2839e3ec7017SPing-Ke Shih {
2840e3ec7017SPing-Ke Shih 	struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv;
2841e3ec7017SPing-Ke Shih 	struct ieee80211_sta *sta = txq->sta;
28425d44f067SPing-Ke Shih 	struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL;
2843e3ec7017SPing-Ke Shih 
2844e3ec7017SPing-Ke Shih 	if (!sta || rtwsta->max_agg_wait <= 0)
2845e3ec7017SPing-Ke Shih 		return false;
2846e3ec7017SPing-Ke Shih 
2847e3ec7017SPing-Ke Shih 	if (rtwdev->stats.tx_tfc_lv <= RTW89_TFC_MID)
2848e3ec7017SPing-Ke Shih 		return false;
2849e3ec7017SPing-Ke Shih 
2850e3ec7017SPing-Ke Shih 	if (*frame_cnt > 1) {
2851e3ec7017SPing-Ke Shih 		*frame_cnt -= 1;
2852e3ec7017SPing-Ke Shih 		*sched_txq = true;
2853e3ec7017SPing-Ke Shih 		*reinvoke = true;
2854e3ec7017SPing-Ke Shih 		rtwtxq->wait_cnt = 1;
2855e3ec7017SPing-Ke Shih 		return false;
2856e3ec7017SPing-Ke Shih 	}
2857e3ec7017SPing-Ke Shih 
2858e3ec7017SPing-Ke Shih 	if (*frame_cnt == 1 && rtwtxq->wait_cnt < rtwsta->max_agg_wait) {
2859e3ec7017SPing-Ke Shih 		*reinvoke = true;
2860e3ec7017SPing-Ke Shih 		rtwtxq->wait_cnt++;
2861e3ec7017SPing-Ke Shih 		return true;
2862e3ec7017SPing-Ke Shih 	}
2863e3ec7017SPing-Ke Shih 
2864e3ec7017SPing-Ke Shih 	rtwtxq->wait_cnt = 0;
2865e3ec7017SPing-Ke Shih 	return false;
2866e3ec7017SPing-Ke Shih }
2867e3ec7017SPing-Ke Shih 
rtw89_core_txq_schedule(struct rtw89_dev * rtwdev,u8 ac,bool * reinvoke)2868e3ec7017SPing-Ke Shih static void rtw89_core_txq_schedule(struct rtw89_dev *rtwdev, u8 ac, bool *reinvoke)
2869e3ec7017SPing-Ke Shih {
2870e3ec7017SPing-Ke Shih 	struct ieee80211_hw *hw = rtwdev->hw;
2871e3ec7017SPing-Ke Shih 	struct ieee80211_txq *txq;
2872a0e97ae3SPo-Hao Huang 	struct rtw89_vif *rtwvif;
2873e3ec7017SPing-Ke Shih 	struct rtw89_txq *rtwtxq;
2874e3ec7017SPing-Ke Shih 	unsigned long frame_cnt;
2875e3ec7017SPing-Ke Shih 	unsigned long byte_cnt;
2876e3ec7017SPing-Ke Shih 	u32 tx_resource;
2877e3ec7017SPing-Ke Shih 	bool sched_txq;
2878e3ec7017SPing-Ke Shih 
2879e3ec7017SPing-Ke Shih 	ieee80211_txq_schedule_start(hw, ac);
2880e3ec7017SPing-Ke Shih 	while ((txq = ieee80211_next_txq(hw, ac))) {
2881e3ec7017SPing-Ke Shih 		rtwtxq = (struct rtw89_txq *)txq->drv_priv;
2882a0e97ae3SPo-Hao Huang 		rtwvif = (struct rtw89_vif *)txq->vif->drv_priv;
2883a0e97ae3SPo-Hao Huang 
2884a0e97ae3SPo-Hao Huang 		if (rtwvif->offchan) {
2885a0e97ae3SPo-Hao Huang 			ieee80211_return_txq(hw, txq, true);
2886a0e97ae3SPo-Hao Huang 			continue;
2887a0e97ae3SPo-Hao Huang 		}
2888e3ec7017SPing-Ke Shih 		tx_resource = rtw89_check_and_reclaim_tx_resource(rtwdev, txq->tid);
2889e3ec7017SPing-Ke Shih 		sched_txq = false;
2890e3ec7017SPing-Ke Shih 
2891e3ec7017SPing-Ke Shih 		ieee80211_txq_get_depth(txq, &frame_cnt, &byte_cnt);
2892e3ec7017SPing-Ke Shih 		if (rtw89_core_txq_agg_wait(rtwdev, txq, &frame_cnt, &sched_txq, reinvoke)) {
2893e3ec7017SPing-Ke Shih 			ieee80211_return_txq(hw, txq, true);
2894e3ec7017SPing-Ke Shih 			continue;
2895e3ec7017SPing-Ke Shih 		}
2896e3ec7017SPing-Ke Shih 		frame_cnt = min_t(unsigned long, frame_cnt, tx_resource);
2897e3ec7017SPing-Ke Shih 		rtw89_core_txq_push(rtwdev, rtwtxq, frame_cnt, byte_cnt);
2898e3ec7017SPing-Ke Shih 		ieee80211_return_txq(hw, txq, sched_txq);
2899e3ec7017SPing-Ke Shih 		if (frame_cnt != 0)
2900e3ec7017SPing-Ke Shih 			rtw89_core_tx_kick_off(rtwdev, rtw89_core_get_qsel(rtwdev, txq->tid));
290129363fb6SKuan-Chung Chen 
290229363fb6SKuan-Chung Chen 		/* bound of tx_resource could get stuck due to burst traffic */
290329363fb6SKuan-Chung Chen 		if (frame_cnt == tx_resource)
290429363fb6SKuan-Chung Chen 			*reinvoke = true;
2905e3ec7017SPing-Ke Shih 	}
2906e3ec7017SPing-Ke Shih 	ieee80211_txq_schedule_end(hw, ac);
2907e3ec7017SPing-Ke Shih }
2908e3ec7017SPing-Ke Shih 
rtw89_ips_work(struct work_struct * work)290989590777SPo Hao Huang static void rtw89_ips_work(struct work_struct *work)
291089590777SPo Hao Huang {
291189590777SPo Hao Huang 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
291289590777SPo Hao Huang 						ips_work);
291389590777SPo Hao Huang 	mutex_lock(&rtwdev->mutex);
29146cfb6cc2SPo-Hao Huang 	rtw89_enter_ips_by_hwflags(rtwdev);
291589590777SPo Hao Huang 	mutex_unlock(&rtwdev->mutex);
291689590777SPo Hao Huang }
291789590777SPo Hao Huang 
rtw89_core_txq_work(struct work_struct * w)2918e3ec7017SPing-Ke Shih static void rtw89_core_txq_work(struct work_struct *w)
2919e3ec7017SPing-Ke Shih {
2920e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev, txq_work);
2921e3ec7017SPing-Ke Shih 	bool reinvoke = false;
2922e3ec7017SPing-Ke Shih 	u8 ac;
2923e3ec7017SPing-Ke Shih 
2924e3ec7017SPing-Ke Shih 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2925e3ec7017SPing-Ke Shih 		rtw89_core_txq_schedule(rtwdev, ac, &reinvoke);
2926e3ec7017SPing-Ke Shih 
2927e3ec7017SPing-Ke Shih 	if (reinvoke) {
2928e3ec7017SPing-Ke Shih 		/* reinvoke to process the last frame */
2929e3ec7017SPing-Ke Shih 		mod_delayed_work(rtwdev->txq_wq, &rtwdev->txq_reinvoke_work, 1);
2930e3ec7017SPing-Ke Shih 	}
2931e3ec7017SPing-Ke Shih }
2932e3ec7017SPing-Ke Shih 
rtw89_core_txq_reinvoke_work(struct work_struct * w)2933e3ec7017SPing-Ke Shih static void rtw89_core_txq_reinvoke_work(struct work_struct *w)
2934e3ec7017SPing-Ke Shih {
2935e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev,
2936e3ec7017SPing-Ke Shih 						txq_reinvoke_work.work);
2937e3ec7017SPing-Ke Shih 
2938e3ec7017SPing-Ke Shih 	queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
2939e3ec7017SPing-Ke Shih }
2940e3ec7017SPing-Ke Shih 
rtw89_forbid_ba_work(struct work_struct * w)2941679955d5SKuan-Chung Chen static void rtw89_forbid_ba_work(struct work_struct *w)
2942679955d5SKuan-Chung Chen {
2943679955d5SKuan-Chung Chen 	struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev,
2944679955d5SKuan-Chung Chen 						forbid_ba_work.work);
2945679955d5SKuan-Chung Chen 	struct rtw89_txq *rtwtxq, *tmp;
2946679955d5SKuan-Chung Chen 
2947679955d5SKuan-Chung Chen 	spin_lock_bh(&rtwdev->ba_lock);
2948679955d5SKuan-Chung Chen 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->forbid_ba_list, list) {
2949679955d5SKuan-Chung Chen 		clear_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
2950679955d5SKuan-Chung Chen 		list_del_init(&rtwtxq->list);
2951679955d5SKuan-Chung Chen 	}
2952679955d5SKuan-Chung Chen 	spin_unlock_bh(&rtwdev->ba_lock);
2953679955d5SKuan-Chung Chen }
2954679955d5SKuan-Chung Chen 
rtw89_core_sta_pending_tx_iter(void * data,struct ieee80211_sta * sta)2955a0e97ae3SPo-Hao Huang static void rtw89_core_sta_pending_tx_iter(void *data,
2956a0e97ae3SPo-Hao Huang 					   struct ieee80211_sta *sta)
2957a0e97ae3SPo-Hao Huang {
2958a0e97ae3SPo-Hao Huang 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2959a0e97ae3SPo-Hao Huang 	struct rtw89_vif *rtwvif_target = data, *rtwvif = rtwsta->rtwvif;
2960a0e97ae3SPo-Hao Huang 	struct rtw89_dev *rtwdev = rtwvif->rtwdev;
2961a0e97ae3SPo-Hao Huang 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
2962a0e97ae3SPo-Hao Huang 	struct sk_buff *skb, *tmp;
2963a0e97ae3SPo-Hao Huang 	int qsel, ret;
2964a0e97ae3SPo-Hao Huang 
2965583e998eSZong-Zhe Yang 	if (rtwvif->chanctx_idx != rtwvif_target->chanctx_idx)
2966a0e97ae3SPo-Hao Huang 		return;
2967a0e97ae3SPo-Hao Huang 
2968a0e97ae3SPo-Hao Huang 	if (skb_queue_len(&rtwsta->roc_queue) == 0)
2969a0e97ae3SPo-Hao Huang 		return;
2970a0e97ae3SPo-Hao Huang 
2971a0e97ae3SPo-Hao Huang 	skb_queue_walk_safe(&rtwsta->roc_queue, skb, tmp) {
2972a0e97ae3SPo-Hao Huang 		skb_unlink(skb, &rtwsta->roc_queue);
2973a0e97ae3SPo-Hao Huang 
2974a0e97ae3SPo-Hao Huang 		ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel);
2975a0e97ae3SPo-Hao Huang 		if (ret) {
2976a0e97ae3SPo-Hao Huang 			rtw89_warn(rtwdev, "pending tx failed with %d\n", ret);
2977a0e97ae3SPo-Hao Huang 			dev_kfree_skb_any(skb);
2978a0e97ae3SPo-Hao Huang 		} else {
2979a0e97ae3SPo-Hao Huang 			rtw89_core_tx_kick_off(rtwdev, qsel);
2980a0e97ae3SPo-Hao Huang 		}
2981a0e97ae3SPo-Hao Huang 	}
2982a0e97ae3SPo-Hao Huang }
2983a0e97ae3SPo-Hao Huang 
rtw89_core_handle_sta_pending_tx(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)2984a0e97ae3SPo-Hao Huang static void rtw89_core_handle_sta_pending_tx(struct rtw89_dev *rtwdev,
2985a0e97ae3SPo-Hao Huang 					     struct rtw89_vif *rtwvif)
2986a0e97ae3SPo-Hao Huang {
2987a0e97ae3SPo-Hao Huang 	ieee80211_iterate_stations_atomic(rtwdev->hw,
2988a0e97ae3SPo-Hao Huang 					  rtw89_core_sta_pending_tx_iter,
2989a0e97ae3SPo-Hao Huang 					  rtwvif);
2990a0e97ae3SPo-Hao Huang }
2991a0e97ae3SPo-Hao Huang 
rtw89_core_send_nullfunc(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,bool qos,bool ps)2992a0e97ae3SPo-Hao Huang static int rtw89_core_send_nullfunc(struct rtw89_dev *rtwdev,
2993a0e97ae3SPo-Hao Huang 				    struct rtw89_vif *rtwvif, bool qos, bool ps)
2994a0e97ae3SPo-Hao Huang {
2995a0e97ae3SPo-Hao Huang 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
2996a0e97ae3SPo-Hao Huang 	struct ieee80211_sta *sta;
2997a0e97ae3SPo-Hao Huang 	struct ieee80211_hdr *hdr;
2998a0e97ae3SPo-Hao Huang 	struct sk_buff *skb;
2999a0e97ae3SPo-Hao Huang 	int ret, qsel;
3000a0e97ae3SPo-Hao Huang 
3001a0e97ae3SPo-Hao Huang 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
3002a0e97ae3SPo-Hao Huang 		return 0;
3003a0e97ae3SPo-Hao Huang 
3004a0e97ae3SPo-Hao Huang 	rcu_read_lock();
3005a0e97ae3SPo-Hao Huang 	sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
3006a0e97ae3SPo-Hao Huang 	if (!sta) {
3007a0e97ae3SPo-Hao Huang 		ret = -EINVAL;
3008a0e97ae3SPo-Hao Huang 		goto out;
3009a0e97ae3SPo-Hao Huang 	}
3010a0e97ae3SPo-Hao Huang 
3011a0e97ae3SPo-Hao Huang 	skb = ieee80211_nullfunc_get(rtwdev->hw, vif, -1, qos);
3012a0e97ae3SPo-Hao Huang 	if (!skb) {
3013a0e97ae3SPo-Hao Huang 		ret = -ENOMEM;
3014a0e97ae3SPo-Hao Huang 		goto out;
3015a0e97ae3SPo-Hao Huang 	}
3016a0e97ae3SPo-Hao Huang 
3017a0e97ae3SPo-Hao Huang 	hdr = (struct ieee80211_hdr *)skb->data;
3018a0e97ae3SPo-Hao Huang 	if (ps)
3019a0e97ae3SPo-Hao Huang 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
3020a0e97ae3SPo-Hao Huang 
3021a0e97ae3SPo-Hao Huang 	ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel);
3022a0e97ae3SPo-Hao Huang 	if (ret) {
3023a0e97ae3SPo-Hao Huang 		rtw89_warn(rtwdev, "nullfunc transmit failed: %d\n", ret);
3024a0e97ae3SPo-Hao Huang 		dev_kfree_skb_any(skb);
3025a0e97ae3SPo-Hao Huang 		goto out;
3026a0e97ae3SPo-Hao Huang 	}
3027a0e97ae3SPo-Hao Huang 
3028a0e97ae3SPo-Hao Huang 	rcu_read_unlock();
3029a0e97ae3SPo-Hao Huang 
3030a0e97ae3SPo-Hao Huang 	return rtw89_core_tx_kick_off_and_wait(rtwdev, skb, qsel,
3031a0e97ae3SPo-Hao Huang 					       RTW89_ROC_TX_TIMEOUT);
3032a0e97ae3SPo-Hao Huang out:
3033a0e97ae3SPo-Hao Huang 	rcu_read_unlock();
3034a0e97ae3SPo-Hao Huang 
3035a0e97ae3SPo-Hao Huang 	return ret;
3036a0e97ae3SPo-Hao Huang }
3037a0e97ae3SPo-Hao Huang 
rtw89_roc_start(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)3038a0e97ae3SPo-Hao Huang void rtw89_roc_start(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
3039a0e97ae3SPo-Hao Huang {
30409d87e7dcSPing-Ke Shih 	const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
3041a0e97ae3SPo-Hao Huang 	struct ieee80211_hw *hw = rtwdev->hw;
3042a0e97ae3SPo-Hao Huang 	struct rtw89_roc *roc = &rtwvif->roc;
3043a0e97ae3SPo-Hao Huang 	struct cfg80211_chan_def roc_chan;
3044a0e97ae3SPo-Hao Huang 	struct rtw89_vif *tmp;
3045a0e97ae3SPo-Hao Huang 	int ret;
3046a0e97ae3SPo-Hao Huang 
3047a0e97ae3SPo-Hao Huang 	lockdep_assert_held(&rtwdev->mutex);
3048a0e97ae3SPo-Hao Huang 
3049a0e97ae3SPo-Hao Huang 	rtw89_leave_ips_by_hwflags(rtwdev);
3050a0e97ae3SPo-Hao Huang 	rtw89_leave_lps(rtwdev);
30515f499ce6SZong-Zhe Yang 	rtw89_chanctx_pause(rtwdev, RTW89_CHANCTX_PAUSE_REASON_ROC);
3052a0e97ae3SPo-Hao Huang 
3053a0e97ae3SPo-Hao Huang 	ret = rtw89_core_send_nullfunc(rtwdev, rtwvif, true, true);
3054a0e97ae3SPo-Hao Huang 	if (ret)
3055a0e97ae3SPo-Hao Huang 		rtw89_debug(rtwdev, RTW89_DBG_TXRX,
3056a0e97ae3SPo-Hao Huang 			    "roc send null-1 failed: %d\n", ret);
3057a0e97ae3SPo-Hao Huang 
3058a0e97ae3SPo-Hao Huang 	rtw89_for_each_rtwvif(rtwdev, tmp)
3059583e998eSZong-Zhe Yang 		if (tmp->chanctx_idx == rtwvif->chanctx_idx)
3060a0e97ae3SPo-Hao Huang 			tmp->offchan = true;
3061a0e97ae3SPo-Hao Huang 
3062a0e97ae3SPo-Hao Huang 	cfg80211_chandef_create(&roc_chan, &roc->chan, NL80211_CHAN_NO_HT);
3063583e998eSZong-Zhe Yang 	rtw89_config_roc_chandef(rtwdev, rtwvif->chanctx_idx, &roc_chan);
3064a0e97ae3SPo-Hao Huang 	rtw89_set_channel(rtwdev);
3065a0e97ae3SPo-Hao Huang 	rtw89_write32_clr(rtwdev,
30669d87e7dcSPing-Ke Shih 			  rtw89_mac_reg_by_idx(rtwdev, mac->rx_fltr, RTW89_MAC_0),
3067a0e97ae3SPo-Hao Huang 			  B_AX_A_UC_CAM_MATCH | B_AX_A_BC_CAM_MATCH);
3068a0e97ae3SPo-Hao Huang 
3069a0e97ae3SPo-Hao Huang 	ieee80211_ready_on_channel(hw);
3070e46987ceSChih-Kang Chang 	cancel_delayed_work(&rtwvif->roc.roc_work);
3071e46987ceSChih-Kang Chang 	ieee80211_queue_delayed_work(hw, &rtwvif->roc.roc_work,
3072e46987ceSChih-Kang Chang 				     msecs_to_jiffies(rtwvif->roc.duration));
3073a0e97ae3SPo-Hao Huang }
3074a0e97ae3SPo-Hao Huang 
rtw89_roc_end(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)3075a0e97ae3SPo-Hao Huang void rtw89_roc_end(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
3076a0e97ae3SPo-Hao Huang {
30779d87e7dcSPing-Ke Shih 	const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
3078a0e97ae3SPo-Hao Huang 	struct ieee80211_hw *hw = rtwdev->hw;
3079a0e97ae3SPo-Hao Huang 	struct rtw89_roc *roc = &rtwvif->roc;
3080a0e97ae3SPo-Hao Huang 	struct rtw89_vif *tmp;
3081a0e97ae3SPo-Hao Huang 	int ret;
3082a0e97ae3SPo-Hao Huang 
3083a0e97ae3SPo-Hao Huang 	lockdep_assert_held(&rtwdev->mutex);
3084a0e97ae3SPo-Hao Huang 
3085a0e97ae3SPo-Hao Huang 	ieee80211_remain_on_channel_expired(hw);
3086a0e97ae3SPo-Hao Huang 
3087a0e97ae3SPo-Hao Huang 	rtw89_leave_ips_by_hwflags(rtwdev);
3088a0e97ae3SPo-Hao Huang 	rtw89_leave_lps(rtwdev);
3089a0e97ae3SPo-Hao Huang 
3090a0e97ae3SPo-Hao Huang 	rtw89_write32_mask(rtwdev,
30919d87e7dcSPing-Ke Shih 			   rtw89_mac_reg_by_idx(rtwdev, mac->rx_fltr, RTW89_MAC_0),
3092a0e97ae3SPo-Hao Huang 			   B_AX_RX_FLTR_CFG_MASK,
3093a0e97ae3SPo-Hao Huang 			   rtwdev->hal.rx_fltr);
3094a0e97ae3SPo-Hao Huang 
3095a0e97ae3SPo-Hao Huang 	roc->state = RTW89_ROC_IDLE;
3096583e998eSZong-Zhe Yang 	rtw89_config_roc_chandef(rtwdev, rtwvif->chanctx_idx, NULL);
30975f499ce6SZong-Zhe Yang 	rtw89_chanctx_proceed(rtwdev);
3098a0e97ae3SPo-Hao Huang 	ret = rtw89_core_send_nullfunc(rtwdev, rtwvif, true, false);
3099a0e97ae3SPo-Hao Huang 	if (ret)
3100a0e97ae3SPo-Hao Huang 		rtw89_debug(rtwdev, RTW89_DBG_TXRX,
3101a0e97ae3SPo-Hao Huang 			    "roc send null-0 failed: %d\n", ret);
3102a0e97ae3SPo-Hao Huang 
3103a0e97ae3SPo-Hao Huang 	rtw89_for_each_rtwvif(rtwdev, tmp)
3104583e998eSZong-Zhe Yang 		if (tmp->chanctx_idx == rtwvif->chanctx_idx)
3105a0e97ae3SPo-Hao Huang 			tmp->offchan = false;
3106a0e97ae3SPo-Hao Huang 
3107a0e97ae3SPo-Hao Huang 	rtw89_core_handle_sta_pending_tx(rtwdev, rtwvif);
3108a0e97ae3SPo-Hao Huang 	queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
3109a0e97ae3SPo-Hao Huang 
3110a0e97ae3SPo-Hao Huang 	if (hw->conf.flags & IEEE80211_CONF_IDLE)
3111a0e97ae3SPo-Hao Huang 		ieee80211_queue_delayed_work(hw, &roc->roc_work,
3112e416514eSDmitry Antipov 					     msecs_to_jiffies(RTW89_ROC_IDLE_TIMEOUT));
3113a0e97ae3SPo-Hao Huang }
3114a0e97ae3SPo-Hao Huang 
rtw89_roc_work(struct work_struct * work)3115a0e97ae3SPo-Hao Huang void rtw89_roc_work(struct work_struct *work)
3116a0e97ae3SPo-Hao Huang {
3117a0e97ae3SPo-Hao Huang 	struct rtw89_vif *rtwvif = container_of(work, struct rtw89_vif,
3118a0e97ae3SPo-Hao Huang 						roc.roc_work.work);
3119a0e97ae3SPo-Hao Huang 	struct rtw89_dev *rtwdev = rtwvif->rtwdev;
3120a0e97ae3SPo-Hao Huang 	struct rtw89_roc *roc = &rtwvif->roc;
3121a0e97ae3SPo-Hao Huang 
3122a0e97ae3SPo-Hao Huang 	mutex_lock(&rtwdev->mutex);
3123a0e97ae3SPo-Hao Huang 
3124a0e97ae3SPo-Hao Huang 	switch (roc->state) {
3125a0e97ae3SPo-Hao Huang 	case RTW89_ROC_IDLE:
3126a0e97ae3SPo-Hao Huang 		rtw89_enter_ips_by_hwflags(rtwdev);
3127a0e97ae3SPo-Hao Huang 		break;
3128a0e97ae3SPo-Hao Huang 	case RTW89_ROC_MGMT:
3129a0e97ae3SPo-Hao Huang 	case RTW89_ROC_NORMAL:
3130a0e97ae3SPo-Hao Huang 		rtw89_roc_end(rtwdev, rtwvif);
3131a0e97ae3SPo-Hao Huang 		break;
3132a0e97ae3SPo-Hao Huang 	default:
3133a0e97ae3SPo-Hao Huang 		break;
3134a0e97ae3SPo-Hao Huang 	}
3135a0e97ae3SPo-Hao Huang 
3136a0e97ae3SPo-Hao Huang 	mutex_unlock(&rtwdev->mutex);
3137a0e97ae3SPo-Hao Huang }
3138a0e97ae3SPo-Hao Huang 
rtw89_get_traffic_level(struct rtw89_dev * rtwdev,u32 throughput,u64 cnt)3139e3ec7017SPing-Ke Shih static enum rtw89_tfc_lv rtw89_get_traffic_level(struct rtw89_dev *rtwdev,
3140e3ec7017SPing-Ke Shih 						 u32 throughput, u64 cnt)
3141e3ec7017SPing-Ke Shih {
3142e3ec7017SPing-Ke Shih 	if (cnt < 100)
3143e3ec7017SPing-Ke Shih 		return RTW89_TFC_IDLE;
3144e3ec7017SPing-Ke Shih 	if (throughput > 50)
3145e3ec7017SPing-Ke Shih 		return RTW89_TFC_HIGH;
3146e3ec7017SPing-Ke Shih 	if (throughput > 10)
3147e3ec7017SPing-Ke Shih 		return RTW89_TFC_MID;
3148e3ec7017SPing-Ke Shih 	if (throughput > 2)
3149e3ec7017SPing-Ke Shih 		return RTW89_TFC_LOW;
3150e3ec7017SPing-Ke Shih 	return RTW89_TFC_ULTRA_LOW;
3151e3ec7017SPing-Ke Shih }
3152e3ec7017SPing-Ke Shih 
rtw89_traffic_stats_calc(struct rtw89_dev * rtwdev,struct rtw89_traffic_stats * stats)3153e3ec7017SPing-Ke Shih static bool rtw89_traffic_stats_calc(struct rtw89_dev *rtwdev,
3154e3ec7017SPing-Ke Shih 				     struct rtw89_traffic_stats *stats)
3155e3ec7017SPing-Ke Shih {
3156e3ec7017SPing-Ke Shih 	enum rtw89_tfc_lv tx_tfc_lv = stats->tx_tfc_lv;
3157e3ec7017SPing-Ke Shih 	enum rtw89_tfc_lv rx_tfc_lv = stats->rx_tfc_lv;
3158e3ec7017SPing-Ke Shih 
3159e3ec7017SPing-Ke Shih 	stats->tx_throughput_raw = (u32)(stats->tx_unicast >> RTW89_TP_SHIFT);
3160e3ec7017SPing-Ke Shih 	stats->rx_throughput_raw = (u32)(stats->rx_unicast >> RTW89_TP_SHIFT);
3161e3ec7017SPing-Ke Shih 
3162e3ec7017SPing-Ke Shih 	ewma_tp_add(&stats->tx_ewma_tp, stats->tx_throughput_raw);
3163e3ec7017SPing-Ke Shih 	ewma_tp_add(&stats->rx_ewma_tp, stats->rx_throughput_raw);
3164e3ec7017SPing-Ke Shih 
3165e3ec7017SPing-Ke Shih 	stats->tx_throughput = ewma_tp_read(&stats->tx_ewma_tp);
3166e3ec7017SPing-Ke Shih 	stats->rx_throughput = ewma_tp_read(&stats->rx_ewma_tp);
3167e3ec7017SPing-Ke Shih 	stats->tx_tfc_lv = rtw89_get_traffic_level(rtwdev, stats->tx_throughput,
3168e3ec7017SPing-Ke Shih 						   stats->tx_cnt);
3169e3ec7017SPing-Ke Shih 	stats->rx_tfc_lv = rtw89_get_traffic_level(rtwdev, stats->rx_throughput,
3170e3ec7017SPing-Ke Shih 						   stats->rx_cnt);
3171e3ec7017SPing-Ke Shih 	stats->tx_avg_len = stats->tx_cnt ?
3172e3ec7017SPing-Ke Shih 			    DIV_ROUND_DOWN_ULL(stats->tx_unicast, stats->tx_cnt) : 0;
3173e3ec7017SPing-Ke Shih 	stats->rx_avg_len = stats->rx_cnt ?
3174e3ec7017SPing-Ke Shih 			    DIV_ROUND_DOWN_ULL(stats->rx_unicast, stats->rx_cnt) : 0;
3175e3ec7017SPing-Ke Shih 
3176e3ec7017SPing-Ke Shih 	stats->tx_unicast = 0;
3177e3ec7017SPing-Ke Shih 	stats->rx_unicast = 0;
3178e3ec7017SPing-Ke Shih 	stats->tx_cnt = 0;
3179e3ec7017SPing-Ke Shih 	stats->rx_cnt = 0;
31805165f168SPo Hao Huang 	stats->rx_tf_periodic = stats->rx_tf_acc;
31815165f168SPo Hao Huang 	stats->rx_tf_acc = 0;
3182e3ec7017SPing-Ke Shih 
3183e3ec7017SPing-Ke Shih 	if (tx_tfc_lv != stats->tx_tfc_lv || rx_tfc_lv != stats->rx_tfc_lv)
3184e3ec7017SPing-Ke Shih 		return true;
3185e3ec7017SPing-Ke Shih 
3186e3ec7017SPing-Ke Shih 	return false;
3187e3ec7017SPing-Ke Shih }
3188e3ec7017SPing-Ke Shih 
rtw89_traffic_stats_track(struct rtw89_dev * rtwdev)3189e3ec7017SPing-Ke Shih static bool rtw89_traffic_stats_track(struct rtw89_dev *rtwdev)
3190e3ec7017SPing-Ke Shih {
3191e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif;
3192e3ec7017SPing-Ke Shih 	bool tfc_changed;
3193e3ec7017SPing-Ke Shih 
3194e3ec7017SPing-Ke Shih 	tfc_changed = rtw89_traffic_stats_calc(rtwdev, &rtwdev->stats);
3195ac83f380SPo-Hao Huang 	rtw89_for_each_rtwvif(rtwdev, rtwvif) {
3196e3ec7017SPing-Ke Shih 		rtw89_traffic_stats_calc(rtwdev, &rtwvif->stats);
3197ac83f380SPo-Hao Huang 		rtw89_fw_h2c_tp_offload(rtwdev, rtwvif);
3198ac83f380SPo-Hao Huang 	}
3199e3ec7017SPing-Ke Shih 
3200e3ec7017SPing-Ke Shih 	return tfc_changed;
3201e3ec7017SPing-Ke Shih }
3202e3ec7017SPing-Ke Shih 
rtw89_vif_enter_lps(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)3203e3ec7017SPing-Ke Shih static void rtw89_vif_enter_lps(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
3204e3ec7017SPing-Ke Shih {
3205d881d0a1SKuan-Chung Chen 	if ((rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION &&
3206d881d0a1SKuan-Chung Chen 	     rtwvif->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT) ||
3207d881d0a1SKuan-Chung Chen 	    rtwvif->tdls_peer)
3208e3ec7017SPing-Ke Shih 		return;
3209e3ec7017SPing-Ke Shih 
3210a0e97ae3SPo-Hao Huang 	if (rtwvif->offchan)
3211a0e97ae3SPo-Hao Huang 		return;
3212a0e97ae3SPo-Hao Huang 
3213e3ec7017SPing-Ke Shih 	if (rtwvif->stats.tx_tfc_lv == RTW89_TFC_IDLE &&
3214e3ec7017SPing-Ke Shih 	    rtwvif->stats.rx_tfc_lv == RTW89_TFC_IDLE)
3215deb1b2aeSChih-Kang Chang 		rtw89_enter_lps(rtwdev, rtwvif, true);
3216e3ec7017SPing-Ke Shih }
3217e3ec7017SPing-Ke Shih 
rtw89_enter_lps_track(struct rtw89_dev * rtwdev)3218e3ec7017SPing-Ke Shih static void rtw89_enter_lps_track(struct rtw89_dev *rtwdev)
3219e3ec7017SPing-Ke Shih {
3220e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif;
3221e3ec7017SPing-Ke Shih 
3222e3ec7017SPing-Ke Shih 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
3223e3ec7017SPing-Ke Shih 		rtw89_vif_enter_lps(rtwdev, rtwvif);
3224e3ec7017SPing-Ke Shih }
3225e3ec7017SPing-Ke Shih 
rtw89_core_rfk_track(struct rtw89_dev * rtwdev)3226c83ff9a3SZong-Zhe Yang static void rtw89_core_rfk_track(struct rtw89_dev *rtwdev)
3227c83ff9a3SZong-Zhe Yang {
3228c83ff9a3SZong-Zhe Yang 	enum rtw89_entity_mode mode;
3229c83ff9a3SZong-Zhe Yang 
3230c83ff9a3SZong-Zhe Yang 	mode = rtw89_get_entity_mode(rtwdev);
3231c83ff9a3SZong-Zhe Yang 	if (mode == RTW89_ENTITY_MODE_MCC)
3232c83ff9a3SZong-Zhe Yang 		return;
3233c83ff9a3SZong-Zhe Yang 
3234c83ff9a3SZong-Zhe Yang 	rtw89_chip_rfk_track(rtwdev);
3235c83ff9a3SZong-Zhe Yang }
3236c83ff9a3SZong-Zhe Yang 
rtw89_core_update_p2p_ps(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif)323715fe9b73SZong-Zhe Yang void rtw89_core_update_p2p_ps(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
323815fe9b73SZong-Zhe Yang {
323915fe9b73SZong-Zhe Yang 	enum rtw89_entity_mode mode = rtw89_get_entity_mode(rtwdev);
324015fe9b73SZong-Zhe Yang 
324115fe9b73SZong-Zhe Yang 	if (mode == RTW89_ENTITY_MODE_MCC)
324215fe9b73SZong-Zhe Yang 		rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_P2P_PS_CHANGE);
324315fe9b73SZong-Zhe Yang 	else
324415fe9b73SZong-Zhe Yang 		rtw89_process_p2p_ps(rtwdev, vif);
324515fe9b73SZong-Zhe Yang }
324615fe9b73SZong-Zhe Yang 
rtw89_traffic_stats_init(struct rtw89_dev * rtwdev,struct rtw89_traffic_stats * stats)3247e3ec7017SPing-Ke Shih void rtw89_traffic_stats_init(struct rtw89_dev *rtwdev,
3248e3ec7017SPing-Ke Shih 			      struct rtw89_traffic_stats *stats)
3249e3ec7017SPing-Ke Shih {
3250e3ec7017SPing-Ke Shih 	stats->tx_unicast = 0;
3251e3ec7017SPing-Ke Shih 	stats->rx_unicast = 0;
3252e3ec7017SPing-Ke Shih 	stats->tx_cnt = 0;
3253e3ec7017SPing-Ke Shih 	stats->rx_cnt = 0;
3254e3ec7017SPing-Ke Shih 	ewma_tp_init(&stats->tx_ewma_tp);
3255e3ec7017SPing-Ke Shih 	ewma_tp_init(&stats->rx_ewma_tp);
3256e3ec7017SPing-Ke Shih }
3257e3ec7017SPing-Ke Shih 
rtw89_track_work(struct work_struct * work)3258e3ec7017SPing-Ke Shih static void rtw89_track_work(struct work_struct *work)
3259e3ec7017SPing-Ke Shih {
3260e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
3261e3ec7017SPing-Ke Shih 						track_work.work);
3262e3ec7017SPing-Ke Shih 	bool tfc_changed;
3263e3ec7017SPing-Ke Shih 
326419e28c7fSChin-Yen Lee 	if (test_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags))
326519e28c7fSChin-Yen Lee 		return;
326619e28c7fSChin-Yen Lee 
3267e3ec7017SPing-Ke Shih 	mutex_lock(&rtwdev->mutex);
3268e3ec7017SPing-Ke Shih 
3269e3ec7017SPing-Ke Shih 	if (!test_bit(RTW89_FLAG_RUNNING, rtwdev->flags))
3270e3ec7017SPing-Ke Shih 		goto out;
3271e3ec7017SPing-Ke Shih 
3272e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work,
3273e3ec7017SPing-Ke Shih 				     RTW89_TRACK_WORK_PERIOD);
3274e3ec7017SPing-Ke Shih 
3275e3ec7017SPing-Ke Shih 	tfc_changed = rtw89_traffic_stats_track(rtwdev);
3276e3ec7017SPing-Ke Shih 	if (rtwdev->scanning)
3277e3ec7017SPing-Ke Shih 		goto out;
3278e3ec7017SPing-Ke Shih 
3279e3ec7017SPing-Ke Shih 	rtw89_leave_lps(rtwdev);
3280e3ec7017SPing-Ke Shih 
3281e3ec7017SPing-Ke Shih 	if (tfc_changed) {
3282e3ec7017SPing-Ke Shih 		rtw89_hci_recalc_int_mit(rtwdev);
3283e3ec7017SPing-Ke Shih 		rtw89_btc_ntfy_wl_sta(rtwdev);
3284e3ec7017SPing-Ke Shih 	}
3285e3ec7017SPing-Ke Shih 	rtw89_mac_bf_monitor_track(rtwdev);
3286e3ec7017SPing-Ke Shih 	rtw89_phy_stat_track(rtwdev);
3287e3ec7017SPing-Ke Shih 	rtw89_phy_env_monitor_track(rtwdev);
3288e3ec7017SPing-Ke Shih 	rtw89_phy_dig(rtwdev);
3289c83ff9a3SZong-Zhe Yang 	rtw89_core_rfk_track(rtwdev);
3290e3ec7017SPing-Ke Shih 	rtw89_phy_ra_update(rtwdev);
3291e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_track(rtwdev);
32927dbdf655SPing-Ke Shih 	rtw89_phy_tx_path_div_track(rtwdev);
3293e3715859SEric Huang 	rtw89_phy_antdiv_track(rtwdev);
329429136c95SEric Huang 	rtw89_phy_ul_tb_ctrl_track(rtwdev);
32950bb18525SYi-Chen Chen 	rtw89_phy_edcca_track(rtwdev);
3296eb2624f5SKuan-Chung Chen 	rtw89_tas_track(rtwdev);
32975f69aabaSZong-Zhe Yang 	rtw89_chanctx_track(rtwdev);
32980b38e627SKuan-Chung Chen 	rtw89_core_rfkill_poll(rtwdev, false);
3299e3ec7017SPing-Ke Shih 
3300e3ec7017SPing-Ke Shih 	if (rtwdev->lps_enabled && !rtwdev->btc.lps)
3301e3ec7017SPing-Ke Shih 		rtw89_enter_lps_track(rtwdev);
3302e3ec7017SPing-Ke Shih 
3303e3ec7017SPing-Ke Shih out:
3304e3ec7017SPing-Ke Shih 	mutex_unlock(&rtwdev->mutex);
3305e3ec7017SPing-Ke Shih }
3306e3ec7017SPing-Ke Shih 
rtw89_core_acquire_bit_map(unsigned long * addr,unsigned long size)3307e3ec7017SPing-Ke Shih u8 rtw89_core_acquire_bit_map(unsigned long *addr, unsigned long size)
3308e3ec7017SPing-Ke Shih {
3309e3ec7017SPing-Ke Shih 	unsigned long bit;
3310e3ec7017SPing-Ke Shih 
3311e3ec7017SPing-Ke Shih 	bit = find_first_zero_bit(addr, size);
3312e3ec7017SPing-Ke Shih 	if (bit < size)
3313e3ec7017SPing-Ke Shih 		set_bit(bit, addr);
3314e3ec7017SPing-Ke Shih 
3315e3ec7017SPing-Ke Shih 	return bit;
3316e3ec7017SPing-Ke Shih }
3317e3ec7017SPing-Ke Shih 
rtw89_core_release_bit_map(unsigned long * addr,u8 bit)3318e3ec7017SPing-Ke Shih void rtw89_core_release_bit_map(unsigned long *addr, u8 bit)
3319e3ec7017SPing-Ke Shih {
3320e3ec7017SPing-Ke Shih 	clear_bit(bit, addr);
3321e3ec7017SPing-Ke Shih }
3322e3ec7017SPing-Ke Shih 
rtw89_core_release_all_bits_map(unsigned long * addr,unsigned int nbits)3323e3ec7017SPing-Ke Shih void rtw89_core_release_all_bits_map(unsigned long *addr, unsigned int nbits)
3324e3ec7017SPing-Ke Shih {
3325e3ec7017SPing-Ke Shih 	bitmap_zero(addr, nbits);
3326e3ec7017SPing-Ke Shih }
3327e3ec7017SPing-Ke Shih 
rtw89_core_acquire_sta_ba_entry(struct rtw89_dev * rtwdev,struct rtw89_sta * rtwsta,u8 tid,u8 * cam_idx)33282def7356SPing-Ke Shih int rtw89_core_acquire_sta_ba_entry(struct rtw89_dev *rtwdev,
33292def7356SPing-Ke Shih 				    struct rtw89_sta *rtwsta, u8 tid, u8 *cam_idx)
33303ffbb5a8SPing-Ke Shih {
33312def7356SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
333208aa8077SPing-Ke Shih 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
333308aa8077SPing-Ke Shih 	struct rtw89_ba_cam_entry *entry = NULL, *tmp;
33343ffbb5a8SPing-Ke Shih 	u8 idx;
333508aa8077SPing-Ke Shih 	int i;
33363ffbb5a8SPing-Ke Shih 
333708aa8077SPing-Ke Shih 	lockdep_assert_held(&rtwdev->mutex);
333808aa8077SPing-Ke Shih 
333908aa8077SPing-Ke Shih 	idx = rtw89_core_acquire_bit_map(cam_info->ba_cam_map, chip->bacam_num);
33402def7356SPing-Ke Shih 	if (idx == chip->bacam_num) {
334108aa8077SPing-Ke Shih 		/* allocate a static BA CAM to tid=0/5, so replace the existing
33423ffbb5a8SPing-Ke Shih 		 * one if BA CAM is full. Hardware will process the original tid
33433ffbb5a8SPing-Ke Shih 		 * automatically.
33443ffbb5a8SPing-Ke Shih 		 */
334508aa8077SPing-Ke Shih 		if (tid != 0 && tid != 5)
33463ffbb5a8SPing-Ke Shih 			return -ENOSPC;
33473ffbb5a8SPing-Ke Shih 
334808aa8077SPing-Ke Shih 		for_each_set_bit(i, cam_info->ba_cam_map, chip->bacam_num) {
334908aa8077SPing-Ke Shih 			tmp = &cam_info->ba_cam_entry[i];
335008aa8077SPing-Ke Shih 			if (tmp->tid == 0 || tmp->tid == 5)
335108aa8077SPing-Ke Shih 				continue;
335208aa8077SPing-Ke Shih 
335308aa8077SPing-Ke Shih 			idx = i;
335408aa8077SPing-Ke Shih 			entry = tmp;
335508aa8077SPing-Ke Shih 			list_del(&entry->list);
335608aa8077SPing-Ke Shih 			break;
33573ffbb5a8SPing-Ke Shih 		}
33583ffbb5a8SPing-Ke Shih 
335908aa8077SPing-Ke Shih 		if (!entry)
336008aa8077SPing-Ke Shih 			return -ENOSPC;
336108aa8077SPing-Ke Shih 	} else {
336208aa8077SPing-Ke Shih 		entry = &cam_info->ba_cam_entry[idx];
336308aa8077SPing-Ke Shih 	}
336408aa8077SPing-Ke Shih 
33653ffbb5a8SPing-Ke Shih 	entry->tid = tid;
336608aa8077SPing-Ke Shih 	list_add_tail(&entry->list, &rtwsta->ba_cam_list);
336708aa8077SPing-Ke Shih 
33683ffbb5a8SPing-Ke Shih 	*cam_idx = idx;
33693ffbb5a8SPing-Ke Shih 
33703ffbb5a8SPing-Ke Shih 	return 0;
33713ffbb5a8SPing-Ke Shih }
33723ffbb5a8SPing-Ke Shih 
rtw89_core_release_sta_ba_entry(struct rtw89_dev * rtwdev,struct rtw89_sta * rtwsta,u8 tid,u8 * cam_idx)33732def7356SPing-Ke Shih int rtw89_core_release_sta_ba_entry(struct rtw89_dev *rtwdev,
33742def7356SPing-Ke Shih 				    struct rtw89_sta *rtwsta, u8 tid, u8 *cam_idx)
33753ffbb5a8SPing-Ke Shih {
337608aa8077SPing-Ke Shih 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
337708aa8077SPing-Ke Shih 	struct rtw89_ba_cam_entry *entry = NULL, *tmp;
337808aa8077SPing-Ke Shih 	u8 idx;
33793ffbb5a8SPing-Ke Shih 
338008aa8077SPing-Ke Shih 	lockdep_assert_held(&rtwdev->mutex);
33813ffbb5a8SPing-Ke Shih 
338208aa8077SPing-Ke Shih 	list_for_each_entry_safe(entry, tmp, &rtwsta->ba_cam_list, list) {
33833ffbb5a8SPing-Ke Shih 		if (entry->tid != tid)
33843ffbb5a8SPing-Ke Shih 			continue;
33853ffbb5a8SPing-Ke Shih 
338608aa8077SPing-Ke Shih 		idx = entry - cam_info->ba_cam_entry;
338708aa8077SPing-Ke Shih 		list_del(&entry->list);
338808aa8077SPing-Ke Shih 
338908aa8077SPing-Ke Shih 		rtw89_core_release_bit_map(cam_info->ba_cam_map, idx);
339008aa8077SPing-Ke Shih 		*cam_idx = idx;
33913ffbb5a8SPing-Ke Shih 		return 0;
33923ffbb5a8SPing-Ke Shih 	}
33933ffbb5a8SPing-Ke Shih 
33943ffbb5a8SPing-Ke Shih 	return -ENOENT;
33953ffbb5a8SPing-Ke Shih }
33963ffbb5a8SPing-Ke Shih 
3397e3ec7017SPing-Ke Shih #define RTW89_TYPE_MAPPING(_type)	\
3398e3ec7017SPing-Ke Shih 	case NL80211_IFTYPE_ ## _type:	\
3399e3ec7017SPing-Ke Shih 		rtwvif->wifi_role = RTW89_WIFI_ROLE_ ## _type;	\
3400e3ec7017SPing-Ke Shih 		break
rtw89_vif_type_mapping(struct ieee80211_vif * vif,bool assoc)3401e3ec7017SPing-Ke Shih void rtw89_vif_type_mapping(struct ieee80211_vif *vif, bool assoc)
3402e3ec7017SPing-Ke Shih {
3403e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
3404e3ec7017SPing-Ke Shih 
3405e3ec7017SPing-Ke Shih 	switch (vif->type) {
3406487b7b70SPing-Ke Shih 	case NL80211_IFTYPE_STATION:
3407487b7b70SPing-Ke Shih 		if (vif->p2p)
3408487b7b70SPing-Ke Shih 			rtwvif->wifi_role = RTW89_WIFI_ROLE_P2P_CLIENT;
3409487b7b70SPing-Ke Shih 		else
3410487b7b70SPing-Ke Shih 			rtwvif->wifi_role = RTW89_WIFI_ROLE_STATION;
3411487b7b70SPing-Ke Shih 		break;
3412487b7b70SPing-Ke Shih 	case NL80211_IFTYPE_AP:
3413487b7b70SPing-Ke Shih 		if (vif->p2p)
3414487b7b70SPing-Ke Shih 			rtwvif->wifi_role = RTW89_WIFI_ROLE_P2P_GO;
3415487b7b70SPing-Ke Shih 		else
3416487b7b70SPing-Ke Shih 			rtwvif->wifi_role = RTW89_WIFI_ROLE_AP;
3417487b7b70SPing-Ke Shih 		break;
3418e3ec7017SPing-Ke Shih 	RTW89_TYPE_MAPPING(ADHOC);
3419e3ec7017SPing-Ke Shih 	RTW89_TYPE_MAPPING(MONITOR);
3420e3ec7017SPing-Ke Shih 	RTW89_TYPE_MAPPING(MESH_POINT);
3421e3ec7017SPing-Ke Shih 	default:
3422e3ec7017SPing-Ke Shih 		WARN_ON(1);
3423e3ec7017SPing-Ke Shih 		break;
3424e3ec7017SPing-Ke Shih 	}
3425e3ec7017SPing-Ke Shih 
3426e3ec7017SPing-Ke Shih 	switch (vif->type) {
3427e3ec7017SPing-Ke Shih 	case NL80211_IFTYPE_AP:
3428e3ec7017SPing-Ke Shih 	case NL80211_IFTYPE_MESH_POINT:
3429e3ec7017SPing-Ke Shih 		rtwvif->net_type = RTW89_NET_TYPE_AP_MODE;
3430e3ec7017SPing-Ke Shih 		rtwvif->self_role = RTW89_SELF_ROLE_AP;
3431e3ec7017SPing-Ke Shih 		break;
3432e3ec7017SPing-Ke Shih 	case NL80211_IFTYPE_ADHOC:
3433e3ec7017SPing-Ke Shih 		rtwvif->net_type = RTW89_NET_TYPE_AD_HOC;
3434e3ec7017SPing-Ke Shih 		rtwvif->self_role = RTW89_SELF_ROLE_CLIENT;
3435e3ec7017SPing-Ke Shih 		break;
3436e3ec7017SPing-Ke Shih 	case NL80211_IFTYPE_STATION:
3437e3ec7017SPing-Ke Shih 		if (assoc) {
3438e3ec7017SPing-Ke Shih 			rtwvif->net_type = RTW89_NET_TYPE_INFRA;
3439e3ec7017SPing-Ke Shih 			rtwvif->trigger = vif->bss_conf.he_support;
3440e3ec7017SPing-Ke Shih 		} else {
3441e3ec7017SPing-Ke Shih 			rtwvif->net_type = RTW89_NET_TYPE_NO_LINK;
3442e3ec7017SPing-Ke Shih 			rtwvif->trigger = false;
3443e3ec7017SPing-Ke Shih 		}
3444e3ec7017SPing-Ke Shih 		rtwvif->self_role = RTW89_SELF_ROLE_CLIENT;
3445e3ec7017SPing-Ke Shih 		rtwvif->addr_cam.sec_ent_mode = RTW89_ADDR_CAM_SEC_NORMAL;
3446e3ec7017SPing-Ke Shih 		break;
3447cd9b6b3bSZong-Zhe Yang 	case NL80211_IFTYPE_MONITOR:
3448cd9b6b3bSZong-Zhe Yang 		break;
3449e3ec7017SPing-Ke Shih 	default:
3450e3ec7017SPing-Ke Shih 		WARN_ON(1);
3451e3ec7017SPing-Ke Shih 		break;
3452e3ec7017SPing-Ke Shih 	}
3453e3ec7017SPing-Ke Shih }
3454e3ec7017SPing-Ke Shih 
rtw89_core_sta_add(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3455e3ec7017SPing-Ke Shih int rtw89_core_sta_add(struct rtw89_dev *rtwdev,
3456e3ec7017SPing-Ke Shih 		       struct ieee80211_vif *vif,
3457e3ec7017SPing-Ke Shih 		       struct ieee80211_sta *sta)
3458e3ec7017SPing-Ke Shih {
3459e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
3460e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
3461f6b24241SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
3462f6b24241SPing-Ke Shih 	u8 ant_num = hal->ant_diversity ? 2 : rtwdev->chip->rf_path_num;
3463e3ec7017SPing-Ke Shih 	int i;
34640731d0b6SPo-Hao Huang 	int ret;
3465e3ec7017SPing-Ke Shih 
34666ce472d6SPing-Ke Shih 	rtwsta->rtwdev = rtwdev;
3467e3ec7017SPing-Ke Shih 	rtwsta->rtwvif = rtwvif;
3468e3ec7017SPing-Ke Shih 	rtwsta->prev_rssi = 0;
346908aa8077SPing-Ke Shih 	INIT_LIST_HEAD(&rtwsta->ba_cam_list);
3470a0e97ae3SPo-Hao Huang 	skb_queue_head_init(&rtwsta->roc_queue);
3471e3ec7017SPing-Ke Shih 
3472e3ec7017SPing-Ke Shih 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
3473e3ec7017SPing-Ke Shih 		rtw89_core_txq_init(rtwdev, sta->txq[i]);
3474e3ec7017SPing-Ke Shih 
3475e3ec7017SPing-Ke Shih 	ewma_rssi_init(&rtwsta->avg_rssi);
34764bb223a1SPing-Ke Shih 	ewma_snr_init(&rtwsta->avg_snr);
3477bd4a3b10SKuan-Chung Chen 	ewma_evm_init(&rtwsta->evm_1ss);
34784bb223a1SPing-Ke Shih 	for (i = 0; i < ant_num; i++) {
34796ce472d6SPing-Ke Shih 		ewma_rssi_init(&rtwsta->rssi[i]);
34804bb223a1SPing-Ke Shih 		ewma_evm_init(&rtwsta->evm_min[i]);
34814bb223a1SPing-Ke Shih 		ewma_evm_init(&rtwsta->evm_max[i]);
34824bb223a1SPing-Ke Shih 	}
3483e3ec7017SPing-Ke Shih 
34847312100dSPing-Ke Shih 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
348540822e07SPing-Ke Shih 		/* for station mode, assign the mac_id from itself */
348640822e07SPing-Ke Shih 		rtwsta->mac_id = rtwvif->mac_id;
3487dcf920ffSZong-Zhe Yang 
3488dcf920ffSZong-Zhe Yang 		/* must do rtw89_reg_6ghz_recalc() before rfk channel */
3489dcf920ffSZong-Zhe Yang 		ret = rtw89_reg_6ghz_recalc(rtwdev, rtwvif, true);
3490dcf920ffSZong-Zhe Yang 		if (ret)
3491dcf920ffSZong-Zhe Yang 			return ret;
3492dcf920ffSZong-Zhe Yang 
3493e3ec7017SPing-Ke Shih 		rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta,
3494e3ec7017SPing-Ke Shih 					 BTC_ROLE_MSTS_STA_CONN_START);
349575d853d4SZong-Zhe Yang 		rtw89_chip_rfk_channel(rtwdev, rtwvif);
34967312100dSPing-Ke Shih 	} else if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
3497578bdd98SZong-Zhe Yang 		rtwsta->mac_id = rtw89_acquire_mac_id(rtwdev);
349846245bc4SZong-Zhe Yang 		if (rtwsta->mac_id == RTW89_MAX_MAC_ID_NUM)
349946245bc4SZong-Zhe Yang 			return -ENOSPC;
35000731d0b6SPo-Hao Huang 
35010731d0b6SPo-Hao Huang 		ret = rtw89_mac_set_macid_pause(rtwdev, rtwsta->mac_id, false);
35020731d0b6SPo-Hao Huang 		if (ret) {
3503578bdd98SZong-Zhe Yang 			rtw89_release_mac_id(rtwdev, rtwsta->mac_id);
35040731d0b6SPo-Hao Huang 			rtw89_warn(rtwdev, "failed to send h2c macid pause\n");
35050731d0b6SPo-Hao Huang 			return ret;
35060731d0b6SPo-Hao Huang 		}
35070731d0b6SPo-Hao Huang 
35080731d0b6SPo-Hao Huang 		ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta,
35090731d0b6SPo-Hao Huang 						 RTW89_ROLE_CREATE);
35100731d0b6SPo-Hao Huang 		if (ret) {
3511578bdd98SZong-Zhe Yang 			rtw89_release_mac_id(rtwdev, rtwsta->mac_id);
35120731d0b6SPo-Hao Huang 			rtw89_warn(rtwdev, "failed to send h2c role info\n");
35130731d0b6SPo-Hao Huang 			return ret;
35140731d0b6SPo-Hao Huang 		}
351531e415e3SZong-Zhe Yang 
35163d49ed07SPing-Ke Shih 		ret = rtw89_chip_h2c_default_cmac_tbl(rtwdev, rtwvif, rtwsta);
35173d49ed07SPing-Ke Shih 		if (ret)
35183d49ed07SPing-Ke Shih 			return ret;
35193d49ed07SPing-Ke Shih 
3520011e2768SPing-Ke Shih 		ret = rtw89_chip_h2c_default_dmac_tbl(rtwdev, rtwvif, rtwsta);
3521011e2768SPing-Ke Shih 		if (ret)
3522011e2768SPing-Ke Shih 			return ret;
3523011e2768SPing-Ke Shih 
352431e415e3SZong-Zhe Yang 		rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_REMOTE_STA_CHANGE);
3525e3ec7017SPing-Ke Shih 	}
3526e3ec7017SPing-Ke Shih 
3527e3ec7017SPing-Ke Shih 	return 0;
3528e3ec7017SPing-Ke Shih }
3529e3ec7017SPing-Ke Shih 
rtw89_core_sta_disassoc(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3530e3ec7017SPing-Ke Shih int rtw89_core_sta_disassoc(struct rtw89_dev *rtwdev,
3531e3ec7017SPing-Ke Shih 			    struct ieee80211_vif *vif,
3532e3ec7017SPing-Ke Shih 			    struct ieee80211_sta *sta)
3533e3ec7017SPing-Ke Shih {
3534d881d0a1SKuan-Chung Chen 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
3535e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
3536e3ec7017SPing-Ke Shih 
3537d2b6da24SPo-Hao Huang 	if (vif->type == NL80211_IFTYPE_STATION)
3538d2b6da24SPo-Hao Huang 		rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, vif, false);
3539d2b6da24SPo-Hao Huang 
3540e3ec7017SPing-Ke Shih 	rtwdev->total_sta_assoc--;
3541d881d0a1SKuan-Chung Chen 	if (sta->tdls)
3542d881d0a1SKuan-Chung Chen 		rtwvif->tdls_peer--;
3543e3ec7017SPing-Ke Shih 	rtwsta->disassoc = true;
3544e3ec7017SPing-Ke Shih 
3545e3ec7017SPing-Ke Shih 	return 0;
3546e3ec7017SPing-Ke Shih }
3547e3ec7017SPing-Ke Shih 
rtw89_core_sta_disconnect(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3548e3ec7017SPing-Ke Shih int rtw89_core_sta_disconnect(struct rtw89_dev *rtwdev,
3549e3ec7017SPing-Ke Shih 			      struct ieee80211_vif *vif,
3550e3ec7017SPing-Ke Shih 			      struct ieee80211_sta *sta)
3551e3ec7017SPing-Ke Shih {
3552e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
355340822e07SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
3554e3ec7017SPing-Ke Shih 	int ret;
3555e3ec7017SPing-Ke Shih 
3556e3ec7017SPing-Ke Shih 	rtw89_mac_bf_monitor_calc(rtwdev, sta, true);
3557e3ec7017SPing-Ke Shih 	rtw89_mac_bf_disassoc(rtwdev, vif, sta);
3558e3ec7017SPing-Ke Shih 	rtw89_core_free_sta_pending_ba(rtwdev, sta);
3559679955d5SKuan-Chung Chen 	rtw89_core_free_sta_pending_forbid_ba(rtwdev, sta);
3560a0e97ae3SPo-Hao Huang 	rtw89_core_free_sta_pending_roc_tx(rtwdev, sta);
3561a0e97ae3SPo-Hao Huang 
35627312100dSPing-Ke Shih 	if (vif->type == NL80211_IFTYPE_AP || sta->tdls)
35631b73e77dSPing-Ke Shih 		rtw89_cam_deinit_addr_cam(rtwdev, &rtwsta->addr_cam);
356439913cc8SPing-Ke Shih 	if (sta->tdls)
356539913cc8SPing-Ke Shih 		rtw89_cam_deinit_bssid_cam(rtwdev, &rtwsta->bssid_cam);
3566e3ec7017SPing-Ke Shih 
35675c12bb66SChin-Yen Lee 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3568e3ec7017SPing-Ke Shih 		rtw89_vif_type_mapping(vif, false);
35695c12bb66SChin-Yen Lee 		rtw89_fw_release_general_pkt_list_vif(rtwdev, rtwvif, true);
35705c12bb66SChin-Yen Lee 	}
3571e3ec7017SPing-Ke Shih 
35727e24cc86SPing-Ke Shih 	ret = rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, vif, sta);
3573e3ec7017SPing-Ke Shih 	if (ret) {
3574e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "failed to send h2c cmac table\n");
3575e3ec7017SPing-Ke Shih 		return ret;
3576e3ec7017SPing-Ke Shih 	}
3577e3ec7017SPing-Ke Shih 
3578742c470bSPing-Ke Shih 	ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, true);
3579e3ec7017SPing-Ke Shih 	if (ret) {
3580e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "failed to send h2c join info\n");
3581e3ec7017SPing-Ke Shih 		return ret;
3582e3ec7017SPing-Ke Shih 	}
3583e3ec7017SPing-Ke Shih 
3584e3ec7017SPing-Ke Shih 	/* update cam aid mac_id net_type */
35851b73e77dSPing-Ke Shih 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
3586e3ec7017SPing-Ke Shih 	if (ret) {
3587e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "failed to send h2c cam\n");
3588e3ec7017SPing-Ke Shih 		return ret;
3589e3ec7017SPing-Ke Shih 	}
3590e3ec7017SPing-Ke Shih 
3591e3ec7017SPing-Ke Shih 	return ret;
3592e3ec7017SPing-Ke Shih }
3593e3ec7017SPing-Ke Shih 
rtw89_core_sta_assoc(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3594e3ec7017SPing-Ke Shih int rtw89_core_sta_assoc(struct rtw89_dev *rtwdev,
3595e3ec7017SPing-Ke Shih 			 struct ieee80211_vif *vif,
3596e3ec7017SPing-Ke Shih 			 struct ieee80211_sta *sta)
3597e3ec7017SPing-Ke Shih {
3598e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
3599e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
360039913cc8SPing-Ke Shih 	struct rtw89_bssid_cam_entry *bssid_cam = rtw89_get_bssid_cam_of(rtwvif, rtwsta);
3601ad3dc722SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
3602583e998eSZong-Zhe Yang 						       rtwvif->chanctx_idx);
3603e3ec7017SPing-Ke Shih 	int ret;
3604e3ec7017SPing-Ke Shih 
36057312100dSPing-Ke Shih 	if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
360639913cc8SPing-Ke Shih 		if (sta->tdls) {
360739913cc8SPing-Ke Shih 			ret = rtw89_cam_init_bssid_cam(rtwdev, rtwvif, bssid_cam, sta->addr);
360839913cc8SPing-Ke Shih 			if (ret) {
360939913cc8SPing-Ke Shih 				rtw89_warn(rtwdev, "failed to send h2c init bssid cam for TDLS\n");
361039913cc8SPing-Ke Shih 				return ret;
361139913cc8SPing-Ke Shih 			}
361239913cc8SPing-Ke Shih 		}
361339913cc8SPing-Ke Shih 
361439913cc8SPing-Ke Shih 		ret = rtw89_cam_init_addr_cam(rtwdev, &rtwsta->addr_cam, bssid_cam);
36151b73e77dSPing-Ke Shih 		if (ret) {
36161b73e77dSPing-Ke Shih 			rtw89_warn(rtwdev, "failed to send h2c init addr cam\n");
36171b73e77dSPing-Ke Shih 			return ret;
36181b73e77dSPing-Ke Shih 		}
36191b73e77dSPing-Ke Shih 	}
3620e3ec7017SPing-Ke Shih 
36217e24cc86SPing-Ke Shih 	ret = rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, vif, sta);
3622e3ec7017SPing-Ke Shih 	if (ret) {
3623e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "failed to send h2c cmac table\n");
3624e3ec7017SPing-Ke Shih 		return ret;
3625e3ec7017SPing-Ke Shih 	}
3626e3ec7017SPing-Ke Shih 
3627742c470bSPing-Ke Shih 	ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, false);
3628e3ec7017SPing-Ke Shih 	if (ret) {
3629e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "failed to send h2c join info\n");
3630e3ec7017SPing-Ke Shih 		return ret;
3631e3ec7017SPing-Ke Shih 	}
3632e3ec7017SPing-Ke Shih 
3633e3ec7017SPing-Ke Shih 	/* update cam aid mac_id net_type */
363481c0b892SChristophe JAILLET 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
3635e3ec7017SPing-Ke Shih 	if (ret) {
3636e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "failed to send h2c cam\n");
3637e3ec7017SPing-Ke Shih 		return ret;
3638e3ec7017SPing-Ke Shih 	}
3639e3ec7017SPing-Ke Shih 
3640e3ec7017SPing-Ke Shih 	rtwdev->total_sta_assoc++;
3641d881d0a1SKuan-Chung Chen 	if (sta->tdls)
3642d881d0a1SKuan-Chung Chen 		rtwvif->tdls_peer++;
3643e3ec7017SPing-Ke Shih 	rtw89_phy_ra_assoc(rtwdev, sta);
3644e3ec7017SPing-Ke Shih 	rtw89_mac_bf_assoc(rtwdev, vif, sta);
3645e3ec7017SPing-Ke Shih 	rtw89_mac_bf_monitor_calc(rtwdev, sta, false);
3646e3ec7017SPing-Ke Shih 
36477312100dSPing-Ke Shih 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
364825ed1a17SPing-Ke Shih 		struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
364925ed1a17SPing-Ke Shih 
365025ed1a17SPing-Ke Shih 		if (bss_conf->he_support &&
365125ed1a17SPing-Ke Shih 		    !(bss_conf->he_oper.params & IEEE80211_HE_OPERATION_ER_SU_DISABLE))
365225ed1a17SPing-Ke Shih 			rtwsta->er_cap = true;
365325ed1a17SPing-Ke Shih 
3654e3ec7017SPing-Ke Shih 		rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta,
3655e3ec7017SPing-Ke Shih 					 BTC_ROLE_MSTS_STA_CONN_END);
3656ad3dc722SZong-Zhe Yang 		rtw89_core_get_no_ul_ofdma_htc(rtwdev, &rtwsta->htc_template, chan);
365729136c95SEric Huang 		rtw89_phy_ul_tb_assoc(rtwdev, rtwvif);
36585c12bb66SChin-Yen Lee 
36595c12bb66SChin-Yen Lee 		ret = rtw89_fw_h2c_general_pkt(rtwdev, rtwvif, rtwsta->mac_id);
36605c12bb66SChin-Yen Lee 		if (ret) {
36615c12bb66SChin-Yen Lee 			rtw89_warn(rtwdev, "failed to send h2c general packet\n");
36625c12bb66SChin-Yen Lee 			return ret;
36635c12bb66SChin-Yen Lee 		}
36647cf6b676SPo-Hao Huang 
36657cf6b676SPo-Hao Huang 		rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, vif, true);
3666e3ec7017SPing-Ke Shih 	}
3667e3ec7017SPing-Ke Shih 
3668e3ec7017SPing-Ke Shih 	return ret;
3669e3ec7017SPing-Ke Shih }
3670e3ec7017SPing-Ke Shih 
rtw89_core_sta_remove(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3671e3ec7017SPing-Ke Shih int rtw89_core_sta_remove(struct rtw89_dev *rtwdev,
3672e3ec7017SPing-Ke Shih 			  struct ieee80211_vif *vif,
3673e3ec7017SPing-Ke Shih 			  struct ieee80211_sta *sta)
3674e3ec7017SPing-Ke Shih {
3675e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
3676e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
36770731d0b6SPo-Hao Huang 	int ret;
3678e3ec7017SPing-Ke Shih 
3679f6baa1d3SZong-Zhe Yang 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3680dcf920ffSZong-Zhe Yang 		rtw89_reg_6ghz_recalc(rtwdev, rtwvif, false);
3681e3ec7017SPing-Ke Shih 		rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta,
3682e3ec7017SPing-Ke Shih 					 BTC_ROLE_MSTS_STA_DIS_CONN);
3683f6baa1d3SZong-Zhe Yang 	} else if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
3684578bdd98SZong-Zhe Yang 		rtw89_release_mac_id(rtwdev, rtwsta->mac_id);
3685e3ec7017SPing-Ke Shih 
36860731d0b6SPo-Hao Huang 		ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta,
36870731d0b6SPo-Hao Huang 						 RTW89_ROLE_REMOVE);
36880731d0b6SPo-Hao Huang 		if (ret) {
36890731d0b6SPo-Hao Huang 			rtw89_warn(rtwdev, "failed to send h2c role info\n");
36900731d0b6SPo-Hao Huang 			return ret;
36910731d0b6SPo-Hao Huang 		}
369231e415e3SZong-Zhe Yang 
369331e415e3SZong-Zhe Yang 		rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_REMOTE_STA_CHANGE);
36940731d0b6SPo-Hao Huang 	}
36950731d0b6SPo-Hao Huang 
3696e3ec7017SPing-Ke Shih 	return 0;
3697e3ec7017SPing-Ke Shih }
3698e3ec7017SPing-Ke Shih 
_rtw89_core_set_tid_config(struct rtw89_dev * rtwdev,struct ieee80211_sta * sta,struct cfg80211_tid_cfg * tid_conf)36993004a0a4SKuan-Chung Chen static void _rtw89_core_set_tid_config(struct rtw89_dev *rtwdev,
37003004a0a4SKuan-Chung Chen 				       struct ieee80211_sta *sta,
37013004a0a4SKuan-Chung Chen 				       struct cfg80211_tid_cfg *tid_conf)
37023004a0a4SKuan-Chung Chen {
37033004a0a4SKuan-Chung Chen 	struct ieee80211_txq *txq;
37043004a0a4SKuan-Chung Chen 	struct rtw89_txq *rtwtxq;
37053004a0a4SKuan-Chung Chen 	u32 mask = tid_conf->mask;
37063004a0a4SKuan-Chung Chen 	u8 tids = tid_conf->tids;
37073004a0a4SKuan-Chung Chen 	int tids_nbit = BITS_PER_BYTE;
37083004a0a4SKuan-Chung Chen 	int i;
37093004a0a4SKuan-Chung Chen 
37103004a0a4SKuan-Chung Chen 	for (i = 0; i < tids_nbit; i++, tids >>= 1) {
37113004a0a4SKuan-Chung Chen 		if (!tids)
37123004a0a4SKuan-Chung Chen 			break;
37133004a0a4SKuan-Chung Chen 
37143004a0a4SKuan-Chung Chen 		if (!(tids & BIT(0)))
37153004a0a4SKuan-Chung Chen 			continue;
37163004a0a4SKuan-Chung Chen 
37173004a0a4SKuan-Chung Chen 		txq = sta->txq[i];
37183004a0a4SKuan-Chung Chen 		rtwtxq = (struct rtw89_txq *)txq->drv_priv;
37193004a0a4SKuan-Chung Chen 
37203004a0a4SKuan-Chung Chen 		if (mask & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
37213004a0a4SKuan-Chung Chen 			if (tid_conf->ampdu == NL80211_TID_CONFIG_ENABLE) {
37223004a0a4SKuan-Chung Chen 				clear_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
37233004a0a4SKuan-Chung Chen 			} else {
37243004a0a4SKuan-Chung Chen 				if (test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags))
37253004a0a4SKuan-Chung Chen 					ieee80211_stop_tx_ba_session(sta, txq->tid);
37263004a0a4SKuan-Chung Chen 				spin_lock_bh(&rtwdev->ba_lock);
37273004a0a4SKuan-Chung Chen 				list_del_init(&rtwtxq->list);
37283004a0a4SKuan-Chung Chen 				set_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
37293004a0a4SKuan-Chung Chen 				spin_unlock_bh(&rtwdev->ba_lock);
37303004a0a4SKuan-Chung Chen 			}
37313004a0a4SKuan-Chung Chen 		}
3732f3d82323SKuan-Chung Chen 
3733f3d82323SKuan-Chung Chen 		if (mask & BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL) && tids == 0xff) {
3734f3d82323SKuan-Chung Chen 			if (tid_conf->amsdu == NL80211_TID_CONFIG_ENABLE)
3735f3d82323SKuan-Chung Chen 				sta->max_amsdu_subframes = 0;
3736f3d82323SKuan-Chung Chen 			else
3737f3d82323SKuan-Chung Chen 				sta->max_amsdu_subframes = 1;
3738f3d82323SKuan-Chung Chen 		}
37393004a0a4SKuan-Chung Chen 	}
37403004a0a4SKuan-Chung Chen }
37413004a0a4SKuan-Chung Chen 
rtw89_core_set_tid_config(struct rtw89_dev * rtwdev,struct ieee80211_sta * sta,struct cfg80211_tid_config * tid_config)37423004a0a4SKuan-Chung Chen void rtw89_core_set_tid_config(struct rtw89_dev *rtwdev,
37433004a0a4SKuan-Chung Chen 			       struct ieee80211_sta *sta,
37443004a0a4SKuan-Chung Chen 			       struct cfg80211_tid_config *tid_config)
37453004a0a4SKuan-Chung Chen {
37463004a0a4SKuan-Chung Chen 	int i;
37473004a0a4SKuan-Chung Chen 
37483004a0a4SKuan-Chung Chen 	for (i = 0; i < tid_config->n_tid_conf; i++)
37493004a0a4SKuan-Chung Chen 		_rtw89_core_set_tid_config(rtwdev, sta,
37503004a0a4SKuan-Chung Chen 					   &tid_config->tid_conf[i]);
37513004a0a4SKuan-Chung Chen }
37523004a0a4SKuan-Chung Chen 
rtw89_init_ht_cap(struct rtw89_dev * rtwdev,struct ieee80211_sta_ht_cap * ht_cap)3753e3ec7017SPing-Ke Shih static void rtw89_init_ht_cap(struct rtw89_dev *rtwdev,
3754e3ec7017SPing-Ke Shih 			      struct ieee80211_sta_ht_cap *ht_cap)
3755e3ec7017SPing-Ke Shih {
3756e3ec7017SPing-Ke Shih 	static const __le16 highest[RF_PATH_MAX] = {
3757e3ec7017SPing-Ke Shih 		cpu_to_le16(150), cpu_to_le16(300), cpu_to_le16(450), cpu_to_le16(600),
3758e3ec7017SPing-Ke Shih 	};
3759e3ec7017SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
3760e3ec7017SPing-Ke Shih 	u8 nss = hal->rx_nss;
3761e3ec7017SPing-Ke Shih 	int i;
3762e3ec7017SPing-Ke Shih 
3763e3ec7017SPing-Ke Shih 	ht_cap->ht_supported = true;
3764e3ec7017SPing-Ke Shih 	ht_cap->cap = 0;
3765e3ec7017SPing-Ke Shih 	ht_cap->cap |= IEEE80211_HT_CAP_SGI_20 |
3766e3ec7017SPing-Ke Shih 		       IEEE80211_HT_CAP_MAX_AMSDU |
3767e3ec7017SPing-Ke Shih 		       IEEE80211_HT_CAP_TX_STBC |
3768e3ec7017SPing-Ke Shih 		       (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
3769e3ec7017SPing-Ke Shih 	ht_cap->cap |= IEEE80211_HT_CAP_LDPC_CODING;
3770e3ec7017SPing-Ke Shih 	ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3771e3ec7017SPing-Ke Shih 		       IEEE80211_HT_CAP_DSSSCCK40 |
3772e3ec7017SPing-Ke Shih 		       IEEE80211_HT_CAP_SGI_40;
3773e3ec7017SPing-Ke Shih 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3774e3ec7017SPing-Ke Shih 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
3775e3ec7017SPing-Ke Shih 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3776e3ec7017SPing-Ke Shih 	for (i = 0; i < nss; i++)
3777e3ec7017SPing-Ke Shih 		ht_cap->mcs.rx_mask[i] = 0xFF;
3778e3ec7017SPing-Ke Shih 	ht_cap->mcs.rx_mask[4] = 0x01;
3779e3ec7017SPing-Ke Shih 	ht_cap->mcs.rx_highest = highest[nss - 1];
3780e3ec7017SPing-Ke Shih }
3781e3ec7017SPing-Ke Shih 
rtw89_init_vht_cap(struct rtw89_dev * rtwdev,struct ieee80211_sta_vht_cap * vht_cap)3782e3ec7017SPing-Ke Shih static void rtw89_init_vht_cap(struct rtw89_dev *rtwdev,
3783e3ec7017SPing-Ke Shih 			       struct ieee80211_sta_vht_cap *vht_cap)
3784e3ec7017SPing-Ke Shih {
3785d221270aSPing-Ke Shih 	static const __le16 highest_bw80[RF_PATH_MAX] = {
3786e3ec7017SPing-Ke Shih 		cpu_to_le16(433), cpu_to_le16(867), cpu_to_le16(1300), cpu_to_le16(1733),
3787e3ec7017SPing-Ke Shih 	};
3788d221270aSPing-Ke Shih 	static const __le16 highest_bw160[RF_PATH_MAX] = {
3789d221270aSPing-Ke Shih 		cpu_to_le16(867), cpu_to_le16(1733), cpu_to_le16(2600), cpu_to_le16(3467),
3790d221270aSPing-Ke Shih 	};
3791d221270aSPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
3792c5bdcddaSPing-Ke Shih 	const __le16 *highest = chip->support_bandwidths & BIT(NL80211_CHAN_WIDTH_160) ?
3793c5bdcddaSPing-Ke Shih 				highest_bw160 : highest_bw80;
3794e3ec7017SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
3795e3ec7017SPing-Ke Shih 	u16 tx_mcs_map = 0, rx_mcs_map = 0;
3796e3ec7017SPing-Ke Shih 	u8 sts_cap = 3;
3797e3ec7017SPing-Ke Shih 	int i;
3798e3ec7017SPing-Ke Shih 
3799e3ec7017SPing-Ke Shih 	for (i = 0; i < 8; i++) {
3800e3ec7017SPing-Ke Shih 		if (i < hal->tx_nss)
3801e3ec7017SPing-Ke Shih 			tx_mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3802e3ec7017SPing-Ke Shih 		else
3803e3ec7017SPing-Ke Shih 			tx_mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3804e3ec7017SPing-Ke Shih 		if (i < hal->rx_nss)
3805e3ec7017SPing-Ke Shih 			rx_mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3806e3ec7017SPing-Ke Shih 		else
3807e3ec7017SPing-Ke Shih 			rx_mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3808e3ec7017SPing-Ke Shih 	}
3809e3ec7017SPing-Ke Shih 
3810e3ec7017SPing-Ke Shih 	vht_cap->vht_supported = true;
3811e3ec7017SPing-Ke Shih 	vht_cap->cap = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3812e3ec7017SPing-Ke Shih 		       IEEE80211_VHT_CAP_SHORT_GI_80 |
3813e3ec7017SPing-Ke Shih 		       IEEE80211_VHT_CAP_RXSTBC_1 |
3814e3ec7017SPing-Ke Shih 		       IEEE80211_VHT_CAP_HTC_VHT |
3815e3ec7017SPing-Ke Shih 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
3816e3ec7017SPing-Ke Shih 		       0;
3817e3ec7017SPing-Ke Shih 	vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
3818e3ec7017SPing-Ke Shih 	vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC;
3819e3ec7017SPing-Ke Shih 	vht_cap->cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
3820e3ec7017SPing-Ke Shih 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
3821e3ec7017SPing-Ke Shih 	vht_cap->cap |= sts_cap << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
3822c5bdcddaSPing-Ke Shih 	if (chip->support_bandwidths & BIT(NL80211_CHAN_WIDTH_160))
3823d221270aSPing-Ke Shih 		vht_cap->cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3824d221270aSPing-Ke Shih 				IEEE80211_VHT_CAP_SHORT_GI_160;
3825e3ec7017SPing-Ke Shih 	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(rx_mcs_map);
3826e3ec7017SPing-Ke Shih 	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(tx_mcs_map);
3827e3ec7017SPing-Ke Shih 	vht_cap->vht_mcs.rx_highest = highest[hal->rx_nss - 1];
3828e3ec7017SPing-Ke Shih 	vht_cap->vht_mcs.tx_highest = highest[hal->tx_nss - 1];
38294f47e0cfSPing-Ke Shih 
38304f47e0cfSPing-Ke Shih 	if (ieee80211_hw_check(rtwdev->hw, SUPPORTS_VHT_EXT_NSS_BW))
38314f47e0cfSPing-Ke Shih 		vht_cap->vht_mcs.tx_highest |=
38324f47e0cfSPing-Ke Shih 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
3833e3ec7017SPing-Ke Shih }
3834e3ec7017SPing-Ke Shih 
rtw89_init_he_cap(struct rtw89_dev * rtwdev,enum nl80211_band band,enum nl80211_iftype iftype,struct ieee80211_sband_iftype_data * iftype_data)3835e3ec7017SPing-Ke Shih static void rtw89_init_he_cap(struct rtw89_dev *rtwdev,
3836e3ec7017SPing-Ke Shih 			      enum nl80211_band band,
3837c1944370SPing-Ke Shih 			      enum nl80211_iftype iftype,
3838c1944370SPing-Ke Shih 			      struct ieee80211_sband_iftype_data *iftype_data)
3839e3ec7017SPing-Ke Shih {
3840e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
3841e3ec7017SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
3842e3ec7017SPing-Ke Shih 	bool no_ng16 = (chip->chip_id == RTL8852A && hal->cv == CHIP_CBV) ||
3843e3ec7017SPing-Ke Shih 		       (chip->chip_id == RTL8852B && hal->cv == CHIP_CAV);
3844c1944370SPing-Ke Shih 	struct ieee80211_sta_he_cap *he_cap;
3845c1944370SPing-Ke Shih 	int nss = hal->rx_nss;
3846c1944370SPing-Ke Shih 	u8 *mac_cap_info;
3847c1944370SPing-Ke Shih 	u8 *phy_cap_info;
3848e3ec7017SPing-Ke Shih 	u16 mcs_map = 0;
3849e3ec7017SPing-Ke Shih 	int i;
3850e3ec7017SPing-Ke Shih 
3851e3ec7017SPing-Ke Shih 	for (i = 0; i < 8; i++) {
3852e3ec7017SPing-Ke Shih 		if (i < nss)
3853e3ec7017SPing-Ke Shih 			mcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
3854e3ec7017SPing-Ke Shih 		else
3855e3ec7017SPing-Ke Shih 			mcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
3856e3ec7017SPing-Ke Shih 	}
3857e3ec7017SPing-Ke Shih 
3858c1944370SPing-Ke Shih 	he_cap = &iftype_data->he_cap;
3859e3ec7017SPing-Ke Shih 	mac_cap_info = he_cap->he_cap_elem.mac_cap_info;
3860e3ec7017SPing-Ke Shih 	phy_cap_info = he_cap->he_cap_elem.phy_cap_info;
3861e3ec7017SPing-Ke Shih 
3862e3ec7017SPing-Ke Shih 	he_cap->has_he = true;
3863e3ec7017SPing-Ke Shih 	mac_cap_info[0] = IEEE80211_HE_MAC_CAP0_HTC_HE;
3864c1944370SPing-Ke Shih 	if (iftype == NL80211_IFTYPE_STATION)
3865e3ec7017SPing-Ke Shih 		mac_cap_info[1] = IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
3866e3ec7017SPing-Ke Shih 	mac_cap_info[2] = IEEE80211_HE_MAC_CAP2_ALL_ACK |
3867e3ec7017SPing-Ke Shih 			  IEEE80211_HE_MAC_CAP2_BSR;
3868e3ec7017SPing-Ke Shih 	mac_cap_info[3] = IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_2;
3869c1944370SPing-Ke Shih 	if (iftype == NL80211_IFTYPE_AP)
3870e3ec7017SPing-Ke Shih 		mac_cap_info[3] |= IEEE80211_HE_MAC_CAP3_OMI_CONTROL;
3871e3ec7017SPing-Ke Shih 	mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_OPS |
3872e3ec7017SPing-Ke Shih 			  IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
3873c1944370SPing-Ke Shih 	if (iftype == NL80211_IFTYPE_STATION)
3874e3ec7017SPing-Ke Shih 		mac_cap_info[5] = IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
3875517eed92SJohannes Berg 	if (band == NL80211_BAND_2GHZ) {
3876517eed92SJohannes Berg 		phy_cap_info[0] =
3877517eed92SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3878517eed92SJohannes Berg 	} else {
3879517eed92SJohannes Berg 		phy_cap_info[0] =
3880e3ec7017SPing-Ke Shih 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
3881c5bdcddaSPing-Ke Shih 		if (chip->support_bandwidths & BIT(NL80211_CHAN_WIDTH_160))
3882d221270aSPing-Ke Shih 			phy_cap_info[0] |= IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3883517eed92SJohannes Berg 	}
3884e3ec7017SPing-Ke Shih 	phy_cap_info[1] = IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3885e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3886e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
3887e3ec7017SPing-Ke Shih 	phy_cap_info[2] = IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
3888e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
3889e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
3890e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP2_DOPPLER_TX;
3891e3ec7017SPing-Ke Shih 	phy_cap_info[3] = IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM;
3892c1944370SPing-Ke Shih 	if (iftype == NL80211_IFTYPE_STATION)
3893e3ec7017SPing-Ke Shih 		phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_16_QAM |
3894e3ec7017SPing-Ke Shih 				   IEEE80211_HE_PHY_CAP3_DCM_MAX_TX_NSS_2;
3895c1944370SPing-Ke Shih 	if (iftype == NL80211_IFTYPE_AP)
3896e3ec7017SPing-Ke Shih 		phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU;
3897e3ec7017SPing-Ke Shih 	phy_cap_info[4] = IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
3898e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
3899c5bdcddaSPing-Ke Shih 	if (chip->support_bandwidths & BIT(NL80211_CHAN_WIDTH_160))
3900fec11deeSPing-Ke Shih 		phy_cap_info[4] |= IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4;
3901e3ec7017SPing-Ke Shih 	phy_cap_info[5] = no_ng16 ? 0 :
3902e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
3903e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
3904e3ec7017SPing-Ke Shih 	phy_cap_info[6] = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
3905e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
3906e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP6_TRIG_SU_BEAMFORMING_FB |
3907e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE;
3908e3ec7017SPing-Ke Shih 	phy_cap_info[7] = IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
3909e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI |
3910e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP7_MAX_NC_1;
3911e3ec7017SPing-Ke Shih 	phy_cap_info[8] = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
3912e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI |
3913e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_996;
3914c5bdcddaSPing-Ke Shih 	if (chip->support_bandwidths & BIT(NL80211_CHAN_WIDTH_160))
3915d221270aSPing-Ke Shih 		phy_cap_info[8] |= IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
3916d221270aSPing-Ke Shih 				   IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
3917e3ec7017SPing-Ke Shih 	phy_cap_info[9] = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
3918e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
3919e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
3920e3ec7017SPing-Ke Shih 			  IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB |
392175c5bd68SMiri Korenblit 			  u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
392275c5bd68SMiri Korenblit 					 IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
3923c1944370SPing-Ke Shih 	if (iftype == NL80211_IFTYPE_STATION)
3924e3ec7017SPing-Ke Shih 		phy_cap_info[9] |= IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
3925e3ec7017SPing-Ke Shih 	he_cap->he_mcs_nss_supp.rx_mcs_80 = cpu_to_le16(mcs_map);
3926e3ec7017SPing-Ke Shih 	he_cap->he_mcs_nss_supp.tx_mcs_80 = cpu_to_le16(mcs_map);
3927c5bdcddaSPing-Ke Shih 	if (chip->support_bandwidths & BIT(NL80211_CHAN_WIDTH_160)) {
3928d221270aSPing-Ke Shih 		he_cap->he_mcs_nss_supp.rx_mcs_160 = cpu_to_le16(mcs_map);
3929d221270aSPing-Ke Shih 		he_cap->he_mcs_nss_supp.tx_mcs_160 = cpu_to_le16(mcs_map);
3930d221270aSPing-Ke Shih 	}
3931e3ec7017SPing-Ke Shih 
393212b604d4SPing-Ke Shih 	if (band == NL80211_BAND_6GHZ) {
393312b604d4SPing-Ke Shih 		__le16 capa;
393412b604d4SPing-Ke Shih 
393512b604d4SPing-Ke Shih 		capa = le16_encode_bits(IEEE80211_HT_MPDU_DENSITY_NONE,
393612b604d4SPing-Ke Shih 					IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START) |
393712b604d4SPing-Ke Shih 		       le16_encode_bits(IEEE80211_VHT_MAX_AMPDU_1024K,
393812b604d4SPing-Ke Shih 					IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP) |
393912b604d4SPing-Ke Shih 		       le16_encode_bits(IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454,
394012b604d4SPing-Ke Shih 					IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
3941c1944370SPing-Ke Shih 		iftype_data->he_6ghz_capa.capa = capa;
394212b604d4SPing-Ke Shih 	}
3943c1944370SPing-Ke Shih }
3944c1944370SPing-Ke Shih 
rtw89_init_eht_cap(struct rtw89_dev * rtwdev,enum nl80211_band band,enum nl80211_iftype iftype,struct ieee80211_sband_iftype_data * iftype_data)39459156181fSPing-Ke Shih static void rtw89_init_eht_cap(struct rtw89_dev *rtwdev,
39469156181fSPing-Ke Shih 			       enum nl80211_band band,
39479156181fSPing-Ke Shih 			       enum nl80211_iftype iftype,
39489156181fSPing-Ke Shih 			       struct ieee80211_sband_iftype_data *iftype_data)
39499156181fSPing-Ke Shih {
39509156181fSPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
39519156181fSPing-Ke Shih 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem;
39529156181fSPing-Ke Shih 	struct ieee80211_eht_mcs_nss_supp *eht_nss;
39539156181fSPing-Ke Shih 	struct ieee80211_sta_eht_cap *eht_cap;
39549156181fSPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
39559156181fSPing-Ke Shih 	bool support_320mhz = false;
39566ebe9955SKuan-Chung Chen 	int sts = 8;
39579156181fSPing-Ke Shih 	u8 val;
39589156181fSPing-Ke Shih 
39599156181fSPing-Ke Shih 	if (chip->chip_gen == RTW89_CHIP_AX)
39609156181fSPing-Ke Shih 		return;
39619156181fSPing-Ke Shih 
39629156181fSPing-Ke Shih 	if (band == NL80211_BAND_6GHZ &&
39639156181fSPing-Ke Shih 	    chip->support_bandwidths & BIT(NL80211_CHAN_WIDTH_320))
39649156181fSPing-Ke Shih 		support_320mhz = true;
39659156181fSPing-Ke Shih 
39669156181fSPing-Ke Shih 	eht_cap = &iftype_data->eht_cap;
39679156181fSPing-Ke Shih 	eht_cap_elem = &eht_cap->eht_cap_elem;
39689156181fSPing-Ke Shih 	eht_nss = &eht_cap->eht_mcs_nss_supp;
39699156181fSPing-Ke Shih 
39709156181fSPing-Ke Shih 	eht_cap->has_eht = true;
39719156181fSPing-Ke Shih 
39729156181fSPing-Ke Shih 	eht_cap_elem->mac_cap_info[0] =
39739156181fSPing-Ke Shih 		u8_encode_bits(IEEE80211_EHT_MAC_CAP0_MAX_MPDU_LEN_7991,
39749156181fSPing-Ke Shih 			       IEEE80211_EHT_MAC_CAP0_MAX_MPDU_LEN_MASK);
39759156181fSPing-Ke Shih 	eht_cap_elem->mac_cap_info[1] = 0;
39769156181fSPing-Ke Shih 
39779156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[0] =
39789156181fSPing-Ke Shih 		IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
39799156181fSPing-Ke Shih 		IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE;
39809156181fSPing-Ke Shih 	if (support_320mhz)
39819156181fSPing-Ke Shih 		eht_cap_elem->phy_cap_info[0] |=
39829156181fSPing-Ke Shih 			IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ;
39839156181fSPing-Ke Shih 
39849156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[0] |=
39859156181fSPing-Ke Shih 		u8_encode_bits(u8_get_bits(sts - 1, BIT(0)),
39869156181fSPing-Ke Shih 			       IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK);
39879156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[1] =
39889156181fSPing-Ke Shih 		u8_encode_bits(u8_get_bits(sts - 1, GENMASK(2, 1)),
39899156181fSPing-Ke Shih 			       IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK) |
39909156181fSPing-Ke Shih 		u8_encode_bits(sts - 1,
39919156181fSPing-Ke Shih 			       IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK);
39929156181fSPing-Ke Shih 	if (support_320mhz)
39939156181fSPing-Ke Shih 		eht_cap_elem->phy_cap_info[1] |=
39949156181fSPing-Ke Shih 			u8_encode_bits(sts - 1,
39959156181fSPing-Ke Shih 				       IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK);
39969156181fSPing-Ke Shih 
39979156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[2] = 0;
39989156181fSPing-Ke Shih 
39999156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[3] =
40009156181fSPing-Ke Shih 		IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
40019156181fSPing-Ke Shih 		IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
40026ebe9955SKuan-Chung Chen 		IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
40036ebe9955SKuan-Chung Chen 		IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK;
40049156181fSPing-Ke Shih 
40059156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[4] =
40069156181fSPing-Ke Shih 		IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
40079156181fSPing-Ke Shih 		u8_encode_bits(1, IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK);
40089156181fSPing-Ke Shih 
40099156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[5] =
40109156181fSPing-Ke Shih 		u8_encode_bits(IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US,
40119156181fSPing-Ke Shih 			       IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
40129156181fSPing-Ke Shih 
40139156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[6] = 0;
40149156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[7] = 0;
40159156181fSPing-Ke Shih 	eht_cap_elem->phy_cap_info[8] = 0;
40169156181fSPing-Ke Shih 
40179156181fSPing-Ke Shih 	val = u8_encode_bits(hal->rx_nss, IEEE80211_EHT_MCS_NSS_RX) |
40189156181fSPing-Ke Shih 	      u8_encode_bits(hal->tx_nss, IEEE80211_EHT_MCS_NSS_TX);
40199156181fSPing-Ke Shih 	eht_nss->bw._80.rx_tx_mcs9_max_nss = val;
40209156181fSPing-Ke Shih 	eht_nss->bw._80.rx_tx_mcs11_max_nss = val;
40219156181fSPing-Ke Shih 	eht_nss->bw._80.rx_tx_mcs13_max_nss = val;
40229156181fSPing-Ke Shih 	eht_nss->bw._160.rx_tx_mcs9_max_nss = val;
40239156181fSPing-Ke Shih 	eht_nss->bw._160.rx_tx_mcs11_max_nss = val;
40249156181fSPing-Ke Shih 	eht_nss->bw._160.rx_tx_mcs13_max_nss = val;
40259156181fSPing-Ke Shih 	if (support_320mhz) {
40269156181fSPing-Ke Shih 		eht_nss->bw._320.rx_tx_mcs9_max_nss = val;
40279156181fSPing-Ke Shih 		eht_nss->bw._320.rx_tx_mcs11_max_nss = val;
40289156181fSPing-Ke Shih 		eht_nss->bw._320.rx_tx_mcs13_max_nss = val;
40299156181fSPing-Ke Shih 	}
40309156181fSPing-Ke Shih }
40319156181fSPing-Ke Shih 
4032c1944370SPing-Ke Shih #define RTW89_SBAND_IFTYPES_NR 2
4033c1944370SPing-Ke Shih 
rtw89_init_he_eht_cap(struct rtw89_dev * rtwdev,enum nl80211_band band,struct ieee80211_supported_band * sband)4034c1944370SPing-Ke Shih static void rtw89_init_he_eht_cap(struct rtw89_dev *rtwdev,
4035c1944370SPing-Ke Shih 				  enum nl80211_band band,
4036c1944370SPing-Ke Shih 				  struct ieee80211_supported_band *sband)
4037c1944370SPing-Ke Shih {
4038c1944370SPing-Ke Shih 	struct ieee80211_sband_iftype_data *iftype_data;
4039c1944370SPing-Ke Shih 	enum nl80211_iftype iftype;
4040c1944370SPing-Ke Shih 	int idx = 0;
4041c1944370SPing-Ke Shih 
4042c1944370SPing-Ke Shih 	iftype_data = kcalloc(RTW89_SBAND_IFTYPES_NR, sizeof(*iftype_data), GFP_KERNEL);
4043c1944370SPing-Ke Shih 	if (!iftype_data)
4044c1944370SPing-Ke Shih 		return;
4045c1944370SPing-Ke Shih 
4046c1944370SPing-Ke Shih 	for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
4047c1944370SPing-Ke Shih 		switch (iftype) {
4048c1944370SPing-Ke Shih 		case NL80211_IFTYPE_STATION:
4049c1944370SPing-Ke Shih 		case NL80211_IFTYPE_AP:
4050c1944370SPing-Ke Shih 			break;
4051c1944370SPing-Ke Shih 		default:
4052c1944370SPing-Ke Shih 			continue;
4053c1944370SPing-Ke Shih 		}
4054c1944370SPing-Ke Shih 
4055c1944370SPing-Ke Shih 		if (idx >= RTW89_SBAND_IFTYPES_NR) {
4056c1944370SPing-Ke Shih 			rtw89_warn(rtwdev, "run out of iftype_data\n");
4057c1944370SPing-Ke Shih 			break;
4058c1944370SPing-Ke Shih 		}
4059c1944370SPing-Ke Shih 
4060c1944370SPing-Ke Shih 		iftype_data[idx].types_mask = BIT(iftype);
4061c1944370SPing-Ke Shih 
4062c1944370SPing-Ke Shih 		rtw89_init_he_cap(rtwdev, band, iftype, &iftype_data[idx]);
40639156181fSPing-Ke Shih 		rtw89_init_eht_cap(rtwdev, band, iftype, &iftype_data[idx]);
406412b604d4SPing-Ke Shih 
4065e3ec7017SPing-Ke Shih 		idx++;
4066e3ec7017SPing-Ke Shih 	}
4067e3ec7017SPing-Ke Shih 
4068e8c18412SJohannes Berg 	_ieee80211_set_sband_iftype_data(sband, iftype_data, idx);
4069e3ec7017SPing-Ke Shih }
4070e3ec7017SPing-Ke Shih 
rtw89_core_set_supported_band(struct rtw89_dev * rtwdev)4071e3ec7017SPing-Ke Shih static int rtw89_core_set_supported_band(struct rtw89_dev *rtwdev)
4072e3ec7017SPing-Ke Shih {
4073e3ec7017SPing-Ke Shih 	struct ieee80211_hw *hw = rtwdev->hw;
4074e3ec7017SPing-Ke Shih 	struct ieee80211_supported_band *sband_2ghz = NULL, *sband_5ghz = NULL;
40750237f65aSZong-Zhe Yang 	struct ieee80211_supported_band *sband_6ghz = NULL;
4076e3ec7017SPing-Ke Shih 	u32 size = sizeof(struct ieee80211_supported_band);
40770237f65aSZong-Zhe Yang 	u8 support_bands = rtwdev->chip->support_bands;
4078e3ec7017SPing-Ke Shih 
40790237f65aSZong-Zhe Yang 	if (support_bands & BIT(NL80211_BAND_2GHZ)) {
4080e3ec7017SPing-Ke Shih 		sband_2ghz = kmemdup(&rtw89_sband_2ghz, size, GFP_KERNEL);
4081e3ec7017SPing-Ke Shih 		if (!sband_2ghz)
4082e3ec7017SPing-Ke Shih 			goto err;
4083e3ec7017SPing-Ke Shih 		rtw89_init_ht_cap(rtwdev, &sband_2ghz->ht_cap);
4084c1944370SPing-Ke Shih 		rtw89_init_he_eht_cap(rtwdev, NL80211_BAND_2GHZ, sband_2ghz);
4085e3ec7017SPing-Ke Shih 		hw->wiphy->bands[NL80211_BAND_2GHZ] = sband_2ghz;
40860237f65aSZong-Zhe Yang 	}
4087e3ec7017SPing-Ke Shih 
40880237f65aSZong-Zhe Yang 	if (support_bands & BIT(NL80211_BAND_5GHZ)) {
4089e3ec7017SPing-Ke Shih 		sband_5ghz = kmemdup(&rtw89_sband_5ghz, size, GFP_KERNEL);
4090e3ec7017SPing-Ke Shih 		if (!sband_5ghz)
4091e3ec7017SPing-Ke Shih 			goto err;
4092e3ec7017SPing-Ke Shih 		rtw89_init_ht_cap(rtwdev, &sband_5ghz->ht_cap);
4093e3ec7017SPing-Ke Shih 		rtw89_init_vht_cap(rtwdev, &sband_5ghz->vht_cap);
4094c1944370SPing-Ke Shih 		rtw89_init_he_eht_cap(rtwdev, NL80211_BAND_5GHZ, sband_5ghz);
4095e3ec7017SPing-Ke Shih 		hw->wiphy->bands[NL80211_BAND_5GHZ] = sband_5ghz;
40960237f65aSZong-Zhe Yang 	}
40970237f65aSZong-Zhe Yang 
40980237f65aSZong-Zhe Yang 	if (support_bands & BIT(NL80211_BAND_6GHZ)) {
40990237f65aSZong-Zhe Yang 		sband_6ghz = kmemdup(&rtw89_sband_6ghz, size, GFP_KERNEL);
41000237f65aSZong-Zhe Yang 		if (!sband_6ghz)
41010237f65aSZong-Zhe Yang 			goto err;
4102c1944370SPing-Ke Shih 		rtw89_init_he_eht_cap(rtwdev, NL80211_BAND_6GHZ, sband_6ghz);
41030237f65aSZong-Zhe Yang 		hw->wiphy->bands[NL80211_BAND_6GHZ] = sband_6ghz;
41040237f65aSZong-Zhe Yang 	}
4105e3ec7017SPing-Ke Shih 
4106e3ec7017SPing-Ke Shih 	return 0;
4107e3ec7017SPing-Ke Shih 
4108e3ec7017SPing-Ke Shih err:
4109e3ec7017SPing-Ke Shih 	hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
4110e3ec7017SPing-Ke Shih 	hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
41110237f65aSZong-Zhe Yang 	hw->wiphy->bands[NL80211_BAND_6GHZ] = NULL;
4112e3ec7017SPing-Ke Shih 	if (sband_2ghz)
4113e8c18412SJohannes Berg 		kfree((__force void *)sband_2ghz->iftype_data);
4114e3ec7017SPing-Ke Shih 	if (sband_5ghz)
4115e8c18412SJohannes Berg 		kfree((__force void *)sband_5ghz->iftype_data);
41160237f65aSZong-Zhe Yang 	if (sband_6ghz)
4117e8c18412SJohannes Berg 		kfree((__force void *)sband_6ghz->iftype_data);
4118e3ec7017SPing-Ke Shih 	kfree(sband_2ghz);
4119e3ec7017SPing-Ke Shih 	kfree(sband_5ghz);
41200237f65aSZong-Zhe Yang 	kfree(sband_6ghz);
4121e3ec7017SPing-Ke Shih 	return -ENOMEM;
4122e3ec7017SPing-Ke Shih }
4123e3ec7017SPing-Ke Shih 
rtw89_core_clr_supported_band(struct rtw89_dev * rtwdev)4124e3ec7017SPing-Ke Shih static void rtw89_core_clr_supported_band(struct rtw89_dev *rtwdev)
4125e3ec7017SPing-Ke Shih {
4126e3ec7017SPing-Ke Shih 	struct ieee80211_hw *hw = rtwdev->hw;
4127e3ec7017SPing-Ke Shih 
4128b7d170d5SZong-Zhe Yang 	if (hw->wiphy->bands[NL80211_BAND_2GHZ])
4129e8c18412SJohannes Berg 		kfree((__force void *)hw->wiphy->bands[NL80211_BAND_2GHZ]->iftype_data);
4130b7d170d5SZong-Zhe Yang 	if (hw->wiphy->bands[NL80211_BAND_5GHZ])
4131e8c18412SJohannes Berg 		kfree((__force void *)hw->wiphy->bands[NL80211_BAND_5GHZ]->iftype_data);
41320237f65aSZong-Zhe Yang 	if (hw->wiphy->bands[NL80211_BAND_6GHZ])
4133e8c18412SJohannes Berg 		kfree((__force void *)hw->wiphy->bands[NL80211_BAND_6GHZ]->iftype_data);
4134e3ec7017SPing-Ke Shih 	kfree(hw->wiphy->bands[NL80211_BAND_2GHZ]);
4135e3ec7017SPing-Ke Shih 	kfree(hw->wiphy->bands[NL80211_BAND_5GHZ]);
41360237f65aSZong-Zhe Yang 	kfree(hw->wiphy->bands[NL80211_BAND_6GHZ]);
4137e3ec7017SPing-Ke Shih 	hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
4138e3ec7017SPing-Ke Shih 	hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
41390237f65aSZong-Zhe Yang 	hw->wiphy->bands[NL80211_BAND_6GHZ] = NULL;
4140e3ec7017SPing-Ke Shih }
4141e3ec7017SPing-Ke Shih 
rtw89_core_ppdu_sts_init(struct rtw89_dev * rtwdev)4142e3ec7017SPing-Ke Shih static void rtw89_core_ppdu_sts_init(struct rtw89_dev *rtwdev)
4143e3ec7017SPing-Ke Shih {
4144e3ec7017SPing-Ke Shih 	int i;
4145e3ec7017SPing-Ke Shih 
4146e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_PHY_MAX; i++)
4147e3ec7017SPing-Ke Shih 		skb_queue_head_init(&rtwdev->ppdu_sts.rx_queue[i]);
4148e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_PHY_MAX; i++)
4149e3ec7017SPing-Ke Shih 		rtwdev->ppdu_sts.curr_rx_ppdu_cnt[i] = U8_MAX;
4150e3ec7017SPing-Ke Shih }
4151e3ec7017SPing-Ke Shih 
rtw89_core_update_beacon_work(struct work_struct * work)4152d62816b4SPing-Ke Shih void rtw89_core_update_beacon_work(struct work_struct *work)
4153d62816b4SPing-Ke Shih {
4154d62816b4SPing-Ke Shih 	struct rtw89_dev *rtwdev;
4155d62816b4SPing-Ke Shih 	struct rtw89_vif *rtwvif = container_of(work, struct rtw89_vif,
4156d62816b4SPing-Ke Shih 						update_beacon_work);
4157d62816b4SPing-Ke Shih 
4158d62816b4SPing-Ke Shih 	if (rtwvif->net_type != RTW89_NET_TYPE_AP_MODE)
4159d62816b4SPing-Ke Shih 		return;
4160d62816b4SPing-Ke Shih 
4161d62816b4SPing-Ke Shih 	rtwdev = rtwvif->rtwdev;
4162d62816b4SPing-Ke Shih 	mutex_lock(&rtwdev->mutex);
4163bcd1ae78SPing-Ke Shih 	rtw89_chip_h2c_update_beacon(rtwdev, rtwvif);
4164d62816b4SPing-Ke Shih 	mutex_unlock(&rtwdev->mutex);
4165d62816b4SPing-Ke Shih }
4166d62816b4SPing-Ke Shih 
rtw89_wait_for_cond(struct rtw89_wait_info * wait,unsigned int cond)416722b10cdbSZong-Zhe Yang int rtw89_wait_for_cond(struct rtw89_wait_info *wait, unsigned int cond)
416822b10cdbSZong-Zhe Yang {
416922b10cdbSZong-Zhe Yang 	struct completion *cmpl = &wait->completion;
41703d530eeaSWolfram Sang 	unsigned long time_left;
417122b10cdbSZong-Zhe Yang 	unsigned int cur;
417222b10cdbSZong-Zhe Yang 
417322b10cdbSZong-Zhe Yang 	cur = atomic_cmpxchg(&wait->cond, RTW89_WAIT_COND_IDLE, cond);
417422b10cdbSZong-Zhe Yang 	if (cur != RTW89_WAIT_COND_IDLE)
417522b10cdbSZong-Zhe Yang 		return -EBUSY;
417622b10cdbSZong-Zhe Yang 
41773d530eeaSWolfram Sang 	time_left = wait_for_completion_timeout(cmpl, RTW89_WAIT_FOR_COND_TIMEOUT);
41783d530eeaSWolfram Sang 	if (time_left == 0) {
417922b10cdbSZong-Zhe Yang 		atomic_set(&wait->cond, RTW89_WAIT_COND_IDLE);
418022b10cdbSZong-Zhe Yang 		return -ETIMEDOUT;
418122b10cdbSZong-Zhe Yang 	}
418222b10cdbSZong-Zhe Yang 
418322b10cdbSZong-Zhe Yang 	if (wait->data.err)
418422b10cdbSZong-Zhe Yang 		return -EFAULT;
418522b10cdbSZong-Zhe Yang 
418622b10cdbSZong-Zhe Yang 	return 0;
418722b10cdbSZong-Zhe Yang }
418822b10cdbSZong-Zhe Yang 
rtw89_complete_cond(struct rtw89_wait_info * wait,unsigned int cond,const struct rtw89_completion_data * data)418922b10cdbSZong-Zhe Yang void rtw89_complete_cond(struct rtw89_wait_info *wait, unsigned int cond,
419022b10cdbSZong-Zhe Yang 			 const struct rtw89_completion_data *data)
419122b10cdbSZong-Zhe Yang {
419222b10cdbSZong-Zhe Yang 	unsigned int cur;
419322b10cdbSZong-Zhe Yang 
419422b10cdbSZong-Zhe Yang 	cur = atomic_cmpxchg(&wait->cond, cond, RTW89_WAIT_COND_IDLE);
419522b10cdbSZong-Zhe Yang 	if (cur != cond)
419622b10cdbSZong-Zhe Yang 		return;
419722b10cdbSZong-Zhe Yang 
419822b10cdbSZong-Zhe Yang 	wait->data = *data;
419922b10cdbSZong-Zhe Yang 	complete(&wait->completion);
420022b10cdbSZong-Zhe Yang }
420122b10cdbSZong-Zhe Yang 
rtw89_core_ntfy_btc_event(struct rtw89_dev * rtwdev,enum rtw89_btc_hmsg event)420264a24cb6SZong-Zhe Yang void rtw89_core_ntfy_btc_event(struct rtw89_dev *rtwdev, enum rtw89_btc_hmsg event)
420364a24cb6SZong-Zhe Yang {
420464a24cb6SZong-Zhe Yang 	u16 bt_req_len;
420564a24cb6SZong-Zhe Yang 
420664a24cb6SZong-Zhe Yang 	switch (event) {
420764a24cb6SZong-Zhe Yang 	case RTW89_BTC_HMSG_SET_BT_REQ_SLOT:
420864a24cb6SZong-Zhe Yang 		bt_req_len = rtw89_coex_query_bt_req_len(rtwdev, RTW89_PHY_0);
420964a24cb6SZong-Zhe Yang 		rtw89_debug(rtwdev, RTW89_DBG_BTC,
421064a24cb6SZong-Zhe Yang 			    "coex updates BT req len to %d TU\n", bt_req_len);
42119ecb40efSZong-Zhe Yang 		rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_BT_SLOT_CHANGE);
421264a24cb6SZong-Zhe Yang 		break;
421364a24cb6SZong-Zhe Yang 	default:
421464a24cb6SZong-Zhe Yang 		if (event < NUM_OF_RTW89_BTC_HMSG)
421564a24cb6SZong-Zhe Yang 			rtw89_debug(rtwdev, RTW89_DBG_BTC,
421664a24cb6SZong-Zhe Yang 				    "unhandled BTC HMSG event: %d\n", event);
421764a24cb6SZong-Zhe Yang 		else
421864a24cb6SZong-Zhe Yang 			rtw89_warn(rtwdev,
421964a24cb6SZong-Zhe Yang 				   "unrecognized BTC HMSG event: %d\n", event);
422064a24cb6SZong-Zhe Yang 		break;
422164a24cb6SZong-Zhe Yang 	}
422264a24cb6SZong-Zhe Yang }
422364a24cb6SZong-Zhe Yang 
rtw89_check_quirks(struct rtw89_dev * rtwdev,const struct dmi_system_id * quirks)42245b919d72SPing-Ke Shih void rtw89_check_quirks(struct rtw89_dev *rtwdev, const struct dmi_system_id *quirks)
42255b919d72SPing-Ke Shih {
42265b919d72SPing-Ke Shih 	const struct dmi_system_id *match;
42275b919d72SPing-Ke Shih 	enum rtw89_quirks quirk;
42285b919d72SPing-Ke Shih 
42295b919d72SPing-Ke Shih 	if (!quirks)
42305b919d72SPing-Ke Shih 		return;
42315b919d72SPing-Ke Shih 
42325b919d72SPing-Ke Shih 	for (match = dmi_first_match(quirks); match; match = dmi_first_match(match + 1)) {
42335b919d72SPing-Ke Shih 		quirk = (uintptr_t)match->driver_data;
42345b919d72SPing-Ke Shih 		if (quirk >= NUM_OF_RTW89_QUIRKS)
42355b919d72SPing-Ke Shih 			continue;
42365b919d72SPing-Ke Shih 
42375b919d72SPing-Ke Shih 		set_bit(quirk, rtwdev->quirks);
42385b919d72SPing-Ke Shih 	}
42395b919d72SPing-Ke Shih }
42405b919d72SPing-Ke Shih EXPORT_SYMBOL(rtw89_check_quirks);
42415b919d72SPing-Ke Shih 
rtw89_core_start(struct rtw89_dev * rtwdev)4242e3ec7017SPing-Ke Shih int rtw89_core_start(struct rtw89_dev *rtwdev)
4243e3ec7017SPing-Ke Shih {
4244e3ec7017SPing-Ke Shih 	int ret;
4245e3ec7017SPing-Ke Shih 
4246e3ec7017SPing-Ke Shih 	ret = rtw89_mac_init(rtwdev);
4247e3ec7017SPing-Ke Shih 	if (ret) {
4248e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "mac init fail, ret:%d\n", ret);
4249e3ec7017SPing-Ke Shih 		return ret;
4250e3ec7017SPing-Ke Shih 	}
4251e3ec7017SPing-Ke Shih 
4252e3ec7017SPing-Ke Shih 	rtw89_btc_ntfy_poweron(rtwdev);
4253e3ec7017SPing-Ke Shih 
4254e3ec7017SPing-Ke Shih 	/* efuse process */
4255e3ec7017SPing-Ke Shih 
4256e3ec7017SPing-Ke Shih 	/* pre-config BB/RF, BB reset/RFC reset */
425748fa9b61SPing-Ke Shih 	ret = rtw89_chip_reset_bb_rf(rtwdev);
425861ebeecbSPing-Ke Shih 	if (ret)
425961ebeecbSPing-Ke Shih 		return ret;
426061ebeecbSPing-Ke Shih 
4261e3ec7017SPing-Ke Shih 	rtw89_phy_init_bb_reg(rtwdev);
426210af1627SPing-Ke Shih 	rtw89_chip_bb_postinit(rtwdev);
4263d9112042SChih-Kang Chang 	rtw89_phy_init_rf_reg(rtwdev, false);
4264e3ec7017SPing-Ke Shih 
4265e3ec7017SPing-Ke Shih 	rtw89_btc_ntfy_init(rtwdev, BTC_MODE_NORMAL);
4266e3ec7017SPing-Ke Shih 
4267e3ec7017SPing-Ke Shih 	rtw89_phy_dm_init(rtwdev);
4268e3ec7017SPing-Ke Shih 
4269e3ec7017SPing-Ke Shih 	rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true);
4270e3ec7017SPing-Ke Shih 	rtw89_mac_update_rts_threshold(rtwdev, RTW89_MAC_0);
4271e3ec7017SPing-Ke Shih 
4272eb2624f5SKuan-Chung Chen 	rtw89_tas_reset(rtwdev);
4273eb2624f5SKuan-Chung Chen 
4274e3ec7017SPing-Ke Shih 	ret = rtw89_hci_start(rtwdev);
4275e3ec7017SPing-Ke Shih 	if (ret) {
4276e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to start hci\n");
4277e3ec7017SPing-Ke Shih 		return ret;
4278e3ec7017SPing-Ke Shih 	}
4279e3ec7017SPing-Ke Shih 
4280e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work,
4281e3ec7017SPing-Ke Shih 				     RTW89_TRACK_WORK_PERIOD);
4282e3ec7017SPing-Ke Shih 
4283e3ec7017SPing-Ke Shih 	set_bit(RTW89_FLAG_RUNNING, rtwdev->flags);
4284e3ec7017SPing-Ke Shih 
42857e2629dcSPing-Ke Shih 	rtw89_chip_rfk_init_late(rtwdev);
4286e3ec7017SPing-Ke Shih 	rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_ON);
4287cad2bd8aSChin-Yen Lee 	rtw89_fw_h2c_fw_log(rtwdev, rtwdev->fw.log.enable);
42888b1b4730SPing-Ke Shih 	rtw89_fw_h2c_init_ba_cam(rtwdev);
4289e3ec7017SPing-Ke Shih 
4290e3ec7017SPing-Ke Shih 	return 0;
4291e3ec7017SPing-Ke Shih }
4292e3ec7017SPing-Ke Shih 
rtw89_core_stop(struct rtw89_dev * rtwdev)4293e3ec7017SPing-Ke Shih void rtw89_core_stop(struct rtw89_dev *rtwdev)
4294e3ec7017SPing-Ke Shih {
4295e3ec7017SPing-Ke Shih 	struct rtw89_btc *btc = &rtwdev->btc;
4296e3ec7017SPing-Ke Shih 
4297e3ec7017SPing-Ke Shih 	/* Prvent to stop twice; enter_ips and ops_stop */
4298e3ec7017SPing-Ke Shih 	if (!test_bit(RTW89_FLAG_RUNNING, rtwdev->flags))
4299e3ec7017SPing-Ke Shih 		return;
4300e3ec7017SPing-Ke Shih 
4301e3ec7017SPing-Ke Shih 	rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_OFF);
4302e3ec7017SPing-Ke Shih 
4303e3ec7017SPing-Ke Shih 	clear_bit(RTW89_FLAG_RUNNING, rtwdev->flags);
4304e3ec7017SPing-Ke Shih 
4305e3ec7017SPing-Ke Shih 	mutex_unlock(&rtwdev->mutex);
4306e3ec7017SPing-Ke Shih 
4307e3ec7017SPing-Ke Shih 	cancel_work_sync(&rtwdev->c2h_work);
43083ea1cd8dSZong-Zhe Yang 	cancel_work_sync(&rtwdev->cancel_6ghz_probe_work);
4309e3ec7017SPing-Ke Shih 	cancel_work_sync(&btc->eapol_notify_work);
4310e3ec7017SPing-Ke Shih 	cancel_work_sync(&btc->arp_notify_work);
4311e3ec7017SPing-Ke Shih 	cancel_work_sync(&btc->dhcp_notify_work);
4312e3ec7017SPing-Ke Shih 	cancel_work_sync(&btc->icmp_notify_work);
4313e3ec7017SPing-Ke Shih 	cancel_delayed_work_sync(&rtwdev->txq_reinvoke_work);
4314e3ec7017SPing-Ke Shih 	cancel_delayed_work_sync(&rtwdev->track_work);
43154843aa37SZong-Zhe Yang 	cancel_delayed_work_sync(&rtwdev->chanctx_work);
4316e3ec7017SPing-Ke Shih 	cancel_delayed_work_sync(&rtwdev->coex_act1_work);
4317e3ec7017SPing-Ke Shih 	cancel_delayed_work_sync(&rtwdev->coex_bt_devinfo_work);
4318e3ec7017SPing-Ke Shih 	cancel_delayed_work_sync(&rtwdev->coex_rfk_chk_work);
4319e3ec7017SPing-Ke Shih 	cancel_delayed_work_sync(&rtwdev->cfo_track_work);
4320679955d5SKuan-Chung Chen 	cancel_delayed_work_sync(&rtwdev->forbid_ba_work);
4321e3715859SEric Huang 	cancel_delayed_work_sync(&rtwdev->antdiv_work);
4322e3ec7017SPing-Ke Shih 
4323e3ec7017SPing-Ke Shih 	mutex_lock(&rtwdev->mutex);
4324e3ec7017SPing-Ke Shih 
4325e3ec7017SPing-Ke Shih 	rtw89_btc_ntfy_poweroff(rtwdev);
4326e3ec7017SPing-Ke Shih 	rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1, true);
4327e3ec7017SPing-Ke Shih 	rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, true);
4328e3ec7017SPing-Ke Shih 	rtw89_hci_stop(rtwdev);
4329e3ec7017SPing-Ke Shih 	rtw89_hci_deinit(rtwdev);
4330e3ec7017SPing-Ke Shih 	rtw89_mac_pwr_off(rtwdev);
4331e3ec7017SPing-Ke Shih 	rtw89_hci_reset(rtwdev);
4332e3ec7017SPing-Ke Shih }
4333e3ec7017SPing-Ke Shih 
rtw89_acquire_mac_id(struct rtw89_dev * rtwdev)4334578bdd98SZong-Zhe Yang u8 rtw89_acquire_mac_id(struct rtw89_dev *rtwdev)
4335578bdd98SZong-Zhe Yang {
4336578bdd98SZong-Zhe Yang 	const struct rtw89_chip_info *chip = rtwdev->chip;
4337fef63150SZong-Zhe Yang 	u8 mac_id_num;
4338578bdd98SZong-Zhe Yang 	u8 mac_id;
4339578bdd98SZong-Zhe Yang 
4340fef63150SZong-Zhe Yang 	if (rtwdev->support_mlo)
4341fef63150SZong-Zhe Yang 		mac_id_num = chip->support_macid_num / chip->support_link_num;
4342fef63150SZong-Zhe Yang 	else
4343fef63150SZong-Zhe Yang 		mac_id_num = chip->support_macid_num;
4344fef63150SZong-Zhe Yang 
4345578bdd98SZong-Zhe Yang 	mac_id = find_first_zero_bit(rtwdev->mac_id_map, mac_id_num);
4346578bdd98SZong-Zhe Yang 	if (mac_id == mac_id_num)
4347578bdd98SZong-Zhe Yang 		return RTW89_MAX_MAC_ID_NUM;
4348578bdd98SZong-Zhe Yang 
4349578bdd98SZong-Zhe Yang 	set_bit(mac_id, rtwdev->mac_id_map);
4350578bdd98SZong-Zhe Yang 	return mac_id;
4351578bdd98SZong-Zhe Yang }
4352578bdd98SZong-Zhe Yang 
rtw89_release_mac_id(struct rtw89_dev * rtwdev,u8 mac_id)4353578bdd98SZong-Zhe Yang void rtw89_release_mac_id(struct rtw89_dev *rtwdev, u8 mac_id)
4354578bdd98SZong-Zhe Yang {
4355578bdd98SZong-Zhe Yang 	clear_bit(mac_id, rtwdev->mac_id_map);
4356578bdd98SZong-Zhe Yang }
4357578bdd98SZong-Zhe Yang 
rtw89_core_init(struct rtw89_dev * rtwdev)4358e3ec7017SPing-Ke Shih int rtw89_core_init(struct rtw89_dev *rtwdev)
4359e3ec7017SPing-Ke Shih {
4360e3ec7017SPing-Ke Shih 	struct rtw89_btc *btc = &rtwdev->btc;
436189590777SPo Hao Huang 	u8 band;
4362e3ec7017SPing-Ke Shih 
4363e3ec7017SPing-Ke Shih 	INIT_LIST_HEAD(&rtwdev->ba_list);
4364679955d5SKuan-Chung Chen 	INIT_LIST_HEAD(&rtwdev->forbid_ba_list);
4365e3ec7017SPing-Ke Shih 	INIT_LIST_HEAD(&rtwdev->rtwvifs_list);
4366e3ec7017SPing-Ke Shih 	INIT_LIST_HEAD(&rtwdev->early_h2c_list);
436789590777SPo Hao Huang 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
436889590777SPo Hao Huang 		if (!(rtwdev->chip->support_bands & BIT(band)))
436989590777SPo Hao Huang 			continue;
437089590777SPo Hao Huang 		INIT_LIST_HEAD(&rtwdev->scan_info.pkt_list[band]);
437189590777SPo Hao Huang 	}
4372e3ec7017SPing-Ke Shih 	INIT_WORK(&rtwdev->ba_work, rtw89_core_ba_work);
4373e3ec7017SPing-Ke Shih 	INIT_WORK(&rtwdev->txq_work, rtw89_core_txq_work);
4374e3ec7017SPing-Ke Shih 	INIT_DELAYED_WORK(&rtwdev->txq_reinvoke_work, rtw89_core_txq_reinvoke_work);
4375e3ec7017SPing-Ke Shih 	INIT_DELAYED_WORK(&rtwdev->track_work, rtw89_track_work);
43764843aa37SZong-Zhe Yang 	INIT_DELAYED_WORK(&rtwdev->chanctx_work, rtw89_chanctx_work);
4377e3ec7017SPing-Ke Shih 	INIT_DELAYED_WORK(&rtwdev->coex_act1_work, rtw89_coex_act1_work);
4378e3ec7017SPing-Ke Shih 	INIT_DELAYED_WORK(&rtwdev->coex_bt_devinfo_work, rtw89_coex_bt_devinfo_work);
4379e3ec7017SPing-Ke Shih 	INIT_DELAYED_WORK(&rtwdev->coex_rfk_chk_work, rtw89_coex_rfk_chk_work);
4380e3ec7017SPing-Ke Shih 	INIT_DELAYED_WORK(&rtwdev->cfo_track_work, rtw89_phy_cfo_track_work);
4381679955d5SKuan-Chung Chen 	INIT_DELAYED_WORK(&rtwdev->forbid_ba_work, rtw89_forbid_ba_work);
4382e3715859SEric Huang 	INIT_DELAYED_WORK(&rtwdev->antdiv_work, rtw89_phy_antdiv_work);
4383e3ec7017SPing-Ke Shih 	rtwdev->txq_wq = alloc_workqueue("rtw89_tx_wq", WQ_UNBOUND | WQ_HIGHPRI, 0);
4384ed9e6166SJiasheng Jiang 	if (!rtwdev->txq_wq)
4385ed9e6166SJiasheng Jiang 		return -ENOMEM;
4386e3ec7017SPing-Ke Shih 	spin_lock_init(&rtwdev->ba_lock);
43877bfd05ffSChin-Yen Lee 	spin_lock_init(&rtwdev->rpwm_lock);
4388e3ec7017SPing-Ke Shih 	mutex_init(&rtwdev->mutex);
4389e3ec7017SPing-Ke Shih 	mutex_init(&rtwdev->rf_mutex);
4390e3ec7017SPing-Ke Shih 	rtwdev->total_sta_assoc = 0;
4391e3ec7017SPing-Ke Shih 
4392ef9dff4cSZong-Zhe Yang 	rtw89_init_wait(&rtwdev->mcc.wait);
43938febd68bSZong-Zhe Yang 	rtw89_init_wait(&rtwdev->mac.fw_ofld_wait);
4394d9dd3ac7SZong-Zhe Yang 	rtw89_init_wait(&rtwdev->wow.wait);
4395f6409a8aSChin-Yen Lee 	rtw89_init_wait(&rtwdev->mac.ps_wait);
4396ef9dff4cSZong-Zhe Yang 
4397e3ec7017SPing-Ke Shih 	INIT_WORK(&rtwdev->c2h_work, rtw89_fw_c2h_work);
439889590777SPo Hao Huang 	INIT_WORK(&rtwdev->ips_work, rtw89_ips_work);
4399b80ad23aSPing-Ke Shih 	INIT_WORK(&rtwdev->load_firmware_work, rtw89_load_firmware_work);
44003ea1cd8dSZong-Zhe Yang 	INIT_WORK(&rtwdev->cancel_6ghz_probe_work, rtw89_cancel_6ghz_probe_work);
4401b80ad23aSPing-Ke Shih 
4402e3ec7017SPing-Ke Shih 	skb_queue_head_init(&rtwdev->c2h_queue);
4403e3ec7017SPing-Ke Shih 	rtw89_core_ppdu_sts_init(rtwdev);
4404e3ec7017SPing-Ke Shih 	rtw89_traffic_stats_init(rtwdev, &rtwdev->stats);
4405e3ec7017SPing-Ke Shih 
4406e3ec7017SPing-Ke Shih 	rtwdev->hal.rx_fltr = DEFAULT_AX_RX_FLTR;
4407aacb84adSPing-Ke Shih 	rtwdev->dbcc_en = false;
4408aacb84adSPing-Ke Shih 	rtwdev->mlo_dbcc_mode = MLO_DBCC_NOT_SUPPORT;
44094ae8ac20SPing-Ke Shih 	rtwdev->mac.qta_mode = RTW89_QTA_SCC;
44104ae8ac20SPing-Ke Shih 
44114ae8ac20SPing-Ke Shih 	if (rtwdev->chip->chip_gen == RTW89_CHIP_BE) {
44124ae8ac20SPing-Ke Shih 		rtwdev->dbcc_en = true;
44134ae8ac20SPing-Ke Shih 		rtwdev->mac.qta_mode = RTW89_QTA_DBCC;
44144ae8ac20SPing-Ke Shih 		rtwdev->mlo_dbcc_mode = MLO_2_PLUS_0_1RF;
44154ae8ac20SPing-Ke Shih 	}
4416e3ec7017SPing-Ke Shih 
4417e3ec7017SPing-Ke Shih 	INIT_WORK(&btc->eapol_notify_work, rtw89_btc_ntfy_eapol_packet_work);
4418e3ec7017SPing-Ke Shih 	INIT_WORK(&btc->arp_notify_work, rtw89_btc_ntfy_arp_packet_work);
4419e3ec7017SPing-Ke Shih 	INIT_WORK(&btc->dhcp_notify_work, rtw89_btc_ntfy_dhcp_packet_work);
4420e3ec7017SPing-Ke Shih 	INIT_WORK(&btc->icmp_notify_work, rtw89_btc_ntfy_icmp_packet_work);
4421e3ec7017SPing-Ke Shih 
4422b80ad23aSPing-Ke Shih 	init_completion(&rtwdev->fw.req.completion);
4423ad1c86e9SPing-Ke Shih 	init_completion(&rtwdev->rfk_wait.completion);
4424b80ad23aSPing-Ke Shih 
4425b80ad23aSPing-Ke Shih 	schedule_work(&rtwdev->load_firmware_work);
4426b80ad23aSPing-Ke Shih 
4427e3ec7017SPing-Ke Shih 	rtw89_ser_init(rtwdev);
4428a88b6cc4SZong-Zhe Yang 	rtw89_entity_init(rtwdev);
4429eb2624f5SKuan-Chung Chen 	rtw89_tas_init(rtwdev);
4430e3ec7017SPing-Ke Shih 
4431e3ec7017SPing-Ke Shih 	return 0;
4432e3ec7017SPing-Ke Shih }
4433e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_init);
4434e3ec7017SPing-Ke Shih 
rtw89_core_deinit(struct rtw89_dev * rtwdev)4435e3ec7017SPing-Ke Shih void rtw89_core_deinit(struct rtw89_dev *rtwdev)
4436e3ec7017SPing-Ke Shih {
4437e3ec7017SPing-Ke Shih 	rtw89_ser_deinit(rtwdev);
4438e3ec7017SPing-Ke Shih 	rtw89_unload_firmware(rtwdev);
4439e3ec7017SPing-Ke Shih 	rtw89_fw_free_all_early_h2c(rtwdev);
4440e3ec7017SPing-Ke Shih 
4441e3ec7017SPing-Ke Shih 	destroy_workqueue(rtwdev->txq_wq);
4442e3ec7017SPing-Ke Shih 	mutex_destroy(&rtwdev->rf_mutex);
4443e3ec7017SPing-Ke Shih 	mutex_destroy(&rtwdev->mutex);
4444e3ec7017SPing-Ke Shih }
4445e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_deinit);
4446e3ec7017SPing-Ke Shih 
rtw89_core_scan_start(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,const u8 * mac_addr,bool hw_scan)444789590777SPo Hao Huang void rtw89_core_scan_start(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
444889590777SPo Hao Huang 			   const u8 *mac_addr, bool hw_scan)
444989590777SPo Hao Huang {
4450ad3dc722SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
4451583e998eSZong-Zhe Yang 						       rtwvif->chanctx_idx);
445289590777SPo Hao Huang 
445389590777SPo Hao Huang 	rtwdev->scanning = true;
445489590777SPo Hao Huang 	rtw89_leave_lps(rtwdev);
44556cfb6cc2SPo-Hao Huang 	if (hw_scan)
44566cfb6cc2SPo-Hao Huang 		rtw89_leave_ips_by_hwflags(rtwdev);
445789590777SPo Hao Huang 
445889590777SPo Hao Huang 	ether_addr_copy(rtwvif->mac_addr, mac_addr);
4459cbb145b9SZong-Zhe Yang 	rtw89_btc_ntfy_scan_start(rtwdev, RTW89_PHY_0, chan->band_type);
4460ed5f66a2SZong-Zhe Yang 	rtw89_chip_rfk_scan(rtwdev, rtwvif, true);
446189590777SPo Hao Huang 	rtw89_hci_recalc_int_mit(rtwdev);
4462280c4447SChih-Kang Chang 	rtw89_phy_config_edcca(rtwdev, true);
446389590777SPo Hao Huang 
446489590777SPo Hao Huang 	rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, mac_addr);
446589590777SPo Hao Huang }
446689590777SPo Hao Huang 
rtw89_core_scan_complete(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,bool hw_scan)446789590777SPo Hao Huang void rtw89_core_scan_complete(struct rtw89_dev *rtwdev,
446889590777SPo Hao Huang 			      struct ieee80211_vif *vif, bool hw_scan)
446989590777SPo Hao Huang {
44706d7d1fefSPo Hao Huang 	struct rtw89_vif *rtwvif = vif ? (struct rtw89_vif *)vif->drv_priv : NULL;
44716d7d1fefSPo Hao Huang 
44726d7d1fefSPo Hao Huang 	if (!rtwvif)
44736d7d1fefSPo Hao Huang 		return;
447489590777SPo Hao Huang 
447589590777SPo Hao Huang 	ether_addr_copy(rtwvif->mac_addr, vif->addr);
447689590777SPo Hao Huang 	rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
447789590777SPo Hao Huang 
4478ed5f66a2SZong-Zhe Yang 	rtw89_chip_rfk_scan(rtwdev, rtwvif, false);
447989590777SPo Hao Huang 	rtw89_btc_ntfy_scan_finish(rtwdev, RTW89_PHY_0);
4480280c4447SChih-Kang Chang 	rtw89_phy_config_edcca(rtwdev, false);
448189590777SPo Hao Huang 
448289590777SPo Hao Huang 	rtwdev->scanning = false;
448389590777SPo Hao Huang 	rtwdev->dig.bypass_dig = true;
4484ee20d538SPo Hao Huang 	if (hw_scan && (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE))
448589590777SPo Hao Huang 		ieee80211_queue_work(rtwdev->hw, &rtwdev->ips_work);
448689590777SPo Hao Huang }
448789590777SPo Hao Huang 
rtw89_read_chip_ver(struct rtw89_dev * rtwdev)4488e3ec7017SPing-Ke Shih static void rtw89_read_chip_ver(struct rtw89_dev *rtwdev)
4489e3ec7017SPing-Ke Shih {
4490a8bdac12SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
4491a6fb2bb8SPing-Ke Shih 	int ret;
4492a6fb2bb8SPing-Ke Shih 	u8 val;
4493e3ec7017SPing-Ke Shih 	u8 cv;
4494e3ec7017SPing-Ke Shih 
4495e3ec7017SPing-Ke Shih 	cv = rtw89_read32_mask(rtwdev, R_AX_SYS_CFG1, B_AX_CHIP_VER_MASK);
4496a8bdac12SPing-Ke Shih 	if (chip->chip_id == RTL8852A && cv <= CHIP_CBV) {
4497e3ec7017SPing-Ke Shih 		if (rtw89_read32(rtwdev, R_AX_GPIO0_7_FUNC_SEL) == RTW89_R32_DEAD)
4498e3ec7017SPing-Ke Shih 			cv = CHIP_CAV;
4499e3ec7017SPing-Ke Shih 		else
4500e3ec7017SPing-Ke Shih 			cv = CHIP_CBV;
4501e3ec7017SPing-Ke Shih 	}
4502e3ec7017SPing-Ke Shih 
4503e3ec7017SPing-Ke Shih 	rtwdev->hal.cv = cv;
4504a6fb2bb8SPing-Ke Shih 
4505190dc12dSPing-Ke Shih 	if (rtw89_is_rtl885xb(rtwdev)) {
4506a6fb2bb8SPing-Ke Shih 		ret = rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_CV, &val);
45079d4f491bSDan Carpenter 		if (ret)
4508a6fb2bb8SPing-Ke Shih 			return;
4509a6fb2bb8SPing-Ke Shih 
4510a6fb2bb8SPing-Ke Shih 		rtwdev->hal.acv = u8_get_bits(val, XTAL_SI_ACV_MASK);
4511a6fb2bb8SPing-Ke Shih 	}
4512e3ec7017SPing-Ke Shih }
4513e3ec7017SPing-Ke Shih 
rtw89_core_setup_phycap(struct rtw89_dev * rtwdev)45141c2423deSJohnson Lin static void rtw89_core_setup_phycap(struct rtw89_dev *rtwdev)
45151c2423deSJohnson Lin {
45161c2423deSJohnson Lin 	rtwdev->hal.support_cckpd =
45171c2423deSJohnson Lin 		!(rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv <= CHIP_CBV) &&
45181c2423deSJohnson Lin 		!(rtwdev->chip->chip_id == RTL8852B && rtwdev->hal.cv <= CHIP_CAV);
45191e6f0d2aSJohnson Lin 	rtwdev->hal.support_igi =
45201e6f0d2aSJohnson Lin 		rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv <= CHIP_CBV;
45211c2423deSJohnson Lin }
45221c2423deSJohnson Lin 
rtw89_core_setup_rfe_parms(struct rtw89_dev * rtwdev)45235395482aSZong-Zhe Yang static void rtw89_core_setup_rfe_parms(struct rtw89_dev *rtwdev)
45245395482aSZong-Zhe Yang {
45255395482aSZong-Zhe Yang 	const struct rtw89_chip_info *chip = rtwdev->chip;
45265395482aSZong-Zhe Yang 	const struct rtw89_rfe_parms_conf *conf = chip->rfe_parms_conf;
45275395482aSZong-Zhe Yang 	struct rtw89_efuse *efuse = &rtwdev->efuse;
45289707ea6dSZong-Zhe Yang 	const struct rtw89_rfe_parms *sel;
45295395482aSZong-Zhe Yang 	u8 rfe_type = efuse->rfe_type;
45305395482aSZong-Zhe Yang 
45319707ea6dSZong-Zhe Yang 	if (!conf) {
45329707ea6dSZong-Zhe Yang 		sel = chip->dflt_parms;
45335395482aSZong-Zhe Yang 		goto out;
45349707ea6dSZong-Zhe Yang 	}
45355395482aSZong-Zhe Yang 
45365395482aSZong-Zhe Yang 	while (conf->rfe_parms) {
45375395482aSZong-Zhe Yang 		if (rfe_type == conf->rfe_type) {
45389707ea6dSZong-Zhe Yang 			sel = conf->rfe_parms;
45399707ea6dSZong-Zhe Yang 			goto out;
45405395482aSZong-Zhe Yang 		}
45415395482aSZong-Zhe Yang 		conf++;
45425395482aSZong-Zhe Yang 	}
45435395482aSZong-Zhe Yang 
45449707ea6dSZong-Zhe Yang 	sel = chip->dflt_parms;
45459707ea6dSZong-Zhe Yang 
45465395482aSZong-Zhe Yang out:
45475ee7b2eaSZong-Zhe Yang 	rtwdev->rfe_parms = rtw89_load_rfe_data_from_fw(rtwdev, sel);
45485ee7b2eaSZong-Zhe Yang 	rtw89_load_txpwr_table(rtwdev, rtwdev->rfe_parms->byr_tbl);
45495395482aSZong-Zhe Yang }
45505395482aSZong-Zhe Yang 
rtw89_chip_efuse_info_setup(struct rtw89_dev * rtwdev)4551e3ec7017SPing-Ke Shih static int rtw89_chip_efuse_info_setup(struct rtw89_dev *rtwdev)
4552e3ec7017SPing-Ke Shih {
455388e6a923SPing-Ke Shih 	const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
4554e3ec7017SPing-Ke Shih 	int ret;
4555e3ec7017SPing-Ke Shih 
4556a712eef6SPing-Ke Shih 	ret = rtw89_mac_partial_init(rtwdev, false);
4557e3ec7017SPing-Ke Shih 	if (ret)
4558e3ec7017SPing-Ke Shih 		return ret;
4559e3ec7017SPing-Ke Shih 
456088e6a923SPing-Ke Shih 	ret = mac->parse_efuse_map(rtwdev);
4561e3ec7017SPing-Ke Shih 	if (ret)
4562e3ec7017SPing-Ke Shih 		return ret;
4563e3ec7017SPing-Ke Shih 
456488e6a923SPing-Ke Shih 	ret = mac->parse_phycap_map(rtwdev);
4565e3ec7017SPing-Ke Shih 	if (ret)
4566e3ec7017SPing-Ke Shih 		return ret;
4567e3ec7017SPing-Ke Shih 
4568e3ec7017SPing-Ke Shih 	ret = rtw89_mac_setup_phycap(rtwdev);
4569e3ec7017SPing-Ke Shih 	if (ret)
4570e3ec7017SPing-Ke Shih 		return ret;
4571e3ec7017SPing-Ke Shih 
45721c2423deSJohnson Lin 	rtw89_core_setup_phycap(rtwdev);
45731c2423deSJohnson Lin 
45749e1aff43SPing-Ke Shih 	rtw89_hci_mac_pre_deinit(rtwdev);
45759e1aff43SPing-Ke Shih 
4576e3ec7017SPing-Ke Shih 	rtw89_mac_pwr_off(rtwdev);
4577e3ec7017SPing-Ke Shih 
4578e3ec7017SPing-Ke Shih 	return 0;
4579e3ec7017SPing-Ke Shih }
4580e3ec7017SPing-Ke Shih 
rtw89_chip_board_info_setup(struct rtw89_dev * rtwdev)4581e3ec7017SPing-Ke Shih static int rtw89_chip_board_info_setup(struct rtw89_dev *rtwdev)
4582e3ec7017SPing-Ke Shih {
4583e3ec7017SPing-Ke Shih 	rtw89_chip_fem_setup(rtwdev);
4584e3ec7017SPing-Ke Shih 
4585e3ec7017SPing-Ke Shih 	return 0;
4586e3ec7017SPing-Ke Shih }
4587e3ec7017SPing-Ke Shih 
rtw89_chip_has_rfkill(struct rtw89_dev * rtwdev)45880b38e627SKuan-Chung Chen static bool rtw89_chip_has_rfkill(struct rtw89_dev *rtwdev)
45890b38e627SKuan-Chung Chen {
45900b38e627SKuan-Chung Chen 	return !!rtwdev->chip->rfkill_init;
45910b38e627SKuan-Chung Chen }
45920b38e627SKuan-Chung Chen 
rtw89_core_rfkill_init(struct rtw89_dev * rtwdev)45930b38e627SKuan-Chung Chen static void rtw89_core_rfkill_init(struct rtw89_dev *rtwdev)
45940b38e627SKuan-Chung Chen {
45950b38e627SKuan-Chung Chen 	const struct rtw89_rfkill_regs *regs = rtwdev->chip->rfkill_init;
45960b38e627SKuan-Chung Chen 
45970b38e627SKuan-Chung Chen 	rtw89_write16_mask(rtwdev, regs->pinmux.addr,
45980b38e627SKuan-Chung Chen 			   regs->pinmux.mask, regs->pinmux.data);
45990b38e627SKuan-Chung Chen 	rtw89_write16_mask(rtwdev, regs->mode.addr,
46000b38e627SKuan-Chung Chen 			   regs->mode.mask, regs->mode.data);
46010b38e627SKuan-Chung Chen }
46020b38e627SKuan-Chung Chen 
rtw89_core_rfkill_get(struct rtw89_dev * rtwdev)46030b38e627SKuan-Chung Chen static bool rtw89_core_rfkill_get(struct rtw89_dev *rtwdev)
46040b38e627SKuan-Chung Chen {
46050b38e627SKuan-Chung Chen 	const struct rtw89_reg_def *reg = &rtwdev->chip->rfkill_get;
46060b38e627SKuan-Chung Chen 
46070b38e627SKuan-Chung Chen 	return !rtw89_read8_mask(rtwdev, reg->addr, reg->mask);
46080b38e627SKuan-Chung Chen }
46090b38e627SKuan-Chung Chen 
rtw89_rfkill_polling_init(struct rtw89_dev * rtwdev)46100b38e627SKuan-Chung Chen static void rtw89_rfkill_polling_init(struct rtw89_dev *rtwdev)
46110b38e627SKuan-Chung Chen {
46120b38e627SKuan-Chung Chen 	if (!rtw89_chip_has_rfkill(rtwdev))
46130b38e627SKuan-Chung Chen 		return;
46140b38e627SKuan-Chung Chen 
46150b38e627SKuan-Chung Chen 	rtw89_core_rfkill_init(rtwdev);
46160b38e627SKuan-Chung Chen 	rtw89_core_rfkill_poll(rtwdev, true);
46170b38e627SKuan-Chung Chen 	wiphy_rfkill_start_polling(rtwdev->hw->wiphy);
46180b38e627SKuan-Chung Chen }
46190b38e627SKuan-Chung Chen 
rtw89_rfkill_polling_deinit(struct rtw89_dev * rtwdev)46200b38e627SKuan-Chung Chen static void rtw89_rfkill_polling_deinit(struct rtw89_dev *rtwdev)
46210b38e627SKuan-Chung Chen {
46220b38e627SKuan-Chung Chen 	if (!rtw89_chip_has_rfkill(rtwdev))
46230b38e627SKuan-Chung Chen 		return;
46240b38e627SKuan-Chung Chen 
46250b38e627SKuan-Chung Chen 	wiphy_rfkill_stop_polling(rtwdev->hw->wiphy);
46260b38e627SKuan-Chung Chen }
46270b38e627SKuan-Chung Chen 
rtw89_core_rfkill_poll(struct rtw89_dev * rtwdev,bool force)46280b38e627SKuan-Chung Chen void rtw89_core_rfkill_poll(struct rtw89_dev *rtwdev, bool force)
46290b38e627SKuan-Chung Chen {
46300b38e627SKuan-Chung Chen 	bool prev, blocked;
46310b38e627SKuan-Chung Chen 
46320b38e627SKuan-Chung Chen 	if (!rtw89_chip_has_rfkill(rtwdev))
46330b38e627SKuan-Chung Chen 		return;
46340b38e627SKuan-Chung Chen 
46350b38e627SKuan-Chung Chen 	prev = test_bit(RTW89_FLAG_HW_RFKILL_STATE, rtwdev->flags);
46360b38e627SKuan-Chung Chen 	blocked = rtw89_core_rfkill_get(rtwdev);
46370b38e627SKuan-Chung Chen 
46380b38e627SKuan-Chung Chen 	if (!force && prev == blocked)
46390b38e627SKuan-Chung Chen 		return;
46400b38e627SKuan-Chung Chen 
46410b38e627SKuan-Chung Chen 	rtw89_info(rtwdev, "rfkill hardware state changed to %s\n",
46420b38e627SKuan-Chung Chen 		   blocked ? "disable" : "enable");
46430b38e627SKuan-Chung Chen 
46440b38e627SKuan-Chung Chen 	if (blocked)
46450b38e627SKuan-Chung Chen 		set_bit(RTW89_FLAG_HW_RFKILL_STATE, rtwdev->flags);
46460b38e627SKuan-Chung Chen 	else
46470b38e627SKuan-Chung Chen 		clear_bit(RTW89_FLAG_HW_RFKILL_STATE, rtwdev->flags);
46480b38e627SKuan-Chung Chen 
46490b38e627SKuan-Chung Chen 	wiphy_rfkill_set_hw_state(rtwdev->hw->wiphy, blocked);
46500b38e627SKuan-Chung Chen }
46510b38e627SKuan-Chung Chen 
rtw89_chip_info_setup(struct rtw89_dev * rtwdev)4652e3ec7017SPing-Ke Shih int rtw89_chip_info_setup(struct rtw89_dev *rtwdev)
4653e3ec7017SPing-Ke Shih {
4654e3ec7017SPing-Ke Shih 	int ret;
4655e3ec7017SPing-Ke Shih 
4656e3ec7017SPing-Ke Shih 	rtw89_read_chip_ver(rtwdev);
4657e3ec7017SPing-Ke Shih 
4658e3ec7017SPing-Ke Shih 	ret = rtw89_wait_firmware_completion(rtwdev);
4659e3ec7017SPing-Ke Shih 	if (ret) {
4660e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to wait firmware completion\n");
4661e3ec7017SPing-Ke Shih 		return ret;
4662e3ec7017SPing-Ke Shih 	}
4663e3ec7017SPing-Ke Shih 
4664e3ec7017SPing-Ke Shih 	ret = rtw89_fw_recognize(rtwdev);
4665e3ec7017SPing-Ke Shih 	if (ret) {
4666e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to recognize firmware\n");
4667e3ec7017SPing-Ke Shih 		return ret;
4668e3ec7017SPing-Ke Shih 	}
4669e3ec7017SPing-Ke Shih 
46705ee7b2eaSZong-Zhe Yang 	ret = rtw89_chip_efuse_info_setup(rtwdev);
46715ee7b2eaSZong-Zhe Yang 	if (ret)
46725ee7b2eaSZong-Zhe Yang 		return ret;
46735ee7b2eaSZong-Zhe Yang 
4674a337d433SPing-Ke Shih 	ret = rtw89_fw_recognize_elements(rtwdev);
4675a337d433SPing-Ke Shih 	if (ret) {
4676a337d433SPing-Ke Shih 		rtw89_err(rtwdev, "failed to recognize firmware elements\n");
4677a337d433SPing-Ke Shih 		return ret;
4678a337d433SPing-Ke Shih 	}
4679a337d433SPing-Ke Shih 
4680e3ec7017SPing-Ke Shih 	ret = rtw89_chip_board_info_setup(rtwdev);
4681e3ec7017SPing-Ke Shih 	if (ret)
4682e3ec7017SPing-Ke Shih 		return ret;
4683e3ec7017SPing-Ke Shih 
46845ee7b2eaSZong-Zhe Yang 	rtw89_core_setup_rfe_parms(rtwdev);
4685e5624482SPing-Ke Shih 	rtwdev->ps_mode = rtw89_update_ps_mode(rtwdev);
4686e5624482SPing-Ke Shih 
4687e3ec7017SPing-Ke Shih 	return 0;
4688e3ec7017SPing-Ke Shih }
4689e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_chip_info_setup);
4690e3ec7017SPing-Ke Shih 
rtw89_core_register_hw(struct rtw89_dev * rtwdev)4691e3ec7017SPing-Ke Shih static int rtw89_core_register_hw(struct rtw89_dev *rtwdev)
4692e3ec7017SPing-Ke Shih {
46934f47e0cfSPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
4694e3ec7017SPing-Ke Shih 	struct ieee80211_hw *hw = rtwdev->hw;
4695e3ec7017SPing-Ke Shih 	struct rtw89_efuse *efuse = &rtwdev->efuse;
4696f48453e0SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
4697e3ec7017SPing-Ke Shih 	int ret;
4698e3ec7017SPing-Ke Shih 	int tx_headroom = IEEE80211_HT_CTL_LEN;
4699e3ec7017SPing-Ke Shih 
4700e3ec7017SPing-Ke Shih 	hw->vif_data_size = sizeof(struct rtw89_vif);
4701e3ec7017SPing-Ke Shih 	hw->sta_data_size = sizeof(struct rtw89_sta);
4702e3ec7017SPing-Ke Shih 	hw->txq_data_size = sizeof(struct rtw89_txq);
470384b50f41SZong-Zhe Yang 	hw->chanctx_data_size = sizeof(struct rtw89_chanctx_cfg);
4704e3ec7017SPing-Ke Shih 
4705e3ec7017SPing-Ke Shih 	SET_IEEE80211_PERM_ADDR(hw, efuse->addr);
4706e3ec7017SPing-Ke Shih 
4707e3ec7017SPing-Ke Shih 	hw->extra_tx_headroom = tx_headroom;
4708e3ec7017SPing-Ke Shih 	hw->queues = IEEE80211_NUM_ACS;
4709e3ec7017SPing-Ke Shih 	hw->max_rx_aggregation_subframes = RTW89_MAX_RX_AGG_NUM;
4710e3ec7017SPing-Ke Shih 	hw->max_tx_aggregation_subframes = RTW89_MAX_TX_AGG_NUM;
471171392bb2SDian-Syuan Yang 	hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
4712e3ec7017SPing-Ke Shih 
471385e02a0aSPing-Ke Shih 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
471485e02a0aSPing-Ke Shih 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
471585e02a0aSPing-Ke Shih 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC;
471685e02a0aSPing-Ke Shih 
4717e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, SIGNAL_DBM);
4718e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
4719e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, MFP_CAPABLE);
4720e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
4721e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
4722e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
4723e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, TX_AMSDU);
4724e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
4725e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
4726e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, SUPPORTS_PS);
4727e3ec7017SPing-Ke Shih 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
472889590777SPo Hao Huang 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
472985393ca6SPing-Ke Shih 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
4730cd9b6b3bSZong-Zhe Yang 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
4731b09df09bSZong-Zhe Yang 
47324f47e0cfSPing-Ke Shih 	if (chip->support_bandwidths & BIT(NL80211_CHAN_WIDTH_160))
47334f47e0cfSPing-Ke Shih 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
47344f47e0cfSPing-Ke Shih 
4735d2b6da24SPo-Hao Huang 	if (RTW89_CHK_FW_FEATURE(BEACON_FILTER, &rtwdev->fw))
4736d2b6da24SPo-Hao Huang 		ieee80211_hw_set(hw, CONNECTION_MONITOR);
4737e3ec7017SPing-Ke Shih 
4738b478ff6bSPing-Ke Shih 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
4739e8ecfdd6SPo Hao Huang 				     BIT(NL80211_IFTYPE_AP) |
4740e8ecfdd6SPo Hao Huang 				     BIT(NL80211_IFTYPE_P2P_CLIENT) |
4741e8ecfdd6SPo Hao Huang 				     BIT(NL80211_IFTYPE_P2P_GO);
4742e8ecfdd6SPo Hao Huang 
4743f48453e0SPing-Ke Shih 	if (hal->ant_diversity) {
4744f48453e0SPing-Ke Shih 		hw->wiphy->available_antennas_tx = 0x3;
4745f48453e0SPing-Ke Shih 		hw->wiphy->available_antennas_rx = 0x3;
4746f48453e0SPing-Ke Shih 	} else {
4747e3ec7017SPing-Ke Shih 		hw->wiphy->available_antennas_tx = BIT(rtwdev->chip->rf_path_num) - 1;
4748e3ec7017SPing-Ke Shih 		hw->wiphy->available_antennas_rx = BIT(rtwdev->chip->rf_path_num) - 1;
4749f48453e0SPing-Ke Shih 	}
4750e3ec7017SPing-Ke Shih 
47516a5a783aSPing-Ke Shih 	hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
475271392bb2SDian-Syuan Yang 			    WIPHY_FLAG_TDLS_EXTERNAL_SETUP |
4753ed9a3c0dSChih-Kang Chang 			    WIPHY_FLAG_AP_UAPSD |
4754ed9a3c0dSChih-Kang Chang 			    WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
4755e2e32a19SPo-Hao Huang 
4756e2e32a19SPo-Hao Huang 	if (!chip->support_rnr)
4757e2e32a19SPo-Hao Huang 		hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
4758e2e32a19SPo-Hao Huang 
4759d50334c3SZong-Zhe Yang 	if (chip->chip_gen == RTW89_CHIP_BE)
4760d50334c3SZong-Zhe Yang 		hw->wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
4761d50334c3SZong-Zhe Yang 
4762fef63150SZong-Zhe Yang 	if (rtwdev->support_mlo)
4763fef63150SZong-Zhe Yang 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
4764fef63150SZong-Zhe Yang 
4765e3ec7017SPing-Ke Shih 	hw->wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
4766e3ec7017SPing-Ke Shih 
476789590777SPo Hao Huang 	hw->wiphy->max_scan_ssids = RTW89_SCANOFLD_MAX_SSID;
476889590777SPo Hao Huang 	hw->wiphy->max_scan_ie_len = RTW89_SCANOFLD_MAX_IE_LEN;
476989590777SPo Hao Huang 
477019e28c7fSChin-Yen Lee #ifdef CONFIG_PM
477119e28c7fSChin-Yen Lee 	hw->wiphy->wowlan = rtwdev->chip->wowlan_stub;
4772c0bc1bceSChin-Yen Lee 	hw->wiphy->max_sched_scan_ssids = RTW89_SCANOFLD_MAX_SSID;
477319e28c7fSChin-Yen Lee #endif
477419e28c7fSChin-Yen Lee 
47753004a0a4SKuan-Chung Chen 	hw->wiphy->tid_config_support.vif |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
47763004a0a4SKuan-Chung Chen 	hw->wiphy->tid_config_support.peer |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
4777f3d82323SKuan-Chung Chen 	hw->wiphy->tid_config_support.vif |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
4778f3d82323SKuan-Chung Chen 	hw->wiphy->tid_config_support.peer |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
4779a0e97ae3SPo-Hao Huang 	hw->wiphy->max_remain_on_channel_duration = 1000;
47803004a0a4SKuan-Chung Chen 
4781e3ec7017SPing-Ke Shih 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
4782dc10daddSKuan-Chung Chen 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_SCAN_RANDOM_SN);
4783dc10daddSKuan-Chung Chen 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL);
4784e3ec7017SPing-Ke Shih 
4785e3ec7017SPing-Ke Shih 	ret = rtw89_core_set_supported_band(rtwdev);
4786e3ec7017SPing-Ke Shih 	if (ret) {
4787e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to set supported band\n");
4788e3ec7017SPing-Ke Shih 		return ret;
4789e3ec7017SPing-Ke Shih 	}
4790e3ec7017SPing-Ke Shih 
4791a002f981SZong-Zhe Yang 	ret = rtw89_regd_setup(rtwdev);
4792a002f981SZong-Zhe Yang 	if (ret) {
4793a002f981SZong-Zhe Yang 		rtw89_err(rtwdev, "failed to set up regd\n");
4794a002f981SZong-Zhe Yang 		goto err_free_supported_band;
4795a002f981SZong-Zhe Yang 	}
4796a002f981SZong-Zhe Yang 
4797e3ec7017SPing-Ke Shih 	hw->wiphy->sar_capa = &rtw89_sar_capa;
4798e3ec7017SPing-Ke Shih 
4799e3ec7017SPing-Ke Shih 	ret = ieee80211_register_hw(hw);
4800e3ec7017SPing-Ke Shih 	if (ret) {
4801e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to register hw\n");
480247515664SPing-Ke Shih 		goto err_free_supported_band;
4803e3ec7017SPing-Ke Shih 	}
4804e3ec7017SPing-Ke Shih 
4805e3ec7017SPing-Ke Shih 	ret = rtw89_regd_init(rtwdev, rtw89_regd_notifier);
4806e3ec7017SPing-Ke Shih 	if (ret) {
4807e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to init regd\n");
480847515664SPing-Ke Shih 		goto err_unregister_hw;
4809e3ec7017SPing-Ke Shih 	}
4810e3ec7017SPing-Ke Shih 
48110b38e627SKuan-Chung Chen 	rtw89_rfkill_polling_init(rtwdev);
48120b38e627SKuan-Chung Chen 
4813e3ec7017SPing-Ke Shih 	return 0;
4814e3ec7017SPing-Ke Shih 
481547515664SPing-Ke Shih err_unregister_hw:
481647515664SPing-Ke Shih 	ieee80211_unregister_hw(hw);
481747515664SPing-Ke Shih err_free_supported_band:
481847515664SPing-Ke Shih 	rtw89_core_clr_supported_band(rtwdev);
481947515664SPing-Ke Shih 
4820e3ec7017SPing-Ke Shih 	return ret;
4821e3ec7017SPing-Ke Shih }
4822e3ec7017SPing-Ke Shih 
rtw89_core_unregister_hw(struct rtw89_dev * rtwdev)4823e3ec7017SPing-Ke Shih static void rtw89_core_unregister_hw(struct rtw89_dev *rtwdev)
4824e3ec7017SPing-Ke Shih {
4825e3ec7017SPing-Ke Shih 	struct ieee80211_hw *hw = rtwdev->hw;
4826e3ec7017SPing-Ke Shih 
48270b38e627SKuan-Chung Chen 	rtw89_rfkill_polling_deinit(rtwdev);
4828e3ec7017SPing-Ke Shih 	ieee80211_unregister_hw(hw);
4829e3ec7017SPing-Ke Shih 	rtw89_core_clr_supported_band(rtwdev);
4830e3ec7017SPing-Ke Shih }
4831e3ec7017SPing-Ke Shih 
rtw89_core_register(struct rtw89_dev * rtwdev)4832e3ec7017SPing-Ke Shih int rtw89_core_register(struct rtw89_dev *rtwdev)
4833e3ec7017SPing-Ke Shih {
4834e3ec7017SPing-Ke Shih 	int ret;
4835e3ec7017SPing-Ke Shih 
4836e3ec7017SPing-Ke Shih 	ret = rtw89_core_register_hw(rtwdev);
4837e3ec7017SPing-Ke Shih 	if (ret) {
4838e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to register core hw\n");
4839e3ec7017SPing-Ke Shih 		return ret;
4840e3ec7017SPing-Ke Shih 	}
4841e3ec7017SPing-Ke Shih 
4842e3ec7017SPing-Ke Shih 	rtw89_debugfs_init(rtwdev);
4843e3ec7017SPing-Ke Shih 
4844e3ec7017SPing-Ke Shih 	return 0;
4845e3ec7017SPing-Ke Shih }
4846e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_register);
4847e3ec7017SPing-Ke Shih 
rtw89_core_unregister(struct rtw89_dev * rtwdev)4848e3ec7017SPing-Ke Shih void rtw89_core_unregister(struct rtw89_dev *rtwdev)
4849e3ec7017SPing-Ke Shih {
4850e3ec7017SPing-Ke Shih 	rtw89_core_unregister_hw(rtwdev);
4851a3f00afcSPing-Ke Shih 
4852a3f00afcSPing-Ke Shih 	rtw89_debugfs_deinit(rtwdev);
4853e3ec7017SPing-Ke Shih }
4854e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_core_unregister);
4855e3ec7017SPing-Ke Shih 
rtw89_alloc_ieee80211_hw(struct device * device,u32 bus_data_size,const struct rtw89_chip_info * chip)48567fc06a07SZong-Zhe Yang struct rtw89_dev *rtw89_alloc_ieee80211_hw(struct device *device,
48577fc06a07SZong-Zhe Yang 					   u32 bus_data_size,
48587fc06a07SZong-Zhe Yang 					   const struct rtw89_chip_info *chip)
48597fc06a07SZong-Zhe Yang {
4860639ec6d6SZong-Zhe Yang 	struct rtw89_fw_info early_fw = {};
486113eb07e0SZong-Zhe Yang 	const struct firmware *firmware;
48627fc06a07SZong-Zhe Yang 	struct ieee80211_hw *hw;
48637fc06a07SZong-Zhe Yang 	struct rtw89_dev *rtwdev;
48647fc06a07SZong-Zhe Yang 	struct ieee80211_ops *ops;
48657fc06a07SZong-Zhe Yang 	u32 driver_data_size;
4866ffde7f34SPing-Ke Shih 	int fw_format = -1;
4867fef63150SZong-Zhe Yang 	bool support_mlo;
4868deebea35SZong-Zhe Yang 	bool no_chanctx;
4869deebea35SZong-Zhe Yang 
4870ffde7f34SPing-Ke Shih 	firmware = rtw89_early_fw_feature_recognize(device, chip, &early_fw, &fw_format);
48717fc06a07SZong-Zhe Yang 
48727fc06a07SZong-Zhe Yang 	ops = kmemdup(&rtw89_ops, sizeof(rtw89_ops), GFP_KERNEL);
48737fc06a07SZong-Zhe Yang 	if (!ops)
48747fc06a07SZong-Zhe Yang 		goto err;
48757fc06a07SZong-Zhe Yang 
4876deebea35SZong-Zhe Yang 	no_chanctx = chip->support_chanctx_num == 0 ||
4877639ec6d6SZong-Zhe Yang 		     !RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &early_fw) ||
4878639ec6d6SZong-Zhe Yang 		     !RTW89_CHK_FW_FEATURE(BEACON_FILTER, &early_fw);
4879deebea35SZong-Zhe Yang 
4880deebea35SZong-Zhe Yang 	if (no_chanctx) {
48810a44dfc0SJohannes Berg 		ops->add_chanctx = ieee80211_emulate_add_chanctx;
48820a44dfc0SJohannes Berg 		ops->remove_chanctx = ieee80211_emulate_remove_chanctx;
48830a44dfc0SJohannes Berg 		ops->change_chanctx = ieee80211_emulate_change_chanctx;
48840a44dfc0SJohannes Berg 		ops->switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx;
48857fc06a07SZong-Zhe Yang 		ops->assign_vif_chanctx = NULL;
48867fc06a07SZong-Zhe Yang 		ops->unassign_vif_chanctx = NULL;
4887a0e97ae3SPo-Hao Huang 		ops->remain_on_channel = NULL;
4888a0e97ae3SPo-Hao Huang 		ops->cancel_remain_on_channel = NULL;
48897fc06a07SZong-Zhe Yang 	}
48907fc06a07SZong-Zhe Yang 
48917fc06a07SZong-Zhe Yang 	driver_data_size = sizeof(struct rtw89_dev) + bus_data_size;
48927fc06a07SZong-Zhe Yang 	hw = ieee80211_alloc_hw(driver_data_size, ops);
48937fc06a07SZong-Zhe Yang 	if (!hw)
48947fc06a07SZong-Zhe Yang 		goto err;
48957fc06a07SZong-Zhe Yang 
4896fef63150SZong-Zhe Yang 	/* TODO: When driver MLO arch. is done, determine whether to support MLO
4897fef63150SZong-Zhe Yang 	 * according to the following conditions.
4898fef63150SZong-Zhe Yang 	 * 1. run with chanctx_ops
4899fef63150SZong-Zhe Yang 	 * 2. chip->support_link_num != 0
4900fef63150SZong-Zhe Yang 	 * 3. FW feature supports AP_LINK_PS
4901fef63150SZong-Zhe Yang 	 */
4902fef63150SZong-Zhe Yang 	support_mlo = false;
4903fef63150SZong-Zhe Yang 
4904f22c0bffSPo-Hao Huang 	hw->wiphy->iface_combinations = rtw89_iface_combs;
49058e73c045SZong-Zhe Yang 
49068e73c045SZong-Zhe Yang 	if (no_chanctx || chip->support_chanctx_num == 1)
49078e73c045SZong-Zhe Yang 		hw->wiphy->n_iface_combinations = 1;
49088e73c045SZong-Zhe Yang 	else
4909f22c0bffSPo-Hao Huang 		hw->wiphy->n_iface_combinations = ARRAY_SIZE(rtw89_iface_combs);
4910f22c0bffSPo-Hao Huang 
49117fc06a07SZong-Zhe Yang 	rtwdev = hw->priv;
49127fc06a07SZong-Zhe Yang 	rtwdev->hw = hw;
49137fc06a07SZong-Zhe Yang 	rtwdev->dev = device;
49147fc06a07SZong-Zhe Yang 	rtwdev->ops = ops;
49157fc06a07SZong-Zhe Yang 	rtwdev->chip = chip;
4916b80ad23aSPing-Ke Shih 	rtwdev->fw.req.firmware = firmware;
4917ffde7f34SPing-Ke Shih 	rtwdev->fw.fw_format = fw_format;
4918fef63150SZong-Zhe Yang 	rtwdev->support_mlo = support_mlo;
49197fc06a07SZong-Zhe Yang 
4920fef63150SZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_CHAN, "probe driver %s chanctx\n",
4921deebea35SZong-Zhe Yang 		    no_chanctx ? "without" : "with");
4922fef63150SZong-Zhe Yang 	rtw89_debug(rtwdev, RTW89_DBG_CHAN, "probe driver %s MLO cap\n",
4923fef63150SZong-Zhe Yang 		    support_mlo ? "with" : "without");
4924deebea35SZong-Zhe Yang 
49257fc06a07SZong-Zhe Yang 	return rtwdev;
49267fc06a07SZong-Zhe Yang 
49277fc06a07SZong-Zhe Yang err:
49287fc06a07SZong-Zhe Yang 	kfree(ops);
492913eb07e0SZong-Zhe Yang 	release_firmware(firmware);
49307fc06a07SZong-Zhe Yang 	return NULL;
49317fc06a07SZong-Zhe Yang }
49327fc06a07SZong-Zhe Yang EXPORT_SYMBOL(rtw89_alloc_ieee80211_hw);
49337fc06a07SZong-Zhe Yang 
rtw89_free_ieee80211_hw(struct rtw89_dev * rtwdev)49347fc06a07SZong-Zhe Yang void rtw89_free_ieee80211_hw(struct rtw89_dev *rtwdev)
49357fc06a07SZong-Zhe Yang {
49367fc06a07SZong-Zhe Yang 	kfree(rtwdev->ops);
49375ee7b2eaSZong-Zhe Yang 	kfree(rtwdev->rfe_data);
4938b80ad23aSPing-Ke Shih 	release_firmware(rtwdev->fw.req.firmware);
49397fc06a07SZong-Zhe Yang 	ieee80211_free_hw(rtwdev->hw);
49407fc06a07SZong-Zhe Yang }
49417fc06a07SZong-Zhe Yang EXPORT_SYMBOL(rtw89_free_ieee80211_hw);
49427fc06a07SZong-Zhe Yang 
4943e3ec7017SPing-Ke Shih MODULE_AUTHOR("Realtek Corporation");
4944e3ec7017SPing-Ke Shih MODULE_DESCRIPTION("Realtek 802.11ax wireless core module");
4945e3ec7017SPing-Ke Shih MODULE_LICENSE("Dual BSD/GPL");
4946