From patchwork Thu Nov 3 23:44: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: 9411631 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 D8F6260585 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 CA2692AF9C for ; Thu, 3 Nov 2016 23:48:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BF0222AFA0; 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 12C112AFBF for ; Thu, 3 Nov 2016 23:48:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933453AbcKCXsp (ORCPT ); Thu, 3 Nov 2016 19:48:45 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:4095 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933384AbcKCXsn (ORCPT ); Thu, 3 Nov 2016 19:48:43 -0400 Received: from sc9-mailhost1.vmware.com (10.113.161.71) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Thu, 3 Nov 2016 16:48:26 -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 8E758184B9; Thu, 3 Nov 2016 16:48:42 -0700 (PDT) Received: from EX13-MBX-038.vmware.com (10.113.191.189) 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-038.vmware.com (10.113.191.189) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 3 Nov 2016 16:48:30 -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:30 -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=dEcGeS+QCiFt/nAYhxZWAflQs4h6F+6wd8iiSEW8nvc=; b=GvGU4XrYCv1apKzmv36M1zRcqXgS94AgwYEoWv33GyysYtDW8B9JXfYiDnxHUCVOIAlJKy8IIuGx2tFgo+BBqxQzIZAgu7E6xD8769LOXWejr8Xhwad8KKEF0v22UP3ECMRWJTJKONAepnK/1KK4UmGGASjrK2Z7L+r8ehjbMc8= 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:25 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive Subject: [PATCH 6/8] libpvrdma: Add main library file Date: Thu, 3 Nov 2016 16:44:35 -0700 Message-ID: <1478216677-6150-7-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: 9398ab43-a9c1-4905-c4ac-08d40443e793 X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 2:Cl1YKzZ89P8PKfThTDDmULSeWSTPM/V8g/7JCV11hMqbzD9MAZGP8Z3QXsXPNu7G8egcuo4sx7TKEQAbS+Sn5Td+RyU0b1kFFv8l8dddnSECD/SU7HK9E2rLv3TBoXo8MclT0vpfC/7/8ZrT3413HJ88DOaeKopkjLYKkhHTlHFQ9vbY11YUq9OPKYNqpzc5SPmDy3cml2IpieWJrbhZBg==; 3:lqfEh/gHC606X4Gn2LZgwYn1OMBIssSOzPt5GP5qiIABl0JrFh1PH5rlSpOaJn0Q/3Mw9NOcPDNYG+7INoa5ljp8TCdBvJYB2Pivd/5A/zm98Z04rmWu1haQ87HRT6oBCRrgc9aMCwwQtsKwMZ906g==; 25:pFUuJCDsuPKFUk+2g3CHUflKagHjcZN9Na0Vax60zyfmDgaiZPMNTJL+I+VT38A8Z1RuFkw7JVcFxmxEH2ox0ne3HEBgaEg5s0sbFVTSCLuPHpgpTrC4iS/IXVQ9kkRG8SBSZF1tkIbifqL6PBmNm3mp4XVYlmvzi36PWX2eAqHTn8IBKxoI3tmdWAapKL9IVhgmKdNI2/mJgR7E6qHeRUGPIndMPr4J5uhzmqJqlNffjVkoipjFgGxcQMLdBsjb2f5o/VTCE2w/NaTqjd7JE2XEeITHnZnwuzwrS2l7LoPsnkTtuPN0Ch/ueHjTVuBMSiAjPGlbySNLfK3KkMi299PeIxX+/YWwVwDH168Ofj14joG1RCbo14Td4t6kRNCiQYM7Q0w1On7cbHAftcv+VydRFOg5gTyMbnnN4xCu+EIlEQGmcoYyCnFfo583SEvJ 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:FfhArQhwZ2D/AWfqOgi6fTI51+8bDCrV/bQTm4v4lK41nHUE2BxDbeggbTd2I8Qv3JEchQHywF2LrJm2Kf3gwsSMTAhKgcZxCUc1J8iB9nRSvyjqGmCQ5KVEKZjQMkkNNG4RP2saYM/Sb8DUsOto6a1wj/7h+IR/FTyLP3wpCDspouFSGc7tqyaP5AbI42O36I+ok64AIsT4+nQdpf1YwBS5FSTd4eo5Gdy9P14ScA+qW8FeSr3C8TzBhObM+4MZ0ino1pStztAGM9l8Mr8arA==; 20:R9twTMBSeihvrjk06zZz3q7Rg2zsqW2AI86EefQNWQd7j3QOs5OjslvRxZfge1DFtXvbzDuq7Exk5V1jzclhP8LjQ1eUJdxzc40SRpnvmBvvNLxFTenFj1SWM+ZF5ffywcvP3kUG9rb6VSMGnr7A/v0cH8PP5SYPOGBy1pQ4trSWSmLlIgxUFt+FCu+L+C56QYynkUBw2Q6xYLPcA5BfFi/1E8KiDlDUiE1TumVo6NZqf7g0cap0WrVNFBXpud+QjA4xP8OcziqftFn55bH/uh41tjVBFGZu/B/CmHpBELxHtW2hyxlhR/qJDLCyHBf/B+rxm0I7XRhdxppsZPvA0LkeQNJKIpBoC3opIGp35D/LDIsV4Ksos1LaJHoBz8PLw/j+4hxjJjBnHL9WhrdB6sbwuo/V7hV/zeZ0bSk5umv49WR+XERTkLrMexRHg+UR47yskjiNycTcRR5LQ6qdH2r8tNJI0G5V9w6j/+tGzZ6UFZgnU9cf08px4P5l2Iqm 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:6giYXn6h3F5jlIpRAYgHVAJEfRhZhyjDgjC9lP6LoGs0hFAgDq/yrE/NC2YSQNV7qVmAB539DmUdKUJQOzY3ok35w8aENEZXeKTBKwUmZ0CM6kZAGpZjTYCbJR6xiY2l4MfAoj1fB7aB2CI+xmSmZxPQALT+EYWav8Hu+5/8FgnD03y8b2AQXDrC0gHFMc2hSnk5i5Xk7dkRQiK878WJpiQ0uT/2nxcmvvLPUx+gSkqSKHqZq+Rs+gA3JcVk6CBC8ggdNrbq14Lg0cTej4vXvUoXQt7CnNdMlu/e/5c9eZQsGS1DknBZTrFNJhAfcgPcAOMq3KdRAQUHxbCXHB7SosH+GYwjJK/SEMmCoOeeUG+oTTbe5Y/4X2dw6oLgM7MiZLQA94jsf3JOqbrqEIkOjR8Q+GXqikiGgLwtFESy8A8qkBwVE4Z3lmh5Hs+KmVBDvadSNHdB0dUpQ5VCM0iGVMKL1kxxH8i7+3OTDq2CkKOPRL+uZaYe/Wmv6Vc8WPAtS971YN3uVuJ8LSzWUxa8Pw== 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)(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-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; BLUPR0501MB836; 23:CylgArdXHcmuUqe9fit5ndymDF2IeNdJYFy+tpTe?= =?us-ascii?Q?y18Ej/n9GY0b1AyU2YnwRm+lgJwnjveeu7IXop0kim4foRjRLgA8jo9f4bkE?= =?us-ascii?Q?hicro1ekhVseiOXzBQ/aMYQp3Eo6i13TTh9Ze8SrG0HTxZrQdEpNt1e0At1o?= =?us-ascii?Q?lBSQxJscHIDp4hu02LV8nhcE5j7sp5aH0RAbLYBQLEDnVCU56SOTfnizwi4S?= =?us-ascii?Q?zyPZDphRF5EGjMyji+ehIHkMElUw0fG0IgqDwff5G5gHXkAFnDDFsG1djSZO?= =?us-ascii?Q?/CSQqPHFSdWAuYjNdiqU9APiBOWD7QnQAvTxF0eEQXi17zfcYXtnRyIgP+nX?= =?us-ascii?Q?MQ1nCmUvjArjSh8eFRv2keKUihAPdM/1NH9y2sOEXwz7qNVpSccQU0MM+C3z?= =?us-ascii?Q?+LkAbn25Z9Tc6EHaX3kr3Qamd1TkylH4zSu5GIPKp7zDMjl7La/qhD1iU9+Y?= =?us-ascii?Q?/wrZeQ8Ugibk6z06hi+feTiDpVZHqISsyjFE1G/zkAy6VzkJOfFFmugahag3?= =?us-ascii?Q?scNXMBljxW5OuV3QIM24v0QWo9P5hqrUgeVmJpPZbbzIHGu8f78zyOcrKMGI?= =?us-ascii?Q?QVc46ezxGqoMuJjw+/wfzi4kzRm9B7hkZzJvC9By4EIsrlpwn/rH5VJSXrO8?= =?us-ascii?Q?B+P8ArT0X/JIDv+J7mYi/MaSPBjDavsyfDsjsRqyX7nRu/nHaLBB3EcBIwcu?= =?us-ascii?Q?zD5bQiKY4GKt6/Bberb0GkP17kCFxilYQsf7qR+cP8aEmvKFiA7vtyXhYRzi?= =?us-ascii?Q?u+Q1fiZgBPmVzX1Z7XLfVXF6T+9gRs/nXVxgw2XjVjCJBChECB2CBisx2lvT?= =?us-ascii?Q?unifMZSsTAL0BL8MIzDFR8vgsTJK1T1cjuTo1IPWkYZ3pQ6QCwLLHL9D3l0e?= =?us-ascii?Q?+5c/S+cjaR5wO/J1nJ9dgFLBB1D9tKYhj0eXhQyisFTO0OsccOW+RR5oajfx?= =?us-ascii?Q?xPI6+5Ev5sktAKDua/M/i6W6H+ub1mb8pA0KydRHEPh4m7/AwOiL3vJvnqq7?= =?us-ascii?Q?Q9TosnMIdnpLOfJ/e8YUbyabfmAm2ILl8f5RUHA6/po6/9dPylLozVpr6GDg?= =?us-ascii?Q?mxuz+SHSmZkBfoZNn2lw+EzowTa7Hedqci3mvrmnc3US5TlgoI2zSIns4rU7?= =?us-ascii?Q?djtRrCwsdgKB9oMrwukoYYuS/NhGJfIJ7YPQkxcIj0fP99dLDhcLM3fLs8Pd?= =?us-ascii?Q?R5yFVJQEySPoaWUsV3+OV+7iP9AWmfn9eN/m5ECoHwS2WqKMbYw/xZ++a3PB?= =?us-ascii?Q?6C6ARBQ/dp5KVD/TYhE=3D?= X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 6:g62kkubiM3FsteSpiqOjJJV6mlyJIUnkCBYwQ+sYbmFwF5xbEJe2NGAIPVPr7/UmaznQpsSvLqAuOVUL6M67S1L46b6Rk2xn07zpSNCghq0EQgLqOgXfc+9TOUfwUROujleIKWISqFuDYUN5xUKLwwEzweG0T3569jvP6x48g/l03KPyHLK/o/k05hZUSU1NLCH4fXc3FSgh6wv44eojtTEjS9TalVEwT436ERYlI2287nqK47fag/xF7XOLGag2YBmDifejawloIW19xvahxdLg+WrpOCl8UPLTLuKbByLThwufPSCof69gb/c7DIZg; 5:ZxGr+8Vh+X6lmr6jjaSZybFIwOHw2eNUzjVmoyI1uwV/rMUHwOfPr/EwVywZs2OZCx3QdVaAHHFrDtM7LJcq+JxBmp4kIN7P9VLgDcMXkINwnuOHBOx7n/jiJJv5jRkG3ZFw3lYd3rEUsBUWc2JU/IobFQ+YW118Ymzc7ozRwjc=; 24:yByLVKxEYr2uokotBRxwDmv2UMVo46IWyBzs/TNRLXEzxFI7fW1xUlUuQVNiO25mxtBWb3XSi1KsVvM256cB1LCZ7Lt1tf2RRzOotRRxXLg= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 7:yYune4vQvDQgSHUesNeI74D3oTXVjYoDKuzJVkqDwzktYWHzrcjZyY3M436+I+w+9Cwqwwql6/A2Puc3HT9YrKrKRHoT27J4PTTOuMZeAyKYWAbMl3k91tGJJrJUcfWi4JTZVQuWaFsL2zNZAZtM9GdaUqITxC3lY416kpEUDe85V5U2I/ck2eScxrojRoJFlbT/pNU8txt8qFZgKTlMZj7b4W9mR1gi2MHPBSd2ShfDIvdUpBbYso3mU2M90KN6VwHwedA4zKfMrUHPkfkRiqjmeufNln6Q+1AH5Ie5u/ETwpKfLOhGvRFC8gJ99xObvWE4dCpMcgKoT0i0ioEm10wv11QsAr8PxKZKDMshAjw=; 20:q0A0QHGwpwYsU04+STbsC7QGDSo4Eakse4CRAm31+TvFlH3+tWRniRYivX/Ws2ytq4cFEdyiMZ3UOUR8cx8QHtWz1okxIRK+qiqvNhj1QUTpq4nHj9FU5TIqnm0exFEEYAqiYzngx8qCMRv3Vx1DPX3EQJ8IcotciW0seXcNNj8= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2016 23:48:25.4778 (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 Registers the pvrdma library with libibverbs and allocates the user context. Signed-off-by: Adit Ranadive --- providers/pvrdma/pvrdma_main.c | 214 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 providers/pvrdma/pvrdma_main.c diff --git a/providers/pvrdma/pvrdma_main.c b/providers/pvrdma/pvrdma_main.c new file mode 100644 index 0000000..909cf1e --- /dev/null +++ b/providers/pvrdma/pvrdma_main.c @@ -0,0 +1,214 @@ +/* + * 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" + +static struct ibv_context_ops pvrdma_ctx_ops = { + .query_device = pvrdma_query_device, + .query_port = pvrdma_query_port, + .alloc_pd = pvrdma_alloc_pd, + .dealloc_pd = pvrdma_free_pd, + + .reg_mr = pvrdma_reg_mr, + .dereg_mr = pvrdma_dereg_mr, + .create_cq = pvrdma_create_cq, + .poll_cq = pvrdma_poll_cq, + .req_notify_cq = pvrdma_req_notify_cq, + .destroy_cq = pvrdma_destroy_cq, + + .create_qp = pvrdma_create_qp, + .query_qp = pvrdma_query_qp, + .modify_qp = pvrdma_modify_qp, + .destroy_qp = pvrdma_destroy_qp, + + .post_send = pvrdma_post_send, + .post_recv = pvrdma_post_recv, + .create_ah = pvrdma_create_ah, + .destroy_ah = pvrdma_destroy_ah, +}; + +int pvrdma_alloc_buf(struct pvrdma_buf *buf, size_t size, int page_size) +{ + int ret; + + buf->length = align(size, page_size); + buf->buf = mmap(NULL, buf->length, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (buf->buf == MAP_FAILED) + return errno; + + ret = ibv_dontfork_range(buf->buf, size); + if (ret) + munmap(buf->buf, buf->length); + + return ret; +} + +void pvrdma_free_buf(struct pvrdma_buf *buf) +{ + ibv_dofork_range(buf->buf, buf->length); + munmap(buf->buf, buf->length); +} + +static int pvrdma_init_context_shared(struct pvrdma_context *context, + struct ibv_device *ibdev, + int cmd_fd) +{ + struct ibv_get_context cmd; + struct pvrdma_alloc_ucontext_resp resp; + + context->ibv_ctx.cmd_fd = cmd_fd; + if (ibv_cmd_get_context(&context->ibv_ctx, &cmd, sizeof(cmd), + &resp.ibv_resp, sizeof(resp))) + return errno; + + context->qp_tbl = calloc(resp.qp_tab_size & 0xFFFF, + sizeof(struct pvrdma_qp *)); + if (!context->qp_tbl) + return -ENOMEM; + + context->uar = mmap(NULL, to_vdev(ibdev)->page_size, PROT_WRITE, + MAP_SHARED, cmd_fd, 0); + if (context->uar == MAP_FAILED) { + free(context->qp_tbl); + return errno; + } + + pthread_spin_init(&context->uar_lock, PTHREAD_PROCESS_PRIVATE); + context->ibv_ctx.ops = pvrdma_ctx_ops; + + return 0; +} + +static void pvrdma_free_context_shared(struct pvrdma_context *context, + struct pvrdma_device *dev) +{ + munmap(context->uar, dev->page_size); + free(context->qp_tbl); +} + +static struct ibv_context *pvrdma_alloc_context(struct ibv_device *ibdev, + int cmd_fd) +{ + struct pvrdma_context *context; + + context = malloc(sizeof(*context)); + if (!context) + return NULL; + + memset(context, 0, sizeof(*context)); + + if (pvrdma_init_context_shared(context, ibdev, cmd_fd)) { + free(context); + return NULL; + } + + return &context->ibv_ctx; +} + +static void pvrdma_free_context(struct ibv_context *ibctx) +{ + struct pvrdma_context *context = to_vctx(ibctx); + + pvrdma_free_context_shared(context, to_vdev(ibctx->device)); + free(context); +} + +static struct ibv_device_ops pvrdma_dev_ops = { + .alloc_context = pvrdma_alloc_context, + .free_context = pvrdma_free_context +}; + +static struct pvrdma_device *pvrdma_driver_init_shared( + const char *uverbs_sys_path, + int abi_version) +{ + struct pvrdma_device *dev; + char name[16]; + + /* We support only a single ABI version for now. */ + if (abi_version != PVRDMA_UVERBS_ABI_VERSION) { + fprintf(stderr, PFX "ABI version %d of %s is not " + "supported (supported %d)\n", + abi_version, uverbs_sys_path, + PVRDMA_UVERBS_ABI_VERSION); + return NULL; + } + + if (ibv_read_sysfs_file(uverbs_sys_path, + "ibdev", name, sizeof(name)) < 0) { + fprintf(stderr, PFX "not ib device\n"); + return NULL; + } + + dev = malloc(sizeof(*dev)); + if (!dev) { + fprintf(stderr, PFX "couldn't allocate device for %s\n", + uverbs_sys_path); + return NULL; + } + + dev->abi_version = abi_version; + dev->page_size = sysconf(_SC_PAGESIZE); + dev->ibv_dev.ops = pvrdma_dev_ops; + + return dev; +} + +static struct ibv_device *pvrdma_driver_init(const char *uverbs_sys_path, + int abi_version) +{ + struct pvrdma_device *dev = pvrdma_driver_init_shared(uverbs_sys_path, + abi_version); + if (!dev) + return NULL; + + return &dev->ibv_dev; +} + +static __attribute__((constructor)) void pvrdma_register_driver(void) +{ + ibv_register_driver("pvrdma", pvrdma_driver_init); +}