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