1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2019-2020  Realtek Corporation
3  */
4 #include <linux/ip.h>
5 #include <linux/udp.h>
6 
7 #include "cam.h"
8 #include "chan.h"
9 #include "coex.h"
10 #include "core.h"
11 #include "efuse.h"
12 #include "fw.h"
13 #include "mac.h"
14 #include "phy.h"
15 #include "ps.h"
16 #include "reg.h"
17 #include "sar.h"
18 #include "ser.h"
19 #include "txrx.h"
20 #include "util.h"
21 
22 static bool rtw89_disable_ps_mode;
23 module_param_named(disable_ps_mode, rtw89_disable_ps_mode, bool, 0644);
24 MODULE_PARM_DESC(disable_ps_mode, "Set Y to disable low power mode");
25 
26 #define RTW89_DEF_CHAN(_freq, _hw_val, _flags, _band)	\
27 	{ .center_freq = _freq, .hw_value = _hw_val, .flags = _flags, .band = _band, }
28 #define RTW89_DEF_CHAN_2G(_freq, _hw_val)	\
29 	RTW89_DEF_CHAN(_freq, _hw_val, 0, NL80211_BAND_2GHZ)
30 #define RTW89_DEF_CHAN_5G(_freq, _hw_val)	\
31 	RTW89_DEF_CHAN(_freq, _hw_val, 0, NL80211_BAND_5GHZ)
32 #define RTW89_DEF_CHAN_5G_NO_HT40MINUS(_freq, _hw_val)	\
33 	RTW89_DEF_CHAN(_freq, _hw_val, IEEE80211_CHAN_NO_HT40MINUS, NL80211_BAND_5GHZ)
34 #define RTW89_DEF_CHAN_6G(_freq, _hw_val)	\
35 	RTW89_DEF_CHAN(_freq, _hw_val, 0, NL80211_BAND_6GHZ)
36 
37 static struct ieee80211_channel rtw89_channels_2ghz[] = {
38 	RTW89_DEF_CHAN_2G(2412, 1),
39 	RTW89_DEF_CHAN_2G(2417, 2),
40 	RTW89_DEF_CHAN_2G(2422, 3),
41 	RTW89_DEF_CHAN_2G(2427, 4),
42 	RTW89_DEF_CHAN_2G(2432, 5),
43 	RTW89_DEF_CHAN_2G(2437, 6),
44 	RTW89_DEF_CHAN_2G(2442, 7),
45 	RTW89_DEF_CHAN_2G(2447, 8),
46 	RTW89_DEF_CHAN_2G(2452, 9),
47 	RTW89_DEF_CHAN_2G(2457, 10),
48 	RTW89_DEF_CHAN_2G(2462, 11),
49 	RTW89_DEF_CHAN_2G(2467, 12),
50 	RTW89_DEF_CHAN_2G(2472, 13),
51 	RTW89_DEF_CHAN_2G(2484, 14),
52 };
53 
54 static struct ieee80211_channel rtw89_channels_5ghz[] = {
55 	RTW89_DEF_CHAN_5G(5180, 36),
56 	RTW89_DEF_CHAN_5G(5200, 40),
57 	RTW89_DEF_CHAN_5G(5220, 44),
58 	RTW89_DEF_CHAN_5G(5240, 48),
59 	RTW89_DEF_CHAN_5G(5260, 52),
60 	RTW89_DEF_CHAN_5G(5280, 56),
61 	RTW89_DEF_CHAN_5G(5300, 60),
62 	RTW89_DEF_CHAN_5G(5320, 64),
63 	RTW89_DEF_CHAN_5G(5500, 100),
64 	RTW89_DEF_CHAN_5G(5520, 104),
65 	RTW89_DEF_CHAN_5G(5540, 108),
66 	RTW89_DEF_CHAN_5G(5560, 112),
67 	RTW89_DEF_CHAN_5G(5580, 116),
68 	RTW89_DEF_CHAN_5G(5600, 120),
69 	RTW89_DEF_CHAN_5G(5620, 124),
70 	RTW89_DEF_CHAN_5G(5640, 128),
71 	RTW89_DEF_CHAN_5G(5660, 132),
72 	RTW89_DEF_CHAN_5G(5680, 136),
73 	RTW89_DEF_CHAN_5G(5700, 140),
74 	RTW89_DEF_CHAN_5G(5720, 144),
75 	RTW89_DEF_CHAN_5G(5745, 149),
76 	RTW89_DEF_CHAN_5G(5765, 153),
77 	RTW89_DEF_CHAN_5G(5785, 157),
78 	RTW89_DEF_CHAN_5G(5805, 161),
79 	RTW89_DEF_CHAN_5G_NO_HT40MINUS(5825, 165),
80 };
81 
82 static struct ieee80211_channel rtw89_channels_6ghz[] = {
83 	RTW89_DEF_CHAN_6G(5955, 1),
84 	RTW89_DEF_CHAN_6G(5975, 5),
85 	RTW89_DEF_CHAN_6G(5995, 9),
86 	RTW89_DEF_CHAN_6G(6015, 13),
87 	RTW89_DEF_CHAN_6G(6035, 17),
88 	RTW89_DEF_CHAN_6G(6055, 21),
89 	RTW89_DEF_CHAN_6G(6075, 25),
90 	RTW89_DEF_CHAN_6G(6095, 29),
91 	RTW89_DEF_CHAN_6G(6115, 33),
92 	RTW89_DEF_CHAN_6G(6135, 37),
93 	RTW89_DEF_CHAN_6G(6155, 41),
94 	RTW89_DEF_CHAN_6G(6175, 45),
95 	RTW89_DEF_CHAN_6G(6195, 49),
96 	RTW89_DEF_CHAN_6G(6215, 53),
97 	RTW89_DEF_CHAN_6G(6235, 57),
98 	RTW89_DEF_CHAN_6G(6255, 61),
99 	RTW89_DEF_CHAN_6G(6275, 65),
100 	RTW89_DEF_CHAN_6G(6295, 69),
101 	RTW89_DEF_CHAN_6G(6315, 73),
102 	RTW89_DEF_CHAN_6G(6335, 77),
103 	RTW89_DEF_CHAN_6G(6355, 81),
104 	RTW89_DEF_CHAN_6G(6375, 85),
105 	RTW89_DEF_CHAN_6G(6395, 89),
106 	RTW89_DEF_CHAN_6G(6415, 93),
107 	RTW89_DEF_CHAN_6G(6435, 97),
108 	RTW89_DEF_CHAN_6G(6455, 101),
109 	RTW89_DEF_CHAN_6G(6475, 105),
110 	RTW89_DEF_CHAN_6G(6495, 109),
111 	RTW89_DEF_CHAN_6G(6515, 113),
112 	RTW89_DEF_CHAN_6G(6535, 117),
113 	RTW89_DEF_CHAN_6G(6555, 121),
114 	RTW89_DEF_CHAN_6G(6575, 125),
115 	RTW89_DEF_CHAN_6G(6595, 129),
116 	RTW89_DEF_CHAN_6G(6615, 133),
117 	RTW89_DEF_CHAN_6G(6635, 137),
118 	RTW89_DEF_CHAN_6G(6655, 141),
119 	RTW89_DEF_CHAN_6G(6675, 145),
120 	RTW89_DEF_CHAN_6G(6695, 149),
121 	RTW89_DEF_CHAN_6G(6715, 153),
122 	RTW89_DEF_CHAN_6G(6735, 157),
123 	RTW89_DEF_CHAN_6G(6755, 161),
124 	RTW89_DEF_CHAN_6G(6775, 165),
125 	RTW89_DEF_CHAN_6G(6795, 169),
126 	RTW89_DEF_CHAN_6G(6815, 173),
127 	RTW89_DEF_CHAN_6G(6835, 177),
128 	RTW89_DEF_CHAN_6G(6855, 181),
129 	RTW89_DEF_CHAN_6G(6875, 185),
130 	RTW89_DEF_CHAN_6G(6895, 189),
131 	RTW89_DEF_CHAN_6G(6915, 193),
132 	RTW89_DEF_CHAN_6G(6935, 197),
133 	RTW89_DEF_CHAN_6G(6955, 201),
134 	RTW89_DEF_CHAN_6G(6975, 205),
135 	RTW89_DEF_CHAN_6G(6995, 209),
136 	RTW89_DEF_CHAN_6G(7015, 213),
137 	RTW89_DEF_CHAN_6G(7035, 217),
138 	RTW89_DEF_CHAN_6G(7055, 221),
139 	RTW89_DEF_CHAN_6G(7075, 225),
140 	RTW89_DEF_CHAN_6G(7095, 229),
141 	RTW89_DEF_CHAN_6G(7115, 233),
142 };
143 
144 static struct ieee80211_rate rtw89_bitrates[] = {
145 	{ .bitrate = 10,  .hw_value = 0x00, },
146 	{ .bitrate = 20,  .hw_value = 0x01, },
147 	{ .bitrate = 55,  .hw_value = 0x02, },
148 	{ .bitrate = 110, .hw_value = 0x03, },
149 	{ .bitrate = 60,  .hw_value = 0x04, },
150 	{ .bitrate = 90,  .hw_value = 0x05, },
151 	{ .bitrate = 120, .hw_value = 0x06, },
152 	{ .bitrate = 180, .hw_value = 0x07, },
153 	{ .bitrate = 240, .hw_value = 0x08, },
154 	{ .bitrate = 360, .hw_value = 0x09, },
155 	{ .bitrate = 480, .hw_value = 0x0a, },
156 	{ .bitrate = 540, .hw_value = 0x0b, },
157 };
158 
159 bool rtw89_ra_report_to_bitrate(struct rtw89_dev *rtwdev, u8 rpt_rate, u16 *bitrate)
160 {
161 	struct ieee80211_rate rate;
162 
163 	if (unlikely(rpt_rate >= ARRAY_SIZE(rtw89_bitrates))) {
164 		rtw89_debug(rtwdev, RTW89_DBG_UNEXP, "invalid rpt rate %d\n", rpt_rate);
165 		return false;
166 	}
167 
168 	rate = rtw89_bitrates[rpt_rate];
169 	*bitrate = rate.bitrate;
170 
171 	return true;
172 }
173 
174 static const struct ieee80211_supported_band rtw89_sband_2ghz = {
175 	.band		= NL80211_BAND_2GHZ,
176 	.channels	= rtw89_channels_2ghz,
177 	.n_channels	= ARRAY_SIZE(rtw89_channels_2ghz),
178 	.bitrates	= rtw89_bitrates,
179 	.n_bitrates	= ARRAY_SIZE(rtw89_bitrates),
180 	.ht_cap		= {0},
181 	.vht_cap	= {0},
182 };
183 
184 static const struct ieee80211_supported_band rtw89_sband_5ghz = {
185 	.band		= NL80211_BAND_5GHZ,
186 	.channels	= rtw89_channels_5ghz,
187 	.n_channels	= ARRAY_SIZE(rtw89_channels_5ghz),
188 
189 	/* 5G has no CCK rates, 1M/2M/5.5M/11M */
190 	.bitrates	= rtw89_bitrates + 4,
191 	.n_bitrates	= ARRAY_SIZE(rtw89_bitrates) - 4,
192 	.ht_cap		= {0},
193 	.vht_cap	= {0},
194 };
195 
196 static const struct ieee80211_supported_band rtw89_sband_6ghz = {
197 	.band		= NL80211_BAND_6GHZ,
198 	.channels	= rtw89_channels_6ghz,
199 	.n_channels	= ARRAY_SIZE(rtw89_channels_6ghz),
200 
201 	/* 6G has no CCK rates, 1M/2M/5.5M/11M */
202 	.bitrates	= rtw89_bitrates + 4,
203 	.n_bitrates	= ARRAY_SIZE(rtw89_bitrates) - 4,
204 };
205 
206 static void rtw89_traffic_stats_accu(struct rtw89_dev *rtwdev,
207 				     struct rtw89_traffic_stats *stats,
208 				     struct sk_buff *skb, bool tx)
209 {
210 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
211 
212 	if (!ieee80211_is_data(hdr->frame_control))
213 		return;
214 
215 	if (is_broadcast_ether_addr(hdr->addr1) ||
216 	    is_multicast_ether_addr(hdr->addr1))
217 		return;
218 
219 	if (tx) {
220 		stats->tx_cnt++;
221 		stats->tx_unicast += skb->len;
222 	} else {
223 		stats->rx_cnt++;
224 		stats->rx_unicast += skb->len;
225 	}
226 }
227 
228 void rtw89_get_default_chandef(struct cfg80211_chan_def *chandef)
229 {
230 	cfg80211_chandef_create(chandef, &rtw89_channels_2ghz[0],
231 				NL80211_CHAN_NO_HT);
232 }
233 
234 static void rtw89_get_channel_params(const struct cfg80211_chan_def *chandef,
235 				     struct rtw89_chan *chan)
236 {
237 	struct ieee80211_channel *channel = chandef->chan;
238 	enum nl80211_chan_width width = chandef->width;
239 	u32 primary_freq, center_freq;
240 	u8 center_chan;
241 	u8 bandwidth = RTW89_CHANNEL_WIDTH_20;
242 	u32 offset;
243 	u8 band;
244 
245 	center_chan = channel->hw_value;
246 	primary_freq = channel->center_freq;
247 	center_freq = chandef->center_freq1;
248 
249 	switch (width) {
250 	case NL80211_CHAN_WIDTH_20_NOHT:
251 	case NL80211_CHAN_WIDTH_20:
252 		bandwidth = RTW89_CHANNEL_WIDTH_20;
253 		break;
254 	case NL80211_CHAN_WIDTH_40:
255 		bandwidth = RTW89_CHANNEL_WIDTH_40;
256 		if (primary_freq > center_freq) {
257 			center_chan -= 2;
258 		} else {
259 			center_chan += 2;
260 		}
261 		break;
262 	case NL80211_CHAN_WIDTH_80:
263 	case NL80211_CHAN_WIDTH_160:
264 		bandwidth = nl_to_rtw89_bandwidth(width);
265 		if (primary_freq > center_freq) {
266 			offset = (primary_freq - center_freq - 10) / 20;
267 			center_chan -= 2 + offset * 4;
268 		} else {
269 			offset = (center_freq - primary_freq - 10) / 20;
270 			center_chan += 2 + offset * 4;
271 		}
272 		break;
273 	default:
274 		center_chan = 0;
275 		break;
276 	}
277 
278 	switch (channel->band) {
279 	default:
280 	case NL80211_BAND_2GHZ:
281 		band = RTW89_BAND_2G;
282 		break;
283 	case NL80211_BAND_5GHZ:
284 		band = RTW89_BAND_5G;
285 		break;
286 	case NL80211_BAND_6GHZ:
287 		band = RTW89_BAND_6G;
288 		break;
289 	}
290 
291 	rtw89_chan_create(chan, center_chan, channel->hw_value, band, bandwidth);
292 }
293 
294 void rtw89_core_set_chip_txpwr(struct rtw89_dev *rtwdev)
295 {
296 	const struct rtw89_chip_info *chip = rtwdev->chip;
297 	const struct rtw89_chan *chan;
298 	enum rtw89_sub_entity_idx sub_entity_idx;
299 	enum rtw89_phy_idx phy_idx;
300 	enum rtw89_entity_mode mode;
301 	bool entity_active;
302 
303 	entity_active = rtw89_get_entity_state(rtwdev);
304 	if (!entity_active)
305 		return;
306 
307 	mode = rtw89_get_entity_mode(rtwdev);
308 	if (WARN(mode != RTW89_ENTITY_MODE_SCC, "Invalid ent mode: %d\n", mode))
309 		return;
310 
311 	sub_entity_idx = RTW89_SUB_ENTITY_0;
312 	phy_idx = RTW89_PHY_0;
313 	chan = rtw89_chan_get(rtwdev, sub_entity_idx);
314 	if (chip->ops->set_txpwr)
315 		chip->ops->set_txpwr(rtwdev, chan, phy_idx);
316 }
317 
318 void rtw89_set_channel(struct rtw89_dev *rtwdev)
319 {
320 	const struct rtw89_chip_info *chip = rtwdev->chip;
321 	const struct cfg80211_chan_def *chandef;
322 	enum rtw89_sub_entity_idx sub_entity_idx;
323 	enum rtw89_mac_idx mac_idx;
324 	enum rtw89_phy_idx phy_idx;
325 	struct rtw89_chan chan;
326 	struct rtw89_channel_help_params bak;
327 	enum rtw89_entity_mode mode;
328 	bool band_changed;
329 	bool entity_active;
330 
331 	entity_active = rtw89_get_entity_state(rtwdev);
332 
333 	mode = rtw89_entity_recalc(rtwdev);
334 	if (WARN(mode != RTW89_ENTITY_MODE_SCC, "Invalid ent mode: %d\n", mode))
335 		return;
336 
337 	sub_entity_idx = RTW89_SUB_ENTITY_0;
338 	mac_idx = RTW89_MAC_0;
339 	phy_idx = RTW89_PHY_0;
340 	chandef = rtw89_chandef_get(rtwdev, sub_entity_idx);
341 	rtw89_get_channel_params(chandef, &chan);
342 	if (WARN(chan.channel == 0, "Invalid channel\n"))
343 		return;
344 
345 	band_changed = rtw89_assign_entity_chan(rtwdev, sub_entity_idx, &chan);
346 
347 	rtw89_chip_set_channel_prepare(rtwdev, &bak, &chan, mac_idx, phy_idx);
348 
349 	chip->ops->set_channel(rtwdev, &chan, mac_idx, phy_idx);
350 
351 	rtw89_core_set_chip_txpwr(rtwdev);
352 
353 	rtw89_chip_set_channel_done(rtwdev, &bak, &chan, mac_idx, phy_idx);
354 
355 	if (!entity_active || band_changed) {
356 		rtw89_btc_ntfy_switch_band(rtwdev, phy_idx, chan.band_type);
357 		rtw89_chip_rfk_band_changed(rtwdev, phy_idx);
358 	}
359 
360 	rtw89_set_entity_state(rtwdev, true);
361 }
362 
363 static enum rtw89_core_tx_type
364 rtw89_core_get_tx_type(struct rtw89_dev *rtwdev,
365 		       struct sk_buff *skb)
366 {
367 	struct ieee80211_hdr *hdr = (void *)skb->data;
368 	__le16 fc = hdr->frame_control;
369 
370 	if (ieee80211_is_mgmt(fc) || ieee80211_is_nullfunc(fc))
371 		return RTW89_CORE_TX_TYPE_MGMT;
372 
373 	return RTW89_CORE_TX_TYPE_DATA;
374 }
375 
376 static void
377 rtw89_core_tx_update_ampdu_info(struct rtw89_dev *rtwdev,
378 				struct rtw89_core_tx_request *tx_req,
379 				enum btc_pkt_type pkt_type)
380 {
381 	struct ieee80211_sta *sta = tx_req->sta;
382 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
383 	struct sk_buff *skb = tx_req->skb;
384 	struct rtw89_sta *rtwsta;
385 	u8 ampdu_num;
386 	u8 tid;
387 
388 	if (pkt_type == PACKET_EAPOL) {
389 		desc_info->bk = true;
390 		return;
391 	}
392 
393 	if (!(IEEE80211_SKB_CB(skb)->flags & IEEE80211_TX_CTL_AMPDU))
394 		return;
395 
396 	if (!sta) {
397 		rtw89_warn(rtwdev, "cannot set ampdu info without sta\n");
398 		return;
399 	}
400 
401 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
402 	rtwsta = (struct rtw89_sta *)sta->drv_priv;
403 
404 	ampdu_num = (u8)((rtwsta->ampdu_params[tid].agg_num ?
405 			  rtwsta->ampdu_params[tid].agg_num :
406 			  4 << sta->deflink.ht_cap.ampdu_factor) - 1);
407 
408 	desc_info->agg_en = true;
409 	desc_info->ampdu_density = sta->deflink.ht_cap.ampdu_density;
410 	desc_info->ampdu_num = ampdu_num;
411 }
412 
413 static void
414 rtw89_core_tx_update_sec_key(struct rtw89_dev *rtwdev,
415 			     struct rtw89_core_tx_request *tx_req)
416 {
417 	const struct rtw89_chip_info *chip = rtwdev->chip;
418 	struct ieee80211_vif *vif = tx_req->vif;
419 	struct ieee80211_sta *sta = tx_req->sta;
420 	struct ieee80211_tx_info *info;
421 	struct ieee80211_key_conf *key;
422 	struct rtw89_vif *rtwvif;
423 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
424 	struct rtw89_addr_cam_entry *addr_cam;
425 	struct rtw89_sec_cam_entry *sec_cam;
426 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
427 	struct sk_buff *skb = tx_req->skb;
428 	u8 sec_type = RTW89_SEC_KEY_TYPE_NONE;
429 	u64 pn64;
430 
431 	if (!vif) {
432 		rtw89_warn(rtwdev, "cannot set sec key without vif\n");
433 		return;
434 	}
435 
436 	rtwvif = (struct rtw89_vif *)vif->drv_priv;
437 	addr_cam = rtw89_get_addr_cam_of(rtwvif, rtwsta);
438 
439 	info = IEEE80211_SKB_CB(skb);
440 	key = info->control.hw_key;
441 	sec_cam = addr_cam->sec_entries[key->hw_key_idx];
442 	if (!sec_cam) {
443 		rtw89_warn(rtwdev, "sec cam entry is empty\n");
444 		return;
445 	}
446 
447 	switch (key->cipher) {
448 	case WLAN_CIPHER_SUITE_WEP40:
449 		sec_type = RTW89_SEC_KEY_TYPE_WEP40;
450 		break;
451 	case WLAN_CIPHER_SUITE_WEP104:
452 		sec_type = RTW89_SEC_KEY_TYPE_WEP104;
453 		break;
454 	case WLAN_CIPHER_SUITE_TKIP:
455 		sec_type = RTW89_SEC_KEY_TYPE_TKIP;
456 		break;
457 	case WLAN_CIPHER_SUITE_CCMP:
458 		sec_type = RTW89_SEC_KEY_TYPE_CCMP128;
459 		break;
460 	case WLAN_CIPHER_SUITE_CCMP_256:
461 		sec_type = RTW89_SEC_KEY_TYPE_CCMP256;
462 		break;
463 	case WLAN_CIPHER_SUITE_GCMP:
464 		sec_type = RTW89_SEC_KEY_TYPE_GCMP128;
465 		break;
466 	case WLAN_CIPHER_SUITE_GCMP_256:
467 		sec_type = RTW89_SEC_KEY_TYPE_GCMP256;
468 		break;
469 	default:
470 		rtw89_warn(rtwdev, "key cipher not supported %d\n", key->cipher);
471 		return;
472 	}
473 
474 	desc_info->sec_en = true;
475 	desc_info->sec_keyid = key->keyidx;
476 	desc_info->sec_type = sec_type;
477 	desc_info->sec_cam_idx = sec_cam->sec_cam_idx;
478 
479 	if (!chip->hw_sec_hdr)
480 		return;
481 
482 	pn64 = atomic64_inc_return(&key->tx_pn);
483 	desc_info->sec_seq[0] = pn64;
484 	desc_info->sec_seq[1] = pn64 >> 8;
485 	desc_info->sec_seq[2] = pn64 >> 16;
486 	desc_info->sec_seq[3] = pn64 >> 24;
487 	desc_info->sec_seq[4] = pn64 >> 32;
488 	desc_info->sec_seq[5] = pn64 >> 40;
489 	desc_info->wp_offset = 1; /* in unit of 8 bytes for security header */
490 }
491 
492 static u16 rtw89_core_get_mgmt_rate(struct rtw89_dev *rtwdev,
493 				    struct rtw89_core_tx_request *tx_req)
494 {
495 	struct sk_buff *skb = tx_req->skb;
496 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
497 	struct ieee80211_vif *vif = tx_info->control.vif;
498 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
499 	u16 lowest_rate;
500 
501 	if (tx_info->flags & IEEE80211_TX_CTL_NO_CCK_RATE ||
502 	    (vif && vif->p2p))
503 		lowest_rate = RTW89_HW_RATE_OFDM6;
504 	else if (chan->band_type == RTW89_BAND_2G)
505 		lowest_rate = RTW89_HW_RATE_CCK1;
506 	else
507 		lowest_rate = RTW89_HW_RATE_OFDM6;
508 
509 	if (!vif || !vif->bss_conf.basic_rates || !tx_req->sta)
510 		return lowest_rate;
511 
512 	return __ffs(vif->bss_conf.basic_rates) + lowest_rate;
513 }
514 
515 static u8 rtw89_core_tx_get_mac_id(struct rtw89_dev *rtwdev,
516 				   struct rtw89_core_tx_request *tx_req)
517 {
518 	struct ieee80211_vif *vif = tx_req->vif;
519 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
520 	struct ieee80211_sta *sta = tx_req->sta;
521 	struct rtw89_sta *rtwsta;
522 
523 	if (!sta)
524 		return rtwvif->mac_id;
525 
526 	rtwsta = (struct rtw89_sta *)sta->drv_priv;
527 	return rtwsta->mac_id;
528 }
529 
530 static void
531 rtw89_core_tx_update_mgmt_info(struct rtw89_dev *rtwdev,
532 			       struct rtw89_core_tx_request *tx_req)
533 {
534 	struct ieee80211_vif *vif = tx_req->vif;
535 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
536 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
537 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
538 	u8 qsel, ch_dma;
539 
540 	qsel = desc_info->hiq ? RTW89_TX_QSEL_B0_HI : RTW89_TX_QSEL_B0_MGMT;
541 	ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
542 
543 	desc_info->qsel = qsel;
544 	desc_info->ch_dma = ch_dma;
545 	desc_info->port = desc_info->hiq ? rtwvif->port : 0;
546 	desc_info->mac_id = rtw89_core_tx_get_mac_id(rtwdev, tx_req);
547 	desc_info->hw_ssn_sel = RTW89_MGMT_HW_SSN_SEL;
548 	desc_info->hw_seq_mode = RTW89_MGMT_HW_SEQ_MODE;
549 
550 	/* fixed data rate for mgmt frames */
551 	desc_info->en_wd_info = true;
552 	desc_info->use_rate = true;
553 	desc_info->dis_data_fb = true;
554 	desc_info->data_rate = rtw89_core_get_mgmt_rate(rtwdev, tx_req);
555 
556 	rtw89_debug(rtwdev, RTW89_DBG_TXRX,
557 		    "tx mgmt frame with rate 0x%x on channel %d (band %d, bw %d)\n",
558 		    desc_info->data_rate, chan->channel, chan->band_type,
559 		    chan->band_width);
560 }
561 
562 static void
563 rtw89_core_tx_update_h2c_info(struct rtw89_dev *rtwdev,
564 			      struct rtw89_core_tx_request *tx_req)
565 {
566 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
567 
568 	desc_info->is_bmc = false;
569 	desc_info->wd_page = false;
570 	desc_info->ch_dma = RTW89_DMA_H2C;
571 }
572 
573 static void rtw89_core_get_no_ul_ofdma_htc(struct rtw89_dev *rtwdev, __le32 *htc)
574 {
575 	static const u8 rtw89_bandwidth_to_om[] = {
576 		[RTW89_CHANNEL_WIDTH_20] = HTC_OM_CHANNEL_WIDTH_20,
577 		[RTW89_CHANNEL_WIDTH_40] = HTC_OM_CHANNEL_WIDTH_40,
578 		[RTW89_CHANNEL_WIDTH_80] = HTC_OM_CHANNEL_WIDTH_80,
579 		[RTW89_CHANNEL_WIDTH_160] = HTC_OM_CHANNEL_WIDTH_160_OR_80_80,
580 		[RTW89_CHANNEL_WIDTH_80_80] = HTC_OM_CHANNEL_WIDTH_160_OR_80_80,
581 	};
582 	const struct rtw89_chip_info *chip = rtwdev->chip;
583 	struct rtw89_hal *hal = &rtwdev->hal;
584 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
585 	u8 om_bandwidth;
586 
587 	if (!chip->dis_2g_40m_ul_ofdma ||
588 	    chan->band_type != RTW89_BAND_2G ||
589 	    chan->band_width != RTW89_CHANNEL_WIDTH_40)
590 		return;
591 
592 	om_bandwidth = chan->band_width < ARRAY_SIZE(rtw89_bandwidth_to_om) ?
593 		       rtw89_bandwidth_to_om[chan->band_width] : 0;
594 	*htc = le32_encode_bits(RTW89_HTC_VARIANT_HE, RTW89_HTC_MASK_VARIANT) |
595 	       le32_encode_bits(RTW89_HTC_VARIANT_HE_CID_OM, RTW89_HTC_MASK_CTL_ID) |
596 	       le32_encode_bits(hal->rx_nss - 1, RTW89_HTC_MASK_HTC_OM_RX_NSS) |
597 	       le32_encode_bits(om_bandwidth, RTW89_HTC_MASK_HTC_OM_CH_WIDTH) |
598 	       le32_encode_bits(1, RTW89_HTC_MASK_HTC_OM_UL_MU_DIS) |
599 	       le32_encode_bits(hal->tx_nss - 1, RTW89_HTC_MASK_HTC_OM_TX_NSTS) |
600 	       le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_ER_SU_DIS) |
601 	       le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_DL_MU_MIMO_RR) |
602 	       le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_UL_MU_DATA_DIS);
603 }
604 
605 static bool
606 __rtw89_core_tx_check_he_qos_htc(struct rtw89_dev *rtwdev,
607 				 struct rtw89_core_tx_request *tx_req,
608 				 enum btc_pkt_type pkt_type)
609 {
610 	struct ieee80211_sta *sta = tx_req->sta;
611 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
612 	struct sk_buff *skb = tx_req->skb;
613 	struct ieee80211_hdr *hdr = (void *)skb->data;
614 	__le16 fc = hdr->frame_control;
615 
616 	/* AP IOT issue with EAPoL, ARP and DHCP */
617 	if (pkt_type < PACKET_MAX)
618 		return false;
619 
620 	if (!sta || !sta->deflink.he_cap.has_he)
621 		return false;
622 
623 	if (!ieee80211_is_data_qos(fc))
624 		return false;
625 
626 	if (skb_headroom(skb) < IEEE80211_HT_CTL_LEN)
627 		return false;
628 
629 	if (rtwsta && rtwsta->ra_report.might_fallback_legacy)
630 		return false;
631 
632 	return true;
633 }
634 
635 static void
636 __rtw89_core_tx_adjust_he_qos_htc(struct rtw89_dev *rtwdev,
637 				  struct rtw89_core_tx_request *tx_req)
638 {
639 	struct ieee80211_sta *sta = tx_req->sta;
640 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
641 	struct sk_buff *skb = tx_req->skb;
642 	struct ieee80211_hdr *hdr = (void *)skb->data;
643 	__le16 fc = hdr->frame_control;
644 	void *data;
645 	__le32 *htc;
646 	u8 *qc;
647 	int hdr_len;
648 
649 	hdr_len = ieee80211_has_a4(fc) ? 32 : 26;
650 	data = skb_push(skb, IEEE80211_HT_CTL_LEN);
651 	memmove(data, data + IEEE80211_HT_CTL_LEN, hdr_len);
652 
653 	hdr = data;
654 	htc = data + hdr_len;
655 	hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_ORDER);
656 	*htc = rtwsta->htc_template ? rtwsta->htc_template :
657 	       le32_encode_bits(RTW89_HTC_VARIANT_HE, RTW89_HTC_MASK_VARIANT) |
658 	       le32_encode_bits(RTW89_HTC_VARIANT_HE_CID_CAS, RTW89_HTC_MASK_CTL_ID);
659 
660 	qc = data + hdr_len - IEEE80211_QOS_CTL_LEN;
661 	qc[0] |= IEEE80211_QOS_CTL_EOSP;
662 }
663 
664 static void
665 rtw89_core_tx_update_he_qos_htc(struct rtw89_dev *rtwdev,
666 				struct rtw89_core_tx_request *tx_req,
667 				enum btc_pkt_type pkt_type)
668 {
669 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
670 	struct ieee80211_vif *vif = tx_req->vif;
671 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
672 
673 	if (!__rtw89_core_tx_check_he_qos_htc(rtwdev, tx_req, pkt_type))
674 		goto desc_bk;
675 
676 	__rtw89_core_tx_adjust_he_qos_htc(rtwdev, tx_req);
677 
678 	desc_info->pkt_size += IEEE80211_HT_CTL_LEN;
679 	desc_info->a_ctrl_bsr = true;
680 
681 desc_bk:
682 	if (!rtwvif || rtwvif->last_a_ctrl == desc_info->a_ctrl_bsr)
683 		return;
684 
685 	rtwvif->last_a_ctrl = desc_info->a_ctrl_bsr;
686 	desc_info->bk = true;
687 }
688 
689 static void
690 rtw89_core_tx_update_data_info(struct rtw89_dev *rtwdev,
691 			       struct rtw89_core_tx_request *tx_req)
692 {
693 	struct ieee80211_vif *vif = tx_req->vif;
694 	struct ieee80211_sta *sta = tx_req->sta;
695 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
696 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
697 	struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern;
698 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
699 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
700 	struct sk_buff *skb = tx_req->skb;
701 	u8 tid, tid_indicate;
702 	u8 qsel, ch_dma;
703 
704 	tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
705 	tid_indicate = rtw89_core_get_tid_indicate(rtwdev, tid);
706 	qsel = desc_info->hiq ? RTW89_TX_QSEL_B0_HI : rtw89_core_get_qsel(rtwdev, tid);
707 	ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
708 
709 	desc_info->ch_dma = ch_dma;
710 	desc_info->tid_indicate = tid_indicate;
711 	desc_info->qsel = qsel;
712 	desc_info->mac_id = rtw89_core_tx_get_mac_id(rtwdev, tx_req);
713 	desc_info->port = desc_info->hiq ? rtwvif->port : 0;
714 	desc_info->er_cap = rtwsta ? rtwsta->er_cap : false;
715 
716 	/* enable wd_info for AMPDU */
717 	desc_info->en_wd_info = true;
718 
719 	if (IEEE80211_SKB_CB(skb)->control.hw_key)
720 		rtw89_core_tx_update_sec_key(rtwdev, tx_req);
721 
722 	if (vif->p2p)
723 		desc_info->data_retry_lowest_rate = RTW89_HW_RATE_OFDM6;
724 	else if (rate_pattern->enable)
725 		desc_info->data_retry_lowest_rate = rate_pattern->rate;
726 	else if (chan->band_type == RTW89_BAND_2G)
727 		desc_info->data_retry_lowest_rate = RTW89_HW_RATE_CCK1;
728 	else
729 		desc_info->data_retry_lowest_rate = RTW89_HW_RATE_OFDM6;
730 }
731 
732 static enum btc_pkt_type
733 rtw89_core_tx_btc_spec_pkt_notify(struct rtw89_dev *rtwdev,
734 				  struct rtw89_core_tx_request *tx_req)
735 {
736 	struct sk_buff *skb = tx_req->skb;
737 	struct udphdr *udphdr;
738 
739 	if (IEEE80211_SKB_CB(skb)->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) {
740 		ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.eapol_notify_work);
741 		return PACKET_EAPOL;
742 	}
743 
744 	if (skb->protocol == htons(ETH_P_ARP)) {
745 		ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.arp_notify_work);
746 		return PACKET_ARP;
747 	}
748 
749 	if (skb->protocol == htons(ETH_P_IP) &&
750 	    ip_hdr(skb)->protocol == IPPROTO_UDP) {
751 		udphdr = udp_hdr(skb);
752 		if (((udphdr->source == htons(67) && udphdr->dest == htons(68)) ||
753 		     (udphdr->source == htons(68) && udphdr->dest == htons(67))) &&
754 		    skb->len > 282) {
755 			ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.dhcp_notify_work);
756 			return PACKET_DHCP;
757 		}
758 	}
759 
760 	if (skb->protocol == htons(ETH_P_IP) &&
761 	    ip_hdr(skb)->protocol == IPPROTO_ICMP) {
762 		ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.icmp_notify_work);
763 		return PACKET_ICMP;
764 	}
765 
766 	return PACKET_MAX;
767 }
768 
769 static void rtw89_core_tx_update_llc_hdr(struct rtw89_dev *rtwdev,
770 					 struct rtw89_tx_desc_info *desc_info,
771 					 struct sk_buff *skb)
772 {
773 	struct ieee80211_hdr *hdr = (void *)skb->data;
774 	__le16 fc = hdr->frame_control;
775 
776 	desc_info->hdr_llc_len = ieee80211_hdrlen(fc);
777 	desc_info->hdr_llc_len >>= 1; /* in unit of 2 bytes */
778 }
779 
780 static void
781 rtw89_core_tx_wake(struct rtw89_dev *rtwdev,
782 		   struct rtw89_core_tx_request *tx_req)
783 {
784 	const struct rtw89_chip_info *chip = rtwdev->chip;
785 
786 	if (!RTW89_CHK_FW_FEATURE(TX_WAKE, &rtwdev->fw))
787 		return;
788 
789 	if (!test_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags))
790 		return;
791 
792 	if (chip->chip_id != RTL8852C &&
793 	    tx_req->tx_type != RTW89_CORE_TX_TYPE_MGMT)
794 		return;
795 
796 	rtw89_mac_notify_wake(rtwdev);
797 }
798 
799 static void
800 rtw89_core_tx_update_desc_info(struct rtw89_dev *rtwdev,
801 			       struct rtw89_core_tx_request *tx_req)
802 {
803 	struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
804 	struct sk_buff *skb = tx_req->skb;
805 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
806 	struct ieee80211_hdr *hdr = (void *)skb->data;
807 	enum rtw89_core_tx_type tx_type;
808 	enum btc_pkt_type pkt_type;
809 	bool is_bmc;
810 	u16 seq;
811 
812 	seq = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
813 	if (tx_req->tx_type != RTW89_CORE_TX_TYPE_FWCMD) {
814 		tx_type = rtw89_core_get_tx_type(rtwdev, skb);
815 		tx_req->tx_type = tx_type;
816 	}
817 	is_bmc = (is_broadcast_ether_addr(hdr->addr1) ||
818 		  is_multicast_ether_addr(hdr->addr1));
819 
820 	desc_info->seq = seq;
821 	desc_info->pkt_size = skb->len;
822 	desc_info->is_bmc = is_bmc;
823 	desc_info->wd_page = true;
824 	desc_info->hiq = info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM;
825 
826 	switch (tx_req->tx_type) {
827 	case RTW89_CORE_TX_TYPE_MGMT:
828 		rtw89_core_tx_update_mgmt_info(rtwdev, tx_req);
829 		break;
830 	case RTW89_CORE_TX_TYPE_DATA:
831 		rtw89_core_tx_update_data_info(rtwdev, tx_req);
832 		pkt_type = rtw89_core_tx_btc_spec_pkt_notify(rtwdev, tx_req);
833 		rtw89_core_tx_update_he_qos_htc(rtwdev, tx_req, pkt_type);
834 		rtw89_core_tx_update_ampdu_info(rtwdev, tx_req, pkt_type);
835 		rtw89_core_tx_update_llc_hdr(rtwdev, desc_info, skb);
836 		break;
837 	case RTW89_CORE_TX_TYPE_FWCMD:
838 		rtw89_core_tx_update_h2c_info(rtwdev, tx_req);
839 		break;
840 	}
841 }
842 
843 void rtw89_core_tx_kick_off(struct rtw89_dev *rtwdev, u8 qsel)
844 {
845 	u8 ch_dma;
846 
847 	ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
848 
849 	rtw89_hci_tx_kick_off(rtwdev, ch_dma);
850 }
851 
852 int rtw89_h2c_tx(struct rtw89_dev *rtwdev,
853 		 struct sk_buff *skb, bool fwdl)
854 {
855 	struct rtw89_core_tx_request tx_req = {0};
856 	u32 cnt;
857 	int ret;
858 
859 	if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) {
860 		rtw89_debug(rtwdev, RTW89_DBG_FW,
861 			    "ignore h2c due to power is off with firmware state=%d\n",
862 			    test_bit(RTW89_FLAG_FW_RDY, rtwdev->flags));
863 		dev_kfree_skb(skb);
864 		return 0;
865 	}
866 
867 	tx_req.skb = skb;
868 	tx_req.tx_type = RTW89_CORE_TX_TYPE_FWCMD;
869 	if (fwdl)
870 		tx_req.desc_info.fw_dl = true;
871 
872 	rtw89_core_tx_update_desc_info(rtwdev, &tx_req);
873 
874 	if (!fwdl)
875 		rtw89_hex_dump(rtwdev, RTW89_DBG_FW, "H2C: ", skb->data, skb->len);
876 
877 	cnt = rtw89_hci_check_and_reclaim_tx_resource(rtwdev, RTW89_TXCH_CH12);
878 	if (cnt == 0) {
879 		rtw89_err(rtwdev, "no tx fwcmd resource\n");
880 		return -ENOSPC;
881 	}
882 
883 	ret = rtw89_hci_tx_write(rtwdev, &tx_req);
884 	if (ret) {
885 		rtw89_err(rtwdev, "failed to transmit skb to HCI\n");
886 		return ret;
887 	}
888 	rtw89_hci_tx_kick_off(rtwdev, RTW89_TXCH_CH12);
889 
890 	return 0;
891 }
892 
893 int rtw89_core_tx_write(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif,
894 			struct ieee80211_sta *sta, struct sk_buff *skb, int *qsel)
895 {
896 	struct rtw89_core_tx_request tx_req = {0};
897 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
898 	int ret;
899 
900 	tx_req.skb = skb;
901 	tx_req.sta = sta;
902 	tx_req.vif = vif;
903 
904 	rtw89_traffic_stats_accu(rtwdev, &rtwdev->stats, skb, true);
905 	rtw89_traffic_stats_accu(rtwdev, &rtwvif->stats, skb, true);
906 	rtw89_core_tx_update_desc_info(rtwdev, &tx_req);
907 	rtw89_core_tx_wake(rtwdev, &tx_req);
908 
909 	ret = rtw89_hci_tx_write(rtwdev, &tx_req);
910 	if (ret) {
911 		rtw89_err(rtwdev, "failed to transmit skb to HCI\n");
912 		return ret;
913 	}
914 
915 	if (qsel)
916 		*qsel = tx_req.desc_info.qsel;
917 
918 	return 0;
919 }
920 
921 static __le32 rtw89_build_txwd_body0(struct rtw89_tx_desc_info *desc_info)
922 {
923 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY0_WP_OFFSET, desc_info->wp_offset) |
924 		    FIELD_PREP(RTW89_TXWD_BODY0_WD_INFO_EN, desc_info->en_wd_info) |
925 		    FIELD_PREP(RTW89_TXWD_BODY0_CHANNEL_DMA, desc_info->ch_dma) |
926 		    FIELD_PREP(RTW89_TXWD_BODY0_HDR_LLC_LEN, desc_info->hdr_llc_len) |
927 		    FIELD_PREP(RTW89_TXWD_BODY0_WD_PAGE, desc_info->wd_page) |
928 		    FIELD_PREP(RTW89_TXWD_BODY0_FW_DL, desc_info->fw_dl) |
929 		    FIELD_PREP(RTW89_TXWD_BODY0_HW_SSN_SEL, desc_info->hw_ssn_sel) |
930 		    FIELD_PREP(RTW89_TXWD_BODY0_HW_SSN_MODE, desc_info->hw_seq_mode);
931 
932 	return cpu_to_le32(dword);
933 }
934 
935 static __le32 rtw89_build_txwd_body0_v1(struct rtw89_tx_desc_info *desc_info)
936 {
937 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY0_WP_OFFSET_V1, desc_info->wp_offset) |
938 		    FIELD_PREP(RTW89_TXWD_BODY0_WD_INFO_EN, desc_info->en_wd_info) |
939 		    FIELD_PREP(RTW89_TXWD_BODY0_CHANNEL_DMA, desc_info->ch_dma) |
940 		    FIELD_PREP(RTW89_TXWD_BODY0_HDR_LLC_LEN, desc_info->hdr_llc_len) |
941 		    FIELD_PREP(RTW89_TXWD_BODY0_WD_PAGE, desc_info->wd_page) |
942 		    FIELD_PREP(RTW89_TXWD_BODY0_FW_DL, desc_info->fw_dl);
943 
944 	return cpu_to_le32(dword);
945 }
946 
947 static __le32 rtw89_build_txwd_body1_v1(struct rtw89_tx_desc_info *desc_info)
948 {
949 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY1_ADDR_INFO_NUM, desc_info->addr_info_nr) |
950 		    FIELD_PREP(RTW89_TXWD_BODY1_SEC_KEYID, desc_info->sec_keyid) |
951 		    FIELD_PREP(RTW89_TXWD_BODY1_SEC_TYPE, desc_info->sec_type);
952 
953 	return cpu_to_le32(dword);
954 }
955 
956 static __le32 rtw89_build_txwd_body2(struct rtw89_tx_desc_info *desc_info)
957 {
958 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY2_TID_INDICATE, desc_info->tid_indicate) |
959 		    FIELD_PREP(RTW89_TXWD_BODY2_QSEL, desc_info->qsel) |
960 		    FIELD_PREP(RTW89_TXWD_BODY2_TXPKT_SIZE, desc_info->pkt_size) |
961 		    FIELD_PREP(RTW89_TXWD_BODY2_MACID, desc_info->mac_id);
962 
963 	return cpu_to_le32(dword);
964 }
965 
966 static __le32 rtw89_build_txwd_body3(struct rtw89_tx_desc_info *desc_info)
967 {
968 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY3_SW_SEQ, desc_info->seq) |
969 		    FIELD_PREP(RTW89_TXWD_BODY3_AGG_EN, desc_info->agg_en) |
970 		    FIELD_PREP(RTW89_TXWD_BODY3_BK, desc_info->bk);
971 
972 	return cpu_to_le32(dword);
973 }
974 
975 static __le32 rtw89_build_txwd_body4(struct rtw89_tx_desc_info *desc_info)
976 {
977 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY4_SEC_IV_L0, desc_info->sec_seq[0]) |
978 		    FIELD_PREP(RTW89_TXWD_BODY4_SEC_IV_L1, desc_info->sec_seq[1]);
979 
980 	return cpu_to_le32(dword);
981 }
982 
983 static __le32 rtw89_build_txwd_body5(struct rtw89_tx_desc_info *desc_info)
984 {
985 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H2, desc_info->sec_seq[2]) |
986 		    FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H3, desc_info->sec_seq[3]) |
987 		    FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H4, desc_info->sec_seq[4]) |
988 		    FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H5, desc_info->sec_seq[5]);
989 
990 	return cpu_to_le32(dword);
991 }
992 
993 static __le32 rtw89_build_txwd_body7_v1(struct rtw89_tx_desc_info *desc_info)
994 {
995 	u32 dword = FIELD_PREP(RTW89_TXWD_BODY7_USE_RATE_V1, desc_info->use_rate) |
996 		    FIELD_PREP(RTW89_TXWD_BODY7_DATA_RATE, desc_info->data_rate);
997 
998 	return cpu_to_le32(dword);
999 }
1000 
1001 static __le32 rtw89_build_txwd_info0(struct rtw89_tx_desc_info *desc_info)
1002 {
1003 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO0_USE_RATE, desc_info->use_rate) |
1004 		    FIELD_PREP(RTW89_TXWD_INFO0_DATA_RATE, desc_info->data_rate) |
1005 		    FIELD_PREP(RTW89_TXWD_INFO0_DISDATAFB, desc_info->dis_data_fb) |
1006 		    FIELD_PREP(RTW89_TXWD_INFO0_MULTIPORT_ID, desc_info->port);
1007 
1008 	return cpu_to_le32(dword);
1009 }
1010 
1011 static __le32 rtw89_build_txwd_info0_v1(struct rtw89_tx_desc_info *desc_info)
1012 {
1013 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO0_DISDATAFB, desc_info->dis_data_fb) |
1014 		    FIELD_PREP(RTW89_TXWD_INFO0_MULTIPORT_ID, desc_info->port) |
1015 		    FIELD_PREP(RTW89_TXWD_INFO0_DATA_ER, desc_info->er_cap) |
1016 		    FIELD_PREP(RTW89_TXWD_INFO0_DATA_BW_ER, 0);
1017 
1018 	return cpu_to_le32(dword);
1019 }
1020 
1021 static __le32 rtw89_build_txwd_info1(struct rtw89_tx_desc_info *desc_info)
1022 {
1023 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO1_MAX_AGGNUM, desc_info->ampdu_num) |
1024 		    FIELD_PREP(RTW89_TXWD_INFO1_A_CTRL_BSR, desc_info->a_ctrl_bsr) |
1025 		    FIELD_PREP(RTW89_TXWD_INFO1_DATA_RTY_LOWEST_RATE,
1026 			       desc_info->data_retry_lowest_rate);
1027 
1028 	return cpu_to_le32(dword);
1029 }
1030 
1031 static __le32 rtw89_build_txwd_info2(struct rtw89_tx_desc_info *desc_info)
1032 {
1033 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO2_AMPDU_DENSITY, desc_info->ampdu_density) |
1034 		    FIELD_PREP(RTW89_TXWD_INFO2_SEC_TYPE, desc_info->sec_type) |
1035 		    FIELD_PREP(RTW89_TXWD_INFO2_SEC_HW_ENC, desc_info->sec_en) |
1036 		    FIELD_PREP(RTW89_TXWD_INFO2_SEC_CAM_IDX, desc_info->sec_cam_idx);
1037 
1038 	return cpu_to_le32(dword);
1039 }
1040 
1041 static __le32 rtw89_build_txwd_info2_v1(struct rtw89_tx_desc_info *desc_info)
1042 {
1043 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO2_AMPDU_DENSITY, desc_info->ampdu_density) |
1044 		    FIELD_PREP(RTW89_TXWD_INFO2_FORCE_KEY_EN, desc_info->sec_en) |
1045 		    FIELD_PREP(RTW89_TXWD_INFO2_SEC_CAM_IDX, desc_info->sec_cam_idx);
1046 
1047 	return cpu_to_le32(dword);
1048 }
1049 
1050 static __le32 rtw89_build_txwd_info4(struct rtw89_tx_desc_info *desc_info)
1051 {
1052 	u32 dword = FIELD_PREP(RTW89_TXWD_INFO4_RTS_EN, 1) |
1053 		    FIELD_PREP(RTW89_TXWD_INFO4_HW_RTS_EN, 1);
1054 
1055 	return cpu_to_le32(dword);
1056 }
1057 
1058 void rtw89_core_fill_txdesc(struct rtw89_dev *rtwdev,
1059 			    struct rtw89_tx_desc_info *desc_info,
1060 			    void *txdesc)
1061 {
1062 	struct rtw89_txwd_body *txwd_body = (struct rtw89_txwd_body *)txdesc;
1063 	struct rtw89_txwd_info *txwd_info;
1064 
1065 	txwd_body->dword0 = rtw89_build_txwd_body0(desc_info);
1066 	txwd_body->dword2 = rtw89_build_txwd_body2(desc_info);
1067 	txwd_body->dword3 = rtw89_build_txwd_body3(desc_info);
1068 
1069 	if (!desc_info->en_wd_info)
1070 		return;
1071 
1072 	txwd_info = (struct rtw89_txwd_info *)(txwd_body + 1);
1073 	txwd_info->dword0 = rtw89_build_txwd_info0(desc_info);
1074 	txwd_info->dword1 = rtw89_build_txwd_info1(desc_info);
1075 	txwd_info->dword2 = rtw89_build_txwd_info2(desc_info);
1076 	txwd_info->dword4 = rtw89_build_txwd_info4(desc_info);
1077 
1078 }
1079 EXPORT_SYMBOL(rtw89_core_fill_txdesc);
1080 
1081 void rtw89_core_fill_txdesc_v1(struct rtw89_dev *rtwdev,
1082 			       struct rtw89_tx_desc_info *desc_info,
1083 			       void *txdesc)
1084 {
1085 	struct rtw89_txwd_body_v1 *txwd_body = (struct rtw89_txwd_body_v1 *)txdesc;
1086 	struct rtw89_txwd_info *txwd_info;
1087 
1088 	txwd_body->dword0 = rtw89_build_txwd_body0_v1(desc_info);
1089 	txwd_body->dword1 = rtw89_build_txwd_body1_v1(desc_info);
1090 	txwd_body->dword2 = rtw89_build_txwd_body2(desc_info);
1091 	txwd_body->dword3 = rtw89_build_txwd_body3(desc_info);
1092 	if (desc_info->sec_en) {
1093 		txwd_body->dword4 = rtw89_build_txwd_body4(desc_info);
1094 		txwd_body->dword5 = rtw89_build_txwd_body5(desc_info);
1095 	}
1096 	txwd_body->dword7 = rtw89_build_txwd_body7_v1(desc_info);
1097 
1098 	if (!desc_info->en_wd_info)
1099 		return;
1100 
1101 	txwd_info = (struct rtw89_txwd_info *)(txwd_body + 1);
1102 	txwd_info->dword0 = rtw89_build_txwd_info0_v1(desc_info);
1103 	txwd_info->dword1 = rtw89_build_txwd_info1(desc_info);
1104 	txwd_info->dword2 = rtw89_build_txwd_info2_v1(desc_info);
1105 	txwd_info->dword4 = rtw89_build_txwd_info4(desc_info);
1106 }
1107 EXPORT_SYMBOL(rtw89_core_fill_txdesc_v1);
1108 
1109 static __le32 rtw89_build_txwd_fwcmd0_v1(struct rtw89_tx_desc_info *desc_info)
1110 {
1111 	u32 dword = FIELD_PREP(AX_RXD_RPKT_LEN_MASK, desc_info->pkt_size) |
1112 		    FIELD_PREP(AX_RXD_RPKT_TYPE_MASK, desc_info->fw_dl ?
1113 						      RTW89_CORE_RX_TYPE_FWDL :
1114 						      RTW89_CORE_RX_TYPE_H2C);
1115 
1116 	return cpu_to_le32(dword);
1117 }
1118 
1119 void rtw89_core_fill_txdesc_fwcmd_v1(struct rtw89_dev *rtwdev,
1120 				     struct rtw89_tx_desc_info *desc_info,
1121 				     void *txdesc)
1122 {
1123 	struct rtw89_rxdesc_short *txwd_v1 = (struct rtw89_rxdesc_short *)txdesc;
1124 
1125 	txwd_v1->dword0 = rtw89_build_txwd_fwcmd0_v1(desc_info);
1126 }
1127 EXPORT_SYMBOL(rtw89_core_fill_txdesc_fwcmd_v1);
1128 
1129 static int rtw89_core_rx_process_mac_ppdu(struct rtw89_dev *rtwdev,
1130 					  struct sk_buff *skb,
1131 					  struct rtw89_rx_phy_ppdu *phy_ppdu)
1132 {
1133 	bool rx_cnt_valid = false;
1134 	u8 plcp_size = 0;
1135 	u8 usr_num = 0;
1136 	u8 *phy_sts;
1137 
1138 	rx_cnt_valid = RTW89_GET_RXINFO_RX_CNT_VLD(skb->data);
1139 	plcp_size = RTW89_GET_RXINFO_PLCP_LEN(skb->data) << 3;
1140 	usr_num = RTW89_GET_RXINFO_USR_NUM(skb->data);
1141 	if (usr_num > RTW89_PPDU_MAX_USR) {
1142 		rtw89_warn(rtwdev, "Invalid user number in mac info\n");
1143 		return -EINVAL;
1144 	}
1145 
1146 	phy_sts = skb->data + RTW89_PPDU_MAC_INFO_SIZE;
1147 	phy_sts += usr_num * RTW89_PPDU_MAC_INFO_USR_SIZE;
1148 	/* 8-byte alignment */
1149 	if (usr_num & BIT(0))
1150 		phy_sts += RTW89_PPDU_MAC_INFO_USR_SIZE;
1151 	if (rx_cnt_valid)
1152 		phy_sts += RTW89_PPDU_MAC_RX_CNT_SIZE;
1153 	phy_sts += plcp_size;
1154 
1155 	phy_ppdu->buf = phy_sts;
1156 	phy_ppdu->len = skb->data + skb->len - phy_sts;
1157 
1158 	return 0;
1159 }
1160 
1161 static void rtw89_core_rx_process_phy_ppdu_iter(void *data,
1162 						struct ieee80211_sta *sta)
1163 {
1164 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
1165 	struct rtw89_rx_phy_ppdu *phy_ppdu = (struct rtw89_rx_phy_ppdu *)data;
1166 	struct rtw89_dev *rtwdev = rtwsta->rtwdev;
1167 	int i;
1168 
1169 	if (rtwsta->mac_id == phy_ppdu->mac_id && phy_ppdu->to_self) {
1170 		ewma_rssi_add(&rtwsta->avg_rssi, phy_ppdu->rssi_avg);
1171 		for (i = 0; i < rtwdev->chip->rf_path_num; i++)
1172 			ewma_rssi_add(&rtwsta->rssi[i], phy_ppdu->rssi[i]);
1173 	}
1174 }
1175 
1176 #define VAR_LEN 0xff
1177 #define VAR_LEN_UNIT 8
1178 static u16 rtw89_core_get_phy_status_ie_len(struct rtw89_dev *rtwdev, u8 *addr)
1179 {
1180 	static const u8 physts_ie_len_tab[32] = {
1181 		16, 32, 24, 24, 8, 8, 8, 8, VAR_LEN, 8, VAR_LEN, 176, VAR_LEN,
1182 		VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, 16, 24, VAR_LEN,
1183 		VAR_LEN, VAR_LEN, 0, 24, 24, 24, 24, 32, 32, 32, 32
1184 	};
1185 	u16 ie_len;
1186 	u8 ie;
1187 
1188 	ie = RTW89_GET_PHY_STS_IE_TYPE(addr);
1189 	if (physts_ie_len_tab[ie] != VAR_LEN)
1190 		ie_len = physts_ie_len_tab[ie];
1191 	else
1192 		ie_len = RTW89_GET_PHY_STS_IE_LEN(addr) * VAR_LEN_UNIT;
1193 
1194 	return ie_len;
1195 }
1196 
1197 static void rtw89_core_parse_phy_status_ie01(struct rtw89_dev *rtwdev, u8 *addr,
1198 					     struct rtw89_rx_phy_ppdu *phy_ppdu)
1199 {
1200 	s16 cfo;
1201 
1202 	phy_ppdu->chan_idx = RTW89_GET_PHY_STS_IE01_CH_IDX(addr);
1203 	if (phy_ppdu->rate < RTW89_HW_RATE_OFDM6)
1204 		return;
1205 	/* sign conversion for S(12,2) */
1206 	if (rtwdev->chip->cfo_src_fd)
1207 		cfo = sign_extend32(RTW89_GET_PHY_STS_IE01_FD_CFO(addr), 11);
1208 	else
1209 		cfo = sign_extend32(RTW89_GET_PHY_STS_IE01_PREMB_CFO(addr), 11);
1210 
1211 	rtw89_phy_cfo_parse(rtwdev, cfo, phy_ppdu);
1212 }
1213 
1214 static int rtw89_core_process_phy_status_ie(struct rtw89_dev *rtwdev, u8 *addr,
1215 					    struct rtw89_rx_phy_ppdu *phy_ppdu)
1216 {
1217 	u8 ie;
1218 
1219 	ie = RTW89_GET_PHY_STS_IE_TYPE(addr);
1220 	switch (ie) {
1221 	case RTW89_PHYSTS_IE01_CMN_OFDM:
1222 		rtw89_core_parse_phy_status_ie01(rtwdev, addr, phy_ppdu);
1223 		break;
1224 	default:
1225 		break;
1226 	}
1227 
1228 	return 0;
1229 }
1230 
1231 static void rtw89_core_update_phy_ppdu(struct rtw89_rx_phy_ppdu *phy_ppdu)
1232 {
1233 	u8 *rssi = phy_ppdu->rssi;
1234 	u8 *buf = phy_ppdu->buf;
1235 
1236 	phy_ppdu->ie = RTW89_GET_PHY_STS_IE_MAP(buf);
1237 	phy_ppdu->rssi_avg = RTW89_GET_PHY_STS_RSSI_AVG(buf);
1238 	rssi[RF_PATH_A] = RTW89_GET_PHY_STS_RSSI_A(buf);
1239 	rssi[RF_PATH_B] = RTW89_GET_PHY_STS_RSSI_B(buf);
1240 	rssi[RF_PATH_C] = RTW89_GET_PHY_STS_RSSI_C(buf);
1241 	rssi[RF_PATH_D] = RTW89_GET_PHY_STS_RSSI_D(buf);
1242 }
1243 
1244 static int rtw89_core_rx_process_phy_ppdu(struct rtw89_dev *rtwdev,
1245 					  struct rtw89_rx_phy_ppdu *phy_ppdu)
1246 {
1247 	if (RTW89_GET_PHY_STS_LEN(phy_ppdu->buf) << 3 != phy_ppdu->len) {
1248 		rtw89_debug(rtwdev, RTW89_DBG_UNEXP, "phy ppdu len mismatch\n");
1249 		return -EINVAL;
1250 	}
1251 	rtw89_core_update_phy_ppdu(phy_ppdu);
1252 	ieee80211_iterate_stations_atomic(rtwdev->hw,
1253 					  rtw89_core_rx_process_phy_ppdu_iter,
1254 					  phy_ppdu);
1255 
1256 	return 0;
1257 }
1258 
1259 static int rtw89_core_rx_parse_phy_sts(struct rtw89_dev *rtwdev,
1260 				       struct rtw89_rx_phy_ppdu *phy_ppdu)
1261 {
1262 	u16 ie_len;
1263 	u8 *pos, *end;
1264 
1265 	/* mark invalid reports and bypass them */
1266 	if (phy_ppdu->ie < RTW89_CCK_PKT)
1267 		return -EINVAL;
1268 
1269 	if (!phy_ppdu->to_self)
1270 		return 0;
1271 
1272 	pos = (u8 *)phy_ppdu->buf + PHY_STS_HDR_LEN;
1273 	end = (u8 *)phy_ppdu->buf + phy_ppdu->len;
1274 	while (pos < end) {
1275 		ie_len = rtw89_core_get_phy_status_ie_len(rtwdev, pos);
1276 		rtw89_core_process_phy_status_ie(rtwdev, pos, phy_ppdu);
1277 		pos += ie_len;
1278 		if (pos > end || ie_len == 0) {
1279 			rtw89_debug(rtwdev, RTW89_DBG_TXRX,
1280 				    "phy status parse failed\n");
1281 			return -EINVAL;
1282 		}
1283 	}
1284 
1285 	return 0;
1286 }
1287 
1288 static void rtw89_core_rx_process_phy_sts(struct rtw89_dev *rtwdev,
1289 					  struct rtw89_rx_phy_ppdu *phy_ppdu)
1290 {
1291 	int ret;
1292 
1293 	ret = rtw89_core_rx_parse_phy_sts(rtwdev, phy_ppdu);
1294 	if (ret)
1295 		rtw89_debug(rtwdev, RTW89_DBG_TXRX, "parse phy sts failed\n");
1296 	else
1297 		phy_ppdu->valid = true;
1298 }
1299 
1300 static u8 rtw89_rxdesc_to_nl_he_gi(struct rtw89_dev *rtwdev,
1301 				   const struct rtw89_rx_desc_info *desc_info,
1302 				   bool rx_status)
1303 {
1304 	switch (desc_info->gi_ltf) {
1305 	case RTW89_GILTF_SGI_4XHE08:
1306 	case RTW89_GILTF_2XHE08:
1307 	case RTW89_GILTF_1XHE08:
1308 		return NL80211_RATE_INFO_HE_GI_0_8;
1309 	case RTW89_GILTF_2XHE16:
1310 	case RTW89_GILTF_1XHE16:
1311 		return NL80211_RATE_INFO_HE_GI_1_6;
1312 	case RTW89_GILTF_LGI_4XHE32:
1313 		return NL80211_RATE_INFO_HE_GI_3_2;
1314 	default:
1315 		rtw89_warn(rtwdev, "invalid gi_ltf=%d", desc_info->gi_ltf);
1316 		return rx_status ? NL80211_RATE_INFO_HE_GI_3_2 : U8_MAX;
1317 	}
1318 }
1319 
1320 static bool rtw89_core_rx_ppdu_match(struct rtw89_dev *rtwdev,
1321 				     struct rtw89_rx_desc_info *desc_info,
1322 				     struct ieee80211_rx_status *status)
1323 {
1324 	u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
1325 	u8 data_rate_mode, bw, rate_idx = MASKBYTE0, gi_ltf;
1326 	u16 data_rate;
1327 	bool ret;
1328 
1329 	data_rate = desc_info->data_rate;
1330 	data_rate_mode = GET_DATA_RATE_MODE(data_rate);
1331 	if (data_rate_mode == DATA_RATE_MODE_NON_HT) {
1332 		rate_idx = GET_DATA_RATE_NOT_HT_IDX(data_rate);
1333 		/* rate_idx is still hardware value here */
1334 	} else if (data_rate_mode == DATA_RATE_MODE_HT) {
1335 		rate_idx = GET_DATA_RATE_HT_IDX(data_rate);
1336 	} else if (data_rate_mode == DATA_RATE_MODE_VHT) {
1337 		rate_idx = GET_DATA_RATE_VHT_HE_IDX(data_rate);
1338 	} else if (data_rate_mode == DATA_RATE_MODE_HE) {
1339 		rate_idx = GET_DATA_RATE_VHT_HE_IDX(data_rate);
1340 	} else {
1341 		rtw89_warn(rtwdev, "invalid RX rate mode %d\n", data_rate_mode);
1342 	}
1343 
1344 	bw = rtw89_hw_to_rate_info_bw(desc_info->bw);
1345 	gi_ltf = rtw89_rxdesc_to_nl_he_gi(rtwdev, desc_info, false);
1346 	ret = rtwdev->ppdu_sts.curr_rx_ppdu_cnt[band] == desc_info->ppdu_cnt &&
1347 	      status->rate_idx == rate_idx &&
1348 	      status->he_gi == gi_ltf &&
1349 	      status->bw == bw;
1350 
1351 	return ret;
1352 }
1353 
1354 struct rtw89_vif_rx_stats_iter_data {
1355 	struct rtw89_dev *rtwdev;
1356 	struct rtw89_rx_phy_ppdu *phy_ppdu;
1357 	struct rtw89_rx_desc_info *desc_info;
1358 	struct sk_buff *skb;
1359 	const u8 *bssid;
1360 };
1361 
1362 static void rtw89_stats_trigger_frame(struct rtw89_dev *rtwdev,
1363 				      struct ieee80211_vif *vif,
1364 				      struct sk_buff *skb)
1365 {
1366 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
1367 	struct ieee80211_trigger *tf = (struct ieee80211_trigger *)skb->data;
1368 	u8 *pos, *end, type;
1369 	u16 aid;
1370 
1371 	if (!ether_addr_equal(vif->bss_conf.bssid, tf->ta) ||
1372 	    rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION ||
1373 	    rtwvif->net_type == RTW89_NET_TYPE_NO_LINK)
1374 		return;
1375 
1376 	type = le64_get_bits(tf->common_info, IEEE80211_TRIGGER_TYPE_MASK);
1377 	if (type != IEEE80211_TRIGGER_TYPE_BASIC)
1378 		return;
1379 
1380 	end = (u8 *)tf + skb->len;
1381 	pos = tf->variable;
1382 
1383 	while (end - pos >= RTW89_TF_BASIC_USER_INFO_SZ) {
1384 		aid = RTW89_GET_TF_USER_INFO_AID12(pos);
1385 		rtw89_debug(rtwdev, RTW89_DBG_TXRX,
1386 			    "[TF] aid: %d, ul_mcs: %d, rua: %d\n",
1387 			    aid, RTW89_GET_TF_USER_INFO_UL_MCS(pos),
1388 			    RTW89_GET_TF_USER_INFO_RUA(pos));
1389 
1390 		if (aid == RTW89_TF_PAD)
1391 			break;
1392 
1393 		if (aid == vif->cfg.aid) {
1394 			rtwvif->stats.rx_tf_acc++;
1395 			rtwdev->stats.rx_tf_acc++;
1396 			break;
1397 		}
1398 
1399 		pos += RTW89_TF_BASIC_USER_INFO_SZ;
1400 	}
1401 }
1402 
1403 static void rtw89_vif_rx_stats_iter(void *data, u8 *mac,
1404 				    struct ieee80211_vif *vif)
1405 {
1406 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
1407 	struct rtw89_vif_rx_stats_iter_data *iter_data = data;
1408 	struct rtw89_dev *rtwdev = iter_data->rtwdev;
1409 	struct rtw89_pkt_stat *pkt_stat = &rtwdev->phystat.cur_pkt_stat;
1410 	struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
1411 	struct sk_buff *skb = iter_data->skb;
1412 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1413 	const u8 *bssid = iter_data->bssid;
1414 
1415 	if (!vif->bss_conf.bssid)
1416 		return;
1417 
1418 	if (ieee80211_is_trigger(hdr->frame_control)) {
1419 		rtw89_stats_trigger_frame(rtwdev, vif, skb);
1420 		return;
1421 	}
1422 
1423 	if (!ether_addr_equal(vif->bss_conf.bssid, bssid))
1424 		return;
1425 
1426 	if (ieee80211_is_beacon(hdr->frame_control))
1427 		pkt_stat->beacon_nr++;
1428 
1429 	if (!ether_addr_equal(vif->addr, hdr->addr1))
1430 		return;
1431 
1432 	if (desc_info->data_rate < RTW89_HW_RATE_NR)
1433 		pkt_stat->rx_rate_cnt[desc_info->data_rate]++;
1434 
1435 	rtw89_traffic_stats_accu(rtwdev, &rtwvif->stats, skb, false);
1436 }
1437 
1438 static void rtw89_core_rx_stats(struct rtw89_dev *rtwdev,
1439 				struct rtw89_rx_phy_ppdu *phy_ppdu,
1440 				struct rtw89_rx_desc_info *desc_info,
1441 				struct sk_buff *skb)
1442 {
1443 	struct rtw89_vif_rx_stats_iter_data iter_data;
1444 
1445 	rtw89_traffic_stats_accu(rtwdev, &rtwdev->stats, skb, false);
1446 
1447 	iter_data.rtwdev = rtwdev;
1448 	iter_data.phy_ppdu = phy_ppdu;
1449 	iter_data.desc_info = desc_info;
1450 	iter_data.skb = skb;
1451 	iter_data.bssid = get_hdr_bssid((struct ieee80211_hdr *)skb->data);
1452 	rtw89_iterate_vifs_bh(rtwdev, rtw89_vif_rx_stats_iter, &iter_data);
1453 }
1454 
1455 static void rtw89_correct_cck_chan(struct rtw89_dev *rtwdev,
1456 				   struct ieee80211_rx_status *status)
1457 {
1458 	const struct rtw89_chan_rcd *rcd =
1459 		rtw89_chan_rcd_get(rtwdev, RTW89_SUB_ENTITY_0);
1460 	u16 chan = rcd->prev_primary_channel;
1461 	u8 band = rcd->prev_band_type == RTW89_BAND_2G ?
1462 		  NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1463 
1464 	if (status->band != NL80211_BAND_2GHZ &&
1465 	    status->encoding == RX_ENC_LEGACY &&
1466 	    status->rate_idx < RTW89_HW_RATE_OFDM6) {
1467 		status->freq = ieee80211_channel_to_frequency(chan, band);
1468 		status->band = band;
1469 	}
1470 }
1471 
1472 static void rtw89_core_hw_to_sband_rate(struct ieee80211_rx_status *rx_status)
1473 {
1474 	if (rx_status->band == NL80211_BAND_2GHZ ||
1475 	    rx_status->encoding != RX_ENC_LEGACY)
1476 		return;
1477 
1478 	/* Some control frames' freq(ACKs in this case) are reported wrong due
1479 	 * to FW notify timing, set to lowest rate to prevent overflow.
1480 	 */
1481 	if (rx_status->rate_idx < RTW89_HW_RATE_OFDM6) {
1482 		rx_status->rate_idx = 0;
1483 		return;
1484 	}
1485 
1486 	/* No 4 CCK rates for non-2G */
1487 	rx_status->rate_idx -= 4;
1488 }
1489 
1490 static void rtw89_core_update_radiotap(struct rtw89_dev *rtwdev,
1491 				       struct sk_buff *skb,
1492 				       struct ieee80211_rx_status *rx_status)
1493 {
1494 	static const struct ieee80211_radiotap_he known_he = {
1495 		.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
1496 				     IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN),
1497 		.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN),
1498 	};
1499 	struct ieee80211_radiotap_he *he;
1500 
1501 	if (!(rtwdev->hw->conf.flags & IEEE80211_CONF_MONITOR))
1502 		return;
1503 
1504 	if (rx_status->encoding == RX_ENC_HE) {
1505 		rx_status->flag |= RX_FLAG_RADIOTAP_HE;
1506 		he = skb_push(skb, sizeof(*he));
1507 		*he = known_he;
1508 	}
1509 }
1510 
1511 static void rtw89_core_rx_to_mac80211(struct rtw89_dev *rtwdev,
1512 				      struct rtw89_rx_phy_ppdu *phy_ppdu,
1513 				      struct rtw89_rx_desc_info *desc_info,
1514 				      struct sk_buff *skb_ppdu,
1515 				      struct ieee80211_rx_status *rx_status)
1516 {
1517 	struct napi_struct *napi = &rtwdev->napi;
1518 
1519 	/* In low power mode, napi isn't scheduled. Receive it to netif. */
1520 	if (unlikely(!test_bit(NAPI_STATE_SCHED, &napi->state)))
1521 		napi = NULL;
1522 
1523 	rtw89_core_hw_to_sband_rate(rx_status);
1524 	rtw89_core_rx_stats(rtwdev, phy_ppdu, desc_info, skb_ppdu);
1525 	rtw89_core_update_radiotap(rtwdev, skb_ppdu, rx_status);
1526 	/* In low power mode, it does RX in thread context. */
1527 	local_bh_disable();
1528 	ieee80211_rx_napi(rtwdev->hw, NULL, skb_ppdu, napi);
1529 	local_bh_enable();
1530 	rtwdev->napi_budget_countdown--;
1531 }
1532 
1533 static void rtw89_core_rx_pending_skb(struct rtw89_dev *rtwdev,
1534 				      struct rtw89_rx_phy_ppdu *phy_ppdu,
1535 				      struct rtw89_rx_desc_info *desc_info,
1536 				      struct sk_buff *skb)
1537 {
1538 	u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
1539 	int curr = rtwdev->ppdu_sts.curr_rx_ppdu_cnt[band];
1540 	struct sk_buff *skb_ppdu = NULL, *tmp;
1541 	struct ieee80211_rx_status *rx_status;
1542 
1543 	if (curr > RTW89_MAX_PPDU_CNT)
1544 		return;
1545 
1546 	skb_queue_walk_safe(&rtwdev->ppdu_sts.rx_queue[band], skb_ppdu, tmp) {
1547 		skb_unlink(skb_ppdu, &rtwdev->ppdu_sts.rx_queue[band]);
1548 		rx_status = IEEE80211_SKB_RXCB(skb_ppdu);
1549 		if (rtw89_core_rx_ppdu_match(rtwdev, desc_info, rx_status))
1550 			rtw89_chip_query_ppdu(rtwdev, phy_ppdu, rx_status);
1551 		rtw89_correct_cck_chan(rtwdev, rx_status);
1552 		rtw89_core_rx_to_mac80211(rtwdev, phy_ppdu, desc_info, skb_ppdu, rx_status);
1553 	}
1554 }
1555 
1556 static void rtw89_core_rx_process_ppdu_sts(struct rtw89_dev *rtwdev,
1557 					   struct rtw89_rx_desc_info *desc_info,
1558 					   struct sk_buff *skb)
1559 {
1560 	struct rtw89_rx_phy_ppdu phy_ppdu = {.buf = skb->data, .valid = false,
1561 					     .len = skb->len,
1562 					     .to_self = desc_info->addr1_match,
1563 					     .rate = desc_info->data_rate,
1564 					     .mac_id = desc_info->mac_id};
1565 	int ret;
1566 
1567 	if (desc_info->mac_info_valid)
1568 		rtw89_core_rx_process_mac_ppdu(rtwdev, skb, &phy_ppdu);
1569 	ret = rtw89_core_rx_process_phy_ppdu(rtwdev, &phy_ppdu);
1570 	if (ret)
1571 		rtw89_debug(rtwdev, RTW89_DBG_TXRX, "process ppdu failed\n");
1572 
1573 	rtw89_core_rx_process_phy_sts(rtwdev, &phy_ppdu);
1574 	rtw89_core_rx_pending_skb(rtwdev, &phy_ppdu, desc_info, skb);
1575 	dev_kfree_skb_any(skb);
1576 }
1577 
1578 static void rtw89_core_rx_process_report(struct rtw89_dev *rtwdev,
1579 					 struct rtw89_rx_desc_info *desc_info,
1580 					 struct sk_buff *skb)
1581 {
1582 	switch (desc_info->pkt_type) {
1583 	case RTW89_CORE_RX_TYPE_C2H:
1584 		rtw89_fw_c2h_irqsafe(rtwdev, skb);
1585 		break;
1586 	case RTW89_CORE_RX_TYPE_PPDU_STAT:
1587 		rtw89_core_rx_process_ppdu_sts(rtwdev, desc_info, skb);
1588 		break;
1589 	default:
1590 		rtw89_debug(rtwdev, RTW89_DBG_TXRX, "unhandled pkt_type=%d\n",
1591 			    desc_info->pkt_type);
1592 		dev_kfree_skb_any(skb);
1593 		break;
1594 	}
1595 }
1596 
1597 void rtw89_core_query_rxdesc(struct rtw89_dev *rtwdev,
1598 			     struct rtw89_rx_desc_info *desc_info,
1599 			     u8 *data, u32 data_offset)
1600 {
1601 	const struct rtw89_chip_info *chip = rtwdev->chip;
1602 	struct rtw89_rxdesc_short *rxd_s;
1603 	struct rtw89_rxdesc_long *rxd_l;
1604 	u8 shift_len, drv_info_len;
1605 
1606 	rxd_s = (struct rtw89_rxdesc_short *)(data + data_offset);
1607 	desc_info->pkt_size = RTW89_GET_RXWD_PKT_SIZE(rxd_s);
1608 	desc_info->drv_info_size = RTW89_GET_RXWD_DRV_INFO_SIZE(rxd_s);
1609 	desc_info->long_rxdesc = RTW89_GET_RXWD_LONG_RXD(rxd_s);
1610 	desc_info->pkt_type = RTW89_GET_RXWD_RPKT_TYPE(rxd_s);
1611 	desc_info->mac_info_valid = RTW89_GET_RXWD_MAC_INFO_VALID(rxd_s);
1612 	if (chip->chip_id == RTL8852C)
1613 		desc_info->bw = RTW89_GET_RXWD_BW_V1(rxd_s);
1614 	else
1615 		desc_info->bw = RTW89_GET_RXWD_BW(rxd_s);
1616 	desc_info->data_rate = RTW89_GET_RXWD_DATA_RATE(rxd_s);
1617 	desc_info->gi_ltf = RTW89_GET_RXWD_GI_LTF(rxd_s);
1618 	desc_info->user_id = RTW89_GET_RXWD_USER_ID(rxd_s);
1619 	desc_info->sr_en = RTW89_GET_RXWD_SR_EN(rxd_s);
1620 	desc_info->ppdu_cnt = RTW89_GET_RXWD_PPDU_CNT(rxd_s);
1621 	desc_info->ppdu_type = RTW89_GET_RXWD_PPDU_TYPE(rxd_s);
1622 	desc_info->free_run_cnt = RTW89_GET_RXWD_FREE_RUN_CNT(rxd_s);
1623 	desc_info->icv_err = RTW89_GET_RXWD_ICV_ERR(rxd_s);
1624 	desc_info->crc32_err = RTW89_GET_RXWD_CRC32_ERR(rxd_s);
1625 	desc_info->hw_dec = RTW89_GET_RXWD_HW_DEC(rxd_s);
1626 	desc_info->sw_dec = RTW89_GET_RXWD_SW_DEC(rxd_s);
1627 	desc_info->addr1_match = RTW89_GET_RXWD_A1_MATCH(rxd_s);
1628 
1629 	shift_len = desc_info->shift << 1; /* 2-byte unit */
1630 	drv_info_len = desc_info->drv_info_size << 3; /* 8-byte unit */
1631 	desc_info->offset = data_offset + shift_len + drv_info_len;
1632 	desc_info->ready = true;
1633 
1634 	if (!desc_info->long_rxdesc)
1635 		return;
1636 
1637 	rxd_l = (struct rtw89_rxdesc_long *)(data + data_offset);
1638 	desc_info->frame_type = RTW89_GET_RXWD_TYPE(rxd_l);
1639 	desc_info->addr_cam_valid = RTW89_GET_RXWD_ADDR_CAM_VLD(rxd_l);
1640 	desc_info->addr_cam_id = RTW89_GET_RXWD_ADDR_CAM_ID(rxd_l);
1641 	desc_info->sec_cam_id = RTW89_GET_RXWD_SEC_CAM_ID(rxd_l);
1642 	desc_info->mac_id = RTW89_GET_RXWD_MAC_ID(rxd_l);
1643 	desc_info->rx_pl_id = RTW89_GET_RXWD_RX_PL_ID(rxd_l);
1644 }
1645 EXPORT_SYMBOL(rtw89_core_query_rxdesc);
1646 
1647 struct rtw89_core_iter_rx_status {
1648 	struct rtw89_dev *rtwdev;
1649 	struct ieee80211_rx_status *rx_status;
1650 	struct rtw89_rx_desc_info *desc_info;
1651 	u8 mac_id;
1652 };
1653 
1654 static
1655 void rtw89_core_stats_sta_rx_status_iter(void *data, struct ieee80211_sta *sta)
1656 {
1657 	struct rtw89_core_iter_rx_status *iter_data =
1658 				(struct rtw89_core_iter_rx_status *)data;
1659 	struct ieee80211_rx_status *rx_status = iter_data->rx_status;
1660 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
1661 	struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
1662 	u8 mac_id = iter_data->mac_id;
1663 
1664 	if (mac_id != rtwsta->mac_id)
1665 		return;
1666 
1667 	rtwsta->rx_status = *rx_status;
1668 	rtwsta->rx_hw_rate = desc_info->data_rate;
1669 }
1670 
1671 static void rtw89_core_stats_sta_rx_status(struct rtw89_dev *rtwdev,
1672 					   struct rtw89_rx_desc_info *desc_info,
1673 					   struct ieee80211_rx_status *rx_status)
1674 {
1675 	struct rtw89_core_iter_rx_status iter_data;
1676 
1677 	if (!desc_info->addr1_match || !desc_info->long_rxdesc)
1678 		return;
1679 
1680 	if (desc_info->frame_type != RTW89_RX_TYPE_DATA)
1681 		return;
1682 
1683 	iter_data.rtwdev = rtwdev;
1684 	iter_data.rx_status = rx_status;
1685 	iter_data.desc_info = desc_info;
1686 	iter_data.mac_id = desc_info->mac_id;
1687 	ieee80211_iterate_stations_atomic(rtwdev->hw,
1688 					  rtw89_core_stats_sta_rx_status_iter,
1689 					  &iter_data);
1690 }
1691 
1692 static void rtw89_core_update_rx_status(struct rtw89_dev *rtwdev,
1693 					struct rtw89_rx_desc_info *desc_info,
1694 					struct ieee80211_rx_status *rx_status)
1695 {
1696 	const struct cfg80211_chan_def *chandef =
1697 		rtw89_chandef_get(rtwdev, RTW89_SUB_ENTITY_0);
1698 	const struct rtw89_chan *cur = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1699 	u16 data_rate;
1700 	u8 data_rate_mode;
1701 
1702 	/* currently using single PHY */
1703 	rx_status->freq = chandef->chan->center_freq;
1704 	rx_status->band = chandef->chan->band;
1705 
1706 	if (rtwdev->scanning &&
1707 	    RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw)) {
1708 		u8 chan = cur->primary_channel;
1709 		u8 band = cur->band_type;
1710 		enum nl80211_band nl_band;
1711 
1712 		nl_band = rtw89_hw_to_nl80211_band(band);
1713 		rx_status->freq = ieee80211_channel_to_frequency(chan, nl_band);
1714 		rx_status->band = nl_band;
1715 	}
1716 
1717 	if (desc_info->icv_err || desc_info->crc32_err)
1718 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
1719 
1720 	if (desc_info->hw_dec &&
1721 	    !(desc_info->sw_dec || desc_info->icv_err))
1722 		rx_status->flag |= RX_FLAG_DECRYPTED;
1723 
1724 	rx_status->bw = rtw89_hw_to_rate_info_bw(desc_info->bw);
1725 
1726 	data_rate = desc_info->data_rate;
1727 	data_rate_mode = GET_DATA_RATE_MODE(data_rate);
1728 	if (data_rate_mode == DATA_RATE_MODE_NON_HT) {
1729 		rx_status->encoding = RX_ENC_LEGACY;
1730 		rx_status->rate_idx = GET_DATA_RATE_NOT_HT_IDX(data_rate);
1731 		/* convert rate_idx after we get the correct band */
1732 	} else if (data_rate_mode == DATA_RATE_MODE_HT) {
1733 		rx_status->encoding = RX_ENC_HT;
1734 		rx_status->rate_idx = GET_DATA_RATE_HT_IDX(data_rate);
1735 		if (desc_info->gi_ltf)
1736 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1737 	} else if (data_rate_mode == DATA_RATE_MODE_VHT) {
1738 		rx_status->encoding = RX_ENC_VHT;
1739 		rx_status->rate_idx = GET_DATA_RATE_VHT_HE_IDX(data_rate);
1740 		rx_status->nss = GET_DATA_RATE_NSS(data_rate) + 1;
1741 		if (desc_info->gi_ltf)
1742 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1743 	} else if (data_rate_mode == DATA_RATE_MODE_HE) {
1744 		rx_status->encoding = RX_ENC_HE;
1745 		rx_status->rate_idx = GET_DATA_RATE_VHT_HE_IDX(data_rate);
1746 		rx_status->nss = GET_DATA_RATE_NSS(data_rate) + 1;
1747 	} else {
1748 		rtw89_warn(rtwdev, "invalid RX rate mode %d\n", data_rate_mode);
1749 	}
1750 
1751 	/* he_gi is used to match ppdu, so we always fill it. */
1752 	rx_status->he_gi = rtw89_rxdesc_to_nl_he_gi(rtwdev, desc_info, true);
1753 	rx_status->flag |= RX_FLAG_MACTIME_START;
1754 	rx_status->mactime = desc_info->free_run_cnt;
1755 
1756 	rtw89_core_stats_sta_rx_status(rtwdev, desc_info, rx_status);
1757 }
1758 
1759 static enum rtw89_ps_mode rtw89_update_ps_mode(struct rtw89_dev *rtwdev)
1760 {
1761 	const struct rtw89_chip_info *chip = rtwdev->chip;
1762 
1763 	if (rtw89_disable_ps_mode || !chip->ps_mode_supported ||
1764 	    RTW89_CHK_FW_FEATURE(NO_DEEP_PS, &rtwdev->fw))
1765 		return RTW89_PS_MODE_NONE;
1766 
1767 	if ((chip->ps_mode_supported & BIT(RTW89_PS_MODE_PWR_GATED)) &&
1768 	    !RTW89_CHK_FW_FEATURE(NO_LPS_PG, &rtwdev->fw))
1769 		return RTW89_PS_MODE_PWR_GATED;
1770 
1771 	if (chip->ps_mode_supported & BIT(RTW89_PS_MODE_CLK_GATED))
1772 		return RTW89_PS_MODE_CLK_GATED;
1773 
1774 	if (chip->ps_mode_supported & BIT(RTW89_PS_MODE_RFOFF))
1775 		return RTW89_PS_MODE_RFOFF;
1776 
1777 	return RTW89_PS_MODE_NONE;
1778 }
1779 
1780 static void rtw89_core_flush_ppdu_rx_queue(struct rtw89_dev *rtwdev,
1781 					   struct rtw89_rx_desc_info *desc_info)
1782 {
1783 	struct rtw89_ppdu_sts_info *ppdu_sts = &rtwdev->ppdu_sts;
1784 	u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
1785 	struct ieee80211_rx_status *rx_status;
1786 	struct sk_buff *skb_ppdu, *tmp;
1787 
1788 	skb_queue_walk_safe(&ppdu_sts->rx_queue[band], skb_ppdu, tmp) {
1789 		skb_unlink(skb_ppdu, &ppdu_sts->rx_queue[band]);
1790 		rx_status = IEEE80211_SKB_RXCB(skb_ppdu);
1791 		rtw89_core_rx_to_mac80211(rtwdev, NULL, desc_info, skb_ppdu, rx_status);
1792 	}
1793 }
1794 
1795 void rtw89_core_rx(struct rtw89_dev *rtwdev,
1796 		   struct rtw89_rx_desc_info *desc_info,
1797 		   struct sk_buff *skb)
1798 {
1799 	struct ieee80211_rx_status *rx_status;
1800 	struct rtw89_ppdu_sts_info *ppdu_sts = &rtwdev->ppdu_sts;
1801 	u8 ppdu_cnt = desc_info->ppdu_cnt;
1802 	u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
1803 
1804 	if (desc_info->pkt_type != RTW89_CORE_RX_TYPE_WIFI) {
1805 		rtw89_core_rx_process_report(rtwdev, desc_info, skb);
1806 		return;
1807 	}
1808 
1809 	if (ppdu_sts->curr_rx_ppdu_cnt[band] != ppdu_cnt) {
1810 		rtw89_core_flush_ppdu_rx_queue(rtwdev, desc_info);
1811 		ppdu_sts->curr_rx_ppdu_cnt[band] = ppdu_cnt;
1812 	}
1813 
1814 	rx_status = IEEE80211_SKB_RXCB(skb);
1815 	memset(rx_status, 0, sizeof(*rx_status));
1816 	rtw89_core_update_rx_status(rtwdev, desc_info, rx_status);
1817 	if (desc_info->long_rxdesc &&
1818 	    BIT(desc_info->frame_type) & PPDU_FILTER_BITMAP)
1819 		skb_queue_tail(&ppdu_sts->rx_queue[band], skb);
1820 	else
1821 		rtw89_core_rx_to_mac80211(rtwdev, NULL, desc_info, skb, rx_status);
1822 }
1823 EXPORT_SYMBOL(rtw89_core_rx);
1824 
1825 void rtw89_core_napi_start(struct rtw89_dev *rtwdev)
1826 {
1827 	if (test_and_set_bit(RTW89_FLAG_NAPI_RUNNING, rtwdev->flags))
1828 		return;
1829 
1830 	napi_enable(&rtwdev->napi);
1831 }
1832 EXPORT_SYMBOL(rtw89_core_napi_start);
1833 
1834 void rtw89_core_napi_stop(struct rtw89_dev *rtwdev)
1835 {
1836 	if (!test_and_clear_bit(RTW89_FLAG_NAPI_RUNNING, rtwdev->flags))
1837 		return;
1838 
1839 	napi_synchronize(&rtwdev->napi);
1840 	napi_disable(&rtwdev->napi);
1841 }
1842 EXPORT_SYMBOL(rtw89_core_napi_stop);
1843 
1844 void rtw89_core_napi_init(struct rtw89_dev *rtwdev)
1845 {
1846 	init_dummy_netdev(&rtwdev->netdev);
1847 	netif_napi_add(&rtwdev->netdev, &rtwdev->napi,
1848 		       rtwdev->hci.ops->napi_poll);
1849 }
1850 EXPORT_SYMBOL(rtw89_core_napi_init);
1851 
1852 void rtw89_core_napi_deinit(struct rtw89_dev *rtwdev)
1853 {
1854 	rtw89_core_napi_stop(rtwdev);
1855 	netif_napi_del(&rtwdev->napi);
1856 }
1857 EXPORT_SYMBOL(rtw89_core_napi_deinit);
1858 
1859 static void rtw89_core_ba_work(struct work_struct *work)
1860 {
1861 	struct rtw89_dev *rtwdev =
1862 		container_of(work, struct rtw89_dev, ba_work);
1863 	struct rtw89_txq *rtwtxq, *tmp;
1864 	int ret;
1865 
1866 	spin_lock_bh(&rtwdev->ba_lock);
1867 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->ba_list, list) {
1868 		struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
1869 		struct ieee80211_sta *sta = txq->sta;
1870 		struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL;
1871 		u8 tid = txq->tid;
1872 
1873 		if (!sta) {
1874 			rtw89_warn(rtwdev, "cannot start BA without sta\n");
1875 			goto skip_ba_work;
1876 		}
1877 
1878 		if (rtwsta->disassoc) {
1879 			rtw89_debug(rtwdev, RTW89_DBG_TXRX,
1880 				    "cannot start BA with disassoc sta\n");
1881 			goto skip_ba_work;
1882 		}
1883 
1884 		ret = ieee80211_start_tx_ba_session(sta, tid, 0);
1885 		if (ret) {
1886 			rtw89_debug(rtwdev, RTW89_DBG_TXRX,
1887 				    "failed to setup BA session for %pM:%2d: %d\n",
1888 				    sta->addr, tid, ret);
1889 			if (ret == -EINVAL)
1890 				set_bit(RTW89_TXQ_F_BLOCK_BA, &rtwtxq->flags);
1891 		}
1892 skip_ba_work:
1893 		list_del_init(&rtwtxq->list);
1894 	}
1895 	spin_unlock_bh(&rtwdev->ba_lock);
1896 }
1897 
1898 static void rtw89_core_free_sta_pending_ba(struct rtw89_dev *rtwdev,
1899 					   struct ieee80211_sta *sta)
1900 {
1901 	struct rtw89_txq *rtwtxq, *tmp;
1902 
1903 	spin_lock_bh(&rtwdev->ba_lock);
1904 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->ba_list, list) {
1905 		struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
1906 
1907 		if (sta == txq->sta)
1908 			list_del_init(&rtwtxq->list);
1909 	}
1910 	spin_unlock_bh(&rtwdev->ba_lock);
1911 }
1912 
1913 static void rtw89_core_free_sta_pending_forbid_ba(struct rtw89_dev *rtwdev,
1914 						  struct ieee80211_sta *sta)
1915 {
1916 	struct rtw89_txq *rtwtxq, *tmp;
1917 
1918 	spin_lock_bh(&rtwdev->ba_lock);
1919 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->forbid_ba_list, list) {
1920 		struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
1921 
1922 		if (sta == txq->sta) {
1923 			clear_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
1924 			list_del_init(&rtwtxq->list);
1925 		}
1926 	}
1927 	spin_unlock_bh(&rtwdev->ba_lock);
1928 }
1929 
1930 static void rtw89_core_stop_tx_ba_session(struct rtw89_dev *rtwdev,
1931 					  struct rtw89_txq *rtwtxq)
1932 {
1933 	struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
1934 	struct ieee80211_sta *sta = txq->sta;
1935 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
1936 
1937 	if (unlikely(!rtwsta) || unlikely(rtwsta->disassoc))
1938 		return;
1939 
1940 	if (!test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags) ||
1941 	    test_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags))
1942 		return;
1943 
1944 	spin_lock_bh(&rtwdev->ba_lock);
1945 	if (!test_and_set_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags))
1946 		list_add_tail(&rtwtxq->list, &rtwdev->forbid_ba_list);
1947 	spin_unlock_bh(&rtwdev->ba_lock);
1948 
1949 	ieee80211_stop_tx_ba_session(sta, txq->tid);
1950 	cancel_delayed_work(&rtwdev->forbid_ba_work);
1951 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->forbid_ba_work,
1952 				     RTW89_FORBID_BA_TIMER);
1953 }
1954 
1955 static void rtw89_core_txq_check_agg(struct rtw89_dev *rtwdev,
1956 				     struct rtw89_txq *rtwtxq,
1957 				     struct sk_buff *skb)
1958 {
1959 	struct ieee80211_hw *hw = rtwdev->hw;
1960 	struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
1961 	struct ieee80211_sta *sta = txq->sta;
1962 	struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL;
1963 
1964 	if (test_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags))
1965 		return;
1966 
1967 	if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
1968 		rtw89_core_stop_tx_ba_session(rtwdev, rtwtxq);
1969 		return;
1970 	}
1971 
1972 	if (unlikely(!sta))
1973 		return;
1974 
1975 	if (unlikely(test_bit(RTW89_TXQ_F_BLOCK_BA, &rtwtxq->flags)))
1976 		return;
1977 
1978 	if (test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags)) {
1979 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_AMPDU;
1980 		return;
1981 	}
1982 
1983 	spin_lock_bh(&rtwdev->ba_lock);
1984 	if (!rtwsta->disassoc && list_empty(&rtwtxq->list)) {
1985 		list_add_tail(&rtwtxq->list, &rtwdev->ba_list);
1986 		ieee80211_queue_work(hw, &rtwdev->ba_work);
1987 	}
1988 	spin_unlock_bh(&rtwdev->ba_lock);
1989 }
1990 
1991 static void rtw89_core_txq_push(struct rtw89_dev *rtwdev,
1992 				struct rtw89_txq *rtwtxq,
1993 				unsigned long frame_cnt,
1994 				unsigned long byte_cnt)
1995 {
1996 	struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
1997 	struct ieee80211_vif *vif = txq->vif;
1998 	struct ieee80211_sta *sta = txq->sta;
1999 	struct sk_buff *skb;
2000 	unsigned long i;
2001 	int ret;
2002 
2003 	rcu_read_lock();
2004 	for (i = 0; i < frame_cnt; i++) {
2005 		skb = ieee80211_tx_dequeue_ni(rtwdev->hw, txq);
2006 		if (!skb) {
2007 			rtw89_debug(rtwdev, RTW89_DBG_TXRX, "dequeue a NULL skb\n");
2008 			goto out;
2009 		}
2010 		rtw89_core_txq_check_agg(rtwdev, rtwtxq, skb);
2011 		ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, NULL);
2012 		if (ret) {
2013 			rtw89_err(rtwdev, "failed to push txq: %d\n", ret);
2014 			ieee80211_free_txskb(rtwdev->hw, skb);
2015 			break;
2016 		}
2017 	}
2018 out:
2019 	rcu_read_unlock();
2020 }
2021 
2022 static u32 rtw89_check_and_reclaim_tx_resource(struct rtw89_dev *rtwdev, u8 tid)
2023 {
2024 	u8 qsel, ch_dma;
2025 
2026 	qsel = rtw89_core_get_qsel(rtwdev, tid);
2027 	ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
2028 
2029 	return rtw89_hci_check_and_reclaim_tx_resource(rtwdev, ch_dma);
2030 }
2031 
2032 static bool rtw89_core_txq_agg_wait(struct rtw89_dev *rtwdev,
2033 				    struct ieee80211_txq *txq,
2034 				    unsigned long *frame_cnt,
2035 				    bool *sched_txq, bool *reinvoke)
2036 {
2037 	struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv;
2038 	struct ieee80211_sta *sta = txq->sta;
2039 	struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL;
2040 
2041 	if (!sta || rtwsta->max_agg_wait <= 0)
2042 		return false;
2043 
2044 	if (rtwdev->stats.tx_tfc_lv <= RTW89_TFC_MID)
2045 		return false;
2046 
2047 	if (*frame_cnt > 1) {
2048 		*frame_cnt -= 1;
2049 		*sched_txq = true;
2050 		*reinvoke = true;
2051 		rtwtxq->wait_cnt = 1;
2052 		return false;
2053 	}
2054 
2055 	if (*frame_cnt == 1 && rtwtxq->wait_cnt < rtwsta->max_agg_wait) {
2056 		*reinvoke = true;
2057 		rtwtxq->wait_cnt++;
2058 		return true;
2059 	}
2060 
2061 	rtwtxq->wait_cnt = 0;
2062 	return false;
2063 }
2064 
2065 static void rtw89_core_txq_schedule(struct rtw89_dev *rtwdev, u8 ac, bool *reinvoke)
2066 {
2067 	struct ieee80211_hw *hw = rtwdev->hw;
2068 	struct ieee80211_txq *txq;
2069 	struct rtw89_txq *rtwtxq;
2070 	unsigned long frame_cnt;
2071 	unsigned long byte_cnt;
2072 	u32 tx_resource;
2073 	bool sched_txq;
2074 
2075 	ieee80211_txq_schedule_start(hw, ac);
2076 	while ((txq = ieee80211_next_txq(hw, ac))) {
2077 		rtwtxq = (struct rtw89_txq *)txq->drv_priv;
2078 		tx_resource = rtw89_check_and_reclaim_tx_resource(rtwdev, txq->tid);
2079 		sched_txq = false;
2080 
2081 		ieee80211_txq_get_depth(txq, &frame_cnt, &byte_cnt);
2082 		if (rtw89_core_txq_agg_wait(rtwdev, txq, &frame_cnt, &sched_txq, reinvoke)) {
2083 			ieee80211_return_txq(hw, txq, true);
2084 			continue;
2085 		}
2086 		frame_cnt = min_t(unsigned long, frame_cnt, tx_resource);
2087 		rtw89_core_txq_push(rtwdev, rtwtxq, frame_cnt, byte_cnt);
2088 		ieee80211_return_txq(hw, txq, sched_txq);
2089 		if (frame_cnt != 0)
2090 			rtw89_core_tx_kick_off(rtwdev, rtw89_core_get_qsel(rtwdev, txq->tid));
2091 
2092 		/* bound of tx_resource could get stuck due to burst traffic */
2093 		if (frame_cnt == tx_resource)
2094 			*reinvoke = true;
2095 	}
2096 	ieee80211_txq_schedule_end(hw, ac);
2097 }
2098 
2099 static void rtw89_ips_work(struct work_struct *work)
2100 {
2101 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
2102 						ips_work);
2103 	mutex_lock(&rtwdev->mutex);
2104 	if (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE)
2105 		rtw89_enter_ips(rtwdev);
2106 	mutex_unlock(&rtwdev->mutex);
2107 }
2108 
2109 static void rtw89_core_txq_work(struct work_struct *w)
2110 {
2111 	struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev, txq_work);
2112 	bool reinvoke = false;
2113 	u8 ac;
2114 
2115 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2116 		rtw89_core_txq_schedule(rtwdev, ac, &reinvoke);
2117 
2118 	if (reinvoke) {
2119 		/* reinvoke to process the last frame */
2120 		mod_delayed_work(rtwdev->txq_wq, &rtwdev->txq_reinvoke_work, 1);
2121 	}
2122 }
2123 
2124 static void rtw89_core_txq_reinvoke_work(struct work_struct *w)
2125 {
2126 	struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev,
2127 						txq_reinvoke_work.work);
2128 
2129 	queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
2130 }
2131 
2132 static void rtw89_forbid_ba_work(struct work_struct *w)
2133 {
2134 	struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev,
2135 						forbid_ba_work.work);
2136 	struct rtw89_txq *rtwtxq, *tmp;
2137 
2138 	spin_lock_bh(&rtwdev->ba_lock);
2139 	list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->forbid_ba_list, list) {
2140 		clear_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
2141 		list_del_init(&rtwtxq->list);
2142 	}
2143 	spin_unlock_bh(&rtwdev->ba_lock);
2144 }
2145 
2146 static enum rtw89_tfc_lv rtw89_get_traffic_level(struct rtw89_dev *rtwdev,
2147 						 u32 throughput, u64 cnt)
2148 {
2149 	if (cnt < 100)
2150 		return RTW89_TFC_IDLE;
2151 	if (throughput > 50)
2152 		return RTW89_TFC_HIGH;
2153 	if (throughput > 10)
2154 		return RTW89_TFC_MID;
2155 	if (throughput > 2)
2156 		return RTW89_TFC_LOW;
2157 	return RTW89_TFC_ULTRA_LOW;
2158 }
2159 
2160 static bool rtw89_traffic_stats_calc(struct rtw89_dev *rtwdev,
2161 				     struct rtw89_traffic_stats *stats)
2162 {
2163 	enum rtw89_tfc_lv tx_tfc_lv = stats->tx_tfc_lv;
2164 	enum rtw89_tfc_lv rx_tfc_lv = stats->rx_tfc_lv;
2165 
2166 	stats->tx_throughput_raw = (u32)(stats->tx_unicast >> RTW89_TP_SHIFT);
2167 	stats->rx_throughput_raw = (u32)(stats->rx_unicast >> RTW89_TP_SHIFT);
2168 
2169 	ewma_tp_add(&stats->tx_ewma_tp, stats->tx_throughput_raw);
2170 	ewma_tp_add(&stats->rx_ewma_tp, stats->rx_throughput_raw);
2171 
2172 	stats->tx_throughput = ewma_tp_read(&stats->tx_ewma_tp);
2173 	stats->rx_throughput = ewma_tp_read(&stats->rx_ewma_tp);
2174 	stats->tx_tfc_lv = rtw89_get_traffic_level(rtwdev, stats->tx_throughput,
2175 						   stats->tx_cnt);
2176 	stats->rx_tfc_lv = rtw89_get_traffic_level(rtwdev, stats->rx_throughput,
2177 						   stats->rx_cnt);
2178 	stats->tx_avg_len = stats->tx_cnt ?
2179 			    DIV_ROUND_DOWN_ULL(stats->tx_unicast, stats->tx_cnt) : 0;
2180 	stats->rx_avg_len = stats->rx_cnt ?
2181 			    DIV_ROUND_DOWN_ULL(stats->rx_unicast, stats->rx_cnt) : 0;
2182 
2183 	stats->tx_unicast = 0;
2184 	stats->rx_unicast = 0;
2185 	stats->tx_cnt = 0;
2186 	stats->rx_cnt = 0;
2187 	stats->rx_tf_periodic = stats->rx_tf_acc;
2188 	stats->rx_tf_acc = 0;
2189 
2190 	if (tx_tfc_lv != stats->tx_tfc_lv || rx_tfc_lv != stats->rx_tfc_lv)
2191 		return true;
2192 
2193 	return false;
2194 }
2195 
2196 static bool rtw89_traffic_stats_track(struct rtw89_dev *rtwdev)
2197 {
2198 	struct rtw89_vif *rtwvif;
2199 	bool tfc_changed;
2200 
2201 	tfc_changed = rtw89_traffic_stats_calc(rtwdev, &rtwdev->stats);
2202 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
2203 		rtw89_traffic_stats_calc(rtwdev, &rtwvif->stats);
2204 
2205 	return tfc_changed;
2206 }
2207 
2208 static void rtw89_vif_enter_lps(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
2209 {
2210 	if ((rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION &&
2211 	     rtwvif->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT) ||
2212 	    rtwvif->tdls_peer)
2213 		return;
2214 
2215 	if (rtwvif->stats.tx_tfc_lv == RTW89_TFC_IDLE &&
2216 	    rtwvif->stats.rx_tfc_lv == RTW89_TFC_IDLE)
2217 		rtw89_enter_lps(rtwdev, rtwvif);
2218 }
2219 
2220 static void rtw89_enter_lps_track(struct rtw89_dev *rtwdev)
2221 {
2222 	struct rtw89_vif *rtwvif;
2223 
2224 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
2225 		rtw89_vif_enter_lps(rtwdev, rtwvif);
2226 }
2227 
2228 void rtw89_traffic_stats_init(struct rtw89_dev *rtwdev,
2229 			      struct rtw89_traffic_stats *stats)
2230 {
2231 	stats->tx_unicast = 0;
2232 	stats->rx_unicast = 0;
2233 	stats->tx_cnt = 0;
2234 	stats->rx_cnt = 0;
2235 	ewma_tp_init(&stats->tx_ewma_tp);
2236 	ewma_tp_init(&stats->rx_ewma_tp);
2237 }
2238 
2239 static void rtw89_track_work(struct work_struct *work)
2240 {
2241 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
2242 						track_work.work);
2243 	bool tfc_changed;
2244 
2245 	if (test_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags))
2246 		return;
2247 
2248 	mutex_lock(&rtwdev->mutex);
2249 
2250 	if (!test_bit(RTW89_FLAG_RUNNING, rtwdev->flags))
2251 		goto out;
2252 
2253 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work,
2254 				     RTW89_TRACK_WORK_PERIOD);
2255 
2256 	tfc_changed = rtw89_traffic_stats_track(rtwdev);
2257 	if (rtwdev->scanning)
2258 		goto out;
2259 
2260 	rtw89_leave_lps(rtwdev);
2261 
2262 	if (tfc_changed) {
2263 		rtw89_hci_recalc_int_mit(rtwdev);
2264 		rtw89_btc_ntfy_wl_sta(rtwdev);
2265 	}
2266 	rtw89_mac_bf_monitor_track(rtwdev);
2267 	rtw89_phy_stat_track(rtwdev);
2268 	rtw89_phy_env_monitor_track(rtwdev);
2269 	rtw89_phy_dig(rtwdev);
2270 	rtw89_chip_rfk_track(rtwdev);
2271 	rtw89_phy_ra_update(rtwdev);
2272 	rtw89_phy_cfo_track(rtwdev);
2273 	rtw89_phy_tx_path_div_track(rtwdev);
2274 	rtw89_phy_ul_tb_ctrl_track(rtwdev);
2275 
2276 	if (rtwdev->lps_enabled && !rtwdev->btc.lps)
2277 		rtw89_enter_lps_track(rtwdev);
2278 
2279 out:
2280 	mutex_unlock(&rtwdev->mutex);
2281 }
2282 
2283 u8 rtw89_core_acquire_bit_map(unsigned long *addr, unsigned long size)
2284 {
2285 	unsigned long bit;
2286 
2287 	bit = find_first_zero_bit(addr, size);
2288 	if (bit < size)
2289 		set_bit(bit, addr);
2290 
2291 	return bit;
2292 }
2293 
2294 void rtw89_core_release_bit_map(unsigned long *addr, u8 bit)
2295 {
2296 	clear_bit(bit, addr);
2297 }
2298 
2299 void rtw89_core_release_all_bits_map(unsigned long *addr, unsigned int nbits)
2300 {
2301 	bitmap_zero(addr, nbits);
2302 }
2303 
2304 int rtw89_core_acquire_sta_ba_entry(struct rtw89_dev *rtwdev,
2305 				    struct rtw89_sta *rtwsta, u8 tid, u8 *cam_idx)
2306 {
2307 	const struct rtw89_chip_info *chip = rtwdev->chip;
2308 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
2309 	struct rtw89_ba_cam_entry *entry = NULL, *tmp;
2310 	u8 idx;
2311 	int i;
2312 
2313 	lockdep_assert_held(&rtwdev->mutex);
2314 
2315 	idx = rtw89_core_acquire_bit_map(cam_info->ba_cam_map, chip->bacam_num);
2316 	if (idx == chip->bacam_num) {
2317 		/* allocate a static BA CAM to tid=0/5, so replace the existing
2318 		 * one if BA CAM is full. Hardware will process the original tid
2319 		 * automatically.
2320 		 */
2321 		if (tid != 0 && tid != 5)
2322 			return -ENOSPC;
2323 
2324 		for_each_set_bit(i, cam_info->ba_cam_map, chip->bacam_num) {
2325 			tmp = &cam_info->ba_cam_entry[i];
2326 			if (tmp->tid == 0 || tmp->tid == 5)
2327 				continue;
2328 
2329 			idx = i;
2330 			entry = tmp;
2331 			list_del(&entry->list);
2332 			break;
2333 		}
2334 
2335 		if (!entry)
2336 			return -ENOSPC;
2337 	} else {
2338 		entry = &cam_info->ba_cam_entry[idx];
2339 	}
2340 
2341 	entry->tid = tid;
2342 	list_add_tail(&entry->list, &rtwsta->ba_cam_list);
2343 
2344 	*cam_idx = idx;
2345 
2346 	return 0;
2347 }
2348 
2349 int rtw89_core_release_sta_ba_entry(struct rtw89_dev *rtwdev,
2350 				    struct rtw89_sta *rtwsta, u8 tid, u8 *cam_idx)
2351 {
2352 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
2353 	struct rtw89_ba_cam_entry *entry = NULL, *tmp;
2354 	u8 idx;
2355 
2356 	lockdep_assert_held(&rtwdev->mutex);
2357 
2358 	list_for_each_entry_safe(entry, tmp, &rtwsta->ba_cam_list, list) {
2359 		if (entry->tid != tid)
2360 			continue;
2361 
2362 		idx = entry - cam_info->ba_cam_entry;
2363 		list_del(&entry->list);
2364 
2365 		rtw89_core_release_bit_map(cam_info->ba_cam_map, idx);
2366 		*cam_idx = idx;
2367 		return 0;
2368 	}
2369 
2370 	return -ENOENT;
2371 }
2372 
2373 #define RTW89_TYPE_MAPPING(_type)	\
2374 	case NL80211_IFTYPE_ ## _type:	\
2375 		rtwvif->wifi_role = RTW89_WIFI_ROLE_ ## _type;	\
2376 		break
2377 void rtw89_vif_type_mapping(struct ieee80211_vif *vif, bool assoc)
2378 {
2379 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2380 
2381 	switch (vif->type) {
2382 	case NL80211_IFTYPE_STATION:
2383 		if (vif->p2p)
2384 			rtwvif->wifi_role = RTW89_WIFI_ROLE_P2P_CLIENT;
2385 		else
2386 			rtwvif->wifi_role = RTW89_WIFI_ROLE_STATION;
2387 		break;
2388 	case NL80211_IFTYPE_AP:
2389 		if (vif->p2p)
2390 			rtwvif->wifi_role = RTW89_WIFI_ROLE_P2P_GO;
2391 		else
2392 			rtwvif->wifi_role = RTW89_WIFI_ROLE_AP;
2393 		break;
2394 	RTW89_TYPE_MAPPING(ADHOC);
2395 	RTW89_TYPE_MAPPING(MONITOR);
2396 	RTW89_TYPE_MAPPING(MESH_POINT);
2397 	default:
2398 		WARN_ON(1);
2399 		break;
2400 	}
2401 
2402 	switch (vif->type) {
2403 	case NL80211_IFTYPE_AP:
2404 	case NL80211_IFTYPE_MESH_POINT:
2405 		rtwvif->net_type = RTW89_NET_TYPE_AP_MODE;
2406 		rtwvif->self_role = RTW89_SELF_ROLE_AP;
2407 		break;
2408 	case NL80211_IFTYPE_ADHOC:
2409 		rtwvif->net_type = RTW89_NET_TYPE_AD_HOC;
2410 		rtwvif->self_role = RTW89_SELF_ROLE_CLIENT;
2411 		break;
2412 	case NL80211_IFTYPE_STATION:
2413 		if (assoc) {
2414 			rtwvif->net_type = RTW89_NET_TYPE_INFRA;
2415 			rtwvif->trigger = vif->bss_conf.he_support;
2416 		} else {
2417 			rtwvif->net_type = RTW89_NET_TYPE_NO_LINK;
2418 			rtwvif->trigger = false;
2419 		}
2420 		rtwvif->self_role = RTW89_SELF_ROLE_CLIENT;
2421 		rtwvif->addr_cam.sec_ent_mode = RTW89_ADDR_CAM_SEC_NORMAL;
2422 		break;
2423 	case NL80211_IFTYPE_MONITOR:
2424 		break;
2425 	default:
2426 		WARN_ON(1);
2427 		break;
2428 	}
2429 }
2430 
2431 int rtw89_core_sta_add(struct rtw89_dev *rtwdev,
2432 		       struct ieee80211_vif *vif,
2433 		       struct ieee80211_sta *sta)
2434 {
2435 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2436 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2437 	int i;
2438 	int ret;
2439 
2440 	rtwsta->rtwdev = rtwdev;
2441 	rtwsta->rtwvif = rtwvif;
2442 	rtwsta->prev_rssi = 0;
2443 	INIT_LIST_HEAD(&rtwsta->ba_cam_list);
2444 
2445 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
2446 		rtw89_core_txq_init(rtwdev, sta->txq[i]);
2447 
2448 	ewma_rssi_init(&rtwsta->avg_rssi);
2449 	for (i = 0; i < rtwdev->chip->rf_path_num; i++)
2450 		ewma_rssi_init(&rtwsta->rssi[i]);
2451 
2452 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
2453 		/* for station mode, assign the mac_id from itself */
2454 		rtwsta->mac_id = rtwvif->mac_id;
2455 		rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta,
2456 					 BTC_ROLE_MSTS_STA_CONN_START);
2457 		rtw89_chip_rfk_channel(rtwdev);
2458 	} else if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
2459 		rtwsta->mac_id = rtw89_core_acquire_bit_map(rtwdev->mac_id_map,
2460 							    RTW89_MAX_MAC_ID_NUM);
2461 		if (rtwsta->mac_id == RTW89_MAX_MAC_ID_NUM)
2462 			return -ENOSPC;
2463 
2464 		ret = rtw89_mac_set_macid_pause(rtwdev, rtwsta->mac_id, false);
2465 		if (ret) {
2466 			rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwsta->mac_id);
2467 			rtw89_warn(rtwdev, "failed to send h2c macid pause\n");
2468 			return ret;
2469 		}
2470 
2471 		ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta,
2472 						 RTW89_ROLE_CREATE);
2473 		if (ret) {
2474 			rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwsta->mac_id);
2475 			rtw89_warn(rtwdev, "failed to send h2c role info\n");
2476 			return ret;
2477 		}
2478 	}
2479 
2480 	return 0;
2481 }
2482 
2483 int rtw89_core_sta_disassoc(struct rtw89_dev *rtwdev,
2484 			    struct ieee80211_vif *vif,
2485 			    struct ieee80211_sta *sta)
2486 {
2487 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2488 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2489 
2490 	rtwdev->total_sta_assoc--;
2491 	if (sta->tdls)
2492 		rtwvif->tdls_peer--;
2493 	rtwsta->disassoc = true;
2494 
2495 	return 0;
2496 }
2497 
2498 int rtw89_core_sta_disconnect(struct rtw89_dev *rtwdev,
2499 			      struct ieee80211_vif *vif,
2500 			      struct ieee80211_sta *sta)
2501 {
2502 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2503 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2504 	int ret;
2505 
2506 	rtw89_mac_bf_monitor_calc(rtwdev, sta, true);
2507 	rtw89_mac_bf_disassoc(rtwdev, vif, sta);
2508 	rtw89_core_free_sta_pending_ba(rtwdev, sta);
2509 	rtw89_core_free_sta_pending_forbid_ba(rtwdev, sta);
2510 	if (vif->type == NL80211_IFTYPE_AP || sta->tdls)
2511 		rtw89_cam_deinit_addr_cam(rtwdev, &rtwsta->addr_cam);
2512 	if (sta->tdls)
2513 		rtw89_cam_deinit_bssid_cam(rtwdev, &rtwsta->bssid_cam);
2514 
2515 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
2516 		rtw89_vif_type_mapping(vif, false);
2517 		rtw89_fw_release_general_pkt_list_vif(rtwdev, rtwvif, true);
2518 	}
2519 
2520 	ret = rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, sta);
2521 	if (ret) {
2522 		rtw89_warn(rtwdev, "failed to send h2c cmac table\n");
2523 		return ret;
2524 	}
2525 
2526 	ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, true);
2527 	if (ret) {
2528 		rtw89_warn(rtwdev, "failed to send h2c join info\n");
2529 		return ret;
2530 	}
2531 
2532 	/* update cam aid mac_id net_type */
2533 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
2534 	if (ret) {
2535 		rtw89_warn(rtwdev, "failed to send h2c cam\n");
2536 		return ret;
2537 	}
2538 
2539 	return ret;
2540 }
2541 
2542 int rtw89_core_sta_assoc(struct rtw89_dev *rtwdev,
2543 			 struct ieee80211_vif *vif,
2544 			 struct ieee80211_sta *sta)
2545 {
2546 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2547 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2548 	struct rtw89_bssid_cam_entry *bssid_cam = rtw89_get_bssid_cam_of(rtwvif, rtwsta);
2549 	int ret;
2550 
2551 	if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
2552 		if (sta->tdls) {
2553 			ret = rtw89_cam_init_bssid_cam(rtwdev, rtwvif, bssid_cam, sta->addr);
2554 			if (ret) {
2555 				rtw89_warn(rtwdev, "failed to send h2c init bssid cam for TDLS\n");
2556 				return ret;
2557 			}
2558 		}
2559 
2560 		ret = rtw89_cam_init_addr_cam(rtwdev, &rtwsta->addr_cam, bssid_cam);
2561 		if (ret) {
2562 			rtw89_warn(rtwdev, "failed to send h2c init addr cam\n");
2563 			return ret;
2564 		}
2565 	}
2566 
2567 	ret = rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, sta);
2568 	if (ret) {
2569 		rtw89_warn(rtwdev, "failed to send h2c cmac table\n");
2570 		return ret;
2571 	}
2572 
2573 	ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, false);
2574 	if (ret) {
2575 		rtw89_warn(rtwdev, "failed to send h2c join info\n");
2576 		return ret;
2577 	}
2578 
2579 	/* update cam aid mac_id net_type */
2580 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
2581 	if (ret) {
2582 		rtw89_warn(rtwdev, "failed to send h2c cam\n");
2583 		return ret;
2584 	}
2585 
2586 	rtwdev->total_sta_assoc++;
2587 	if (sta->tdls)
2588 		rtwvif->tdls_peer++;
2589 	rtw89_phy_ra_assoc(rtwdev, sta);
2590 	rtw89_mac_bf_assoc(rtwdev, vif, sta);
2591 	rtw89_mac_bf_monitor_calc(rtwdev, sta, false);
2592 
2593 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
2594 		struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
2595 
2596 		if (bss_conf->he_support &&
2597 		    !(bss_conf->he_oper.params & IEEE80211_HE_OPERATION_ER_SU_DISABLE))
2598 			rtwsta->er_cap = true;
2599 
2600 		rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta,
2601 					 BTC_ROLE_MSTS_STA_CONN_END);
2602 		rtw89_core_get_no_ul_ofdma_htc(rtwdev, &rtwsta->htc_template);
2603 		rtw89_phy_ul_tb_assoc(rtwdev, rtwvif);
2604 
2605 		ret = rtw89_fw_h2c_general_pkt(rtwdev, rtwvif, rtwsta->mac_id);
2606 		if (ret) {
2607 			rtw89_warn(rtwdev, "failed to send h2c general packet\n");
2608 			return ret;
2609 		}
2610 	}
2611 
2612 	return ret;
2613 }
2614 
2615 int rtw89_core_sta_remove(struct rtw89_dev *rtwdev,
2616 			  struct ieee80211_vif *vif,
2617 			  struct ieee80211_sta *sta)
2618 {
2619 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2620 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2621 	int ret;
2622 
2623 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
2624 		rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta,
2625 					 BTC_ROLE_MSTS_STA_DIS_CONN);
2626 	else if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
2627 		rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwsta->mac_id);
2628 
2629 		ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta,
2630 						 RTW89_ROLE_REMOVE);
2631 		if (ret) {
2632 			rtw89_warn(rtwdev, "failed to send h2c role info\n");
2633 			return ret;
2634 		}
2635 	}
2636 
2637 	return 0;
2638 }
2639 
2640 static void _rtw89_core_set_tid_config(struct rtw89_dev *rtwdev,
2641 				       struct ieee80211_sta *sta,
2642 				       struct cfg80211_tid_cfg *tid_conf)
2643 {
2644 	struct ieee80211_txq *txq;
2645 	struct rtw89_txq *rtwtxq;
2646 	u32 mask = tid_conf->mask;
2647 	u8 tids = tid_conf->tids;
2648 	int tids_nbit = BITS_PER_BYTE;
2649 	int i;
2650 
2651 	for (i = 0; i < tids_nbit; i++, tids >>= 1) {
2652 		if (!tids)
2653 			break;
2654 
2655 		if (!(tids & BIT(0)))
2656 			continue;
2657 
2658 		txq = sta->txq[i];
2659 		rtwtxq = (struct rtw89_txq *)txq->drv_priv;
2660 
2661 		if (mask & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
2662 			if (tid_conf->ampdu == NL80211_TID_CONFIG_ENABLE) {
2663 				clear_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
2664 			} else {
2665 				if (test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags))
2666 					ieee80211_stop_tx_ba_session(sta, txq->tid);
2667 				spin_lock_bh(&rtwdev->ba_lock);
2668 				list_del_init(&rtwtxq->list);
2669 				set_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
2670 				spin_unlock_bh(&rtwdev->ba_lock);
2671 			}
2672 		}
2673 
2674 		if (mask & BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL) && tids == 0xff) {
2675 			if (tid_conf->amsdu == NL80211_TID_CONFIG_ENABLE)
2676 				sta->max_amsdu_subframes = 0;
2677 			else
2678 				sta->max_amsdu_subframes = 1;
2679 		}
2680 	}
2681 }
2682 
2683 void rtw89_core_set_tid_config(struct rtw89_dev *rtwdev,
2684 			       struct ieee80211_sta *sta,
2685 			       struct cfg80211_tid_config *tid_config)
2686 {
2687 	int i;
2688 
2689 	for (i = 0; i < tid_config->n_tid_conf; i++)
2690 		_rtw89_core_set_tid_config(rtwdev, sta,
2691 					   &tid_config->tid_conf[i]);
2692 }
2693 
2694 static void rtw89_init_ht_cap(struct rtw89_dev *rtwdev,
2695 			      struct ieee80211_sta_ht_cap *ht_cap)
2696 {
2697 	static const __le16 highest[RF_PATH_MAX] = {
2698 		cpu_to_le16(150), cpu_to_le16(300), cpu_to_le16(450), cpu_to_le16(600),
2699 	};
2700 	struct rtw89_hal *hal = &rtwdev->hal;
2701 	u8 nss = hal->rx_nss;
2702 	int i;
2703 
2704 	ht_cap->ht_supported = true;
2705 	ht_cap->cap = 0;
2706 	ht_cap->cap |= IEEE80211_HT_CAP_SGI_20 |
2707 		       IEEE80211_HT_CAP_MAX_AMSDU |
2708 		       IEEE80211_HT_CAP_TX_STBC |
2709 		       (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
2710 	ht_cap->cap |= IEEE80211_HT_CAP_LDPC_CODING;
2711 	ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2712 		       IEEE80211_HT_CAP_DSSSCCK40 |
2713 		       IEEE80211_HT_CAP_SGI_40;
2714 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2715 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2716 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2717 	for (i = 0; i < nss; i++)
2718 		ht_cap->mcs.rx_mask[i] = 0xFF;
2719 	ht_cap->mcs.rx_mask[4] = 0x01;
2720 	ht_cap->mcs.rx_highest = highest[nss - 1];
2721 }
2722 
2723 static void rtw89_init_vht_cap(struct rtw89_dev *rtwdev,
2724 			       struct ieee80211_sta_vht_cap *vht_cap)
2725 {
2726 	static const __le16 highest_bw80[RF_PATH_MAX] = {
2727 		cpu_to_le16(433), cpu_to_le16(867), cpu_to_le16(1300), cpu_to_le16(1733),
2728 	};
2729 	static const __le16 highest_bw160[RF_PATH_MAX] = {
2730 		cpu_to_le16(867), cpu_to_le16(1733), cpu_to_le16(2600), cpu_to_le16(3467),
2731 	};
2732 	const struct rtw89_chip_info *chip = rtwdev->chip;
2733 	const __le16 *highest = chip->support_bw160 ? highest_bw160 : highest_bw80;
2734 	struct rtw89_hal *hal = &rtwdev->hal;
2735 	u16 tx_mcs_map = 0, rx_mcs_map = 0;
2736 	u8 sts_cap = 3;
2737 	int i;
2738 
2739 	for (i = 0; i < 8; i++) {
2740 		if (i < hal->tx_nss)
2741 			tx_mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
2742 		else
2743 			tx_mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
2744 		if (i < hal->rx_nss)
2745 			rx_mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
2746 		else
2747 			rx_mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
2748 	}
2749 
2750 	vht_cap->vht_supported = true;
2751 	vht_cap->cap = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
2752 		       IEEE80211_VHT_CAP_SHORT_GI_80 |
2753 		       IEEE80211_VHT_CAP_RXSTBC_1 |
2754 		       IEEE80211_VHT_CAP_HTC_VHT |
2755 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
2756 		       0;
2757 	vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
2758 	vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC;
2759 	vht_cap->cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2760 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
2761 	vht_cap->cap |= sts_cap << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
2762 	if (chip->support_bw160)
2763 		vht_cap->cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2764 				IEEE80211_VHT_CAP_SHORT_GI_160;
2765 	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(rx_mcs_map);
2766 	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(tx_mcs_map);
2767 	vht_cap->vht_mcs.rx_highest = highest[hal->rx_nss - 1];
2768 	vht_cap->vht_mcs.tx_highest = highest[hal->tx_nss - 1];
2769 }
2770 
2771 #define RTW89_SBAND_IFTYPES_NR 2
2772 
2773 static void rtw89_init_he_cap(struct rtw89_dev *rtwdev,
2774 			      enum nl80211_band band,
2775 			      struct ieee80211_supported_band *sband)
2776 {
2777 	const struct rtw89_chip_info *chip = rtwdev->chip;
2778 	struct rtw89_hal *hal = &rtwdev->hal;
2779 	struct ieee80211_sband_iftype_data *iftype_data;
2780 	bool no_ng16 = (chip->chip_id == RTL8852A && hal->cv == CHIP_CBV) ||
2781 		       (chip->chip_id == RTL8852B && hal->cv == CHIP_CAV);
2782 	u16 mcs_map = 0;
2783 	int i;
2784 	int nss = hal->rx_nss;
2785 	int idx = 0;
2786 
2787 	iftype_data = kcalloc(RTW89_SBAND_IFTYPES_NR, sizeof(*iftype_data), GFP_KERNEL);
2788 	if (!iftype_data)
2789 		return;
2790 
2791 	for (i = 0; i < 8; i++) {
2792 		if (i < nss)
2793 			mcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
2794 		else
2795 			mcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
2796 	}
2797 
2798 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
2799 		struct ieee80211_sta_he_cap *he_cap;
2800 		u8 *mac_cap_info;
2801 		u8 *phy_cap_info;
2802 
2803 		switch (i) {
2804 		case NL80211_IFTYPE_STATION:
2805 		case NL80211_IFTYPE_AP:
2806 			break;
2807 		default:
2808 			continue;
2809 		}
2810 
2811 		if (idx >= RTW89_SBAND_IFTYPES_NR) {
2812 			rtw89_warn(rtwdev, "run out of iftype_data\n");
2813 			break;
2814 		}
2815 
2816 		iftype_data[idx].types_mask = BIT(i);
2817 		he_cap = &iftype_data[idx].he_cap;
2818 		mac_cap_info = he_cap->he_cap_elem.mac_cap_info;
2819 		phy_cap_info = he_cap->he_cap_elem.phy_cap_info;
2820 
2821 		he_cap->has_he = true;
2822 		mac_cap_info[0] = IEEE80211_HE_MAC_CAP0_HTC_HE;
2823 		if (i == NL80211_IFTYPE_STATION)
2824 			mac_cap_info[1] = IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
2825 		mac_cap_info[2] = IEEE80211_HE_MAC_CAP2_ALL_ACK |
2826 				  IEEE80211_HE_MAC_CAP2_BSR;
2827 		mac_cap_info[3] = IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_2;
2828 		if (i == NL80211_IFTYPE_AP)
2829 			mac_cap_info[3] |= IEEE80211_HE_MAC_CAP3_OMI_CONTROL;
2830 		mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_OPS |
2831 				  IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
2832 		if (i == NL80211_IFTYPE_STATION)
2833 			mac_cap_info[5] = IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
2834 		if (band == NL80211_BAND_2GHZ) {
2835 			phy_cap_info[0] =
2836 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
2837 		} else {
2838 			phy_cap_info[0] =
2839 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
2840 			if (chip->support_bw160)
2841 				phy_cap_info[0] |= IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
2842 		}
2843 		phy_cap_info[1] = IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2844 				  IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2845 				  IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
2846 		phy_cap_info[2] = IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2847 				  IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2848 				  IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2849 				  IEEE80211_HE_PHY_CAP2_DOPPLER_TX;
2850 		phy_cap_info[3] = IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM;
2851 		if (i == NL80211_IFTYPE_STATION)
2852 			phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_16_QAM |
2853 					   IEEE80211_HE_PHY_CAP3_DCM_MAX_TX_NSS_2;
2854 		if (i == NL80211_IFTYPE_AP)
2855 			phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU;
2856 		phy_cap_info[4] = IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
2857 				  IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
2858 		if (chip->support_bw160)
2859 			phy_cap_info[4] |= IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4;
2860 		phy_cap_info[5] = no_ng16 ? 0 :
2861 				  IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
2862 				  IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
2863 		phy_cap_info[6] = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
2864 				  IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
2865 				  IEEE80211_HE_PHY_CAP6_TRIG_SU_BEAMFORMING_FB |
2866 				  IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE;
2867 		phy_cap_info[7] = IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
2868 				  IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI |
2869 				  IEEE80211_HE_PHY_CAP7_MAX_NC_1;
2870 		phy_cap_info[8] = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
2871 				  IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI |
2872 				  IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_996;
2873 		if (chip->support_bw160)
2874 			phy_cap_info[8] |= IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
2875 					   IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
2876 		phy_cap_info[9] = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
2877 				  IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
2878 				  IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
2879 				  IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB |
2880 				  u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
2881 						 IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
2882 		if (i == NL80211_IFTYPE_STATION)
2883 			phy_cap_info[9] |= IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
2884 		he_cap->he_mcs_nss_supp.rx_mcs_80 = cpu_to_le16(mcs_map);
2885 		he_cap->he_mcs_nss_supp.tx_mcs_80 = cpu_to_le16(mcs_map);
2886 		if (chip->support_bw160) {
2887 			he_cap->he_mcs_nss_supp.rx_mcs_160 = cpu_to_le16(mcs_map);
2888 			he_cap->he_mcs_nss_supp.tx_mcs_160 = cpu_to_le16(mcs_map);
2889 		}
2890 
2891 		if (band == NL80211_BAND_6GHZ) {
2892 			__le16 capa;
2893 
2894 			capa = le16_encode_bits(IEEE80211_HT_MPDU_DENSITY_NONE,
2895 						IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START) |
2896 			       le16_encode_bits(IEEE80211_VHT_MAX_AMPDU_1024K,
2897 						IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP) |
2898 			       le16_encode_bits(IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454,
2899 						IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
2900 			iftype_data[idx].he_6ghz_capa.capa = capa;
2901 		}
2902 
2903 		idx++;
2904 	}
2905 
2906 	sband->iftype_data = iftype_data;
2907 	sband->n_iftype_data = idx;
2908 }
2909 
2910 static int rtw89_core_set_supported_band(struct rtw89_dev *rtwdev)
2911 {
2912 	struct ieee80211_hw *hw = rtwdev->hw;
2913 	struct ieee80211_supported_band *sband_2ghz = NULL, *sband_5ghz = NULL;
2914 	struct ieee80211_supported_band *sband_6ghz = NULL;
2915 	u32 size = sizeof(struct ieee80211_supported_band);
2916 	u8 support_bands = rtwdev->chip->support_bands;
2917 
2918 	if (support_bands & BIT(NL80211_BAND_2GHZ)) {
2919 		sband_2ghz = kmemdup(&rtw89_sband_2ghz, size, GFP_KERNEL);
2920 		if (!sband_2ghz)
2921 			goto err;
2922 		rtw89_init_ht_cap(rtwdev, &sband_2ghz->ht_cap);
2923 		rtw89_init_he_cap(rtwdev, NL80211_BAND_2GHZ, sband_2ghz);
2924 		hw->wiphy->bands[NL80211_BAND_2GHZ] = sband_2ghz;
2925 	}
2926 
2927 	if (support_bands & BIT(NL80211_BAND_5GHZ)) {
2928 		sband_5ghz = kmemdup(&rtw89_sband_5ghz, size, GFP_KERNEL);
2929 		if (!sband_5ghz)
2930 			goto err;
2931 		rtw89_init_ht_cap(rtwdev, &sband_5ghz->ht_cap);
2932 		rtw89_init_vht_cap(rtwdev, &sband_5ghz->vht_cap);
2933 		rtw89_init_he_cap(rtwdev, NL80211_BAND_5GHZ, sband_5ghz);
2934 		hw->wiphy->bands[NL80211_BAND_5GHZ] = sband_5ghz;
2935 	}
2936 
2937 	if (support_bands & BIT(NL80211_BAND_6GHZ)) {
2938 		sband_6ghz = kmemdup(&rtw89_sband_6ghz, size, GFP_KERNEL);
2939 		if (!sband_6ghz)
2940 			goto err;
2941 		rtw89_init_he_cap(rtwdev, NL80211_BAND_6GHZ, sband_6ghz);
2942 		hw->wiphy->bands[NL80211_BAND_6GHZ] = sband_6ghz;
2943 	}
2944 
2945 	return 0;
2946 
2947 err:
2948 	hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
2949 	hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
2950 	hw->wiphy->bands[NL80211_BAND_6GHZ] = NULL;
2951 	if (sband_2ghz)
2952 		kfree(sband_2ghz->iftype_data);
2953 	if (sband_5ghz)
2954 		kfree(sband_5ghz->iftype_data);
2955 	if (sband_6ghz)
2956 		kfree(sband_6ghz->iftype_data);
2957 	kfree(sband_2ghz);
2958 	kfree(sband_5ghz);
2959 	kfree(sband_6ghz);
2960 	return -ENOMEM;
2961 }
2962 
2963 static void rtw89_core_clr_supported_band(struct rtw89_dev *rtwdev)
2964 {
2965 	struct ieee80211_hw *hw = rtwdev->hw;
2966 
2967 	kfree(hw->wiphy->bands[NL80211_BAND_2GHZ]->iftype_data);
2968 	kfree(hw->wiphy->bands[NL80211_BAND_5GHZ]->iftype_data);
2969 	if (hw->wiphy->bands[NL80211_BAND_6GHZ])
2970 		kfree(hw->wiphy->bands[NL80211_BAND_6GHZ]->iftype_data);
2971 	kfree(hw->wiphy->bands[NL80211_BAND_2GHZ]);
2972 	kfree(hw->wiphy->bands[NL80211_BAND_5GHZ]);
2973 	kfree(hw->wiphy->bands[NL80211_BAND_6GHZ]);
2974 	hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
2975 	hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
2976 	hw->wiphy->bands[NL80211_BAND_6GHZ] = NULL;
2977 }
2978 
2979 static void rtw89_core_ppdu_sts_init(struct rtw89_dev *rtwdev)
2980 {
2981 	int i;
2982 
2983 	for (i = 0; i < RTW89_PHY_MAX; i++)
2984 		skb_queue_head_init(&rtwdev->ppdu_sts.rx_queue[i]);
2985 	for (i = 0; i < RTW89_PHY_MAX; i++)
2986 		rtwdev->ppdu_sts.curr_rx_ppdu_cnt[i] = U8_MAX;
2987 }
2988 
2989 void rtw89_core_update_beacon_work(struct work_struct *work)
2990 {
2991 	struct rtw89_dev *rtwdev;
2992 	struct rtw89_vif *rtwvif = container_of(work, struct rtw89_vif,
2993 						update_beacon_work);
2994 
2995 	if (rtwvif->net_type != RTW89_NET_TYPE_AP_MODE)
2996 		return;
2997 
2998 	rtwdev = rtwvif->rtwdev;
2999 	mutex_lock(&rtwdev->mutex);
3000 	rtw89_fw_h2c_update_beacon(rtwdev, rtwvif);
3001 	mutex_unlock(&rtwdev->mutex);
3002 }
3003 
3004 int rtw89_wait_for_cond(struct rtw89_wait_info *wait, unsigned int cond)
3005 {
3006 	struct completion *cmpl = &wait->completion;
3007 	unsigned long timeout;
3008 	unsigned int cur;
3009 
3010 	cur = atomic_cmpxchg(&wait->cond, RTW89_WAIT_COND_IDLE, cond);
3011 	if (cur != RTW89_WAIT_COND_IDLE)
3012 		return -EBUSY;
3013 
3014 	timeout = wait_for_completion_timeout(cmpl, RTW89_WAIT_FOR_COND_TIMEOUT);
3015 	if (timeout == 0) {
3016 		atomic_set(&wait->cond, RTW89_WAIT_COND_IDLE);
3017 		return -ETIMEDOUT;
3018 	}
3019 
3020 	if (wait->data.err)
3021 		return -EFAULT;
3022 
3023 	return 0;
3024 }
3025 
3026 void rtw89_complete_cond(struct rtw89_wait_info *wait, unsigned int cond,
3027 			 const struct rtw89_completion_data *data)
3028 {
3029 	unsigned int cur;
3030 
3031 	cur = atomic_cmpxchg(&wait->cond, cond, RTW89_WAIT_COND_IDLE);
3032 	if (cur != cond)
3033 		return;
3034 
3035 	wait->data = *data;
3036 	complete(&wait->completion);
3037 }
3038 
3039 int rtw89_core_start(struct rtw89_dev *rtwdev)
3040 {
3041 	int ret;
3042 
3043 	rtwdev->mac.qta_mode = RTW89_QTA_SCC;
3044 	ret = rtw89_mac_init(rtwdev);
3045 	if (ret) {
3046 		rtw89_err(rtwdev, "mac init fail, ret:%d\n", ret);
3047 		return ret;
3048 	}
3049 
3050 	rtw89_btc_ntfy_poweron(rtwdev);
3051 
3052 	/* efuse process */
3053 
3054 	/* pre-config BB/RF, BB reset/RFC reset */
3055 	ret = rtw89_chip_disable_bb_rf(rtwdev);
3056 	if (ret)
3057 		return ret;
3058 	ret = rtw89_chip_enable_bb_rf(rtwdev);
3059 	if (ret)
3060 		return ret;
3061 
3062 	rtw89_phy_init_bb_reg(rtwdev);
3063 	rtw89_phy_init_rf_reg(rtwdev, false);
3064 
3065 	rtw89_btc_ntfy_init(rtwdev, BTC_MODE_NORMAL);
3066 
3067 	rtw89_phy_dm_init(rtwdev);
3068 
3069 	rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true);
3070 	rtw89_mac_update_rts_threshold(rtwdev, RTW89_MAC_0);
3071 
3072 	ret = rtw89_hci_start(rtwdev);
3073 	if (ret) {
3074 		rtw89_err(rtwdev, "failed to start hci\n");
3075 		return ret;
3076 	}
3077 
3078 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work,
3079 				     RTW89_TRACK_WORK_PERIOD);
3080 
3081 	set_bit(RTW89_FLAG_RUNNING, rtwdev->flags);
3082 
3083 	rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_ON);
3084 	rtw89_fw_h2c_fw_log(rtwdev, rtwdev->fw.fw_log_enable);
3085 	rtw89_fw_h2c_init_ba_cam(rtwdev);
3086 
3087 	return 0;
3088 }
3089 
3090 void rtw89_core_stop(struct rtw89_dev *rtwdev)
3091 {
3092 	struct rtw89_btc *btc = &rtwdev->btc;
3093 
3094 	/* Prvent to stop twice; enter_ips and ops_stop */
3095 	if (!test_bit(RTW89_FLAG_RUNNING, rtwdev->flags))
3096 		return;
3097 
3098 	rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_OFF);
3099 
3100 	clear_bit(RTW89_FLAG_RUNNING, rtwdev->flags);
3101 
3102 	mutex_unlock(&rtwdev->mutex);
3103 
3104 	cancel_work_sync(&rtwdev->c2h_work);
3105 	cancel_work_sync(&btc->eapol_notify_work);
3106 	cancel_work_sync(&btc->arp_notify_work);
3107 	cancel_work_sync(&btc->dhcp_notify_work);
3108 	cancel_work_sync(&btc->icmp_notify_work);
3109 	cancel_delayed_work_sync(&rtwdev->txq_reinvoke_work);
3110 	cancel_delayed_work_sync(&rtwdev->track_work);
3111 	cancel_delayed_work_sync(&rtwdev->coex_act1_work);
3112 	cancel_delayed_work_sync(&rtwdev->coex_bt_devinfo_work);
3113 	cancel_delayed_work_sync(&rtwdev->coex_rfk_chk_work);
3114 	cancel_delayed_work_sync(&rtwdev->cfo_track_work);
3115 	cancel_delayed_work_sync(&rtwdev->forbid_ba_work);
3116 
3117 	mutex_lock(&rtwdev->mutex);
3118 
3119 	rtw89_btc_ntfy_poweroff(rtwdev);
3120 	rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1, true);
3121 	rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, true);
3122 	rtw89_hci_stop(rtwdev);
3123 	rtw89_hci_deinit(rtwdev);
3124 	rtw89_mac_pwr_off(rtwdev);
3125 	rtw89_hci_reset(rtwdev);
3126 }
3127 
3128 int rtw89_core_init(struct rtw89_dev *rtwdev)
3129 {
3130 	struct rtw89_btc *btc = &rtwdev->btc;
3131 	int ret;
3132 	u8 band;
3133 
3134 	INIT_LIST_HEAD(&rtwdev->ba_list);
3135 	INIT_LIST_HEAD(&rtwdev->forbid_ba_list);
3136 	INIT_LIST_HEAD(&rtwdev->rtwvifs_list);
3137 	INIT_LIST_HEAD(&rtwdev->early_h2c_list);
3138 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3139 		if (!(rtwdev->chip->support_bands & BIT(band)))
3140 			continue;
3141 		INIT_LIST_HEAD(&rtwdev->scan_info.pkt_list[band]);
3142 	}
3143 	INIT_WORK(&rtwdev->ba_work, rtw89_core_ba_work);
3144 	INIT_WORK(&rtwdev->txq_work, rtw89_core_txq_work);
3145 	INIT_DELAYED_WORK(&rtwdev->txq_reinvoke_work, rtw89_core_txq_reinvoke_work);
3146 	INIT_DELAYED_WORK(&rtwdev->track_work, rtw89_track_work);
3147 	INIT_DELAYED_WORK(&rtwdev->coex_act1_work, rtw89_coex_act1_work);
3148 	INIT_DELAYED_WORK(&rtwdev->coex_bt_devinfo_work, rtw89_coex_bt_devinfo_work);
3149 	INIT_DELAYED_WORK(&rtwdev->coex_rfk_chk_work, rtw89_coex_rfk_chk_work);
3150 	INIT_DELAYED_WORK(&rtwdev->cfo_track_work, rtw89_phy_cfo_track_work);
3151 	INIT_DELAYED_WORK(&rtwdev->forbid_ba_work, rtw89_forbid_ba_work);
3152 	rtwdev->txq_wq = alloc_workqueue("rtw89_tx_wq", WQ_UNBOUND | WQ_HIGHPRI, 0);
3153 	if (!rtwdev->txq_wq)
3154 		return -ENOMEM;
3155 	spin_lock_init(&rtwdev->ba_lock);
3156 	spin_lock_init(&rtwdev->rpwm_lock);
3157 	mutex_init(&rtwdev->mutex);
3158 	mutex_init(&rtwdev->rf_mutex);
3159 	rtwdev->total_sta_assoc = 0;
3160 
3161 	rtw89_init_wait(&rtwdev->mcc.wait);
3162 
3163 	INIT_WORK(&rtwdev->c2h_work, rtw89_fw_c2h_work);
3164 	INIT_WORK(&rtwdev->ips_work, rtw89_ips_work);
3165 	skb_queue_head_init(&rtwdev->c2h_queue);
3166 	rtw89_core_ppdu_sts_init(rtwdev);
3167 	rtw89_traffic_stats_init(rtwdev, &rtwdev->stats);
3168 
3169 	rtwdev->hal.rx_fltr = DEFAULT_AX_RX_FLTR;
3170 
3171 	INIT_WORK(&btc->eapol_notify_work, rtw89_btc_ntfy_eapol_packet_work);
3172 	INIT_WORK(&btc->arp_notify_work, rtw89_btc_ntfy_arp_packet_work);
3173 	INIT_WORK(&btc->dhcp_notify_work, rtw89_btc_ntfy_dhcp_packet_work);
3174 	INIT_WORK(&btc->icmp_notify_work, rtw89_btc_ntfy_icmp_packet_work);
3175 
3176 	ret = rtw89_load_firmware(rtwdev);
3177 	if (ret) {
3178 		rtw89_warn(rtwdev, "no firmware loaded\n");
3179 		destroy_workqueue(rtwdev->txq_wq);
3180 		return ret;
3181 	}
3182 	rtw89_ser_init(rtwdev);
3183 	rtw89_entity_init(rtwdev);
3184 
3185 	return 0;
3186 }
3187 EXPORT_SYMBOL(rtw89_core_init);
3188 
3189 void rtw89_core_deinit(struct rtw89_dev *rtwdev)
3190 {
3191 	rtw89_ser_deinit(rtwdev);
3192 	rtw89_unload_firmware(rtwdev);
3193 	rtw89_fw_free_all_early_h2c(rtwdev);
3194 
3195 	destroy_workqueue(rtwdev->txq_wq);
3196 	mutex_destroy(&rtwdev->rf_mutex);
3197 	mutex_destroy(&rtwdev->mutex);
3198 }
3199 EXPORT_SYMBOL(rtw89_core_deinit);
3200 
3201 void rtw89_core_scan_start(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
3202 			   const u8 *mac_addr, bool hw_scan)
3203 {
3204 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3205 
3206 	rtwdev->scanning = true;
3207 	rtw89_leave_lps(rtwdev);
3208 	if (hw_scan && (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE))
3209 		rtw89_leave_ips(rtwdev);
3210 
3211 	ether_addr_copy(rtwvif->mac_addr, mac_addr);
3212 	rtw89_btc_ntfy_scan_start(rtwdev, RTW89_PHY_0, chan->band_type);
3213 	rtw89_chip_rfk_scan(rtwdev, true);
3214 	rtw89_hci_recalc_int_mit(rtwdev);
3215 
3216 	rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, mac_addr);
3217 }
3218 
3219 void rtw89_core_scan_complete(struct rtw89_dev *rtwdev,
3220 			      struct ieee80211_vif *vif, bool hw_scan)
3221 {
3222 	struct rtw89_vif *rtwvif = vif ? (struct rtw89_vif *)vif->drv_priv : NULL;
3223 
3224 	if (!rtwvif)
3225 		return;
3226 
3227 	ether_addr_copy(rtwvif->mac_addr, vif->addr);
3228 	rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
3229 
3230 	rtw89_chip_rfk_scan(rtwdev, false);
3231 	rtw89_btc_ntfy_scan_finish(rtwdev, RTW89_PHY_0);
3232 
3233 	rtwdev->scanning = false;
3234 	rtwdev->dig.bypass_dig = true;
3235 	if (hw_scan && (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE))
3236 		ieee80211_queue_work(rtwdev->hw, &rtwdev->ips_work);
3237 }
3238 
3239 static void rtw89_read_chip_ver(struct rtw89_dev *rtwdev)
3240 {
3241 	const struct rtw89_chip_info *chip = rtwdev->chip;
3242 	u8 cv;
3243 
3244 	cv = rtw89_read32_mask(rtwdev, R_AX_SYS_CFG1, B_AX_CHIP_VER_MASK);
3245 	if (chip->chip_id == RTL8852A && cv <= CHIP_CBV) {
3246 		if (rtw89_read32(rtwdev, R_AX_GPIO0_7_FUNC_SEL) == RTW89_R32_DEAD)
3247 			cv = CHIP_CAV;
3248 		else
3249 			cv = CHIP_CBV;
3250 	}
3251 
3252 	rtwdev->hal.cv = cv;
3253 }
3254 
3255 static void rtw89_core_setup_phycap(struct rtw89_dev *rtwdev)
3256 {
3257 	rtwdev->hal.support_cckpd =
3258 		!(rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv <= CHIP_CBV) &&
3259 		!(rtwdev->chip->chip_id == RTL8852B && rtwdev->hal.cv <= CHIP_CAV);
3260 	rtwdev->hal.support_igi =
3261 		rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv <= CHIP_CBV;
3262 }
3263 
3264 static int rtw89_chip_efuse_info_setup(struct rtw89_dev *rtwdev)
3265 {
3266 	int ret;
3267 
3268 	ret = rtw89_mac_partial_init(rtwdev);
3269 	if (ret)
3270 		return ret;
3271 
3272 	ret = rtw89_parse_efuse_map(rtwdev);
3273 	if (ret)
3274 		return ret;
3275 
3276 	ret = rtw89_parse_phycap_map(rtwdev);
3277 	if (ret)
3278 		return ret;
3279 
3280 	ret = rtw89_mac_setup_phycap(rtwdev);
3281 	if (ret)
3282 		return ret;
3283 
3284 	rtw89_core_setup_phycap(rtwdev);
3285 
3286 	rtw89_mac_pwr_off(rtwdev);
3287 
3288 	return 0;
3289 }
3290 
3291 static int rtw89_chip_board_info_setup(struct rtw89_dev *rtwdev)
3292 {
3293 	rtw89_chip_fem_setup(rtwdev);
3294 
3295 	return 0;
3296 }
3297 
3298 int rtw89_chip_info_setup(struct rtw89_dev *rtwdev)
3299 {
3300 	int ret;
3301 
3302 	rtw89_read_chip_ver(rtwdev);
3303 
3304 	ret = rtw89_wait_firmware_completion(rtwdev);
3305 	if (ret) {
3306 		rtw89_err(rtwdev, "failed to wait firmware completion\n");
3307 		return ret;
3308 	}
3309 
3310 	ret = rtw89_fw_recognize(rtwdev);
3311 	if (ret) {
3312 		rtw89_err(rtwdev, "failed to recognize firmware\n");
3313 		return ret;
3314 	}
3315 
3316 	ret = rtw89_chip_efuse_info_setup(rtwdev);
3317 	if (ret)
3318 		return ret;
3319 
3320 	ret = rtw89_chip_board_info_setup(rtwdev);
3321 	if (ret)
3322 		return ret;
3323 
3324 	rtwdev->ps_mode = rtw89_update_ps_mode(rtwdev);
3325 
3326 	return 0;
3327 }
3328 EXPORT_SYMBOL(rtw89_chip_info_setup);
3329 
3330 static int rtw89_core_register_hw(struct rtw89_dev *rtwdev)
3331 {
3332 	struct ieee80211_hw *hw = rtwdev->hw;
3333 	struct rtw89_efuse *efuse = &rtwdev->efuse;
3334 	int ret;
3335 	int tx_headroom = IEEE80211_HT_CTL_LEN;
3336 
3337 	hw->vif_data_size = sizeof(struct rtw89_vif);
3338 	hw->sta_data_size = sizeof(struct rtw89_sta);
3339 	hw->txq_data_size = sizeof(struct rtw89_txq);
3340 	hw->chanctx_data_size = sizeof(struct rtw89_chanctx_cfg);
3341 
3342 	SET_IEEE80211_PERM_ADDR(hw, efuse->addr);
3343 
3344 	hw->extra_tx_headroom = tx_headroom;
3345 	hw->queues = IEEE80211_NUM_ACS;
3346 	hw->max_rx_aggregation_subframes = RTW89_MAX_RX_AGG_NUM;
3347 	hw->max_tx_aggregation_subframes = RTW89_MAX_TX_AGG_NUM;
3348 	hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
3349 
3350 	ieee80211_hw_set(hw, SIGNAL_DBM);
3351 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
3352 	ieee80211_hw_set(hw, MFP_CAPABLE);
3353 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
3354 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3355 	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
3356 	ieee80211_hw_set(hw, TX_AMSDU);
3357 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
3358 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
3359 	ieee80211_hw_set(hw, SUPPORTS_PS);
3360 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
3361 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
3362 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3363 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
3364 
3365 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
3366 				     BIT(NL80211_IFTYPE_AP) |
3367 				     BIT(NL80211_IFTYPE_P2P_CLIENT) |
3368 				     BIT(NL80211_IFTYPE_P2P_GO);
3369 
3370 	hw->wiphy->available_antennas_tx = BIT(rtwdev->chip->rf_path_num) - 1;
3371 	hw->wiphy->available_antennas_rx = BIT(rtwdev->chip->rf_path_num) - 1;
3372 
3373 	hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3374 			    WIPHY_FLAG_TDLS_EXTERNAL_SETUP |
3375 			    WIPHY_FLAG_AP_UAPSD;
3376 	hw->wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3377 
3378 	hw->wiphy->max_scan_ssids = RTW89_SCANOFLD_MAX_SSID;
3379 	hw->wiphy->max_scan_ie_len = RTW89_SCANOFLD_MAX_IE_LEN;
3380 
3381 #ifdef CONFIG_PM
3382 	hw->wiphy->wowlan = rtwdev->chip->wowlan_stub;
3383 #endif
3384 
3385 	hw->wiphy->tid_config_support.vif |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
3386 	hw->wiphy->tid_config_support.peer |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
3387 	hw->wiphy->tid_config_support.vif |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
3388 	hw->wiphy->tid_config_support.peer |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
3389 
3390 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
3391 
3392 	ret = rtw89_core_set_supported_band(rtwdev);
3393 	if (ret) {
3394 		rtw89_err(rtwdev, "failed to set supported band\n");
3395 		return ret;
3396 	}
3397 
3398 	hw->wiphy->reg_notifier = rtw89_regd_notifier;
3399 	hw->wiphy->sar_capa = &rtw89_sar_capa;
3400 
3401 	ret = ieee80211_register_hw(hw);
3402 	if (ret) {
3403 		rtw89_err(rtwdev, "failed to register hw\n");
3404 		goto err;
3405 	}
3406 
3407 	ret = rtw89_regd_init(rtwdev, rtw89_regd_notifier);
3408 	if (ret) {
3409 		rtw89_err(rtwdev, "failed to init regd\n");
3410 		goto err;
3411 	}
3412 
3413 	return 0;
3414 
3415 err:
3416 	return ret;
3417 }
3418 
3419 static void rtw89_core_unregister_hw(struct rtw89_dev *rtwdev)
3420 {
3421 	struct ieee80211_hw *hw = rtwdev->hw;
3422 
3423 	ieee80211_unregister_hw(hw);
3424 	rtw89_core_clr_supported_band(rtwdev);
3425 }
3426 
3427 int rtw89_core_register(struct rtw89_dev *rtwdev)
3428 {
3429 	int ret;
3430 
3431 	ret = rtw89_core_register_hw(rtwdev);
3432 	if (ret) {
3433 		rtw89_err(rtwdev, "failed to register core hw\n");
3434 		return ret;
3435 	}
3436 
3437 	rtw89_debugfs_init(rtwdev);
3438 
3439 	return 0;
3440 }
3441 EXPORT_SYMBOL(rtw89_core_register);
3442 
3443 void rtw89_core_unregister(struct rtw89_dev *rtwdev)
3444 {
3445 	rtw89_core_unregister_hw(rtwdev);
3446 }
3447 EXPORT_SYMBOL(rtw89_core_unregister);
3448 
3449 struct rtw89_dev *rtw89_alloc_ieee80211_hw(struct device *device,
3450 					   u32 bus_data_size,
3451 					   const struct rtw89_chip_info *chip)
3452 {
3453 	const struct firmware *firmware;
3454 	struct ieee80211_hw *hw;
3455 	struct rtw89_dev *rtwdev;
3456 	struct ieee80211_ops *ops;
3457 	u32 driver_data_size;
3458 	u32 early_feat_map = 0;
3459 	bool no_chanctx;
3460 
3461 	firmware = rtw89_early_fw_feature_recognize(device, chip, &early_feat_map);
3462 
3463 	ops = kmemdup(&rtw89_ops, sizeof(rtw89_ops), GFP_KERNEL);
3464 	if (!ops)
3465 		goto err;
3466 
3467 	no_chanctx = chip->support_chanctx_num == 0 ||
3468 		     !(early_feat_map & BIT(RTW89_FW_FEATURE_SCAN_OFFLOAD));
3469 
3470 	if (no_chanctx) {
3471 		ops->add_chanctx = NULL;
3472 		ops->remove_chanctx = NULL;
3473 		ops->change_chanctx = NULL;
3474 		ops->assign_vif_chanctx = NULL;
3475 		ops->unassign_vif_chanctx = NULL;
3476 	}
3477 
3478 	driver_data_size = sizeof(struct rtw89_dev) + bus_data_size;
3479 	hw = ieee80211_alloc_hw(driver_data_size, ops);
3480 	if (!hw)
3481 		goto err;
3482 
3483 	rtwdev = hw->priv;
3484 	rtwdev->hw = hw;
3485 	rtwdev->dev = device;
3486 	rtwdev->ops = ops;
3487 	rtwdev->chip = chip;
3488 	rtwdev->fw.firmware = firmware;
3489 
3490 	rtw89_debug(rtwdev, RTW89_DBG_FW, "probe driver %s chanctx\n",
3491 		    no_chanctx ? "without" : "with");
3492 
3493 	return rtwdev;
3494 
3495 err:
3496 	kfree(ops);
3497 	release_firmware(firmware);
3498 	return NULL;
3499 }
3500 EXPORT_SYMBOL(rtw89_alloc_ieee80211_hw);
3501 
3502 void rtw89_free_ieee80211_hw(struct rtw89_dev *rtwdev)
3503 {
3504 	kfree(rtwdev->ops);
3505 	release_firmware(rtwdev->fw.firmware);
3506 	ieee80211_free_hw(rtwdev->hw);
3507 }
3508 EXPORT_SYMBOL(rtw89_free_ieee80211_hw);
3509 
3510 MODULE_AUTHOR("Realtek Corporation");
3511 MODULE_DESCRIPTION("Realtek 802.11ax wireless core module");
3512 MODULE_LICENSE("Dual BSD/GPL");
3513