ipz_pt_fn.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 ehca_pd;
  51. struct ipz_small_queue_page;
  52. /* struct generic ehca page */
  53. struct ipz_page {
  54. u8 entries[EHCA_PAGESIZE];
  55. };
  56. #define IPZ_SPAGE_PER_KPAGE (PAGE_SIZE / 512)
  57. struct ipz_small_queue_page {
  58. unsigned long page;
  59. unsigned long bitmap[IPZ_SPAGE_PER_KPAGE / BITS_PER_LONG];
  60. int fill;
  61. void *mapped_addr;
  62. u32 mmap_count;
  63. struct list_head list;
  64. };
  65. /* struct generic queue in linux kernel virtual memory (kv) */
  66. struct ipz_queue {
  67. u64 current_q_offset; /* current queue entry */
  68. struct ipz_page **queue_pages; /* array of pages belonging to queue */
  69. u32 qe_size; /* queue entry size */
  70. u32 act_nr_of_sg;
  71. u32 queue_length; /* queue length allocated in bytes */
  72. u32 pagesize;
  73. u32 toggle_state; /* toggle flag - per page */
  74. u32 offset; /* save offset within page for small_qp */
  75. struct ipz_small_queue_page *small_page;
  76. };
  77. /*
  78. * return current Queue Entry for a certain q_offset
  79. * returns address (kv) of Queue Entry
  80. */
  81. static inline void *ipz_qeit_calc(struct ipz_queue *queue, u64 q_offset)
  82. {
  83. struct ipz_page *current_page;
  84. if (q_offset >= queue->queue_length)
  85. return NULL;
  86. current_page = (queue->queue_pages)[q_offset >> EHCA_PAGESHIFT];
  87. return &current_page->entries[q_offset & (EHCA_PAGESIZE - 1)];
  88. }
  89. /*
  90. * return current Queue Entry
  91. * returns address (kv) of Queue Entry
  92. */
  93. static inline void *ipz_qeit_get(struct ipz_queue *queue)
  94. {
  95. return ipz_qeit_calc(queue, queue->current_q_offset);
  96. }
  97. /*
  98. * return current Queue Page , increment Queue Page iterator from
  99. * page to page in struct ipz_queue, last increment will return 0! and
  100. * NOT wrap
  101. * returns address (kv) of Queue Page
  102. * warning don't use in parallel with ipz_QE_get_inc()
  103. */
  104. void *ipz_qpageit_get_inc(struct ipz_queue *queue);
  105. /*
  106. * return current Queue Entry, increment Queue Entry iterator by one
  107. * step in struct ipz_queue, will wrap in ringbuffer
  108. * returns address (kv) of Queue Entry BEFORE increment
  109. * warning don't use in parallel with ipz_qpageit_get_inc()
  110. */
  111. static inline void *ipz_qeit_get_inc(struct ipz_queue *queue)
  112. {
  113. void *ret = ipz_qeit_get(queue);
  114. queue->current_q_offset += queue->qe_size;
  115. if (queue->current_q_offset >= queue->queue_length) {
  116. queue->current_q_offset = 0;
  117. /* toggle the valid flag */
  118. queue->toggle_state = (~queue->toggle_state) & 1;
  119. }
  120. return ret;
  121. }
  122. /*
  123. * return a bool indicating whether current Queue Entry is valid
  124. */
  125. static inline int ipz_qeit_is_valid(struct ipz_queue *queue)
  126. {
  127. struct ehca_cqe *cqe = ipz_qeit_get(queue);
  128. return ((cqe->cqe_flags >> 7) == (queue->toggle_state & 1));
  129. }
  130. /*
  131. * return current Queue Entry, increment Queue Entry iterator by one
  132. * step in struct ipz_queue, will wrap in ringbuffer
  133. * returns address (kv) of Queue Entry BEFORE increment
  134. * returns 0 and does not increment, if wrong valid state
  135. * warning don't use in parallel with ipz_qpageit_get_inc()
  136. */
  137. static inline void *ipz_qeit_get_inc_valid(struct ipz_queue *queue)
  138. {
  139. return ipz_qeit_is_valid(queue) ? ipz_qeit_get_inc(queue) : NULL;
  140. }
  141. /*
  142. * returns and resets Queue Entry iterator
  143. * returns address (kv) of first Queue Entry
  144. */
  145. static inline void *ipz_qeit_reset(struct ipz_queue *queue)
  146. {
  147. queue->current_q_offset = 0;
  148. return ipz_qeit_get(queue);
  149. }
  150. /*
  151. * return the q_offset corresponding to an absolute address
  152. */
  153. int ipz_queue_abs_to_offset(struct ipz_queue *queue, u64 addr, u64 *q_offset);
  154. /*
  155. * return the next queue offset. don't modify the queue.
  156. */
  157. static inline u64 ipz_queue_advance_offset(struct ipz_queue *queue, u64 offset)
  158. {
  159. offset += queue->qe_size;
  160. if (offset >= queue->queue_length) offset = 0;
  161. return offset;
  162. }
  163. /* struct generic page table */
  164. struct ipz_pt {
  165. u64 entries[EHCA_PT_ENTRIES];
  166. };
  167. /* struct page table for a queue, only to be used in pf */
  168. struct ipz_qpt {
  169. /* queue page tables (kv), use u64 because we know the element length */
  170. u64 *qpts;
  171. u32 n_qpts;
  172. u32 n_ptes; /* number of page table entries */
  173. u64 *current_pte_addr;
  174. };
  175. /*
  176. * constructor for a ipz_queue_t, placement new for ipz_queue_t,
  177. * new for all dependent datastructors
  178. * all QP Tables are the same
  179. * flow:
  180. * allocate+pin queue
  181. * see ipz_qpt_ctor()
  182. * returns true if ok, false if out of memory
  183. */
  184. int ipz_queue_ctor(struct ehca_pd *pd, struct ipz_queue *queue,
  185. const u32 nr_of_pages, const u32 pagesize,
  186. const u32 qe_size, const u32 nr_of_sg,
  187. int is_small);
  188. /*
  189. * destructor for a ipz_queue_t
  190. * -# free queue
  191. * see ipz_queue_ctor()
  192. * returns true if ok, false if queue was NULL-ptr of free failed
  193. */
  194. int ipz_queue_dtor(struct ehca_pd *pd, struct ipz_queue *queue);
  195. /*
  196. * constructor for a ipz_qpt_t,
  197. * placement new for struct ipz_queue, new for all dependent datastructors
  198. * all QP Tables are the same,
  199. * flow:
  200. * -# allocate+pin queue
  201. * -# initialise ptcb
  202. * -# allocate+pin PTs
  203. * -# link PTs to a ring, according to HCA Arch, set bit62 id needed
  204. * -# the ring must have room for exactly nr_of_PTEs
  205. * see ipz_qpt_ctor()
  206. */
  207. void ipz_qpt_ctor(struct ipz_qpt *qpt,
  208. const u32 nr_of_qes,
  209. const u32 pagesize,
  210. const u32 qe_size,
  211. const u8 lowbyte, const u8 toggle,
  212. u32 * act_nr_of_QEs, u32 * act_nr_of_pages);
  213. /*
  214. * return current Queue Entry, increment Queue Entry iterator by one
  215. * step in struct ipz_queue, will wrap in ringbuffer
  216. * returns address (kv) of Queue Entry BEFORE increment
  217. * warning don't use in parallel with ipz_qpageit_get_inc()
  218. * warning unpredictable results may occur if steps>act_nr_of_queue_entries
  219. * fix EQ page problems
  220. */
  221. void *ipz_qeit_eq_get_inc(struct ipz_queue *queue);
  222. /*
  223. * return current Event Queue Entry, increment Queue Entry iterator
  224. * by one step in struct ipz_queue if valid, will wrap in ringbuffer
  225. * returns address (kv) of Queue Entry BEFORE increment
  226. * returns 0 and does not increment, if wrong valid state
  227. * warning don't use in parallel with ipz_queue_QPageit_get_inc()
  228. * warning unpredictable results may occur if steps>act_nr_of_queue_entries
  229. */
  230. static inline void *ipz_eqit_eq_get_inc_valid(struct ipz_queue *queue)
  231. {
  232. void *ret = ipz_qeit_get(queue);
  233. u32 qe = *(u8 *)ret;
  234. if ((qe >> 7) != (queue->toggle_state & 1))
  235. return NULL;
  236. ipz_qeit_eq_get_inc(queue); /* this is a good one */
  237. return ret;
  238. }
  239. static inline void *ipz_eqit_eq_peek_valid(struct ipz_queue *queue)
  240. {
  241. void *ret = ipz_qeit_get(queue);
  242. u32 qe = *(u8 *)ret;
  243. if ((qe >> 7) != (queue->toggle_state & 1))
  244. return NULL;
  245. return ret;
  246. }
  247. /* returns address (GX) of first queue entry */
  248. static inline u64 ipz_qpt_get_firstpage(struct ipz_qpt *qpt)
  249. {
  250. return be64_to_cpu(qpt->qpts[0]);
  251. }
  252. /* returns address (kv) of first page of queue page table */
  253. static inline void *ipz_qpt_get_qpt(struct ipz_qpt *qpt)
  254. {
  255. return qpt->qpts;
  256. }
  257. #endif /* __IPZ_PT_FN_H__ */