]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
power: sequencing: pcie-m2: Create serdev for PCI devices present before probe
authorManivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
Tue, 19 May 2026 08:55:59 +0000 (14:25 +0530)
committerBartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Mon, 8 Jun 2026 07:47:56 +0000 (09:47 +0200)
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.

Tested-by: Wei Deng <wei.deng@oss.qualcomm.com>
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
Link: https://patch.msgid.link/20260519-pwrseq-m2-bt-v3-4-b39dc2ae3966@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
drivers/power/sequencing/pwrseq-pcie-m2.c

index 038271207a27e486920d0f41842a9805d685f500..8164c442897751b05b91406c6a5c980649751c69 100644 (file)
@@ -236,7 +236,7 @@ err_destroy_changeset:
        return ret;
 }
 
-static int pwrseq_pcie_m2_create_serdev(struct pwrseq_pcie_m2_ctx *ctx,
+static int pwrseq_pcie_m2_create_serdev_one(struct pwrseq_pcie_m2_ctx *ctx,
                                        struct pci_dev *pdev)
 {
        struct serdev_controller *serdev_ctrl;
@@ -259,6 +259,14 @@ 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 */
+       scoped_guard(mutex, &ctx->list_lock) {
+               list_for_each_entry(pci_dev, &ctx->pci_devices, list) {
+                       if (pci_dev->pdev == pdev)
+                               return 0;
+               }
+       }
+
        pci_dev = kzalloc(sizeof(*pci_dev), GFP_KERNEL);
        if (!pci_dev) {
                ret = -ENOMEM;
@@ -368,7 +376,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_one(ctx, pdev);
                        if (ret)
                                return notifier_from_errno(ret);
                }
@@ -400,7 +408,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 +416,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_one(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 +527,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: