cq.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2015 HGST, a Western Digital Company.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <rdma/ib_verbs.h>
  17. /* # of WCs to poll for with a single call to ib_poll_cq */
  18. #define IB_POLL_BATCH 16
  19. /* # of WCs to iterate over before yielding */
  20. #define IB_POLL_BUDGET_IRQ 256
  21. #define IB_POLL_BUDGET_WORKQUEUE 65536
  22. #define IB_POLL_FLAGS \
  23. (IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS)
  24. static int __ib_process_cq(struct ib_cq *cq, int budget)
  25. {
  26. int i, n, completed = 0;
  27. /*
  28. * budget might be (-1) if the caller does not
  29. * want to bound this call, thus we need unsigned
  30. * minimum here.
  31. */
  32. while ((n = ib_poll_cq(cq, min_t(u32, IB_POLL_BATCH,
  33. budget - completed), cq->wc)) > 0) {
  34. for (i = 0; i < n; i++) {
  35. struct ib_wc *wc = &cq->wc[i];
  36. if (wc->wr_cqe)
  37. wc->wr_cqe->done(cq, wc);
  38. else
  39. WARN_ON_ONCE(wc->status == IB_WC_SUCCESS);
  40. }
  41. completed += n;
  42. if (n != IB_POLL_BATCH ||
  43. (budget != -1 && completed >= budget))
  44. break;
  45. }
  46. return completed;
  47. }
  48. /**
  49. * ib_process_direct_cq - process a CQ in caller context
  50. * @cq: CQ to process
  51. * @budget: number of CQEs to poll for
  52. *
  53. * This function is used to process all outstanding CQ entries on a
  54. * %IB_POLL_DIRECT CQ. It does not offload CQ processing to a different
  55. * context and does not ask for completion interrupts from the HCA.
  56. *
  57. * Note: do not pass -1 as %budget unless it is guaranteed that the number
  58. * of completions that will be processed is small.
  59. */
  60. int ib_process_cq_direct(struct ib_cq *cq, int budget)
  61. {
  62. WARN_ON_ONCE(cq->poll_ctx != IB_POLL_DIRECT);
  63. return __ib_process_cq(cq, budget);
  64. }
  65. EXPORT_SYMBOL(ib_process_cq_direct);
  66. static void ib_cq_completion_direct(struct ib_cq *cq, void *private)
  67. {
  68. WARN_ONCE(1, "got unsolicited completion for CQ 0x%p\n", cq);
  69. }
  70. static int ib_poll_handler(struct irq_poll *iop, int budget)
  71. {
  72. struct ib_cq *cq = container_of(iop, struct ib_cq, iop);
  73. int completed;
  74. completed = __ib_process_cq(cq, budget);
  75. if (completed < budget) {
  76. irq_poll_complete(&cq->iop);
  77. if (ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
  78. irq_poll_sched(&cq->iop);
  79. }
  80. return completed;
  81. }
  82. static void ib_cq_completion_softirq(struct ib_cq *cq, void *private)
  83. {
  84. irq_poll_sched(&cq->iop);
  85. }
  86. static void ib_cq_poll_work(struct work_struct *work)
  87. {
  88. struct ib_cq *cq = container_of(work, struct ib_cq, work);
  89. int completed;
  90. completed = __ib_process_cq(cq, IB_POLL_BUDGET_WORKQUEUE);
  91. if (completed >= IB_POLL_BUDGET_WORKQUEUE ||
  92. ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
  93. queue_work(ib_comp_wq, &cq->work);
  94. }
  95. static void ib_cq_completion_workqueue(struct ib_cq *cq, void *private)
  96. {
  97. queue_work(ib_comp_wq, &cq->work);
  98. }
  99. /**
  100. * ib_alloc_cq - allocate a completion queue
  101. * @dev: device to allocate the CQ for
  102. * @private: driver private data, accessible from cq->cq_context
  103. * @nr_cqe: number of CQEs to allocate
  104. * @comp_vector: HCA completion vectors for this CQ
  105. * @poll_ctx: context to poll the CQ from.
  106. *
  107. * This is the proper interface to allocate a CQ for in-kernel users. A
  108. * CQ allocated with this interface will automatically be polled from the
  109. * specified context. The ULP must use wr->wr_cqe instead of wr->wr_id
  110. * to use this CQ abstraction.
  111. */
  112. struct ib_cq *ib_alloc_cq(struct ib_device *dev, void *private,
  113. int nr_cqe, int comp_vector, enum ib_poll_context poll_ctx)
  114. {
  115. struct ib_cq_init_attr cq_attr = {
  116. .cqe = nr_cqe,
  117. .comp_vector = comp_vector,
  118. };
  119. struct ib_cq *cq;
  120. int ret = -ENOMEM;
  121. cq = dev->create_cq(dev, &cq_attr, NULL, NULL);
  122. if (IS_ERR(cq))
  123. return cq;
  124. cq->device = dev;
  125. cq->uobject = NULL;
  126. cq->event_handler = NULL;
  127. cq->cq_context = private;
  128. cq->poll_ctx = poll_ctx;
  129. atomic_set(&cq->usecnt, 0);
  130. cq->wc = kmalloc_array(IB_POLL_BATCH, sizeof(*cq->wc), GFP_KERNEL);
  131. if (!cq->wc)
  132. goto out_destroy_cq;
  133. switch (cq->poll_ctx) {
  134. case IB_POLL_DIRECT:
  135. cq->comp_handler = ib_cq_completion_direct;
  136. break;
  137. case IB_POLL_SOFTIRQ:
  138. cq->comp_handler = ib_cq_completion_softirq;
  139. irq_poll_init(&cq->iop, IB_POLL_BUDGET_IRQ, ib_poll_handler);
  140. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  141. break;
  142. case IB_POLL_WORKQUEUE:
  143. cq->comp_handler = ib_cq_completion_workqueue;
  144. INIT_WORK(&cq->work, ib_cq_poll_work);
  145. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  146. break;
  147. default:
  148. ret = -EINVAL;
  149. goto out_free_wc;
  150. }
  151. return cq;
  152. out_free_wc:
  153. kfree(cq->wc);
  154. out_destroy_cq:
  155. cq->device->destroy_cq(cq);
  156. return ERR_PTR(ret);
  157. }
  158. EXPORT_SYMBOL(ib_alloc_cq);
  159. /**
  160. * ib_free_cq - free a completion queue
  161. * @cq: completion queue to free.
  162. */
  163. void ib_free_cq(struct ib_cq *cq)
  164. {
  165. int ret;
  166. if (WARN_ON_ONCE(atomic_read(&cq->usecnt)))
  167. return;
  168. switch (cq->poll_ctx) {
  169. case IB_POLL_DIRECT:
  170. break;
  171. case IB_POLL_SOFTIRQ:
  172. irq_poll_disable(&cq->iop);
  173. break;
  174. case IB_POLL_WORKQUEUE:
  175. cancel_work_sync(&cq->work);
  176. break;
  177. default:
  178. WARN_ON_ONCE(1);
  179. }
  180. kfree(cq->wc);
  181. ret = cq->device->destroy_cq(cq);
  182. WARN_ON_ONCE(ret);
  183. }
  184. EXPORT_SYMBOL(ib_free_cq);