ehca_cq.c 11 KB

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