From patchwork Wed Aug 3 23:27:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9262343 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 CB0556048F for ; Wed, 3 Aug 2016 23:32:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BB26B27F3E for ; Wed, 3 Aug 2016 23:32:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AFD5928068; Wed, 3 Aug 2016 23:32:39 +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 D7D4D27F3E for ; Wed, 3 Aug 2016 23:32:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932168AbcHCXc1 (ORCPT ); Wed, 3 Aug 2016 19:32:27 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:3386 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932449AbcHCXcW (ORCPT ); Wed, 3 Aug 2016 19:32:22 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Wed, 3 Aug 2016 16:27:37 -0700 Received: from EX13-CAS-004.vmware.com (smtp-inbound.vmware.com [10.113.191.54]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id CB74240498; Wed, 3 Aug 2016 16:28:27 -0700 (PDT) Received: from EX13-CAS-002.vmware.com (10.113.191.52) by EX13-MBX-029.vmware.com (10.113.191.49) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Wed, 3 Aug 2016 16:28:27 -0700 Received: from NAM03-DM3-obe.outbound.protection.outlook.com (10.113.170.11) by EX13-CAS-002.vmware.com (10.113.191.52) with Microsoft SMTP Server (TLS) id 15.0.1156.6 via Frontend Transport; Wed, 3 Aug 2016 16:28:27 -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=8g/zGrPNyaYuwVqIKHeETGoEa4EPXYSWQu2abRNayTU=; b=aXFcyU9spkFjeETcQ42I8vx6EnAXxJMNVCeOw8p8RtJlMN06SHYyOD+9kAANwJEiVr37M1Vu2h9SVtFrMVFasQcSMkaqjJFvU+RhvIupgJkr6wDIWKvu2YwCgCymyERscJGtmyn964TULpYeag6lXanCRaw+sRlFe2HCivuMS3E= 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 SN2PR0501MB847.namprd05.prod.outlook.com (10.160.14.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.549.15; Wed, 3 Aug 2016 23:28:22 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , Subject: [PATCH v3 10/15] IB/pvrdma: Add support for memory regions Date: Wed, 3 Aug 2016 16:27:39 -0700 Message-ID: <1470266864-16888-11-git-send-email-aditr@vmware.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1470266864-16888-1-git-send-email-aditr@vmware.com> References: <1470266864-16888-1-git-send-email-aditr@vmware.com> MIME-Version: 1.0 X-Originating-IP: [208.91.1.34] X-ClientProxiedBy: CY1PR12CA0084.namprd12.prod.outlook.com (10.163.230.52) To SN2PR0501MB847.namprd05.prod.outlook.com (10.160.14.145) X-MS-Office365-Filtering-Correlation-Id: 3a2a6a46-8cf3-4c44-de38-08d3bbf5dcec X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB847; 2:cgl1NgJikUf9uvifWnbmISH5wEEE3KWVO9ZjQPnP38Yk67X6SC6IElc9eHZvPlp20Xh5oujcEHXx1CzfDdvTvd82HcK4Z3PI9Cz6NAoSQbjQKSZLEP+rgPdgsRL7ysVFmmZJP3DXGk88xiHDkgAmPoWBOQTSaA5JNmA01oV8fRmYPSgI1R6bSVWeG2Uxtpnj; 3:WvUD2P7nIfZt3fYAJhHOro8CFd0Fszx/FNh3oT+/Yrsk9O+bIFwLKb332bFDRzjKMimMwbRgV14rFqx88K6bVwu1oIJLyfF/jqxFNEITjXKdxrapuSfZ4G+qTGC6AsWE; 25:AKdXvYporyXd0fwDNY2b5kJhnV6//WddoXroUDIO1PZqtx9kJvft9qrJsPz/hyFn8SiJaEwlLQdrtn5lUUS3FJpG4xb9pgNGdILsjDqKelQMEp8astZXCKfO6yurCGGZSYSkZhsaz9Y3XIS3enRf6PstYPZT3X5qmpwyerOXsy220Pz1fKvh/sOEER4iQ4I5Dir3cMnpm5gAHC2NNws9rxOd6AagtHilEHhdAsJ3Zref/XEPnjY7NCd/Gm/SWxoPgfb2grnJqXDHBxTGNQBdw6OVT0B9M+FXUznKXKUE6PO2S6RV96d5i6j5i9LsozU1mLDSZKmuEwK6dGEaJ1/IMyvGWT7HdPbSmUFW1WXo+RUUUuFYLFLKqhr9QASNHmPu1sBLc3/l//4NfU5/bGsNhQ== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:SN2PR0501MB847; X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB847; 31:JULzmklTSA79L602DeE3XkWjOh6oKUn8bhxr0iDmC5PEiBLy9nphYQ7GcggoouAWkyfqm/a/ItAXkq89PieN4uw2KpPGTiogH7GGrLNa6MONF3P/hU8fJFpu41Ap/IbN+us5Nlam+m2dP9R+ZJbNMnASmtylEBnpmIKA6fyqTbhjHlql5xGaJB6o2PNZ3ZkqB5PquTkURxSwYQOwzswixe6vKWYeU/NAfp9tw4ok4P0=; 20:Q80qrJNlxB8No5K3M+ikArIb7JmEmQxN1ukaTFW9Vk3liAvHBANTvS0nJoWe2ZVLr8PPiOE46SIdmqdgqBk1dOa4eSYDKE1a/QZO60XvaRC5pCEgnCrhNoS9rtfEFKZA09asrEMCR2kgJa+2N6/B495TjJAMxhLJEEdtd4dViawdiA5nMyxj2BqfAirrXGeLteHTKqz++Sn00Yij0y8xZJlGADU0rgxVc1rS5LGF/mkJIhRbt5K0OZ/rQTTh+S39 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)(8121501046)(5005006)(10201501046)(3002001); SRVR:SN2PR0501MB847; BCL:0; PCL:0; RULEID:; SRVR:SN2PR0501MB847; X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB847; 4:YLBn4ACplPRWo9XJkdCZvSJbupxDScyB5Hu5hz8uQC+Tyd4lBe3YCWA1UQUn5s0Z0QHXDJQbYxcNzoo9b+rCiJAaCGhAhKXueqmNSowcMgTQDzRb0w2zazCPpqOkR8+PaVpkp+V9lIXiVkDJZnqseYSZDxWRLstsfHVl/SanxTpwPyKYmx25D8kL+2+14UzcJULfWROUTjhEEgYOIg8QtvzOsojH97qOoEXXLG7mEhdbNDd+IfwNrQJyVFXjNtPFZkcFnOTPHf3qIOIyf8vWCKwJjtlV+mKKWlS8lUEl6v2DgFsgxwSJmprSxHFDJ27iGYt2GdcVZzqTi/u1VB830+1lOmdX0pFKsSupYFMgGdKPZj4sO4H5V0ojMO0YRXm/w14S7HkMaQt9rrmnke2Y0a5ZVeNlIrvj2DRISW41BK6SbXLiF3m82fNepQD/42uPEzczhBiHqIeFJ5YbMDrKmScG1lka89ramAL+SmGhA6U= X-Forefront-PRVS: 00235A1EEF X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(199003)(189002)(4001450100002)(66066001)(106356001)(189998001)(33646002)(92566002)(107886002)(5001770100001)(77096005)(15975445007)(229853001)(48376002)(50466002)(101416001)(97736004)(2950100001)(42186005)(586003)(47776003)(68736007)(76176999)(50986999)(4001430100002)(2906002)(105586002)(7846002)(7736002)(305945005)(81156014)(50226002)(86362001)(81166006)(8676002)(6116002)(36756003)(3846002)(5003940100001)(19580405001)(19580395003)(2201001)(4326007)(7099028)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:SN2PR0501MB847; H:promb-2s-dhcp95-136.eng.vmware.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (EX13-EDG-OU-001.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; SN2PR0501MB847; 23:8P/nmgMabL/oCtevjYFDlkYye1rb/HaayslA+weF?= =?us-ascii?Q?QR8ruZ3PQziwAFSCftXo9eS8li6DEL97MhTulqhaKoTXp5OSp/+PLRrCC/XW?= =?us-ascii?Q?KcdXA//xtCZJ+aHpm421rXIcmVMSKW1yVuN0gS+p+k0C7cfXaXN+AgpL6yxr?= =?us-ascii?Q?AD9U9LYaksaX2oxm3KdMqhC9X9J+JhSzzA/PcqpKytyq79lCSrH+qtpXsm4W?= =?us-ascii?Q?XnusNegEvVyikJvYzJp7dbmXeAIIwcbbblPEcSg2P/lYGb1mLLH7mwYdC1rj?= =?us-ascii?Q?mudno1Tcm8IG86EDAOnUuHUB4+YBJVYfEN6kga+uD6rzFAztGd3xxQK20J+d?= =?us-ascii?Q?WW1IgO/NPPEGe0ACbZx+RwpP/QSmJ7zJJxhfUuXr/0oOkXwDV3QZu8nnieY8?= =?us-ascii?Q?e4bordW06JKoxcLfgP+pHQ5lGv2Sw0sCFFmZdRiSHkq/TxiWrkKiAEvOKngF?= =?us-ascii?Q?Gx6tRUqiskovt8iuniUKGEswQAxftAVsOmQJeOnk5lorQ3hZyhwj9RZDuz4r?= =?us-ascii?Q?b86G/wH6jbyJ2YJe4kFVSyyoEd/pOL9xyNDYflWUdtVmlcgrcNH7A9rkRkUz?= =?us-ascii?Q?qS5vtd5n3AgTkBYPWO5w97VbyKbvEDVf5RtDiEGCuN/nXvSLOtWB0w4OONDi?= =?us-ascii?Q?Hk6FOpRj7DQUU21tsPPxWZR3ocV6jH15hB5WeJIqex4HshEaK7QvSboTTpuI?= =?us-ascii?Q?0Xz6EjpUkEb9LKmvoC2ZoDotDcmlu+ruMPA56bmr4dMZJr01/tM+WsuwDztI?= =?us-ascii?Q?STeKHDLuA71YxWgfh6WLSHkxQZdIhizHzxemoDXZLUH/tQXhX3VXOUK4PVYX?= =?us-ascii?Q?rOzTldf8k5J1bSxNzJbV2qzYxzRWtMwq0bHnvv0w5fI4ZsnN5w1kJXKxKFrx?= =?us-ascii?Q?4NNSakU3TqIbL238ftyQWRbl57lF+f7yCwOdTA5DxRxFINQG2Jhq3FTlnV2F?= =?us-ascii?Q?B4G+CxBJ+j8eXtpjLHACKCLf5NEh1dnwldYuETvC0gIHQrhtFQ3JCKQEysUX?= =?us-ascii?Q?be/aX3i1wDewDKTjs0fSPG+5WJ1pnMgmLKDLnYvf85uSj7XWBhx1uXGVKaO9?= =?us-ascii?Q?tcbZl8SyfzkfkS1VLHpfm1secQLTEtnBhNDbo0giuabNUDHU+xK441pI3zAY?= =?us-ascii?Q?C1HiXQzlyjCeqwVQJl5vQGhAXMGUTLuSq88np3Lt+2/WxUgzki8Fxr9hq0go?= =?us-ascii?Q?b69ruDtoeDX7xycBQMY4WU4DnRdOObGRl7KNUJ/D2KaEI8jmP5b6+aKrKw?= =?us-ascii?Q?=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB847; 6:AXWP/jjw1Sz9PYpxe/mmkHFPgH8WImKocDwcojdiGIUdIUv3vyUYdliaCQ6Y2vKIHuWDOC68vDoXXgqytxyakRcEVhZaMHG/qTBNmujpPRomEUINXHzzYau4gZ8GpCozT11lJUYkQKRXb+qiPFqXOiB/1TYiRFkOQFtgcknTScXvyn+kEbXtxWlLTzCrufX9TyMWNWPVV2Dn/ik32Ii26FBqCG4gxAhY66UigzQlDFOpSfc2szSMs/ub7TYcxBNqLYN6oUVKBN1RKHi6GZl2/4mCpgVoCUoJ/4P9B3w8CsY=; 5:/aDd9V0naraLYXrgmA8XCSIe/PACxd8WREit6GDvn9X+0jtyh8aJaPgVrxhvjycaBEQyRSGHOcn0Z1lgoq4NGugnFjPK0CFpLBDwDzrbdgDRfnw1Vj4YR5oaqb8dZ9AXZdwYmLnkmFzwWe1BRSv0JQ==; 24:pNzs9coZdUOFTmFdCTH9fhn8uJlvM+dpzpzVsgzIeA6V0/3XKsHO8OaWlpsEPYwWnYszc42xrnIJgR1KYzi7mNVEkqQBIJzPO/VxGtjImRY=; 7:oZgA5e+ucDAzbdxlimGA2f6kPVLQIqrOkZU1eEEqJdzZDd3lqcYyk9vcd9beiVwjmqqQjJQFbXkaxvRw0XKFbr2BglKJInaMGd6TmnhGZdtxRTXDN17b6fFsSSDT7oEl6KT/HD2supLX+RuV5SrvBmU1oTssyhHg4QsmPbsEySr2JWH83d949rih/HOY31hvCvkyk/P8OjlVLZX8QQ8C6SN0udu/Ywk7xKigPMzcmpAROzWv1DDnzULSzjZWxdfH SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB847; 20:26/uZDXN7vNovip2VUhpaiW6FdPSZqXssrsIJo7EthPSEdbbau1Ay1HWJD8mpsjFw1CA3hbpcl6d3kbrJ9kBzAoflwZGU7hgukfz+tmGQsZWMHihTWndTmxOE017vfzglfjix6w0EBoDGk9X/qaWpixs+y/LlbH30sCMfZWjDHo= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2016 23:28:22.6170 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN2PR0501MB847 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. Changes v2->v3: - Removed boolean in pvrdma_cmd_post. 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 | 331 +++++++++++++++++++++++++++++++ 1 file changed, 331 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..261b8d4 --- /dev/null +++ b/drivers/infiniband/hw/pvrdma/pvrdma_mr.c @@ -0,0 +1,331 @@ +/* + * 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, &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, &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; +}