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.
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:
16 * Redistribution and use in source and binary forms, with or
17 * without modification, are permitted provided that the following
20 * - Redistributions of source code must retain the above
21 * copyright notice, this list of conditions and the following
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.
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
38 * $Id: verbs.c 1349 2004-12-16 21:09:43Z roland $
41 #include <linux/errno.h>
42 #include <linux/err.h>
43 #include <linux/string.h>
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_cache.h>
48 int ib_rate_to_mult(enum ib_rate 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;
63 EXPORT_SYMBOL(ib_rate_to_mult
);
65 enum ib_rate
mult_to_ib_rate(int 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
;
80 EXPORT_SYMBOL(mult_to_ib_rate
);
82 /* Protection domains */
84 struct ib_pd
*ib_alloc_pd(struct ib_device
*device
)
88 pd
= device
->alloc_pd(device
, NULL
, NULL
);
93 atomic_set(&pd
->usecnt
, 0);
98 EXPORT_SYMBOL(ib_alloc_pd
);
100 int ib_dealloc_pd(struct ib_pd
*pd
)
102 if (atomic_read(&pd
->usecnt
))
105 return pd
->device
->dealloc_pd(pd
);
107 EXPORT_SYMBOL(ib_dealloc_pd
);
109 /* Address handles */
111 struct ib_ah
*ib_create_ah(struct ib_pd
*pd
, struct ib_ah_attr
*ah_attr
)
115 ah
= pd
->device
->create_ah(pd
, ah_attr
);
118 ah
->device
= pd
->device
;
121 atomic_inc(&pd
->usecnt
);
126 EXPORT_SYMBOL(ib_create_ah
);
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
)
131 struct ib_ah_attr ah_attr
;
136 memset(&ah_attr
, 0, sizeof ah_attr
);
137 ah_attr
.dlid
= wc
->slid
;
139 ah_attr
.src_path_bits
= wc
->dlid_path_bits
;
140 ah_attr
.port_num
= port_num
;
142 if (wc
->wc_flags
& IB_WC_GRH
) {
143 ah_attr
.ah_flags
= IB_AH_GRH
;
144 ah_attr
.grh
.dgid
= grh
->sgid
;
146 ret
= ib_find_cached_gid(pd
->device
, &grh
->dgid
, &port_num
,
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
;
158 return ib_create_ah(pd
, &ah_attr
);
160 EXPORT_SYMBOL(ib_create_ah_from_wc
);
162 int ib_modify_ah(struct ib_ah
*ah
, struct ib_ah_attr
*ah_attr
)
164 return ah
->device
->modify_ah
?
165 ah
->device
->modify_ah(ah
, ah_attr
) :
168 EXPORT_SYMBOL(ib_modify_ah
);
170 int ib_query_ah(struct ib_ah
*ah
, struct ib_ah_attr
*ah_attr
)
172 return ah
->device
->query_ah
?
173 ah
->device
->query_ah(ah
, ah_attr
) :
176 EXPORT_SYMBOL(ib_query_ah
);
178 int ib_destroy_ah(struct ib_ah
*ah
)
184 ret
= ah
->device
->destroy_ah(ah
);
186 atomic_dec(&pd
->usecnt
);
190 EXPORT_SYMBOL(ib_destroy_ah
);
192 /* Shared receive queues */
194 struct ib_srq
*ib_create_srq(struct ib_pd
*pd
,
195 struct ib_srq_init_attr
*srq_init_attr
)
199 if (!pd
->device
->create_srq
)
200 return ERR_PTR(-ENOSYS
);
202 srq
= pd
->device
->create_srq(pd
, srq_init_attr
, NULL
);
205 srq
->device
= pd
->device
;
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);
216 EXPORT_SYMBOL(ib_create_srq
);
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
)
222 return srq
->device
->modify_srq(srq
, srq_attr
, srq_attr_mask
);
224 EXPORT_SYMBOL(ib_modify_srq
);
226 int ib_query_srq(struct ib_srq
*srq
,
227 struct ib_srq_attr
*srq_attr
)
229 return srq
->device
->query_srq
?
230 srq
->device
->query_srq(srq
, srq_attr
) : -ENOSYS
;
232 EXPORT_SYMBOL(ib_query_srq
);
234 int ib_destroy_srq(struct ib_srq
*srq
)
239 if (atomic_read(&srq
->usecnt
))
244 ret
= srq
->device
->destroy_srq(srq
);
246 atomic_dec(&pd
->usecnt
);
250 EXPORT_SYMBOL(ib_destroy_srq
);
254 struct ib_qp
*ib_create_qp(struct ib_pd
*pd
,
255 struct ib_qp_init_attr
*qp_init_attr
)
259 qp
= pd
->device
->create_qp(pd
, qp_init_attr
, NULL
);
262 qp
->device
= pd
->device
;
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
;
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
);
280 EXPORT_SYMBOL(ib_create_qp
);
282 static const struct {
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] = {
288 [IB_QPS_RESET
] = { .valid
= 1 },
289 [IB_QPS_ERR
] = { .valid
= 1 },
293 [IB_QPT_UD
] = (IB_QP_PKEY_INDEX
|
296 [IB_QPT_UC
] = (IB_QP_PKEY_INDEX
|
299 [IB_QPT_RC
] = (IB_QP_PKEY_INDEX
|
302 [IB_QPT_SMI
] = (IB_QP_PKEY_INDEX
|
304 [IB_QPT_GSI
] = (IB_QP_PKEY_INDEX
|
310 [IB_QPS_RESET
] = { .valid
= 1 },
311 [IB_QPS_ERR
] = { .valid
= 1 },
315 [IB_QPT_UD
] = (IB_QP_PKEY_INDEX
|
318 [IB_QPT_UC
] = (IB_QP_PKEY_INDEX
|
321 [IB_QPT_RC
] = (IB_QP_PKEY_INDEX
|
324 [IB_QPT_SMI
] = (IB_QP_PKEY_INDEX
|
326 [IB_QPT_GSI
] = (IB_QP_PKEY_INDEX
|
333 [IB_QPT_UC
] = (IB_QP_AV
|
337 [IB_QPT_RC
] = (IB_QP_AV
|
341 IB_QP_MAX_DEST_RD_ATOMIC
|
342 IB_QP_MIN_RNR_TIMER
),
345 [IB_QPT_UD
] = (IB_QP_PKEY_INDEX
|
347 [IB_QPT_UC
] = (IB_QP_ALT_PATH
|
350 [IB_QPT_RC
] = (IB_QP_ALT_PATH
|
353 [IB_QPT_SMI
] = (IB_QP_PKEY_INDEX
|
355 [IB_QPT_GSI
] = (IB_QP_PKEY_INDEX
|
361 [IB_QPS_RESET
] = { .valid
= 1 },
362 [IB_QPS_ERR
] = { .valid
= 1 },
366 [IB_QPT_UD
] = IB_QP_SQ_PSN
,
367 [IB_QPT_UC
] = IB_QP_SQ_PSN
,
368 [IB_QPT_RC
] = (IB_QP_TIMEOUT
|
372 IB_QP_MAX_QP_RD_ATOMIC
),
373 [IB_QPT_SMI
] = IB_QP_SQ_PSN
,
374 [IB_QPT_GSI
] = IB_QP_SQ_PSN
,
377 [IB_QPT_UD
] = (IB_QP_CUR_STATE
|
379 [IB_QPT_UC
] = (IB_QP_CUR_STATE
|
382 IB_QP_PATH_MIG_STATE
),
383 [IB_QPT_RC
] = (IB_QP_CUR_STATE
|
386 IB_QP_MIN_RNR_TIMER
|
387 IB_QP_PATH_MIG_STATE
),
388 [IB_QPT_SMI
] = (IB_QP_CUR_STATE
|
390 [IB_QPT_GSI
] = (IB_QP_CUR_STATE
|
396 [IB_QPS_RESET
] = { .valid
= 1 },
397 [IB_QPS_ERR
] = { .valid
= 1 },
401 [IB_QPT_UD
] = (IB_QP_CUR_STATE
|
403 [IB_QPT_UC
] = (IB_QP_CUR_STATE
|
406 IB_QP_PATH_MIG_STATE
),
407 [IB_QPT_RC
] = (IB_QP_CUR_STATE
|
410 IB_QP_PATH_MIG_STATE
|
411 IB_QP_MIN_RNR_TIMER
),
412 [IB_QPT_SMI
] = (IB_QP_CUR_STATE
|
414 [IB_QPT_GSI
] = (IB_QP_CUR_STATE
|
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
430 [IB_QPS_RESET
] = { .valid
= 1 },
431 [IB_QPS_ERR
] = { .valid
= 1 },
435 [IB_QPT_UD
] = (IB_QP_CUR_STATE
|
437 [IB_QPT_UC
] = (IB_QP_CUR_STATE
|
440 IB_QP_PATH_MIG_STATE
),
441 [IB_QPT_RC
] = (IB_QP_CUR_STATE
|
444 IB_QP_MIN_RNR_TIMER
|
445 IB_QP_PATH_MIG_STATE
),
446 [IB_QPT_SMI
] = (IB_QP_CUR_STATE
|
448 [IB_QPT_GSI
] = (IB_QP_CUR_STATE
|
455 [IB_QPT_UD
] = (IB_QP_PKEY_INDEX
|
457 [IB_QPT_UC
] = (IB_QP_AV
|
461 IB_QP_PATH_MIG_STATE
),
462 [IB_QPT_RC
] = (IB_QP_PORT
|
467 IB_QP_MAX_QP_RD_ATOMIC
|
468 IB_QP_MAX_DEST_RD_ATOMIC
|
472 IB_QP_MIN_RNR_TIMER
|
473 IB_QP_PATH_MIG_STATE
),
474 [IB_QPT_SMI
] = (IB_QP_PKEY_INDEX
|
476 [IB_QPT_GSI
] = (IB_QP_PKEY_INDEX
|
482 [IB_QPS_RESET
] = { .valid
= 1 },
483 [IB_QPS_ERR
] = { .valid
= 1 },
487 [IB_QPT_UD
] = (IB_QP_CUR_STATE
|
489 [IB_QPT_UC
] = (IB_QP_CUR_STATE
|
491 [IB_QPT_SMI
] = (IB_QP_CUR_STATE
|
493 [IB_QPT_GSI
] = (IB_QP_CUR_STATE
|
499 [IB_QPS_RESET
] = { .valid
= 1 },
500 [IB_QPS_ERR
] = { .valid
= 1 }
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
)
507 enum ib_qp_attr_mask req_param
, opt_param
;
509 if (cur_state
< 0 || cur_state
> IB_QPS_ERR
||
510 next_state
< 0 || next_state
> IB_QPS_ERR
)
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
)
518 if (!qp_state_table
[cur_state
][next_state
].valid
)
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
];
524 if ((mask
& req_param
) != req_param
)
527 if (mask
& ~(req_param
| opt_param
| IB_QP_STATE
))
532 EXPORT_SYMBOL(ib_modify_qp_is_ok
);
534 int ib_modify_qp(struct ib_qp
*qp
,
535 struct ib_qp_attr
*qp_attr
,
538 return qp
->device
->modify_qp(qp
, qp_attr
, qp_attr_mask
);
540 EXPORT_SYMBOL(ib_modify_qp
);
542 int ib_query_qp(struct ib_qp
*qp
,
543 struct ib_qp_attr
*qp_attr
,
545 struct ib_qp_init_attr
*qp_init_attr
)
547 return qp
->device
->query_qp
?
548 qp
->device
->query_qp(qp
, qp_attr
, qp_attr_mask
, qp_init_attr
) :
551 EXPORT_SYMBOL(ib_query_qp
);
553 int ib_destroy_qp(struct ib_qp
*qp
)
556 struct ib_cq
*scq
, *rcq
;
565 ret
= qp
->device
->destroy_qp(qp
);
567 atomic_dec(&pd
->usecnt
);
568 atomic_dec(&scq
->usecnt
);
569 atomic_dec(&rcq
->usecnt
);
571 atomic_dec(&srq
->usecnt
);
576 EXPORT_SYMBOL(ib_destroy_qp
);
578 /* Completion queues */
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
)
587 cq
= device
->create_cq(device
, cqe
, NULL
, 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);
600 EXPORT_SYMBOL(ib_create_cq
);
602 int ib_destroy_cq(struct ib_cq
*cq
)
604 if (atomic_read(&cq
->usecnt
))
607 return cq
->device
->destroy_cq(cq
);
609 EXPORT_SYMBOL(ib_destroy_cq
);
611 int ib_resize_cq(struct ib_cq
*cq
, int cqe
)
613 return cq
->device
->resize_cq
?
614 cq
->device
->resize_cq(cq
, cqe
, NULL
) : -ENOSYS
;
616 EXPORT_SYMBOL(ib_resize_cq
);
620 struct ib_mr
*ib_get_dma_mr(struct ib_pd
*pd
, int mr_access_flags
)
624 mr
= pd
->device
->get_dma_mr(pd
, mr_access_flags
);
627 mr
->device
= pd
->device
;
630 atomic_inc(&pd
->usecnt
);
631 atomic_set(&mr
->usecnt
, 0);
636 EXPORT_SYMBOL(ib_get_dma_mr
);
638 struct ib_mr
*ib_reg_phys_mr(struct ib_pd
*pd
,
639 struct ib_phys_buf
*phys_buf_array
,
646 mr
= pd
->device
->reg_phys_mr(pd
, phys_buf_array
, num_phys_buf
,
647 mr_access_flags
, iova_start
);
650 mr
->device
= pd
->device
;
653 atomic_inc(&pd
->usecnt
);
654 atomic_set(&mr
->usecnt
, 0);
659 EXPORT_SYMBOL(ib_reg_phys_mr
);
661 int ib_rereg_phys_mr(struct ib_mr
*mr
,
664 struct ib_phys_buf
*phys_buf_array
,
669 struct ib_pd
*old_pd
;
672 if (!mr
->device
->rereg_phys_mr
)
675 if (atomic_read(&mr
->usecnt
))
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
);
684 if (!ret
&& (mr_rereg_mask
& IB_MR_REREG_PD
)) {
685 atomic_dec(&old_pd
->usecnt
);
686 atomic_inc(&pd
->usecnt
);
691 EXPORT_SYMBOL(ib_rereg_phys_mr
);
693 int ib_query_mr(struct ib_mr
*mr
, struct ib_mr_attr
*mr_attr
)
695 return mr
->device
->query_mr
?
696 mr
->device
->query_mr(mr
, mr_attr
) : -ENOSYS
;
698 EXPORT_SYMBOL(ib_query_mr
);
700 int ib_dereg_mr(struct ib_mr
*mr
)
705 if (atomic_read(&mr
->usecnt
))
709 ret
= mr
->device
->dereg_mr(mr
);
711 atomic_dec(&pd
->usecnt
);
715 EXPORT_SYMBOL(ib_dereg_mr
);
719 struct ib_mw
*ib_alloc_mw(struct ib_pd
*pd
)
723 if (!pd
->device
->alloc_mw
)
724 return ERR_PTR(-ENOSYS
);
726 mw
= pd
->device
->alloc_mw(pd
);
728 mw
->device
= pd
->device
;
731 atomic_inc(&pd
->usecnt
);
736 EXPORT_SYMBOL(ib_alloc_mw
);
738 int ib_dealloc_mw(struct ib_mw
*mw
)
744 ret
= mw
->device
->dealloc_mw(mw
);
746 atomic_dec(&pd
->usecnt
);
750 EXPORT_SYMBOL(ib_dealloc_mw
);
752 /* "Fast" memory regions */
754 struct ib_fmr
*ib_alloc_fmr(struct ib_pd
*pd
,
756 struct ib_fmr_attr
*fmr_attr
)
760 if (!pd
->device
->alloc_fmr
)
761 return ERR_PTR(-ENOSYS
);
763 fmr
= pd
->device
->alloc_fmr(pd
, mr_access_flags
, fmr_attr
);
765 fmr
->device
= pd
->device
;
767 atomic_inc(&pd
->usecnt
);
772 EXPORT_SYMBOL(ib_alloc_fmr
);
774 int ib_unmap_fmr(struct list_head
*fmr_list
)
778 if (list_empty(fmr_list
))
781 fmr
= list_entry(fmr_list
->next
, struct ib_fmr
, list
);
782 return fmr
->device
->unmap_fmr(fmr_list
);
784 EXPORT_SYMBOL(ib_unmap_fmr
);
786 int ib_dealloc_fmr(struct ib_fmr
*fmr
)
792 ret
= fmr
->device
->dealloc_fmr(fmr
);
794 atomic_dec(&pd
->usecnt
);
798 EXPORT_SYMBOL(ib_dealloc_fmr
);
800 /* Multicast groups */
802 int ib_attach_mcast(struct ib_qp
*qp
, union ib_gid
*gid
, u16 lid
)
804 if (!qp
->device
->attach_mcast
)
806 if (gid
->raw
[0] != 0xff || qp
->qp_type
!= IB_QPT_UD
)
809 return qp
->device
->attach_mcast(qp
, gid
, lid
);
811 EXPORT_SYMBOL(ib_attach_mcast
);
813 int ib_detach_mcast(struct ib_qp
*qp
, union ib_gid
*gid
, u16 lid
)
815 if (!qp
->device
->detach_mcast
)
817 if (gid
->raw
[0] != 0xff || qp
->qp_type
!= IB_QPT_UD
)
820 return qp
->device
->detach_mcast(qp
, gid
, lid
);
822 EXPORT_SYMBOL(ib_detach_mcast
);