ipz_pt_fn.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * IBM eServer eHCA Infiniband device driver for Linux on POWER
  3. *
  4. * internal queue handling
  5. *
  6. * Authors: Waleri Fomin <fomin@de.ibm.com>
  7. * Reinhard Ernst <rernst@de.ibm.com>
  8. * Christoph Raisch <raisch@de.ibm.com>
  9. *
  10. * Copyright (c) 2005 IBM Corporation
  11. *
  12. * All rights reserved.
  13. *
  14. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  15. * BSD.
  16. *
  17. * OpenIB BSD License
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions are met:
  21. *
  22. * Redistributions of source code must retain the above copyright notice, this
  23. * list of conditions and the following disclaimer.
  24. *
  25. * Redistributions in binary form must reproduce the above copyright notice,
  26. * this list of conditions and the following disclaimer in the documentation
  27. * and/or other materials
  28. * provided with the distribution.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  37. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  38. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. #ifndef __IPZ_PT_FN_H__
  43. #define __IPZ_PT_FN_H__
  44. #define EHCA_PAGESHIFT 12
  45. #define EHCA_PAGESIZE 4096UL
  46. #define EHCA_PAGEMASK (~(EHCA_PAGESIZE-1))
  47. #define EHCA_PT_ENTRIES 512UL
  48. #include "ehca_tools.h"
  49. #include "ehca_qes.h"
  50. /* struct generic ehca page */
  51. struct ipz_page {
  52. u8 entries[EHCA_PAGESIZE];
  53. };
  54. /* struct generic queue in linux kernel virtual memory (kv) */
  55. struct ipz_queue {
  56. u64 current_q_offset; /* current queue entry */
  57. struct ipz_page **queue_pages; /* array of pages belonging to queue */
  58. u32 qe_size; /* queue entry size */
  59. u32 act_nr_of_sg;
  60. u32 queue_length; /* queue length allocated in bytes */
  61. u32 pagesize;
  62. u32 toggle_state; /* toggle flag - per page */
  63. u32 dummy3; /* 64 bit alignment */
  64. };
  65. /*
  66. * return current Queue Entry for a certain q_offset
  67. * returns address (kv) of Queue Entry
  68. */
  69. static inline void *ipz_qeit_calc(struct ipz_queue *queue, u64 q_offset)
  70. {
  71. struct ipz_page *current_page;
  72. if (q_offset >= queue->queue_length)
  73. return NULL;
  74. current_page = (queue->queue_pages)[q_offset >> EHCA_PAGESHIFT];
  75. return &current_page->entries[q_offset & (EHCA_PAGESIZE - 1)];
  76. }
  77. /*
  78. * return current Queue Entry
  79. * returns address (kv) of Queue Entry
  80. */
  81. static inline void *ipz_qeit_get(struct ipz_queue *queue)
  82. {
  83. return ipz_qeit_calc(queue, queue->current_q_offset);
  84. }
  85. /*
  86. * return current Queue Page , increment Queue Page iterator from
  87. * page to page in struct ipz_queue, last increment will return 0! and
  88. * NOT wrap
  89. * returns address (kv) of Queue Page
  90. * warning don't use in parallel with ipz_QE_get_inc()
  91. */
  92. void *ipz_qpageit_get_inc(struct ipz_queue *queue);
  93. /*
  94. * return current Queue Entry, increment Queue Entry iterator by one
  95. * step in struct ipz_queue, will wrap in ringbuffer
  96. * returns address (kv) of Queue Entry BEFORE increment
  97. * warning don't use in parallel with ipz_qpageit_get_inc()
  98. * warning unpredictable results may occur if steps>act_nr_of_queue_entries
  99. */
  100. static inline void *ipz_qeit_get_inc(struct ipz_queue *queue)
  101. {
  102. void *ret = ipz_qeit_get(queue);
  103. queue->current_q_offset += queue->qe_size;
  104. if (queue->current_q_offset >= queue->queue_length) {
  105. queue->current_q_offset = 0;
  106. /* toggle the valid flag */
  107. queue->toggle_state = (~queue->toggle_state) & 1;
  108. }
  109. return ret;
  110. }
  111. /*
  112. * return current Queue Entry, increment Queue Entry iterator by one
  113. * step in struct ipz_queue, will wrap in ringbuffer
  114. * returns address (kv) of Queue Entry BEFORE increment
  115. * returns 0 and does not increment, if wrong valid state
  116. * warning don't use in parallel with ipz_qpageit_get_inc()
  117. * warning unpredictable results may occur if steps>act_nr_of_queue_entries
  118. */
  119. static inline void *ipz_qeit_get_inc_valid(struct ipz_queue *queue)
  120. {
  121. struct ehca_cqe *cqe = ipz_qeit_get(queue);
  122. u32 cqe_flags = cqe->cqe_flags;
  123. if ((cqe_flags >> 7) != (queue->toggle_state & 1))
  124. return NULL;
  125. ipz_qeit_get_inc(queue);
  126. return cqe;
  127. }
  128. /*
  129. * returns and resets Queue Entry iterator
  130. * returns address (kv) of first Queue Entry
  131. */
  132. static inline void *ipz_qeit_reset(struct ipz_queue *queue)
  133. {
  134. queue->current_q_offset = 0;
  135. return ipz_qeit_get(queue);
  136. }
  137. /* struct generic page table */
  138. struct ipz_pt {
  139. u64 entries[EHCA_PT_ENTRIES];
  140. };
  141. /* struct page table for a queue, only to be used in pf */
  142. struct ipz_qpt {
  143. /* queue page tables (kv), use u64 because we know the element length */
  144. u64 *qpts;
  145. u32 n_qpts;
  146. u32 n_ptes; /* number of page table entries */
  147. u64 *current_pte_addr;
  148. };
  149. /*
  150. * constructor for a ipz_queue_t, placement new for ipz_queue_t,
  151. * new for all dependent datastructors
  152. * all QP Tables are the same
  153. * flow:
  154. * allocate+pin queue
  155. * see ipz_qpt_ctor()
  156. * returns true if ok, false if out of memory
  157. */
  158. int ipz_queue_ctor(struct ipz_queue *queue, const u32 nr_of_pages,
  159. const u32 pagesize, const u32 qe_size,
  160. const u32 nr_of_sg);
  161. /*
  162. * destructor for a ipz_queue_t
  163. * -# free queue
  164. * see ipz_queue_ctor()
  165. * returns true if ok, false if queue was NULL-ptr of free failed
  166. */
  167. int ipz_queue_dtor(struct ipz_queue *queue);
  168. /*
  169. * constructor for a ipz_qpt_t,
  170. * placement new for struct ipz_queue, new for all dependent datastructors
  171. * all QP Tables are the same,
  172. * flow:
  173. * -# allocate+pin queue
  174. * -# initialise ptcb
  175. * -# allocate+pin PTs
  176. * -# link PTs to a ring, according to HCA Arch, set bit62 id needed
  177. * -# the ring must have room for exactly nr_of_PTEs
  178. * see ipz_qpt_ctor()
  179. */
  180. void ipz_qpt_ctor(struct ipz_qpt *qpt,
  181. const u32 nr_of_qes,
  182. const u32 pagesize,
  183. const u32 qe_size,
  184. const u8 lowbyte, const u8 toggle,
  185. u32 * act_nr_of_QEs, u32 * act_nr_of_pages);
  186. /*
  187. * return current Queue Entry, increment Queue Entry iterator by one
  188. * step in struct ipz_queue, will wrap in ringbuffer
  189. * returns address (kv) of Queue Entry BEFORE increment
  190. * warning don't use in parallel with ipz_qpageit_get_inc()
  191. * warning unpredictable results may occur if steps>act_nr_of_queue_entries
  192. * fix EQ page problems
  193. */
  194. void *ipz_qeit_eq_get_inc(struct ipz_queue *queue);
  195. /*
  196. * return current Event Queue Entry, increment Queue Entry iterator
  197. * by one step in struct ipz_queue if valid, will wrap in ringbuffer
  198. * returns address (kv) of Queue Entry BEFORE increment
  199. * returns 0 and does not increment, if wrong valid state
  200. * warning don't use in parallel with ipz_queue_QPageit_get_inc()
  201. * warning unpredictable results may occur if steps>act_nr_of_queue_entries
  202. */
  203. static inline void *ipz_eqit_eq_get_inc_valid(struct ipz_queue *queue)
  204. {
  205. void *ret = ipz_qeit_get(queue);
  206. u32 qe = *(u8 *) ret;
  207. if ((qe >> 7) != (queue->toggle_state & 1))
  208. return NULL;
  209. ipz_qeit_eq_get_inc(queue); /* this is a good one */
  210. return ret;
  211. }
  212. /* returns address (GX) of first queue entry */
  213. static inline u64 ipz_qpt_get_firstpage(struct ipz_qpt *qpt)
  214. {
  215. return be64_to_cpu(qpt->qpts[0]);
  216. }
  217. /* returns address (kv) of first page of queue page table */
  218. static inline void *ipz_qpt_get_qpt(struct ipz_qpt *qpt)
  219. {
  220. return qpt->qpts;
  221. }
  222. #endif /* __IPZ_PT_FN_H__ */