From 7038756642711fa53143906b3b3f29900eb4a4ea Mon Sep 17 00:00:00 2001
From: Haren Myneni <haren@linux.ibm.com>
Date: Tue, 13 Aug 2024 14:40:25 -0700
Subject: [PATCH 3/4] drmgr/pci: Add kernel interface support for device tree
 update

Use the following kernel interfaces for PCI device type to update
the device tree if this feature is enabled in the kernel.

dt add index <DRC index>  --> for IO add
dt remove index <DRC index> --> for IO remove

Signed-off-by: Haren Myneni <haren@linux.ibm.com>
---
 src/drmgr/drslot_chrp_pci.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/src/drmgr/drslot_chrp_pci.c b/src/drmgr/drslot_chrp_pci.c
index 3b48de30be66..56f8bb397e67 100644
--- a/src/drmgr/drslot_chrp_pci.c
+++ b/src/drmgr/drslot_chrp_pci.c
@@ -366,7 +366,6 @@ static int add_work(struct dr_node *node)
 	int pow_state;	/* Tells us if power was turned on when	 */
 	int iso_state;	/* Tells us isolation state after 	 */
 	int rc;
-	struct of_node *new_nodes;/* nodes returned from configure_connector */
 
 	/* if we're continuing, set LED_ON and see if a card is really there. */
 	if (process_led(node, LED_ON))
@@ -425,16 +424,26 @@ static int add_work(struct dr_node *node)
 	 * the return status requires a message, print it out
 	 * and exit, otherwise, add the nodes to the OF tree.
 	 */
-	new_nodes = configure_connector(node->drc_index);
-	if (new_nodes == NULL) {
-		rtas_set_indicator(ISOLATION_STATE, node->drc_index, ISOLATE);
-		set_power(node->drc_power, POWER_OFF);
-		return -1;
+	if (kernel_dlpar_exists()) {
+		rc = do_dt_kernel_dlpar(node->drc_index, ADD);
+	} else {
+		struct of_node *new_nodes; /* nodes returned from */
+					   /* configure_connector */
+
+		new_nodes = configure_connector(node->drc_index);
+		if (new_nodes == NULL) {
+			rtas_set_indicator(ISOLATION_STATE, node->drc_index,
+					ISOLATE);
+			set_power(node->drc_power, POWER_OFF);
+			return -1;
+		}
+
+		say(DEBUG, "Adding %s to %s\n", new_nodes->name,
+				node->ofdt_path);
+		rc = add_device_tree_nodes(node->ofdt_path, new_nodes);
+		free_of_node(new_nodes);
 	}
 
-	say(DEBUG, "Adding %s to %s\n", new_nodes->name, node->ofdt_path);
-	rc = add_device_tree_nodes(node->ofdt_path, new_nodes);
-	free_of_node(new_nodes);
 	if (rc) {
 		say(DEBUG, "add_device_tree_nodes failed at %s\n",
 		    node->ofdt_path);
@@ -692,7 +701,10 @@ static struct dr_node *remove_work(struct dr_node *all_nodes)
 	 * the device tree.
 	 */
 	for (child = node->children; child; child = child->next) {
-		rc = remove_device_tree_nodes(child->ofdt_path);
+		if (kernel_dlpar_exists())
+			rc = do_dt_kernel_dlpar(child->drc_index, REMOVE);
+		else
+			rc = remove_device_tree_nodes(child->ofdt_path);
 		if (rc) {
 			say(ERROR, "%s", sw_error);
 			rtas_set_indicator(ISOLATION_STATE, node->drc_index,
-- 
2.46.0

