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 __attribute_const__
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 __attribute_const__
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 __attribute_const__
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 __attribute_const__
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 if (!(*qp_attr_mask
& IB_QP_VID
))
883 qp_attr
->vlan_id
= rdma_get_vlan_id(&sgid
);
885 ret
= rdma_addr_find_dmac_by_grh(&sgid
, &qp_attr
->ah_attr
.grh
.dgid
,
886 qp_attr
->ah_attr
.dmac
, &qp_attr
->vlan_id
);
889 ret
= rdma_addr_find_smac_by_sgid(&sgid
, qp_attr
->smac
, NULL
);
893 *qp_attr_mask
|= IB_QP_SMAC
;
894 if (qp_attr
->vlan_id
< 0xFFFF)
895 *qp_attr_mask
|= IB_QP_VID
;
900 EXPORT_SYMBOL(ib_resolve_eth_l2_attrs
);
903 int ib_modify_qp(struct ib_qp
*qp
,
904 struct ib_qp_attr
*qp_attr
,
909 ret
= ib_resolve_eth_l2_attrs(qp
, qp_attr
, &qp_attr_mask
);
913 return qp
->device
->modify_qp(qp
->real_qp
, qp_attr
, qp_attr_mask
, NULL
);
915 EXPORT_SYMBOL(ib_modify_qp
);
917 int ib_query_qp(struct ib_qp
*qp
,
918 struct ib_qp_attr
*qp_attr
,
920 struct ib_qp_init_attr
*qp_init_attr
)
922 return qp
->device
->query_qp
?
923 qp
->device
->query_qp(qp
->real_qp
, qp_attr
, qp_attr_mask
, qp_init_attr
) :
926 EXPORT_SYMBOL(ib_query_qp
);
928 int ib_close_qp(struct ib_qp
*qp
)
930 struct ib_qp
*real_qp
;
933 real_qp
= qp
->real_qp
;
937 spin_lock_irqsave(&real_qp
->device
->event_handler_lock
, flags
);
938 list_del(&qp
->open_list
);
939 spin_unlock_irqrestore(&real_qp
->device
->event_handler_lock
, flags
);
941 atomic_dec(&real_qp
->usecnt
);
946 EXPORT_SYMBOL(ib_close_qp
);
948 static int __ib_destroy_shared_qp(struct ib_qp
*qp
)
950 struct ib_xrcd
*xrcd
;
951 struct ib_qp
*real_qp
;
954 real_qp
= qp
->real_qp
;
955 xrcd
= real_qp
->xrcd
;
957 mutex_lock(&xrcd
->tgt_qp_mutex
);
959 if (atomic_read(&real_qp
->usecnt
) == 0)
960 list_del(&real_qp
->xrcd_list
);
963 mutex_unlock(&xrcd
->tgt_qp_mutex
);
966 ret
= ib_destroy_qp(real_qp
);
968 atomic_dec(&xrcd
->usecnt
);
970 __ib_insert_xrcd_qp(xrcd
, real_qp
);
976 int ib_destroy_qp(struct ib_qp
*qp
)
979 struct ib_cq
*scq
, *rcq
;
983 if (atomic_read(&qp
->usecnt
))
986 if (qp
->real_qp
!= qp
)
987 return __ib_destroy_shared_qp(qp
);
994 ret
= qp
->device
->destroy_qp(qp
);
997 atomic_dec(&pd
->usecnt
);
999 atomic_dec(&scq
->usecnt
);
1001 atomic_dec(&rcq
->usecnt
);
1003 atomic_dec(&srq
->usecnt
);
1008 EXPORT_SYMBOL(ib_destroy_qp
);
1010 /* Completion queues */
1012 struct ib_cq
*ib_create_cq(struct ib_device
*device
,
1013 ib_comp_handler comp_handler
,
1014 void (*event_handler
)(struct ib_event
*, void *),
1015 void *cq_context
, int cqe
, int comp_vector
)
1019 cq
= device
->create_cq(device
, cqe
, comp_vector
, NULL
, NULL
);
1022 cq
->device
= device
;
1024 cq
->comp_handler
= comp_handler
;
1025 cq
->event_handler
= event_handler
;
1026 cq
->cq_context
= cq_context
;
1027 atomic_set(&cq
->usecnt
, 0);
1032 EXPORT_SYMBOL(ib_create_cq
);
1034 int ib_modify_cq(struct ib_cq
*cq
, u16 cq_count
, u16 cq_period
)
1036 return cq
->device
->modify_cq
?
1037 cq
->device
->modify_cq(cq
, cq_count
, cq_period
) : -ENOSYS
;
1039 EXPORT_SYMBOL(ib_modify_cq
);
1041 int ib_destroy_cq(struct ib_cq
*cq
)
1043 if (atomic_read(&cq
->usecnt
))
1046 return cq
->device
->destroy_cq(cq
);
1048 EXPORT_SYMBOL(ib_destroy_cq
);
1050 int ib_resize_cq(struct ib_cq
*cq
, int cqe
)
1052 return cq
->device
->resize_cq
?
1053 cq
->device
->resize_cq(cq
, cqe
, NULL
) : -ENOSYS
;
1055 EXPORT_SYMBOL(ib_resize_cq
);
1057 /* Memory regions */
1059 struct ib_mr
*ib_get_dma_mr(struct ib_pd
*pd
, int mr_access_flags
)
1064 err
= ib_check_mr_access(mr_access_flags
);
1066 return ERR_PTR(err
);
1068 mr
= pd
->device
->get_dma_mr(pd
, mr_access_flags
);
1071 mr
->device
= pd
->device
;
1074 atomic_inc(&pd
->usecnt
);
1075 atomic_set(&mr
->usecnt
, 0);
1080 EXPORT_SYMBOL(ib_get_dma_mr
);
1082 struct ib_mr
*ib_reg_phys_mr(struct ib_pd
*pd
,
1083 struct ib_phys_buf
*phys_buf_array
,
1085 int mr_access_flags
,
1091 err
= ib_check_mr_access(mr_access_flags
);
1093 return ERR_PTR(err
);
1095 if (!pd
->device
->reg_phys_mr
)
1096 return ERR_PTR(-ENOSYS
);
1098 mr
= pd
->device
->reg_phys_mr(pd
, phys_buf_array
, num_phys_buf
,
1099 mr_access_flags
, iova_start
);
1102 mr
->device
= pd
->device
;
1105 atomic_inc(&pd
->usecnt
);
1106 atomic_set(&mr
->usecnt
, 0);
1111 EXPORT_SYMBOL(ib_reg_phys_mr
);
1113 int ib_rereg_phys_mr(struct ib_mr
*mr
,
1116 struct ib_phys_buf
*phys_buf_array
,
1118 int mr_access_flags
,
1121 struct ib_pd
*old_pd
;
1124 ret
= ib_check_mr_access(mr_access_flags
);
1128 if (!mr
->device
->rereg_phys_mr
)
1131 if (atomic_read(&mr
->usecnt
))
1136 ret
= mr
->device
->rereg_phys_mr(mr
, mr_rereg_mask
, pd
,
1137 phys_buf_array
, num_phys_buf
,
1138 mr_access_flags
, iova_start
);
1140 if (!ret
&& (mr_rereg_mask
& IB_MR_REREG_PD
)) {
1141 atomic_dec(&old_pd
->usecnt
);
1142 atomic_inc(&pd
->usecnt
);
1147 EXPORT_SYMBOL(ib_rereg_phys_mr
);
1149 int ib_query_mr(struct ib_mr
*mr
, struct ib_mr_attr
*mr_attr
)
1151 return mr
->device
->query_mr
?
1152 mr
->device
->query_mr(mr
, mr_attr
) : -ENOSYS
;
1154 EXPORT_SYMBOL(ib_query_mr
);
1156 int ib_dereg_mr(struct ib_mr
*mr
)
1161 if (atomic_read(&mr
->usecnt
))
1165 ret
= mr
->device
->dereg_mr(mr
);
1167 atomic_dec(&pd
->usecnt
);
1171 EXPORT_SYMBOL(ib_dereg_mr
);
1173 struct ib_mr
*ib_create_mr(struct ib_pd
*pd
,
1174 struct ib_mr_init_attr
*mr_init_attr
)
1178 if (!pd
->device
->create_mr
)
1179 return ERR_PTR(-ENOSYS
);
1181 mr
= pd
->device
->create_mr(pd
, mr_init_attr
);
1184 mr
->device
= pd
->device
;
1187 atomic_inc(&pd
->usecnt
);
1188 atomic_set(&mr
->usecnt
, 0);
1193 EXPORT_SYMBOL(ib_create_mr
);
1195 int ib_destroy_mr(struct ib_mr
*mr
)
1200 if (atomic_read(&mr
->usecnt
))
1204 ret
= mr
->device
->destroy_mr(mr
);
1206 atomic_dec(&pd
->usecnt
);
1210 EXPORT_SYMBOL(ib_destroy_mr
);
1212 struct ib_mr
*ib_alloc_fast_reg_mr(struct ib_pd
*pd
, int max_page_list_len
)
1216 if (!pd
->device
->alloc_fast_reg_mr
)
1217 return ERR_PTR(-ENOSYS
);
1219 mr
= pd
->device
->alloc_fast_reg_mr(pd
, max_page_list_len
);
1222 mr
->device
= pd
->device
;
1225 atomic_inc(&pd
->usecnt
);
1226 atomic_set(&mr
->usecnt
, 0);
1231 EXPORT_SYMBOL(ib_alloc_fast_reg_mr
);
1233 struct ib_fast_reg_page_list
*ib_alloc_fast_reg_page_list(struct ib_device
*device
,
1234 int max_page_list_len
)
1236 struct ib_fast_reg_page_list
*page_list
;
1238 if (!device
->alloc_fast_reg_page_list
)
1239 return ERR_PTR(-ENOSYS
);
1241 page_list
= device
->alloc_fast_reg_page_list(device
, max_page_list_len
);
1243 if (!IS_ERR(page_list
)) {
1244 page_list
->device
= device
;
1245 page_list
->max_page_list_len
= max_page_list_len
;
1250 EXPORT_SYMBOL(ib_alloc_fast_reg_page_list
);
1252 void ib_free_fast_reg_page_list(struct ib_fast_reg_page_list
*page_list
)
1254 page_list
->device
->free_fast_reg_page_list(page_list
);
1256 EXPORT_SYMBOL(ib_free_fast_reg_page_list
);
1258 /* Memory windows */
1260 struct ib_mw
*ib_alloc_mw(struct ib_pd
*pd
, enum ib_mw_type type
)
1264 if (!pd
->device
->alloc_mw
)
1265 return ERR_PTR(-ENOSYS
);
1267 mw
= pd
->device
->alloc_mw(pd
, type
);
1269 mw
->device
= pd
->device
;
1273 atomic_inc(&pd
->usecnt
);
1278 EXPORT_SYMBOL(ib_alloc_mw
);
1280 int ib_dealloc_mw(struct ib_mw
*mw
)
1286 ret
= mw
->device
->dealloc_mw(mw
);
1288 atomic_dec(&pd
->usecnt
);
1292 EXPORT_SYMBOL(ib_dealloc_mw
);
1294 /* "Fast" memory regions */
1296 struct ib_fmr
*ib_alloc_fmr(struct ib_pd
*pd
,
1297 int mr_access_flags
,
1298 struct ib_fmr_attr
*fmr_attr
)
1302 if (!pd
->device
->alloc_fmr
)
1303 return ERR_PTR(-ENOSYS
);
1305 fmr
= pd
->device
->alloc_fmr(pd
, mr_access_flags
, fmr_attr
);
1307 fmr
->device
= pd
->device
;
1309 atomic_inc(&pd
->usecnt
);
1314 EXPORT_SYMBOL(ib_alloc_fmr
);
1316 int ib_unmap_fmr(struct list_head
*fmr_list
)
1320 if (list_empty(fmr_list
))
1323 fmr
= list_entry(fmr_list
->next
, struct ib_fmr
, list
);
1324 return fmr
->device
->unmap_fmr(fmr_list
);
1326 EXPORT_SYMBOL(ib_unmap_fmr
);
1328 int ib_dealloc_fmr(struct ib_fmr
*fmr
)
1334 ret
= fmr
->device
->dealloc_fmr(fmr
);
1336 atomic_dec(&pd
->usecnt
);
1340 EXPORT_SYMBOL(ib_dealloc_fmr
);
1342 /* Multicast groups */
1344 int ib_attach_mcast(struct ib_qp
*qp
, union ib_gid
*gid
, u16 lid
)
1348 if (!qp
->device
->attach_mcast
)
1350 if (gid
->raw
[0] != 0xff || qp
->qp_type
!= IB_QPT_UD
)
1353 ret
= qp
->device
->attach_mcast(qp
, gid
, lid
);
1355 atomic_inc(&qp
->usecnt
);
1358 EXPORT_SYMBOL(ib_attach_mcast
);
1360 int ib_detach_mcast(struct ib_qp
*qp
, union ib_gid
*gid
, u16 lid
)
1364 if (!qp
->device
->detach_mcast
)
1366 if (gid
->raw
[0] != 0xff || qp
->qp_type
!= IB_QPT_UD
)
1369 ret
= qp
->device
->detach_mcast(qp
, gid
, lid
);
1371 atomic_dec(&qp
->usecnt
);
1374 EXPORT_SYMBOL(ib_detach_mcast
);
1376 struct ib_xrcd
*ib_alloc_xrcd(struct ib_device
*device
)
1378 struct ib_xrcd
*xrcd
;
1380 if (!device
->alloc_xrcd
)
1381 return ERR_PTR(-ENOSYS
);
1383 xrcd
= device
->alloc_xrcd(device
, NULL
, NULL
);
1384 if (!IS_ERR(xrcd
)) {
1385 xrcd
->device
= device
;
1387 atomic_set(&xrcd
->usecnt
, 0);
1388 mutex_init(&xrcd
->tgt_qp_mutex
);
1389 INIT_LIST_HEAD(&xrcd
->tgt_qp_list
);
1394 EXPORT_SYMBOL(ib_alloc_xrcd
);
1396 int ib_dealloc_xrcd(struct ib_xrcd
*xrcd
)
1401 if (atomic_read(&xrcd
->usecnt
))
1404 while (!list_empty(&xrcd
->tgt_qp_list
)) {
1405 qp
= list_entry(xrcd
->tgt_qp_list
.next
, struct ib_qp
, xrcd_list
);
1406 ret
= ib_destroy_qp(qp
);
1411 return xrcd
->device
->dealloc_xrcd(xrcd
);
1413 EXPORT_SYMBOL(ib_dealloc_xrcd
);
1415 struct ib_flow
*ib_create_flow(struct ib_qp
*qp
,
1416 struct ib_flow_attr
*flow_attr
,
1419 struct ib_flow
*flow_id
;
1420 if (!qp
->device
->create_flow
)
1421 return ERR_PTR(-ENOSYS
);
1423 flow_id
= qp
->device
->create_flow(qp
, flow_attr
, domain
);
1424 if (!IS_ERR(flow_id
))
1425 atomic_inc(&qp
->usecnt
);
1428 EXPORT_SYMBOL(ib_create_flow
);
1430 int ib_destroy_flow(struct ib_flow
*flow_id
)
1433 struct ib_qp
*qp
= flow_id
->qp
;
1435 err
= qp
->device
->destroy_flow(flow_id
);
1437 atomic_dec(&qp
->usecnt
);
1440 EXPORT_SYMBOL(ib_destroy_flow
);
1442 int ib_check_mr_status(struct ib_mr
*mr
, u32 check_mask
,
1443 struct ib_mr_status
*mr_status
)
1445 return mr
->device
->check_mr_status
?
1446 mr
->device
->check_mr_status(mr
, check_mask
, mr_status
) : -ENOSYS
;
1448 EXPORT_SYMBOL(ib_check_mr_status
);