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

Krishna Kumar
Mon Oct 18 16:21:32 PDT 2004


This patch encapsulates the search of existing device/port open into
a new routine that the existing 3 users can call instead of each of
them going through the ib_mad_port_list list (lock and lockless version).

Thanks,

- KK

--- ib_mad.c.org        2004-10-18 16:16:46.000000000 -0700
+++ ib_mad.c    2004-10-18 16:19:25.000000000 -0700
@@ -89,6 +89,38 @@
                                    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;
+
+       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 +132,7 @@
                                           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 +190,7 @@
        }

        /* 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 +1672,11 @@
        };
        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 +1796,15 @@
  */
 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 openib-general mailing list