mlx5: Implement create_mr and destroy_mr
authorSagi Grimberg <sagig@mellanox.com>
Sun, 23 Feb 2014 12:19:06 +0000 (14:19 +0200)
committerRoland Dreier <roland@purestorage.com>
Fri, 7 Mar 2014 19:26:49 +0000 (11:26 -0800)
Support create_mr and destroy_mr verbs.  Creating ib_mr may be done
for either ib_mr that will register regular page lists like
alloc_fast_reg_mr routine, or indirect ib_mrs that can register other
(pre-registered) ib_mrs in an indirect manner.

In addition user may request signature enable, that will mean that the
created ib_mr may be attached with signature attributes (BSF, PSVs).

Currently we only allow direct/indirect registration modes.

Signed-off-by: Sagi Grimberg <sagig@mellanox.com>
Signed-off-by: Roland Dreier <roland@purestorage.com>
drivers/infiniband/hw/mlx5/main.c
drivers/infiniband/hw/mlx5/mlx5_ib.h
drivers/infiniband/hw/mlx5/mr.c
drivers/net/ethernet/mellanox/mlx5/core/mr.c
include/linux/mlx5/device.h
include/linux/mlx5/driver.h

index aa03e732b6a88d7cfa81a3d9ba4dee61d8f6c70d..7260a299c6dbb7acbd8d935b7b94fe936e75b0d0 100644 (file)
@@ -1423,9 +1423,11 @@ static int init_one(struct pci_dev *pdev,
        dev->ib_dev.get_dma_mr          = mlx5_ib_get_dma_mr;
        dev->ib_dev.reg_user_mr         = mlx5_ib_reg_user_mr;
        dev->ib_dev.dereg_mr            = mlx5_ib_dereg_mr;
+       dev->ib_dev.destroy_mr          = mlx5_ib_destroy_mr;
        dev->ib_dev.attach_mcast        = mlx5_ib_mcg_attach;
        dev->ib_dev.detach_mcast        = mlx5_ib_mcg_detach;
        dev->ib_dev.process_mad         = mlx5_ib_process_mad;
+       dev->ib_dev.create_mr           = mlx5_ib_create_mr;
        dev->ib_dev.alloc_fast_reg_mr   = mlx5_ib_alloc_fast_reg_mr;
        dev->ib_dev.alloc_fast_reg_page_list = mlx5_ib_alloc_fast_reg_page_list;
        dev->ib_dev.free_fast_reg_page_list  = mlx5_ib_free_fast_reg_page_list;
index 389e3196577323966faa7c9d4befdf6d42e2e075..79c4f14c4d3ef61f156e7e156324e89518132e80 100644 (file)
@@ -265,6 +265,7 @@ struct mlx5_ib_mr {
        enum ib_wc_status       status;
        struct mlx5_ib_dev     *dev;
        struct mlx5_create_mkey_mbox_out out;
+       struct mlx5_core_sig_ctx    *sig;
 };
 
 struct mlx5_ib_fast_reg_page_list {
@@ -495,6 +496,9 @@ struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
                                  u64 virt_addr, int access_flags,
                                  struct ib_udata *udata);
 int mlx5_ib_dereg_mr(struct ib_mr *ibmr);
+int mlx5_ib_destroy_mr(struct ib_mr *ibmr);
+struct ib_mr *mlx5_ib_create_mr(struct ib_pd *pd,
+                               struct ib_mr_init_attr *mr_init_attr);
 struct ib_mr *mlx5_ib_alloc_fast_reg_mr(struct ib_pd *pd,
                                        int max_page_list_len);
 struct ib_fast_reg_page_list *mlx5_ib_alloc_fast_reg_page_list(struct ib_device *ibdev,
index 7c95ca1f0c251c37294353f2ed6b0efca57b0e36..032445c47608b2969deb1f7d12fe88fcd0fcf37f 100644 (file)
@@ -992,6 +992,117 @@ int mlx5_ib_dereg_mr(struct ib_mr *ibmr)
        return 0;
 }
 
+struct ib_mr *mlx5_ib_create_mr(struct ib_pd *pd,
+                               struct ib_mr_init_attr *mr_init_attr)
+{
+       struct mlx5_ib_dev *dev = to_mdev(pd->device);
+       struct mlx5_create_mkey_mbox_in *in;
+       struct mlx5_ib_mr *mr;
+       int access_mode, err;
+       int ndescs = roundup(mr_init_attr->max_reg_descriptors, 4);
+
+       mr = kzalloc(sizeof(*mr), GFP_KERNEL);
+       if (!mr)
+               return ERR_PTR(-ENOMEM);
+
+       in = kzalloc(sizeof(*in), GFP_KERNEL);
+       if (!in) {
+               err = -ENOMEM;
+               goto err_free;
+       }
+
+       in->seg.status = 1 << 6; /* free */
+       in->seg.xlt_oct_size = cpu_to_be32(ndescs);
+       in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
+       in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
+       access_mode = MLX5_ACCESS_MODE_MTT;
+
+       if (mr_init_attr->flags & IB_MR_SIGNATURE_EN) {
+               u32 psv_index[2];
+
+               in->seg.flags_pd = cpu_to_be32(be32_to_cpu(in->seg.flags_pd) |
+                                                          MLX5_MKEY_BSF_EN);
+               in->seg.bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
+               mr->sig = kzalloc(sizeof(*mr->sig), GFP_KERNEL);
+               if (!mr->sig) {
+                       err = -ENOMEM;
+                       goto err_free_in;
+               }
+
+               /* create mem & wire PSVs */
+               err = mlx5_core_create_psv(&dev->mdev, to_mpd(pd)->pdn,
+                                          2, psv_index);
+               if (err)
+                       goto err_free_sig;
+
+               access_mode = MLX5_ACCESS_MODE_KLM;
+               mr->sig->psv_memory.psv_idx = psv_index[0];
+               mr->sig->psv_wire.psv_idx = psv_index[1];
+       }
+
+       in->seg.flags = MLX5_PERM_UMR_EN | access_mode;
+       err = mlx5_core_create_mkey(&dev->mdev, &mr->mmr, in, sizeof(*in),
+                                   NULL, NULL, NULL);
+       if (err)
+               goto err_destroy_psv;
+
+       mr->ibmr.lkey = mr->mmr.key;
+       mr->ibmr.rkey = mr->mmr.key;
+       mr->umem = NULL;
+       kfree(in);
+
+       return &mr->ibmr;
+
+err_destroy_psv:
+       if (mr->sig) {
+               if (mlx5_core_destroy_psv(&dev->mdev,
+                                         mr->sig->psv_memory.psv_idx))
+                       mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
+                                    mr->sig->psv_memory.psv_idx);
+               if (mlx5_core_destroy_psv(&dev->mdev,
+                                         mr->sig->psv_wire.psv_idx))
+                       mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
+                                    mr->sig->psv_wire.psv_idx);
+       }
+err_free_sig:
+       kfree(mr->sig);
+err_free_in:
+       kfree(in);
+err_free:
+       kfree(mr);
+       return ERR_PTR(err);
+}
+
+int mlx5_ib_destroy_mr(struct ib_mr *ibmr)
+{
+       struct mlx5_ib_dev *dev = to_mdev(ibmr->device);
+       struct mlx5_ib_mr *mr = to_mmr(ibmr);
+       int err;
+
+       if (mr->sig) {
+               if (mlx5_core_destroy_psv(&dev->mdev,
+                                         mr->sig->psv_memory.psv_idx))
+                       mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
+                                    mr->sig->psv_memory.psv_idx);
+               if (mlx5_core_destroy_psv(&dev->mdev,
+                                         mr->sig->psv_wire.psv_idx))
+                       mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
+                                    mr->sig->psv_wire.psv_idx);
+               kfree(mr->sig);
+       }
+
+       err = mlx5_core_destroy_mkey(&dev->mdev, &mr->mmr);
+       if (err) {
+               mlx5_ib_warn(dev, "failed to destroy mkey 0x%x (%d)\n",
+                            mr->mmr.key, err);
+               return err;
+       }
+
+       kfree(mr);
+
+       return err;
+}
+
 struct ib_mr *mlx5_ib_alloc_fast_reg_mr(struct ib_pd *pd,
                                        int max_page_list_len)
 {
index 35e514dc7b7d7148cbed9198b0d3348e6db88a62..bb746bbe73c66c386275f27415341cb7c64c4b2d 100644 (file)
@@ -144,3 +144,64 @@ int mlx5_core_dump_fill_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr,
        return err;
 }
 EXPORT_SYMBOL(mlx5_core_dump_fill_mkey);
+
+int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn,
+                        int npsvs, u32 *sig_index)
+{
+       struct mlx5_allocate_psv_in in;
+       struct mlx5_allocate_psv_out out;
+       int i, err;
+
+       if (npsvs > MLX5_MAX_PSVS)
+               return -EINVAL;
+
+       memset(&in, 0, sizeof(in));
+       memset(&out, 0, sizeof(out));
+
+       in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_CREATE_PSV);
+       in.npsv_pd = cpu_to_be32((npsvs << 28) | pdn);
+       err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
+       if (err) {
+               mlx5_core_err(dev, "cmd exec failed %d\n", err);
+               return err;
+       }
+
+       if (out.hdr.status) {
+               mlx5_core_err(dev, "create_psv bad status %d\n", out.hdr.status);
+               return mlx5_cmd_status_to_err(&out.hdr);
+       }
+
+       for (i = 0; i < npsvs; i++)
+               sig_index[i] = be32_to_cpu(out.psv_idx[i]) & 0xffffff;
+
+       return err;
+}
+EXPORT_SYMBOL(mlx5_core_create_psv);
+
+int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num)
+{
+       struct mlx5_destroy_psv_in in;
+       struct mlx5_destroy_psv_out out;
+       int err;
+
+       memset(&in, 0, sizeof(in));
+       memset(&out, 0, sizeof(out));
+
+       in.psv_number = cpu_to_be32(psv_num);
+       in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DESTROY_PSV);
+       err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
+       if (err) {
+               mlx5_core_err(dev, "destroy_psv cmd exec failed %d\n", err);
+               goto out;
+       }
+
+       if (out.hdr.status) {
+               mlx5_core_err(dev, "destroy_psv bad status %d\n", out.hdr.status);
+               err = mlx5_cmd_status_to_err(&out.hdr);
+               goto out;
+       }
+
+out:
+       return err;
+}
+EXPORT_SYMBOL(mlx5_core_destroy_psv);
index 817a6fae6d2c37ac62c171dc4b2f3f2c91ae9f0a..f714fc427765aa65dc39fbbd8b4ece07a5c124bb 100644 (file)
@@ -48,6 +48,8 @@ enum {
        MLX5_MAX_COMMANDS               = 32,
        MLX5_CMD_DATA_BLOCK_SIZE        = 512,
        MLX5_PCI_CMD_XPORT              = 7,
+       MLX5_MKEY_BSF_OCTO_SIZE         = 4,
+       MLX5_MAX_PSVS                   = 4,
 };
 
 enum {
@@ -936,4 +938,27 @@ enum {
        MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO        = 1 <<  0
 };
 
+struct mlx5_allocate_psv_in {
+       struct mlx5_inbox_hdr   hdr;
+       __be32                  npsv_pd;
+       __be32                  rsvd_psv0;
+};
+
+struct mlx5_allocate_psv_out {
+       struct mlx5_outbox_hdr  hdr;
+       u8                      rsvd[8];
+       __be32                  psv_idx[4];
+};
+
+struct mlx5_destroy_psv_in {
+       struct mlx5_inbox_hdr   hdr;
+       __be32                  psv_number;
+       u8                      rsvd[4];
+};
+
+struct mlx5_destroy_psv_out {
+       struct mlx5_outbox_hdr  hdr;
+       u8                      rsvd[8];
+};
+
 #endif /* MLX5_DEVICE_H */
index 130bc8d77fa5e38173b90c157092189cb3ff7479..e1cb657ccade39dbb481178365e56b358f75ed1c 100644 (file)
@@ -401,6 +401,22 @@ struct mlx5_eq {
        struct mlx5_rsc_debug   *dbg;
 };
 
+struct mlx5_core_psv {
+       u32     psv_idx;
+       struct psv_layout {
+               u32     pd;
+               u16     syndrome;
+               u16     reserved;
+               u16     bg;
+               u16     app_tag;
+               u32     ref_tag;
+       } psv;
+};
+
+struct mlx5_core_sig_ctx {
+       struct mlx5_core_psv    psv_memory;
+       struct mlx5_core_psv    psv_wire;
+};
 
 struct mlx5_core_mr {
        u64                     iova;
@@ -746,6 +762,9 @@ void mlx5_db_free(struct mlx5_core_dev *dev, struct mlx5_db *db);
 const char *mlx5_command_str(int command);
 int mlx5_cmdif_debugfs_init(struct mlx5_core_dev *dev);
 void mlx5_cmdif_debugfs_cleanup(struct mlx5_core_dev *dev);
+int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn,
+                        int npsvs, u32 *sig_index);
+int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num);
 
 static inline u32 mlx5_mkey_to_idx(u32 mkey)
 {