Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / infiniband / core / mad.c
index 23628c6..5ad41a6 100644 (file)
@@ -1,5 +1,7 @@
 /*
  * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
+ * Copyright (c) 2005 Intel Corporation.  All rights reserved.
+ * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  *
- * $Id: mad.c 1389 2004-12-27 22:56:47Z roland $
+ * $Id: mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
  */
-
 #include <linux/dma-mapping.h>
 
 #include "mad_priv.h"
+#include "mad_rmpp.h"
 #include "smi.h"
 #include "agent.h"
 
@@ -45,6 +47,7 @@ MODULE_AUTHOR("Sean Hefty");
 
 
 kmem_cache_t *ib_mad_cache;
+
 static struct list_head ib_mad_port_list;
 static u32 ib_mad_client_id = 0;
 
@@ -58,16 +61,12 @@ static int method_in_use(struct ib_mad_mgmt_method_table **method,
 static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
 static struct ib_mad_agent_private *find_mad_agent(
                                        struct ib_mad_port_private *port_priv,
-                                       struct ib_mad *mad, int solicited);
+                                       struct ib_mad *mad);
 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
                                    struct ib_mad_private *mad);
 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
-static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
-                                   struct ib_mad_send_wc *mad_send_wc);
 static void timeout_sends(void *data);
-static void cancel_sends(void *data);
 static void local_completions(void *data);
-static int solicited_mad(struct ib_mad *mad);
 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
                              struct ib_mad_agent_private *agent_priv,
                              u8 mgmt_class);
@@ -197,8 +196,8 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
        if (qpn == -1)
                goto error1;
 
-       if (rmpp_version)
-               goto error1;    /* XXX: until RMPP implemented */
+       if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
+               goto error1;
 
        /* Validate MAD registration request if supplied */
        if (mad_reg_req) {
@@ -228,6 +227,11 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
                        if (!is_vendor_oui(mad_reg_req->oui))
                                goto error1;
                }
+               /* Make sure class supplied is consistent with RMPP */
+               if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
+                       if (rmpp_version)
+                               goto error1;
+               }
                /* Make sure class supplied is consistent with QP type */
                if (qp_type == IB_QPT_SMI) {
                        if ((mad_reg_req->mgmt_class !=
@@ -256,27 +260,33 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
        }
 
        /* Allocate structures */
-       mad_agent_priv = kmalloc(sizeof *mad_agent_priv, GFP_KERNEL);
+       mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
        if (!mad_agent_priv) {
                ret = ERR_PTR(-ENOMEM);
                goto error1;
        }
 
+       mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
+                                                IB_ACCESS_LOCAL_WRITE);
+       if (IS_ERR(mad_agent_priv->agent.mr)) {
+               ret = ERR_PTR(-ENOMEM);
+               goto error2;
+       }
+
        if (mad_reg_req) {
                reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
                if (!reg_req) {
                        ret = ERR_PTR(-ENOMEM);
-                       goto error2;
+                       goto error3;
                }
                /* Make a copy of the MAD registration request */
                memcpy(reg_req, mad_reg_req, sizeof *reg_req);
        }
 
        /* Now, fill in the various structures */
-       memset(mad_agent_priv, 0, sizeof *mad_agent_priv);
        mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
        mad_agent_priv->reg_req = reg_req;
-       mad_agent_priv->rmpp_version = rmpp_version;
+       mad_agent_priv->agent.rmpp_version = rmpp_version;
        mad_agent_priv->agent.device = device;
        mad_agent_priv->agent.recv_handler = recv_handler;
        mad_agent_priv->agent.send_handler = send_handler;
@@ -301,7 +311,7 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
                                if (method) {
                                        if (method_in_use(&method,
                                                           mad_reg_req))
-                                               goto error3;
+                                               goto error4;
                                }
                        }
                        ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
@@ -317,14 +327,14 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
                                        if (is_vendor_method_in_use(
                                                        vendor_class,
                                                        mad_reg_req))
-                                               goto error3;
+                                               goto error4;
                                }
                        }
                        ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
                }
                if (ret2) {
                        ret = ERR_PTR(ret2);
-                       goto error3;
+                       goto error4;
                }
        }
 
@@ -335,20 +345,22 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
        spin_lock_init(&mad_agent_priv->lock);
        INIT_LIST_HEAD(&mad_agent_priv->send_list);
        INIT_LIST_HEAD(&mad_agent_priv->wait_list);
+       INIT_LIST_HEAD(&mad_agent_priv->done_list);
+       INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
        INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv);
        INIT_LIST_HEAD(&mad_agent_priv->local_list);
        INIT_WORK(&mad_agent_priv->local_work, local_completions,
                   mad_agent_priv);
-       INIT_LIST_HEAD(&mad_agent_priv->canceled_list);
-       INIT_WORK(&mad_agent_priv->canceled_work, cancel_sends, mad_agent_priv);
        atomic_set(&mad_agent_priv->refcount, 1);
-       init_waitqueue_head(&mad_agent_priv->wait);
+       init_completion(&mad_agent_priv->comp);
 
        return &mad_agent_priv->agent;
 
-error3:
+error4:
        spin_unlock_irqrestore(&port_priv->reg_lock, flags);
        kfree(reg_req);
+error3:
+       ib_dereg_mr(mad_agent_priv->agent.mr);
 error2:
        kfree(mad_agent_priv);
 error1:
@@ -440,14 +452,13 @@ struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
                goto error1;
        }
        /* Allocate structures */
-       mad_snoop_priv = kmalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
+       mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
        if (!mad_snoop_priv) {
                ret = ERR_PTR(-ENOMEM);
                goto error1;
        }
 
        /* Now, fill in the various structures */
-       memset(mad_snoop_priv, 0, sizeof *mad_snoop_priv);
        mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
        mad_snoop_priv->agent.device = device;
        mad_snoop_priv->agent.recv_handler = recv_handler;
@@ -456,7 +467,7 @@ struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
        mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
        mad_snoop_priv->agent.port_num = port_num;
        mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
-       init_waitqueue_head(&mad_snoop_priv->wait);
+       init_completion(&mad_snoop_priv->comp);
        mad_snoop_priv->snoop_index = register_snoop_agent(
                                                &port_priv->qp_info[qpn],
                                                mad_snoop_priv);
@@ -475,6 +486,18 @@ error1:
 }
 EXPORT_SYMBOL(ib_register_mad_snoop);
 
+static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
+{
+       if (atomic_dec_and_test(&mad_agent_priv->refcount))
+               complete(&mad_agent_priv->comp);
+}
+
+static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
+{
+       if (atomic_dec_and_test(&mad_snoop_priv->refcount))
+               complete(&mad_snoop_priv->comp);
+}
+
 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
 {
        struct ib_mad_port_private *port_priv;
@@ -487,25 +510,22 @@ static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
         * MADs, preventing us from queuing additional work
         */
        cancel_mads(mad_agent_priv);
-
        port_priv = mad_agent_priv->qp_info->port_priv;
-
        cancel_delayed_work(&mad_agent_priv->timed_work);
-       flush_workqueue(port_priv->wq);
 
        spin_lock_irqsave(&port_priv->reg_lock, flags);
        remove_mad_reg_req(mad_agent_priv);
        list_del(&mad_agent_priv->agent_list);
        spin_unlock_irqrestore(&port_priv->reg_lock, flags);
 
-       /* XXX: Cleanup pending RMPP receives for this agent */
+       flush_workqueue(port_priv->wq);
+       ib_cancel_rmpp_recvs(mad_agent_priv);
 
-       atomic_dec(&mad_agent_priv->refcount);
-       wait_event(mad_agent_priv->wait,
-                  !atomic_read(&mad_agent_priv->refcount));
+       deref_mad_agent(mad_agent_priv);
+       wait_for_completion(&mad_agent_priv->comp);
 
-       if (mad_agent_priv->reg_req)
-               kfree(mad_agent_priv->reg_req);
+       kfree(mad_agent_priv->reg_req);
+       ib_dereg_mr(mad_agent_priv->agent.mr);
        kfree(mad_agent_priv);
 }
 
@@ -520,9 +540,8 @@ static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
        atomic_dec(&qp_info->snoop_count);
        spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
 
-       atomic_dec(&mad_snoop_priv->refcount);
-       wait_event(mad_snoop_priv->wait,
-                  !atomic_read(&mad_snoop_priv->refcount));
+       deref_snoop_agent(mad_snoop_priv);
+       wait_for_completion(&mad_snoop_priv->comp);
 
        kfree(mad_snoop_priv);
 }
@@ -551,6 +570,13 @@ int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
 }
 EXPORT_SYMBOL(ib_unregister_mad_agent);
 
+static inline int response_mad(struct ib_mad *mad)
+{
+       /* Trap represses are responses although response bit is reset */
+       return ((mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
+               (mad->mad_hdr.method & IB_MGMT_METHOD_RESP));
+}
+
 static void dequeue_mad(struct ib_mad_list_head *mad_list)
 {
        struct ib_mad_queue *mad_queue;
@@ -565,7 +591,7 @@ static void dequeue_mad(struct ib_mad_list_head *mad_list)
 }
 
 static void snoop_send(struct ib_mad_qp_info *qp_info,
-                      struct ib_send_wr *send_wr,
+                      struct ib_mad_send_buf *send_buf,
                       struct ib_mad_send_wc *mad_send_wc,
                       int mad_snoop_flags)
 {
@@ -583,9 +609,8 @@ static void snoop_send(struct ib_mad_qp_info *qp_info,
                atomic_inc(&mad_snoop_priv->refcount);
                spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
                mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
-                                                   send_wr, mad_send_wc);
-               if (atomic_dec_and_test(&mad_snoop_priv->refcount))
-                       wake_up(&mad_snoop_priv->wait);
+                                                   send_buf, mad_send_wc);
+               deref_snoop_agent(mad_snoop_priv);
                spin_lock_irqsave(&qp_info->snoop_lock, flags);
        }
        spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
@@ -610,8 +635,7 @@ static void snoop_recv(struct ib_mad_qp_info *qp_info,
                spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
                mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
                                                   mad_recv_wc);
-               if (atomic_dec_and_test(&mad_snoop_priv->refcount))
-                       wake_up(&mad_snoop_priv->wait);
+               deref_snoop_agent(mad_snoop_priv);
                spin_lock_irqsave(&qp_info->snoop_lock, flags);
        }
        spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
@@ -640,10 +664,10 @@ static void build_smp_wc(u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
  * Return < 0 if error
  */
 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
-                                 struct ib_smp *smp,
-                                 struct ib_send_wr *send_wr)
+                                 struct ib_mad_send_wr_private *mad_send_wr)
 {
-       int ret, solicited;
+       int ret;
+       struct ib_smp *smp = mad_send_wr->send_buf.mad;
        unsigned long flags;
        struct ib_mad_local_private *local;
        struct ib_mad_private *mad_priv;
@@ -652,15 +676,24 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
        struct ib_device *device = mad_agent_priv->agent.device;
        u8 port_num = mad_agent_priv->agent.port_num;
        struct ib_wc mad_wc;
+       struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
 
-       if (!smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
+       /*
+        * Directed route handling starts if the initial LID routed part of
+        * a request or the ending LID routed part of a response is empty.
+        * If we are at the start of the LID routed part, don't update the
+        * hop_ptr or hop_cnt.  See section 14.2.2, Vol 1 IB spec.
+        */
+       if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
+            IB_LID_PERMISSIVE &&
+           !smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
                ret = -EINVAL;
                printk(KERN_ERR PFX "Invalid directed route\n");
                goto out;
        }
        /* Check to post send on QP or process locally */
-       ret = smi_check_local_dr_smp(smp, device, port_num);
-       if (!ret || !device->process_mad)
+       ret = smi_check_local_smp(smp, device);
+       if (!ret)
                goto out;
 
        local = kmalloc(sizeof *local, GFP_ATOMIC);
@@ -679,7 +712,8 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
                goto out;
        }
 
-       build_smp_wc(send_wr->wr_id, smp->dr_slid, send_wr->wr.ud.pkey_index,
+       build_smp_wc(send_wr->wr_id, be16_to_cpu(smp->dr_slid),
+                    send_wr->wr.ud.pkey_index,
                     send_wr->wr.ud.port_num, &mad_wc);
 
        /* No GRH for DR SMP */
@@ -689,11 +723,7 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
        switch (ret)
        {
        case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
-               /*
-                * See if response is solicited and
-                * there is a recv handler
-                */
-               if (solicited_mad(&mad_priv->mad.mad) &&
+               if (response_mad(&mad_priv->mad.mad) &&
                    mad_agent_priv->agent.recv_handler) {
                        local->mad_priv = mad_priv;
                        local->recv_mad_agent = mad_agent_priv;
@@ -710,15 +740,13 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
                break;
        case IB_MAD_RESULT_SUCCESS:
                /* Treat like an incoming receive MAD */
-               solicited = solicited_mad(&mad_priv->mad.mad);
                port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
                                            mad_agent_priv->agent.port_num);
                if (port_priv) {
                        mad_priv->mad.mad.mad_hdr.tid =
                                ((struct ib_mad *)smp)->mad_hdr.tid;
                        recv_mad_agent = find_mad_agent(port_priv,
-                                                      &mad_priv->mad.mad,
-                                                       solicited);
+                                                       &mad_priv->mad.mad);
                }
                if (!port_priv || !recv_mad_agent) {
                        kmem_cache_free(ib_mad_cache, mad_priv);
@@ -736,13 +764,7 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
                goto out;
        }
 
-       local->send_wr = *send_wr;
-       local->send_wr.sg_list = local->sg_list;
-       memcpy(local->sg_list, send_wr->sg_list,
-              sizeof *send_wr->sg_list * send_wr->num_sge);
-       local->send_wr.next = NULL;
-       local->tid = send_wr->wr.ud.mad_hdr->tid;
-       local->wr_id = send_wr->wr_id;
+       local->mad_send_wr = mad_send_wr;
        /* Reference MAD agent until send side of local completion handled */
        atomic_inc(&mad_agent_priv->refcount);
        /* Queue local completion to local list */
@@ -750,42 +772,265 @@ static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
        list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
        spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
        queue_work(mad_agent_priv->qp_info->port_priv->wq,
-                 &mad_agent_priv->local_work);
+                  &mad_agent_priv->local_work);
        ret = 1;
 out:
        return ret;
 }
 
-static int ib_send_mad(struct ib_mad_agent_private *mad_agent_priv,
-                      struct ib_mad_send_wr_private *mad_send_wr)
+static int get_pad_size(int hdr_len, int data_len)
+{
+       int seg_size, pad;
+
+       seg_size = sizeof(struct ib_mad) - hdr_len;
+       if (data_len && seg_size) {
+               pad = seg_size - data_len % seg_size;
+               return pad == seg_size ? 0 : pad;
+       } else
+               return seg_size;
+}
+
+static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
+{
+       struct ib_rmpp_segment *s, *t;
+
+       list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
+               list_del(&s->list);
+               kfree(s);
+       }
+}
+
+static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
+                               gfp_t gfp_mask)
+{
+       struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
+       struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
+       struct ib_rmpp_segment *seg = NULL;
+       int left, seg_size, pad;
+
+       send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
+       seg_size = send_buf->seg_size;
+       pad = send_wr->pad;
+
+       /* Allocate data segments. */
+       for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
+               seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
+               if (!seg) {
+                       printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
+                              "alloc failed for len %zd, gfp %#x\n",
+                              sizeof (*seg) + seg_size, gfp_mask);
+                       free_send_rmpp_list(send_wr);
+                       return -ENOMEM;
+               }
+               seg->num = ++send_buf->seg_count;
+               list_add_tail(&seg->list, &send_wr->rmpp_list);
+       }
+
+       /* Zero any padding */
+       if (pad)
+               memset(seg->data + seg_size - pad, 0, pad);
+
+       rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
+                                         agent.rmpp_version;
+       rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
+       ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
+
+       send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
+                                       struct ib_rmpp_segment, list);
+       send_wr->last_ack_seg = send_wr->cur_seg;
+       return 0;
+}
+
+struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
+                                           u32 remote_qpn, u16 pkey_index,
+                                           int rmpp_active,
+                                           int hdr_len, int data_len,
+                                           gfp_t gfp_mask)
+{
+       struct ib_mad_agent_private *mad_agent_priv;
+       struct ib_mad_send_wr_private *mad_send_wr;
+       int pad, message_size, ret, size;
+       void *buf;
+
+       mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
+                                     agent);
+       pad = get_pad_size(hdr_len, data_len);
+       message_size = hdr_len + data_len + pad;
+
+       if ((!mad_agent->rmpp_version &&
+            (rmpp_active || message_size > sizeof(struct ib_mad))) ||
+           (!rmpp_active && message_size > sizeof(struct ib_mad)))
+               return ERR_PTR(-EINVAL);
+
+       size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
+       buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
+       if (!buf)
+               return ERR_PTR(-ENOMEM);
+
+       mad_send_wr = buf + size;
+       INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
+       mad_send_wr->send_buf.mad = buf;
+       mad_send_wr->send_buf.hdr_len = hdr_len;
+       mad_send_wr->send_buf.data_len = data_len;
+       mad_send_wr->pad = pad;
+
+       mad_send_wr->mad_agent_priv = mad_agent_priv;
+       mad_send_wr->sg_list[0].length = hdr_len;
+       mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
+       mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
+       mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
+
+       mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
+       mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
+       mad_send_wr->send_wr.num_sge = 2;
+       mad_send_wr->send_wr.opcode = IB_WR_SEND;
+       mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
+       mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
+       mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
+       mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
+
+       if (rmpp_active) {
+               ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
+               if (ret) {
+                       kfree(buf);
+                       return ERR_PTR(ret);
+               }
+       }
+
+       mad_send_wr->send_buf.mad_agent = mad_agent;
+       atomic_inc(&mad_agent_priv->refcount);
+       return &mad_send_wr->send_buf;
+}
+EXPORT_SYMBOL(ib_create_send_mad);
+
+int ib_get_mad_data_offset(u8 mgmt_class)
+{
+       if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
+               return IB_MGMT_SA_HDR;
+       else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
+                (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
+                (mgmt_class == IB_MGMT_CLASS_BIS))
+               return IB_MGMT_DEVICE_HDR;
+       else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
+                (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
+               return IB_MGMT_VENDOR_HDR;
+       else
+               return IB_MGMT_MAD_HDR;
+}
+EXPORT_SYMBOL(ib_get_mad_data_offset);
+
+int ib_is_mad_class_rmpp(u8 mgmt_class)
+{
+       if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
+           (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
+           (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
+           (mgmt_class == IB_MGMT_CLASS_BIS) ||
+           ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
+            (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
+               return 1;
+       return 0;
+}
+EXPORT_SYMBOL(ib_is_mad_class_rmpp);
+
+void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
+{
+       struct ib_mad_send_wr_private *mad_send_wr;
+       struct list_head *list;
+
+       mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
+                                  send_buf);
+       list = &mad_send_wr->cur_seg->list;
+
+       if (mad_send_wr->cur_seg->num < seg_num) {
+               list_for_each_entry(mad_send_wr->cur_seg, list, list)
+                       if (mad_send_wr->cur_seg->num == seg_num)
+                               break;
+       } else if (mad_send_wr->cur_seg->num > seg_num) {
+               list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
+                       if (mad_send_wr->cur_seg->num == seg_num)
+                               break;
+       }
+       return mad_send_wr->cur_seg->data;
+}
+EXPORT_SYMBOL(ib_get_rmpp_segment);
+
+static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
+{
+       if (mad_send_wr->send_buf.seg_count)
+               return ib_get_rmpp_segment(&mad_send_wr->send_buf,
+                                          mad_send_wr->seg_num);
+       else
+               return mad_send_wr->send_buf.mad +
+                      mad_send_wr->send_buf.hdr_len;
+}
+
+void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
+{
+       struct ib_mad_agent_private *mad_agent_priv;
+       struct ib_mad_send_wr_private *mad_send_wr;
+
+       mad_agent_priv = container_of(send_buf->mad_agent,
+                                     struct ib_mad_agent_private, agent);
+       mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
+                                  send_buf);
+
+       free_send_rmpp_list(mad_send_wr);
+       kfree(send_buf->mad);
+       deref_mad_agent(mad_agent_priv);
+}
+EXPORT_SYMBOL(ib_free_send_mad);
+
+int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
 {
        struct ib_mad_qp_info *qp_info;
+       struct list_head *list;
        struct ib_send_wr *bad_send_wr;
+       struct ib_mad_agent *mad_agent;
+       struct ib_sge *sge;
        unsigned long flags;
        int ret;
 
-       /* Replace user's WR ID with our own to find WR upon completion */
-       qp_info = mad_agent_priv->qp_info;
-       mad_send_wr->wr_id = mad_send_wr->send_wr.wr_id;
+       /* Set WR ID to find mad_send_wr upon completion */
+       qp_info = mad_send_wr->mad_agent_priv->qp_info;
        mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
        mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
 
+       mad_agent = mad_send_wr->send_buf.mad_agent;
+       sge = mad_send_wr->sg_list;
+       sge[0].addr = dma_map_single(mad_agent->device->dma_device,
+                                    mad_send_wr->send_buf.mad,
+                                    sge[0].length,
+                                    DMA_TO_DEVICE);
+       pci_unmap_addr_set(mad_send_wr, header_mapping, sge[0].addr);
+
+       sge[1].addr = dma_map_single(mad_agent->device->dma_device,
+                                    ib_get_payload(mad_send_wr),
+                                    sge[1].length,
+                                    DMA_TO_DEVICE);
+       pci_unmap_addr_set(mad_send_wr, payload_mapping, sge[1].addr);
+
        spin_lock_irqsave(&qp_info->send_queue.lock, flags);
-       if (qp_info->send_queue.count++ < qp_info->send_queue.max_active) {
-               list_add_tail(&mad_send_wr->mad_list.list,
-                             &qp_info->send_queue.list);
-               spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
-               ret = ib_post_send(mad_agent_priv->agent.qp,
-                                  &mad_send_wr->send_wr, &bad_send_wr);
-               if (ret) {
-                       printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
-                       dequeue_mad(&mad_send_wr->mad_list);
-               }
+       if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
+               ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
+                                  &bad_send_wr);
+               list = &qp_info->send_queue.list;
        } else {
-               list_add_tail(&mad_send_wr->mad_list.list,
-                             &qp_info->overflow_list);
-               spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
                ret = 0;
+               list = &qp_info->overflow_list;
+       }
+
+       if (!ret) {
+               qp_info->send_queue.count++;
+               list_add_tail(&mad_send_wr->mad_list.list, list);
+       }
+       spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
+       if (ret) {
+               dma_unmap_single(mad_agent->device->dma_device,
+                                pci_unmap_addr(mad_send_wr, header_mapping),
+                                sge[0].length, DMA_TO_DEVICE);
+               dma_unmap_single(mad_agent->device->dma_device,
+                                pci_unmap_addr(mad_send_wr, payload_mapping),
+                                sge[1].length, DMA_TO_DEVICE);
        }
        return ret;
 }
@@ -794,45 +1039,35 @@ static int ib_send_mad(struct ib_mad_agent_private *mad_agent_priv,
  * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
  *  with the registered client
  */
-int ib_post_send_mad(struct ib_mad_agent *mad_agent,
-                    struct ib_send_wr *send_wr,
-                    struct ib_send_wr **bad_send_wr)
+int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
+                    struct ib_mad_send_buf **bad_send_buf)
 {
-       int ret = -EINVAL;
        struct ib_mad_agent_private *mad_agent_priv;
-
-       /* Validate supplied parameters */
-       if (!bad_send_wr)
-               goto error1;
-
-       if (!mad_agent || !send_wr)
-               goto error2;
-
-       if (!mad_agent->send_handler)
-               goto error2;
-
-       mad_agent_priv = container_of(mad_agent,
-                                     struct ib_mad_agent_private,
-                                     agent);
+       struct ib_mad_send_buf *next_send_buf;
+       struct ib_mad_send_wr_private *mad_send_wr;
+       unsigned long flags;
+       int ret = -EINVAL;
 
        /* Walk list of send WRs and post each on send list */
-       while (send_wr) {
-               unsigned long                   flags;
-               struct ib_send_wr               *next_send_wr;
-               struct ib_mad_send_wr_private   *mad_send_wr;
-               struct ib_smp                   *smp;
-
-               /* Validate more parameters */
-               if (send_wr->num_sge > IB_MAD_SEND_REQ_MAX_SG)
-                       goto error2;
+       for (; send_buf; send_buf = next_send_buf) {
 
-               if (send_wr->wr.ud.timeout_ms && !mad_agent->recv_handler)
-                       goto error2;
+               mad_send_wr = container_of(send_buf,
+                                          struct ib_mad_send_wr_private,
+                                          send_buf);
+               mad_agent_priv = mad_send_wr->mad_agent_priv;
+
+               if (!send_buf->mad_agent->send_handler ||
+                   (send_buf->timeout_ms &&
+                    !send_buf->mad_agent->recv_handler)) {
+                       ret = -EINVAL;
+                       goto error;
+               }
 
-               if (!send_wr->wr.ud.mad_hdr) {
-                       printk(KERN_ERR PFX "MAD header must be supplied "
-                              "in WR %p\n", send_wr);
-                       goto error2;
+               if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
+                       if (mad_agent_priv->agent.rmpp_version) {
+                               ret = -EINVAL;
+                               goto error;
+                       }
                }
 
                /*
@@ -840,39 +1075,24 @@ int ib_post_send_mad(struct ib_mad_agent *mad_agent,
                 * current one completes, and the user modifies the work
                 * request associated with the completion
                 */
-               next_send_wr = (struct ib_send_wr *)send_wr->next;
+               next_send_buf = send_buf->next;
+               mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
 
-               smp = (struct ib_smp *)send_wr->wr.ud.mad_hdr;
-               if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
-                       ret = handle_outgoing_dr_smp(mad_agent_priv, smp,
-                                                    send_wr);
+               if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
+                   IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
+                       ret = handle_outgoing_dr_smp(mad_agent_priv,
+                                                    mad_send_wr);
                        if (ret < 0)            /* error */
-                               goto error2;
+                               goto error;
                        else if (ret == 1)      /* locally consumed */
-                               goto next;
+                               continue;
                }
 
-               /* Allocate MAD send WR tracking structure */
-               mad_send_wr = kmalloc(sizeof *mad_send_wr, GFP_ATOMIC);
-               if (!mad_send_wr) {
-                       printk(KERN_ERR PFX "No memory for "
-                              "ib_mad_send_wr_private\n");
-                       ret = -ENOMEM;
-                       goto error2;
-               }
-
-               mad_send_wr->send_wr = *send_wr;
-               mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
-               memcpy(mad_send_wr->sg_list, send_wr->sg_list,
-                      sizeof *send_wr->sg_list * send_wr->num_sge);
-               mad_send_wr->send_wr.next = NULL;
-               mad_send_wr->tid = send_wr->wr.ud.mad_hdr->tid;
-               mad_send_wr->agent = mad_agent;
+               mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
                /* Timeout will be updated after send completes */
-               mad_send_wr->timeout = msecs_to_jiffies(send_wr->wr.
-                                                       ud.timeout_ms);
-               mad_send_wr->retry = 0;
-               /* One reference for each work request to QP + response */
+               mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
+               mad_send_wr->retries = send_buf->retries;
+               /* Reference for work request to QP + response */
                mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
                mad_send_wr->status = IB_WC_SUCCESS;
 
@@ -883,23 +1103,25 @@ int ib_post_send_mad(struct ib_mad_agent *mad_agent,
                              &mad_agent_priv->send_list);
                spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
 
-               ret = ib_send_mad(mad_agent_priv, mad_send_wr);
-               if (ret) {
+               if (mad_agent_priv->agent.rmpp_version) {
+                       ret = ib_send_rmpp_mad(mad_send_wr);
+                       if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
+                               ret = ib_send_mad(mad_send_wr);
+               } else
+                       ret = ib_send_mad(mad_send_wr);
+               if (ret < 0) {
                        /* Fail send request */
                        spin_lock_irqsave(&mad_agent_priv->lock, flags);
                        list_del(&mad_send_wr->agent_list);
                        spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
                        atomic_dec(&mad_agent_priv->refcount);
-                       goto error2;
+                       goto error;
                }
-next:
-               send_wr = next_send_wr;
        }
        return 0;
-
-error2:
-       *bad_send_wr = send_wr;
-error1:
+error:
+       if (bad_send_buf)
+               *bad_send_buf = send_buf;
        return ret;
 }
 EXPORT_SYMBOL(ib_post_send_mad);
@@ -910,41 +1132,28 @@ EXPORT_SYMBOL(ib_post_send_mad);
  */
 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
 {
-       struct ib_mad_recv_buf *entry;
+       struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
        struct ib_mad_private_header *mad_priv_hdr;
        struct ib_mad_private *priv;
+       struct list_head free_list;
 
-       mad_priv_hdr = container_of(mad_recv_wc,
-                                   struct ib_mad_private_header,
-                                   recv_wc);
-       priv = container_of(mad_priv_hdr, struct ib_mad_private, header);
+       INIT_LIST_HEAD(&free_list);
+       list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
 
-       /*
-        * Walk receive buffer list associated with this WC
-        * No need to remove them from list of receive buffers
-        */
-       list_for_each_entry(entry, &mad_recv_wc->recv_buf.list, list) {
-               /* Free previous receive buffer */
-               kmem_cache_free(ib_mad_cache, priv);
+       list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
+                                       &free_list, list) {
+               mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
+                                          recv_buf);
                mad_priv_hdr = container_of(mad_recv_wc,
                                            struct ib_mad_private_header,
                                            recv_wc);
                priv = container_of(mad_priv_hdr, struct ib_mad_private,
                                    header);
+               kmem_cache_free(ib_mad_cache, priv);
        }
-
-       /* Free last buffer */
-       kmem_cache_free(ib_mad_cache, priv);
 }
 EXPORT_SYMBOL(ib_free_recv_mad);
 
-void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc,
-                         void *buf)
-{
-       printk(KERN_ERR PFX "ib_coalesce_recv_mad() not implemented yet\n");
-}
-EXPORT_SYMBOL(ib_coalesce_recv_mad);
-
 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
                                        u8 rmpp_version,
                                        ib_mad_send_handler send_handler,
@@ -983,14 +1192,12 @@ static int method_in_use(struct ib_mad_mgmt_method_table **method,
 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
 {
        /* Allocate management method table */
-       *method = kmalloc(sizeof **method, GFP_ATOMIC);
+       *method = kzalloc(sizeof **method, GFP_ATOMIC);
        if (!*method) {
                printk(KERN_ERR PFX "No memory for "
                       "ib_mad_mgmt_method_table\n");
                return -ENOMEM;
        }
-       /* Clear management method table */
-       memset(*method, 0, sizeof **method);
 
        return 0;
 }
@@ -1081,15 +1288,14 @@ static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
        class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
        if (!*class) {
                /* Allocate management class table for "new" class version */
-               *class = kmalloc(sizeof **class, GFP_ATOMIC);
+               *class = kzalloc(sizeof **class, GFP_ATOMIC);
                if (!*class) {
                        printk(KERN_ERR PFX "No memory for "
                               "ib_mad_mgmt_class_table\n");
                        ret = -ENOMEM;
                        goto error1;
                }
-               /* Clear management class table */
-               memset(*class, 0, sizeof(**class));
+
                /* Allocate method table for this management class */
                method = &(*class)->method_table[mgmt_class];
                if ((ret = allocate_method_table(method)))
@@ -1153,25 +1359,24 @@ static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
                                mad_reg_req->mgmt_class_version].vendor;
        if (!*vendor_table) {
                /* Allocate mgmt vendor class table for "new" class version */
-               vendor = kmalloc(sizeof *vendor, GFP_ATOMIC);
+               vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
                if (!vendor) {
                        printk(KERN_ERR PFX "No memory for "
                               "ib_mad_mgmt_vendor_class_table\n");
                        goto error1;
                }
-               /* Clear management vendor class table */
-               memset(vendor, 0, sizeof(*vendor));
+
                *vendor_table = vendor;
        }
        if (!(*vendor_table)->vendor_class[vclass]) {
                /* Allocate table for this management vendor class */
-               vendor_class = kmalloc(sizeof *vendor_class, GFP_ATOMIC);
+               vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
                if (!vendor_class) {
                        printk(KERN_ERR PFX "No memory for "
                               "ib_mad_mgmt_vendor_class\n");
                        goto error2;
                }
-               memset(vendor_class, 0, sizeof(*vendor_class));
+
                (*vendor_table)->vendor_class[vclass] = vendor_class;
        }
        for (i = 0; i < MAX_MGMT_OUI; i++) {
@@ -1338,42 +1543,15 @@ out:
        return;
 }
 
-static int response_mad(struct ib_mad *mad)
-{
-       /* Trap represses are responses although response bit is reset */
-       return ((mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
-               (mad->mad_hdr.method & IB_MGMT_METHOD_RESP));
-}
-
-static int solicited_mad(struct ib_mad *mad)
-{
-       /* CM MADs are never solicited */
-       if (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CM) {
-               return 0;
-       }
-
-       /* XXX: Determine whether MAD is using RMPP */
-
-       /* Not using RMPP */
-       /* Is this MAD a response to a previous MAD ? */
-       return response_mad(mad);
-}
-
 static struct ib_mad_agent_private *
 find_mad_agent(struct ib_mad_port_private *port_priv,
-              struct ib_mad *mad,
-              int solicited)
+              struct ib_mad *mad)
 {
        struct ib_mad_agent_private *mad_agent = NULL;
        unsigned long flags;
 
        spin_lock_irqsave(&port_priv->reg_lock, flags);
-
-       /*
-        * Whether MAD was solicited determines type of routing to
-        * MAD client.
-        */
-       if (solicited) {
+       if (response_mad(mad)) {
                u32 hi_tid;
                struct ib_mad_agent_private *entry;
 
@@ -1382,8 +1560,7 @@ find_mad_agent(struct ib_mad_port_private *port_priv,
                 * of MAD.
                 */
                hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
-               list_for_each_entry(entry, &port_priv->agent_list,
-                                   agent_list) {
+               list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
                        if (entry->agent.hi_tid == hi_tid) {
                                mad_agent = entry;
                                break;
@@ -1477,27 +1654,71 @@ out:
        return valid;
 }
 
-/*
- * Return start of fully reassembled MAD, or NULL, if MAD isn't assembled yet
- */
-static struct ib_mad_private *
-reassemble_recv(struct ib_mad_agent_private *mad_agent_priv,
-               struct ib_mad_private *recv)
+static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
+                      struct ib_mad_hdr *mad_hdr)
 {
-       /* Until we have RMPP, all receives are reassembled!... */
-       INIT_LIST_HEAD(&recv->header.recv_wc.recv_buf.list);
-       return recv;
+       struct ib_rmpp_mad *rmpp_mad;
+
+       rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
+       return !mad_agent_priv->agent.rmpp_version ||
+               !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
+                                   IB_MGMT_RMPP_FLAG_ACTIVE) ||
+               (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
 }
 
-static struct ib_mad_send_wr_private*
-find_send_req(struct ib_mad_agent_private *mad_agent_priv,
-             u64 tid)
+static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
+                                    struct ib_mad_recv_wc *rwc)
+{
+       return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
+               rwc->recv_buf.mad->mad_hdr.mgmt_class;
+}
+
+static inline int rcv_has_same_gid(struct ib_mad_send_wr_private *wr,
+                                  struct ib_mad_recv_wc *rwc )
+{
+       struct ib_ah_attr attr;
+       u8 send_resp, rcv_resp;
+
+       send_resp = ((struct ib_mad *)(wr->send_buf.mad))->
+                    mad_hdr.method & IB_MGMT_METHOD_RESP;
+       rcv_resp = rwc->recv_buf.mad->mad_hdr.method & IB_MGMT_METHOD_RESP;
+
+       if (!send_resp && rcv_resp)
+               /* is request/response. GID/LIDs are both local (same). */
+               return 1;
+
+       if (send_resp == rcv_resp)
+               /* both requests, or both responses. GIDs different */
+               return 0;
+
+       if (ib_query_ah(wr->send_buf.ah, &attr))
+               /* Assume not equal, to avoid false positives. */
+               return 0;
+
+       if (!(attr.ah_flags & IB_AH_GRH) && !(rwc->wc->wc_flags & IB_WC_GRH))
+               return attr.dlid == rwc->wc->slid;
+       else if ((attr.ah_flags & IB_AH_GRH) &&
+                (rwc->wc->wc_flags & IB_WC_GRH))
+               return memcmp(attr.grh.dgid.raw,
+                             rwc->recv_buf.grh->sgid.raw, 16) == 0;
+       else
+               /* one has GID, other does not.  Assume different */
+               return 0;
+}
+struct ib_mad_send_wr_private*
+ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
+                struct ib_mad_recv_wc *mad_recv_wc)
 {
        struct ib_mad_send_wr_private *mad_send_wr;
+       struct ib_mad *mad;
+
+       mad = (struct ib_mad *)mad_recv_wc->recv_buf.mad;
 
        list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
                            agent_list) {
-               if (mad_send_wr->tid == tid)
+               if ((mad_send_wr->tid == mad->mad_hdr.tid) &&
+                   rcv_has_same_class(mad_send_wr, mad_recv_wc) &&
+                   rcv_has_same_gid(mad_send_wr, mad_recv_wc))
                        return mad_send_wr;
        }
 
@@ -1507,7 +1728,11 @@ find_send_req(struct ib_mad_agent_private *mad_agent_priv,
         */
        list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
                            agent_list) {
-               if (mad_send_wr->tid == tid && mad_send_wr->timeout) {
+               if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
+                   mad_send_wr->tid == mad->mad_hdr.tid &&
+                   mad_send_wr->timeout &&
+                   rcv_has_same_class(mad_send_wr, mad_recv_wc) &&
+                   rcv_has_same_gid(mad_send_wr, mad_recv_wc)) {
                        /* Verify request has not been canceled */
                        return (mad_send_wr->status == IB_WC_SUCCESS) ?
                                mad_send_wr : NULL;
@@ -1516,55 +1741,61 @@ find_send_req(struct ib_mad_agent_private *mad_agent_priv,
        return NULL;
 }
 
+void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
+{
+       mad_send_wr->timeout = 0;
+       if (mad_send_wr->refcount == 1) {
+               list_del(&mad_send_wr->agent_list);
+               list_add_tail(&mad_send_wr->agent_list,
+                             &mad_send_wr->mad_agent_priv->done_list);
+       }
+}
+
 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
-                                struct ib_mad_private *recv,
-                                int solicited)
+                                struct ib_mad_recv_wc *mad_recv_wc)
 {
        struct ib_mad_send_wr_private *mad_send_wr;
        struct ib_mad_send_wc mad_send_wc;
        unsigned long flags;
 
-       /* Fully reassemble receive before processing */
-       recv = reassemble_recv(mad_agent_priv, recv);
-       if (!recv) {
-               if (atomic_dec_and_test(&mad_agent_priv->refcount))
-                       wake_up(&mad_agent_priv->wait);
-               return;
+       INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
+       list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
+       if (mad_agent_priv->agent.rmpp_version) {
+               mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
+                                                     mad_recv_wc);
+               if (!mad_recv_wc) {
+                       deref_mad_agent(mad_agent_priv);
+                       return;
+               }
        }
 
        /* Complete corresponding request */
-       if (solicited) {
+       if (response_mad(mad_recv_wc->recv_buf.mad)) {
                spin_lock_irqsave(&mad_agent_priv->lock, flags);
-               mad_send_wr = find_send_req(mad_agent_priv,
-                                           recv->mad.mad.mad_hdr.tid);
+               mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
                if (!mad_send_wr) {
                        spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
-                       ib_free_recv_mad(&recv->header.recv_wc);
-                       if (atomic_dec_and_test(&mad_agent_priv->refcount))
-                               wake_up(&mad_agent_priv->wait);
+                       ib_free_recv_mad(mad_recv_wc);
+                       deref_mad_agent(mad_agent_priv);
                        return;
                }
-               /* Timeout = 0 means that we won't wait for a response */
-               mad_send_wr->timeout = 0;
+               ib_mark_mad_done(mad_send_wr);
                spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
 
                /* Defined behavior is to complete response before request */
-               recv->header.recv_wc.wc->wr_id = mad_send_wr->wr_id;
-               mad_agent_priv->agent.recv_handler(
-                                               &mad_agent_priv->agent,
-                                               &recv->header.recv_wc);
+               mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
+               mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
+                                                  mad_recv_wc);
                atomic_dec(&mad_agent_priv->refcount);
 
                mad_send_wc.status = IB_WC_SUCCESS;
                mad_send_wc.vendor_err = 0;
-               mad_send_wc.wr_id = mad_send_wr->wr_id;
+               mad_send_wc.send_buf = &mad_send_wr->send_buf;
                ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
        } else {
-               mad_agent_priv->agent.recv_handler(
-                                               &mad_agent_priv->agent,
-                                               &recv->header.recv_wc);
-               if (atomic_dec_and_test(&mad_agent_priv->refcount))
-                       wake_up(&mad_agent_priv->wait);
+               mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
+                                                  mad_recv_wc);
+               deref_mad_agent(mad_agent_priv);
        }
 }
 
@@ -1576,7 +1807,6 @@ static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
        struct ib_mad_private *recv, *response;
        struct ib_mad_list_head *mad_list;
        struct ib_mad_agent_private *mad_agent;
-       int solicited;
 
        response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
        if (!response)
@@ -1623,9 +1853,7 @@ static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
                                            port_priv->device->node_type,
                                            port_priv->port_num))
                        goto out;
-               if (!smi_check_local_dr_smp(&recv->mad.smp,
-                                           port_priv->device,
-                                           port_priv->port_num))
+               if (!smi_check_local_smp(&recv->mad.smp, port_priv->device))
                        goto out;
        }
 
@@ -1652,21 +1880,19 @@ local:
                        if (ret & IB_MAD_RESULT_CONSUMED)
                                goto out;
                        if (ret & IB_MAD_RESULT_REPLY) {
-                               /* Send response */
-                               if (!agent_send(response, &recv->grh, wc,
-                                               port_priv->device,
-                                               port_priv->port_num))
-                                       response = NULL;
+                               agent_send_response(&response->mad.mad,
+                                                   &recv->grh, wc,
+                                                   port_priv->device,
+                                                   port_priv->port_num,
+                                                   qp_info->qp->qp_num);
                                goto out;
                        }
                }
        }
 
-       /* Determine corresponding MAD agent for incoming receive MAD */
-       solicited = solicited_mad(&recv->mad.mad);
-       mad_agent = find_mad_agent(port_priv, &recv->mad.mad, solicited);
+       mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
        if (mad_agent) {
-               ib_mad_complete_recv(mad_agent, recv, solicited);
+               ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
                /*
                 * recv is freed up in error cases in ib_mad_complete_recv
                 * or via recv_handler in ib_mad_complete_recv()
@@ -1710,26 +1936,31 @@ static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
        }
 }
 
-static void wait_for_response(struct ib_mad_agent_private *mad_agent_priv,
-                             struct ib_mad_send_wr_private *mad_send_wr )
+static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
 {
+       struct ib_mad_agent_private *mad_agent_priv;
        struct ib_mad_send_wr_private *temp_mad_send_wr;
        struct list_head *list_item;
        unsigned long delay;
 
+       mad_agent_priv = mad_send_wr->mad_agent_priv;
        list_del(&mad_send_wr->agent_list);
 
        delay = mad_send_wr->timeout;
        mad_send_wr->timeout += jiffies;
 
-       list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
-               temp_mad_send_wr = list_entry(list_item,
-                                             struct ib_mad_send_wr_private,
-                                             agent_list);
-               if (time_after(mad_send_wr->timeout,
-                              temp_mad_send_wr->timeout))
-                       break;
+       if (delay) {
+               list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
+                       temp_mad_send_wr = list_entry(list_item,
+                                               struct ib_mad_send_wr_private,
+                                               agent_list);
+                       if (time_after(mad_send_wr->timeout,
+                                      temp_mad_send_wr->timeout))
+                               break;
+               }
        }
+       else
+               list_item = &mad_agent_priv->wait_list;
        list_add(&mad_send_wr->agent_list, list_item);
 
        /* Reschedule a work item if we have a shorter timeout */
@@ -1740,19 +1971,32 @@ static void wait_for_response(struct ib_mad_agent_private *mad_agent_priv,
        }
 }
 
+void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
+                         int timeout_ms)
+{
+       mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
+       wait_for_response(mad_send_wr);
+}
+
 /*
  * Process a send work completion
  */
-static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
-                                   struct ib_mad_send_wc *mad_send_wc)
+void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
+                            struct ib_mad_send_wc *mad_send_wc)
 {
        struct ib_mad_agent_private     *mad_agent_priv;
        unsigned long                   flags;
+       int                             ret;
 
-       mad_agent_priv = container_of(mad_send_wr->agent,
-                                     struct ib_mad_agent_private, agent);
-
+       mad_agent_priv = mad_send_wr->mad_agent_priv;
        spin_lock_irqsave(&mad_agent_priv->lock, flags);
+       if (mad_agent_priv->agent.rmpp_version) {
+               ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
+               if (ret == IB_RMPP_RESULT_CONSUMED)
+                       goto done;
+       } else
+               ret = IB_RMPP_RESULT_UNHANDLED;
+
        if (mad_send_wc->status != IB_WC_SUCCESS &&
            mad_send_wr->status == IB_WC_SUCCESS) {
                mad_send_wr->status = mad_send_wc->status;
@@ -1762,10 +2006,9 @@ static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
        if (--mad_send_wr->refcount > 0) {
                if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
                    mad_send_wr->status == IB_WC_SUCCESS) {
-                       wait_for_response(mad_agent_priv, mad_send_wr);
+                       wait_for_response(mad_send_wr);
                }
-               spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
-               return;
+               goto done;
        }
 
        /* Remove send from MAD agent and notify client of completion */
@@ -1775,14 +2018,17 @@ static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
 
        if (mad_send_wr->status != IB_WC_SUCCESS )
                mad_send_wc->status = mad_send_wr->status;
-       mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
-                                           mad_send_wc);
+       if (ret == IB_RMPP_RESULT_INTERNAL)
+               ib_rmpp_send_handler(mad_send_wc);
+       else
+               mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
+                                                  mad_send_wc);
 
        /* Release reference on agent taken when sending */
-       if (atomic_dec_and_test(&mad_agent_priv->refcount))
-               wake_up(&mad_agent_priv->wait);
-
-       kfree(mad_send_wr);
+       deref_mad_agent(mad_agent_priv);
+       return;
+done:
+       spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
 }
 
 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
@@ -1793,6 +2039,7 @@ static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
        struct ib_mad_qp_info           *qp_info;
        struct ib_mad_queue             *send_queue;
        struct ib_send_wr               *bad_send_wr;
+       struct ib_mad_send_wc           mad_send_wc;
        unsigned long flags;
        int ret;
 
@@ -1803,6 +2050,12 @@ static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
        qp_info = send_queue->qp_info;
 
 retry:
+       dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
+                        pci_unmap_addr(mad_send_wr, header_mapping),
+                        mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
+       dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
+                        pci_unmap_addr(mad_send_wr, payload_mapping),
+                        mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
        queued_send_wr = NULL;
        spin_lock_irqsave(&send_queue->lock, flags);
        list_del(&mad_list->list);
@@ -1819,17 +2072,17 @@ retry:
        }
        spin_unlock_irqrestore(&send_queue->lock, flags);
 
-       /* Restore client wr_id in WC and complete send */
-       wc->wr_id = mad_send_wr->wr_id;
+       mad_send_wc.send_buf = &mad_send_wr->send_buf;
+       mad_send_wc.status = wc->status;
+       mad_send_wc.vendor_err = wc->vendor_err;
        if (atomic_read(&qp_info->snoop_count))
-               snoop_send(qp_info, &mad_send_wr->send_wr,
-                          (struct ib_mad_send_wc *)wc,
+               snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
                           IB_MAD_SNOOP_SEND_COMPLETIONS);
-       ib_mad_complete_send_wr(mad_send_wr, (struct ib_mad_send_wc *)wc);
+       ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
 
        if (queued_send_wr) {
                ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
-                               &bad_send_wr);
+                                  &bad_send_wr);
                if (ret) {
                        printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
                        mad_send_wr = queued_send_wr;
@@ -1961,6 +2214,8 @@ static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
 
        /* Empty wait list to prevent receives from finding a request */
        list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
+       /* Empty local completion list as well */
+       list_splice_init(&mad_agent_priv->local_list, &cancel_list);
        spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
 
        /* Report all cancelled requests */
@@ -1969,103 +2224,73 @@ static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
 
        list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
                                 &cancel_list, agent_list) {
-               mad_send_wc.wr_id = mad_send_wr->wr_id;
+               mad_send_wc.send_buf = &mad_send_wr->send_buf;
+               list_del(&mad_send_wr->agent_list);
                mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
                                                   &mad_send_wc);
-
-               list_del(&mad_send_wr->agent_list);
-               kfree(mad_send_wr);
                atomic_dec(&mad_agent_priv->refcount);
        }
 }
 
 static struct ib_mad_send_wr_private*
-find_send_by_wr_id(struct ib_mad_agent_private *mad_agent_priv,
-                  u64 wr_id)
+find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
+            struct ib_mad_send_buf *send_buf)
 {
        struct ib_mad_send_wr_private *mad_send_wr;
 
        list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
                            agent_list) {
-               if (mad_send_wr->wr_id == wr_id)
+               if (&mad_send_wr->send_buf == send_buf)
                        return mad_send_wr;
        }
 
        list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
                            agent_list) {
-               if (mad_send_wr->wr_id == wr_id)
+               if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
+                   &mad_send_wr->send_buf == send_buf)
                        return mad_send_wr;
        }
        return NULL;
 }
 
-void cancel_sends(void *data)
-{
-       struct ib_mad_agent_private *mad_agent_priv;
-       struct ib_mad_send_wr_private *mad_send_wr;
-       struct ib_mad_send_wc mad_send_wc;
-       unsigned long flags;
-
-       mad_agent_priv = data;
-
-       mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
-       mad_send_wc.vendor_err = 0;
-
-       spin_lock_irqsave(&mad_agent_priv->lock, flags);
-       while (!list_empty(&mad_agent_priv->canceled_list)) {
-               mad_send_wr = list_entry(mad_agent_priv->canceled_list.next,
-                                        struct ib_mad_send_wr_private,
-                                        agent_list);
-
-               list_del(&mad_send_wr->agent_list);
-               spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
-
-               mad_send_wc.wr_id = mad_send_wr->wr_id;
-               mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
-                                                  &mad_send_wc);
-
-               kfree(mad_send_wr);
-               if (atomic_dec_and_test(&mad_agent_priv->refcount))
-                       wake_up(&mad_agent_priv->wait);
-               spin_lock_irqsave(&mad_agent_priv->lock, flags);
-       }
-       spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
-}
-
-void ib_cancel_mad(struct ib_mad_agent *mad_agent,
-                 u64 wr_id)
+int ib_modify_mad(struct ib_mad_agent *mad_agent,
+                 struct ib_mad_send_buf *send_buf, u32 timeout_ms)
 {
        struct ib_mad_agent_private *mad_agent_priv;
        struct ib_mad_send_wr_private *mad_send_wr;
        unsigned long flags;
+       int active;
 
        mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
                                      agent);
        spin_lock_irqsave(&mad_agent_priv->lock, flags);
-       mad_send_wr = find_send_by_wr_id(mad_agent_priv, wr_id);
-       if (!mad_send_wr) {
+       mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
+       if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
                spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
-               goto out;
+               return -EINVAL;
        }
 
-       if (mad_send_wr->status == IB_WC_SUCCESS)
-               mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
-
-       if (mad_send_wr->refcount != 0) {
+       active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
+       if (!timeout_ms) {
                mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
-               spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
-               goto out;
+               mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
        }
 
-       list_del(&mad_send_wr->agent_list);
-       list_add_tail(&mad_send_wr->agent_list, &mad_agent_priv->canceled_list);
-       adjust_timeout(mad_agent_priv);
+       mad_send_wr->send_buf.timeout_ms = timeout_ms;
+       if (active)
+               mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
+       else
+               ib_reset_mad_timeout(mad_send_wr, timeout_ms);
+
        spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
+       return 0;
+}
+EXPORT_SYMBOL(ib_modify_mad);
 
-       queue_work(mad_agent_priv->qp_info->port_priv->wq,
-                  &mad_agent_priv->canceled_work);
-out:
-       return;
+void ib_cancel_mad(struct ib_mad_agent *mad_agent,
+                  struct ib_mad_send_buf *send_buf)
+{
+       ib_modify_mad(mad_agent, send_buf, 0);
 }
 EXPORT_SYMBOL(ib_cancel_mad);
 
@@ -2075,6 +2300,7 @@ static void local_completions(void *data)
        struct ib_mad_local_private *local;
        struct ib_mad_agent_private *recv_mad_agent;
        unsigned long flags;
+       int recv = 0;
        struct ib_wc wc;
        struct ib_mad_send_wc mad_send_wc;
 
@@ -2085,27 +2311,30 @@ static void local_completions(void *data)
                local = list_entry(mad_agent_priv->local_list.next,
                                   struct ib_mad_local_private,
                                   completion_list);
+               list_del(&local->completion_list);
                spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
                if (local->mad_priv) {
                        recv_mad_agent = local->recv_mad_agent;
                        if (!recv_mad_agent) {
                                printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
-                               kmem_cache_free(ib_mad_cache, local->mad_priv);
                                goto local_send_completion;
                        }
 
+                       recv = 1;
                        /*
                         * Defined behavior is to complete response
                         * before request
                         */
-                       build_smp_wc(local->wr_id, IB_LID_PERMISSIVE,
-                                    0 /* pkey index */,
-                                    recv_mad_agent->agent.port_num, &wc);
+                       build_smp_wc((unsigned long) local->mad_send_wr,
+                                    be16_to_cpu(IB_LID_PERMISSIVE),
+                                    0, recv_mad_agent->agent.port_num, &wc);
 
                        local->mad_priv->header.recv_wc.wc = &wc;
                        local->mad_priv->header.recv_wc.mad_len =
                                                sizeof(struct ib_mad);
-                       INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.recv_buf.list);
+                       INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
+                       list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
+                                &local->mad_priv->header.recv_wc.rmpp_list);
                        local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
                        local->mad_priv->header.recv_wc.recv_buf.mad =
                                                &local->mad_priv->mad.mad;
@@ -2125,22 +2354,56 @@ local_send_completion:
                /* Complete send */
                mad_send_wc.status = IB_WC_SUCCESS;
                mad_send_wc.vendor_err = 0;
-               mad_send_wc.wr_id = local->wr_id;
+               mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
                if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
-                       snoop_send(mad_agent_priv->qp_info, &local->send_wr,
-                                 &mad_send_wc,
-                                  IB_MAD_SNOOP_SEND_COMPLETIONS);
+                       snoop_send(mad_agent_priv->qp_info,
+                                  &local->mad_send_wr->send_buf,
+                                  &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
                mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
                                                   &mad_send_wc);
 
                spin_lock_irqsave(&mad_agent_priv->lock, flags);
-               list_del(&local->completion_list);
                atomic_dec(&mad_agent_priv->refcount);
+               if (!recv)
+                       kmem_cache_free(ib_mad_cache, local->mad_priv);
                kfree(local);
        }
        spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
 }
 
+static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
+{
+       int ret;
+
+       if (!mad_send_wr->retries--)
+               return -ETIMEDOUT;
+
+       mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
+
+       if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
+               ret = ib_retry_rmpp(mad_send_wr);
+               switch (ret) {
+               case IB_RMPP_RESULT_UNHANDLED:
+                       ret = ib_send_mad(mad_send_wr);
+                       break;
+               case IB_RMPP_RESULT_CONSUMED:
+                       ret = 0;
+                       break;
+               default:
+                       ret = -ECOMM;
+                       break;
+               }
+       } else
+               ret = ib_send_mad(mad_send_wr);
+
+       if (!ret) {
+               mad_send_wr->refcount++;
+               list_add_tail(&mad_send_wr->agent_list,
+                             &mad_send_wr->mad_agent_priv->send_list);
+       }
+       return ret;
+}
+
 static void timeout_sends(void *data)
 {
        struct ib_mad_agent_private *mad_agent_priv;
@@ -2149,8 +2412,6 @@ static void timeout_sends(void *data)
        unsigned long flags, delay;
 
        mad_agent_priv = (struct ib_mad_agent_private *)data;
-
-       mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
        mad_send_wc.vendor_err = 0;
 
        spin_lock_irqsave(&mad_agent_priv->lock, flags);
@@ -2170,24 +2431,35 @@ static void timeout_sends(void *data)
                }
 
                list_del(&mad_send_wr->agent_list);
+               if (mad_send_wr->status == IB_WC_SUCCESS &&
+                   !retry_send(mad_send_wr))
+                       continue;
+
                spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
 
-               mad_send_wc.wr_id = mad_send_wr->wr_id;
+               if (mad_send_wr->status == IB_WC_SUCCESS)
+                       mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
+               else
+                       mad_send_wc.status = mad_send_wr->status;
+               mad_send_wc.send_buf = &mad_send_wr->send_buf;
                mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
                                                   &mad_send_wc);
 
-               kfree(mad_send_wr);
                atomic_dec(&mad_agent_priv->refcount);
                spin_lock_irqsave(&mad_agent_priv->lock, flags);
        }
        spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
 }
 
-static void ib_mad_thread_completion_handler(struct ib_cq *cq)
+static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
 {
        struct ib_mad_port_private *port_priv = cq->cq_context;
+       unsigned long flags;
 
-       queue_work(port_priv->wq, &port_priv->work);
+       spin_lock_irqsave(&ib_mad_port_list_lock, flags);
+       if (!list_empty(&port_priv->port_list))
+               queue_work(port_priv->wq, &port_priv->work);
+       spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
 }
 
 /*
@@ -2226,11 +2498,11 @@ static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
                        }
                }
                sg_list.addr = dma_map_single(qp_info->port_priv->
-                                               device->dma_device,
-                                       &mad_priv->grh,
-                                       sizeof *mad_priv -
-                                               sizeof mad_priv->header,
-                                       DMA_FROM_DEVICE);
+                                               device->dma_device,
+                                             &mad_priv->grh,
+                                             sizeof *mad_priv -
+                                               sizeof mad_priv->header,
+                                             DMA_FROM_DEVICE);
                pci_unmap_addr_set(&mad_priv->header, mapping, sg_list.addr);
                recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
                mad_priv->header.mad_list.mad_queue = recv_queue;
@@ -2431,8 +2703,7 @@ error:
 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
 {
        ib_destroy_qp(qp_info->qp);
-       if (qp_info->snoop_table)
-               kfree(qp_info->snoop_table);
+       kfree(qp_info->snoop_table);
 }
 
 /*
@@ -2447,21 +2718,13 @@ static int ib_mad_port_open(struct ib_device *device,
        unsigned long flags;
        char name[sizeof "ib_mad123"];
 
-       /* First, check if port already open at MAD layer */
-       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);
-               return 0;
-       }
-
        /* Create new device info */
-       port_priv = kmalloc(sizeof *port_priv, GFP_KERNEL);
+       port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
        if (!port_priv) {
                printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
                return -ENOMEM;
        }
-       memset(port_priv, 0, sizeof *port_priv);
+
        port_priv->device = device;
        port_priv->port_num = port_num;
        spin_lock_init(&port_priv->reg_lock);
@@ -2471,8 +2734,7 @@ static int ib_mad_port_open(struct ib_device *device,
 
        cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
        port_priv->cq = ib_create_cq(port_priv->device,
-                                    (ib_comp_handler)
-                                       ib_mad_thread_completion_handler,
+                                    ib_mad_thread_completion_handler,
                                     NULL, port_priv, cq_size);
        if (IS_ERR(port_priv->cq)) {
                printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
@@ -2509,18 +2771,23 @@ static int ib_mad_port_open(struct ib_device *device,
        }
        INIT_WORK(&port_priv->work, ib_mad_completion_handler, port_priv);
 
+       spin_lock_irqsave(&ib_mad_port_list_lock, flags);
+       list_add_tail(&port_priv->port_list, &ib_mad_port_list);
+       spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
+
        ret = ib_mad_port_start(port_priv);
        if (ret) {
                printk(KERN_ERR PFX "Couldn't start port\n");
                goto error9;
        }
 
-       spin_lock_irqsave(&ib_mad_port_list_lock, flags);
-       list_add_tail(&port_priv->port_list, &ib_mad_port_list);
-       spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
        return 0;
 
 error9:
+       spin_lock_irqsave(&ib_mad_port_list_lock, flags);
+       list_del_init(&port_priv->port_list);
+       spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
+
        destroy_workqueue(port_priv->wq);
 error8:
        destroy_mad_qp(&port_priv->qp_info[1]);
@@ -2557,11 +2824,9 @@ static int ib_mad_port_close(struct ib_device *device, int port_num)
                printk(KERN_ERR PFX "Port %d not found\n", port_num);
                return -ENODEV;
        }
-       list_del(&port_priv->port_list);
+       list_del_init(&port_priv->port_list);
        spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
 
-       /* Stop processing completions. */
-       flush_workqueue(port_priv->wq);
        destroy_workqueue(port_priv->wq);
        destroy_mad_qp(&port_priv->qp_info[1]);
        destroy_mad_qp(&port_priv->qp_info[0]);
@@ -2579,57 +2844,54 @@ static int ib_mad_port_close(struct ib_device *device, int port_num)
 
 static void ib_mad_init_device(struct ib_device *device)
 {
-       int ret, num_ports, cur_port, i, ret2;
+       int start, end, i;
 
        if (device->node_type == IB_NODE_SWITCH) {
-               num_ports = 1;
-               cur_port = 0;
+               start = 0;
+               end   = 0;
        } else {
-               num_ports = device->phys_port_cnt;
-               cur_port = 1;
+               start = 1;
+               end   = device->phys_port_cnt;
        }
-       for (i = 0; i < num_ports; i++, cur_port++) {
-               ret = ib_mad_port_open(device, cur_port);
-               if (ret) {
+
+       for (i = start; i <= end; i++) {
+               if (ib_mad_port_open(device, i)) {
                        printk(KERN_ERR PFX "Couldn't open %s port %d\n",
-                              device->name, cur_port);
-                       goto error_device_open;
+                              device->name, i);
+                       goto error;
                }
-               ret = ib_agent_port_open(device, cur_port);
-               if (ret) {
+               if (ib_agent_port_open(device, i)) {
                        printk(KERN_ERR PFX "Couldn't open %s port %d "
                               "for agents\n",
-                              device->name, cur_port);
-                       goto error_device_open;
+                              device->name, i);
+                       goto error_agent;
                }
        }
+       return;
 
-       goto error_device_query;
+error_agent:
+       if (ib_mad_port_close(device, i))
+               printk(KERN_ERR PFX "Couldn't close %s port %d\n",
+                      device->name, i);
 
-error_device_open:
-       while (i > 0) {
-               cur_port--;
-               ret2 = ib_agent_port_close(device, cur_port);
-               if (ret2) {
+error:
+       i--;
+
+       while (i >= start) {
+               if (ib_agent_port_close(device, i))
                        printk(KERN_ERR PFX "Couldn't close %s port %d "
                               "for agents\n",
-                              device->name, cur_port);
-               }
-               ret2 = ib_mad_port_close(device, cur_port);
-               if (ret2) {
+                              device->name, i);
+               if (ib_mad_port_close(device, i))
                        printk(KERN_ERR PFX "Couldn't close %s port %d\n",
-                              device->name, cur_port);
-               }
+                              device->name, i);
                i--;
        }
-
-error_device_query:
-       return;
 }
 
 static void ib_mad_remove_device(struct ib_device *device)
 {
-       int ret = 0, i, num_ports, cur_port, ret2;
+       int i, num_ports, cur_port;
 
        if (device->node_type == IB_NODE_SWITCH) {
                num_ports = 1;
@@ -2639,21 +2901,13 @@ static void ib_mad_remove_device(struct ib_device *device)
                cur_port = 1;
        }
        for (i = 0; i < num_ports; i++, cur_port++) {
-               ret2 = ib_agent_port_close(device, cur_port);
-               if (ret2) {
+               if (ib_agent_port_close(device, cur_port))
                        printk(KERN_ERR PFX "Couldn't close %s port %d "
                               "for agents\n",
                               device->name, cur_port);
-                       if (!ret)
-                               ret = ret2;
-               }
-               ret2 = ib_mad_port_close(device, cur_port);
-               if (ret2) {
+               if (ib_mad_port_close(device, cur_port))
                        printk(KERN_ERR PFX "Couldn't close %s port %d\n",
                               device->name, cur_port);
-                       if (!ret)
-                               ret = ret2;
-               }
        }
 }
 
@@ -2668,7 +2922,6 @@ static int __init ib_mad_init_module(void)
        int ret;
 
        spin_lock_init(&ib_mad_port_list_lock);
-       spin_lock_init(&ib_agent_port_list_lock);
 
        ib_mad_cache = kmem_cache_create("ib_mad",
                                         sizeof(struct ib_mad_private),
@@ -2709,3 +2962,4 @@ static void __exit ib_mad_cleanup_module(void)
 
 module_init(ib_mad_init_module);
 module_exit(ib_mad_cleanup_module);
+