From patchwork Wed Aug 3 23:27:35 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9262355 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 AE5646048B for ; Wed, 3 Aug 2016 23:39:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E62427FB6 for ; Wed, 3 Aug 2016 23:39:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 92C472807E; Wed, 3 Aug 2016 23:39:33 +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 BBD1928068 for ; Wed, 3 Aug 2016 23:39:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932363AbcHCXjb (ORCPT ); Wed, 3 Aug 2016 19:39:31 -0400 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]:44318 "EHLO smtp-outbound-1.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756994AbcHCXj2 (ORCPT ); Wed, 3 Aug 2016 19:39:28 -0400 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id EDDDE98686; Wed, 3 Aug 2016 16:28:19 -0700 (PDT) Received: from EX13-CAS-003.vmware.com (smtp-inbound.vmware.com [10.113.191.53]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id E47FE40498; Wed, 3 Aug 2016 16:28:20 -0700 (PDT) Received: from EX13-MBX-039.vmware.com (10.113.191.190) by EX13-MBX-023.vmware.com (10.113.191.43) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Wed, 3 Aug 2016 16:28:20 -0700 Received: from EX13-CAS-002.vmware.com (10.113.191.52) by EX13-MBX-039.vmware.com (10.113.191.190) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Wed, 3 Aug 2016 16:28:20 -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:20 -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=HrkLuZEIJ6rwjElc7725rgW9kzwV9U7n6WM/xiTRI/I=; b=mgCd/t//T9/sCaJe6mCXhR4gVPtY3bjL2lx+UHeHi9X14W/Lkxx50fRNvXEL2AnhBrNPX/ocWHWelbC7AJDKlQviqL0IduatFqsBvfeA2GTzu5A38xSkZYlg1AcKsyLKht1PmaDHMuQc9jyaBRJ7zK6Vk14t9+AGIBfVSya2kVQ= 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:16 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , Subject: [PATCH v3 06/15] IB/pvrdma: Add helper functions Date: Wed, 3 Aug 2016 16:27:35 -0700 Message-ID: <1470266864-16888-7-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: 5218d350-3df9-45fa-b69e-08d3bbf5d97a X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB847; 2:COPNcx/obDOTqBNUuYgfolpNn1ClHJmo9o6Wup7jdcw5oUP4ivJ6ebDdcrHx/60Nk+1zo/gUH86vP75mFbyaulcJEJTjiqTfgJeweEKUBDG0s0u7ZWymZbVyg4LNoWDtMnDZZEE2sLnuqzL7em3O3qu+Aa5JRBkhIqa/gIj9r6xiZP97/nZ25+t7nZYv1M64; 3:Mmgb7JahfhvmhOA/DBrVXfgTI6fMgtLug9LNp6z4U/ughWDtHMj4g5jQdWEbJ2YLd6tAqffsAisN9hdY6BnsAdmTqYFaeyU0w5s52vGp1Bm0F2h+i8gXyRXV4kh5cUDe; 25:HzsBpHKH3o5xhZjh2+BEPolkv5rzr8dB/e9OgpAm74qP0w0FFffwcnANjRbHRwA00r7ikPLNYgJ7NG2573rRW/TGzPVouYHnOxtkVi5VCHcGTNnvGIp4wAzmmmvzsuGVymAsnLuRjGIfedbO511jkGMhm7KHqvd67qvuYKF2I9T9Y+nvywRMQpAjLmeOW10HLwIv+vP596X83GVyD/jyeOMnAwuTV2F2W0RC5P2h9KbqJJ6MPBzEdeN4wX6ox+c2Zek0eCtnkf9h8iKuRM2Wpn3ahYh2bdfpplOxxLC21uL4BeonEMMznE8qO7Gpp3Owv9iyFO05Enl1MQDdIn/UnlCWszgeGpiCbQIWiftEqX7D4TxucmizXLicxqiA+a+dzvkEPycKEF0SPRzOhcjfTQ== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:SN2PR0501MB847; X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB847; 31:uOx6QnQ8uUFEryuC8jPwZiOr4DmgsNx6cpuJ2rjEXlMG9lneZT4BsB+2k8bfr0tCuYyyjajHyOmGxajZQGIKVAZgcA11RScXZvy9A+6PTR/0YJljVrhcwYYDNI3Y4wDaZi+xeKwJLnk1V7aT8UCoJYYFAbOexUT+0ind7eUvNlO1VV3gWbRYNuBKTDiCww0F+ylDzQy5NymQXOM/bE1/v1sV9V63zGJqLRPp5GwhoBs=; 20:M+MD3Anzd0u0FhuPJlt3kOEamLFu27x78A9PxYLP7bX9S34QI2/OVEgm9g9WkLbET8ailbA5Y5vnQgefgXKtPCv/WWa2wTF5Zcef/kbdAeoCU+bDxZTk0+RuXSMiV01DJ4yE8VnbIrnVyxB8nCSekHc7CIWO3hS9cSsrf33J5R9tEwedxXlgMRtXgdmeVUgPgdnF3IgpmfC/xPBJxdJzPK8TNiO9KI9Ig/55Aa7HCiGDHFni5QQF6OkNWW2vEtgg 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:mPrIQEJsJMstinAEbkT9WEIVQFBS44h3SUAEBforeeZR1FmUtgXwMqWaTfKr9dNAH4H/kTs97Z6LJqOtpKaO+Q7T7HMDgQpyxNJPySNnwVpn+I2CUtA3ALLpT6gnh6/OOuBYClXXVXJV/gKVI6/XOEQJRbV+uSZN/heKCaWM70pjrtYw1IzoODw/QQsQ6sHsfjEmlPKn+Z3uNn1M/46MPCL1gYxsOsuIr50YqjLt0yFQ2FHB3yqQaqPgxZGByZ/bLZjbQ8zFvlUk7QaPESsu6YDVP2YXUnc50EQTVrVSO2R3mtzEiy9/QKpDSuX1ocEQEgvrzleisIiW4Ly2OflZ7LYvRfoziQrXjnYWViC41LVmjJIvgQkOBUtkxwendPe5slyM3fazZ8iDf1Wc+tN7BADB6fhki+jkqbkLL8G3tt3dHio2oHIGkEGcz861xvoWwM4MjPViMD2zga+jsMGZCVhiBBZSv65+VtzGe2MEZp8= X-Forefront-PRVS: 00235A1EEF X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(199003)(174864002)(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 (protection.outlook.com: vmware.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; SN2PR0501MB847; 23:ziXi9oNAG9YbeDUKw1X6ErNphuFaW6S9OPP0MknR?= =?us-ascii?Q?Yi3CyAjDJURuiSruzdY3IRAiW6CCjb6NnD9lW9xMLm4Sn/2c0sigLBkAKGcL?= =?us-ascii?Q?FfozIUp8qFNvoedLyu3YoPjQ0HNs2L4xL95X7wcSCaXQ+fzKgecr0ptykPil?= =?us-ascii?Q?KggiPoFRvU2JbBzgNOOdaR4KpioaumkG9/p9eykLq8Y6zCVM8IR1e6Igu1TE?= =?us-ascii?Q?NqmhMLk1ekB+jIR4bzrpn7EyfBXGcCI1NngyNikkJ19iXHxJc7cTc60qeJFK?= =?us-ascii?Q?2WP1geUJoKVg46e/jnMC0Y7oDhwcdSD/y7nSuDp97JjytxP5qOaOFjP5gvbx?= =?us-ascii?Q?Y71NRNkZzd01CxLVB68IBuGLIrWcgkGBHNt1gBK1KXUnidSxfen104WJV7+h?= =?us-ascii?Q?h0pGloHANYU05R8B9k+TI+TbHAHCYOV81tXnCEOni9TTeITyxewonBCbhq+j?= =?us-ascii?Q?Gpg12tRqyS+pHlgOLPvVmHCm/qooqwRFtdX+Yz+1arQrGe78JIciF3D85mkZ?= =?us-ascii?Q?UwiIGT1f4zJ7+JX0BOYPOiDZOGvtefM7XTEQ0aDmdTmO8xjZNLTxkyfa1Mj+?= =?us-ascii?Q?YJ9Gx4Rp/MdEAMbtSRxDtKHNpLPUh2FcuTUNA3tf0ocx6bEPLxzeKg9gsnQY?= =?us-ascii?Q?68OEv1Bccfeyv+d48XQ8ox1t3MoZhnFQEi+fjutTxGEP5pYLQiL+qzwzFgSA?= =?us-ascii?Q?S+3mf6u4COByNvbvjYTn3rbEkeqS/hSSBKZsmIpMxD5enslCF//9gNZ1j8zM?= =?us-ascii?Q?is1zpjpHZk5v5S2sDdrXrVIKddWwyGptcS/Rzmk9mJmy4LUneJVlJ+uHN20B?= =?us-ascii?Q?qljB13o3vz7dvK28OENelJ/1mwISUbv3HyoFJe0rj0PqSCXNUfZSn+vRgA+o?= =?us-ascii?Q?NqHhVcIyN6+4Qo6vQeSx9CHCFkfmKRyN5yQV3fTB4fmUCwNj5JVCiFL99kxy?= =?us-ascii?Q?jhCyFpquTghZjQ0c4exfxOVqBLp7u54MGui2c0WTtmtPZlHB8WBFWVCDWbfV?= =?us-ascii?Q?LK9/gC4mEM+1Zo8PuDNTCe5ZAF1QD+MFP47lKBd4KTq1cTWKIKcYmFXC/uI/?= =?us-ascii?Q?VNvPhDHEb9o6N/C57++0uHXHsajcEYmzButpYvc+7IjjwAuTtAERI3aljUyF?= =?us-ascii?Q?ASq8zTsm+GaosaAjIbSkphH07Jk2Kkkf1QjQt+0qq5BajDO65whdl2m4ZVrD?= =?us-ascii?Q?wji4Rt6e1VYV1RsyjI7kR9ZF/XPLl5NCvSXHUEC4dwKWQmce90SZf3rP9K/W?= =?us-ascii?Q?pTpRBA/MjPusJ+Yrzu8=3D?= X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB847; 6:g7gEVzssuu/hvhWK9FvCY+Tvfujq0XYt01EEWqFR4GtPAeFB3KFHawfG5MWgPkI1cJTWfWNJhCH2OsnBnF+qVdhA8rfFKcz0nyQgy9teVVyvKVSROCJB51pCsvkQnup3DRVYgK8amO3IdWeSLhCSir1eIV8+50nHRFHF2AmbJs3zMRghO6asux/ArLK08VhgzJmWRYORI4WkaBUS83nQk236qLjMspWVySUPAI0uV03/Qmf6p7lw+RGmlB1a7k/PKWBkt4bfFVZzmf4EI/lK8XVHrGtTEveFRdo90dc/Dts=; 5:KmOC9na/XE99Uik8CJwEzPCExlrONdy9/7l1QgpLOgmo52rOw+r/Lz/BDy9oA+vJvZ63Rz8R5Zr7WpA8i19DnN6Q72g3UOR4QVPNCWp9oijG+aZNco0vOKu6mUKafnzT57EkJg95A/Dpw/5bLokGwA==; 24:cVyt4lNj1q6yv9a9BXwKajerARwHbbkE8a6SByLH9v1sPl73F5pQrjytcNMi3hhDoorL04BXo7emWLWn5xzPANFHaiw5eOc5zotZtm/CiYE=; 7:dboyIUL5Zv9gC6LTaot78mgm/WAd7/XsvS3v//5A7HHPqkNU0hC3occ5V6ZLzrjoxD5IKY3gWVWb0orxxYh8kLoD70FyRUdWtCt+NAYGpYQO95oCOLo7IpyfxwqEwlOFj22lbxYll5ti87WzRffzX0kx9ThdC9TU7+Jk+agL6ZzcfvOOCRzi6hwC5K3rNet58Pxsw93wUCjRQGiR/Xnhqj3qq743anDOy9yiXBbdiVpcyWA0sRzst0+sE4Kb1o61 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB847; 20:hqymrAva653OG5c3grS4fY8t99dVQW6cz4gxIG2v7gI89b9eTx+a2ab+x+Q9WI6Vo55mqFge6vgH4CW74+s+shOwRBV6nEqjlXrxKxeaE5Q4PXg1EVI8Jw305AlgrUBWNDFW65UUBToQbwJPdgGJpssUV1Y4u/IB1SfwAwAXv/A= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2016 23:28:16.8357 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN2PR0501MB847 X-OriginatorOrg: vmware.com 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 helper functions to store guest page addresses in a page directory structure. The page directory pointer is passed down to the backend which then maps the entire memory for the RDMA object by traversing the directory. We add some more helper functions for converting to/from RDMA stack address handles from/to PVRDMA ones. 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_misc.c | 309 +++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 drivers/infiniband/hw/pvrdma/pvrdma_misc.c diff --git a/drivers/infiniband/hw/pvrdma/pvrdma_misc.c b/drivers/infiniband/hw/pvrdma/pvrdma_misc.c new file mode 100644 index 0000000..97a1fcd --- /dev/null +++ b/drivers/infiniband/hw/pvrdma/pvrdma_misc.c @@ -0,0 +1,309 @@ +/* + * 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 + +#include "pvrdma.h" + +int pvrdma_page_dir_init(struct pvrdma_dev *dev, struct pvrdma_page_dir *pdir, + u64 npages, bool alloc_pages) +{ + u64 i; + + if (npages > PVRDMA_PAGE_DIR_MAX_PAGES) + return -EINVAL; + + memset(pdir, 0, sizeof(*pdir)); + + pdir->dir = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE, + &pdir->dir_dma, GFP_KERNEL); + if (!pdir->dir) + goto err; + + pdir->ntables = PVRDMA_PAGE_DIR_TABLE(npages - 1) + 1; + pdir->tables = kcalloc(pdir->ntables, sizeof(*pdir->tables), + GFP_KERNEL); + if (!pdir->tables) + goto err; + + for (i = 0; i < pdir->ntables; i++) { + u64 *table; + dma_addr_t table_base; + + table = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE, + &table_base, GFP_KERNEL); + if (!table) + goto err; + + pdir->tables[i] = table; + pdir->dir[i] = table_base; + } + + pdir->npages = npages; + + if (alloc_pages) { + pdir->pages = kcalloc(npages, sizeof(*pdir->pages), + GFP_KERNEL); + if (!pdir->pages) + goto err; + + for (i = 0; i < pdir->npages; i++) { + void *page; + dma_addr_t page_dma; + + page = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE, + &page_dma, GFP_KERNEL); + if (!page) + goto err; + + pdir->pages[i] = page; + pvrdma_page_dir_insert_dma(pdir, i, page_dma); + } + } + + return 0; + +err: + pvrdma_page_dir_cleanup(dev, pdir); + + return -ENOMEM; +} + +static u64 *pvrdma_page_dir_table(struct pvrdma_page_dir *pdir, u64 idx) +{ + return pdir->tables[PVRDMA_PAGE_DIR_TABLE(idx)]; +} + +dma_addr_t pvrdma_page_dir_get_dma(struct pvrdma_page_dir *pdir, u64 idx) +{ + return pvrdma_page_dir_table(pdir, idx)[PVRDMA_PAGE_DIR_PAGE(idx)]; +} + +static void pvrdma_page_dir_cleanup_pages(struct pvrdma_dev *dev, + struct pvrdma_page_dir *pdir) +{ + if (pdir->pages) { + u64 i; + + for (i = 0; i < pdir->npages && pdir->pages[i]; i++) { + dma_addr_t page_dma = pvrdma_page_dir_get_dma(pdir, i); + + dma_free_coherent(&dev->pdev->dev, PAGE_SIZE, + pdir->pages[i], page_dma); + } + + kfree(pdir->pages); + } +} + +static void pvrdma_page_dir_cleanup_tables(struct pvrdma_dev *dev, + struct pvrdma_page_dir *pdir) +{ + if (pdir->tables) { + int i; + + pvrdma_page_dir_cleanup_pages(dev, pdir); + + for (i = 0; i < pdir->ntables; i++) { + u64 *table = pdir->tables[i]; + + if (table) + dma_free_coherent(&dev->pdev->dev, PAGE_SIZE, + table, pdir->dir[i]); + } + + kfree(pdir->tables); + } +} + +void pvrdma_page_dir_cleanup(struct pvrdma_dev *dev, + struct pvrdma_page_dir *pdir) +{ + if (pdir->dir) { + pvrdma_page_dir_cleanup_tables(dev, pdir); + dma_free_coherent(&dev->pdev->dev, PAGE_SIZE, + pdir->dir, pdir->dir_dma); + } +} + +int pvrdma_page_dir_insert_dma(struct pvrdma_page_dir *pdir, u64 idx, + dma_addr_t daddr) +{ + u64 *table; + + if (idx >= pdir->npages) + return -EINVAL; + + table = pvrdma_page_dir_table(pdir, idx); + table[PVRDMA_PAGE_DIR_PAGE(idx)] = daddr; + + return 0; +} + +int pvrdma_page_dir_insert_umem(struct pvrdma_page_dir *pdir, + struct ib_umem *umem, u64 offset) +{ + u64 i = offset; + int j, entry; + int ret = 0, len = 0; + struct scatterlist *sg; + + if (offset >= pdir->npages) + return -EINVAL; + + for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) { + len = sg_dma_len(sg) >> PAGE_SHIFT; + for (j = 0; j < len; j++) { + dma_addr_t addr = sg_dma_address(sg) + + umem->page_size * j; + + ret = pvrdma_page_dir_insert_dma(pdir, i, addr); + if (ret) + goto exit; + + i++; + } + } + +exit: + return ret; +} + +int pvrdma_page_dir_insert_page_list(struct pvrdma_page_dir *pdir, + u64 *page_list, + int num_pages) +{ + int i; + int ret; + + if (num_pages > pdir->npages) + return -EINVAL; + + for (i = 0; i < num_pages; i++) { + ret = pvrdma_page_dir_insert_dma(pdir, i, page_list[i]); + if (ret) + return ret; + } + + return 0; +} + +void ib_qp_cap_to_pvrdma(const struct ib_qp_cap *src, struct pvrdma_qp_cap *dst) +{ + dst->max_send_wr = src->max_send_wr; + dst->max_recv_wr = src->max_recv_wr; + dst->max_send_sge = src->max_send_sge; + dst->max_recv_sge = src->max_recv_sge; + dst->max_inline_data = src->max_inline_data; +} + +void pvrdma_qp_cap_to_ib(const struct pvrdma_qp_cap *src, struct ib_qp_cap *dst) +{ + dst->max_send_wr = src->max_send_wr; + dst->max_recv_wr = src->max_recv_wr; + dst->max_send_sge = src->max_send_sge; + dst->max_recv_sge = src->max_recv_sge; + dst->max_inline_data = src->max_inline_data; +} + +void pvrdma_gid_to_ib(const union pvrdma_gid *src, union ib_gid *dst) +{ + BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid)); + memcpy(dst, src, sizeof(*src)); +} + +void ib_gid_to_pvrdma(const union ib_gid *src, union pvrdma_gid *dst) +{ + BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid)); + memcpy(dst, src, sizeof(*src)); +} + +void ib_global_route_to_pvrdma(const struct ib_global_route *src, + struct pvrdma_global_route *dst) +{ + ib_gid_to_pvrdma(&src->dgid, &dst->dgid); + dst->flow_label = src->flow_label; + dst->sgid_index = src->sgid_index; + dst->hop_limit = src->hop_limit; + dst->traffic_class = src->traffic_class; +} + +void pvrdma_global_route_to_ib(const struct pvrdma_global_route *src, + struct ib_global_route *dst) +{ + pvrdma_gid_to_ib(&src->dgid, &dst->dgid); + dst->flow_label = src->flow_label; + dst->sgid_index = src->sgid_index; + dst->hop_limit = src->hop_limit; + dst->traffic_class = src->traffic_class; +} + +void ib_ah_attr_to_pvrdma(const struct ib_ah_attr *src, + struct pvrdma_ah_attr *dst) +{ + ib_global_route_to_pvrdma(&src->grh, &dst->grh); + dst->dlid = src->dlid; + dst->sl = src->sl; + dst->src_path_bits = src->src_path_bits; + dst->static_rate = src->static_rate; + dst->ah_flags = src->ah_flags; + dst->port_num = src->port_num; + memcpy(&dst->dmac, &src->dmac, sizeof(dst->dmac)); +} + +void pvrdma_ah_attr_to_ib(const struct pvrdma_ah_attr *src, + struct ib_ah_attr *dst) +{ + pvrdma_global_route_to_ib(&src->grh, &dst->grh); + dst->dlid = src->dlid; + dst->sl = src->sl; + dst->src_path_bits = src->src_path_bits; + dst->static_rate = src->static_rate; + dst->ah_flags = src->ah_flags; + dst->port_num = src->port_num; + memcpy(&dst->dmac, &src->dmac, sizeof(dst->dmac)); +}