Skip to contentSkip to conversation

wifi: rtw89: preparation of multiple interface concurrency support

PATCH Superseded Ping-Ke Shih 2 participants 20 messages Mar 10, 2023 – Apr 12, 2023 Realtek wireless driver (rtw89)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
author maintainer
#18 lore raw

PATCH 4/5 wifi: rtw89: add flag check for power state

In reply to Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

Use POWER_ON flag to make sure power on/off is symmetric. Since both remain_on_channel and hw_scan both alter the power state, this makes sure that we don't enter/leave IPS mode twice.
Also, replace IPS related functions with inline function that does similar logic so we can track it more easily.

---
 drivers/net/wireless/realtek/rtw89/core.c | 7 +++----
 drivers/net/wireless/realtek/rtw89/ps.c   | 6 ++++++
 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/net/wireless/realtek/rtw89/core.c +3 -4
index 594b5e5ea43ce..7f8f0e551eb04 100644
@@ -2207,8 +2207,7 @@ static void rtw89_ips_work(struct work_struct *work)
 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
 						ips_work);
 	mutex_lock(&rtwdev->mutex);
-	if (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE)
-		rtw89_enter_ips(rtwdev);
+	rtw89_enter_ips_by_hwflags(rtwdev);
 	mutex_unlock(&rtwdev->mutex);
 }
 
@@ -3505,8 +3504,8 @@ void rtw89_core_scan_start(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
 
 	rtwdev->scanning = true;
 	rtw89_leave_lps(rtwdev);
-	if (hw_scan && (rtwdev->hw->conf.flags & IEEE80211_CONF_IDLE))
-		rtw89_leave_ips(rtwdev);
+	if (hw_scan)
+		rtw89_leave_ips_by_hwflags(rtwdev);
 
 	ether_addr_copy(rtwvif->mac_addr, mac_addr);
 	rtw89_btc_ntfy_scan_start(rtwdev, RTW89_PHY_0, chan->band_type);
drivers/net/wireless/realtek/rtw89/ps.c +6 -0
index 40498812205ea..cf72861c7adcd 100644
@@ -155,6 +155,9 @@ void rtw89_enter_ips(struct rtw89_dev *rtwdev)
 
 	set_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags);
 
+	if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
+		return;
+
 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
 		rtw89_mac_vif_deinit(rtwdev, rtwvif);
 
@@ -166,6 +169,9 @@ void rtw89_leave_ips(struct rtw89_dev *rtwdev)
 	struct rtw89_vif *rtwvif;
 	int ret;
 
+	if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
+		return;
+
 	ret = rtw89_core_start(rtwdev);
 	if (ret)
 		rtw89_err(rtwdev, "failed to leave idle state\n");
Signature · 1 line
2.25.1
author maintainer
#10 lore raw

PATCH 2/5 wifi: rtw89: add function to wait for completion of TX skbs

In reply to Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

Allocate a per-skb completion to track those skbs we are interested in and wait for them to complete transmission with TX status. To avoid race condition between process and softirq without addtional locking, we use a work to free the tx_wait struct later when waiter is finished referencing it. This must be called in process context and with a timeout value greater than zero since it might sleep.

We use another workqueue so works can be processed concurrently and when the PCI device is removed unexpectedly, all pending works can be flushed. This prevents some works that were scheduled but never processed leads to memory leak.

---
 drivers/net/wireless/realtek/rtw89/core.c | 53 +++++++++++++++++++++++
 drivers/net/wireless/realtek/rtw89/core.h | 33 ++++++++++++++
 drivers/net/wireless/realtek/rtw89/pci.c  |  6 +++
 drivers/net/wireless/realtek/rtw89/pci.h  |  4 +-
 4 files changed, 94 insertions(+), 2 deletions(-)
drivers/net/wireless/realtek/rtw89/core.c +53 -0
index 43080d50db171..369f7fb4e0779 100644
@@ -849,6 +849,55 @@ void rtw89_core_tx_kick_off(struct rtw89_dev *rtwdev, u8 qsel)
 	rtw89_hci_tx_kick_off(rtwdev, ch_dma);
 }
 
+static void rtw89_core_free_tx_wait_work(struct work_struct *work)
+{
+	struct rtw89_tx_wait_info *wait =
+			container_of(work, struct rtw89_tx_wait_info, work);
+	struct rtw89_dev *rtwdev = wait->rtwdev;
+	int done, ret;
+
+	ret = read_poll_timeout(atomic_read, done, done, 1000, 100000, false,
+				&wait->wait_done);
+
+	if (ret)
+		rtw89_err(rtwdev, "tx wait timed out, stop polling\n");
+	else
+		kfree(wait);
+}
+
+int rtw89_core_tx_kick_off_and_wait(struct rtw89_dev *rtwdev, struct sk_buff *skb,
+				    int qsel, unsigned int timeout)
+{
+	struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
+	struct rtw89_tx_wait_info *wait;
+	unsigned long time_left;
+	int ret = 0;
+
+	skb_data->wait = kzalloc(sizeof(*wait), GFP_KERNEL);
+	wait = skb_data->wait;
+	if (!wait) {
+		rtw89_warn(rtwdev, "alloc tx wait info failed\n");
+		rtw89_core_tx_kick_off(rtwdev, qsel);
+		return 0;
+	}
+
+	init_completion(&wait->completion);
+	INIT_WORK(&wait->work, rtw89_core_free_tx_wait_work);
+	wait->rtwdev = rtwdev;
+
+	rtw89_core_tx_kick_off(rtwdev, qsel);
+	time_left = wait_for_completion_timeout(&wait->completion,
+						msecs_to_jiffies(timeout));
+	if (time_left == 0)
+		ret = -ETIMEDOUT;
+	else if (!wait->tx_done)
+		ret = -EAGAIN;
+
+	atomic_inc(&wait->wait_done);
+
+	return ret;
+}
+
 int rtw89_h2c_tx(struct rtw89_dev *rtwdev,
 		 struct sk_buff *skb, bool fwdl)
 {
@@ -3193,6 +3242,9 @@ int rtw89_core_init(struct rtw89_dev *rtwdev)
 	rtwdev->txq_wq = alloc_workqueue("rtw89_tx_wq", WQ_UNBOUND | WQ_HIGHPRI, 0);
 	if (!rtwdev->txq_wq)
 		return -ENOMEM;
+	rtwdev->gc_wq = alloc_workqueue("rtw89_gc_wq", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
+	if (!rtwdev->gc_wq)
+		return -ENOMEM;
 	spin_lock_init(&rtwdev->ba_lock);
 	spin_lock_init(&rtwdev->rpwm_lock);
 	mutex_init(&rtwdev->mutex);
@@ -3234,6 +3286,7 @@ void rtw89_core_deinit(struct rtw89_dev *rtwdev)
 	rtw89_fw_free_all_early_h2c(rtwdev);
 
 	destroy_workqueue(rtwdev->txq_wq);
+	destroy_workqueue(rtwdev->gc_wq);
 	mutex_destroy(&rtwdev->rf_mutex);
 	mutex_destroy(&rtwdev->mutex);
 }
drivers/net/wireless/realtek/rtw89/core.h +33 -0
index aefc95f83d277..dd4165394c8de 100644
@@ -2421,6 +2421,19 @@ struct rtw89_phy_rate_pattern {
 	bool enable;
 };
 
+struct rtw89_tx_wait_info {
+	struct rtw89_dev *rtwdev;
+	struct completion completion;
+	struct work_struct work;
+	atomic_t wait_done;
+	bool tx_done;
+};
+
+struct rtw89_tx_skb_data {
+	struct rtw89_tx_wait_info *wait;
+	u8 hci_priv[];
+};
+
 #define RTW89_P2P_MAX_NOA_NUM 2
 
 struct rtw89_vif {
@@ -3758,6 +3771,7 @@ struct rtw89_dev {
 	/* used to protect rf read write */
 	struct mutex rf_mutex;
 	struct workqueue_struct *txq_wq;
+	struct workqueue_struct *gc_wq;
 	struct work_struct txq_work;
 	struct delayed_work txq_reinvoke_work;
 	/* used to protect ba_list and forbid_ba_list */
@@ -3969,6 +3983,14 @@ static inline void rtw89_hci_clear(struct rtw89_dev *rtwdev, struct pci_dev *pde
 		rtwdev->hci.ops->clear(rtwdev, pdev);
 }
 
+static inline
+struct rtw89_tx_skb_data *RTW89_TX_SKB_CB(struct sk_buff *skb)
+{
+	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+
+	return (struct rtw89_tx_skb_data *)info->status.status_driver_data;
+}
+
 static inline u8 rtw89_read8(struct rtw89_dev *rtwdev, u32 addr)
 {
 	return rtwdev->hci.ops->read8(rtwdev, addr);
@@ -4612,11 +4634,22 @@ static inline struct sk_buff *rtw89_alloc_skb_for_rx(struct rtw89_dev *rtwdev,
 	return dev_alloc_skb(length);
 }
 
+static inline void rtw89_core_tx_wait_complete(struct rtw89_dev *rtwdev,
+					       struct rtw89_tx_wait_info *wait,
+					       bool tx_done)
+{
+	wait->tx_done = tx_done;
+	complete(&wait->completion);
+	queue_work(rtwdev->gc_wq, &wait->work);
+}
+
 int rtw89_core_tx_write(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif,
 			struct ieee80211_sta *sta, struct sk_buff *skb, int *qsel);
 int rtw89_h2c_tx(struct rtw89_dev *rtwdev,
 		 struct sk_buff *skb, bool fwdl);
 void rtw89_core_tx_kick_off(struct rtw89_dev *rtwdev, u8 qsel);
+int rtw89_core_tx_kick_off_and_wait(struct rtw89_dev *rtwdev, struct sk_buff *skb,
+				    int qsel, unsigned int timeout);
 void rtw89_core_fill_txdesc(struct rtw89_dev *rtwdev,
 			    struct rtw89_tx_desc_info *desc_info,
 			    void *txdesc);
drivers/net/wireless/realtek/rtw89/pci.c +6 -0
index ec8bb5f10482e..ca6b63944ee99 100644
@@ -364,6 +364,8 @@ static void rtw89_pci_tx_status(struct rtw89_dev *rtwdev,
 				struct rtw89_pci_tx_ring *tx_ring,
 				struct sk_buff *skb, u8 tx_status)
 {
+	struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
+	struct rtw89_tx_wait_info *wait = skb_data->wait;
 	struct ieee80211_tx_info *info;
 
 	info = IEEE80211_SKB_CB(skb);
@@ -394,6 +396,8 @@ static void rtw89_pci_tx_status(struct rtw89_dev *rtwdev,
 		}
 	}
 
+	if (wait)
+		rtw89_core_tx_wait_complete(rtwdev, wait, tx_status == RTW89_TX_DONE);
 	ieee80211_tx_status_ni(rtwdev->hw, skb);
 }
 
@@ -1203,6 +1207,7 @@ static int rtw89_pci_txwd_submit(struct rtw89_dev *rtwdev,
 	struct pci_dev *pdev = rtwpci->pdev;
 	struct sk_buff *skb = tx_req->skb;
 	struct rtw89_pci_tx_data *tx_data = RTW89_PCI_TX_SKB_CB(skb);
+	struct rtw89_tx_skb_data *skb_data = RTW89_TX_SKB_CB(skb);
 	bool en_wd_info = desc_info->en_wd_info;
 	u32 txwd_len;
 	u32 txwp_len;
@@ -1218,6 +1223,7 @@ static int rtw89_pci_txwd_submit(struct rtw89_dev *rtwdev,
 	}
 
 	tx_data->dma = dma;
+	skb_data->wait = NULL;
 
 	txwp_len = sizeof(*txwp_info);
 	txwd_len = chip->txwd_body_size;
drivers/net/wireless/realtek/rtw89/pci.h +2 -2
index 1e19740db8c54..0e4bd210b100f 100644
@@ -1004,9 +1004,9 @@ rtw89_pci_rxbd_increase(struct rtw89_pci_rx_ring *rx_ring, u32 cnt)
 
 static inline struct rtw89_pci_tx_data *RTW89_PCI_TX_SKB_CB(struct sk_buff *skb)
 {
-	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+	struct rtw89_tx_skb_data *data = RTW89_TX_SKB_CB(skb);
 
-	return (struct rtw89_pci_tx_data *)info->status.status_driver_data;
+	return (struct rtw89_pci_tx_data *)data->hci_priv;
 }
 
 static inline struct rtw89_pci_tx_bd_32 *
Signature · 1 line
2.25.1

Ping-Ke Shih

author maintainer
#0 lore raw

cover wifi: rtw89: preparation of multiple interface concurrency support

To support concurrency, we implement beacon filter, CQM and ieee80211::remain_on_channel ops. Since our firmware doesn't support to TX null packet while doing remain-on-channel, driver does this instead. To ensure null packet send out before switching channel, patch 2/5 adds a waiting mechanism.

The patches 4/5 and 5/5 refine things we found during developing.

Po-Hao Huang (5):
wifi: rtw89: 8852c: add beacon filter and CQM support
wifi: rtw89: add function to wait for completion of TX skbs
wifi: rtw89: add ieee80211::remain_on_channel ops
wifi: rtw89: add flag check for power state
wifi: rtw89: fix authentication fail during scan

 drivers/net/wireless/realtek/rtw89/chan.c     |  35 +++
 drivers/net/wireless/realtek/rtw89/chan.h     |   3 +
 drivers/net/wireless/realtek/rtw89/core.c     | 284 +++++++++++++++++-
 drivers/net/wireless/realtek/rtw89/core.h     |  64 ++++
 drivers/net/wireless/realtek/rtw89/fw.c       | 111 ++++++-
 drivers/net/wireless/realtek/rtw89/fw.h       |  94 ++++++
 drivers/net/wireless/realtek/rtw89/mac.c      |  60 ++++
 drivers/net/wireless/realtek/rtw89/mac.h      |   1 +
 drivers/net/wireless/realtek/rtw89/mac80211.c |  88 +++++-
 drivers/net/wireless/realtek/rtw89/pci.c      |   6 +
 drivers/net/wireless/realtek/rtw89/pci.h      |   4 +-
 drivers/net/wireless/realtek/rtw89/ps.c       |   6 +
 drivers/net/wireless/realtek/rtw89/ps.h       |  16 +
 13 files changed, 757 insertions(+), 15 deletions(-)
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH 1/5 wifi: rtw89: 8852c: add beacon filter and CQM support

From: Po-Hao Huang <[email protected]>

Adding this supports beacon filter and connection quality monitor. To make host CPU wake up less, let firmware perform signal monitoring and beacon processing, then notify driver upon signal changes or beacon loss.

This feature needs firmware 0.27.56 or newer to support it.

---
 drivers/net/wireless/realtek/rtw89/core.c     | 12 ++-
 drivers/net/wireless/realtek/rtw89/core.h     |  1 +
 drivers/net/wireless/realtek/rtw89/fw.c       | 96 +++++++++++++++++++
 drivers/net/wireless/realtek/rtw89/fw.h       | 94 ++++++++++++++++++
 drivers/net/wireless/realtek/rtw89/mac.c      | 57 +++++++++++
 drivers/net/wireless/realtek/rtw89/mac.h      |  1 +
 drivers/net/wireless/realtek/rtw89/mac80211.c |  8 ++
 7 files changed, 268 insertions(+), 1 deletion(-)
drivers/net/wireless/realtek/rtw89/core.c +11 -1
index 489fa7a86160d..43080d50db171 100644
@@ -1438,6 +1438,8 @@ static void rtw89_vif_rx_stats_iter(void *data, u8 *mac,
 	struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
 	struct sk_buff *skb = iter_data->skb;
 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
+	struct rtw89_rx_phy_ppdu *phy_ppdu =
+		(struct rtw89_rx_phy_ppdu *)iter_data->phy_ppdu;
 	const u8 *bssid = iter_data->bssid;
 
 	if (rtwdev->scanning &&
@@ -1456,8 +1458,11 @@ static void rtw89_vif_rx_stats_iter(void *data, u8 *mac,
 	if (!ether_addr_equal(vif->bss_conf.bssid, bssid))
 		return;
 
-	if (ieee80211_is_beacon(hdr->frame_control))
+	if (ieee80211_is_beacon(hdr->frame_control)) {
+		if (vif->type == NL80211_IFTYPE_STATION)
+			rtw89_fw_h2c_rssi_offload(rtwdev, phy_ppdu);
 		pkt_stat->beacon_nr++;
+	}
 
 	if (!ether_addr_equal(vif->addr, hdr->addr1))
 		return;
@@ -2520,6 +2525,9 @@ int rtw89_core_sta_disassoc(struct rtw89_dev *rtwdev,
 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
 
+	if (vif->type == NL80211_IFTYPE_STATION)
+		rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, vif, false);
+
 	rtwdev->total_sta_assoc--;
 	if (sta->tdls)
 		rtwvif->tdls_peer--;
@@ -3394,6 +3402,8 @@ static int rtw89_core_register_hw(struct rtw89_dev *rtwdev)
 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
+	if (RTW89_CHK_FW_FEATURE(BEACON_FILTER, &rtwdev->fw))
+		ieee80211_hw_set(hw, CONNECTION_MONITOR);
 
 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
 				     BIT(NL80211_IFTYPE_AP) |
drivers/net/wireless/realtek/rtw89/core.h +1 -0
index b1a886898c5a0..aefc95f83d277 100644
@@ -3026,6 +3026,7 @@ enum rtw89_fw_feature {
 	RTW89_FW_FEATURE_NO_PACKET_DROP,
 	RTW89_FW_FEATURE_NO_DEEP_PS,
 	RTW89_FW_FEATURE_NO_LPS_PG,
+	RTW89_FW_FEATURE_BEACON_FILTER,
 };
 
 struct rtw89_fw_suit {
drivers/net/wireless/realtek/rtw89/fw.c +96 -0

108 lines not on this page · raw message

drivers/net/wireless/realtek/rtw89/fw.h +94 -0

130 lines not on this page · raw message

drivers/net/wireless/realtek/rtw89/mac.c +57 -0

69 lines not on this page · raw message

drivers/net/wireless/realtek/rtw89/mac.h +1 -0
index 8064d3953d7f2..899c45c6774e2 100644
@@ -359,6 +359,7 @@ enum rtw89_mac_c2h_ofld_func {
 	RTW89_MAC_C2H_FUNC_MACID_PAUSE,
 	RTW89_MAC_C2H_FUNC_TSF32_TOGL_RPT = 0x6,
 	RTW89_MAC_C2H_FUNC_SCANOFLD_RSP = 0x9,
+	RTW89_MAC_C2H_FUNC_BCNFLTR_RPT = 0xd,
 	RTW89_MAC_C2H_FUNC_OFLD_MAX,
 };
 
drivers/net/wireless/realtek/rtw89/mac80211.c +8 -0
index 367a7bf319dae..629b32dafecb8 100644
@@ -114,6 +114,11 @@ static int rtw89_ops_add_interface(struct ieee80211_hw *hw,
 		    vif->addr, vif->type, vif->p2p);
 
 	mutex_lock(&rtwdev->mutex);
+
+	if (RTW89_CHK_FW_FEATURE(BEACON_FILTER, &rtwdev->fw))
+		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
+				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
+
 	rtwvif->rtwdev = rtwdev;
 	list_add_tail(&rtwvif->list, &rtwdev->rtwvifs_list);
 	INIT_WORK(&rtwvif->update_beacon_work, rtw89_core_update_beacon_work);
@@ -425,6 +430,9 @@ static void rtw89_ops_bss_info_changed(struct ieee80211_hw *hw,
 	if (changed & BSS_CHANGED_P2P_PS)
 		rtw89_process_p2p_ps(rtwdev, vif);
 
+	if (changed & BSS_CHANGED_CQM)
+		rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, vif, true);
+
 	mutex_unlock(&rtwdev->mutex);
 }
 
Signature · 1 line
2.25.1
author maintainer
#19 lore raw

PATCH 5/5 wifi: rtw89: fix authentication fail during scan

In reply to Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

We used to store operating channel info after associated. However, scan might happen before that. Without switching back to operating channel, authentication or association might fail. Therefore, we switch back to operating channel when the scanning vif's BSSID is non-zero, which implies connected or during attempt to connect.

---
 drivers/net/wireless/realtek/rtw89/fw.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/net/wireless/realtek/rtw89/fw.c +9 -6
index 2088b56263dfa..502adc70dc5bc 100644
@@ -3041,7 +3041,7 @@ static void rtw89_hw_scan_add_chan(struct rtw89_dev *rtwdev, int chan_type,
 }
 
 static int rtw89_hw_scan_add_chan_list(struct rtw89_dev *rtwdev,
-				       struct rtw89_vif *rtwvif)
+				       struct rtw89_vif *rtwvif, bool connected)
 {
 	struct cfg80211_scan_request *req = rtwvif->scan_req;
 	struct rtw89_mac_chinfo	*ch_info, *tmp;
@@ -3085,7 +3085,7 @@ static int rtw89_hw_scan_add_chan_list(struct rtw89_dev *rtwdev,
 			type = RTW89_CHAN_ACTIVE;
 		rtw89_hw_scan_add_chan(rtwdev, type, req->n_ssids, ch_info);
 
-		if (rtwvif->net_type != RTW89_NET_TYPE_NO_LINK &&
+		if (connected &&
 		    off_chan_time + ch_info->period > RTW89_OFF_CHAN_TIME) {
 			tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
 			if (!tmp) {
@@ -3118,7 +3118,7 @@ static int rtw89_hw_scan_add_chan_list(struct rtw89_dev *rtwdev,
 }
 
 static int rtw89_hw_scan_prehandle(struct rtw89_dev *rtwdev,
-				   struct rtw89_vif *rtwvif)
+				   struct rtw89_vif *rtwvif, bool connected)
 {
 	int ret;
 
@@ -3127,7 +3127,7 @@ static int rtw89_hw_scan_prehandle(struct rtw89_dev *rtwdev,
 		rtw89_err(rtwdev, "Update probe request failed\n");
 		goto out;
 	}
-	ret = rtw89_hw_scan_add_chan_list(rtwdev, rtwvif);
+	ret = rtw89_hw_scan_add_chan_list(rtwdev, rtwvif, connected);
 out:
 	return ret;
 }
@@ -3205,16 +3205,19 @@ int rtw89_hw_scan_offload(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif,
 {
 	struct rtw89_scan_option opt = {0};
 	struct rtw89_vif *rtwvif;
+	bool connected;
 	int ret = 0;
 
 	rtwvif = vif ? (struct rtw89_vif *)vif->drv_priv : NULL;
 	if (!rtwvif)
 		return -EINVAL;
 
+	/* This variable implies connected or during attempt to connect */
+	connected = !is_zero_ether_addr(rtwvif->bssid);
 	opt.enable = enable;
-	opt.target_ch_mode = rtwvif->net_type != RTW89_NET_TYPE_NO_LINK;
+	opt.target_ch_mode = connected;
 	if (enable) {
-		ret = rtw89_hw_scan_prehandle(rtwdev, rtwvif);
+		ret = rtw89_hw_scan_prehandle(rtwdev, rtwvif, connected);
 		if (ret)
 			goto out;
 	}
Signature · 1 line
2.25.1
author maintainer
#17 lore raw

PATCH 3/5 wifi: rtw89: add ieee80211::remain_on_channel ops

In reply to Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

Add support of remain on channel ops. Since channel context is required to enable multi-channel concurrent(MCC) and the current ROC in mac80211 don't support more than 1 channel context, add this to let P2P and other protocols relying on this work as expected. The off-channel duration and cancel timing is purely controlled by upper layers.

---
 drivers/net/wireless/realtek/rtw89/chan.c     |  35 +++
 drivers/net/wireless/realtek/rtw89/chan.h     |   3 +
 drivers/net/wireless/realtek/rtw89/core.c     | 212 +++++++++++++++++-
 drivers/net/wireless/realtek/rtw89/core.h     |  30 +++
 drivers/net/wireless/realtek/rtw89/mac.c      |   5 +-
 drivers/net/wireless/realtek/rtw89/mac80211.c |  80 ++++++-
 drivers/net/wireless/realtek/rtw89/ps.h       |  16 ++
 7 files changed, 378 insertions(+), 3 deletions(-)
drivers/net/wireless/realtek/rtw89/chan.c +35 -0
index 90596806bc93f..4663db4ce2f66 100644
@@ -141,6 +141,38 @@ void rtw89_config_entity_chandef(struct rtw89_dev *rtwdev,
 	__rtw89_config_entity_chandef(rtwdev, idx, chandef, true);
 }
 
+void rtw89_config_roc_chandef(struct rtw89_dev *rtwdev,
+			      enum rtw89_sub_entity_idx idx,
+			      const struct cfg80211_chan_def *chandef)
+{
+	struct rtw89_hal *hal = &rtwdev->hal;
+	enum rtw89_sub_entity_idx cur;
+
+	if (chandef) {
+		cur = atomic_cmpxchg(&hal->roc_entity_idx,
+				     RTW89_SUB_ENTITY_IDLE, idx);
+		if (cur != RTW89_SUB_ENTITY_IDLE) {
+			rtw89_debug(rtwdev, RTW89_DBG_TXRX,
+				    "ROC still processing on entity %d\n", idx);
+			return;
+		}
+
+		hal->roc_chandef = *chandef;
+	} else {
+		cur = atomic_cmpxchg(&hal->roc_entity_idx, idx,
+				     RTW89_SUB_ENTITY_IDLE);
+		if (cur == idx)
+			return;
+
+		if (cur == RTW89_SUB_ENTITY_IDLE)
+			rtw89_debug(rtwdev, RTW89_DBG_TXRX,
+				    "ROC already finished on entity %d\n", idx);
+		else
+			rtw89_debug(rtwdev, RTW89_DBG_TXRX,
+				    "ROC is processing on entity %d\n", cur);
+	}
+}
+
 static void rtw89_config_default_chandef(struct rtw89_dev *rtwdev)
 {
 	struct cfg80211_chan_def chandef = {0};
@@ -154,6 +186,7 @@ void rtw89_entity_init(struct rtw89_dev *rtwdev)
 	struct rtw89_hal *hal = &rtwdev->hal;
 
 	bitmap_zero(hal->entity_map, NUM_OF_RTW89_SUB_ENTITY);
+	atomic_set(&hal->roc_entity_idx, RTW89_SUB_ENTITY_IDLE);
 	rtw89_config_default_chandef(rtwdev);
 }
 
@@ -229,6 +262,8 @@ void rtw89_chanctx_ops_remove(struct rtw89_dev *rtwdev,
 			rtwvif->sub_entity_idx = RTW89_SUB_ENTITY_0;
 	}
 
+	atomic_cmpxchg(&hal->roc_entity_idx, roll, RTW89_SUB_ENTITY_0);
+
 	drop = roll;
 
 out:
drivers/net/wireless/realtek/rtw89/chan.h +3 -0
index ecbd4503bead9..bdf369db50417 100644
@@ -45,6 +45,9 @@ bool rtw89_assign_entity_chan(struct rtw89_dev *rtwdev,
 void rtw89_config_entity_chandef(struct rtw89_dev *rtwdev,
 				 enum rtw89_sub_entity_idx idx,
 				 const struct cfg80211_chan_def *chandef);
+void rtw89_config_roc_chandef(struct rtw89_dev *rtwdev,
+			      enum rtw89_sub_entity_idx idx,
+			      const struct cfg80211_chan_def *chandef);
 void rtw89_entity_init(struct rtw89_dev *rtwdev);
 enum rtw89_entity_mode rtw89_entity_recalc(struct rtw89_dev *rtwdev);
 int rtw89_chanctx_ops_add(struct rtw89_dev *rtwdev,
drivers/net/wireless/realtek/rtw89/core.c +211 -1

272 lines not on this page · raw message

drivers/net/wireless/realtek/rtw89/core.h +30 -0
index dd4165394c8de..f9093dc9e2b92 100644
@@ -569,6 +569,7 @@ enum rtw89_sub_entity_idx {
 	RTW89_SUB_ENTITY_0 = 0,
 
 	NUM_OF_RTW89_SUB_ENTITY,
+	RTW89_SUB_ENTITY_IDLE = NUM_OF_RTW89_SUB_ENTITY,
 };
 
 enum rtw89_rf_path {
@@ -2395,6 +2396,7 @@ struct rtw89_sta {
 	struct rtw89_addr_cam_entry addr_cam; /* AP mode or TDLS peer only */
 	struct rtw89_bssid_cam_entry bssid_cam; /* TDLS peer only */
 	struct list_head ba_cam_list;
+	struct sk_buff_head roc_queue;
 
 	bool use_cfg_mask;
 	struct cfg80211_bitrate_mask mask;
@@ -2434,11 +2436,28 @@ struct rtw89_tx_skb_data {
 	u8 hci_priv[];
 };
 
+#define RTW89_ROC_IDLE_TIMEOUT 500
+#define RTW89_ROC_TX_TIMEOUT 30
+enum rtw89_roc_state {
+	RTW89_ROC_IDLE,
+	RTW89_ROC_NORMAL,
+	RTW89_ROC_MGMT,
+};
+
+struct rtw89_roc {
+	struct ieee80211_channel chan;
+	struct delayed_work roc_work;
+	enum ieee80211_roc_type type;
+	enum rtw89_roc_state state;
+	int duration;
+};
+
 #define RTW89_P2P_MAX_NOA_NUM 2
 
 struct rtw89_vif {
 	struct list_head list;
 	struct rtw89_dev *rtwdev;
+	struct rtw89_roc roc;
 	enum rtw89_sub_entity_idx sub_entity_idx;
 
 	u8 mac_id;
@@ -2454,6 +2473,7 @@ struct rtw89_vif {
 	u8 bcn_hit_cond;
 	u8 hit_rule;
 	u8 last_noa_nr;
+	bool offchan;
 	bool trigger;
 	bool lsig_txop;
 	u8 tgt_ind;
@@ -3165,9 +3185,11 @@ struct rtw89_hal {
 	bool tx_path_diversity;
 	bool support_cckpd;
 	bool support_igi;
+	atomic_t roc_entity_idx;
 
 	DECLARE_BITMAP(entity_map, NUM_OF_RTW89_SUB_ENTITY);
 	struct rtw89_sub_entity sub[NUM_OF_RTW89_SUB_ENTITY];
+	struct cfg80211_chan_def roc_chandef;
 
 	bool entity_active;
 	enum rtw89_entity_mode entity_mode;
@@ -3828,6 +3850,7 @@ struct rtw89_dev {
 	struct delayed_work coex_rfk_chk_work;
 	struct delayed_work cfo_track_work;
 	struct delayed_work forbid_ba_work;
+	struct delayed_work roc_work;
 	struct rtw89_ppdu_sts_info ppdu_sts;
 	u8 total_sta_assoc;
 	bool scanning;
@@ -4343,6 +4366,10 @@ const struct cfg80211_chan_def *rtw89_chandef_get(struct rtw89_dev *rtwdev,
 						  enum rtw89_sub_entity_idx idx)
 {
 	struct rtw89_hal *hal = &rtwdev->hal;
+	enum rtw89_sub_entity_idx roc_idx = atomic_read(&hal->roc_entity_idx);
+
+	if (roc_idx == idx)
+		return &hal->roc_chandef;
 
 	return &hal->sub[idx].chandef;
 }
@@ -4719,6 +4746,9 @@ void rtw89_complete_cond(struct rtw89_wait_info *wait, unsigned int cond,
 int rtw89_core_start(struct rtw89_dev *rtwdev);
 void rtw89_core_stop(struct rtw89_dev *rtwdev);
 void rtw89_core_update_beacon_work(struct work_struct *work);
+void rtw89_roc_work(struct work_struct *work);
+void rtw89_roc_start(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif);
+void rtw89_roc_end(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif);
 void rtw89_core_scan_start(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
 			   const u8 *mac_addr, bool hw_scan);
 void rtw89_core_scan_complete(struct rtw89_dev *rtwdev,
drivers/net/wireless/realtek/rtw89/mac.c +4 -1
index 0e1471c34be00..8cceda1cf3994 100644
@@ -4191,6 +4191,9 @@ rtw89_mac_c2h_scanofld_rsp(struct rtw89_dev *rtwdev, struct sk_buff *c2h,
 	u16 chan;
 	int ret;
 
+	if (!rtwvif)
+		return;
+
 	tx_fail = RTW89_GET_MAC_C2H_SCANOFLD_TX_FAIL(c2h->data);
 	status = RTW89_GET_MAC_C2H_SCANOFLD_STATUS(c2h->data);
 	chan = RTW89_GET_MAC_C2H_SCANOFLD_PRI_CH(c2h->data);
@@ -4258,7 +4261,7 @@ rtw89_mac_bcn_fltr_rpt(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
 
 	switch (type) {
 	case RTW89_BCN_FLTR_BEACON_LOSS:
-		if (!rtwdev->scanning)
+		if (!rtwdev->scanning && !rtwvif->offchan)
 			ieee80211_connection_loss(vif);
 		else
 			rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, vif, true);
drivers/net/wireless/realtek/rtw89/mac80211.c +79 -1

137 lines not on this page · raw message

drivers/net/wireless/realtek/rtw89/ps.h +16 -0
index 6ac1f7ea53394..c9e29d92fc1b9 100644
@@ -15,4 +15,20 @@ void rtw89_leave_ips(struct rtw89_dev *rtwdev);
 void rtw89_set_coex_ctrl_lps(struct rtw89_dev *rtwdev, bool btc_ctrl);
 void rtw89_process_p2p_ps(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif);
 
+static inline void rtw89_leave_ips_by_hwflags(struct rtw89_dev *rtwdev)
+{
+	struct ieee80211_hw *hw = rtwdev->hw;
+
+	if (hw->conf.flags & IEEE80211_CONF_IDLE)
+		rtw89_leave_ips(rtwdev);
+}
+
+static inline void rtw89_enter_ips_by_hwflags(struct rtw89_dev *rtwdev)
+{
+	struct ieee80211_hw *hw = rtwdev->hw;
+
+	if (hw->conf.flags & IEEE80211_CONF_IDLE)
+		rtw89_enter_ips(rtwdev);
+}
+
 #endif
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/5] wifi: rtw89: 8852c: add beacon filter and CQM support

In reply to Ping-Ke Shih
Quoted 11 lines · Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

Adding this supports beacon filter and connection quality monitor. To make host CPU wake up less, let firmware perform signal monitoring and beacon processing, then notify driver upon signal changes or beacon loss.

This feature needs firmware 0.27.56 or newer to support it.

[...]

drivers/net/wireless/realtek/rtw89/core.c static void rtw89_vif_rx_stats_iter(void *data, u8 *mac, · line 1442

Quoted 8 lines
drivers/net/wireless/realtek/rtw89/core.c +2 -0
@@ -1438,6 +1438,8 @@ static void rtw89_vif_rx_stats_iter(void *data, u8 *mac,
 	struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
 	struct sk_buff *skb = iter_data->skb;
 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
+	struct rtw89_rx_phy_ppdu *phy_ppdu =
+		(struct rtw89_rx_phy_ppdu *)iter_data->phy_ppdu;

Why the cast? I don't think it's needed.

drivers/net/wireless/realtek/rtw89/core.c static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb) · line 3214

Quoted 12 lines
drivers/net/wireless/realtek/rtw89/core.c +8 -0
@@ -3181,6 +3204,15 @@ static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb)
 #define RTW89_GET_MAC_C2H_REV_ACK_H2C_SEQ(c2h) \
 	le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))
 
+#define RTW89_GET_MAC_BCNFLTR_RPT_MACID(c2h) \
+	le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(7, 0))
+#define RTW89_GET_MAC_BCNFLTR_RPT_TYPE(c2h) \
+	le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(9, 8))
+#define RTW89_GET_MAC_BCNFLTR_RPT_EVENT(c2h) \
+	le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(11, 10))
+#define RTW89_GET_MAC_BCNFLTR_RPT_MA(c2h) \
+	le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))

I have to admit that I every time I see this code pattern it makes me regret it. Something like what Arnd proposed back in the day would look so much cleaner:

https://lore.kernel.org/all/CAK8P3a1rsKZZKMKFTDWgE3usX9gYKJqUvTMxSdEuZrp8BaKdaA@mail.gmail.com/

Of course this is just a generic comment about rtw89, and has nothing to do with this patchset, but it would be great if someone could take a look and try out Arnd's proposal. It would be good to start with just one or two commands and send that as an RFC to see how it looks like.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#11 lore raw

Re: [PATCH 2/5] wifi: rtw89: add function to wait for completion of TX skbs

In reply to Ping-Ke Shih
Quoted 16 lines · Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

Allocate a per-skb completion to track those skbs we are interested in and wait for them to complete transmission with TX status. To avoid race condition between process and softirq without addtional locking, we use a work to free the tx_wait struct later when waiter is finished referencing it. This must be called in process context and with a timeout value greater than zero since it might sleep.

We use another workqueue so works can be processed concurrently and when the PCI device is removed unexpectedly, all pending works can be flushed. This prevents some works that were scheduled but never processed leads to memory leak.

[...]

Quoted 5 lines
drivers/net/wireless/realtek/rtw89/core.c +5 -0
+static void rtw89_core_free_tx_wait_work(struct work_struct *work)
+{
+	struct rtw89_tx_wait_info *wait =
+			container_of(work, struct rtw89_tx_wait_info, work);
+	struct rtw89_dev *rtwdev = wait->rtwdev;

drivers/net/wireless/realtek/rtw89/core.c void rtw89_core_tx_kick_off(struct rtw89_dev *rtwdev, u8 qsel) · line 866

Quoted 10 lines
drivers/net/wireless/realtek/rtw89/core.c +10 -0
+	int done, ret;
+
+	ret = read_poll_timeout(atomic_read, done, done, 1000, 100000, false,
+				&wait->wait_done);
+
+	if (ret)
+		rtw89_err(rtwdev, "tx wait timed out, stop polling\n");
+	else
+		kfree(wait);
+}

I admit I didn't try to understand this patch in detail but this function just looks odd to me. Why there's polling able to free something?

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
author maintainer
#3 lore raw

RE: [PATCH 1/5] wifi: rtw89: 8852c: add beacon filter and CQM support

In reply to Kalle Valo
Quoted 25 lines

-----Original Message-----
From: Kalle Valo <[email protected]>
Sent: Wednesday, March 15, 2023 4:32 PM
To: Ping-Ke Shih <[email protected]>
Cc: Bernie Huang <[email protected]>; [email protected]
Subject: Re: [PATCH 1/5] wifi: rtw89: 8852c: add beacon filter and CQM support

Quoted 11 lines · Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

Adding this supports beacon filter and connection quality monitor. To make host CPU wake up less, let firmware perform signal monitoring and beacon processing, then notify driver upon signal changes or beacon loss.

This feature needs firmware 0.27.56 or newer to support it.

[...]

Quoted 10 lines

drivers/net/wireless/realtek/rtw89/core.c static void rtw89_vif_rx_stats_iter(void *data, u8 *mac, · line 1442

Quoted 8 lines
drivers/net/wireless/realtek/rtw89/core.c +2 -0
@@ -1438,6 +1438,8 @@ static void rtw89_vif_rx_stats_iter(void *data, u8 *mac,
      struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
      struct sk_buff *skb = iter_data->skb;
      struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
+     struct rtw89_rx_phy_ppdu *phy_ppdu =
+             (struct rtw89_rx_phy_ppdu *)iter_data->phy_ppdu;

Why the cast? I don't think it's needed.

Will fix it by next version.

Quoted 18 lines

drivers/net/wireless/realtek/rtw89/core.c static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb) · line 3214

Quoted 13 lines
drivers/net/wireless/realtek/rtw89/core.c +8 -0
@@ -3181,6 +3204,15 @@ static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb)
 #define RTW89_GET_MAC_C2H_REV_ACK_H2C_SEQ(c2h) \
      le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))
 
+#define RTW89_GET_MAC_BCNFLTR_RPT_MACID(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(7, 0))
+#define RTW89_GET_MAC_BCNFLTR_RPT_TYPE(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(9, 8))
+#define RTW89_GET_MAC_BCNFLTR_RPT_EVENT(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(11, 10))
+#define RTW89_GET_MAC_BCNFLTR_RPT_MA(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))

I have to admit that I every time I see this code pattern it makes me regret it. Something like what Arnd proposed back in the day would look so much cleaner:

Quoted 7 lines

https://lore.kernel.org/all/CAK8P3a1rsKZZKMKFTDWgE3usX9gYKJqUvTMxSdEuZrp8BaKdaA@mail.gmail.com/

Of course this is just a generic comment about rtw89, and has nothing to do with this patchset, but it would be great if someone could take a look and try out Arnd's proposal. It would be good to start with just one or two commands and send that as an RFC to see how it looks like.

I write a draft RFC here. Please see if it's in expectation. If so, I can change all of them by another patch or RFC.

In header file:

#define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK GENMASK(7, 0) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_TYPE_MASK GENMASK(9, 8) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK GENMASK(11, 10) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK GENMASK(23, 16)

Access the values via le32_get_bits() in functions somewhere:

	const __le32 *c2h = skb->data;

	type =   le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);
	sig =    le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK) - MAX_RSSI;
	event =  le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK);
	mac_id = le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);

Ping-Ke

author maintainer
#4 lore raw

RE: [PATCH 1/5] wifi: rtw89: 8852c: add beacon filter and CQM support

Quoted 82 lines

-----Original Message-----
From: Ping-Ke Shih <[email protected]>
Sent: Wednesday, March 15, 2023 4:58 PM
To: Kalle Valo <[email protected]>
Cc: Bernie Huang <[email protected]>; [email protected]
Subject: RE: [PATCH 1/5] wifi: rtw89: 8852c: add beacon filter and CQM support

Quoted 23 lines

-----Original Message-----
From: Kalle Valo <[email protected]>
Sent: Wednesday, March 15, 2023 4:32 PM
To: Ping-Ke Shih <[email protected]>
Cc: Bernie Huang <[email protected]>; [email protected]
Subject: Re: [PATCH 1/5] wifi: rtw89: 8852c: add beacon filter and CQM support

Quoted 11 lines · Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

Adding this supports beacon filter and connection quality monitor. To make host CPU wake up less, let firmware perform signal monitoring and beacon processing, then notify driver upon signal changes or beacon loss.

This feature needs firmware 0.27.56 or newer to support it.

[...]

Quoted 10 lines

drivers/net/wireless/realtek/rtw89/core.c static void rtw89_vif_rx_stats_iter(void *data, u8 *mac, · line 1442

Quoted 8 lines
drivers/net/wireless/realtek/rtw89/core.c +2 -0
@@ -1438,6 +1438,8 @@ static void rtw89_vif_rx_stats_iter(void *data, u8 *mac,
      struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
      struct sk_buff *skb = iter_data->skb;
      struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
+     struct rtw89_rx_phy_ppdu *phy_ppdu =
+             (struct rtw89_rx_phy_ppdu *)iter_data->phy_ppdu;

Why the cast? I don't think it's needed.

Will fix it by next version.

Quoted 18 lines

drivers/net/wireless/realtek/rtw89/core.c static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb) · line 3214

Quoted 13 lines
drivers/net/wireless/realtek/rtw89/core.c +8 -0
@@ -3181,6 +3204,15 @@ static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb)
 #define RTW89_GET_MAC_C2H_REV_ACK_H2C_SEQ(c2h) \
      le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))
 
+#define RTW89_GET_MAC_BCNFLTR_RPT_MACID(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(7, 0))
+#define RTW89_GET_MAC_BCNFLTR_RPT_TYPE(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(9, 8))
+#define RTW89_GET_MAC_BCNFLTR_RPT_EVENT(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(11, 10))
+#define RTW89_GET_MAC_BCNFLTR_RPT_MA(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))

I have to admit that I every time I see this code pattern it makes me regret it. Something like what Arnd proposed back in the day would look so much cleaner:

Quoted 7 lines

https://lore.kernel.org/all/CAK8P3a1rsKZZKMKFTDWgE3usX9gYKJqUvTMxSdEuZrp8BaKdaA@mail.gmail.com/

Of course this is just a generic comment about rtw89, and has nothing to do with this patchset, but it would be great if someone could take a look and try out Arnd's proposal. It would be good to start with just one or two commands and send that as an RFC to see how it looks like.

I write a draft RFC here. Please see if it's in expectation. If so, I can change all of them by another patch or RFC.

In header file:

#define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK GENMASK(7, 0) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_TYPE_MASK GENMASK(9, 8) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK GENMASK(11, 10) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK GENMASK(23, 16)

Quoted 9 lines

Access the values via le32_get_bits() in functions somewhere:

	const __le32 *c2h = skb->data;

	type =   le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);
	sig =    le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK) - MAX_RSSI;
	event =  le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK);
	mac_id = le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);

I forget to propose a RFC for writing (e.g. H2C commands to firmware), and here they are:

	__le32 *h2c = skb->data;

#define RTW89_H2C_DISCONNECT_DETECT_W0_ENABLE BIT(0)
#define RTW89_H2C_DISCONNECT_DETECT_W0_TRYOK_BCNFAIL_COUNT_EN BIT(1) #define RTW89_H2C_DISCONNECT_DETECT_W0_DISCONNECT BIT(2)
#define RTW89_H2C_DISCONNECT_DETECT_W0_MAC_ID GENMASK(15, 8) #define RTW89_H2C_DISCONNECT_DETECT_W0_CHECK_PERIOD GENMASK(23, 16) #define RTW89_H2C_DISCONNECT_DETECT_W0_TRY_PKT_COUNT GENMASK(31, 24) #define RTW89_H2C_DISCONNECT_DETECT_W1_TRYOK_BCNFAIL_COUNT_LIMIT GENMASK(7, 0)

	h2c[0] = le32_encode_bits(enable, RTW89_H2C_DISCONNECT_DETECT_W0_ENABLE) |
		 le32_encode_bits(!enable, RTW89_H2C_DISCONNECT_DETECT_W0_DISCONNECT) |
		 le32_encode_bits(macid, RTW89_H2C_DISCONNECT_DETECT_W0_MAC_ID) |
		 le32_encode_bits(100, RTW89_H2C_DISCONNECT_DETECT_W0_CHECK_PERIOD);
	h2c[1] = le32_encode_bits(5, RTW89_H2C_DISCONNECT_DETECT_W1_TRYOK_BCNFAIL_COUNT_LIMIT);

Ping-Ke

author maintainer
#12 lore raw

RE: [PATCH 2/5] wifi: rtw89: add function to wait for completion of TX skbs

In reply to Kalle Valo
Quoted 41 lines

-----Original Message-----
From: Kalle Valo <[email protected]>
Sent: Wednesday, March 15, 2023 4:40 PM
To: Ping-Ke Shih <[email protected]>
Cc: Bernie Huang <[email protected]>; [email protected]
Subject: Re: [PATCH 2/5] wifi: rtw89: add function to wait for completion of TX skbs

Quoted 16 lines · Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

Allocate a per-skb completion to track those skbs we are interested in and wait for them to complete transmission with TX status. To avoid race condition between process and softirq without addtional locking, we use a work to free the tx_wait struct later when waiter is finished referencing it. This must be called in process context and with a timeout value greater than zero since it might sleep.

We use another workqueue so works can be processed concurrently and when the PCI device is removed unexpectedly, all pending works can be flushed. This prevents some works that were scheduled but never processed leads to memory leak.

[...]

Quoted 11 lines
drivers/net/wireless/realtek/rtw89/core.c +11 -0
+static void rtw89_core_free_tx_wait_work(struct work_struct *work)
+{
+     struct rtw89_tx_wait_info *wait =
+                     container_of(work, struct rtw89_tx_wait_info, work);
+     struct rtw89_dev *rtwdev = wait->rtwdev;
+     int done, ret;
+
+     ret = read_poll_timeout(atomic_read, done, done, 1000, 100000, false,
+                             &wait->wait_done);
+
+     if (ret)
Quoted 9 lines

drivers/net/wireless/realtek/rtw89/core.c void rtw89_core_tx_kick_off(struct rtw89_dev *rtwdev, u8 qsel) · line 866

Quoted 4 lines
drivers/net/wireless/realtek/rtw89/core.c +4 -0
+             rtw89_err(rtwdev, "tx wait timed out, stop polling\n");
+     else
+             kfree(wait);
+}

I admit I didn't try to understand this patch in detail but this function just looks odd to me. Why there's polling able to free something?

Three works are involved in the "wait/completion".

work 1. remain-on-channel work
It trigger TX null data and wait (kmalloc 'wait' object, and wait for completion)

work 2. TX completion by napi_poll
It returns TX status (failed or succeed to TX), and complete the 'wait' object, and queue rtw89_core_free_tx_wait_work() to free 'wait' object.

We queue this by work 2, because time of work 1 is predictable, but it is hard to estimate time of work 2. The read_poll_timeout() is for the work 1 predictable time.

work 3. This work is to do garbage collection of 'wait' object
It polls if work 1 is done before doing free 'wait' object.

Things are complex because work 1 and 2 are done asynchronously, so one of them can't free 'wait' object, or it will causes use-after-free in other side.

Use case 1: (work 1 could free 'wait' object)
work 1 work 2 work 3
wait
completion
wait ok
free 'wait'

Use case 2: (work 2 could free 'wait' object)
work 1 work 2 work 3
wait
wait timeout
completion
free 'wait'

I can add a comment as a hint why we can use a read_poll_timeout() to assist in freeing something.

Ping-Ke

author maintainer
#5 lore raw

Re: [PATCH 1/5] wifi: rtw89: 8852c: add beacon filter and CQM support

In reply to Ping-Ke Shih
Quoted 103 lines · Ping-Ke Shih
Quoted 80 lines

-----Original Message-----
From: Ping-Ke Shih <[email protected]>
Sent: Wednesday, March 15, 2023 4:58 PM
To: Kalle Valo <[email protected]>
Cc: Bernie Huang <[email protected]>; [email protected]
Subject: RE: [PATCH 1/5] wifi: rtw89: 8852c: add beacon filter and CQM support

Quoted 23 lines

-----Original Message-----
From: Kalle Valo <[email protected]>
Sent: Wednesday, March 15, 2023 4:32 PM
To: Ping-Ke Shih <[email protected]>
Cc: Bernie Huang <[email protected]>; [email protected]
Subject: Re: [PATCH 1/5] wifi: rtw89: 8852c: add beacon filter and CQM support

Quoted 11 lines · Ping-Ke Shih

From: Po-Hao Huang <[email protected]>

Adding this supports beacon filter and connection quality monitor. To make host CPU wake up less, let firmware perform signal monitoring and beacon processing, then notify driver upon signal changes or beacon loss.

This feature needs firmware 0.27.56 or newer to support it.

[...]

Quoted 10 lines

drivers/net/wireless/realtek/rtw89/core.c static void rtw89_vif_rx_stats_iter(void *data, u8 *mac, · line 1442

Quoted 8 lines
drivers/net/wireless/realtek/rtw89/core.c +2 -0
@@ -1438,6 +1438,8 @@ static void rtw89_vif_rx_stats_iter(void *data, u8 *mac,
      struct rtw89_rx_desc_info *desc_info = iter_data->desc_info;
      struct sk_buff *skb = iter_data->skb;
      struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
+     struct rtw89_rx_phy_ppdu *phy_ppdu =
+             (struct rtw89_rx_phy_ppdu *)iter_data->phy_ppdu;

Why the cast? I don't think it's needed.

Will fix it by next version.

Quoted 18 lines

drivers/net/wireless/realtek/rtw89/core.c static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct · line 3215

Quoted 13 lines
drivers/net/wireless/realtek/rtw89/core.c +8 -0
@@ -3181,6 +3204,15 @@ static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct
 sk_buff *skb)
 #define RTW89_GET_MAC_C2H_REV_ACK_H2C_SEQ(c2h) \
      le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))
 
+#define RTW89_GET_MAC_BCNFLTR_RPT_MACID(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(7, 0))
+#define RTW89_GET_MAC_BCNFLTR_RPT_TYPE(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(9, 8))
+#define RTW89_GET_MAC_BCNFLTR_RPT_EVENT(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(11, 10))
+#define RTW89_GET_MAC_BCNFLTR_RPT_MA(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))

I have to admit that I every time I see this code pattern it makes me regret it. Something like what Arnd proposed back in the day would look so much cleaner:

Quoted 7 lines

https://lore.kernel.org/all/CAK8P3a1rsKZZKMKFTDWgE3usX9gYKJqUvTMxSdEuZrp8BaKdaA@mail.gmail.com/

Of course this is just a generic comment about rtw89, and has nothing to do with this patchset, but it would be great if someone could take a look and try out Arnd's proposal. It would be good to start with just one or two commands and send that as an RFC to see how it looks like.

I write a draft RFC here. Please see if it's in expectation. If so, I can change all of them by another patch or RFC.

In header file:

#define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK GENMASK(7, 0) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_TYPE_MASK GENMASK(9, 8) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK GENMASK(11, 10) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK GENMASK(23, 16)

Quoted 9 lines

Access the values via le32_get_bits() in functions somewhere:

	const __le32 *c2h = skb->data;

	type =   le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);
	sig =    le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK) - MAX_RSSI;
	event =  le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK);
	mac_id = le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);

I forget to propose a RFC for writing (e.g. H2C commands to firmware), and here they are:

	__le32 *h2c = skb->data;

#define RTW89_H2C_DISCONNECT_DETECT_W0_ENABLE BIT(0)
#define RTW89_H2C_DISCONNECT_DETECT_W0_TRYOK_BCNFAIL_COUNT_EN BIT(1) #define RTW89_H2C_DISCONNECT_DETECT_W0_DISCONNECT BIT(2)
#define RTW89_H2C_DISCONNECT_DETECT_W0_MAC_ID GENMASK(15, 8) #define RTW89_H2C_DISCONNECT_DETECT_W0_CHECK_PERIOD GENMASK(23, 16) #define RTW89_H2C_DISCONNECT_DETECT_W0_TRY_PKT_COUNT GENMASK(31, 24) #define RTW89_H2C_DISCONNECT_DETECT_W1_TRYOK_BCNFAIL_COUNT_LIMIT GENMASK(7, 0)

Quoted 6 lines
	h2c[0] = le32_encode_bits(enable, RTW89_H2C_DISCONNECT_DETECT_W0_ENABLE) |
		 le32_encode_bits(!enable, RTW89_H2C_DISCONNECT_DETECT_W0_DISCONNECT) |
		 le32_encode_bits(macid, RTW89_H2C_DISCONNECT_DETECT_W0_MAC_ID) |
		 le32_encode_bits(100, RTW89_H2C_DISCONNECT_DETECT_W0_CHECK_PERIOD);
	h2c[1] = le32_encode_bits(5, RTW89_H2C_DISCONNECT_DETECT_W1_TRYOK_BCNFAIL_COUNT_LIMIT);

I have sent v3 (v2 is messed up) with above style and it looks more clear. If this is okay, I will convert existing code to this style.

Ping-Ke

#6 lore raw

rtw88/rtw89: command/event structure handling

In reply to Ping-Ke Shih

(changing the subject and adding Arnd)

Quoted 38 lines · Ping-Ke Shih
Quoted 17 lines

drivers/net/wireless/realtek/rtw89/fw.h static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb) · line 3214

Quoted 12 lines
drivers/net/wireless/realtek/rtw89/fw.h +8 -0
@@ -3181,6 +3204,15 @@ static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb)
 #define RTW89_GET_MAC_C2H_REV_ACK_H2C_SEQ(c2h) \
      le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))
 
+#define RTW89_GET_MAC_BCNFLTR_RPT_MACID(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(7, 0))
+#define RTW89_GET_MAC_BCNFLTR_RPT_TYPE(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(9, 8))
+#define RTW89_GET_MAC_BCNFLTR_RPT_EVENT(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(11, 10))
+#define RTW89_GET_MAC_BCNFLTR_RPT_MA(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))

I have to admit that I every time I see this code pattern it makes me regret it. Something like what Arnd proposed back in the day would look so much cleaner:

Quoted 7 lines

https://lore.kernel.org/all/CAK8P3a1rsKZZKMKFTDWgE3usX9gYKJqUvTMxSdEuZrp8BaKdaA@mail.gmail.com/

Of course this is just a generic comment about rtw89, and has nothing to do with this patchset, but it would be great if someone could take a look and try out Arnd's proposal. It would be good to start with just one or two commands and send that as an RFC to see how it looks like.

I write a draft RFC here. Please see if it's in expectation. If so, I can change all of them by another patch or RFC.

In header file:

#define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK GENMASK(7, 0) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_TYPE_MASK GENMASK(9, 8) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK GENMASK(11, 10) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK GENMASK(23, 16)

Access the values via le32_get_bits() in functions somewhere:

Quoted 7 lines
	const __le32 *c2h = skb->data;

	type =   le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);
	sig = le32_get_bits(c2h[2],
RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK) - MAX_RSSI;
	event =  le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK);
	mac_id = le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);

I was thinking more something towards Arnd's idea he suggests in [1]. Here's my proposal for the beacon filter command as pseudo code (so not compiled and very much buggy!) from the patch[2] which started this recent discussion.

So in the header file we would have something like this:

#define RTW89_C2H_BEACON_FILTER_WORD0_MACID_MASK GENMASK(7, 0) #define RTW89_C2H_BEACON_FILTER_WORD0_TYPE_MASK GENMASK(9, 8) #define RTW89_C2H_BEACON_FILTER_WORD0_EVENT_MASK GENMASK(11, 10) #define RTW89_C2H_BEACON_FILTER_WORD0_MA_MASK GENMASK(23, 16)

struct rtw89_h2c_cfg_beacon_filter {
     __le32 word0;
}

static inline void rtw89_h2c_cfg_beacon_filter_set_word0(struct rtw89_h2c_cfg_beacon_filter *cmd,

        u32 macid, u32 type, u32 event_mask, u32 ma)
{
        le32_replace_bits(cmd->word0, macid, RTW89_C2H_BEACON_FILTER_WORD0_MACID_MASK);
        le32_replace_bits(cmd->word0, type, RTW89_C2H_BEACON_FILTER_WORD0_TYPE_MASK);
        le32_replace_bits(cmd->word0, event, RTW89_C2H_BEACON_FILTER_WORD0_EVENT_MASK);
        le32_replace_bits(cmd->word0, ma, RTW89_C2H_BEACON_FILTER_WORD0_MA_MASK);
}

static inline u32 rtw89_h2c_cfg_beacon_filter_get_mac_id(const struct rtw89_h2c_cfg_beacon_filter *cmd)

{
        return le32_get_bits(cmd->word0, RTW89_C2H_BEACON_FILTER_WORD0_MACID_MASK);
}

And an example how to use these:

struct rtw89_h2c_cfg_beacon_filter *cmd;

skb = rtw89_fw_h2c_alloc_skb_with_hdr(rtwdev, sizeof(*cmd)); cmd = (struct rtw89_h2c_cfg_beacon_filter *)skb->data; rtw89_h2c_cfg_beacon_filter_set_word0(cmd, 1, 2, 0, 0);

I'm sure this is very buggy and I'm missing a lot but I hope you get the idea anyway. My keypoints here are:

* there's a clear struct for the command (an "object" from OOP point of view), something like "__le32 *c2h" is very confusing
* no casting
* no pointer arithmetic
* you get length with a simple "sizeof(*cmd)"

Downside of course is that there's quite a lot of boilerplate code but I still consider that positives outweight the negatives. Thoughts?

And I'll emphasise that this is not a blocker for anything but it would be nice to clean this up both in rtw88 and rtw89 at some point, if we can.

[1] https://lore.kernel.org/all/CAK8P3a1rsKZZKMKFTDWgE3usX9gYKJqUvTMxSdEuZrp8BaKdaA@mail.gmail.com/

[2] https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#13 lore raw

Re: [PATCH 2/5] wifi: rtw89: add function to wait for completion of TX skbs

In reply to Ping-Ke Shih
Quoted 57 lines · Ping-Ke Shih
Quoted 11 lines
Quoted 11 lines
drivers/net/wireless/realtek/rtw89/core.c +11 -0
+static void rtw89_core_free_tx_wait_work(struct work_struct *work)
+{
+     struct rtw89_tx_wait_info *wait =
+                     container_of(work, struct rtw89_tx_wait_info, work);
+     struct rtw89_dev *rtwdev = wait->rtwdev;
+     int done, ret;
+
+     ret = read_poll_timeout(atomic_read, done, done, 1000, 100000, false,
+                             &wait->wait_done);
+
+     if (ret)
Quoted 9 lines

drivers/net/wireless/realtek/rtw89/core.c void rtw89_core_tx_kick_off(struct rtw89_dev *rtwdev, u8 qsel) · line 866

Quoted 4 lines
drivers/net/wireless/realtek/rtw89/core.c +4 -0
+             rtw89_err(rtwdev, "tx wait timed out, stop polling\n");
+     else
+             kfree(wait);
+}

I admit I didn't try to understand this patch in detail but this function just looks odd to me. Why there's polling able to free something?

Three works are involved in the "wait/completion".

work 1. remain-on-channel work
It trigger TX null data and wait (kmalloc 'wait' object, and wait for completion)

work 2. TX completion by napi_poll
It returns TX status (failed or succeed to TX), and complete the 'wait' object, and queue rtw89_core_free_tx_wait_work() to free 'wait' object.

We queue this by work 2, because time of work 1 is predictable, but it is hard to estimate time of work 2. The read_poll_timeout() is for the work 1 predictable time.

work 3. This work is to do garbage collection of 'wait' object
It polls if work 1 is done before doing free 'wait' object.

Things are complex because work 1 and 2 are done asynchronously, so one of them can't free 'wait' object, or it will causes use-after-free in other side.

Use case 1: (work 1 could free 'wait' object)
work 1 work 2 work 3
wait
completion
wait ok
free 'wait'

Use case 2: (work 2 could free 'wait' object)
work 1 work 2 work 3
wait
wait timeout
completion
free 'wait'

Quoted 2 lines

I can add a comment as a hint why we can use a read_poll_timeout() to assist in freeing something.

I would expect that there's polling if you are waiting something from hardware, or maybe when implementing a spin lock, but not when waiting for another kernel thread. This just doesn't feel right but I don't have time to propose a good alternative either, sorry.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#7 lore raw

Re: rtw88/rtw89: command/event structure handling

In reply to Kalle Valo
Quoted 98 lines · Kalle Valo

(changing the subject and adding Arnd)

Quoted 38 lines · Ping-Ke Shih
Quoted 17 lines

drivers/net/wireless/realtek/rtw89/fw.h static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb) · line 3214

Quoted 12 lines
drivers/net/wireless/realtek/rtw89/fw.h +8 -0
@@ -3181,6 +3204,15 @@ static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct sk_buff *skb)
 #define RTW89_GET_MAC_C2H_REV_ACK_H2C_SEQ(c2h) \
      le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))
 
+#define RTW89_GET_MAC_BCNFLTR_RPT_MACID(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(7, 0))
+#define RTW89_GET_MAC_BCNFLTR_RPT_TYPE(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(9, 8))
+#define RTW89_GET_MAC_BCNFLTR_RPT_EVENT(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(11, 10))
+#define RTW89_GET_MAC_BCNFLTR_RPT_MA(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))

I have to admit that I every time I see this code pattern it makes me regret it. Something like what Arnd proposed back in the day would look so much cleaner:

Quoted 7 lines

https://lore.kernel.org/all/CAK8P3a1rsKZZKMKFTDWgE3usX9gYKJqUvTMxSdEuZrp8BaKdaA@mail.gmail.com/

Of course this is just a generic comment about rtw89, and has nothing to do with this patchset, but it would be great if someone could take a look and try out Arnd's proposal. It would be good to start with just one or two commands and send that as an RFC to see how it looks like.

I write a draft RFC here. Please see if it's in expectation. If so, I can change all of them by another patch or RFC.

In header file:

#define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK GENMASK(7, 0) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_TYPE_MASK GENMASK(9, 8) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK GENMASK(11, 10) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK GENMASK(23, 16)

Access the values via le32_get_bits() in functions somewhere:

Quoted 7 lines
	const __le32 *c2h = skb->data;

	type =   le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);
	sig = le32_get_bits(c2h[2],
RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK) - MAX_RSSI;
	event =  le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK);
	mac_id = le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);

I was thinking more something towards Arnd's idea he suggests in [1]. Here's my proposal for the beacon filter command as pseudo code (so not compiled and very much buggy!) from the patch[2] which started this recent discussion.

So in the header file we would have something like this:

#define RTW89_C2H_BEACON_FILTER_WORD0_MACID_MASK GENMASK(7, 0) #define RTW89_C2H_BEACON_FILTER_WORD0_TYPE_MASK GENMASK(9, 8) #define RTW89_C2H_BEACON_FILTER_WORD0_EVENT_MASK GENMASK(11, 10) #define RTW89_C2H_BEACON_FILTER_WORD0_MA_MASK GENMASK(23, 16)

struct rtw89_h2c_cfg_beacon_filter {
     __le32 word0;
}

static inline void rtw89_h2c_cfg_beacon_filter_set_word0(struct rtw89_h2c_cfg_beacon_filter *cmd,

        u32 macid, u32 type, u32 event_mask, u32 ma)
{
        le32_replace_bits(cmd->word0, macid, RTW89_C2H_BEACON_FILTER_WORD0_MACID_MASK);
        le32_replace_bits(cmd->word0, type, RTW89_C2H_BEACON_FILTER_WORD0_TYPE_MASK);
        le32_replace_bits(cmd->word0, event, RTW89_C2H_BEACON_FILTER_WORD0_EVENT_MASK);
        le32_replace_bits(cmd->word0, ma, RTW89_C2H_BEACON_FILTER_WORD0_MA_MASK);
}

static inline u32 rtw89_h2c_cfg_beacon_filter_get_mac_id(const struct rtw89_h2c_cfg_beacon_filter *cmd)

{
        return le32_get_bits(cmd->word0, RTW89_C2H_BEACON_FILTER_WORD0_MACID_MASK);
}

And an example how to use these:

struct rtw89_h2c_cfg_beacon_filter *cmd;

skb = rtw89_fw_h2c_alloc_skb_with_hdr(rtwdev, sizeof(*cmd)); cmd = (struct rtw89_h2c_cfg_beacon_filter *)skb->data; rtw89_h2c_cfg_beacon_filter_set_word0(cmd, 1, 2, 0, 0);

I'm sure this is very buggy and I'm missing a lot but I hope you get the idea anyway. My keypoints here are:

* there's a clear struct for the command (an "object" from OOP point of view), something like "__le32 *c2h" is very confusing
* no casting
* no pointer arithmetic
* you get length with a simple "sizeof(*cmd)"

Quoted 6 lines

Downside of course is that there's quite a lot of boilerplate code but I still consider that positives outweight the negatives. Thoughts?

And I'll emphasise that this is not a blocker for anything but it would be nice to clean this up both in rtw88 and rtw89 at some point, if we can.

Heh, I didn't notice that Ping had done almost the same in v4:

https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

The only difference I notice that you didn't use special functions for setting or getting the fields:

	h2c->w0 = le32_encode_bits(connect, RTW89_H2C_BCNFLTR_W0_MON_RSSI) |
		  le32_encode_bits(connect, RTW89_H2C_BCNFLTR_W0_MON_BCN) |
		  le32_encode_bits(connect, RTW89_H2C_BCNFLTR_W0_MON_EN) |
		  le32_encode_bits(RTW89_BCN_FLTR_OFFLOAD_MODE_DEFAULT,
				   RTW89_H2C_BCNFLTR_W0_MODE) |
		  le32_encode_bits(RTW89_BCN_LOSS_CNT, RTW89_H2C_BCNFLTR_W0_BCN_LOSS_CNT) |
		  le32_encode_bits(bss_conf->cqm_rssi_hyst, RTW89_H2C_BCNFLTR_W0_RSSI_HYST) |
		  le32_encode_bits(bss_conf->cqm_rssi_thold + MAX_RSSI,
				   RTW89_H2C_BCNFLTR_W0_RSSI_THRESHOLD) |
		  le32_encode_bits(rtwvif->mac_id, RTW89_H2C_BCNFLTR_W0_MAC_ID);

And I understand why you did it like this, less boilerplate code. So looks good to me, thanks!

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
author maintainer
#8 lore raw

Re: rtw88/rtw89: command/event structure handling

Quoted 95 lines · Kalle Valo
Quoted 92 lines · Kalle Valo

(changing the subject and adding Arnd)

Quoted 39 lines · Ping-Ke Shih
Quoted 18 lines

drivers/net/wireless/realtek/rtw89/fw.h static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct · line 3215

Quoted 13 lines
drivers/net/wireless/realtek/rtw89/fw.h +8 -0
@@ -3181,6 +3204,15 @@ static inline struct rtw89_fw_c2h_attr *RTW89_SKB_C2H_CB(struct
 sk_buff *skb)
 #define RTW89_GET_MAC_C2H_REV_ACK_H2C_SEQ(c2h) \
      le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))
 
+#define RTW89_GET_MAC_BCNFLTR_RPT_MACID(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(7, 0))
+#define RTW89_GET_MAC_BCNFLTR_RPT_TYPE(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(9, 8))
+#define RTW89_GET_MAC_BCNFLTR_RPT_EVENT(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(11, 10))
+#define RTW89_GET_MAC_BCNFLTR_RPT_MA(c2h) \
+     le32_get_bits(*((const __le32 *)(c2h) + 2), GENMASK(23, 16))

I have to admit that I every time I see this code pattern it makes me regret it. Something like what Arnd proposed back in the day would look so much cleaner:

Quoted 7 lines

https://lore.kernel.org/all/CAK8P3a1rsKZZKMKFTDWgE3usX9gYKJqUvTMxSdEuZrp8BaKdaA@mail.gmail.com/

Of course this is just a generic comment about rtw89, and has nothing to do with this patchset, but it would be great if someone could take a look and try out Arnd's proposal. It would be good to start with just one or two commands and send that as an RFC to see how it looks like.

I write a draft RFC here. Please see if it's in expectation. If so, I can change all of them by another patch or RFC.

In header file:

#define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK GENMASK(7, 0) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_TYPE_MASK GENMASK(9, 8) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK GENMASK(11, 10) #define RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK GENMASK(23, 16)

Access the values via le32_get_bits() in functions somewhere:

Quoted 7 lines
     const __le32 *c2h = skb->data;

     type =   le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);
     sig = le32_get_bits(c2h[2],
RTW89_C2H_MAC_BCNFLTR_RPT_W2_MA_MASK) - MAX_RSSI;
     event =  le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_EVENT_MASK);
     mac_id = le32_get_bits(c2h[2], RTW89_C2H_MAC_BCNFLTR_RPT_W2_MACID_MASK);

I was thinking more something towards Arnd's idea he suggests in [1]. Here's my proposal for the beacon filter command as pseudo code (so not compiled and very much buggy!) from the patch[2] which started this recent discussion.

So in the header file we would have something like this:

#define RTW89_C2H_BEACON_FILTER_WORD0_MACID_MASK GENMASK(7, 0) #define RTW89_C2H_BEACON_FILTER_WORD0_TYPE_MASK GENMASK(9, 8) #define RTW89_C2H_BEACON_FILTER_WORD0_EVENT_MASK GENMASK(11, 10) #define RTW89_C2H_BEACON_FILTER_WORD0_MA_MASK GENMASK(23, 16)

struct rtw89_h2c_cfg_beacon_filter {
     __le32 word0;
}

static inline void rtw89_h2c_cfg_beacon_filter_set_word0(struct rtw89_h2c_cfg_beacon_filter *cmd,

        u32 macid, u32 type, u32 event_mask, u32 ma)
{
        le32_replace_bits(cmd->word0, macid, RTW89_C2H_BEACON_FILTER_WORD0_MACID_MASK);
        le32_replace_bits(cmd->word0, type, RTW89_C2H_BEACON_FILTER_WORD0_TYPE_MASK);
        le32_replace_bits(cmd->word0, event, RTW89_C2H_BEACON_FILTER_WORD0_EVENT_MASK);
        le32_replace_bits(cmd->word0, ma, RTW89_C2H_BEACON_FILTER_WORD0_MA_MASK);
}

static inline u32 rtw89_h2c_cfg_beacon_filter_get_mac_id(const struct rtw89_h2c_cfg_beacon_filter *cmd)

{
        return le32_get_bits(cmd->word0, RTW89_C2H_BEACON_FILTER_WORD0_MACID_MASK);
}

And an example how to use these:

struct rtw89_h2c_cfg_beacon_filter *cmd;

skb = rtw89_fw_h2c_alloc_skb_with_hdr(rtwdev, sizeof(*cmd)); cmd = (struct rtw89_h2c_cfg_beacon_filter *)skb->data; rtw89_h2c_cfg_beacon_filter_set_word0(cmd, 1, 2, 0, 0);

Quoted 8 lines
Quoted 8 lines

I'm sure this is very buggy and I'm missing a lot but I hope you get the idea anyway. My keypoints here are:

* there's a clear struct for the command (an "object" from OOP point of view), something like "__le32 *c2h" is very confusing
* no casting
* no pointer arithmetic
* you get length with a simple "sizeof(*cmd)"

Super appreciate to propose pseudo code and these clear rules to us. :-) I will record these rules in our internal wiki page.

Quoted 7 lines
Quoted 7 lines

Downside of course is that there's quite a lot of boilerplate code but I still consider that positives outweight the negatives. Thoughts?

And I'll emphasise that this is not a blocker for anything but it would be nice to clean this up both in rtw88 and rtw89 at some point, if we can.

Since they will be a lot of works and I have a lot of local patches on hand, can I apply these rules to the patches and submit them ahead? Until all things or a bunch of conversion are completed (maybe weeks or one or two months later), I can submit patches that only convert these H2C/C2H with new rules.

Does it work to you?

Quoted 19 lines

Heh, I didn't notice that Ping had done almost the same in v4:

https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

The only difference I notice that you didn't use special functions for setting or getting the fields:

        h2c->w0 = le32_encode_bits(connect, RTW89_H2C_BCNFLTR_W0_MON_RSSI) |
                  le32_encode_bits(connect, RTW89_H2C_BCNFLTR_W0_MON_BCN) |
                  le32_encode_bits(connect, RTW89_H2C_BCNFLTR_W0_MON_EN) |
                  le32_encode_bits(RTW89_BCN_FLTR_OFFLOAD_MODE_DEFAULT,
                                   RTW89_H2C_BCNFLTR_W0_MODE) |
                  le32_encode_bits(RTW89_BCN_LOSS_CNT, RTW89_H2C_BCNFLTR_W0_BCN_LOSS_CNT) |
                  le32_encode_bits(bss_conf->cqm_rssi_hyst, RTW89_H2C_BCNFLTR_W0_RSSI_HYST) |
                  le32_encode_bits(bss_conf->cqm_rssi_thold + MAX_RSSI,
                                   RTW89_H2C_BCNFLTR_W0_RSSI_THRESHOLD) |
                  le32_encode_bits(rtwvif->mac_id, RTW89_H2C_BCNFLTR_W0_MAC_ID);
Quoted 2 lines

And I understand why you did it like this, less boilerplate code. So looks good to me, thanks!

Sorry, I forget to mention this in original discussion thread. As I mentioned in v4, I will open mind to convert them again if any suggestion to improve these things in the future.

Appreciate your suggestion again.

Ping-Ke

#9 lore raw

Re: rtw88/rtw89: command/event structure handling

Quoted 7 lines · Ping-Ke Shih
Quoted 6 lines
Quoted 6 lines

Downside of course is that there's quite a lot of boilerplate code but I still consider that positives outweight the negatives. Thoughts?

And I'll emphasise that this is not a blocker for anything but it would be nice to clean this up both in rtw88 and rtw89 at some point, if we can.

Quoted 7 lines

Since they will be a lot of works and I have a lot of local patches on hand, can I apply these rules to the patches and submit them ahead? Until all things or a bunch of conversion are completed (maybe weeks or one or two months later), I can submit patches that only convert these H2C/C2H with new rules.

Does it work to you?

Yes, that's totally fine. No rush with this cleanup. Thanks for working on this.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
author maintainer
#14 lore raw

Re: [PATCH 2/5] wifi: rtw89: add function to wait for completion of TX skbs

In reply to Kalle Valo
Quoted 6 lines · Kalle Valo

I would expect that there's polling if you are waiting something from hardware, or maybe when implementing a spin lock, but not when waiting for another kernel thread. This just doesn't feel right but I don't have time to propose a good alternative either, sorry.

I have found a solution that uses an owner variable with a spin lock to determine which side to free completion object. Simply show two use cases below:

Use case 1: (normal case; free completion object by work 1)

    work 1                                work 2
    wait
                                          (spin_lock)
                                          complete
                                          check & set owner --> owner = work1
		                          (spin_unlock)
    wait ok
    (spin_lock)
    check & check owner --> free by work 1
    (spin_unlock)
    free completion

Use case 2: (timeout case; free completion object by work 2)

    work 1                                work 2
    wait
    wait timeout
    (spin_lock)
    check & set owner --> owner = work 2
    (spin_unlock)
                                         (spin_lock)
                                         completion
                                         check & set owner --> free by work 2
                                         (spin_unlock)
                                         free completion

I will apply this by v5.

Ping-Ke

author maintainer
#15 lore raw

Re: [PATCH 2/5] wifi: rtw89: add function to wait for completion of TX skbs

Quoted 38 lines · Ping-Ke Shih
Quoted 5 lines · Kalle Valo

I would expect that there's polling if you are waiting something from hardware, or maybe when implementing a spin lock, but not when waiting for another kernel thread. This just doesn't feel right but I don't have time to propose a good alternative either, sorry.

I have found a solution that uses an owner variable with a spin lock to determine which side to free completion object. Simply show two use cases below:

Use case 1: (normal case; free completion object by work 1)

    work 1                                work 2
    wait
                                          (spin_lock)
                                          complete
                                          check & set owner --> owner = work1
		                          (spin_unlock)
    wait ok
    (spin_lock)
    check & check owner --> free by work 1
    (spin_unlock)
    free completion

Use case 2: (timeout case; free completion object by work 2)

    work 1                                work 2
    wait
    wait timeout
    (spin_lock)
    check & set owner --> owner = work 2
    (spin_unlock)
                                         (spin_lock)
                                         completion
                                         check & set owner --> free by work 2
                                         (spin_unlock)
                                         free completion
Quoted 2 lines

I will apply this by v5.

We have a better idea that use kfree_rcu() to free completion, so no need spin_lock(). Then, the use cases become below, and I have sent this change by v6.

Use case 1: (normal case)

    work 1                                work 2
    (rcu_assign_pointer(wait))
    wait
                                          (rcu_read_lock)
                                          wait = rcu_dereference();
                                          if (wait)    --> wait != NULL
                                              complete
                                          (rcu_read_unlock)
    wait ok
    (rcu_assign_pointer(NULL))
    kfree_rcu(completion)

Use case 2: (timeout case)

    work 1                                work 2
    (rcu_assign_pointer(wait))
    wait
    wait timeout
    (rcu_assign_pointer(NULL))
    kfree_rcu(completion)
                                          (rcu_read_lock)
                                          wait = rcu_dereference();
                                          if (wait)    --> wait == NULL
                                              complete
                                          (rcu_read_unlock)

Ping-Ke

#16 lore raw

Re: [PATCH 2/5] wifi: rtw89: add function to wait for completion of TX skbs

Quoted 62 lines · Ping-Ke Shih
Quoted 38 lines · Ping-Ke Shih
Quoted 5 lines · Kalle Valo

I would expect that there's polling if you are waiting something from hardware, or maybe when implementing a spin lock, but not when waiting for another kernel thread. This just doesn't feel right but I don't have time to propose a good alternative either, sorry.

I have found a solution that uses an owner variable with a spin lock to determine which side to free completion object. Simply show two use cases below:

Use case 1: (normal case; free completion object by work 1)

    work 1                                work 2
    wait
                                          (spin_lock)
                                          complete
                                          check & set owner --> owner = work1
		                          (spin_unlock)
    wait ok
    (spin_lock)
    check & check owner --> free by work 1
    (spin_unlock)
    free completion

Use case 2: (timeout case; free completion object by work 2)

    work 1                                work 2
    wait
    wait timeout
    (spin_lock)
    check & set owner --> owner = work 2
    (spin_unlock)
                                         (spin_lock)
                                         completion
                                         check & set owner --> free by work 2
                                         (spin_unlock)
                                         free completion
Quoted 2 lines

I will apply this by v5.

We have a better idea that use kfree_rcu() to free completion, so no need spin_lock(). Then, the use cases become below, and I have sent this change by v6.

Use case 1: (normal case)

    work 1                                work 2
    (rcu_assign_pointer(wait))
    wait
                                          (rcu_read_lock)
                                          wait = rcu_dereference();
                                          if (wait)    --> wait != NULL
                                              complete
                                          (rcu_read_unlock)
    wait ok
    (rcu_assign_pointer(NULL))
    kfree_rcu(completion)

Use case 2: (timeout case)

Quoted 11 lines
    work 1                                work 2
    (rcu_assign_pointer(wait))
    wait
    wait timeout
    (rcu_assign_pointer(NULL))
    kfree_rcu(completion)
                                          (rcu_read_lock)
                                          wait = rcu_dereference();
                                          if (wait)    --> wait == NULL
                                              complete
                                          (rcu_read_unlock)

Thanks, I haven't had a chance to look at v6 yet (nor v5 for that matter) but at least based on this mail your idea looks much better than polling.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches