From patchwork Wed Jul 6 06:14:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9215531 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 7BC1C607D9 for ; Wed, 6 Jul 2016 06:31:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6CC6A286FC for ; Wed, 6 Jul 2016 06:31:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6198F286FE; Wed, 6 Jul 2016 06:31:41 +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 8E0BD286FA for ; Wed, 6 Jul 2016 06:31:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752160AbcGFGbj (ORCPT ); Wed, 6 Jul 2016 02:31:39 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:54493 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751046AbcGFGbi (ORCPT ); Wed, 6 Jul 2016 02:31:38 -0400 Received: from sc9-mailhost1.vmware.com (10.113.161.71) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Tue, 5 Jul 2016 23:15:13 -0700 Received: from EX13-CAS-005.vmware.com (ex13-cas-005.vmware.com [10.113.191.55]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id D4792180DE; Tue, 5 Jul 2016 23:15:26 -0700 (PDT) Received: from EX13-CAS-001.vmware.com (10.113.191.51) by EX13-MBX-022.vmware.com (10.113.191.42) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Tue, 5 Jul 2016 23:15:26 -0700 Received: from NAM03-BY2-obe.outbound.protection.outlook.com (10.113.170.11) by EX13-CAS-001.vmware.com (10.113.191.51) with Microsoft SMTP Server (TLS) id 15.0.1156.6 via Frontend Transport; Tue, 5 Jul 2016 23:15:26 -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=2JirFf0cnLkALjhRpMcc1fY6Sg6l6tNqFuB6jHPnRi4=; b=hi11TVIsFzrtAKfwr39q1eLTsIEv2RS22XM5u3nQdb7UmptMY3rxotg+FqlrarxoviqW02pSJpAbHLDWZSSGFCiZ6SNyNH6ahGHqiYustlcFPOq76UDhc3HvdXaolLxrPSTck3xcDDcdnR/WOmhAEuIoH0vSkYJeJjcB4SqJRpc= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=aditr@vmware.com; Received: from promb-2s-dhcp95-136.eng.vmware.com (208.91.1.34) by SN2PR0501MB848.namprd05.prod.outlook.com (10.160.14.146) with Microsoft SMTP Server (TLS) id 15.1.528.16; Wed, 6 Jul 2016 06:15:21 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , Subject: [PATCH v1 09/15] IB/pvrdma: Add support for memory regions Date: Tue, 5 Jul 2016 23:14:42 -0700 Message-ID: <1467785688-23229-10-git-send-email-aditr@vmware.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1467785688-23229-1-git-send-email-aditr@vmware.com> References: <1467785688-23229-1-git-send-email-aditr@vmware.com> MIME-Version: 1.0 X-Originating-IP: [208.91.1.34] X-ClientProxiedBy: CO2PR11CA0028.namprd11.prod.outlook.com (10.141.242.166) To SN2PR0501MB848.namprd05.prod.outlook.com (10.160.14.146) X-MS-Office365-Filtering-Correlation-Id: bcc9a15f-65d6-43ba-ffa0-08d3a564e9b6 X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB848; 2:fsI5xVAL8wRghERzS+WSqaMAdB2J9myF2M1RhW7Ey0du57xz0NAAWmiz0brLLnUdfvDX/YpsMDC23dTL3RzyNhSQ2g+d/5AvgZ5bXCIa0qYFb6GxGKpplPf06npEYq0fX9N+Z9SeNoBusEczeK4+iqpW5FcZJ+QMoANO79mwElUMgX4H7FrcQRepFb2k2lw+; 3:Hud+8HQnk8K3rG5Z/9AZDST8w12TCztNh/3l4d7YVSrOJ9RBqPoBwz9YvwKXWjZzR7s7lYfWuNUsAZTMXFeaV+iEixxE9ECz/Us3hwp89BcOhUQWTvSwJa0wiqfbVSFr; 25:NOva8kpzlD1fiB5JJDzV6FSIc4padKtnvOYP0z/dmJCGKLBIuMsDZgCBx/1OdTKnKwtO6KeCPuw4HOTk3YgetrhQ7gCHeM6+w3e+3vQK5rZNtURvc0OFFi6VzcUedoVCAMfKVhLc4Y/KH460iNXoSQSfdWq8f2Ol0sjeBMLQ4LyeCnSpulduhFnJCJcBgalIH3jAye2o0ZEPSHk4N2QO+CHQ0VLGsrYttOR3pMOqpYmnqmSvbb+H+95buyexrQ76X+ml86CtQdIrL44HxA1j4KeDzHYjilORnCnmOLyrypVOi+6rHTMOKT/3Fh2+Ds+oLQiDwac0fGpYme+aW1FXJUD4YcBXNN56fLSEVT743kVhxD0EJJRYszY5XAIIT6eM2i8m5XH+k8ehdbi7/cxawSX/vkl2m6A0pJrEhfQNXIY= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:SN2PR0501MB848; X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB848; 31:s47UcY7WbZFTwgYGma/gTt/+gvhYVhWbrb6xQu3/GAks/ibjuoOZCeJw6wVRRFN6TSG1RqVLpvGIjyQmBAzoNGLzYS02t0zn9uxIwNbMbQZGs+LdnIaYfZZkCbPS0j//WMLeBaugXWsoQWPcKki1+ytfYTdZMXCqdjJG4tXr+aps3B7avQcPRm/TtVrNID8zp8iNsqYMDD3jzmjXHahrcw==; 20:Fv9UJs0e8dwWrydgUxYxP/JCOX+pe3Xg6osTgRUQhi5jDes+dJf/cGqgikEcaTg3OmhB7L9HJftU7xQ334jthZp/5Tzb34gsYdTHmIHou6s8JXoz9EtgRXF9ntXVSgvRVDlgnnM4oxVGEEABo0TYcHBdAb7Gh2YovfSFtzn6DyuIdE4RfN21kY5XagOFnmJT07B8tyfuQsSacHiRugs0jFTVSFjHimSTkOri9HwWdvF+oRYm7wakhBDLbZn3Ul9J; 4:4wWaKtQgqSRS2zk95IKIcTsKyKrM5ve3DcPTIp6lUKznR5ET++DYDTxCoquWJmTJNDaye9b137z7B7eQzhKnfQ412+je7ALNO/FKCtEFiXyGnVEVIDC8s6kbm5tvfGrs2VPNoj2kENaBFl0eLjFRS7VjpACNIF9c+KWw4tqge9Tx4DuLOLwHcql1JGEENGF8R19GmQN6XsG2JE2q/4WBuG/wc6bbxTnf0z0gnTjWCUCxonYIK2JoXJCmfZQMGZRPKAYtkOHS6hxPM1IIxTZK89FuS6E2/LQCxlHvyC5wFGYVwB3iXGdY7zDpgx49Hyer0jdI6YGlBuM8qgHkao7tx73sDRT0fuFuKi1btSFdsPE5nMzsV35mlwnyLOv4y9FUnuMFCj5jf7tmDWlKFyXl4EZG98mqq1PzuiUAjbj97avKMjJ7VOKNbVbteTldAqGa8MaxzgyIl nBKCxqhW4UlVOE3j/o6vxDYqx7eqY/JQ1 o= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(250305191791016)(61668805478150)(22074186197030); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001); SRVR:SN2PR0501MB848; BCL:0; PCL:0; RULEID:; SRVR:SN2PR0501MB848; X-Forefront-PRVS: 0995196AA2 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(189002)(199003)(81166006)(81156014)(4001450100002)(92566002)(86362001)(19580395003)(4001430100002)(19580405001)(7846002)(7736002)(305945005)(8676002)(105586002)(2201001)(586003)(42186005)(33646002)(106356001)(3846002)(5003940100001)(6116002)(48376002)(36756003)(50466002)(5001770100001)(97736004)(189998001)(229853001)(47776003)(66066001)(76176999)(107886002)(50986999)(68736007)(101416001)(50226002)(4326007)(2950100001)(15975445007)(77096005)(2906002)(7099028)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:SN2PR0501MB848; H:promb-2s-dhcp95-136.eng.vmware.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (EX13-EDG-OU-002.vmware.com: aditr@vmware.com does not designate permitted sender hosts) Received-SPF: None (protection.outlook.com: vmware.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; SN2PR0501MB848; 23:jgN8hBkvL66zjWIKIyxP4MMsxlW91gPMIi4yXoMB?= =?us-ascii?Q?FR8lFXBmdXUCknv8+mAEmErZwPOdzozMoz4KpN1Oyx/zlWUcISjbdXuUaNN6?= =?us-ascii?Q?vVVCiZBPo2BvLKo1OKN1ja6KhNeAzQqaswyeE7gGs69xRjFPSYJl6cJkq3Ip?= =?us-ascii?Q?qcGISzo0+vrpIzD3iDU2kAipJ7xYVkVOPtCEtK9AyWjQRyg/uh1/NLhPtXmH?= =?us-ascii?Q?fIQHAkA8RZdEr7sEArvvMx8ImTK7S7Lgi0Wfkp/DEoId41V3JpA/nsQIqLH6?= =?us-ascii?Q?jlG7eWkybJMwrc1CmNNXIwYfL2ela6s3igh2kG7KjELDZYKPuQ4TJgCfaLmU?= =?us-ascii?Q?8V71A0/RQV6kTe7sRanKSzDm/gnXmqORg+it2OYJwwDPCIgOKrPOHphrmckc?= =?us-ascii?Q?61WKQl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FfdLNAWI= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jul 2016 06:15:21.7702 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN2PR0501MB848 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 --- drivers/infiniband/hw/pvrdma/pvrdma_mr.c | 333 +++++++++++++++++++++++++++++++ 1 file changed, 333 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..ea5edd5 --- /dev/null +++ b/drivers/infiniband/hw/pvrdma/pvrdma_mr.c @@ -0,0 +1,333 @@ +/* + * 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\n"); + 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, true, &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 host */ + 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, true, &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, true, &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; + union pvrdma_cmd_resp rsp; + 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, false, &rsp); + 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; +}