1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2019-2020 Realtek Corporation
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");
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)
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),
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 RTW89_DEF_CHAN_5G(5845, 169),
81 RTW89_DEF_CHAN_5G(5865, 173),
82 RTW89_DEF_CHAN_5G(5885, 177),
85 static struct ieee80211_channel rtw89_channels_6ghz[] = {
86 RTW89_DEF_CHAN_6G(5955, 1),
87 RTW89_DEF_CHAN_6G(5975, 5),
88 RTW89_DEF_CHAN_6G(5995, 9),
89 RTW89_DEF_CHAN_6G(6015, 13),
90 RTW89_DEF_CHAN_6G(6035, 17),
91 RTW89_DEF_CHAN_6G(6055, 21),
92 RTW89_DEF_CHAN_6G(6075, 25),
93 RTW89_DEF_CHAN_6G(6095, 29),
94 RTW89_DEF_CHAN_6G(6115, 33),
95 RTW89_DEF_CHAN_6G(6135, 37),
96 RTW89_DEF_CHAN_6G(6155, 41),
97 RTW89_DEF_CHAN_6G(6175, 45),
98 RTW89_DEF_CHAN_6G(6195, 49),
99 RTW89_DEF_CHAN_6G(6215, 53),
100 RTW89_DEF_CHAN_6G(6235, 57),
101 RTW89_DEF_CHAN_6G(6255, 61),
102 RTW89_DEF_CHAN_6G(6275, 65),
103 RTW89_DEF_CHAN_6G(6295, 69),
104 RTW89_DEF_CHAN_6G(6315, 73),
105 RTW89_DEF_CHAN_6G(6335, 77),
106 RTW89_DEF_CHAN_6G(6355, 81),
107 RTW89_DEF_CHAN_6G(6375, 85),
108 RTW89_DEF_CHAN_6G(6395, 89),
109 RTW89_DEF_CHAN_6G(6415, 93),
110 RTW89_DEF_CHAN_6G(6435, 97),
111 RTW89_DEF_CHAN_6G(6455, 101),
112 RTW89_DEF_CHAN_6G(6475, 105),
113 RTW89_DEF_CHAN_6G(6495, 109),
114 RTW89_DEF_CHAN_6G(6515, 113),
115 RTW89_DEF_CHAN_6G(6535, 117),
116 RTW89_DEF_CHAN_6G(6555, 121),
117 RTW89_DEF_CHAN_6G(6575, 125),
118 RTW89_DEF_CHAN_6G(6595, 129),
119 RTW89_DEF_CHAN_6G(6615, 133),
120 RTW89_DEF_CHAN_6G(6635, 137),
121 RTW89_DEF_CHAN_6G(6655, 141),
122 RTW89_DEF_CHAN_6G(6675, 145),
123 RTW89_DEF_CHAN_6G(6695, 149),
124 RTW89_DEF_CHAN_6G(6715, 153),
125 RTW89_DEF_CHAN_6G(6735, 157),
126 RTW89_DEF_CHAN_6G(6755, 161),
127 RTW89_DEF_CHAN_6G(6775, 165),
128 RTW89_DEF_CHAN_6G(6795, 169),
129 RTW89_DEF_CHAN_6G(6815, 173),
130 RTW89_DEF_CHAN_6G(6835, 177),
131 RTW89_DEF_CHAN_6G(6855, 181),
132 RTW89_DEF_CHAN_6G(6875, 185),
133 RTW89_DEF_CHAN_6G(6895, 189),
134 RTW89_DEF_CHAN_6G(6915, 193),
135 RTW89_DEF_CHAN_6G(6935, 197),
136 RTW89_DEF_CHAN_6G(6955, 201),
137 RTW89_DEF_CHAN_6G(6975, 205),
138 RTW89_DEF_CHAN_6G(6995, 209),
139 RTW89_DEF_CHAN_6G(7015, 213),
140 RTW89_DEF_CHAN_6G(7035, 217),
141 RTW89_DEF_CHAN_6G(7055, 221),
142 RTW89_DEF_CHAN_6G(7075, 225),
143 RTW89_DEF_CHAN_6G(7095, 229),
144 RTW89_DEF_CHAN_6G(7115, 233),
147 static struct ieee80211_rate rtw89_bitrates[] = {
148 { .bitrate = 10, .hw_value = 0x00, },
149 { .bitrate = 20, .hw_value = 0x01, },
150 { .bitrate = 55, .hw_value = 0x02, },
151 { .bitrate = 110, .hw_value = 0x03, },
152 { .bitrate = 60, .hw_value = 0x04, },
153 { .bitrate = 90, .hw_value = 0x05, },
154 { .bitrate = 120, .hw_value = 0x06, },
155 { .bitrate = 180, .hw_value = 0x07, },
156 { .bitrate = 240, .hw_value = 0x08, },
157 { .bitrate = 360, .hw_value = 0x09, },
158 { .bitrate = 480, .hw_value = 0x0a, },
159 { .bitrate = 540, .hw_value = 0x0b, },
162 static const struct ieee80211_iface_limit rtw89_iface_limits[] = {
165 .types = BIT(NL80211_IFTYPE_STATION),
169 .types = BIT(NL80211_IFTYPE_P2P_CLIENT) |
170 BIT(NL80211_IFTYPE_P2P_GO) |
171 BIT(NL80211_IFTYPE_AP),
175 static const struct ieee80211_iface_combination rtw89_iface_combs[] = {
177 .limits = rtw89_iface_limits,
178 .n_limits = ARRAY_SIZE(rtw89_iface_limits),
180 .num_different_channels = 1,
184 bool rtw89_ra_report_to_bitrate(struct rtw89_dev *rtwdev, u8 rpt_rate, u16 *bitrate)
186 struct ieee80211_rate rate;
188 if (unlikely(rpt_rate >= ARRAY_SIZE(rtw89_bitrates))) {
189 rtw89_debug(rtwdev, RTW89_DBG_UNEXP, "invalid rpt rate %d\n", rpt_rate);
193 rate = rtw89_bitrates[rpt_rate];
194 *bitrate = rate.bitrate;
199 static const struct ieee80211_supported_band rtw89_sband_2ghz = {
200 .band = NL80211_BAND_2GHZ,
201 .channels = rtw89_channels_2ghz,
202 .n_channels = ARRAY_SIZE(rtw89_channels_2ghz),
203 .bitrates = rtw89_bitrates,
204 .n_bitrates = ARRAY_SIZE(rtw89_bitrates),
209 static const struct ieee80211_supported_band rtw89_sband_5ghz = {
210 .band = NL80211_BAND_5GHZ,
211 .channels = rtw89_channels_5ghz,
212 .n_channels = ARRAY_SIZE(rtw89_channels_5ghz),
214 /* 5G has no CCK rates, 1M/2M/5.5M/11M */
215 .bitrates = rtw89_bitrates + 4,
216 .n_bitrates = ARRAY_SIZE(rtw89_bitrates) - 4,
221 static const struct ieee80211_supported_band rtw89_sband_6ghz = {
222 .band = NL80211_BAND_6GHZ,
223 .channels = rtw89_channels_6ghz,
224 .n_channels = ARRAY_SIZE(rtw89_channels_6ghz),
226 /* 6G has no CCK rates, 1M/2M/5.5M/11M */
227 .bitrates = rtw89_bitrates + 4,
228 .n_bitrates = ARRAY_SIZE(rtw89_bitrates) - 4,
231 static void rtw89_traffic_stats_accu(struct rtw89_dev *rtwdev,
232 struct rtw89_traffic_stats *stats,
233 struct sk_buff *skb, bool tx)
235 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
237 if (!ieee80211_is_data(hdr->frame_control))
240 if (is_broadcast_ether_addr(hdr->addr1) ||
241 is_multicast_ether_addr(hdr->addr1))
246 stats->tx_unicast += skb->len;
249 stats->rx_unicast += skb->len;
253 void rtw89_get_default_chandef(struct cfg80211_chan_def *chandef)
255 cfg80211_chandef_create(chandef, &rtw89_channels_2ghz[0],
259 void rtw89_get_channel_params(const struct cfg80211_chan_def *chandef,
260 struct rtw89_chan *chan)
262 struct ieee80211_channel *channel = chandef->chan;
263 enum nl80211_chan_width width = chandef->width;
264 u32 primary_freq, center_freq;
266 u8 bandwidth = RTW89_CHANNEL_WIDTH_20;
270 center_chan = channel->hw_value;
271 primary_freq = channel->center_freq;
272 center_freq = chandef->center_freq1;
275 case NL80211_CHAN_WIDTH_20_NOHT:
276 case NL80211_CHAN_WIDTH_20:
277 bandwidth = RTW89_CHANNEL_WIDTH_20;
279 case NL80211_CHAN_WIDTH_40:
280 bandwidth = RTW89_CHANNEL_WIDTH_40;
281 if (primary_freq > center_freq) {
287 case NL80211_CHAN_WIDTH_80:
288 case NL80211_CHAN_WIDTH_160:
289 bandwidth = nl_to_rtw89_bandwidth(width);
290 if (primary_freq > center_freq) {
291 offset = (primary_freq - center_freq - 10) / 20;
292 center_chan -= 2 + offset * 4;
294 offset = (center_freq - primary_freq - 10) / 20;
295 center_chan += 2 + offset * 4;
303 switch (channel->band) {
305 case NL80211_BAND_2GHZ:
306 band = RTW89_BAND_2G;
308 case NL80211_BAND_5GHZ:
309 band = RTW89_BAND_5G;
311 case NL80211_BAND_6GHZ:
312 band = RTW89_BAND_6G;
316 rtw89_chan_create(chan, center_chan, channel->hw_value, band, bandwidth);
319 void rtw89_core_set_chip_txpwr(struct rtw89_dev *rtwdev)
321 struct rtw89_hal *hal = &rtwdev->hal;
322 const struct rtw89_chip_info *chip = rtwdev->chip;
323 const struct rtw89_chan *chan;
324 enum rtw89_sub_entity_idx sub_entity_idx;
325 enum rtw89_sub_entity_idx roc_idx;
326 enum rtw89_phy_idx phy_idx;
327 enum rtw89_entity_mode mode;
330 entity_active = rtw89_get_entity_state(rtwdev);
334 mode = rtw89_get_entity_mode(rtwdev);
336 case RTW89_ENTITY_MODE_SCC:
337 case RTW89_ENTITY_MODE_MCC:
338 sub_entity_idx = RTW89_SUB_ENTITY_0;
340 case RTW89_ENTITY_MODE_MCC_PREPARE:
341 sub_entity_idx = RTW89_SUB_ENTITY_1;
344 WARN(1, "Invalid ent mode: %d\n", mode);
348 roc_idx = atomic_read(&hal->roc_entity_idx);
349 if (roc_idx != RTW89_SUB_ENTITY_IDLE)
350 sub_entity_idx = roc_idx;
352 phy_idx = RTW89_PHY_0;
353 chan = rtw89_chan_get(rtwdev, sub_entity_idx);
354 chip->ops->set_txpwr(rtwdev, chan, phy_idx);
357 void rtw89_set_channel(struct rtw89_dev *rtwdev)
359 struct rtw89_hal *hal = &rtwdev->hal;
360 const struct rtw89_chip_info *chip = rtwdev->chip;
361 const struct rtw89_chan_rcd *chan_rcd;
362 const struct rtw89_chan *chan;
363 enum rtw89_sub_entity_idx sub_entity_idx;
364 enum rtw89_sub_entity_idx roc_idx;
365 enum rtw89_mac_idx mac_idx;
366 enum rtw89_phy_idx phy_idx;
367 struct rtw89_channel_help_params bak;
368 enum rtw89_entity_mode mode;
371 entity_active = rtw89_get_entity_state(rtwdev);
373 mode = rtw89_entity_recalc(rtwdev);
375 case RTW89_ENTITY_MODE_SCC:
376 case RTW89_ENTITY_MODE_MCC:
377 sub_entity_idx = RTW89_SUB_ENTITY_0;
379 case RTW89_ENTITY_MODE_MCC_PREPARE:
380 sub_entity_idx = RTW89_SUB_ENTITY_1;
383 WARN(1, "Invalid ent mode: %d\n", mode);
387 roc_idx = atomic_read(&hal->roc_entity_idx);
388 if (roc_idx != RTW89_SUB_ENTITY_IDLE)
389 sub_entity_idx = roc_idx;
391 mac_idx = RTW89_MAC_0;
392 phy_idx = RTW89_PHY_0;
394 chan = rtw89_chan_get(rtwdev, sub_entity_idx);
395 chan_rcd = rtw89_chan_rcd_get(rtwdev, sub_entity_idx);
397 rtw89_chip_set_channel_prepare(rtwdev, &bak, chan, mac_idx, phy_idx);
399 chip->ops->set_channel(rtwdev, chan, mac_idx, phy_idx);
401 chip->ops->set_txpwr(rtwdev, chan, phy_idx);
403 rtw89_chip_set_channel_done(rtwdev, &bak, chan, mac_idx, phy_idx);
405 if (!entity_active || chan_rcd->band_changed) {
406 rtw89_btc_ntfy_switch_band(rtwdev, phy_idx, chan->band_type);
407 rtw89_chip_rfk_band_changed(rtwdev, phy_idx);
410 rtw89_set_entity_state(rtwdev, true);
413 void rtw89_get_channel(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
414 struct rtw89_chan *chan)
416 const struct cfg80211_chan_def *chandef;
418 chandef = rtw89_chandef_get(rtwdev, rtwvif->sub_entity_idx);
419 rtw89_get_channel_params(chandef, chan);
422 static enum rtw89_core_tx_type
423 rtw89_core_get_tx_type(struct rtw89_dev *rtwdev,
426 struct ieee80211_hdr *hdr = (void *)skb->data;
427 __le16 fc = hdr->frame_control;
429 if (ieee80211_is_mgmt(fc) || ieee80211_is_nullfunc(fc))
430 return RTW89_CORE_TX_TYPE_MGMT;
432 return RTW89_CORE_TX_TYPE_DATA;
436 rtw89_core_tx_update_ampdu_info(struct rtw89_dev *rtwdev,
437 struct rtw89_core_tx_request *tx_req,
438 enum btc_pkt_type pkt_type)
440 struct ieee80211_sta *sta = tx_req->sta;
441 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
442 struct sk_buff *skb = tx_req->skb;
443 struct rtw89_sta *rtwsta;
447 if (pkt_type == PACKET_EAPOL) {
448 desc_info->bk = true;
452 if (!(IEEE80211_SKB_CB(skb)->flags & IEEE80211_TX_CTL_AMPDU))
456 rtw89_warn(rtwdev, "cannot set ampdu info without sta\n");
460 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
461 rtwsta = (struct rtw89_sta *)sta->drv_priv;
463 ampdu_num = (u8)((rtwsta->ampdu_params[tid].agg_num ?
464 rtwsta->ampdu_params[tid].agg_num :
465 4 << sta->deflink.ht_cap.ampdu_factor) - 1);
467 desc_info->agg_en = true;
468 desc_info->ampdu_density = sta->deflink.ht_cap.ampdu_density;
469 desc_info->ampdu_num = ampdu_num;
473 rtw89_core_tx_update_sec_key(struct rtw89_dev *rtwdev,
474 struct rtw89_core_tx_request *tx_req)
476 const struct rtw89_chip_info *chip = rtwdev->chip;
477 struct ieee80211_vif *vif = tx_req->vif;
478 struct ieee80211_sta *sta = tx_req->sta;
479 struct ieee80211_tx_info *info;
480 struct ieee80211_key_conf *key;
481 struct rtw89_vif *rtwvif;
482 struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
483 struct rtw89_addr_cam_entry *addr_cam;
484 struct rtw89_sec_cam_entry *sec_cam;
485 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
486 struct sk_buff *skb = tx_req->skb;
487 u8 sec_type = RTW89_SEC_KEY_TYPE_NONE;
491 rtw89_warn(rtwdev, "cannot set sec key without vif\n");
495 rtwvif = (struct rtw89_vif *)vif->drv_priv;
496 addr_cam = rtw89_get_addr_cam_of(rtwvif, rtwsta);
498 info = IEEE80211_SKB_CB(skb);
499 key = info->control.hw_key;
500 sec_cam = addr_cam->sec_entries[key->hw_key_idx];
502 rtw89_warn(rtwdev, "sec cam entry is empty\n");
506 switch (key->cipher) {
507 case WLAN_CIPHER_SUITE_WEP40:
508 sec_type = RTW89_SEC_KEY_TYPE_WEP40;
510 case WLAN_CIPHER_SUITE_WEP104:
511 sec_type = RTW89_SEC_KEY_TYPE_WEP104;
513 case WLAN_CIPHER_SUITE_TKIP:
514 sec_type = RTW89_SEC_KEY_TYPE_TKIP;
516 case WLAN_CIPHER_SUITE_CCMP:
517 sec_type = RTW89_SEC_KEY_TYPE_CCMP128;
519 case WLAN_CIPHER_SUITE_CCMP_256:
520 sec_type = RTW89_SEC_KEY_TYPE_CCMP256;
522 case WLAN_CIPHER_SUITE_GCMP:
523 sec_type = RTW89_SEC_KEY_TYPE_GCMP128;
525 case WLAN_CIPHER_SUITE_GCMP_256:
526 sec_type = RTW89_SEC_KEY_TYPE_GCMP256;
529 rtw89_warn(rtwdev, "key cipher not supported %d\n", key->cipher);
533 desc_info->sec_en = true;
534 desc_info->sec_keyid = key->keyidx;
535 desc_info->sec_type = sec_type;
536 desc_info->sec_cam_idx = sec_cam->sec_cam_idx;
538 if (!chip->hw_sec_hdr)
541 pn64 = atomic64_inc_return(&key->tx_pn);
542 desc_info->sec_seq[0] = pn64;
543 desc_info->sec_seq[1] = pn64 >> 8;
544 desc_info->sec_seq[2] = pn64 >> 16;
545 desc_info->sec_seq[3] = pn64 >> 24;
546 desc_info->sec_seq[4] = pn64 >> 32;
547 desc_info->sec_seq[5] = pn64 >> 40;
548 desc_info->wp_offset = 1; /* in unit of 8 bytes for security header */
551 static u16 rtw89_core_get_mgmt_rate(struct rtw89_dev *rtwdev,
552 struct rtw89_core_tx_request *tx_req,
553 const struct rtw89_chan *chan)
555 struct sk_buff *skb = tx_req->skb;
556 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
557 struct ieee80211_vif *vif = tx_info->control.vif;
560 if (tx_info->flags & IEEE80211_TX_CTL_NO_CCK_RATE ||
562 lowest_rate = RTW89_HW_RATE_OFDM6;
563 else if (chan->band_type == RTW89_BAND_2G)
564 lowest_rate = RTW89_HW_RATE_CCK1;
566 lowest_rate = RTW89_HW_RATE_OFDM6;
568 if (!vif || !vif->bss_conf.basic_rates || !tx_req->sta)
571 return __ffs(vif->bss_conf.basic_rates) + lowest_rate;
574 static u8 rtw89_core_tx_get_mac_id(struct rtw89_dev *rtwdev,
575 struct rtw89_core_tx_request *tx_req)
577 struct ieee80211_vif *vif = tx_req->vif;
578 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
579 struct ieee80211_sta *sta = tx_req->sta;
580 struct rtw89_sta *rtwsta;
583 return rtwvif->mac_id;
585 rtwsta = (struct rtw89_sta *)sta->drv_priv;
586 return rtwsta->mac_id;
590 rtw89_core_tx_update_mgmt_info(struct rtw89_dev *rtwdev,
591 struct rtw89_core_tx_request *tx_req)
593 struct ieee80211_vif *vif = tx_req->vif;
594 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
595 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
596 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
597 rtwvif->sub_entity_idx);
600 qsel = desc_info->hiq ? RTW89_TX_QSEL_B0_HI : RTW89_TX_QSEL_B0_MGMT;
601 ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
603 desc_info->qsel = qsel;
604 desc_info->ch_dma = ch_dma;
605 desc_info->port = desc_info->hiq ? rtwvif->port : 0;
606 desc_info->mac_id = rtw89_core_tx_get_mac_id(rtwdev, tx_req);
607 desc_info->hw_ssn_sel = RTW89_MGMT_HW_SSN_SEL;
608 desc_info->hw_seq_mode = RTW89_MGMT_HW_SEQ_MODE;
610 /* fixed data rate for mgmt frames */
611 desc_info->en_wd_info = true;
612 desc_info->use_rate = true;
613 desc_info->dis_data_fb = true;
614 desc_info->data_rate = rtw89_core_get_mgmt_rate(rtwdev, tx_req, chan);
616 rtw89_debug(rtwdev, RTW89_DBG_TXRX,
617 "tx mgmt frame with rate 0x%x on channel %d (band %d, bw %d)\n",
618 desc_info->data_rate, chan->channel, chan->band_type,
623 rtw89_core_tx_update_h2c_info(struct rtw89_dev *rtwdev,
624 struct rtw89_core_tx_request *tx_req)
626 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
628 desc_info->is_bmc = false;
629 desc_info->wd_page = false;
630 desc_info->ch_dma = RTW89_DMA_H2C;
633 static void rtw89_core_get_no_ul_ofdma_htc(struct rtw89_dev *rtwdev, __le32 *htc,
634 const struct rtw89_chan *chan)
636 static const u8 rtw89_bandwidth_to_om[] = {
637 [RTW89_CHANNEL_WIDTH_20] = HTC_OM_CHANNEL_WIDTH_20,
638 [RTW89_CHANNEL_WIDTH_40] = HTC_OM_CHANNEL_WIDTH_40,
639 [RTW89_CHANNEL_WIDTH_80] = HTC_OM_CHANNEL_WIDTH_80,
640 [RTW89_CHANNEL_WIDTH_160] = HTC_OM_CHANNEL_WIDTH_160_OR_80_80,
641 [RTW89_CHANNEL_WIDTH_80_80] = HTC_OM_CHANNEL_WIDTH_160_OR_80_80,
643 const struct rtw89_chip_info *chip = rtwdev->chip;
644 struct rtw89_hal *hal = &rtwdev->hal;
647 if (!chip->dis_2g_40m_ul_ofdma ||
648 chan->band_type != RTW89_BAND_2G ||
649 chan->band_width != RTW89_CHANNEL_WIDTH_40)
652 om_bandwidth = chan->band_width < ARRAY_SIZE(rtw89_bandwidth_to_om) ?
653 rtw89_bandwidth_to_om[chan->band_width] : 0;
654 *htc = le32_encode_bits(RTW89_HTC_VARIANT_HE, RTW89_HTC_MASK_VARIANT) |
655 le32_encode_bits(RTW89_HTC_VARIANT_HE_CID_OM, RTW89_HTC_MASK_CTL_ID) |
656 le32_encode_bits(hal->rx_nss - 1, RTW89_HTC_MASK_HTC_OM_RX_NSS) |
657 le32_encode_bits(om_bandwidth, RTW89_HTC_MASK_HTC_OM_CH_WIDTH) |
658 le32_encode_bits(1, RTW89_HTC_MASK_HTC_OM_UL_MU_DIS) |
659 le32_encode_bits(hal->tx_nss - 1, RTW89_HTC_MASK_HTC_OM_TX_NSTS) |
660 le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_ER_SU_DIS) |
661 le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_DL_MU_MIMO_RR) |
662 le32_encode_bits(0, RTW89_HTC_MASK_HTC_OM_UL_MU_DATA_DIS);
666 __rtw89_core_tx_check_he_qos_htc(struct rtw89_dev *rtwdev,
667 struct rtw89_core_tx_request *tx_req,
668 enum btc_pkt_type pkt_type)
670 struct ieee80211_sta *sta = tx_req->sta;
671 struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
672 struct sk_buff *skb = tx_req->skb;
673 struct ieee80211_hdr *hdr = (void *)skb->data;
674 __le16 fc = hdr->frame_control;
676 /* AP IOT issue with EAPoL, ARP and DHCP */
677 if (pkt_type < PACKET_MAX)
680 if (!sta || !sta->deflink.he_cap.has_he)
683 if (!ieee80211_is_data_qos(fc))
686 if (skb_headroom(skb) < IEEE80211_HT_CTL_LEN)
689 if (rtwsta && rtwsta->ra_report.might_fallback_legacy)
696 __rtw89_core_tx_adjust_he_qos_htc(struct rtw89_dev *rtwdev,
697 struct rtw89_core_tx_request *tx_req)
699 struct ieee80211_sta *sta = tx_req->sta;
700 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
701 struct sk_buff *skb = tx_req->skb;
702 struct ieee80211_hdr *hdr = (void *)skb->data;
703 __le16 fc = hdr->frame_control;
709 hdr_len = ieee80211_has_a4(fc) ? 32 : 26;
710 data = skb_push(skb, IEEE80211_HT_CTL_LEN);
711 memmove(data, data + IEEE80211_HT_CTL_LEN, hdr_len);
714 htc = data + hdr_len;
715 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_ORDER);
716 *htc = rtwsta->htc_template ? rtwsta->htc_template :
717 le32_encode_bits(RTW89_HTC_VARIANT_HE, RTW89_HTC_MASK_VARIANT) |
718 le32_encode_bits(RTW89_HTC_VARIANT_HE_CID_CAS, RTW89_HTC_MASK_CTL_ID);
720 qc = data + hdr_len - IEEE80211_QOS_CTL_LEN;
721 qc[0] |= IEEE80211_QOS_CTL_EOSP;
725 rtw89_core_tx_update_he_qos_htc(struct rtw89_dev *rtwdev,
726 struct rtw89_core_tx_request *tx_req,
727 enum btc_pkt_type pkt_type)
729 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
730 struct ieee80211_vif *vif = tx_req->vif;
731 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
733 if (!__rtw89_core_tx_check_he_qos_htc(rtwdev, tx_req, pkt_type))
736 __rtw89_core_tx_adjust_he_qos_htc(rtwdev, tx_req);
738 desc_info->pkt_size += IEEE80211_HT_CTL_LEN;
739 desc_info->a_ctrl_bsr = true;
742 if (!rtwvif || rtwvif->last_a_ctrl == desc_info->a_ctrl_bsr)
745 rtwvif->last_a_ctrl = desc_info->a_ctrl_bsr;
746 desc_info->bk = true;
749 static u16 rtw89_core_get_data_rate(struct rtw89_dev *rtwdev,
750 struct rtw89_core_tx_request *tx_req)
752 struct ieee80211_vif *vif = tx_req->vif;
753 struct ieee80211_sta *sta = tx_req->sta;
754 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
755 struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern;
756 enum rtw89_sub_entity_idx idx = rtwvif->sub_entity_idx;
757 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, idx);
760 if (rate_pattern->enable)
761 return rate_pattern->rate;
764 lowest_rate = RTW89_HW_RATE_OFDM6;
765 else if (chan->band_type == RTW89_BAND_2G)
766 lowest_rate = RTW89_HW_RATE_CCK1;
768 lowest_rate = RTW89_HW_RATE_OFDM6;
770 if (!sta || !sta->deflink.supp_rates[chan->band_type])
773 return __ffs(sta->deflink.supp_rates[chan->band_type]) + lowest_rate;
777 rtw89_core_tx_update_data_info(struct rtw89_dev *rtwdev,
778 struct rtw89_core_tx_request *tx_req)
780 struct ieee80211_vif *vif = tx_req->vif;
781 struct ieee80211_sta *sta = tx_req->sta;
782 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
783 struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
784 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
785 struct sk_buff *skb = tx_req->skb;
786 u8 tid, tid_indicate;
789 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
790 tid_indicate = rtw89_core_get_tid_indicate(rtwdev, tid);
791 qsel = desc_info->hiq ? RTW89_TX_QSEL_B0_HI : rtw89_core_get_qsel(rtwdev, tid);
792 ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
794 desc_info->ch_dma = ch_dma;
795 desc_info->tid_indicate = tid_indicate;
796 desc_info->qsel = qsel;
797 desc_info->mac_id = rtw89_core_tx_get_mac_id(rtwdev, tx_req);
798 desc_info->port = desc_info->hiq ? rtwvif->port : 0;
799 desc_info->er_cap = rtwsta ? rtwsta->er_cap : false;
801 /* enable wd_info for AMPDU */
802 desc_info->en_wd_info = true;
804 if (IEEE80211_SKB_CB(skb)->control.hw_key)
805 rtw89_core_tx_update_sec_key(rtwdev, tx_req);
807 desc_info->data_retry_lowest_rate = rtw89_core_get_data_rate(rtwdev, tx_req);
810 static enum btc_pkt_type
811 rtw89_core_tx_btc_spec_pkt_notify(struct rtw89_dev *rtwdev,
812 struct rtw89_core_tx_request *tx_req)
814 struct sk_buff *skb = tx_req->skb;
815 struct udphdr *udphdr;
817 if (IEEE80211_SKB_CB(skb)->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) {
818 ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.eapol_notify_work);
822 if (skb->protocol == htons(ETH_P_ARP)) {
823 ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.arp_notify_work);
827 if (skb->protocol == htons(ETH_P_IP) &&
828 ip_hdr(skb)->protocol == IPPROTO_UDP) {
829 udphdr = udp_hdr(skb);
830 if (((udphdr->source == htons(67) && udphdr->dest == htons(68)) ||
831 (udphdr->source == htons(68) && udphdr->dest == htons(67))) &&
833 ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.dhcp_notify_work);
838 if (skb->protocol == htons(ETH_P_IP) &&
839 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
840 ieee80211_queue_work(rtwdev->hw, &rtwdev->btc.icmp_notify_work);
847 static void rtw89_core_tx_update_llc_hdr(struct rtw89_dev *rtwdev,
848 struct rtw89_tx_desc_info *desc_info,
851 struct ieee80211_hdr *hdr = (void *)skb->data;
852 __le16 fc = hdr->frame_control;
854 desc_info->hdr_llc_len = ieee80211_hdrlen(fc);
855 desc_info->hdr_llc_len >>= 1; /* in unit of 2 bytes */
859 rtw89_core_tx_wake(struct rtw89_dev *rtwdev,
860 struct rtw89_core_tx_request *tx_req)
862 const struct rtw89_chip_info *chip = rtwdev->chip;
864 if (!RTW89_CHK_FW_FEATURE(TX_WAKE, &rtwdev->fw))
867 if (!test_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags))
870 if (chip->chip_id != RTL8852C &&
871 tx_req->tx_type != RTW89_CORE_TX_TYPE_MGMT)
874 rtw89_mac_notify_wake(rtwdev);
878 rtw89_core_tx_update_desc_info(struct rtw89_dev *rtwdev,
879 struct rtw89_core_tx_request *tx_req)
881 struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
882 struct sk_buff *skb = tx_req->skb;
883 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
884 struct ieee80211_hdr *hdr = (void *)skb->data;
885 enum rtw89_core_tx_type tx_type;
886 enum btc_pkt_type pkt_type;
890 seq = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
891 if (tx_req->tx_type != RTW89_CORE_TX_TYPE_FWCMD) {
892 tx_type = rtw89_core_get_tx_type(rtwdev, skb);
893 tx_req->tx_type = tx_type;
895 is_bmc = (is_broadcast_ether_addr(hdr->addr1) ||
896 is_multicast_ether_addr(hdr->addr1));
898 desc_info->seq = seq;
899 desc_info->pkt_size = skb->len;
900 desc_info->is_bmc = is_bmc;
901 desc_info->wd_page = true;
902 desc_info->hiq = info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM;
904 switch (tx_req->tx_type) {
905 case RTW89_CORE_TX_TYPE_MGMT:
906 rtw89_core_tx_update_mgmt_info(rtwdev, tx_req);
908 case RTW89_CORE_TX_TYPE_DATA:
909 rtw89_core_tx_update_data_info(rtwdev, tx_req);
910 pkt_type = rtw89_core_tx_btc_spec_pkt_notify(rtwdev, tx_req);
911 rtw89_core_tx_update_he_qos_htc(rtwdev, tx_req, pkt_type);
912 rtw89_core_tx_update_ampdu_info(rtwdev, tx_req, pkt_type);
913 rtw89_core_tx_update_llc_hdr(rtwdev, desc_info, skb);
915 case RTW89_CORE_TX_TYPE_FWCMD:
916 rtw89_core_tx_update_h2c_info(rtwdev, tx_req);
921 void rtw89_core_tx_kick_off(struct rtw89_dev *rtwdev, u8 qsel)
925 ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
927 rtw89_hci_tx_kick_off(rtwdev, ch_dma);
930 int rtw89_core_tx_kick_off_and_wait(struct rtw89_dev *rtwdev, struct sk_buff *skb,
931 int qsel, unsigned int timeout)
933 struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
934 struct rtw89_tx_wait_info *wait;
935 unsigned long time_left;
938 wait = kzalloc(sizeof(*wait), GFP_KERNEL);
940 rtw89_core_tx_kick_off(rtwdev, qsel);
944 init_completion(&wait->completion);
945 rcu_assign_pointer(skb_data->wait, wait);
947 rtw89_core_tx_kick_off(rtwdev, qsel);
948 time_left = wait_for_completion_timeout(&wait->completion,
949 msecs_to_jiffies(timeout));
952 else if (!wait->tx_done)
955 rcu_assign_pointer(skb_data->wait, NULL);
956 kfree_rcu(wait, rcu_head);
961 int rtw89_h2c_tx(struct rtw89_dev *rtwdev,
962 struct sk_buff *skb, bool fwdl)
964 struct rtw89_core_tx_request tx_req = {0};
968 if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) {
969 rtw89_debug(rtwdev, RTW89_DBG_FW,
970 "ignore h2c due to power is off with firmware state=%d\n",
971 test_bit(RTW89_FLAG_FW_RDY, rtwdev->flags));
977 tx_req.tx_type = RTW89_CORE_TX_TYPE_FWCMD;
979 tx_req.desc_info.fw_dl = true;
981 rtw89_core_tx_update_desc_info(rtwdev, &tx_req);
984 rtw89_hex_dump(rtwdev, RTW89_DBG_FW, "H2C: ", skb->data, skb->len);
986 cnt = rtw89_hci_check_and_reclaim_tx_resource(rtwdev, RTW89_TXCH_CH12);
988 rtw89_err(rtwdev, "no tx fwcmd resource\n");
992 ret = rtw89_hci_tx_write(rtwdev, &tx_req);
994 rtw89_err(rtwdev, "failed to transmit skb to HCI\n");
997 rtw89_hci_tx_kick_off(rtwdev, RTW89_TXCH_CH12);
1002 int rtw89_core_tx_write(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif,
1003 struct ieee80211_sta *sta, struct sk_buff *skb, int *qsel)
1005 struct rtw89_core_tx_request tx_req = {0};
1006 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
1013 rtw89_traffic_stats_accu(rtwdev, &rtwdev->stats, skb, true);
1014 rtw89_traffic_stats_accu(rtwdev, &rtwvif->stats, skb, true);
1015 rtw89_core_tx_update_desc_info(rtwdev, &tx_req);
1016 rtw89_core_tx_wake(rtwdev, &tx_req);
1018 ret = rtw89_hci_tx_write(rtwdev, &tx_req);
1020 rtw89_err(rtwdev, "failed to transmit skb to HCI\n");
1025 *qsel = tx_req.desc_info.qsel;
1030 static __le32 rtw89_build_txwd_body0(struct rtw89_tx_desc_info *desc_info)
1032 u32 dword = FIELD_PREP(RTW89_TXWD_BODY0_WP_OFFSET, desc_info->wp_offset) |
1033 FIELD_PREP(RTW89_TXWD_BODY0_WD_INFO_EN, desc_info->en_wd_info) |
1034 FIELD_PREP(RTW89_TXWD_BODY0_CHANNEL_DMA, desc_info->ch_dma) |
1035 FIELD_PREP(RTW89_TXWD_BODY0_HDR_LLC_LEN, desc_info->hdr_llc_len) |
1036 FIELD_PREP(RTW89_TXWD_BODY0_WD_PAGE, desc_info->wd_page) |
1037 FIELD_PREP(RTW89_TXWD_BODY0_FW_DL, desc_info->fw_dl) |
1038 FIELD_PREP(RTW89_TXWD_BODY0_HW_SSN_SEL, desc_info->hw_ssn_sel) |
1039 FIELD_PREP(RTW89_TXWD_BODY0_HW_SSN_MODE, desc_info->hw_seq_mode);
1041 return cpu_to_le32(dword);
1044 static __le32 rtw89_build_txwd_body0_v1(struct rtw89_tx_desc_info *desc_info)
1046 u32 dword = FIELD_PREP(RTW89_TXWD_BODY0_WP_OFFSET_V1, desc_info->wp_offset) |
1047 FIELD_PREP(RTW89_TXWD_BODY0_WD_INFO_EN, desc_info->en_wd_info) |
1048 FIELD_PREP(RTW89_TXWD_BODY0_CHANNEL_DMA, desc_info->ch_dma) |
1049 FIELD_PREP(RTW89_TXWD_BODY0_HDR_LLC_LEN, desc_info->hdr_llc_len) |
1050 FIELD_PREP(RTW89_TXWD_BODY0_WD_PAGE, desc_info->wd_page) |
1051 FIELD_PREP(RTW89_TXWD_BODY0_FW_DL, desc_info->fw_dl);
1053 return cpu_to_le32(dword);
1056 static __le32 rtw89_build_txwd_body1_v1(struct rtw89_tx_desc_info *desc_info)
1058 u32 dword = FIELD_PREP(RTW89_TXWD_BODY1_ADDR_INFO_NUM, desc_info->addr_info_nr) |
1059 FIELD_PREP(RTW89_TXWD_BODY1_SEC_KEYID, desc_info->sec_keyid) |
1060 FIELD_PREP(RTW89_TXWD_BODY1_SEC_TYPE, desc_info->sec_type);
1062 return cpu_to_le32(dword);
1065 static __le32 rtw89_build_txwd_body2(struct rtw89_tx_desc_info *desc_info)
1067 u32 dword = FIELD_PREP(RTW89_TXWD_BODY2_TID_INDICATE, desc_info->tid_indicate) |
1068 FIELD_PREP(RTW89_TXWD_BODY2_QSEL, desc_info->qsel) |
1069 FIELD_PREP(RTW89_TXWD_BODY2_TXPKT_SIZE, desc_info->pkt_size) |
1070 FIELD_PREP(RTW89_TXWD_BODY2_MACID, desc_info->mac_id);
1072 return cpu_to_le32(dword);
1075 static __le32 rtw89_build_txwd_body3(struct rtw89_tx_desc_info *desc_info)
1077 u32 dword = FIELD_PREP(RTW89_TXWD_BODY3_SW_SEQ, desc_info->seq) |
1078 FIELD_PREP(RTW89_TXWD_BODY3_AGG_EN, desc_info->agg_en) |
1079 FIELD_PREP(RTW89_TXWD_BODY3_BK, desc_info->bk);
1081 return cpu_to_le32(dword);
1084 static __le32 rtw89_build_txwd_body4(struct rtw89_tx_desc_info *desc_info)
1086 u32 dword = FIELD_PREP(RTW89_TXWD_BODY4_SEC_IV_L0, desc_info->sec_seq[0]) |
1087 FIELD_PREP(RTW89_TXWD_BODY4_SEC_IV_L1, desc_info->sec_seq[1]);
1089 return cpu_to_le32(dword);
1092 static __le32 rtw89_build_txwd_body5(struct rtw89_tx_desc_info *desc_info)
1094 u32 dword = FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H2, desc_info->sec_seq[2]) |
1095 FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H3, desc_info->sec_seq[3]) |
1096 FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H4, desc_info->sec_seq[4]) |
1097 FIELD_PREP(RTW89_TXWD_BODY5_SEC_IV_H5, desc_info->sec_seq[5]);
1099 return cpu_to_le32(dword);
1102 static __le32 rtw89_build_txwd_body7_v1(struct rtw89_tx_desc_info *desc_info)
1104 u32 dword = FIELD_PREP(RTW89_TXWD_BODY7_USE_RATE_V1, desc_info->use_rate) |
1105 FIELD_PREP(RTW89_TXWD_BODY7_DATA_RATE, desc_info->data_rate);
1107 return cpu_to_le32(dword);
1110 static __le32 rtw89_build_txwd_info0(struct rtw89_tx_desc_info *desc_info)
1112 u32 dword = FIELD_PREP(RTW89_TXWD_INFO0_USE_RATE, desc_info->use_rate) |
1113 FIELD_PREP(RTW89_TXWD_INFO0_DATA_RATE, desc_info->data_rate) |
1114 FIELD_PREP(RTW89_TXWD_INFO0_DISDATAFB, desc_info->dis_data_fb) |
1115 FIELD_PREP(RTW89_TXWD_INFO0_MULTIPORT_ID, desc_info->port);
1117 return cpu_to_le32(dword);
1120 static __le32 rtw89_build_txwd_info0_v1(struct rtw89_tx_desc_info *desc_info)
1122 u32 dword = FIELD_PREP(RTW89_TXWD_INFO0_DISDATAFB, desc_info->dis_data_fb) |
1123 FIELD_PREP(RTW89_TXWD_INFO0_MULTIPORT_ID, desc_info->port) |
1124 FIELD_PREP(RTW89_TXWD_INFO0_DATA_ER, desc_info->er_cap) |
1125 FIELD_PREP(RTW89_TXWD_INFO0_DATA_BW_ER, 0);
1127 return cpu_to_le32(dword);
1130 static __le32 rtw89_build_txwd_info1(struct rtw89_tx_desc_info *desc_info)
1132 u32 dword = FIELD_PREP(RTW89_TXWD_INFO1_MAX_AGGNUM, desc_info->ampdu_num) |
1133 FIELD_PREP(RTW89_TXWD_INFO1_A_CTRL_BSR, desc_info->a_ctrl_bsr) |
1134 FIELD_PREP(RTW89_TXWD_INFO1_DATA_RTY_LOWEST_RATE,
1135 desc_info->data_retry_lowest_rate);
1137 return cpu_to_le32(dword);
1140 static __le32 rtw89_build_txwd_info2(struct rtw89_tx_desc_info *desc_info)
1142 u32 dword = FIELD_PREP(RTW89_TXWD_INFO2_AMPDU_DENSITY, desc_info->ampdu_density) |
1143 FIELD_PREP(RTW89_TXWD_INFO2_SEC_TYPE, desc_info->sec_type) |
1144 FIELD_PREP(RTW89_TXWD_INFO2_SEC_HW_ENC, desc_info->sec_en) |
1145 FIELD_PREP(RTW89_TXWD_INFO2_SEC_CAM_IDX, desc_info->sec_cam_idx);
1147 return cpu_to_le32(dword);
1150 static __le32 rtw89_build_txwd_info2_v1(struct rtw89_tx_desc_info *desc_info)
1152 u32 dword = FIELD_PREP(RTW89_TXWD_INFO2_AMPDU_DENSITY, desc_info->ampdu_density) |
1153 FIELD_PREP(RTW89_TXWD_INFO2_FORCE_KEY_EN, desc_info->sec_en) |
1154 FIELD_PREP(RTW89_TXWD_INFO2_SEC_CAM_IDX, desc_info->sec_cam_idx);
1156 return cpu_to_le32(dword);
1159 static __le32 rtw89_build_txwd_info4(struct rtw89_tx_desc_info *desc_info)
1161 u32 dword = FIELD_PREP(RTW89_TXWD_INFO4_RTS_EN, 1) |
1162 FIELD_PREP(RTW89_TXWD_INFO4_HW_RTS_EN, 1);
1164 return cpu_to_le32(dword);
1167 void rtw89_core_fill_txdesc(struct rtw89_dev *rtwdev,
1168 struct rtw89_tx_desc_info *desc_info,
1171 struct rtw89_txwd_body *txwd_body = (struct rtw89_txwd_body *)txdesc;
1172 struct rtw89_txwd_info *txwd_info;
1174 txwd_body->dword0 = rtw89_build_txwd_body0(desc_info);
1175 txwd_body->dword2 = rtw89_build_txwd_body2(desc_info);
1176 txwd_body->dword3 = rtw89_build_txwd_body3(desc_info);
1178 if (!desc_info->en_wd_info)
1181 txwd_info = (struct rtw89_txwd_info *)(txwd_body + 1);
1182 txwd_info->dword0 = rtw89_build_txwd_info0(desc_info);
1183 txwd_info->dword1 = rtw89_build_txwd_info1(desc_info);
1184 txwd_info->dword2 = rtw89_build_txwd_info2(desc_info);
1185 txwd_info->dword4 = rtw89_build_txwd_info4(desc_info);
1188 EXPORT_SYMBOL(rtw89_core_fill_txdesc);
1190 void rtw89_core_fill_txdesc_v1(struct rtw89_dev *rtwdev,
1191 struct rtw89_tx_desc_info *desc_info,
1194 struct rtw89_txwd_body_v1 *txwd_body = (struct rtw89_txwd_body_v1 *)txdesc;
1195 struct rtw89_txwd_info *txwd_info;
1197 txwd_body->dword0 = rtw89_build_txwd_body0_v1(desc_info);
1198 txwd_body->dword1 = rtw89_build_txwd_body1_v1(desc_info);
1199 txwd_body->dword2 = rtw89_build_txwd_body2(desc_info);
1200 txwd_body->dword3 = rtw89_build_txwd_body3(desc_info);
1201 if (desc_info->sec_en) {
1202 txwd_body->dword4 = rtw89_build_txwd_body4(desc_info);
1203 txwd_body->dword5 = rtw89_build_txwd_body5(desc_info);
1205 txwd_body->dword7 = rtw89_build_txwd_body7_v1(desc_info);
1207 if (!desc_info->en_wd_info)
1210 txwd_info = (struct rtw89_txwd_info *)(txwd_body + 1);
1211 txwd_info->dword0 = rtw89_build_txwd_info0_v1(desc_info);
1212 txwd_info->dword1 = rtw89_build_txwd_info1(desc_info);
1213 txwd_info->dword2 = rtw89_build_txwd_info2_v1(desc_info);
1214 txwd_info->dword4 = rtw89_build_txwd_info4(desc_info);
1216 EXPORT_SYMBOL(rtw89_core_fill_txdesc_v1);
1218 static __le32 rtw89_build_txwd_fwcmd0_v1(struct rtw89_tx_desc_info *desc_info)
1220 u32 dword = FIELD_PREP(AX_RXD_RPKT_LEN_MASK, desc_info->pkt_size) |
1221 FIELD_PREP(AX_RXD_RPKT_TYPE_MASK, desc_info->fw_dl ?
1222 RTW89_CORE_RX_TYPE_FWDL :
1223 RTW89_CORE_RX_TYPE_H2C);
1225 return cpu_to_le32(dword);
1228 void rtw89_core_fill_txdesc_fwcmd_v1(struct rtw89_dev *rtwdev,
1229 struct rtw89_tx_desc_info *desc_info,
1232 struct rtw89_rxdesc_short *txwd_v1 = (struct rtw89_rxdesc_short *)txdesc;
1234 txwd_v1->dword0 = rtw89_build_txwd_fwcmd0_v1(desc_info);
1236 EXPORT_SYMBOL(rtw89_core_fill_txdesc_fwcmd_v1);
1238 static int rtw89_core_rx_process_mac_ppdu(struct rtw89_dev *rtwdev,
1239 struct sk_buff *skb,
1240 struct rtw89_rx_phy_ppdu *phy_ppdu)
1242 const struct rtw89_rxinfo *rxinfo = (const struct rtw89_rxinfo *)skb->data;
1243 bool rx_cnt_valid = false;
1248 rx_cnt_valid = le32_get_bits(rxinfo->w0, RTW89_RXINFO_W0_RX_CNT_VLD);
1249 plcp_size = le32_get_bits(rxinfo->w1, RTW89_RXINFO_W1_PLCP_LEN) << 3;
1250 usr_num = le32_get_bits(rxinfo->w0, RTW89_RXINFO_W0_USR_NUM);
1251 if (usr_num > RTW89_PPDU_MAX_USR) {
1252 rtw89_warn(rtwdev, "Invalid user number in mac info\n");
1256 phy_sts = skb->data + RTW89_PPDU_MAC_INFO_SIZE;
1257 phy_sts += usr_num * RTW89_PPDU_MAC_INFO_USR_SIZE;
1258 /* 8-byte alignment */
1259 if (usr_num & BIT(0))
1260 phy_sts += RTW89_PPDU_MAC_INFO_USR_SIZE;
1262 phy_sts += RTW89_PPDU_MAC_RX_CNT_SIZE;
1263 phy_sts += plcp_size;
1265 phy_ppdu->buf = phy_sts;
1266 phy_ppdu->len = skb->data + skb->len - phy_sts;
1271 static void rtw89_core_rx_process_phy_ppdu_iter(void *data,
1272 struct ieee80211_sta *sta)
1274 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
1275 struct rtw89_rx_phy_ppdu *phy_ppdu = (struct rtw89_rx_phy_ppdu *)data;
1276 struct rtw89_dev *rtwdev = rtwsta->rtwdev;
1277 struct rtw89_hal *hal = &rtwdev->hal;
1278 u8 ant_num = hal->ant_diversity ? 2 : rtwdev->chip->rf_path_num;
1279 u8 ant_pos = U8_MAX;
1283 if (rtwsta->mac_id != phy_ppdu->mac_id || !phy_ppdu->to_self)
1286 if (hal->ant_diversity && hal->antenna_rx) {
1287 ant_pos = __ffs(hal->antenna_rx);
1291 ewma_rssi_add(&rtwsta->avg_rssi, phy_ppdu->rssi_avg);
1293 if (ant_pos < ant_num) {
1294 ewma_rssi_add(&rtwsta->rssi[ant_pos], phy_ppdu->rssi[0]);
1296 for (i = 0; i < rtwdev->chip->rf_path_num; i++)
1297 ewma_rssi_add(&rtwsta->rssi[i], phy_ppdu->rssi[i]);
1300 if (phy_ppdu->ofdm.has) {
1301 ewma_snr_add(&rtwsta->avg_snr, phy_ppdu->ofdm.avg_snr);
1302 ewma_evm_add(&rtwsta->evm_min[evm_pos], phy_ppdu->ofdm.evm_min);
1303 ewma_evm_add(&rtwsta->evm_max[evm_pos], phy_ppdu->ofdm.evm_max);
1307 #define VAR_LEN 0xff
1308 #define VAR_LEN_UNIT 8
1309 static u16 rtw89_core_get_phy_status_ie_len(struct rtw89_dev *rtwdev,
1310 const struct rtw89_phy_sts_iehdr *iehdr)
1312 static const u8 physts_ie_len_tab[32] = {
1313 16, 32, 24, 24, 8, 8, 8, 8, VAR_LEN, 8, VAR_LEN, 176, VAR_LEN,
1314 VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, VAR_LEN, 16, 24, VAR_LEN,
1315 VAR_LEN, VAR_LEN, 0, 24, 24, 24, 24, 32, 32, 32, 32
1320 ie = le32_get_bits(iehdr->w0, RTW89_PHY_STS_IEHDR_TYPE);
1321 if (physts_ie_len_tab[ie] != VAR_LEN)
1322 ie_len = physts_ie_len_tab[ie];
1324 ie_len = le32_get_bits(iehdr->w0, RTW89_PHY_STS_IEHDR_LEN) * VAR_LEN_UNIT;
1329 static void rtw89_core_parse_phy_status_ie01(struct rtw89_dev *rtwdev,
1330 const struct rtw89_phy_sts_iehdr *iehdr,
1331 struct rtw89_rx_phy_ppdu *phy_ppdu)
1333 const struct rtw89_phy_sts_ie0 *ie = (const struct rtw89_phy_sts_ie0 *)iehdr;
1337 phy_ppdu->chan_idx = le32_get_bits(ie->w0, RTW89_PHY_STS_IE01_W0_CH_IDX);
1338 if (phy_ppdu->rate < RTW89_HW_RATE_OFDM6)
1341 if (!phy_ppdu->to_self)
1344 phy_ppdu->ofdm.avg_snr = le32_get_bits(ie->w2, RTW89_PHY_STS_IE01_W2_AVG_SNR);
1345 phy_ppdu->ofdm.evm_max = le32_get_bits(ie->w2, RTW89_PHY_STS_IE01_W2_EVM_MAX);
1346 phy_ppdu->ofdm.evm_min = le32_get_bits(ie->w2, RTW89_PHY_STS_IE01_W2_EVM_MIN);
1347 phy_ppdu->ofdm.has = true;
1349 /* sign conversion for S(12,2) */
1350 if (rtwdev->chip->cfo_src_fd) {
1351 t = le32_get_bits(ie->w1, RTW89_PHY_STS_IE01_W1_FD_CFO);
1352 cfo = sign_extend32(t, 11);
1354 t = le32_get_bits(ie->w1, RTW89_PHY_STS_IE01_W1_PREMB_CFO);
1355 cfo = sign_extend32(t, 11);
1358 rtw89_phy_cfo_parse(rtwdev, cfo, phy_ppdu);
1361 static int rtw89_core_process_phy_status_ie(struct rtw89_dev *rtwdev,
1362 const struct rtw89_phy_sts_iehdr *iehdr,
1363 struct rtw89_rx_phy_ppdu *phy_ppdu)
1367 ie = le32_get_bits(iehdr->w0, RTW89_PHY_STS_IEHDR_TYPE);
1370 case RTW89_PHYSTS_IE01_CMN_OFDM:
1371 rtw89_core_parse_phy_status_ie01(rtwdev, iehdr, phy_ppdu);
1380 static void rtw89_core_update_phy_ppdu(struct rtw89_rx_phy_ppdu *phy_ppdu)
1382 const struct rtw89_phy_sts_hdr *hdr = phy_ppdu->buf;
1383 u8 *rssi = phy_ppdu->rssi;
1385 phy_ppdu->ie = le32_get_bits(hdr->w0, RTW89_PHY_STS_HDR_W0_IE_MAP);
1386 phy_ppdu->rssi_avg = le32_get_bits(hdr->w0, RTW89_PHY_STS_HDR_W0_RSSI_AVG);
1387 rssi[RF_PATH_A] = le32_get_bits(hdr->w1, RTW89_PHY_STS_HDR_W1_RSSI_A);
1388 rssi[RF_PATH_B] = le32_get_bits(hdr->w1, RTW89_PHY_STS_HDR_W1_RSSI_B);
1389 rssi[RF_PATH_C] = le32_get_bits(hdr->w1, RTW89_PHY_STS_HDR_W1_RSSI_C);
1390 rssi[RF_PATH_D] = le32_get_bits(hdr->w1, RTW89_PHY_STS_HDR_W1_RSSI_D);
1393 static int rtw89_core_rx_process_phy_ppdu(struct rtw89_dev *rtwdev,
1394 struct rtw89_rx_phy_ppdu *phy_ppdu)
1396 const struct rtw89_phy_sts_hdr *hdr = phy_ppdu->buf;
1397 u32 len_from_header;
1399 len_from_header = le32_get_bits(hdr->w0, RTW89_PHY_STS_HDR_W0_LEN) << 3;
1401 if (len_from_header != phy_ppdu->len) {
1402 rtw89_debug(rtwdev, RTW89_DBG_UNEXP, "phy ppdu len mismatch\n");
1405 rtw89_core_update_phy_ppdu(phy_ppdu);
1410 static int rtw89_core_rx_parse_phy_sts(struct rtw89_dev *rtwdev,
1411 struct rtw89_rx_phy_ppdu *phy_ppdu)
1416 /* mark invalid reports and bypass them */
1417 if (phy_ppdu->ie < RTW89_CCK_PKT)
1420 pos = phy_ppdu->buf + PHY_STS_HDR_LEN;
1421 end = phy_ppdu->buf + phy_ppdu->len;
1423 const struct rtw89_phy_sts_iehdr *iehdr = pos;
1425 ie_len = rtw89_core_get_phy_status_ie_len(rtwdev, iehdr);
1426 rtw89_core_process_phy_status_ie(rtwdev, iehdr, phy_ppdu);
1428 if (pos > end || ie_len == 0) {
1429 rtw89_debug(rtwdev, RTW89_DBG_TXRX,
1430 "phy status parse failed\n");
1435 rtw89_phy_antdiv_parse(rtwdev, phy_ppdu);
1440 static void rtw89_core_rx_process_phy_sts(struct rtw89_dev *rtwdev,
1441 struct rtw89_rx_phy_ppdu *phy_ppdu)
1445 ret = rtw89_core_rx_parse_phy_sts(rtwdev, phy_ppdu);
1447 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "parse phy sts failed\n");
1449 phy_ppdu->valid = true;
1451 ieee80211_iterate_stations_atomic(rtwdev->hw,
1452 rtw89_core_rx_process_phy_ppdu_iter,
1456 static u8 rtw89_rxdesc_to_nl_he_gi(struct rtw89_dev *rtwdev,
1457 const struct rtw89_rx_desc_info *desc_info,
1460 switch (desc_info->gi_ltf) {
1461 case RTW89_GILTF_SGI_4XHE08:
1462 case RTW89_GILTF_2XHE08:
1463 case RTW89_GILTF_1XHE08:
1464 return NL80211_RATE_INFO_HE_GI_0_8;
1465 case RTW89_GILTF_2XHE16:
1466 case RTW89_GILTF_1XHE16:
1467 return NL80211_RATE_INFO_HE_GI_1_6;
1468 case RTW89_GILTF_LGI_4XHE32:
1469 return NL80211_RATE_INFO_HE_GI_3_2;
1471 rtw89_warn(rtwdev, "invalid gi_ltf=%d", desc_info->gi_ltf);
1472 return rx_status ? NL80211_RATE_INFO_HE_GI_3_2 : U8_MAX;
1476 static bool rtw89_core_rx_ppdu_match(struct rtw89_dev *rtwdev,
1477 struct rtw89_rx_desc_info *desc_info,
1478 struct ieee80211_rx_status *status)
1480 u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
1481 u8 data_rate_mode, bw, rate_idx = MASKBYTE0, gi_ltf;
1485 data_rate = desc_info->data_rate;
1486 data_rate_mode = rtw89_get_data_rate_mode(rtwdev, data_rate);
1487 if (data_rate_mode == DATA_RATE_MODE_NON_HT) {
1488 rate_idx = rtw89_get_data_not_ht_idx(rtwdev, data_rate);
1489 /* rate_idx is still hardware value here */
1490 } else if (data_rate_mode == DATA_RATE_MODE_HT) {
1491 rate_idx = rtw89_get_data_ht_mcs(rtwdev, data_rate);
1492 } else if (data_rate_mode == DATA_RATE_MODE_VHT) {
1493 rate_idx = rtw89_get_data_mcs(rtwdev, data_rate);
1494 } else if (data_rate_mode == DATA_RATE_MODE_HE) {
1495 rate_idx = rtw89_get_data_mcs(rtwdev, data_rate);
1497 rtw89_warn(rtwdev, "invalid RX rate mode %d\n", data_rate_mode);
1500 bw = rtw89_hw_to_rate_info_bw(desc_info->bw);
1501 gi_ltf = rtw89_rxdesc_to_nl_he_gi(rtwdev, desc_info, false);
1502 ret = rtwdev->ppdu_sts.curr_rx_ppdu_cnt[band] == desc_info->ppdu_cnt &&
1503 status->rate_idx == rate_idx &&
1504 status->he_gi == gi_ltf &&
1510 struct rtw89_vif_rx_stats_iter_data {
1511 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;
1518 static void rtw89_stats_trigger_frame(struct rtw89_dev *rtwdev,
1519 struct ieee80211_vif *vif,
1520 struct sk_buff *skb)
1522 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
1523 struct ieee80211_trigger *tf = (struct ieee80211_trigger *)skb->data;
1524 u8 *pos, *end, type;
1527 if (!ether_addr_equal(vif->bss_conf.bssid, tf->ta) ||
1528 rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION ||
1529 rtwvif->net_type == RTW89_NET_TYPE_NO_LINK)
1532 type = le64_get_bits(tf->common_info, IEEE80211_TRIGGER_TYPE_MASK);
1533 if (type != IEEE80211_TRIGGER_TYPE_BASIC)
1536 end = (u8 *)tf + skb->len;
1539 while (end - pos >= RTW89_TF_BASIC_USER_INFO_SZ) {
1540 aid = RTW89_GET_TF_USER_INFO_AID12(pos);
1541 rtw89_debug(rtwdev, RTW89_DBG_TXRX,
1542 "[TF] aid: %d, ul_mcs: %d, rua: %d\n",
1543 aid, RTW89_GET_TF_USER_INFO_UL_MCS(pos),
1544 RTW89_GET_TF_USER_INFO_RUA(pos));
1546 if (aid == RTW89_TF_PAD)
1549 if (aid == vif->cfg.aid) {
1550 rtwvif->stats.rx_tf_acc++;
1551 rtwdev->stats.rx_tf_acc++;
1555 pos += RTW89_TF_BASIC_USER_INFO_SZ;
1559 static void rtw89_cancel_6ghz_probe_work(struct work_struct *work)
1561 struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
1562 cancel_6ghz_probe_work);
1563 struct list_head *pkt_list = rtwdev->scan_info.pkt_list;
1564 struct rtw89_pktofld_info *info;
1566 mutex_lock(&rtwdev->mutex);
1568 if (!rtwdev->scanning)
1571 list_for_each_entry(info, &pkt_list[NL80211_BAND_6GHZ], list) {
1572 if (!info->cancel || !test_bit(info->id, rtwdev->pkt_offload))
1575 rtw89_fw_h2c_del_pkt_offload(rtwdev, info->id);
1577 /* Don't delete/free info from pkt_list at this moment. Let it
1578 * be deleted/freed in rtw89_release_pkt_list() after scanning,
1579 * since if during scanning, pkt_list is accessed in bottom half.
1584 mutex_unlock(&rtwdev->mutex);
1587 static void rtw89_core_cancel_6ghz_probe_tx(struct rtw89_dev *rtwdev,
1588 struct sk_buff *skb)
1590 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1591 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1592 struct list_head *pkt_list = rtwdev->scan_info.pkt_list;
1593 struct rtw89_pktofld_info *info;
1594 const u8 *ies = mgmt->u.beacon.variable, *ssid_ie;
1595 bool queue_work = false;
1597 if (rx_status->band != NL80211_BAND_6GHZ)
1600 ssid_ie = cfg80211_find_ie(WLAN_EID_SSID, ies, skb->len);
1602 list_for_each_entry(info, &pkt_list[NL80211_BAND_6GHZ], list) {
1603 if (ether_addr_equal(info->bssid, mgmt->bssid)) {
1604 info->cancel = true;
1609 if (!ssid_ie || ssid_ie[1] != info->ssid_len || info->ssid_len == 0)
1612 if (memcmp(&ssid_ie[2], info->ssid, info->ssid_len) == 0) {
1613 info->cancel = true;
1619 ieee80211_queue_work(rtwdev->hw, &rtwdev->cancel_6ghz_probe_work);
1622 static void rtw89_vif_rx_stats_iter(void *data, u8 *mac,
1623 struct ieee80211_vif *vif)
1625 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
1626 struct rtw89_vif_rx_stats_iter_data *iter_data = data;
1627 struct rtw89_dev *rtwdev = iter_data->rtwdev;
1628 struct rtw89_pkt_stat *pkt_stat = &rtwdev->phystat.cur_pkt_stat;
1629 struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
1630 struct sk_buff *skb = iter_data->skb;
1631 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1632 struct rtw89_rx_phy_ppdu *phy_ppdu = iter_data->phy_ppdu;
1633 const u8 *bssid = iter_data->bssid;
1635 if (rtwdev->scanning &&
1636 (ieee80211_is_beacon(hdr->frame_control) ||
1637 ieee80211_is_probe_resp(hdr->frame_control)))
1638 rtw89_core_cancel_6ghz_probe_tx(rtwdev, skb);
1640 if (!vif->bss_conf.bssid)
1643 if (ieee80211_is_trigger(hdr->frame_control)) {
1644 rtw89_stats_trigger_frame(rtwdev, vif, skb);
1648 if (!ether_addr_equal(vif->bss_conf.bssid, bssid))
1651 if (ieee80211_is_beacon(hdr->frame_control)) {
1652 if (vif->type == NL80211_IFTYPE_STATION)
1653 rtw89_fw_h2c_rssi_offload(rtwdev, phy_ppdu);
1654 pkt_stat->beacon_nr++;
1657 if (!ether_addr_equal(vif->addr, hdr->addr1))
1660 if (desc_info->data_rate < RTW89_HW_RATE_NR)
1661 pkt_stat->rx_rate_cnt[desc_info->data_rate]++;
1663 rtw89_traffic_stats_accu(rtwdev, &rtwvif->stats, skb, false);
1666 static void rtw89_core_rx_stats(struct rtw89_dev *rtwdev,
1667 struct rtw89_rx_phy_ppdu *phy_ppdu,
1668 struct rtw89_rx_desc_info *desc_info,
1669 struct sk_buff *skb)
1671 struct rtw89_vif_rx_stats_iter_data iter_data;
1673 rtw89_traffic_stats_accu(rtwdev, &rtwdev->stats, skb, false);
1675 iter_data.rtwdev = rtwdev;
1676 iter_data.phy_ppdu = phy_ppdu;
1677 iter_data.desc_info = desc_info;
1678 iter_data.skb = skb;
1679 iter_data.bssid = get_hdr_bssid((struct ieee80211_hdr *)skb->data);
1680 rtw89_iterate_vifs_bh(rtwdev, rtw89_vif_rx_stats_iter, &iter_data);
1683 static void rtw89_correct_cck_chan(struct rtw89_dev *rtwdev,
1684 struct ieee80211_rx_status *status)
1686 const struct rtw89_chan_rcd *rcd =
1687 rtw89_chan_rcd_get(rtwdev, RTW89_SUB_ENTITY_0);
1688 u16 chan = rcd->prev_primary_channel;
1689 u8 band = rtw89_hw_to_nl80211_band(rcd->prev_band_type);
1691 if (status->band != NL80211_BAND_2GHZ &&
1692 status->encoding == RX_ENC_LEGACY &&
1693 status->rate_idx < RTW89_HW_RATE_OFDM6) {
1694 status->freq = ieee80211_channel_to_frequency(chan, band);
1695 status->band = band;
1699 static void rtw89_core_hw_to_sband_rate(struct ieee80211_rx_status *rx_status)
1701 if (rx_status->band == NL80211_BAND_2GHZ ||
1702 rx_status->encoding != RX_ENC_LEGACY)
1705 /* Some control frames' freq(ACKs in this case) are reported wrong due
1706 * to FW notify timing, set to lowest rate to prevent overflow.
1708 if (rx_status->rate_idx < RTW89_HW_RATE_OFDM6) {
1709 rx_status->rate_idx = 0;
1713 /* No 4 CCK rates for non-2G */
1714 rx_status->rate_idx -= 4;
1717 static void rtw89_core_update_radiotap(struct rtw89_dev *rtwdev,
1718 struct sk_buff *skb,
1719 struct ieee80211_rx_status *rx_status)
1721 static const struct ieee80211_radiotap_he known_he = {
1722 .data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
1723 IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN),
1724 .data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN),
1726 struct ieee80211_radiotap_he *he;
1728 if (!(rtwdev->hw->conf.flags & IEEE80211_CONF_MONITOR))
1731 if (rx_status->encoding == RX_ENC_HE) {
1732 rx_status->flag |= RX_FLAG_RADIOTAP_HE;
1733 he = skb_push(skb, sizeof(*he));
1738 static void rtw89_core_rx_to_mac80211(struct rtw89_dev *rtwdev,
1739 struct rtw89_rx_phy_ppdu *phy_ppdu,
1740 struct rtw89_rx_desc_info *desc_info,
1741 struct sk_buff *skb_ppdu,
1742 struct ieee80211_rx_status *rx_status)
1744 struct napi_struct *napi = &rtwdev->napi;
1746 /* In low power mode, napi isn't scheduled. Receive it to netif. */
1747 if (unlikely(!test_bit(NAPI_STATE_SCHED, &napi->state)))
1750 rtw89_core_hw_to_sband_rate(rx_status);
1751 rtw89_core_rx_stats(rtwdev, phy_ppdu, desc_info, skb_ppdu);
1752 rtw89_core_update_radiotap(rtwdev, skb_ppdu, rx_status);
1753 /* In low power mode, it does RX in thread context. */
1755 ieee80211_rx_napi(rtwdev->hw, NULL, skb_ppdu, napi);
1757 rtwdev->napi_budget_countdown--;
1760 static void rtw89_core_rx_pending_skb(struct rtw89_dev *rtwdev,
1761 struct rtw89_rx_phy_ppdu *phy_ppdu,
1762 struct rtw89_rx_desc_info *desc_info,
1763 struct sk_buff *skb)
1765 u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
1766 int curr = rtwdev->ppdu_sts.curr_rx_ppdu_cnt[band];
1767 struct sk_buff *skb_ppdu = NULL, *tmp;
1768 struct ieee80211_rx_status *rx_status;
1770 if (curr > RTW89_MAX_PPDU_CNT)
1773 skb_queue_walk_safe(&rtwdev->ppdu_sts.rx_queue[band], skb_ppdu, tmp) {
1774 skb_unlink(skb_ppdu, &rtwdev->ppdu_sts.rx_queue[band]);
1775 rx_status = IEEE80211_SKB_RXCB(skb_ppdu);
1776 if (rtw89_core_rx_ppdu_match(rtwdev, desc_info, rx_status))
1777 rtw89_chip_query_ppdu(rtwdev, phy_ppdu, rx_status);
1778 rtw89_correct_cck_chan(rtwdev, rx_status);
1779 rtw89_core_rx_to_mac80211(rtwdev, phy_ppdu, desc_info, skb_ppdu, rx_status);
1783 static void rtw89_core_rx_process_ppdu_sts(struct rtw89_dev *rtwdev,
1784 struct rtw89_rx_desc_info *desc_info,
1785 struct sk_buff *skb)
1787 struct rtw89_rx_phy_ppdu phy_ppdu = {.buf = skb->data, .valid = false,
1789 .to_self = desc_info->addr1_match,
1790 .rate = desc_info->data_rate,
1791 .mac_id = desc_info->mac_id};
1794 if (desc_info->mac_info_valid)
1795 rtw89_core_rx_process_mac_ppdu(rtwdev, skb, &phy_ppdu);
1796 ret = rtw89_core_rx_process_phy_ppdu(rtwdev, &phy_ppdu);
1798 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "process ppdu failed\n");
1800 rtw89_core_rx_process_phy_sts(rtwdev, &phy_ppdu);
1801 rtw89_core_rx_pending_skb(rtwdev, &phy_ppdu, desc_info, skb);
1802 dev_kfree_skb_any(skb);
1805 static void rtw89_core_rx_process_report(struct rtw89_dev *rtwdev,
1806 struct rtw89_rx_desc_info *desc_info,
1807 struct sk_buff *skb)
1809 switch (desc_info->pkt_type) {
1810 case RTW89_CORE_RX_TYPE_C2H:
1811 rtw89_fw_c2h_irqsafe(rtwdev, skb);
1813 case RTW89_CORE_RX_TYPE_PPDU_STAT:
1814 rtw89_core_rx_process_ppdu_sts(rtwdev, desc_info, skb);
1817 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "unhandled pkt_type=%d\n",
1818 desc_info->pkt_type);
1819 dev_kfree_skb_any(skb);
1824 void rtw89_core_query_rxdesc(struct rtw89_dev *rtwdev,
1825 struct rtw89_rx_desc_info *desc_info,
1826 u8 *data, u32 data_offset)
1828 const struct rtw89_chip_info *chip = rtwdev->chip;
1829 struct rtw89_rxdesc_short *rxd_s;
1830 struct rtw89_rxdesc_long *rxd_l;
1831 u8 shift_len, drv_info_len;
1833 rxd_s = (struct rtw89_rxdesc_short *)(data + data_offset);
1834 desc_info->pkt_size = le32_get_bits(rxd_s->dword0, AX_RXD_RPKT_LEN_MASK);
1835 desc_info->drv_info_size = le32_get_bits(rxd_s->dword0, AX_RXD_DRV_INFO_SIZE_MASK);
1836 desc_info->long_rxdesc = le32_get_bits(rxd_s->dword0, AX_RXD_LONG_RXD);
1837 desc_info->pkt_type = le32_get_bits(rxd_s->dword0, AX_RXD_RPKT_TYPE_MASK);
1838 desc_info->mac_info_valid = le32_get_bits(rxd_s->dword0, AX_RXD_MAC_INFO_VLD);
1839 if (chip->chip_id == RTL8852C)
1840 desc_info->bw = le32_get_bits(rxd_s->dword1, AX_RXD_BW_v1_MASK);
1842 desc_info->bw = le32_get_bits(rxd_s->dword1, AX_RXD_BW_MASK);
1843 desc_info->data_rate = le32_get_bits(rxd_s->dword1, AX_RXD_RX_DATARATE_MASK);
1844 desc_info->gi_ltf = le32_get_bits(rxd_s->dword1, AX_RXD_RX_GI_LTF_MASK);
1845 desc_info->user_id = le32_get_bits(rxd_s->dword1, AX_RXD_USER_ID_MASK);
1846 desc_info->sr_en = le32_get_bits(rxd_s->dword1, AX_RXD_SR_EN);
1847 desc_info->ppdu_cnt = le32_get_bits(rxd_s->dword1, AX_RXD_PPDU_CNT_MASK);
1848 desc_info->ppdu_type = le32_get_bits(rxd_s->dword1, AX_RXD_PPDU_TYPE_MASK);
1849 desc_info->free_run_cnt = le32_get_bits(rxd_s->dword2, AX_RXD_FREERUN_CNT_MASK);
1850 desc_info->icv_err = le32_get_bits(rxd_s->dword3, AX_RXD_ICV_ERR);
1851 desc_info->crc32_err = le32_get_bits(rxd_s->dword3, AX_RXD_CRC32_ERR);
1852 desc_info->hw_dec = le32_get_bits(rxd_s->dword3, AX_RXD_HW_DEC);
1853 desc_info->sw_dec = le32_get_bits(rxd_s->dword3, AX_RXD_SW_DEC);
1854 desc_info->addr1_match = le32_get_bits(rxd_s->dword3, AX_RXD_A1_MATCH);
1856 shift_len = desc_info->shift << 1; /* 2-byte unit */
1857 drv_info_len = desc_info->drv_info_size << 3; /* 8-byte unit */
1858 desc_info->offset = data_offset + shift_len + drv_info_len;
1859 if (desc_info->long_rxdesc)
1860 desc_info->rxd_len = sizeof(struct rtw89_rxdesc_long);
1862 desc_info->rxd_len = sizeof(struct rtw89_rxdesc_short);
1863 desc_info->ready = true;
1865 if (!desc_info->long_rxdesc)
1868 rxd_l = (struct rtw89_rxdesc_long *)(data + data_offset);
1869 desc_info->frame_type = le32_get_bits(rxd_l->dword4, AX_RXD_TYPE_MASK);
1870 desc_info->addr_cam_valid = le32_get_bits(rxd_l->dword5, AX_RXD_ADDR_CAM_VLD);
1871 desc_info->addr_cam_id = le32_get_bits(rxd_l->dword5, AX_RXD_ADDR_CAM_MASK);
1872 desc_info->sec_cam_id = le32_get_bits(rxd_l->dword5, AX_RXD_SEC_CAM_IDX_MASK);
1873 desc_info->mac_id = le32_get_bits(rxd_l->dword5, AX_RXD_MAC_ID_MASK);
1874 desc_info->rx_pl_id = le32_get_bits(rxd_l->dword5, AX_RXD_RX_PL_ID_MASK);
1876 EXPORT_SYMBOL(rtw89_core_query_rxdesc);
1878 struct rtw89_core_iter_rx_status {
1879 struct rtw89_dev *rtwdev;
1880 struct ieee80211_rx_status *rx_status;
1881 struct rtw89_rx_desc_info *desc_info;
1886 void rtw89_core_stats_sta_rx_status_iter(void *data, struct ieee80211_sta *sta)
1888 struct rtw89_core_iter_rx_status *iter_data =
1889 (struct rtw89_core_iter_rx_status *)data;
1890 struct ieee80211_rx_status *rx_status = iter_data->rx_status;
1891 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
1892 struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
1893 u8 mac_id = iter_data->mac_id;
1895 if (mac_id != rtwsta->mac_id)
1898 rtwsta->rx_status = *rx_status;
1899 rtwsta->rx_hw_rate = desc_info->data_rate;
1902 static void rtw89_core_stats_sta_rx_status(struct rtw89_dev *rtwdev,
1903 struct rtw89_rx_desc_info *desc_info,
1904 struct ieee80211_rx_status *rx_status)
1906 struct rtw89_core_iter_rx_status iter_data;
1908 if (!desc_info->addr1_match || !desc_info->long_rxdesc)
1911 if (desc_info->frame_type != RTW89_RX_TYPE_DATA)
1914 iter_data.rtwdev = rtwdev;
1915 iter_data.rx_status = rx_status;
1916 iter_data.desc_info = desc_info;
1917 iter_data.mac_id = desc_info->mac_id;
1918 ieee80211_iterate_stations_atomic(rtwdev->hw,
1919 rtw89_core_stats_sta_rx_status_iter,
1923 static void rtw89_core_update_rx_status(struct rtw89_dev *rtwdev,
1924 struct rtw89_rx_desc_info *desc_info,
1925 struct ieee80211_rx_status *rx_status)
1927 const struct cfg80211_chan_def *chandef =
1928 rtw89_chandef_get(rtwdev, RTW89_SUB_ENTITY_0);
1932 /* currently using single PHY */
1933 rx_status->freq = chandef->chan->center_freq;
1934 rx_status->band = chandef->chan->band;
1936 if (rtwdev->scanning &&
1937 RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw)) {
1938 const struct rtw89_chan *cur = rtw89_scan_chan_get(rtwdev);
1939 u8 chan = cur->primary_channel;
1940 u8 band = cur->band_type;
1941 enum nl80211_band nl_band;
1943 nl_band = rtw89_hw_to_nl80211_band(band);
1944 rx_status->freq = ieee80211_channel_to_frequency(chan, nl_band);
1945 rx_status->band = nl_band;
1948 if (desc_info->icv_err || desc_info->crc32_err)
1949 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
1951 if (desc_info->hw_dec &&
1952 !(desc_info->sw_dec || desc_info->icv_err))
1953 rx_status->flag |= RX_FLAG_DECRYPTED;
1955 rx_status->bw = rtw89_hw_to_rate_info_bw(desc_info->bw);
1957 data_rate = desc_info->data_rate;
1958 data_rate_mode = rtw89_get_data_rate_mode(rtwdev, data_rate);
1959 if (data_rate_mode == DATA_RATE_MODE_NON_HT) {
1960 rx_status->encoding = RX_ENC_LEGACY;
1961 rx_status->rate_idx = rtw89_get_data_not_ht_idx(rtwdev, data_rate);
1962 /* convert rate_idx after we get the correct band */
1963 } else if (data_rate_mode == DATA_RATE_MODE_HT) {
1964 rx_status->encoding = RX_ENC_HT;
1965 rx_status->rate_idx = rtw89_get_data_ht_mcs(rtwdev, data_rate);
1966 if (desc_info->gi_ltf)
1967 rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1968 } else if (data_rate_mode == DATA_RATE_MODE_VHT) {
1969 rx_status->encoding = RX_ENC_VHT;
1970 rx_status->rate_idx = rtw89_get_data_mcs(rtwdev, data_rate);
1971 rx_status->nss = rtw89_get_data_nss(rtwdev, data_rate) + 1;
1972 if (desc_info->gi_ltf)
1973 rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1974 } else if (data_rate_mode == DATA_RATE_MODE_HE) {
1975 rx_status->encoding = RX_ENC_HE;
1976 rx_status->rate_idx = rtw89_get_data_mcs(rtwdev, data_rate);
1977 rx_status->nss = rtw89_get_data_nss(rtwdev, data_rate) + 1;
1979 rtw89_warn(rtwdev, "invalid RX rate mode %d\n", data_rate_mode);
1982 /* he_gi is used to match ppdu, so we always fill it. */
1983 rx_status->he_gi = rtw89_rxdesc_to_nl_he_gi(rtwdev, desc_info, true);
1984 rx_status->flag |= RX_FLAG_MACTIME_START;
1985 rx_status->mactime = desc_info->free_run_cnt;
1987 rtw89_core_stats_sta_rx_status(rtwdev, desc_info, rx_status);
1990 static enum rtw89_ps_mode rtw89_update_ps_mode(struct rtw89_dev *rtwdev)
1992 const struct rtw89_chip_info *chip = rtwdev->chip;
1994 if (rtw89_disable_ps_mode || !chip->ps_mode_supported ||
1995 RTW89_CHK_FW_FEATURE(NO_DEEP_PS, &rtwdev->fw))
1996 return RTW89_PS_MODE_NONE;
1998 if ((chip->ps_mode_supported & BIT(RTW89_PS_MODE_PWR_GATED)) &&
1999 !RTW89_CHK_FW_FEATURE(NO_LPS_PG, &rtwdev->fw))
2000 return RTW89_PS_MODE_PWR_GATED;
2002 if (chip->ps_mode_supported & BIT(RTW89_PS_MODE_CLK_GATED))
2003 return RTW89_PS_MODE_CLK_GATED;
2005 if (chip->ps_mode_supported & BIT(RTW89_PS_MODE_RFOFF))
2006 return RTW89_PS_MODE_RFOFF;
2008 return RTW89_PS_MODE_NONE;
2011 static void rtw89_core_flush_ppdu_rx_queue(struct rtw89_dev *rtwdev,
2012 struct rtw89_rx_desc_info *desc_info)
2014 struct rtw89_ppdu_sts_info *ppdu_sts = &rtwdev->ppdu_sts;
2015 u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
2016 struct ieee80211_rx_status *rx_status;
2017 struct sk_buff *skb_ppdu, *tmp;
2019 skb_queue_walk_safe(&ppdu_sts->rx_queue[band], skb_ppdu, tmp) {
2020 skb_unlink(skb_ppdu, &ppdu_sts->rx_queue[band]);
2021 rx_status = IEEE80211_SKB_RXCB(skb_ppdu);
2022 rtw89_core_rx_to_mac80211(rtwdev, NULL, desc_info, skb_ppdu, rx_status);
2026 void rtw89_core_rx(struct rtw89_dev *rtwdev,
2027 struct rtw89_rx_desc_info *desc_info,
2028 struct sk_buff *skb)
2030 struct ieee80211_rx_status *rx_status;
2031 struct rtw89_ppdu_sts_info *ppdu_sts = &rtwdev->ppdu_sts;
2032 u8 ppdu_cnt = desc_info->ppdu_cnt;
2033 u8 band = desc_info->bb_sel ? RTW89_PHY_1 : RTW89_PHY_0;
2035 if (desc_info->pkt_type != RTW89_CORE_RX_TYPE_WIFI) {
2036 rtw89_core_rx_process_report(rtwdev, desc_info, skb);
2040 if (ppdu_sts->curr_rx_ppdu_cnt[band] != ppdu_cnt) {
2041 rtw89_core_flush_ppdu_rx_queue(rtwdev, desc_info);
2042 ppdu_sts->curr_rx_ppdu_cnt[band] = ppdu_cnt;
2045 rx_status = IEEE80211_SKB_RXCB(skb);
2046 memset(rx_status, 0, sizeof(*rx_status));
2047 rtw89_core_update_rx_status(rtwdev, desc_info, rx_status);
2048 if (desc_info->long_rxdesc &&
2049 BIT(desc_info->frame_type) & PPDU_FILTER_BITMAP)
2050 skb_queue_tail(&ppdu_sts->rx_queue[band], skb);
2052 rtw89_core_rx_to_mac80211(rtwdev, NULL, desc_info, skb, rx_status);
2054 EXPORT_SYMBOL(rtw89_core_rx);
2056 void rtw89_core_napi_start(struct rtw89_dev *rtwdev)
2058 if (test_and_set_bit(RTW89_FLAG_NAPI_RUNNING, rtwdev->flags))
2061 napi_enable(&rtwdev->napi);
2063 EXPORT_SYMBOL(rtw89_core_napi_start);
2065 void rtw89_core_napi_stop(struct rtw89_dev *rtwdev)
2067 if (!test_and_clear_bit(RTW89_FLAG_NAPI_RUNNING, rtwdev->flags))
2070 napi_synchronize(&rtwdev->napi);
2071 napi_disable(&rtwdev->napi);
2073 EXPORT_SYMBOL(rtw89_core_napi_stop);
2075 void rtw89_core_napi_init(struct rtw89_dev *rtwdev)
2077 init_dummy_netdev(&rtwdev->netdev);
2078 netif_napi_add(&rtwdev->netdev, &rtwdev->napi,
2079 rtwdev->hci.ops->napi_poll);
2081 EXPORT_SYMBOL(rtw89_core_napi_init);
2083 void rtw89_core_napi_deinit(struct rtw89_dev *rtwdev)
2085 rtw89_core_napi_stop(rtwdev);
2086 netif_napi_del(&rtwdev->napi);
2088 EXPORT_SYMBOL(rtw89_core_napi_deinit);
2090 static void rtw89_core_ba_work(struct work_struct *work)
2092 struct rtw89_dev *rtwdev =
2093 container_of(work, struct rtw89_dev, ba_work);
2094 struct rtw89_txq *rtwtxq, *tmp;
2097 spin_lock_bh(&rtwdev->ba_lock);
2098 list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->ba_list, list) {
2099 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2100 struct ieee80211_sta *sta = txq->sta;
2101 struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL;
2105 rtw89_warn(rtwdev, "cannot start BA without sta\n");
2109 if (rtwsta->disassoc) {
2110 rtw89_debug(rtwdev, RTW89_DBG_TXRX,
2111 "cannot start BA with disassoc sta\n");
2115 ret = ieee80211_start_tx_ba_session(sta, tid, 0);
2117 rtw89_debug(rtwdev, RTW89_DBG_TXRX,
2118 "failed to setup BA session for %pM:%2d: %d\n",
2119 sta->addr, tid, ret);
2121 set_bit(RTW89_TXQ_F_BLOCK_BA, &rtwtxq->flags);
2124 list_del_init(&rtwtxq->list);
2126 spin_unlock_bh(&rtwdev->ba_lock);
2129 static void rtw89_core_free_sta_pending_ba(struct rtw89_dev *rtwdev,
2130 struct ieee80211_sta *sta)
2132 struct rtw89_txq *rtwtxq, *tmp;
2134 spin_lock_bh(&rtwdev->ba_lock);
2135 list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->ba_list, list) {
2136 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2138 if (sta == txq->sta)
2139 list_del_init(&rtwtxq->list);
2141 spin_unlock_bh(&rtwdev->ba_lock);
2144 static void rtw89_core_free_sta_pending_forbid_ba(struct rtw89_dev *rtwdev,
2145 struct ieee80211_sta *sta)
2147 struct rtw89_txq *rtwtxq, *tmp;
2149 spin_lock_bh(&rtwdev->ba_lock);
2150 list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->forbid_ba_list, list) {
2151 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2153 if (sta == txq->sta) {
2154 clear_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
2155 list_del_init(&rtwtxq->list);
2158 spin_unlock_bh(&rtwdev->ba_lock);
2161 static void rtw89_core_free_sta_pending_roc_tx(struct rtw89_dev *rtwdev,
2162 struct ieee80211_sta *sta)
2164 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2165 struct sk_buff *skb, *tmp;
2167 skb_queue_walk_safe(&rtwsta->roc_queue, skb, tmp) {
2168 skb_unlink(skb, &rtwsta->roc_queue);
2169 dev_kfree_skb_any(skb);
2173 static void rtw89_core_stop_tx_ba_session(struct rtw89_dev *rtwdev,
2174 struct rtw89_txq *rtwtxq)
2176 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2177 struct ieee80211_sta *sta = txq->sta;
2178 struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
2180 if (unlikely(!rtwsta) || unlikely(rtwsta->disassoc))
2183 if (!test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags) ||
2184 test_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags))
2187 spin_lock_bh(&rtwdev->ba_lock);
2188 if (!test_and_set_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags))
2189 list_add_tail(&rtwtxq->list, &rtwdev->forbid_ba_list);
2190 spin_unlock_bh(&rtwdev->ba_lock);
2192 ieee80211_stop_tx_ba_session(sta, txq->tid);
2193 cancel_delayed_work(&rtwdev->forbid_ba_work);
2194 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->forbid_ba_work,
2195 RTW89_FORBID_BA_TIMER);
2198 static void rtw89_core_txq_check_agg(struct rtw89_dev *rtwdev,
2199 struct rtw89_txq *rtwtxq,
2200 struct sk_buff *skb)
2202 struct ieee80211_hw *hw = rtwdev->hw;
2203 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2204 struct ieee80211_sta *sta = txq->sta;
2205 struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL;
2207 if (test_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags))
2210 if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
2211 rtw89_core_stop_tx_ba_session(rtwdev, rtwtxq);
2218 if (unlikely(test_bit(RTW89_TXQ_F_BLOCK_BA, &rtwtxq->flags)))
2221 if (test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags)) {
2222 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_AMPDU;
2226 spin_lock_bh(&rtwdev->ba_lock);
2227 if (!rtwsta->disassoc && list_empty(&rtwtxq->list)) {
2228 list_add_tail(&rtwtxq->list, &rtwdev->ba_list);
2229 ieee80211_queue_work(hw, &rtwdev->ba_work);
2231 spin_unlock_bh(&rtwdev->ba_lock);
2234 static void rtw89_core_txq_push(struct rtw89_dev *rtwdev,
2235 struct rtw89_txq *rtwtxq,
2236 unsigned long frame_cnt,
2237 unsigned long byte_cnt)
2239 struct ieee80211_txq *txq = rtw89_txq_to_txq(rtwtxq);
2240 struct ieee80211_vif *vif = txq->vif;
2241 struct ieee80211_sta *sta = txq->sta;
2242 struct sk_buff *skb;
2247 for (i = 0; i < frame_cnt; i++) {
2248 skb = ieee80211_tx_dequeue_ni(rtwdev->hw, txq);
2250 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "dequeue a NULL skb\n");
2253 rtw89_core_txq_check_agg(rtwdev, rtwtxq, skb);
2254 ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, NULL);
2256 rtw89_err(rtwdev, "failed to push txq: %d\n", ret);
2257 ieee80211_free_txskb(rtwdev->hw, skb);
2265 static u32 rtw89_check_and_reclaim_tx_resource(struct rtw89_dev *rtwdev, u8 tid)
2269 qsel = rtw89_core_get_qsel(rtwdev, tid);
2270 ch_dma = rtw89_core_get_ch_dma(rtwdev, qsel);
2272 return rtw89_hci_check_and_reclaim_tx_resource(rtwdev, ch_dma);
2275 static bool rtw89_core_txq_agg_wait(struct rtw89_dev *rtwdev,
2276 struct ieee80211_txq *txq,
2277 unsigned long *frame_cnt,
2278 bool *sched_txq, bool *reinvoke)
2280 struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv;
2281 struct ieee80211_sta *sta = txq->sta;
2282 struct rtw89_sta *rtwsta = sta ? (struct rtw89_sta *)sta->drv_priv : NULL;
2284 if (!sta || rtwsta->max_agg_wait <= 0)
2287 if (rtwdev->stats.tx_tfc_lv <= RTW89_TFC_MID)
2290 if (*frame_cnt > 1) {
2294 rtwtxq->wait_cnt = 1;
2298 if (*frame_cnt == 1 && rtwtxq->wait_cnt < rtwsta->max_agg_wait) {
2304 rtwtxq->wait_cnt = 0;
2308 static void rtw89_core_txq_schedule(struct rtw89_dev *rtwdev, u8 ac, bool *reinvoke)
2310 struct ieee80211_hw *hw = rtwdev->hw;
2311 struct ieee80211_txq *txq;
2312 struct rtw89_vif *rtwvif;
2313 struct rtw89_txq *rtwtxq;
2314 unsigned long frame_cnt;
2315 unsigned long byte_cnt;
2319 ieee80211_txq_schedule_start(hw, ac);
2320 while ((txq = ieee80211_next_txq(hw, ac))) {
2321 rtwtxq = (struct rtw89_txq *)txq->drv_priv;
2322 rtwvif = (struct rtw89_vif *)txq->vif->drv_priv;
2324 if (rtwvif->offchan) {
2325 ieee80211_return_txq(hw, txq, true);
2328 tx_resource = rtw89_check_and_reclaim_tx_resource(rtwdev, txq->tid);
2331 ieee80211_txq_get_depth(txq, &frame_cnt, &byte_cnt);
2332 if (rtw89_core_txq_agg_wait(rtwdev, txq, &frame_cnt, &sched_txq, reinvoke)) {
2333 ieee80211_return_txq(hw, txq, true);
2336 frame_cnt = min_t(unsigned long, frame_cnt, tx_resource);
2337 rtw89_core_txq_push(rtwdev, rtwtxq, frame_cnt, byte_cnt);
2338 ieee80211_return_txq(hw, txq, sched_txq);
2340 rtw89_core_tx_kick_off(rtwdev, rtw89_core_get_qsel(rtwdev, txq->tid));
2342 /* bound of tx_resource could get stuck due to burst traffic */
2343 if (frame_cnt == tx_resource)
2346 ieee80211_txq_schedule_end(hw, ac);
2349 static void rtw89_ips_work(struct work_struct *work)
2351 struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
2353 mutex_lock(&rtwdev->mutex);
2354 rtw89_enter_ips_by_hwflags(rtwdev);
2355 mutex_unlock(&rtwdev->mutex);
2358 static void rtw89_core_txq_work(struct work_struct *w)
2360 struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev, txq_work);
2361 bool reinvoke = false;
2364 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2365 rtw89_core_txq_schedule(rtwdev, ac, &reinvoke);
2368 /* reinvoke to process the last frame */
2369 mod_delayed_work(rtwdev->txq_wq, &rtwdev->txq_reinvoke_work, 1);
2373 static void rtw89_core_txq_reinvoke_work(struct work_struct *w)
2375 struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev,
2376 txq_reinvoke_work.work);
2378 queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
2381 static void rtw89_forbid_ba_work(struct work_struct *w)
2383 struct rtw89_dev *rtwdev = container_of(w, struct rtw89_dev,
2384 forbid_ba_work.work);
2385 struct rtw89_txq *rtwtxq, *tmp;
2387 spin_lock_bh(&rtwdev->ba_lock);
2388 list_for_each_entry_safe(rtwtxq, tmp, &rtwdev->forbid_ba_list, list) {
2389 clear_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
2390 list_del_init(&rtwtxq->list);
2392 spin_unlock_bh(&rtwdev->ba_lock);
2395 static void rtw89_core_sta_pending_tx_iter(void *data,
2396 struct ieee80211_sta *sta)
2398 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2399 struct rtw89_vif *rtwvif_target = data, *rtwvif = rtwsta->rtwvif;
2400 struct rtw89_dev *rtwdev = rtwvif->rtwdev;
2401 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
2402 struct sk_buff *skb, *tmp;
2405 if (rtwvif->sub_entity_idx != rtwvif_target->sub_entity_idx)
2408 if (skb_queue_len(&rtwsta->roc_queue) == 0)
2411 skb_queue_walk_safe(&rtwsta->roc_queue, skb, tmp) {
2412 skb_unlink(skb, &rtwsta->roc_queue);
2414 ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel);
2416 rtw89_warn(rtwdev, "pending tx failed with %d\n", ret);
2417 dev_kfree_skb_any(skb);
2419 rtw89_core_tx_kick_off(rtwdev, qsel);
2424 static void rtw89_core_handle_sta_pending_tx(struct rtw89_dev *rtwdev,
2425 struct rtw89_vif *rtwvif)
2427 ieee80211_iterate_stations_atomic(rtwdev->hw,
2428 rtw89_core_sta_pending_tx_iter,
2432 static int rtw89_core_send_nullfunc(struct rtw89_dev *rtwdev,
2433 struct rtw89_vif *rtwvif, bool qos, bool ps)
2435 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
2436 struct ieee80211_sta *sta;
2437 struct ieee80211_hdr *hdr;
2438 struct sk_buff *skb;
2441 if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
2445 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
2451 skb = ieee80211_nullfunc_get(rtwdev->hw, vif, -1, qos);
2457 hdr = (struct ieee80211_hdr *)skb->data;
2459 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
2461 ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel);
2463 rtw89_warn(rtwdev, "nullfunc transmit failed: %d\n", ret);
2464 dev_kfree_skb_any(skb);
2470 return rtw89_core_tx_kick_off_and_wait(rtwdev, skb, qsel,
2471 RTW89_ROC_TX_TIMEOUT);
2478 void rtw89_roc_start(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
2480 const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
2481 struct ieee80211_hw *hw = rtwdev->hw;
2482 struct rtw89_roc *roc = &rtwvif->roc;
2483 struct cfg80211_chan_def roc_chan;
2484 struct rtw89_vif *tmp;
2487 lockdep_assert_held(&rtwdev->mutex);
2489 ieee80211_queue_delayed_work(hw, &rtwvif->roc.roc_work,
2490 msecs_to_jiffies(rtwvif->roc.duration));
2492 rtw89_leave_ips_by_hwflags(rtwdev);
2493 rtw89_leave_lps(rtwdev);
2495 ret = rtw89_core_send_nullfunc(rtwdev, rtwvif, true, true);
2497 rtw89_debug(rtwdev, RTW89_DBG_TXRX,
2498 "roc send null-1 failed: %d\n", ret);
2500 rtw89_for_each_rtwvif(rtwdev, tmp)
2501 if (tmp->sub_entity_idx == rtwvif->sub_entity_idx)
2502 tmp->offchan = true;
2504 cfg80211_chandef_create(&roc_chan, &roc->chan, NL80211_CHAN_NO_HT);
2505 rtw89_config_roc_chandef(rtwdev, rtwvif->sub_entity_idx, &roc_chan);
2506 rtw89_set_channel(rtwdev);
2507 rtw89_write32_clr(rtwdev,
2508 rtw89_mac_reg_by_idx(rtwdev, mac->rx_fltr, RTW89_MAC_0),
2509 B_AX_A_UC_CAM_MATCH | B_AX_A_BC_CAM_MATCH);
2511 ieee80211_ready_on_channel(hw);
2514 void rtw89_roc_end(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
2516 const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
2517 struct ieee80211_hw *hw = rtwdev->hw;
2518 struct rtw89_roc *roc = &rtwvif->roc;
2519 struct rtw89_vif *tmp;
2522 lockdep_assert_held(&rtwdev->mutex);
2524 ieee80211_remain_on_channel_expired(hw);
2526 rtw89_leave_ips_by_hwflags(rtwdev);
2527 rtw89_leave_lps(rtwdev);
2529 rtw89_write32_mask(rtwdev,
2530 rtw89_mac_reg_by_idx(rtwdev, mac->rx_fltr, RTW89_MAC_0),
2531 B_AX_RX_FLTR_CFG_MASK,
2532 rtwdev->hal.rx_fltr);
2534 roc->state = RTW89_ROC_IDLE;
2535 rtw89_config_roc_chandef(rtwdev, rtwvif->sub_entity_idx, NULL);
2536 rtw89_set_channel(rtwdev);
2537 ret = rtw89_core_send_nullfunc(rtwdev, rtwvif, true, false);
2539 rtw89_debug(rtwdev, RTW89_DBG_TXRX,
2540 "roc send null-0 failed: %d\n", ret);
2542 rtw89_for_each_rtwvif(rtwdev, tmp)
2543 if (tmp->sub_entity_idx == rtwvif->sub_entity_idx)
2544 tmp->offchan = false;
2546 rtw89_core_handle_sta_pending_tx(rtwdev, rtwvif);
2547 queue_work(rtwdev->txq_wq, &rtwdev->txq_work);
2549 if (hw->conf.flags & IEEE80211_CONF_IDLE)
2550 ieee80211_queue_delayed_work(hw, &roc->roc_work,
2551 RTW89_ROC_IDLE_TIMEOUT);
2554 void rtw89_roc_work(struct work_struct *work)
2556 struct rtw89_vif *rtwvif = container_of(work, struct rtw89_vif,
2558 struct rtw89_dev *rtwdev = rtwvif->rtwdev;
2559 struct rtw89_roc *roc = &rtwvif->roc;
2561 mutex_lock(&rtwdev->mutex);
2563 switch (roc->state) {
2564 case RTW89_ROC_IDLE:
2565 rtw89_enter_ips_by_hwflags(rtwdev);
2567 case RTW89_ROC_MGMT:
2568 case RTW89_ROC_NORMAL:
2569 rtw89_roc_end(rtwdev, rtwvif);
2575 mutex_unlock(&rtwdev->mutex);
2578 static enum rtw89_tfc_lv rtw89_get_traffic_level(struct rtw89_dev *rtwdev,
2579 u32 throughput, u64 cnt)
2582 return RTW89_TFC_IDLE;
2583 if (throughput > 50)
2584 return RTW89_TFC_HIGH;
2585 if (throughput > 10)
2586 return RTW89_TFC_MID;
2588 return RTW89_TFC_LOW;
2589 return RTW89_TFC_ULTRA_LOW;
2592 static bool rtw89_traffic_stats_calc(struct rtw89_dev *rtwdev,
2593 struct rtw89_traffic_stats *stats)
2595 enum rtw89_tfc_lv tx_tfc_lv = stats->tx_tfc_lv;
2596 enum rtw89_tfc_lv rx_tfc_lv = stats->rx_tfc_lv;
2598 stats->tx_throughput_raw = (u32)(stats->tx_unicast >> RTW89_TP_SHIFT);
2599 stats->rx_throughput_raw = (u32)(stats->rx_unicast >> RTW89_TP_SHIFT);
2601 ewma_tp_add(&stats->tx_ewma_tp, stats->tx_throughput_raw);
2602 ewma_tp_add(&stats->rx_ewma_tp, stats->rx_throughput_raw);
2604 stats->tx_throughput = ewma_tp_read(&stats->tx_ewma_tp);
2605 stats->rx_throughput = ewma_tp_read(&stats->rx_ewma_tp);
2606 stats->tx_tfc_lv = rtw89_get_traffic_level(rtwdev, stats->tx_throughput,
2608 stats->rx_tfc_lv = rtw89_get_traffic_level(rtwdev, stats->rx_throughput,
2610 stats->tx_avg_len = stats->tx_cnt ?
2611 DIV_ROUND_DOWN_ULL(stats->tx_unicast, stats->tx_cnt) : 0;
2612 stats->rx_avg_len = stats->rx_cnt ?
2613 DIV_ROUND_DOWN_ULL(stats->rx_unicast, stats->rx_cnt) : 0;
2615 stats->tx_unicast = 0;
2616 stats->rx_unicast = 0;
2619 stats->rx_tf_periodic = stats->rx_tf_acc;
2620 stats->rx_tf_acc = 0;
2622 if (tx_tfc_lv != stats->tx_tfc_lv || rx_tfc_lv != stats->rx_tfc_lv)
2628 static bool rtw89_traffic_stats_track(struct rtw89_dev *rtwdev)
2630 struct rtw89_vif *rtwvif;
2633 tfc_changed = rtw89_traffic_stats_calc(rtwdev, &rtwdev->stats);
2634 rtw89_for_each_rtwvif(rtwdev, rtwvif) {
2635 rtw89_traffic_stats_calc(rtwdev, &rtwvif->stats);
2636 rtw89_fw_h2c_tp_offload(rtwdev, rtwvif);
2642 static void rtw89_vif_enter_lps(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
2644 if ((rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION &&
2645 rtwvif->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT) ||
2649 if (rtwvif->offchan)
2652 if (rtwvif->stats.tx_tfc_lv == RTW89_TFC_IDLE &&
2653 rtwvif->stats.rx_tfc_lv == RTW89_TFC_IDLE)
2654 rtw89_enter_lps(rtwdev, rtwvif, true);
2657 static void rtw89_enter_lps_track(struct rtw89_dev *rtwdev)
2659 struct rtw89_vif *rtwvif;
2661 rtw89_for_each_rtwvif(rtwdev, rtwvif)
2662 rtw89_vif_enter_lps(rtwdev, rtwvif);
2665 void rtw89_traffic_stats_init(struct rtw89_dev *rtwdev,
2666 struct rtw89_traffic_stats *stats)
2668 stats->tx_unicast = 0;
2669 stats->rx_unicast = 0;
2672 ewma_tp_init(&stats->tx_ewma_tp);
2673 ewma_tp_init(&stats->rx_ewma_tp);
2676 static void rtw89_track_work(struct work_struct *work)
2678 struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
2682 if (test_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags))
2685 mutex_lock(&rtwdev->mutex);
2687 if (!test_bit(RTW89_FLAG_RUNNING, rtwdev->flags))
2690 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work,
2691 RTW89_TRACK_WORK_PERIOD);
2693 tfc_changed = rtw89_traffic_stats_track(rtwdev);
2694 if (rtwdev->scanning)
2697 rtw89_leave_lps(rtwdev);
2700 rtw89_hci_recalc_int_mit(rtwdev);
2701 rtw89_btc_ntfy_wl_sta(rtwdev);
2703 rtw89_mac_bf_monitor_track(rtwdev);
2704 rtw89_phy_stat_track(rtwdev);
2705 rtw89_phy_env_monitor_track(rtwdev);
2706 rtw89_phy_dig(rtwdev);
2707 rtw89_chip_rfk_track(rtwdev);
2708 rtw89_phy_ra_update(rtwdev);
2709 rtw89_phy_cfo_track(rtwdev);
2710 rtw89_phy_tx_path_div_track(rtwdev);
2711 rtw89_phy_antdiv_track(rtwdev);
2712 rtw89_phy_ul_tb_ctrl_track(rtwdev);
2713 rtw89_tas_track(rtwdev);
2715 if (rtwdev->lps_enabled && !rtwdev->btc.lps)
2716 rtw89_enter_lps_track(rtwdev);
2719 mutex_unlock(&rtwdev->mutex);
2722 u8 rtw89_core_acquire_bit_map(unsigned long *addr, unsigned long size)
2726 bit = find_first_zero_bit(addr, size);
2733 void rtw89_core_release_bit_map(unsigned long *addr, u8 bit)
2735 clear_bit(bit, addr);
2738 void rtw89_core_release_all_bits_map(unsigned long *addr, unsigned int nbits)
2740 bitmap_zero(addr, nbits);
2743 int rtw89_core_acquire_sta_ba_entry(struct rtw89_dev *rtwdev,
2744 struct rtw89_sta *rtwsta, u8 tid, u8 *cam_idx)
2746 const struct rtw89_chip_info *chip = rtwdev->chip;
2747 struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
2748 struct rtw89_ba_cam_entry *entry = NULL, *tmp;
2752 lockdep_assert_held(&rtwdev->mutex);
2754 idx = rtw89_core_acquire_bit_map(cam_info->ba_cam_map, chip->bacam_num);
2755 if (idx == chip->bacam_num) {
2756 /* allocate a static BA CAM to tid=0/5, so replace the existing
2757 * one if BA CAM is full. Hardware will process the original tid
2760 if (tid != 0 && tid != 5)
2763 for_each_set_bit(i, cam_info->ba_cam_map, chip->bacam_num) {
2764 tmp = &cam_info->ba_cam_entry[i];
2765 if (tmp->tid == 0 || tmp->tid == 5)
2770 list_del(&entry->list);
2777 entry = &cam_info->ba_cam_entry[idx];
2781 list_add_tail(&entry->list, &rtwsta->ba_cam_list);
2788 int rtw89_core_release_sta_ba_entry(struct rtw89_dev *rtwdev,
2789 struct rtw89_sta *rtwsta, u8 tid, u8 *cam_idx)
2791 struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
2792 struct rtw89_ba_cam_entry *entry = NULL, *tmp;
2795 lockdep_assert_held(&rtwdev->mutex);
2797 list_for_each_entry_safe(entry, tmp, &rtwsta->ba_cam_list, list) {
2798 if (entry->tid != tid)
2801 idx = entry - cam_info->ba_cam_entry;
2802 list_del(&entry->list);
2804 rtw89_core_release_bit_map(cam_info->ba_cam_map, idx);
2812 #define RTW89_TYPE_MAPPING(_type) \
2813 case NL80211_IFTYPE_ ## _type: \
2814 rtwvif->wifi_role = RTW89_WIFI_ROLE_ ## _type; \
2816 void rtw89_vif_type_mapping(struct ieee80211_vif *vif, bool assoc)
2818 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2820 switch (vif->type) {
2821 case NL80211_IFTYPE_STATION:
2823 rtwvif->wifi_role = RTW89_WIFI_ROLE_P2P_CLIENT;
2825 rtwvif->wifi_role = RTW89_WIFI_ROLE_STATION;
2827 case NL80211_IFTYPE_AP:
2829 rtwvif->wifi_role = RTW89_WIFI_ROLE_P2P_GO;
2831 rtwvif->wifi_role = RTW89_WIFI_ROLE_AP;
2833 RTW89_TYPE_MAPPING(ADHOC);
2834 RTW89_TYPE_MAPPING(MONITOR);
2835 RTW89_TYPE_MAPPING(MESH_POINT);
2841 switch (vif->type) {
2842 case NL80211_IFTYPE_AP:
2843 case NL80211_IFTYPE_MESH_POINT:
2844 rtwvif->net_type = RTW89_NET_TYPE_AP_MODE;
2845 rtwvif->self_role = RTW89_SELF_ROLE_AP;
2847 case NL80211_IFTYPE_ADHOC:
2848 rtwvif->net_type = RTW89_NET_TYPE_AD_HOC;
2849 rtwvif->self_role = RTW89_SELF_ROLE_CLIENT;
2851 case NL80211_IFTYPE_STATION:
2853 rtwvif->net_type = RTW89_NET_TYPE_INFRA;
2854 rtwvif->trigger = vif->bss_conf.he_support;
2856 rtwvif->net_type = RTW89_NET_TYPE_NO_LINK;
2857 rtwvif->trigger = false;
2859 rtwvif->self_role = RTW89_SELF_ROLE_CLIENT;
2860 rtwvif->addr_cam.sec_ent_mode = RTW89_ADDR_CAM_SEC_NORMAL;
2862 case NL80211_IFTYPE_MONITOR:
2870 int rtw89_core_sta_add(struct rtw89_dev *rtwdev,
2871 struct ieee80211_vif *vif,
2872 struct ieee80211_sta *sta)
2874 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2875 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2876 struct rtw89_hal *hal = &rtwdev->hal;
2877 u8 ant_num = hal->ant_diversity ? 2 : rtwdev->chip->rf_path_num;
2881 rtwsta->rtwdev = rtwdev;
2882 rtwsta->rtwvif = rtwvif;
2883 rtwsta->prev_rssi = 0;
2884 INIT_LIST_HEAD(&rtwsta->ba_cam_list);
2885 skb_queue_head_init(&rtwsta->roc_queue);
2887 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
2888 rtw89_core_txq_init(rtwdev, sta->txq[i]);
2890 ewma_rssi_init(&rtwsta->avg_rssi);
2891 ewma_snr_init(&rtwsta->avg_snr);
2892 for (i = 0; i < ant_num; i++) {
2893 ewma_rssi_init(&rtwsta->rssi[i]);
2894 ewma_evm_init(&rtwsta->evm_min[i]);
2895 ewma_evm_init(&rtwsta->evm_max[i]);
2898 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
2899 /* for station mode, assign the mac_id from itself */
2900 rtwsta->mac_id = rtwvif->mac_id;
2901 /* must do rtw89_reg_6ghz_power_recalc() before rfk channel */
2902 rtw89_reg_6ghz_power_recalc(rtwdev, rtwvif, true);
2903 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta,
2904 BTC_ROLE_MSTS_STA_CONN_START);
2905 rtw89_chip_rfk_channel(rtwdev);
2906 } else if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
2907 rtwsta->mac_id = rtw89_core_acquire_bit_map(rtwdev->mac_id_map,
2908 RTW89_MAX_MAC_ID_NUM);
2909 if (rtwsta->mac_id == RTW89_MAX_MAC_ID_NUM)
2912 ret = rtw89_mac_set_macid_pause(rtwdev, rtwsta->mac_id, false);
2914 rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwsta->mac_id);
2915 rtw89_warn(rtwdev, "failed to send h2c macid pause\n");
2919 ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta,
2922 rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwsta->mac_id);
2923 rtw89_warn(rtwdev, "failed to send h2c role info\n");
2931 int rtw89_core_sta_disassoc(struct rtw89_dev *rtwdev,
2932 struct ieee80211_vif *vif,
2933 struct ieee80211_sta *sta)
2935 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2936 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2938 if (vif->type == NL80211_IFTYPE_STATION)
2939 rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, vif, false);
2941 rtwdev->total_sta_assoc--;
2943 rtwvif->tdls_peer--;
2944 rtwsta->disassoc = true;
2949 int rtw89_core_sta_disconnect(struct rtw89_dev *rtwdev,
2950 struct ieee80211_vif *vif,
2951 struct ieee80211_sta *sta)
2953 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
2954 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2957 rtw89_mac_bf_monitor_calc(rtwdev, sta, true);
2958 rtw89_mac_bf_disassoc(rtwdev, vif, sta);
2959 rtw89_core_free_sta_pending_ba(rtwdev, sta);
2960 rtw89_core_free_sta_pending_forbid_ba(rtwdev, sta);
2961 rtw89_core_free_sta_pending_roc_tx(rtwdev, sta);
2963 if (vif->type == NL80211_IFTYPE_AP || sta->tdls)
2964 rtw89_cam_deinit_addr_cam(rtwdev, &rtwsta->addr_cam);
2966 rtw89_cam_deinit_bssid_cam(rtwdev, &rtwsta->bssid_cam);
2968 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
2969 rtw89_vif_type_mapping(vif, false);
2970 rtw89_fw_release_general_pkt_list_vif(rtwdev, rtwvif, true);
2973 ret = rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, sta);
2975 rtw89_warn(rtwdev, "failed to send h2c cmac table\n");
2979 ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, true);
2981 rtw89_warn(rtwdev, "failed to send h2c join info\n");
2985 /* update cam aid mac_id net_type */
2986 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
2988 rtw89_warn(rtwdev, "failed to send h2c cam\n");
2995 int rtw89_core_sta_assoc(struct rtw89_dev *rtwdev,
2996 struct ieee80211_vif *vif,
2997 struct ieee80211_sta *sta)
2999 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
3000 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
3001 struct rtw89_bssid_cam_entry *bssid_cam = rtw89_get_bssid_cam_of(rtwvif, rtwsta);
3002 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
3003 rtwvif->sub_entity_idx);
3006 if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
3008 ret = rtw89_cam_init_bssid_cam(rtwdev, rtwvif, bssid_cam, sta->addr);
3010 rtw89_warn(rtwdev, "failed to send h2c init bssid cam for TDLS\n");
3015 ret = rtw89_cam_init_addr_cam(rtwdev, &rtwsta->addr_cam, bssid_cam);
3017 rtw89_warn(rtwdev, "failed to send h2c init addr cam\n");
3022 ret = rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, sta);
3024 rtw89_warn(rtwdev, "failed to send h2c cmac table\n");
3028 ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, false);
3030 rtw89_warn(rtwdev, "failed to send h2c join info\n");
3034 /* update cam aid mac_id net_type */
3035 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
3037 rtw89_warn(rtwdev, "failed to send h2c cam\n");
3041 rtwdev->total_sta_assoc++;
3043 rtwvif->tdls_peer++;
3044 rtw89_phy_ra_assoc(rtwdev, sta);
3045 rtw89_mac_bf_assoc(rtwdev, vif, sta);
3046 rtw89_mac_bf_monitor_calc(rtwdev, sta, false);
3048 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3049 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
3051 if (bss_conf->he_support &&
3052 !(bss_conf->he_oper.params & IEEE80211_HE_OPERATION_ER_SU_DISABLE))
3053 rtwsta->er_cap = true;
3055 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta,
3056 BTC_ROLE_MSTS_STA_CONN_END);
3057 rtw89_core_get_no_ul_ofdma_htc(rtwdev, &rtwsta->htc_template, chan);
3058 rtw89_phy_ul_tb_assoc(rtwdev, rtwvif);
3060 ret = rtw89_fw_h2c_general_pkt(rtwdev, rtwvif, rtwsta->mac_id);
3062 rtw89_warn(rtwdev, "failed to send h2c general packet\n");
3070 int rtw89_core_sta_remove(struct rtw89_dev *rtwdev,
3071 struct ieee80211_vif *vif,
3072 struct ieee80211_sta *sta)
3074 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
3075 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
3078 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3079 rtw89_reg_6ghz_power_recalc(rtwdev, rtwvif, false);
3080 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, rtwsta,
3081 BTC_ROLE_MSTS_STA_DIS_CONN);
3082 } else if (vif->type == NL80211_IFTYPE_AP || sta->tdls) {
3083 rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwsta->mac_id);
3085 ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta,
3088 rtw89_warn(rtwdev, "failed to send h2c role info\n");
3096 static void _rtw89_core_set_tid_config(struct rtw89_dev *rtwdev,
3097 struct ieee80211_sta *sta,
3098 struct cfg80211_tid_cfg *tid_conf)
3100 struct ieee80211_txq *txq;
3101 struct rtw89_txq *rtwtxq;
3102 u32 mask = tid_conf->mask;
3103 u8 tids = tid_conf->tids;
3104 int tids_nbit = BITS_PER_BYTE;
3107 for (i = 0; i < tids_nbit; i++, tids >>= 1) {
3111 if (!(tids & BIT(0)))
3115 rtwtxq = (struct rtw89_txq *)txq->drv_priv;
3117 if (mask & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
3118 if (tid_conf->ampdu == NL80211_TID_CONFIG_ENABLE) {
3119 clear_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
3121 if (test_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags))
3122 ieee80211_stop_tx_ba_session(sta, txq->tid);
3123 spin_lock_bh(&rtwdev->ba_lock);
3124 list_del_init(&rtwtxq->list);
3125 set_bit(RTW89_TXQ_F_FORBID_BA, &rtwtxq->flags);
3126 spin_unlock_bh(&rtwdev->ba_lock);
3130 if (mask & BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL) && tids == 0xff) {
3131 if (tid_conf->amsdu == NL80211_TID_CONFIG_ENABLE)
3132 sta->max_amsdu_subframes = 0;
3134 sta->max_amsdu_subframes = 1;
3139 void rtw89_core_set_tid_config(struct rtw89_dev *rtwdev,
3140 struct ieee80211_sta *sta,
3141 struct cfg80211_tid_config *tid_config)
3145 for (i = 0; i < tid_config->n_tid_conf; i++)
3146 _rtw89_core_set_tid_config(rtwdev, sta,
3147 &tid_config->tid_conf[i]);
3150 static void rtw89_init_ht_cap(struct rtw89_dev *rtwdev,
3151 struct ieee80211_sta_ht_cap *ht_cap)
3153 static const __le16 highest[RF_PATH_MAX] = {
3154 cpu_to_le16(150), cpu_to_le16(300), cpu_to_le16(450), cpu_to_le16(600),
3156 struct rtw89_hal *hal = &rtwdev->hal;
3157 u8 nss = hal->rx_nss;
3160 ht_cap->ht_supported = true;
3162 ht_cap->cap |= IEEE80211_HT_CAP_SGI_20 |
3163 IEEE80211_HT_CAP_MAX_AMSDU |
3164 IEEE80211_HT_CAP_TX_STBC |
3165 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
3166 ht_cap->cap |= IEEE80211_HT_CAP_LDPC_CODING;
3167 ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3168 IEEE80211_HT_CAP_DSSSCCK40 |
3169 IEEE80211_HT_CAP_SGI_40;
3170 ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3171 ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
3172 ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3173 for (i = 0; i < nss; i++)
3174 ht_cap->mcs.rx_mask[i] = 0xFF;
3175 ht_cap->mcs.rx_mask[4] = 0x01;
3176 ht_cap->mcs.rx_highest = highest[nss - 1];
3179 static void rtw89_init_vht_cap(struct rtw89_dev *rtwdev,
3180 struct ieee80211_sta_vht_cap *vht_cap)
3182 static const __le16 highest_bw80[RF_PATH_MAX] = {
3183 cpu_to_le16(433), cpu_to_le16(867), cpu_to_le16(1300), cpu_to_le16(1733),
3185 static const __le16 highest_bw160[RF_PATH_MAX] = {
3186 cpu_to_le16(867), cpu_to_le16(1733), cpu_to_le16(2600), cpu_to_le16(3467),
3188 const struct rtw89_chip_info *chip = rtwdev->chip;
3189 const __le16 *highest = chip->support_bw160 ? highest_bw160 : highest_bw80;
3190 struct rtw89_hal *hal = &rtwdev->hal;
3191 u16 tx_mcs_map = 0, rx_mcs_map = 0;
3195 for (i = 0; i < 8; i++) {
3196 if (i < hal->tx_nss)
3197 tx_mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3199 tx_mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3200 if (i < hal->rx_nss)
3201 rx_mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3203 rx_mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3206 vht_cap->vht_supported = true;
3207 vht_cap->cap = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3208 IEEE80211_VHT_CAP_SHORT_GI_80 |
3209 IEEE80211_VHT_CAP_RXSTBC_1 |
3210 IEEE80211_VHT_CAP_HTC_VHT |
3211 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
3213 vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
3214 vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC;
3215 vht_cap->cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
3216 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
3217 vht_cap->cap |= sts_cap << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
3218 if (chip->support_bw160)
3219 vht_cap->cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3220 IEEE80211_VHT_CAP_SHORT_GI_160;
3221 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(rx_mcs_map);
3222 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(tx_mcs_map);
3223 vht_cap->vht_mcs.rx_highest = highest[hal->rx_nss - 1];
3224 vht_cap->vht_mcs.tx_highest = highest[hal->tx_nss - 1];
3227 #define RTW89_SBAND_IFTYPES_NR 2
3229 static void rtw89_init_he_cap(struct rtw89_dev *rtwdev,
3230 enum nl80211_band band,
3231 struct ieee80211_supported_band *sband)
3233 const struct rtw89_chip_info *chip = rtwdev->chip;
3234 struct rtw89_hal *hal = &rtwdev->hal;
3235 struct ieee80211_sband_iftype_data *iftype_data;
3236 bool no_ng16 = (chip->chip_id == RTL8852A && hal->cv == CHIP_CBV) ||
3237 (chip->chip_id == RTL8852B && hal->cv == CHIP_CAV);
3240 int nss = hal->rx_nss;
3243 iftype_data = kcalloc(RTW89_SBAND_IFTYPES_NR, sizeof(*iftype_data), GFP_KERNEL);
3247 for (i = 0; i < 8; i++) {
3249 mcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
3251 mcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
3254 for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
3255 struct ieee80211_sta_he_cap *he_cap;
3260 case NL80211_IFTYPE_STATION:
3261 case NL80211_IFTYPE_AP:
3267 if (idx >= RTW89_SBAND_IFTYPES_NR) {
3268 rtw89_warn(rtwdev, "run out of iftype_data\n");
3272 iftype_data[idx].types_mask = BIT(i);
3273 he_cap = &iftype_data[idx].he_cap;
3274 mac_cap_info = he_cap->he_cap_elem.mac_cap_info;
3275 phy_cap_info = he_cap->he_cap_elem.phy_cap_info;
3277 he_cap->has_he = true;
3278 mac_cap_info[0] = IEEE80211_HE_MAC_CAP0_HTC_HE;
3279 if (i == NL80211_IFTYPE_STATION)
3280 mac_cap_info[1] = IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
3281 mac_cap_info[2] = IEEE80211_HE_MAC_CAP2_ALL_ACK |
3282 IEEE80211_HE_MAC_CAP2_BSR;
3283 mac_cap_info[3] = IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_2;
3284 if (i == NL80211_IFTYPE_AP)
3285 mac_cap_info[3] |= IEEE80211_HE_MAC_CAP3_OMI_CONTROL;
3286 mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_OPS |
3287 IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
3288 if (i == NL80211_IFTYPE_STATION)
3289 mac_cap_info[5] = IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
3290 if (band == NL80211_BAND_2GHZ) {
3292 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3295 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
3296 if (chip->support_bw160)
3297 phy_cap_info[0] |= IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3299 phy_cap_info[1] = IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3300 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3301 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
3302 phy_cap_info[2] = IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
3303 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
3304 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
3305 IEEE80211_HE_PHY_CAP2_DOPPLER_TX;
3306 phy_cap_info[3] = IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM;
3307 if (i == NL80211_IFTYPE_STATION)
3308 phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_16_QAM |
3309 IEEE80211_HE_PHY_CAP3_DCM_MAX_TX_NSS_2;
3310 if (i == NL80211_IFTYPE_AP)
3311 phy_cap_info[3] |= IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU;
3312 phy_cap_info[4] = IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
3313 IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
3314 if (chip->support_bw160)
3315 phy_cap_info[4] |= IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4;
3316 phy_cap_info[5] = no_ng16 ? 0 :
3317 IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
3318 IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
3319 phy_cap_info[6] = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
3320 IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
3321 IEEE80211_HE_PHY_CAP6_TRIG_SU_BEAMFORMING_FB |
3322 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE;
3323 phy_cap_info[7] = IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
3324 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI |
3325 IEEE80211_HE_PHY_CAP7_MAX_NC_1;
3326 phy_cap_info[8] = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
3327 IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI |
3328 IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_996;
3329 if (chip->support_bw160)
3330 phy_cap_info[8] |= IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
3331 IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
3332 phy_cap_info[9] = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
3333 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
3334 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
3335 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB |
3336 u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
3337 IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
3338 if (i == NL80211_IFTYPE_STATION)
3339 phy_cap_info[9] |= IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
3340 he_cap->he_mcs_nss_supp.rx_mcs_80 = cpu_to_le16(mcs_map);
3341 he_cap->he_mcs_nss_supp.tx_mcs_80 = cpu_to_le16(mcs_map);
3342 if (chip->support_bw160) {
3343 he_cap->he_mcs_nss_supp.rx_mcs_160 = cpu_to_le16(mcs_map);
3344 he_cap->he_mcs_nss_supp.tx_mcs_160 = cpu_to_le16(mcs_map);
3347 if (band == NL80211_BAND_6GHZ) {
3350 capa = le16_encode_bits(IEEE80211_HT_MPDU_DENSITY_NONE,
3351 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START) |
3352 le16_encode_bits(IEEE80211_VHT_MAX_AMPDU_1024K,
3353 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP) |
3354 le16_encode_bits(IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454,
3355 IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
3356 iftype_data[idx].he_6ghz_capa.capa = capa;
3362 sband->iftype_data = iftype_data;
3363 sband->n_iftype_data = idx;
3366 static int rtw89_core_set_supported_band(struct rtw89_dev *rtwdev)
3368 struct ieee80211_hw *hw = rtwdev->hw;
3369 struct ieee80211_supported_band *sband_2ghz = NULL, *sband_5ghz = NULL;
3370 struct ieee80211_supported_band *sband_6ghz = NULL;
3371 u32 size = sizeof(struct ieee80211_supported_band);
3372 u8 support_bands = rtwdev->chip->support_bands;
3374 if (support_bands & BIT(NL80211_BAND_2GHZ)) {
3375 sband_2ghz = kmemdup(&rtw89_sband_2ghz, size, GFP_KERNEL);
3378 rtw89_init_ht_cap(rtwdev, &sband_2ghz->ht_cap);
3379 rtw89_init_he_cap(rtwdev, NL80211_BAND_2GHZ, sband_2ghz);
3380 hw->wiphy->bands[NL80211_BAND_2GHZ] = sband_2ghz;
3383 if (support_bands & BIT(NL80211_BAND_5GHZ)) {
3384 sband_5ghz = kmemdup(&rtw89_sband_5ghz, size, GFP_KERNEL);
3387 rtw89_init_ht_cap(rtwdev, &sband_5ghz->ht_cap);
3388 rtw89_init_vht_cap(rtwdev, &sband_5ghz->vht_cap);
3389 rtw89_init_he_cap(rtwdev, NL80211_BAND_5GHZ, sband_5ghz);
3390 hw->wiphy->bands[NL80211_BAND_5GHZ] = sband_5ghz;
3393 if (support_bands & BIT(NL80211_BAND_6GHZ)) {
3394 sband_6ghz = kmemdup(&rtw89_sband_6ghz, size, GFP_KERNEL);
3397 rtw89_init_he_cap(rtwdev, NL80211_BAND_6GHZ, sband_6ghz);
3398 hw->wiphy->bands[NL80211_BAND_6GHZ] = sband_6ghz;
3404 hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
3405 hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
3406 hw->wiphy->bands[NL80211_BAND_6GHZ] = NULL;
3408 kfree(sband_2ghz->iftype_data);
3410 kfree(sband_5ghz->iftype_data);
3412 kfree(sband_6ghz->iftype_data);
3419 static void rtw89_core_clr_supported_band(struct rtw89_dev *rtwdev)
3421 struct ieee80211_hw *hw = rtwdev->hw;
3423 if (hw->wiphy->bands[NL80211_BAND_2GHZ])
3424 kfree(hw->wiphy->bands[NL80211_BAND_2GHZ]->iftype_data);
3425 if (hw->wiphy->bands[NL80211_BAND_5GHZ])
3426 kfree(hw->wiphy->bands[NL80211_BAND_5GHZ]->iftype_data);
3427 if (hw->wiphy->bands[NL80211_BAND_6GHZ])
3428 kfree(hw->wiphy->bands[NL80211_BAND_6GHZ]->iftype_data);
3429 kfree(hw->wiphy->bands[NL80211_BAND_2GHZ]);
3430 kfree(hw->wiphy->bands[NL80211_BAND_5GHZ]);
3431 kfree(hw->wiphy->bands[NL80211_BAND_6GHZ]);
3432 hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
3433 hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
3434 hw->wiphy->bands[NL80211_BAND_6GHZ] = NULL;
3437 static void rtw89_core_ppdu_sts_init(struct rtw89_dev *rtwdev)
3441 for (i = 0; i < RTW89_PHY_MAX; i++)
3442 skb_queue_head_init(&rtwdev->ppdu_sts.rx_queue[i]);
3443 for (i = 0; i < RTW89_PHY_MAX; i++)
3444 rtwdev->ppdu_sts.curr_rx_ppdu_cnt[i] = U8_MAX;
3447 void rtw89_core_update_beacon_work(struct work_struct *work)
3449 struct rtw89_dev *rtwdev;
3450 struct rtw89_vif *rtwvif = container_of(work, struct rtw89_vif,
3451 update_beacon_work);
3453 if (rtwvif->net_type != RTW89_NET_TYPE_AP_MODE)
3456 rtwdev = rtwvif->rtwdev;
3457 mutex_lock(&rtwdev->mutex);
3458 rtw89_fw_h2c_update_beacon(rtwdev, rtwvif);
3459 mutex_unlock(&rtwdev->mutex);
3462 int rtw89_wait_for_cond(struct rtw89_wait_info *wait, unsigned int cond)
3464 struct completion *cmpl = &wait->completion;
3465 unsigned long timeout;
3468 cur = atomic_cmpxchg(&wait->cond, RTW89_WAIT_COND_IDLE, cond);
3469 if (cur != RTW89_WAIT_COND_IDLE)
3472 timeout = wait_for_completion_timeout(cmpl, RTW89_WAIT_FOR_COND_TIMEOUT);
3474 atomic_set(&wait->cond, RTW89_WAIT_COND_IDLE);
3484 void rtw89_complete_cond(struct rtw89_wait_info *wait, unsigned int cond,
3485 const struct rtw89_completion_data *data)
3489 cur = atomic_cmpxchg(&wait->cond, cond, RTW89_WAIT_COND_IDLE);
3494 complete(&wait->completion);
3497 void rtw89_core_ntfy_btc_event(struct rtw89_dev *rtwdev, enum rtw89_btc_hmsg event)
3502 case RTW89_BTC_HMSG_SET_BT_REQ_SLOT:
3503 bt_req_len = rtw89_coex_query_bt_req_len(rtwdev, RTW89_PHY_0);
3504 rtw89_debug(rtwdev, RTW89_DBG_BTC,
3505 "coex updates BT req len to %d TU\n", bt_req_len);
3508 if (event < NUM_OF_RTW89_BTC_HMSG)
3509 rtw89_debug(rtwdev, RTW89_DBG_BTC,
3510 "unhandled BTC HMSG event: %d\n", event);
3513 "unrecognized BTC HMSG event: %d\n", event);
3518 int rtw89_core_start(struct rtw89_dev *rtwdev)
3522 rtwdev->mac.qta_mode = RTW89_QTA_SCC;
3523 ret = rtw89_mac_init(rtwdev);
3525 rtw89_err(rtwdev, "mac init fail, ret:%d\n", ret);
3529 rtw89_btc_ntfy_poweron(rtwdev);
3533 /* pre-config BB/RF, BB reset/RFC reset */
3534 ret = rtw89_chip_disable_bb_rf(rtwdev);
3537 ret = rtw89_chip_enable_bb_rf(rtwdev);
3541 rtw89_phy_init_bb_reg(rtwdev);
3542 rtw89_phy_init_rf_reg(rtwdev, false);
3544 rtw89_btc_ntfy_init(rtwdev, BTC_MODE_NORMAL);
3546 rtw89_phy_dm_init(rtwdev);
3548 rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true);
3549 rtw89_mac_update_rts_threshold(rtwdev, RTW89_MAC_0);
3551 rtw89_tas_reset(rtwdev);
3553 ret = rtw89_hci_start(rtwdev);
3555 rtw89_err(rtwdev, "failed to start hci\n");
3559 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work,
3560 RTW89_TRACK_WORK_PERIOD);
3562 set_bit(RTW89_FLAG_RUNNING, rtwdev->flags);
3564 rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_ON);
3565 rtw89_fw_h2c_fw_log(rtwdev, rtwdev->fw.log.enable);
3566 rtw89_fw_h2c_init_ba_cam(rtwdev);
3571 void rtw89_core_stop(struct rtw89_dev *rtwdev)
3573 struct rtw89_btc *btc = &rtwdev->btc;
3575 /* Prvent to stop twice; enter_ips and ops_stop */
3576 if (!test_bit(RTW89_FLAG_RUNNING, rtwdev->flags))
3579 rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_OFF);
3581 clear_bit(RTW89_FLAG_RUNNING, rtwdev->flags);
3583 mutex_unlock(&rtwdev->mutex);
3585 cancel_work_sync(&rtwdev->c2h_work);
3586 cancel_work_sync(&rtwdev->cancel_6ghz_probe_work);
3587 cancel_work_sync(&btc->eapol_notify_work);
3588 cancel_work_sync(&btc->arp_notify_work);
3589 cancel_work_sync(&btc->dhcp_notify_work);
3590 cancel_work_sync(&btc->icmp_notify_work);
3591 cancel_delayed_work_sync(&rtwdev->txq_reinvoke_work);
3592 cancel_delayed_work_sync(&rtwdev->track_work);
3593 cancel_delayed_work_sync(&rtwdev->chanctx_work);
3594 cancel_delayed_work_sync(&rtwdev->coex_act1_work);
3595 cancel_delayed_work_sync(&rtwdev->coex_bt_devinfo_work);
3596 cancel_delayed_work_sync(&rtwdev->coex_rfk_chk_work);
3597 cancel_delayed_work_sync(&rtwdev->cfo_track_work);
3598 cancel_delayed_work_sync(&rtwdev->forbid_ba_work);
3599 cancel_delayed_work_sync(&rtwdev->antdiv_work);
3601 mutex_lock(&rtwdev->mutex);
3603 rtw89_btc_ntfy_poweroff(rtwdev);
3604 rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1, true);
3605 rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, true);
3606 rtw89_hci_stop(rtwdev);
3607 rtw89_hci_deinit(rtwdev);
3608 rtw89_mac_pwr_off(rtwdev);
3609 rtw89_hci_reset(rtwdev);
3612 int rtw89_core_init(struct rtw89_dev *rtwdev)
3614 struct rtw89_btc *btc = &rtwdev->btc;
3617 INIT_LIST_HEAD(&rtwdev->ba_list);
3618 INIT_LIST_HEAD(&rtwdev->forbid_ba_list);
3619 INIT_LIST_HEAD(&rtwdev->rtwvifs_list);
3620 INIT_LIST_HEAD(&rtwdev->early_h2c_list);
3621 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3622 if (!(rtwdev->chip->support_bands & BIT(band)))
3624 INIT_LIST_HEAD(&rtwdev->scan_info.pkt_list[band]);
3626 INIT_WORK(&rtwdev->ba_work, rtw89_core_ba_work);
3627 INIT_WORK(&rtwdev->txq_work, rtw89_core_txq_work);
3628 INIT_DELAYED_WORK(&rtwdev->txq_reinvoke_work, rtw89_core_txq_reinvoke_work);
3629 INIT_DELAYED_WORK(&rtwdev->track_work, rtw89_track_work);
3630 INIT_DELAYED_WORK(&rtwdev->chanctx_work, rtw89_chanctx_work);
3631 INIT_DELAYED_WORK(&rtwdev->coex_act1_work, rtw89_coex_act1_work);
3632 INIT_DELAYED_WORK(&rtwdev->coex_bt_devinfo_work, rtw89_coex_bt_devinfo_work);
3633 INIT_DELAYED_WORK(&rtwdev->coex_rfk_chk_work, rtw89_coex_rfk_chk_work);
3634 INIT_DELAYED_WORK(&rtwdev->cfo_track_work, rtw89_phy_cfo_track_work);
3635 INIT_DELAYED_WORK(&rtwdev->forbid_ba_work, rtw89_forbid_ba_work);
3636 INIT_DELAYED_WORK(&rtwdev->antdiv_work, rtw89_phy_antdiv_work);
3637 rtwdev->txq_wq = alloc_workqueue("rtw89_tx_wq", WQ_UNBOUND | WQ_HIGHPRI, 0);
3638 if (!rtwdev->txq_wq)
3640 spin_lock_init(&rtwdev->ba_lock);
3641 spin_lock_init(&rtwdev->rpwm_lock);
3642 mutex_init(&rtwdev->mutex);
3643 mutex_init(&rtwdev->rf_mutex);
3644 rtwdev->total_sta_assoc = 0;
3646 rtw89_init_wait(&rtwdev->mcc.wait);
3647 rtw89_init_wait(&rtwdev->mac.fw_ofld_wait);
3649 INIT_WORK(&rtwdev->c2h_work, rtw89_fw_c2h_work);
3650 INIT_WORK(&rtwdev->ips_work, rtw89_ips_work);
3651 INIT_WORK(&rtwdev->load_firmware_work, rtw89_load_firmware_work);
3652 INIT_WORK(&rtwdev->cancel_6ghz_probe_work, rtw89_cancel_6ghz_probe_work);
3654 skb_queue_head_init(&rtwdev->c2h_queue);
3655 rtw89_core_ppdu_sts_init(rtwdev);
3656 rtw89_traffic_stats_init(rtwdev, &rtwdev->stats);
3658 rtwdev->hal.rx_fltr = DEFAULT_AX_RX_FLTR;
3660 INIT_WORK(&btc->eapol_notify_work, rtw89_btc_ntfy_eapol_packet_work);
3661 INIT_WORK(&btc->arp_notify_work, rtw89_btc_ntfy_arp_packet_work);
3662 INIT_WORK(&btc->dhcp_notify_work, rtw89_btc_ntfy_dhcp_packet_work);
3663 INIT_WORK(&btc->icmp_notify_work, rtw89_btc_ntfy_icmp_packet_work);
3665 init_completion(&rtwdev->fw.req.completion);
3667 schedule_work(&rtwdev->load_firmware_work);
3669 rtw89_ser_init(rtwdev);
3670 rtw89_entity_init(rtwdev);
3671 rtw89_tas_init(rtwdev);
3675 EXPORT_SYMBOL(rtw89_core_init);
3677 void rtw89_core_deinit(struct rtw89_dev *rtwdev)
3679 rtw89_ser_deinit(rtwdev);
3680 rtw89_unload_firmware(rtwdev);
3681 rtw89_fw_free_all_early_h2c(rtwdev);
3683 destroy_workqueue(rtwdev->txq_wq);
3684 mutex_destroy(&rtwdev->rf_mutex);
3685 mutex_destroy(&rtwdev->mutex);
3687 EXPORT_SYMBOL(rtw89_core_deinit);
3689 void rtw89_core_scan_start(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
3690 const u8 *mac_addr, bool hw_scan)
3692 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev,
3693 rtwvif->sub_entity_idx);
3695 rtwdev->scanning = true;
3696 rtw89_leave_lps(rtwdev);
3698 rtw89_leave_ips_by_hwflags(rtwdev);
3700 ether_addr_copy(rtwvif->mac_addr, mac_addr);
3701 rtw89_btc_ntfy_scan_start(rtwdev, RTW89_PHY_0, chan->band_type);
3702 rtw89_chip_rfk_scan(rtwdev, true);
3703 rtw89_hci_recalc_int_mit(rtwdev);
3704 rtw89_phy_config_edcca(rtwdev, true);
3706 rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, mac_addr);
3709 void rtw89_core_scan_complete(struct rtw89_dev *rtwdev,
3710 struct ieee80211_vif *vif, bool hw_scan)
3712 struct rtw89_vif *rtwvif = vif ? (struct rtw89_vif *)vif->drv_priv : NULL;
3717 ether_addr_copy(rtwvif->mac_addr, vif->addr);
3718 rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
3720 rtw89_chip_rfk_scan(rtwdev, false);
3721 rtw89_btc_ntfy_scan_finish(rtwdev, RTW89_PHY_0);
3722 rtw89_phy_config_edcca(rtwdev, false);
3724 rtwdev->scanning = false;
3725 rtwdev->dig.bypass_dig = true;
3726 if (hw_scan && (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE))
3727 ieee80211_queue_work(rtwdev->hw, &rtwdev->ips_work);
3730 static void rtw89_read_chip_ver(struct rtw89_dev *rtwdev)
3732 const struct rtw89_chip_info *chip = rtwdev->chip;
3737 cv = rtw89_read32_mask(rtwdev, R_AX_SYS_CFG1, B_AX_CHIP_VER_MASK);
3738 if (chip->chip_id == RTL8852A && cv <= CHIP_CBV) {
3739 if (rtw89_read32(rtwdev, R_AX_GPIO0_7_FUNC_SEL) == RTW89_R32_DEAD)
3745 rtwdev->hal.cv = cv;
3747 if (chip->chip_id == RTL8852B || chip->chip_id == RTL8851B) {
3748 ret = rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_CV, &val);
3752 rtwdev->hal.acv = u8_get_bits(val, XTAL_SI_ACV_MASK);
3756 static void rtw89_core_setup_phycap(struct rtw89_dev *rtwdev)
3758 rtwdev->hal.support_cckpd =
3759 !(rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv <= CHIP_CBV) &&
3760 !(rtwdev->chip->chip_id == RTL8852B && rtwdev->hal.cv <= CHIP_CAV);
3761 rtwdev->hal.support_igi =
3762 rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv <= CHIP_CBV;
3765 static void rtw89_core_setup_rfe_parms(struct rtw89_dev *rtwdev)
3767 const struct rtw89_chip_info *chip = rtwdev->chip;
3768 const struct rtw89_rfe_parms_conf *conf = chip->rfe_parms_conf;
3769 struct rtw89_efuse *efuse = &rtwdev->efuse;
3770 u8 rfe_type = efuse->rfe_type;
3775 while (conf->rfe_parms) {
3776 if (rfe_type == conf->rfe_type) {
3777 rtwdev->rfe_parms = conf->rfe_parms;
3784 rtwdev->rfe_parms = chip->dflt_parms;
3787 static int rtw89_chip_efuse_info_setup(struct rtw89_dev *rtwdev)
3791 ret = rtw89_mac_partial_init(rtwdev);
3795 ret = rtw89_parse_efuse_map(rtwdev);
3799 ret = rtw89_parse_phycap_map(rtwdev);
3803 ret = rtw89_mac_setup_phycap(rtwdev);
3807 rtw89_core_setup_phycap(rtwdev);
3808 rtw89_core_setup_rfe_parms(rtwdev);
3810 rtw89_mac_pwr_off(rtwdev);
3815 static int rtw89_chip_board_info_setup(struct rtw89_dev *rtwdev)
3817 rtw89_chip_fem_setup(rtwdev);
3822 int rtw89_chip_info_setup(struct rtw89_dev *rtwdev)
3826 rtw89_read_chip_ver(rtwdev);
3828 ret = rtw89_wait_firmware_completion(rtwdev);
3830 rtw89_err(rtwdev, "failed to wait firmware completion\n");
3834 ret = rtw89_fw_recognize(rtwdev);
3836 rtw89_err(rtwdev, "failed to recognize firmware\n");
3840 ret = rtw89_fw_recognize_elements(rtwdev);
3842 rtw89_err(rtwdev, "failed to recognize firmware elements\n");
3846 ret = rtw89_chip_efuse_info_setup(rtwdev);
3850 ret = rtw89_chip_board_info_setup(rtwdev);
3854 rtwdev->ps_mode = rtw89_update_ps_mode(rtwdev);
3858 EXPORT_SYMBOL(rtw89_chip_info_setup);
3860 static int rtw89_core_register_hw(struct rtw89_dev *rtwdev)
3862 struct ieee80211_hw *hw = rtwdev->hw;
3863 struct rtw89_efuse *efuse = &rtwdev->efuse;
3864 struct rtw89_hal *hal = &rtwdev->hal;
3866 int tx_headroom = IEEE80211_HT_CTL_LEN;
3868 hw->vif_data_size = sizeof(struct rtw89_vif);
3869 hw->sta_data_size = sizeof(struct rtw89_sta);
3870 hw->txq_data_size = sizeof(struct rtw89_txq);
3871 hw->chanctx_data_size = sizeof(struct rtw89_chanctx_cfg);
3873 SET_IEEE80211_PERM_ADDR(hw, efuse->addr);
3875 hw->extra_tx_headroom = tx_headroom;
3876 hw->queues = IEEE80211_NUM_ACS;
3877 hw->max_rx_aggregation_subframes = RTW89_MAX_RX_AGG_NUM;
3878 hw->max_tx_aggregation_subframes = RTW89_MAX_TX_AGG_NUM;
3879 hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
3881 ieee80211_hw_set(hw, SIGNAL_DBM);
3882 ieee80211_hw_set(hw, HAS_RATE_CONTROL);
3883 ieee80211_hw_set(hw, MFP_CAPABLE);
3884 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
3885 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3886 ieee80211_hw_set(hw, RX_INCLUDES_FCS);
3887 ieee80211_hw_set(hw, TX_AMSDU);
3888 ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
3889 ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
3890 ieee80211_hw_set(hw, SUPPORTS_PS);
3891 ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
3892 ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
3893 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3894 ieee80211_hw_set(hw, WANT_MONITOR_VIF);
3895 if (RTW89_CHK_FW_FEATURE(BEACON_FILTER, &rtwdev->fw))
3896 ieee80211_hw_set(hw, CONNECTION_MONITOR);
3898 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
3899 BIT(NL80211_IFTYPE_AP) |
3900 BIT(NL80211_IFTYPE_P2P_CLIENT) |
3901 BIT(NL80211_IFTYPE_P2P_GO);
3903 if (hal->ant_diversity) {
3904 hw->wiphy->available_antennas_tx = 0x3;
3905 hw->wiphy->available_antennas_rx = 0x3;
3907 hw->wiphy->available_antennas_tx = BIT(rtwdev->chip->rf_path_num) - 1;
3908 hw->wiphy->available_antennas_rx = BIT(rtwdev->chip->rf_path_num) - 1;
3911 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3912 WIPHY_FLAG_TDLS_EXTERNAL_SETUP |
3913 WIPHY_FLAG_AP_UAPSD | WIPHY_FLAG_SPLIT_SCAN_6GHZ;
3914 hw->wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3916 hw->wiphy->max_scan_ssids = RTW89_SCANOFLD_MAX_SSID;
3917 hw->wiphy->max_scan_ie_len = RTW89_SCANOFLD_MAX_IE_LEN;
3920 hw->wiphy->wowlan = rtwdev->chip->wowlan_stub;
3923 hw->wiphy->tid_config_support.vif |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
3924 hw->wiphy->tid_config_support.peer |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
3925 hw->wiphy->tid_config_support.vif |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
3926 hw->wiphy->tid_config_support.peer |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
3927 hw->wiphy->max_remain_on_channel_duration = 1000;
3929 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CAN_REPLACE_PTK0);
3931 ret = rtw89_core_set_supported_band(rtwdev);
3933 rtw89_err(rtwdev, "failed to set supported band\n");
3937 ret = rtw89_regd_setup(rtwdev);
3939 rtw89_err(rtwdev, "failed to set up regd\n");
3940 goto err_free_supported_band;
3943 hw->wiphy->sar_capa = &rtw89_sar_capa;
3945 ret = ieee80211_register_hw(hw);
3947 rtw89_err(rtwdev, "failed to register hw\n");
3948 goto err_free_supported_band;
3951 ret = rtw89_regd_init(rtwdev, rtw89_regd_notifier);
3953 rtw89_err(rtwdev, "failed to init regd\n");
3954 goto err_unregister_hw;
3960 ieee80211_unregister_hw(hw);
3961 err_free_supported_band:
3962 rtw89_core_clr_supported_band(rtwdev);
3967 static void rtw89_core_unregister_hw(struct rtw89_dev *rtwdev)
3969 struct ieee80211_hw *hw = rtwdev->hw;
3971 ieee80211_unregister_hw(hw);
3972 rtw89_core_clr_supported_band(rtwdev);
3975 int rtw89_core_register(struct rtw89_dev *rtwdev)
3979 ret = rtw89_core_register_hw(rtwdev);
3981 rtw89_err(rtwdev, "failed to register core hw\n");
3985 rtw89_debugfs_init(rtwdev);
3989 EXPORT_SYMBOL(rtw89_core_register);
3991 void rtw89_core_unregister(struct rtw89_dev *rtwdev)
3993 rtw89_core_unregister_hw(rtwdev);
3995 EXPORT_SYMBOL(rtw89_core_unregister);
3997 struct rtw89_dev *rtw89_alloc_ieee80211_hw(struct device *device,
3999 const struct rtw89_chip_info *chip)
4001 struct rtw89_fw_info early_fw = {};
4002 const struct firmware *firmware;
4003 struct ieee80211_hw *hw;
4004 struct rtw89_dev *rtwdev;
4005 struct ieee80211_ops *ops;
4006 u32 driver_data_size;
4010 firmware = rtw89_early_fw_feature_recognize(device, chip, &early_fw, &fw_format);
4012 ops = kmemdup(&rtw89_ops, sizeof(rtw89_ops), GFP_KERNEL);
4016 no_chanctx = chip->support_chanctx_num == 0 ||
4017 !RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &early_fw) ||
4018 !RTW89_CHK_FW_FEATURE(BEACON_FILTER, &early_fw);
4021 ops->add_chanctx = NULL;
4022 ops->remove_chanctx = NULL;
4023 ops->change_chanctx = NULL;
4024 ops->assign_vif_chanctx = NULL;
4025 ops->unassign_vif_chanctx = NULL;
4026 ops->remain_on_channel = NULL;
4027 ops->cancel_remain_on_channel = NULL;
4030 driver_data_size = sizeof(struct rtw89_dev) + bus_data_size;
4031 hw = ieee80211_alloc_hw(driver_data_size, ops);
4035 hw->wiphy->iface_combinations = rtw89_iface_combs;
4036 hw->wiphy->n_iface_combinations = ARRAY_SIZE(rtw89_iface_combs);
4040 rtwdev->dev = device;
4042 rtwdev->chip = chip;
4043 rtwdev->fw.req.firmware = firmware;
4044 rtwdev->fw.fw_format = fw_format;
4046 rtw89_debug(rtwdev, RTW89_DBG_FW, "probe driver %s chanctx\n",
4047 no_chanctx ? "without" : "with");
4053 release_firmware(firmware);
4056 EXPORT_SYMBOL(rtw89_alloc_ieee80211_hw);
4058 void rtw89_free_ieee80211_hw(struct rtw89_dev *rtwdev)
4061 release_firmware(rtwdev->fw.req.firmware);
4062 ieee80211_free_hw(rtwdev->hw);
4064 EXPORT_SYMBOL(rtw89_free_ieee80211_hw);
4066 MODULE_AUTHOR("Realtek Corporation");
4067 MODULE_DESCRIPTION("Realtek 802.11ax wireless core module");
4068 MODULE_LICENSE("Dual BSD/GPL");