blob: 9b68b175069b9a1b6f95bfc1beae065b9ee6b682 [file] [log] [blame]
Heiko J Schickfab97222006-09-22 15:22:22 -07001/*
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
47
Heiko J Schickfab97222006-09-22 15:22:22 -070048#include "ehca_iverbs.h"
49#include "ehca_classes.h"
50#include "ehca_irq.h"
51#include "hcp_if.h"
52
53static struct kmem_cache *cq_cache;
54
55int ehca_cq_assign_qp(struct ehca_cq *cq, struct ehca_qp *qp)
56{
57 unsigned int qp_num = qp->real_qp_num;
58 unsigned int key = qp_num & (QP_HASHTAB_LEN-1);
Joachim Fenkes9844b712007-07-09 15:29:03 +020059 unsigned long flags;
Heiko J Schickfab97222006-09-22 15:22:22 -070060
Joachim Fenkes9844b712007-07-09 15:29:03 +020061 spin_lock_irqsave(&cq->spinlock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -070062 hlist_add_head(&qp->list_entries, &cq->qp_hashtab[key]);
Joachim Fenkes9844b712007-07-09 15:29:03 +020063 spin_unlock_irqrestore(&cq->spinlock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -070064
65 ehca_dbg(cq->ib_cq.device, "cq_num=%x real_qp_num=%x",
66 cq->cq_number, qp_num);
67
68 return 0;
69}
70
71int ehca_cq_unassign_qp(struct ehca_cq *cq, unsigned int real_qp_num)
72{
73 int ret = -EINVAL;
74 unsigned int key = real_qp_num & (QP_HASHTAB_LEN-1);
75 struct hlist_node *iter;
76 struct ehca_qp *qp;
Joachim Fenkes9844b712007-07-09 15:29:03 +020077 unsigned long flags;
Heiko J Schickfab97222006-09-22 15:22:22 -070078
Joachim Fenkes9844b712007-07-09 15:29:03 +020079 spin_lock_irqsave(&cq->spinlock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -070080 hlist_for_each(iter, &cq->qp_hashtab[key]) {
81 qp = hlist_entry(iter, struct ehca_qp, list_entries);
82 if (qp->real_qp_num == real_qp_num) {
83 hlist_del(iter);
84 ehca_dbg(cq->ib_cq.device,
85 "removed qp from cq .cq_num=%x real_qp_num=%x",
86 cq->cq_number, real_qp_num);
87 ret = 0;
88 break;
89 }
90 }
Joachim Fenkes9844b712007-07-09 15:29:03 +020091 spin_unlock_irqrestore(&cq->spinlock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -070092 if (ret)
93 ehca_err(cq->ib_cq.device,
94 "qp not found cq_num=%x real_qp_num=%x",
95 cq->cq_number, real_qp_num);
96
97 return ret;
98}
99
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200100struct ehca_qp *ehca_cq_get_qp(struct ehca_cq *cq, int real_qp_num)
Heiko J Schickfab97222006-09-22 15:22:22 -0700101{
102 struct ehca_qp *ret = NULL;
103 unsigned int key = real_qp_num & (QP_HASHTAB_LEN-1);
104 struct hlist_node *iter;
105 struct ehca_qp *qp;
106 hlist_for_each(iter, &cq->qp_hashtab[key]) {
107 qp = hlist_entry(iter, struct ehca_qp, list_entries);
108 if (qp->real_qp_num == real_qp_num) {
109 ret = qp;
110 break;
111 }
112 }
113 return ret;
114}
115
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300116struct ib_cq *ehca_create_cq(struct ib_device *device,
117 const struct ib_cq_init_attr *attr,
Heiko J Schickfab97222006-09-22 15:22:22 -0700118 struct ib_ucontext *context,
119 struct ib_udata *udata)
120{
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300121 int cqe = attr->cqe;
Heiko J Schickfab97222006-09-22 15:22:22 -0700122 static const u32 additional_cqe = 20;
123 struct ib_cq *cq;
124 struct ehca_cq *my_cq;
125 struct ehca_shca *shca =
126 container_of(device, struct ehca_shca, ib_device);
127 struct ipz_adapter_handle adapter_handle;
128 struct ehca_alloc_cq_parms param; /* h_call's out parameters */
129 struct h_galpa gal;
130 void *vpage;
131 u32 counter;
132 u64 rpage, cqx_fec, h_ret;
Tejun Heocbbbce12013-02-27 17:04:21 -0800133 int ipz_rc, i;
Heiko J Schickfab97222006-09-22 15:22:22 -0700134 unsigned long flags;
135
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300136 if (attr->flags)
137 return ERR_PTR(-EINVAL);
138
Heiko J Schickfab97222006-09-22 15:22:22 -0700139 if (cqe >= 0xFFFFFFFF - 64 - additional_cqe)
140 return ERR_PTR(-EINVAL);
141
Stefan Roscher19f42822008-10-22 15:52:31 -0700142 if (!atomic_add_unless(&shca->num_cqs, 1, shca->max_num_cqs)) {
Stefan Roscherd227fa72008-04-29 13:46:53 -0700143 ehca_err(device, "Unable to create CQ, max number of %i "
Stefan Roscher19f42822008-10-22 15:52:31 -0700144 "CQs reached.", shca->max_num_cqs);
Stefan Roscherd227fa72008-04-29 13:46:53 -0700145 ehca_err(device, "To increase the maximum number of CQs "
146 "use the number_of_cqs module parameter.\n");
147 return ERR_PTR(-ENOSPC);
148 }
149
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800150 my_cq = kmem_cache_zalloc(cq_cache, GFP_KERNEL);
Heiko J Schickfab97222006-09-22 15:22:22 -0700151 if (!my_cq) {
152 ehca_err(device, "Out of memory for ehca_cq struct device=%p",
153 device);
Stefan Roscherd227fa72008-04-29 13:46:53 -0700154 atomic_dec(&shca->num_cqs);
Heiko J Schickfab97222006-09-22 15:22:22 -0700155 return ERR_PTR(-ENOMEM);
156 }
157
Heiko J Schickfab97222006-09-22 15:22:22 -0700158 memset(&param, 0, sizeof(struct ehca_alloc_cq_parms));
159
160 spin_lock_init(&my_cq->spinlock);
161 spin_lock_init(&my_cq->cb_lock);
162 spin_lock_init(&my_cq->task_lock);
Joachim Fenkes28db6be2007-07-09 15:30:39 +0200163 atomic_set(&my_cq->nr_events, 0);
Hoang-Nam Nguyen31726792007-02-28 18:01:02 +0100164 init_waitqueue_head(&my_cq->wait_completion);
Heiko J Schickfab97222006-09-22 15:22:22 -0700165
166 cq = &my_cq->ib_cq;
167
168 adapter_handle = shca->ipz_hca_handle;
169 param.eq_handle = shca->eq.ipz_eq_handle;
170
Tejun Heocbbbce12013-02-27 17:04:21 -0800171 idr_preload(GFP_KERNEL);
172 write_lock_irqsave(&ehca_cq_idr_lock, flags);
173 my_cq->token = idr_alloc(&ehca_cq_idr, my_cq, 0, 0x2000000, GFP_NOWAIT);
174 write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
175 idr_preload_end();
Heiko J Schickfab97222006-09-22 15:22:22 -0700176
Tejun Heocbbbce12013-02-27 17:04:21 -0800177 if (my_cq->token < 0) {
Heiko J Schickfab97222006-09-22 15:22:22 -0700178 cq = ERR_PTR(-ENOMEM);
179 ehca_err(device, "Can't allocate new idr entry. device=%p",
180 device);
181 goto create_cq_exit1;
182 }
183
184 /*
185 * CQs maximum depth is 4GB-64, but we need additional 20 as buffer
186 * for receiving errors CQEs.
187 */
188 param.nr_cqe = cqe + additional_cqe;
189 h_ret = hipz_h_alloc_resource_cq(adapter_handle, my_cq, &param);
190
191 if (h_ret != H_SUCCESS) {
192 ehca_err(device, "hipz_h_alloc_resource_cq() failed "
Stephen Rothwell3750f602009-01-16 14:55:28 -0800193 "h_ret=%lli device=%p", h_ret, device);
Heiko J Schickfab97222006-09-22 15:22:22 -0700194 cq = ERR_PTR(ehca2ib_return_code(h_ret));
195 goto create_cq_exit2;
196 }
197
Stefan Roschere2f81da2007-07-20 16:04:17 +0200198 ipz_rc = ipz_queue_ctor(NULL, &my_cq->ipz_queue, param.act_pages,
199 EHCA_PAGESIZE, sizeof(struct ehca_cqe), 0, 0);
Heiko J Schickfab97222006-09-22 15:22:22 -0700200 if (!ipz_rc) {
Joachim Fenkese3722192007-09-11 15:32:22 +0200201 ehca_err(device, "ipz_queue_ctor() failed ipz_rc=%i device=%p",
Heiko J Schickfab97222006-09-22 15:22:22 -0700202 ipz_rc, device);
203 cq = ERR_PTR(-EINVAL);
204 goto create_cq_exit3;
205 }
206
207 for (counter = 0; counter < param.act_pages; counter++) {
208 vpage = ipz_qpageit_get_inc(&my_cq->ipz_queue);
209 if (!vpage) {
210 ehca_err(device, "ipz_qpageit_get_inc() "
211 "returns NULL device=%p", device);
212 cq = ERR_PTR(-EAGAIN);
213 goto create_cq_exit4;
214 }
Michael Ellermanb4b8d1e2012-07-25 21:19:53 +0000215 rpage = __pa(vpage);
Heiko J Schickfab97222006-09-22 15:22:22 -0700216
217 h_ret = hipz_h_register_rpage_cq(adapter_handle,
218 my_cq->ipz_cq_handle,
219 &my_cq->pf,
220 0,
221 0,
222 rpage,
223 1,
224 my_cq->galpas.
225 kernel);
226
227 if (h_ret < H_SUCCESS) {
228 ehca_err(device, "hipz_h_register_rpage_cq() failed "
Stephen Rothwell3750f602009-01-16 14:55:28 -0800229 "ehca_cq=%p cq_num=%x h_ret=%lli counter=%i "
Heiko J Schickfab97222006-09-22 15:22:22 -0700230 "act_pages=%i", my_cq, my_cq->cq_number,
231 h_ret, counter, param.act_pages);
232 cq = ERR_PTR(-EINVAL);
233 goto create_cq_exit4;
234 }
235
236 if (counter == (param.act_pages - 1)) {
237 vpage = ipz_qpageit_get_inc(&my_cq->ipz_queue);
238 if ((h_ret != H_SUCCESS) || vpage) {
239 ehca_err(device, "Registration of pages not "
240 "complete ehca_cq=%p cq_num=%x "
Stephen Rothwell3750f602009-01-16 14:55:28 -0800241 "h_ret=%lli", my_cq, my_cq->cq_number,
Heiko J Schickfab97222006-09-22 15:22:22 -0700242 h_ret);
243 cq = ERR_PTR(-EAGAIN);
244 goto create_cq_exit4;
245 }
246 } else {
247 if (h_ret != H_PAGE_REGISTERED) {
248 ehca_err(device, "Registration of page failed "
Stephen Rothwell3750f602009-01-16 14:55:28 -0800249 "ehca_cq=%p cq_num=%x h_ret=%lli "
Heiko J Schickfab97222006-09-22 15:22:22 -0700250 "counter=%i act_pages=%i",
251 my_cq, my_cq->cq_number,
252 h_ret, counter, param.act_pages);
253 cq = ERR_PTR(-ENOMEM);
254 goto create_cq_exit4;
255 }
256 }
257 }
258
259 ipz_qeit_reset(&my_cq->ipz_queue);
260
261 gal = my_cq->galpas.kernel;
262 cqx_fec = hipz_galpa_load(gal, CQTEMM_OFFSET(cqx_fec));
Stephen Rothwell3750f602009-01-16 14:55:28 -0800263 ehca_dbg(device, "ehca_cq=%p cq_num=%x CQX_FEC=%llx",
Heiko J Schickfab97222006-09-22 15:22:22 -0700264 my_cq, my_cq->cq_number, cqx_fec);
265
266 my_cq->ib_cq.cqe = my_cq->nr_of_entries =
267 param.act_nr_of_entries - additional_cqe;
268 my_cq->cq_number = (my_cq->ipz_cq_handle.handle) & 0xffff;
269
270 for (i = 0; i < QP_HASHTAB_LEN; i++)
271 INIT_HLIST_HEAD(&my_cq->qp_hashtab[i]);
272
Alexander Schmidtb9012e02008-09-20 20:05:21 -0700273 INIT_LIST_HEAD(&my_cq->sqp_err_list);
274 INIT_LIST_HEAD(&my_cq->rqp_err_list);
275
Heiko J Schickfab97222006-09-22 15:22:22 -0700276 if (context) {
277 struct ipz_queue *ipz_queue = &my_cq->ipz_queue;
278 struct ehca_create_cq_resp resp;
Heiko J Schickfab97222006-09-22 15:22:22 -0700279 memset(&resp, 0, sizeof(resp));
280 resp.cq_number = my_cq->cq_number;
281 resp.token = my_cq->token;
282 resp.ipz_queue.qe_size = ipz_queue->qe_size;
283 resp.ipz_queue.act_nr_of_sg = ipz_queue->act_nr_of_sg;
284 resp.ipz_queue.queue_length = ipz_queue->queue_length;
285 resp.ipz_queue.pagesize = ipz_queue->pagesize;
286 resp.ipz_queue.toggle_state = ipz_queue->toggle_state;
Hoang-Nam Nguyene390d3b2007-09-11 15:31:06 +0200287 resp.fw_handle_ofs = (u32)
288 (my_cq->galpas.user.fw_handle & (PAGE_SIZE - 1));
Heiko J Schickfab97222006-09-22 15:22:22 -0700289 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
290 ehca_err(device, "Copy to udata failed.");
Yann Droneaud5bdb0f022014-03-10 23:06:25 +0100291 cq = ERR_PTR(-EFAULT);
Hoang-Nam Nguyen4c34bdf2007-01-24 00:13:35 +0100292 goto create_cq_exit4;
Heiko J Schickfab97222006-09-22 15:22:22 -0700293 }
294 }
295
296 return cq;
297
Heiko J Schickfab97222006-09-22 15:22:22 -0700298create_cq_exit4:
Stefan Roschere2f81da2007-07-20 16:04:17 +0200299 ipz_queue_dtor(NULL, &my_cq->ipz_queue);
Heiko J Schickfab97222006-09-22 15:22:22 -0700300
301create_cq_exit3:
302 h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
303 if (h_ret != H_SUCCESS)
304 ehca_err(device, "hipz_h_destroy_cq() failed ehca_cq=%p "
Stephen Rothwell3750f602009-01-16 14:55:28 -0800305 "cq_num=%x h_ret=%lli", my_cq, my_cq->cq_number, h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -0700306
307create_cq_exit2:
Joachim Fenkes26ed6872007-07-09 15:31:10 +0200308 write_lock_irqsave(&ehca_cq_idr_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700309 idr_remove(&ehca_cq_idr, my_cq->token);
Joachim Fenkes26ed6872007-07-09 15:31:10 +0200310 write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700311
312create_cq_exit1:
313 kmem_cache_free(cq_cache, my_cq);
314
Stefan Roscherd227fa72008-04-29 13:46:53 -0700315 atomic_dec(&shca->num_cqs);
Heiko J Schickfab97222006-09-22 15:22:22 -0700316 return cq;
317}
318
319int ehca_destroy_cq(struct ib_cq *cq)
320{
321 u64 h_ret;
Heiko J Schickfab97222006-09-22 15:22:22 -0700322 struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
323 int cq_num = my_cq->cq_number;
324 struct ib_device *device = cq->device;
325 struct ehca_shca *shca = container_of(device, struct ehca_shca,
326 ib_device);
327 struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
Heiko J Schickfab97222006-09-22 15:22:22 -0700328 unsigned long flags;
329
Hoang-Nam Nguyen4c34bdf2007-01-24 00:13:35 +0100330 if (cq->uobject) {
331 if (my_cq->mm_count_galpa || my_cq->mm_count_queue) {
332 ehca_err(device, "Resources still referenced in "
333 "user space cq_num=%x", my_cq->cq_number);
334 return -EINVAL;
335 }
Hoang-Nam Nguyen4c34bdf2007-01-24 00:13:35 +0100336 }
337
Joachim Fenkes28db6be2007-07-09 15:30:39 +0200338 /*
339 * remove the CQ from the idr first to make sure
340 * no more interrupt tasklets will touch this CQ
341 */
Joachim Fenkes26ed6872007-07-09 15:31:10 +0200342 write_lock_irqsave(&ehca_cq_idr_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700343 idr_remove(&ehca_cq_idr, my_cq->token);
Joachim Fenkes26ed6872007-07-09 15:31:10 +0200344 write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700345
Joachim Fenkes28db6be2007-07-09 15:30:39 +0200346 /* now wait until all pending events have completed */
347 wait_event(my_cq->wait_completion, !atomic_read(&my_cq->nr_events));
348
349 /* nobody's using our CQ any longer -- we can destroy it */
Heiko J Schickfab97222006-09-22 15:22:22 -0700350 h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 0);
351 if (h_ret == H_R_STATE) {
352 /* cq in err: read err data and destroy it forcibly */
Stephen Rothwell3750f602009-01-16 14:55:28 -0800353 ehca_dbg(device, "ehca_cq=%p cq_num=%x resource=%llx in err "
Heiko J Schickfab97222006-09-22 15:22:22 -0700354 "state. Try to delete it forcibly.",
355 my_cq, cq_num, my_cq->ipz_cq_handle.handle);
356 ehca_error_data(shca, my_cq, my_cq->ipz_cq_handle.handle);
357 h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
358 if (h_ret == H_SUCCESS)
359 ehca_dbg(device, "cq_num=%x deleted successfully.",
360 cq_num);
361 }
362 if (h_ret != H_SUCCESS) {
Stephen Rothwell3750f602009-01-16 14:55:28 -0800363 ehca_err(device, "hipz_h_destroy_cq() failed h_ret=%lli "
Heiko J Schickfab97222006-09-22 15:22:22 -0700364 "ehca_cq=%p cq_num=%x", h_ret, my_cq, cq_num);
365 return ehca2ib_return_code(h_ret);
366 }
Stefan Roschere2f81da2007-07-20 16:04:17 +0200367 ipz_queue_dtor(NULL, &my_cq->ipz_queue);
Heiko J Schickfab97222006-09-22 15:22:22 -0700368 kmem_cache_free(cq_cache, my_cq);
369
Stefan Roscherd227fa72008-04-29 13:46:53 -0700370 atomic_dec(&shca->num_cqs);
Heiko J Schickfab97222006-09-22 15:22:22 -0700371 return 0;
372}
373
374int ehca_resize_cq(struct ib_cq *cq, int cqe, struct ib_udata *udata)
375{
Heiko J Schickfab97222006-09-22 15:22:22 -0700376 /* TODO: proper resize needs to be done */
377 ehca_err(cq->device, "not implemented yet");
378
379 return -EFAULT;
380}
381
382int ehca_init_cq_cache(void)
383{
384 cq_cache = kmem_cache_create("ehca_cache_cq",
385 sizeof(struct ehca_cq), 0,
386 SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900387 NULL);
Heiko J Schickfab97222006-09-22 15:22:22 -0700388 if (!cq_cache)
389 return -ENOMEM;
390 return 0;
391}
392
393void ehca_cleanup_cq_cache(void)
394{
395 if (cq_cache)
396 kmem_cache_destroy(cq_cache);
397}