Merge tag 'chrome-platform-for-linus-4.13' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / infiniband / core / verbs.c
blobc973a83c898b487710600f4e4f6441c99595ca6c
1 /*
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
18 * conditions are met:
20 * - Redistributions of source code must retain the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer.
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
36 * SOFTWARE.
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>
44 #include <linux/in.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>
52 #include <rdma/rw.h>
54 #include "core_priv.h"
56 static const char * const ib_events[] = {
57 [IB_EVENT_CQ_ERR] = "CQ error",
58 [IB_EVENT_QP_FATAL] = "QP fatal error",
59 [IB_EVENT_QP_REQ_ERR] = "QP request error",
60 [IB_EVENT_QP_ACCESS_ERR] = "QP access error",
61 [IB_EVENT_COMM_EST] = "communication established",
62 [IB_EVENT_SQ_DRAINED] = "send queue drained",
63 [IB_EVENT_PATH_MIG] = "path migration successful",
64 [IB_EVENT_PATH_MIG_ERR] = "path migration error",
65 [IB_EVENT_DEVICE_FATAL] = "device fatal error",
66 [IB_EVENT_PORT_ACTIVE] = "port active",
67 [IB_EVENT_PORT_ERR] = "port error",
68 [IB_EVENT_LID_CHANGE] = "LID change",
69 [IB_EVENT_PKEY_CHANGE] = "P_key change",
70 [IB_EVENT_SM_CHANGE] = "SM change",
71 [IB_EVENT_SRQ_ERR] = "SRQ error",
72 [IB_EVENT_SRQ_LIMIT_REACHED] = "SRQ limit reached",
73 [IB_EVENT_QP_LAST_WQE_REACHED] = "last WQE reached",
74 [IB_EVENT_CLIENT_REREGISTER] = "client reregister",
75 [IB_EVENT_GID_CHANGE] = "GID changed",
78 const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
80 size_t index = event;
82 return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
83 ib_events[index] : "unrecognized event";
85 EXPORT_SYMBOL(ib_event_msg);
87 static const char * const wc_statuses[] = {
88 [IB_WC_SUCCESS] = "success",
89 [IB_WC_LOC_LEN_ERR] = "local length error",
90 [IB_WC_LOC_QP_OP_ERR] = "local QP operation error",
91 [IB_WC_LOC_EEC_OP_ERR] = "local EE context operation error",
92 [IB_WC_LOC_PROT_ERR] = "local protection error",
93 [IB_WC_WR_FLUSH_ERR] = "WR flushed",
94 [IB_WC_MW_BIND_ERR] = "memory management operation error",
95 [IB_WC_BAD_RESP_ERR] = "bad response error",
96 [IB_WC_LOC_ACCESS_ERR] = "local access error",
97 [IB_WC_REM_INV_REQ_ERR] = "invalid request error",
98 [IB_WC_REM_ACCESS_ERR] = "remote access error",
99 [IB_WC_REM_OP_ERR] = "remote operation error",
100 [IB_WC_RETRY_EXC_ERR] = "transport retry counter exceeded",
101 [IB_WC_RNR_RETRY_EXC_ERR] = "RNR retry counter exceeded",
102 [IB_WC_LOC_RDD_VIOL_ERR] = "local RDD violation error",
103 [IB_WC_REM_INV_RD_REQ_ERR] = "remote invalid RD request",
104 [IB_WC_REM_ABORT_ERR] = "operation aborted",
105 [IB_WC_INV_EECN_ERR] = "invalid EE context number",
106 [IB_WC_INV_EEC_STATE_ERR] = "invalid EE context state",
107 [IB_WC_FATAL_ERR] = "fatal error",
108 [IB_WC_RESP_TIMEOUT_ERR] = "response timeout error",
109 [IB_WC_GENERAL_ERR] = "general error",
112 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
114 size_t index = status;
116 return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
117 wc_statuses[index] : "unrecognized status";
119 EXPORT_SYMBOL(ib_wc_status_msg);
121 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
123 switch (rate) {
124 case IB_RATE_2_5_GBPS: return 1;
125 case IB_RATE_5_GBPS: return 2;
126 case IB_RATE_10_GBPS: return 4;
127 case IB_RATE_20_GBPS: return 8;
128 case IB_RATE_30_GBPS: return 12;
129 case IB_RATE_40_GBPS: return 16;
130 case IB_RATE_60_GBPS: return 24;
131 case IB_RATE_80_GBPS: return 32;
132 case IB_RATE_120_GBPS: return 48;
133 default: return -1;
136 EXPORT_SYMBOL(ib_rate_to_mult);
138 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
140 switch (mult) {
141 case 1: return IB_RATE_2_5_GBPS;
142 case 2: return IB_RATE_5_GBPS;
143 case 4: return IB_RATE_10_GBPS;
144 case 8: return IB_RATE_20_GBPS;
145 case 12: return IB_RATE_30_GBPS;
146 case 16: return IB_RATE_40_GBPS;
147 case 24: return IB_RATE_60_GBPS;
148 case 32: return IB_RATE_80_GBPS;
149 case 48: return IB_RATE_120_GBPS;
150 default: return IB_RATE_PORT_CURRENT;
153 EXPORT_SYMBOL(mult_to_ib_rate);
155 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
157 switch (rate) {
158 case IB_RATE_2_5_GBPS: return 2500;
159 case IB_RATE_5_GBPS: return 5000;
160 case IB_RATE_10_GBPS: return 10000;
161 case IB_RATE_20_GBPS: return 20000;
162 case IB_RATE_30_GBPS: return 30000;
163 case IB_RATE_40_GBPS: return 40000;
164 case IB_RATE_60_GBPS: return 60000;
165 case IB_RATE_80_GBPS: return 80000;
166 case IB_RATE_120_GBPS: return 120000;
167 case IB_RATE_14_GBPS: return 14062;
168 case IB_RATE_56_GBPS: return 56250;
169 case IB_RATE_112_GBPS: return 112500;
170 case IB_RATE_168_GBPS: return 168750;
171 case IB_RATE_25_GBPS: return 25781;
172 case IB_RATE_100_GBPS: return 103125;
173 case IB_RATE_200_GBPS: return 206250;
174 case IB_RATE_300_GBPS: return 309375;
175 default: return -1;
178 EXPORT_SYMBOL(ib_rate_to_mbps);
180 __attribute_const__ enum rdma_transport_type
181 rdma_node_get_transport(enum rdma_node_type node_type)
183 switch (node_type) {
184 case RDMA_NODE_IB_CA:
185 case RDMA_NODE_IB_SWITCH:
186 case RDMA_NODE_IB_ROUTER:
187 return RDMA_TRANSPORT_IB;
188 case RDMA_NODE_RNIC:
189 return RDMA_TRANSPORT_IWARP;
190 case RDMA_NODE_USNIC:
191 return RDMA_TRANSPORT_USNIC;
192 case RDMA_NODE_USNIC_UDP:
193 return RDMA_TRANSPORT_USNIC_UDP;
194 default:
195 BUG();
196 return 0;
199 EXPORT_SYMBOL(rdma_node_get_transport);
201 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
203 if (device->get_link_layer)
204 return device->get_link_layer(device, port_num);
206 switch (rdma_node_get_transport(device->node_type)) {
207 case RDMA_TRANSPORT_IB:
208 return IB_LINK_LAYER_INFINIBAND;
209 case RDMA_TRANSPORT_IWARP:
210 case RDMA_TRANSPORT_USNIC:
211 case RDMA_TRANSPORT_USNIC_UDP:
212 return IB_LINK_LAYER_ETHERNET;
213 default:
214 return IB_LINK_LAYER_UNSPECIFIED;
217 EXPORT_SYMBOL(rdma_port_get_link_layer);
219 /* Protection domains */
222 * ib_alloc_pd - Allocates an unused protection domain.
223 * @device: The device on which to allocate the protection domain.
225 * A protection domain object provides an association between QPs, shared
226 * receive queues, address handles, memory regions, and memory windows.
228 * Every PD has a local_dma_lkey which can be used as the lkey value for local
229 * memory operations.
231 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
232 const char *caller)
234 struct ib_pd *pd;
235 int mr_access_flags = 0;
237 pd = device->alloc_pd(device, NULL, NULL);
238 if (IS_ERR(pd))
239 return pd;
241 pd->device = device;
242 pd->uobject = NULL;
243 pd->__internal_mr = NULL;
244 atomic_set(&pd->usecnt, 0);
245 pd->flags = flags;
247 if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
248 pd->local_dma_lkey = device->local_dma_lkey;
249 else
250 mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
252 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
253 pr_warn("%s: enabling unsafe global rkey\n", caller);
254 mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
257 if (mr_access_flags) {
258 struct ib_mr *mr;
260 mr = pd->device->get_dma_mr(pd, mr_access_flags);
261 if (IS_ERR(mr)) {
262 ib_dealloc_pd(pd);
263 return ERR_CAST(mr);
266 mr->device = pd->device;
267 mr->pd = pd;
268 mr->uobject = NULL;
269 mr->need_inval = false;
271 pd->__internal_mr = mr;
273 if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
274 pd->local_dma_lkey = pd->__internal_mr->lkey;
276 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
277 pd->unsafe_global_rkey = pd->__internal_mr->rkey;
280 return pd;
282 EXPORT_SYMBOL(__ib_alloc_pd);
285 * ib_dealloc_pd - Deallocates a protection domain.
286 * @pd: The protection domain to deallocate.
288 * It is an error to call this function while any resources in the pd still
289 * exist. The caller is responsible to synchronously destroy them and
290 * guarantee no new allocations will happen.
292 void ib_dealloc_pd(struct ib_pd *pd)
294 int ret;
296 if (pd->__internal_mr) {
297 ret = pd->device->dereg_mr(pd->__internal_mr);
298 WARN_ON(ret);
299 pd->__internal_mr = NULL;
302 /* uverbs manipulates usecnt with proper locking, while the kabi
303 requires the caller to guarantee we can't race here. */
304 WARN_ON(atomic_read(&pd->usecnt));
306 /* Making delalloc_pd a void return is a WIP, no driver should return
307 an error here. */
308 ret = pd->device->dealloc_pd(pd);
309 WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
311 EXPORT_SYMBOL(ib_dealloc_pd);
313 /* Address handles */
315 struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr)
317 struct ib_ah *ah;
319 ah = pd->device->create_ah(pd, ah_attr, NULL);
321 if (!IS_ERR(ah)) {
322 ah->device = pd->device;
323 ah->pd = pd;
324 ah->uobject = NULL;
325 ah->type = ah_attr->type;
326 atomic_inc(&pd->usecnt);
329 return ah;
331 EXPORT_SYMBOL(rdma_create_ah);
333 int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
335 const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
336 struct iphdr ip4h_checked;
337 const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;
339 /* If it's IPv6, the version must be 6, otherwise, the first
340 * 20 bytes (before the IPv4 header) are garbled.
342 if (ip6h->version != 6)
343 return (ip4h->version == 4) ? 4 : 0;
344 /* version may be 6 or 4 because the first 20 bytes could be garbled */
346 /* RoCE v2 requires no options, thus header length
347 * must be 5 words
349 if (ip4h->ihl != 5)
350 return 6;
352 /* Verify checksum.
353 * We can't write on scattered buffers so we need to copy to
354 * temp buffer.
356 memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
357 ip4h_checked.check = 0;
358 ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
359 /* if IPv4 header checksum is OK, believe it */
360 if (ip4h->check == ip4h_checked.check)
361 return 4;
362 return 6;
364 EXPORT_SYMBOL(ib_get_rdma_header_version);
366 static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
367 u8 port_num,
368 const struct ib_grh *grh)
370 int grh_version;
372 if (rdma_protocol_ib(device, port_num))
373 return RDMA_NETWORK_IB;
375 grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
377 if (grh_version == 4)
378 return RDMA_NETWORK_IPV4;
380 if (grh->next_hdr == IPPROTO_UDP)
381 return RDMA_NETWORK_IPV6;
383 return RDMA_NETWORK_ROCE_V1;
386 struct find_gid_index_context {
387 u16 vlan_id;
388 enum ib_gid_type gid_type;
391 static bool find_gid_index(const union ib_gid *gid,
392 const struct ib_gid_attr *gid_attr,
393 void *context)
395 struct find_gid_index_context *ctx =
396 (struct find_gid_index_context *)context;
398 if (ctx->gid_type != gid_attr->gid_type)
399 return false;
401 if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) ||
402 (is_vlan_dev(gid_attr->ndev) &&
403 vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id))
404 return false;
406 return true;
409 static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num,
410 u16 vlan_id, const union ib_gid *sgid,
411 enum ib_gid_type gid_type,
412 u16 *gid_index)
414 struct find_gid_index_context context = {.vlan_id = vlan_id,
415 .gid_type = gid_type};
417 return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index,
418 &context, gid_index);
421 int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
422 enum rdma_network_type net_type,
423 union ib_gid *sgid, union ib_gid *dgid)
425 struct sockaddr_in src_in;
426 struct sockaddr_in dst_in;
427 __be32 src_saddr, dst_saddr;
429 if (!sgid || !dgid)
430 return -EINVAL;
432 if (net_type == RDMA_NETWORK_IPV4) {
433 memcpy(&src_in.sin_addr.s_addr,
434 &hdr->roce4grh.saddr, 4);
435 memcpy(&dst_in.sin_addr.s_addr,
436 &hdr->roce4grh.daddr, 4);
437 src_saddr = src_in.sin_addr.s_addr;
438 dst_saddr = dst_in.sin_addr.s_addr;
439 ipv6_addr_set_v4mapped(src_saddr,
440 (struct in6_addr *)sgid);
441 ipv6_addr_set_v4mapped(dst_saddr,
442 (struct in6_addr *)dgid);
443 return 0;
444 } else if (net_type == RDMA_NETWORK_IPV6 ||
445 net_type == RDMA_NETWORK_IB) {
446 *dgid = hdr->ibgrh.dgid;
447 *sgid = hdr->ibgrh.sgid;
448 return 0;
449 } else {
450 return -EINVAL;
453 EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
455 int ib_init_ah_from_wc(struct ib_device *device, u8 port_num,
456 const struct ib_wc *wc, const struct ib_grh *grh,
457 struct rdma_ah_attr *ah_attr)
459 u32 flow_class;
460 u16 gid_index;
461 int ret;
462 enum rdma_network_type net_type = RDMA_NETWORK_IB;
463 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
464 int hoplimit = 0xff;
465 union ib_gid dgid;
466 union ib_gid sgid;
468 memset(ah_attr, 0, sizeof *ah_attr);
469 ah_attr->type = rdma_ah_find_type(device, port_num);
470 if (rdma_cap_eth_ah(device, port_num)) {
471 if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
472 net_type = wc->network_hdr_type;
473 else
474 net_type = ib_get_net_type_by_grh(device, port_num, grh);
475 gid_type = ib_network_to_gid_type(net_type);
477 ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
478 &sgid, &dgid);
479 if (ret)
480 return ret;
482 if (rdma_protocol_roce(device, port_num)) {
483 int if_index = 0;
484 u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
485 wc->vlan_id : 0xffff;
486 struct net_device *idev;
487 struct net_device *resolved_dev;
489 if (!(wc->wc_flags & IB_WC_GRH))
490 return -EPROTOTYPE;
492 if (!device->get_netdev)
493 return -EOPNOTSUPP;
495 idev = device->get_netdev(device, port_num);
496 if (!idev)
497 return -ENODEV;
499 ret = rdma_addr_find_l2_eth_by_grh(&dgid, &sgid,
500 ah_attr->roce.dmac,
501 wc->wc_flags & IB_WC_WITH_VLAN ?
502 NULL : &vlan_id,
503 &if_index, &hoplimit);
504 if (ret) {
505 dev_put(idev);
506 return ret;
509 resolved_dev = dev_get_by_index(&init_net, if_index);
510 if (resolved_dev->flags & IFF_LOOPBACK) {
511 dev_put(resolved_dev);
512 resolved_dev = idev;
513 dev_hold(resolved_dev);
515 rcu_read_lock();
516 if (resolved_dev != idev && !rdma_is_upper_dev_rcu(idev,
517 resolved_dev))
518 ret = -EHOSTUNREACH;
519 rcu_read_unlock();
520 dev_put(idev);
521 dev_put(resolved_dev);
522 if (ret)
523 return ret;
525 ret = get_sgid_index_from_eth(device, port_num, vlan_id,
526 &dgid, gid_type, &gid_index);
527 if (ret)
528 return ret;
531 rdma_ah_set_dlid(ah_attr, wc->slid);
532 rdma_ah_set_sl(ah_attr, wc->sl);
533 rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
534 rdma_ah_set_port_num(ah_attr, port_num);
536 if (wc->wc_flags & IB_WC_GRH) {
537 if (!rdma_cap_eth_ah(device, port_num)) {
538 if (dgid.global.interface_id != cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
539 ret = ib_find_cached_gid_by_port(device, &dgid,
540 IB_GID_TYPE_IB,
541 port_num, NULL,
542 &gid_index);
543 if (ret)
544 return ret;
545 } else {
546 gid_index = 0;
550 flow_class = be32_to_cpu(grh->version_tclass_flow);
551 rdma_ah_set_grh(ah_attr, &sgid,
552 flow_class & 0xFFFFF,
553 (u8)gid_index, hoplimit,
554 (flow_class >> 20) & 0xFF);
557 return 0;
559 EXPORT_SYMBOL(ib_init_ah_from_wc);
561 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
562 const struct ib_grh *grh, u8 port_num)
564 struct rdma_ah_attr ah_attr;
565 int ret;
567 ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr);
568 if (ret)
569 return ERR_PTR(ret);
571 return rdma_create_ah(pd, &ah_attr);
573 EXPORT_SYMBOL(ib_create_ah_from_wc);
575 int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
577 if (ah->type != ah_attr->type)
578 return -EINVAL;
580 return ah->device->modify_ah ?
581 ah->device->modify_ah(ah, ah_attr) :
582 -ENOSYS;
584 EXPORT_SYMBOL(rdma_modify_ah);
586 int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
588 return ah->device->query_ah ?
589 ah->device->query_ah(ah, ah_attr) :
590 -ENOSYS;
592 EXPORT_SYMBOL(rdma_query_ah);
594 int rdma_destroy_ah(struct ib_ah *ah)
596 struct ib_pd *pd;
597 int ret;
599 pd = ah->pd;
600 ret = ah->device->destroy_ah(ah);
601 if (!ret)
602 atomic_dec(&pd->usecnt);
604 return ret;
606 EXPORT_SYMBOL(rdma_destroy_ah);
608 /* Shared receive queues */
610 struct ib_srq *ib_create_srq(struct ib_pd *pd,
611 struct ib_srq_init_attr *srq_init_attr)
613 struct ib_srq *srq;
615 if (!pd->device->create_srq)
616 return ERR_PTR(-ENOSYS);
618 srq = pd->device->create_srq(pd, srq_init_attr, NULL);
620 if (!IS_ERR(srq)) {
621 srq->device = pd->device;
622 srq->pd = pd;
623 srq->uobject = NULL;
624 srq->event_handler = srq_init_attr->event_handler;
625 srq->srq_context = srq_init_attr->srq_context;
626 srq->srq_type = srq_init_attr->srq_type;
627 if (srq->srq_type == IB_SRQT_XRC) {
628 srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
629 srq->ext.xrc.cq = srq_init_attr->ext.xrc.cq;
630 atomic_inc(&srq->ext.xrc.xrcd->usecnt);
631 atomic_inc(&srq->ext.xrc.cq->usecnt);
633 atomic_inc(&pd->usecnt);
634 atomic_set(&srq->usecnt, 0);
637 return srq;
639 EXPORT_SYMBOL(ib_create_srq);
641 int ib_modify_srq(struct ib_srq *srq,
642 struct ib_srq_attr *srq_attr,
643 enum ib_srq_attr_mask srq_attr_mask)
645 return srq->device->modify_srq ?
646 srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
647 -ENOSYS;
649 EXPORT_SYMBOL(ib_modify_srq);
651 int ib_query_srq(struct ib_srq *srq,
652 struct ib_srq_attr *srq_attr)
654 return srq->device->query_srq ?
655 srq->device->query_srq(srq, srq_attr) : -ENOSYS;
657 EXPORT_SYMBOL(ib_query_srq);
659 int ib_destroy_srq(struct ib_srq *srq)
661 struct ib_pd *pd;
662 enum ib_srq_type srq_type;
663 struct ib_xrcd *uninitialized_var(xrcd);
664 struct ib_cq *uninitialized_var(cq);
665 int ret;
667 if (atomic_read(&srq->usecnt))
668 return -EBUSY;
670 pd = srq->pd;
671 srq_type = srq->srq_type;
672 if (srq_type == IB_SRQT_XRC) {
673 xrcd = srq->ext.xrc.xrcd;
674 cq = srq->ext.xrc.cq;
677 ret = srq->device->destroy_srq(srq);
678 if (!ret) {
679 atomic_dec(&pd->usecnt);
680 if (srq_type == IB_SRQT_XRC) {
681 atomic_dec(&xrcd->usecnt);
682 atomic_dec(&cq->usecnt);
686 return ret;
688 EXPORT_SYMBOL(ib_destroy_srq);
690 /* Queue pairs */
692 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
694 struct ib_qp *qp = context;
695 unsigned long flags;
697 spin_lock_irqsave(&qp->device->event_handler_lock, flags);
698 list_for_each_entry(event->element.qp, &qp->open_list, open_list)
699 if (event->element.qp->event_handler)
700 event->element.qp->event_handler(event, event->element.qp->qp_context);
701 spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
704 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
706 mutex_lock(&xrcd->tgt_qp_mutex);
707 list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
708 mutex_unlock(&xrcd->tgt_qp_mutex);
711 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
712 void (*event_handler)(struct ib_event *, void *),
713 void *qp_context)
715 struct ib_qp *qp;
716 unsigned long flags;
717 int err;
719 qp = kzalloc(sizeof *qp, GFP_KERNEL);
720 if (!qp)
721 return ERR_PTR(-ENOMEM);
723 qp->real_qp = real_qp;
724 err = ib_open_shared_qp_security(qp, real_qp->device);
725 if (err) {
726 kfree(qp);
727 return ERR_PTR(err);
730 qp->real_qp = real_qp;
731 atomic_inc(&real_qp->usecnt);
732 qp->device = real_qp->device;
733 qp->event_handler = event_handler;
734 qp->qp_context = qp_context;
735 qp->qp_num = real_qp->qp_num;
736 qp->qp_type = real_qp->qp_type;
738 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
739 list_add(&qp->open_list, &real_qp->open_list);
740 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
742 return qp;
745 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
746 struct ib_qp_open_attr *qp_open_attr)
748 struct ib_qp *qp, *real_qp;
750 if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
751 return ERR_PTR(-EINVAL);
753 qp = ERR_PTR(-EINVAL);
754 mutex_lock(&xrcd->tgt_qp_mutex);
755 list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
756 if (real_qp->qp_num == qp_open_attr->qp_num) {
757 qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
758 qp_open_attr->qp_context);
759 break;
762 mutex_unlock(&xrcd->tgt_qp_mutex);
763 return qp;
765 EXPORT_SYMBOL(ib_open_qp);
767 static struct ib_qp *ib_create_xrc_qp(struct ib_qp *qp,
768 struct ib_qp_init_attr *qp_init_attr)
770 struct ib_qp *real_qp = qp;
772 qp->event_handler = __ib_shared_qp_event_handler;
773 qp->qp_context = qp;
774 qp->pd = NULL;
775 qp->send_cq = qp->recv_cq = NULL;
776 qp->srq = NULL;
777 qp->xrcd = qp_init_attr->xrcd;
778 atomic_inc(&qp_init_attr->xrcd->usecnt);
779 INIT_LIST_HEAD(&qp->open_list);
781 qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
782 qp_init_attr->qp_context);
783 if (!IS_ERR(qp))
784 __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
785 else
786 real_qp->device->destroy_qp(real_qp);
787 return qp;
790 struct ib_qp *ib_create_qp(struct ib_pd *pd,
791 struct ib_qp_init_attr *qp_init_attr)
793 struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
794 struct ib_qp *qp;
795 int ret;
797 if (qp_init_attr->rwq_ind_tbl &&
798 (qp_init_attr->recv_cq ||
799 qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
800 qp_init_attr->cap.max_recv_sge))
801 return ERR_PTR(-EINVAL);
804 * If the callers is using the RDMA API calculate the resources
805 * needed for the RDMA READ/WRITE operations.
807 * Note that these callers need to pass in a port number.
809 if (qp_init_attr->cap.max_rdma_ctxs)
810 rdma_rw_init_qp(device, qp_init_attr);
812 qp = device->create_qp(pd, qp_init_attr, NULL);
813 if (IS_ERR(qp))
814 return qp;
816 ret = ib_create_qp_security(qp, device);
817 if (ret) {
818 ib_destroy_qp(qp);
819 return ERR_PTR(ret);
822 qp->device = device;
823 qp->real_qp = qp;
824 qp->uobject = NULL;
825 qp->qp_type = qp_init_attr->qp_type;
826 qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
828 atomic_set(&qp->usecnt, 0);
829 qp->mrs_used = 0;
830 spin_lock_init(&qp->mr_lock);
831 INIT_LIST_HEAD(&qp->rdma_mrs);
832 INIT_LIST_HEAD(&qp->sig_mrs);
834 if (qp_init_attr->qp_type == IB_QPT_XRC_TGT)
835 return ib_create_xrc_qp(qp, qp_init_attr);
837 qp->event_handler = qp_init_attr->event_handler;
838 qp->qp_context = qp_init_attr->qp_context;
839 if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
840 qp->recv_cq = NULL;
841 qp->srq = NULL;
842 } else {
843 qp->recv_cq = qp_init_attr->recv_cq;
844 if (qp_init_attr->recv_cq)
845 atomic_inc(&qp_init_attr->recv_cq->usecnt);
846 qp->srq = qp_init_attr->srq;
847 if (qp->srq)
848 atomic_inc(&qp_init_attr->srq->usecnt);
851 qp->pd = pd;
852 qp->send_cq = qp_init_attr->send_cq;
853 qp->xrcd = NULL;
855 atomic_inc(&pd->usecnt);
856 if (qp_init_attr->send_cq)
857 atomic_inc(&qp_init_attr->send_cq->usecnt);
858 if (qp_init_attr->rwq_ind_tbl)
859 atomic_inc(&qp->rwq_ind_tbl->usecnt);
861 if (qp_init_attr->cap.max_rdma_ctxs) {
862 ret = rdma_rw_init_mrs(qp, qp_init_attr);
863 if (ret) {
864 pr_err("failed to init MR pool ret= %d\n", ret);
865 ib_destroy_qp(qp);
866 return ERR_PTR(ret);
871 * Note: all hw drivers guarantee that max_send_sge is lower than
872 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
873 * max_send_sge <= max_sge_rd.
875 qp->max_write_sge = qp_init_attr->cap.max_send_sge;
876 qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
877 device->attrs.max_sge_rd);
879 return qp;
881 EXPORT_SYMBOL(ib_create_qp);
883 static const struct {
884 int valid;
885 enum ib_qp_attr_mask req_param[IB_QPT_MAX];
886 enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
887 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
888 [IB_QPS_RESET] = {
889 [IB_QPS_RESET] = { .valid = 1 },
890 [IB_QPS_INIT] = {
891 .valid = 1,
892 .req_param = {
893 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
894 IB_QP_PORT |
895 IB_QP_QKEY),
896 [IB_QPT_RAW_PACKET] = IB_QP_PORT,
897 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
898 IB_QP_PORT |
899 IB_QP_ACCESS_FLAGS),
900 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
901 IB_QP_PORT |
902 IB_QP_ACCESS_FLAGS),
903 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
904 IB_QP_PORT |
905 IB_QP_ACCESS_FLAGS),
906 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
907 IB_QP_PORT |
908 IB_QP_ACCESS_FLAGS),
909 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
910 IB_QP_QKEY),
911 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
912 IB_QP_QKEY),
916 [IB_QPS_INIT] = {
917 [IB_QPS_RESET] = { .valid = 1 },
918 [IB_QPS_ERR] = { .valid = 1 },
919 [IB_QPS_INIT] = {
920 .valid = 1,
921 .opt_param = {
922 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
923 IB_QP_PORT |
924 IB_QP_QKEY),
925 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
926 IB_QP_PORT |
927 IB_QP_ACCESS_FLAGS),
928 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
929 IB_QP_PORT |
930 IB_QP_ACCESS_FLAGS),
931 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
932 IB_QP_PORT |
933 IB_QP_ACCESS_FLAGS),
934 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
935 IB_QP_PORT |
936 IB_QP_ACCESS_FLAGS),
937 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
938 IB_QP_QKEY),
939 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
940 IB_QP_QKEY),
943 [IB_QPS_RTR] = {
944 .valid = 1,
945 .req_param = {
946 [IB_QPT_UC] = (IB_QP_AV |
947 IB_QP_PATH_MTU |
948 IB_QP_DEST_QPN |
949 IB_QP_RQ_PSN),
950 [IB_QPT_RC] = (IB_QP_AV |
951 IB_QP_PATH_MTU |
952 IB_QP_DEST_QPN |
953 IB_QP_RQ_PSN |
954 IB_QP_MAX_DEST_RD_ATOMIC |
955 IB_QP_MIN_RNR_TIMER),
956 [IB_QPT_XRC_INI] = (IB_QP_AV |
957 IB_QP_PATH_MTU |
958 IB_QP_DEST_QPN |
959 IB_QP_RQ_PSN),
960 [IB_QPT_XRC_TGT] = (IB_QP_AV |
961 IB_QP_PATH_MTU |
962 IB_QP_DEST_QPN |
963 IB_QP_RQ_PSN |
964 IB_QP_MAX_DEST_RD_ATOMIC |
965 IB_QP_MIN_RNR_TIMER),
967 .opt_param = {
968 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
969 IB_QP_QKEY),
970 [IB_QPT_UC] = (IB_QP_ALT_PATH |
971 IB_QP_ACCESS_FLAGS |
972 IB_QP_PKEY_INDEX),
973 [IB_QPT_RC] = (IB_QP_ALT_PATH |
974 IB_QP_ACCESS_FLAGS |
975 IB_QP_PKEY_INDEX),
976 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
977 IB_QP_ACCESS_FLAGS |
978 IB_QP_PKEY_INDEX),
979 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
980 IB_QP_ACCESS_FLAGS |
981 IB_QP_PKEY_INDEX),
982 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
983 IB_QP_QKEY),
984 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
985 IB_QP_QKEY),
989 [IB_QPS_RTR] = {
990 [IB_QPS_RESET] = { .valid = 1 },
991 [IB_QPS_ERR] = { .valid = 1 },
992 [IB_QPS_RTS] = {
993 .valid = 1,
994 .req_param = {
995 [IB_QPT_UD] = IB_QP_SQ_PSN,
996 [IB_QPT_UC] = IB_QP_SQ_PSN,
997 [IB_QPT_RC] = (IB_QP_TIMEOUT |
998 IB_QP_RETRY_CNT |
999 IB_QP_RNR_RETRY |
1000 IB_QP_SQ_PSN |
1001 IB_QP_MAX_QP_RD_ATOMIC),
1002 [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
1003 IB_QP_RETRY_CNT |
1004 IB_QP_RNR_RETRY |
1005 IB_QP_SQ_PSN |
1006 IB_QP_MAX_QP_RD_ATOMIC),
1007 [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
1008 IB_QP_SQ_PSN),
1009 [IB_QPT_SMI] = IB_QP_SQ_PSN,
1010 [IB_QPT_GSI] = IB_QP_SQ_PSN,
1012 .opt_param = {
1013 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1014 IB_QP_QKEY),
1015 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1016 IB_QP_ALT_PATH |
1017 IB_QP_ACCESS_FLAGS |
1018 IB_QP_PATH_MIG_STATE),
1019 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1020 IB_QP_ALT_PATH |
1021 IB_QP_ACCESS_FLAGS |
1022 IB_QP_MIN_RNR_TIMER |
1023 IB_QP_PATH_MIG_STATE),
1024 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1025 IB_QP_ALT_PATH |
1026 IB_QP_ACCESS_FLAGS |
1027 IB_QP_PATH_MIG_STATE),
1028 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1029 IB_QP_ALT_PATH |
1030 IB_QP_ACCESS_FLAGS |
1031 IB_QP_MIN_RNR_TIMER |
1032 IB_QP_PATH_MIG_STATE),
1033 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1034 IB_QP_QKEY),
1035 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1036 IB_QP_QKEY),
1037 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1041 [IB_QPS_RTS] = {
1042 [IB_QPS_RESET] = { .valid = 1 },
1043 [IB_QPS_ERR] = { .valid = 1 },
1044 [IB_QPS_RTS] = {
1045 .valid = 1,
1046 .opt_param = {
1047 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1048 IB_QP_QKEY),
1049 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1050 IB_QP_ACCESS_FLAGS |
1051 IB_QP_ALT_PATH |
1052 IB_QP_PATH_MIG_STATE),
1053 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1054 IB_QP_ACCESS_FLAGS |
1055 IB_QP_ALT_PATH |
1056 IB_QP_PATH_MIG_STATE |
1057 IB_QP_MIN_RNR_TIMER),
1058 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1059 IB_QP_ACCESS_FLAGS |
1060 IB_QP_ALT_PATH |
1061 IB_QP_PATH_MIG_STATE),
1062 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1063 IB_QP_ACCESS_FLAGS |
1064 IB_QP_ALT_PATH |
1065 IB_QP_PATH_MIG_STATE |
1066 IB_QP_MIN_RNR_TIMER),
1067 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1068 IB_QP_QKEY),
1069 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1070 IB_QP_QKEY),
1071 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1074 [IB_QPS_SQD] = {
1075 .valid = 1,
1076 .opt_param = {
1077 [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1078 [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1079 [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1080 [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1081 [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1082 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1083 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1087 [IB_QPS_SQD] = {
1088 [IB_QPS_RESET] = { .valid = 1 },
1089 [IB_QPS_ERR] = { .valid = 1 },
1090 [IB_QPS_RTS] = {
1091 .valid = 1,
1092 .opt_param = {
1093 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1094 IB_QP_QKEY),
1095 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1096 IB_QP_ALT_PATH |
1097 IB_QP_ACCESS_FLAGS |
1098 IB_QP_PATH_MIG_STATE),
1099 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1100 IB_QP_ALT_PATH |
1101 IB_QP_ACCESS_FLAGS |
1102 IB_QP_MIN_RNR_TIMER |
1103 IB_QP_PATH_MIG_STATE),
1104 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1105 IB_QP_ALT_PATH |
1106 IB_QP_ACCESS_FLAGS |
1107 IB_QP_PATH_MIG_STATE),
1108 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1109 IB_QP_ALT_PATH |
1110 IB_QP_ACCESS_FLAGS |
1111 IB_QP_MIN_RNR_TIMER |
1112 IB_QP_PATH_MIG_STATE),
1113 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1114 IB_QP_QKEY),
1115 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1116 IB_QP_QKEY),
1119 [IB_QPS_SQD] = {
1120 .valid = 1,
1121 .opt_param = {
1122 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1123 IB_QP_QKEY),
1124 [IB_QPT_UC] = (IB_QP_AV |
1125 IB_QP_ALT_PATH |
1126 IB_QP_ACCESS_FLAGS |
1127 IB_QP_PKEY_INDEX |
1128 IB_QP_PATH_MIG_STATE),
1129 [IB_QPT_RC] = (IB_QP_PORT |
1130 IB_QP_AV |
1131 IB_QP_TIMEOUT |
1132 IB_QP_RETRY_CNT |
1133 IB_QP_RNR_RETRY |
1134 IB_QP_MAX_QP_RD_ATOMIC |
1135 IB_QP_MAX_DEST_RD_ATOMIC |
1136 IB_QP_ALT_PATH |
1137 IB_QP_ACCESS_FLAGS |
1138 IB_QP_PKEY_INDEX |
1139 IB_QP_MIN_RNR_TIMER |
1140 IB_QP_PATH_MIG_STATE),
1141 [IB_QPT_XRC_INI] = (IB_QP_PORT |
1142 IB_QP_AV |
1143 IB_QP_TIMEOUT |
1144 IB_QP_RETRY_CNT |
1145 IB_QP_RNR_RETRY |
1146 IB_QP_MAX_QP_RD_ATOMIC |
1147 IB_QP_ALT_PATH |
1148 IB_QP_ACCESS_FLAGS |
1149 IB_QP_PKEY_INDEX |
1150 IB_QP_PATH_MIG_STATE),
1151 [IB_QPT_XRC_TGT] = (IB_QP_PORT |
1152 IB_QP_AV |
1153 IB_QP_TIMEOUT |
1154 IB_QP_MAX_DEST_RD_ATOMIC |
1155 IB_QP_ALT_PATH |
1156 IB_QP_ACCESS_FLAGS |
1157 IB_QP_PKEY_INDEX |
1158 IB_QP_MIN_RNR_TIMER |
1159 IB_QP_PATH_MIG_STATE),
1160 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1161 IB_QP_QKEY),
1162 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1163 IB_QP_QKEY),
1167 [IB_QPS_SQE] = {
1168 [IB_QPS_RESET] = { .valid = 1 },
1169 [IB_QPS_ERR] = { .valid = 1 },
1170 [IB_QPS_RTS] = {
1171 .valid = 1,
1172 .opt_param = {
1173 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1174 IB_QP_QKEY),
1175 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1176 IB_QP_ACCESS_FLAGS),
1177 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1178 IB_QP_QKEY),
1179 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1180 IB_QP_QKEY),
1184 [IB_QPS_ERR] = {
1185 [IB_QPS_RESET] = { .valid = 1 },
1186 [IB_QPS_ERR] = { .valid = 1 }
1190 int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1191 enum ib_qp_type type, enum ib_qp_attr_mask mask,
1192 enum rdma_link_layer ll)
1194 enum ib_qp_attr_mask req_param, opt_param;
1196 if (cur_state < 0 || cur_state > IB_QPS_ERR ||
1197 next_state < 0 || next_state > IB_QPS_ERR)
1198 return 0;
1200 if (mask & IB_QP_CUR_STATE &&
1201 cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1202 cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1203 return 0;
1205 if (!qp_state_table[cur_state][next_state].valid)
1206 return 0;
1208 req_param = qp_state_table[cur_state][next_state].req_param[type];
1209 opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1211 if ((mask & req_param) != req_param)
1212 return 0;
1214 if (mask & ~(req_param | opt_param | IB_QP_STATE))
1215 return 0;
1217 return 1;
1219 EXPORT_SYMBOL(ib_modify_qp_is_ok);
1221 int ib_resolve_eth_dmac(struct ib_device *device,
1222 struct rdma_ah_attr *ah_attr)
1224 int ret = 0;
1225 struct ib_global_route *grh;
1227 if (!rdma_is_port_valid(device, rdma_ah_get_port_num(ah_attr)))
1228 return -EINVAL;
1230 if (ah_attr->type != RDMA_AH_ATTR_TYPE_ROCE)
1231 return 0;
1233 grh = rdma_ah_retrieve_grh(ah_attr);
1235 if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw)) {
1236 rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
1237 ah_attr->roce.dmac);
1238 } else {
1239 union ib_gid sgid;
1240 struct ib_gid_attr sgid_attr;
1241 int ifindex;
1242 int hop_limit;
1244 ret = ib_query_gid(device,
1245 rdma_ah_get_port_num(ah_attr),
1246 grh->sgid_index,
1247 &sgid, &sgid_attr);
1249 if (ret || !sgid_attr.ndev) {
1250 if (!ret)
1251 ret = -ENXIO;
1252 goto out;
1255 ifindex = sgid_attr.ndev->ifindex;
1257 ret =
1258 rdma_addr_find_l2_eth_by_grh(&sgid, &grh->dgid,
1259 ah_attr->roce.dmac,
1260 NULL, &ifindex, &hop_limit);
1262 dev_put(sgid_attr.ndev);
1264 grh->hop_limit = hop_limit;
1266 out:
1267 return ret;
1269 EXPORT_SYMBOL(ib_resolve_eth_dmac);
1271 int ib_modify_qp(struct ib_qp *qp,
1272 struct ib_qp_attr *qp_attr,
1273 int qp_attr_mask)
1276 if (qp_attr_mask & IB_QP_AV) {
1277 int ret;
1279 ret = ib_resolve_eth_dmac(qp->device, &qp_attr->ah_attr);
1280 if (ret)
1281 return ret;
1284 return ib_security_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
1286 EXPORT_SYMBOL(ib_modify_qp);
1288 int ib_query_qp(struct ib_qp *qp,
1289 struct ib_qp_attr *qp_attr,
1290 int qp_attr_mask,
1291 struct ib_qp_init_attr *qp_init_attr)
1293 return qp->device->query_qp ?
1294 qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
1295 -ENOSYS;
1297 EXPORT_SYMBOL(ib_query_qp);
1299 int ib_close_qp(struct ib_qp *qp)
1301 struct ib_qp *real_qp;
1302 unsigned long flags;
1304 real_qp = qp->real_qp;
1305 if (real_qp == qp)
1306 return -EINVAL;
1308 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1309 list_del(&qp->open_list);
1310 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1312 atomic_dec(&real_qp->usecnt);
1313 ib_close_shared_qp_security(qp->qp_sec);
1314 kfree(qp);
1316 return 0;
1318 EXPORT_SYMBOL(ib_close_qp);
1320 static int __ib_destroy_shared_qp(struct ib_qp *qp)
1322 struct ib_xrcd *xrcd;
1323 struct ib_qp *real_qp;
1324 int ret;
1326 real_qp = qp->real_qp;
1327 xrcd = real_qp->xrcd;
1329 mutex_lock(&xrcd->tgt_qp_mutex);
1330 ib_close_qp(qp);
1331 if (atomic_read(&real_qp->usecnt) == 0)
1332 list_del(&real_qp->xrcd_list);
1333 else
1334 real_qp = NULL;
1335 mutex_unlock(&xrcd->tgt_qp_mutex);
1337 if (real_qp) {
1338 ret = ib_destroy_qp(real_qp);
1339 if (!ret)
1340 atomic_dec(&xrcd->usecnt);
1341 else
1342 __ib_insert_xrcd_qp(xrcd, real_qp);
1345 return 0;
1348 int ib_destroy_qp(struct ib_qp *qp)
1350 struct ib_pd *pd;
1351 struct ib_cq *scq, *rcq;
1352 struct ib_srq *srq;
1353 struct ib_rwq_ind_table *ind_tbl;
1354 struct ib_qp_security *sec;
1355 int ret;
1357 WARN_ON_ONCE(qp->mrs_used > 0);
1359 if (atomic_read(&qp->usecnt))
1360 return -EBUSY;
1362 if (qp->real_qp != qp)
1363 return __ib_destroy_shared_qp(qp);
1365 pd = qp->pd;
1366 scq = qp->send_cq;
1367 rcq = qp->recv_cq;
1368 srq = qp->srq;
1369 ind_tbl = qp->rwq_ind_tbl;
1370 sec = qp->qp_sec;
1371 if (sec)
1372 ib_destroy_qp_security_begin(sec);
1374 if (!qp->uobject)
1375 rdma_rw_cleanup_mrs(qp);
1377 ret = qp->device->destroy_qp(qp);
1378 if (!ret) {
1379 if (pd)
1380 atomic_dec(&pd->usecnt);
1381 if (scq)
1382 atomic_dec(&scq->usecnt);
1383 if (rcq)
1384 atomic_dec(&rcq->usecnt);
1385 if (srq)
1386 atomic_dec(&srq->usecnt);
1387 if (ind_tbl)
1388 atomic_dec(&ind_tbl->usecnt);
1389 if (sec)
1390 ib_destroy_qp_security_end(sec);
1391 } else {
1392 if (sec)
1393 ib_destroy_qp_security_abort(sec);
1396 return ret;
1398 EXPORT_SYMBOL(ib_destroy_qp);
1400 /* Completion queues */
1402 struct ib_cq *ib_create_cq(struct ib_device *device,
1403 ib_comp_handler comp_handler,
1404 void (*event_handler)(struct ib_event *, void *),
1405 void *cq_context,
1406 const struct ib_cq_init_attr *cq_attr)
1408 struct ib_cq *cq;
1410 cq = device->create_cq(device, cq_attr, NULL, NULL);
1412 if (!IS_ERR(cq)) {
1413 cq->device = device;
1414 cq->uobject = NULL;
1415 cq->comp_handler = comp_handler;
1416 cq->event_handler = event_handler;
1417 cq->cq_context = cq_context;
1418 atomic_set(&cq->usecnt, 0);
1421 return cq;
1423 EXPORT_SYMBOL(ib_create_cq);
1425 int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1427 return cq->device->modify_cq ?
1428 cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
1430 EXPORT_SYMBOL(ib_modify_cq);
1432 int ib_destroy_cq(struct ib_cq *cq)
1434 if (atomic_read(&cq->usecnt))
1435 return -EBUSY;
1437 return cq->device->destroy_cq(cq);
1439 EXPORT_SYMBOL(ib_destroy_cq);
1441 int ib_resize_cq(struct ib_cq *cq, int cqe)
1443 return cq->device->resize_cq ?
1444 cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
1446 EXPORT_SYMBOL(ib_resize_cq);
1448 /* Memory regions */
1450 int ib_dereg_mr(struct ib_mr *mr)
1452 struct ib_pd *pd = mr->pd;
1453 int ret;
1455 ret = mr->device->dereg_mr(mr);
1456 if (!ret)
1457 atomic_dec(&pd->usecnt);
1459 return ret;
1461 EXPORT_SYMBOL(ib_dereg_mr);
1464 * ib_alloc_mr() - Allocates a memory region
1465 * @pd: protection domain associated with the region
1466 * @mr_type: memory region type
1467 * @max_num_sg: maximum sg entries available for registration.
1469 * Notes:
1470 * Memory registeration page/sg lists must not exceed max_num_sg.
1471 * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
1472 * max_num_sg * used_page_size.
1475 struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
1476 enum ib_mr_type mr_type,
1477 u32 max_num_sg)
1479 struct ib_mr *mr;
1481 if (!pd->device->alloc_mr)
1482 return ERR_PTR(-ENOSYS);
1484 mr = pd->device->alloc_mr(pd, mr_type, max_num_sg);
1485 if (!IS_ERR(mr)) {
1486 mr->device = pd->device;
1487 mr->pd = pd;
1488 mr->uobject = NULL;
1489 atomic_inc(&pd->usecnt);
1490 mr->need_inval = false;
1493 return mr;
1495 EXPORT_SYMBOL(ib_alloc_mr);
1497 /* "Fast" memory regions */
1499 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
1500 int mr_access_flags,
1501 struct ib_fmr_attr *fmr_attr)
1503 struct ib_fmr *fmr;
1505 if (!pd->device->alloc_fmr)
1506 return ERR_PTR(-ENOSYS);
1508 fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
1509 if (!IS_ERR(fmr)) {
1510 fmr->device = pd->device;
1511 fmr->pd = pd;
1512 atomic_inc(&pd->usecnt);
1515 return fmr;
1517 EXPORT_SYMBOL(ib_alloc_fmr);
1519 int ib_unmap_fmr(struct list_head *fmr_list)
1521 struct ib_fmr *fmr;
1523 if (list_empty(fmr_list))
1524 return 0;
1526 fmr = list_entry(fmr_list->next, struct ib_fmr, list);
1527 return fmr->device->unmap_fmr(fmr_list);
1529 EXPORT_SYMBOL(ib_unmap_fmr);
1531 int ib_dealloc_fmr(struct ib_fmr *fmr)
1533 struct ib_pd *pd;
1534 int ret;
1536 pd = fmr->pd;
1537 ret = fmr->device->dealloc_fmr(fmr);
1538 if (!ret)
1539 atomic_dec(&pd->usecnt);
1541 return ret;
1543 EXPORT_SYMBOL(ib_dealloc_fmr);
1545 /* Multicast groups */
1547 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1549 int ret;
1551 if (!qp->device->attach_mcast)
1552 return -ENOSYS;
1553 if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD ||
1554 lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
1555 lid == be16_to_cpu(IB_LID_PERMISSIVE))
1556 return -EINVAL;
1558 ret = qp->device->attach_mcast(qp, gid, lid);
1559 if (!ret)
1560 atomic_inc(&qp->usecnt);
1561 return ret;
1563 EXPORT_SYMBOL(ib_attach_mcast);
1565 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1567 int ret;
1569 if (!qp->device->detach_mcast)
1570 return -ENOSYS;
1571 if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD ||
1572 lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
1573 lid == be16_to_cpu(IB_LID_PERMISSIVE))
1574 return -EINVAL;
1576 ret = qp->device->detach_mcast(qp, gid, lid);
1577 if (!ret)
1578 atomic_dec(&qp->usecnt);
1579 return ret;
1581 EXPORT_SYMBOL(ib_detach_mcast);
1583 struct ib_xrcd *ib_alloc_xrcd(struct ib_device *device)
1585 struct ib_xrcd *xrcd;
1587 if (!device->alloc_xrcd)
1588 return ERR_PTR(-ENOSYS);
1590 xrcd = device->alloc_xrcd(device, NULL, NULL);
1591 if (!IS_ERR(xrcd)) {
1592 xrcd->device = device;
1593 xrcd->inode = NULL;
1594 atomic_set(&xrcd->usecnt, 0);
1595 mutex_init(&xrcd->tgt_qp_mutex);
1596 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
1599 return xrcd;
1601 EXPORT_SYMBOL(ib_alloc_xrcd);
1603 int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1605 struct ib_qp *qp;
1606 int ret;
1608 if (atomic_read(&xrcd->usecnt))
1609 return -EBUSY;
1611 while (!list_empty(&xrcd->tgt_qp_list)) {
1612 qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
1613 ret = ib_destroy_qp(qp);
1614 if (ret)
1615 return ret;
1618 return xrcd->device->dealloc_xrcd(xrcd);
1620 EXPORT_SYMBOL(ib_dealloc_xrcd);
1623 * ib_create_wq - Creates a WQ associated with the specified protection
1624 * domain.
1625 * @pd: The protection domain associated with the WQ.
1626 * @wq_init_attr: A list of initial attributes required to create the
1627 * WQ. If WQ creation succeeds, then the attributes are updated to
1628 * the actual capabilities of the created WQ.
1630 * wq_init_attr->max_wr and wq_init_attr->max_sge determine
1631 * the requested size of the WQ, and set to the actual values allocated
1632 * on return.
1633 * If ib_create_wq() succeeds, then max_wr and max_sge will always be
1634 * at least as large as the requested values.
1636 struct ib_wq *ib_create_wq(struct ib_pd *pd,
1637 struct ib_wq_init_attr *wq_attr)
1639 struct ib_wq *wq;
1641 if (!pd->device->create_wq)
1642 return ERR_PTR(-ENOSYS);
1644 wq = pd->device->create_wq(pd, wq_attr, NULL);
1645 if (!IS_ERR(wq)) {
1646 wq->event_handler = wq_attr->event_handler;
1647 wq->wq_context = wq_attr->wq_context;
1648 wq->wq_type = wq_attr->wq_type;
1649 wq->cq = wq_attr->cq;
1650 wq->device = pd->device;
1651 wq->pd = pd;
1652 wq->uobject = NULL;
1653 atomic_inc(&pd->usecnt);
1654 atomic_inc(&wq_attr->cq->usecnt);
1655 atomic_set(&wq->usecnt, 0);
1657 return wq;
1659 EXPORT_SYMBOL(ib_create_wq);
1662 * ib_destroy_wq - Destroys the specified WQ.
1663 * @wq: The WQ to destroy.
1665 int ib_destroy_wq(struct ib_wq *wq)
1667 int err;
1668 struct ib_cq *cq = wq->cq;
1669 struct ib_pd *pd = wq->pd;
1671 if (atomic_read(&wq->usecnt))
1672 return -EBUSY;
1674 err = wq->device->destroy_wq(wq);
1675 if (!err) {
1676 atomic_dec(&pd->usecnt);
1677 atomic_dec(&cq->usecnt);
1679 return err;
1681 EXPORT_SYMBOL(ib_destroy_wq);
1684 * ib_modify_wq - Modifies the specified WQ.
1685 * @wq: The WQ to modify.
1686 * @wq_attr: On input, specifies the WQ attributes to modify.
1687 * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
1688 * are being modified.
1689 * On output, the current values of selected WQ attributes are returned.
1691 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
1692 u32 wq_attr_mask)
1694 int err;
1696 if (!wq->device->modify_wq)
1697 return -ENOSYS;
1699 err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
1700 return err;
1702 EXPORT_SYMBOL(ib_modify_wq);
1705 * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
1706 * @device: The device on which to create the rwq indirection table.
1707 * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
1708 * create the Indirection Table.
1710 * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
1711 * than the created ib_rwq_ind_table object and the caller is responsible
1712 * for its memory allocation/free.
1714 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
1715 struct ib_rwq_ind_table_init_attr *init_attr)
1717 struct ib_rwq_ind_table *rwq_ind_table;
1718 int i;
1719 u32 table_size;
1721 if (!device->create_rwq_ind_table)
1722 return ERR_PTR(-ENOSYS);
1724 table_size = (1 << init_attr->log_ind_tbl_size);
1725 rwq_ind_table = device->create_rwq_ind_table(device,
1726 init_attr, NULL);
1727 if (IS_ERR(rwq_ind_table))
1728 return rwq_ind_table;
1730 rwq_ind_table->ind_tbl = init_attr->ind_tbl;
1731 rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
1732 rwq_ind_table->device = device;
1733 rwq_ind_table->uobject = NULL;
1734 atomic_set(&rwq_ind_table->usecnt, 0);
1736 for (i = 0; i < table_size; i++)
1737 atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
1739 return rwq_ind_table;
1741 EXPORT_SYMBOL(ib_create_rwq_ind_table);
1744 * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
1745 * @wq_ind_table: The Indirection Table to destroy.
1747 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
1749 int err, i;
1750 u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
1751 struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
1753 if (atomic_read(&rwq_ind_table->usecnt))
1754 return -EBUSY;
1756 err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
1757 if (!err) {
1758 for (i = 0; i < table_size; i++)
1759 atomic_dec(&ind_tbl[i]->usecnt);
1762 return err;
1764 EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
1766 struct ib_flow *ib_create_flow(struct ib_qp *qp,
1767 struct ib_flow_attr *flow_attr,
1768 int domain)
1770 struct ib_flow *flow_id;
1771 if (!qp->device->create_flow)
1772 return ERR_PTR(-ENOSYS);
1774 flow_id = qp->device->create_flow(qp, flow_attr, domain);
1775 if (!IS_ERR(flow_id)) {
1776 atomic_inc(&qp->usecnt);
1777 flow_id->qp = qp;
1779 return flow_id;
1781 EXPORT_SYMBOL(ib_create_flow);
1783 int ib_destroy_flow(struct ib_flow *flow_id)
1785 int err;
1786 struct ib_qp *qp = flow_id->qp;
1788 err = qp->device->destroy_flow(flow_id);
1789 if (!err)
1790 atomic_dec(&qp->usecnt);
1791 return err;
1793 EXPORT_SYMBOL(ib_destroy_flow);
1795 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
1796 struct ib_mr_status *mr_status)
1798 return mr->device->check_mr_status ?
1799 mr->device->check_mr_status(mr, check_mask, mr_status) : -ENOSYS;
1801 EXPORT_SYMBOL(ib_check_mr_status);
1803 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
1804 int state)
1806 if (!device->set_vf_link_state)
1807 return -ENOSYS;
1809 return device->set_vf_link_state(device, vf, port, state);
1811 EXPORT_SYMBOL(ib_set_vf_link_state);
1813 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
1814 struct ifla_vf_info *info)
1816 if (!device->get_vf_config)
1817 return -ENOSYS;
1819 return device->get_vf_config(device, vf, port, info);
1821 EXPORT_SYMBOL(ib_get_vf_config);
1823 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
1824 struct ifla_vf_stats *stats)
1826 if (!device->get_vf_stats)
1827 return -ENOSYS;
1829 return device->get_vf_stats(device, vf, port, stats);
1831 EXPORT_SYMBOL(ib_get_vf_stats);
1833 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
1834 int type)
1836 if (!device->set_vf_guid)
1837 return -ENOSYS;
1839 return device->set_vf_guid(device, vf, port, guid, type);
1841 EXPORT_SYMBOL(ib_set_vf_guid);
1844 * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
1845 * and set it the memory region.
1846 * @mr: memory region
1847 * @sg: dma mapped scatterlist
1848 * @sg_nents: number of entries in sg
1849 * @sg_offset: offset in bytes into sg
1850 * @page_size: page vector desired page size
1852 * Constraints:
1853 * - The first sg element is allowed to have an offset.
1854 * - Each sg element must either be aligned to page_size or virtually
1855 * contiguous to the previous element. In case an sg element has a
1856 * non-contiguous offset, the mapping prefix will not include it.
1857 * - The last sg element is allowed to have length less than page_size.
1858 * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
1859 * then only max_num_sg entries will be mapped.
1860 * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
1861 * constraints holds and the page_size argument is ignored.
1863 * Returns the number of sg elements that were mapped to the memory region.
1865 * After this completes successfully, the memory region
1866 * is ready for registration.
1868 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
1869 unsigned int *sg_offset, unsigned int page_size)
1871 if (unlikely(!mr->device->map_mr_sg))
1872 return -ENOSYS;
1874 mr->page_size = page_size;
1876 return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
1878 EXPORT_SYMBOL(ib_map_mr_sg);
1881 * ib_sg_to_pages() - Convert the largest prefix of a sg list
1882 * to a page vector
1883 * @mr: memory region
1884 * @sgl: dma mapped scatterlist
1885 * @sg_nents: number of entries in sg
1886 * @sg_offset_p: IN: start offset in bytes into sg
1887 * OUT: offset in bytes for element n of the sg of the first
1888 * byte that has not been processed where n is the return
1889 * value of this function.
1890 * @set_page: driver page assignment function pointer
1892 * Core service helper for drivers to convert the largest
1893 * prefix of given sg list to a page vector. The sg list
1894 * prefix converted is the prefix that meet the requirements
1895 * of ib_map_mr_sg.
1897 * Returns the number of sg elements that were assigned to
1898 * a page vector.
1900 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
1901 unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
1903 struct scatterlist *sg;
1904 u64 last_end_dma_addr = 0;
1905 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
1906 unsigned int last_page_off = 0;
1907 u64 page_mask = ~((u64)mr->page_size - 1);
1908 int i, ret;
1910 if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
1911 return -EINVAL;
1913 mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
1914 mr->length = 0;
1916 for_each_sg(sgl, sg, sg_nents, i) {
1917 u64 dma_addr = sg_dma_address(sg) + sg_offset;
1918 u64 prev_addr = dma_addr;
1919 unsigned int dma_len = sg_dma_len(sg) - sg_offset;
1920 u64 end_dma_addr = dma_addr + dma_len;
1921 u64 page_addr = dma_addr & page_mask;
1924 * For the second and later elements, check whether either the
1925 * end of element i-1 or the start of element i is not aligned
1926 * on a page boundary.
1928 if (i && (last_page_off != 0 || page_addr != dma_addr)) {
1929 /* Stop mapping if there is a gap. */
1930 if (last_end_dma_addr != dma_addr)
1931 break;
1934 * Coalesce this element with the last. If it is small
1935 * enough just update mr->length. Otherwise start
1936 * mapping from the next page.
1938 goto next_page;
1941 do {
1942 ret = set_page(mr, page_addr);
1943 if (unlikely(ret < 0)) {
1944 sg_offset = prev_addr - sg_dma_address(sg);
1945 mr->length += prev_addr - dma_addr;
1946 if (sg_offset_p)
1947 *sg_offset_p = sg_offset;
1948 return i || sg_offset ? i : ret;
1950 prev_addr = page_addr;
1951 next_page:
1952 page_addr += mr->page_size;
1953 } while (page_addr < end_dma_addr);
1955 mr->length += dma_len;
1956 last_end_dma_addr = end_dma_addr;
1957 last_page_off = end_dma_addr & ~page_mask;
1959 sg_offset = 0;
1962 if (sg_offset_p)
1963 *sg_offset_p = 0;
1964 return i;
1966 EXPORT_SYMBOL(ib_sg_to_pages);
1968 struct ib_drain_cqe {
1969 struct ib_cqe cqe;
1970 struct completion done;
1973 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
1975 struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
1976 cqe);
1978 complete(&cqe->done);
1982 * Post a WR and block until its completion is reaped for the SQ.
1984 static void __ib_drain_sq(struct ib_qp *qp)
1986 struct ib_cq *cq = qp->send_cq;
1987 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
1988 struct ib_drain_cqe sdrain;
1989 struct ib_send_wr swr = {}, *bad_swr;
1990 int ret;
1992 swr.wr_cqe = &sdrain.cqe;
1993 sdrain.cqe.done = ib_drain_qp_done;
1994 init_completion(&sdrain.done);
1996 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
1997 if (ret) {
1998 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
1999 return;
2002 ret = ib_post_send(qp, &swr, &bad_swr);
2003 if (ret) {
2004 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2005 return;
2008 if (cq->poll_ctx == IB_POLL_DIRECT)
2009 while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
2010 ib_process_cq_direct(cq, -1);
2011 else
2012 wait_for_completion(&sdrain.done);
2016 * Post a WR and block until its completion is reaped for the RQ.
2018 static void __ib_drain_rq(struct ib_qp *qp)
2020 struct ib_cq *cq = qp->recv_cq;
2021 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2022 struct ib_drain_cqe rdrain;
2023 struct ib_recv_wr rwr = {}, *bad_rwr;
2024 int ret;
2026 rwr.wr_cqe = &rdrain.cqe;
2027 rdrain.cqe.done = ib_drain_qp_done;
2028 init_completion(&rdrain.done);
2030 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2031 if (ret) {
2032 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2033 return;
2036 ret = ib_post_recv(qp, &rwr, &bad_rwr);
2037 if (ret) {
2038 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2039 return;
2042 if (cq->poll_ctx == IB_POLL_DIRECT)
2043 while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
2044 ib_process_cq_direct(cq, -1);
2045 else
2046 wait_for_completion(&rdrain.done);
2050 * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2051 * application.
2052 * @qp: queue pair to drain
2054 * If the device has a provider-specific drain function, then
2055 * call that. Otherwise call the generic drain function
2056 * __ib_drain_sq().
2058 * The caller must:
2060 * ensure there is room in the CQ and SQ for the drain work request and
2061 * completion.
2063 * allocate the CQ using ib_alloc_cq().
2065 * ensure that there are no other contexts that are posting WRs concurrently.
2066 * Otherwise the drain is not guaranteed.
2068 void ib_drain_sq(struct ib_qp *qp)
2070 if (qp->device->drain_sq)
2071 qp->device->drain_sq(qp);
2072 else
2073 __ib_drain_sq(qp);
2075 EXPORT_SYMBOL(ib_drain_sq);
2078 * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2079 * application.
2080 * @qp: queue pair to drain
2082 * If the device has a provider-specific drain function, then
2083 * call that. Otherwise call the generic drain function
2084 * __ib_drain_rq().
2086 * The caller must:
2088 * ensure there is room in the CQ and RQ for the drain work request and
2089 * completion.
2091 * allocate the CQ using ib_alloc_cq().
2093 * ensure that there are no other contexts that are posting WRs concurrently.
2094 * Otherwise the drain is not guaranteed.
2096 void ib_drain_rq(struct ib_qp *qp)
2098 if (qp->device->drain_rq)
2099 qp->device->drain_rq(qp);
2100 else
2101 __ib_drain_rq(qp);
2103 EXPORT_SYMBOL(ib_drain_rq);
2106 * ib_drain_qp() - Block until all CQEs have been consumed by the
2107 * application on both the RQ and SQ.
2108 * @qp: queue pair to drain
2110 * The caller must:
2112 * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2113 * and completions.
2115 * allocate the CQs using ib_alloc_cq().
2117 * ensure that there are no other contexts that are posting WRs concurrently.
2118 * Otherwise the drain is not guaranteed.
2120 void ib_drain_qp(struct ib_qp *qp)
2122 ib_drain_sq(qp);
2123 if (!qp->srq)
2124 ib_drain_rq(qp);
2126 EXPORT_SYMBOL(ib_drain_qp);