ehca_cq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * IBM eServer eHCA Infiniband device driver for Linux on POWER
  3. *
  4. * Completion queue handling
  5. *
  6. * Authors: Waleri Fomin <fomin@de.ibm.com>
  7. * Khadija Souissi <souissi@de.ibm.com>
  8. * Reinhard Ernst <rernst@de.ibm.com>
  9. * Heiko J Schick <schickhj@de.ibm.com>
  10. * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
  11. *
  12. *
  13. * Copyright (c) 2005 IBM Corporation
  14. *
  15. * All rights reserved.
  16. *
  17. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  18. * BSD.
  19. *
  20. * OpenIB BSD License
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions are met:
  24. *
  25. * Redistributions of source code must retain the above copyright notice, this
  26. * list of conditions and the following disclaimer.
  27. *
  28. * Redistributions in binary form must reproduce the above copyright notice,
  29. * this list of conditions and the following disclaimer in the documentation
  30. * and/or other materials
  31. * provided with the distribution.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  34. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  35. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  37. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  38. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  39. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  40. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  41. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  43. * POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. #include <asm/current.h>
  46. #include "ehca_iverbs.h"
  47. #include "ehca_classes.h"
  48. #include "ehca_irq.h"
  49. #include "hcp_if.h"
  50. static struct kmem_cache *cq_cache;
  51. int ehca_cq_assign_qp(struct ehca_cq *cq, struct ehca_qp *qp)
  52. {
  53. unsigned int qp_num = qp->real_qp_num;
  54. unsigned int key = qp_num & (QP_HASHTAB_LEN-1);
  55. unsigned long spl_flags;
  56. spin_lock_irqsave(&cq->spinlock, spl_flags);
  57. hlist_add_head(&qp->list_entries, &cq->qp_hashtab[key]);
  58. spin_unlock_irqrestore(&cq->spinlock, spl_flags);
  59. ehca_dbg(cq->ib_cq.device, "cq_num=%x real_qp_num=%x",
  60. cq->cq_number, qp_num);
  61. return 0;
  62. }
  63. int ehca_cq_unassign_qp(struct ehca_cq *cq, unsigned int real_qp_num)
  64. {
  65. int ret = -EINVAL;
  66. unsigned int key = real_qp_num & (QP_HASHTAB_LEN-1);
  67. struct hlist_node *iter;
  68. struct ehca_qp *qp;
  69. unsigned long spl_flags;
  70. spin_lock_irqsave(&cq->spinlock, spl_flags);
  71. hlist_for_each(iter, &cq->qp_hashtab[key]) {
  72. qp = hlist_entry(iter, struct ehca_qp, list_entries);
  73. if (qp->real_qp_num == real_qp_num) {
  74. hlist_del(iter);
  75. ehca_dbg(cq->ib_cq.device,
  76. "removed qp from cq .cq_num=%x real_qp_num=%x",
  77. cq->cq_number, real_qp_num);
  78. ret = 0;
  79. break;
  80. }
  81. }
  82. spin_unlock_irqrestore(&cq->spinlock, spl_flags);
  83. if (ret)
  84. ehca_err(cq->ib_cq.device,
  85. "qp not found cq_num=%x real_qp_num=%x",
  86. cq->cq_number, real_qp_num);
  87. return ret;
  88. }
  89. struct ehca_qp* ehca_cq_get_qp(struct ehca_cq *cq, int real_qp_num)
  90. {
  91. struct ehca_qp *ret = NULL;
  92. unsigned int key = real_qp_num & (QP_HASHTAB_LEN-1);
  93. struct hlist_node *iter;
  94. struct ehca_qp *qp;
  95. hlist_for_each(iter, &cq->qp_hashtab[key]) {
  96. qp = hlist_entry(iter, struct ehca_qp, list_entries);
  97. if (qp->real_qp_num == real_qp_num) {
  98. ret = qp;
  99. break;
  100. }
  101. }
  102. return ret;
  103. }
  104. struct ib_cq *ehca_create_cq(struct ib_device *device, int cqe,
  105. struct ib_ucontext *context,
  106. struct ib_udata *udata)
  107. {
  108. static const u32 additional_cqe = 20;
  109. struct ib_cq *cq;
  110. struct ehca_cq *my_cq;
  111. struct ehca_shca *shca =
  112. container_of(device, struct ehca_shca, ib_device);
  113. struct ipz_adapter_handle adapter_handle;
  114. struct ehca_alloc_cq_parms param; /* h_call's out parameters */
  115. struct h_galpa gal;
  116. void *vpage;
  117. u32 counter;
  118. u64 rpage, cqx_fec, h_ret;
  119. int ipz_rc, ret, i;
  120. unsigned long flags;
  121. if (cqe >= 0xFFFFFFFF - 64 - additional_cqe)
  122. return ERR_PTR(-EINVAL);
  123. my_cq = kmem_cache_zalloc(cq_cache, GFP_KERNEL);
  124. if (!my_cq) {
  125. ehca_err(device, "Out of memory for ehca_cq struct device=%p",
  126. device);
  127. return ERR_PTR(-ENOMEM);
  128. }
  129. memset(&param, 0, sizeof(struct ehca_alloc_cq_parms));
  130. spin_lock_init(&my_cq->spinlock);
  131. spin_lock_init(&my_cq->cb_lock);
  132. spin_lock_init(&my_cq->task_lock);
  133. my_cq->ownpid = current->tgid;
  134. cq = &my_cq->ib_cq;
  135. adapter_handle = shca->ipz_hca_handle;
  136. param.eq_handle = shca->eq.ipz_eq_handle;
  137. do {
  138. if (!idr_pre_get(&ehca_cq_idr, GFP_KERNEL)) {
  139. cq = ERR_PTR(-ENOMEM);
  140. ehca_err(device, "Can't reserve idr nr. device=%p",
  141. device);
  142. goto create_cq_exit1;
  143. }
  144. spin_lock_irqsave(&ehca_cq_idr_lock, flags);
  145. ret = idr_get_new(&ehca_cq_idr, my_cq, &my_cq->token);
  146. spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  147. } while (ret == -EAGAIN);
  148. if (ret) {
  149. cq = ERR_PTR(-ENOMEM);
  150. ehca_err(device, "Can't allocate new idr entry. device=%p",
  151. device);
  152. goto create_cq_exit1;
  153. }
  154. /*
  155. * CQs maximum depth is 4GB-64, but we need additional 20 as buffer
  156. * for receiving errors CQEs.
  157. */
  158. param.nr_cqe = cqe + additional_cqe;
  159. h_ret = hipz_h_alloc_resource_cq(adapter_handle, my_cq, &param);
  160. if (h_ret != H_SUCCESS) {
  161. ehca_err(device, "hipz_h_alloc_resource_cq() failed "
  162. "h_ret=%lx device=%p", h_ret, device);
  163. cq = ERR_PTR(ehca2ib_return_code(h_ret));
  164. goto create_cq_exit2;
  165. }
  166. ipz_rc = ipz_queue_ctor(&my_cq->ipz_queue, param.act_pages,
  167. EHCA_PAGESIZE, sizeof(struct ehca_cqe), 0);
  168. if (!ipz_rc) {
  169. ehca_err(device, "ipz_queue_ctor() failed ipz_rc=%x device=%p",
  170. ipz_rc, device);
  171. cq = ERR_PTR(-EINVAL);
  172. goto create_cq_exit3;
  173. }
  174. for (counter = 0; counter < param.act_pages; counter++) {
  175. vpage = ipz_qpageit_get_inc(&my_cq->ipz_queue);
  176. if (!vpage) {
  177. ehca_err(device, "ipz_qpageit_get_inc() "
  178. "returns NULL device=%p", device);
  179. cq = ERR_PTR(-EAGAIN);
  180. goto create_cq_exit4;
  181. }
  182. rpage = virt_to_abs(vpage);
  183. h_ret = hipz_h_register_rpage_cq(adapter_handle,
  184. my_cq->ipz_cq_handle,
  185. &my_cq->pf,
  186. 0,
  187. 0,
  188. rpage,
  189. 1,
  190. my_cq->galpas.
  191. kernel);
  192. if (h_ret < H_SUCCESS) {
  193. ehca_err(device, "hipz_h_register_rpage_cq() failed "
  194. "ehca_cq=%p cq_num=%x h_ret=%lx counter=%i "
  195. "act_pages=%i", my_cq, my_cq->cq_number,
  196. h_ret, counter, param.act_pages);
  197. cq = ERR_PTR(-EINVAL);
  198. goto create_cq_exit4;
  199. }
  200. if (counter == (param.act_pages - 1)) {
  201. vpage = ipz_qpageit_get_inc(&my_cq->ipz_queue);
  202. if ((h_ret != H_SUCCESS) || vpage) {
  203. ehca_err(device, "Registration of pages not "
  204. "complete ehca_cq=%p cq_num=%x "
  205. "h_ret=%lx", my_cq, my_cq->cq_number,
  206. h_ret);
  207. cq = ERR_PTR(-EAGAIN);
  208. goto create_cq_exit4;
  209. }
  210. } else {
  211. if (h_ret != H_PAGE_REGISTERED) {
  212. ehca_err(device, "Registration of page failed "
  213. "ehca_cq=%p cq_num=%x h_ret=%lx"
  214. "counter=%i act_pages=%i",
  215. my_cq, my_cq->cq_number,
  216. h_ret, counter, param.act_pages);
  217. cq = ERR_PTR(-ENOMEM);
  218. goto create_cq_exit4;
  219. }
  220. }
  221. }
  222. ipz_qeit_reset(&my_cq->ipz_queue);
  223. gal = my_cq->galpas.kernel;
  224. cqx_fec = hipz_galpa_load(gal, CQTEMM_OFFSET(cqx_fec));
  225. ehca_dbg(device, "ehca_cq=%p cq_num=%x CQX_FEC=%lx",
  226. my_cq, my_cq->cq_number, cqx_fec);
  227. my_cq->ib_cq.cqe = my_cq->nr_of_entries =
  228. param.act_nr_of_entries - additional_cqe;
  229. my_cq->cq_number = (my_cq->ipz_cq_handle.handle) & 0xffff;
  230. for (i = 0; i < QP_HASHTAB_LEN; i++)
  231. INIT_HLIST_HEAD(&my_cq->qp_hashtab[i]);
  232. if (context) {
  233. struct ipz_queue *ipz_queue = &my_cq->ipz_queue;
  234. struct ehca_create_cq_resp resp;
  235. memset(&resp, 0, sizeof(resp));
  236. resp.cq_number = my_cq->cq_number;
  237. resp.token = my_cq->token;
  238. resp.ipz_queue.qe_size = ipz_queue->qe_size;
  239. resp.ipz_queue.act_nr_of_sg = ipz_queue->act_nr_of_sg;
  240. resp.ipz_queue.queue_length = ipz_queue->queue_length;
  241. resp.ipz_queue.pagesize = ipz_queue->pagesize;
  242. resp.ipz_queue.toggle_state = ipz_queue->toggle_state;
  243. if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
  244. ehca_err(device, "Copy to udata failed.");
  245. goto create_cq_exit4;
  246. }
  247. }
  248. return cq;
  249. create_cq_exit4:
  250. ipz_queue_dtor(&my_cq->ipz_queue);
  251. create_cq_exit3:
  252. h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
  253. if (h_ret != H_SUCCESS)
  254. ehca_err(device, "hipz_h_destroy_cq() failed ehca_cq=%p "
  255. "cq_num=%x h_ret=%lx", my_cq, my_cq->cq_number, h_ret);
  256. create_cq_exit2:
  257. spin_lock_irqsave(&ehca_cq_idr_lock, flags);
  258. idr_remove(&ehca_cq_idr, my_cq->token);
  259. spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  260. create_cq_exit1:
  261. kmem_cache_free(cq_cache, my_cq);
  262. return cq;
  263. }
  264. int ehca_destroy_cq(struct ib_cq *cq)
  265. {
  266. u64 h_ret;
  267. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  268. int cq_num = my_cq->cq_number;
  269. struct ib_device *device = cq->device;
  270. struct ehca_shca *shca = container_of(device, struct ehca_shca,
  271. ib_device);
  272. struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
  273. u32 cur_pid = current->tgid;
  274. unsigned long flags;
  275. if (cq->uobject) {
  276. if (my_cq->mm_count_galpa || my_cq->mm_count_queue) {
  277. ehca_err(device, "Resources still referenced in "
  278. "user space cq_num=%x", my_cq->cq_number);
  279. return -EINVAL;
  280. }
  281. if (my_cq->ownpid != cur_pid) {
  282. ehca_err(device, "Invalid caller pid=%x ownpid=%x "
  283. "cq_num=%x",
  284. cur_pid, my_cq->ownpid, my_cq->cq_number);
  285. return -EINVAL;
  286. }
  287. }
  288. spin_lock_irqsave(&ehca_cq_idr_lock, flags);
  289. while (my_cq->nr_callbacks) {
  290. spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  291. yield();
  292. spin_lock_irqsave(&ehca_cq_idr_lock, flags);
  293. }
  294. idr_remove(&ehca_cq_idr, my_cq->token);
  295. spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  296. h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 0);
  297. if (h_ret == H_R_STATE) {
  298. /* cq in err: read err data and destroy it forcibly */
  299. ehca_dbg(device, "ehca_cq=%p cq_num=%x ressource=%lx in err "
  300. "state. Try to delete it forcibly.",
  301. my_cq, cq_num, my_cq->ipz_cq_handle.handle);
  302. ehca_error_data(shca, my_cq, my_cq->ipz_cq_handle.handle);
  303. h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
  304. if (h_ret == H_SUCCESS)
  305. ehca_dbg(device, "cq_num=%x deleted successfully.",
  306. cq_num);
  307. }
  308. if (h_ret != H_SUCCESS) {
  309. ehca_err(device, "hipz_h_destroy_cq() failed h_ret=%lx "
  310. "ehca_cq=%p cq_num=%x", h_ret, my_cq, cq_num);
  311. return ehca2ib_return_code(h_ret);
  312. }
  313. ipz_queue_dtor(&my_cq->ipz_queue);
  314. kmem_cache_free(cq_cache, my_cq);
  315. return 0;
  316. }
  317. int ehca_resize_cq(struct ib_cq *cq, int cqe, struct ib_udata *udata)
  318. {
  319. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  320. u32 cur_pid = current->tgid;
  321. if (cq->uobject && my_cq->ownpid != cur_pid) {
  322. ehca_err(cq->device, "Invalid caller pid=%x ownpid=%x",
  323. cur_pid, my_cq->ownpid);
  324. return -EINVAL;
  325. }
  326. /* TODO: proper resize needs to be done */
  327. ehca_err(cq->device, "not implemented yet");
  328. return -EFAULT;
  329. }
  330. int ehca_init_cq_cache(void)
  331. {
  332. cq_cache = kmem_cache_create("ehca_cache_cq",
  333. sizeof(struct ehca_cq), 0,
  334. SLAB_HWCACHE_ALIGN,
  335. NULL, NULL);
  336. if (!cq_cache)
  337. return -ENOMEM;
  338. return 0;
  339. }
  340. void ehca_cleanup_cq_cache(void)
  341. {
  342. if (cq_cache)
  343. kmem_cache_destroy(cq_cache);
  344. }