From patchwork Wed Jul 6 06:14:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9215483 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 B83DC60752 for ; Wed, 6 Jul 2016 06:23:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A6E1B286EE for ; Wed, 6 Jul 2016 06:23:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 97A57286E0; Wed, 6 Jul 2016 06:23:36 +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 86411286E0 for ; Wed, 6 Jul 2016 06:23:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751363AbcGFGXd (ORCPT ); Wed, 6 Jul 2016 02:23:33 -0400 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]:37472 "EHLO smtp-outbound-1.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751149AbcGFGXb (ORCPT ); Wed, 6 Jul 2016 02:23:31 -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 6D5F89849D; Tue, 5 Jul 2016 23:15:26 -0700 (PDT) Received: from EX13-CAS-006.vmware.com (ex13-cas-006.vmware.com [10.113.191.56]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 9B167407BA; Tue, 5 Jul 2016 23:15:26 -0700 (PDT) Received: from EX13-CAS-003.vmware.com (10.113.191.53) by EX13-MBX-013.vmware.com (10.113.191.33) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Tue, 5 Jul 2016 23:15:26 -0700 Received: from NAM03-DM3-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; 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=HrkLuZEIJ6rwjElc7725rgW9kzwV9U7n6WM/xiTRI/I=; b=GtKvjpHh4pNDaEFFnbJLRcD/Fl3iVoYIKt9guN2K5UKrmKtcrexqBy7ZrbfXQDdj8qbDgKYCfMysMoE5hVat/VeOlD2YI/Ve4YRTInax+33O7sdNqmLkVDxURBtq4LSQ1F36oJcyB6M9D6Na4XZyjBOyzbiLNb6gbYuy0UEs2l8= 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:20 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , Subject: [PATCH v1 08/15] IB/pvrdma: Add helper functions Date: Tue, 5 Jul 2016 23:14:41 -0700 Message-ID: <1467785688-23229-9-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: 2e79d5dd-bc50-4314-d7a7-08d3a564e907 X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB848; 2:tK/WV0rQ1L8LWzf3I93Ki8q99r69D4wxYTf0cABYFAnJ8Mfebva0ilAMEC7/2KBTOxhQjyqkWIv5RDOshNREUOx1vSZY3JKmJRIMbiVGDVQGpvrbvzMh7z/dnHdwbyjnkGN3T2A+O6F5wWnmfCFtrXY2B8ZkSEpPLZz3RvO91dP0SXYXED5ir3KVwld9iWG3; 3:5YhJBeZDeCInSgnMRKu0XquxIjVLPntHk71G7xC0PycmkXG1Rs1jq94c0AudUGbeUxlRwtJYQNZ8tAQufq8SkG92MwhTtc8i8iV/9DtJ4eF5YIIEbJiYwdlDlsY+kWgG X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:SN2PR0501MB848; X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB848; 25:FshABb/Sp501/hD+7NrLkhNeT11AgfKToJK7sdVTN4MBgU1X+l02+Dhg3Ro1Q5vvIObvJegErT2WLGQjs8fvHcNkcZ0ooWunzdIKOQQHZjrGgRcVAlItwzt9swvsFdTBgnDW6Rg0p1dLN2WgR6j53ZaH8lml9fgfuxDNq01KvGwniTw4wIl9kvsA2xqDW/Mx+6eYkeY3TKKKK/qATbJMmEmqBMjOFAOukhEMWHu7g51l8XUpk+UnvTLOHR1JAL6wrIslHddgEZwAu3DJv9EIb1hEnwSJoV+EZspX0VftfzVfPTRHpjn4y2O3cRrWemil0ZIgMOsD3Zo8IV37kia2/s7Ri9rLwYqvfHj8SQze/BjcG+5AdqhN81Kzx4SfOArVdhowc/8ANdOKEX1xjK4Z6t3y0mZm5HUEl06uxHNk3NisnqMGCfLK5yrIJdgPDBjofd2bNLdtR4UKgfqZIRsXrA41uZFxx/lj/GWtnyFN1Q0MZc1g7kgUKQWi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U2ekDFFCcvrTnpoYCCGVVD5M6DyVDNgkkLHn7vpEUrOFxa7j0t6XgNyPapmjfRJ59s/UZyjhEv3fnhoiI8Erz1lp2mYusUCEXjww4T8cSly0ptL8tuT8+cOENRY66QACtxcfo7JCIhuucNroxcofJy1sH3wdq0sXvd1uVI+E2xr58UGSe/cDvzFY2Wk7x24AcEpoJ4GVijGd 5IESnZH0r495g2Ngvjgqlk8i4Ar7EaFCO 8= 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)(174864002)(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 (protection.outlook.com: vmware.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; SN2PR0501MB848; 23:wIEAFhmohdy45bZBZxT4eWzsyjBoBePV2YEhFcQL?= =?us-ascii?Q?g1N4pYJjlMBi5zt8Jp54v02TA8wUul8726D4kccr86vbYL4U+8XBxw5LRgCx?= =?us-ascii?Q?cA/D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A8sd/e3RrxCZ6lTSTTt4CpUOP/1JpCb43OQ6CZI8i+cKWStHx2mLIROz0Fx0LTSxCJ0AS/RHDRJG7CbkrV0/FKmdaCIYVlH71V668Qa7Ngz44NzOW2c5ENvXrjA68NieH86UeebcZAD5SanLUl8t1cQ0ThvuvLjPL/ SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB848; 20:B4+2F/ZdPQKANatwYBMHM/SxrdN302pr6xHtow8hHA1oTxWZzYCT6v6y5ySNsLduW8OKIhlkdS1DH8eR/3v6yG1uRusWDxlGqYglW48E08yCxjOcNLl73vO8FQzjcNWm+rCdS9MfXmCs5eTFnPx0sqKl4LG3aVBzEwQNRoMK8WM= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jul 2016 06:15:20.6233 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN2PR0501MB848 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)); +}