From patchwork Mon Sep 12 04:49:17 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9325673 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 48D876048B for ; Mon, 12 Sep 2016 04:50:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3E9BF28ACC for ; Mon, 12 Sep 2016 04:50:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 31C4E28B06; Mon, 12 Sep 2016 04:50:48 +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 1EE0A28ACC for ; Mon, 12 Sep 2016 04:50:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932556AbcILEun (ORCPT ); Mon, 12 Sep 2016 00:50:43 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:38044 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932341AbcILEui (ORCPT ); Mon, 12 Sep 2016 00:50:38 -0400 Received: from sc9-mailhost2.vmware.com (10.113.161.72) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Sun, 11 Sep 2016 21:50:23 -0700 Received: from EX13-CAS-005.vmware.com (smtp-inbound.vmware.com [10.113.191.55]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 3FB46B04DB; Sun, 11 Sep 2016 21:50:34 -0700 (PDT) Received: from EX13-CAS-001.vmware.com (10.113.191.51) by EX13-MBX-016.vmware.com (10.113.191.36) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Sun, 11 Sep 2016 21:50:33 -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; Sun, 11 Sep 2016 21:50:33 -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=AZzULHvJ60R8pEjtk2n+bUVHADOR2AJknl9yOExchbs=; b=YV1ZfWHMS259Q7IoF/u8tLbORfrgcGIWGE0pDliqYkXvLOhQzk1EfkPAIZiKqPlkcjsGrKR/SFyrO7VdssztMOWxxAieEz+BNvE5aM3N8b7Jdx+K8SkxiC6kxV7xzoiWEzg6l+lGDTPk+lUjq3ZuJpPxpNEE3s5jrKL7v45rfb4= 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:28 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , , , Subject: [PATCH v4 07/16] IB/pvrdma: Add helper functions Date: Sun, 11 Sep 2016 21:49:17 -0700 Message-ID: <1473655766-31628-8-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: 8f54365f-efcc-481c-7fea-08d3dac8525a X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 2:QEXTwRqFKXSgSQuIKCGyB3/AfCfxTyG2D6JmLK0OyMYGZ+SFQdY7XEpeM+raWOxObIspJ3/bUAHlLl4HJacrzzR/L0+ah1It9CfluG/11Tufo5TKx4ui3CcEqLASIoxeswS50woMRNj+dTp4H1ZzWpDzde2iP6RUSv4CT1+uDG38ENckEBB5jewSjSjNdrdl; 3:qsym1O7JdzVREqX4YZvJ2MX+StZFHu8v9bGoeyNZR8BF+nFOaVZKFfHOK2dnSgAB+FPDJ+6VkJzx1ovELjsK2i6l3CedC10nzl7ltXCn58DS7pL6O99Iyz7SCKPHSlDp; 25:NasxAAHE02z4BaK4SLcwBedft6XlB9sirehqOdtW4qXkq0Lz51mC1s3+RfWOxSzHDywV+ifUTyyaeYIEgaVxkI+56Hp4ymn3qqCbjYYRkwDexVI+IgSxmrKO1KLkpoYU6fNZL+4ajBKjRKtDQzJRVs300U6D7Ei8cM1y8Vfhcf7QP2ZGVkxtqdcsiqwprllrlbDQhFrEl+0EsDCOEiMlVui7vPG8A49fenMlOPerVuI/z5fqSRYp9q4+HByGhDKdaMsylJ7M3rc8yX9/UQy5W2GNFi76wtO0tgvYRxactEZk1dvKEJoXTVjQLKV3m0pFbQCFIbOUuT/Jo++nTy0e75Zd+JCq8P4IaDH0SbaRZ/doIXXlPoCm48sJnnrslBcsIMONNKmzUPU6uh6WKMpKZeRhGku1mqSnyUGIEmQyEL0= 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:o+RQi1CfLSzwIHWxeNbZBEGjQw0s+AWTksO5zdeqW+01UiEL0TASLxkj6L2oWLoD4I3aaIc+4iC7qxMSHbZVwYqwV9CkqhENUqIPelkJ0eIv73X2cVNjFwhw+zlhqYGS6LWc2EdcwQEvLXQKmrmJyz85kEsoyflNJ5nJvp7Xj66mQiynOQxFxVpuP57WWefB8VfMm1Pl120+46C9gJbBHEWWxjTlV3mYhJYsNvsZhYU=; 20:LxR0LyRKca7CpYjypGeZnXHZ6+3xULnz/Jvn4yAL+i4iDamMY/KMBFCi1E2pLmRXQYzRv+W6pQEn7n/QgvVH1byzZhVy7t+MttmOJ6dXz8PZ9Tig3J9ZgRx9Sl/jm1SsgPR/AEM+UpqarqJDdBkd4Dp3i0SMl+ZS9BqgToVEwot1jNrE09hcmRVctGahhw76jTC1lUPY8KsNHOaaJf/fXfNnhR9dm934PSls8W5JNePC0DHPg3hwTX2tB7+Fz+jc9k41sIl2QStkS6FDZii3dWOlgc3Mkp9MgCalLs6VM9AtLznmJ9tY+DUEXyBNwmCEmnDJ5T2Ea9jtaQg96MS2ZFxQ6ILS958a2V86oSqwm7atBlih1ZOLV31PfdWOPilofQncdBXbh5THAbhUn2Ee5YOngXpEImYNTC2ZA1u9dt0k5GTI0sC8fUfs/vqYxpaX0861MCG/NdA5Lwpr5QZ7++zZAq/pbOOOgJMxiO3WHEtheGYXk3aj4/oCzuUOrdyC 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:C/zuQIBsUsN3CPNQZxsYN0DqfjePjqPvYhM7kWtGPSUSfH7AlZjen3eEPrr/M0uo6B/QBquewqkQFSbwrL0myReODDJrFvSRLMCOHU6+8jFNZLXbBpJvrwIwsX3oBm7AR1dyaijWCRObIdR56FjGF5zuUSo98RN1krp9UuB6tOlGj7OaqNFsdrugajOLBv+ydiKVZggduZieulCcxCDt9zbhFHJiYmfDoTUotvJN4So64di5FTbi18NLRGiVFY9BedXmW8ZLh27S54FxdKoNL+s0g0dWSPT1AbZSYClPS0e9/EJsXm1ppay8XwePs7ezwEvdFs5idGXqRa0sBuzYF6xuEy7lLXKQXc3iqeHE5ISrkqDBv7Rz1PIDKMBUBFjevtLhuBJ7B2cUxyaz83DGhSSJsDAZdLB5HkVxs4VkikUwKdxKwZag7b45RrCQBe3CLPufzzMv8dYn6TDTsNZbLd5PFoXgHQsdPFIuHpSvirDgU964vMAsY7zHFkqMZBZ2 X-Forefront-PRVS: 006339698F X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(174864002)(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:Rcl62IA4hTyztrq606QkSB1PrvvoFVEt76y6ATwb?= =?us-ascii?Q?HhM7p2NKJPAuKNXTIvQj9NXgSE6p2s8yLZ16iNyRYkTdIfd6wW9DjHeAyv3H?= =?us-ascii?Q?VMRWrnFHE6O5Nv3cKse27pjKQWPkoN9N18F4lv9hXBb85iWgJbx0taIT7yim?= =?us-asc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PM X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 20:yqJWt5ulNaktr+qLYpwr/MskkfoYev5BUjWb1lCKHV33XfnNNru3N0EUFcFVWacjfnJjLVLPZppgMCBJhvNh/Eh2E8T6HTNotfFlaLuSx61tTLLENU7j/o16T2i1rcevVcKVBQplyMN67vpBCt5E0sqcG7ZEpjoibOOFdKQsFk8= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Sep 2016 04:50:28.7239 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR0501MB836 Received-SPF: None (EX13-EDG-OU-001.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 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 Reviewed-by: Yuval Shaia --- Changes v3->v4: - Updated conversion functions to func_name(dst, src) format. - Removed unneeded local variables. --- drivers/infiniband/hw/pvrdma/pvrdma_misc.c | 303 +++++++++++++++++++++++++++++ 1 file changed, 303 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..1f12cd6 --- /dev/null +++ b/drivers/infiniband/hw/pvrdma/pvrdma_misc.c @@ -0,0 +1,303 @@ +/* + * 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++) { + pdir->tables[i] = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE, + &pdir->dir[i], GFP_KERNEL); + if (!pdir->tables[i]) + goto err; + } + + 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++) { + dma_addr_t page_dma; + + pdir->pages[i] = dma_alloc_coherent(&dev->pdev->dev, + PAGE_SIZE, + &page_dma, + GFP_KERNEL); + if (!pdir->pages[i]) + goto err; + + 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 pvrdma_qp_cap_to_ib(struct ib_qp_cap *dst, const struct pvrdma_qp_cap *src) +{ + 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 ib_qp_cap_to_pvrdma(struct pvrdma_qp_cap *dst, const struct ib_qp_cap *src) +{ + 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(union ib_gid *dst, const union pvrdma_gid *src) +{ + BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid)); + memcpy(dst, src, sizeof(*src)); +} + +void ib_gid_to_pvrdma(union pvrdma_gid *dst, const union ib_gid *src) +{ + BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid)); + memcpy(dst, src, sizeof(*src)); +} + +void pvrdma_global_route_to_ib(struct ib_global_route *dst, + const struct pvrdma_global_route *src) +{ + pvrdma_gid_to_ib(&dst->dgid, &src->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_global_route_to_pvrdma(struct pvrdma_global_route *dst, + const struct ib_global_route *src) +{ + ib_gid_to_pvrdma(&dst->dgid, &src->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_ah_attr_to_ib(struct ib_ah_attr *dst, + const struct pvrdma_ah_attr *src) +{ + pvrdma_global_route_to_ib(&dst->grh, &src->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 ib_ah_attr_to_pvrdma(struct pvrdma_ah_attr *dst, + const struct ib_ah_attr *src) +{ + ib_global_route_to_pvrdma(&dst->grh, &src->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)); +}