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
39 #include <linux/errno.h>
40 #include <linux/err.h>
41 #include <linux/export.h>
42 #include <linux/string.h>
43 #include <linux/slab.h>
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_cache.h>
47 #include <rdma/ib_addr.h>
49 #include "core_priv.h"
51 int ib_rate_to_mult(enum ib_rate rate
)
54 case IB_RATE_2_5_GBPS
: return 1;
55 case IB_RATE_5_GBPS
: return 2;
56 case IB_RATE_10_GBPS
: return 4;
57 case IB_RATE_20_GBPS
: return 8;
58 case IB_RATE_30_GBPS
: return 12;
59 case IB_RATE_40_GBPS
: return 16;
60 case IB_RATE_60_GBPS
: return 24;
61 case IB_RATE_80_GBPS
: return 32;
62 case IB_RATE_120_GBPS
: return 48;
66 EXPORT_SYMBOL(ib_rate_to_mult
);
68 enum ib_rate
mult_to_ib_rate(int mult
)
71 case 1: return IB_RATE_2_5_GBPS
;
72 case 2: return IB_RATE_5_GBPS
;
73 case 4: return IB_RATE_10_GBPS
;
74 case 8: return IB_RATE_20_GBPS
;
75 case 12: return IB_RATE_30_GBPS
;
76 case 16: return IB_RATE_40_GBPS
;
77 case 24: return IB_RATE_60_GBPS
;
78 case 32: return IB_RATE_80_GBPS
;
79 case 48: return IB_RATE_120_GBPS
;
80 default: return IB_RATE_PORT_CURRENT
;
83 EXPORT_SYMBOL(mult_to_ib_rate
);
85 int ib_rate_to_mbps(enum ib_rate rate
)
88 case IB_RATE_2_5_GBPS
: return 2500;
89 case IB_RATE_5_GBPS
: return 5000;
90 case IB_RATE_10_GBPS
: return 10000;
91 case IB_RATE_20_GBPS
: return 20000;
92 case IB_RATE_30_GBPS
: return 30000;
93 case IB_RATE_40_GBPS
: return 40000;
94 case IB_RATE_60_GBPS
: return 60000;
95 case IB_RATE_80_GBPS
: return 80000;
96 case IB_RATE_120_GBPS
: return 120000;
97 case IB_RATE_14_GBPS
: return 14062;
98 case IB_RATE_56_GBPS
: return 56250;
99 case IB_RATE_112_GBPS
: return 112500;
100 case IB_RATE_168_GBPS
: return 168750;
101 case IB_RATE_25_GBPS
: return 25781;
102 case IB_RATE_100_GBPS
: return 103125;
103 case IB_RATE_200_GBPS
: return 206250;
104 case IB_RATE_300_GBPS
: return 309375;
108 EXPORT_SYMBOL(ib_rate_to_mbps
);
110 enum rdma_transport_type
111 rdma_node_get_transport(enum rdma_node_type node_type
)
114 case RDMA_NODE_IB_CA
:
115 case RDMA_NODE_IB_SWITCH
:
116 case RDMA_NODE_IB_ROUTER
:
117 return RDMA_TRANSPORT_IB
;
119 return RDMA_TRANSPORT_IWARP
;
120 case RDMA_NODE_USNIC
:
121 return RDMA_TRANSPORT_USNIC
;
122 case RDMA_NODE_USNIC_UDP
:
123 return RDMA_TRANSPORT_USNIC_UDP
;
129 EXPORT_SYMBOL(rdma_node_get_transport
);
131 enum rdma_link_layer
rdma_port_get_link_layer(struct ib_device
*device
, u8 port_num
)
133 if (device
->get_link_layer
)
134 return device
->get_link_layer(device
, port_num
);
136 switch (rdma_node_get_transport(device
->node_type
)) {
137 case RDMA_TRANSPORT_IB
:
138 return IB_LINK_LAYER_INFINIBAND
;
139 case RDMA_TRANSPORT_IWARP
:
140 case RDMA_TRANSPORT_USNIC
:
141 case RDMA_TRANSPORT_USNIC_UDP
:
142 return IB_LINK_LAYER_ETHERNET
;
144 return IB_LINK_LAYER_UNSPECIFIED
;
147 EXPORT_SYMBOL(rdma_port_get_link_layer
);
149 /* Protection domains */
151 struct ib_pd
*ib_alloc_pd(struct ib_device
*device
)
155 pd
= device
->alloc_pd(device
, NULL
, NULL
);
160 atomic_set(&pd
->usecnt
, 0);
165 EXPORT_SYMBOL(ib_alloc_pd
);
167 int ib_dealloc_pd(struct ib_pd
*pd
)
169 if (atomic_read(&pd
->usecnt
))
172 return pd
->device
->dealloc_pd(pd
);
174 EXPORT_SYMBOL(ib_dealloc_pd
);
176 /* Address handles */
178 struct ib_ah
*ib_create_ah(struct ib_pd
*pd
, struct ib_ah_attr
*ah_attr
)
182 ah
= pd
->device
->create_ah(pd
, ah_attr
);
185 ah
->device
= pd
->device
;
188 atomic_inc(&pd
->usecnt
);
193 EXPORT_SYMBOL(ib_create_ah
);
195 int ib_init_ah_from_wc(struct ib_device
*device
, u8 port_num
, struct ib_wc
*wc
,
196 struct ib_grh
*grh
, struct ib_ah_attr
*ah_attr
)
201 int is_eth
= (rdma_port_get_link_layer(device
, port_num
) ==
202 IB_LINK_LAYER_ETHERNET
);
204 memset(ah_attr
, 0, sizeof *ah_attr
);
206 if (!(wc
->wc_flags
& IB_WC_GRH
))
209 if (wc
->wc_flags
& IB_WC_WITH_SMAC
&&
210 wc
->wc_flags
& IB_WC_WITH_VLAN
) {
211 memcpy(ah_attr
->dmac
, wc
->smac
, ETH_ALEN
);
212 ah_attr
->vlan_id
= wc
->vlan_id
;
214 ret
= rdma_addr_find_dmac_by_grh(&grh
->dgid
, &grh
->sgid
,
215 ah_attr
->dmac
, &ah_attr
->vlan_id
);
220 ah_attr
->vlan_id
= 0xffff;
223 ah_attr
->dlid
= wc
->slid
;
224 ah_attr
->sl
= wc
->sl
;
225 ah_attr
->src_path_bits
= wc
->dlid_path_bits
;
226 ah_attr
->port_num
= port_num
;
228 if (wc
->wc_flags
& IB_WC_GRH
) {
229 ah_attr
->ah_flags
= IB_AH_GRH
;
230 ah_attr
->grh
.dgid
= grh
->sgid
;
232 ret
= ib_find_cached_gid(device
, &grh
->dgid
, &port_num
,
237 ah_attr
->grh
.sgid_index
= (u8
) gid_index
;
238 flow_class
= be32_to_cpu(grh
->version_tclass_flow
);
239 ah_attr
->grh
.flow_label
= flow_class
& 0xFFFFF;
240 ah_attr
->grh
.hop_limit
= 0xFF;
241 ah_attr
->grh
.traffic_class
= (flow_class
>> 20) & 0xFF;
245 EXPORT_SYMBOL(ib_init_ah_from_wc
);
247 struct ib_ah
*ib_create_ah_from_wc(struct ib_pd
*pd
, struct ib_wc
*wc
,
248 struct ib_grh
*grh
, u8 port_num
)
250 struct ib_ah_attr ah_attr
;
253 ret
= ib_init_ah_from_wc(pd
->device
, port_num
, wc
, grh
, &ah_attr
);
257 return ib_create_ah(pd
, &ah_attr
);
259 EXPORT_SYMBOL(ib_create_ah_from_wc
);
261 int ib_modify_ah(struct ib_ah
*ah
, struct ib_ah_attr
*ah_attr
)
263 return ah
->device
->modify_ah
?
264 ah
->device
->modify_ah(ah
, ah_attr
) :
267 EXPORT_SYMBOL(ib_modify_ah
);
269 int ib_query_ah(struct ib_ah
*ah
, struct ib_ah_attr
*ah_attr
)
271 return ah
->device
->query_ah
?
272 ah
->device
->query_ah(ah
, ah_attr
) :
275 EXPORT_SYMBOL(ib_query_ah
);
277 int ib_destroy_ah(struct ib_ah
*ah
)
283 ret
= ah
->device
->destroy_ah(ah
);
285 atomic_dec(&pd
->usecnt
);
289 EXPORT_SYMBOL(ib_destroy_ah
);
291 /* Shared receive queues */
293 struct ib_srq
*ib_create_srq(struct ib_pd
*pd
,
294 struct ib_srq_init_attr
*srq_init_attr
)
298 if (!pd
->device
->create_srq
)
299 return ERR_PTR(-ENOSYS
);
301 srq
= pd
->device
->create_srq(pd
, srq_init_attr
, NULL
);
304 srq
->device
= pd
->device
;
307 srq
->event_handler
= srq_init_attr
->event_handler
;
308 srq
->srq_context
= srq_init_attr
->srq_context
;
309 srq
->srq_type
= srq_init_attr
->srq_type
;
310 if (srq
->srq_type
== IB_SRQT_XRC
) {
311 srq
->ext
.xrc
.xrcd
= srq_init_attr
->ext
.xrc
.xrcd
;
312 srq
->ext
.xrc
.cq
= srq_init_attr
->ext
.xrc
.cq
;
313 atomic_inc(&srq
->ext
.xrc
.xrcd
->usecnt
);
314 atomic_inc(&srq
->ext
.xrc
.cq
->usecnt
);
316 atomic_inc(&pd
->usecnt
);
317 atomic_set(&srq
->usecnt
, 0);
322 EXPORT_SYMBOL(ib_create_srq
);
324 int ib_modify_srq(struct ib_srq
*srq
,
325 struct ib_srq_attr
*srq_attr
,
326 enum ib_srq_attr_mask srq_attr_mask
)
328 return srq
->device
->modify_srq
?
329 srq
->device
->modify_srq(srq
, srq_attr
, srq_attr_mask
, NULL
) :
332 EXPORT_SYMBOL(ib_modify_srq
);
334 int ib_query_srq(struct ib_srq
*srq
,
335 struct ib_srq_attr
*srq_attr
)
337 return srq
->device
->query_srq
?
338 srq
->device
->query_srq(srq
, srq_attr
) : -ENOSYS
;
340 EXPORT_SYMBOL(ib_query_srq
);
342 int ib_destroy_srq(struct ib_srq
*srq
)
345 enum ib_srq_type srq_type
;
346 struct ib_xrcd
*uninitialized_var(xrcd
);
347 struct ib_cq
*uninitialized_var(cq
);
350 if (atomic_read(&srq
->usecnt
))
354 srq_type
= srq
->srq_type
;
355 if (srq_type
== IB_SRQT_XRC
) {
356 xrcd
= srq
->ext
.xrc
.xrcd
;
357 cq
= srq
->ext
.xrc
.cq
;
360 ret
= srq
->device
->destroy_srq(srq
);
362 atomic_dec(&pd
->usecnt
);
363 if (srq_type
== IB_SRQT_XRC
) {
364 atomic_dec(&xrcd
->usecnt
);
365 atomic_dec(&cq
->usecnt
);
371 EXPORT_SYMBOL(ib_destroy_srq
);
375 static void __ib_shared_qp_event_handler(struct ib_event
*event
, void *context
)
377 struct ib_qp
*qp
= context
;
380 spin_lock_irqsave(&qp
->device
->event_handler_lock
, flags
);
381 list_for_each_entry(event
->element
.qp
, &qp
->open_list
, open_list
)
382 if (event
->element
.qp
->event_handler
)
383 event
->element
.qp
->event_handler(event
, event
->element
.qp
->qp_context
);
384 spin_unlock_irqrestore(&qp
->device
->event_handler_lock
, flags
);
387 static void __ib_insert_xrcd_qp(struct ib_xrcd
*xrcd
, struct ib_qp
*qp
)
389 mutex_lock(&xrcd
->tgt_qp_mutex
);
390 list_add(&qp
->xrcd_list
, &xrcd
->tgt_qp_list
);
391 mutex_unlock(&xrcd
->tgt_qp_mutex
);
394 static struct ib_qp
*__ib_open_qp(struct ib_qp
*real_qp
,
395 void (*event_handler
)(struct ib_event
*, void *),
401 qp
= kzalloc(sizeof *qp
, GFP_KERNEL
);
403 return ERR_PTR(-ENOMEM
);
405 qp
->real_qp
= real_qp
;
406 atomic_inc(&real_qp
->usecnt
);
407 qp
->device
= real_qp
->device
;
408 qp
->event_handler
= event_handler
;
409 qp
->qp_context
= qp_context
;
410 qp
->qp_num
= real_qp
->qp_num
;
411 qp
->qp_type
= real_qp
->qp_type
;
413 spin_lock_irqsave(&real_qp
->device
->event_handler_lock
, flags
);
414 list_add(&qp
->open_list
, &real_qp
->open_list
);
415 spin_unlock_irqrestore(&real_qp
->device
->event_handler_lock
, flags
);
420 struct ib_qp
*ib_open_qp(struct ib_xrcd
*xrcd
,
421 struct ib_qp_open_attr
*qp_open_attr
)
423 struct ib_qp
*qp
, *real_qp
;
425 if (qp_open_attr
->qp_type
!= IB_QPT_XRC_TGT
)
426 return ERR_PTR(-EINVAL
);
428 qp
= ERR_PTR(-EINVAL
);
429 mutex_lock(&xrcd
->tgt_qp_mutex
);
430 list_for_each_entry(real_qp
, &xrcd
->tgt_qp_list
, xrcd_list
) {
431 if (real_qp
->qp_num
== qp_open_attr
->qp_num
) {
432 qp
= __ib_open_qp(real_qp
, qp_open_attr
->event_handler
,
433 qp_open_attr
->qp_context
);
437 mutex_unlock(&xrcd
->tgt_qp_mutex
);
440 EXPORT_SYMBOL(ib_open_qp
);
442 struct ib_qp
*ib_create_qp(struct ib_pd
*pd
,
443 struct ib_qp_init_attr
*qp_init_attr
)
445 struct ib_qp
*qp
, *real_qp
;
446 struct ib_device
*device
;
448 device
= pd
? pd
->device
: qp_init_attr
->xrcd
->device
;
449 qp
= device
->create_qp(pd
, qp_init_attr
, NULL
);
455 qp
->qp_type
= qp_init_attr
->qp_type
;
457 atomic_set(&qp
->usecnt
, 0);
458 if (qp_init_attr
->qp_type
== IB_QPT_XRC_TGT
) {
459 qp
->event_handler
= __ib_shared_qp_event_handler
;
462 qp
->send_cq
= qp
->recv_cq
= NULL
;
464 qp
->xrcd
= qp_init_attr
->xrcd
;
465 atomic_inc(&qp_init_attr
->xrcd
->usecnt
);
466 INIT_LIST_HEAD(&qp
->open_list
);
469 qp
= __ib_open_qp(real_qp
, qp_init_attr
->event_handler
,
470 qp_init_attr
->qp_context
);
472 __ib_insert_xrcd_qp(qp_init_attr
->xrcd
, real_qp
);
474 real_qp
->device
->destroy_qp(real_qp
);
476 qp
->event_handler
= qp_init_attr
->event_handler
;
477 qp
->qp_context
= qp_init_attr
->qp_context
;
478 if (qp_init_attr
->qp_type
== IB_QPT_XRC_INI
) {
482 qp
->recv_cq
= qp_init_attr
->recv_cq
;
483 atomic_inc(&qp_init_attr
->recv_cq
->usecnt
);
484 qp
->srq
= qp_init_attr
->srq
;
486 atomic_inc(&qp_init_attr
->srq
->usecnt
);
490 qp
->send_cq
= qp_init_attr
->send_cq
;
493 atomic_inc(&pd
->usecnt
);
494 atomic_inc(&qp_init_attr
->send_cq
->usecnt
);
500 EXPORT_SYMBOL(ib_create_qp
);
502 static const struct {
504 enum ib_qp_attr_mask req_param
[IB_QPT_MAX
];
505 enum ib_qp_attr_mask req_param_add_eth
[IB_QPT_MAX
];
506 enum ib_qp_attr_mask opt_param
[IB_QPT_MAX
];
507 enum ib_qp_attr_mask opt_param_add_eth
[IB_QPT_MAX
];
508 } qp_state_table
[IB_QPS_ERR
+ 1][IB_QPS_ERR
+ 1] = {
510 [IB_QPS_RESET
] = { .valid
= 1 },
514 [IB_QPT_UD
] = (IB_QP_PKEY_INDEX
|
517 [IB_QPT_RAW_PACKET
] = IB_QP_PORT
,
518 [IB_QPT_UC
] = (IB_QP_PKEY_INDEX
|
521 [IB_QPT_RC
] = (IB_QP_PKEY_INDEX
|
524 [IB_QPT_XRC_INI
] = (IB_QP_PKEY_INDEX
|
527 [IB_QPT_XRC_TGT
] = (IB_QP_PKEY_INDEX
|
530 [IB_QPT_SMI
] = (IB_QP_PKEY_INDEX
|
532 [IB_QPT_GSI
] = (IB_QP_PKEY_INDEX
|
538 [IB_QPS_RESET
] = { .valid
= 1 },
539 [IB_QPS_ERR
] = { .valid
= 1 },
543 [IB_QPT_UD
] = (IB_QP_PKEY_INDEX
|
546 [IB_QPT_UC
] = (IB_QP_PKEY_INDEX
|
549 [IB_QPT_RC
] = (IB_QP_PKEY_INDEX
|
552 [IB_QPT_XRC_INI
] = (IB_QP_PKEY_INDEX
|
555 [IB_QPT_XRC_TGT
] = (IB_QP_PKEY_INDEX
|
558 [IB_QPT_SMI
] = (IB_QP_PKEY_INDEX
|
560 [IB_QPT_GSI
] = (IB_QP_PKEY_INDEX
|
567 [IB_QPT_UC
] = (IB_QP_AV
|
571 [IB_QPT_RC
] = (IB_QP_AV
|
575 IB_QP_MAX_DEST_RD_ATOMIC
|
576 IB_QP_MIN_RNR_TIMER
),
577 [IB_QPT_XRC_INI
] = (IB_QP_AV
|
581 [IB_QPT_XRC_TGT
] = (IB_QP_AV
|
585 IB_QP_MAX_DEST_RD_ATOMIC
|
586 IB_QP_MIN_RNR_TIMER
),
588 .req_param_add_eth
= {
589 [IB_QPT_RC
] = (IB_QP_SMAC
),
590 [IB_QPT_UC
] = (IB_QP_SMAC
),
591 [IB_QPT_XRC_INI
] = (IB_QP_SMAC
),
592 [IB_QPT_XRC_TGT
] = (IB_QP_SMAC
)
595 [IB_QPT_UD
] = (IB_QP_PKEY_INDEX
|
597 [IB_QPT_UC
] = (IB_QP_ALT_PATH
|
600 [IB_QPT_RC
] = (IB_QP_ALT_PATH
|
603 [IB_QPT_XRC_INI
] = (IB_QP_ALT_PATH
|
606 [IB_QPT_XRC_TGT
] = (IB_QP_ALT_PATH
|
609 [IB_QPT_SMI
] = (IB_QP_PKEY_INDEX
|
611 [IB_QPT_GSI
] = (IB_QP_PKEY_INDEX
|
614 .opt_param_add_eth
= {
615 [IB_QPT_RC
] = (IB_QP_ALT_SMAC
|
618 [IB_QPT_UC
] = (IB_QP_ALT_SMAC
|
621 [IB_QPT_XRC_INI
] = (IB_QP_ALT_SMAC
|
624 [IB_QPT_XRC_TGT
] = (IB_QP_ALT_SMAC
|
631 [IB_QPS_RESET
] = { .valid
= 1 },
632 [IB_QPS_ERR
] = { .valid
= 1 },
636 [IB_QPT_UD
] = IB_QP_SQ_PSN
,
637 [IB_QPT_UC
] = IB_QP_SQ_PSN
,
638 [IB_QPT_RC
] = (IB_QP_TIMEOUT
|
642 IB_QP_MAX_QP_RD_ATOMIC
),
643 [IB_QPT_XRC_INI
] = (IB_QP_TIMEOUT
|
647 IB_QP_MAX_QP_RD_ATOMIC
),
648 [IB_QPT_XRC_TGT
] = (IB_QP_TIMEOUT
|
650 [IB_QPT_SMI
] = IB_QP_SQ_PSN
,
651 [IB_QPT_GSI
] = IB_QP_SQ_PSN
,
654 [IB_QPT_UD
] = (IB_QP_CUR_STATE
|
656 [IB_QPT_UC
] = (IB_QP_CUR_STATE
|
659 IB_QP_PATH_MIG_STATE
),
660 [IB_QPT_RC
] = (IB_QP_CUR_STATE
|
663 IB_QP_MIN_RNR_TIMER
|
664 IB_QP_PATH_MIG_STATE
),
665 [IB_QPT_XRC_INI
] = (IB_QP_CUR_STATE
|
668 IB_QP_PATH_MIG_STATE
),
669 [IB_QPT_XRC_TGT
] = (IB_QP_CUR_STATE
|
672 IB_QP_MIN_RNR_TIMER
|
673 IB_QP_PATH_MIG_STATE
),
674 [IB_QPT_SMI
] = (IB_QP_CUR_STATE
|
676 [IB_QPT_GSI
] = (IB_QP_CUR_STATE
|
682 [IB_QPS_RESET
] = { .valid
= 1 },
683 [IB_QPS_ERR
] = { .valid
= 1 },
687 [IB_QPT_UD
] = (IB_QP_CUR_STATE
|
689 [IB_QPT_UC
] = (IB_QP_CUR_STATE
|
692 IB_QP_PATH_MIG_STATE
),
693 [IB_QPT_RC
] = (IB_QP_CUR_STATE
|
696 IB_QP_PATH_MIG_STATE
|
697 IB_QP_MIN_RNR_TIMER
),
698 [IB_QPT_XRC_INI
] = (IB_QP_CUR_STATE
|
701 IB_QP_PATH_MIG_STATE
),
702 [IB_QPT_XRC_TGT
] = (IB_QP_CUR_STATE
|
705 IB_QP_PATH_MIG_STATE
|
706 IB_QP_MIN_RNR_TIMER
),
707 [IB_QPT_SMI
] = (IB_QP_CUR_STATE
|
709 [IB_QPT_GSI
] = (IB_QP_CUR_STATE
|
716 [IB_QPT_UD
] = IB_QP_EN_SQD_ASYNC_NOTIFY
,
717 [IB_QPT_UC
] = IB_QP_EN_SQD_ASYNC_NOTIFY
,
718 [IB_QPT_RC
] = IB_QP_EN_SQD_ASYNC_NOTIFY
,
719 [IB_QPT_XRC_INI
] = IB_QP_EN_SQD_ASYNC_NOTIFY
,
720 [IB_QPT_XRC_TGT
] = IB_QP_EN_SQD_ASYNC_NOTIFY
, /* ??? */
721 [IB_QPT_SMI
] = IB_QP_EN_SQD_ASYNC_NOTIFY
,
722 [IB_QPT_GSI
] = IB_QP_EN_SQD_ASYNC_NOTIFY
727 [IB_QPS_RESET
] = { .valid
= 1 },
728 [IB_QPS_ERR
] = { .valid
= 1 },
732 [IB_QPT_UD
] = (IB_QP_CUR_STATE
|
734 [IB_QPT_UC
] = (IB_QP_CUR_STATE
|
737 IB_QP_PATH_MIG_STATE
),
738 [IB_QPT_RC
] = (IB_QP_CUR_STATE
|
741 IB_QP_MIN_RNR_TIMER
|
742 IB_QP_PATH_MIG_STATE
),
743 [IB_QPT_XRC_INI
] = (IB_QP_CUR_STATE
|
746 IB_QP_PATH_MIG_STATE
),
747 [IB_QPT_XRC_TGT
] = (IB_QP_CUR_STATE
|
750 IB_QP_MIN_RNR_TIMER
|
751 IB_QP_PATH_MIG_STATE
),
752 [IB_QPT_SMI
] = (IB_QP_CUR_STATE
|
754 [IB_QPT_GSI
] = (IB_QP_CUR_STATE
|
761 [IB_QPT_UD
] = (IB_QP_PKEY_INDEX
|
763 [IB_QPT_UC
] = (IB_QP_AV
|
767 IB_QP_PATH_MIG_STATE
),
768 [IB_QPT_RC
] = (IB_QP_PORT
|
773 IB_QP_MAX_QP_RD_ATOMIC
|
774 IB_QP_MAX_DEST_RD_ATOMIC
|
778 IB_QP_MIN_RNR_TIMER
|
779 IB_QP_PATH_MIG_STATE
),
780 [IB_QPT_XRC_INI
] = (IB_QP_PORT
|
785 IB_QP_MAX_QP_RD_ATOMIC
|
789 IB_QP_PATH_MIG_STATE
),
790 [IB_QPT_XRC_TGT
] = (IB_QP_PORT
|
793 IB_QP_MAX_DEST_RD_ATOMIC
|
797 IB_QP_MIN_RNR_TIMER
|
798 IB_QP_PATH_MIG_STATE
),
799 [IB_QPT_SMI
] = (IB_QP_PKEY_INDEX
|
801 [IB_QPT_GSI
] = (IB_QP_PKEY_INDEX
|
807 [IB_QPS_RESET
] = { .valid
= 1 },
808 [IB_QPS_ERR
] = { .valid
= 1 },
812 [IB_QPT_UD
] = (IB_QP_CUR_STATE
|
814 [IB_QPT_UC
] = (IB_QP_CUR_STATE
|
816 [IB_QPT_SMI
] = (IB_QP_CUR_STATE
|
818 [IB_QPT_GSI
] = (IB_QP_CUR_STATE
|
824 [IB_QPS_RESET
] = { .valid
= 1 },
825 [IB_QPS_ERR
] = { .valid
= 1 }
829 int ib_modify_qp_is_ok(enum ib_qp_state cur_state
, enum ib_qp_state next_state
,
830 enum ib_qp_type type
, enum ib_qp_attr_mask mask
,
831 enum rdma_link_layer ll
)
833 enum ib_qp_attr_mask req_param
, opt_param
;
835 if (cur_state
< 0 || cur_state
> IB_QPS_ERR
||
836 next_state
< 0 || next_state
> IB_QPS_ERR
)
839 if (mask
& IB_QP_CUR_STATE
&&
840 cur_state
!= IB_QPS_RTR
&& cur_state
!= IB_QPS_RTS
&&
841 cur_state
!= IB_QPS_SQD
&& cur_state
!= IB_QPS_SQE
)
844 if (!qp_state_table
[cur_state
][next_state
].valid
)
847 req_param
= qp_state_table
[cur_state
][next_state
].req_param
[type
];
848 opt_param
= qp_state_table
[cur_state
][next_state
].opt_param
[type
];
850 if (ll
== IB_LINK_LAYER_ETHERNET
) {
851 req_param
|= qp_state_table
[cur_state
][next_state
].
852 req_param_add_eth
[type
];
853 opt_param
|= qp_state_table
[cur_state
][next_state
].
854 opt_param_add_eth
[type
];
857 if ((mask
& req_param
) != req_param
)
860 if (mask
& ~(req_param
| opt_param
| IB_QP_STATE
))
865 EXPORT_SYMBOL(ib_modify_qp_is_ok
);
867 int ib_resolve_eth_l2_attrs(struct ib_qp
*qp
,
868 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
873 if ((*qp_attr_mask
& IB_QP_AV
) &&
874 (rdma_port_get_link_layer(qp
->device
, qp_attr
->ah_attr
.port_num
) == IB_LINK_LAYER_ETHERNET
)) {
875 ret
= ib_query_gid(qp
->device
, qp_attr
->ah_attr
.port_num
,
876 qp_attr
->ah_attr
.grh
.sgid_index
, &sgid
);
879 if (rdma_link_local_addr((struct in6_addr
*)qp_attr
->ah_attr
.grh
.dgid
.raw
)) {
880 rdma_get_ll_mac((struct in6_addr
*)qp_attr
->ah_attr
.grh
.dgid
.raw
, qp_attr
->ah_attr
.dmac
);
881 rdma_get_ll_mac((struct in6_addr
*)sgid
.raw
, qp_attr
->smac
);
882 qp_attr
->vlan_id
= rdma_get_vlan_id(&sgid
);
884 ret
= rdma_addr_find_dmac_by_grh(&sgid
, &qp_attr
->ah_attr
.grh
.dgid
,
885 qp_attr
->ah_attr
.dmac
, &qp_attr
->vlan_id
);
888 ret
= rdma_addr_find_smac_by_sgid(&sgid
, qp_attr
->smac
, NULL
);
892 *qp_attr_mask
|= IB_QP_SMAC
;
893 if (qp_attr
->vlan_id
< 0xFFFF)
894 *qp_attr_mask
|= IB_QP_VID
;
899 EXPORT_SYMBOL(ib_resolve_eth_l2_attrs
);
902 int ib_modify_qp(struct ib_qp
*qp
,
903 struct ib_qp_attr
*qp_attr
,
908 ret
= ib_resolve_eth_l2_attrs(qp
, qp_attr
, &qp_attr_mask
);
912 return qp
->device
->modify_qp(qp
->real_qp
, qp_attr
, qp_attr_mask
, NULL
);
914 EXPORT_SYMBOL(ib_modify_qp
);
916 int ib_query_qp(struct ib_qp
*qp
,
917 struct ib_qp_attr
*qp_attr
,
919 struct ib_qp_init_attr
*qp_init_attr
)
921 return qp
->device
->query_qp
?
922 qp
->device
->query_qp(qp
->real_qp
, qp_attr
, qp_attr_mask
, qp_init_attr
) :
925 EXPORT_SYMBOL(ib_query_qp
);
927 int ib_close_qp(struct ib_qp
*qp
)
929 struct ib_qp
*real_qp
;
932 real_qp
= qp
->real_qp
;
936 spin_lock_irqsave(&real_qp
->device
->event_handler_lock
, flags
);
937 list_del(&qp
->open_list
);
938 spin_unlock_irqrestore(&real_qp
->device
->event_handler_lock
, flags
);
940 atomic_dec(&real_qp
->usecnt
);
945 EXPORT_SYMBOL(ib_close_qp
);
947 static int __ib_destroy_shared_qp(struct ib_qp
*qp
)
949 struct ib_xrcd
*xrcd
;
950 struct ib_qp
*real_qp
;
953 real_qp
= qp
->real_qp
;
954 xrcd
= real_qp
->xrcd
;
956 mutex_lock(&xrcd
->tgt_qp_mutex
);
958 if (atomic_read(&real_qp
->usecnt
) == 0)
959 list_del(&real_qp
->xrcd_list
);
962 mutex_unlock(&xrcd
->tgt_qp_mutex
);
965 ret
= ib_destroy_qp(real_qp
);
967 atomic_dec(&xrcd
->usecnt
);
969 __ib_insert_xrcd_qp(xrcd
, real_qp
);
975 int ib_destroy_qp(struct ib_qp
*qp
)
978 struct ib_cq
*scq
, *rcq
;
982 if (atomic_read(&qp
->usecnt
))
985 if (qp
->real_qp
!= qp
)
986 return __ib_destroy_shared_qp(qp
);
993 ret
= qp
->device
->destroy_qp(qp
);
996 atomic_dec(&pd
->usecnt
);
998 atomic_dec(&scq
->usecnt
);
1000 atomic_dec(&rcq
->usecnt
);
1002 atomic_dec(&srq
->usecnt
);
1007 EXPORT_SYMBOL(ib_destroy_qp
);
1009 /* Completion queues */
1011 struct ib_cq
*ib_create_cq(struct ib_device
*device
,
1012 ib_comp_handler comp_handler
,
1013 void (*event_handler
)(struct ib_event
*, void *),
1014 void *cq_context
, int cqe
, int comp_vector
)
1018 cq
= device
->create_cq(device
, cqe
, comp_vector
, NULL
, NULL
);
1021 cq
->device
= device
;
1023 cq
->comp_handler
= comp_handler
;
1024 cq
->event_handler
= event_handler
;
1025 cq
->cq_context
= cq_context
;
1026 atomic_set(&cq
->usecnt
, 0);
1031 EXPORT_SYMBOL(ib_create_cq
);
1033 int ib_modify_cq(struct ib_cq
*cq
, u16 cq_count
, u16 cq_period
)
1035 return cq
->device
->modify_cq
?
1036 cq
->device
->modify_cq(cq
, cq_count
, cq_period
) : -ENOSYS
;
1038 EXPORT_SYMBOL(ib_modify_cq
);
1040 int ib_destroy_cq(struct ib_cq
*cq
)
1042 if (atomic_read(&cq
->usecnt
))
1045 return cq
->device
->destroy_cq(cq
);
1047 EXPORT_SYMBOL(ib_destroy_cq
);
1049 int ib_resize_cq(struct ib_cq
*cq
, int cqe
)
1051 return cq
->device
->resize_cq
?
1052 cq
->device
->resize_cq(cq
, cqe
, NULL
) : -ENOSYS
;
1054 EXPORT_SYMBOL(ib_resize_cq
);
1056 /* Memory regions */
1058 struct ib_mr
*ib_get_dma_mr(struct ib_pd
*pd
, int mr_access_flags
)
1063 err
= ib_check_mr_access(mr_access_flags
);
1065 return ERR_PTR(err
);
1067 mr
= pd
->device
->get_dma_mr(pd
, mr_access_flags
);
1070 mr
->device
= pd
->device
;
1073 atomic_inc(&pd
->usecnt
);
1074 atomic_set(&mr
->usecnt
, 0);
1079 EXPORT_SYMBOL(ib_get_dma_mr
);
1081 struct ib_mr
*ib_reg_phys_mr(struct ib_pd
*pd
,
1082 struct ib_phys_buf
*phys_buf_array
,
1084 int mr_access_flags
,
1090 err
= ib_check_mr_access(mr_access_flags
);
1092 return ERR_PTR(err
);
1094 if (!pd
->device
->reg_phys_mr
)
1095 return ERR_PTR(-ENOSYS
);
1097 mr
= pd
->device
->reg_phys_mr(pd
, phys_buf_array
, num_phys_buf
,
1098 mr_access_flags
, iova_start
);
1101 mr
->device
= pd
->device
;
1104 atomic_inc(&pd
->usecnt
);
1105 atomic_set(&mr
->usecnt
, 0);
1110 EXPORT_SYMBOL(ib_reg_phys_mr
);
1112 int ib_rereg_phys_mr(struct ib_mr
*mr
,
1115 struct ib_phys_buf
*phys_buf_array
,
1117 int mr_access_flags
,
1120 struct ib_pd
*old_pd
;
1123 ret
= ib_check_mr_access(mr_access_flags
);
1127 if (!mr
->device
->rereg_phys_mr
)
1130 if (atomic_read(&mr
->usecnt
))
1135 ret
= mr
->device
->rereg_phys_mr(mr
, mr_rereg_mask
, pd
,
1136 phys_buf_array
, num_phys_buf
,
1137 mr_access_flags
, iova_start
);
1139 if (!ret
&& (mr_rereg_mask
& IB_MR_REREG_PD
)) {
1140 atomic_dec(&old_pd
->usecnt
);
1141 atomic_inc(&pd
->usecnt
);
1146 EXPORT_SYMBOL(ib_rereg_phys_mr
);
1148 int ib_query_mr(struct ib_mr
*mr
, struct ib_mr_attr
*mr_attr
)
1150 return mr
->device
->query_mr
?
1151 mr
->device
->query_mr(mr
, mr_attr
) : -ENOSYS
;
1153 EXPORT_SYMBOL(ib_query_mr
);
1155 int ib_dereg_mr(struct ib_mr
*mr
)
1160 if (atomic_read(&mr
->usecnt
))
1164 ret
= mr
->device
->dereg_mr(mr
);
1166 atomic_dec(&pd
->usecnt
);
1170 EXPORT_SYMBOL(ib_dereg_mr
);
1172 struct ib_mr
*ib_create_mr(struct ib_pd
*pd
,
1173 struct ib_mr_init_attr
*mr_init_attr
)
1177 if (!pd
->device
->create_mr
)
1178 return ERR_PTR(-ENOSYS
);
1180 mr
= pd
->device
->create_mr(pd
, mr_init_attr
);
1183 mr
->device
= pd
->device
;
1186 atomic_inc(&pd
->usecnt
);
1187 atomic_set(&mr
->usecnt
, 0);
1192 EXPORT_SYMBOL(ib_create_mr
);
1194 int ib_destroy_mr(struct ib_mr
*mr
)
1199 if (atomic_read(&mr
->usecnt
))
1203 ret
= mr
->device
->destroy_mr(mr
);
1205 atomic_dec(&pd
->usecnt
);
1209 EXPORT_SYMBOL(ib_destroy_mr
);
1211 struct ib_mr
*ib_alloc_fast_reg_mr(struct ib_pd
*pd
, int max_page_list_len
)
1215 if (!pd
->device
->alloc_fast_reg_mr
)
1216 return ERR_PTR(-ENOSYS
);
1218 mr
= pd
->device
->alloc_fast_reg_mr(pd
, max_page_list_len
);
1221 mr
->device
= pd
->device
;
1224 atomic_inc(&pd
->usecnt
);
1225 atomic_set(&mr
->usecnt
, 0);
1230 EXPORT_SYMBOL(ib_alloc_fast_reg_mr
);
1232 struct ib_fast_reg_page_list
*ib_alloc_fast_reg_page_list(struct ib_device
*device
,
1233 int max_page_list_len
)
1235 struct ib_fast_reg_page_list
*page_list
;
1237 if (!device
->alloc_fast_reg_page_list
)
1238 return ERR_PTR(-ENOSYS
);
1240 page_list
= device
->alloc_fast_reg_page_list(device
, max_page_list_len
);
1242 if (!IS_ERR(page_list
)) {
1243 page_list
->device
= device
;
1244 page_list
->max_page_list_len
= max_page_list_len
;
1249 EXPORT_SYMBOL(ib_alloc_fast_reg_page_list
);
1251 void ib_free_fast_reg_page_list(struct ib_fast_reg_page_list
*page_list
)
1253 page_list
->device
->free_fast_reg_page_list(page_list
);
1255 EXPORT_SYMBOL(ib_free_fast_reg_page_list
);
1257 /* Memory windows */
1259 struct ib_mw
*ib_alloc_mw(struct ib_pd
*pd
, enum ib_mw_type type
)
1263 if (!pd
->device
->alloc_mw
)
1264 return ERR_PTR(-ENOSYS
);
1266 mw
= pd
->device
->alloc_mw(pd
, type
);
1268 mw
->device
= pd
->device
;
1272 atomic_inc(&pd
->usecnt
);
1277 EXPORT_SYMBOL(ib_alloc_mw
);
1279 int ib_dealloc_mw(struct ib_mw
*mw
)
1285 ret
= mw
->device
->dealloc_mw(mw
);
1287 atomic_dec(&pd
->usecnt
);
1291 EXPORT_SYMBOL(ib_dealloc_mw
);
1293 /* "Fast" memory regions */
1295 struct ib_fmr
*ib_alloc_fmr(struct ib_pd
*pd
,
1296 int mr_access_flags
,
1297 struct ib_fmr_attr
*fmr_attr
)
1301 if (!pd
->device
->alloc_fmr
)
1302 return ERR_PTR(-ENOSYS
);
1304 fmr
= pd
->device
->alloc_fmr(pd
, mr_access_flags
, fmr_attr
);
1306 fmr
->device
= pd
->device
;
1308 atomic_inc(&pd
->usecnt
);
1313 EXPORT_SYMBOL(ib_alloc_fmr
);
1315 int ib_unmap_fmr(struct list_head
*fmr_list
)
1319 if (list_empty(fmr_list
))
1322 fmr
= list_entry(fmr_list
->next
, struct ib_fmr
, list
);
1323 return fmr
->device
->unmap_fmr(fmr_list
);
1325 EXPORT_SYMBOL(ib_unmap_fmr
);
1327 int ib_dealloc_fmr(struct ib_fmr
*fmr
)
1333 ret
= fmr
->device
->dealloc_fmr(fmr
);
1335 atomic_dec(&pd
->usecnt
);
1339 EXPORT_SYMBOL(ib_dealloc_fmr
);
1341 /* Multicast groups */
1343 int ib_attach_mcast(struct ib_qp
*qp
, union ib_gid
*gid
, u16 lid
)
1347 if (!qp
->device
->attach_mcast
)
1349 if (gid
->raw
[0] != 0xff || qp
->qp_type
!= IB_QPT_UD
)
1352 ret
= qp
->device
->attach_mcast(qp
, gid
, lid
);
1354 atomic_inc(&qp
->usecnt
);
1357 EXPORT_SYMBOL(ib_attach_mcast
);
1359 int ib_detach_mcast(struct ib_qp
*qp
, union ib_gid
*gid
, u16 lid
)
1363 if (!qp
->device
->detach_mcast
)
1365 if (gid
->raw
[0] != 0xff || qp
->qp_type
!= IB_QPT_UD
)
1368 ret
= qp
->device
->detach_mcast(qp
, gid
, lid
);
1370 atomic_dec(&qp
->usecnt
);
1373 EXPORT_SYMBOL(ib_detach_mcast
);
1375 struct ib_xrcd
*ib_alloc_xrcd(struct ib_device
*device
)
1377 struct ib_xrcd
*xrcd
;
1379 if (!device
->alloc_xrcd
)
1380 return ERR_PTR(-ENOSYS
);
1382 xrcd
= device
->alloc_xrcd(device
, NULL
, NULL
);
1383 if (!IS_ERR(xrcd
)) {
1384 xrcd
->device
= device
;
1386 atomic_set(&xrcd
->usecnt
, 0);
1387 mutex_init(&xrcd
->tgt_qp_mutex
);
1388 INIT_LIST_HEAD(&xrcd
->tgt_qp_list
);
1393 EXPORT_SYMBOL(ib_alloc_xrcd
);
1395 int ib_dealloc_xrcd(struct ib_xrcd
*xrcd
)
1400 if (atomic_read(&xrcd
->usecnt
))
1403 while (!list_empty(&xrcd
->tgt_qp_list
)) {
1404 qp
= list_entry(xrcd
->tgt_qp_list
.next
, struct ib_qp
, xrcd_list
);
1405 ret
= ib_destroy_qp(qp
);
1410 return xrcd
->device
->dealloc_xrcd(xrcd
);
1412 EXPORT_SYMBOL(ib_dealloc_xrcd
);
1414 struct ib_flow
*ib_create_flow(struct ib_qp
*qp
,
1415 struct ib_flow_attr
*flow_attr
,
1418 struct ib_flow
*flow_id
;
1419 if (!qp
->device
->create_flow
)
1420 return ERR_PTR(-ENOSYS
);
1422 flow_id
= qp
->device
->create_flow(qp
, flow_attr
, domain
);
1423 if (!IS_ERR(flow_id
))
1424 atomic_inc(&qp
->usecnt
);
1427 EXPORT_SYMBOL(ib_create_flow
);
1429 int ib_destroy_flow(struct ib_flow
*flow_id
)
1432 struct ib_qp
*qp
= flow_id
->qp
;
1434 err
= qp
->device
->destroy_flow(flow_id
);
1436 atomic_dec(&qp
->usecnt
);
1439 EXPORT_SYMBOL(ib_destroy_flow
);
1441 int ib_check_mr_status(struct ib_mr
*mr
, u32 check_mask
,
1442 struct ib_mr_status
*mr_status
)
1444 return mr
->device
->check_mr_status
?
1445 mr
->device
->check_mr_status(mr
, check_mask
, mr_status
) : -ENOSYS
;
1447 EXPORT_SYMBOL(ib_check_mr_status
);