From patchwork Thu Nov 3 23:44:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9411637 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 217B760585 for ; Thu, 3 Nov 2016 23:48:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0FADF2AF77 for ; Thu, 3 Nov 2016 23:48:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0427A2AF9C; Thu, 3 Nov 2016 23:48:51 +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 60AC62AF77 for ; Thu, 3 Nov 2016 23:48:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933384AbcKCXst (ORCPT ); Thu, 3 Nov 2016 19:48:49 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:4100 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933240AbcKCXsr (ORCPT ); Thu, 3 Nov 2016 19:48:47 -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:30 -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 08418184B5; Thu, 3 Nov 2016 16:48:47 -0700 (PDT) Received: from EX13-CAS-003.vmware.com (10.113.191.53) by EX13-MBX-001.vmware.com (10.113.191.21) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 3 Nov 2016 16:48:28 -0700 Received: from NAM02-CY1-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; Thu, 3 Nov 2016 16:48:28 -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=GHDCZ4K+rbbk0AeUWMECeSRclJGA/bueZ7N1wDulpPQ=; b=rak0GVG/dkUOlEmokYKnsm+P2mH1isHsW05A++FWeRFch4LPtyAuCDEkjSru4gWs8JdbddF74521axd5r1xiAMg+rNOPw+3oH2CT/9ptd8fJ12sGDr3KC7rIZTWmfVvNXqVDrTHAXzqZRB55DzO+e4WO/4Zn7WFBGfP9FWXOWTg= 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:21 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive Subject: [PATCH 2/8] libpvrdma: Add ring traversal Date: Thu, 3 Nov 2016 16:44:31 -0700 Message-ID: <1478216677-6150-3-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: e9222511-5688-487e-5572-08d40443e52e X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 2:EYR3VUc6KmVX4Unu5db9+/DVmf4T/KOv+JILAR9G71Id9QlEKd+UoWUlsA3TRtMfO+2NrHBAnrh13Oul+L72vdLF9D/XCyszayGipOrXUC2BQo34Q0pEqfUltBf1bbuXbfBn2HmxR2iW6D0jMLmO1e26MuFJW8/3NLBsxTV2LcqkmbogSMKlvxyjWP/i8KJkilcnIcZTwXsINmOMQq1vsQ==; 3:1P+KmTW3HBAepKhu7jWey52+UtbTnsh7q7zxIHVVIvzhhmCdumb+ruGS+syq9nb5dHT7wrSLiKZfOgdIlQdJL+iq/QMqnv5e5OXBHBm34COJ9bK9RHtpAQ+zJyEt7xo5WwU4bdPorAmfih2rWmrkhQ== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BLUPR0501MB836; X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 25:euAxQQvjm0enRIeCFwq6IlB1iWsC6r8Vk6HXg8s9lhlMf5s+xt9KxUcqjWDyNwWurMV+aatw/cnd3QIdcEUOf5d/HfzKVIEJhQAzcTY8EnA13x/wVuBjkDZJUe/uK6SEs3woY/FJTJtnqXjSMjdPk7L0f3DWQ2Htj7x0+LGDSy+/FMxmTBG4rx/9P2hUurQuI9DSBBydD+n+TKdelXXCfH3sEPhcoVqhxjjzI0/I+sghrO/3GXknNztHl9Yyv9g52HVHgG14wfAlaMZL7PMh1ZDreUwBkgCWOorzvVfL5dlAwZhlATnf+ep6Po7+0QdBDAtmDFwq4vpCfSwhxnYWJkjaXJN50EVjrJc7PC9onRJtlGnnQ9egc8zr17X+WtGV4oWq3qZNd4yJ5alKMlG5TbIOB+gAFoiWriUEreDH1WWG/2N3/LP8uhHnvCOHiNSE0gJ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Q2aGBudN0anEF0ix3SznLevV+ITAVG1tFMNtKFp1/w+WdSKD4GBmRzsE62k9KLnJQDKb96IyWQQmOUeS1iAbqmdyGjF0vk17e1wiJhQWEqoOVmhK84/DXnWV5Qh0jqCr71ty9AaprVlzUBQ+pHk1TVadj8tA7tqpqRwN4fQiG2OAUyxtOjEUZrfjDotuwJFz92CzNdl1AqRgN7Fg+jTFr2eIAdG5+XU1 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:DpYyoIm8nIkcTqdRDb/KpJjfD3CecTpVM9z+YEGeuFBQmN7X4gssytjMQ2vwFJQ3JCCKM3zWpgQBnvf7PXsJVqi1dy59FCo/ds4xnlBetJ7jC14R8XnGeCgjSqxDwTP5JJpFuxJAjy1Ih2ExyhE/zaikY27VaRpQfLplhZpx4Tyde2y+8YUhvLjOhBShtua4fjnSOfyzd+gKxSo9rARCASQ4dCBecGHdcjh62/2QInZLwl42z8ybXPUGqS6iTRPSfZE/I1NrfXUgjc1WEcGErY1/DfaszEH5DiFvMS3OCzti8W7VMzUwbKHhOTw+0rkobtOlyuC/fc5JdJlP7iG7t/bGwblF48nzg7XhjUf64ysj6taWx0MiKFWbS9QXjVLkzLZub4c2/PWadm8EsCJEbwUHes+NkCL53OMz5PZk/IZHqwCHV7079DbCHbk7cfukOE5XcvILApHCKKv7MCXfXSx9ViSRvLYSQ5Zz7uxjja9vgdauNgL7lhnYUyI6nL9VxpUp8WBYJQ+YFhbVeAgFPQ== X-Forefront-PRVS: 011579F31F X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(979002)(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)(969003)(989001)(999001)(1009001)(1019001); 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:3fLTeRakIMoJ+1a58TMpy/NWyNSIVR+qexayugvR?= =?us-ascii?Q?GUk6oe93orgF2dQsqp9eEtXgjowc9Vc13HQIA6f675KMZ8/bEuNa8tIZ9H4b?= =?us-ascii?Q?9ArxMvwk1pym2RPgwZ8vYvVGX/zcyz9MRqcHwDKZvKF1ywLAl4Uz61cIAhv0?= =?us-ascii?Q?IvXj6InxoQbZfPYB/EqnZ1k9rnnT6gA2nOk0iats8xA/SHiNMNbHfkjJmhlU?= =?us-ascii?Q?vx6H6y1ltSnxe/Q6805kx/pVaZguEGIU0cJLDQ5gyQhuSD3dyL7vmlaCnX6K?= =?us-ascii?Q?Wupfu9ahkLvz8j4W+xjIIvW8Qnos8mYUok8OOPyH+VHc/wzeui2g0dpOewXS?= =?us-ascii?Q?Sy8ztSo7Ok7wRJsgnJcp/0G0gBaW+iTTpc24OAjE4fWcqfSNg5FATM2z7OZr?= =?us-ascii?Q?y7juu8GX1LR145WC4nDlfgokq90aqZuEYSIZgFO2EWQfH6A9R3RB6T54881a?= =?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Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 6:rsuSBUje94ZDY6SJJwNJ2PyQs4m3T16SVUt6OXFX+md0ULzAsVBjgA5DAqrA5d/nv8S4UXfvEekkjW+KQxbNxcrH6NhraZxLO7MFFalh1bku/TkdZfvc+6mBrkoQ295nHNS4Iqdtt9MzNQwyR/H5Nsli1a88W9isgzNw6BtV24WRd79Ho/QXRvq8FWaHXGJ+v9xOoEDASU7xm1G5vWURb5ZV3tpemPBuZ/3At76OBenyBHbla2ZmYw5JvGBRlll+tH5j9dk+xUqOwLGxcONCH34WLsD2fCXYE694EIueYGAbK5wSC0U7sbpxHZXaj+WA; 5:vew57avv2gWw1xxt07/P/4EuKHDhLifJv+Y3MTCp4c16nODro9SBFx/K9oZKdT2u15HazKMMqBPV0td2TghhOSAhkNr0GuGuOL6nPsTvs9HKbb+evncc4IWgEmEkAzhXBsti7pfr3rJhtaN4Jfp4+uURK1zNOsobNHU+TZEQ2Mc=; 24:FJWkzi/RNMdxTsnBII28AUdLa3YdQKP6aOQLblRP+53HUmJOLbmJJyonK+lw5RBuGc+cXMMkgjm72qtPur2UDp6U5oJ3f70dhgccjEWr7ZM= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 7:lkT/n4l+DlbGgFFHEnAf28mozQmZ/i5n1w4S+zrvwWd4MgTOKzR4Vqp4o7/T4AsTU3C+CYSNpbHXtm6FwBCgmpPgDNsP8AvKIwsbWLvZiPKfEMHB1hQKq2RjTdOUtXobEmFWA+rCHs7o5VWgGS0BkTbsFCFXJFpxDaharXPSxzy9Eyzgp/BoWDKVhk462qMbdvx3TN5Aw2JKf6YKFXa86rro6G6CAlyF951IRXD8USt3yx7Jowh4pVec1Bh7Tllc+RJd3AkNzSH92GIFxPyV8KgiyIwO4PvQc2ZoA/Q1PJE1JG+UgpMfFsTyI3VxU8+WcSu41L73aD/gwcJ/UfN+zrs5OTpE3O9N58WpVAebZ8Y=; 20:AhfaR19dn2cDhctcb6l27YIdEai44X1zKNTbwRvqFxB5jgjp0z5wMl8xKROTiEZWm6K36N4wlrqgUKHJQBC+u+Y0d7ngQrJxMeVCa8pYhRnbVVA+QrXj1Qxjt5QRY+V+Ge4veSsWf5TPie8e3dahbn4PcTISLFI2tfD/cSXyawo= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2016 23:48:21.4576 (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 CQs and QPs use these structures to traverse the CQE/WQE rings. Signed-off-by: Adit Ranadive --- providers/pvrdma/pvrdma_ring.h | 136 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 providers/pvrdma/pvrdma_ring.h diff --git a/providers/pvrdma/pvrdma_ring.h b/providers/pvrdma/pvrdma_ring.h new file mode 100644 index 0000000..e99a551 --- /dev/null +++ b/providers/pvrdma/pvrdma_ring.h @@ -0,0 +1,136 @@ +/* + * 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 in the file COPYING. If not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + * 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. + */ + +#ifndef __PVRDMA_RING_H__ +#define __PVRDMA_RING_H__ + +#include + +#define PVRDMA_INVALID_IDX -1 /* Invalid index. */ +#define atomic_read(_x) *(_x) +#define atomic_set(_x, _y) (*(_x) = (_y)) + +typedef uint32_t atomic_t; + +struct pvrdma_ring { + atomic_t prod_tail; /* Producer tail. */ + atomic_t cons_head; /* Consumer head. */ +}; + +struct pvrdma_ring_state { + struct pvrdma_ring tx; /* Tx ring. */ + struct pvrdma_ring rx; /* Rx ring. */ +}; + +static inline int pvrdma_idx_valid(__u32 idx, __u32 max_elems) +{ + /* Generates fewer instructions than a less-than. */ + return (idx & ~((max_elems << 1) - 1)) == 0; +} + +static inline __s32 pvrdma_idx(atomic_t *var, __u32 max_elems) +{ + const unsigned idx = atomic_read(var); + + if (pvrdma_idx_valid(idx, max_elems)) + return idx & (max_elems - 1); + return PVRDMA_INVALID_IDX; +} + +static inline void pvrdma_idx_ring_inc(atomic_t *var, __u32 max_elems) +{ + __u32 idx = atomic_read(var) + 1; /* Increment. */ + + idx &= (max_elems << 1) - 1; /* Modulo size, flip gen. */ + atomic_set(var, idx); +} + +static inline __s32 pvrdma_idx_ring_has_space(const struct pvrdma_ring *r, + __u32 max_elems, __u32 *out_tail) +{ + const __u32 tail = atomic_read(&r->prod_tail); + const __u32 head = atomic_read(&r->cons_head); + + if (pvrdma_idx_valid(tail, max_elems) && + pvrdma_idx_valid(head, max_elems)) { + *out_tail = tail & (max_elems - 1); + return tail != (head ^ max_elems); + } + return PVRDMA_INVALID_IDX; +} + +static inline __s32 pvrdma_idx_ring_has_data(const struct pvrdma_ring *r, + __u32 max_elems, __u32 *out_head) +{ + const __u32 tail = atomic_read(&r->prod_tail); + const __u32 head = atomic_read(&r->cons_head); + + if (pvrdma_idx_valid(tail, max_elems) && + pvrdma_idx_valid(head, max_elems)) { + *out_head = head & (max_elems - 1); + return tail != head; + } + return PVRDMA_INVALID_IDX; +} + +static inline __s32 pvrdma_idx_ring_is_valid_idx(const struct pvrdma_ring *r, + __u32 max_elems, __u32 *idx) +{ + const __u32 tail = atomic_read(&r->prod_tail); + const __u32 head = atomic_read(&r->cons_head); + + if (pvrdma_idx_valid(tail, max_elems) && + pvrdma_idx_valid(head, max_elems) && + pvrdma_idx_valid(*idx, max_elems)) { + if (tail > head && (*idx < tail && *idx >= head)) + return 1; + else if (head > tail && (*idx >= head || *idx < tail)) + return 1; + } + return 0; +} + +#endif /* __PVRDMA_RING_H__ */