blob: 6ebfa27e4e16edadbe3a44e803a20c264dbb916d [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
46#include <asm/current.h>
47
48#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);
59 unsigned long spl_flags;
60
61 spin_lock_irqsave(&cq->spinlock, spl_flags);
62 hlist_add_head(&qp->list_entries, &cq->qp_hashtab[key]);
63 spin_unlock_irqrestore(&cq->spinlock, spl_flags);
64
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;
77 unsigned long spl_flags;
78
79 spin_lock_irqsave(&cq->spinlock, spl_flags);
80 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 }
91 spin_unlock_irqrestore(&cq->spinlock, spl_flags);
92 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
100struct ehca_qp* ehca_cq_get_qp(struct ehca_cq *cq, int real_qp_num)
101{
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
116struct ib_cq *ehca_create_cq(struct ib_device *device, int cqe,
117 struct ib_ucontext *context,
118 struct ib_udata *udata)
119{
120 static const u32 additional_cqe = 20;
121 struct ib_cq *cq;
122 struct ehca_cq *my_cq;
123 struct ehca_shca *shca =
124 container_of(device, struct ehca_shca, ib_device);
125 struct ipz_adapter_handle adapter_handle;
126 struct ehca_alloc_cq_parms param; /* h_call's out parameters */
127 struct h_galpa gal;
128 void *vpage;
129 u32 counter;
130 u64 rpage, cqx_fec, h_ret;
131 int ipz_rc, ret, i;
132 unsigned long flags;
133
134 if (cqe >= 0xFFFFFFFF - 64 - additional_cqe)
135 return ERR_PTR(-EINVAL);
136
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800137 my_cq = kmem_cache_zalloc(cq_cache, GFP_KERNEL);
Heiko J Schickfab97222006-09-22 15:22:22 -0700138 if (!my_cq) {
139 ehca_err(device, "Out of memory for ehca_cq struct device=%p",
140 device);
141 return ERR_PTR(-ENOMEM);
142 }
143
Heiko J Schickfab97222006-09-22 15:22:22 -0700144 memset(&param, 0, sizeof(struct ehca_alloc_cq_parms));
145
146 spin_lock_init(&my_cq->spinlock);
147 spin_lock_init(&my_cq->cb_lock);
148 spin_lock_init(&my_cq->task_lock);
149 my_cq->ownpid = current->tgid;
150
151 cq = &my_cq->ib_cq;
152
153 adapter_handle = shca->ipz_hca_handle;
154 param.eq_handle = shca->eq.ipz_eq_handle;
155
156 do {
157 if (!idr_pre_get(&ehca_cq_idr, GFP_KERNEL)) {
158 cq = ERR_PTR(-ENOMEM);
159 ehca_err(device, "Can't reserve idr nr. device=%p",
160 device);
161 goto create_cq_exit1;
162 }
163
164 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
165 ret = idr_get_new(&ehca_cq_idr, my_cq, &my_cq->token);
166 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
167
168 } while (ret == -EAGAIN);
169
170 if (ret) {
171 cq = ERR_PTR(-ENOMEM);
172 ehca_err(device, "Can't allocate new idr entry. device=%p",
173 device);
174 goto create_cq_exit1;
175 }
176
177 /*
178 * CQs maximum depth is 4GB-64, but we need additional 20 as buffer
179 * for receiving errors CQEs.
180 */
181 param.nr_cqe = cqe + additional_cqe;
182 h_ret = hipz_h_alloc_resource_cq(adapter_handle, my_cq, &param);
183
184 if (h_ret != H_SUCCESS) {
185 ehca_err(device, "hipz_h_alloc_resource_cq() failed "
186 "h_ret=%lx device=%p", h_ret, device);
187 cq = ERR_PTR(ehca2ib_return_code(h_ret));
188 goto create_cq_exit2;
189 }
190
191 ipz_rc = ipz_queue_ctor(&my_cq->ipz_queue, param.act_pages,
192 EHCA_PAGESIZE, sizeof(struct ehca_cqe), 0);
193 if (!ipz_rc) {
194 ehca_err(device, "ipz_queue_ctor() failed ipz_rc=%x device=%p",
195 ipz_rc, device);
196 cq = ERR_PTR(-EINVAL);
197 goto create_cq_exit3;
198 }
199
200 for (counter = 0; counter < param.act_pages; counter++) {
201 vpage = ipz_qpageit_get_inc(&my_cq->ipz_queue);
202 if (!vpage) {
203 ehca_err(device, "ipz_qpageit_get_inc() "
204 "returns NULL device=%p", device);
205 cq = ERR_PTR(-EAGAIN);
206 goto create_cq_exit4;
207 }
208 rpage = virt_to_abs(vpage);
209
210 h_ret = hipz_h_register_rpage_cq(adapter_handle,
211 my_cq->ipz_cq_handle,
212 &my_cq->pf,
213 0,
214 0,
215 rpage,
216 1,
217 my_cq->galpas.
218 kernel);
219
220 if (h_ret < H_SUCCESS) {
221 ehca_err(device, "hipz_h_register_rpage_cq() failed "
222 "ehca_cq=%p cq_num=%x h_ret=%lx counter=%i "
223 "act_pages=%i", my_cq, my_cq->cq_number,
224 h_ret, counter, param.act_pages);
225 cq = ERR_PTR(-EINVAL);
226 goto create_cq_exit4;
227 }
228
229 if (counter == (param.act_pages - 1)) {
230 vpage = ipz_qpageit_get_inc(&my_cq->ipz_queue);
231 if ((h_ret != H_SUCCESS) || vpage) {
232 ehca_err(device, "Registration of pages not "
233 "complete ehca_cq=%p cq_num=%x "
234 "h_ret=%lx", my_cq, my_cq->cq_number,
235 h_ret);
236 cq = ERR_PTR(-EAGAIN);
237 goto create_cq_exit4;
238 }
239 } else {
240 if (h_ret != H_PAGE_REGISTERED) {
241 ehca_err(device, "Registration of page failed "
242 "ehca_cq=%p cq_num=%x h_ret=%lx"
243 "counter=%i act_pages=%i",
244 my_cq, my_cq->cq_number,
245 h_ret, counter, param.act_pages);
246 cq = ERR_PTR(-ENOMEM);
247 goto create_cq_exit4;
248 }
249 }
250 }
251
252 ipz_qeit_reset(&my_cq->ipz_queue);
253
254 gal = my_cq->galpas.kernel;
255 cqx_fec = hipz_galpa_load(gal, CQTEMM_OFFSET(cqx_fec));
256 ehca_dbg(device, "ehca_cq=%p cq_num=%x CQX_FEC=%lx",
257 my_cq, my_cq->cq_number, cqx_fec);
258
259 my_cq->ib_cq.cqe = my_cq->nr_of_entries =
260 param.act_nr_of_entries - additional_cqe;
261 my_cq->cq_number = (my_cq->ipz_cq_handle.handle) & 0xffff;
262
263 for (i = 0; i < QP_HASHTAB_LEN; i++)
264 INIT_HLIST_HEAD(&my_cq->qp_hashtab[i]);
265
266 if (context) {
267 struct ipz_queue *ipz_queue = &my_cq->ipz_queue;
268 struct ehca_create_cq_resp resp;
Heiko J Schickfab97222006-09-22 15:22:22 -0700269 memset(&resp, 0, sizeof(resp));
270 resp.cq_number = my_cq->cq_number;
271 resp.token = my_cq->token;
272 resp.ipz_queue.qe_size = ipz_queue->qe_size;
273 resp.ipz_queue.act_nr_of_sg = ipz_queue->act_nr_of_sg;
274 resp.ipz_queue.queue_length = ipz_queue->queue_length;
275 resp.ipz_queue.pagesize = ipz_queue->pagesize;
276 resp.ipz_queue.toggle_state = ipz_queue->toggle_state;
Heiko J Schickfab97222006-09-22 15:22:22 -0700277 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
278 ehca_err(device, "Copy to udata failed.");
Hoang-Nam Nguyen4c34bdf2007-01-24 00:13:35 +0100279 goto create_cq_exit4;
Heiko J Schickfab97222006-09-22 15:22:22 -0700280 }
281 }
282
283 return cq;
284
Heiko J Schickfab97222006-09-22 15:22:22 -0700285create_cq_exit4:
286 ipz_queue_dtor(&my_cq->ipz_queue);
287
288create_cq_exit3:
289 h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
290 if (h_ret != H_SUCCESS)
291 ehca_err(device, "hipz_h_destroy_cq() failed ehca_cq=%p "
292 "cq_num=%x h_ret=%lx", my_cq, my_cq->cq_number, h_ret);
293
294create_cq_exit2:
295 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
296 idr_remove(&ehca_cq_idr, my_cq->token);
297 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
298
299create_cq_exit1:
300 kmem_cache_free(cq_cache, my_cq);
301
302 return cq;
303}
304
305int ehca_destroy_cq(struct ib_cq *cq)
306{
307 u64 h_ret;
Heiko J Schickfab97222006-09-22 15:22:22 -0700308 struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
309 int cq_num = my_cq->cq_number;
310 struct ib_device *device = cq->device;
311 struct ehca_shca *shca = container_of(device, struct ehca_shca,
312 ib_device);
313 struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
314 u32 cur_pid = current->tgid;
315 unsigned long flags;
316
Hoang-Nam Nguyen4c34bdf2007-01-24 00:13:35 +0100317 if (cq->uobject) {
318 if (my_cq->mm_count_galpa || my_cq->mm_count_queue) {
319 ehca_err(device, "Resources still referenced in "
320 "user space cq_num=%x", my_cq->cq_number);
321 return -EINVAL;
322 }
323 if (my_cq->ownpid != cur_pid) {
324 ehca_err(device, "Invalid caller pid=%x ownpid=%x "
325 "cq_num=%x",
326 cur_pid, my_cq->ownpid, my_cq->cq_number);
327 return -EINVAL;
328 }
329 }
330
Heiko J Schickfab97222006-09-22 15:22:22 -0700331 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
Hoang-Nam Nguyence29d722007-01-19 22:50:10 +0100332 while (my_cq->nr_callbacks) {
333 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700334 yield();
Hoang-Nam Nguyence29d722007-01-19 22:50:10 +0100335 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
336 }
Heiko J Schickfab97222006-09-22 15:22:22 -0700337
338 idr_remove(&ehca_cq_idr, my_cq->token);
339 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
340
Heiko J Schickfab97222006-09-22 15:22:22 -0700341 h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 0);
342 if (h_ret == H_R_STATE) {
343 /* cq in err: read err data and destroy it forcibly */
344 ehca_dbg(device, "ehca_cq=%p cq_num=%x ressource=%lx in err "
345 "state. Try to delete it forcibly.",
346 my_cq, cq_num, my_cq->ipz_cq_handle.handle);
347 ehca_error_data(shca, my_cq, my_cq->ipz_cq_handle.handle);
348 h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
349 if (h_ret == H_SUCCESS)
350 ehca_dbg(device, "cq_num=%x deleted successfully.",
351 cq_num);
352 }
353 if (h_ret != H_SUCCESS) {
354 ehca_err(device, "hipz_h_destroy_cq() failed h_ret=%lx "
355 "ehca_cq=%p cq_num=%x", h_ret, my_cq, cq_num);
356 return ehca2ib_return_code(h_ret);
357 }
358 ipz_queue_dtor(&my_cq->ipz_queue);
359 kmem_cache_free(cq_cache, my_cq);
360
361 return 0;
362}
363
364int ehca_resize_cq(struct ib_cq *cq, int cqe, struct ib_udata *udata)
365{
366 struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
367 u32 cur_pid = current->tgid;
368
Hoang-Nam Nguyen4c34bdf2007-01-24 00:13:35 +0100369 if (cq->uobject && my_cq->ownpid != cur_pid) {
Heiko J Schickfab97222006-09-22 15:22:22 -0700370 ehca_err(cq->device, "Invalid caller pid=%x ownpid=%x",
371 cur_pid, my_cq->ownpid);
372 return -EINVAL;
373 }
374
375 /* TODO: proper resize needs to be done */
376 ehca_err(cq->device, "not implemented yet");
377
378 return -EFAULT;
379}
380
381int ehca_init_cq_cache(void)
382{
383 cq_cache = kmem_cache_create("ehca_cache_cq",
384 sizeof(struct ehca_cq), 0,
385 SLAB_HWCACHE_ALIGN,
386 NULL, NULL);
387 if (!cq_cache)
388 return -ENOMEM;
389 return 0;
390}
391
392void ehca_cleanup_cq_cache(void)
393{
394 if (cq_cache)
395 kmem_cache_destroy(cq_cache);
396}