ipz_pt_fn.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. void *ipz_qpageit_get_inc(struct ipz_queue *queue)
  43. {
  44. void *ret = ipz_qeit_get(queue);
  45. queue->current_q_offset += queue->pagesize;
  46. if (queue->current_q_offset > queue->queue_length) {
  47. queue->current_q_offset -= queue->pagesize;
  48. ret = NULL;
  49. }
  50. if (((u64)ret) % EHCA_PAGESIZE) {
  51. ehca_gen_err("ERROR!! not at PAGE-Boundary");
  52. return NULL;
  53. }
  54. return ret;
  55. }
  56. void *ipz_qeit_eq_get_inc(struct ipz_queue *queue)
  57. {
  58. void *ret = ipz_qeit_get(queue);
  59. u64 last_entry_in_q = queue->queue_length - queue->qe_size;
  60. queue->current_q_offset += queue->qe_size;
  61. if (queue->current_q_offset > last_entry_in_q) {
  62. queue->current_q_offset = 0;
  63. queue->toggle_state = (~queue->toggle_state) & 1;
  64. }
  65. return ret;
  66. }
  67. int ipz_queue_abs_to_offset(struct ipz_queue *queue, u64 addr, u64 *q_offset)
  68. {
  69. int i;
  70. for (i = 0; i < queue->queue_length / queue->pagesize; i++) {
  71. u64 page = (u64)virt_to_abs(queue->queue_pages[i]);
  72. if (addr >= page && addr < page + queue->pagesize) {
  73. *q_offset = addr - page + i * queue->pagesize;
  74. return 0;
  75. }
  76. }
  77. return -EINVAL;
  78. }
  79. int ipz_queue_ctor(struct ipz_queue *queue,
  80. const u32 nr_of_pages,
  81. const u32 pagesize, const u32 qe_size, const u32 nr_of_sg)
  82. {
  83. int pages_per_kpage = PAGE_SIZE >> EHCA_PAGESHIFT;
  84. int f;
  85. if (pagesize > PAGE_SIZE) {
  86. ehca_gen_err("FATAL ERROR: pagesize=%x is greater "
  87. "than kernel page size", pagesize);
  88. return 0;
  89. }
  90. if (!pages_per_kpage) {
  91. ehca_gen_err("FATAL ERROR: invalid kernel page size. "
  92. "pages_per_kpage=%x", pages_per_kpage);
  93. return 0;
  94. }
  95. queue->queue_length = nr_of_pages * pagesize;
  96. queue->queue_pages = vmalloc(nr_of_pages * sizeof(void *));
  97. if (!queue->queue_pages) {
  98. ehca_gen_err("ERROR!! didn't get the memory");
  99. return 0;
  100. }
  101. memset(queue->queue_pages, 0, nr_of_pages * sizeof(void *));
  102. /*
  103. * allocate pages for queue:
  104. * outer loop allocates whole kernel pages (page aligned) and
  105. * inner loop divides a kernel page into smaller hca queue pages
  106. */
  107. f = 0;
  108. while (f < nr_of_pages) {
  109. u8 *kpage = (u8*)get_zeroed_page(GFP_KERNEL);
  110. int k;
  111. if (!kpage)
  112. goto ipz_queue_ctor_exit0; /*NOMEM*/
  113. for (k = 0; k < pages_per_kpage && f < nr_of_pages; k++) {
  114. (queue->queue_pages)[f] = (struct ipz_page *)kpage;
  115. kpage += EHCA_PAGESIZE;
  116. f++;
  117. }
  118. }
  119. queue->current_q_offset = 0;
  120. queue->qe_size = qe_size;
  121. queue->act_nr_of_sg = nr_of_sg;
  122. queue->pagesize = pagesize;
  123. queue->toggle_state = 1;
  124. return 1;
  125. ipz_queue_ctor_exit0:
  126. ehca_gen_err("Couldn't get alloc pages queue=%p f=%x nr_of_pages=%x",
  127. queue, f, nr_of_pages);
  128. for (f = 0; f < nr_of_pages; f += pages_per_kpage) {
  129. if (!(queue->queue_pages)[f])
  130. break;
  131. free_page((unsigned long)(queue->queue_pages)[f]);
  132. }
  133. return 0;
  134. }
  135. int ipz_queue_dtor(struct ipz_queue *queue)
  136. {
  137. int pages_per_kpage = PAGE_SIZE >> EHCA_PAGESHIFT;
  138. int g;
  139. int nr_pages;
  140. if (!queue || !queue->queue_pages) {
  141. ehca_gen_dbg("queue or queue_pages is NULL");
  142. return 0;
  143. }
  144. nr_pages = queue->queue_length / queue->pagesize;
  145. for (g = 0; g < nr_pages; g += pages_per_kpage)
  146. free_page((unsigned long)(queue->queue_pages)[g]);
  147. vfree(queue->queue_pages);
  148. return 1;
  149. }