ehca_cq.c 11 KB

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