]> www.infradead.org Git - nvme.git/commitdiff
PCI: Add managed pcim_intx()
authorPhilipp Stanner <pstanner@redhat.com>
Thu, 13 Jun 2024 11:50:23 +0000 (13:50 +0200)
committerBjorn Helgaas <bhelgaas@google.com>
Thu, 11 Jul 2024 21:20:01 +0000 (16:20 -0500)
pci_intx() is a "hybrid" function, i.e., it is managed if
pcim_enable_device() has been called, but unmanaged otherwise.

Add pcim_intx(), which is always managed, and implement pci_intx() using
it.

Remove the now-unused struct pci_devres.orig_intx and .restore_intx and
find_pci_dr().

Link: https://lore.kernel.org/r/20240613115032.29098-11-pstanner@redhat.com
Signed-off-by: Philipp Stanner <pstanner@redhat.com>
[kwilczynski: squashed in
https://lore.kernel.org/r/426645d40776198e0fcc942f4a6cac4433c7a9aa.camel@redhat.com
to fix problem reported and tested by Ashish Kalra <Ashish.Kalra@amd.com>:
https://lore.kernel.org/r/20240708214656.4721-1-Ashish.Kalra@amd.com
https://lore.kernel.org/r/8c4634e9-4f02-4c54-9c89-d75e2f4bf026@amd.com/]
Signed-off-by: Krzysztof WilczyƄski <kwilczynski@kernel.org>
[bhelgaas: commit log]
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
drivers/pci/devres.c
drivers/pci/pci.c
drivers/pci/pci.h

index d3c29ec30ad735e6560efd36d3e59f35bfe235aa..f7b7184384124cf7bd70a6db7ae30661a522e473 100644 (file)
@@ -43,6 +43,11 @@ struct pcim_iomap_devres {
        void __iomem *table[PCI_STD_NUM_BARS];
 };
 
+/* Used to restore the old INTx state on driver detach. */
+struct pcim_intx_devres {
+       int orig_intx;
+};
+
 enum pcim_addr_devres_type {
        /* Default initializer. */
        PCIM_ADDR_DEVRES_TYPE_INVALID,
@@ -406,27 +411,78 @@ static inline bool mask_contains_bar(int mask, int bar)
        return mask & BIT(bar);
 }
 
-static void pcim_release(struct device *gendev, void *res)
+/*
+ * This is a copy of pci_intx() used to bypass the problem of recursive
+ * function calls due to the hybrid nature of pci_intx().
+ */
+static void __pcim_intx(struct pci_dev *pdev, int enable)
 {
-       struct pci_dev *dev = to_pci_dev(gendev);
-       struct pci_devres *this = res;
+       u16 pci_command, new;
 
-       if (this->restore_intx)
-               pci_intx(dev, this->orig_intx);
+       pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
 
-       if (pci_is_enabled(dev) && !dev->pinned)
-               pci_disable_device(dev);
+       if (enable)
+               new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
+       else
+               new = pci_command | PCI_COMMAND_INTX_DISABLE;
+
+       if (new != pci_command)
+               pci_write_config_word(pdev, PCI_COMMAND, new);
 }
 
-/*
- * TODO: After the last four callers in pci.c are ported, find_pci_dr()
- * needs to be made static again.
+static void pcim_intx_restore(struct device *dev, void *data)
+{
+       struct pci_dev *pdev = to_pci_dev(dev);
+       struct pcim_intx_devres *res = data;
+
+       __pcim_intx(pdev, res->orig_intx);
+}
+
+static struct pcim_intx_devres *get_or_create_intx_devres(struct device *dev)
+{
+       struct pcim_intx_devres *res;
+
+       res = devres_find(dev, pcim_intx_restore, NULL, NULL);
+       if (res)
+               return res;
+
+       res = devres_alloc(pcim_intx_restore, sizeof(*res), GFP_KERNEL);
+       if (res)
+               devres_add(dev, res);
+
+       return res;
+}
+
+/**
+ * pcim_intx - managed pci_intx()
+ * @pdev: the PCI device to operate on
+ * @enable: boolean: whether to enable or disable PCI INTx
+ *
+ * Returns: 0 on success, -ENOMEM on error.
+ *
+ * Enable/disable PCI INTx for device @pdev.
+ * Restore the original state on driver detach.
  */
-struct pci_devres *find_pci_dr(struct pci_dev *pdev)
+int pcim_intx(struct pci_dev *pdev, int enable)
 {
-       if (pci_is_managed(pdev))
-               return devres_find(&pdev->dev, pcim_release, NULL, NULL);
-       return NULL;
+       struct pcim_intx_devres *res;
+
+       res = get_or_create_intx_devres(&pdev->dev);
+       if (!res)
+               return -ENOMEM;
+
+       res->orig_intx = !enable;
+       __pcim_intx(pdev, enable);
+
+       return 0;
+}
+
+static void pcim_release(struct device *gendev, void *res)
+{
+       struct pci_dev *dev = to_pci_dev(gendev);
+
+       if (pci_is_enabled(dev) && !dev->pinned)
+               pci_disable_device(dev);
 }
 
 static struct pci_devres *get_pci_dr(struct pci_dev *pdev)
index a0fad5990f03cffda6d1819e1982b278e7af935c..807f8be043cd43d0ddc1d6b3cdefa65aed522064 100644 (file)
@@ -4442,12 +4442,18 @@ void pci_disable_parity(struct pci_dev *dev)
  * NOTE:
  * This is a "hybrid" function: It's normally unmanaged, but becomes managed
  * when pcim_enable_device() has been called in advance. This hybrid feature is
- * DEPRECATED!
+ * DEPRECATED! If you want managed cleanup, use pcim_intx() instead.
  */
 void pci_intx(struct pci_dev *pdev, int enable)
 {
        u16 pci_command, new;
 
+       /* Preserve the "hybrid" behavior for backwards compatibility */
+       if (pci_is_managed(pdev)) {
+               WARN_ON_ONCE(pcim_intx(pdev, enable) != 0);
+               return;
+       }
+
        pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
 
        if (enable)
@@ -4455,17 +4461,8 @@ void pci_intx(struct pci_dev *pdev, int enable)
        else
                new = pci_command | PCI_COMMAND_INTX_DISABLE;
 
-       if (new != pci_command) {
-               struct pci_devres *dr;
-
+       if (new != pci_command)
                pci_write_config_word(pdev, PCI_COMMAND, new);
-
-               dr = find_pci_dr(pdev);
-               if (dr && !dr->restore_intx) {
-                       dr->restore_intx = 1;
-                       dr->orig_intx = !enable;
-               }
-       }
 }
 EXPORT_SYMBOL_GPL(pci_intx);
 
index 9a12f6774431fefd8ba3166329688793bc88bfe8..21cb44176350819d28305979e361b902734551f5 100644 (file)
@@ -816,16 +816,17 @@ static inline pci_power_t mid_pci_get_power_state(struct pci_dev *pdev)
  * there's no need to track it separately.  pci_devres is initialized
  * when a device is enabled using managed PCI device enable interface.
  *
- * TODO: Struct pci_devres and find_pci_dr() only need to be here because
- * they're used in pci.c.  Port or move these functions to devres.c and
- * then remove them from here.
+ * TODO: Struct pci_devres only needs to be here because they're used in pci.c.
+ * Port or move these functions to devres.c and then remove them from here.
  */
 struct pci_devres {
-       unsigned int orig_intx:1;
-       unsigned int restore_intx:1;
+       /*
+        * TODO:
+        * This struct is now surplus. Remove it by refactoring pci/devres.c
+        */
 };
 
-struct pci_devres *find_pci_dr(struct pci_dev *pdev);
+int pcim_intx(struct pci_dev *dev, int enable);
 
 int pcim_request_region(struct pci_dev *pdev, int bar, const char *name);
 int pcim_request_region_exclusive(struct pci_dev *pdev, int bar,