2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
4 * Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved.
5 * Copyright (c) 2004 Infinicon Corporation. All rights reserved.
6 * Copyright (c) 2004 Intel Corporation. All rights reserved.
7 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
8 * Copyright (c) 2004 Voltaire Corporation. All rights reserved.
9 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
10 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
12 * This software is available to you under a choice of one of two
13 * licenses. You may choose to be licensed under the terms of the GNU
14 * General Public License (GPL) Version 2, available from the file
15 * COPYING in the main directory of this source tree, or the
16 * OpenIB.org BSD license below:
18 * Redistribution and use in source and binary forms, with or
19 * without modification, are permitted provided that the following
22 * - Redistributions of source code must retain the above
23 * copyright notice, this list of conditions and the following
26 * - Redistributions in binary form must reproduce the above
27 * copyright notice, this list of conditions and the following
28 * disclaimer in the documentation and/or other materials
29 * provided with the distribution.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
35 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
36 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
37 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43 #include <linux/errno.h>
44 #include <linux/err.h>
45 #include <linux/string.h>
46 #include <linux/slab.h>
48 #include <linux/in6.h>
50 #include <rdma/ib_verbs.h>
51 #include <rdma/ib_cache.h>
52 #include <rdma/ib_addr.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip6.h>
57 #include <machine/in_cksum.h>
59 #include "core_priv.h"
61 static const char * const ib_events[] = {
62 [IB_EVENT_CQ_ERR] = "CQ error",
63 [IB_EVENT_QP_FATAL] = "QP fatal error",
64 [IB_EVENT_QP_REQ_ERR] = "QP request error",
65 [IB_EVENT_QP_ACCESS_ERR] = "QP access error",
66 [IB_EVENT_COMM_EST] = "communication established",
67 [IB_EVENT_SQ_DRAINED] = "send queue drained",
68 [IB_EVENT_PATH_MIG] = "path migration successful",
69 [IB_EVENT_PATH_MIG_ERR] = "path migration error",
70 [IB_EVENT_DEVICE_FATAL] = "device fatal error",
71 [IB_EVENT_PORT_ACTIVE] = "port active",
72 [IB_EVENT_PORT_ERR] = "port error",
73 [IB_EVENT_LID_CHANGE] = "LID change",
74 [IB_EVENT_PKEY_CHANGE] = "P_key change",
75 [IB_EVENT_SM_CHANGE] = "SM change",
76 [IB_EVENT_SRQ_ERR] = "SRQ error",
77 [IB_EVENT_SRQ_LIMIT_REACHED] = "SRQ limit reached",
78 [IB_EVENT_QP_LAST_WQE_REACHED] = "last WQE reached",
79 [IB_EVENT_CLIENT_REREGISTER] = "client reregister",
80 [IB_EVENT_GID_CHANGE] = "GID changed",
83 const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
87 return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
88 ib_events[index] : "unrecognized event";
90 EXPORT_SYMBOL(ib_event_msg);
92 static const char * const wc_statuses[] = {
93 [IB_WC_SUCCESS] = "success",
94 [IB_WC_LOC_LEN_ERR] = "local length error",
95 [IB_WC_LOC_QP_OP_ERR] = "local QP operation error",
96 [IB_WC_LOC_EEC_OP_ERR] = "local EE context operation error",
97 [IB_WC_LOC_PROT_ERR] = "local protection error",
98 [IB_WC_WR_FLUSH_ERR] = "WR flushed",
99 [IB_WC_MW_BIND_ERR] = "memory management operation error",
100 [IB_WC_BAD_RESP_ERR] = "bad response error",
101 [IB_WC_LOC_ACCESS_ERR] = "local access error",
102 [IB_WC_REM_INV_REQ_ERR] = "invalid request error",
103 [IB_WC_REM_ACCESS_ERR] = "remote access error",
104 [IB_WC_REM_OP_ERR] = "remote operation error",
105 [IB_WC_RETRY_EXC_ERR] = "transport retry counter exceeded",
106 [IB_WC_RNR_RETRY_EXC_ERR] = "RNR retry counter exceeded",
107 [IB_WC_LOC_RDD_VIOL_ERR] = "local RDD violation error",
108 [IB_WC_REM_INV_RD_REQ_ERR] = "remote invalid RD request",
109 [IB_WC_REM_ABORT_ERR] = "operation aborted",
110 [IB_WC_INV_EECN_ERR] = "invalid EE context number",
111 [IB_WC_INV_EEC_STATE_ERR] = "invalid EE context state",
112 [IB_WC_FATAL_ERR] = "fatal error",
113 [IB_WC_RESP_TIMEOUT_ERR] = "response timeout error",
114 [IB_WC_GENERAL_ERR] = "general error",
117 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
119 size_t index = status;
121 return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
122 wc_statuses[index] : "unrecognized status";
124 EXPORT_SYMBOL(ib_wc_status_msg);
126 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
129 case IB_RATE_2_5_GBPS: return 1;
130 case IB_RATE_5_GBPS: return 2;
131 case IB_RATE_10_GBPS: return 4;
132 case IB_RATE_20_GBPS: return 8;
133 case IB_RATE_30_GBPS: return 12;
134 case IB_RATE_40_GBPS: return 16;
135 case IB_RATE_60_GBPS: return 24;
136 case IB_RATE_80_GBPS: return 32;
137 case IB_RATE_120_GBPS: return 48;
141 EXPORT_SYMBOL(ib_rate_to_mult);
143 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
146 case 1: return IB_RATE_2_5_GBPS;
147 case 2: return IB_RATE_5_GBPS;
148 case 4: return IB_RATE_10_GBPS;
149 case 8: return IB_RATE_20_GBPS;
150 case 12: return IB_RATE_30_GBPS;
151 case 16: return IB_RATE_40_GBPS;
152 case 24: return IB_RATE_60_GBPS;
153 case 32: return IB_RATE_80_GBPS;
154 case 48: return IB_RATE_120_GBPS;
155 default: return IB_RATE_PORT_CURRENT;
158 EXPORT_SYMBOL(mult_to_ib_rate);
160 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
163 case IB_RATE_2_5_GBPS: return 2500;
164 case IB_RATE_5_GBPS: return 5000;
165 case IB_RATE_10_GBPS: return 10000;
166 case IB_RATE_20_GBPS: return 20000;
167 case IB_RATE_30_GBPS: return 30000;
168 case IB_RATE_40_GBPS: return 40000;
169 case IB_RATE_60_GBPS: return 60000;
170 case IB_RATE_80_GBPS: return 80000;
171 case IB_RATE_120_GBPS: return 120000;
172 case IB_RATE_14_GBPS: return 14062;
173 case IB_RATE_56_GBPS: return 56250;
174 case IB_RATE_112_GBPS: return 112500;
175 case IB_RATE_168_GBPS: return 168750;
176 case IB_RATE_25_GBPS: return 25781;
177 case IB_RATE_100_GBPS: return 103125;
178 case IB_RATE_200_GBPS: return 206250;
179 case IB_RATE_300_GBPS: return 309375;
183 EXPORT_SYMBOL(ib_rate_to_mbps);
185 __attribute_const__ enum rdma_transport_type
186 rdma_node_get_transport(enum rdma_node_type node_type)
189 case RDMA_NODE_IB_CA:
190 case RDMA_NODE_IB_SWITCH:
191 case RDMA_NODE_IB_ROUTER:
192 return RDMA_TRANSPORT_IB;
194 return RDMA_TRANSPORT_IWARP;
195 case RDMA_NODE_USNIC:
196 return RDMA_TRANSPORT_USNIC;
197 case RDMA_NODE_USNIC_UDP:
198 return RDMA_TRANSPORT_USNIC_UDP;
204 EXPORT_SYMBOL(rdma_node_get_transport);
206 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
208 if (device->get_link_layer)
209 return device->get_link_layer(device, port_num);
211 switch (rdma_node_get_transport(device->node_type)) {
212 case RDMA_TRANSPORT_IB:
213 return IB_LINK_LAYER_INFINIBAND;
214 case RDMA_TRANSPORT_IWARP:
215 case RDMA_TRANSPORT_USNIC:
216 case RDMA_TRANSPORT_USNIC_UDP:
217 return IB_LINK_LAYER_ETHERNET;
219 return IB_LINK_LAYER_UNSPECIFIED;
222 EXPORT_SYMBOL(rdma_port_get_link_layer);
224 /* Protection domains */
227 * ib_alloc_pd - Allocates an unused protection domain.
228 * @device: The device on which to allocate the protection domain.
230 * A protection domain object provides an association between QPs, shared
231 * receive queues, address handles, memory regions, and memory windows.
233 * Every PD has a local_dma_lkey which can be used as the lkey value for local
236 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
240 int mr_access_flags = 0;
242 pd = device->alloc_pd(device, NULL, NULL);
248 pd->__internal_mr = NULL;
249 atomic_set(&pd->usecnt, 0);
252 if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
253 pd->local_dma_lkey = device->local_dma_lkey;
255 mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
257 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
258 pr_warn("%s: enabling unsafe global rkey\n", caller);
259 mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
262 if (mr_access_flags) {
265 mr = pd->device->get_dma_mr(pd, mr_access_flags);
271 mr->device = pd->device;
274 mr->need_inval = false;
276 pd->__internal_mr = mr;
278 if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
279 pd->local_dma_lkey = pd->__internal_mr->lkey;
281 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
282 pd->unsafe_global_rkey = pd->__internal_mr->rkey;
287 EXPORT_SYMBOL(__ib_alloc_pd);
290 * ib_dealloc_pd - Deallocates a protection domain.
291 * @pd: The protection domain to deallocate.
293 * It is an error to call this function while any resources in the pd still
294 * exist. The caller is responsible to synchronously destroy them and
295 * guarantee no new allocations will happen.
297 void ib_dealloc_pd(struct ib_pd *pd)
301 if (pd->__internal_mr) {
302 ret = pd->device->dereg_mr(pd->__internal_mr);
304 pd->__internal_mr = NULL;
307 /* uverbs manipulates usecnt with proper locking, while the kabi
308 requires the caller to guarantee we can't race here. */
309 WARN_ON(atomic_read(&pd->usecnt));
311 /* Making delalloc_pd a void return is a WIP, no driver should return
313 ret = pd->device->dealloc_pd(pd);
314 WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
316 EXPORT_SYMBOL(ib_dealloc_pd);
318 /* Address handles */
320 struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
324 ah = pd->device->create_ah(pd, ah_attr);
327 ah->device = pd->device;
330 atomic_inc(&pd->usecnt);
335 EXPORT_SYMBOL(ib_create_ah);
337 static int ib_get_header_version(const union rdma_network_hdr *hdr)
339 const struct ip *ip4h = (const struct ip *)&hdr->roce4grh;
340 struct ip ip4h_checked;
341 const struct ip6_hdr *ip6h = (const struct ip6_hdr *)&hdr->ibgrh;
343 /* If it's IPv6, the version must be 6, otherwise, the first
344 * 20 bytes (before the IPv4 header) are garbled.
346 if ((ip6h->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
347 return (ip4h->ip_v == 4) ? 4 : 0;
348 /* version may be 6 or 4 because the first 20 bytes could be garbled */
350 /* RoCE v2 requires no options, thus header length
353 if (ip4h->ip_hl != 5)
357 * We can't write on scattered buffers so we need to copy to
360 memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
361 ip4h_checked.ip_sum = 0;
362 #if defined(INET) || defined(INET6)
363 ip4h_checked.ip_sum = in_cksum_hdr(&ip4h_checked);
365 /* if IPv4 header checksum is OK, believe it */
366 if (ip4h->ip_sum == ip4h_checked.ip_sum)
371 static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
373 const struct ib_grh *grh)
377 if (rdma_protocol_ib(device, port_num))
378 return RDMA_NETWORK_IB;
380 grh_version = ib_get_header_version((const union rdma_network_hdr *)grh);
382 if (grh_version == 4)
383 return RDMA_NETWORK_IPV4;
385 if (grh->next_hdr == IPPROTO_UDP)
386 return RDMA_NETWORK_IPV6;
388 return RDMA_NETWORK_ROCE_V1;
391 struct find_gid_index_context {
393 enum ib_gid_type gid_type;
396 static bool find_gid_index(const union ib_gid *gid,
397 const struct ib_gid_attr *gid_attr,
400 struct find_gid_index_context *ctx =
401 (struct find_gid_index_context *)context;
403 if (ctx->gid_type != gid_attr->gid_type)
406 if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) ||
407 (is_vlan_dev(gid_attr->ndev) &&
408 vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id))
414 static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num,
415 u16 vlan_id, const union ib_gid *sgid,
416 enum ib_gid_type gid_type,
419 struct find_gid_index_context context = {.vlan_id = vlan_id,
420 .gid_type = gid_type};
422 return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index,
423 &context, gid_index);
426 static int get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
427 enum rdma_network_type net_type,
428 union ib_gid *sgid, union ib_gid *dgid)
430 struct sockaddr_in src_in;
431 struct sockaddr_in dst_in;
432 __be32 src_saddr, dst_saddr;
437 if (net_type == RDMA_NETWORK_IPV4) {
438 memcpy(&src_in.sin_addr.s_addr,
439 &hdr->roce4grh.ip_src, 4);
440 memcpy(&dst_in.sin_addr.s_addr,
441 &hdr->roce4grh.ip_dst, 4);
442 src_saddr = src_in.sin_addr.s_addr;
443 dst_saddr = dst_in.sin_addr.s_addr;
444 ipv6_addr_set_v4mapped(src_saddr,
445 (struct in6_addr *)sgid);
446 ipv6_addr_set_v4mapped(dst_saddr,
447 (struct in6_addr *)dgid);
449 } else if (net_type == RDMA_NETWORK_IPV6 ||
450 net_type == RDMA_NETWORK_IB) {
451 *dgid = hdr->ibgrh.dgid;
452 *sgid = hdr->ibgrh.sgid;
459 int ib_init_ah_from_wc(struct ib_device *device, u8 port_num,
460 const struct ib_wc *wc, const struct ib_grh *grh,
461 struct ib_ah_attr *ah_attr)
466 enum rdma_network_type net_type = RDMA_NETWORK_IB;
467 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
472 memset(ah_attr, 0, sizeof *ah_attr);
473 if (rdma_cap_eth_ah(device, port_num)) {
474 if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
475 net_type = wc->network_hdr_type;
477 net_type = ib_get_net_type_by_grh(device, port_num, grh);
478 gid_type = ib_network_to_gid_type(net_type);
480 ret = get_gids_from_rdma_hdr((const union rdma_network_hdr *)grh, net_type,
485 if (rdma_protocol_roce(device, port_num)) {
487 u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
488 wc->vlan_id : 0xffff;
489 struct net_device *idev;
490 struct net_device *resolved_dev;
492 if (!(wc->wc_flags & IB_WC_GRH))
495 if (!device->get_netdev)
498 idev = device->get_netdev(device, port_num);
502 ret = rdma_addr_find_l2_eth_by_grh(&dgid, &sgid,
504 wc->wc_flags & IB_WC_WITH_VLAN ?
506 &if_index, &hoplimit);
512 resolved_dev = dev_get_by_index(&init_net, if_index);
513 if (resolved_dev->if_flags & IFF_LOOPBACK) {
514 dev_put(resolved_dev);
516 dev_hold(resolved_dev);
519 if (resolved_dev != idev && !rdma_is_upper_dev_rcu(idev,
524 dev_put(resolved_dev);
528 ret = get_sgid_index_from_eth(device, port_num, vlan_id,
529 &dgid, gid_type, &gid_index);
534 ah_attr->dlid = wc->slid;
535 ah_attr->sl = wc->sl;
536 ah_attr->src_path_bits = wc->dlid_path_bits;
537 ah_attr->port_num = port_num;
539 if (wc->wc_flags & IB_WC_GRH) {
540 ah_attr->ah_flags = IB_AH_GRH;
541 ah_attr->grh.dgid = sgid;
543 if (!rdma_cap_eth_ah(device, port_num)) {
544 if (dgid.global.interface_id != cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
545 ret = ib_find_cached_gid_by_port(device, &dgid,
556 ah_attr->grh.sgid_index = (u8) gid_index;
557 flow_class = be32_to_cpu(grh->version_tclass_flow);
558 ah_attr->grh.flow_label = flow_class & 0xFFFFF;
559 ah_attr->grh.hop_limit = hoplimit;
560 ah_attr->grh.traffic_class = (flow_class >> 20) & 0xFF;
564 EXPORT_SYMBOL(ib_init_ah_from_wc);
566 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
567 const struct ib_grh *grh, u8 port_num)
569 struct ib_ah_attr ah_attr;
572 ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr);
576 return ib_create_ah(pd, &ah_attr);
578 EXPORT_SYMBOL(ib_create_ah_from_wc);
580 int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
582 return ah->device->modify_ah ?
583 ah->device->modify_ah(ah, ah_attr) :
586 EXPORT_SYMBOL(ib_modify_ah);
588 int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
590 return ah->device->query_ah ?
591 ah->device->query_ah(ah, ah_attr) :
594 EXPORT_SYMBOL(ib_query_ah);
596 int ib_destroy_ah(struct ib_ah *ah)
602 ret = ah->device->destroy_ah(ah);
604 atomic_dec(&pd->usecnt);
608 EXPORT_SYMBOL(ib_destroy_ah);
610 /* Shared receive queues */
612 struct ib_srq *ib_create_srq(struct ib_pd *pd,
613 struct ib_srq_init_attr *srq_init_attr)
617 if (!pd->device->create_srq)
618 return ERR_PTR(-ENOSYS);
620 srq = pd->device->create_srq(pd, srq_init_attr, NULL);
623 srq->device = pd->device;
626 srq->event_handler = srq_init_attr->event_handler;
627 srq->srq_context = srq_init_attr->srq_context;
628 srq->srq_type = srq_init_attr->srq_type;
629 if (srq->srq_type == IB_SRQT_XRC) {
630 srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
631 srq->ext.xrc.cq = srq_init_attr->ext.xrc.cq;
632 atomic_inc(&srq->ext.xrc.xrcd->usecnt);
633 atomic_inc(&srq->ext.xrc.cq->usecnt);
635 atomic_inc(&pd->usecnt);
636 atomic_set(&srq->usecnt, 0);
641 EXPORT_SYMBOL(ib_create_srq);
643 int ib_modify_srq(struct ib_srq *srq,
644 struct ib_srq_attr *srq_attr,
645 enum ib_srq_attr_mask srq_attr_mask)
647 return srq->device->modify_srq ?
648 srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
651 EXPORT_SYMBOL(ib_modify_srq);
653 int ib_query_srq(struct ib_srq *srq,
654 struct ib_srq_attr *srq_attr)
656 return srq->device->query_srq ?
657 srq->device->query_srq(srq, srq_attr) : -ENOSYS;
659 EXPORT_SYMBOL(ib_query_srq);
661 int ib_destroy_srq(struct ib_srq *srq)
664 enum ib_srq_type srq_type;
665 struct ib_xrcd *uninitialized_var(xrcd);
666 struct ib_cq *uninitialized_var(cq);
669 if (atomic_read(&srq->usecnt))
673 srq_type = srq->srq_type;
674 if (srq_type == IB_SRQT_XRC) {
675 xrcd = srq->ext.xrc.xrcd;
676 cq = srq->ext.xrc.cq;
679 ret = srq->device->destroy_srq(srq);
681 atomic_dec(&pd->usecnt);
682 if (srq_type == IB_SRQT_XRC) {
683 atomic_dec(&xrcd->usecnt);
684 atomic_dec(&cq->usecnt);
690 EXPORT_SYMBOL(ib_destroy_srq);
694 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
696 struct ib_qp *qp = context;
699 spin_lock_irqsave(&qp->device->event_handler_lock, flags);
700 list_for_each_entry(event->element.qp, &qp->open_list, open_list)
701 if (event->element.qp->event_handler)
702 event->element.qp->event_handler(event, event->element.qp->qp_context);
703 spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
706 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
708 mutex_lock(&xrcd->tgt_qp_mutex);
709 list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
710 mutex_unlock(&xrcd->tgt_qp_mutex);
713 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
714 void (*event_handler)(struct ib_event *, void *),
720 qp = kzalloc(sizeof *qp, GFP_KERNEL);
722 return ERR_PTR(-ENOMEM);
724 qp->real_qp = real_qp;
725 atomic_inc(&real_qp->usecnt);
726 qp->device = real_qp->device;
727 qp->event_handler = event_handler;
728 qp->qp_context = qp_context;
729 qp->qp_num = real_qp->qp_num;
730 qp->qp_type = real_qp->qp_type;
732 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
733 list_add(&qp->open_list, &real_qp->open_list);
734 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
739 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
740 struct ib_qp_open_attr *qp_open_attr)
742 struct ib_qp *qp, *real_qp;
744 if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
745 return ERR_PTR(-EINVAL);
747 qp = ERR_PTR(-EINVAL);
748 mutex_lock(&xrcd->tgt_qp_mutex);
749 list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
750 if (real_qp->qp_num == qp_open_attr->qp_num) {
751 qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
752 qp_open_attr->qp_context);
756 mutex_unlock(&xrcd->tgt_qp_mutex);
759 EXPORT_SYMBOL(ib_open_qp);
761 static struct ib_qp *ib_create_xrc_qp(struct ib_qp *qp,
762 struct ib_qp_init_attr *qp_init_attr)
764 struct ib_qp *real_qp = qp;
766 qp->event_handler = __ib_shared_qp_event_handler;
769 qp->send_cq = qp->recv_cq = NULL;
771 qp->xrcd = qp_init_attr->xrcd;
772 atomic_inc(&qp_init_attr->xrcd->usecnt);
773 INIT_LIST_HEAD(&qp->open_list);
775 qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
776 qp_init_attr->qp_context);
778 __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
780 real_qp->device->destroy_qp(real_qp);
784 struct ib_qp *ib_create_qp(struct ib_pd *pd,
785 struct ib_qp_init_attr *qp_init_attr)
787 struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
790 if (qp_init_attr->rwq_ind_tbl &&
791 (qp_init_attr->recv_cq ||
792 qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
793 qp_init_attr->cap.max_recv_sge))
794 return ERR_PTR(-EINVAL);
796 qp = device->create_qp(pd, qp_init_attr, NULL);
803 qp->qp_type = qp_init_attr->qp_type;
804 qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
806 atomic_set(&qp->usecnt, 0);
807 spin_lock_init(&qp->mr_lock);
809 if (qp_init_attr->qp_type == IB_QPT_XRC_TGT)
810 return ib_create_xrc_qp(qp, qp_init_attr);
812 qp->event_handler = qp_init_attr->event_handler;
813 qp->qp_context = qp_init_attr->qp_context;
814 if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
818 qp->recv_cq = qp_init_attr->recv_cq;
819 if (qp_init_attr->recv_cq)
820 atomic_inc(&qp_init_attr->recv_cq->usecnt);
821 qp->srq = qp_init_attr->srq;
823 atomic_inc(&qp_init_attr->srq->usecnt);
827 qp->send_cq = qp_init_attr->send_cq;
830 atomic_inc(&pd->usecnt);
831 if (qp_init_attr->send_cq)
832 atomic_inc(&qp_init_attr->send_cq->usecnt);
833 if (qp_init_attr->rwq_ind_tbl)
834 atomic_inc(&qp->rwq_ind_tbl->usecnt);
837 * Note: all hw drivers guarantee that max_send_sge is lower than
838 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
839 * max_send_sge <= max_sge_rd.
841 qp->max_write_sge = qp_init_attr->cap.max_send_sge;
842 qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
843 device->attrs.max_sge_rd);
847 EXPORT_SYMBOL(ib_create_qp);
849 static const struct {
851 enum ib_qp_attr_mask req_param[IB_QPT_MAX];
852 enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
853 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
855 [IB_QPS_RESET] = { .valid = 1 },
859 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
862 [IB_QPT_RAW_PACKET] = IB_QP_PORT,
863 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
866 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
869 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
872 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
875 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
877 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
883 [IB_QPS_RESET] = { .valid = 1 },
884 [IB_QPS_ERR] = { .valid = 1 },
888 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
891 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
894 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
897 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
900 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
903 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
905 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
912 [IB_QPT_UC] = (IB_QP_AV |
916 [IB_QPT_RC] = (IB_QP_AV |
920 IB_QP_MAX_DEST_RD_ATOMIC |
921 IB_QP_MIN_RNR_TIMER),
922 [IB_QPT_XRC_INI] = (IB_QP_AV |
926 [IB_QPT_XRC_TGT] = (IB_QP_AV |
930 IB_QP_MAX_DEST_RD_ATOMIC |
931 IB_QP_MIN_RNR_TIMER),
934 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
936 [IB_QPT_UC] = (IB_QP_ALT_PATH |
939 [IB_QPT_RC] = (IB_QP_ALT_PATH |
942 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
945 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
948 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
950 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
956 [IB_QPS_RESET] = { .valid = 1 },
957 [IB_QPS_ERR] = { .valid = 1 },
961 [IB_QPT_UD] = IB_QP_SQ_PSN,
962 [IB_QPT_UC] = IB_QP_SQ_PSN,
963 [IB_QPT_RC] = (IB_QP_TIMEOUT |
967 IB_QP_MAX_QP_RD_ATOMIC),
968 [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
972 IB_QP_MAX_QP_RD_ATOMIC),
973 [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
975 [IB_QPT_SMI] = IB_QP_SQ_PSN,
976 [IB_QPT_GSI] = IB_QP_SQ_PSN,
979 [IB_QPT_UD] = (IB_QP_CUR_STATE |
981 [IB_QPT_UC] = (IB_QP_CUR_STATE |
984 IB_QP_PATH_MIG_STATE),
985 [IB_QPT_RC] = (IB_QP_CUR_STATE |
988 IB_QP_MIN_RNR_TIMER |
989 IB_QP_PATH_MIG_STATE),
990 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
993 IB_QP_PATH_MIG_STATE),
994 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
997 IB_QP_MIN_RNR_TIMER |
998 IB_QP_PATH_MIG_STATE),
999 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1001 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1007 [IB_QPS_RESET] = { .valid = 1 },
1008 [IB_QPS_ERR] = { .valid = 1 },
1012 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1014 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1015 IB_QP_ACCESS_FLAGS |
1017 IB_QP_PATH_MIG_STATE),
1018 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1019 IB_QP_ACCESS_FLAGS |
1021 IB_QP_PATH_MIG_STATE |
1022 IB_QP_MIN_RNR_TIMER),
1023 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1024 IB_QP_ACCESS_FLAGS |
1026 IB_QP_PATH_MIG_STATE),
1027 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1028 IB_QP_ACCESS_FLAGS |
1030 IB_QP_PATH_MIG_STATE |
1031 IB_QP_MIN_RNR_TIMER),
1032 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1034 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1041 [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1042 [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1043 [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1044 [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1045 [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1046 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1047 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1052 [IB_QPS_RESET] = { .valid = 1 },
1053 [IB_QPS_ERR] = { .valid = 1 },
1057 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1059 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1061 IB_QP_ACCESS_FLAGS |
1062 IB_QP_PATH_MIG_STATE),
1063 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1065 IB_QP_ACCESS_FLAGS |
1066 IB_QP_MIN_RNR_TIMER |
1067 IB_QP_PATH_MIG_STATE),
1068 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1070 IB_QP_ACCESS_FLAGS |
1071 IB_QP_PATH_MIG_STATE),
1072 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1074 IB_QP_ACCESS_FLAGS |
1075 IB_QP_MIN_RNR_TIMER |
1076 IB_QP_PATH_MIG_STATE),
1077 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1079 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1086 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1088 [IB_QPT_UC] = (IB_QP_AV |
1090 IB_QP_ACCESS_FLAGS |
1092 IB_QP_PATH_MIG_STATE),
1093 [IB_QPT_RC] = (IB_QP_PORT |
1098 IB_QP_MAX_QP_RD_ATOMIC |
1099 IB_QP_MAX_DEST_RD_ATOMIC |
1101 IB_QP_ACCESS_FLAGS |
1103 IB_QP_MIN_RNR_TIMER |
1104 IB_QP_PATH_MIG_STATE),
1105 [IB_QPT_XRC_INI] = (IB_QP_PORT |
1110 IB_QP_MAX_QP_RD_ATOMIC |
1112 IB_QP_ACCESS_FLAGS |
1114 IB_QP_PATH_MIG_STATE),
1115 [IB_QPT_XRC_TGT] = (IB_QP_PORT |
1118 IB_QP_MAX_DEST_RD_ATOMIC |
1120 IB_QP_ACCESS_FLAGS |
1122 IB_QP_MIN_RNR_TIMER |
1123 IB_QP_PATH_MIG_STATE),
1124 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1126 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1132 [IB_QPS_RESET] = { .valid = 1 },
1133 [IB_QPS_ERR] = { .valid = 1 },
1137 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1139 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1140 IB_QP_ACCESS_FLAGS),
1141 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1143 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1149 [IB_QPS_RESET] = { .valid = 1 },
1150 [IB_QPS_ERR] = { .valid = 1 }
1154 int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1155 enum ib_qp_type type, enum ib_qp_attr_mask mask,
1156 enum rdma_link_layer ll)
1158 enum ib_qp_attr_mask req_param, opt_param;
1160 if (cur_state < 0 || cur_state > IB_QPS_ERR ||
1161 next_state < 0 || next_state > IB_QPS_ERR)
1164 if (mask & IB_QP_CUR_STATE &&
1165 cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1166 cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1169 if (!qp_state_table[cur_state][next_state].valid)
1172 req_param = qp_state_table[cur_state][next_state].req_param[type];
1173 opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1175 if ((mask & req_param) != req_param)
1178 if (mask & ~(req_param | opt_param | IB_QP_STATE))
1183 EXPORT_SYMBOL(ib_modify_qp_is_ok);
1185 int ib_resolve_eth_dmac(struct ib_qp *qp,
1186 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
1190 if (*qp_attr_mask & IB_QP_AV) {
1191 if (qp_attr->ah_attr.port_num < rdma_start_port(qp->device) ||
1192 qp_attr->ah_attr.port_num > rdma_end_port(qp->device))
1195 if (!rdma_cap_eth_ah(qp->device, qp_attr->ah_attr.port_num))
1198 if (rdma_link_local_addr((struct in6_addr *)qp_attr->ah_attr.grh.dgid.raw)) {
1199 rdma_get_ll_mac((struct in6_addr *)qp_attr->ah_attr.grh.dgid.raw,
1200 qp_attr->ah_attr.dmac);
1203 struct ib_gid_attr sgid_attr;
1207 ret = ib_query_gid(qp->device,
1208 qp_attr->ah_attr.port_num,
1209 qp_attr->ah_attr.grh.sgid_index,
1212 if (ret || !sgid_attr.ndev) {
1218 ifindex = sgid_attr.ndev->if_index;
1220 ret = rdma_addr_find_l2_eth_by_grh(&sgid,
1221 &qp_attr->ah_attr.grh.dgid,
1222 qp_attr->ah_attr.dmac,
1223 NULL, &ifindex, &hop_limit);
1225 dev_put(sgid_attr.ndev);
1227 qp_attr->ah_attr.grh.hop_limit = hop_limit;
1233 EXPORT_SYMBOL(ib_resolve_eth_dmac);
1236 int ib_modify_qp(struct ib_qp *qp,
1237 struct ib_qp_attr *qp_attr,
1242 ret = ib_resolve_eth_dmac(qp, qp_attr, &qp_attr_mask);
1246 return qp->device->modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
1248 EXPORT_SYMBOL(ib_modify_qp);
1250 int ib_query_qp(struct ib_qp *qp,
1251 struct ib_qp_attr *qp_attr,
1253 struct ib_qp_init_attr *qp_init_attr)
1255 return qp->device->query_qp ?
1256 qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
1259 EXPORT_SYMBOL(ib_query_qp);
1261 int ib_close_qp(struct ib_qp *qp)
1263 struct ib_qp *real_qp;
1264 unsigned long flags;
1266 real_qp = qp->real_qp;
1270 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1271 list_del(&qp->open_list);
1272 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1274 atomic_dec(&real_qp->usecnt);
1279 EXPORT_SYMBOL(ib_close_qp);
1281 static int __ib_destroy_shared_qp(struct ib_qp *qp)
1283 struct ib_xrcd *xrcd;
1284 struct ib_qp *real_qp;
1287 real_qp = qp->real_qp;
1288 xrcd = real_qp->xrcd;
1290 mutex_lock(&xrcd->tgt_qp_mutex);
1292 if (atomic_read(&real_qp->usecnt) == 0)
1293 list_del(&real_qp->xrcd_list);
1296 mutex_unlock(&xrcd->tgt_qp_mutex);
1299 ret = ib_destroy_qp(real_qp);
1301 atomic_dec(&xrcd->usecnt);
1303 __ib_insert_xrcd_qp(xrcd, real_qp);
1309 int ib_destroy_qp(struct ib_qp *qp)
1312 struct ib_cq *scq, *rcq;
1314 struct ib_rwq_ind_table *ind_tbl;
1317 if (atomic_read(&qp->usecnt))
1320 if (qp->real_qp != qp)
1321 return __ib_destroy_shared_qp(qp);
1327 ind_tbl = qp->rwq_ind_tbl;
1329 ret = qp->device->destroy_qp(qp);
1332 atomic_dec(&pd->usecnt);
1334 atomic_dec(&scq->usecnt);
1336 atomic_dec(&rcq->usecnt);
1338 atomic_dec(&srq->usecnt);
1340 atomic_dec(&ind_tbl->usecnt);
1345 EXPORT_SYMBOL(ib_destroy_qp);
1347 /* Completion queues */
1349 struct ib_cq *ib_create_cq(struct ib_device *device,
1350 ib_comp_handler comp_handler,
1351 void (*event_handler)(struct ib_event *, void *),
1353 const struct ib_cq_init_attr *cq_attr)
1357 cq = device->create_cq(device, cq_attr, NULL, NULL);
1360 cq->device = device;
1362 cq->comp_handler = comp_handler;
1363 cq->event_handler = event_handler;
1364 cq->cq_context = cq_context;
1365 atomic_set(&cq->usecnt, 0);
1370 EXPORT_SYMBOL(ib_create_cq);
1372 int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1374 return cq->device->modify_cq ?
1375 cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
1377 EXPORT_SYMBOL(ib_modify_cq);
1379 int ib_destroy_cq(struct ib_cq *cq)
1381 if (atomic_read(&cq->usecnt))
1384 return cq->device->destroy_cq(cq);
1386 EXPORT_SYMBOL(ib_destroy_cq);
1388 int ib_resize_cq(struct ib_cq *cq, int cqe)
1390 return cq->device->resize_cq ?
1391 cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
1393 EXPORT_SYMBOL(ib_resize_cq);
1395 /* Memory regions */
1397 int ib_dereg_mr(struct ib_mr *mr)
1399 struct ib_pd *pd = mr->pd;
1402 ret = mr->device->dereg_mr(mr);
1404 atomic_dec(&pd->usecnt);
1408 EXPORT_SYMBOL(ib_dereg_mr);
1411 * ib_alloc_mr() - Allocates a memory region
1412 * @pd: protection domain associated with the region
1413 * @mr_type: memory region type
1414 * @max_num_sg: maximum sg entries available for registration.
1417 * Memory registeration page/sg lists must not exceed max_num_sg.
1418 * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
1419 * max_num_sg * used_page_size.
1422 struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
1423 enum ib_mr_type mr_type,
1428 if (!pd->device->alloc_mr)
1429 return ERR_PTR(-ENOSYS);
1431 mr = pd->device->alloc_mr(pd, mr_type, max_num_sg);
1433 mr->device = pd->device;
1436 atomic_inc(&pd->usecnt);
1437 mr->need_inval = false;
1442 EXPORT_SYMBOL(ib_alloc_mr);
1444 /* "Fast" memory regions */
1446 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
1447 int mr_access_flags,
1448 struct ib_fmr_attr *fmr_attr)
1452 if (!pd->device->alloc_fmr)
1453 return ERR_PTR(-ENOSYS);
1455 fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
1457 fmr->device = pd->device;
1459 atomic_inc(&pd->usecnt);
1464 EXPORT_SYMBOL(ib_alloc_fmr);
1466 int ib_unmap_fmr(struct list_head *fmr_list)
1470 if (list_empty(fmr_list))
1473 fmr = list_entry(fmr_list->next, struct ib_fmr, list);
1474 return fmr->device->unmap_fmr(fmr_list);
1476 EXPORT_SYMBOL(ib_unmap_fmr);
1478 int ib_dealloc_fmr(struct ib_fmr *fmr)
1484 ret = fmr->device->dealloc_fmr(fmr);
1486 atomic_dec(&pd->usecnt);
1490 EXPORT_SYMBOL(ib_dealloc_fmr);
1492 /* Multicast groups */
1494 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1498 if (!qp->device->attach_mcast)
1500 if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
1503 ret = qp->device->attach_mcast(qp, gid, lid);
1505 atomic_inc(&qp->usecnt);
1508 EXPORT_SYMBOL(ib_attach_mcast);
1510 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1514 if (!qp->device->detach_mcast)
1516 if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD)
1519 ret = qp->device->detach_mcast(qp, gid, lid);
1521 atomic_dec(&qp->usecnt);
1524 EXPORT_SYMBOL(ib_detach_mcast);
1526 struct ib_xrcd *ib_alloc_xrcd(struct ib_device *device)
1528 struct ib_xrcd *xrcd;
1530 if (!device->alloc_xrcd)
1531 return ERR_PTR(-ENOSYS);
1533 xrcd = device->alloc_xrcd(device, NULL, NULL);
1534 if (!IS_ERR(xrcd)) {
1535 xrcd->device = device;
1537 atomic_set(&xrcd->usecnt, 0);
1538 mutex_init(&xrcd->tgt_qp_mutex);
1539 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
1544 EXPORT_SYMBOL(ib_alloc_xrcd);
1546 int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1551 if (atomic_read(&xrcd->usecnt))
1554 while (!list_empty(&xrcd->tgt_qp_list)) {
1555 qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
1556 ret = ib_destroy_qp(qp);
1561 return xrcd->device->dealloc_xrcd(xrcd);
1563 EXPORT_SYMBOL(ib_dealloc_xrcd);
1566 * ib_create_wq - Creates a WQ associated with the specified protection
1568 * @pd: The protection domain associated with the WQ.
1569 * @wq_init_attr: A list of initial attributes required to create the
1570 * WQ. If WQ creation succeeds, then the attributes are updated to
1571 * the actual capabilities of the created WQ.
1573 * wq_init_attr->max_wr and wq_init_attr->max_sge determine
1574 * the requested size of the WQ, and set to the actual values allocated
1576 * If ib_create_wq() succeeds, then max_wr and max_sge will always be
1577 * at least as large as the requested values.
1579 struct ib_wq *ib_create_wq(struct ib_pd *pd,
1580 struct ib_wq_init_attr *wq_attr)
1584 if (!pd->device->create_wq)
1585 return ERR_PTR(-ENOSYS);
1587 wq = pd->device->create_wq(pd, wq_attr, NULL);
1589 wq->event_handler = wq_attr->event_handler;
1590 wq->wq_context = wq_attr->wq_context;
1591 wq->wq_type = wq_attr->wq_type;
1592 wq->cq = wq_attr->cq;
1593 wq->device = pd->device;
1596 atomic_inc(&pd->usecnt);
1597 atomic_inc(&wq_attr->cq->usecnt);
1598 atomic_set(&wq->usecnt, 0);
1602 EXPORT_SYMBOL(ib_create_wq);
1605 * ib_destroy_wq - Destroys the specified WQ.
1606 * @wq: The WQ to destroy.
1608 int ib_destroy_wq(struct ib_wq *wq)
1611 struct ib_cq *cq = wq->cq;
1612 struct ib_pd *pd = wq->pd;
1614 if (atomic_read(&wq->usecnt))
1617 err = wq->device->destroy_wq(wq);
1619 atomic_dec(&pd->usecnt);
1620 atomic_dec(&cq->usecnt);
1624 EXPORT_SYMBOL(ib_destroy_wq);
1627 * ib_modify_wq - Modifies the specified WQ.
1628 * @wq: The WQ to modify.
1629 * @wq_attr: On input, specifies the WQ attributes to modify.
1630 * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
1631 * are being modified.
1632 * On output, the current values of selected WQ attributes are returned.
1634 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
1639 if (!wq->device->modify_wq)
1642 err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
1645 EXPORT_SYMBOL(ib_modify_wq);
1648 * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
1649 * @device: The device on which to create the rwq indirection table.
1650 * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
1651 * create the Indirection Table.
1653 * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
1654 * than the created ib_rwq_ind_table object and the caller is responsible
1655 * for its memory allocation/free.
1657 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
1658 struct ib_rwq_ind_table_init_attr *init_attr)
1660 struct ib_rwq_ind_table *rwq_ind_table;
1664 if (!device->create_rwq_ind_table)
1665 return ERR_PTR(-ENOSYS);
1667 table_size = (1 << init_attr->log_ind_tbl_size);
1668 rwq_ind_table = device->create_rwq_ind_table(device,
1670 if (IS_ERR(rwq_ind_table))
1671 return rwq_ind_table;
1673 rwq_ind_table->ind_tbl = init_attr->ind_tbl;
1674 rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
1675 rwq_ind_table->device = device;
1676 rwq_ind_table->uobject = NULL;
1677 atomic_set(&rwq_ind_table->usecnt, 0);
1679 for (i = 0; i < table_size; i++)
1680 atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
1682 return rwq_ind_table;
1684 EXPORT_SYMBOL(ib_create_rwq_ind_table);
1687 * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
1688 * @wq_ind_table: The Indirection Table to destroy.
1690 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
1693 u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
1694 struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
1696 if (atomic_read(&rwq_ind_table->usecnt))
1699 err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
1701 for (i = 0; i < table_size; i++)
1702 atomic_dec(&ind_tbl[i]->usecnt);
1707 EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
1709 struct ib_flow *ib_create_flow(struct ib_qp *qp,
1710 struct ib_flow_attr *flow_attr,
1713 struct ib_flow *flow_id;
1714 if (!qp->device->create_flow)
1715 return ERR_PTR(-ENOSYS);
1717 flow_id = qp->device->create_flow(qp, flow_attr, domain);
1718 if (!IS_ERR(flow_id))
1719 atomic_inc(&qp->usecnt);
1722 EXPORT_SYMBOL(ib_create_flow);
1724 int ib_destroy_flow(struct ib_flow *flow_id)
1727 struct ib_qp *qp = flow_id->qp;
1729 err = qp->device->destroy_flow(flow_id);
1731 atomic_dec(&qp->usecnt);
1734 EXPORT_SYMBOL(ib_destroy_flow);
1736 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
1737 struct ib_mr_status *mr_status)
1739 return mr->device->check_mr_status ?
1740 mr->device->check_mr_status(mr, check_mask, mr_status) : -ENOSYS;
1742 EXPORT_SYMBOL(ib_check_mr_status);
1744 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
1747 if (!device->set_vf_link_state)
1750 return device->set_vf_link_state(device, vf, port, state);
1752 EXPORT_SYMBOL(ib_set_vf_link_state);
1754 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
1755 struct ifla_vf_info *info)
1757 if (!device->get_vf_config)
1760 return device->get_vf_config(device, vf, port, info);
1762 EXPORT_SYMBOL(ib_get_vf_config);
1764 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
1765 struct ifla_vf_stats *stats)
1767 if (!device->get_vf_stats)
1770 return device->get_vf_stats(device, vf, port, stats);
1772 EXPORT_SYMBOL(ib_get_vf_stats);
1774 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
1777 if (!device->set_vf_guid)
1780 return device->set_vf_guid(device, vf, port, guid, type);
1782 EXPORT_SYMBOL(ib_set_vf_guid);
1785 * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
1786 * and set it the memory region.
1787 * @mr: memory region
1788 * @sg: dma mapped scatterlist
1789 * @sg_nents: number of entries in sg
1790 * @sg_offset: offset in bytes into sg
1791 * @page_size: page vector desired page size
1794 * - The first sg element is allowed to have an offset.
1795 * - Each sg element must either be aligned to page_size or virtually
1796 * contiguous to the previous element. In case an sg element has a
1797 * non-contiguous offset, the mapping prefix will not include it.
1798 * - The last sg element is allowed to have length less than page_size.
1799 * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
1800 * then only max_num_sg entries will be mapped.
1801 * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
1802 * constraints holds and the page_size argument is ignored.
1804 * Returns the number of sg elements that were mapped to the memory region.
1806 * After this completes successfully, the memory region
1807 * is ready for registration.
1809 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
1810 unsigned int *sg_offset, unsigned int page_size)
1812 if (unlikely(!mr->device->map_mr_sg))
1815 mr->page_size = page_size;
1817 return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
1819 EXPORT_SYMBOL(ib_map_mr_sg);
1822 * ib_sg_to_pages() - Convert the largest prefix of a sg list
1824 * @mr: memory region
1825 * @sgl: dma mapped scatterlist
1826 * @sg_nents: number of entries in sg
1827 * @sg_offset_p: IN: start offset in bytes into sg
1828 * OUT: offset in bytes for element n of the sg of the first
1829 * byte that has not been processed where n is the return
1830 * value of this function.
1831 * @set_page: driver page assignment function pointer
1833 * Core service helper for drivers to convert the largest
1834 * prefix of given sg list to a page vector. The sg list
1835 * prefix converted is the prefix that meet the requirements
1838 * Returns the number of sg elements that were assigned to
1841 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
1842 unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
1844 struct scatterlist *sg;
1845 u64 last_end_dma_addr = 0;
1846 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
1847 unsigned int last_page_off = 0;
1848 u64 page_mask = ~((u64)mr->page_size - 1);
1851 if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
1854 mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
1857 for_each_sg(sgl, sg, sg_nents, i) {
1858 u64 dma_addr = sg_dma_address(sg) + sg_offset;
1859 u64 prev_addr = dma_addr;
1860 unsigned int dma_len = sg_dma_len(sg) - sg_offset;
1861 u64 end_dma_addr = dma_addr + dma_len;
1862 u64 page_addr = dma_addr & page_mask;
1865 * For the second and later elements, check whether either the
1866 * end of element i-1 or the start of element i is not aligned
1867 * on a page boundary.
1869 if (i && (last_page_off != 0 || page_addr != dma_addr)) {
1870 /* Stop mapping if there is a gap. */
1871 if (last_end_dma_addr != dma_addr)
1875 * Coalesce this element with the last. If it is small
1876 * enough just update mr->length. Otherwise start
1877 * mapping from the next page.
1883 ret = set_page(mr, page_addr);
1884 if (unlikely(ret < 0)) {
1885 sg_offset = prev_addr - sg_dma_address(sg);
1886 mr->length += prev_addr - dma_addr;
1888 *sg_offset_p = sg_offset;
1889 return i || sg_offset ? i : ret;
1891 prev_addr = page_addr;
1893 page_addr += mr->page_size;
1894 } while (page_addr < end_dma_addr);
1896 mr->length += dma_len;
1897 last_end_dma_addr = end_dma_addr;
1898 last_page_off = end_dma_addr & ~page_mask;
1907 EXPORT_SYMBOL(ib_sg_to_pages);
1909 struct ib_drain_cqe {
1911 struct completion done;
1914 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
1916 struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
1919 complete(&cqe->done);
1923 * Post a WR and block until its completion is reaped for the SQ.
1925 static void __ib_drain_sq(struct ib_qp *qp)
1927 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
1928 struct ib_drain_cqe sdrain;
1929 struct ib_send_wr swr = {}, *bad_swr;
1932 if (qp->send_cq->poll_ctx == IB_POLL_DIRECT) {
1933 WARN_ONCE(qp->send_cq->poll_ctx == IB_POLL_DIRECT,
1934 "IB_POLL_DIRECT poll_ctx not supported for drain\n");
1938 swr.wr_cqe = &sdrain.cqe;
1939 sdrain.cqe.done = ib_drain_qp_done;
1940 init_completion(&sdrain.done);
1942 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
1944 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
1948 ret = ib_post_send(qp, &swr, &bad_swr);
1950 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
1954 wait_for_completion(&sdrain.done);
1958 * Post a WR and block until its completion is reaped for the RQ.
1960 static void __ib_drain_rq(struct ib_qp *qp)
1962 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
1963 struct ib_drain_cqe rdrain;
1964 struct ib_recv_wr rwr = {}, *bad_rwr;
1967 if (qp->recv_cq->poll_ctx == IB_POLL_DIRECT) {
1968 WARN_ONCE(qp->recv_cq->poll_ctx == IB_POLL_DIRECT,
1969 "IB_POLL_DIRECT poll_ctx not supported for drain\n");
1973 rwr.wr_cqe = &rdrain.cqe;
1974 rdrain.cqe.done = ib_drain_qp_done;
1975 init_completion(&rdrain.done);
1977 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
1979 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
1983 ret = ib_post_recv(qp, &rwr, &bad_rwr);
1985 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
1989 wait_for_completion(&rdrain.done);
1993 * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
1995 * @qp: queue pair to drain
1997 * If the device has a provider-specific drain function, then
1998 * call that. Otherwise call the generic drain function
2003 * ensure there is room in the CQ and SQ for the drain work request and
2006 * allocate the CQ using ib_alloc_cq() and the CQ poll context cannot be
2009 * ensure that there are no other contexts that are posting WRs concurrently.
2010 * Otherwise the drain is not guaranteed.
2012 void ib_drain_sq(struct ib_qp *qp)
2014 if (qp->device->drain_sq)
2015 qp->device->drain_sq(qp);
2019 EXPORT_SYMBOL(ib_drain_sq);
2022 * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2024 * @qp: queue pair to drain
2026 * If the device has a provider-specific drain function, then
2027 * call that. Otherwise call the generic drain function
2032 * ensure there is room in the CQ and RQ for the drain work request and
2035 * allocate the CQ using ib_alloc_cq() and the CQ poll context cannot be
2038 * ensure that there are no other contexts that are posting WRs concurrently.
2039 * Otherwise the drain is not guaranteed.
2041 void ib_drain_rq(struct ib_qp *qp)
2043 if (qp->device->drain_rq)
2044 qp->device->drain_rq(qp);
2048 EXPORT_SYMBOL(ib_drain_rq);
2051 * ib_drain_qp() - Block until all CQEs have been consumed by the
2052 * application on both the RQ and SQ.
2053 * @qp: queue pair to drain
2057 * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2060 * allocate the CQs using ib_alloc_cq() and the CQ poll context cannot be
2063 * ensure that there are no other contexts that are posting WRs concurrently.
2064 * Otherwise the drain is not guaranteed.
2066 void ib_drain_qp(struct ib_qp *qp)
2072 EXPORT_SYMBOL(ib_drain_qp);