From patchwork Thu Nov 3 23:44:34 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9411629 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 4F69360585 for ; Thu, 3 Nov 2016 23:48:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 354A82AFA0 for ; Thu, 3 Nov 2016 23:48:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2402A2AF9C; Thu, 3 Nov 2016 23:48:47 +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 2486C2AF9C for ; Thu, 3 Nov 2016 23:48:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933452AbcKCXso (ORCPT ); Thu, 3 Nov 2016 19:48:44 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:24608 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933372AbcKCXsn (ORCPT ); Thu, 3 Nov 2016 19:48:43 -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; Thu, 3 Nov 2016 16:48:35 -0700 Received: from EX13-CAS-005.vmware.com (smtp-inbound.vmware.com [10.113.191.55]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id 816CD18519; Thu, 3 Nov 2016 16:48:42 -0700 (PDT) Received: from EX13-MBX-037.vmware.com (10.113.191.78) by EX13-MBX-010.vmware.com (10.113.191.30) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 3 Nov 2016 16:48:30 -0700 Received: from EX13-CAS-001.vmware.com (10.113.191.51) by EX13-MBX-037.vmware.com (10.113.191.78) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 3 Nov 2016 16:48:29 -0700 Received: from NAM02-CY1-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; Thu, 3 Nov 2016 16:48:29 -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=gxQNOFjNRbi5If3oYix0wrNUkuaJme5hQ5ACzs67LY4=; b=ek24mi+c8LJItE8RPyMlKp5r9G0PkxORMAhb+MfeBMNQhxnaZFYTyE0qSaORvOOrBWu9C3EfLnBRG8aXBCv+sLOB5J2dp5z2WTKqj9AJGTl7zUUnZ2CM89dhBnas+79FP0BRMd6WrT1npijRQh6iFiTxbcMheGS0RNpl4FWrDvU= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=aditr@vmware.com; Received: from gandalf.eng.vmware.com (208.91.1.34) by BLUPR0501MB836.namprd05.prod.outlook.com (10.141.251.150) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.707.1; Thu, 3 Nov 2016 23:48:24 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive Subject: [PATCH 5/8] libpvrdma: Add misc verbs functions Date: Thu, 3 Nov 2016 16:44:34 -0700 Message-ID: <1478216677-6150-6-git-send-email-aditr@vmware.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1478216677-6150-1-git-send-email-aditr@vmware.com> References: <1478216677-6150-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: f0674b8b-f611-4956-e2e0-08d40443e707 X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 2:LrLj5acRjlubUwTsDUMT3mUW7uJSB4BlHdmPCERSnG/+7jtN3vZPfOvSJvltbM+aSQIjEC2o2hr/+5q3qP8fKtWJP7LC9313pC/UgBLl+LhDU11pz2KVdjPxS1szCuHbf/JCUTcFHYiznkufyX6CzfiURg7af7bus4nNP8yDXnoCnqyrpx7Zf+MKWU7pC7G8rSoJGx4FTib8WVDhMuv3VA==; 3:Ab0XFpP9sPe0TaGIi2QYq5VBgc10to8CVa58cdKT3km+ZtsPr/LbQ5AjG+J0B5Tqf/vVU+BiIDGYqyX/elvk/iPQ7uNphGxi1dshnV7Bt1o8dwoJCR63FzzueQYcH7D8ALtherdR74OE7v9Xj4ujlA==; 25:UWCN4oapKjQbcqclOb53gUjXByFGBQLPiABlSXjXYL3AmZXpLA+rLGmzkrdq5kIZWf91HDq7JUveKf169db6GmoiK6d+6Dbm0zs0++tsNljaBwul0aHawZSpX/yD43FpmH2+j+PLfWT69YDvcF/mgrDz4IcJ35fwC/wnrJUdImGX0iSq3knRbE7Y3VWJd9eyb8g/PvjSAC4Svfz6K7SH7RWYxz63jeO+pU1YuotIvwmAKQtWYAcfKOrGClGqFquKr3iVulf6V5EklBQrFmkUiC7ORFb1DDS9weoxcNh1vC0/oo48eQE4Bu1Ib0oJzt4gkvXczhBhj73laifLI6i14CxY0sIenbMgmXpcHk0NdPK43pptVWmU6XMr2Hi1Dy/anj/lHw7kEp6wjEfiIWQLSoVi8q72ZayS039ntouBq2H7dzeCp852ceo1Jlm5rR6U 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:72cEXXnRQuWAhK8FF0f2WrOa6QZ4aai725H+c8Y1hxPnktR0D9UPQ9eygjH/3YeY1e0Uwxj0YEix4HHqiKCzkuEZX3rdqys8awsNgIbMGJ/jqYOD8V3RvNv+rJ8jqeZb9QQjrghTaItegfoVmVno22EwIoe4fWZJmCY787cL8F1tKDrUV9tO/zb9jktb+ex2qBZkFqi7fTYsnBFbk02uUc3n2AckNyHiAoGLKYLWdUdGQeOFYqO8A1geb7OCp+o6Ty/jSfw1AjV9LFxGWDoysw==; 20:f6EbmkdFjfc7jwdtTOY7Ow5PU121y8GzOATefAmZkryJ3P79cdhkV9gEhVscjVwdOpx0LNmfVCrCXTbQKVbnn4I5ueutECh3giCEshqBDicH2krh+ACiovCQXK+AY0C8Vs9zSodfZitoOBNtzJ5zUT5pmePjL7yK4ycu5SUnHzQaaIFxtNNBxrTeEZyK3bRn+KQWl/2lSwp0x1KFNxnrqXSAqb0ByXwg4yGyX4KrSYsi6u2sPi9XgO+dAdxowiGus984INxiAMqxFiTIJ7/dazLnj1PdpxB/FgOsKQNwwPCN0JSha86asurvQqwVatPGU2gCQd3YtfNvh7Afio03kn6HV+OI+eezgkqZ1G6ONWHeCtNhQtp9W7iDw8k4NaQxlGK8WAzcypWZ4dC0Ob/eHKddMZlSnUN06yjkijFSbF1YI1BFBzCRWJfMY7abzHiwgBFLN6CKRL5iopB/1rQM5s7CeVmwOvDpijRaogIkKt+GLeWODao+2QRjK1eoQ900 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)(8121501046)(5005006)(3002001)(10201501046); SRVR:BLUPR0501MB836; BCL:0; PCL:0; RULEID:; SRVR:BLUPR0501MB836; X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 4:Z/sD/V4fW7+ccHz542dKCMqxN/q3XXxmVy5eMcLQqNeO3CPksVT3+eMxRYolVs6a4GNbu6ZcKV2Esf0rvwDAJcUueye2aeWe3W68epncH3DxeaDvktwWHtsOeOafIZdkhNC3ko8iz8zHOHlhlt0wkAlfClNiFSmNwKmWPnh7ZLpV2wB1dYG8W2M5zYECS7/DYeNRZkV2L4HzRy72pnlcb1IEc0oG6/4US9P9JBC8tKAWhWg+ATs3QXbquSB35XAbNuZXLnLp/uuWtCJp3USkS7Bnxzo9YM4m7cKLA+29ltaZJxBJWeJz+uSDkhICZQNiAtX3S6IQio+K+H0a5w+HEPA/iesHvLXssnRiWwArk21fCcaH/wajP0y1E3HerW0ha+1Z0PXR6i7CvAok3x9o8/CH5ZtRUAiBgb6xO0irJtlpgJUDMXEmZNzo+fGAAmTp+waqd/ki8zJxiwzGt94RjeNxg55rnRI3506e6586hxlLIljNLVor77qbbf30XBg29C/adtyTze23AzkqsMPDyw== X-Forefront-PRVS: 011579F31F X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(199003)(189002)(15975445007)(8676002)(6116002)(5003940100001)(106356001)(5001770100001)(97736004)(3846002)(586003)(107886002)(19580395003)(19580405001)(81156014)(81166006)(189998001)(66066001)(47776003)(4001430100002)(50226002)(7846002)(92566002)(7736002)(305945005)(575784001)(2201001)(101416001)(6666003)(42186005)(50986999)(6636002)(86362001)(77096005)(6862003)(48376002)(50466002)(105586002)(76176999)(2950100002)(2906002)(4326007)(68736007)(36756003)(5660300001)(229853001)(33646002)(7099028)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR0501MB836; H:gandalf.eng.vmware.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX: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; BLUPR0501MB836; 23:mOUJ+7ufi/5DO8aPZ7420mgT01jQN8RZ0VRrWv86?= =?us-ascii?Q?Lr8KmWLJdFQFLaGB/f7DDdxZrFbt9fpoZ66QRip8Pfq2Th7rtdKMsJwPjqjX?= =?us-ascii?Q?OvtR088Rc+7mPbS34x9oWdHBxDarRm87Yd6bihnkvhoITFIRq1wYmqn23Kbp?= =?us-ascii?Q?IMhb0ybYgYMIzaT9lKPulV1jx0SRbRrhlZq0FIkWfv0zwyRaEdVGwpih7gOF?= =?us-ascii?Q?vvdQcNQeTBIvaCHSlYDNJxmeStkUwlTk3MKYOs1xXWbrKixsELgSdNSKdkka?= =?us-ascii?Q?TW2e8SG9uDM6+0lTxluuDzRegvyRyiQ+Il3CDZB62sHlhsKXsC0sXauWSreb?= =?us-ascii?Q?wUtc0A3tKptq0cPf9H/YgBy1/zLs1FAScUstljIG2f8pgu3zRYepFmPY4D//?= =?us-ascii?Q?3Tgg3FW+kHEqbsw/hCvTNVwcKvdvdUR7WrmmOECEFzhuNYCDl0vI0OsBjI46?= =?us-ascii?Q?MuPWbAUe7VFrC6zLmsLwriGnxfbcFUXWng/GxyDe23ZeQaOn0rndpfaTBP7+?= =?us-ascii?Q?2H1y5XVMViZE4HfO7o5STgoIKTem3gknYjQfld4u5TYFCZjSnpceSdKfmOxT?= =?us-ascii?Q?ICku7EsYKqEK6oC4XuGAMhraQnqNtbQaP4mFB875Tx5BojkTqdUL+bYrnRsO?= =?us-ascii?Q?jAbmNci+vXZ8nrY2nbWVALwkgsAYuMilOpgEjIFulg+CVZmp7ei0pZ2I94gs?= =?us-ascii?Q?UITtolxa3UPc3eh5ypbcyfysw9CmyZwnA+E6RJ6m5W8GXY2eUe7U0d5UxvYO?= =?us-ascii?Q?IiO7WsqZ6JADynlp9cIP7dvz643HMjRLGWXUnuAcEvYAPVDUNBI07vUuL+YP?= =?us-ascii?Q?yxFArDY/kJeWOobJdb43wlXzVaaDmpdVI82KO75eaMUSL2tlCqi/zaT2M9n2?= =?us-ascii?Q?cxLgDo/UsLbX8P74up7TKoX8o9K1asUZw8o97ShMt/j+yKeaJhxvS9789Alp?= =?us-ascii?Q?5DrNyTlgbYEGpZKZkRSeQq0NjbcWchoxqeUwQ0OF/CsuqX9HwywvO7rwnJgG?= =?us-ascii?Q?F/T7+qlj0AhzsP0tA6zP5FtGTWYRjTFMAIwo0anLSEVNS4k6tmS2XuuKt6G2?= =?us-ascii?Q?lY35zxvccVdzeEVZ2PRCqUSaxIbdj1Mw6JCAm2Sz3JgzzMOl4Rcl1mQ4FNI3?= =?us-ascii?Q?rg5i+V3ucKDGAtqXvVY7JRlmCnqtj1mgY1YNRKfk/Ks5sKjTEnkn7IQLKoPH?= =?us-ascii?Q?axrgReyOEIq1iCMX+tPo+CNOPbYd7siPm4Hzq6FAjrzgUjSbuQK3rXiMPbmp?= =?us-ascii?Q?uUTxTd3eS/tBAhg/Dsi66tsIpVwJDXhcJOCtxujj?= X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 6:4m9DWyXxfaVOWS9aMZ1QkL6EeGddG/564CUromHT4wkHqR7G2KHnH5axq/+0hUaU9DBVASYf4M8kuz+9NMZqGNxNk1ngHzMvfofb/N3li6toptlBt41vBqnsBxKu62MmTUKuKgXfn65NZN43IFb7jdcLIbyIknVtrs2mrNOueEBeSEaMkEvB995J/+ZQc5uDy1MjDHW5vhH6BWYbewqE0FHNhgAOVTw4ZYJtzjWhxpyZD/NwDxY5j0R0d/PBR1M43MmT1wxeAdUz2gcgYgmWlxZYevA7igbFIlBw1W26PvBdhKldeVvPHXzvM5c5E2R3; 5:EcgSxrzew2jYKZTgMhg2dl7G1z16GJjlU4T/acaZP47/acXOE+Xavmy0zvvZRwXJfU0/gls4IDvuKEPQRp+ej57zp69t82xyKU253S72mUgLC28Kqu7lSYsA4TPR0xsLuNqGTMpwNG6f4Dys4hYsDyTJZfOw8olyqATrIkJ7s48=; 24:m7gpVwRBhkZoJaCtStMRVjY4yXBOs3lHfv97byZDmr76FcRkgnEKmEApKHuXKTAeoYNVwmAPGRPMMQco+ie2MMJd0uyFdYh58m2j8ynmE+Q= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 7:UXbp5Os1Oi+0Llmn48KPKcCEzybRRwHST0ZAmcVO/hpNZMaAzhL9aVKjoB59RpvCvWwW82Oiel3PS0tDrSXzhGMaXmkptEv7cJXnZ4MNkaLAAtTnorhakBtNDaE1cTjLcYHBOShU7sDoWRzY9s6D6w5MqruByix+auO2NRr8zI4pE3bO88Pruv9AtbFxSsNXR8DBq+NMCKXwxDg9LTfr/2NbewfauKR5hQcxcH1UEB1HxFE1jeZYSHlcD5TA2vum5ODZve/UhAFzO1cnsgOmADUIwWEIaYP3YHutSUDG++XllYD2xtPYQeEovaKLUsRZRzkB2aas2T7E2a7IKL5XXkl4U4Dd3ZeF3/sfwsGantw=; 20:n7n4pMvgdfSgH5BRGcdVcCu8LmVaKZbBMyg6gMfoz0GYK+T3m/Q7IvldWMiT/ybD5WJ+VSefQ91FVYLMp04nVsbl8jkMEZ85bXPAhNo26MZ4wFafvXi8GSJrWn19/tnMU7a+5gmiGG0G+xmSEDgM0XYWPzAlBpz+QCJ6jNcu3Qg= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2016 23:48:24.5611 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR0501MB836 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 includes other verbs functions that dont necessarily fit anywhere else. Signed-off-by: Adit Ranadive --- providers/pvrdma/verbs.c | 234 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 234 insertions(+) create mode 100644 providers/pvrdma/verbs.c diff --git a/providers/pvrdma/verbs.c b/providers/pvrdma/verbs.c new file mode 100644 index 0000000..1646708 --- /dev/null +++ b/providers/pvrdma/verbs.c @@ -0,0 +1,234 @@ +/* + * 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 "pvrdma.h" + +int pvrdma_query_device(struct ibv_context *context, + struct ibv_device_attr *attr) +{ + struct ibv_query_device cmd; + uint64_t raw_fw_ver; + unsigned major, minor, sub_minor; + int ret; + + ret = ibv_cmd_query_device(context, attr, &raw_fw_ver, + &cmd, sizeof(cmd)); + if (ret) + return ret; + + major = (raw_fw_ver >> 32) & 0xffff; + minor = (raw_fw_ver >> 16) & 0xffff; + sub_minor = raw_fw_ver & 0xffff; + + snprintf(attr->fw_ver, sizeof(attr->fw_ver), + "%d.%d.%03d", major, minor, sub_minor); + + return 0; +} + +int pvrdma_query_port(struct ibv_context *context, uint8_t port, + struct ibv_port_attr *attr) +{ + struct ibv_query_port cmd; + + return ibv_cmd_query_port(context, port, attr, &cmd, sizeof(cmd)); +} + +struct ibv_pd *pvrdma_alloc_pd(struct ibv_context *context) +{ + struct ibv_alloc_pd cmd; + struct pvrdma_alloc_pd_resp resp; + struct pvrdma_pd *pd; + + pd = malloc(sizeof(*pd)); + if (!pd) + return NULL; + + if (ibv_cmd_alloc_pd(context, &pd->ibv_pd, &cmd, sizeof(cmd), + &resp.ibv_resp, sizeof(resp))) { + free(pd); + return NULL; + } + + pd->pdn = resp.pdn; + + return &pd->ibv_pd; +} + +int pvrdma_free_pd(struct ibv_pd *pd) +{ + int ret; + + ret = ibv_cmd_dealloc_pd(pd); + if (ret) + return ret; + + free(to_vpd(pd)); + + return 0; +} + +struct ibv_mr *pvrdma_reg_mr(struct ibv_pd *pd, void *addr, size_t length, + int access) +{ + struct ibv_mr *mr; + struct ibv_reg_mr cmd; + struct ibv_reg_mr_resp resp; + int ret; + + mr = malloc(sizeof(*mr)); + if (!mr) + return NULL; + + ret = ibv_cmd_reg_mr(pd, addr, length, (uintptr_t) addr, + access, mr, &cmd, sizeof(cmd), + &resp, sizeof(resp)); + if (ret) { + free(mr); + return NULL; + } + + return mr; +} + +int pvrdma_dereg_mr(struct ibv_mr *mr) +{ + int ret; + + ret = ibv_cmd_dereg_mr(mr); + if (ret) + return ret; + + free(mr); + + return 0; +} + +static int is_multicast_gid(const union ibv_gid *gid) +{ + return gid->raw[0] == 0xff; +} + +static int is_link_local_gid(const union ibv_gid *gid) +{ + uint32_t *hi = (uint32_t *)(gid->raw); + uint32_t *lo = (uint32_t *)(gid->raw + 4); + if (hi[0] == htonl(0xfe800000) && lo[0] == 0) + return 1; + + return 0; +} + +static int is_ipv6_addr_v4mapped(const struct in6_addr *a) +{ + return ((a->s6_addr32[0] | a->s6_addr32[1]) | + (a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL || + /* IPv4 encoded multicast addresses */ + (a->s6_addr32[0] == htonl(0xff0e0000) && + ((a->s6_addr32[1] | + (a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL)); +} + +static void set_mac_from_gid(const union ibv_gid *gid, + __u8 mac[6]) +{ + if (is_link_local_gid(gid)) { + /* + * The MAC is embedded in GID[8-10,13-15] with the + * 7th most significant bit inverted. + */ + memcpy(mac, gid->raw + 8, 3); + memcpy(mac + 3, gid->raw + 13, 3); + mac[0] ^= 2; + } +} + +struct ibv_ah *pvrdma_create_ah(struct ibv_pd *pd, + struct ibv_ah_attr *attr) +{ + struct pvrdma_ah *ah; + struct pvrdma_av *av; + struct ibv_port_attr port_attr; + + if (!attr->is_global) + return NULL; + + if (ibv_query_port(pd->context, attr->port_num, &port_attr)) + return NULL; + + if (port_attr.link_layer == IBV_LINK_LAYER_UNSPECIFIED || + port_attr.link_layer == IBV_LINK_LAYER_INFINIBAND) + return NULL; + + if (port_attr.link_layer == IBV_LINK_LAYER_ETHERNET && + (!is_link_local_gid(&attr->grh.dgid) && + !is_multicast_gid(&attr->grh.dgid) && + !is_ipv6_addr_v4mapped((struct in6_addr *)attr->grh.dgid.raw))) + return NULL; + + ah = calloc(1, sizeof(*ah)); + if (!ah) + return NULL; + + av = &ah->av; + av->port_pd = to_vpd(pd)->pdn | (attr->port_num << 24); + av->src_path_bits = attr->src_path_bits; + av->src_path_bits |= 0x80; + av->gid_index = attr->grh.sgid_index; + av->hop_limit = attr->grh.hop_limit; + av->sl_tclass_flowlabel = (attr->grh.traffic_class << 20) | + attr->grh.flow_label; + memcpy(av->dgid, attr->grh.dgid.raw, 16); + set_mac_from_gid(&attr->grh.dgid, av->dmac); + + return &ah->ibv_ah; +} + +int pvrdma_destroy_ah(struct ibv_ah *ah) +{ + free(to_vah(ah)); + + return 0; +}