cq.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #define IB_POLL_BATCH_DIRECT 8
  20. /* # of WCs to iterate over before yielding */
  21. #define IB_POLL_BUDGET_IRQ 256
  22. #define IB_POLL_BUDGET_WORKQUEUE 65536
  23. #define IB_POLL_FLAGS \
  24. (IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS)
  25. static int __ib_process_cq(struct ib_cq *cq, int budget, struct ib_wc *wcs,
  26. int batch)
  27. {
  28. int i, n, completed = 0;
  29. /*
  30. * budget might be (-1) if the caller does not
  31. * want to bound this call, thus we need unsigned
  32. * minimum here.
  33. */
  34. while ((n = ib_poll_cq(cq, min_t(u32, batch,
  35. budget - completed), wcs)) > 0) {
  36. for (i = 0; i < n; i++) {
  37. struct ib_wc *wc = &wcs[i];
  38. if (wc->wr_cqe)
  39. wc->wr_cqe->done(cq, wc);
  40. else
  41. WARN_ON_ONCE(wc->status == IB_WC_SUCCESS);
  42. }
  43. completed += n;
  44. if (n != batch || (budget != -1 && completed >= budget))
  45. break;
  46. }
  47. return completed;
  48. }
  49. /**
  50. * ib_process_direct_cq - process a CQ in caller context
  51. * @cq: CQ to process
  52. * @budget: number of CQEs to poll for
  53. *
  54. * This function is used to process all outstanding CQ entries.
  55. * It does not offload CQ processing to a different context and does
  56. * not ask for completion interrupts from the HCA.
  57. * Using direct processing on CQ with non IB_POLL_DIRECT type may trigger
  58. * concurrent processing.
  59. *
  60. * Note: do not pass -1 as %budget unless it is guaranteed that the number
  61. * of completions that will be processed is small.
  62. */
  63. int ib_process_cq_direct(struct ib_cq *cq, int budget)
  64. {
  65. struct ib_wc wcs[IB_POLL_BATCH_DIRECT];
  66. return __ib_process_cq(cq, budget, wcs, IB_POLL_BATCH_DIRECT);
  67. }
  68. EXPORT_SYMBOL(ib_process_cq_direct);
  69. static void ib_cq_completion_direct(struct ib_cq *cq, void *private)
  70. {
  71. WARN_ONCE(1, "got unsolicited completion for CQ 0x%p\n", cq);
  72. }
  73. static int ib_poll_handler(struct irq_poll *iop, int budget)
  74. {
  75. struct ib_cq *cq = container_of(iop, struct ib_cq, iop);
  76. int completed;
  77. completed = __ib_process_cq(cq, budget, cq->wc, IB_POLL_BATCH);
  78. if (completed < budget) {
  79. irq_poll_complete(&cq->iop);
  80. if (ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
  81. irq_poll_sched(&cq->iop);
  82. }
  83. return completed;
  84. }
  85. static void ib_cq_completion_softirq(struct ib_cq *cq, void *private)
  86. {
  87. irq_poll_sched(&cq->iop);
  88. }
  89. static void ib_cq_poll_work(struct work_struct *work)
  90. {
  91. struct ib_cq *cq = container_of(work, struct ib_cq, work);
  92. int completed;
  93. completed = __ib_process_cq(cq, IB_POLL_BUDGET_WORKQUEUE, cq->wc,
  94. IB_POLL_BATCH);
  95. if (completed >= IB_POLL_BUDGET_WORKQUEUE ||
  96. ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
  97. queue_work(cq->comp_wq, &cq->work);
  98. }
  99. static void ib_cq_completion_workqueue(struct ib_cq *cq, void *private)
  100. {
  101. queue_work(cq->comp_wq, &cq->work);
  102. }
  103. /**
  104. * __ib_alloc_cq - allocate a completion queue
  105. * @dev: device to allocate the CQ for
  106. * @private: driver private data, accessible from cq->cq_context
  107. * @nr_cqe: number of CQEs to allocate
  108. * @comp_vector: HCA completion vectors for this CQ
  109. * @poll_ctx: context to poll the CQ from.
  110. * @caller: module owner name.
  111. *
  112. * This is the proper interface to allocate a CQ for in-kernel users. A
  113. * CQ allocated with this interface will automatically be polled from the
  114. * specified context. The ULP must use wr->wr_cqe instead of wr->wr_id
  115. * to use this CQ abstraction.
  116. */
  117. struct ib_cq *__ib_alloc_cq(struct ib_device *dev, void *private,
  118. int nr_cqe, int comp_vector,
  119. enum ib_poll_context poll_ctx, const char *caller)
  120. {
  121. struct ib_cq_init_attr cq_attr = {
  122. .cqe = nr_cqe,
  123. .comp_vector = comp_vector,
  124. };
  125. struct ib_cq *cq;
  126. int ret = -ENOMEM;
  127. cq = dev->create_cq(dev, &cq_attr, NULL, NULL);
  128. if (IS_ERR(cq))
  129. return cq;
  130. cq->device = dev;
  131. cq->uobject = NULL;
  132. cq->event_handler = NULL;
  133. cq->cq_context = private;
  134. cq->poll_ctx = poll_ctx;
  135. atomic_set(&cq->usecnt, 0);
  136. cq->wc = kmalloc_array(IB_POLL_BATCH, sizeof(*cq->wc), GFP_KERNEL);
  137. if (!cq->wc)
  138. goto out_destroy_cq;
  139. cq->res.type = RDMA_RESTRACK_CQ;
  140. cq->res.kern_name = caller;
  141. rdma_restrack_add(&cq->res);
  142. switch (cq->poll_ctx) {
  143. case IB_POLL_DIRECT:
  144. cq->comp_handler = ib_cq_completion_direct;
  145. break;
  146. case IB_POLL_SOFTIRQ:
  147. cq->comp_handler = ib_cq_completion_softirq;
  148. irq_poll_init(&cq->iop, IB_POLL_BUDGET_IRQ, ib_poll_handler);
  149. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  150. break;
  151. case IB_POLL_WORKQUEUE:
  152. case IB_POLL_UNBOUND_WORKQUEUE:
  153. cq->comp_handler = ib_cq_completion_workqueue;
  154. INIT_WORK(&cq->work, ib_cq_poll_work);
  155. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  156. cq->comp_wq = (cq->poll_ctx == IB_POLL_WORKQUEUE) ?
  157. ib_comp_wq : ib_comp_unbound_wq;
  158. break;
  159. default:
  160. ret = -EINVAL;
  161. goto out_free_wc;
  162. }
  163. return cq;
  164. out_free_wc:
  165. kfree(cq->wc);
  166. rdma_restrack_del(&cq->res);
  167. out_destroy_cq:
  168. cq->device->destroy_cq(cq);
  169. return ERR_PTR(ret);
  170. }
  171. EXPORT_SYMBOL(__ib_alloc_cq);
  172. /**
  173. * ib_free_cq - free a completion queue
  174. * @cq: completion queue to free.
  175. */
  176. void ib_free_cq(struct ib_cq *cq)
  177. {
  178. int ret;
  179. if (WARN_ON_ONCE(atomic_read(&cq->usecnt)))
  180. return;
  181. switch (cq->poll_ctx) {
  182. case IB_POLL_DIRECT:
  183. break;
  184. case IB_POLL_SOFTIRQ:
  185. irq_poll_disable(&cq->iop);
  186. break;
  187. case IB_POLL_WORKQUEUE:
  188. case IB_POLL_UNBOUND_WORKQUEUE:
  189. cancel_work_sync(&cq->work);
  190. break;
  191. default:
  192. WARN_ON_ONCE(1);
  193. }
  194. kfree(cq->wc);
  195. rdma_restrack_del(&cq->res);
  196. ret = cq->device->destroy_cq(cq);
  197. WARN_ON_ONCE(ret);
  198. }
  199. EXPORT_SYMBOL(ib_free_cq);