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 Cisco Systems. All rights reserved.
9 * This software is available to you under a choice of one of two
10 * licenses. You may choose to be licensed under the terms of the GNU
11 * General Public License (GPL) Version 2, available from the file
12 * COPYING in the main directory of this source tree, or the
13 * OpenIB.org BSD license below:
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
23 * - Redistributions in binary form must reproduce the above
24 * copyright notice, this list of conditions and the following
25 * disclaimer in the documentation and/or other materials
26 * provided with the distribution.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 * $Id: verbs.c 1349 2004-12-16 21:09:43Z roland $
40 #include <linux/errno.h>
41 #include <linux/err.h>
46 /* Protection domains */
48 struct ib_pd
*ib_alloc_pd(struct ib_device
*device
)
52 pd
= device
->alloc_pd(device
, NULL
, NULL
);
57 atomic_set(&pd
->usecnt
, 0);
62 EXPORT_SYMBOL(ib_alloc_pd
);
64 int ib_dealloc_pd(struct ib_pd
*pd
)
66 if (atomic_read(&pd
->usecnt
))
69 return pd
->device
->dealloc_pd(pd
);
71 EXPORT_SYMBOL(ib_dealloc_pd
);
75 struct ib_ah
*ib_create_ah(struct ib_pd
*pd
, struct ib_ah_attr
*ah_attr
)
79 ah
= pd
->device
->create_ah(pd
, ah_attr
);
82 ah
->device
= pd
->device
;
85 atomic_inc(&pd
->usecnt
);
90 EXPORT_SYMBOL(ib_create_ah
);
92 struct ib_ah
*ib_create_ah_from_wc(struct ib_pd
*pd
, struct ib_wc
*wc
,
93 struct ib_grh
*grh
, u8 port_num
)
95 struct ib_ah_attr ah_attr
;
100 memset(&ah_attr
, 0, sizeof ah_attr
);
101 ah_attr
.dlid
= wc
->slid
;
103 ah_attr
.src_path_bits
= wc
->dlid_path_bits
;
104 ah_attr
.port_num
= port_num
;
106 if (wc
->wc_flags
& IB_WC_GRH
) {
107 ah_attr
.ah_flags
= IB_AH_GRH
;
108 ah_attr
.grh
.dgid
= grh
->dgid
;
110 ret
= ib_find_cached_gid(pd
->device
, &grh
->sgid
, &port_num
,
115 ah_attr
.grh
.sgid_index
= (u8
) gid_index
;
116 flow_class
= be32_to_cpu(grh
->version_tclass_flow
);
117 ah_attr
.grh
.flow_label
= flow_class
& 0xFFFFF;
118 ah_attr
.grh
.traffic_class
= (flow_class
>> 20) & 0xFF;
119 ah_attr
.grh
.hop_limit
= grh
->hop_limit
;
122 return ib_create_ah(pd
, &ah_attr
);
124 EXPORT_SYMBOL(ib_create_ah_from_wc
);
126 int ib_modify_ah(struct ib_ah
*ah
, struct ib_ah_attr
*ah_attr
)
128 return ah
->device
->modify_ah
?
129 ah
->device
->modify_ah(ah
, ah_attr
) :
132 EXPORT_SYMBOL(ib_modify_ah
);
134 int ib_query_ah(struct ib_ah
*ah
, struct ib_ah_attr
*ah_attr
)
136 return ah
->device
->query_ah
?
137 ah
->device
->query_ah(ah
, ah_attr
) :
140 EXPORT_SYMBOL(ib_query_ah
);
142 int ib_destroy_ah(struct ib_ah
*ah
)
148 ret
= ah
->device
->destroy_ah(ah
);
150 atomic_dec(&pd
->usecnt
);
154 EXPORT_SYMBOL(ib_destroy_ah
);
158 struct ib_qp
*ib_create_qp(struct ib_pd
*pd
,
159 struct ib_qp_init_attr
*qp_init_attr
)
163 qp
= pd
->device
->create_qp(pd
, qp_init_attr
, NULL
);
166 qp
->device
= pd
->device
;
168 qp
->send_cq
= qp_init_attr
->send_cq
;
169 qp
->recv_cq
= qp_init_attr
->recv_cq
;
170 qp
->srq
= qp_init_attr
->srq
;
172 qp
->event_handler
= qp_init_attr
->event_handler
;
173 qp
->qp_context
= qp_init_attr
->qp_context
;
174 qp
->qp_type
= qp_init_attr
->qp_type
;
175 atomic_inc(&pd
->usecnt
);
176 atomic_inc(&qp_init_attr
->send_cq
->usecnt
);
177 atomic_inc(&qp_init_attr
->recv_cq
->usecnt
);
178 if (qp_init_attr
->srq
)
179 atomic_inc(&qp_init_attr
->srq
->usecnt
);
184 EXPORT_SYMBOL(ib_create_qp
);
186 int ib_modify_qp(struct ib_qp
*qp
,
187 struct ib_qp_attr
*qp_attr
,
190 return qp
->device
->modify_qp(qp
, qp_attr
, qp_attr_mask
);
192 EXPORT_SYMBOL(ib_modify_qp
);
194 int ib_query_qp(struct ib_qp
*qp
,
195 struct ib_qp_attr
*qp_attr
,
197 struct ib_qp_init_attr
*qp_init_attr
)
199 return qp
->device
->query_qp
?
200 qp
->device
->query_qp(qp
, qp_attr
, qp_attr_mask
, qp_init_attr
) :
203 EXPORT_SYMBOL(ib_query_qp
);
205 int ib_destroy_qp(struct ib_qp
*qp
)
208 struct ib_cq
*scq
, *rcq
;
217 ret
= qp
->device
->destroy_qp(qp
);
219 atomic_dec(&pd
->usecnt
);
220 atomic_dec(&scq
->usecnt
);
221 atomic_dec(&rcq
->usecnt
);
223 atomic_dec(&srq
->usecnt
);
228 EXPORT_SYMBOL(ib_destroy_qp
);
230 /* Completion queues */
232 struct ib_cq
*ib_create_cq(struct ib_device
*device
,
233 ib_comp_handler comp_handler
,
234 void (*event_handler
)(struct ib_event
*, void *),
235 void *cq_context
, int cqe
)
239 cq
= device
->create_cq(device
, cqe
, NULL
, NULL
);
244 cq
->comp_handler
= comp_handler
;
245 cq
->event_handler
= event_handler
;
246 cq
->cq_context
= cq_context
;
247 atomic_set(&cq
->usecnt
, 0);
252 EXPORT_SYMBOL(ib_create_cq
);
254 int ib_destroy_cq(struct ib_cq
*cq
)
256 if (atomic_read(&cq
->usecnt
))
259 return cq
->device
->destroy_cq(cq
);
261 EXPORT_SYMBOL(ib_destroy_cq
);
263 int ib_resize_cq(struct ib_cq
*cq
,
268 if (!cq
->device
->resize_cq
)
271 ret
= cq
->device
->resize_cq(cq
, &cqe
);
277 EXPORT_SYMBOL(ib_resize_cq
);
281 struct ib_mr
*ib_get_dma_mr(struct ib_pd
*pd
, int mr_access_flags
)
285 mr
= pd
->device
->get_dma_mr(pd
, mr_access_flags
);
288 mr
->device
= pd
->device
;
291 atomic_inc(&pd
->usecnt
);
292 atomic_set(&mr
->usecnt
, 0);
297 EXPORT_SYMBOL(ib_get_dma_mr
);
299 struct ib_mr
*ib_reg_phys_mr(struct ib_pd
*pd
,
300 struct ib_phys_buf
*phys_buf_array
,
307 mr
= pd
->device
->reg_phys_mr(pd
, phys_buf_array
, num_phys_buf
,
308 mr_access_flags
, iova_start
);
311 mr
->device
= pd
->device
;
314 atomic_inc(&pd
->usecnt
);
315 atomic_set(&mr
->usecnt
, 0);
320 EXPORT_SYMBOL(ib_reg_phys_mr
);
322 int ib_rereg_phys_mr(struct ib_mr
*mr
,
325 struct ib_phys_buf
*phys_buf_array
,
330 struct ib_pd
*old_pd
;
333 if (!mr
->device
->rereg_phys_mr
)
336 if (atomic_read(&mr
->usecnt
))
341 ret
= mr
->device
->rereg_phys_mr(mr
, mr_rereg_mask
, pd
,
342 phys_buf_array
, num_phys_buf
,
343 mr_access_flags
, iova_start
);
345 if (!ret
&& (mr_rereg_mask
& IB_MR_REREG_PD
)) {
346 atomic_dec(&old_pd
->usecnt
);
347 atomic_inc(&pd
->usecnt
);
352 EXPORT_SYMBOL(ib_rereg_phys_mr
);
354 int ib_query_mr(struct ib_mr
*mr
, struct ib_mr_attr
*mr_attr
)
356 return mr
->device
->query_mr
?
357 mr
->device
->query_mr(mr
, mr_attr
) : -ENOSYS
;
359 EXPORT_SYMBOL(ib_query_mr
);
361 int ib_dereg_mr(struct ib_mr
*mr
)
366 if (atomic_read(&mr
->usecnt
))
370 ret
= mr
->device
->dereg_mr(mr
);
372 atomic_dec(&pd
->usecnt
);
376 EXPORT_SYMBOL(ib_dereg_mr
);
380 struct ib_mw
*ib_alloc_mw(struct ib_pd
*pd
)
384 if (!pd
->device
->alloc_mw
)
385 return ERR_PTR(-ENOSYS
);
387 mw
= pd
->device
->alloc_mw(pd
);
389 mw
->device
= pd
->device
;
392 atomic_inc(&pd
->usecnt
);
397 EXPORT_SYMBOL(ib_alloc_mw
);
399 int ib_dealloc_mw(struct ib_mw
*mw
)
405 ret
= mw
->device
->dealloc_mw(mw
);
407 atomic_dec(&pd
->usecnt
);
411 EXPORT_SYMBOL(ib_dealloc_mw
);
413 /* "Fast" memory regions */
415 struct ib_fmr
*ib_alloc_fmr(struct ib_pd
*pd
,
417 struct ib_fmr_attr
*fmr_attr
)
421 if (!pd
->device
->alloc_fmr
)
422 return ERR_PTR(-ENOSYS
);
424 fmr
= pd
->device
->alloc_fmr(pd
, mr_access_flags
, fmr_attr
);
426 fmr
->device
= pd
->device
;
428 atomic_inc(&pd
->usecnt
);
433 EXPORT_SYMBOL(ib_alloc_fmr
);
435 int ib_unmap_fmr(struct list_head
*fmr_list
)
439 if (list_empty(fmr_list
))
442 fmr
= list_entry(fmr_list
->next
, struct ib_fmr
, list
);
443 return fmr
->device
->unmap_fmr(fmr_list
);
445 EXPORT_SYMBOL(ib_unmap_fmr
);
447 int ib_dealloc_fmr(struct ib_fmr
*fmr
)
453 ret
= fmr
->device
->dealloc_fmr(fmr
);
455 atomic_dec(&pd
->usecnt
);
459 EXPORT_SYMBOL(ib_dealloc_fmr
);
461 /* Multicast groups */
463 int ib_attach_mcast(struct ib_qp
*qp
, union ib_gid
*gid
, u16 lid
)
465 return qp
->device
->attach_mcast
?
466 qp
->device
->attach_mcast(qp
, gid
, lid
) :
469 EXPORT_SYMBOL(ib_attach_mcast
);
471 int ib_detach_mcast(struct ib_qp
*qp
, union ib_gid
*gid
, u16 lid
)
473 return qp
->device
->detach_mcast
?
474 qp
->device
->detach_mcast(qp
, gid
, lid
) :
477 EXPORT_SYMBOL(ib_detach_mcast
);