rxe_pool.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #ifndef RXE_POOL_H
  34. #define RXE_POOL_H
  35. #define RXE_POOL_ALIGN (16)
  36. #define RXE_POOL_CACHE_FLAGS (0)
  37. enum rxe_pool_flags {
  38. RXE_POOL_ATOMIC = BIT(0),
  39. RXE_POOL_INDEX = BIT(1),
  40. RXE_POOL_KEY = BIT(2),
  41. };
  42. enum rxe_elem_type {
  43. RXE_TYPE_UC,
  44. RXE_TYPE_PD,
  45. RXE_TYPE_AH,
  46. RXE_TYPE_SRQ,
  47. RXE_TYPE_QP,
  48. RXE_TYPE_CQ,
  49. RXE_TYPE_MR,
  50. RXE_TYPE_MW,
  51. RXE_TYPE_MC_GRP,
  52. RXE_TYPE_MC_ELEM,
  53. RXE_NUM_TYPES, /* keep me last */
  54. };
  55. struct rxe_type_info {
  56. char *name;
  57. size_t size;
  58. void (*cleanup)(void *obj);
  59. enum rxe_pool_flags flags;
  60. u32 max_index;
  61. u32 min_index;
  62. size_t key_offset;
  63. size_t key_size;
  64. struct kmem_cache *cache;
  65. };
  66. extern struct rxe_type_info rxe_type_info[];
  67. enum rxe_pool_state {
  68. rxe_pool_invalid,
  69. rxe_pool_valid,
  70. };
  71. struct rxe_pool_entry {
  72. struct rxe_pool *pool;
  73. struct kref ref_cnt;
  74. struct list_head list;
  75. /* only used if indexed or keyed */
  76. struct rb_node node;
  77. u32 index;
  78. };
  79. struct rxe_pool {
  80. struct rxe_dev *rxe;
  81. spinlock_t pool_lock; /* pool spinlock */
  82. size_t elem_size;
  83. struct kref ref_cnt;
  84. void (*cleanup)(void *obj);
  85. enum rxe_pool_state state;
  86. enum rxe_pool_flags flags;
  87. enum rxe_elem_type type;
  88. unsigned int max_elem;
  89. atomic_t num_elem;
  90. /* only used if indexed or keyed */
  91. struct rb_root tree;
  92. unsigned long *table;
  93. size_t table_size;
  94. u32 max_index;
  95. u32 min_index;
  96. u32 last;
  97. size_t key_offset;
  98. size_t key_size;
  99. };
  100. /* initialize slab caches for managed objects */
  101. int rxe_cache_init(void);
  102. /* cleanup slab caches for managed objects */
  103. void rxe_cache_exit(void);
  104. /* initialize a pool of objects with given limit on
  105. * number of elements. gets parameters from rxe_type_info
  106. * pool elements will be allocated out of a slab cache
  107. */
  108. int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
  109. enum rxe_elem_type type, u32 max_elem);
  110. /* free resources from object pool */
  111. int rxe_pool_cleanup(struct rxe_pool *pool);
  112. /* allocate an object from pool */
  113. void *rxe_alloc(struct rxe_pool *pool);
  114. /* assign an index to an indexed object and insert object into
  115. * pool's rb tree
  116. */
  117. void rxe_add_index(void *elem);
  118. /* drop an index and remove object from rb tree */
  119. void rxe_drop_index(void *elem);
  120. /* assign a key to a keyed object and insert object into
  121. * pool's rb tree
  122. */
  123. void rxe_add_key(void *elem, void *key);
  124. /* remove elem from rb tree */
  125. void rxe_drop_key(void *elem);
  126. /* lookup an indexed object from index. takes a reference on object */
  127. void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
  128. /* lookup keyed object from key. takes a reference on the object */
  129. void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
  130. /* cleanup an object when all references are dropped */
  131. void rxe_elem_release(struct kref *kref);
  132. /* take a reference on an object */
  133. #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
  134. /* drop a reference on an object */
  135. #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
  136. #endif /* RXE_POOL_H */