Merge git://git.infradead.org/mtd-2.6
[linux-2.6] / drivers / infiniband / hw / ehca / ehca_cq.c
1 /*
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
53 static struct kmem_cache *cq_cache;
54
55 int 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 flags;
60
61         spin_lock_irqsave(&cq->spinlock, flags);
62         hlist_add_head(&qp->list_entries, &cq->qp_hashtab[key]);
63         spin_unlock_irqrestore(&cq->spinlock, 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
71 int 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 flags;
78
79         spin_lock_irqsave(&cq->spinlock, 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, 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
100 struct 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
116 struct ib_cq *ehca_create_cq(struct ib_device *device, int cqe, int comp_vector,
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
137         my_cq = kmem_cache_zalloc(cq_cache, GFP_KERNEL);
138         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
144         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         atomic_set(&my_cq->nr_events, 0);
150         init_waitqueue_head(&my_cq->wait_completion);
151         my_cq->ownpid = current->tgid;
152
153         cq = &my_cq->ib_cq;
154
155         adapter_handle = shca->ipz_hca_handle;
156         param.eq_handle = shca->eq.ipz_eq_handle;
157
158         do {
159                 if (!idr_pre_get(&ehca_cq_idr, GFP_KERNEL)) {
160                         cq = ERR_PTR(-ENOMEM);
161                         ehca_err(device, "Can't reserve idr nr. device=%p",
162                                  device);
163                         goto create_cq_exit1;
164                 }
165
166                 write_lock_irqsave(&ehca_cq_idr_lock, flags);
167                 ret = idr_get_new(&ehca_cq_idr, my_cq, &my_cq->token);
168                 write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
169         } while (ret == -EAGAIN);
170
171         if (ret) {
172                 cq = ERR_PTR(-ENOMEM);
173                 ehca_err(device, "Can't allocate new idr entry. device=%p",
174                          device);
175                 goto create_cq_exit1;
176         }
177
178         if (my_cq->token > 0x1FFFFFF) {
179                 cq = ERR_PTR(-ENOMEM);
180                 ehca_err(device, "Invalid number of cq. device=%p", device);
181                 goto create_cq_exit2;
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 "
193                          "h_ret=%li device=%p", h_ret, device);
194                 cq = ERR_PTR(ehca2ib_return_code(h_ret));
195                 goto create_cq_exit2;
196         }
197
198         ipz_rc = ipz_queue_ctor(NULL, &my_cq->ipz_queue, param.act_pages,
199                                 EHCA_PAGESIZE, sizeof(struct ehca_cqe), 0, 0);
200         if (!ipz_rc) {
201                 ehca_err(device, "ipz_queue_ctor() failed ipz_rc=%i device=%p",
202                          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                 }
215                 rpage = virt_to_abs(vpage);
216
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 "
229                                  "ehca_cq=%p cq_num=%x h_ret=%li counter=%i "
230                                  "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 "
241                                          "h_ret=%li", my_cq, my_cq->cq_number,
242                                          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 "
249                                          "ehca_cq=%p cq_num=%x h_ret=%li "
250                                          "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));
263         ehca_dbg(device, "ehca_cq=%p cq_num=%x CQX_FEC=%lx",
264                  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
273         if (context) {
274                 struct ipz_queue *ipz_queue = &my_cq->ipz_queue;
275                 struct ehca_create_cq_resp resp;
276                 memset(&resp, 0, sizeof(resp));
277                 resp.cq_number = my_cq->cq_number;
278                 resp.token = my_cq->token;
279                 resp.ipz_queue.qe_size = ipz_queue->qe_size;
280                 resp.ipz_queue.act_nr_of_sg = ipz_queue->act_nr_of_sg;
281                 resp.ipz_queue.queue_length = ipz_queue->queue_length;
282                 resp.ipz_queue.pagesize = ipz_queue->pagesize;
283                 resp.ipz_queue.toggle_state = ipz_queue->toggle_state;
284                 resp.fw_handle_ofs = (u32)
285                         (my_cq->galpas.user.fw_handle & (PAGE_SIZE - 1));
286                 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
287                         ehca_err(device, "Copy to udata failed.");
288                         goto create_cq_exit4;
289                 }
290         }
291
292         return cq;
293
294 create_cq_exit4:
295         ipz_queue_dtor(NULL, &my_cq->ipz_queue);
296
297 create_cq_exit3:
298         h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
299         if (h_ret != H_SUCCESS)
300                 ehca_err(device, "hipz_h_destroy_cq() failed ehca_cq=%p "
301                          "cq_num=%x h_ret=%li", my_cq, my_cq->cq_number, h_ret);
302
303 create_cq_exit2:
304         write_lock_irqsave(&ehca_cq_idr_lock, flags);
305         idr_remove(&ehca_cq_idr, my_cq->token);
306         write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
307
308 create_cq_exit1:
309         kmem_cache_free(cq_cache, my_cq);
310
311         return cq;
312 }
313
314 int ehca_destroy_cq(struct ib_cq *cq)
315 {
316         u64 h_ret;
317         struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
318         int cq_num = my_cq->cq_number;
319         struct ib_device *device = cq->device;
320         struct ehca_shca *shca = container_of(device, struct ehca_shca,
321                                               ib_device);
322         struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
323         u32 cur_pid = current->tgid;
324         unsigned long flags;
325
326         if (cq->uobject) {
327                 if (my_cq->mm_count_galpa || my_cq->mm_count_queue) {
328                         ehca_err(device, "Resources still referenced in "
329                                  "user space cq_num=%x", my_cq->cq_number);
330                         return -EINVAL;
331                 }
332                 if (my_cq->ownpid != cur_pid) {
333                         ehca_err(device, "Invalid caller pid=%x ownpid=%x "
334                                  "cq_num=%x",
335                                  cur_pid, my_cq->ownpid, my_cq->cq_number);
336                         return -EINVAL;
337                 }
338         }
339
340         /*
341          * remove the CQ from the idr first to make sure
342          * no more interrupt tasklets will touch this CQ
343          */
344         write_lock_irqsave(&ehca_cq_idr_lock, flags);
345         idr_remove(&ehca_cq_idr, my_cq->token);
346         write_unlock_irqrestore(&ehca_cq_idr_lock, flags);
347
348         /* now wait until all pending events have completed */
349         wait_event(my_cq->wait_completion, !atomic_read(&my_cq->nr_events));
350
351         /* nobody's using our CQ any longer -- we can destroy it */
352         h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 0);
353         if (h_ret == H_R_STATE) {
354                 /* cq in err: read err data and destroy it forcibly */
355                 ehca_dbg(device, "ehca_cq=%p cq_num=%x ressource=%lx in err "
356                          "state. Try to delete it forcibly.",
357                          my_cq, cq_num, my_cq->ipz_cq_handle.handle);
358                 ehca_error_data(shca, my_cq, my_cq->ipz_cq_handle.handle);
359                 h_ret = hipz_h_destroy_cq(adapter_handle, my_cq, 1);
360                 if (h_ret == H_SUCCESS)
361                         ehca_dbg(device, "cq_num=%x deleted successfully.",
362                                  cq_num);
363         }
364         if (h_ret != H_SUCCESS) {
365                 ehca_err(device, "hipz_h_destroy_cq() failed h_ret=%li "
366                          "ehca_cq=%p cq_num=%x", h_ret, my_cq, cq_num);
367                 return ehca2ib_return_code(h_ret);
368         }
369         ipz_queue_dtor(NULL, &my_cq->ipz_queue);
370         kmem_cache_free(cq_cache, my_cq);
371
372         return 0;
373 }
374
375 int ehca_resize_cq(struct ib_cq *cq, int cqe, struct ib_udata *udata)
376 {
377         struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
378         u32 cur_pid = current->tgid;
379
380         if (cq->uobject && my_cq->ownpid != cur_pid) {
381                 ehca_err(cq->device, "Invalid caller pid=%x ownpid=%x",
382                          cur_pid, my_cq->ownpid);
383                 return -EINVAL;
384         }
385
386         /* TODO: proper resize needs to be done */
387         ehca_err(cq->device, "not implemented yet");
388
389         return -EFAULT;
390 }
391
392 int ehca_init_cq_cache(void)
393 {
394         cq_cache = kmem_cache_create("ehca_cache_cq",
395                                      sizeof(struct ehca_cq), 0,
396                                      SLAB_HWCACHE_ALIGN,
397                                      NULL);
398         if (!cq_cache)
399                 return -ENOMEM;
400         return 0;
401 }
402
403 void ehca_cleanup_cq_cache(void)
404 {
405         if (cq_cache)
406                 kmem_cache_destroy(cq_cache);
407 }