ehca_cq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <linux/slab.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 flags;
  56. spin_lock_irqsave(&cq->spinlock, flags);
  57. hlist_add_head(&qp->list_entries, &cq->qp_hashtab[key]);
  58. spin_unlock_irqrestore(&cq->spinlock, 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 flags;
  70. spin_lock_irqsave(&cq->spinlock, 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, 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, int comp_vector,
  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. if (!atomic_add_unless(&shca->num_cqs, 1, shca->max_num_cqs)) {
  124. ehca_err(device, "Unable to create CQ, max number of %i "
  125. "CQs reached.", shca->max_num_cqs);
  126. ehca_err(device, "To increase the maximum number of CQs "
  127. "use the number_of_cqs module parameter.\n");
  128. return ERR_PTR(-ENOSPC);
  129. }
  130. my_cq = kmem_cache_zalloc(cq_cache, GFP_KERNEL);
  131. if (!my_cq) {
  132. ehca_err(device, "Out of memory for ehca_cq struct device=%p",
  133. device);
  134. atomic_dec(&shca->num_cqs);
  135. return ERR_PTR(-ENOMEM);
  136. }
  137. memset(&param, 0, sizeof(struct ehca_alloc_cq_parms));
  138. spin_lock_init(&my_cq->spinlock);
  139. spin_lock_init(&my_cq->cb_lock);
  140. spin_lock_init(&my_cq->task_lock);
  141. atomic_set(&my_cq->nr_events, 0);
  142. init_waitqueue_head(&my_cq->wait_completion);
  143. cq = &my_cq->ib_cq;
  144. adapter_handle = shca->ipz_hca_handle;
  145. param.eq_handle = shca->eq.ipz_eq_handle;
  146. do {
  147. if (!idr_pre_get(&ehca_cq_idr, GFP_KERNEL)) {
  148. cq = ERR_PTR(-ENOMEM);
  149. ehca_err(device, "Can't reserve idr nr. device=%p",
  150. device);
  151. goto create_cq_exit1;
  152. }
  153. write_lock_irqsave(&ehca_cq_idr_lock, flags);
  154. ret = idr_get_new(&ehca_cq_idr, my_cq, &my_cq->token);
  155. write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  156. } while (ret == -EAGAIN);
  157. if (ret) {
  158. cq = ERR_PTR(-ENOMEM);
  159. ehca_err(device, "Can't allocate new idr entry. device=%p",
  160. device);
  161. goto create_cq_exit1;
  162. }
  163. if (my_cq->token > 0x1FFFFFF) {
  164. cq = ERR_PTR(-ENOMEM);
  165. ehca_err(device, "Invalid number of cq. device=%p", device);
  166. goto create_cq_exit2;
  167. }
  168. /*
  169. * CQs maximum depth is 4GB-64, but we need additional 20 as buffer
  170. * for receiving errors CQEs.
  171. */
  172. param.nr_cqe = cqe + additional_cqe;
  173. h_ret = hipz_h_alloc_resource_cq(adapter_handle, my_cq, &param);
  174. if (h_ret != H_SUCCESS) {
  175. ehca_err(device, "hipz_h_alloc_resource_cq() failed "
  176. "h_ret=%lli device=%p", h_ret, device);
  177. cq = ERR_PTR(ehca2ib_return_code(h_ret));
  178. goto create_cq_exit2;
  179. }
  180. ipz_rc = ipz_queue_ctor(NULL, &my_cq->ipz_queue, param.act_pages,
  181. EHCA_PAGESIZE, sizeof(struct ehca_cqe), 0, 0);
  182. if (!ipz_rc) {
  183. ehca_err(device, "ipz_queue_ctor() failed ipz_rc=%i device=%p",
  184. ipz_rc, device);
  185. cq = ERR_PTR(-EINVAL);
  186. goto create_cq_exit3;
  187. }
  188. for (counter = 0; counter < param.act_pages; counter++) {
  189. vpage = ipz_qpageit_get_inc(&my_cq->ipz_queue);
  190. if (!vpage) {
  191. ehca_err(device, "ipz_qpageit_get_inc() "
  192. "returns NULL device=%p", device);
  193. cq = ERR_PTR(-EAGAIN);
  194. goto create_cq_exit4;
  195. }
  196. rpage = virt_to_abs(vpage);
  197. h_ret = hipz_h_register_rpage_cq(adapter_handle,
  198. my_cq->ipz_cq_handle,
  199. &my_cq->pf,
  200. 0,
  201. 0,
  202. rpage,
  203. 1,
  204. my_cq->galpas.
  205. kernel);
  206. if (h_ret < H_SUCCESS) {
  207. ehca_err(device, "hipz_h_register_rpage_cq() failed "
  208. "ehca_cq=%p cq_num=%x h_ret=%lli counter=%i "
  209. "act_pages=%i", my_cq, my_cq->cq_number,
  210. h_ret, counter, param.act_pages);
  211. cq = ERR_PTR(-EINVAL);
  212. goto create_cq_exit4;
  213. }
  214. if (counter == (param.act_pages - 1)) {
  215. vpage = ipz_qpageit_get_inc(&my_cq->ipz_queue);
  216. if ((h_ret != H_SUCCESS) || vpage) {
  217. ehca_err(device, "Registration of pages not "
  218. "complete ehca_cq=%p cq_num=%x "
  219. "h_ret=%lli", my_cq, my_cq->cq_number,
  220. h_ret);
  221. cq = ERR_PTR(-EAGAIN);
  222. goto create_cq_exit4;
  223. }
  224. } else {
  225. if (h_ret != H_PAGE_REGISTERED) {
  226. ehca_err(device, "Registration of page failed "
  227. "ehca_cq=%p cq_num=%x h_ret=%lli "
  228. "counter=%i act_pages=%i",
  229. my_cq, my_cq->cq_number,
  230. h_ret, counter, param.act_pages);
  231. cq = ERR_PTR(-ENOMEM);
  232. goto create_cq_exit4;
  233. }
  234. }
  235. }
  236. ipz_qeit_reset(&my_cq->ipz_queue);
  237. gal = my_cq->galpas.kernel;
  238. cqx_fec = hipz_galpa_load(gal, CQTEMM_OFFSET(cqx_fec));
  239. ehca_dbg(device, "ehca_cq=%p cq_num=%x CQX_FEC=%llx",
  240. my_cq, my_cq->cq_number, cqx_fec);
  241. my_cq->ib_cq.cqe = my_cq->nr_of_entries =
  242. param.act_nr_of_entries - additional_cqe;
  243. my_cq->cq_number = (my_cq->ipz_cq_handle.handle) & 0xffff;
  244. for (i = 0; i < QP_HASHTAB_LEN; i++)
  245. INIT_HLIST_HEAD(&my_cq->qp_hashtab[i]);
  246. INIT_LIST_HEAD(&my_cq->sqp_err_list);
  247. INIT_LIST_HEAD(&my_cq->rqp_err_list);
  248. if (context) {
  249. struct ipz_queue *ipz_queue = &my_cq->ipz_queue;
  250. struct ehca_create_cq_resp resp;
  251. memset(&resp, 0, sizeof(resp));
  252. resp.cq_number = my_cq->cq_number;
  253. resp.token = my_cq->token;
  254. resp.ipz_queue.qe_size = ipz_queue->qe_size;
  255. resp.ipz_queue.act_nr_of_sg = ipz_queue->act_nr_of_sg;
  256. resp.ipz_queue.queue_length = ipz_queue->queue_length;
  257. resp.ipz_queue.pagesize = ipz_queue->pagesize;
  258. resp.ipz_queue.toggle_state = ipz_queue->toggle_state;
  259. resp.fw_handle_ofs = (u32)
  260. (my_cq->galpas.user.fw_handle & (PAGE_SIZE - 1));
  261. if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
  262. ehca_err(device, "Copy to udata failed.");
  263. cq = ERR_PTR(-EFAULT);
  264. goto create_cq_exit4;
  265. }
  266. }
  267. return cq;
  268. create_cq_exit4:
  269. ipz_queue_dtor(NULL, &my_cq->ipz_queue);
  270. create_cq_exit3:
  271. h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
  272. if (h_ret != H_SUCCESS)
  273. ehca_err(device, "hipz_h_destroy_cq() failed ehca_cq=%p "
  274. "cq_num=%x h_ret=%lli", my_cq, my_cq->cq_number, h_ret);
  275. create_cq_exit2:
  276. write_lock_irqsave(&ehca_cq_idr_lock, flags);
  277. idr_remove(&ehca_cq_idr, my_cq->token);
  278. write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  279. create_cq_exit1:
  280. kmem_cache_free(cq_cache, my_cq);
  281. atomic_dec(&shca->num_cqs);
  282. return cq;
  283. }
  284. int ehca_destroy_cq(struct ib_cq *cq)
  285. {
  286. u64 h_ret;
  287. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  288. int cq_num = my_cq->cq_number;
  289. struct ib_device *device = cq->device;
  290. struct ehca_shca *shca = container_of(device, struct ehca_shca,
  291. ib_device);
  292. struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
  293. unsigned long flags;
  294. if (cq->uobject) {
  295. if (my_cq->mm_count_galpa || my_cq->mm_count_queue) {
  296. ehca_err(device, "Resources still referenced in "
  297. "user space cq_num=%x", my_cq->cq_number);
  298. return -EINVAL;
  299. }
  300. }
  301. /*
  302. * remove the CQ from the idr first to make sure
  303. * no more interrupt tasklets will touch this CQ
  304. */
  305. write_lock_irqsave(&ehca_cq_idr_lock, flags);
  306. idr_remove(&ehca_cq_idr, my_cq->token);
  307. write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  308. /* now wait until all pending events have completed */
  309. wait_event(my_cq->wait_completion, !atomic_read(&my_cq->nr_events));
  310. /* nobody's using our CQ any longer -- we can destroy it */
  311. h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 0);
  312. if (h_ret == H_R_STATE) {
  313. /* cq in err: read err data and destroy it forcibly */
  314. ehca_dbg(device, "ehca_cq=%p cq_num=%x resource=%llx in err "
  315. "state. Try to delete it forcibly.",
  316. my_cq, cq_num, my_cq->ipz_cq_handle.handle);
  317. ehca_error_data(shca, my_cq, my_cq->ipz_cq_handle.handle);
  318. h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
  319. if (h_ret == H_SUCCESS)
  320. ehca_dbg(device, "cq_num=%x deleted successfully.",
  321. cq_num);
  322. }
  323. if (h_ret != H_SUCCESS) {
  324. ehca_err(device, "hipz_h_destroy_cq() failed h_ret=%lli "
  325. "ehca_cq=%p cq_num=%x", h_ret, my_cq, cq_num);
  326. return ehca2ib_return_code(h_ret);
  327. }
  328. ipz_queue_dtor(NULL, &my_cq->ipz_queue);
  329. kmem_cache_free(cq_cache, my_cq);
  330. atomic_dec(&shca->num_cqs);
  331. return 0;
  332. }
  333. int ehca_resize_cq(struct ib_cq *cq, int cqe, struct ib_udata *udata)
  334. {
  335. /* TODO: proper resize needs to be done */
  336. ehca_err(cq->device, "not implemented yet");
  337. return -EFAULT;
  338. }
  339. int ehca_init_cq_cache(void)
  340. {
  341. cq_cache = kmem_cache_create("ehca_cache_cq",
  342. sizeof(struct ehca_cq), 0,
  343. SLAB_HWCACHE_ALIGN,
  344. NULL);
  345. if (!cq_cache)
  346. return -ENOMEM;
  347. return 0;
  348. }
  349. void ehca_cleanup_cq_cache(void)
  350. {
  351. if (cq_cache)
  352. kmem_cache_destroy(cq_cache);
  353. }