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 <linux/in6.h>
46 #include <net/addrconf.h>
47 #include <linux/security.h>
49 #include <rdma/ib_verbs.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/ib_addr.h>
54 #include "core_priv.h"
56 static int ib_resolve_eth_dmac(struct ib_device *device,
57 struct rdma_ah_attr *ah_attr);
59 static const char * const ib_events[] = {
60 [IB_EVENT_CQ_ERR] = "CQ error",
61 [IB_EVENT_QP_FATAL] = "QP fatal error",
62 [IB_EVENT_QP_REQ_ERR] = "QP request error",
63 [IB_EVENT_QP_ACCESS_ERR] = "QP access error",
64 [IB_EVENT_COMM_EST] = "communication established",
65 [IB_EVENT_SQ_DRAINED] = "send queue drained",
66 [IB_EVENT_PATH_MIG] = "path migration successful",
67 [IB_EVENT_PATH_MIG_ERR] = "path migration error",
68 [IB_EVENT_DEVICE_FATAL] = "device fatal error",
69 [IB_EVENT_PORT_ACTIVE] = "port active",
70 [IB_EVENT_PORT_ERR] = "port error",
71 [IB_EVENT_LID_CHANGE] = "LID change",
72 [IB_EVENT_PKEY_CHANGE] = "P_key change",
73 [IB_EVENT_SM_CHANGE] = "SM change",
74 [IB_EVENT_SRQ_ERR] = "SRQ error",
75 [IB_EVENT_SRQ_LIMIT_REACHED] = "SRQ limit reached",
76 [IB_EVENT_QP_LAST_WQE_REACHED] = "last WQE reached",
77 [IB_EVENT_CLIENT_REREGISTER] = "client reregister",
78 [IB_EVENT_GID_CHANGE] = "GID changed",
81 const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
85 return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
86 ib_events[index] : "unrecognized event";
88 EXPORT_SYMBOL(ib_event_msg);
90 static const char * const wc_statuses[] = {
91 [IB_WC_SUCCESS] = "success",
92 [IB_WC_LOC_LEN_ERR] = "local length error",
93 [IB_WC_LOC_QP_OP_ERR] = "local QP operation error",
94 [IB_WC_LOC_EEC_OP_ERR] = "local EE context operation error",
95 [IB_WC_LOC_PROT_ERR] = "local protection error",
96 [IB_WC_WR_FLUSH_ERR] = "WR flushed",
97 [IB_WC_MW_BIND_ERR] = "memory management operation error",
98 [IB_WC_BAD_RESP_ERR] = "bad response error",
99 [IB_WC_LOC_ACCESS_ERR] = "local access error",
100 [IB_WC_REM_INV_REQ_ERR] = "invalid request error",
101 [IB_WC_REM_ACCESS_ERR] = "remote access error",
102 [IB_WC_REM_OP_ERR] = "remote operation error",
103 [IB_WC_RETRY_EXC_ERR] = "transport retry counter exceeded",
104 [IB_WC_RNR_RETRY_EXC_ERR] = "RNR retry counter exceeded",
105 [IB_WC_LOC_RDD_VIOL_ERR] = "local RDD violation error",
106 [IB_WC_REM_INV_RD_REQ_ERR] = "remote invalid RD request",
107 [IB_WC_REM_ABORT_ERR] = "operation aborted",
108 [IB_WC_INV_EECN_ERR] = "invalid EE context number",
109 [IB_WC_INV_EEC_STATE_ERR] = "invalid EE context state",
110 [IB_WC_FATAL_ERR] = "fatal error",
111 [IB_WC_RESP_TIMEOUT_ERR] = "response timeout error",
112 [IB_WC_GENERAL_ERR] = "general error",
115 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
117 size_t index = status;
119 return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
120 wc_statuses[index] : "unrecognized status";
122 EXPORT_SYMBOL(ib_wc_status_msg);
124 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
127 case IB_RATE_2_5_GBPS: return 1;
128 case IB_RATE_5_GBPS: return 2;
129 case IB_RATE_10_GBPS: return 4;
130 case IB_RATE_20_GBPS: return 8;
131 case IB_RATE_30_GBPS: return 12;
132 case IB_RATE_40_GBPS: return 16;
133 case IB_RATE_60_GBPS: return 24;
134 case IB_RATE_80_GBPS: return 32;
135 case IB_RATE_120_GBPS: return 48;
136 case IB_RATE_14_GBPS: return 6;
137 case IB_RATE_56_GBPS: return 22;
138 case IB_RATE_112_GBPS: return 45;
139 case IB_RATE_168_GBPS: return 67;
140 case IB_RATE_25_GBPS: return 10;
141 case IB_RATE_100_GBPS: return 40;
142 case IB_RATE_200_GBPS: return 80;
143 case IB_RATE_300_GBPS: return 120;
144 case IB_RATE_28_GBPS: return 11;
145 case IB_RATE_50_GBPS: return 20;
146 case IB_RATE_400_GBPS: return 160;
147 case IB_RATE_600_GBPS: return 240;
151 EXPORT_SYMBOL(ib_rate_to_mult);
153 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
156 case 1: return IB_RATE_2_5_GBPS;
157 case 2: return IB_RATE_5_GBPS;
158 case 4: return IB_RATE_10_GBPS;
159 case 8: return IB_RATE_20_GBPS;
160 case 12: return IB_RATE_30_GBPS;
161 case 16: return IB_RATE_40_GBPS;
162 case 24: return IB_RATE_60_GBPS;
163 case 32: return IB_RATE_80_GBPS;
164 case 48: return IB_RATE_120_GBPS;
165 case 6: return IB_RATE_14_GBPS;
166 case 22: return IB_RATE_56_GBPS;
167 case 45: return IB_RATE_112_GBPS;
168 case 67: return IB_RATE_168_GBPS;
169 case 10: return IB_RATE_25_GBPS;
170 case 40: return IB_RATE_100_GBPS;
171 case 80: return IB_RATE_200_GBPS;
172 case 120: return IB_RATE_300_GBPS;
173 case 11: return IB_RATE_28_GBPS;
174 case 20: return IB_RATE_50_GBPS;
175 case 160: return IB_RATE_400_GBPS;
176 case 240: return IB_RATE_600_GBPS;
177 default: return IB_RATE_PORT_CURRENT;
180 EXPORT_SYMBOL(mult_to_ib_rate);
182 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
185 case IB_RATE_2_5_GBPS: return 2500;
186 case IB_RATE_5_GBPS: return 5000;
187 case IB_RATE_10_GBPS: return 10000;
188 case IB_RATE_20_GBPS: return 20000;
189 case IB_RATE_30_GBPS: return 30000;
190 case IB_RATE_40_GBPS: return 40000;
191 case IB_RATE_60_GBPS: return 60000;
192 case IB_RATE_80_GBPS: return 80000;
193 case IB_RATE_120_GBPS: return 120000;
194 case IB_RATE_14_GBPS: return 14062;
195 case IB_RATE_56_GBPS: return 56250;
196 case IB_RATE_112_GBPS: return 112500;
197 case IB_RATE_168_GBPS: return 168750;
198 case IB_RATE_25_GBPS: return 25781;
199 case IB_RATE_100_GBPS: return 103125;
200 case IB_RATE_200_GBPS: return 206250;
201 case IB_RATE_300_GBPS: return 309375;
202 case IB_RATE_28_GBPS: return 28125;
203 case IB_RATE_50_GBPS: return 53125;
204 case IB_RATE_400_GBPS: return 425000;
205 case IB_RATE_600_GBPS: return 637500;
209 EXPORT_SYMBOL(ib_rate_to_mbps);
211 __attribute_const__ enum rdma_transport_type
212 rdma_node_get_transport(unsigned int node_type)
215 if (node_type == RDMA_NODE_USNIC)
216 return RDMA_TRANSPORT_USNIC;
217 if (node_type == RDMA_NODE_USNIC_UDP)
218 return RDMA_TRANSPORT_USNIC_UDP;
219 if (node_type == RDMA_NODE_RNIC)
220 return RDMA_TRANSPORT_IWARP;
221 if (node_type == RDMA_NODE_UNSPECIFIED)
222 return RDMA_TRANSPORT_UNSPECIFIED;
224 return RDMA_TRANSPORT_IB;
226 EXPORT_SYMBOL(rdma_node_get_transport);
228 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
230 enum rdma_transport_type lt;
231 if (device->ops.get_link_layer)
232 return device->ops.get_link_layer(device, port_num);
234 lt = rdma_node_get_transport(device->node_type);
235 if (lt == RDMA_TRANSPORT_IB)
236 return IB_LINK_LAYER_INFINIBAND;
238 return IB_LINK_LAYER_ETHERNET;
240 EXPORT_SYMBOL(rdma_port_get_link_layer);
242 /* Protection domains */
245 * ib_alloc_pd - Allocates an unused protection domain.
246 * @device: The device on which to allocate the protection domain.
248 * A protection domain object provides an association between QPs, shared
249 * receive queues, address handles, memory regions, and memory windows.
251 * Every PD has a local_dma_lkey which can be used as the lkey value for local
254 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
258 int mr_access_flags = 0;
261 pd = rdma_zalloc_drv_obj(device, ib_pd);
263 return ERR_PTR(-ENOMEM);
267 pd->__internal_mr = NULL;
268 atomic_set(&pd->usecnt, 0);
271 pd->res.type = RDMA_RESTRACK_PD;
272 rdma_restrack_set_task(&pd->res, caller);
274 ret = device->ops.alloc_pd(pd, NULL);
279 rdma_restrack_kadd(&pd->res);
281 if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
282 pd->local_dma_lkey = device->local_dma_lkey;
284 mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
286 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
287 pr_warn("%s: enabling unsafe global rkey\n", caller);
288 mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
291 if (mr_access_flags) {
294 mr = pd->device->ops.get_dma_mr(pd, mr_access_flags);
300 mr->device = pd->device;
302 mr->type = IB_MR_TYPE_DMA;
304 mr->need_inval = false;
306 pd->__internal_mr = mr;
308 if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
309 pd->local_dma_lkey = pd->__internal_mr->lkey;
311 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
312 pd->unsafe_global_rkey = pd->__internal_mr->rkey;
317 EXPORT_SYMBOL(__ib_alloc_pd);
320 * ib_dealloc_pd_user - Deallocates a protection domain.
321 * @pd: The protection domain to deallocate.
322 * @udata: Valid user data or NULL for kernel object
324 * It is an error to call this function while any resources in the pd still
325 * exist. The caller is responsible to synchronously destroy them and
326 * guarantee no new allocations will happen.
328 void ib_dealloc_pd_user(struct ib_pd *pd, struct ib_udata *udata)
332 if (pd->__internal_mr) {
333 ret = pd->device->ops.dereg_mr(pd->__internal_mr, NULL);
335 pd->__internal_mr = NULL;
338 /* uverbs manipulates usecnt with proper locking, while the kabi
339 requires the caller to guarantee we can't race here. */
340 WARN_ON(atomic_read(&pd->usecnt));
342 rdma_restrack_del(&pd->res);
343 pd->device->ops.dealloc_pd(pd, udata);
346 EXPORT_SYMBOL(ib_dealloc_pd_user);
348 /* Address handles */
351 * rdma_copy_ah_attr - Copy rdma ah attribute from source to destination.
352 * @dest: Pointer to destination ah_attr. Contents of the destination
353 * pointer is assumed to be invalid and attribute are overwritten.
354 * @src: Pointer to source ah_attr.
356 void rdma_copy_ah_attr(struct rdma_ah_attr *dest,
357 const struct rdma_ah_attr *src)
360 if (dest->grh.sgid_attr)
361 rdma_hold_gid_attr(dest->grh.sgid_attr);
363 EXPORT_SYMBOL(rdma_copy_ah_attr);
366 * rdma_replace_ah_attr - Replace valid ah_attr with new new one.
367 * @old: Pointer to existing ah_attr which needs to be replaced.
368 * old is assumed to be valid or zero'd
369 * @new: Pointer to the new ah_attr.
371 * rdma_replace_ah_attr() first releases any reference in the old ah_attr if
372 * old the ah_attr is valid; after that it copies the new attribute and holds
373 * the reference to the replaced ah_attr.
375 void rdma_replace_ah_attr(struct rdma_ah_attr *old,
376 const struct rdma_ah_attr *new)
378 rdma_destroy_ah_attr(old);
380 if (old->grh.sgid_attr)
381 rdma_hold_gid_attr(old->grh.sgid_attr);
383 EXPORT_SYMBOL(rdma_replace_ah_attr);
386 * rdma_move_ah_attr - Move ah_attr pointed by source to destination.
387 * @dest: Pointer to destination ah_attr to copy to.
388 * dest is assumed to be valid or zero'd
389 * @src: Pointer to the new ah_attr.
391 * rdma_move_ah_attr() first releases any reference in the destination ah_attr
392 * if it is valid. This also transfers ownership of internal references from
393 * src to dest, making src invalid in the process. No new reference of the src
396 void rdma_move_ah_attr(struct rdma_ah_attr *dest, struct rdma_ah_attr *src)
398 rdma_destroy_ah_attr(dest);
400 src->grh.sgid_attr = NULL;
402 EXPORT_SYMBOL(rdma_move_ah_attr);
405 * Validate that the rdma_ah_attr is valid for the device before passing it
408 static int rdma_check_ah_attr(struct ib_device *device,
409 struct rdma_ah_attr *ah_attr)
411 if (!rdma_is_port_valid(device, ah_attr->port_num))
414 if ((rdma_is_grh_required(device, ah_attr->port_num) ||
415 ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) &&
416 !(ah_attr->ah_flags & IB_AH_GRH))
419 if (ah_attr->grh.sgid_attr) {
421 * Make sure the passed sgid_attr is consistent with the
424 if (ah_attr->grh.sgid_attr->index != ah_attr->grh.sgid_index ||
425 ah_attr->grh.sgid_attr->port_num != ah_attr->port_num)
432 * If the ah requires a GRH then ensure that sgid_attr pointer is filled in.
433 * On success the caller is responsible to call rdma_unfill_sgid_attr().
435 static int rdma_fill_sgid_attr(struct ib_device *device,
436 struct rdma_ah_attr *ah_attr,
437 const struct ib_gid_attr **old_sgid_attr)
439 const struct ib_gid_attr *sgid_attr;
440 struct ib_global_route *grh;
443 *old_sgid_attr = ah_attr->grh.sgid_attr;
445 ret = rdma_check_ah_attr(device, ah_attr);
449 if (!(ah_attr->ah_flags & IB_AH_GRH))
452 grh = rdma_ah_retrieve_grh(ah_attr);
457 rdma_get_gid_attr(device, ah_attr->port_num, grh->sgid_index);
458 if (IS_ERR(sgid_attr))
459 return PTR_ERR(sgid_attr);
461 /* Move ownerhip of the kref into the ah_attr */
462 grh->sgid_attr = sgid_attr;
466 static void rdma_unfill_sgid_attr(struct rdma_ah_attr *ah_attr,
467 const struct ib_gid_attr *old_sgid_attr)
470 * Fill didn't change anything, the caller retains ownership of
473 if (ah_attr->grh.sgid_attr == old_sgid_attr)
477 * Otherwise, we need to undo what rdma_fill_sgid_attr so the caller
478 * doesn't see any change in the rdma_ah_attr. If we get here
479 * old_sgid_attr is NULL.
481 rdma_destroy_ah_attr(ah_attr);
484 static const struct ib_gid_attr *
485 rdma_update_sgid_attr(struct rdma_ah_attr *ah_attr,
486 const struct ib_gid_attr *old_attr)
489 rdma_put_gid_attr(old_attr);
490 if (ah_attr->ah_flags & IB_AH_GRH) {
491 rdma_hold_gid_attr(ah_attr->grh.sgid_attr);
492 return ah_attr->grh.sgid_attr;
497 static struct ib_ah *_rdma_create_ah(struct ib_pd *pd,
498 struct rdma_ah_attr *ah_attr,
500 struct ib_udata *udata)
502 struct ib_device *device = pd->device;
506 might_sleep_if(flags & RDMA_CREATE_AH_SLEEPABLE);
508 if (!device->ops.create_ah)
509 return ERR_PTR(-EOPNOTSUPP);
511 ah = rdma_zalloc_drv_obj_gfp(
513 (flags & RDMA_CREATE_AH_SLEEPABLE) ? GFP_KERNEL : GFP_ATOMIC);
515 return ERR_PTR(-ENOMEM);
519 ah->type = ah_attr->type;
520 ah->sgid_attr = rdma_update_sgid_attr(ah_attr, NULL);
522 ret = device->ops.create_ah(ah, ah_attr, flags, udata);
528 atomic_inc(&pd->usecnt);
533 * rdma_create_ah - Creates an address handle for the
534 * given address vector.
535 * @pd: The protection domain associated with the address handle.
536 * @ah_attr: The attributes of the address vector.
537 * @flags: Create address handle flags (see enum rdma_create_ah_flags).
539 * It returns 0 on success and returns appropriate error code on error.
540 * The address handle is used to reference a local or global destination
541 * in all UD QP post sends.
543 struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
546 const struct ib_gid_attr *old_sgid_attr;
550 ret = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
554 ah = _rdma_create_ah(pd, ah_attr, flags, NULL);
556 rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
559 EXPORT_SYMBOL(rdma_create_ah);
562 * rdma_create_user_ah - Creates an address handle for the
563 * given address vector.
564 * It resolves destination mac address for ah attribute of RoCE type.
565 * @pd: The protection domain associated with the address handle.
566 * @ah_attr: The attributes of the address vector.
567 * @udata: pointer to user's input output buffer information need by
570 * It returns 0 on success and returns appropriate error code on error.
571 * The address handle is used to reference a local or global destination
572 * in all UD QP post sends.
574 struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
575 struct rdma_ah_attr *ah_attr,
576 struct ib_udata *udata)
578 const struct ib_gid_attr *old_sgid_attr;
582 err = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
586 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
587 err = ib_resolve_eth_dmac(pd->device, ah_attr);
594 ah = _rdma_create_ah(pd, ah_attr, RDMA_CREATE_AH_SLEEPABLE, udata);
597 rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
600 EXPORT_SYMBOL(rdma_create_user_ah);
602 int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
604 const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
605 struct iphdr ip4h_checked;
606 const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;
608 /* If it's IPv6, the version must be 6, otherwise, the first
609 * 20 bytes (before the IPv4 header) are garbled.
611 if (ip6h->version != 6)
612 return (ip4h->version == 4) ? 4 : 0;
613 /* version may be 6 or 4 because the first 20 bytes could be garbled */
615 /* RoCE v2 requires no options, thus header length
622 * We can't write on scattered buffers so we need to copy to
625 memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
626 ip4h_checked.check = 0;
627 ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
628 /* if IPv4 header checksum is OK, believe it */
629 if (ip4h->check == ip4h_checked.check)
633 EXPORT_SYMBOL(ib_get_rdma_header_version);
635 static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
637 const struct ib_grh *grh)
641 if (rdma_protocol_ib(device, port_num))
642 return RDMA_NETWORK_IB;
644 grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
646 if (grh_version == 4)
647 return RDMA_NETWORK_IPV4;
649 if (grh->next_hdr == IPPROTO_UDP)
650 return RDMA_NETWORK_IPV6;
652 return RDMA_NETWORK_ROCE_V1;
655 struct find_gid_index_context {
657 enum ib_gid_type gid_type;
660 static bool find_gid_index(const union ib_gid *gid,
661 const struct ib_gid_attr *gid_attr,
664 struct find_gid_index_context *ctx = context;
665 u16 vlan_id = 0xffff;
668 if (ctx->gid_type != gid_attr->gid_type)
671 ret = rdma_read_gid_l2_fields(gid_attr, &vlan_id, NULL);
675 return ctx->vlan_id == vlan_id;
678 static const struct ib_gid_attr *
679 get_sgid_attr_from_eth(struct ib_device *device, u8 port_num,
680 u16 vlan_id, const union ib_gid *sgid,
681 enum ib_gid_type gid_type)
683 struct find_gid_index_context context = {.vlan_id = vlan_id,
684 .gid_type = gid_type};
686 return rdma_find_gid_by_filter(device, sgid, port_num, find_gid_index,
690 int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
691 enum rdma_network_type net_type,
692 union ib_gid *sgid, union ib_gid *dgid)
694 struct sockaddr_in src_in;
695 struct sockaddr_in dst_in;
696 __be32 src_saddr, dst_saddr;
701 if (net_type == RDMA_NETWORK_IPV4) {
702 memcpy(&src_in.sin_addr.s_addr,
703 &hdr->roce4grh.saddr, 4);
704 memcpy(&dst_in.sin_addr.s_addr,
705 &hdr->roce4grh.daddr, 4);
706 src_saddr = src_in.sin_addr.s_addr;
707 dst_saddr = dst_in.sin_addr.s_addr;
708 ipv6_addr_set_v4mapped(src_saddr,
709 (struct in6_addr *)sgid);
710 ipv6_addr_set_v4mapped(dst_saddr,
711 (struct in6_addr *)dgid);
713 } else if (net_type == RDMA_NETWORK_IPV6 ||
714 net_type == RDMA_NETWORK_IB) {
715 *dgid = hdr->ibgrh.dgid;
716 *sgid = hdr->ibgrh.sgid;
722 EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
724 /* Resolve destination mac address and hop limit for unicast destination
725 * GID entry, considering the source GID entry as well.
726 * ah_attribute must have have valid port_num, sgid_index.
728 static int ib_resolve_unicast_gid_dmac(struct ib_device *device,
729 struct rdma_ah_attr *ah_attr)
731 struct ib_global_route *grh = rdma_ah_retrieve_grh(ah_attr);
732 const struct ib_gid_attr *sgid_attr = grh->sgid_attr;
733 int hop_limit = 0xff;
736 /* If destination is link local and source GID is RoCEv1,
737 * IP stack is not used.
739 if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) &&
740 sgid_attr->gid_type == IB_GID_TYPE_ROCE) {
741 rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
746 ret = rdma_addr_find_l2_eth_by_grh(&sgid_attr->gid, &grh->dgid,
748 sgid_attr, &hop_limit);
750 grh->hop_limit = hop_limit;
755 * This function initializes address handle attributes from the incoming packet.
756 * Incoming packet has dgid of the receiver node on which this code is
757 * getting executed and, sgid contains the GID of the sender.
759 * When resolving mac address of destination, the arrived dgid is used
760 * as sgid and, sgid is used as dgid because sgid contains destinations
761 * GID whom to respond to.
763 * On success the caller is responsible to call rdma_destroy_ah_attr on the
766 int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num,
767 const struct ib_wc *wc, const struct ib_grh *grh,
768 struct rdma_ah_attr *ah_attr)
772 enum rdma_network_type net_type = RDMA_NETWORK_IB;
773 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
774 const struct ib_gid_attr *sgid_attr;
781 memset(ah_attr, 0, sizeof *ah_attr);
782 ah_attr->type = rdma_ah_find_type(device, port_num);
783 if (rdma_cap_eth_ah(device, port_num)) {
784 if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
785 net_type = wc->network_hdr_type;
787 net_type = ib_get_net_type_by_grh(device, port_num, grh);
788 gid_type = ib_network_to_gid_type(net_type);
790 ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
795 rdma_ah_set_sl(ah_attr, wc->sl);
796 rdma_ah_set_port_num(ah_attr, port_num);
798 if (rdma_protocol_roce(device, port_num)) {
799 u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
800 wc->vlan_id : 0xffff;
802 if (!(wc->wc_flags & IB_WC_GRH))
805 sgid_attr = get_sgid_attr_from_eth(device, port_num,
808 if (IS_ERR(sgid_attr))
809 return PTR_ERR(sgid_attr);
811 flow_class = be32_to_cpu(grh->version_tclass_flow);
812 rdma_move_grh_sgid_attr(ah_attr,
814 flow_class & 0xFFFFF,
816 (flow_class >> 20) & 0xFF,
819 ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
821 rdma_destroy_ah_attr(ah_attr);
825 rdma_ah_set_dlid(ah_attr, wc->slid);
826 rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
828 if ((wc->wc_flags & IB_WC_GRH) == 0)
831 if (dgid.global.interface_id !=
832 cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
833 sgid_attr = rdma_find_gid_by_port(
834 device, &dgid, IB_GID_TYPE_IB, port_num, NULL);
836 sgid_attr = rdma_get_gid_attr(device, port_num, 0);
838 if (IS_ERR(sgid_attr))
839 return PTR_ERR(sgid_attr);
840 flow_class = be32_to_cpu(grh->version_tclass_flow);
841 rdma_move_grh_sgid_attr(ah_attr,
843 flow_class & 0xFFFFF,
845 (flow_class >> 20) & 0xFF,
851 EXPORT_SYMBOL(ib_init_ah_attr_from_wc);
854 * rdma_move_grh_sgid_attr - Sets the sgid attribute of GRH, taking ownership
857 * @attr: Pointer to AH attribute structure
858 * @dgid: Destination GID
859 * @flow_label: Flow label
860 * @hop_limit: Hop limit
861 * @traffic_class: traffic class
862 * @sgid_attr: Pointer to SGID attribute
864 * This takes ownership of the sgid_attr reference. The caller must ensure
865 * rdma_destroy_ah_attr() is called before destroying the rdma_ah_attr after
866 * calling this function.
868 void rdma_move_grh_sgid_attr(struct rdma_ah_attr *attr, union ib_gid *dgid,
869 u32 flow_label, u8 hop_limit, u8 traffic_class,
870 const struct ib_gid_attr *sgid_attr)
872 rdma_ah_set_grh(attr, dgid, flow_label, sgid_attr->index, hop_limit,
874 attr->grh.sgid_attr = sgid_attr;
876 EXPORT_SYMBOL(rdma_move_grh_sgid_attr);
879 * rdma_destroy_ah_attr - Release reference to SGID attribute of
881 * @ah_attr: Pointer to ah attribute
883 * Release reference to the SGID attribute of the ah attribute if it is
884 * non NULL. It is safe to call this multiple times, and safe to call it on
885 * a zero initialized ah_attr.
887 void rdma_destroy_ah_attr(struct rdma_ah_attr *ah_attr)
889 if (ah_attr->grh.sgid_attr) {
890 rdma_put_gid_attr(ah_attr->grh.sgid_attr);
891 ah_attr->grh.sgid_attr = NULL;
894 EXPORT_SYMBOL(rdma_destroy_ah_attr);
896 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
897 const struct ib_grh *grh, u8 port_num)
899 struct rdma_ah_attr ah_attr;
903 ret = ib_init_ah_attr_from_wc(pd->device, port_num, wc, grh, &ah_attr);
907 ah = rdma_create_ah(pd, &ah_attr, RDMA_CREATE_AH_SLEEPABLE);
909 rdma_destroy_ah_attr(&ah_attr);
912 EXPORT_SYMBOL(ib_create_ah_from_wc);
914 int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
916 const struct ib_gid_attr *old_sgid_attr;
919 if (ah->type != ah_attr->type)
922 ret = rdma_fill_sgid_attr(ah->device, ah_attr, &old_sgid_attr);
926 ret = ah->device->ops.modify_ah ?
927 ah->device->ops.modify_ah(ah, ah_attr) :
930 ah->sgid_attr = rdma_update_sgid_attr(ah_attr, ah->sgid_attr);
931 rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
934 EXPORT_SYMBOL(rdma_modify_ah);
936 int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
938 ah_attr->grh.sgid_attr = NULL;
940 return ah->device->ops.query_ah ?
941 ah->device->ops.query_ah(ah, ah_attr) :
944 EXPORT_SYMBOL(rdma_query_ah);
946 int rdma_destroy_ah_user(struct ib_ah *ah, u32 flags, struct ib_udata *udata)
948 const struct ib_gid_attr *sgid_attr = ah->sgid_attr;
951 might_sleep_if(flags & RDMA_DESTROY_AH_SLEEPABLE);
955 ah->device->ops.destroy_ah(ah, flags);
956 atomic_dec(&pd->usecnt);
958 rdma_put_gid_attr(sgid_attr);
963 EXPORT_SYMBOL(rdma_destroy_ah_user);
965 /* Shared receive queues */
967 struct ib_srq *ib_create_srq(struct ib_pd *pd,
968 struct ib_srq_init_attr *srq_init_attr)
973 if (!pd->device->ops.create_srq)
974 return ERR_PTR(-EOPNOTSUPP);
976 srq = rdma_zalloc_drv_obj(pd->device, ib_srq);
978 return ERR_PTR(-ENOMEM);
980 srq->device = pd->device;
982 srq->event_handler = srq_init_attr->event_handler;
983 srq->srq_context = srq_init_attr->srq_context;
984 srq->srq_type = srq_init_attr->srq_type;
986 if (ib_srq_has_cq(srq->srq_type)) {
987 srq->ext.cq = srq_init_attr->ext.cq;
988 atomic_inc(&srq->ext.cq->usecnt);
990 if (srq->srq_type == IB_SRQT_XRC) {
991 srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
992 atomic_inc(&srq->ext.xrc.xrcd->usecnt);
994 atomic_inc(&pd->usecnt);
996 ret = pd->device->ops.create_srq(srq, srq_init_attr, NULL);
998 atomic_dec(&srq->pd->usecnt);
999 if (srq->srq_type == IB_SRQT_XRC)
1000 atomic_dec(&srq->ext.xrc.xrcd->usecnt);
1001 if (ib_srq_has_cq(srq->srq_type))
1002 atomic_dec(&srq->ext.cq->usecnt);
1004 return ERR_PTR(ret);
1009 EXPORT_SYMBOL(ib_create_srq);
1011 int ib_modify_srq(struct ib_srq *srq,
1012 struct ib_srq_attr *srq_attr,
1013 enum ib_srq_attr_mask srq_attr_mask)
1015 return srq->device->ops.modify_srq ?
1016 srq->device->ops.modify_srq(srq, srq_attr, srq_attr_mask,
1017 NULL) : -EOPNOTSUPP;
1019 EXPORT_SYMBOL(ib_modify_srq);
1021 int ib_query_srq(struct ib_srq *srq,
1022 struct ib_srq_attr *srq_attr)
1024 return srq->device->ops.query_srq ?
1025 srq->device->ops.query_srq(srq, srq_attr) : -EOPNOTSUPP;
1027 EXPORT_SYMBOL(ib_query_srq);
1029 int ib_destroy_srq_user(struct ib_srq *srq, struct ib_udata *udata)
1031 if (atomic_read(&srq->usecnt))
1034 srq->device->ops.destroy_srq(srq, udata);
1036 atomic_dec(&srq->pd->usecnt);
1037 if (srq->srq_type == IB_SRQT_XRC)
1038 atomic_dec(&srq->ext.xrc.xrcd->usecnt);
1039 if (ib_srq_has_cq(srq->srq_type))
1040 atomic_dec(&srq->ext.cq->usecnt);
1045 EXPORT_SYMBOL(ib_destroy_srq_user);
1049 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
1051 struct ib_qp *qp = context;
1052 unsigned long flags;
1054 spin_lock_irqsave(&qp->device->event_handler_lock, flags);
1055 list_for_each_entry(event->element.qp, &qp->open_list, open_list)
1056 if (event->element.qp->event_handler)
1057 event->element.qp->event_handler(event, event->element.qp->qp_context);
1058 spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
1061 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
1063 mutex_lock(&xrcd->tgt_qp_mutex);
1064 list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
1065 mutex_unlock(&xrcd->tgt_qp_mutex);
1068 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
1069 void (*event_handler)(struct ib_event *, void *),
1073 unsigned long flags;
1076 qp = kzalloc(sizeof *qp, GFP_KERNEL);
1078 return ERR_PTR(-ENOMEM);
1080 qp->real_qp = real_qp;
1081 err = ib_open_shared_qp_security(qp, real_qp->device);
1084 return ERR_PTR(err);
1087 qp->real_qp = real_qp;
1088 atomic_inc(&real_qp->usecnt);
1089 qp->device = real_qp->device;
1090 qp->event_handler = event_handler;
1091 qp->qp_context = qp_context;
1092 qp->qp_num = real_qp->qp_num;
1093 qp->qp_type = real_qp->qp_type;
1095 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1096 list_add(&qp->open_list, &real_qp->open_list);
1097 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1102 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
1103 struct ib_qp_open_attr *qp_open_attr)
1105 struct ib_qp *qp, *real_qp;
1107 if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
1108 return ERR_PTR(-EINVAL);
1110 qp = ERR_PTR(-EINVAL);
1111 mutex_lock(&xrcd->tgt_qp_mutex);
1112 list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
1113 if (real_qp->qp_num == qp_open_attr->qp_num) {
1114 qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
1115 qp_open_attr->qp_context);
1119 mutex_unlock(&xrcd->tgt_qp_mutex);
1122 EXPORT_SYMBOL(ib_open_qp);
1124 static struct ib_qp *create_xrc_qp_user(struct ib_qp *qp,
1125 struct ib_qp_init_attr *qp_init_attr,
1126 struct ib_udata *udata)
1128 struct ib_qp *real_qp = qp;
1130 qp->event_handler = __ib_shared_qp_event_handler;
1131 qp->qp_context = qp;
1133 qp->send_cq = qp->recv_cq = NULL;
1135 qp->xrcd = qp_init_attr->xrcd;
1136 atomic_inc(&qp_init_attr->xrcd->usecnt);
1137 INIT_LIST_HEAD(&qp->open_list);
1139 qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
1140 qp_init_attr->qp_context);
1144 __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
1148 struct ib_qp *ib_create_qp_user(struct ib_pd *pd,
1149 struct ib_qp_init_attr *qp_init_attr,
1150 struct ib_udata *udata)
1152 struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
1156 if (qp_init_attr->rwq_ind_tbl &&
1157 (qp_init_attr->recv_cq ||
1158 qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
1159 qp_init_attr->cap.max_recv_sge))
1160 return ERR_PTR(-EINVAL);
1162 if ((qp_init_attr->create_flags & IB_QP_CREATE_INTEGRITY_EN) &&
1163 !(device->attrs.device_cap_flags & IB_DEVICE_INTEGRITY_HANDOVER))
1164 return ERR_PTR(-EINVAL);
1167 * If the callers is using the RDMA API calculate the resources
1168 * needed for the RDMA READ/WRITE operations.
1170 * Note that these callers need to pass in a port number.
1172 if (qp_init_attr->cap.max_rdma_ctxs)
1173 rdma_rw_init_qp(device, qp_init_attr);
1175 qp = _ib_create_qp(device, pd, qp_init_attr, NULL, NULL);
1179 ret = ib_create_qp_security(qp, device);
1183 qp->qp_type = qp_init_attr->qp_type;
1184 qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
1186 atomic_set(&qp->usecnt, 0);
1188 spin_lock_init(&qp->mr_lock);
1189 INIT_LIST_HEAD(&qp->rdma_mrs);
1190 INIT_LIST_HEAD(&qp->sig_mrs);
1193 if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) {
1194 struct ib_qp *xrc_qp =
1195 create_xrc_qp_user(qp, qp_init_attr, udata);
1197 if (IS_ERR(xrc_qp)) {
1198 ret = PTR_ERR(xrc_qp);
1204 qp->event_handler = qp_init_attr->event_handler;
1205 qp->qp_context = qp_init_attr->qp_context;
1206 if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
1210 qp->recv_cq = qp_init_attr->recv_cq;
1211 if (qp_init_attr->recv_cq)
1212 atomic_inc(&qp_init_attr->recv_cq->usecnt);
1213 qp->srq = qp_init_attr->srq;
1215 atomic_inc(&qp_init_attr->srq->usecnt);
1218 qp->send_cq = qp_init_attr->send_cq;
1221 atomic_inc(&pd->usecnt);
1222 if (qp_init_attr->send_cq)
1223 atomic_inc(&qp_init_attr->send_cq->usecnt);
1224 if (qp_init_attr->rwq_ind_tbl)
1225 atomic_inc(&qp->rwq_ind_tbl->usecnt);
1227 if (qp_init_attr->cap.max_rdma_ctxs) {
1228 ret = rdma_rw_init_mrs(qp, qp_init_attr);
1234 * Note: all hw drivers guarantee that max_send_sge is lower than
1235 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
1236 * max_send_sge <= max_sge_rd.
1238 qp->max_write_sge = qp_init_attr->cap.max_send_sge;
1239 qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
1240 device->attrs.max_sge_rd);
1241 if (qp_init_attr->create_flags & IB_QP_CREATE_INTEGRITY_EN)
1242 qp->integrity_en = true;
1248 return ERR_PTR(ret);
1251 EXPORT_SYMBOL(ib_create_qp_user);
1253 static const struct {
1255 enum ib_qp_attr_mask req_param[IB_QPT_MAX];
1256 enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
1257 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
1259 [IB_QPS_RESET] = { .valid = 1 },
1263 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1266 [IB_QPT_RAW_PACKET] = IB_QP_PORT,
1267 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
1269 IB_QP_ACCESS_FLAGS),
1270 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
1272 IB_QP_ACCESS_FLAGS),
1273 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
1275 IB_QP_ACCESS_FLAGS),
1276 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
1278 IB_QP_ACCESS_FLAGS),
1279 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1281 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1287 [IB_QPS_RESET] = { .valid = 1 },
1288 [IB_QPS_ERR] = { .valid = 1 },
1292 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1295 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
1297 IB_QP_ACCESS_FLAGS),
1298 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
1300 IB_QP_ACCESS_FLAGS),
1301 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
1303 IB_QP_ACCESS_FLAGS),
1304 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
1306 IB_QP_ACCESS_FLAGS),
1307 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1309 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1316 [IB_QPT_UC] = (IB_QP_AV |
1320 [IB_QPT_RC] = (IB_QP_AV |
1324 IB_QP_MAX_DEST_RD_ATOMIC |
1325 IB_QP_MIN_RNR_TIMER),
1326 [IB_QPT_XRC_INI] = (IB_QP_AV |
1330 [IB_QPT_XRC_TGT] = (IB_QP_AV |
1334 IB_QP_MAX_DEST_RD_ATOMIC |
1335 IB_QP_MIN_RNR_TIMER),
1338 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1340 [IB_QPT_UC] = (IB_QP_ALT_PATH |
1341 IB_QP_ACCESS_FLAGS |
1343 [IB_QPT_RC] = (IB_QP_ALT_PATH |
1344 IB_QP_ACCESS_FLAGS |
1346 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
1347 IB_QP_ACCESS_FLAGS |
1349 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
1350 IB_QP_ACCESS_FLAGS |
1352 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1354 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1360 [IB_QPS_RESET] = { .valid = 1 },
1361 [IB_QPS_ERR] = { .valid = 1 },
1365 [IB_QPT_UD] = IB_QP_SQ_PSN,
1366 [IB_QPT_UC] = IB_QP_SQ_PSN,
1367 [IB_QPT_RC] = (IB_QP_TIMEOUT |
1371 IB_QP_MAX_QP_RD_ATOMIC),
1372 [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
1376 IB_QP_MAX_QP_RD_ATOMIC),
1377 [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
1379 [IB_QPT_SMI] = IB_QP_SQ_PSN,
1380 [IB_QPT_GSI] = IB_QP_SQ_PSN,
1383 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1385 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1387 IB_QP_ACCESS_FLAGS |
1388 IB_QP_PATH_MIG_STATE),
1389 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1391 IB_QP_ACCESS_FLAGS |
1392 IB_QP_MIN_RNR_TIMER |
1393 IB_QP_PATH_MIG_STATE),
1394 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1396 IB_QP_ACCESS_FLAGS |
1397 IB_QP_PATH_MIG_STATE),
1398 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1400 IB_QP_ACCESS_FLAGS |
1401 IB_QP_MIN_RNR_TIMER |
1402 IB_QP_PATH_MIG_STATE),
1403 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1405 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1407 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1412 [IB_QPS_RESET] = { .valid = 1 },
1413 [IB_QPS_ERR] = { .valid = 1 },
1417 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1419 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1420 IB_QP_ACCESS_FLAGS |
1422 IB_QP_PATH_MIG_STATE),
1423 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1424 IB_QP_ACCESS_FLAGS |
1426 IB_QP_PATH_MIG_STATE |
1427 IB_QP_MIN_RNR_TIMER),
1428 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1429 IB_QP_ACCESS_FLAGS |
1431 IB_QP_PATH_MIG_STATE),
1432 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1433 IB_QP_ACCESS_FLAGS |
1435 IB_QP_PATH_MIG_STATE |
1436 IB_QP_MIN_RNR_TIMER),
1437 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1439 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1441 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1447 [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1448 [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1449 [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1450 [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1451 [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1452 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1453 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1458 [IB_QPS_RESET] = { .valid = 1 },
1459 [IB_QPS_ERR] = { .valid = 1 },
1463 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1465 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1467 IB_QP_ACCESS_FLAGS |
1468 IB_QP_PATH_MIG_STATE),
1469 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1471 IB_QP_ACCESS_FLAGS |
1472 IB_QP_MIN_RNR_TIMER |
1473 IB_QP_PATH_MIG_STATE),
1474 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1476 IB_QP_ACCESS_FLAGS |
1477 IB_QP_PATH_MIG_STATE),
1478 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1480 IB_QP_ACCESS_FLAGS |
1481 IB_QP_MIN_RNR_TIMER |
1482 IB_QP_PATH_MIG_STATE),
1483 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1485 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1492 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1494 [IB_QPT_UC] = (IB_QP_AV |
1496 IB_QP_ACCESS_FLAGS |
1498 IB_QP_PATH_MIG_STATE),
1499 [IB_QPT_RC] = (IB_QP_PORT |
1504 IB_QP_MAX_QP_RD_ATOMIC |
1505 IB_QP_MAX_DEST_RD_ATOMIC |
1507 IB_QP_ACCESS_FLAGS |
1509 IB_QP_MIN_RNR_TIMER |
1510 IB_QP_PATH_MIG_STATE),
1511 [IB_QPT_XRC_INI] = (IB_QP_PORT |
1516 IB_QP_MAX_QP_RD_ATOMIC |
1518 IB_QP_ACCESS_FLAGS |
1520 IB_QP_PATH_MIG_STATE),
1521 [IB_QPT_XRC_TGT] = (IB_QP_PORT |
1524 IB_QP_MAX_DEST_RD_ATOMIC |
1526 IB_QP_ACCESS_FLAGS |
1528 IB_QP_MIN_RNR_TIMER |
1529 IB_QP_PATH_MIG_STATE),
1530 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1532 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1538 [IB_QPS_RESET] = { .valid = 1 },
1539 [IB_QPS_ERR] = { .valid = 1 },
1543 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1545 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1546 IB_QP_ACCESS_FLAGS),
1547 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1549 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1555 [IB_QPS_RESET] = { .valid = 1 },
1556 [IB_QPS_ERR] = { .valid = 1 }
1560 bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1561 enum ib_qp_type type, enum ib_qp_attr_mask mask)
1563 enum ib_qp_attr_mask req_param, opt_param;
1565 if (mask & IB_QP_CUR_STATE &&
1566 cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1567 cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1570 if (!qp_state_table[cur_state][next_state].valid)
1573 req_param = qp_state_table[cur_state][next_state].req_param[type];
1574 opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1576 if ((mask & req_param) != req_param)
1579 if (mask & ~(req_param | opt_param | IB_QP_STATE))
1584 EXPORT_SYMBOL(ib_modify_qp_is_ok);
1587 * ib_resolve_eth_dmac - Resolve destination mac address
1588 * @device: Device to consider
1589 * @ah_attr: address handle attribute which describes the
1590 * source and destination parameters
1591 * ib_resolve_eth_dmac() resolves destination mac address and L3 hop limit It
1592 * returns 0 on success or appropriate error code. It initializes the
1593 * necessary ah_attr fields when call is successful.
1595 static int ib_resolve_eth_dmac(struct ib_device *device,
1596 struct rdma_ah_attr *ah_attr)
1600 if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1601 if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1604 memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
1605 ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
1607 ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
1608 (char *)ah_attr->roce.dmac);
1611 ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
1616 static bool is_qp_type_connected(const struct ib_qp *qp)
1618 return (qp->qp_type == IB_QPT_UC ||
1619 qp->qp_type == IB_QPT_RC ||
1620 qp->qp_type == IB_QPT_XRC_INI ||
1621 qp->qp_type == IB_QPT_XRC_TGT);
1625 * IB core internal function to perform QP attributes modification.
1627 static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
1628 int attr_mask, struct ib_udata *udata)
1630 u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1631 const struct ib_gid_attr *old_sgid_attr_av;
1632 const struct ib_gid_attr *old_sgid_attr_alt_av;
1635 if (attr_mask & IB_QP_AV) {
1636 ret = rdma_fill_sgid_attr(qp->device, &attr->ah_attr,
1641 if (attr_mask & IB_QP_ALT_PATH) {
1643 * FIXME: This does not track the migration state, so if the
1644 * user loads a new alternate path after the HW has migrated
1645 * from primary->alternate we will keep the wrong
1646 * references. This is OK for IB because the reference
1647 * counting does not serve any functional purpose.
1649 ret = rdma_fill_sgid_attr(qp->device, &attr->alt_ah_attr,
1650 &old_sgid_attr_alt_av);
1655 * Today the core code can only handle alternate paths and APM
1656 * for IB. Ban them in roce mode.
1658 if (!(rdma_protocol_ib(qp->device,
1659 attr->alt_ah_attr.port_num) &&
1660 rdma_protocol_ib(qp->device, port))) {
1667 * If the user provided the qp_attr then we have to resolve it. Kernel
1668 * users have to provide already resolved rdma_ah_attr's
1670 if (udata && (attr_mask & IB_QP_AV) &&
1671 attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE &&
1672 is_qp_type_connected(qp)) {
1673 ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr);
1678 if (rdma_ib_or_roce(qp->device, port)) {
1679 if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) {
1680 dev_warn(&qp->device->dev,
1681 "%s rq_psn overflow, masking to 24 bits\n",
1683 attr->rq_psn &= 0xffffff;
1686 if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) {
1687 dev_warn(&qp->device->dev,
1688 " %s sq_psn overflow, masking to 24 bits\n",
1690 attr->sq_psn &= 0xffffff;
1695 * Bind this qp to a counter automatically based on the rdma counter
1696 * rules. This only set in RST2INIT with port specified
1698 if (!qp->counter && (attr_mask & IB_QP_PORT) &&
1699 ((attr_mask & IB_QP_STATE) && attr->qp_state == IB_QPS_INIT))
1700 rdma_counter_bind_qp_auto(qp, attr->port_num);
1702 ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
1706 if (attr_mask & IB_QP_PORT)
1707 qp->port = attr->port_num;
1708 if (attr_mask & IB_QP_AV)
1710 rdma_update_sgid_attr(&attr->ah_attr, qp->av_sgid_attr);
1711 if (attr_mask & IB_QP_ALT_PATH)
1712 qp->alt_path_sgid_attr = rdma_update_sgid_attr(
1713 &attr->alt_ah_attr, qp->alt_path_sgid_attr);
1716 if (attr_mask & IB_QP_ALT_PATH)
1717 rdma_unfill_sgid_attr(&attr->alt_ah_attr, old_sgid_attr_alt_av);
1719 if (attr_mask & IB_QP_AV)
1720 rdma_unfill_sgid_attr(&attr->ah_attr, old_sgid_attr_av);
1725 * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
1726 * @ib_qp: The QP to modify.
1727 * @attr: On input, specifies the QP attributes to modify. On output,
1728 * the current values of selected QP attributes are returned.
1729 * @attr_mask: A bit-mask used to specify which attributes of the QP
1730 * are being modified.
1731 * @udata: pointer to user's input output buffer information
1732 * are being modified.
1733 * It returns 0 on success and returns appropriate error code on error.
1735 int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
1736 int attr_mask, struct ib_udata *udata)
1738 return _ib_modify_qp(ib_qp->real_qp, attr, attr_mask, udata);
1740 EXPORT_SYMBOL(ib_modify_qp_with_udata);
1742 int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width)
1746 struct net_device *netdev;
1747 struct ethtool_link_ksettings lksettings;
1749 if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
1752 netdev = ib_device_get_netdev(dev, port_num);
1757 rc = __ethtool_get_link_ksettings(netdev, &lksettings);
1763 netdev_speed = lksettings.base.speed;
1765 netdev_speed = SPEED_1000;
1766 pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name,
1770 if (netdev_speed <= SPEED_1000) {
1771 *width = IB_WIDTH_1X;
1772 *speed = IB_SPEED_SDR;
1773 } else if (netdev_speed <= SPEED_10000) {
1774 *width = IB_WIDTH_1X;
1775 *speed = IB_SPEED_FDR10;
1776 } else if (netdev_speed <= SPEED_20000) {
1777 *width = IB_WIDTH_4X;
1778 *speed = IB_SPEED_DDR;
1779 } else if (netdev_speed <= SPEED_25000) {
1780 *width = IB_WIDTH_1X;
1781 *speed = IB_SPEED_EDR;
1782 } else if (netdev_speed <= SPEED_40000) {
1783 *width = IB_WIDTH_4X;
1784 *speed = IB_SPEED_FDR10;
1786 *width = IB_WIDTH_4X;
1787 *speed = IB_SPEED_EDR;
1792 EXPORT_SYMBOL(ib_get_eth_speed);
1794 int ib_modify_qp(struct ib_qp *qp,
1795 struct ib_qp_attr *qp_attr,
1798 return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
1800 EXPORT_SYMBOL(ib_modify_qp);
1802 int ib_query_qp(struct ib_qp *qp,
1803 struct ib_qp_attr *qp_attr,
1805 struct ib_qp_init_attr *qp_init_attr)
1807 qp_attr->ah_attr.grh.sgid_attr = NULL;
1808 qp_attr->alt_ah_attr.grh.sgid_attr = NULL;
1810 return qp->device->ops.query_qp ?
1811 qp->device->ops.query_qp(qp->real_qp, qp_attr, qp_attr_mask,
1812 qp_init_attr) : -EOPNOTSUPP;
1814 EXPORT_SYMBOL(ib_query_qp);
1816 int ib_close_qp(struct ib_qp *qp)
1818 struct ib_qp *real_qp;
1819 unsigned long flags;
1821 real_qp = qp->real_qp;
1825 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1826 list_del(&qp->open_list);
1827 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1829 atomic_dec(&real_qp->usecnt);
1831 ib_close_shared_qp_security(qp->qp_sec);
1836 EXPORT_SYMBOL(ib_close_qp);
1838 static int __ib_destroy_shared_qp(struct ib_qp *qp)
1840 struct ib_xrcd *xrcd;
1841 struct ib_qp *real_qp;
1844 real_qp = qp->real_qp;
1845 xrcd = real_qp->xrcd;
1847 mutex_lock(&xrcd->tgt_qp_mutex);
1849 if (atomic_read(&real_qp->usecnt) == 0)
1850 list_del(&real_qp->xrcd_list);
1853 mutex_unlock(&xrcd->tgt_qp_mutex);
1856 ret = ib_destroy_qp(real_qp);
1858 atomic_dec(&xrcd->usecnt);
1860 __ib_insert_xrcd_qp(xrcd, real_qp);
1866 int ib_destroy_qp_user(struct ib_qp *qp, struct ib_udata *udata)
1868 const struct ib_gid_attr *alt_path_sgid_attr = qp->alt_path_sgid_attr;
1869 const struct ib_gid_attr *av_sgid_attr = qp->av_sgid_attr;
1871 struct ib_cq *scq, *rcq;
1873 struct ib_rwq_ind_table *ind_tbl;
1874 struct ib_qp_security *sec;
1877 WARN_ON_ONCE(qp->mrs_used > 0);
1879 if (atomic_read(&qp->usecnt))
1882 if (qp->real_qp != qp)
1883 return __ib_destroy_shared_qp(qp);
1889 ind_tbl = qp->rwq_ind_tbl;
1892 ib_destroy_qp_security_begin(sec);
1895 rdma_rw_cleanup_mrs(qp);
1897 rdma_counter_unbind_qp(qp, true);
1898 rdma_restrack_del(&qp->res);
1899 ret = qp->device->ops.destroy_qp(qp, udata);
1901 if (alt_path_sgid_attr)
1902 rdma_put_gid_attr(alt_path_sgid_attr);
1904 rdma_put_gid_attr(av_sgid_attr);
1906 atomic_dec(&pd->usecnt);
1908 atomic_dec(&scq->usecnt);
1910 atomic_dec(&rcq->usecnt);
1912 atomic_dec(&srq->usecnt);
1914 atomic_dec(&ind_tbl->usecnt);
1916 ib_destroy_qp_security_end(sec);
1919 ib_destroy_qp_security_abort(sec);
1924 EXPORT_SYMBOL(ib_destroy_qp_user);
1926 /* Completion queues */
1928 struct ib_cq *__ib_create_cq(struct ib_device *device,
1929 ib_comp_handler comp_handler,
1930 void (*event_handler)(struct ib_event *, void *),
1932 const struct ib_cq_init_attr *cq_attr,
1938 cq = rdma_zalloc_drv_obj(device, ib_cq);
1940 return ERR_PTR(-ENOMEM);
1942 cq->device = device;
1944 cq->comp_handler = comp_handler;
1945 cq->event_handler = event_handler;
1946 cq->cq_context = cq_context;
1947 atomic_set(&cq->usecnt, 0);
1948 cq->res.type = RDMA_RESTRACK_CQ;
1949 rdma_restrack_set_task(&cq->res, caller);
1951 ret = device->ops.create_cq(cq, cq_attr, NULL);
1954 return ERR_PTR(ret);
1957 rdma_restrack_kadd(&cq->res);
1960 EXPORT_SYMBOL(__ib_create_cq);
1962 int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1964 return cq->device->ops.modify_cq ?
1965 cq->device->ops.modify_cq(cq, cq_count,
1966 cq_period) : -EOPNOTSUPP;
1968 EXPORT_SYMBOL(rdma_set_cq_moderation);
1970 int ib_destroy_cq_user(struct ib_cq *cq, struct ib_udata *udata)
1972 if (atomic_read(&cq->usecnt))
1975 rdma_restrack_del(&cq->res);
1976 cq->device->ops.destroy_cq(cq, udata);
1980 EXPORT_SYMBOL(ib_destroy_cq_user);
1982 int ib_resize_cq(struct ib_cq *cq, int cqe)
1984 return cq->device->ops.resize_cq ?
1985 cq->device->ops.resize_cq(cq, cqe, NULL) : -EOPNOTSUPP;
1987 EXPORT_SYMBOL(ib_resize_cq);
1989 /* Memory regions */
1991 int ib_dereg_mr_user(struct ib_mr *mr, struct ib_udata *udata)
1993 struct ib_pd *pd = mr->pd;
1994 struct ib_dm *dm = mr->dm;
1995 struct ib_sig_attrs *sig_attrs = mr->sig_attrs;
1998 rdma_restrack_del(&mr->res);
1999 ret = mr->device->ops.dereg_mr(mr, udata);
2001 atomic_dec(&pd->usecnt);
2003 atomic_dec(&dm->usecnt);
2009 EXPORT_SYMBOL(ib_dereg_mr_user);
2012 * ib_alloc_mr_user() - Allocates a memory region
2013 * @pd: protection domain associated with the region
2014 * @mr_type: memory region type
2015 * @max_num_sg: maximum sg entries available for registration.
2016 * @udata: user data or null for kernel objects
2019 * Memory registeration page/sg lists must not exceed max_num_sg.
2020 * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
2021 * max_num_sg * used_page_size.
2024 struct ib_mr *ib_alloc_mr_user(struct ib_pd *pd, enum ib_mr_type mr_type,
2025 u32 max_num_sg, struct ib_udata *udata)
2029 if (!pd->device->ops.alloc_mr)
2030 return ERR_PTR(-EOPNOTSUPP);
2032 if (WARN_ON_ONCE(mr_type == IB_MR_TYPE_INTEGRITY))
2033 return ERR_PTR(-EINVAL);
2035 mr = pd->device->ops.alloc_mr(pd, mr_type, max_num_sg, udata);
2037 mr->device = pd->device;
2041 atomic_inc(&pd->usecnt);
2042 mr->need_inval = false;
2043 mr->res.type = RDMA_RESTRACK_MR;
2044 rdma_restrack_kadd(&mr->res);
2046 mr->sig_attrs = NULL;
2051 EXPORT_SYMBOL(ib_alloc_mr_user);
2054 * ib_alloc_mr_integrity() - Allocates an integrity memory region
2055 * @pd: protection domain associated with the region
2056 * @max_num_data_sg: maximum data sg entries available for registration
2057 * @max_num_meta_sg: maximum metadata sg entries available for
2061 * Memory registration page/sg lists must not exceed max_num_sg,
2062 * also the integrity page/sg lists must not exceed max_num_meta_sg.
2065 struct ib_mr *ib_alloc_mr_integrity(struct ib_pd *pd,
2066 u32 max_num_data_sg,
2067 u32 max_num_meta_sg)
2070 struct ib_sig_attrs *sig_attrs;
2072 if (!pd->device->ops.alloc_mr_integrity ||
2073 !pd->device->ops.map_mr_sg_pi)
2074 return ERR_PTR(-EOPNOTSUPP);
2076 if (!max_num_meta_sg)
2077 return ERR_PTR(-EINVAL);
2079 sig_attrs = kzalloc(sizeof(struct ib_sig_attrs), GFP_KERNEL);
2081 return ERR_PTR(-ENOMEM);
2083 mr = pd->device->ops.alloc_mr_integrity(pd, max_num_data_sg,
2090 mr->device = pd->device;
2094 atomic_inc(&pd->usecnt);
2095 mr->need_inval = false;
2096 mr->res.type = RDMA_RESTRACK_MR;
2097 rdma_restrack_kadd(&mr->res);
2098 mr->type = IB_MR_TYPE_INTEGRITY;
2099 mr->sig_attrs = sig_attrs;
2103 EXPORT_SYMBOL(ib_alloc_mr_integrity);
2105 /* "Fast" memory regions */
2107 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
2108 int mr_access_flags,
2109 struct ib_fmr_attr *fmr_attr)
2113 if (!pd->device->ops.alloc_fmr)
2114 return ERR_PTR(-EOPNOTSUPP);
2116 fmr = pd->device->ops.alloc_fmr(pd, mr_access_flags, fmr_attr);
2118 fmr->device = pd->device;
2120 atomic_inc(&pd->usecnt);
2125 EXPORT_SYMBOL(ib_alloc_fmr);
2127 int ib_unmap_fmr(struct list_head *fmr_list)
2131 if (list_empty(fmr_list))
2134 fmr = list_entry(fmr_list->next, struct ib_fmr, list);
2135 return fmr->device->ops.unmap_fmr(fmr_list);
2137 EXPORT_SYMBOL(ib_unmap_fmr);
2139 int ib_dealloc_fmr(struct ib_fmr *fmr)
2145 ret = fmr->device->ops.dealloc_fmr(fmr);
2147 atomic_dec(&pd->usecnt);
2151 EXPORT_SYMBOL(ib_dealloc_fmr);
2153 /* Multicast groups */
2155 static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
2157 struct ib_qp_init_attr init_attr = {};
2158 struct ib_qp_attr attr = {};
2159 int num_eth_ports = 0;
2162 /* If QP state >= init, it is assigned to a port and we can check this
2165 if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
2166 if (attr.qp_state >= IB_QPS_INIT) {
2167 if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
2168 IB_LINK_LAYER_INFINIBAND)
2174 /* Can't get a quick answer, iterate over all ports */
2175 for (port = 0; port < qp->device->phys_port_cnt; port++)
2176 if (rdma_port_get_link_layer(qp->device, port) !=
2177 IB_LINK_LAYER_INFINIBAND)
2180 /* If we have at lease one Ethernet port, RoCE annex declares that
2181 * multicast LID should be ignored. We can't tell at this step if the
2182 * QP belongs to an IB or Ethernet port.
2187 /* If all the ports are IB, we can check according to IB spec. */
2189 return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
2190 lid == be16_to_cpu(IB_LID_PERMISSIVE));
2193 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
2197 if (!qp->device->ops.attach_mcast)
2200 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2201 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
2204 ret = qp->device->ops.attach_mcast(qp, gid, lid);
2206 atomic_inc(&qp->usecnt);
2209 EXPORT_SYMBOL(ib_attach_mcast);
2211 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
2215 if (!qp->device->ops.detach_mcast)
2218 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2219 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
2222 ret = qp->device->ops.detach_mcast(qp, gid, lid);
2224 atomic_dec(&qp->usecnt);
2227 EXPORT_SYMBOL(ib_detach_mcast);
2229 struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller)
2231 struct ib_xrcd *xrcd;
2233 if (!device->ops.alloc_xrcd)
2234 return ERR_PTR(-EOPNOTSUPP);
2236 xrcd = device->ops.alloc_xrcd(device, NULL);
2237 if (!IS_ERR(xrcd)) {
2238 xrcd->device = device;
2240 atomic_set(&xrcd->usecnt, 0);
2241 mutex_init(&xrcd->tgt_qp_mutex);
2242 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
2247 EXPORT_SYMBOL(__ib_alloc_xrcd);
2249 int ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
2254 if (atomic_read(&xrcd->usecnt))
2257 while (!list_empty(&xrcd->tgt_qp_list)) {
2258 qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
2259 ret = ib_destroy_qp(qp);
2263 mutex_destroy(&xrcd->tgt_qp_mutex);
2265 return xrcd->device->ops.dealloc_xrcd(xrcd, udata);
2267 EXPORT_SYMBOL(ib_dealloc_xrcd);
2270 * ib_create_wq - Creates a WQ associated with the specified protection
2272 * @pd: The protection domain associated with the WQ.
2273 * @wq_attr: A list of initial attributes required to create the
2274 * WQ. If WQ creation succeeds, then the attributes are updated to
2275 * the actual capabilities of the created WQ.
2277 * wq_attr->max_wr and wq_attr->max_sge determine
2278 * the requested size of the WQ, and set to the actual values allocated
2280 * If ib_create_wq() succeeds, then max_wr and max_sge will always be
2281 * at least as large as the requested values.
2283 struct ib_wq *ib_create_wq(struct ib_pd *pd,
2284 struct ib_wq_init_attr *wq_attr)
2288 if (!pd->device->ops.create_wq)
2289 return ERR_PTR(-EOPNOTSUPP);
2291 wq = pd->device->ops.create_wq(pd, wq_attr, NULL);
2293 wq->event_handler = wq_attr->event_handler;
2294 wq->wq_context = wq_attr->wq_context;
2295 wq->wq_type = wq_attr->wq_type;
2296 wq->cq = wq_attr->cq;
2297 wq->device = pd->device;
2300 atomic_inc(&pd->usecnt);
2301 atomic_inc(&wq_attr->cq->usecnt);
2302 atomic_set(&wq->usecnt, 0);
2306 EXPORT_SYMBOL(ib_create_wq);
2309 * ib_destroy_wq - Destroys the specified user WQ.
2310 * @wq: The WQ to destroy.
2311 * @udata: Valid user data
2313 int ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
2315 struct ib_cq *cq = wq->cq;
2316 struct ib_pd *pd = wq->pd;
2318 if (atomic_read(&wq->usecnt))
2321 wq->device->ops.destroy_wq(wq, udata);
2322 atomic_dec(&pd->usecnt);
2323 atomic_dec(&cq->usecnt);
2327 EXPORT_SYMBOL(ib_destroy_wq);
2330 * ib_modify_wq - Modifies the specified WQ.
2331 * @wq: The WQ to modify.
2332 * @wq_attr: On input, specifies the WQ attributes to modify.
2333 * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
2334 * are being modified.
2335 * On output, the current values of selected WQ attributes are returned.
2337 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
2342 if (!wq->device->ops.modify_wq)
2345 err = wq->device->ops.modify_wq(wq, wq_attr, wq_attr_mask, NULL);
2348 EXPORT_SYMBOL(ib_modify_wq);
2351 * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
2352 * @device: The device on which to create the rwq indirection table.
2353 * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
2354 * create the Indirection Table.
2356 * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
2357 * than the created ib_rwq_ind_table object and the caller is responsible
2358 * for its memory allocation/free.
2360 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
2361 struct ib_rwq_ind_table_init_attr *init_attr)
2363 struct ib_rwq_ind_table *rwq_ind_table;
2367 if (!device->ops.create_rwq_ind_table)
2368 return ERR_PTR(-EOPNOTSUPP);
2370 table_size = (1 << init_attr->log_ind_tbl_size);
2371 rwq_ind_table = device->ops.create_rwq_ind_table(device,
2373 if (IS_ERR(rwq_ind_table))
2374 return rwq_ind_table;
2376 rwq_ind_table->ind_tbl = init_attr->ind_tbl;
2377 rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
2378 rwq_ind_table->device = device;
2379 rwq_ind_table->uobject = NULL;
2380 atomic_set(&rwq_ind_table->usecnt, 0);
2382 for (i = 0; i < table_size; i++)
2383 atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
2385 return rwq_ind_table;
2387 EXPORT_SYMBOL(ib_create_rwq_ind_table);
2390 * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
2391 * @wq_ind_table: The Indirection Table to destroy.
2393 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
2396 u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
2397 struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
2399 if (atomic_read(&rwq_ind_table->usecnt))
2402 err = rwq_ind_table->device->ops.destroy_rwq_ind_table(rwq_ind_table);
2404 for (i = 0; i < table_size; i++)
2405 atomic_dec(&ind_tbl[i]->usecnt);
2410 EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
2412 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
2413 struct ib_mr_status *mr_status)
2415 if (!mr->device->ops.check_mr_status)
2418 return mr->device->ops.check_mr_status(mr, check_mask, mr_status);
2420 EXPORT_SYMBOL(ib_check_mr_status);
2422 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
2425 if (!device->ops.set_vf_link_state)
2428 return device->ops.set_vf_link_state(device, vf, port, state);
2430 EXPORT_SYMBOL(ib_set_vf_link_state);
2432 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
2433 struct ifla_vf_info *info)
2435 if (!device->ops.get_vf_config)
2438 return device->ops.get_vf_config(device, vf, port, info);
2440 EXPORT_SYMBOL(ib_get_vf_config);
2442 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
2443 struct ifla_vf_stats *stats)
2445 if (!device->ops.get_vf_stats)
2448 return device->ops.get_vf_stats(device, vf, port, stats);
2450 EXPORT_SYMBOL(ib_get_vf_stats);
2452 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
2455 if (!device->ops.set_vf_guid)
2458 return device->ops.set_vf_guid(device, vf, port, guid, type);
2460 EXPORT_SYMBOL(ib_set_vf_guid);
2463 * ib_map_mr_sg_pi() - Map the dma mapped SG lists for PI (protection
2464 * information) and set an appropriate memory region for registration.
2465 * @mr: memory region
2466 * @data_sg: dma mapped scatterlist for data
2467 * @data_sg_nents: number of entries in data_sg
2468 * @data_sg_offset: offset in bytes into data_sg
2469 * @meta_sg: dma mapped scatterlist for metadata
2470 * @meta_sg_nents: number of entries in meta_sg
2471 * @meta_sg_offset: offset in bytes into meta_sg
2472 * @page_size: page vector desired page size
2475 * - The MR must be allocated with type IB_MR_TYPE_INTEGRITY.
2477 * Return: 0 on success.
2479 * After this completes successfully, the memory region
2480 * is ready for registration.
2482 int ib_map_mr_sg_pi(struct ib_mr *mr, struct scatterlist *data_sg,
2483 int data_sg_nents, unsigned int *data_sg_offset,
2484 struct scatterlist *meta_sg, int meta_sg_nents,
2485 unsigned int *meta_sg_offset, unsigned int page_size)
2487 if (unlikely(!mr->device->ops.map_mr_sg_pi ||
2488 WARN_ON_ONCE(mr->type != IB_MR_TYPE_INTEGRITY)))
2491 mr->page_size = page_size;
2493 return mr->device->ops.map_mr_sg_pi(mr, data_sg, data_sg_nents,
2494 data_sg_offset, meta_sg,
2495 meta_sg_nents, meta_sg_offset);
2497 EXPORT_SYMBOL(ib_map_mr_sg_pi);
2500 * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
2501 * and set it the memory region.
2502 * @mr: memory region
2503 * @sg: dma mapped scatterlist
2504 * @sg_nents: number of entries in sg
2505 * @sg_offset: offset in bytes into sg
2506 * @page_size: page vector desired page size
2509 * - The first sg element is allowed to have an offset.
2510 * - Each sg element must either be aligned to page_size or virtually
2511 * contiguous to the previous element. In case an sg element has a
2512 * non-contiguous offset, the mapping prefix will not include it.
2513 * - The last sg element is allowed to have length less than page_size.
2514 * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
2515 * then only max_num_sg entries will be mapped.
2516 * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
2517 * constraints holds and the page_size argument is ignored.
2519 * Returns the number of sg elements that were mapped to the memory region.
2521 * After this completes successfully, the memory region
2522 * is ready for registration.
2524 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
2525 unsigned int *sg_offset, unsigned int page_size)
2527 if (unlikely(!mr->device->ops.map_mr_sg))
2530 mr->page_size = page_size;
2532 return mr->device->ops.map_mr_sg(mr, sg, sg_nents, sg_offset);
2534 EXPORT_SYMBOL(ib_map_mr_sg);
2537 * ib_sg_to_pages() - Convert the largest prefix of a sg list
2539 * @mr: memory region
2540 * @sgl: dma mapped scatterlist
2541 * @sg_nents: number of entries in sg
2542 * @sg_offset_p: IN: start offset in bytes into sg
2543 * OUT: offset in bytes for element n of the sg of the first
2544 * byte that has not been processed where n is the return
2545 * value of this function.
2546 * @set_page: driver page assignment function pointer
2548 * Core service helper for drivers to convert the largest
2549 * prefix of given sg list to a page vector. The sg list
2550 * prefix converted is the prefix that meet the requirements
2553 * Returns the number of sg elements that were assigned to
2556 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
2557 unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
2559 struct scatterlist *sg;
2560 u64 last_end_dma_addr = 0;
2561 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
2562 unsigned int last_page_off = 0;
2563 u64 page_mask = ~((u64)mr->page_size - 1);
2566 if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
2569 mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
2572 for_each_sg(sgl, sg, sg_nents, i) {
2573 u64 dma_addr = sg_dma_address(sg) + sg_offset;
2574 u64 prev_addr = dma_addr;
2575 unsigned int dma_len = sg_dma_len(sg) - sg_offset;
2576 u64 end_dma_addr = dma_addr + dma_len;
2577 u64 page_addr = dma_addr & page_mask;
2580 * For the second and later elements, check whether either the
2581 * end of element i-1 or the start of element i is not aligned
2582 * on a page boundary.
2584 if (i && (last_page_off != 0 || page_addr != dma_addr)) {
2585 /* Stop mapping if there is a gap. */
2586 if (last_end_dma_addr != dma_addr)
2590 * Coalesce this element with the last. If it is small
2591 * enough just update mr->length. Otherwise start
2592 * mapping from the next page.
2598 ret = set_page(mr, page_addr);
2599 if (unlikely(ret < 0)) {
2600 sg_offset = prev_addr - sg_dma_address(sg);
2601 mr->length += prev_addr - dma_addr;
2603 *sg_offset_p = sg_offset;
2604 return i || sg_offset ? i : ret;
2606 prev_addr = page_addr;
2608 page_addr += mr->page_size;
2609 } while (page_addr < end_dma_addr);
2611 mr->length += dma_len;
2612 last_end_dma_addr = end_dma_addr;
2613 last_page_off = end_dma_addr & ~page_mask;
2622 EXPORT_SYMBOL(ib_sg_to_pages);
2624 struct ib_drain_cqe {
2626 struct completion done;
2629 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
2631 struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
2634 complete(&cqe->done);
2638 * Post a WR and block until its completion is reaped for the SQ.
2640 static void __ib_drain_sq(struct ib_qp *qp)
2642 struct ib_cq *cq = qp->send_cq;
2643 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2644 struct ib_drain_cqe sdrain;
2645 struct ib_rdma_wr swr = {
2648 { .wr_cqe = &sdrain.cqe, },
2649 .opcode = IB_WR_RDMA_WRITE,
2654 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2656 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2660 sdrain.cqe.done = ib_drain_qp_done;
2661 init_completion(&sdrain.done);
2663 ret = ib_post_send(qp, &swr.wr, NULL);
2665 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2669 if (cq->poll_ctx == IB_POLL_DIRECT)
2670 while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
2671 ib_process_cq_direct(cq, -1);
2673 wait_for_completion(&sdrain.done);
2677 * Post a WR and block until its completion is reaped for the RQ.
2679 static void __ib_drain_rq(struct ib_qp *qp)
2681 struct ib_cq *cq = qp->recv_cq;
2682 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2683 struct ib_drain_cqe rdrain;
2684 struct ib_recv_wr rwr = {};
2687 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2689 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2693 rwr.wr_cqe = &rdrain.cqe;
2694 rdrain.cqe.done = ib_drain_qp_done;
2695 init_completion(&rdrain.done);
2697 ret = ib_post_recv(qp, &rwr, NULL);
2699 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2703 if (cq->poll_ctx == IB_POLL_DIRECT)
2704 while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
2705 ib_process_cq_direct(cq, -1);
2707 wait_for_completion(&rdrain.done);
2711 * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2713 * @qp: queue pair to drain
2715 * If the device has a provider-specific drain function, then
2716 * call that. Otherwise call the generic drain function
2721 * ensure there is room in the CQ and SQ for the drain work request and
2724 * allocate the CQ using ib_alloc_cq().
2726 * ensure that there are no other contexts that are posting WRs concurrently.
2727 * Otherwise the drain is not guaranteed.
2729 void ib_drain_sq(struct ib_qp *qp)
2731 if (qp->device->ops.drain_sq)
2732 qp->device->ops.drain_sq(qp);
2736 EXPORT_SYMBOL(ib_drain_sq);
2739 * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2741 * @qp: queue pair to drain
2743 * If the device has a provider-specific drain function, then
2744 * call that. Otherwise call the generic drain function
2749 * ensure there is room in the CQ and RQ for the drain work request and
2752 * allocate the CQ using ib_alloc_cq().
2754 * ensure that there are no other contexts that are posting WRs concurrently.
2755 * Otherwise the drain is not guaranteed.
2757 void ib_drain_rq(struct ib_qp *qp)
2759 if (qp->device->ops.drain_rq)
2760 qp->device->ops.drain_rq(qp);
2764 EXPORT_SYMBOL(ib_drain_rq);
2767 * ib_drain_qp() - Block until all CQEs have been consumed by the
2768 * application on both the RQ and SQ.
2769 * @qp: queue pair to drain
2773 * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2776 * allocate the CQs using ib_alloc_cq().
2778 * ensure that there are no other contexts that are posting WRs concurrently.
2779 * Otherwise the drain is not guaranteed.
2781 void ib_drain_qp(struct ib_qp *qp)
2787 EXPORT_SYMBOL(ib_drain_qp);
2789 struct net_device *rdma_alloc_netdev(struct ib_device *device, u8 port_num,
2790 enum rdma_netdev_t type, const char *name,
2791 unsigned char name_assign_type,
2792 void (*setup)(struct net_device *))
2794 struct rdma_netdev_alloc_params params;
2795 struct net_device *netdev;
2798 if (!device->ops.rdma_netdev_get_params)
2799 return ERR_PTR(-EOPNOTSUPP);
2801 rc = device->ops.rdma_netdev_get_params(device, port_num, type,
2806 netdev = alloc_netdev_mqs(params.sizeof_priv, name, name_assign_type,
2807 setup, params.txqs, params.rxqs);
2809 return ERR_PTR(-ENOMEM);
2813 EXPORT_SYMBOL(rdma_alloc_netdev);
2815 int rdma_init_netdev(struct ib_device *device, u8 port_num,
2816 enum rdma_netdev_t type, const char *name,
2817 unsigned char name_assign_type,
2818 void (*setup)(struct net_device *),
2819 struct net_device *netdev)
2821 struct rdma_netdev_alloc_params params;
2824 if (!device->ops.rdma_netdev_get_params)
2827 rc = device->ops.rdma_netdev_get_params(device, port_num, type,
2832 return params.initialize_rdma_netdev(device, port_num,
2833 netdev, params.param);
2835 EXPORT_SYMBOL(rdma_init_netdev);
2837 void __rdma_block_iter_start(struct ib_block_iter *biter,
2838 struct scatterlist *sglist, unsigned int nents,
2841 memset(biter, 0, sizeof(struct ib_block_iter));
2842 biter->__sg = sglist;
2843 biter->__sg_nents = nents;
2845 /* Driver provides best block size to use */
2846 biter->__pg_bit = __fls(pgsz);
2848 EXPORT_SYMBOL(__rdma_block_iter_start);
2850 bool __rdma_block_iter_next(struct ib_block_iter *biter)
2852 unsigned int block_offset;
2854 if (!biter->__sg_nents || !biter->__sg)
2857 biter->__dma_addr = sg_dma_address(biter->__sg) + biter->__sg_advance;
2858 block_offset = biter->__dma_addr & (BIT_ULL(biter->__pg_bit) - 1);
2859 biter->__sg_advance += BIT_ULL(biter->__pg_bit) - block_offset;
2861 if (biter->__sg_advance >= sg_dma_len(biter->__sg)) {
2862 biter->__sg_advance = 0;
2863 biter->__sg = sg_next(biter->__sg);
2864 biter->__sg_nents--;
2869 EXPORT_SYMBOL(__rdma_block_iter_next);