ipz_pt_fn.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  13. * BSD.
  14. *
  15. * OpenIB BSD License
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions are met:
  19. *
  20. * Redistributions of source code must retain the above copyright notice, this
  21. * list of conditions and the following disclaimer.
  22. *
  23. * Redistributions in binary form must reproduce the above copyright notice,
  24. * this list of conditions and the following disclaimer in the documentation
  25. * and/or other materials
  26. * provided with the distribution.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  35. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  36. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. #include "ehca_tools.h"
  41. #include "ipz_pt_fn.h"
  42. #include "ehca_classes.h"
  43. #define PAGES_PER_KPAGE (PAGE_SIZE >> EHCA_PAGESHIFT)
  44. struct kmem_cache *small_qp_cache;
  45. void *ipz_qpageit_get_inc(struct ipz_queue *queue)
  46. {
  47. void *ret = ipz_qeit_get(queue);
  48. queue->current_q_offset += queue->pagesize;
  49. if (queue->current_q_offset > queue->queue_length) {
  50. queue->current_q_offset -= queue->pagesize;
  51. ret = NULL;
  52. }
  53. if (((u64)ret) % queue->pagesize) {
  54. ehca_gen_err("ERROR!! not at PAGE-Boundary");
  55. return NULL;
  56. }
  57. return ret;
  58. }
  59. void *ipz_qeit_eq_get_inc(struct ipz_queue *queue)
  60. {
  61. void *ret = ipz_qeit_get(queue);
  62. u64 last_entry_in_q = queue->queue_length - queue->qe_size;
  63. queue->current_q_offset += queue->qe_size;
  64. if (queue->current_q_offset > last_entry_in_q) {
  65. queue->current_q_offset = 0;
  66. queue->toggle_state = (~queue->toggle_state) & 1;
  67. }
  68. return ret;
  69. }
  70. int ipz_queue_abs_to_offset(struct ipz_queue *queue, u64 addr, u64 *q_offset)
  71. {
  72. int i;
  73. for (i = 0; i < queue->queue_length / queue->pagesize; i++) {
  74. u64 page = (u64)virt_to_abs(queue->queue_pages[i]);
  75. if (addr >= page && addr < page + queue->pagesize) {
  76. *q_offset = addr - page + i * queue->pagesize;
  77. return 0;
  78. }
  79. }
  80. return -EINVAL;
  81. }
  82. #if PAGE_SHIFT < EHCA_PAGESHIFT
  83. #error Kernel pages must be at least as large than eHCA pages (4K) !
  84. #endif
  85. /*
  86. * allocate pages for queue:
  87. * outer loop allocates whole kernel pages (page aligned) and
  88. * inner loop divides a kernel page into smaller hca queue pages
  89. */
  90. static int alloc_queue_pages(struct ipz_queue *queue, const u32 nr_of_pages)
  91. {
  92. int k, f = 0;
  93. u8 *kpage;
  94. while (f < nr_of_pages) {
  95. kpage = (u8 *)get_zeroed_page(GFP_KERNEL);
  96. if (!kpage)
  97. goto out;
  98. for (k = 0; k < PAGES_PER_KPAGE && f < nr_of_pages; k++) {
  99. queue->queue_pages[f] = (struct ipz_page *)kpage;
  100. kpage += EHCA_PAGESIZE;
  101. f++;
  102. }
  103. }
  104. return 1;
  105. out:
  106. for (f = 0; f < nr_of_pages && queue->queue_pages[f];
  107. f += PAGES_PER_KPAGE)
  108. free_page((unsigned long)(queue->queue_pages)[f]);
  109. return 0;
  110. }
  111. static int alloc_small_queue_page(struct ipz_queue *queue, struct ehca_pd *pd)
  112. {
  113. int order = ilog2(queue->pagesize) - 9;
  114. struct ipz_small_queue_page *page;
  115. unsigned long bit;
  116. mutex_lock(&pd->lock);
  117. if (!list_empty(&pd->free[order]))
  118. page = list_entry(pd->free[order].next,
  119. struct ipz_small_queue_page, list);
  120. else {
  121. page = kmem_cache_zalloc(small_qp_cache, GFP_KERNEL);
  122. if (!page)
  123. goto out;
  124. page->page = get_zeroed_page(GFP_KERNEL);
  125. if (!page->page) {
  126. kmem_cache_free(small_qp_cache, page);
  127. goto out;
  128. }
  129. list_add(&page->list, &pd->free[order]);
  130. }
  131. bit = find_first_zero_bit(page->bitmap, IPZ_SPAGE_PER_KPAGE >> order);
  132. __set_bit(bit, page->bitmap);
  133. page->fill++;
  134. if (page->fill == IPZ_SPAGE_PER_KPAGE >> order)
  135. list_move(&page->list, &pd->full[order]);
  136. mutex_unlock(&pd->lock);
  137. queue->queue_pages[0] = (void *)(page->page | (bit << (order + 9)));
  138. queue->small_page = page;
  139. queue->offset = bit << (order + 9);
  140. return 1;
  141. out:
  142. ehca_err(pd->ib_pd.device, "failed to allocate small queue page");
  143. mutex_unlock(&pd->lock);
  144. return 0;
  145. }
  146. static void free_small_queue_page(struct ipz_queue *queue, struct ehca_pd *pd)
  147. {
  148. int order = ilog2(queue->pagesize) - 9;
  149. struct ipz_small_queue_page *page = queue->small_page;
  150. unsigned long bit;
  151. int free_page = 0;
  152. bit = ((unsigned long)queue->queue_pages[0] & ~PAGE_MASK)
  153. >> (order + 9);
  154. mutex_lock(&pd->lock);
  155. __clear_bit(bit, page->bitmap);
  156. page->fill--;
  157. if (page->fill == 0) {
  158. list_del(&page->list);
  159. free_page = 1;
  160. }
  161. if (page->fill == (IPZ_SPAGE_PER_KPAGE >> order) - 1)
  162. /* the page was full until we freed the chunk */
  163. list_move_tail(&page->list, &pd->free[order]);
  164. mutex_unlock(&pd->lock);
  165. if (free_page) {
  166. free_page(page->page);
  167. kmem_cache_free(small_qp_cache, page);
  168. }
  169. }
  170. int ipz_queue_ctor(struct ehca_pd *pd, struct ipz_queue *queue,
  171. const u32 nr_of_pages, const u32 pagesize,
  172. const u32 qe_size, const u32 nr_of_sg,
  173. int is_small)
  174. {
  175. if (pagesize > PAGE_SIZE) {
  176. ehca_gen_err("FATAL ERROR: pagesize=%x "
  177. "is greater than kernel page size", pagesize);
  178. return 0;
  179. }
  180. /* init queue fields */
  181. queue->queue_length = nr_of_pages * pagesize;
  182. queue->pagesize = pagesize;
  183. queue->qe_size = qe_size;
  184. queue->act_nr_of_sg = nr_of_sg;
  185. queue->current_q_offset = 0;
  186. queue->toggle_state = 1;
  187. queue->small_page = NULL;
  188. /* allocate queue page pointers */
  189. queue->queue_pages = vmalloc(nr_of_pages * sizeof(void *));
  190. if (!queue->queue_pages) {
  191. ehca_gen_err("Couldn't allocate queue page list");
  192. return 0;
  193. }
  194. memset(queue->queue_pages, 0, nr_of_pages * sizeof(void *));
  195. /* allocate actual queue pages */
  196. if (is_small) {
  197. if (!alloc_small_queue_page(queue, pd))
  198. goto ipz_queue_ctor_exit0;
  199. } else
  200. if (!alloc_queue_pages(queue, nr_of_pages))
  201. goto ipz_queue_ctor_exit0;
  202. return 1;
  203. ipz_queue_ctor_exit0:
  204. ehca_gen_err("Couldn't alloc pages queue=%p "
  205. "nr_of_pages=%x", queue, nr_of_pages);
  206. vfree(queue->queue_pages);
  207. return 0;
  208. }
  209. int ipz_queue_dtor(struct ehca_pd *pd, struct ipz_queue *queue)
  210. {
  211. int i, nr_pages;
  212. if (!queue || !queue->queue_pages) {
  213. ehca_gen_dbg("queue or queue_pages is NULL");
  214. return 0;
  215. }
  216. if (queue->small_page)
  217. free_small_queue_page(queue, pd);
  218. else {
  219. nr_pages = queue->queue_length / queue->pagesize;
  220. for (i = 0; i < nr_pages; i += PAGES_PER_KPAGE)
  221. free_page((unsigned long)queue->queue_pages[i]);
  222. }
  223. vfree(queue->queue_pages);
  224. return 1;
  225. }
  226. int ehca_init_small_qp_cache(void)
  227. {
  228. small_qp_cache = kmem_cache_create("ehca_cache_small_qp",
  229. sizeof(struct ipz_small_queue_page),
  230. 0, SLAB_HWCACHE_ALIGN, NULL);
  231. if (!small_qp_cache)
  232. return -ENOMEM;
  233. return 0;
  234. }
  235. void ehca_cleanup_small_qp_cache(void)
  236. {
  237. kmem_cache_destroy(small_qp_cache);
  238. }