[PATCH] IB: Add copyright notices
[safe/jmp/linux-2.6] / drivers / infiniband / core / verbs.c
1 /*
2  * Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
3  * Copyright (c) 2004 Infinicon Corporation.  All rights reserved.
4  * Copyright (c) 2004 Intel Corporation.  All rights reserved.
5  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
6  * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
7  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
8  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
9  *
10  * This software is available to you under a choice of one of two
11  * licenses.  You may choose to be licensed under the terms of the GNU
12  * General Public License (GPL) Version 2, available from the file
13  * COPYING in the main directory of this source tree, or the
14  * OpenIB.org BSD license below:
15  *
16  *     Redistribution and use in source and binary forms, with or
17  *     without modification, are permitted provided that the following
18  *     conditions are met:
19  *
20  *      - Redistributions of source code must retain the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer.
23  *
24  *      - Redistributions in binary form must reproduce the above
25  *        copyright notice, this list of conditions and the following
26  *        disclaimer in the documentation and/or other materials
27  *        provided with the distribution.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36  * SOFTWARE.
37  *
38  * $Id: verbs.c 1349 2004-12-16 21:09:43Z roland $
39  */
40
41 #include <linux/errno.h>
42 #include <linux/err.h>
43
44 #include <ib_verbs.h>
45 #include <ib_cache.h>
46
47 /* Protection domains */
48
49 struct ib_pd *ib_alloc_pd(struct ib_device *device)
50 {
51         struct ib_pd *pd;
52
53         pd = device->alloc_pd(device, NULL, NULL);
54
55         if (!IS_ERR(pd)) {
56                 pd->device  = device;
57                 pd->uobject = NULL;
58                 atomic_set(&pd->usecnt, 0);
59         }
60
61         return pd;
62 }
63 EXPORT_SYMBOL(ib_alloc_pd);
64
65 int ib_dealloc_pd(struct ib_pd *pd)
66 {
67         if (atomic_read(&pd->usecnt))
68                 return -EBUSY;
69
70         return pd->device->dealloc_pd(pd);
71 }
72 EXPORT_SYMBOL(ib_dealloc_pd);
73
74 /* Address handles */
75
76 struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
77 {
78         struct ib_ah *ah;
79
80         ah = pd->device->create_ah(pd, ah_attr);
81
82         if (!IS_ERR(ah)) {
83                 ah->device  = pd->device;
84                 ah->pd      = pd;
85                 ah->uobject = NULL;
86                 atomic_inc(&pd->usecnt);
87         }
88
89         return ah;
90 }
91 EXPORT_SYMBOL(ib_create_ah);
92
93 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, struct ib_wc *wc,
94                                    struct ib_grh *grh, u8 port_num)
95 {
96         struct ib_ah_attr ah_attr;
97         u32 flow_class;
98         u16 gid_index;
99         int ret;
100
101         memset(&ah_attr, 0, sizeof ah_attr);
102         ah_attr.dlid = wc->slid;
103         ah_attr.sl = wc->sl;
104         ah_attr.src_path_bits = wc->dlid_path_bits;
105         ah_attr.port_num = port_num;
106
107         if (wc->wc_flags & IB_WC_GRH) {
108                 ah_attr.ah_flags = IB_AH_GRH;
109                 ah_attr.grh.dgid = grh->dgid;
110
111                 ret = ib_find_cached_gid(pd->device, &grh->sgid, &port_num,
112                                          &gid_index);
113                 if (ret)
114                         return ERR_PTR(ret);
115
116                 ah_attr.grh.sgid_index = (u8) gid_index;
117                 flow_class = be32_to_cpu(grh->version_tclass_flow);
118                 ah_attr.grh.flow_label = flow_class & 0xFFFFF;
119                 ah_attr.grh.traffic_class = (flow_class >> 20) & 0xFF;
120                 ah_attr.grh.hop_limit = grh->hop_limit;
121         }
122
123         return ib_create_ah(pd, &ah_attr);
124 }
125 EXPORT_SYMBOL(ib_create_ah_from_wc);
126
127 int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
128 {
129         return ah->device->modify_ah ?
130                 ah->device->modify_ah(ah, ah_attr) :
131                 -ENOSYS;
132 }
133 EXPORT_SYMBOL(ib_modify_ah);
134
135 int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
136 {
137         return ah->device->query_ah ?
138                 ah->device->query_ah(ah, ah_attr) :
139                 -ENOSYS;
140 }
141 EXPORT_SYMBOL(ib_query_ah);
142
143 int ib_destroy_ah(struct ib_ah *ah)
144 {
145         struct ib_pd *pd;
146         int ret;
147
148         pd = ah->pd;
149         ret = ah->device->destroy_ah(ah);
150         if (!ret)
151                 atomic_dec(&pd->usecnt);
152
153         return ret;
154 }
155 EXPORT_SYMBOL(ib_destroy_ah);
156
157 /* Queue pairs */
158
159 struct ib_qp *ib_create_qp(struct ib_pd *pd,
160                            struct ib_qp_init_attr *qp_init_attr)
161 {
162         struct ib_qp *qp;
163
164         qp = pd->device->create_qp(pd, qp_init_attr, NULL);
165
166         if (!IS_ERR(qp)) {
167                 qp->device        = pd->device;
168                 qp->pd            = pd;
169                 qp->send_cq       = qp_init_attr->send_cq;
170                 qp->recv_cq       = qp_init_attr->recv_cq;
171                 qp->srq           = qp_init_attr->srq;
172                 qp->uobject       = NULL;
173                 qp->event_handler = qp_init_attr->event_handler;
174                 qp->qp_context    = qp_init_attr->qp_context;
175                 qp->qp_type       = qp_init_attr->qp_type;
176                 atomic_inc(&pd->usecnt);
177                 atomic_inc(&qp_init_attr->send_cq->usecnt);
178                 atomic_inc(&qp_init_attr->recv_cq->usecnt);
179                 if (qp_init_attr->srq)
180                         atomic_inc(&qp_init_attr->srq->usecnt);
181         }
182
183         return qp;
184 }
185 EXPORT_SYMBOL(ib_create_qp);
186
187 int ib_modify_qp(struct ib_qp *qp,
188                  struct ib_qp_attr *qp_attr,
189                  int qp_attr_mask)
190 {
191         return qp->device->modify_qp(qp, qp_attr, qp_attr_mask);
192 }
193 EXPORT_SYMBOL(ib_modify_qp);
194
195 int ib_query_qp(struct ib_qp *qp,
196                 struct ib_qp_attr *qp_attr,
197                 int qp_attr_mask,
198                 struct ib_qp_init_attr *qp_init_attr)
199 {
200         return qp->device->query_qp ?
201                 qp->device->query_qp(qp, qp_attr, qp_attr_mask, qp_init_attr) :
202                 -ENOSYS;
203 }
204 EXPORT_SYMBOL(ib_query_qp);
205
206 int ib_destroy_qp(struct ib_qp *qp)
207 {
208         struct ib_pd *pd;
209         struct ib_cq *scq, *rcq;
210         struct ib_srq *srq;
211         int ret;
212
213         pd  = qp->pd;
214         scq = qp->send_cq;
215         rcq = qp->recv_cq;
216         srq = qp->srq;
217
218         ret = qp->device->destroy_qp(qp);
219         if (!ret) {
220                 atomic_dec(&pd->usecnt);
221                 atomic_dec(&scq->usecnt);
222                 atomic_dec(&rcq->usecnt);
223                 if (srq)
224                         atomic_dec(&srq->usecnt);
225         }
226
227         return ret;
228 }
229 EXPORT_SYMBOL(ib_destroy_qp);
230
231 /* Completion queues */
232
233 struct ib_cq *ib_create_cq(struct ib_device *device,
234                            ib_comp_handler comp_handler,
235                            void (*event_handler)(struct ib_event *, void *),
236                            void *cq_context, int cqe)
237 {
238         struct ib_cq *cq;
239
240         cq = device->create_cq(device, cqe, NULL, NULL);
241
242         if (!IS_ERR(cq)) {
243                 cq->device        = device;
244                 cq->uobject       = NULL;
245                 cq->comp_handler  = comp_handler;
246                 cq->event_handler = event_handler;
247                 cq->cq_context    = cq_context;
248                 atomic_set(&cq->usecnt, 0);
249         }
250
251         return cq;
252 }
253 EXPORT_SYMBOL(ib_create_cq);
254
255 int ib_destroy_cq(struct ib_cq *cq)
256 {
257         if (atomic_read(&cq->usecnt))
258                 return -EBUSY;
259
260         return cq->device->destroy_cq(cq);
261 }
262 EXPORT_SYMBOL(ib_destroy_cq);
263
264 int ib_resize_cq(struct ib_cq *cq,
265                  int           cqe)
266 {
267         int ret;
268
269         if (!cq->device->resize_cq)
270                 return -ENOSYS;
271
272         ret = cq->device->resize_cq(cq, &cqe);
273         if (!ret)
274                 cq->cqe = cqe;
275
276         return ret;
277 }
278 EXPORT_SYMBOL(ib_resize_cq);
279
280 /* Memory regions */
281
282 struct ib_mr *ib_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
283 {
284         struct ib_mr *mr;
285
286         mr = pd->device->get_dma_mr(pd, mr_access_flags);
287
288         if (!IS_ERR(mr)) {
289                 mr->device  = pd->device;
290                 mr->pd      = pd;
291                 mr->uobject = NULL;
292                 atomic_inc(&pd->usecnt);
293                 atomic_set(&mr->usecnt, 0);
294         }
295
296         return mr;
297 }
298 EXPORT_SYMBOL(ib_get_dma_mr);
299
300 struct ib_mr *ib_reg_phys_mr(struct ib_pd *pd,
301                              struct ib_phys_buf *phys_buf_array,
302                              int num_phys_buf,
303                              int mr_access_flags,
304                              u64 *iova_start)
305 {
306         struct ib_mr *mr;
307
308         mr = pd->device->reg_phys_mr(pd, phys_buf_array, num_phys_buf,
309                                      mr_access_flags, iova_start);
310
311         if (!IS_ERR(mr)) {
312                 mr->device  = pd->device;
313                 mr->pd      = pd;
314                 mr->uobject = NULL;
315                 atomic_inc(&pd->usecnt);
316                 atomic_set(&mr->usecnt, 0);
317         }
318
319         return mr;
320 }
321 EXPORT_SYMBOL(ib_reg_phys_mr);
322
323 int ib_rereg_phys_mr(struct ib_mr *mr,
324                      int mr_rereg_mask,
325                      struct ib_pd *pd,
326                      struct ib_phys_buf *phys_buf_array,
327                      int num_phys_buf,
328                      int mr_access_flags,
329                      u64 *iova_start)
330 {
331         struct ib_pd *old_pd;
332         int ret;
333
334         if (!mr->device->rereg_phys_mr)
335                 return -ENOSYS;
336
337         if (atomic_read(&mr->usecnt))
338                 return -EBUSY;
339
340         old_pd = mr->pd;
341
342         ret = mr->device->rereg_phys_mr(mr, mr_rereg_mask, pd,
343                                         phys_buf_array, num_phys_buf,
344                                         mr_access_flags, iova_start);
345
346         if (!ret && (mr_rereg_mask & IB_MR_REREG_PD)) {
347                 atomic_dec(&old_pd->usecnt);
348                 atomic_inc(&pd->usecnt);
349         }
350
351         return ret;
352 }
353 EXPORT_SYMBOL(ib_rereg_phys_mr);
354
355 int ib_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
356 {
357         return mr->device->query_mr ?
358                 mr->device->query_mr(mr, mr_attr) : -ENOSYS;
359 }
360 EXPORT_SYMBOL(ib_query_mr);
361
362 int ib_dereg_mr(struct ib_mr *mr)
363 {
364         struct ib_pd *pd;
365         int ret;
366
367         if (atomic_read(&mr->usecnt))
368                 return -EBUSY;
369
370         pd = mr->pd;
371         ret = mr->device->dereg_mr(mr);
372         if (!ret)
373                 atomic_dec(&pd->usecnt);
374
375         return ret;
376 }
377 EXPORT_SYMBOL(ib_dereg_mr);
378
379 /* Memory windows */
380
381 struct ib_mw *ib_alloc_mw(struct ib_pd *pd)
382 {
383         struct ib_mw *mw;
384
385         if (!pd->device->alloc_mw)
386                 return ERR_PTR(-ENOSYS);
387
388         mw = pd->device->alloc_mw(pd);
389         if (!IS_ERR(mw)) {
390                 mw->device  = pd->device;
391                 mw->pd      = pd;
392                 mw->uobject = NULL;
393                 atomic_inc(&pd->usecnt);
394         }
395
396         return mw;
397 }
398 EXPORT_SYMBOL(ib_alloc_mw);
399
400 int ib_dealloc_mw(struct ib_mw *mw)
401 {
402         struct ib_pd *pd;
403         int ret;
404
405         pd = mw->pd;
406         ret = mw->device->dealloc_mw(mw);
407         if (!ret)
408                 atomic_dec(&pd->usecnt);
409
410         return ret;
411 }
412 EXPORT_SYMBOL(ib_dealloc_mw);
413
414 /* "Fast" memory regions */
415
416 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
417                             int mr_access_flags,
418                             struct ib_fmr_attr *fmr_attr)
419 {
420         struct ib_fmr *fmr;
421
422         if (!pd->device->alloc_fmr)
423                 return ERR_PTR(-ENOSYS);
424
425         fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
426         if (!IS_ERR(fmr)) {
427                 fmr->device = pd->device;
428                 fmr->pd     = pd;
429                 atomic_inc(&pd->usecnt);
430         }
431
432         return fmr;
433 }
434 EXPORT_SYMBOL(ib_alloc_fmr);
435
436 int ib_unmap_fmr(struct list_head *fmr_list)
437 {
438         struct ib_fmr *fmr;
439
440         if (list_empty(fmr_list))
441                 return 0;
442
443         fmr = list_entry(fmr_list->next, struct ib_fmr, list);
444         return fmr->device->unmap_fmr(fmr_list);
445 }
446 EXPORT_SYMBOL(ib_unmap_fmr);
447
448 int ib_dealloc_fmr(struct ib_fmr *fmr)
449 {
450         struct ib_pd *pd;
451         int ret;
452
453         pd = fmr->pd;
454         ret = fmr->device->dealloc_fmr(fmr);
455         if (!ret)
456                 atomic_dec(&pd->usecnt);
457
458         return ret;
459 }
460 EXPORT_SYMBOL(ib_dealloc_fmr);
461
462 /* Multicast groups */
463
464 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
465 {
466         return qp->device->attach_mcast ?
467                 qp->device->attach_mcast(qp, gid, lid) :
468                 -ENOSYS;
469 }
470 EXPORT_SYMBOL(ib_attach_mcast);
471
472 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
473 {
474         return qp->device->detach_mcast ?
475                 qp->device->detach_mcast(qp, gid, lid) :
476                 -ENOSYS;
477 }
478 EXPORT_SYMBOL(ib_detach_mcast);