[openib-general] [PATCH] Encapsulate searching of device/port into 1 routine

Krishna Kumar krkumar at us.ibm.com
Mon Oct 18 17:14:42 PDT 2004


While creating patch, I inadvertently deleted a line. The function is
ib_get_mad_port() where I missed declaring flags. Please use this patch
instead.

thx,

- KK

--- ib_mad.c.org	2004-10-18 16:16:46.000000000 -0700
+++ ib_mad.c	2004-10-18 17:09:51.000000000 -0700
@@ -89,6 +89,39 @@ static void ib_mad_complete_send_wr(stru
 				    struct ib_mad_send_wc *mad_send_wc);

 /*
+ * Returns a ib_mad_port_private structure or NULL for a device/port.
+ * Assumes ib_mad_port_list_lock is being held.
+ */
+static inline struct ib_mad_port_private *
+__ib_get_mad_port(struct ib_device *device, int port_num)
+{
+	struct ib_mad_port_private *entry;
+
+	BUG_ON(!spin_is_locked(&ib_mad_port_list_lock));
+	list_for_each_entry(entry, &ib_mad_port_list, port_list) {
+		if (entry->device == device && entry->port_num == port_num)
+			return entry;
+	}
+	return NULL;
+}
+
+/*
+ * Wrapper function to return a ib_mad_port_private structure or NULL for
+ * a device/port.
+ */
+static inline struct ib_mad_port_private *
+ib_get_mad_port(struct ib_device *device, int port_num)
+{
+	struct ib_mad_port_private *entry;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ib_mad_port_list_lock, flags);
+	entry = __ib_get_mad_port(device, port_num);
+	spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
+
+	return entry;
+}
+/*
  * ib_register_mad_agent - Register to send/receive MADs
  */
 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
@@ -100,7 +133,7 @@ struct ib_mad_agent *ib_register_mad_age
 					   ib_mad_recv_handler recv_handler,
 					   void *context)
 {
-	struct ib_mad_port_private *entry, *port_priv = NULL;
+	struct ib_mad_port_private *port_priv;
 	struct ib_mad_agent *ret;
 	struct ib_mad_agent_private *mad_agent_priv;
 	struct ib_mad_reg_req *reg_req = NULL;
@@ -158,14 +191,7 @@ struct ib_mad_agent *ib_register_mad_age
 	}

 	/* Validate device and port */
-	spin_lock_irqsave(&ib_mad_port_list_lock, flags);
-	list_for_each_entry(entry, &ib_mad_port_list, port_list) {
-		if (entry->device == device && entry->port_num == port_num) {
-			port_priv = entry;
-			break;
-		}
-	}
-	spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
+	port_priv = ib_get_mad_port(device, port_num);
 	if (!port_priv) {
 		ret = ERR_PTR(-ENODEV);
 		goto error1;
@@ -1647,18 +1673,11 @@ static int ib_mad_port_open(struct ib_de
 	};
 	struct ib_qp_init_attr qp_init_attr;
 	struct ib_qp_cap qp_cap;
-	struct ib_mad_port_private *entry, *port_priv = NULL;
+	struct ib_mad_port_private *port_priv;
 	unsigned long flags;

 	/* First, check if port already open at MAD layer */
-	spin_lock_irqsave(&ib_mad_port_list_lock, flags);
-	list_for_each_entry(entry, &ib_mad_port_list, port_list) {
-		if (entry->device == device && entry->port_num == port_num) {
-			port_priv = entry;
-			break;
-		}
-	}
-	spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
+	port_priv = ib_get_mad_port(device, port_num);
 	if (port_priv) {
 		printk(KERN_DEBUG PFX "%s port %d already open\n",
 		       device->name, port_num);
@@ -1778,20 +1797,15 @@ error3:
  */
 static int ib_mad_port_close(struct ib_device *device, int port_num)
 {
-	struct ib_mad_port_private *entry, *port_priv = NULL;
+	struct ib_mad_port_private *port_priv;
 	unsigned long flags;

 	spin_lock_irqsave(&ib_mad_port_list_lock, flags);
-	list_for_each_entry(entry, &ib_mad_port_list, port_list) {
-		if (entry->device == device && entry->port_num == port_num) {
-			port_priv = entry;
-			break;
-		}
-	}
+	port_priv = __ib_get_mad_port(device, port_num);

 	if (port_priv == NULL) {
-		printk(KERN_ERR PFX "Port %d not found\n", port_num);
 		spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
+		printk(KERN_ERR PFX "Port %d not found\n", port_num);
 		return -ENODEV;
 	}



More information about the general mailing list