ehca_uverbs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <asm/current.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 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=%lx physical=%lx", vsize, physical);
  108. /* VM_IO | VM_RESERVED 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_RESERVED;
  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 rqueue",
  192. qp->ib_qp.qp_num);
  193. ret = ehca_mmap_queue(vma, &qp->ipz_rqueue,
  194. &qp->mm_count_rqueue);
  195. if (unlikely(ret)) {
  196. ehca_err(qp->ib_qp.device,
  197. "ehca_mmap_queue(rq) failed rc=%i qp_num=%x",
  198. ret, qp->ib_qp.qp_num);
  199. return ret;
  200. }
  201. break;
  202. case 2: /* qp squeue_addr */
  203. ehca_dbg(qp->ib_qp.device, "qp_num=%x squeue",
  204. qp->ib_qp.qp_num);
  205. ret = ehca_mmap_queue(vma, &qp->ipz_squeue,
  206. &qp->mm_count_squeue);
  207. if (unlikely(ret)) {
  208. ehca_err(qp->ib_qp.device,
  209. "ehca_mmap_queue(sq) failed rc=%i qp_num=%x",
  210. ret, qp->ib_qp.qp_num);
  211. return ret;
  212. }
  213. break;
  214. default:
  215. ehca_err(qp->ib_qp.device, "bad resource type=%x qp=num=%x",
  216. rsrc_type, qp->ib_qp.qp_num);
  217. return -EINVAL;
  218. }
  219. return 0;
  220. }
  221. int ehca_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  222. {
  223. u64 fileoffset = vma->vm_pgoff;
  224. u32 idr_handle = fileoffset & 0x1FFFFFF;
  225. u32 q_type = (fileoffset >> 27) & 0x1; /* CQ, QP,... */
  226. u32 rsrc_type = (fileoffset >> 25) & 0x3; /* sq,rq,cmnd_window */
  227. u32 cur_pid = current->tgid;
  228. u32 ret;
  229. struct ehca_cq *cq;
  230. struct ehca_qp *qp;
  231. struct ehca_pd *pd;
  232. struct ib_uobject *uobject;
  233. switch (q_type) {
  234. case 0: /* CQ */
  235. read_lock(&ehca_cq_idr_lock);
  236. cq = idr_find(&ehca_cq_idr, idr_handle);
  237. read_unlock(&ehca_cq_idr_lock);
  238. /* make sure this mmap really belongs to the authorized user */
  239. if (!cq)
  240. return -EINVAL;
  241. if (cq->ownpid != cur_pid) {
  242. ehca_err(cq->ib_cq.device,
  243. "Invalid caller pid=%x ownpid=%x",
  244. cur_pid, cq->ownpid);
  245. return -ENOMEM;
  246. }
  247. if (!cq->ib_cq.uobject || cq->ib_cq.uobject->context != context)
  248. return -EINVAL;
  249. ret = ehca_mmap_cq(vma, cq, rsrc_type);
  250. if (unlikely(ret)) {
  251. ehca_err(cq->ib_cq.device,
  252. "ehca_mmap_cq() failed rc=%i cq_num=%x",
  253. ret, cq->cq_number);
  254. return ret;
  255. }
  256. break;
  257. case 1: /* QP */
  258. read_lock(&ehca_qp_idr_lock);
  259. qp = idr_find(&ehca_qp_idr, idr_handle);
  260. read_unlock(&ehca_qp_idr_lock);
  261. /* make sure this mmap really belongs to the authorized user */
  262. if (!qp)
  263. return -EINVAL;
  264. pd = container_of(qp->ib_qp.pd, struct ehca_pd, ib_pd);
  265. if (pd->ownpid != cur_pid) {
  266. ehca_err(qp->ib_qp.device,
  267. "Invalid caller pid=%x ownpid=%x",
  268. cur_pid, pd->ownpid);
  269. return -ENOMEM;
  270. }
  271. uobject = IS_SRQ(qp) ? qp->ib_srq.uobject : qp->ib_qp.uobject;
  272. if (!uobject || uobject->context != context)
  273. return -EINVAL;
  274. ret = ehca_mmap_qp(vma, qp, rsrc_type);
  275. if (unlikely(ret)) {
  276. ehca_err(qp->ib_qp.device,
  277. "ehca_mmap_qp() failed rc=%i qp_num=%x",
  278. ret, qp->ib_qp.qp_num);
  279. return ret;
  280. }
  281. break;
  282. default:
  283. ehca_gen_err("bad queue type %x", q_type);
  284. return -EINVAL;
  285. }
  286. return 0;
  287. }