IB: convert struct class_device to struct device
[safe/jmp/linux-2.6] / drivers / infiniband / hw / mthca / mthca_provider.c
index e2db5e0..696e1f3 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
- * Copyright (c) 2005 Cisco Systems. All rights reserved.
+ * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  *
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  *
- * $Id: mthca_provider.c 1397 2004-12-28 05:09:00Z roland $
+ * $Id: mthca_provider.c 4859 2006-01-09 21:55:10Z roland $
  */
 
-#include <ib_smi.h>
+#include <rdma/ib_smi.h>
+#include <rdma/ib_umem.h>
+#include <rdma/ib_user_verbs.h>
 #include <linux/mm.h>
 
 #include "mthca_dev.h"
 #include "mthca_user.h"
 #include "mthca_memfree.h"
 
+static void init_query_mad(struct ib_smp *mad)
+{
+       mad->base_version  = 1;
+       mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
+       mad->class_version = 1;
+       mad->method        = IB_MGMT_METHOD_GET;
+}
+
 static int mthca_query_device(struct ib_device *ibdev,
                              struct ib_device_attr *props)
 {
        struct ib_smp *in_mad  = NULL;
        struct ib_smp *out_mad = NULL;
        int err = -ENOMEM;
-       struct mthca_devmdev = to_mdev(ibdev);
+       struct mthca_dev *mdev = to_mdev(ibdev);
 
        u8 status;
 
-       in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
+       in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
        out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
        if (!in_mad || !out_mad)
                goto out;
@@ -63,12 +73,8 @@ static int mthca_query_device(struct ib_device *ibdev,
 
        props->fw_ver              = mdev->fw_ver;
 
-       memset(in_mad, 0, sizeof *in_mad);
-       in_mad->base_version       = 1;
-       in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
-       in_mad->class_version      = 1;
-       in_mad->method             = IB_MGMT_METHOD_GET;
-       in_mad->attr_id            = IB_SMP_ATTR_NODE_INFO;
+       init_query_mad(in_mad);
+       in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
 
        err = mthca_MAD_IFC(mdev, 1, 1,
                            1, NULL, NULL, in_mad, out_mad,
@@ -84,21 +90,42 @@ static int mthca_query_device(struct ib_device *ibdev,
        props->vendor_id           = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
                0xffffff;
        props->vendor_part_id      = be16_to_cpup((__be16 *) (out_mad->data + 30));
-       props->hw_ver              = be16_to_cpup((__be16 *) (out_mad->data + 32));
+       props->hw_ver              = be32_to_cpup((__be32 *) (out_mad->data + 32));
        memcpy(&props->sys_image_guid, out_mad->data +  4, 8);
-       memcpy(&props->node_guid,      out_mad->data + 12, 8);
 
        props->max_mr_size         = ~0ull;
+       props->page_size_cap       = mdev->limits.page_size_cap;
        props->max_qp              = mdev->limits.num_qps - mdev->limits.reserved_qps;
-       props->max_qp_wr           = 0xffff;
+       props->max_qp_wr           = mdev->limits.max_wqes;
        props->max_sge             = mdev->limits.max_sg;
        props->max_cq              = mdev->limits.num_cqs - mdev->limits.reserved_cqs;
-       props->max_cqe             = 0xffff;
+       props->max_cqe             = mdev->limits.max_cqes;
        props->max_mr              = mdev->limits.num_mpts - mdev->limits.reserved_mrws;
        props->max_pd              = mdev->limits.num_pds - mdev->limits.reserved_pds;
        props->max_qp_rd_atom      = 1 << mdev->qp_table.rdb_shift;
-       props->max_qp_init_rd_atom = 1 << mdev->qp_table.rdb_shift;
+       props->max_qp_init_rd_atom = mdev->limits.max_qp_init_rdma;
+       props->max_res_rd_atom     = props->max_qp_rd_atom * props->max_qp;
+       props->max_srq             = mdev->limits.num_srqs - mdev->limits.reserved_srqs;
+       props->max_srq_wr          = mdev->limits.max_srq_wqes;
+       props->max_srq_sge         = mdev->limits.max_srq_sge;
        props->local_ca_ack_delay  = mdev->limits.local_ca_ack_delay;
+       props->atomic_cap          = mdev->limits.flags & DEV_LIM_FLAG_ATOMIC ?
+                                       IB_ATOMIC_HCA : IB_ATOMIC_NONE;
+       props->max_pkeys           = mdev->limits.pkey_table_len;
+       props->max_mcast_grp       = mdev->limits.num_mgms + mdev->limits.num_amgms;
+       props->max_mcast_qp_attach = MTHCA_QP_PER_MGM;
+       props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
+                                          props->max_mcast_grp;
+       /*
+        * If Sinai memory key optimization is being used, then only
+        * the 8-bit key portion will change.  For other HCAs, the
+        * unused index bits will also be used for FMR remapping.
+        */
+       if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
+               props->max_map_per_fmr = 255;
+       else
+               props->max_map_per_fmr =
+                       (1 << (32 - ilog2(mdev->limits.num_mpts))) - 1;
 
        err = 0;
  out:
@@ -115,18 +142,16 @@ static int mthca_query_port(struct ib_device *ibdev,
        int err = -ENOMEM;
        u8 status;
 
-       in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
+       in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
        out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
        if (!in_mad || !out_mad)
                goto out;
 
-       memset(in_mad, 0, sizeof *in_mad);
-       in_mad->base_version       = 1;
-       in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
-       in_mad->class_version      = 1;
-       in_mad->method             = IB_MGMT_METHOD_GET;
-       in_mad->attr_id            = IB_SMP_ATTR_PORT_INFO;
-       in_mad->attr_mod           = cpu_to_be32(port);
+       memset(props, 0, sizeof *props);
+
+       init_query_mad(in_mad);
+       in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
+       in_mad->attr_mod = cpu_to_be32(port);
 
        err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
                            port, NULL, NULL, in_mad, out_mad,
@@ -146,10 +171,17 @@ static int mthca_query_port(struct ib_device *ibdev,
        props->phys_state        = out_mad->data[33] >> 4;
        props->port_cap_flags    = be32_to_cpup((__be32 *) (out_mad->data + 20));
        props->gid_tbl_len       = to_mdev(ibdev)->limits.gid_table_len;
+       props->max_msg_sz        = 0x80000000;
        props->pkey_tbl_len      = to_mdev(ibdev)->limits.pkey_table_len;
+       props->bad_pkey_cntr     = be16_to_cpup((__be16 *) (out_mad->data + 46));
        props->qkey_viol_cntr    = be16_to_cpup((__be16 *) (out_mad->data + 48));
        props->active_width      = out_mad->data[31] & 0xf;
        props->active_speed      = out_mad->data[35] >> 4;
+       props->max_mtu           = out_mad->data[41] & 0xf;
+       props->active_mtu        = out_mad->data[36] >> 4;
+       props->subnet_timeout    = out_mad->data[51] & 0x1f;
+       props->max_vl_num        = out_mad->data[37] >> 4;
+       props->init_type_reply   = out_mad->data[41] >> 4;
 
  out:
        kfree(in_mad);
@@ -157,6 +189,23 @@ static int mthca_query_port(struct ib_device *ibdev,
        return err;
 }
 
+static int mthca_modify_device(struct ib_device *ibdev,
+                              int mask,
+                              struct ib_device_modify *props)
+{
+       if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
+               return -EOPNOTSUPP;
+
+       if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
+               if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
+                       return -ERESTARTSYS;
+               memcpy(ibdev->node_desc, props->node_desc, 64);
+               mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
+       }
+
+       return 0;
+}
+
 static int mthca_modify_port(struct ib_device *ibdev,
                             u8 port, int port_modify_mask,
                             struct ib_port_modify *props)
@@ -166,7 +215,7 @@ static int mthca_modify_port(struct ib_device *ibdev,
        int err;
        u8 status;
 
-       if (down_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
+       if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
                return -ERESTARTSYS;
 
        err = mthca_query_port(ibdev, port, &attr);
@@ -188,7 +237,7 @@ static int mthca_modify_port(struct ib_device *ibdev,
        }
 
 out:
-       up(&to_mdev(ibdev)->cap_mask_mutex);
+       mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
        return err;
 }
 
@@ -200,18 +249,14 @@ static int mthca_query_pkey(struct ib_device *ibdev,
        int err = -ENOMEM;
        u8 status;
 
-       in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
+       in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
        out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
        if (!in_mad || !out_mad)
                goto out;
 
-       memset(in_mad, 0, sizeof *in_mad);
-       in_mad->base_version       = 1;
-       in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
-       in_mad->class_version      = 1;
-       in_mad->method             = IB_MGMT_METHOD_GET;
-       in_mad->attr_id            = IB_SMP_ATTR_PKEY_TABLE;
-       in_mad->attr_mod           = cpu_to_be32(index / 32);
+       init_query_mad(in_mad);
+       in_mad->attr_id  = IB_SMP_ATTR_PKEY_TABLE;
+       in_mad->attr_mod = cpu_to_be32(index / 32);
 
        err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
                            port, NULL, NULL, in_mad, out_mad,
@@ -239,18 +284,14 @@ static int mthca_query_gid(struct ib_device *ibdev, u8 port,
        int err = -ENOMEM;
        u8 status;
 
-       in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
+       in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
        out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
        if (!in_mad || !out_mad)
                goto out;
 
-       memset(in_mad, 0, sizeof *in_mad);
-       in_mad->base_version       = 1;
-       in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
-       in_mad->class_version      = 1;
-       in_mad->method             = IB_MGMT_METHOD_GET;
-       in_mad->attr_id            = IB_SMP_ATTR_PORT_INFO;
-       in_mad->attr_mod           = cpu_to_be32(port);
+       init_query_mad(in_mad);
+       in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
+       in_mad->attr_mod = cpu_to_be32(port);
 
        err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
                            port, NULL, NULL, in_mad, out_mad,
@@ -264,13 +305,9 @@ static int mthca_query_gid(struct ib_device *ibdev, u8 port,
 
        memcpy(gid->raw, out_mad->data + 8, 8);
 
-       memset(in_mad, 0, sizeof *in_mad);
-       in_mad->base_version       = 1;
-       in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
-       in_mad->class_version      = 1;
-       in_mad->method             = IB_MGMT_METHOD_GET;
-       in_mad->attr_id            = IB_SMP_ATTR_GUID_INFO;
-       in_mad->attr_mod           = cpu_to_be32(index / 8);
+       init_query_mad(in_mad);
+       in_mad->attr_id  = IB_SMP_ATTR_GUID_INFO;
+       in_mad->attr_mod = cpu_to_be32(index / 8);
 
        err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
                            port, NULL, NULL, in_mad, out_mad,
@@ -282,7 +319,7 @@ static int mthca_query_gid(struct ib_device *ibdev, u8 port,
                goto out;
        }
 
-       memcpy(gid->raw + 8, out_mad->data + (index % 8) * 16, 8);
+       memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
 
  out:
        kfree(in_mad);
@@ -422,6 +459,79 @@ static int mthca_ah_destroy(struct ib_ah *ah)
        return 0;
 }
 
+static struct ib_srq *mthca_create_srq(struct ib_pd *pd,
+                                      struct ib_srq_init_attr *init_attr,
+                                      struct ib_udata *udata)
+{
+       struct mthca_create_srq ucmd;
+       struct mthca_ucontext *context = NULL;
+       struct mthca_srq *srq;
+       int err;
+
+       srq = kmalloc(sizeof *srq, GFP_KERNEL);
+       if (!srq)
+               return ERR_PTR(-ENOMEM);
+
+       if (pd->uobject) {
+               context = to_mucontext(pd->uobject->context);
+
+               if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
+                       err = -EFAULT;
+                       goto err_free;
+               }
+
+               err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
+                                       context->db_tab, ucmd.db_index,
+                                       ucmd.db_page);
+
+               if (err)
+                       goto err_free;
+
+               srq->mr.ibmr.lkey = ucmd.lkey;
+               srq->db_index     = ucmd.db_index;
+       }
+
+       err = mthca_alloc_srq(to_mdev(pd->device), to_mpd(pd),
+                             &init_attr->attr, srq);
+
+       if (err && pd->uobject)
+               mthca_unmap_user_db(to_mdev(pd->device), &context->uar,
+                                   context->db_tab, ucmd.db_index);
+
+       if (err)
+               goto err_free;
+
+       if (context && ib_copy_to_udata(udata, &srq->srqn, sizeof (__u32))) {
+               mthca_free_srq(to_mdev(pd->device), srq);
+               err = -EFAULT;
+               goto err_free;
+       }
+
+       return &srq->ibsrq;
+
+err_free:
+       kfree(srq);
+
+       return ERR_PTR(err);
+}
+
+static int mthca_destroy_srq(struct ib_srq *srq)
+{
+       struct mthca_ucontext *context;
+
+       if (srq->uobject) {
+               context = to_mucontext(srq->uobject->context);
+
+               mthca_unmap_user_db(to_mdev(srq->device), &context->uar,
+                                   context->db_tab, to_msrq(srq)->db_index);
+       }
+
+       mthca_free_srq(to_mdev(srq->device), to_msrq(srq));
+       kfree(srq);
+
+       return 0;
+}
+
 static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
                                     struct ib_qp_init_attr *init_attr,
                                     struct ib_udata *udata)
@@ -430,6 +540,9 @@ static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
        struct mthca_qp *qp;
        int err;
 
+       if (init_attr->create_flags)
+               return ERR_PTR(-EINVAL);
+
        switch (init_attr->qp_type) {
        case IB_QPT_RC:
        case IB_QPT_UC:
@@ -444,8 +557,10 @@ static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
                if (pd->uobject) {
                        context = to_mucontext(pd->uobject->context);
 
-                       if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
+                       if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
+                               kfree(qp);
                                return ERR_PTR(-EFAULT);
+                       }
 
                        err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
                                                context->db_tab,
@@ -525,11 +640,11 @@ static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
                return ERR_PTR(err);
        }
 
-       init_attr->cap.max_inline_data = 0;
        init_attr->cap.max_send_wr     = qp->sq.max;
        init_attr->cap.max_recv_wr     = qp->rq.max;
        init_attr->cap.max_send_sge    = qp->sq.max_gs;
        init_attr->cap.max_recv_sge    = qp->rq.max_gs;
+       init_attr->cap.max_inline_data = qp->max_inline_data;
 
        return &qp->ibqp;
 }
@@ -552,6 +667,7 @@ static int mthca_destroy_qp(struct ib_qp *qp)
 }
 
 static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
+                                    int comp_vector,
                                     struct ib_ucontext *context,
                                     struct ib_udata *udata)
 {
@@ -560,6 +676,9 @@ static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
        int nent;
        int err;
 
+       if (entries < 1 || entries > to_mdev(ibdev)->limits.max_cqes)
+               return ERR_PTR(-EINVAL);
+
        if (context) {
                if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
                        return ERR_PTR(-EFAULT);
@@ -584,9 +703,9 @@ static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
        }
 
        if (context) {
-               cq->mr.ibmr.lkey    = ucmd.lkey;
-               cq->set_ci_db_index = ucmd.set_db_index;
-               cq->arm_db_index    = ucmd.arm_db_index;
+               cq->buf.mr.ibmr.lkey = ucmd.lkey;
+               cq->set_ci_db_index  = ucmd.set_db_index;
+               cq->arm_db_index     = ucmd.arm_db_index;
        }
 
        for (nent = 1; nent <= entries; nent <<= 1)
@@ -604,6 +723,8 @@ static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
                goto err_free;
        }
 
+       cq->resize_buf = NULL;
+
        return &cq->ibcq;
 
 err_free:
@@ -622,6 +743,130 @@ err_unmap_set:
        return ERR_PTR(err);
 }
 
+static int mthca_alloc_resize_buf(struct mthca_dev *dev, struct mthca_cq *cq,
+                                 int entries)
+{
+       int ret;
+
+       spin_lock_irq(&cq->lock);
+       if (cq->resize_buf) {
+               ret = -EBUSY;
+               goto unlock;
+       }
+
+       cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
+       if (!cq->resize_buf) {
+               ret = -ENOMEM;
+               goto unlock;
+       }
+
+       cq->resize_buf->state = CQ_RESIZE_ALLOC;
+
+       ret = 0;
+
+unlock:
+       spin_unlock_irq(&cq->lock);
+
+       if (ret)
+               return ret;
+
+       ret = mthca_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
+       if (ret) {
+               spin_lock_irq(&cq->lock);
+               kfree(cq->resize_buf);
+               cq->resize_buf = NULL;
+               spin_unlock_irq(&cq->lock);
+               return ret;
+       }
+
+       cq->resize_buf->cqe = entries - 1;
+
+       spin_lock_irq(&cq->lock);
+       cq->resize_buf->state = CQ_RESIZE_READY;
+       spin_unlock_irq(&cq->lock);
+
+       return 0;
+}
+
+static int mthca_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
+{
+       struct mthca_dev *dev = to_mdev(ibcq->device);
+       struct mthca_cq *cq = to_mcq(ibcq);
+       struct mthca_resize_cq ucmd;
+       u32 lkey;
+       u8 status;
+       int ret;
+
+       if (entries < 1 || entries > dev->limits.max_cqes)
+               return -EINVAL;
+
+       mutex_lock(&cq->mutex);
+
+       entries = roundup_pow_of_two(entries + 1);
+       if (entries == ibcq->cqe + 1) {
+               ret = 0;
+               goto out;
+       }
+
+       if (cq->is_kernel) {
+               ret = mthca_alloc_resize_buf(dev, cq, entries);
+               if (ret)
+                       goto out;
+               lkey = cq->resize_buf->buf.mr.ibmr.lkey;
+       } else {
+               if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
+                       ret = -EFAULT;
+                       goto out;
+               }
+               lkey = ucmd.lkey;
+       }
+
+       ret = mthca_RESIZE_CQ(dev, cq->cqn, lkey, ilog2(entries), &status);
+       if (status)
+               ret = -EINVAL;
+
+       if (ret) {
+               if (cq->resize_buf) {
+                       mthca_free_cq_buf(dev, &cq->resize_buf->buf,
+                                         cq->resize_buf->cqe);
+                       kfree(cq->resize_buf);
+                       spin_lock_irq(&cq->lock);
+                       cq->resize_buf = NULL;
+                       spin_unlock_irq(&cq->lock);
+               }
+               goto out;
+       }
+
+       if (cq->is_kernel) {
+               struct mthca_cq_buf tbuf;
+               int tcqe;
+
+               spin_lock_irq(&cq->lock);
+               if (cq->resize_buf->state == CQ_RESIZE_READY) {
+                       mthca_cq_resize_copy_cqes(cq);
+                       tbuf         = cq->buf;
+                       tcqe         = cq->ibcq.cqe;
+                       cq->buf      = cq->resize_buf->buf;
+                       cq->ibcq.cqe = cq->resize_buf->cqe;
+               } else {
+                       tbuf = cq->resize_buf->buf;
+                       tcqe = cq->resize_buf->cqe;
+               }
+
+               kfree(cq->resize_buf);
+               cq->resize_buf = NULL;
+               spin_unlock_irq(&cq->lock);
+
+               mthca_free_cq_buf(dev, &tbuf, tcqe);
+       } else
+               ibcq->cqe = entries - 1;
+
+out:
+       mutex_unlock(&cq->mutex);
+
+       return ret;
+}
+
 static int mthca_destroy_cq(struct ib_cq *cq)
 {
        if (cq->uobject) {
@@ -667,6 +912,8 @@ static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
                return ERR_PTR(err);
        }
 
+       mr->umem = NULL;
+
        return &mr->ibmr;
 }
 
@@ -679,45 +926,27 @@ static struct ib_mr *mthca_reg_phys_mr(struct ib_pd       *pd,
        struct mthca_mr *mr;
        u64 *page_list;
        u64 total_size;
-       u64 mask;
+       unsigned long mask;
        int shift;
        int npages;
        int err;
        int i, j, n;
 
-       /* First check that we have enough alignment */
-       if ((*iova_start & ~PAGE_MASK) != (buffer_list[0].addr & ~PAGE_MASK))
-               return ERR_PTR(-EINVAL);
-
-       if (num_phys_buf > 1 &&
-           ((buffer_list[0].addr + buffer_list[0].size) & ~PAGE_MASK))
-               return ERR_PTR(-EINVAL);
-
-       mask = 0;
+       mask = buffer_list[0].addr ^ *iova_start;
        total_size = 0;
        for (i = 0; i < num_phys_buf; ++i) {
-               if (i != 0 && buffer_list[i].addr & ~PAGE_MASK)
-                       return ERR_PTR(-EINVAL);
-               if (i != 0 && i != num_phys_buf - 1 &&
-                   (buffer_list[i].size & ~PAGE_MASK))
-                       return ERR_PTR(-EINVAL);
+               if (i != 0)
+                       mask |= buffer_list[i].addr;
+               if (i != num_phys_buf - 1)
+                       mask |= buffer_list[i].addr + buffer_list[i].size;
 
                total_size += buffer_list[i].size;
-               if (i > 0)
-                       mask |= buffer_list[i].addr;
        }
 
-       /* Find largest page shift we can use to cover buffers */
-       for (shift = PAGE_SHIFT; shift < 31; ++shift)
-               if (num_phys_buf > 1) {
-                       if ((1ULL << shift) & mask)
-                               break;
-               } else {
-                       if (1ULL << shift >=
-                           buffer_list[0].size +
-                           (buffer_list[0].addr & ((1ULL << shift) - 1)))
-                               break;
-               }
+       if (mask & ~PAGE_MASK)
+               return ERR_PTR(-EINVAL);
+
+       shift = __ffs(mask | 1 << 31);
 
        buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
        buffer_list[0].addr &= ~0ull << shift;
@@ -766,11 +995,13 @@ static struct ib_mr *mthca_reg_phys_mr(struct ib_pd       *pd,
        }
 
        kfree(page_list);
+       mr->umem = NULL;
+
        return &mr->ibmr;
 }
 
-static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
-                                      int acc, struct ib_udata *udata)
+static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
+                                      u64 virt, int acc, struct ib_udata *udata)
 {
        struct mthca_dev *dev = to_mdev(pd->device);
        struct ib_umem_chunk *chunk;
@@ -779,21 +1010,28 @@ static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
        int shift, n, len;
        int i, j, k;
        int err = 0;
-
-       shift = ffs(region->page_size) - 1;
+       int write_mtt_size;
 
        mr = kmalloc(sizeof *mr, GFP_KERNEL);
        if (!mr)
                return ERR_PTR(-ENOMEM);
 
+       mr->umem = ib_umem_get(pd->uobject->context, start, length, acc);
+       if (IS_ERR(mr->umem)) {
+               err = PTR_ERR(mr->umem);
+               goto err;
+       }
+
+       shift = ffs(mr->umem->page_size) - 1;
+
        n = 0;
-       list_for_each_entry(chunk, &region->chunk_list, list)
+       list_for_each_entry(chunk, &mr->umem->chunk_list, list)
                n += chunk->nents;
 
        mr->mtt = mthca_alloc_mtt(dev, n);
        if (IS_ERR(mr->mtt)) {
                err = PTR_ERR(mr->mtt);
-               goto err;
+               goto err_umem;
        }
 
        pages = (u64 *) __get_free_page(GFP_KERNEL);
@@ -804,21 +1042,20 @@ static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
 
        i = n = 0;
 
-       list_for_each_entry(chunk, &region->chunk_list, list)
+       write_mtt_size = min(mthca_write_mtt_size(dev), (int) (PAGE_SIZE / sizeof *pages));
+
+       list_for_each_entry(chunk, &mr->umem->chunk_list, list)
                for (j = 0; j < chunk->nmap; ++j) {
                        len = sg_dma_len(&chunk->page_list[j]) >> shift;
                        for (k = 0; k < len; ++k) {
                                pages[i++] = sg_dma_address(&chunk->page_list[j]) +
-                                       region->page_size * k;
+                                       mr->umem->page_size * k;
                                /*
-                                * Be friendly to WRITE_MTT command
-                                * and leave two empty slots for the
-                                * index and reserved fields of the
-                                * mailbox.
+                                * Be friendly to write_mtt and pass it chunks
+                                * of appropriate size.
                                 */
-                               if (i == PAGE_SIZE / sizeof (u64) - 2) {
-                                       err = mthca_write_mtt(dev, mr->mtt,
-                                                             n, pages, i);
+                               if (i == write_mtt_size) {
+                                       err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
                                        if (err)
                                                goto mtt_done;
                                        n += i;
@@ -834,8 +1071,8 @@ mtt_done:
        if (err)
                goto err_mtt;
 
-       err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, region->virt_base,
-                            region->length, convert_access(acc), mr);
+       err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, virt, length,
+                            convert_access(acc), mr);
 
        if (err)
                goto err_mtt;
@@ -845,6 +1082,9 @@ mtt_done:
 err_mtt:
        mthca_free_mtt(dev, mr->mtt);
 
+err_umem:
+       ib_umem_release(mr->umem);
+
 err:
        kfree(mr);
        return ERR_PTR(err);
@@ -853,8 +1093,12 @@ err:
 static int mthca_dereg_mr(struct ib_mr *mr)
 {
        struct mthca_mr *mmr = to_mmr(mr);
+
        mthca_free_mr(to_mdev(mr->device), mmr);
+       if (mmr->umem)
+               ib_umem_release(mmr->umem);
        kfree(mmr);
+
        return 0;
 }
 
@@ -926,23 +1170,29 @@ static int mthca_unmap_fmr(struct list_head *fmr_list)
        return 0;
 }
 
-static ssize_t show_rev(struct class_device *cdev, char *buf)
+static ssize_t show_rev(struct device *device, struct device_attribute *attr,
+                       char *buf)
 {
-       struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
+       struct mthca_dev *dev =
+               container_of(device, struct mthca_dev, ib_dev.dev);
        return sprintf(buf, "%x\n", dev->rev_id);
 }
 
-static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
+static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
+                          char *buf)
 {
-       struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
-       return sprintf(buf, "%x.%x.%x\n", (int) (dev->fw_ver >> 32),
+       struct mthca_dev *dev =
+               container_of(device, struct mthca_dev, ib_dev.dev);
+       return sprintf(buf, "%d.%d.%d\n", (int) (dev->fw_ver >> 32),
                       (int) (dev->fw_ver >> 16) & 0xffff,
                       (int) dev->fw_ver & 0xffff);
 }
 
-static ssize_t show_hca(struct class_device *cdev, char *buf)
+static ssize_t show_hca(struct device *device, struct device_attribute *attr,
+                       char *buf)
 {
-       struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
+       struct mthca_dev *dev =
+               container_of(device, struct mthca_dev, ib_dev.dev);
        switch (dev->pdev->device) {
        case PCI_DEVICE_ID_MELLANOX_TAVOR:
                return sprintf(buf, "MT23108\n");
@@ -958,30 +1208,113 @@ static ssize_t show_hca(struct class_device *cdev, char *buf)
        }
 }
 
-static CLASS_DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
-static CLASS_DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
-static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
+static ssize_t show_board(struct device *device, struct device_attribute *attr,
+                         char *buf)
+{
+       struct mthca_dev *dev =
+               container_of(device, struct mthca_dev, ib_dev.dev);
+       return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
+}
+
+static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
+static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
+static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
+static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
 
-static struct class_device_attribute *mthca_class_attributes[] = {
-       &class_device_attr_hw_rev,
-       &class_device_attr_fw_ver,
-       &class_device_attr_hca_type
+static struct device_attribute *mthca_dev_attributes[] = {
+       &dev_attr_hw_rev,
+       &dev_attr_fw_ver,
+       &dev_attr_hca_type,
+       &dev_attr_board_id
 };
 
+static int mthca_init_node_data(struct mthca_dev *dev)
+{
+       struct ib_smp *in_mad  = NULL;
+       struct ib_smp *out_mad = NULL;
+       int err = -ENOMEM;
+       u8 status;
+
+       in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
+       out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
+       if (!in_mad || !out_mad)
+               goto out;
+
+       init_query_mad(in_mad);
+       in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
+
+       err = mthca_MAD_IFC(dev, 1, 1,
+                           1, NULL, NULL, in_mad, out_mad,
+                           &status);
+       if (err)
+               goto out;
+       if (status) {
+               err = -EINVAL;
+               goto out;
+       }
+
+       memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
+
+       in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
+
+       err = mthca_MAD_IFC(dev, 1, 1,
+                           1, NULL, NULL, in_mad, out_mad,
+                           &status);
+       if (err)
+               goto out;
+       if (status) {
+               err = -EINVAL;
+               goto out;
+       }
+
+       if (mthca_is_memfree(dev))
+               dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
+       memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
+
+out:
+       kfree(in_mad);
+       kfree(out_mad);
+       return err;
+}
+
 int mthca_register_device(struct mthca_dev *dev)
 {
        int ret;
        int i;
 
+       ret = mthca_init_node_data(dev);
+       if (ret)
+               return ret;
+
        strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
        dev->ib_dev.owner                = THIS_MODULE;
 
-       dev->ib_dev.node_type            = IB_NODE_CA;
+       dev->ib_dev.uverbs_abi_ver       = MTHCA_UVERBS_ABI_VERSION;
+       dev->ib_dev.uverbs_cmd_mask      =
+               (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
+               (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
+               (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
+               (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
+               (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
+               (1ull << IB_USER_VERBS_CMD_REG_MR)              |
+               (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
+               (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
+               (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
+               (1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
+               (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
+               (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
+               (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
+               (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
+               (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
+               (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
+               (1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
+       dev->ib_dev.node_type            = RDMA_NODE_IB_CA;
        dev->ib_dev.phys_port_cnt        = dev->limits.num_ports;
+       dev->ib_dev.num_comp_vectors     = 1;
        dev->ib_dev.dma_device           = &dev->pdev->dev;
-       dev->ib_dev.class_dev.dev        = &dev->pdev->dev;
        dev->ib_dev.query_device         = mthca_query_device;
        dev->ib_dev.query_port           = mthca_query_port;
+       dev->ib_dev.modify_device        = mthca_modify_device;
        dev->ib_dev.modify_port          = mthca_modify_port;
        dev->ib_dev.query_pkey           = mthca_query_pkey;
        dev->ib_dev.query_gid            = mthca_query_gid;
@@ -991,11 +1324,32 @@ int mthca_register_device(struct mthca_dev *dev)
        dev->ib_dev.alloc_pd             = mthca_alloc_pd;
        dev->ib_dev.dealloc_pd           = mthca_dealloc_pd;
        dev->ib_dev.create_ah            = mthca_ah_create;
+       dev->ib_dev.query_ah             = mthca_ah_query;
        dev->ib_dev.destroy_ah           = mthca_ah_destroy;
+
+       if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
+               dev->ib_dev.create_srq           = mthca_create_srq;
+               dev->ib_dev.modify_srq           = mthca_modify_srq;
+               dev->ib_dev.query_srq            = mthca_query_srq;
+               dev->ib_dev.destroy_srq          = mthca_destroy_srq;
+               dev->ib_dev.uverbs_cmd_mask     |=
+                       (1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
+                       (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
+                       (1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
+                       (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
+
+               if (mthca_is_memfree(dev))
+                       dev->ib_dev.post_srq_recv = mthca_arbel_post_srq_recv;
+               else
+                       dev->ib_dev.post_srq_recv = mthca_tavor_post_srq_recv;
+       }
+
        dev->ib_dev.create_qp            = mthca_create_qp;
        dev->ib_dev.modify_qp            = mthca_modify_qp;
+       dev->ib_dev.query_qp             = mthca_query_qp;
        dev->ib_dev.destroy_qp           = mthca_destroy_qp;
        dev->ib_dev.create_cq            = mthca_create_cq;
+       dev->ib_dev.resize_cq            = mthca_resize_cq;
        dev->ib_dev.destroy_cq           = mthca_destroy_cq;
        dev->ib_dev.poll_cq              = mthca_poll_cq;
        dev->ib_dev.get_dma_mr           = mthca_get_dma_mr;
@@ -1027,25 +1381,28 @@ int mthca_register_device(struct mthca_dev *dev)
                dev->ib_dev.post_recv     = mthca_tavor_post_receive;
        }
 
-       init_MUTEX(&dev->cap_mask_mutex);
+       mutex_init(&dev->cap_mask_mutex);
 
        ret = ib_register_device(&dev->ib_dev);
        if (ret)
                return ret;
 
-       for (i = 0; i < ARRAY_SIZE(mthca_class_attributes); ++i) {
-               ret = class_device_create_file(&dev->ib_dev.class_dev,
-                                              mthca_class_attributes[i]);
+       for (i = 0; i < ARRAY_SIZE(mthca_dev_attributes); ++i) {
+               ret = device_create_file(&dev->ib_dev.dev,
+                                        mthca_dev_attributes[i]);
                if (ret) {
                        ib_unregister_device(&dev->ib_dev);
                        return ret;
                }
        }
 
+       mthca_start_catas_poll(dev);
+
        return 0;
 }
 
 void mthca_unregister_device(struct mthca_dev *dev)
 {
+       mthca_stop_catas_poll(dev);
        ib_unregister_device(&dev->ib_dev);
 }