Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6
[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, 2006 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 #include <linux/string.h>
44
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_cache.h>
47
48 /* Protection domains */
49
50 struct ib_pd *ib_alloc_pd(struct ib_device *device)
51 {
52         struct ib_pd *pd;
53
54         pd = device->alloc_pd(device, NULL, NULL);
55
56         if (!IS_ERR(pd)) {
57                 pd->device  = device;
58                 pd->uobject = NULL;
59                 atomic_set(&pd->usecnt, 0);
60         }
61
62         return pd;
63 }
64 EXPORT_SYMBOL(ib_alloc_pd);
65
66 int ib_dealloc_pd(struct ib_pd *pd)
67 {
68         if (atomic_read(&pd->usecnt))
69                 return -EBUSY;
70
71         return pd->device->dealloc_pd(pd);
72 }
73 EXPORT_SYMBOL(ib_dealloc_pd);
74
75 /* Address handles */
76
77 struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
78 {
79         struct ib_ah *ah;
80
81         ah = pd->device->create_ah(pd, ah_attr);
82
83         if (!IS_ERR(ah)) {
84                 ah->device  = pd->device;
85                 ah->pd      = pd;
86                 ah->uobject = NULL;
87                 atomic_inc(&pd->usecnt);
88         }
89
90         return ah;
91 }
92 EXPORT_SYMBOL(ib_create_ah);
93
94 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, struct ib_wc *wc,
95                                    struct ib_grh *grh, u8 port_num)
96 {
97         struct ib_ah_attr ah_attr;
98         u32 flow_class;
99         u16 gid_index;
100         int ret;
101
102         memset(&ah_attr, 0, sizeof ah_attr);
103         ah_attr.dlid = wc->slid;
104         ah_attr.sl = wc->sl;
105         ah_attr.src_path_bits = wc->dlid_path_bits;
106         ah_attr.port_num = port_num;
107
108         if (wc->wc_flags & IB_WC_GRH) {
109                 ah_attr.ah_flags = IB_AH_GRH;
110                 ah_attr.grh.dgid = grh->sgid;
111
112                 ret = ib_find_cached_gid(pd->device, &grh->dgid, &port_num,
113                                          &gid_index);
114                 if (ret)
115                         return ERR_PTR(ret);
116
117                 ah_attr.grh.sgid_index = (u8) gid_index;
118                 flow_class = be32_to_cpu(grh->version_tclass_flow);
119                 ah_attr.grh.flow_label = flow_class & 0xFFFFF;
120                 ah_attr.grh.traffic_class = (flow_class >> 20) & 0xFF;
121                 ah_attr.grh.hop_limit = grh->hop_limit;
122         }
123
124         return ib_create_ah(pd, &ah_attr);
125 }
126 EXPORT_SYMBOL(ib_create_ah_from_wc);
127
128 int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
129 {
130         return ah->device->modify_ah ?
131                 ah->device->modify_ah(ah, ah_attr) :
132                 -ENOSYS;
133 }
134 EXPORT_SYMBOL(ib_modify_ah);
135
136 int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
137 {
138         return ah->device->query_ah ?
139                 ah->device->query_ah(ah, ah_attr) :
140                 -ENOSYS;
141 }
142 EXPORT_SYMBOL(ib_query_ah);
143
144 int ib_destroy_ah(struct ib_ah *ah)
145 {
146         struct ib_pd *pd;
147         int ret;
148
149         pd = ah->pd;
150         ret = ah->device->destroy_ah(ah);
151         if (!ret)
152                 atomic_dec(&pd->usecnt);
153
154         return ret;
155 }
156 EXPORT_SYMBOL(ib_destroy_ah);
157
158 /* Shared receive queues */
159
160 struct ib_srq *ib_create_srq(struct ib_pd *pd,
161                              struct ib_srq_init_attr *srq_init_attr)
162 {
163         struct ib_srq *srq;
164
165         if (!pd->device->create_srq)
166                 return ERR_PTR(-ENOSYS);
167
168         srq = pd->device->create_srq(pd, srq_init_attr, NULL);
169
170         if (!IS_ERR(srq)) {
171                 srq->device        = pd->device;
172                 srq->pd            = pd;
173                 srq->uobject       = NULL;
174                 srq->event_handler = srq_init_attr->event_handler;
175                 srq->srq_context   = srq_init_attr->srq_context;
176                 atomic_inc(&pd->usecnt);
177                 atomic_set(&srq->usecnt, 0);
178         }
179
180         return srq;
181 }
182 EXPORT_SYMBOL(ib_create_srq);
183
184 int ib_modify_srq(struct ib_srq *srq,
185                   struct ib_srq_attr *srq_attr,
186                   enum ib_srq_attr_mask srq_attr_mask)
187 {
188         return srq->device->modify_srq(srq, srq_attr, srq_attr_mask);
189 }
190 EXPORT_SYMBOL(ib_modify_srq);
191
192 int ib_query_srq(struct ib_srq *srq,
193                  struct ib_srq_attr *srq_attr)
194 {
195         return srq->device->query_srq ?
196                 srq->device->query_srq(srq, srq_attr) : -ENOSYS;
197 }
198 EXPORT_SYMBOL(ib_query_srq);
199
200 int ib_destroy_srq(struct ib_srq *srq)
201 {
202         struct ib_pd *pd;
203         int ret;
204
205         if (atomic_read(&srq->usecnt))
206                 return -EBUSY;
207
208         pd = srq->pd;
209
210         ret = srq->device->destroy_srq(srq);
211         if (!ret)
212                 atomic_dec(&pd->usecnt);
213
214         return ret;
215 }
216 EXPORT_SYMBOL(ib_destroy_srq);
217
218 /* Queue pairs */
219
220 struct ib_qp *ib_create_qp(struct ib_pd *pd,
221                            struct ib_qp_init_attr *qp_init_attr)
222 {
223         struct ib_qp *qp;
224
225         qp = pd->device->create_qp(pd, qp_init_attr, NULL);
226
227         if (!IS_ERR(qp)) {
228                 qp->device        = pd->device;
229                 qp->pd            = pd;
230                 qp->send_cq       = qp_init_attr->send_cq;
231                 qp->recv_cq       = qp_init_attr->recv_cq;
232                 qp->srq           = qp_init_attr->srq;
233                 qp->uobject       = NULL;
234                 qp->event_handler = qp_init_attr->event_handler;
235                 qp->qp_context    = qp_init_attr->qp_context;
236                 qp->qp_type       = qp_init_attr->qp_type;
237                 atomic_inc(&pd->usecnt);
238                 atomic_inc(&qp_init_attr->send_cq->usecnt);
239                 atomic_inc(&qp_init_attr->recv_cq->usecnt);
240                 if (qp_init_attr->srq)
241                         atomic_inc(&qp_init_attr->srq->usecnt);
242         }
243
244         return qp;
245 }
246 EXPORT_SYMBOL(ib_create_qp);
247
248 static const struct {
249         int                     valid;
250         enum ib_qp_attr_mask    req_param[IB_QPT_RAW_ETY + 1];
251         enum ib_qp_attr_mask    opt_param[IB_QPT_RAW_ETY + 1];
252 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
253         [IB_QPS_RESET] = {
254                 [IB_QPS_RESET] = { .valid = 1 },
255                 [IB_QPS_ERR]   = { .valid = 1 },
256                 [IB_QPS_INIT]  = {
257                         .valid = 1,
258                         .req_param = {
259                                 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX                |
260                                                 IB_QP_PORT                      |
261                                                 IB_QP_QKEY),
262                                 [IB_QPT_UC]  = (IB_QP_PKEY_INDEX                |
263                                                 IB_QP_PORT                      |
264                                                 IB_QP_ACCESS_FLAGS),
265                                 [IB_QPT_RC]  = (IB_QP_PKEY_INDEX                |
266                                                 IB_QP_PORT                      |
267                                                 IB_QP_ACCESS_FLAGS),
268                                 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX                |
269                                                 IB_QP_QKEY),
270                                 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX                |
271                                                 IB_QP_QKEY),
272                         }
273                 },
274         },
275         [IB_QPS_INIT]  = {
276                 [IB_QPS_RESET] = { .valid = 1 },
277                 [IB_QPS_ERR] =   { .valid = 1 },
278                 [IB_QPS_INIT]  = {
279                         .valid = 1,
280                         .opt_param = {
281                                 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX                |
282                                                 IB_QP_PORT                      |
283                                                 IB_QP_QKEY),
284                                 [IB_QPT_UC]  = (IB_QP_PKEY_INDEX                |
285                                                 IB_QP_PORT                      |
286                                                 IB_QP_ACCESS_FLAGS),
287                                 [IB_QPT_RC]  = (IB_QP_PKEY_INDEX                |
288                                                 IB_QP_PORT                      |
289                                                 IB_QP_ACCESS_FLAGS),
290                                 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX                |
291                                                 IB_QP_QKEY),
292                                 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX                |
293                                                 IB_QP_QKEY),
294                         }
295                 },
296                 [IB_QPS_RTR]   = {
297                         .valid = 1,
298                         .req_param = {
299                                 [IB_QPT_UC]  = (IB_QP_AV                        |
300                                                 IB_QP_PATH_MTU                  |
301                                                 IB_QP_DEST_QPN                  |
302                                                 IB_QP_RQ_PSN),
303                                 [IB_QPT_RC]  = (IB_QP_AV                        |
304                                                 IB_QP_PATH_MTU                  |
305                                                 IB_QP_DEST_QPN                  |
306                                                 IB_QP_RQ_PSN                    |
307                                                 IB_QP_MAX_DEST_RD_ATOMIC        |
308                                                 IB_QP_MIN_RNR_TIMER),
309                         },
310                         .opt_param = {
311                                  [IB_QPT_UD]  = (IB_QP_PKEY_INDEX               |
312                                                  IB_QP_QKEY),
313                                  [IB_QPT_UC]  = (IB_QP_ALT_PATH                 |
314                                                  IB_QP_ACCESS_FLAGS             |
315                                                  IB_QP_PKEY_INDEX),
316                                  [IB_QPT_RC]  = (IB_QP_ALT_PATH                 |
317                                                  IB_QP_ACCESS_FLAGS             |
318                                                  IB_QP_PKEY_INDEX),
319                                  [IB_QPT_SMI] = (IB_QP_PKEY_INDEX               |
320                                                  IB_QP_QKEY),
321                                  [IB_QPT_GSI] = (IB_QP_PKEY_INDEX               |
322                                                  IB_QP_QKEY),
323                          }
324                 }
325         },
326         [IB_QPS_RTR]   = {
327                 [IB_QPS_RESET] = { .valid = 1 },
328                 [IB_QPS_ERR] =   { .valid = 1 },
329                 [IB_QPS_RTS]   = {
330                         .valid = 1,
331                         .req_param = {
332                                 [IB_QPT_UD]  = IB_QP_SQ_PSN,
333                                 [IB_QPT_UC]  = IB_QP_SQ_PSN,
334                                 [IB_QPT_RC]  = (IB_QP_TIMEOUT                   |
335                                                 IB_QP_RETRY_CNT                 |
336                                                 IB_QP_RNR_RETRY                 |
337                                                 IB_QP_SQ_PSN                    |
338                                                 IB_QP_MAX_QP_RD_ATOMIC),
339                                 [IB_QPT_SMI] = IB_QP_SQ_PSN,
340                                 [IB_QPT_GSI] = IB_QP_SQ_PSN,
341                         },
342                         .opt_param = {
343                                  [IB_QPT_UD]  = (IB_QP_CUR_STATE                |
344                                                  IB_QP_QKEY),
345                                  [IB_QPT_UC]  = (IB_QP_CUR_STATE                |
346                                                  IB_QP_ALT_PATH                 |
347                                                  IB_QP_ACCESS_FLAGS             |
348                                                  IB_QP_PATH_MIG_STATE),
349                                  [IB_QPT_RC]  = (IB_QP_CUR_STATE                |
350                                                  IB_QP_ALT_PATH                 |
351                                                  IB_QP_ACCESS_FLAGS             |
352                                                  IB_QP_MIN_RNR_TIMER            |
353                                                  IB_QP_PATH_MIG_STATE),
354                                  [IB_QPT_SMI] = (IB_QP_CUR_STATE                |
355                                                  IB_QP_QKEY),
356                                  [IB_QPT_GSI] = (IB_QP_CUR_STATE                |
357                                                  IB_QP_QKEY),
358                          }
359                 }
360         },
361         [IB_QPS_RTS]   = {
362                 [IB_QPS_RESET] = { .valid = 1 },
363                 [IB_QPS_ERR] =   { .valid = 1 },
364                 [IB_QPS_RTS]   = {
365                         .valid = 1,
366                         .opt_param = {
367                                 [IB_QPT_UD]  = (IB_QP_CUR_STATE                 |
368                                                 IB_QP_QKEY),
369                                 [IB_QPT_UC]  = (IB_QP_CUR_STATE                 |
370                                                 IB_QP_ACCESS_FLAGS              |
371                                                 IB_QP_ALT_PATH                  |
372                                                 IB_QP_PATH_MIG_STATE),
373                                 [IB_QPT_RC]  = (IB_QP_CUR_STATE                 |
374                                                 IB_QP_ACCESS_FLAGS              |
375                                                 IB_QP_ALT_PATH                  |
376                                                 IB_QP_PATH_MIG_STATE            |
377                                                 IB_QP_MIN_RNR_TIMER),
378                                 [IB_QPT_SMI] = (IB_QP_CUR_STATE                 |
379                                                 IB_QP_QKEY),
380                                 [IB_QPT_GSI] = (IB_QP_CUR_STATE                 |
381                                                 IB_QP_QKEY),
382                         }
383                 },
384                 [IB_QPS_SQD]   = {
385                         .valid = 1,
386                         .opt_param = {
387                                 [IB_QPT_UD]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
388                                 [IB_QPT_UC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
389                                 [IB_QPT_RC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
390                                 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
391                                 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
392                         }
393                 },
394         },
395         [IB_QPS_SQD]   = {
396                 [IB_QPS_RESET] = { .valid = 1 },
397                 [IB_QPS_ERR] =   { .valid = 1 },
398                 [IB_QPS_RTS]   = {
399                         .valid = 1,
400                         .opt_param = {
401                                 [IB_QPT_UD]  = (IB_QP_CUR_STATE                 |
402                                                 IB_QP_QKEY),
403                                 [IB_QPT_UC]  = (IB_QP_CUR_STATE                 |
404                                                 IB_QP_ALT_PATH                  |
405                                                 IB_QP_ACCESS_FLAGS              |
406                                                 IB_QP_PATH_MIG_STATE),
407                                 [IB_QPT_RC]  = (IB_QP_CUR_STATE                 |
408                                                 IB_QP_ALT_PATH                  |
409                                                 IB_QP_ACCESS_FLAGS              |
410                                                 IB_QP_MIN_RNR_TIMER             |
411                                                 IB_QP_PATH_MIG_STATE),
412                                 [IB_QPT_SMI] = (IB_QP_CUR_STATE                 |
413                                                 IB_QP_QKEY),
414                                 [IB_QPT_GSI] = (IB_QP_CUR_STATE                 |
415                                                 IB_QP_QKEY),
416                         }
417                 },
418                 [IB_QPS_SQD]   = {
419                         .valid = 1,
420                         .opt_param = {
421                                 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX                |
422                                                 IB_QP_QKEY),
423                                 [IB_QPT_UC]  = (IB_QP_AV                        |
424                                                 IB_QP_ALT_PATH                  |
425                                                 IB_QP_ACCESS_FLAGS              |
426                                                 IB_QP_PKEY_INDEX                |
427                                                 IB_QP_PATH_MIG_STATE),
428                                 [IB_QPT_RC]  = (IB_QP_PORT                      |
429                                                 IB_QP_AV                        |
430                                                 IB_QP_TIMEOUT                   |
431                                                 IB_QP_RETRY_CNT                 |
432                                                 IB_QP_RNR_RETRY                 |
433                                                 IB_QP_MAX_QP_RD_ATOMIC          |
434                                                 IB_QP_MAX_DEST_RD_ATOMIC        |
435                                                 IB_QP_ALT_PATH                  |
436                                                 IB_QP_ACCESS_FLAGS              |
437                                                 IB_QP_PKEY_INDEX                |
438                                                 IB_QP_MIN_RNR_TIMER             |
439                                                 IB_QP_PATH_MIG_STATE),
440                                 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX                |
441                                                 IB_QP_QKEY),
442                                 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX                |
443                                                 IB_QP_QKEY),
444                         }
445                 }
446         },
447         [IB_QPS_SQE]   = {
448                 [IB_QPS_RESET] = { .valid = 1 },
449                 [IB_QPS_ERR] =   { .valid = 1 },
450                 [IB_QPS_RTS]   = {
451                         .valid = 1,
452                         .opt_param = {
453                                 [IB_QPT_UD]  = (IB_QP_CUR_STATE                 |
454                                                 IB_QP_QKEY),
455                                 [IB_QPT_UC]  = (IB_QP_CUR_STATE                 |
456                                                 IB_QP_ACCESS_FLAGS),
457                                 [IB_QPT_SMI] = (IB_QP_CUR_STATE                 |
458                                                 IB_QP_QKEY),
459                                 [IB_QPT_GSI] = (IB_QP_CUR_STATE                 |
460                                                 IB_QP_QKEY),
461                         }
462                 }
463         },
464         [IB_QPS_ERR] = {
465                 [IB_QPS_RESET] = { .valid = 1 },
466                 [IB_QPS_ERR] =   { .valid = 1 }
467         }
468 };
469
470 int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
471                        enum ib_qp_type type, enum ib_qp_attr_mask mask)
472 {
473         enum ib_qp_attr_mask req_param, opt_param;
474
475         if (cur_state  < 0 || cur_state  > IB_QPS_ERR ||
476             next_state < 0 || next_state > IB_QPS_ERR)
477                 return 0;
478
479         if (mask & IB_QP_CUR_STATE  &&
480             cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
481             cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
482                 return 0;
483
484         if (!qp_state_table[cur_state][next_state].valid)
485                 return 0;
486
487         req_param = qp_state_table[cur_state][next_state].req_param[type];
488         opt_param = qp_state_table[cur_state][next_state].opt_param[type];
489
490         if ((mask & req_param) != req_param)
491                 return 0;
492
493         if (mask & ~(req_param | opt_param | IB_QP_STATE))
494                 return 0;
495
496         return 1;
497 }
498 EXPORT_SYMBOL(ib_modify_qp_is_ok);
499
500 int ib_modify_qp(struct ib_qp *qp,
501                  struct ib_qp_attr *qp_attr,
502                  int qp_attr_mask)
503 {
504         return qp->device->modify_qp(qp, qp_attr, qp_attr_mask);
505 }
506 EXPORT_SYMBOL(ib_modify_qp);
507
508 int ib_query_qp(struct ib_qp *qp,
509                 struct ib_qp_attr *qp_attr,
510                 int qp_attr_mask,
511                 struct ib_qp_init_attr *qp_init_attr)
512 {
513         return qp->device->query_qp ?
514                 qp->device->query_qp(qp, qp_attr, qp_attr_mask, qp_init_attr) :
515                 -ENOSYS;
516 }
517 EXPORT_SYMBOL(ib_query_qp);
518
519 int ib_destroy_qp(struct ib_qp *qp)
520 {
521         struct ib_pd *pd;
522         struct ib_cq *scq, *rcq;
523         struct ib_srq *srq;
524         int ret;
525
526         pd  = qp->pd;
527         scq = qp->send_cq;
528         rcq = qp->recv_cq;
529         srq = qp->srq;
530
531         ret = qp->device->destroy_qp(qp);
532         if (!ret) {
533                 atomic_dec(&pd->usecnt);
534                 atomic_dec(&scq->usecnt);
535                 atomic_dec(&rcq->usecnt);
536                 if (srq)
537                         atomic_dec(&srq->usecnt);
538         }
539
540         return ret;
541 }
542 EXPORT_SYMBOL(ib_destroy_qp);
543
544 /* Completion queues */
545
546 struct ib_cq *ib_create_cq(struct ib_device *device,
547                            ib_comp_handler comp_handler,
548                            void (*event_handler)(struct ib_event *, void *),
549                            void *cq_context, int cqe)
550 {
551         struct ib_cq *cq;
552
553         cq = device->create_cq(device, cqe, NULL, NULL);
554
555         if (!IS_ERR(cq)) {
556                 cq->device        = device;
557                 cq->uobject       = NULL;
558                 cq->comp_handler  = comp_handler;
559                 cq->event_handler = event_handler;
560                 cq->cq_context    = cq_context;
561                 atomic_set(&cq->usecnt, 0);
562         }
563
564         return cq;
565 }
566 EXPORT_SYMBOL(ib_create_cq);
567
568 int ib_destroy_cq(struct ib_cq *cq)
569 {
570         if (atomic_read(&cq->usecnt))
571                 return -EBUSY;
572
573         return cq->device->destroy_cq(cq);
574 }
575 EXPORT_SYMBOL(ib_destroy_cq);
576
577 int ib_resize_cq(struct ib_cq *cq, int cqe)
578 {
579         return cq->device->resize_cq ?
580                 cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
581 }
582 EXPORT_SYMBOL(ib_resize_cq);
583
584 /* Memory regions */
585
586 struct ib_mr *ib_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
587 {
588         struct ib_mr *mr;
589
590         mr = pd->device->get_dma_mr(pd, mr_access_flags);
591
592         if (!IS_ERR(mr)) {
593                 mr->device  = pd->device;
594                 mr->pd      = pd;
595                 mr->uobject = NULL;
596                 atomic_inc(&pd->usecnt);
597                 atomic_set(&mr->usecnt, 0);
598         }
599
600         return mr;
601 }
602 EXPORT_SYMBOL(ib_get_dma_mr);
603
604 struct ib_mr *ib_reg_phys_mr(struct ib_pd *pd,
605                              struct ib_phys_buf *phys_buf_array,
606                              int num_phys_buf,
607                              int mr_access_flags,
608                              u64 *iova_start)
609 {
610         struct ib_mr *mr;
611
612         mr = pd->device->reg_phys_mr(pd, phys_buf_array, num_phys_buf,
613                                      mr_access_flags, iova_start);
614
615         if (!IS_ERR(mr)) {
616                 mr->device  = pd->device;
617                 mr->pd      = pd;
618                 mr->uobject = NULL;
619                 atomic_inc(&pd->usecnt);
620                 atomic_set(&mr->usecnt, 0);
621         }
622
623         return mr;
624 }
625 EXPORT_SYMBOL(ib_reg_phys_mr);
626
627 int ib_rereg_phys_mr(struct ib_mr *mr,
628                      int mr_rereg_mask,
629                      struct ib_pd *pd,
630                      struct ib_phys_buf *phys_buf_array,
631                      int num_phys_buf,
632                      int mr_access_flags,
633                      u64 *iova_start)
634 {
635         struct ib_pd *old_pd;
636         int ret;
637
638         if (!mr->device->rereg_phys_mr)
639                 return -ENOSYS;
640
641         if (atomic_read(&mr->usecnt))
642                 return -EBUSY;
643
644         old_pd = mr->pd;
645
646         ret = mr->device->rereg_phys_mr(mr, mr_rereg_mask, pd,
647                                         phys_buf_array, num_phys_buf,
648                                         mr_access_flags, iova_start);
649
650         if (!ret && (mr_rereg_mask & IB_MR_REREG_PD)) {
651                 atomic_dec(&old_pd->usecnt);
652                 atomic_inc(&pd->usecnt);
653         }
654
655         return ret;
656 }
657 EXPORT_SYMBOL(ib_rereg_phys_mr);
658
659 int ib_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
660 {
661         return mr->device->query_mr ?
662                 mr->device->query_mr(mr, mr_attr) : -ENOSYS;
663 }
664 EXPORT_SYMBOL(ib_query_mr);
665
666 int ib_dereg_mr(struct ib_mr *mr)
667 {
668         struct ib_pd *pd;
669         int ret;
670
671         if (atomic_read(&mr->usecnt))
672                 return -EBUSY;
673
674         pd = mr->pd;
675         ret = mr->device->dereg_mr(mr);
676         if (!ret)
677                 atomic_dec(&pd->usecnt);
678
679         return ret;
680 }
681 EXPORT_SYMBOL(ib_dereg_mr);
682
683 /* Memory windows */
684
685 struct ib_mw *ib_alloc_mw(struct ib_pd *pd)
686 {
687         struct ib_mw *mw;
688
689         if (!pd->device->alloc_mw)
690                 return ERR_PTR(-ENOSYS);
691
692         mw = pd->device->alloc_mw(pd);
693         if (!IS_ERR(mw)) {
694                 mw->device  = pd->device;
695                 mw->pd      = pd;
696                 mw->uobject = NULL;
697                 atomic_inc(&pd->usecnt);
698         }
699
700         return mw;
701 }
702 EXPORT_SYMBOL(ib_alloc_mw);
703
704 int ib_dealloc_mw(struct ib_mw *mw)
705 {
706         struct ib_pd *pd;
707         int ret;
708
709         pd = mw->pd;
710         ret = mw->device->dealloc_mw(mw);
711         if (!ret)
712                 atomic_dec(&pd->usecnt);
713
714         return ret;
715 }
716 EXPORT_SYMBOL(ib_dealloc_mw);
717
718 /* "Fast" memory regions */
719
720 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
721                             int mr_access_flags,
722                             struct ib_fmr_attr *fmr_attr)
723 {
724         struct ib_fmr *fmr;
725
726         if (!pd->device->alloc_fmr)
727                 return ERR_PTR(-ENOSYS);
728
729         fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
730         if (!IS_ERR(fmr)) {
731                 fmr->device = pd->device;
732                 fmr->pd     = pd;
733                 atomic_inc(&pd->usecnt);
734         }
735
736         return fmr;
737 }
738 EXPORT_SYMBOL(ib_alloc_fmr);
739
740 int ib_unmap_fmr(struct list_head *fmr_list)
741 {
742         struct ib_fmr *fmr;
743
744         if (list_empty(fmr_list))
745                 return 0;
746
747         fmr = list_entry(fmr_list->next, struct ib_fmr, list);
748         return fmr->device->unmap_fmr(fmr_list);
749 }
750 EXPORT_SYMBOL(ib_unmap_fmr);
751
752 int ib_dealloc_fmr(struct ib_fmr *fmr)
753 {
754         struct ib_pd *pd;
755         int ret;
756
757         pd = fmr->pd;
758         ret = fmr->device->dealloc_fmr(fmr);
759         if (!ret)
760                 atomic_dec(&pd->usecnt);
761
762         return ret;
763 }
764 EXPORT_SYMBOL(ib_dealloc_fmr);
765
766 /* Multicast groups */
767
768 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
769 {
770         if (!qp->device->attach_mcast)
771                 return -ENOSYS;
772         if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
773                 return -EINVAL;
774
775         return qp->device->attach_mcast(qp, gid, lid);
776 }
777 EXPORT_SYMBOL(ib_attach_mcast);
778
779 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
780 {
781         if (!qp->device->detach_mcast)
782                 return -ENOSYS;
783         if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
784                 return -EINVAL;
785
786         return qp->device->detach_mcast(qp, gid, lid);
787 }
788 EXPORT_SYMBOL(ib_detach_mcast);