ehca_cq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 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. 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. atomic_set(&my_cq->nr_events, 0);
  134. init_waitqueue_head(&my_cq->wait_completion);
  135. my_cq->ownpid = current->tgid;
  136. cq = &my_cq->ib_cq;
  137. adapter_handle = shca->ipz_hca_handle;
  138. param.eq_handle = shca->eq.ipz_eq_handle;
  139. do {
  140. if (!idr_pre_get(&ehca_cq_idr, GFP_KERNEL)) {
  141. cq = ERR_PTR(-ENOMEM);
  142. ehca_err(device, "Can't reserve idr nr. device=%p",
  143. device);
  144. goto create_cq_exit1;
  145. }
  146. write_lock_irqsave(&ehca_cq_idr_lock, flags);
  147. ret = idr_get_new(&ehca_cq_idr, my_cq, &my_cq->token);
  148. write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  149. } while (ret == -EAGAIN);
  150. if (ret) {
  151. cq = ERR_PTR(-ENOMEM);
  152. ehca_err(device, "Can't allocate new idr entry. device=%p",
  153. device);
  154. goto create_cq_exit1;
  155. }
  156. if (my_cq->token > 0x1FFFFFF) {
  157. cq = ERR_PTR(-ENOMEM);
  158. ehca_err(device, "Invalid number of cq. device=%p", device);
  159. goto create_cq_exit2;
  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=%li 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 = virt_to_abs(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=%li 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=%li", 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=%li"
  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=%lx",
  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. if (context) {
  240. struct ipz_queue *ipz_queue = &my_cq->ipz_queue;
  241. struct ehca_create_cq_resp resp;
  242. memset(&resp, 0, sizeof(resp));
  243. resp.cq_number = my_cq->cq_number;
  244. resp.token = my_cq->token;
  245. resp.ipz_queue.qe_size = ipz_queue->qe_size;
  246. resp.ipz_queue.act_nr_of_sg = ipz_queue->act_nr_of_sg;
  247. resp.ipz_queue.queue_length = ipz_queue->queue_length;
  248. resp.ipz_queue.pagesize = ipz_queue->pagesize;
  249. resp.ipz_queue.toggle_state = ipz_queue->toggle_state;
  250. resp.fw_handle_ofs = (u32)
  251. (my_cq->galpas.user.fw_handle & (PAGE_SIZE - 1));
  252. if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
  253. ehca_err(device, "Copy to udata failed.");
  254. goto create_cq_exit4;
  255. }
  256. }
  257. return cq;
  258. create_cq_exit4:
  259. ipz_queue_dtor(NULL, &my_cq->ipz_queue);
  260. create_cq_exit3:
  261. h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
  262. if (h_ret != H_SUCCESS)
  263. ehca_err(device, "hipz_h_destroy_cq() failed ehca_cq=%p "
  264. "cq_num=%x h_ret=%li", my_cq, my_cq->cq_number, h_ret);
  265. create_cq_exit2:
  266. write_lock_irqsave(&ehca_cq_idr_lock, flags);
  267. idr_remove(&ehca_cq_idr, my_cq->token);
  268. write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  269. create_cq_exit1:
  270. kmem_cache_free(cq_cache, my_cq);
  271. return cq;
  272. }
  273. int ehca_destroy_cq(struct ib_cq *cq)
  274. {
  275. u64 h_ret;
  276. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  277. int cq_num = my_cq->cq_number;
  278. struct ib_device *device = cq->device;
  279. struct ehca_shca *shca = container_of(device, struct ehca_shca,
  280. ib_device);
  281. struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
  282. u32 cur_pid = current->tgid;
  283. unsigned long flags;
  284. if (cq->uobject) {
  285. if (my_cq->mm_count_galpa || my_cq->mm_count_queue) {
  286. ehca_err(device, "Resources still referenced in "
  287. "user space cq_num=%x", my_cq->cq_number);
  288. return -EINVAL;
  289. }
  290. if (my_cq->ownpid != cur_pid) {
  291. ehca_err(device, "Invalid caller pid=%x ownpid=%x "
  292. "cq_num=%x",
  293. cur_pid, my_cq->ownpid, my_cq->cq_number);
  294. return -EINVAL;
  295. }
  296. }
  297. /*
  298. * remove the CQ from the idr first to make sure
  299. * no more interrupt tasklets will touch this CQ
  300. */
  301. write_lock_irqsave(&ehca_cq_idr_lock, flags);
  302. idr_remove(&ehca_cq_idr, my_cq->token);
  303. write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
  304. /* now wait until all pending events have completed */
  305. wait_event(my_cq->wait_completion, !atomic_read(&my_cq->nr_events));
  306. /* nobody's using our CQ any longer -- we can destroy it */
  307. h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 0);
  308. if (h_ret == H_R_STATE) {
  309. /* cq in err: read err data and destroy it forcibly */
  310. ehca_dbg(device, "ehca_cq=%p cq_num=%x ressource=%lx in err "
  311. "state. Try to delete it forcibly.",
  312. my_cq, cq_num, my_cq->ipz_cq_handle.handle);
  313. ehca_error_data(shca, my_cq, my_cq->ipz_cq_handle.handle);
  314. h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
  315. if (h_ret == H_SUCCESS)
  316. ehca_dbg(device, "cq_num=%x deleted successfully.",
  317. cq_num);
  318. }
  319. if (h_ret != H_SUCCESS) {
  320. ehca_err(device, "hipz_h_destroy_cq() failed h_ret=%li "
  321. "ehca_cq=%p cq_num=%x", h_ret, my_cq, cq_num);
  322. return ehca2ib_return_code(h_ret);
  323. }
  324. ipz_queue_dtor(NULL, &my_cq->ipz_queue);
  325. kmem_cache_free(cq_cache, my_cq);
  326. return 0;
  327. }
  328. int ehca_resize_cq(struct ib_cq *cq, int cqe, struct ib_udata *udata)
  329. {
  330. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  331. u32 cur_pid = current->tgid;
  332. if (cq->uobject && my_cq->ownpid != cur_pid) {
  333. ehca_err(cq->device, "Invalid caller pid=%x ownpid=%x",
  334. cur_pid, my_cq->ownpid);
  335. return -EINVAL;
  336. }
  337. /* TODO: proper resize needs to be done */
  338. ehca_err(cq->device, "not implemented yet");
  339. return -EFAULT;
  340. }
  341. int ehca_init_cq_cache(void)
  342. {
  343. cq_cache = kmem_cache_create("ehca_cache_cq",
  344. sizeof(struct ehca_cq), 0,
  345. SLAB_HWCACHE_ALIGN,
  346. NULL);
  347. if (!cq_cache)
  348. return -ENOMEM;
  349. return 0;
  350. }
  351. void ehca_cleanup_cq_cache(void)
  352. {
  353. if (cq_cache)
  354. kmem_cache_destroy(cq_cache);
  355. }