162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (c) 2012-2016 VMware, Inc. All rights reserved. 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or 562306a36Sopenharmony_ci * modify it under the terms of EITHER the GNU General Public License 662306a36Sopenharmony_ci * version 2 as published by the Free Software Foundation or the BSD 762306a36Sopenharmony_ci * 2-Clause License. This program is distributed in the hope that it 862306a36Sopenharmony_ci * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED 962306a36Sopenharmony_ci * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 1062306a36Sopenharmony_ci * See the GNU General Public License version 2 for more details at 1162306a36Sopenharmony_ci * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html. 1262306a36Sopenharmony_ci * 1362306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License 1462306a36Sopenharmony_ci * along with this program available in the file COPYING in the main 1562306a36Sopenharmony_ci * directory of this source tree. 1662306a36Sopenharmony_ci * 1762306a36Sopenharmony_ci * The BSD 2-Clause License 1862306a36Sopenharmony_ci * 1962306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or 2062306a36Sopenharmony_ci * without modification, are permitted provided that the following 2162306a36Sopenharmony_ci * conditions are met: 2262306a36Sopenharmony_ci * 2362306a36Sopenharmony_ci * - Redistributions of source code must retain the above 2462306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 2562306a36Sopenharmony_ci * disclaimer. 2662306a36Sopenharmony_ci * 2762306a36Sopenharmony_ci * - Redistributions in binary form must reproduce the above 2862306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 2962306a36Sopenharmony_ci * disclaimer in the documentation and/or other materials 3062306a36Sopenharmony_ci * provided with the distribution. 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 3362306a36Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3462306a36Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 3562306a36Sopenharmony_ci * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 3662306a36Sopenharmony_ci * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 3762306a36Sopenharmony_ci * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 3862306a36Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 3962306a36Sopenharmony_ci * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4062306a36Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4162306a36Sopenharmony_ci * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4262306a36Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 4362306a36Sopenharmony_ci * OF THE POSSIBILITY OF SUCH DAMAGE. 4462306a36Sopenharmony_ci */ 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci#ifndef __PVRDMA_RING_H__ 4762306a36Sopenharmony_ci#define __PVRDMA_RING_H__ 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci#include <linux/types.h> 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci#define PVRDMA_INVALID_IDX -1 /* Invalid index. */ 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_cistruct pvrdma_ring { 5462306a36Sopenharmony_ci atomic_t prod_tail; /* Producer tail. */ 5562306a36Sopenharmony_ci atomic_t cons_head; /* Consumer head. */ 5662306a36Sopenharmony_ci}; 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_cistruct pvrdma_ring_state { 5962306a36Sopenharmony_ci struct pvrdma_ring tx; /* Tx ring. */ 6062306a36Sopenharmony_ci struct pvrdma_ring rx; /* Rx ring. */ 6162306a36Sopenharmony_ci}; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_cistatic inline int pvrdma_idx_valid(__u32 idx, __u32 max_elems) 6462306a36Sopenharmony_ci{ 6562306a36Sopenharmony_ci /* Generates fewer instructions than a less-than. */ 6662306a36Sopenharmony_ci return (idx & ~((max_elems << 1) - 1)) == 0; 6762306a36Sopenharmony_ci} 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_cistatic inline __s32 pvrdma_idx(atomic_t *var, __u32 max_elems) 7062306a36Sopenharmony_ci{ 7162306a36Sopenharmony_ci const unsigned int idx = atomic_read(var); 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci if (pvrdma_idx_valid(idx, max_elems)) 7462306a36Sopenharmony_ci return idx & (max_elems - 1); 7562306a36Sopenharmony_ci return PVRDMA_INVALID_IDX; 7662306a36Sopenharmony_ci} 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_cistatic inline void pvrdma_idx_ring_inc(atomic_t *var, __u32 max_elems) 7962306a36Sopenharmony_ci{ 8062306a36Sopenharmony_ci __u32 idx = atomic_read(var) + 1; /* Increment. */ 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci idx &= (max_elems << 1) - 1; /* Modulo size, flip gen. */ 8362306a36Sopenharmony_ci atomic_set(var, idx); 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_cistatic inline __s32 pvrdma_idx_ring_has_space(const struct pvrdma_ring *r, 8762306a36Sopenharmony_ci __u32 max_elems, __u32 *out_tail) 8862306a36Sopenharmony_ci{ 8962306a36Sopenharmony_ci const __u32 tail = atomic_read(&r->prod_tail); 9062306a36Sopenharmony_ci const __u32 head = atomic_read(&r->cons_head); 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci if (pvrdma_idx_valid(tail, max_elems) && 9362306a36Sopenharmony_ci pvrdma_idx_valid(head, max_elems)) { 9462306a36Sopenharmony_ci *out_tail = tail & (max_elems - 1); 9562306a36Sopenharmony_ci return tail != (head ^ max_elems); 9662306a36Sopenharmony_ci } 9762306a36Sopenharmony_ci return PVRDMA_INVALID_IDX; 9862306a36Sopenharmony_ci} 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_cistatic inline __s32 pvrdma_idx_ring_has_data(const struct pvrdma_ring *r, 10162306a36Sopenharmony_ci __u32 max_elems, __u32 *out_head) 10262306a36Sopenharmony_ci{ 10362306a36Sopenharmony_ci const __u32 tail = atomic_read(&r->prod_tail); 10462306a36Sopenharmony_ci const __u32 head = atomic_read(&r->cons_head); 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci if (pvrdma_idx_valid(tail, max_elems) && 10762306a36Sopenharmony_ci pvrdma_idx_valid(head, max_elems)) { 10862306a36Sopenharmony_ci *out_head = head & (max_elems - 1); 10962306a36Sopenharmony_ci return tail != head; 11062306a36Sopenharmony_ci } 11162306a36Sopenharmony_ci return PVRDMA_INVALID_IDX; 11262306a36Sopenharmony_ci} 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci#endif /* __PVRDMA_RING_H__ */ 115