1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2012-2016 VMware, Inc. All rights reserved.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or
5*4882a593Smuzhiyun * modify it under the terms of EITHER the GNU General Public License
6*4882a593Smuzhiyun * version 2 as published by the Free Software Foundation or the BSD
7*4882a593Smuzhiyun * 2-Clause License. This program is distributed in the hope that it
8*4882a593Smuzhiyun * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
9*4882a593Smuzhiyun * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
10*4882a593Smuzhiyun * See the GNU General Public License version 2 for more details at
11*4882a593Smuzhiyun * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License
14*4882a593Smuzhiyun * along with this program available in the file COPYING in the main
15*4882a593Smuzhiyun * directory of this source tree.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * The BSD 2-Clause License
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or
20*4882a593Smuzhiyun * without modification, are permitted provided that the following
21*4882a593Smuzhiyun * conditions are met:
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * - Redistributions of source code must retain the above
24*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
25*4882a593Smuzhiyun * disclaimer.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above
28*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
29*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials
30*4882a593Smuzhiyun * provided with the distribution.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35*4882a593Smuzhiyun * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36*4882a593Smuzhiyun * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37*4882a593Smuzhiyun * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38*4882a593Smuzhiyun * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39*4882a593Smuzhiyun * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40*4882a593Smuzhiyun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41*4882a593Smuzhiyun * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42*4882a593Smuzhiyun * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43*4882a593Smuzhiyun * OF THE POSSIBILITY OF SUCH DAMAGE.
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #ifndef __PVRDMA_RING_H__
47*4882a593Smuzhiyun #define __PVRDMA_RING_H__
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #include <linux/types.h>
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #define PVRDMA_INVALID_IDX -1 /* Invalid index. */
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun struct pvrdma_ring {
54*4882a593Smuzhiyun atomic_t prod_tail; /* Producer tail. */
55*4882a593Smuzhiyun atomic_t cons_head; /* Consumer head. */
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun struct pvrdma_ring_state {
59*4882a593Smuzhiyun struct pvrdma_ring tx; /* Tx ring. */
60*4882a593Smuzhiyun struct pvrdma_ring rx; /* Rx ring. */
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun
pvrdma_idx_valid(__u32 idx,__u32 max_elems)63*4882a593Smuzhiyun static inline int pvrdma_idx_valid(__u32 idx, __u32 max_elems)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun /* Generates fewer instructions than a less-than. */
66*4882a593Smuzhiyun return (idx & ~((max_elems << 1) - 1)) == 0;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
pvrdma_idx(atomic_t * var,__u32 max_elems)69*4882a593Smuzhiyun static inline __s32 pvrdma_idx(atomic_t *var, __u32 max_elems)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun const unsigned int idx = atomic_read(var);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun if (pvrdma_idx_valid(idx, max_elems))
74*4882a593Smuzhiyun return idx & (max_elems - 1);
75*4882a593Smuzhiyun return PVRDMA_INVALID_IDX;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
pvrdma_idx_ring_inc(atomic_t * var,__u32 max_elems)78*4882a593Smuzhiyun static inline void pvrdma_idx_ring_inc(atomic_t *var, __u32 max_elems)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun __u32 idx = atomic_read(var) + 1; /* Increment. */
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun idx &= (max_elems << 1) - 1; /* Modulo size, flip gen. */
83*4882a593Smuzhiyun atomic_set(var, idx);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
pvrdma_idx_ring_has_space(const struct pvrdma_ring * r,__u32 max_elems,__u32 * out_tail)86*4882a593Smuzhiyun static inline __s32 pvrdma_idx_ring_has_space(const struct pvrdma_ring *r,
87*4882a593Smuzhiyun __u32 max_elems, __u32 *out_tail)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun const __u32 tail = atomic_read(&r->prod_tail);
90*4882a593Smuzhiyun const __u32 head = atomic_read(&r->cons_head);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun if (pvrdma_idx_valid(tail, max_elems) &&
93*4882a593Smuzhiyun pvrdma_idx_valid(head, max_elems)) {
94*4882a593Smuzhiyun *out_tail = tail & (max_elems - 1);
95*4882a593Smuzhiyun return tail != (head ^ max_elems);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun return PVRDMA_INVALID_IDX;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
pvrdma_idx_ring_has_data(const struct pvrdma_ring * r,__u32 max_elems,__u32 * out_head)100*4882a593Smuzhiyun static inline __s32 pvrdma_idx_ring_has_data(const struct pvrdma_ring *r,
101*4882a593Smuzhiyun __u32 max_elems, __u32 *out_head)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun const __u32 tail = atomic_read(&r->prod_tail);
104*4882a593Smuzhiyun const __u32 head = atomic_read(&r->cons_head);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun if (pvrdma_idx_valid(tail, max_elems) &&
107*4882a593Smuzhiyun pvrdma_idx_valid(head, max_elems)) {
108*4882a593Smuzhiyun *out_head = head & (max_elems - 1);
109*4882a593Smuzhiyun return tail != head;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun return PVRDMA_INVALID_IDX;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun #endif /* __PVRDMA_RING_H__ */
115