ehca_uverbs.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * IBM eServer eHCA Infiniband device driver for Linux on POWER
  3. *
  4. * userspace support verbs
  5. *
  6. * Authors: Christoph Raisch <raisch@de.ibm.com>
  7. * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
  8. * Heiko J Schick <schickhj@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. #include "ehca_classes.h"
  43. #include "ehca_iverbs.h"
  44. #include "ehca_mrmw.h"
  45. #include "ehca_tools.h"
  46. #include "hcp_if.h"
  47. struct ib_ucontext *ehca_alloc_ucontext(struct ib_device *device,
  48. struct ib_udata *udata)
  49. {
  50. struct ehca_ucontext *my_context;
  51. my_context = kzalloc(sizeof *my_context, GFP_KERNEL);
  52. if (!my_context) {
  53. ehca_err(device, "Out of memory device=%p", device);
  54. return ERR_PTR(-ENOMEM);
  55. }
  56. return &my_context->ib_ucontext;
  57. }
  58. int ehca_dealloc_ucontext(struct ib_ucontext *context)
  59. {
  60. kfree(container_of(context, struct ehca_ucontext, ib_ucontext));
  61. return 0;
  62. }
  63. static void ehca_mm_open(struct vm_area_struct *vma)
  64. {
  65. u32 *count = (u32 *)vma->vm_private_data;
  66. if (!count) {
  67. ehca_gen_err("Invalid vma struct vm_start=%lx vm_end=%lx",
  68. vma->vm_start, vma->vm_end);
  69. return;
  70. }
  71. (*count)++;
  72. if (!(*count))
  73. ehca_gen_err("Use count overflow vm_start=%lx vm_end=%lx",
  74. vma->vm_start, vma->vm_end);
  75. ehca_gen_dbg("vm_start=%lx vm_end=%lx count=%x",
  76. vma->vm_start, vma->vm_end, *count);
  77. }
  78. static void ehca_mm_close(struct vm_area_struct *vma)
  79. {
  80. u32 *count = (u32 *)vma->vm_private_data;
  81. if (!count) {
  82. ehca_gen_err("Invalid vma struct vm_start=%lx vm_end=%lx",
  83. vma->vm_start, vma->vm_end);
  84. return;
  85. }
  86. (*count)--;
  87. ehca_gen_dbg("vm_start=%lx vm_end=%lx count=%x",
  88. vma->vm_start, vma->vm_end, *count);
  89. }
  90. static struct vm_operations_struct vm_ops = {
  91. .open = ehca_mm_open,
  92. .close = ehca_mm_close,
  93. };
  94. static int ehca_mmap_fw(struct vm_area_struct *vma, struct h_galpas *galpas,
  95. u32 *mm_count)
  96. {
  97. int ret;
  98. u64 vsize, physical;
  99. vsize = vma->vm_end - vma->vm_start;
  100. if (vsize < EHCA_PAGESIZE) {
  101. ehca_gen_err("invalid vsize=%lx", vma->vm_end - vma->vm_start);
  102. return -EINVAL;
  103. }
  104. physical = galpas->user.fw_handle;
  105. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  106. ehca_gen_dbg("vsize=%lx physical=%lx", vsize, physical);
  107. /* VM_IO | VM_RESERVED are set by remap_pfn_range() */
  108. ret = remap_4k_pfn(vma, vma->vm_start, physical >> EHCA_PAGESHIFT,
  109. vma->vm_page_prot);
  110. if (unlikely(ret)) {
  111. ehca_gen_err("remap_pfn_range() failed ret=%i", ret);
  112. return -ENOMEM;
  113. }
  114. vma->vm_private_data = mm_count;
  115. (*mm_count)++;
  116. vma->vm_ops = &vm_ops;
  117. return 0;
  118. }
  119. static int ehca_mmap_queue(struct vm_area_struct *vma, struct ipz_queue *queue,
  120. u32 *mm_count)
  121. {
  122. int ret;
  123. u64 start, ofs;
  124. struct page *page;
  125. vma->vm_flags |= VM_RESERVED;
  126. start = vma->vm_start;
  127. for (ofs = 0; ofs < queue->queue_length; ofs += PAGE_SIZE) {
  128. u64 virt_addr = (u64)ipz_qeit_calc(queue, ofs);
  129. page = virt_to_page(virt_addr);
  130. ret = vm_insert_page(vma, start, page);
  131. if (unlikely(ret)) {
  132. ehca_gen_err("vm_insert_page() failed rc=%i", ret);
  133. return ret;
  134. }
  135. start += PAGE_SIZE;
  136. }
  137. vma->vm_private_data = mm_count;
  138. (*mm_count)++;
  139. vma->vm_ops = &vm_ops;
  140. return 0;
  141. }
  142. static int ehca_mmap_cq(struct vm_area_struct *vma, struct ehca_cq *cq,
  143. u32 rsrc_type)
  144. {
  145. int ret;
  146. switch (rsrc_type) {
  147. case 0: /* galpa fw handle */
  148. ehca_dbg(cq->ib_cq.device, "cq_num=%x fw", cq->cq_number);
  149. ret = ehca_mmap_fw(vma, &cq->galpas, &cq->mm_count_galpa);
  150. if (unlikely(ret)) {
  151. ehca_err(cq->ib_cq.device,
  152. "ehca_mmap_fw() failed rc=%i cq_num=%x",
  153. ret, cq->cq_number);
  154. return ret;
  155. }
  156. break;
  157. case 1: /* cq queue_addr */
  158. ehca_dbg(cq->ib_cq.device, "cq_num=%x queue", cq->cq_number);
  159. ret = ehca_mmap_queue(vma, &cq->ipz_queue, &cq->mm_count_queue);
  160. if (unlikely(ret)) {
  161. ehca_err(cq->ib_cq.device,
  162. "ehca_mmap_queue() failed rc=%i cq_num=%x",
  163. ret, cq->cq_number);
  164. return ret;
  165. }
  166. break;
  167. default:
  168. ehca_err(cq->ib_cq.device, "bad resource type=%x cq_num=%x",
  169. rsrc_type, cq->cq_number);
  170. return -EINVAL;
  171. }
  172. return 0;
  173. }
  174. static int ehca_mmap_qp(struct vm_area_struct *vma, struct ehca_qp *qp,
  175. u32 rsrc_type)
  176. {
  177. int ret;
  178. switch (rsrc_type) {
  179. case 0: /* galpa fw handle */
  180. ehca_dbg(qp->ib_qp.device, "qp_num=%x fw", qp->ib_qp.qp_num);
  181. ret = ehca_mmap_fw(vma, &qp->galpas, &qp->mm_count_galpa);
  182. if (unlikely(ret)) {
  183. ehca_err(qp->ib_qp.device,
  184. "remap_pfn_range() failed ret=%i qp_num=%x",
  185. ret, qp->ib_qp.qp_num);
  186. return -ENOMEM;
  187. }
  188. break;
  189. case 1: /* qp rqueue_addr */
  190. ehca_dbg(qp->ib_qp.device, "qp_num=%x rqueue",
  191. qp->ib_qp.qp_num);
  192. ret = ehca_mmap_queue(vma, &qp->ipz_rqueue,
  193. &qp->mm_count_rqueue);
  194. if (unlikely(ret)) {
  195. ehca_err(qp->ib_qp.device,
  196. "ehca_mmap_queue(rq) failed rc=%i qp_num=%x",
  197. ret, qp->ib_qp.qp_num);
  198. return ret;
  199. }
  200. break;
  201. case 2: /* qp squeue_addr */
  202. ehca_dbg(qp->ib_qp.device, "qp_num=%x squeue",
  203. qp->ib_qp.qp_num);
  204. ret = ehca_mmap_queue(vma, &qp->ipz_squeue,
  205. &qp->mm_count_squeue);
  206. if (unlikely(ret)) {
  207. ehca_err(qp->ib_qp.device,
  208. "ehca_mmap_queue(sq) failed rc=%i qp_num=%x",
  209. ret, qp->ib_qp.qp_num);
  210. return ret;
  211. }
  212. break;
  213. default:
  214. ehca_err(qp->ib_qp.device, "bad resource type=%x qp=num=%x",
  215. rsrc_type, qp->ib_qp.qp_num);
  216. return -EINVAL;
  217. }
  218. return 0;
  219. }
  220. int ehca_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  221. {
  222. u64 fileoffset = vma->vm_pgoff;
  223. u32 idr_handle = fileoffset & 0x1FFFFFF;
  224. u32 q_type = (fileoffset >> 27) & 0x1; /* CQ, QP,... */
  225. u32 rsrc_type = (fileoffset >> 25) & 0x3; /* sq,rq,cmnd_window */
  226. u32 ret;
  227. struct ehca_cq *cq;
  228. struct ehca_qp *qp;
  229. struct ib_uobject *uobject;
  230. switch (q_type) {
  231. case 0: /* CQ */
  232. read_lock(&ehca_cq_idr_lock);
  233. cq = idr_find(&ehca_cq_idr, idr_handle);
  234. read_unlock(&ehca_cq_idr_lock);
  235. /* make sure this mmap really belongs to the authorized user */
  236. if (!cq)
  237. return -EINVAL;
  238. if (!cq->ib_cq.uobject || cq->ib_cq.uobject->context != context)
  239. return -EINVAL;
  240. ret = ehca_mmap_cq(vma, cq, rsrc_type);
  241. if (unlikely(ret)) {
  242. ehca_err(cq->ib_cq.device,
  243. "ehca_mmap_cq() failed rc=%i cq_num=%x",
  244. ret, cq->cq_number);
  245. return ret;
  246. }
  247. break;
  248. case 1: /* QP */
  249. read_lock(&ehca_qp_idr_lock);
  250. qp = idr_find(&ehca_qp_idr, idr_handle);
  251. read_unlock(&ehca_qp_idr_lock);
  252. /* make sure this mmap really belongs to the authorized user */
  253. if (!qp)
  254. return -EINVAL;
  255. uobject = IS_SRQ(qp) ? qp->ib_srq.uobject : qp->ib_qp.uobject;
  256. if (!uobject || uobject->context != context)
  257. return -EINVAL;
  258. ret = ehca_mmap_qp(vma, qp, rsrc_type);
  259. if (unlikely(ret)) {
  260. ehca_err(qp->ib_qp.device,
  261. "ehca_mmap_qp() failed rc=%i qp_num=%x",
  262. ret, qp->ib_qp.qp_num);
  263. return ret;
  264. }
  265. break;
  266. default:
  267. ehca_gen_err("bad queue type %x", q_type);
  268. return -EINVAL;
  269. }
  270. return 0;
  271. }