[openib-general] [PATCH] Consolidate access to ib_agent_port_list

Krishna Kumar
Mon Oct 25 13:11:11 PDT 2004


Hi,

This patch is similar to one for MAD that I sent some time earlier.

I could also have split the search routine into two, a get_by_dev
and a get_by_agent, but I felt it was too cumbursome.

Thanks,

- KK

--- ib_agent.c.org      2004-10-25 12:37:56.000000000 -0700
+++ ib_agent.c  2004-10-25 12:42:55.000000000 -0700
@@ -303,12 +303,52 @@
                                              slid, mad, mad_response);
 }

+static inline struct ib_agent_port_private *
+__ib_get_agent_mad(struct ib_device *device, int port_num,
+                  struct ib_mad_agent *mad_agent)
+{
+       struct ib_agent_port_private *entry;
+
+       BUG_ON(!spin_is_locked(&ib_agent_port_list_lock);
+       BUG_ON(!(!!device ^ !!mad_agent));  /* Exactly one MUST be (!NULL) */
+
+       if (device) {
+               list_for_each_entry(entry, &ib_agent_port_list, port_list) {
+                       if (entry->dr_smp_agent->device == device &&
+                           entry->port_num == port_num)
+                               return entry;
+               }
+       } else {
+               list_for_each_entry(entry, &ib_agent_port_list, port_list) {
+                       if ((entry->dr_smp_agent == mad_agent) ||
+                           (entry->lr_smp_agent == mad_agent) ||
+                           (entry->perf_mgmt_agent == mad_agent))
+                               return entry;
+               }
+       }
+       return NULL;
+}
+
+static inline struct ib_agent_port_private *
+ib_get_agent_mad(struct ib_device *device, int port_num,
+                struct ib_mad_agent *mad_agent)
+{
+       struct ib_agent_port_private *entry;
+       unsigned long flags;
+
+       spin_lock_irqsave(&ib_agent_port_list_lock, flags);
+       entry = __ib_get_agent_mad(device, port_num, mad_agent);
+       spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
+
+       return entry;
+}
+
 void agent_mad_send(struct ib_mad_agent *mad_agent,
                    struct ib_mad *mad,
                    struct ib_grh *grh,
                    struct ib_mad_recv_wc *mad_recv_wc)
 {
-       struct ib_agent_port_private *entry, *port_priv = NULL;
+       struct ib_agent_port_private *port_priv;
        struct ib_agent_send_wr *agent_send_wr;
        struct ib_sge gather_list;
        struct ib_send_wr send_wr;
@@ -318,16 +358,7 @@
        unsigned long flags;

        /* Find matching MAD agent */
-       spin_lock_irqsave(&ib_agent_port_list_lock, flags);
-       list_for_each_entry(entry, &ib_agent_port_list, port_list) {
-               if ((entry->dr_smp_agent == mad_agent) ||
-                   (entry->lr_smp_agent == mad_agent) ||
-                   (entry->perf_mgmt_agent == mad_agent)) {
-                       port_priv = entry;
-                       break;
-               }
-       }
-       spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
+       port_priv = ib_get_agent_mad(NULL, 0, mad_agent);
        if (!port_priv) {
                printk(KERN_ERR SPFX "agent_mad_send: no matching MAD agent %p\n",
                       mad_agent);
@@ -524,22 +555,13 @@
 static void agent_send_handler(struct ib_mad_agent *mad_agent,
                               struct ib_mad_send_wc *mad_send_wc)
 {
-       struct ib_agent_port_private    *entry, *port_priv = NULL;
+       struct ib_agent_port_private    *port_priv;
        struct ib_agent_send_wr         *agent_send_wr;
        struct list_head                *send_wr;
        unsigned long                   flags;

        /* Find matching MAD agent */
-       spin_lock_irqsave(&ib_agent_port_list_lock, flags);
-       list_for_each_entry(entry, &ib_agent_port_list, port_list) {
-               if ((entry->dr_smp_agent == mad_agent) ||
-                   (entry->lr_smp_agent == mad_agent) ||
-                   (entry->perf_mgmt_agent == mad_agent)) {
-                       port_priv = entry;
-                       break;
-               }
-       }
-       spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
+       port_priv = ib_get_agent_mad(NULL, 0, mad_agent);
        if (!port_priv) {
                printk(KERN_ERR SPFX "agent_send_handler: no matching MAD agent "
                       "%p\n", mad_agent);
@@ -579,20 +601,10 @@
 static void agent_recv_handler(struct ib_mad_agent *mad_agent,
                               struct ib_mad_recv_wc *mad_recv_wc)
 {
-       struct ib_agent_port_private *entry, *port_priv = NULL;
-       unsigned long flags;
+       struct ib_agent_port_private *port_priv;

        /* Find matching MAD agent */
-       spin_lock_irqsave(&ib_agent_port_list_lock, flags);
-       list_for_each_entry(entry, &ib_agent_port_list, port_list) {
-               if ((entry->dr_smp_agent == mad_agent) ||
-                   (entry->lr_smp_agent == mad_agent) ||
-                   (entry->perf_mgmt_agent == mad_agent)) {
-                       port_priv = entry;
-                       break;
-               }
-       }
-       spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
+       port_priv = ib_get_agent_mad(NULL, 0, mad_agent);
        if (!port_priv) {
                printk(KERN_ERR SPFX "agent_recv_handler: no matching MAD agent %p\n",
                       mad_agent);
@@ -615,20 +627,12 @@
                .addr = 0,
                .size = (unsigned long) high_memory - PAGE_OFFSET
        };
-       struct ib_agent_port_private *entry, *port_priv = NULL;
+       struct ib_agent_port_private *port_priv;
        struct ib_mad_reg_req reg_req;
        unsigned long flags;

        /* First, check if port already open for SMI */
-       spin_lock_irqsave(&ib_agent_port_list_lock, flags);
-       list_for_each_entry(entry, &ib_agent_port_list, port_list) {
-               if (entry->dr_smp_agent->device == device &&
-                   entry->port_num == port_num) {
-                       port_priv = entry;
-                       break;
-               }
-       }
-       spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
+       port_priv = ib_get_agent_mad(device, port_num, NULL);
        if (port_priv) {
                printk(KERN_DEBUG SPFX "%s port %d already open\n",
                       device->name, port_num);
@@ -729,21 +733,14 @@

 static int ib_agent_port_close(struct ib_device *device, int port_num)
 {
-       struct ib_agent_port_private *entry, *port_priv = NULL;
+       struct ib_agent_port_private *port_priv;
        unsigned long flags;

        spin_lock_irqsave(&ib_agent_port_list_lock, flags);
-       list_for_each_entry(entry, &ib_agent_port_list, port_list) {
-               if (entry->dr_smp_agent->device == device &&
-                   entry->port_num == port_num) {
-                       port_priv = entry;
-                       break;
-               }
-       }
-
+       port_priv = __ib_get_agent_mad(NULL, 0, mad_agent);
        if (port_priv == NULL) {
-               printk(KERN_ERR SPFX "Port %d not found\n", port_num);
                spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
+               printk(KERN_ERR SPFX "Port %d not found\n", port_num);
                return -ENODEV;
        }




More information about the openib-general mailing list