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