From patchwork Mon Oct 3 02:10:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9359913 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 4D98060459 for ; Mon, 3 Oct 2016 02:11:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3FE5028904 for ; Mon, 3 Oct 2016 02:11:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3460428977; Mon, 3 Oct 2016 02:11:05 +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 B1EEB28986 for ; Mon, 3 Oct 2016 02:11:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751882AbcJCCLB (ORCPT ); Sun, 2 Oct 2016 22:11:01 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:46302 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751934AbcJCCK7 (ORCPT ); Sun, 2 Oct 2016 22:10:59 -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; Sun, 2 Oct 2016 19:10:26 -0700 Received: from EX13-CAS-002.vmware.com (ex13-cas-002.vmware.com [10.113.191.52]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id EE15B183C1; Sun, 2 Oct 2016 19:10:58 -0700 (PDT) Received: from EX13-CAS-004.vmware.com (10.113.191.54) by EX13-MBX-028.vmware.com (10.113.191.48) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Sun, 2 Oct 2016 19:10:58 -0700 Received: from NAM01-BY2-obe.outbound.protection.outlook.com (10.113.170.11) by EX13-CAS-004.vmware.com (10.113.191.54) with Microsoft SMTP Server (TLS) id 15.0.1156.6 via Frontend Transport; Sun, 2 Oct 2016 19:10:58 -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=mRSAP1X9SOQczL2b5L3tcAcBvMyZK4idkIG/7aK3KIw=; b=KrPf7KnDwp10vqUWVpTu4rnLQVAZOZ4ykHcvhoqJ8hcE1cuXk4bE0UA2IlxA2uvJOguvHWtKZdv0tjrz4RbzdC2b5v8ONnT4xl57OXRHewVNgMutA2nDx2ygiGF3b6AVuLgSYOuZmsFAN/D0ydU2qm9tnKXdL/TK1WMVO8Byd5U= 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 DM2PR0501MB841.namprd05.prod.outlook.com (10.242.115.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.649.6; Mon, 3 Oct 2016 02:10:56 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , Subject: [PATCH v6 03/16] IB/pvrdma: Add functions for ring traversal Date: Sun, 2 Oct 2016 19:10:23 -0700 Message-ID: <72f95d36213c2bbcc1ec31c36c8f24e3aa10c4a2.1475458407.git.aditr@vmware.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [208.91.1.34] X-ClientProxiedBy: CY1PR1101CA0039.namprd11.prod.outlook.com (10.169.17.49) To DM2PR0501MB841.namprd05.prod.outlook.com (10.242.115.147) X-MS-Office365-Filtering-Correlation-Id: 983cbb84-cb42-4b5c-ca35-08d3eb328358 X-Microsoft-Exchange-Diagnostics: 1; DM2PR0501MB841; 2:RJij2mtjgqTdmREAYYoTZNbWQziqfvXtT9+T8GLuGvJwB9YpF/qHaLi2oHLBAKlPoPPQVGxSkOfgYlBSa8LHJ0++GuYexridISsW57RYO8C9z1aHc8IypGh9QG5zIYmV7T55Fh9q65x3b9NSIoaoo2jkVO05bbO4d79DdPD1as8blLasJ9Wu9CjSqBtLK3miZLXSlhn6fsK31iabNhdZwg==; 3:r3arQgsSV5scP6/xIQ2U3d8sBzuDWyKMHVFZkZjnTH1tIw9Q8Dekt7265yOFfgtE6Rzc1CvT7LkBaCQnGI6QMoNDfOuOBhI5fZJkbrwR3H/cVHuklctnOZ+t9KYvuftDL7wKBMd2uf0oJ4wqswl8Nw== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DM2PR0501MB841; X-Microsoft-Exchange-Diagnostics: 1; DM2PR0501MB841; 25:Sidod/9MgEscQ6+sCvSH/pIp0CGIGQaIL+cTfM04S6uUpvuaha/GNY3BvqAHKC8NUWQO6cqcfc7A2+N5sOsDX7TXbIt+MwrTA+n9SayXVmr0O4//7wVPG7m2mXRhnBiZA1ZIF0hAjkUnCj9IvCMB/Tbddpte9AoTp+LI11zlbUzKJVsUdCJNhcPqm1s5P9MWU2aLhHCGcawTHl5XVgRfM+40dJnp4xW/nXJBNSk4f3QRUxIyCaS5Bp2OEmofJUDwiACh5ZqxSkMvoCwwlGu8mzHhIFGNp3Gd8Zv/kAighvT2kZDpnDAPbWB4sykvmY7y5eKmnSyuDy0Q0y6qSxbzU8LvBSBasSUzbMiVpvzKu2XKw/TwWyHDhRtK9ZyF5LLgeKtK3+7VMKioSB8XTJwRU1i0609rsbk8Yu8cEycBIYVkMBFGO1ss2w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m/JcjJYlzxNBHMZo9jtvkIaaqOiJN5Ya8u/AuyFxZFux3Vjb/uvqTupTyP3Hp7K7I5SzSbErberruPREhG9qHqBLTLVSkHaGkMUZ+84WZKDoURFJF0wxcFbavht+jXGvsJFrIZDp4y5S4HdClSjtbDx8lvGghmPfvm6Vcf5a/nzu2L/GSs2yv/ucaWz5eI2dJyp3rLTSGpyFuB3rQVqMWT 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)(10201501046)(3002001); SRVR:DM2PR0501MB841; BCL:0; PCL:0; RULEID:; SRVR:DM2PR0501MB841; X-Microsoft-Exchange-Diagnostics: 1; DM2PR0501MB841; 4:y9wkYjYThftvYm1Es8GzGmIpfGzTMCbt/PCN9RjOSw90mIbWun3og3Eu9AfRgKVgMqiL8lUyWtC11H/A9uBXUhLVpGT/Pa5YZbpEuESagRJg2LFToIRm+bYyIPhNSZWWzwrSNtz2QiV10T4z04ukI9/3gcl7Vedd6GTUmySQgsAtF4oryWLuu0CRVdLGARVNghB0A45Ua4OiupTVXkLrTjt5/mG5Xn81AGryiDfDnJSDrmozXl6fp6Ze6hf6RcxrlvS4FejugtydQxXoCu8HsacS/8VTWQZCEOLFMDJAVWeGCBod2lcBh9wVE5e5dFNDwQmpJQvO03GPxHOg5l/L5vbvXaf2h5y8k4l+0cP2OzGdsf5jw5qrF7wQ5uLmnePAl26J/V91dnWe7dRLF5aHLJwXTX+HqccynbK48czedzV92/Apl7pnNA/oKPNuDDSxUt9KJJ26Iy69mIOwb/4YSuhhUBu1kTZXykPBolAxxwIgIC8mcEBBgJS/s0BULJHYT/uS9QapvXb90m3x+4DUig== X-Forefront-PRVS: 008421A8FF X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(189002)(199003)(97736004)(189998001)(42186005)(19580405001)(47776003)(8676002)(5001770100001)(33646002)(2906002)(19580395003)(15975445007)(77096005)(6862003)(50986999)(76176999)(106356001)(2950100002)(101416001)(5003940100001)(4326007)(5660300001)(107886002)(105586002)(92566002)(7846002)(7736002)(586003)(118296001)(229853001)(68736007)(4001430100002)(305945005)(2201001)(48376002)(6636002)(50466002)(66066001)(86362001)(50226002)(6116002)(3846002)(81156014)(6666003)(36756003)(81166006)(7099028)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:DM2PR0501MB841; H:promb-2s-dhcp95-136.eng.vmware.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A: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; DM2PR0501MB841; 23:2lg12eNKb+8ugGQa6PjHKHJy5JRycQJDz9UYqYkf?= =?us-ascii?Q?dDhDPPtvGtrzp516oMpMafr4GxpBd3vl05B5FcZT5YwNqEoGn43TFrlZ2OlU?= =?us-ascii?Q?xc2Xq+82iYk/mn/Ma9+GoY+msDDROUL1p7Q+TMAU5ZRUYtpUBM7l1lSOtSAB?= =?us-ascii?Q?tsBIcSWwXze/TVeGBo7QN6M35aGn10KcR+SMr46GtAFbFbw1u7XPQUxUKUZ0?= =?us-ascii?Q?XlQYB73fzahkcqRYoKTIREllyTJXiQq7pLvUQYDcFrMxp1CWC8/Nxbu6emMe?= =?us-ascii?Q?jYOUnSmQgS5L1f81MQ/YYO3BCoUH4HClQT2EeBe5HmiUMu9QcRMfoJyIYAt0?= =?us-ascii?Q?4a+7PYSQE2UwDxGZNcRIn1xzvkQ0MvXBgxl52wsqQQc9hGfhuIp4Wcr9mAF+?= =?us-ascii?Q?U2SUI41YKywGksnVESoYHdMYaL6m9bI1/4cMwKt36nhEkyW5laZLjsiV03Ed?= =?us-ascii?Q?7q/G+yjBXnmnHSY9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bl6YqcdSyAG0YqycFd6FZ5/+Butu8uPxwWz92gmV/hICBmnZ7mNwk01lDV56ZUd6Zrcw1Kh/JC0AL+Hgy/2PvbSFoa/tAwLgKU= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Oct 2016 02:10:56.4570 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR0501MB841 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 functions to traverse the CQ and QP rings. These are independent from the user-space defined ones. Reviewed-by: Jorgen Hansen Reviewed-by: George Zhang Reviewed-by: Aditya Sarwade Reviewed-by: Bryan Tan Signed-off-by: Adit Ranadive --- Changes v5->v6: - These functions were originally in pvrdma_uapi.h which is now removed. --- drivers/infiniband/hw/pvrdma/pvrdma_ring.h | 131 +++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 drivers/infiniband/hw/pvrdma/pvrdma_ring.h diff --git a/drivers/infiniband/hw/pvrdma/pvrdma_ring.h b/drivers/infiniband/hw/pvrdma/pvrdma_ring.h new file mode 100644 index 0000000..ed9022a --- /dev/null +++ b/drivers/infiniband/hw/pvrdma/pvrdma_ring.h @@ -0,0 +1,131 @@ +/* + * 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. + */ + +#ifndef __PVRDMA_RING_H__ +#define __PVRDMA_RING_H__ + +#include + +#define PVRDMA_INVALID_IDX -1 /* Invalid index. */ + +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 int 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 bool 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 true; + else if (head > tail && (*idx >= head || *idx < tail)) + return true; + } + return false; +} + +#endif /* __PVRDMA_RING_H__ */