[RFC,v3,06/22] scsi: libsas: Improve sas_ex_discover_expander() error handling

Message ID 1666693096-180008-7-git-send-email-john.garry@huawei.com
State New
Headers
Series blk-mq/libata/scsi: SCSI driver tagging improvements Part I |

Commit Message

John Garry Oct. 25, 2022, 10:18 a.m. UTC
  Errors in function sas_ex_discover_expander() are generally ignored, so add
handling to gracefully handles errors in all cases.

Signed-off-by: John Garry <john.garry@huawei.com>
---
 drivers/scsi/libsas/sas_expander.c | 50 ++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 16 deletions(-)
  

Patch

diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
index 2907ca5d0ed4..9e33014ec412 100644
--- a/drivers/scsi/libsas/sas_expander.c
+++ b/drivers/scsi/libsas/sas_expander.c
@@ -919,7 +919,6 @@  static struct domain_device *sas_ex_discover_expander(
 	struct sas_rphy *rphy;
 	struct sas_expander_device *edev;
 	struct asd_sas_port *port;
-	int res;
 
 	if (phy->routing_attr == DIRECT_ROUTING) {
 		pr_warn("ex %016llx:%02d:D <--> ex %016llx:0x%x is not allowed\n",
@@ -933,9 +932,11 @@  static struct domain_device *sas_ex_discover_expander(
 		return NULL;
 
 	phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
-	/* FIXME: better error handling */
-	BUG_ON(sas_port_add(phy->port) != 0);
+	if (!phy->port)
+		goto err_put_child;
 
+	if (!sas_port_add(phy->port))
+		goto err_delete_port;
 
 	switch (phy->attached_dev_type) {
 	case SAS_EDGE_EXPANDER_DEVICE:
@@ -948,8 +949,15 @@  static struct domain_device *sas_ex_discover_expander(
 		break;
 	default:
 		rphy = NULL;	/* shut gcc up */
-		BUG();
+		pr_warn("ex unhandled dev type %d %016llx:%02d:D <--> ex %016llx:%02d\n",
+			phy->attached_dev_type,
+			SAS_ADDR(parent->sas_addr), phy_id,
+			SAS_ADDR(phy->attached_sas_addr),
+			phy->attached_phy_id);
 	}
+	if (!rphy)
+		goto err_free_port;
+
 	port = parent->port;
 	child->rphy = rphy;
 	get_device(&rphy->dev);
@@ -967,26 +975,36 @@  static struct domain_device *sas_ex_discover_expander(
 	parent->port->disc.max_level = max(parent->port->disc.max_level,
 					   edev->level);
 	sas_init_dev(child);
+
 	sas_fill_in_rphy(child, rphy);
-	sas_rphy_add(rphy);
+	if (!sas_rphy_add(rphy))
+		goto err_rphy_free;
 
 	spin_lock_irq(&parent->port->dev_list_lock);
 	list_add_tail(&child->dev_list_node, &parent->port->dev_list);
 	spin_unlock_irq(&parent->port->dev_list_lock);
 
-	res = sas_discover_expander(child);
-	if (res) {
-		sas_rphy_delete(rphy);
-		spin_lock_irq(&parent->port->dev_list_lock);
-		list_del(&child->dev_list_node);
-		spin_unlock_irq(&parent->port->dev_list_lock);
-		sas_put_device(child);
-		sas_port_delete(phy->port);
-		phy->port = NULL;
-		return NULL;
-	}
+	if (!sas_discover_expander(child))
+		goto err_rphy_remove;
+
 	list_add_tail(&child->siblings, &parent->ex_dev.children);
 	return child;
+
+err_rphy_remove:
+	spin_lock_irq(&parent->port->dev_list_lock);
+	list_del(&child->dev_list_node);
+	spin_unlock_irq(&parent->port->dev_list_lock);
+	sas_rphy_remove(rphy);
+err_rphy_free:
+	sas_rphy_free(rphy);
+err_free_port:
+	sas_port_free(phy->port);
+err_delete_port:
+	sas_port_delete(phy->port);
+	phy->port = NULL;
+err_put_child:
+	sas_put_device(child);
+	return NULL;
 }
 
 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)