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