ehca_uverbs.c 8.1 KB

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