From patchwork Mon Sep 12 04:49:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9325695 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A169E6048B for ; Mon, 12 Sep 2016 04:51:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 97C9B28ACC for ; Mon, 12 Sep 2016 04:51:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8C1CA28B18; Mon, 12 Sep 2016 04:51:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7ED0D28ACC for ; Mon, 12 Sep 2016 04:51:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932676AbcILEv1 (ORCPT ); Mon, 12 Sep 2016 00:51:27 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:34220 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932258AbcILEup (ORCPT ); Mon, 12 Sep 2016 00:50:45 -0400 Received: from sc9-mailhost2.vmware.com (10.113.161.72) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Sun, 11 Sep 2016 21:50:30 -0700 Received: from EX13-CAS-004.vmware.com (ex13-cas-004.vmware.com [10.113.191.54]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 4BA82B04DB; Sun, 11 Sep 2016 21:50:40 -0700 (PDT) Received: from EX13-MBX-039.vmware.com (10.113.191.190) by EX13-MBX-013.vmware.com (10.113.191.33) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Sun, 11 Sep 2016 21:50:40 -0700 Received: from EX13-CAS-003.vmware.com (10.113.191.53) by EX13-MBX-039.vmware.com (10.113.191.190) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Sun, 11 Sep 2016 21:50:39 -0700 Received: from NAM01-SN1-obe.outbound.protection.outlook.com (10.113.170.11) by EX13-CAS-003.vmware.com (10.113.191.53) with Microsoft SMTP Server (TLS) id 15.0.1156.6 via Frontend Transport; Sun, 11 Sep 2016 21:50:39 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onevmw.onmicrosoft.com; s=selector1-vmware-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=/dLeBoCvSSe2M8ujgreB2oJJdTBXBddILO6Wpluwo4Q=; b=cy3IzQPiXnlpoJnLBweE+5g0OYNyllGlJr4BfogMZt6ib/XxQ34aCN79Hse264pokpUMt4Qj18Io7lFK91/4O7phWQ6OT9hJgvkjsfGo5p85H6JFJBIm8Jd/gurrhctJjILE7z4Q8caL3fJHRBWw4Me2721nBZOj3UJGsAdDk8I= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none; redhat.com; dmarc=none action=none header.from=vmware.com; Received: from promb-2s-dhcp95-136.eng.vmware.com (208.91.1.34) by BLUPR0501MB836.namprd05.prod.outlook.com (10.141.251.150) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.619.3; Mon, 12 Sep 2016 04:50:35 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , , , Subject: [PATCH v4 11/16] IB/pvrdma: Add support for memory regions Date: Sun, 11 Sep 2016 21:49:21 -0700 Message-ID: <1473655766-31628-12-git-send-email-aditr@vmware.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1473655766-31628-1-git-send-email-aditr@vmware.com> References: <1473655766-31628-1-git-send-email-aditr@vmware.com> MIME-Version: 1.0 X-Originating-IP: [208.91.1.34] X-ClientProxiedBy: BY2PR04CA052.namprd04.prod.outlook.com (10.141.249.170) To BLUPR0501MB836.namprd05.prod.outlook.com (10.141.251.150) X-MS-Office365-Filtering-Correlation-Id: 71ab88b3-f06f-4a51-1318-08d3dac85655 X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 2:OgtYFQ/Bzyp2aZ5j327bAxl2mI8aHQ1ngr8FCjIEjPrYwTKuQyj9eDl+zKrStAFd6pZkfNG7k5FrlbFpTIpyackHhTQTFNxLrHKo56iaKSeAKmpVQOEU7XiUMHY/SS8EUhCpVfXH+xCYmI/hBqhYSk8q3eHJxWeoEosDW5IbyhFKYv1If8jm9Gx9Grf5SCZA; 3:1naQPsVbqnmb/877/yxk66HNntN3B8qIlqrcbHR4W5crwe5/QLRLlM00TtV+4eQkWd9CevL9+Ls6lURaiR7ZdgVAiYxUitE5bD6Fi3p02HpPp6sJoRgdcND8P3DQnhy5; 25:G2i/AjCztIFkJtY21oskgWSrOnW8ajcipwdR/+RP3s0pas1vnbeXoMXfLsAThMgkCaeY6Z9+qv0avpBfB26ObwJHbNsn/r1YfPCO9BoBNweCcnfY7d3cDTpOjPdj1HPmF7vGPYd2Mfsk9XoePMhvKG2e5Z400tPUsw0ZqIl3ueDuMX8mibaB7P41feWpnpxmOpstYgHsRbK/wdZtsdsDIApsphJjqq/I6kDTP8LomI1l97wawsm9A8or9urHAbGsNqNcFhy2b0Dn2zeLugGaluD7NtlZlRLrKUMiscfsM9TSsQHlrdKGuT1D/6OAke2lCuHpu6GCZwiNC6mKA+hISFqqbrDCE8hURsqD0p1BjXti1EfahrpCUYbMXysKNcHirc6WNUmXKrp43UTvRhd5gOi8OGvBS0uul+nMZxYKxt4= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BLUPR0501MB836; X-LD-Processed: b39138ca-3cee-4b4a-a4d6-cd83d9dd62f0,ExtAddr X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 31:sbQqPzkxeXogwhLTkKG9OnQ2BJs5DAuNT3/j42TnPbXKRYB7ZtcHPLnfMBJjbuErcFZHN/9Q1Dmp0YjQ7LqRbsXLivq73TlVZ6068EU46ppNVqTnpghs8CuIj8dING5Knw5m1Fitp+3nwrMv07sQdrLRrNvBxHFPy+Fh3yMSaxZ/subxTm3PqYY8bHrfO/lD3kZA5Z6+YfmVBkDQb2sCKBZlBSV15qGiTaH/0wyOQR0=; 20:RHyP0AEzg5iJOBM2ObKj/wcCix/uNndxYUH7mjwAUmZ1TtA5U2c4NrTDp66lA1PTQPPkxh9chn57hkoq5Ax0OhfspzvDwSAAA59InyiSRFmSeaWQx7uWdtG39tjiTyJb/sbZPsKKkp29qGYu/u1rgPKMOeTFqp51MpqTdFIps6PLAy5i1v4sb0XX9jEdZ1NT5pKTuBb/soCv6bHMh0fsBtrpuT5x6GgMBVJQYOAcA22pjga9b9tHwFrZ1CpWmdAbfNs1xYjOTMujD8b0lKjvoScKwVW5gmSkQ8X7tbdGWTJbWSCGYSo6N2J1g/G5zTqWuDCnBxIkdhJvycSs7i79x3q8YvkOIHTH1eM7qRRl/LZiDVzEhVbS3ecvteYAtFNzSr+yF9aoqNpRGLw0uXyVLqqMbTJtqW7aMaI1NZwAFw4tLneZbFME+eaAQAEu2JI+m7U6r2u62tiL9YF/YFNPpoAMQXYpDLRaMnWh07UR4juGSUegy01LeyIF/c6hX19q X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(250305191791016)(61668805478150)(22074186197030); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046); SRVR:BLUPR0501MB836; BCL:0; PCL:0; RULEID:; SRVR:BLUPR0501MB836; X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 4:dl1A8JH+U3uB7uA/QOjTpymp/ZUVryZ3HfaPDZFatgmttwiUIoOnRdU7EM6uMfyUIwyNVELUjFyUdkcVRCI43Da6szh67aDWW/pz+XeGX362Yl4ynLSKS/J8TDgi5XeKYwA0b3X2zEtiogSks3vxMTi5j0xaFdTabdkGw3NU+KYyre/iYWadPUPtfTTtEVH0KBdivdrSCc/L/5bMZiBc/IFEHQyH5+QFFWK7AZaNxPzNobcSPu1gOetQTpU9gUpoXOBbMohJUgOJfEzhw3faDQ0+U+bGQsfNNU+qGygV0qOimarLcciIuqUJxleB1dHpJyQ0Uhemgr8j/aNdqMe30XqV+UxdnIC6NZcLigLj7Gs0JDK4qj5SUsY0QvGuP1oD/AnKfY6GNwIocOKFrOImJ3Tyj/9IFTrt+34w6dBFvR8OjLnSHu2YjHwBnGqac62130G2m23HhcFMuAb0z5tKB/AGsAs5OuESG1IahXfp/MhRiuDe3TdkZfajhXxgPcqH X-Forefront-PRVS: 006339698F X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(66066001)(107886002)(4001430100002)(5003940100001)(36756003)(47776003)(189998001)(4001450100002)(87386001)(86362001)(48376002)(2201001)(50466002)(2906002)(50226002)(8676002)(81166006)(586003)(4326007)(7846002)(5660300001)(305945005)(42186005)(76176999)(7736002)(50986999)(6116002)(3846002)(15975445007)(4001520100001)(5001770100001)(19580405001)(33646002)(92566002)(19580395003)(2950100001)(77096005)(229853001)(7099028)(5001760100003)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR0501MB836; H:promb-2s-dhcp95-136.eng.vmware.com; FPR:; SPF:None; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BLUPR0501MB836; 23:4w5J/26yrnIp7Ode4ZLO+s5OHwVQ3zRTompV3BRV?= =?us-ascii?Q?Ob5nEp1Pj1s9M6lF1MOkgK4khhIQt6y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In1z/63xM3O+t7CoQ5V9u+uzarefnlv7rk5m7eF2r0xz0YqHm2bd SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 20:mJ4sevhJBZlj6m7M+MYZwqF358hHew5wUqNKHbNXXJ4+juszdLdrcGw8JY7BoRhAmWu2RytoDeI+AEZSuSOGRkr/Q5eWgDyTVB+YGDh6i13uTQHZY7cLNpGRHw4ldpfECnAYx2npncVKtgKlNTYUb2DlH7JE2khhGhUgIiKCuJA= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Sep 2016 04:50:35.4012 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR0501MB836 Received-SPF: None (EX13-EDG-OU-002.vmware.com: aditr@vmware.com does not designate permitted sender hosts) Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds support for creating and destroying memory regions. The PVRDMA device supports User MRs, DMA MRs (no Remote Read/Write support), Fast Register MRs. Reviewed-by: Jorgen Hansen Reviewed-by: George Zhang Reviewed-by: Aditya Sarwade Reviewed-by: Bryan Tan Signed-off-by: Adit Ranadive Reviewed-by: Yuval Shaia --- Changes v3->v4: - Changed access flag check for DMA MR to using bit operation. - Removed some local variables. Changes v2->v3: - Removed boolean in pvrdma_cmd_post. --- drivers/infiniband/hw/pvrdma/pvrdma_mr.c | 332 +++++++++++++++++++++++++++++++ 1 file changed, 332 insertions(+) create mode 100644 drivers/infiniband/hw/pvrdma/pvrdma_mr.c diff --git a/drivers/infiniband/hw/pvrdma/pvrdma_mr.c b/drivers/infiniband/hw/pvrdma/pvrdma_mr.c new file mode 100644 index 0000000..6163f17 --- /dev/null +++ b/drivers/infiniband/hw/pvrdma/pvrdma_mr.c @@ -0,0 +1,332 @@ +/* + * Copyright (c) 2012-2016 VMware, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of EITHER the GNU General Public License + * version 2 as published by the Free Software Foundation or the BSD + * 2-Clause License. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License version 2 for more details at + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html. + * + * You should have received a copy of the GNU General Public License + * along with this program available in the file COPYING in the main + * directory of this source tree. + * + * The BSD 2-Clause License + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include "pvrdma.h" + +/** + * pvrdma_get_dma_mr - get a DMA memory region + * @pd: protection domain + * @acc: access flags + * + * @return: ib_mr pointer on success, otherwise returns an errno. + */ +struct ib_mr *pvrdma_get_dma_mr(struct ib_pd *pd, int acc) +{ + struct pvrdma_dev *dev = to_vdev(pd->device); + struct pvrdma_user_mr *mr; + union pvrdma_cmd_req req; + union pvrdma_cmd_resp rsp; + struct pvrdma_cmd_create_mr *cmd = &req.create_mr; + struct pvrdma_cmd_create_mr_resp *resp = &rsp.create_mr_resp; + int ret; + + if (!(acc & IB_ACCESS_LOCAL_WRITE)) { + dev_warn(&dev->pdev->dev, + "unsupported dma mr access flags %#x\n", acc); + return ERR_PTR(-EOPNOTSUPP); + } + + mr = kzalloc(sizeof(*mr), GFP_KERNEL); + if (!mr) + return ERR_PTR(-ENOMEM); + + memset(cmd, 0, sizeof(*cmd)); + cmd->hdr.cmd = PVRDMA_CMD_CREATE_MR; + cmd->pd_handle = to_vpd(pd)->pd_handle; + cmd->access_flags = acc; + cmd->flags = PVRDMA_MR_FLAG_DMA; + + ret = pvrdma_cmd_post(dev, &req, &rsp); + if (ret < 0) { + dev_warn(&dev->pdev->dev, "could not get DMA mem region\n"); + kfree(mr); + return ERR_PTR(ret); + } + + mr->mmr.mr_handle = resp->mr_handle; + mr->ibmr.lkey = resp->lkey; + mr->ibmr.rkey = resp->rkey; + + return &mr->ibmr; +} + +/** + * pvrdma_reg_user_mr - register a userspace memory region + * @pd: protection domain + * @start: starting address + * @length: length of region + * @virt_addr: I/O virtual address + * @access_flags: access flags for memory region + * @udata: user data + * + * @return: ib_mr pointer on success, otherwise returns an errno. + */ +struct ib_mr *pvrdma_reg_user_mr(struct ib_pd *pd, u64 start, u64 length, + u64 virt_addr, int access_flags, + struct ib_udata *udata) +{ + struct pvrdma_dev *dev = to_vdev(pd->device); + struct pvrdma_user_mr *mr = NULL; + struct ib_umem *umem; + union pvrdma_cmd_req req; + union pvrdma_cmd_resp rsp; + struct pvrdma_cmd_create_mr *cmd = &req.create_mr; + struct pvrdma_cmd_create_mr_resp *resp = &rsp.create_mr_resp; + int nchunks; + int ret; + int entry; + struct scatterlist *sg; + + if (length == 0 || length > dev->dsr->caps.max_mr_size) { + dev_warn(&dev->pdev->dev, "invalid mem region length\n"); + return ERR_PTR(-EINVAL); + } + + umem = ib_umem_get(pd->uobject->context, start, + length, access_flags, 0); + if (IS_ERR(umem)) { + dev_warn(&dev->pdev->dev, + "could not get umem for mem region\n"); + return ERR_CAST(umem); + } + + nchunks = 0; + for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) + nchunks += sg_dma_len(sg) >> PAGE_SHIFT; + + if (nchunks < 0 || nchunks > PVRDMA_PAGE_DIR_MAX_PAGES) { + dev_warn(&dev->pdev->dev, "overflow %d pages in mem region\n", + nchunks); + ret = -EINVAL; + goto err_umem; + } + + mr = kzalloc(sizeof(*mr), GFP_KERNEL); + if (!mr) { + ret = -ENOMEM; + goto err_umem; + } + + mr->mmr.iova = virt_addr; + mr->mmr.size = length; + mr->umem = umem; + + ret = pvrdma_page_dir_init(dev, &mr->pdir, nchunks, false); + if (ret) { + dev_warn(&dev->pdev->dev, + "could not allocate page directory\n"); + goto err_umem; + } + + ret = pvrdma_page_dir_insert_umem(&mr->pdir, mr->umem, 0); + if (ret) + goto err_pdir; + + /* Send to device */ + memset(cmd, 0, sizeof(*cmd)); + cmd->hdr.cmd = PVRDMA_CMD_CREATE_MR; + cmd->start = start; + cmd->length = length; + cmd->pd_handle = to_vpd(pd)->pd_handle; + cmd->access_flags = access_flags; + cmd->nchunks = nchunks; + cmd->pdir_dma = mr->pdir.dir_dma; + + ret = pvrdma_cmd_post(dev, &req, &rsp); + + if (ret < 0 || rsp.hdr.ack != PVRDMA_CMD_CREATE_MR_RESP) { + dev_warn(&dev->pdev->dev, "could not register mem region\n"); + goto err_pdir; + } + + mr->mmr.mr_handle = resp->mr_handle; + mr->ibmr.lkey = resp->lkey; + mr->ibmr.rkey = resp->rkey; + + return &mr->ibmr; + +err_pdir: + pvrdma_page_dir_cleanup(dev, &mr->pdir); +err_umem: + ib_umem_release(umem); + kfree(mr); + + return ERR_PTR(ret); +} + +/** + * pvrdma_alloc_mr - allocate a memory region + * @pd: protection domain + * @mr_type: type of memory region + * @max_num_sg: maximum number of pages + * + * @return: ib_mr pointer on success, otherwise returns an errno. + */ +struct ib_mr *pvrdma_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type, + u32 max_num_sg) +{ + struct pvrdma_dev *dev = to_vdev(pd->device); + struct pvrdma_user_mr *mr; + union pvrdma_cmd_req req; + union pvrdma_cmd_resp rsp; + struct pvrdma_cmd_create_mr *cmd = &req.create_mr; + struct pvrdma_cmd_create_mr_resp *resp = &rsp.create_mr_resp; + int size = max_num_sg * sizeof(u64); + int ret; + + if (mr_type != IB_MR_TYPE_MEM_REG || + max_num_sg > PVRDMA_MAX_FAST_REG_PAGES) + return ERR_PTR(-EINVAL); + + mr = kzalloc(sizeof(*mr), GFP_KERNEL); + if (!mr) + return ERR_PTR(-ENOMEM); + + mr->pages = kzalloc(size, GFP_KERNEL); + if (!mr->pages) { + ret = -ENOMEM; + goto freemr; + } + + ret = pvrdma_page_dir_init(dev, &mr->pdir, max_num_sg, false); + if (ret) { + dev_warn(&dev->pdev->dev, + "failed to allocate page dir for mr\n"); + ret = -ENOMEM; + goto freepages; + } + + memset(cmd, 0, sizeof(*cmd)); + cmd->hdr.cmd = PVRDMA_CMD_CREATE_MR; + cmd->pd_handle = to_vpd(pd)->pd_handle; + cmd->access_flags = 0; + cmd->flags = PVRDMA_MR_FLAG_FRMR; + cmd->nchunks = max_num_sg; + + ret = pvrdma_cmd_post(dev, &req, &rsp); + if (ret < 0) { + dev_warn(&dev->pdev->dev, "could not create FR mem region\n"); + ret = -EINVAL; + goto freepdir; + } + + mr->max_pages = max_num_sg; + mr->mmr.mr_handle = resp->mr_handle; + mr->ibmr.lkey = resp->lkey; + mr->ibmr.rkey = resp->rkey; + mr->page_shift = PAGE_SHIFT; + mr->umem = NULL; + + return &mr->ibmr; + +freepdir: + pvrdma_page_dir_cleanup(dev, &mr->pdir); +freepages: + kfree(mr->pages); +freemr: + kfree(mr); + return ERR_PTR(ret); +} + +/** + * pvrdma_dereg_mr - deregister a memory region + * @ibmr: memory region + * + * @return: 0 on success. + */ +int pvrdma_dereg_mr(struct ib_mr *ibmr) +{ + struct pvrdma_user_mr *mr = to_vmr(ibmr); + struct pvrdma_dev *dev = to_vdev(ibmr->device); + union pvrdma_cmd_req req; + struct pvrdma_cmd_destroy_mr *cmd = &req.destroy_mr; + int ret; + + memset(cmd, 0, sizeof(*cmd)); + cmd->hdr.cmd = PVRDMA_CMD_DESTROY_MR; + cmd->mr_handle = mr->mmr.mr_handle; + ret = pvrdma_cmd_post(dev, &req, NULL); + if (ret < 0) + dev_warn(&dev->pdev->dev, "could not deregister mem region\n"); + + pvrdma_page_dir_cleanup(dev, &mr->pdir); + if (mr->umem) + ib_umem_release(mr->umem); + + kfree(mr->pages); + kfree(mr); + + return 0; +} + +static int pvrdma_set_page(struct ib_mr *ibmr, u64 addr) +{ + struct pvrdma_user_mr *mr = to_vmr(ibmr); + + if (mr->npages == mr->max_pages) + return -ENOMEM; + + mr->pages[mr->npages++] = addr; + return 0; +} + +int pvrdma_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents, + unsigned int *sg_offset) +{ + struct pvrdma_user_mr *mr = to_vmr(ibmr); + struct pvrdma_dev *dev = to_vdev(ibmr->device); + int ret; + + mr->npages = 0; + + ret = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, pvrdma_set_page); + if (ret < 0) + dev_warn(&dev->pdev->dev, "could not map sg to pages\n"); + + return ret; +}