pvrdma_ring.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef __PVRDMA_RING_H__
  46. #define __PVRDMA_RING_H__
  47. #include <linux/types.h>
  48. #define PVRDMA_INVALID_IDX -1 /* Invalid index. */
  49. struct pvrdma_ring {
  50. atomic_t prod_tail; /* Producer tail. */
  51. atomic_t cons_head; /* Consumer head. */
  52. };
  53. struct pvrdma_ring_state {
  54. struct pvrdma_ring tx; /* Tx ring. */
  55. struct pvrdma_ring rx; /* Rx ring. */
  56. };
  57. static inline int pvrdma_idx_valid(__u32 idx, __u32 max_elems)
  58. {
  59. /* Generates fewer instructions than a less-than. */
  60. return (idx & ~((max_elems << 1) - 1)) == 0;
  61. }
  62. static inline __s32 pvrdma_idx(atomic_t *var, __u32 max_elems)
  63. {
  64. const unsigned int idx = atomic_read(var);
  65. if (pvrdma_idx_valid(idx, max_elems))
  66. return idx & (max_elems - 1);
  67. return PVRDMA_INVALID_IDX;
  68. }
  69. static inline void pvrdma_idx_ring_inc(atomic_t *var, __u32 max_elems)
  70. {
  71. __u32 idx = atomic_read(var) + 1; /* Increment. */
  72. idx &= (max_elems << 1) - 1; /* Modulo size, flip gen. */
  73. atomic_set(var, idx);
  74. }
  75. static inline __s32 pvrdma_idx_ring_has_space(const struct pvrdma_ring *r,
  76. __u32 max_elems, __u32 *out_tail)
  77. {
  78. const __u32 tail = atomic_read(&r->prod_tail);
  79. const __u32 head = atomic_read(&r->cons_head);
  80. if (pvrdma_idx_valid(tail, max_elems) &&
  81. pvrdma_idx_valid(head, max_elems)) {
  82. *out_tail = tail & (max_elems - 1);
  83. return tail != (head ^ max_elems);
  84. }
  85. return PVRDMA_INVALID_IDX;
  86. }
  87. static inline __s32 pvrdma_idx_ring_has_data(const struct pvrdma_ring *r,
  88. __u32 max_elems, __u32 *out_head)
  89. {
  90. const __u32 tail = atomic_read(&r->prod_tail);
  91. const __u32 head = atomic_read(&r->cons_head);
  92. if (pvrdma_idx_valid(tail, max_elems) &&
  93. pvrdma_idx_valid(head, max_elems)) {
  94. *out_head = head & (max_elems - 1);
  95. return tail != head;
  96. }
  97. return PVRDMA_INVALID_IDX;
  98. }
  99. #endif /* __PVRDMA_RING_H__ */