rdmavt_mr.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef DEF_RDMAVT_INCMR_H
  2. #define DEF_RDMAVT_INCMR_H
  3. /*
  4. * Copyright(c) 2016 Intel Corporation.
  5. *
  6. * This file is provided under a dual BSD/GPLv2 license. When using or
  7. * redistributing this file, you may do so under either license.
  8. *
  9. * GPL LICENSE SUMMARY
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * BSD LICENSE
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * - Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * - Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * - Neither the name of Intel Corporation nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. */
  49. /*
  50. * For Memory Regions. This stuff should probably be moved into rdmavt/mr.h once
  51. * drivers no longer need access to the MR directly.
  52. */
  53. #include <linux/percpu-refcount.h>
  54. /*
  55. * A segment is a linear region of low physical memory.
  56. * Used by the verbs layer.
  57. */
  58. struct rvt_seg {
  59. void *vaddr;
  60. size_t length;
  61. };
  62. /* The number of rvt_segs that fit in a page. */
  63. #define RVT_SEGSZ (PAGE_SIZE / sizeof(struct rvt_seg))
  64. struct rvt_segarray {
  65. struct rvt_seg segs[RVT_SEGSZ];
  66. };
  67. struct rvt_mregion {
  68. struct ib_pd *pd; /* shares refcnt of ibmr.pd */
  69. u64 user_base; /* User's address for this region */
  70. u64 iova; /* IB start address of this region */
  71. size_t length;
  72. u32 lkey;
  73. u32 offset; /* offset (bytes) to start of region */
  74. int access_flags;
  75. u32 max_segs; /* number of rvt_segs in all the arrays */
  76. u32 mapsz; /* size of the map array */
  77. atomic_t lkey_invalid; /* true if current lkey is invalid */
  78. u8 page_shift; /* 0 - non unform/non powerof2 sizes */
  79. u8 lkey_published; /* in global table */
  80. struct percpu_ref refcount;
  81. struct completion comp; /* complete when refcount goes to zero */
  82. struct rvt_segarray *map[0]; /* the segments */
  83. };
  84. #define RVT_MAX_LKEY_TABLE_BITS 23
  85. struct rvt_lkey_table {
  86. /* read mostly fields */
  87. u32 max; /* size of the table */
  88. u32 shift; /* lkey/rkey shift */
  89. struct rvt_mregion __rcu **table;
  90. /* writeable fields */
  91. /* protect changes in this struct */
  92. spinlock_t lock ____cacheline_aligned_in_smp;
  93. u32 next; /* next unused index (speeds search) */
  94. u32 gen; /* generation count */
  95. };
  96. /*
  97. * These keep track of the copy progress within a memory region.
  98. * Used by the verbs layer.
  99. */
  100. struct rvt_sge {
  101. struct rvt_mregion *mr;
  102. void *vaddr; /* kernel virtual address of segment */
  103. u32 sge_length; /* length of the SGE */
  104. u32 length; /* remaining length of the segment */
  105. u16 m; /* current index: mr->map[m] */
  106. u16 n; /* current index: mr->map[m]->segs[n] */
  107. };
  108. struct rvt_sge_state {
  109. struct rvt_sge *sg_list; /* next SGE to be used if any */
  110. struct rvt_sge sge; /* progress state for the current SGE */
  111. u32 total_len;
  112. u8 num_sge;
  113. };
  114. static inline void rvt_put_mr(struct rvt_mregion *mr)
  115. {
  116. percpu_ref_put(&mr->refcount);
  117. }
  118. static inline void rvt_get_mr(struct rvt_mregion *mr)
  119. {
  120. percpu_ref_get(&mr->refcount);
  121. }
  122. static inline void rvt_put_ss(struct rvt_sge_state *ss)
  123. {
  124. while (ss->num_sge) {
  125. rvt_put_mr(ss->sge.mr);
  126. if (--ss->num_sge)
  127. ss->sge = *ss->sg_list++;
  128. }
  129. }
  130. static inline u32 rvt_get_sge_length(struct rvt_sge *sge, u32 length)
  131. {
  132. u32 len = sge->length;
  133. if (len > length)
  134. len = length;
  135. if (len > sge->sge_length)
  136. len = sge->sge_length;
  137. return len;
  138. }
  139. static inline void rvt_update_sge(struct rvt_sge_state *ss, u32 length,
  140. bool release)
  141. {
  142. struct rvt_sge *sge = &ss->sge;
  143. sge->vaddr += length;
  144. sge->length -= length;
  145. sge->sge_length -= length;
  146. if (sge->sge_length == 0) {
  147. if (release)
  148. rvt_put_mr(sge->mr);
  149. if (--ss->num_sge)
  150. *sge = *ss->sg_list++;
  151. } else if (sge->length == 0 && sge->mr->lkey) {
  152. if (++sge->n >= RVT_SEGSZ) {
  153. if (++sge->m >= sge->mr->mapsz)
  154. return;
  155. sge->n = 0;
  156. }
  157. sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
  158. sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
  159. }
  160. }
  161. static inline void rvt_skip_sge(struct rvt_sge_state *ss, u32 length,
  162. bool release)
  163. {
  164. struct rvt_sge *sge = &ss->sge;
  165. while (length) {
  166. u32 len = rvt_get_sge_length(sge, length);
  167. WARN_ON_ONCE(len == 0);
  168. rvt_update_sge(ss, len, release);
  169. length -= len;
  170. }
  171. }
  172. bool rvt_ss_has_lkey(struct rvt_sge_state *ss, u32 lkey);
  173. bool rvt_mr_has_lkey(struct rvt_mregion *mr, u32 lkey);
  174. #endif /* DEF_RDMAVT_INCMRH */