[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 13/18] pci: introduce pci_find_the_only_child()
From: |
Vladimir Sementsov-Ogievskiy |
Subject: |
[PATCH v5 13/18] pci: introduce pci_find_the_only_child() |
Date: |
Thu, 16 Feb 2023 21:03:51 +0300 |
To be used in further patch to identify the device hot-plugged into
pcie-root-port.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Reviewed-by: Anton Kuchin <antonkuchin@yandex-team.ru>
---
include/hw/pci/pci.h | 1 +
hw/pci/pci.c | 33 +++++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
index d5a40cd058..b6c9c44527 100644
--- a/include/hw/pci/pci.h
+++ b/include/hw/pci/pci.h
@@ -341,6 +341,7 @@ void pci_for_each_device_under_bus_reverse(PCIBus *bus,
void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin,
pci_bus_fn end, void *parent_state);
PCIDevice *pci_get_function_0(PCIDevice *pci_dev);
+PCIDevice *pci_find_the_only_child(PCIBus *bus, int bus_num, Error **errp);
/* Use this wrapper when specific scan order is not required. */
static inline
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 208c16f450..34fd1fb5b8 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -1771,6 +1771,39 @@ void pci_for_each_device(PCIBus *bus, int bus_num,
}
}
+typedef struct TheOnlyChild {
+ PCIDevice *dev;
+ int count;
+} TheOnlyChild;
+
+static void the_only_child_fn(PCIBus *bus, PCIDevice *dev, void *opaque)
+{
+ TheOnlyChild *s = opaque;
+
+ s->dev = dev;
+ s->count++;
+}
+
+PCIDevice *pci_find_the_only_child(PCIBus *bus, int bus_num, Error **errp)
+{
+ TheOnlyChild res = {0};
+
+ pci_for_each_device(bus, bus_num, the_only_child_fn, &res);
+
+ if (!res.dev) {
+ assert(res.count == 0);
+ error_setg(errp, "No child devices found");
+ return NULL;
+ }
+
+ if (res.count > 1) {
+ error_setg(errp, "Several child devices found");
+ return NULL;
+ }
+
+ return res.dev;
+}
+
const pci_class_desc *get_class_desc(int class)
{
const pci_class_desc *desc;
--
2.34.1
- [PATCH v5 02/18] pci/shpc: change shpc_get_status() return type to uint8_t, (continued)
- [PATCH v5 02/18] pci/shpc: change shpc_get_status() return type to uint8_t, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 04/18] pci/shpc: more generic handle hot-unplug in shpc_slot_command(), Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 03/18] pci/shpc: shpc_slot_command(): handle PWRONLY -> ENABLED transition, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 05/18] pci/shpc: pass PCIDevice pointer to shpc_slot_command(), Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 07/18] pcie: pcie_cap_slot_write_config(): use correct macro, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 09/18] pcie: drop unused PCIExpressIndicator, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 11/18] pcie: introduce pcie_sltctl_powered_off() helper, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 08/18] pcie_regs: drop duplicated indicator value macros, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 10/18] pcie: pcie_cap_slot_enable_power() use correct helper, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 12/18] pcie: set power indicator to off on reset by default, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 13/18] pci: introduce pci_find_the_only_child(),
Vladimir Sementsov-Ogievskiy <=
- [PATCH v5 14/18] qapi/qdev.json: unite DEVICE_* event data into single structure, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 15/18] qapi: add HOTPLUG_STATE infrastructure, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 16/18] shpc: implement HOTPLUG_STATE event and query-hotplug, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 17/18] pcie: implement HOTPLUG_STATE event and query-hotplug, Vladimir Sementsov-Ogievskiy, 2023/02/16
- [PATCH v5 18/18] qapi: introduce DEVICE_ON event, Vladimir Sementsov-Ogievskiy, 2023/02/16