Skip to contentSkip to conversation

Fixes/improvements for the PCI M.2 power sequencing driver

PATCH Superseded Manivannan Sadhasivam 5 participants 22 messages May 7 – May 11

Subsystems (6)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fixes/improvements for the PCI M.2 power sequencing driver

Hi,

This series has several key improvements and fixes to the M.2 power sequencing driver and also the BT HCI_QCA driver. Notably, this series allows the M.2 power sequencing driver to work with more M.2 cards, not just WCN7850. It also allows the BT HCI_QCA driver to detect whether it can control BT_EN (or W_DISABLE2#) signal on the connector and set the HCI_QUIRK_NON_PERSISTENT_SETUP quirk.

Testing
=======

This series was tested on Lenovo Thinkpad T14s together with the below DTS patches:
https://github.com/Mani-Sadhasivam/linux/commit/29534d15307551b2355eb254601dec511169f0aa
https://github.com/Mani-Sadhasivam/linux/commit/f4eaacfe647674be200847092b43cdef2194fc55

Merge Strategy
==============

Since the BT HCI_QCA changes depend on the pwrseq changes, it would be good to merge the whole series through pwrseq tree or through an immutable branch.

---
Changes in v2:
- Dropped the pwrseq_is_fixed() change in favor or exporting pwrseq device's dev pointer and using it to check for the presence of W_DISABLE2# property
- Dropped the BT_EN fix for the Qcom WCN devices since it will be handled separately
- Collected tags
- Link to v1: https://patch.msgid.link/[email protected]

---
Manivannan Sadhasivam (9):
power: sequencing: pcie-m2: Fix inconsistent function prefixes
power: sequencing: pcie-m2: Allow creating serdev for multiple PCI devices
power: sequencing: pcie-m2: Improve PCI device ID check
power: sequencing: pcie-m2: Create serdev for PCI devices present before probe
power: sequencing: pcie-m2: Create BT node based on the pci_device_id[] table
Bluetooth: hci_qca: Add M.2 Bluetooth device support using pwrseq
Bluetooth: hci_qca: Rename 'power_ctrl_enabled' to 'bt_en_available'
power: sequencing: Add an API to return the pwrseq device's 'dev' pointer
Bluetooth: hci_qca: Set 'bt_en_available' based on W_DISABLE2# presence in M.2 connector

 drivers/bluetooth/hci_qca.c               |  28 +++-
 drivers/power/sequencing/core.c           |   9 ++
 drivers/power/sequencing/pwrseq-pcie-m2.c | 234 ++++++++++++++++++++++--------
 include/linux/pwrseq/consumer.h           |   7 +
 4 files changed, 217 insertions(+), 61 deletions(-)

---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260422-pwrseq-m2-bt-abdaa71094eb

Best regards,

Signature · 1 line
Manivannan Sadhasivam <[email protected]>
#1 lore raw

PATCH v2 1/9 power: sequencing: pcie-m2: Fix inconsistent function prefixes

All functions in this driver follow 'pwrseq_pcie_m2' prefix except a few. Fix them to avoid inconsistency.

---
 drivers/power/sequencing/pwrseq-pcie-m2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/power/sequencing/pwrseq-pcie-m2.c +4 -4
index ef69ae268059..b2ed336fd5ad 100644
@@ -177,7 +177,7 @@ static int pwrseq_pcie_m2_match(struct pwrseq_device *pwrseq,
 	return PWRSEQ_NO_MATCH;
 }
 
-static int pwrseq_m2_pcie_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
+static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
 					struct device_node *parent)
 {
 	struct device *dev = ctx->dev;
@@ -254,7 +254,7 @@ static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx)
 		goto err_put_ctrl;
 	}
 
-	ret = pwrseq_m2_pcie_create_bt_node(ctx, serdev_parent);
+	ret = pwrseq_pcie_m2_create_bt_node(ctx, serdev_parent);
 	if (ret)
 		goto err_free_serdev;
 
@@ -299,7 +299,7 @@ static void pwrseq_pcie_m2_remove_serdev(struct pwrseq_pcie_m2_ctx *ctx)
 	}
 }
 
-static int pwrseq_m2_pcie_notify(struct notifier_block *nb, unsigned long action,
+static int pwrseq_pcie_m2_notify(struct notifier_block *nb, unsigned long action,
 			      void *data)
 {
 	struct pwrseq_pcie_m2_ctx *ctx = container_of(nb, struct pwrseq_pcie_m2_ctx, nb);
@@ -364,7 +364,7 @@ static int pwrseq_pcie_m2_register_notifier(struct pwrseq_pcie_m2_ctx *ctx, stru
 	if (pwrseq_pcie_m2_check_remote_node(dev, 3, 0, "serial")) {
 		if (pwrseq_pcie_m2_check_remote_node(dev, 0, 0, "pcie")) {
 			ctx->dev = dev;
-			ctx->nb.notifier_call = pwrseq_m2_pcie_notify;
+			ctx->nb.notifier_call = pwrseq_pcie_m2_notify;
 			ret = bus_register_notifier(&pci_bus_type, &ctx->nb);
 			if (ret)
 				return dev_err_probe(dev, ret,
Signature · 1 line
2.51.0
#2 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1091159

---Test result---

Test Summary:

CheckPatch                    PASS      5.83 seconds
GitLint                       FAIL      2.49 seconds
SubjectPrefix                 FAIL      0.89 seconds
BuildKernel                   PASS      27.15 seconds
CheckAllWarning               PASS      29.30 seconds
CheckSparse                   PASS      28.12 seconds
BuildKernel32                 PASS      25.66 seconds
TestRunnerSetup               PASS      566.63 seconds
TestRunner_l2cap-tester       PASS      378.06 seconds
TestRunner_iso-tester         PASS      594.73 seconds
TestRunner_bnep-tester        PASS      18.80 seconds
TestRunner_mgmt-tester        PASS      2022.73 seconds
TestRunner_rfcomm-tester      PASS      63.46 seconds
TestRunner_sco-tester         PASS      141.37 seconds
TestRunner_ioctl-tester       PASS      133.16 seconds
TestRunner_mesh-tester        PASS      59.66 seconds
TestRunner_smp-tester         PASS      17.73 seconds
TestRunner_userchan-tester    PASS      19.19 seconds
TestRunner_6lowpan-tester     PASS      51.12 seconds
IncrementalBuild              PASS      37.89 seconds

Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[v2,2/9] power: sequencing: pcie-m2: Allow creating serdev for multiple PCI devices

WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
1: T1 Title exceeds max length (83>80): "[v2,2/9] power: sequencing: pcie-m2: Allow creating serdev for multiple PCI devices"
[v2,4/9] power: sequencing: pcie-m2: Create serdev for PCI devices present before probe

WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
1: T1 Title exceeds max length (87>80): "[v2,4/9] power: sequencing: pcie-m2: Create serdev for PCI devices present before probe"
[v2,5/9] power: sequencing: pcie-m2: Create BT node based on the pci_device_id[] table

WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
1: T1 Title exceeds max length (86>80): "[v2,5/9] power: sequencing: pcie-m2: Create BT node based on the pci_device_id[] table"
[v2,8/9] power: sequencing: Add an API to return the pwrseq device's 'dev' pointer

WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
1: T1 Title exceeds max length (82>80): "[v2,8/9] power: sequencing: Add an API to return the pwrseq device's 'dev' pointer"
[v2,9/9] Bluetooth: hci_qca: Set 'bt_en_available' based on W_DISABLE2# presence in M.2 connector

WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
1: T1 Title exceeds max length (97>80): "[v2,9/9] Bluetooth: hci_qca: Set 'bt_en_available' based on W_DISABLE2# presence in M.2 connector"
##############################
Test: SubjectPrefix - FAIL
Desc: Check subject contains "Bluetooth" prefix
Output:
"Bluetooth: " prefix is not specified in the subject
"Bluetooth: " prefix is not specified in the subject
"Bluetooth: " prefix is not specified in the subject
"Bluetooth: " prefix is not specified in the subject
"Bluetooth: " prefix is not specified in the subject
"Bluetooth: " prefix is not specified in the subject

https://github.com/bluez/bluetooth-next/pull/153

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH v2 2/9 power: sequencing: pcie-m2: Allow creating serdev for multiple PCI devices

In reply to Manivannan Sadhasivam

Current code makes it possible to create serdev for only one PCI device. But for scaling this driver, it is necessary to allow creating serdev for multiple PCI devices.

Hence, add provision for it by creating 'struct pwrseq_pci_dev' for each PCI device that requires serdev and add them to 'pwrseq_pcie_m2_ctx::pci_devices' list.

---
 drivers/power/sequencing/pwrseq-pcie-m2.c | 127 +++++++++++++++++++++---------
 1 file changed, 88 insertions(+), 39 deletions(-)
drivers/power/sequencing/pwrseq-pcie-m2.c +88 -39
index b2ed336fd5ad..469e130330fa 100644
@@ -7,6 +7,7 @@
 #include <linux/device.h>
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
+#include <linux/list.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -19,6 +20,13 @@
 #include <linux/serdev.h>
 #include <linux/slab.h>
 
+struct pwrseq_pci_dev {
+	struct serdev_device *serdev;
+	struct of_changeset *ocs;
+	struct pci_dev *pdev;
+	struct list_head list;
+};
+
 struct pwrseq_pcie_m2_pdata {
 	const struct pwrseq_target_data **targets;
 };
@@ -32,9 +40,9 @@ struct pwrseq_pcie_m2_ctx {
 	struct notifier_block nb;
 	struct gpio_desc *w_disable1_gpio;
 	struct gpio_desc *w_disable2_gpio;
-	struct serdev_device *serdev;
-	struct of_changeset *ocs;
 	struct device *dev;
+	struct list_head pci_devices;
+	struct mutex list_lock;
 };
 
 static int pwrseq_pcie_m2_vregs_enable(struct pwrseq_device *pwrseq)
@@ -178,38 +186,39 @@ static int pwrseq_pcie_m2_match(struct pwrseq_device *pwrseq,
 }
 
 static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
+					struct pwrseq_pci_dev *pci_dev,
 					struct device_node *parent)
 {
 	struct device *dev = ctx->dev;
 	struct device_node *np;
 	int ret;
 
-	ctx->ocs = kzalloc_obj(*ctx->ocs);
-	if (!ctx->ocs)
+	pci_dev->ocs = kzalloc_obj(*pci_dev->ocs);
+	if (!pci_dev->ocs)
 		return -ENOMEM;
 
-	of_changeset_init(ctx->ocs);
+	of_changeset_init(pci_dev->ocs);
 
-	np = of_changeset_create_node(ctx->ocs, parent, "bluetooth");
+	np = of_changeset_create_node(pci_dev->ocs, parent, "bluetooth");
 	if (!np) {
 		dev_err(dev, "Failed to create bluetooth node\n");
 		ret = -ENODEV;
 		goto err_destroy_changeset;
 	}
 
-	ret = of_changeset_add_prop_string(ctx->ocs, np, "compatible", "qcom,wcn7850-bt");
+	ret = of_changeset_add_prop_string(pci_dev->ocs, np, "compatible", "qcom,wcn7850-bt");
 	if (ret) {
 		dev_err(dev, "Failed to add bluetooth compatible: %d\n", ret);
 		goto err_destroy_changeset;
 	}
 
-	ret = of_changeset_apply(ctx->ocs);
+	ret = of_changeset_apply(pci_dev->ocs);
 	if (ret) {
 		dev_err(dev, "Failed to apply changeset: %d\n", ret);
 		goto err_destroy_changeset;
 	}
 
-	ret = device_add_of_node(&ctx->serdev->dev, np);
+	ret = device_add_of_node(&pci_dev->serdev->dev, np);
 	if (ret) {
 		dev_err(dev, "Failed to add OF node: %d\n", ret);
 		goto err_revert_changeset;
@@ -218,19 +227,21 @@ static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
 	return 0;
 
 err_revert_changeset:
-	of_changeset_revert(ctx->ocs);
+	of_changeset_revert(pci_dev->ocs);
 err_destroy_changeset:
-	of_changeset_destroy(ctx->ocs);
-	kfree(ctx->ocs);
-	ctx->ocs = NULL;
+	of_changeset_destroy(pci_dev->ocs);
+	kfree(pci_dev->ocs);
+	pci_dev->ocs = NULL;
 
 	return ret;
 }
 
-static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx)
+static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
+					struct pci_dev *pdev)
 {
 	struct serdev_controller *serdev_ctrl;
 	struct device *dev = ctx->dev;
+	struct pwrseq_pci_dev *pci_dev;
 	int ret;
 
 	struct device_node *serdev_parent __free(device_node) =
@@ -248,17 +259,23 @@ static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx)
 		return 0;
 	}
 
-	ctx->serdev = serdev_device_alloc(serdev_ctrl);
-	if (!ctx->serdev) {
+	pci_dev = kzalloc(sizeof(*pci_dev), GFP_KERNEL);
+	if (!pci_dev) {
 		ret = -ENOMEM;
 		goto err_put_ctrl;
 	}
 
-	ret = pwrseq_pcie_m2_create_bt_node(ctx, serdev_parent);
+	pci_dev->serdev = serdev_device_alloc(serdev_ctrl);
+	if (!pci_dev->serdev) {
+		ret = -ENOMEM;
+		goto err_free_pci_dev;
+	}
+
+	ret = pwrseq_pcie_m2_create_bt_node(ctx, pci_dev, serdev_parent);
 	if (ret)
 		goto err_free_serdev;
 
-	ret = serdev_device_add(ctx->serdev);
+	ret = serdev_device_add(pci_dev->serdev);
 	if (ret) {
 		dev_err(dev, "Failed to add serdev for WCN7850: %d\n", ret);
 		goto err_free_dt_node;
@@ -266,37 +283,64 @@ static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx)
 
 	serdev_controller_put(serdev_ctrl);
 
+	pci_dev->pdev = pci_dev_get(pdev);
+
+	mutex_lock(&ctx->list_lock);
+	list_add_tail(&pci_dev->list, &ctx->pci_devices);
+	mutex_unlock(&ctx->list_lock);
+
 	return 0;
 
 err_free_dt_node:
-	device_remove_of_node(&ctx->serdev->dev);
-	of_changeset_revert(ctx->ocs);
-	of_changeset_destroy(ctx->ocs);
-	kfree(ctx->ocs);
-	ctx->ocs = NULL;
+	device_remove_of_node(&pci_dev->serdev->dev);
+	of_changeset_revert(pci_dev->ocs);
+	of_changeset_destroy(pci_dev->ocs);
+	kfree(pci_dev->ocs);
+	pci_dev->ocs = NULL;
 err_free_serdev:
-	serdev_device_put(ctx->serdev);
-	ctx->serdev = NULL;
+	serdev_device_put(pci_dev->serdev);
+	pci_dev->serdev = NULL;
+err_free_pci_dev:
+	kfree(pci_dev);
 err_put_ctrl:
 	serdev_controller_put(serdev_ctrl);
 
 	return ret;
 }
 
-static void pwrseq_pcie_m2_remove_serdev(struct pwrseq_pcie_m2_ctx *ctx)
+static void __pwrseq_pcie_m2_remove_serdev(struct pwrseq_pcie_m2_ctx *ctx,
+					   struct pwrseq_pci_dev *pci_dev)
 {
-	if (ctx->serdev) {
-		device_remove_of_node(&ctx->serdev->dev);
-		serdev_device_remove(ctx->serdev);
-		ctx->serdev = NULL;
+	if (pci_dev->serdev) {
+		device_remove_of_node(&pci_dev->serdev->dev);
+		serdev_device_remove(pci_dev->serdev);
 	}
 
-	if (ctx->ocs) {
-		of_changeset_revert(ctx->ocs);
-		of_changeset_destroy(ctx->ocs);
-		kfree(ctx->ocs);
-		ctx->ocs = NULL;
+	if (pci_dev->ocs) {
+		of_changeset_revert(pci_dev->ocs);
+		of_changeset_destroy(pci_dev->ocs);
+		kfree(pci_dev->ocs);
 	}
+
+	pci_dev_put(pci_dev->pdev);
+	list_del(&pci_dev->list);
+	kfree(pci_dev);
+}
+
+static void pwrseq_pcie_m2_remove_serdev(struct pwrseq_pcie_m2_ctx *ctx,
+					 struct pci_dev *pdev)
+{
+	struct pwrseq_pci_dev *pci_dev, *tmp;
+
+	mutex_lock(&ctx->list_lock);
+	list_for_each_entry_safe(pci_dev, tmp, &ctx->pci_devices, list) {
+		if (!pdev || pci_dev->pdev == pdev) {
+			__pwrseq_pcie_m2_remove_serdev(ctx, pci_dev);
+			if (pdev)
+				break;
+		}
+	}
+	mutex_unlock(&ctx->list_lock);
 }
 
 static int pwrseq_pcie_m2_notify(struct notifier_block *nb, unsigned long action,
@@ -320,7 +364,7 @@ static int pwrseq_pcie_m2_notify(struct notifier_block *nb, unsigned long action
 	case BUS_NOTIFY_ADD_DEVICE:
 		/* Create serdev device for WCN7850 */
 		if (pdev->vendor == PCI_VENDOR_ID_QCOM && pdev->device == 0x1107) {
-			ret = pwrseq_pcie_m2_create_serdev(ctx);
+			ret = pwrseq_pcie_m2_create_serdev(ctx, pdev);
 			if (ret)
 				return notifier_from_errno(ret);
 		}
@@ -328,7 +372,7 @@ static int pwrseq_pcie_m2_notify(struct notifier_block *nb, unsigned long action
 	case BUS_NOTIFY_REMOVED_DEVICE:
 		/* Destroy serdev device for WCN7850 */
 		if (pdev->vendor == PCI_VENDOR_ID_QCOM && pdev->device == 0x1107)
-			pwrseq_pcie_m2_remove_serdev(ctx);
+			pwrseq_pcie_m2_remove_serdev(ctx, pdev);
 
 		break;
 	}
@@ -432,16 +476,20 @@ static int pwrseq_pcie_m2_probe(struct platform_device *pdev)
 		goto err_free_regulators;
 	}
 
+	mutex_init(&ctx->list_lock);
+	INIT_LIST_HEAD(&ctx->pci_devices);
 	/*
 	 * Register a notifier for creating protocol devices for
 	 * non-discoverable busses like UART.
 	 */
 	ret = pwrseq_pcie_m2_register_notifier(ctx, dev);
 	if (ret)
-		goto err_free_regulators;
+		goto err_destroy_mutex;
 
 	return 0;
 
+err_destroy_mutex:
+	mutex_destroy(&ctx->list_lock);
 err_free_regulators:
 	regulator_bulk_free(ctx->num_vregs, ctx->regs);
 
@@ -453,7 +501,8 @@ static void pwrseq_pcie_m2_remove(struct platform_device *pdev)
 	struct pwrseq_pcie_m2_ctx *ctx = platform_get_drvdata(pdev);
 
 	bus_unregister_notifier(&pci_bus_type, &ctx->nb);
-	pwrseq_pcie_m2_remove_serdev(ctx);
+	pwrseq_pcie_m2_remove_serdev(ctx, NULL);
+	mutex_destroy(&ctx->list_lock);
 
 	regulator_bulk_free(ctx->num_vregs, ctx->regs);
 }
Signature · 1 line
2.51.0
#4 lore raw

Re: [PATCH v2 2/9] power: sequencing: pcie-m2: Allow creating serdev for multiple PCI devices

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Logic flaw prevents tracking multiple PCI devices, defeating the core purpose of the patch.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Logic flaw prevents tracking multiple PCI devices, defeating the core purpose of the patch.
- [High] Lockless check of serdev_ctrl->serdev introduces a data race during concurrent device additions. --

commit c7f58507c704ae72da8e4afb0e0855215048187d
Author: Manivannan Sadhasivam <[email protected]>

power: sequencing: pcie-m2: Allow creating serdev for multiple PCI devices

This patch introduces a list to track multiple PCI devices per M.2 slot by creating a 'pwrseq_pci_dev' for each device requiring a serdev. It replaces the single serdev reference with this list to scale the driver.

Quoted 4 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +0 -0
index b2ed336fd5ad9..469e130330faa 100644

[ ... ]

drivers/power/sequencing/pwrseq-pcie-m2.c static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx) · line 260

Quoted 3 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +0 -0
@@ -248,17 +259,23 @@ static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx)
 		return 0;
 	}

Since the intent is to track multiple PCI devices per M.2 slot, does this existing early bailout:

	if (serdev_ctrl->serdev) {
		serdev_controller_put(serdev_ctrl);
		return 0;
	}

prevent adding subsequent PCI devices to the ctx->pci_devices list?

If serdev_ctrl->serdev is already set by the first device, won't this return 0 without allocating pwrseq_pci_dev, leaving the list with only one device?

Additionally, is it possible for two PCI devices to trigger the BUS_NOTIFY_ADD_DEVICE notifier concurrently?

If both threads observe serdev_ctrl->serdev as NULL without synchronization, could they both proceed to allocate and call serdev_device_add()? This could overwrite the ctrl->serdev pointer locklessly.

If the slower thread ultimately fails device_add() due to a sysfs name collision, it could execute ctrl->serdev = NULL in its error path. Would this corrupt the serdev controller state and silently orphan the serdev that was successfully registered by the first thread?

Quoted 8 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +2 -2
-	ctx->serdev = serdev_device_alloc(serdev_ctrl);
-	if (!ctx->serdev) {
+	pci_dev = kzalloc(sizeof(*pci_dev), GFP_KERNEL);
+	if (!pci_dev) {
 		ret = -ENOMEM;
 		goto err_put_ctrl;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

PATCH v2 3/9 power: sequencing: pcie-m2: Improve PCI device ID check

In reply to Manivannan Sadhasivam

Instead of hardcoding the PCI device check, use pci_match_id() to check for the known IDs using the pwrseq_m2_pci_ids[] array.

This makes adding support for new devices easier.

---
 drivers/power/sequencing/pwrseq-pcie-m2.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/power/sequencing/pwrseq-pcie-m2.c +7 -4
index 469e130330fa..038271207a27 100644
@@ -343,6 +343,11 @@ static void pwrseq_pcie_m2_remove_serdev(struct pwrseq_pcie_m2_ctx *ctx,
 	mutex_unlock(&ctx->list_lock);
 }
 
+static const struct pci_device_id pwrseq_m2_pci_ids[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x1107) },
+	{ } /* Sentinel */
+};
+
 static int pwrseq_pcie_m2_notify(struct notifier_block *nb, unsigned long action,
 			      void *data)
 {
@@ -362,16 +367,14 @@ static int pwrseq_pcie_m2_notify(struct notifier_block *nb, unsigned long action
 
 	switch (action) {
 	case BUS_NOTIFY_ADD_DEVICE:
-		/* Create serdev device for WCN7850 */
-		if (pdev->vendor == PCI_VENDOR_ID_QCOM && pdev->device == 0x1107) {
+		if (pci_match_id(pwrseq_m2_pci_ids, pdev)) {
 			ret = pwrseq_pcie_m2_create_serdev(ctx, pdev);
 			if (ret)
 				return notifier_from_errno(ret);
 		}
 		break;
 	case BUS_NOTIFY_REMOVED_DEVICE:
-		/* Destroy serdev device for WCN7850 */
-		if (pdev->vendor == PCI_VENDOR_ID_QCOM && pdev->device == 0x1107)
+		if (pci_match_id(pwrseq_m2_pci_ids, pdev))
 			pwrseq_pcie_m2_remove_serdev(ctx, pdev);
 
 		break;
Signature · 1 line
2.51.0
#6 lore raw

PATCH v2 4/9 power: sequencing: pcie-m2: Create serdev for PCI devices present before probe

In reply to Manivannan Sadhasivam

So far, the driver is registering a notifier to create serdev for the PCI devices that are going to be attached after probe. But it doesn't handle the devices present before probe. Due to this, serdev is not getting created for those existing devices.

Hence, create serdev for PCI devices available before probe as well.

Note that the serdev for available devices are created before registering the notifier. There is a small window where a device could appear after pwrseq_pcie_m2_create_serdev(), before notifier registration. But since M.2 cards are fixed to a slot, they are mostly added either before booting the host or after using hotplug. So this window is mostly theoretical.

---
 drivers/power/sequencing/pwrseq-pcie-m2.c | 83 ++++++++++++++++++++++++++-----
 1 file changed, 70 insertions(+), 13 deletions(-)
drivers/power/sequencing/pwrseq-pcie-m2.c +70 -13
index 038271207a27..0a37a375a89d 100644
@@ -236,7 +236,7 @@ static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
 	return ret;
 }
 
-static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
+static int __pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
 					struct pci_dev *pdev)
 {
 	struct serdev_controller *serdev_ctrl;
@@ -259,6 +259,16 @@ static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
 		return 0;
 	}
 
+	/* Bail out if the serdev device was already created for the PCI dev */
+	mutex_lock(&ctx->list_lock);
+	list_for_each_entry(pci_dev, &ctx->pci_devices, list) {
+		if (pci_dev->pdev == pdev) {
+			mutex_unlock(&ctx->list_lock);
+			return 0;
+		}
+	}
+	mutex_unlock(&ctx->list_lock);
+
 	pci_dev = kzalloc(sizeof(*pci_dev), GFP_KERNEL);
 	if (!pci_dev) {
 		ret = -ENOMEM;
@@ -368,7 +378,7 @@ static int pwrseq_pcie_m2_notify(struct notifier_block *nb, unsigned long action
 	switch (action) {
 	case BUS_NOTIFY_ADD_DEVICE:
 		if (pci_match_id(pwrseq_m2_pci_ids, pdev)) {
-			ret = pwrseq_pcie_m2_create_serdev(ctx, pdev);
+			ret = __pwrseq_pcie_m2_create_serdev(ctx, pdev);
 			if (ret)
 				return notifier_from_errno(ret);
 		}
@@ -400,7 +410,7 @@ static bool pwrseq_pcie_m2_check_remote_node(struct device *dev, u8 port, u8 end
  * protocol device needs to be created manually with the help of the notifier
  * of the discoverable bus like PCIe.
  */
-static int pwrseq_pcie_m2_register_notifier(struct pwrseq_pcie_m2_ctx *ctx, struct device *dev)
+static int pwrseq_pcie_m2_register_notifier(struct pwrseq_pcie_m2_ctx *ctx)
 {
 	int ret;
 
@@ -408,18 +418,56 @@ static int pwrseq_pcie_m2_register_notifier(struct pwrseq_pcie_m2_ctx *ctx, stru
 	 * Register a PCI notifier for Key E connector that has PCIe as Port
 	 * 0/Endpoint 0 interface and Serial as Port 3/Endpoint 0 interface.
 	 */
-	if (pwrseq_pcie_m2_check_remote_node(dev, 3, 0, "serial")) {
-		if (pwrseq_pcie_m2_check_remote_node(dev, 0, 0, "pcie")) {
-			ctx->dev = dev;
-			ctx->nb.notifier_call = pwrseq_pcie_m2_notify;
-			ret = bus_register_notifier(&pci_bus_type, &ctx->nb);
-			if (ret)
-				return dev_err_probe(dev, ret,
-						     "Failed to register notifier for serdev\n");
+	if (!pwrseq_pcie_m2_check_remote_node(ctx->dev, 3, 0, "serial") ||
+	    !pwrseq_pcie_m2_check_remote_node(ctx->dev, 0, 0, "pcie"))
+		return 0;
+
+	ctx->nb.notifier_call = pwrseq_pcie_m2_notify;
+	ret = bus_register_notifier(&pci_bus_type, &ctx->nb);
+	if (ret)
+		return dev_err_probe(ctx->dev, ret,
+				     "Failed to register notifier for serdev\n");
+	return 0;
+}
+
+static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx)
+{
+	struct pci_dev *pdev = NULL;
+	int ret;
+
+	if (!pwrseq_pcie_m2_check_remote_node(ctx->dev, 3, 0, "serial") ||
+	    !pwrseq_pcie_m2_check_remote_node(ctx->dev, 0, 0, "pcie"))
+		return 0;
+
+	struct device_node *pci_parent __free(device_node) =
+				of_graph_get_remote_node(dev_of_node(ctx->dev), 0, 0);
+	if (!pci_parent)
+		return 0;
+
+	/* Create serdev for existing PCI devices if required */
+	for_each_pci_dev(pdev) {
+		if (!pdev->dev.parent || pci_parent != pdev->dev.parent->of_node)
+			continue;
+
+		if (!pci_match_id(pwrseq_m2_pci_ids, pdev))
+			continue;
+
+		ret = __pwrseq_pcie_m2_create_serdev(ctx, pdev);
+		if (ret) {
+			dev_err_probe(ctx->dev, ret,
+				      "Failed to create serdev for PCI device (%s)\n",
+				      pci_name(pdev));
+			pci_dev_put(pdev);
+			goto err_remove_serdev;
 		}
 	}
 
 	return 0;
+
+err_remove_serdev:
+	pwrseq_pcie_m2_remove_serdev(ctx, NULL);
+
+	return ret;
 }
 
 static int pwrseq_pcie_m2_probe(struct platform_device *pdev)
@@ -481,16 +529,25 @@ static int pwrseq_pcie_m2_probe(struct platform_device *pdev)
 
 	mutex_init(&ctx->list_lock);
 	INIT_LIST_HEAD(&ctx->pci_devices);
+	ctx->dev = dev;
+
+	/* Create serdev for available PCI devices (if required) */
+	ret = pwrseq_pcie_m2_create_serdev(ctx);
+	if (ret)
+		goto err_destroy_mutex;
+
 	/*
 	 * Register a notifier for creating protocol devices for
 	 * non-discoverable busses like UART.
 	 */
-	ret = pwrseq_pcie_m2_register_notifier(ctx, dev);
+	ret = pwrseq_pcie_m2_register_notifier(ctx);
 	if (ret)
-		goto err_destroy_mutex;
+		goto err_remove_serdev;
 
 	return 0;
 
+err_remove_serdev:
+	pwrseq_pcie_m2_remove_serdev(ctx, NULL);
 err_destroy_mutex:
 	mutex_destroy(&ctx->list_lock);
 err_free_regulators:
Signature · 1 line
2.51.0
#7 lore raw

Re: [PATCH v2 4/9] power: sequencing: pcie-m2: Create serdev for PCI devices present before probe

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Reference leak of 'serdev_ctrl' on early return in '__pwrseq_pcie_m2_create_serdev()'. - [High]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Reference leak of 'serdev_ctrl' on early return in '__pwrseq_pcie_m2_create_serdev()'.
- [High] Potential NULL pointer dereference in 'pwrseq_pcie_m2_notify()' when accessing 'pdev->dev.parent->of_node'.
- [Medium] Subsystem guidelines violation: mixing 'goto' error handling with '__free()' cleanup helpers. --

commit 1d901a33bb02968191e0403afce66cf3ae8ed0f3
Author: Manivannan Sadhasivam <[email protected]>

power: sequencing: pcie-m2: Create serdev for PCI devices present before probe

This commit adds support to create serdev devices for PCI devices that are already present before the driver's probe function registers the PCI notifier.

Quoted 11 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +1 -1
@@ -236,7 +236,7 @@ static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
 	return ret;
 }
 
-static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
+static int __pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
 					struct pci_dev *pdev)
 {
 	struct serdev_controller *serdev_ctrl;

[ ... ]

drivers/power/sequencing/pwrseq-pcie-m2.c static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx, · line 267

Quoted 12 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +6 -0
 	/* Bail out if the device was already attached to this controller */
 	if (serdev_ctrl->serdev) {
 		serdev_controller_put(serdev_ctrl);
 		return 0;
 	}
 
+	/* Bail out if the serdev device was already created for the PCI dev */
+	mutex_lock(&ctx->list_lock);
+	list_for_each_entry(pci_dev, &ctx->pci_devices, list) {
+		if (pci_dev->pdev == pdev) {
+			mutex_unlock(&ctx->list_lock);
+			return 0;

Does this early return path leak the reference to serdev_ctrl?

The of_find_serdev_controller_by_node() call earlier in the function acquires a reference, and the previous bailout path correctly calls serdev_controller_put(serdev_ctrl), but this new path appears to return without releasing the reference.

Also, looking at the rest of __pwrseq_pcie_m2_create_serdev() in the source, it declares serdev_parent using the __free(device_node) macro while also using goto labels for error paths like err_put_ctrl. Does this mix of goto and __free() cleanup helpers violate the subsystem guidelines?

[ ... ]

Quoted 8 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +8 -0
+static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx)
+{
+	struct pci_dev *pdev = NULL;
+	int ret;
+
+	if (!pwrseq_pcie_m2_check_remote_node(ctx->dev, 3, 0, "serial") ||
+	    !pwrseq_pcie_m2_check_remote_node(ctx->dev, 0, 0, "pcie"))
+		return 0;

drivers/power/sequencing/pwrseq-pcie-m2.c static int pwrseq_pcie_m2_register_notifier(struct pwrseq_pcie_m2_ctx *ctx, stru · line 450

Quoted 10 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +10 -0
+
+	struct device_node *pci_parent __free(device_node) =
+				of_graph_get_remote_node(dev_of_node(ctx->dev), 0, 0);
+	if (!pci_parent)
+		return 0;
+
+	/* Create serdev for existing PCI devices if required */
+	for_each_pci_dev(pdev) {
+		if (!pdev->dev.parent || pci_parent != pdev->dev.parent->of_node)
+			continue;

It is good to see the defensive !pdev->dev.parent check here to prevent a null pointer dereference.

However, the existing pwrseq_pcie_m2_notify() function performs a similar check without validating the parent:

	if (!pci_parent || (pci_parent != pdev->dev.parent->of_node))

Could a PCI device without a parent triggering a BUS_NOTIFY_ADD_DEVICE event cause a crash in pwrseq_pcie_m2_notify() because it is missing the same defensive check?

[ ... ]

Quoted 7 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +7 -0
+		ret = __pwrseq_pcie_m2_create_serdev(ctx, pdev);
+		if (ret) {
+			dev_err_probe(ctx->dev, ret,
+				      "Failed to create serdev for PCI device (%s)\n",
+				      pci_name(pdev));
+			pci_dev_put(pdev);
+			goto err_remove_serdev;

drivers/power/sequencing/pwrseq-pcie-m2.c static int pwrseq_pcie_m2_register_notifier(struct pwrseq_pcie_m2_ctx *ctx, stru · line 471

Quoted 10 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +5 -0
 		}
 	}
 
 	return 0;
+
+err_remove_serdev:
+	pwrseq_pcie_m2_remove_serdev(ctx, NULL);
+
+	return ret;
 }

Similar to the function above, this newly added function mixes the __free(device_node) cleanup helper for pci_parent with the err_remove_serdev goto label. Should all resources here be converted to scope-based cleanup to avoid ownership confusion?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#8 lore raw

Re: [PATCH v2 4/9] power: sequencing: pcie-m2: Create serdev for PCI devices present before probe

In reply to Manivannan Sadhasivam
Quoted 26 lines · On Thu, 7 May 2026 18:06:12 +0200, Manivannan Sadhasivam via B4 Relay…

From: Manivannan Sadhasivam <[email protected]>

So far, the driver is registering a notifier to create serdev for the PCI devices that are going to be attached after probe. But it doesn't handle the devices present before probe. Due to this, serdev is not getting created for those existing devices.

Hence, create serdev for PCI devices available before probe as well.

Note that the serdev for available devices are created before registering the notifier. There is a small window where a device could appear after pwrseq_pcie_m2_create_serdev(), before notifier registration. But since M.2 cards are fixed to a slot, they are mostly added either before booting the host or after using hotplug. So this window is mostly theoretical.

---
 drivers/power/sequencing/pwrseq-pcie-m2.c | 83 ++++++++++++++++++++++++++-----
 1 file changed, 70 insertions(+), 13 deletions(-)
drivers/power/sequencing/pwrseq-pcie-m2.c +0 -0
index 038271207a27..0a37a375a89d 100644

drivers/power/sequencing/pwrseq-pcie-m2.c static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx, · line 240

Quoted 6 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +1 -1
@@ -236,7 +236,7 @@ static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
 	return ret;
 }
 
-static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
+static int __pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
 					struct pci_dev *pdev)

It may just be a personal preference but I dislike the __ prefix that doesn't really indicate how the function is different from the one without it. I prefer the name to reflect that - if it's because the function expects a mutex to be already taken then it should be called something_something_unlocked() etc.

Maybe call it pwrseq_pcie_m2_create_serdev_for_pci()? Or at least:
pwrseq_pcie_m2_do_create_serdev()?

Quoted 5 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +0 -0
 {
 	struct serdev_controller *serdev_ctrl;
@@ -259,6 +259,16 @@ static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
 		return 0;
 	}

drivers/power/sequencing/pwrseq-pcie-m2.c static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx, · line 270

Quoted 10 lines
drivers/power/sequencing/pwrseq-pcie-m2.c +9 -0
 
+	/* Bail out if the serdev device was already created for the PCI dev */
+	mutex_lock(&ctx->list_lock);
+	list_for_each_entry(pci_dev, &ctx->pci_devices, list) {
+		if (pci_dev->pdev == pdev) {
+			mutex_unlock(&ctx->list_lock);
+			return 0;
+		}
+	}
+	mutex_unlock(&ctx->list_lock);

Is there any reason to not use guard() here?

Bart

#9 lore raw

PATCH v2 5/9 power: sequencing: pcie-m2: Create BT node based on the pci_device_id[] table

In reply to Manivannan Sadhasivam

Currently, pwrseq_pcie_m2_create_bt_node() hardcodes the BT compatible for creating the devicetree node. But to allow adding support for more devices in the future, create the BT node based on the pci_device_id[] table. The BT compatible is passed using 'driver_data'.

---
 drivers/power/sequencing/pwrseq-pcie-m2.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
drivers/power/sequencing/pwrseq-pcie-m2.c +20 -9
index 0a37a375a89d..efeb25ba9c79 100644
@@ -185,14 +185,29 @@ static int pwrseq_pcie_m2_match(struct pwrseq_device *pwrseq,
 	return PWRSEQ_NO_MATCH;
 }
 
+static const struct pci_device_id pwrseq_m2_pci_ids[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x1107),
+	  .driver_data = (kernel_ulong_t)"qcom,wcn7850-bt" },
+	{ } /* Sentinel */
+};
+
 static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
 					struct pwrseq_pci_dev *pci_dev,
-					struct device_node *parent)
+					struct device_node *parent,
+					struct pci_dev *pdev)
 {
+	const struct pci_device_id *id;
 	struct device *dev = ctx->dev;
+	const char *compatible;
 	struct device_node *np;
 	int ret;
 
+	id = pci_match_id(pwrseq_m2_pci_ids, pdev);
+	if (WARN_ON_ONCE(!id)) /* Shouldn't happen */
+		return -ENODEV;
+
+	compatible = (const char *)id->driver_data;
+
 	pci_dev->ocs = kzalloc_obj(*pci_dev->ocs);
 	if (!pci_dev->ocs)
 		return -ENOMEM;
@@ -206,7 +221,7 @@ static int pwrseq_pcie_m2_create_bt_node(struct pwrseq_pcie_m2_ctx *ctx,
 		goto err_destroy_changeset;
 	}
 
-	ret = of_changeset_add_prop_string(pci_dev->ocs, np, "compatible", "qcom,wcn7850-bt");
+	ret = of_changeset_add_prop_string(pci_dev->ocs, np, "compatible", compatible);
 	if (ret) {
 		dev_err(dev, "Failed to add bluetooth compatible: %d\n", ret);
 		goto err_destroy_changeset;
@@ -281,13 +296,14 @@ static int __pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
 		goto err_free_pci_dev;
 	}
 
-	ret = pwrseq_pcie_m2_create_bt_node(ctx, pci_dev, serdev_parent);
+	ret = pwrseq_pcie_m2_create_bt_node(ctx, pci_dev, serdev_parent, pdev);
 	if (ret)
 		goto err_free_serdev;
 
 	ret = serdev_device_add(pci_dev->serdev);
 	if (ret) {
-		dev_err(dev, "Failed to add serdev for WCN7850: %d\n", ret);
+		dev_err(dev, "Failed to add serdev for PCI device (%s): %d\n",
+			pci_name(pdev), ret);
 		goto err_free_dt_node;
 	}
 
@@ -353,11 +369,6 @@ static void pwrseq_pcie_m2_remove_serdev(struct pwrseq_pcie_m2_ctx *ctx,
 	mutex_unlock(&ctx->list_lock);
 }
 
-static const struct pci_device_id pwrseq_m2_pci_ids[] = {
-	{ PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x1107) },
-	{ } /* Sentinel */
-};
-
 static int pwrseq_pcie_m2_notify(struct notifier_block *nb, unsigned long action,
 			      void *data)
 {
Signature · 1 line
2.51.0
#10 lore raw

PATCH v2 6/9 Bluetooth: hci_qca: Add M.2 Bluetooth device support using pwrseq

In reply to Manivannan Sadhasivam

Power supply to the M.2 Bluetooth device attached to the host using M.2 connector is controlled using the 'uart' pwrseq device. So add support for getting the pwrseq device if the OF graph link is present. Once obtained, the existing pwrseq APIs can be used to control the power supplies of the M.2 card.

---
 drivers/bluetooth/hci_qca.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/bluetooth/hci_qca.c +13 -0
index cd1834246b47..c83fe72bc549 100644
@@ -26,6 +26,7 @@
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/of.h>
+#include <linux/of_graph.h>
 #include <linux/acpi.h>
 #include <linux/platform_device.h>
 #include <linux/pwrseq/consumer.h>
@@ -2443,6 +2444,18 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 	case QCA_WCN6750:
 	case QCA_WCN6855:
 	case QCA_WCN7850:
+		/*
+		 * OF graph link is only present for BT devices attached through
+		 * the M.2 Key E connector.
+		 */
+		if (of_graph_is_present(dev_of_node(&serdev->ctrl->dev))) {
+			qcadev->bt_power->pwrseq = devm_pwrseq_get(&serdev->ctrl->dev,
+								   "uart");
+			if (IS_ERR(qcadev->bt_power->pwrseq))
+				return PTR_ERR(qcadev->bt_power->pwrseq);
+			break;
+		}
+
 		if (!device_property_present(&serdev->dev, "enable-gpios")) {
 			/*
 			 * Backward compatibility with old DT sources. If the
Signature · 1 line
2.51.0
#11 lore raw

Re: [PATCH v2 6/9] Bluetooth: hci_qca: Add M.2 Bluetooth device support using pwrseq

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Resource leak due to `devm_pwrseq_get` being called with the parent UART controller device instead…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Resource leak due to devm_pwrseq_get being called with the parent UART controller device instead of the serdev device being probed. --

commit f9b32f0ee9703eed7ebec9d68e29f842c80446dd
Author: Manivannan Sadhasivam <[email protected]>

Bluetooth: hci_qca: Add M.2 Bluetooth device support using pwrseq

This commit adds support for controlling the power supply of an M.2 Bluetooth device using the 'uart' pwrseq device. It detects if the device is attached through the M.2 Key E connector by checking for the presence of an OF graph link, and if so, retrieves the pwrseq device.

Quoted 4 lines
drivers/bluetooth/hci_qca.c +0 -0
index cd1834246b479..c83fe72bc5499 100644

[ ... ]

drivers/bluetooth/hci_qca.c static int qca_serdev_probe(struct serdev_device *serdev) · line 2453

Quoted 11 lines
drivers/bluetooth/hci_qca.c +7 -0
@@ -2443,6 +2444,18 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 	case QCA_WCN6750:
 	case QCA_WCN6855:
 	case QCA_WCN7850:
+		/*
+		 * OF graph link is only present for BT devices attached through
+		 * the M.2 Key E connector.
+		 */
+		if (of_graph_is_present(dev_of_node(&serdev->ctrl->dev))) {
+			qcadev->bt_power->pwrseq = devm_pwrseq_get(&serdev->ctrl->dev,
+								   "uart");

Does tying this power sequencer resource to the parent UART controller (&serdev->ctrl->dev) instead of the serdev device (&serdev->dev) cause a resource leak?

Since devm_pwrseq_get() binds the resource cleanup to the lifecycle of the device passed to it, the cleanup will not execute if qca_serdev_probe() fails later and returns an error like -EPROBE_DEFER, or if the hci_qca driver is later unbound.

The device resource framework only cleans up resources associated with &serdev->dev on probe failure or unbind.

This seems like it would leave an unreleased reference on the power sequencer provider device and an unbalanced module reference, which might accumulate on every probe deferral.

Quoted 8 lines
drivers/bluetooth/hci_qca.c +5 -0
+			if (IS_ERR(qcadev->bt_power->pwrseq))
+				return PTR_ERR(qcadev->bt_power->pwrseq);
+			break;
+		}
+
 		if (!device_property_present(&serdev->dev, "enable-gpios")) {
 			/*
 			 * Backward compatibility with old DT sources. If the
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#12 lore raw

PATCH v2 7/9 Bluetooth: hci_qca: Rename 'power_ctrl_enabled' to 'bt_en_available'

In reply to Manivannan Sadhasivam

'power_ctrl_enabled' flag is used to indicate the availability of the BT_EN GPIO in devicetree. But the naming causes confusion with the new pwrctrl framework.

So rename it to 'bt_en_available' to make it clear and explicit.

---
 drivers/bluetooth/hci_qca.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/bluetooth/hci_qca.c +4 -4
index c83fe72bc549..3e71a72ea7c7 100644
@@ -2391,7 +2391,7 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 	struct hci_dev *hdev;
 	const struct qca_device_data *data;
 	int err;
-	bool power_ctrl_enabled = true;
+	bool bt_en_available = true;
 
 	qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
 	if (!qcadev)
@@ -2499,7 +2499,7 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 		    (data->soc_type == QCA_WCN6750 ||
 		     data->soc_type == QCA_WCN6855 ||
 		     data->soc_type == QCA_WCN7850))
-			power_ctrl_enabled = false;
+			bt_en_available = false;
 
 		qcadev->sw_ctrl = devm_gpiod_get_optional(&serdev->dev, "swctrl",
 					       GPIOD_IN);
@@ -2537,7 +2537,7 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 		}
 
 		if (!qcadev->bt_en)
-			power_ctrl_enabled = false;
+			bt_en_available = false;
 
 		qcadev->susclk = devm_clk_get_optional_enabled_with_rate(
 					&serdev->dev, NULL, SUSCLK_RATE_32KHZ);
@@ -2555,7 +2555,7 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 
 	hdev = qcadev->serdev_hu.hdev;
 
-	if (power_ctrl_enabled) {
+	if (bt_en_available) {
 		hci_set_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_SETUP);
 		hdev->shutdown = qca_hci_shutdown;
 	}
Signature · 1 line
2.51.0
#13 lore raw

Re: [PATCH v2 7/9] Bluetooth: hci_qca: Rename 'power_ctrl_enabled' to 'bt_en_available'

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The rename of 'power_ctrl_enabled' to 'bt_en_available' introduces a misleading semantic…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The rename of 'power_ctrl_enabled' to 'bt_en_available' introduces a misleading semantic mismatch and contradicts the commit message. --

commit ebe40bb62789f8cbdbd8baa6c8be848d58c9f72b
Author: Manivannan Sadhasivam <[email protected]>

Bluetooth: hci_qca: Rename 'power_ctrl_enabled' to 'bt_en_available'

This commit renames the power_ctrl_enabled flag to bt_en_available in qca_serdev_probe(), with the commit message stating that the flag only indicates the availability of the BT_EN GPIO.

Quoted 3 lines
drivers/bluetooth/hci_qca.c +0 -0

[ ... ]

drivers/bluetooth/hci_qca.c static int qca_serdev_probe(struct serdev_device *serdev) · line 2394

Quoted 6 lines
drivers/bluetooth/hci_qca.c +1 -1
@@ -2391,7 +2391,7 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 	struct hci_dev *hdev;
 	const struct qca_device_data *data;
 	int err;
-	bool power_ctrl_enabled = true;
+	bool bt_en_available = true;

Is the description in the commit message accurate?

Looking at qca_serdev_probe(), there are several paths where the BT_EN GPIO is not present, but this flag remains true. For example, with a QCA_WCN3990 device, qcadev->bt_en can be NULL. Since it doesn't match the specific SoC types in the checks below, bt_en_available is never set to false:

qca_serdev_probe() {
        ...
        qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable",
                                       GPIOD_OUT_LOW);
        ...
        if (!qcadev->bt_en &&
            (data->soc_type == QCA_WCN6750 ||
             data->soc_type == QCA_WCN6855 ||
             data->soc_type == QCA_WCN7850))
                bt_en_available = false;
        ...
}

The original power_ctrl_enabled flag tracked whether any driver-managed power control (such as regulators or a generic power sequencer) was available, ensuring the qca_hci_shutdown callback is registered.

Does renaming this generic power control flag to bt_en_available introduce a mismatch where bt_en_available is true while qcadev->bt_en is NULL, potentially confusing future modifications?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
maintainer Reviewed-by on v2 7/9
#14 lore raw

Re: [PATCH v2 7/9] Bluetooth: hci_qca: Rename 'power_ctrl_enabled' to 'bt_en_available'

In reply to Manivannan Sadhasivam
Quoted 11 lines · On Thu, 7 May 2026 18:06:15 +0200, Manivannan Sadhasivam via B4 Relay…

From: Manivannan Sadhasivam <[email protected]>

'power_ctrl_enabled' flag is used to indicate the availability of the BT_EN GPIO in devicetree. But the naming causes confusion with the new pwrctrl framework.

So rename it to 'bt_en_available' to make it clear and explicit.

---

#15 lore raw

PATCH v2 8/9 power: sequencing: Add an API to return the pwrseq device's 'dev' pointer

In reply to Manivannan Sadhasivam

The consumer drivers can make use of the pwrseq device's 'dev' pointer to query the pwrseq provider's DT node to check for existence of specific properties.

Hence, add an API to return the pwrseq device's 'dev' pointer to consumers.

Note that since pwrseq_get() would've increased the pwrseq refcount, there is no need to increase the refcount in this API again.

---
 drivers/power/sequencing/core.c | 9 +++++++++
 include/linux/pwrseq/consumer.h | 7 +++++++
 2 files changed, 16 insertions(+)
drivers/power/sequencing/core.c +9 -0
index 4dff71be11b6..96ad557297f5 100644
@@ -965,6 +965,15 @@ int pwrseq_power_off(struct pwrseq_desc *desc)
 }
 EXPORT_SYMBOL_GPL(pwrseq_power_off);
 
+struct device *pwrseq_to_device(struct pwrseq_desc *desc)
+{
+	if (!desc)
+		return NULL;
+
+	return &desc->pwrseq->dev;
+}
+EXPORT_SYMBOL_GPL(pwrseq_to_device);
+
 #if IS_ENABLED(CONFIG_DEBUG_FS)
 
 struct pwrseq_debugfs_count_ctx {
include/linux/pwrseq/consumer.h +7 -0
index 7d583b4f266e..3c907c9e1885 100644
@@ -23,6 +23,8 @@ devm_pwrseq_get(struct device *dev, const char *target);
 int pwrseq_power_on(struct pwrseq_desc *desc);
 int pwrseq_power_off(struct pwrseq_desc *desc);
 
+struct device *pwrseq_to_device(struct pwrseq_desc *desc);
+
 #else /* CONFIG_POWER_SEQUENCING */
 
 static inline struct pwrseq_desc * __must_check
@@ -51,6 +53,11 @@ static inline int pwrseq_power_off(struct pwrseq_desc *desc)
 	return -ENOSYS;
 }
 
+static inline struct device *pwrseq_to_device(struct pwrseq_desc *desc)
+{
+	return NULL;
+}
+
 #endif /* CONFIG_POWER_SEQUENCING */
 
 #endif /* __POWER_SEQUENCING_CONSUMER_H__ */
Signature · 1 line
2.51.0
#16 lore raw

Re: [PATCH v2 8/9] power: sequencing: Add an API to return the pwrseq device's 'dev' pointer

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Exposing the internal `struct device` breaks abstraction boundaries and bypasses subsystem locks…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Exposing the internal struct device breaks abstraction boundaries and bypasses subsystem locks, leading to a Use-After-Free on the device's fwnode.
- [Medium] The API returns NULL when disabled or passed a NULL descriptor, causing a null pointer dereference in property querying APIs. --

commit 92d97440cc414d34602dcddead25760951c5cdce
Author: Manivannan Sadhasivam <[email protected]>

power: sequencing: Add an API to return the pwrseq device's 'dev' pointer

This commit adds a new pwrseq_to_device() API to retrieve the underlying device pointer from a power sequencing descriptor. The intent is to allow consumers to query the power sequencer's device tree node for specific properties.

Quoted 5 lines
drivers/power/sequencing/core.c +0 -0
index 4dff71be11b60..96ad557297f5b 100644

drivers/power/sequencing/core.c int pwrseq_power_off(struct pwrseq_desc *desc) · line 973

Quoted 9 lines
drivers/power/sequencing/core.c +6 -0
@@ -965,6 +965,15 @@ int pwrseq_power_off(struct pwrseq_desc *desc)
 }
 EXPORT_SYMBOL_GPL(pwrseq_power_off);
 
+struct device *pwrseq_to_device(struct pwrseq_desc *desc)
+{
+	if (!desc)
+		return NULL;
+
+	return &desc->pwrseq->dev;

Does exposing the internal device pointer bypass the subsystem locks and registration checks?

The power sequencing framework internally protects device teardown by wrapping device accesses in rw_lock and checking device_is_registered(), such as in pwrseq_power_off():

	guard(rwsem_read)(&pwrseq->rw_lock);
	if (!device_is_registered(&pwrseq->dev))
		return -ENODEV;

If a consumer queries properties on the returned device directly, they might do so concurrently with or after provider unregistration. In pwrseq_device_unregister(), device_del() is called, which drops the reference to the parent device. If the parent is subsequently freed, its fwnode can be destroyed, leaving pwrseq->dev.fwnode as a dangling pointer.

To safely allow property queries without exposing the raw device pointer, could the framework provide a dedicated wrapper function like pwrseq_property_present() that internally holds the lock and checks registration?

Quoted 2 lines
drivers/power/sequencing/core.c +2 -0
+}
+EXPORT_SYMBOL_GPL(pwrseq_to_device);

[ ... ]

Quoted 4 lines
include/linux/pwrseq/consumer.h +0 -0
index 7d583b4f266e6..3c907c9e1885d 100644

[ ... ]

include/linux/pwrseq/consumer.h static inline int pwrseq_power_off(struct pwrseq_desc *desc) · line 58

Quoted 7 lines
include/linux/pwrseq/consumer.h +3 -0
@@ -51,6 +53,11 @@ static inline int pwrseq_power_off(struct pwrseq_desc *desc)
 	return -ENOSYS;
 }
 
+static inline struct device *pwrseq_to_device(struct pwrseq_desc *desc)
+{
+	return NULL;

Will returning NULL here cause a NULL pointer dereference in consumers?

The commit message notes this API is intended for checking the existence of specific properties on the device. However, functions like device_property_present() do not accept a NULL device pointer and will unconditionally dereference it (e.g., when calling __dev_fwnode_const()).

If a consumer uses optional power sequencers or dummy configurations when CONFIG_POWER_SEQUENCING is disabled, passing the result to device_property_present() could crash the kernel.

Quoted 3 lines
include/linux/pwrseq/consumer.h +2 -0
+}
+
 #endif /* CONFIG_POWER_SEQUENCING */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#17 lore raw

Re: [PATCH v2 8/9] power: sequencing: Add an API to return the pwrseq device's 'dev' pointer

In reply to Manivannan Sadhasivam
Quoted 6 lines · On Thu, 7 May 2026 18:06:16 +0200, Manivannan Sadhasivam via B4 Relay…

From: Manivannan Sadhasivam <[email protected]>

The consumer drivers can make use of the pwrseq device's 'dev' pointer to query the pwrseq provider's DT node to check for existence of specific properties.

Quoted 7 lines

Hence, add an API to return the pwrseq device's 'dev' pointer to consumers.

Note that since pwrseq_get() would've increased the pwrseq refcount, there is no need to increase the refcount in this API again.

---

Thanks, I like this approach much more.

Bart

#18 lore raw

PATCH v2 9/9 Bluetooth: hci_qca: Set 'bt_en_available' based on W_DISABLE2# presence in M.2 connector

In reply to Manivannan Sadhasivam

Check if the M.2 connector supports the W_DISABLE2# property or not by querying the pwrseq provider's DT node. If not available, then set 'bt_en_available' flag to 'false'. This flag is used to set the HCI_QUIRK_NON_PERSISTENT_SETUP HCI quirk, which informs the HCI layer whether the shutdown() callback for the device can be triggered or not.

---
 drivers/bluetooth/hci_qca.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/bluetooth/hci_qca.c +7 -0
index 3e71a72ea7c7..b5439b9956cf 100644
@@ -2449,10 +2449,17 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 		 * the M.2 Key E connector.
 		 */
 		if (of_graph_is_present(dev_of_node(&serdev->ctrl->dev))) {
+			struct device *dev;
+
 			qcadev->bt_power->pwrseq = devm_pwrseq_get(&serdev->ctrl->dev,
 								   "uart");
 			if (IS_ERR(qcadev->bt_power->pwrseq))
 				return PTR_ERR(qcadev->bt_power->pwrseq);
+
+			dev = pwrseq_to_device(qcadev->bt_power->pwrseq);
+			if (!device_property_present(dev, "w-disable2-gpios"))
+				bt_en_available = false;
+
 			break;
 		}
 
Signature · 1 line
2.51.0
#19 lore raw

Re: [PATCH v2 9/9] Bluetooth: hci_qca: Set 'bt_en_available' based on W_DISABLE2# presence in M.2 connector

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Architecture/Layering Violation: Bluetooth driver inspects internal DT properties of the power…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Architecture/Layering Violation: Bluetooth driver inspects internal DT properties of the power sequencer provider. --

commit 30256477703156517ef407c26086261c2d43650f
Author: Manivannan Sadhasivam <[email protected]>

Bluetooth: hci_qca: Set 'bt_en_available' based on W_DISABLE2# presence in M.2 connector

This commit checks if the M.2 connector supports the W_DISABLE2# property by querying the power sequencer provider's device node. If unavailable, it sets the 'bt_en_available' flag to false, which is used to set the HCI_QUIRK_NON_PERSISTENT_SETUP quirk.

Quoted 3 lines
drivers/bluetooth/hci_qca.c +0 -0

[ ... ]

Quoted 4 lines
drivers/bluetooth/hci_qca.c +0 -0
@@ -2449,10 +2449,17 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 		 * the M.2 Key E connector.
 		 */
 		if (of_graph_is_present(dev_of_node(&serdev->ctrl->dev))) {

drivers/bluetooth/hci_qca.c static int qca_serdev_probe(struct serdev_device *serdev) · line 2461

Quoted 10 lines
drivers/bluetooth/hci_qca.c +6 -0
+			struct device *dev;
+
 			qcadev->bt_power->pwrseq = devm_pwrseq_get(&serdev->ctrl->dev,
 								   "uart");
 			if (IS_ERR(qcadev->bt_power->pwrseq))
 				return PTR_ERR(qcadev->bt_power->pwrseq);
+
+			dev = pwrseq_to_device(qcadev->bt_power->pwrseq);
+			if (!device_property_present(dev, "w-disable2-gpios"))
+				bt_en_available = false;

Does inspecting the internal properties of the power sequencer provider break the pwrseq abstraction model?

By extracting the provider's device with pwrseq_to_device() and directly querying the "w-disable2-gpios" property, the consumer (Bluetooth driver) becomes tightly coupled to the internal hardware details of the power sequencer.

Could this cause silent failures on platforms using ACPI, or if a different power sequencer is used that handles resets without the "w-disable2-gpios" property?

Would it be more robust if the pwrseq framework exposed a generic capability query API rather than forcing consumers to parse the provider's device properties?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
maintainer Reviewed-by on v2 9/9
#20 lore raw

Re: [PATCH v2 9/9] Bluetooth: hci_qca: Set 'bt_en_available' based on W_DISABLE2# presence in M.2 connector

In reply to Manivannan Sadhasivam
Quoted 26 lines · On Thu, 7 May 2026 18:06:17 +0200, Manivannan Sadhasivam via B4 Relay…

From: Manivannan Sadhasivam <[email protected]>

Check if the M.2 connector supports the W_DISABLE2# property or not by querying the pwrseq provider's DT node. If not available, then set 'bt_en_available' flag to 'false'. This flag is used to set the HCI_QUIRK_NON_PERSISTENT_SETUP HCI quirk, which informs the HCI layer whether the shutdown() callback for the device can be triggered or not.

---
 drivers/bluetooth/hci_qca.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/bluetooth/hci_qca.c +2 -0
index 3e71a72ea7c7..b5439b9956cf 100644
@@ -2449,10 +2449,17 @@ static int qca_serdev_probe(struct serdev_device *serdev)
 		 * the M.2 Key E connector.
 		 */
 		if (of_graph_is_present(dev_of_node(&serdev->ctrl->dev))) {
+			struct device *dev;
+
 			qcadev->bt_power->pwrseq = devm_pwrseq_get(&serdev->ctrl->dev,
 								   "uart");

drivers/bluetooth/hci_qca.c static int qca_serdev_probe(struct serdev_device *serdev) · line 2465

Quoted 10 lines
drivers/bluetooth/hci_qca.c +5 -0
 			if (IS_ERR(qcadev->bt_power->pwrseq))
 				return PTR_ERR(qcadev->bt_power->pwrseq);
+
+			dev = pwrseq_to_device(qcadev->bt_power->pwrseq);
+			if (!device_property_present(dev, "w-disable2-gpios"))
+				bt_en_available = false;
+
 			break;
 		}
 

Just one nit: I'd switch the order of patches 7 and 8 in this series so that I can queue the pwrseq patches in an immutable branch and provide it to the bluetooth tree for v7.2

Bart

Tested-by on the v2 series
#21 lore raw
In reply to Manivannan Sadhasivam

Hi Mani,

Tested with WCN685x on both M.2 card (pwrseq-pcie-m2) and direct attach (pwrseq-qcom-wcn). BT works correctly in both cases.

Quoted 58 lines · On 5/8/2026 12:06 AM, Manivannan Sadhasivam via B4 Relay wrote:

Hi,

This series has several key improvements and fixes to the M.2 power sequencing driver and also the BT HCI_QCA driver. Notably, this series allows the M.2 power sequencing driver to work with more M.2 cards, not just WCN7850. It also allows the BT HCI_QCA driver to detect whether it can control BT_EN (or W_DISABLE2#) signal on the connector and set the HCI_QUIRK_NON_PERSISTENT_SETUP quirk.

Testing
=======

This series was tested on Lenovo Thinkpad T14s together with the below DTS patches:
https://github.com/Mani-Sadhasivam/linux/commit/29534d15307551b2355eb254601dec511169f0aa
https://github.com/Mani-Sadhasivam/linux/commit/f4eaacfe647674be200847092b43cdef2194fc55

Merge Strategy
==============

Since the BT HCI_QCA changes depend on the pwrseq changes, it would be good to merge the whole series through pwrseq tree or through an immutable branch.

---
Changes in v2:
- Dropped the pwrseq_is_fixed() change in favor or exporting pwrseq device's dev pointer and using it to check for the presence of W_DISABLE2# property
- Dropped the BT_EN fix for the Qcom WCN devices since it will be handled separately
- Collected tags
- Link to v1: https://patch.msgid.link/[email protected]

---
Manivannan Sadhasivam (9):
power: sequencing: pcie-m2: Fix inconsistent function prefixes
power: sequencing: pcie-m2: Allow creating serdev for multiple PCI devices
power: sequencing: pcie-m2: Improve PCI device ID check
power: sequencing: pcie-m2: Create serdev for PCI devices present before probe
power: sequencing: pcie-m2: Create BT node based on the pci_device_id[] table
Bluetooth: hci_qca: Add M.2 Bluetooth device support using pwrseq
Bluetooth: hci_qca: Rename 'power_ctrl_enabled' to 'bt_en_available'
power: sequencing: Add an API to return the pwrseq device's 'dev' pointer
Bluetooth: hci_qca: Set 'bt_en_available' based on W_DISABLE2# presence in M.2 connector

 drivers/bluetooth/hci_qca.c               |  28 +++-
 drivers/power/sequencing/core.c           |   9 ++
 drivers/power/sequencing/pwrseq-pcie-m2.c | 234 ++++++++++++++++++++++--------
 include/linux/pwrseq/consumer.h           |   7 +
 4 files changed, 217 insertions(+), 61 deletions(-)

---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260422-pwrseq-m2-bt-abdaa71094eb

Best regards,

Signature · 1 line
Manivannan Sadhasivam <[email protected]>
Signature · 2 lines
Best Regards,
Wei Deng