[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / drivers / infiniband / core / verbs.c
blob7c08ed0cd7dd78d99a58c16fadda7bb13de797d7
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.
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer.
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * SOFTWARE.
36 * $Id: verbs.c 1349 2004-12-16 21:09:43Z roland $
39 #include <linux/errno.h>
40 #include <linux/err.h>
42 #include <ib_verbs.h>
44 /* Protection domains */
46 struct ib_pd *ib_alloc_pd(struct ib_device *device)
48 struct ib_pd *pd;
50 pd = device->alloc_pd(device);
52 if (!IS_ERR(pd)) {
53 pd->device = device;
54 atomic_set(&pd->usecnt, 0);
57 return pd;
59 EXPORT_SYMBOL(ib_alloc_pd);
61 int ib_dealloc_pd(struct ib_pd *pd)
63 if (atomic_read(&pd->usecnt))
64 return -EBUSY;
66 return pd->device->dealloc_pd(pd);
68 EXPORT_SYMBOL(ib_dealloc_pd);
70 /* Address handles */
72 struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
74 struct ib_ah *ah;
76 ah = pd->device->create_ah(pd, ah_attr);
78 if (!IS_ERR(ah)) {
79 ah->device = pd->device;
80 ah->pd = pd;
81 atomic_inc(&pd->usecnt);
84 return ah;
86 EXPORT_SYMBOL(ib_create_ah);
88 int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
90 return ah->device->modify_ah ?
91 ah->device->modify_ah(ah, ah_attr) :
92 -ENOSYS;
94 EXPORT_SYMBOL(ib_modify_ah);
96 int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
98 return ah->device->query_ah ?
99 ah->device->query_ah(ah, ah_attr) :
100 -ENOSYS;
102 EXPORT_SYMBOL(ib_query_ah);
104 int ib_destroy_ah(struct ib_ah *ah)
106 struct ib_pd *pd;
107 int ret;
109 pd = ah->pd;
110 ret = ah->device->destroy_ah(ah);
111 if (!ret)
112 atomic_dec(&pd->usecnt);
114 return ret;
116 EXPORT_SYMBOL(ib_destroy_ah);
118 /* Queue pairs */
120 struct ib_qp *ib_create_qp(struct ib_pd *pd,
121 struct ib_qp_init_attr *qp_init_attr)
123 struct ib_qp *qp;
125 qp = pd->device->create_qp(pd, qp_init_attr);
127 if (!IS_ERR(qp)) {
128 qp->device = pd->device;
129 qp->pd = pd;
130 qp->send_cq = qp_init_attr->send_cq;
131 qp->recv_cq = qp_init_attr->recv_cq;
132 qp->srq = qp_init_attr->srq;
133 qp->event_handler = qp_init_attr->event_handler;
134 qp->qp_context = qp_init_attr->qp_context;
135 qp->qp_type = qp_init_attr->qp_type;
136 atomic_inc(&pd->usecnt);
137 atomic_inc(&qp_init_attr->send_cq->usecnt);
138 atomic_inc(&qp_init_attr->recv_cq->usecnt);
139 if (qp_init_attr->srq)
140 atomic_inc(&qp_init_attr->srq->usecnt);
143 return qp;
145 EXPORT_SYMBOL(ib_create_qp);
147 int ib_modify_qp(struct ib_qp *qp,
148 struct ib_qp_attr *qp_attr,
149 int qp_attr_mask)
151 return qp->device->modify_qp(qp, qp_attr, qp_attr_mask);
153 EXPORT_SYMBOL(ib_modify_qp);
155 int ib_query_qp(struct ib_qp *qp,
156 struct ib_qp_attr *qp_attr,
157 int qp_attr_mask,
158 struct ib_qp_init_attr *qp_init_attr)
160 return qp->device->query_qp ?
161 qp->device->query_qp(qp, qp_attr, qp_attr_mask, qp_init_attr) :
162 -ENOSYS;
164 EXPORT_SYMBOL(ib_query_qp);
166 int ib_destroy_qp(struct ib_qp *qp)
168 struct ib_pd *pd;
169 struct ib_cq *scq, *rcq;
170 struct ib_srq *srq;
171 int ret;
173 pd = qp->pd;
174 scq = qp->send_cq;
175 rcq = qp->recv_cq;
176 srq = qp->srq;
178 ret = qp->device->destroy_qp(qp);
179 if (!ret) {
180 atomic_dec(&pd->usecnt);
181 atomic_dec(&scq->usecnt);
182 atomic_dec(&rcq->usecnt);
183 if (srq)
184 atomic_dec(&srq->usecnt);
187 return ret;
189 EXPORT_SYMBOL(ib_destroy_qp);
191 /* Completion queues */
193 struct ib_cq *ib_create_cq(struct ib_device *device,
194 ib_comp_handler comp_handler,
195 void (*event_handler)(struct ib_event *, void *),
196 void *cq_context, int cqe)
198 struct ib_cq *cq;
200 cq = device->create_cq(device, cqe);
202 if (!IS_ERR(cq)) {
203 cq->device = device;
204 cq->comp_handler = comp_handler;
205 cq->event_handler = event_handler;
206 cq->cq_context = cq_context;
207 atomic_set(&cq->usecnt, 0);
210 return cq;
212 EXPORT_SYMBOL(ib_create_cq);
214 int ib_destroy_cq(struct ib_cq *cq)
216 if (atomic_read(&cq->usecnt))
217 return -EBUSY;
219 return cq->device->destroy_cq(cq);
221 EXPORT_SYMBOL(ib_destroy_cq);
223 int ib_resize_cq(struct ib_cq *cq,
224 int cqe)
226 int ret;
228 if (!cq->device->resize_cq)
229 return -ENOSYS;
231 ret = cq->device->resize_cq(cq, &cqe);
232 if (!ret)
233 cq->cqe = cqe;
235 return ret;
237 EXPORT_SYMBOL(ib_resize_cq);
239 /* Memory regions */
241 struct ib_mr *ib_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
243 struct ib_mr *mr;
245 mr = pd->device->get_dma_mr(pd, mr_access_flags);
247 if (!IS_ERR(mr)) {
248 mr->device = pd->device;
249 mr->pd = pd;
250 atomic_inc(&pd->usecnt);
251 atomic_set(&mr->usecnt, 0);
254 return mr;
256 EXPORT_SYMBOL(ib_get_dma_mr);
258 struct ib_mr *ib_reg_phys_mr(struct ib_pd *pd,
259 struct ib_phys_buf *phys_buf_array,
260 int num_phys_buf,
261 int mr_access_flags,
262 u64 *iova_start)
264 struct ib_mr *mr;
266 mr = pd->device->reg_phys_mr(pd, phys_buf_array, num_phys_buf,
267 mr_access_flags, iova_start);
269 if (!IS_ERR(mr)) {
270 mr->device = pd->device;
271 mr->pd = pd;
272 atomic_inc(&pd->usecnt);
273 atomic_set(&mr->usecnt, 0);
276 return mr;
278 EXPORT_SYMBOL(ib_reg_phys_mr);
280 int ib_rereg_phys_mr(struct ib_mr *mr,
281 int mr_rereg_mask,
282 struct ib_pd *pd,
283 struct ib_phys_buf *phys_buf_array,
284 int num_phys_buf,
285 int mr_access_flags,
286 u64 *iova_start)
288 struct ib_pd *old_pd;
289 int ret;
291 if (!mr->device->rereg_phys_mr)
292 return -ENOSYS;
294 if (atomic_read(&mr->usecnt))
295 return -EBUSY;
297 old_pd = mr->pd;
299 ret = mr->device->rereg_phys_mr(mr, mr_rereg_mask, pd,
300 phys_buf_array, num_phys_buf,
301 mr_access_flags, iova_start);
303 if (!ret && (mr_rereg_mask & IB_MR_REREG_PD)) {
304 atomic_dec(&old_pd->usecnt);
305 atomic_inc(&pd->usecnt);
308 return ret;
310 EXPORT_SYMBOL(ib_rereg_phys_mr);
312 int ib_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
314 return mr->device->query_mr ?
315 mr->device->query_mr(mr, mr_attr) : -ENOSYS;
317 EXPORT_SYMBOL(ib_query_mr);
319 int ib_dereg_mr(struct ib_mr *mr)
321 struct ib_pd *pd;
322 int ret;
324 if (atomic_read(&mr->usecnt))
325 return -EBUSY;
327 pd = mr->pd;
328 ret = mr->device->dereg_mr(mr);
329 if (!ret)
330 atomic_dec(&pd->usecnt);
332 return ret;
334 EXPORT_SYMBOL(ib_dereg_mr);
336 /* Memory windows */
338 struct ib_mw *ib_alloc_mw(struct ib_pd *pd)
340 struct ib_mw *mw;
342 if (!pd->device->alloc_mw)
343 return ERR_PTR(-ENOSYS);
345 mw = pd->device->alloc_mw(pd);
346 if (!IS_ERR(mw)) {
347 mw->device = pd->device;
348 mw->pd = pd;
349 atomic_inc(&pd->usecnt);
352 return mw;
354 EXPORT_SYMBOL(ib_alloc_mw);
356 int ib_dealloc_mw(struct ib_mw *mw)
358 struct ib_pd *pd;
359 int ret;
361 pd = mw->pd;
362 ret = mw->device->dealloc_mw(mw);
363 if (!ret)
364 atomic_dec(&pd->usecnt);
366 return ret;
368 EXPORT_SYMBOL(ib_dealloc_mw);
370 /* "Fast" memory regions */
372 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
373 int mr_access_flags,
374 struct ib_fmr_attr *fmr_attr)
376 struct ib_fmr *fmr;
378 if (!pd->device->alloc_fmr)
379 return ERR_PTR(-ENOSYS);
381 fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
382 if (!IS_ERR(fmr)) {
383 fmr->device = pd->device;
384 fmr->pd = pd;
385 atomic_inc(&pd->usecnt);
388 return fmr;
390 EXPORT_SYMBOL(ib_alloc_fmr);
392 int ib_unmap_fmr(struct list_head *fmr_list)
394 struct ib_fmr *fmr;
396 if (list_empty(fmr_list))
397 return 0;
399 fmr = list_entry(fmr_list->next, struct ib_fmr, list);
400 return fmr->device->unmap_fmr(fmr_list);
402 EXPORT_SYMBOL(ib_unmap_fmr);
404 int ib_dealloc_fmr(struct ib_fmr *fmr)
406 struct ib_pd *pd;
407 int ret;
409 pd = fmr->pd;
410 ret = fmr->device->dealloc_fmr(fmr);
411 if (!ret)
412 atomic_dec(&pd->usecnt);
414 return ret;
416 EXPORT_SYMBOL(ib_dealloc_fmr);
418 /* Multicast groups */
420 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
422 return qp->device->attach_mcast ?
423 qp->device->attach_mcast(qp, gid, lid) :
424 -ENOSYS;
426 EXPORT_SYMBOL(ib_attach_mcast);
428 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
430 return qp->device->detach_mcast ?
431 qp->device->detach_mcast(qp, gid, lid) :
432 -ENOSYS;
434 EXPORT_SYMBOL(ib_detach_mcast);