x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / infiniband / core / verbs.c
blob16ebc6372c31abe449f4e892d26e2d384cec56fc
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 int ib_resolve_eth_dmac(struct ib_device *device,
57 struct rdma_ah_attr *ah_attr);
59 static const char * const ib_events[] = {
60 [IB_EVENT_CQ_ERR] = "CQ error",
61 [IB_EVENT_QP_FATAL] = "QP fatal error",
62 [IB_EVENT_QP_REQ_ERR] = "QP request error",
63 [IB_EVENT_QP_ACCESS_ERR] = "QP access error",
64 [IB_EVENT_COMM_EST] = "communication established",
65 [IB_EVENT_SQ_DRAINED] = "send queue drained",
66 [IB_EVENT_PATH_MIG] = "path migration successful",
67 [IB_EVENT_PATH_MIG_ERR] = "path migration error",
68 [IB_EVENT_DEVICE_FATAL] = "device fatal error",
69 [IB_EVENT_PORT_ACTIVE] = "port active",
70 [IB_EVENT_PORT_ERR] = "port error",
71 [IB_EVENT_LID_CHANGE] = "LID change",
72 [IB_EVENT_PKEY_CHANGE] = "P_key change",
73 [IB_EVENT_SM_CHANGE] = "SM change",
74 [IB_EVENT_SRQ_ERR] = "SRQ error",
75 [IB_EVENT_SRQ_LIMIT_REACHED] = "SRQ limit reached",
76 [IB_EVENT_QP_LAST_WQE_REACHED] = "last WQE reached",
77 [IB_EVENT_CLIENT_REREGISTER] = "client reregister",
78 [IB_EVENT_GID_CHANGE] = "GID changed",
81 const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
83 size_t index = event;
85 return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
86 ib_events[index] : "unrecognized event";
88 EXPORT_SYMBOL(ib_event_msg);
90 static const char * const wc_statuses[] = {
91 [IB_WC_SUCCESS] = "success",
92 [IB_WC_LOC_LEN_ERR] = "local length error",
93 [IB_WC_LOC_QP_OP_ERR] = "local QP operation error",
94 [IB_WC_LOC_EEC_OP_ERR] = "local EE context operation error",
95 [IB_WC_LOC_PROT_ERR] = "local protection error",
96 [IB_WC_WR_FLUSH_ERR] = "WR flushed",
97 [IB_WC_MW_BIND_ERR] = "memory management operation error",
98 [IB_WC_BAD_RESP_ERR] = "bad response error",
99 [IB_WC_LOC_ACCESS_ERR] = "local access error",
100 [IB_WC_REM_INV_REQ_ERR] = "invalid request error",
101 [IB_WC_REM_ACCESS_ERR] = "remote access error",
102 [IB_WC_REM_OP_ERR] = "remote operation error",
103 [IB_WC_RETRY_EXC_ERR] = "transport retry counter exceeded",
104 [IB_WC_RNR_RETRY_EXC_ERR] = "RNR retry counter exceeded",
105 [IB_WC_LOC_RDD_VIOL_ERR] = "local RDD violation error",
106 [IB_WC_REM_INV_RD_REQ_ERR] = "remote invalid RD request",
107 [IB_WC_REM_ABORT_ERR] = "operation aborted",
108 [IB_WC_INV_EECN_ERR] = "invalid EE context number",
109 [IB_WC_INV_EEC_STATE_ERR] = "invalid EE context state",
110 [IB_WC_FATAL_ERR] = "fatal error",
111 [IB_WC_RESP_TIMEOUT_ERR] = "response timeout error",
112 [IB_WC_GENERAL_ERR] = "general error",
115 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
117 size_t index = status;
119 return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
120 wc_statuses[index] : "unrecognized status";
122 EXPORT_SYMBOL(ib_wc_status_msg);
124 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
126 switch (rate) {
127 case IB_RATE_2_5_GBPS: return 1;
128 case IB_RATE_5_GBPS: return 2;
129 case IB_RATE_10_GBPS: return 4;
130 case IB_RATE_20_GBPS: return 8;
131 case IB_RATE_30_GBPS: return 12;
132 case IB_RATE_40_GBPS: return 16;
133 case IB_RATE_60_GBPS: return 24;
134 case IB_RATE_80_GBPS: return 32;
135 case IB_RATE_120_GBPS: return 48;
136 case IB_RATE_14_GBPS: return 6;
137 case IB_RATE_56_GBPS: return 22;
138 case IB_RATE_112_GBPS: return 45;
139 case IB_RATE_168_GBPS: return 67;
140 case IB_RATE_25_GBPS: return 10;
141 case IB_RATE_100_GBPS: return 40;
142 case IB_RATE_200_GBPS: return 80;
143 case IB_RATE_300_GBPS: return 120;
144 default: return -1;
147 EXPORT_SYMBOL(ib_rate_to_mult);
149 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
151 switch (mult) {
152 case 1: return IB_RATE_2_5_GBPS;
153 case 2: return IB_RATE_5_GBPS;
154 case 4: return IB_RATE_10_GBPS;
155 case 8: return IB_RATE_20_GBPS;
156 case 12: return IB_RATE_30_GBPS;
157 case 16: return IB_RATE_40_GBPS;
158 case 24: return IB_RATE_60_GBPS;
159 case 32: return IB_RATE_80_GBPS;
160 case 48: return IB_RATE_120_GBPS;
161 case 6: return IB_RATE_14_GBPS;
162 case 22: return IB_RATE_56_GBPS;
163 case 45: return IB_RATE_112_GBPS;
164 case 67: return IB_RATE_168_GBPS;
165 case 10: return IB_RATE_25_GBPS;
166 case 40: return IB_RATE_100_GBPS;
167 case 80: return IB_RATE_200_GBPS;
168 case 120: return IB_RATE_300_GBPS;
169 default: return IB_RATE_PORT_CURRENT;
172 EXPORT_SYMBOL(mult_to_ib_rate);
174 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
176 switch (rate) {
177 case IB_RATE_2_5_GBPS: return 2500;
178 case IB_RATE_5_GBPS: return 5000;
179 case IB_RATE_10_GBPS: return 10000;
180 case IB_RATE_20_GBPS: return 20000;
181 case IB_RATE_30_GBPS: return 30000;
182 case IB_RATE_40_GBPS: return 40000;
183 case IB_RATE_60_GBPS: return 60000;
184 case IB_RATE_80_GBPS: return 80000;
185 case IB_RATE_120_GBPS: return 120000;
186 case IB_RATE_14_GBPS: return 14062;
187 case IB_RATE_56_GBPS: return 56250;
188 case IB_RATE_112_GBPS: return 112500;
189 case IB_RATE_168_GBPS: return 168750;
190 case IB_RATE_25_GBPS: return 25781;
191 case IB_RATE_100_GBPS: return 103125;
192 case IB_RATE_200_GBPS: return 206250;
193 case IB_RATE_300_GBPS: return 309375;
194 default: return -1;
197 EXPORT_SYMBOL(ib_rate_to_mbps);
199 __attribute_const__ enum rdma_transport_type
200 rdma_node_get_transport(enum rdma_node_type node_type)
203 if (node_type == RDMA_NODE_USNIC)
204 return RDMA_TRANSPORT_USNIC;
205 if (node_type == RDMA_NODE_USNIC_UDP)
206 return RDMA_TRANSPORT_USNIC_UDP;
207 if (node_type == RDMA_NODE_RNIC)
208 return RDMA_TRANSPORT_IWARP;
210 return RDMA_TRANSPORT_IB;
212 EXPORT_SYMBOL(rdma_node_get_transport);
214 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
216 enum rdma_transport_type lt;
217 if (device->get_link_layer)
218 return device->get_link_layer(device, port_num);
220 lt = rdma_node_get_transport(device->node_type);
221 if (lt == RDMA_TRANSPORT_IB)
222 return IB_LINK_LAYER_INFINIBAND;
224 return IB_LINK_LAYER_ETHERNET;
226 EXPORT_SYMBOL(rdma_port_get_link_layer);
228 /* Protection domains */
231 * ib_alloc_pd - Allocates an unused protection domain.
232 * @device: The device on which to allocate the protection domain.
234 * A protection domain object provides an association between QPs, shared
235 * receive queues, address handles, memory regions, and memory windows.
237 * Every PD has a local_dma_lkey which can be used as the lkey value for local
238 * memory operations.
240 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
241 const char *caller)
243 struct ib_pd *pd;
244 int mr_access_flags = 0;
246 pd = device->alloc_pd(device, NULL, NULL);
247 if (IS_ERR(pd))
248 return pd;
250 pd->device = device;
251 pd->uobject = NULL;
252 pd->__internal_mr = NULL;
253 atomic_set(&pd->usecnt, 0);
254 pd->flags = flags;
256 if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
257 pd->local_dma_lkey = device->local_dma_lkey;
258 else
259 mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
261 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
262 pr_warn("%s: enabling unsafe global rkey\n", caller);
263 mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
266 pd->res.type = RDMA_RESTRACK_PD;
267 pd->res.kern_name = caller;
268 rdma_restrack_add(&pd->res);
270 if (mr_access_flags) {
271 struct ib_mr *mr;
273 mr = pd->device->get_dma_mr(pd, mr_access_flags);
274 if (IS_ERR(mr)) {
275 ib_dealloc_pd(pd);
276 return ERR_CAST(mr);
279 mr->device = pd->device;
280 mr->pd = pd;
281 mr->uobject = NULL;
282 mr->need_inval = false;
284 pd->__internal_mr = mr;
286 if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
287 pd->local_dma_lkey = pd->__internal_mr->lkey;
289 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
290 pd->unsafe_global_rkey = pd->__internal_mr->rkey;
293 return pd;
295 EXPORT_SYMBOL(__ib_alloc_pd);
298 * ib_dealloc_pd - Deallocates a protection domain.
299 * @pd: The protection domain to deallocate.
301 * It is an error to call this function while any resources in the pd still
302 * exist. The caller is responsible to synchronously destroy them and
303 * guarantee no new allocations will happen.
305 void ib_dealloc_pd(struct ib_pd *pd)
307 int ret;
309 if (pd->__internal_mr) {
310 ret = pd->device->dereg_mr(pd->__internal_mr);
311 WARN_ON(ret);
312 pd->__internal_mr = NULL;
315 /* uverbs manipulates usecnt with proper locking, while the kabi
316 requires the caller to guarantee we can't race here. */
317 WARN_ON(atomic_read(&pd->usecnt));
319 rdma_restrack_del(&pd->res);
320 /* Making delalloc_pd a void return is a WIP, no driver should return
321 an error here. */
322 ret = pd->device->dealloc_pd(pd);
323 WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
325 EXPORT_SYMBOL(ib_dealloc_pd);
327 /* Address handles */
329 static struct ib_ah *_rdma_create_ah(struct ib_pd *pd,
330 struct rdma_ah_attr *ah_attr,
331 struct ib_udata *udata)
333 struct ib_ah *ah;
335 ah = pd->device->create_ah(pd, ah_attr, udata);
337 if (!IS_ERR(ah)) {
338 ah->device = pd->device;
339 ah->pd = pd;
340 ah->uobject = NULL;
341 ah->type = ah_attr->type;
342 atomic_inc(&pd->usecnt);
345 return ah;
348 struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr)
350 return _rdma_create_ah(pd, ah_attr, NULL);
352 EXPORT_SYMBOL(rdma_create_ah);
355 * rdma_create_user_ah - Creates an address handle for the
356 * given address vector.
357 * It resolves destination mac address for ah attribute of RoCE type.
358 * @pd: The protection domain associated with the address handle.
359 * @ah_attr: The attributes of the address vector.
360 * @udata: pointer to user's input output buffer information need by
361 * provider driver.
363 * It returns 0 on success and returns appropriate error code on error.
364 * The address handle is used to reference a local or global destination
365 * in all UD QP post sends.
367 struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
368 struct rdma_ah_attr *ah_attr,
369 struct ib_udata *udata)
371 int err;
373 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
374 err = ib_resolve_eth_dmac(pd->device, ah_attr);
375 if (err)
376 return ERR_PTR(err);
379 return _rdma_create_ah(pd, ah_attr, udata);
381 EXPORT_SYMBOL(rdma_create_user_ah);
383 int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
385 const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
386 struct iphdr ip4h_checked;
387 const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;
389 /* If it's IPv6, the version must be 6, otherwise, the first
390 * 20 bytes (before the IPv4 header) are garbled.
392 if (ip6h->version != 6)
393 return (ip4h->version == 4) ? 4 : 0;
394 /* version may be 6 or 4 because the first 20 bytes could be garbled */
396 /* RoCE v2 requires no options, thus header length
397 * must be 5 words
399 if (ip4h->ihl != 5)
400 return 6;
402 /* Verify checksum.
403 * We can't write on scattered buffers so we need to copy to
404 * temp buffer.
406 memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
407 ip4h_checked.check = 0;
408 ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
409 /* if IPv4 header checksum is OK, believe it */
410 if (ip4h->check == ip4h_checked.check)
411 return 4;
412 return 6;
414 EXPORT_SYMBOL(ib_get_rdma_header_version);
416 static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
417 u8 port_num,
418 const struct ib_grh *grh)
420 int grh_version;
422 if (rdma_protocol_ib(device, port_num))
423 return RDMA_NETWORK_IB;
425 grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
427 if (grh_version == 4)
428 return RDMA_NETWORK_IPV4;
430 if (grh->next_hdr == IPPROTO_UDP)
431 return RDMA_NETWORK_IPV6;
433 return RDMA_NETWORK_ROCE_V1;
436 struct find_gid_index_context {
437 u16 vlan_id;
438 enum ib_gid_type gid_type;
441 static bool find_gid_index(const union ib_gid *gid,
442 const struct ib_gid_attr *gid_attr,
443 void *context)
445 struct find_gid_index_context *ctx = context;
447 if (ctx->gid_type != gid_attr->gid_type)
448 return false;
450 if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) ||
451 (is_vlan_dev(gid_attr->ndev) &&
452 vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id))
453 return false;
455 return true;
458 static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num,
459 u16 vlan_id, const union ib_gid *sgid,
460 enum ib_gid_type gid_type,
461 u16 *gid_index)
463 struct find_gid_index_context context = {.vlan_id = vlan_id,
464 .gid_type = gid_type};
466 return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index,
467 &context, gid_index);
470 int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
471 enum rdma_network_type net_type,
472 union ib_gid *sgid, union ib_gid *dgid)
474 struct sockaddr_in src_in;
475 struct sockaddr_in dst_in;
476 __be32 src_saddr, dst_saddr;
478 if (!sgid || !dgid)
479 return -EINVAL;
481 if (net_type == RDMA_NETWORK_IPV4) {
482 memcpy(&src_in.sin_addr.s_addr,
483 &hdr->roce4grh.saddr, 4);
484 memcpy(&dst_in.sin_addr.s_addr,
485 &hdr->roce4grh.daddr, 4);
486 src_saddr = src_in.sin_addr.s_addr;
487 dst_saddr = dst_in.sin_addr.s_addr;
488 ipv6_addr_set_v4mapped(src_saddr,
489 (struct in6_addr *)sgid);
490 ipv6_addr_set_v4mapped(dst_saddr,
491 (struct in6_addr *)dgid);
492 return 0;
493 } else if (net_type == RDMA_NETWORK_IPV6 ||
494 net_type == RDMA_NETWORK_IB) {
495 *dgid = hdr->ibgrh.dgid;
496 *sgid = hdr->ibgrh.sgid;
497 return 0;
498 } else {
499 return -EINVAL;
502 EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
504 /* Resolve destination mac address and hop limit for unicast destination
505 * GID entry, considering the source GID entry as well.
506 * ah_attribute must have have valid port_num, sgid_index.
508 static int ib_resolve_unicast_gid_dmac(struct ib_device *device,
509 struct rdma_ah_attr *ah_attr)
511 struct ib_gid_attr sgid_attr;
512 struct ib_global_route *grh;
513 int hop_limit = 0xff;
514 union ib_gid sgid;
515 int ret;
517 grh = rdma_ah_retrieve_grh(ah_attr);
519 ret = ib_query_gid(device,
520 rdma_ah_get_port_num(ah_attr),
521 grh->sgid_index,
522 &sgid, &sgid_attr);
523 if (ret || !sgid_attr.ndev) {
524 if (!ret)
525 ret = -ENXIO;
526 return ret;
529 /* If destination is link local and source GID is RoCEv1,
530 * IP stack is not used.
532 if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) &&
533 sgid_attr.gid_type == IB_GID_TYPE_ROCE) {
534 rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
535 ah_attr->roce.dmac);
536 goto done;
539 ret = rdma_addr_find_l2_eth_by_grh(&sgid, &grh->dgid,
540 ah_attr->roce.dmac,
541 sgid_attr.ndev, &hop_limit);
542 done:
543 dev_put(sgid_attr.ndev);
545 grh->hop_limit = hop_limit;
546 return ret;
550 * This function initializes address handle attributes from the incoming packet.
551 * Incoming packet has dgid of the receiver node on which this code is
552 * getting executed and, sgid contains the GID of the sender.
554 * When resolving mac address of destination, the arrived dgid is used
555 * as sgid and, sgid is used as dgid because sgid contains destinations
556 * GID whom to respond to.
559 int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num,
560 const struct ib_wc *wc, const struct ib_grh *grh,
561 struct rdma_ah_attr *ah_attr)
563 u32 flow_class;
564 u16 gid_index;
565 int ret;
566 enum rdma_network_type net_type = RDMA_NETWORK_IB;
567 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
568 int hoplimit = 0xff;
569 union ib_gid dgid;
570 union ib_gid sgid;
572 might_sleep();
574 memset(ah_attr, 0, sizeof *ah_attr);
575 ah_attr->type = rdma_ah_find_type(device, port_num);
576 if (rdma_cap_eth_ah(device, port_num)) {
577 if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
578 net_type = wc->network_hdr_type;
579 else
580 net_type = ib_get_net_type_by_grh(device, port_num, grh);
581 gid_type = ib_network_to_gid_type(net_type);
583 ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
584 &sgid, &dgid);
585 if (ret)
586 return ret;
588 rdma_ah_set_sl(ah_attr, wc->sl);
589 rdma_ah_set_port_num(ah_attr, port_num);
591 if (rdma_protocol_roce(device, port_num)) {
592 u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
593 wc->vlan_id : 0xffff;
595 if (!(wc->wc_flags & IB_WC_GRH))
596 return -EPROTOTYPE;
598 ret = get_sgid_index_from_eth(device, port_num,
599 vlan_id, &dgid,
600 gid_type, &gid_index);
601 if (ret)
602 return ret;
604 flow_class = be32_to_cpu(grh->version_tclass_flow);
605 rdma_ah_set_grh(ah_attr, &sgid,
606 flow_class & 0xFFFFF,
607 (u8)gid_index, hoplimit,
608 (flow_class >> 20) & 0xFF);
609 return ib_resolve_unicast_gid_dmac(device, ah_attr);
610 } else {
611 rdma_ah_set_dlid(ah_attr, wc->slid);
612 rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
614 if (wc->wc_flags & IB_WC_GRH) {
615 if (dgid.global.interface_id != cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
616 ret = ib_find_cached_gid_by_port(device, &dgid,
617 IB_GID_TYPE_IB,
618 port_num, NULL,
619 &gid_index);
620 if (ret)
621 return ret;
622 } else {
623 gid_index = 0;
626 flow_class = be32_to_cpu(grh->version_tclass_flow);
627 rdma_ah_set_grh(ah_attr, &sgid,
628 flow_class & 0xFFFFF,
629 (u8)gid_index, hoplimit,
630 (flow_class >> 20) & 0xFF);
632 return 0;
635 EXPORT_SYMBOL(ib_init_ah_attr_from_wc);
637 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
638 const struct ib_grh *grh, u8 port_num)
640 struct rdma_ah_attr ah_attr;
641 int ret;
643 ret = ib_init_ah_attr_from_wc(pd->device, port_num, wc, grh, &ah_attr);
644 if (ret)
645 return ERR_PTR(ret);
647 return rdma_create_ah(pd, &ah_attr);
649 EXPORT_SYMBOL(ib_create_ah_from_wc);
651 int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
653 if (ah->type != ah_attr->type)
654 return -EINVAL;
656 return ah->device->modify_ah ?
657 ah->device->modify_ah(ah, ah_attr) :
658 -ENOSYS;
660 EXPORT_SYMBOL(rdma_modify_ah);
662 int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
664 return ah->device->query_ah ?
665 ah->device->query_ah(ah, ah_attr) :
666 -ENOSYS;
668 EXPORT_SYMBOL(rdma_query_ah);
670 int rdma_destroy_ah(struct ib_ah *ah)
672 struct ib_pd *pd;
673 int ret;
675 pd = ah->pd;
676 ret = ah->device->destroy_ah(ah);
677 if (!ret)
678 atomic_dec(&pd->usecnt);
680 return ret;
682 EXPORT_SYMBOL(rdma_destroy_ah);
684 /* Shared receive queues */
686 struct ib_srq *ib_create_srq(struct ib_pd *pd,
687 struct ib_srq_init_attr *srq_init_attr)
689 struct ib_srq *srq;
691 if (!pd->device->create_srq)
692 return ERR_PTR(-ENOSYS);
694 srq = pd->device->create_srq(pd, srq_init_attr, NULL);
696 if (!IS_ERR(srq)) {
697 srq->device = pd->device;
698 srq->pd = pd;
699 srq->uobject = NULL;
700 srq->event_handler = srq_init_attr->event_handler;
701 srq->srq_context = srq_init_attr->srq_context;
702 srq->srq_type = srq_init_attr->srq_type;
703 if (ib_srq_has_cq(srq->srq_type)) {
704 srq->ext.cq = srq_init_attr->ext.cq;
705 atomic_inc(&srq->ext.cq->usecnt);
707 if (srq->srq_type == IB_SRQT_XRC) {
708 srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
709 atomic_inc(&srq->ext.xrc.xrcd->usecnt);
711 atomic_inc(&pd->usecnt);
712 atomic_set(&srq->usecnt, 0);
715 return srq;
717 EXPORT_SYMBOL(ib_create_srq);
719 int ib_modify_srq(struct ib_srq *srq,
720 struct ib_srq_attr *srq_attr,
721 enum ib_srq_attr_mask srq_attr_mask)
723 return srq->device->modify_srq ?
724 srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
725 -ENOSYS;
727 EXPORT_SYMBOL(ib_modify_srq);
729 int ib_query_srq(struct ib_srq *srq,
730 struct ib_srq_attr *srq_attr)
732 return srq->device->query_srq ?
733 srq->device->query_srq(srq, srq_attr) : -ENOSYS;
735 EXPORT_SYMBOL(ib_query_srq);
737 int ib_destroy_srq(struct ib_srq *srq)
739 struct ib_pd *pd;
740 enum ib_srq_type srq_type;
741 struct ib_xrcd *uninitialized_var(xrcd);
742 struct ib_cq *uninitialized_var(cq);
743 int ret;
745 if (atomic_read(&srq->usecnt))
746 return -EBUSY;
748 pd = srq->pd;
749 srq_type = srq->srq_type;
750 if (ib_srq_has_cq(srq_type))
751 cq = srq->ext.cq;
752 if (srq_type == IB_SRQT_XRC)
753 xrcd = srq->ext.xrc.xrcd;
755 ret = srq->device->destroy_srq(srq);
756 if (!ret) {
757 atomic_dec(&pd->usecnt);
758 if (srq_type == IB_SRQT_XRC)
759 atomic_dec(&xrcd->usecnt);
760 if (ib_srq_has_cq(srq_type))
761 atomic_dec(&cq->usecnt);
764 return ret;
766 EXPORT_SYMBOL(ib_destroy_srq);
768 /* Queue pairs */
770 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
772 struct ib_qp *qp = context;
773 unsigned long flags;
775 spin_lock_irqsave(&qp->device->event_handler_lock, flags);
776 list_for_each_entry(event->element.qp, &qp->open_list, open_list)
777 if (event->element.qp->event_handler)
778 event->element.qp->event_handler(event, event->element.qp->qp_context);
779 spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
782 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
784 mutex_lock(&xrcd->tgt_qp_mutex);
785 list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
786 mutex_unlock(&xrcd->tgt_qp_mutex);
789 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
790 void (*event_handler)(struct ib_event *, void *),
791 void *qp_context)
793 struct ib_qp *qp;
794 unsigned long flags;
795 int err;
797 qp = kzalloc(sizeof *qp, GFP_KERNEL);
798 if (!qp)
799 return ERR_PTR(-ENOMEM);
801 qp->real_qp = real_qp;
802 err = ib_open_shared_qp_security(qp, real_qp->device);
803 if (err) {
804 kfree(qp);
805 return ERR_PTR(err);
808 qp->real_qp = real_qp;
809 atomic_inc(&real_qp->usecnt);
810 qp->device = real_qp->device;
811 qp->event_handler = event_handler;
812 qp->qp_context = qp_context;
813 qp->qp_num = real_qp->qp_num;
814 qp->qp_type = real_qp->qp_type;
816 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
817 list_add(&qp->open_list, &real_qp->open_list);
818 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
820 return qp;
823 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
824 struct ib_qp_open_attr *qp_open_attr)
826 struct ib_qp *qp, *real_qp;
828 if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
829 return ERR_PTR(-EINVAL);
831 qp = ERR_PTR(-EINVAL);
832 mutex_lock(&xrcd->tgt_qp_mutex);
833 list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
834 if (real_qp->qp_num == qp_open_attr->qp_num) {
835 qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
836 qp_open_attr->qp_context);
837 break;
840 mutex_unlock(&xrcd->tgt_qp_mutex);
841 return qp;
843 EXPORT_SYMBOL(ib_open_qp);
845 static struct ib_qp *ib_create_xrc_qp(struct ib_qp *qp,
846 struct ib_qp_init_attr *qp_init_attr)
848 struct ib_qp *real_qp = qp;
850 qp->event_handler = __ib_shared_qp_event_handler;
851 qp->qp_context = qp;
852 qp->pd = NULL;
853 qp->send_cq = qp->recv_cq = NULL;
854 qp->srq = NULL;
855 qp->xrcd = qp_init_attr->xrcd;
856 atomic_inc(&qp_init_attr->xrcd->usecnt);
857 INIT_LIST_HEAD(&qp->open_list);
859 qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
860 qp_init_attr->qp_context);
861 if (!IS_ERR(qp))
862 __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
863 else
864 real_qp->device->destroy_qp(real_qp);
865 return qp;
868 struct ib_qp *ib_create_qp(struct ib_pd *pd,
869 struct ib_qp_init_attr *qp_init_attr)
871 struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
872 struct ib_qp *qp;
873 int ret;
875 if (qp_init_attr->rwq_ind_tbl &&
876 (qp_init_attr->recv_cq ||
877 qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
878 qp_init_attr->cap.max_recv_sge))
879 return ERR_PTR(-EINVAL);
882 * If the callers is using the RDMA API calculate the resources
883 * needed for the RDMA READ/WRITE operations.
885 * Note that these callers need to pass in a port number.
887 if (qp_init_attr->cap.max_rdma_ctxs)
888 rdma_rw_init_qp(device, qp_init_attr);
890 qp = _ib_create_qp(device, pd, qp_init_attr, NULL);
891 if (IS_ERR(qp))
892 return qp;
894 ret = ib_create_qp_security(qp, device);
895 if (ret) {
896 ib_destroy_qp(qp);
897 return ERR_PTR(ret);
900 qp->real_qp = qp;
901 qp->uobject = NULL;
902 qp->qp_type = qp_init_attr->qp_type;
903 qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
905 atomic_set(&qp->usecnt, 0);
906 qp->mrs_used = 0;
907 spin_lock_init(&qp->mr_lock);
908 INIT_LIST_HEAD(&qp->rdma_mrs);
909 INIT_LIST_HEAD(&qp->sig_mrs);
910 qp->port = 0;
912 if (qp_init_attr->qp_type == IB_QPT_XRC_TGT)
913 return ib_create_xrc_qp(qp, qp_init_attr);
915 qp->event_handler = qp_init_attr->event_handler;
916 qp->qp_context = qp_init_attr->qp_context;
917 if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
918 qp->recv_cq = NULL;
919 qp->srq = NULL;
920 } else {
921 qp->recv_cq = qp_init_attr->recv_cq;
922 if (qp_init_attr->recv_cq)
923 atomic_inc(&qp_init_attr->recv_cq->usecnt);
924 qp->srq = qp_init_attr->srq;
925 if (qp->srq)
926 atomic_inc(&qp_init_attr->srq->usecnt);
929 qp->send_cq = qp_init_attr->send_cq;
930 qp->xrcd = NULL;
932 atomic_inc(&pd->usecnt);
933 if (qp_init_attr->send_cq)
934 atomic_inc(&qp_init_attr->send_cq->usecnt);
935 if (qp_init_attr->rwq_ind_tbl)
936 atomic_inc(&qp->rwq_ind_tbl->usecnt);
938 if (qp_init_attr->cap.max_rdma_ctxs) {
939 ret = rdma_rw_init_mrs(qp, qp_init_attr);
940 if (ret) {
941 pr_err("failed to init MR pool ret= %d\n", ret);
942 ib_destroy_qp(qp);
943 return ERR_PTR(ret);
948 * Note: all hw drivers guarantee that max_send_sge is lower than
949 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
950 * max_send_sge <= max_sge_rd.
952 qp->max_write_sge = qp_init_attr->cap.max_send_sge;
953 qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
954 device->attrs.max_sge_rd);
956 return qp;
958 EXPORT_SYMBOL(ib_create_qp);
960 static const struct {
961 int valid;
962 enum ib_qp_attr_mask req_param[IB_QPT_MAX];
963 enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
964 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
965 [IB_QPS_RESET] = {
966 [IB_QPS_RESET] = { .valid = 1 },
967 [IB_QPS_INIT] = {
968 .valid = 1,
969 .req_param = {
970 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
971 IB_QP_PORT |
972 IB_QP_QKEY),
973 [IB_QPT_RAW_PACKET] = IB_QP_PORT,
974 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
975 IB_QP_PORT |
976 IB_QP_ACCESS_FLAGS),
977 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
978 IB_QP_PORT |
979 IB_QP_ACCESS_FLAGS),
980 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
981 IB_QP_PORT |
982 IB_QP_ACCESS_FLAGS),
983 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
984 IB_QP_PORT |
985 IB_QP_ACCESS_FLAGS),
986 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
987 IB_QP_QKEY),
988 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
989 IB_QP_QKEY),
993 [IB_QPS_INIT] = {
994 [IB_QPS_RESET] = { .valid = 1 },
995 [IB_QPS_ERR] = { .valid = 1 },
996 [IB_QPS_INIT] = {
997 .valid = 1,
998 .opt_param = {
999 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1000 IB_QP_PORT |
1001 IB_QP_QKEY),
1002 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
1003 IB_QP_PORT |
1004 IB_QP_ACCESS_FLAGS),
1005 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
1006 IB_QP_PORT |
1007 IB_QP_ACCESS_FLAGS),
1008 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
1009 IB_QP_PORT |
1010 IB_QP_ACCESS_FLAGS),
1011 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
1012 IB_QP_PORT |
1013 IB_QP_ACCESS_FLAGS),
1014 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1015 IB_QP_QKEY),
1016 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1017 IB_QP_QKEY),
1020 [IB_QPS_RTR] = {
1021 .valid = 1,
1022 .req_param = {
1023 [IB_QPT_UC] = (IB_QP_AV |
1024 IB_QP_PATH_MTU |
1025 IB_QP_DEST_QPN |
1026 IB_QP_RQ_PSN),
1027 [IB_QPT_RC] = (IB_QP_AV |
1028 IB_QP_PATH_MTU |
1029 IB_QP_DEST_QPN |
1030 IB_QP_RQ_PSN |
1031 IB_QP_MAX_DEST_RD_ATOMIC |
1032 IB_QP_MIN_RNR_TIMER),
1033 [IB_QPT_XRC_INI] = (IB_QP_AV |
1034 IB_QP_PATH_MTU |
1035 IB_QP_DEST_QPN |
1036 IB_QP_RQ_PSN),
1037 [IB_QPT_XRC_TGT] = (IB_QP_AV |
1038 IB_QP_PATH_MTU |
1039 IB_QP_DEST_QPN |
1040 IB_QP_RQ_PSN |
1041 IB_QP_MAX_DEST_RD_ATOMIC |
1042 IB_QP_MIN_RNR_TIMER),
1044 .opt_param = {
1045 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1046 IB_QP_QKEY),
1047 [IB_QPT_UC] = (IB_QP_ALT_PATH |
1048 IB_QP_ACCESS_FLAGS |
1049 IB_QP_PKEY_INDEX),
1050 [IB_QPT_RC] = (IB_QP_ALT_PATH |
1051 IB_QP_ACCESS_FLAGS |
1052 IB_QP_PKEY_INDEX),
1053 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
1054 IB_QP_ACCESS_FLAGS |
1055 IB_QP_PKEY_INDEX),
1056 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
1057 IB_QP_ACCESS_FLAGS |
1058 IB_QP_PKEY_INDEX),
1059 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1060 IB_QP_QKEY),
1061 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1062 IB_QP_QKEY),
1066 [IB_QPS_RTR] = {
1067 [IB_QPS_RESET] = { .valid = 1 },
1068 [IB_QPS_ERR] = { .valid = 1 },
1069 [IB_QPS_RTS] = {
1070 .valid = 1,
1071 .req_param = {
1072 [IB_QPT_UD] = IB_QP_SQ_PSN,
1073 [IB_QPT_UC] = IB_QP_SQ_PSN,
1074 [IB_QPT_RC] = (IB_QP_TIMEOUT |
1075 IB_QP_RETRY_CNT |
1076 IB_QP_RNR_RETRY |
1077 IB_QP_SQ_PSN |
1078 IB_QP_MAX_QP_RD_ATOMIC),
1079 [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
1080 IB_QP_RETRY_CNT |
1081 IB_QP_RNR_RETRY |
1082 IB_QP_SQ_PSN |
1083 IB_QP_MAX_QP_RD_ATOMIC),
1084 [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
1085 IB_QP_SQ_PSN),
1086 [IB_QPT_SMI] = IB_QP_SQ_PSN,
1087 [IB_QPT_GSI] = IB_QP_SQ_PSN,
1089 .opt_param = {
1090 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1091 IB_QP_QKEY),
1092 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1093 IB_QP_ALT_PATH |
1094 IB_QP_ACCESS_FLAGS |
1095 IB_QP_PATH_MIG_STATE),
1096 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1097 IB_QP_ALT_PATH |
1098 IB_QP_ACCESS_FLAGS |
1099 IB_QP_MIN_RNR_TIMER |
1100 IB_QP_PATH_MIG_STATE),
1101 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1102 IB_QP_ALT_PATH |
1103 IB_QP_ACCESS_FLAGS |
1104 IB_QP_PATH_MIG_STATE),
1105 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1106 IB_QP_ALT_PATH |
1107 IB_QP_ACCESS_FLAGS |
1108 IB_QP_MIN_RNR_TIMER |
1109 IB_QP_PATH_MIG_STATE),
1110 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1111 IB_QP_QKEY),
1112 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1113 IB_QP_QKEY),
1114 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1118 [IB_QPS_RTS] = {
1119 [IB_QPS_RESET] = { .valid = 1 },
1120 [IB_QPS_ERR] = { .valid = 1 },
1121 [IB_QPS_RTS] = {
1122 .valid = 1,
1123 .opt_param = {
1124 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1125 IB_QP_QKEY),
1126 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1127 IB_QP_ACCESS_FLAGS |
1128 IB_QP_ALT_PATH |
1129 IB_QP_PATH_MIG_STATE),
1130 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1131 IB_QP_ACCESS_FLAGS |
1132 IB_QP_ALT_PATH |
1133 IB_QP_PATH_MIG_STATE |
1134 IB_QP_MIN_RNR_TIMER),
1135 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1136 IB_QP_ACCESS_FLAGS |
1137 IB_QP_ALT_PATH |
1138 IB_QP_PATH_MIG_STATE),
1139 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1140 IB_QP_ACCESS_FLAGS |
1141 IB_QP_ALT_PATH |
1142 IB_QP_PATH_MIG_STATE |
1143 IB_QP_MIN_RNR_TIMER),
1144 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1145 IB_QP_QKEY),
1146 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1147 IB_QP_QKEY),
1148 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1151 [IB_QPS_SQD] = {
1152 .valid = 1,
1153 .opt_param = {
1154 [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1155 [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1156 [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1157 [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1158 [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1159 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1160 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1164 [IB_QPS_SQD] = {
1165 [IB_QPS_RESET] = { .valid = 1 },
1166 [IB_QPS_ERR] = { .valid = 1 },
1167 [IB_QPS_RTS] = {
1168 .valid = 1,
1169 .opt_param = {
1170 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1171 IB_QP_QKEY),
1172 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1173 IB_QP_ALT_PATH |
1174 IB_QP_ACCESS_FLAGS |
1175 IB_QP_PATH_MIG_STATE),
1176 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1177 IB_QP_ALT_PATH |
1178 IB_QP_ACCESS_FLAGS |
1179 IB_QP_MIN_RNR_TIMER |
1180 IB_QP_PATH_MIG_STATE),
1181 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1182 IB_QP_ALT_PATH |
1183 IB_QP_ACCESS_FLAGS |
1184 IB_QP_PATH_MIG_STATE),
1185 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1186 IB_QP_ALT_PATH |
1187 IB_QP_ACCESS_FLAGS |
1188 IB_QP_MIN_RNR_TIMER |
1189 IB_QP_PATH_MIG_STATE),
1190 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1191 IB_QP_QKEY),
1192 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1193 IB_QP_QKEY),
1196 [IB_QPS_SQD] = {
1197 .valid = 1,
1198 .opt_param = {
1199 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1200 IB_QP_QKEY),
1201 [IB_QPT_UC] = (IB_QP_AV |
1202 IB_QP_ALT_PATH |
1203 IB_QP_ACCESS_FLAGS |
1204 IB_QP_PKEY_INDEX |
1205 IB_QP_PATH_MIG_STATE),
1206 [IB_QPT_RC] = (IB_QP_PORT |
1207 IB_QP_AV |
1208 IB_QP_TIMEOUT |
1209 IB_QP_RETRY_CNT |
1210 IB_QP_RNR_RETRY |
1211 IB_QP_MAX_QP_RD_ATOMIC |
1212 IB_QP_MAX_DEST_RD_ATOMIC |
1213 IB_QP_ALT_PATH |
1214 IB_QP_ACCESS_FLAGS |
1215 IB_QP_PKEY_INDEX |
1216 IB_QP_MIN_RNR_TIMER |
1217 IB_QP_PATH_MIG_STATE),
1218 [IB_QPT_XRC_INI] = (IB_QP_PORT |
1219 IB_QP_AV |
1220 IB_QP_TIMEOUT |
1221 IB_QP_RETRY_CNT |
1222 IB_QP_RNR_RETRY |
1223 IB_QP_MAX_QP_RD_ATOMIC |
1224 IB_QP_ALT_PATH |
1225 IB_QP_ACCESS_FLAGS |
1226 IB_QP_PKEY_INDEX |
1227 IB_QP_PATH_MIG_STATE),
1228 [IB_QPT_XRC_TGT] = (IB_QP_PORT |
1229 IB_QP_AV |
1230 IB_QP_TIMEOUT |
1231 IB_QP_MAX_DEST_RD_ATOMIC |
1232 IB_QP_ALT_PATH |
1233 IB_QP_ACCESS_FLAGS |
1234 IB_QP_PKEY_INDEX |
1235 IB_QP_MIN_RNR_TIMER |
1236 IB_QP_PATH_MIG_STATE),
1237 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1238 IB_QP_QKEY),
1239 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1240 IB_QP_QKEY),
1244 [IB_QPS_SQE] = {
1245 [IB_QPS_RESET] = { .valid = 1 },
1246 [IB_QPS_ERR] = { .valid = 1 },
1247 [IB_QPS_RTS] = {
1248 .valid = 1,
1249 .opt_param = {
1250 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1251 IB_QP_QKEY),
1252 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1253 IB_QP_ACCESS_FLAGS),
1254 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1255 IB_QP_QKEY),
1256 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1257 IB_QP_QKEY),
1261 [IB_QPS_ERR] = {
1262 [IB_QPS_RESET] = { .valid = 1 },
1263 [IB_QPS_ERR] = { .valid = 1 }
1267 int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1268 enum ib_qp_type type, enum ib_qp_attr_mask mask,
1269 enum rdma_link_layer ll)
1271 enum ib_qp_attr_mask req_param, opt_param;
1273 if (cur_state < 0 || cur_state > IB_QPS_ERR ||
1274 next_state < 0 || next_state > IB_QPS_ERR)
1275 return 0;
1277 if (mask & IB_QP_CUR_STATE &&
1278 cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1279 cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1280 return 0;
1282 if (!qp_state_table[cur_state][next_state].valid)
1283 return 0;
1285 req_param = qp_state_table[cur_state][next_state].req_param[type];
1286 opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1288 if ((mask & req_param) != req_param)
1289 return 0;
1291 if (mask & ~(req_param | opt_param | IB_QP_STATE))
1292 return 0;
1294 return 1;
1296 EXPORT_SYMBOL(ib_modify_qp_is_ok);
1298 static int ib_resolve_eth_dmac(struct ib_device *device,
1299 struct rdma_ah_attr *ah_attr)
1301 int ret = 0;
1302 struct ib_global_route *grh;
1304 if (!rdma_is_port_valid(device, rdma_ah_get_port_num(ah_attr)))
1305 return -EINVAL;
1307 grh = rdma_ah_retrieve_grh(ah_attr);
1309 if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1310 if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1311 __be32 addr = 0;
1313 memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
1314 ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
1315 } else {
1316 ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
1317 (char *)ah_attr->roce.dmac);
1319 } else {
1320 ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
1322 return ret;
1326 * IB core internal function to perform QP attributes modification.
1328 static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
1329 int attr_mask, struct ib_udata *udata)
1331 u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1332 int ret;
1334 if (rdma_ib_or_roce(qp->device, port)) {
1335 if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) {
1336 pr_warn("%s: %s rq_psn overflow, masking to 24 bits\n",
1337 __func__, qp->device->name);
1338 attr->rq_psn &= 0xffffff;
1341 if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) {
1342 pr_warn("%s: %s sq_psn overflow, masking to 24 bits\n",
1343 __func__, qp->device->name);
1344 attr->sq_psn &= 0xffffff;
1348 ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
1349 if (!ret && (attr_mask & IB_QP_PORT))
1350 qp->port = attr->port_num;
1352 return ret;
1355 static bool is_qp_type_connected(const struct ib_qp *qp)
1357 return (qp->qp_type == IB_QPT_UC ||
1358 qp->qp_type == IB_QPT_RC ||
1359 qp->qp_type == IB_QPT_XRC_INI ||
1360 qp->qp_type == IB_QPT_XRC_TGT);
1364 * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
1365 * @ib_qp: The QP to modify.
1366 * @attr: On input, specifies the QP attributes to modify. On output,
1367 * the current values of selected QP attributes are returned.
1368 * @attr_mask: A bit-mask used to specify which attributes of the QP
1369 * are being modified.
1370 * @udata: pointer to user's input output buffer information
1371 * are being modified.
1372 * It returns 0 on success and returns appropriate error code on error.
1374 int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
1375 int attr_mask, struct ib_udata *udata)
1377 struct ib_qp *qp = ib_qp->real_qp;
1378 int ret;
1380 if (attr_mask & IB_QP_AV &&
1381 attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE &&
1382 is_qp_type_connected(qp)) {
1383 ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr);
1384 if (ret)
1385 return ret;
1387 return _ib_modify_qp(qp, attr, attr_mask, udata);
1389 EXPORT_SYMBOL(ib_modify_qp_with_udata);
1391 int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width)
1393 int rc;
1394 u32 netdev_speed;
1395 struct net_device *netdev;
1396 struct ethtool_link_ksettings lksettings;
1398 if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
1399 return -EINVAL;
1401 if (!dev->get_netdev)
1402 return -EOPNOTSUPP;
1404 netdev = dev->get_netdev(dev, port_num);
1405 if (!netdev)
1406 return -ENODEV;
1408 rtnl_lock();
1409 rc = __ethtool_get_link_ksettings(netdev, &lksettings);
1410 rtnl_unlock();
1412 dev_put(netdev);
1414 if (!rc) {
1415 netdev_speed = lksettings.base.speed;
1416 } else {
1417 netdev_speed = SPEED_1000;
1418 pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name,
1419 netdev_speed);
1422 if (netdev_speed <= SPEED_1000) {
1423 *width = IB_WIDTH_1X;
1424 *speed = IB_SPEED_SDR;
1425 } else if (netdev_speed <= SPEED_10000) {
1426 *width = IB_WIDTH_1X;
1427 *speed = IB_SPEED_FDR10;
1428 } else if (netdev_speed <= SPEED_20000) {
1429 *width = IB_WIDTH_4X;
1430 *speed = IB_SPEED_DDR;
1431 } else if (netdev_speed <= SPEED_25000) {
1432 *width = IB_WIDTH_1X;
1433 *speed = IB_SPEED_EDR;
1434 } else if (netdev_speed <= SPEED_40000) {
1435 *width = IB_WIDTH_4X;
1436 *speed = IB_SPEED_FDR10;
1437 } else {
1438 *width = IB_WIDTH_4X;
1439 *speed = IB_SPEED_EDR;
1442 return 0;
1444 EXPORT_SYMBOL(ib_get_eth_speed);
1446 int ib_modify_qp(struct ib_qp *qp,
1447 struct ib_qp_attr *qp_attr,
1448 int qp_attr_mask)
1450 return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
1452 EXPORT_SYMBOL(ib_modify_qp);
1454 int ib_query_qp(struct ib_qp *qp,
1455 struct ib_qp_attr *qp_attr,
1456 int qp_attr_mask,
1457 struct ib_qp_init_attr *qp_init_attr)
1459 return qp->device->query_qp ?
1460 qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
1461 -ENOSYS;
1463 EXPORT_SYMBOL(ib_query_qp);
1465 int ib_close_qp(struct ib_qp *qp)
1467 struct ib_qp *real_qp;
1468 unsigned long flags;
1470 real_qp = qp->real_qp;
1471 if (real_qp == qp)
1472 return -EINVAL;
1474 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1475 list_del(&qp->open_list);
1476 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1478 atomic_dec(&real_qp->usecnt);
1479 if (qp->qp_sec)
1480 ib_close_shared_qp_security(qp->qp_sec);
1481 kfree(qp);
1483 return 0;
1485 EXPORT_SYMBOL(ib_close_qp);
1487 static int __ib_destroy_shared_qp(struct ib_qp *qp)
1489 struct ib_xrcd *xrcd;
1490 struct ib_qp *real_qp;
1491 int ret;
1493 real_qp = qp->real_qp;
1494 xrcd = real_qp->xrcd;
1496 mutex_lock(&xrcd->tgt_qp_mutex);
1497 ib_close_qp(qp);
1498 if (atomic_read(&real_qp->usecnt) == 0)
1499 list_del(&real_qp->xrcd_list);
1500 else
1501 real_qp = NULL;
1502 mutex_unlock(&xrcd->tgt_qp_mutex);
1504 if (real_qp) {
1505 ret = ib_destroy_qp(real_qp);
1506 if (!ret)
1507 atomic_dec(&xrcd->usecnt);
1508 else
1509 __ib_insert_xrcd_qp(xrcd, real_qp);
1512 return 0;
1515 int ib_destroy_qp(struct ib_qp *qp)
1517 struct ib_pd *pd;
1518 struct ib_cq *scq, *rcq;
1519 struct ib_srq *srq;
1520 struct ib_rwq_ind_table *ind_tbl;
1521 struct ib_qp_security *sec;
1522 int ret;
1524 WARN_ON_ONCE(qp->mrs_used > 0);
1526 if (atomic_read(&qp->usecnt))
1527 return -EBUSY;
1529 if (qp->real_qp != qp)
1530 return __ib_destroy_shared_qp(qp);
1532 pd = qp->pd;
1533 scq = qp->send_cq;
1534 rcq = qp->recv_cq;
1535 srq = qp->srq;
1536 ind_tbl = qp->rwq_ind_tbl;
1537 sec = qp->qp_sec;
1538 if (sec)
1539 ib_destroy_qp_security_begin(sec);
1541 if (!qp->uobject)
1542 rdma_rw_cleanup_mrs(qp);
1544 rdma_restrack_del(&qp->res);
1545 ret = qp->device->destroy_qp(qp);
1546 if (!ret) {
1547 if (pd)
1548 atomic_dec(&pd->usecnt);
1549 if (scq)
1550 atomic_dec(&scq->usecnt);
1551 if (rcq)
1552 atomic_dec(&rcq->usecnt);
1553 if (srq)
1554 atomic_dec(&srq->usecnt);
1555 if (ind_tbl)
1556 atomic_dec(&ind_tbl->usecnt);
1557 if (sec)
1558 ib_destroy_qp_security_end(sec);
1559 } else {
1560 if (sec)
1561 ib_destroy_qp_security_abort(sec);
1564 return ret;
1566 EXPORT_SYMBOL(ib_destroy_qp);
1568 /* Completion queues */
1570 struct ib_cq *ib_create_cq(struct ib_device *device,
1571 ib_comp_handler comp_handler,
1572 void (*event_handler)(struct ib_event *, void *),
1573 void *cq_context,
1574 const struct ib_cq_init_attr *cq_attr)
1576 struct ib_cq *cq;
1578 cq = device->create_cq(device, cq_attr, NULL, NULL);
1580 if (!IS_ERR(cq)) {
1581 cq->device = device;
1582 cq->uobject = NULL;
1583 cq->comp_handler = comp_handler;
1584 cq->event_handler = event_handler;
1585 cq->cq_context = cq_context;
1586 atomic_set(&cq->usecnt, 0);
1587 cq->res.type = RDMA_RESTRACK_CQ;
1588 rdma_restrack_add(&cq->res);
1591 return cq;
1593 EXPORT_SYMBOL(ib_create_cq);
1595 int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1597 return cq->device->modify_cq ?
1598 cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
1600 EXPORT_SYMBOL(rdma_set_cq_moderation);
1602 int ib_destroy_cq(struct ib_cq *cq)
1604 if (atomic_read(&cq->usecnt))
1605 return -EBUSY;
1607 rdma_restrack_del(&cq->res);
1608 return cq->device->destroy_cq(cq);
1610 EXPORT_SYMBOL(ib_destroy_cq);
1612 int ib_resize_cq(struct ib_cq *cq, int cqe)
1614 return cq->device->resize_cq ?
1615 cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
1617 EXPORT_SYMBOL(ib_resize_cq);
1619 /* Memory regions */
1621 int ib_dereg_mr(struct ib_mr *mr)
1623 struct ib_pd *pd = mr->pd;
1624 int ret;
1626 ret = mr->device->dereg_mr(mr);
1627 if (!ret)
1628 atomic_dec(&pd->usecnt);
1630 return ret;
1632 EXPORT_SYMBOL(ib_dereg_mr);
1635 * ib_alloc_mr() - Allocates a memory region
1636 * @pd: protection domain associated with the region
1637 * @mr_type: memory region type
1638 * @max_num_sg: maximum sg entries available for registration.
1640 * Notes:
1641 * Memory registeration page/sg lists must not exceed max_num_sg.
1642 * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
1643 * max_num_sg * used_page_size.
1646 struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
1647 enum ib_mr_type mr_type,
1648 u32 max_num_sg)
1650 struct ib_mr *mr;
1652 if (!pd->device->alloc_mr)
1653 return ERR_PTR(-ENOSYS);
1655 mr = pd->device->alloc_mr(pd, mr_type, max_num_sg);
1656 if (!IS_ERR(mr)) {
1657 mr->device = pd->device;
1658 mr->pd = pd;
1659 mr->uobject = NULL;
1660 atomic_inc(&pd->usecnt);
1661 mr->need_inval = false;
1664 return mr;
1666 EXPORT_SYMBOL(ib_alloc_mr);
1668 /* "Fast" memory regions */
1670 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
1671 int mr_access_flags,
1672 struct ib_fmr_attr *fmr_attr)
1674 struct ib_fmr *fmr;
1676 if (!pd->device->alloc_fmr)
1677 return ERR_PTR(-ENOSYS);
1679 fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
1680 if (!IS_ERR(fmr)) {
1681 fmr->device = pd->device;
1682 fmr->pd = pd;
1683 atomic_inc(&pd->usecnt);
1686 return fmr;
1688 EXPORT_SYMBOL(ib_alloc_fmr);
1690 int ib_unmap_fmr(struct list_head *fmr_list)
1692 struct ib_fmr *fmr;
1694 if (list_empty(fmr_list))
1695 return 0;
1697 fmr = list_entry(fmr_list->next, struct ib_fmr, list);
1698 return fmr->device->unmap_fmr(fmr_list);
1700 EXPORT_SYMBOL(ib_unmap_fmr);
1702 int ib_dealloc_fmr(struct ib_fmr *fmr)
1704 struct ib_pd *pd;
1705 int ret;
1707 pd = fmr->pd;
1708 ret = fmr->device->dealloc_fmr(fmr);
1709 if (!ret)
1710 atomic_dec(&pd->usecnt);
1712 return ret;
1714 EXPORT_SYMBOL(ib_dealloc_fmr);
1716 /* Multicast groups */
1718 static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
1720 struct ib_qp_init_attr init_attr = {};
1721 struct ib_qp_attr attr = {};
1722 int num_eth_ports = 0;
1723 int port;
1725 /* If QP state >= init, it is assigned to a port and we can check this
1726 * port only.
1728 if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
1729 if (attr.qp_state >= IB_QPS_INIT) {
1730 if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
1731 IB_LINK_LAYER_INFINIBAND)
1732 return true;
1733 goto lid_check;
1737 /* Can't get a quick answer, iterate over all ports */
1738 for (port = 0; port < qp->device->phys_port_cnt; port++)
1739 if (rdma_port_get_link_layer(qp->device, port) !=
1740 IB_LINK_LAYER_INFINIBAND)
1741 num_eth_ports++;
1743 /* If we have at lease one Ethernet port, RoCE annex declares that
1744 * multicast LID should be ignored. We can't tell at this step if the
1745 * QP belongs to an IB or Ethernet port.
1747 if (num_eth_ports)
1748 return true;
1750 /* If all the ports are IB, we can check according to IB spec. */
1751 lid_check:
1752 return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
1753 lid == be16_to_cpu(IB_LID_PERMISSIVE));
1756 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1758 int ret;
1760 if (!qp->device->attach_mcast)
1761 return -ENOSYS;
1763 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
1764 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
1765 return -EINVAL;
1767 ret = qp->device->attach_mcast(qp, gid, lid);
1768 if (!ret)
1769 atomic_inc(&qp->usecnt);
1770 return ret;
1772 EXPORT_SYMBOL(ib_attach_mcast);
1774 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1776 int ret;
1778 if (!qp->device->detach_mcast)
1779 return -ENOSYS;
1781 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
1782 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
1783 return -EINVAL;
1785 ret = qp->device->detach_mcast(qp, gid, lid);
1786 if (!ret)
1787 atomic_dec(&qp->usecnt);
1788 return ret;
1790 EXPORT_SYMBOL(ib_detach_mcast);
1792 struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller)
1794 struct ib_xrcd *xrcd;
1796 if (!device->alloc_xrcd)
1797 return ERR_PTR(-ENOSYS);
1799 xrcd = device->alloc_xrcd(device, NULL, NULL);
1800 if (!IS_ERR(xrcd)) {
1801 xrcd->device = device;
1802 xrcd->inode = NULL;
1803 atomic_set(&xrcd->usecnt, 0);
1804 mutex_init(&xrcd->tgt_qp_mutex);
1805 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
1808 return xrcd;
1810 EXPORT_SYMBOL(__ib_alloc_xrcd);
1812 int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1814 struct ib_qp *qp;
1815 int ret;
1817 if (atomic_read(&xrcd->usecnt))
1818 return -EBUSY;
1820 while (!list_empty(&xrcd->tgt_qp_list)) {
1821 qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
1822 ret = ib_destroy_qp(qp);
1823 if (ret)
1824 return ret;
1827 return xrcd->device->dealloc_xrcd(xrcd);
1829 EXPORT_SYMBOL(ib_dealloc_xrcd);
1832 * ib_create_wq - Creates a WQ associated with the specified protection
1833 * domain.
1834 * @pd: The protection domain associated with the WQ.
1835 * @wq_attr: A list of initial attributes required to create the
1836 * WQ. If WQ creation succeeds, then the attributes are updated to
1837 * the actual capabilities of the created WQ.
1839 * wq_attr->max_wr and wq_attr->max_sge determine
1840 * the requested size of the WQ, and set to the actual values allocated
1841 * on return.
1842 * If ib_create_wq() succeeds, then max_wr and max_sge will always be
1843 * at least as large as the requested values.
1845 struct ib_wq *ib_create_wq(struct ib_pd *pd,
1846 struct ib_wq_init_attr *wq_attr)
1848 struct ib_wq *wq;
1850 if (!pd->device->create_wq)
1851 return ERR_PTR(-ENOSYS);
1853 wq = pd->device->create_wq(pd, wq_attr, NULL);
1854 if (!IS_ERR(wq)) {
1855 wq->event_handler = wq_attr->event_handler;
1856 wq->wq_context = wq_attr->wq_context;
1857 wq->wq_type = wq_attr->wq_type;
1858 wq->cq = wq_attr->cq;
1859 wq->device = pd->device;
1860 wq->pd = pd;
1861 wq->uobject = NULL;
1862 atomic_inc(&pd->usecnt);
1863 atomic_inc(&wq_attr->cq->usecnt);
1864 atomic_set(&wq->usecnt, 0);
1866 return wq;
1868 EXPORT_SYMBOL(ib_create_wq);
1871 * ib_destroy_wq - Destroys the specified WQ.
1872 * @wq: The WQ to destroy.
1874 int ib_destroy_wq(struct ib_wq *wq)
1876 int err;
1877 struct ib_cq *cq = wq->cq;
1878 struct ib_pd *pd = wq->pd;
1880 if (atomic_read(&wq->usecnt))
1881 return -EBUSY;
1883 err = wq->device->destroy_wq(wq);
1884 if (!err) {
1885 atomic_dec(&pd->usecnt);
1886 atomic_dec(&cq->usecnt);
1888 return err;
1890 EXPORT_SYMBOL(ib_destroy_wq);
1893 * ib_modify_wq - Modifies the specified WQ.
1894 * @wq: The WQ to modify.
1895 * @wq_attr: On input, specifies the WQ attributes to modify.
1896 * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
1897 * are being modified.
1898 * On output, the current values of selected WQ attributes are returned.
1900 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
1901 u32 wq_attr_mask)
1903 int err;
1905 if (!wq->device->modify_wq)
1906 return -ENOSYS;
1908 err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
1909 return err;
1911 EXPORT_SYMBOL(ib_modify_wq);
1914 * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
1915 * @device: The device on which to create the rwq indirection table.
1916 * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
1917 * create the Indirection Table.
1919 * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
1920 * than the created ib_rwq_ind_table object and the caller is responsible
1921 * for its memory allocation/free.
1923 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
1924 struct ib_rwq_ind_table_init_attr *init_attr)
1926 struct ib_rwq_ind_table *rwq_ind_table;
1927 int i;
1928 u32 table_size;
1930 if (!device->create_rwq_ind_table)
1931 return ERR_PTR(-ENOSYS);
1933 table_size = (1 << init_attr->log_ind_tbl_size);
1934 rwq_ind_table = device->create_rwq_ind_table(device,
1935 init_attr, NULL);
1936 if (IS_ERR(rwq_ind_table))
1937 return rwq_ind_table;
1939 rwq_ind_table->ind_tbl = init_attr->ind_tbl;
1940 rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
1941 rwq_ind_table->device = device;
1942 rwq_ind_table->uobject = NULL;
1943 atomic_set(&rwq_ind_table->usecnt, 0);
1945 for (i = 0; i < table_size; i++)
1946 atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
1948 return rwq_ind_table;
1950 EXPORT_SYMBOL(ib_create_rwq_ind_table);
1953 * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
1954 * @wq_ind_table: The Indirection Table to destroy.
1956 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
1958 int err, i;
1959 u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
1960 struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
1962 if (atomic_read(&rwq_ind_table->usecnt))
1963 return -EBUSY;
1965 err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
1966 if (!err) {
1967 for (i = 0; i < table_size; i++)
1968 atomic_dec(&ind_tbl[i]->usecnt);
1971 return err;
1973 EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
1975 struct ib_flow *ib_create_flow(struct ib_qp *qp,
1976 struct ib_flow_attr *flow_attr,
1977 int domain)
1979 struct ib_flow *flow_id;
1980 if (!qp->device->create_flow)
1981 return ERR_PTR(-ENOSYS);
1983 flow_id = qp->device->create_flow(qp, flow_attr, domain);
1984 if (!IS_ERR(flow_id)) {
1985 atomic_inc(&qp->usecnt);
1986 flow_id->qp = qp;
1988 return flow_id;
1990 EXPORT_SYMBOL(ib_create_flow);
1992 int ib_destroy_flow(struct ib_flow *flow_id)
1994 int err;
1995 struct ib_qp *qp = flow_id->qp;
1997 err = qp->device->destroy_flow(flow_id);
1998 if (!err)
1999 atomic_dec(&qp->usecnt);
2000 return err;
2002 EXPORT_SYMBOL(ib_destroy_flow);
2004 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
2005 struct ib_mr_status *mr_status)
2007 return mr->device->check_mr_status ?
2008 mr->device->check_mr_status(mr, check_mask, mr_status) : -ENOSYS;
2010 EXPORT_SYMBOL(ib_check_mr_status);
2012 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
2013 int state)
2015 if (!device->set_vf_link_state)
2016 return -ENOSYS;
2018 return device->set_vf_link_state(device, vf, port, state);
2020 EXPORT_SYMBOL(ib_set_vf_link_state);
2022 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
2023 struct ifla_vf_info *info)
2025 if (!device->get_vf_config)
2026 return -ENOSYS;
2028 return device->get_vf_config(device, vf, port, info);
2030 EXPORT_SYMBOL(ib_get_vf_config);
2032 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
2033 struct ifla_vf_stats *stats)
2035 if (!device->get_vf_stats)
2036 return -ENOSYS;
2038 return device->get_vf_stats(device, vf, port, stats);
2040 EXPORT_SYMBOL(ib_get_vf_stats);
2042 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
2043 int type)
2045 if (!device->set_vf_guid)
2046 return -ENOSYS;
2048 return device->set_vf_guid(device, vf, port, guid, type);
2050 EXPORT_SYMBOL(ib_set_vf_guid);
2053 * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
2054 * and set it the memory region.
2055 * @mr: memory region
2056 * @sg: dma mapped scatterlist
2057 * @sg_nents: number of entries in sg
2058 * @sg_offset: offset in bytes into sg
2059 * @page_size: page vector desired page size
2061 * Constraints:
2062 * - The first sg element is allowed to have an offset.
2063 * - Each sg element must either be aligned to page_size or virtually
2064 * contiguous to the previous element. In case an sg element has a
2065 * non-contiguous offset, the mapping prefix will not include it.
2066 * - The last sg element is allowed to have length less than page_size.
2067 * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
2068 * then only max_num_sg entries will be mapped.
2069 * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
2070 * constraints holds and the page_size argument is ignored.
2072 * Returns the number of sg elements that were mapped to the memory region.
2074 * After this completes successfully, the memory region
2075 * is ready for registration.
2077 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
2078 unsigned int *sg_offset, unsigned int page_size)
2080 if (unlikely(!mr->device->map_mr_sg))
2081 return -ENOSYS;
2083 mr->page_size = page_size;
2085 return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
2087 EXPORT_SYMBOL(ib_map_mr_sg);
2090 * ib_sg_to_pages() - Convert the largest prefix of a sg list
2091 * to a page vector
2092 * @mr: memory region
2093 * @sgl: dma mapped scatterlist
2094 * @sg_nents: number of entries in sg
2095 * @sg_offset_p: IN: start offset in bytes into sg
2096 * OUT: offset in bytes for element n of the sg of the first
2097 * byte that has not been processed where n is the return
2098 * value of this function.
2099 * @set_page: driver page assignment function pointer
2101 * Core service helper for drivers to convert the largest
2102 * prefix of given sg list to a page vector. The sg list
2103 * prefix converted is the prefix that meet the requirements
2104 * of ib_map_mr_sg.
2106 * Returns the number of sg elements that were assigned to
2107 * a page vector.
2109 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
2110 unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
2112 struct scatterlist *sg;
2113 u64 last_end_dma_addr = 0;
2114 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
2115 unsigned int last_page_off = 0;
2116 u64 page_mask = ~((u64)mr->page_size - 1);
2117 int i, ret;
2119 if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
2120 return -EINVAL;
2122 mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
2123 mr->length = 0;
2125 for_each_sg(sgl, sg, sg_nents, i) {
2126 u64 dma_addr = sg_dma_address(sg) + sg_offset;
2127 u64 prev_addr = dma_addr;
2128 unsigned int dma_len = sg_dma_len(sg) - sg_offset;
2129 u64 end_dma_addr = dma_addr + dma_len;
2130 u64 page_addr = dma_addr & page_mask;
2133 * For the second and later elements, check whether either the
2134 * end of element i-1 or the start of element i is not aligned
2135 * on a page boundary.
2137 if (i && (last_page_off != 0 || page_addr != dma_addr)) {
2138 /* Stop mapping if there is a gap. */
2139 if (last_end_dma_addr != dma_addr)
2140 break;
2143 * Coalesce this element with the last. If it is small
2144 * enough just update mr->length. Otherwise start
2145 * mapping from the next page.
2147 goto next_page;
2150 do {
2151 ret = set_page(mr, page_addr);
2152 if (unlikely(ret < 0)) {
2153 sg_offset = prev_addr - sg_dma_address(sg);
2154 mr->length += prev_addr - dma_addr;
2155 if (sg_offset_p)
2156 *sg_offset_p = sg_offset;
2157 return i || sg_offset ? i : ret;
2159 prev_addr = page_addr;
2160 next_page:
2161 page_addr += mr->page_size;
2162 } while (page_addr < end_dma_addr);
2164 mr->length += dma_len;
2165 last_end_dma_addr = end_dma_addr;
2166 last_page_off = end_dma_addr & ~page_mask;
2168 sg_offset = 0;
2171 if (sg_offset_p)
2172 *sg_offset_p = 0;
2173 return i;
2175 EXPORT_SYMBOL(ib_sg_to_pages);
2177 struct ib_drain_cqe {
2178 struct ib_cqe cqe;
2179 struct completion done;
2182 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
2184 struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
2185 cqe);
2187 complete(&cqe->done);
2191 * Post a WR and block until its completion is reaped for the SQ.
2193 static void __ib_drain_sq(struct ib_qp *qp)
2195 struct ib_cq *cq = qp->send_cq;
2196 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2197 struct ib_drain_cqe sdrain;
2198 struct ib_send_wr swr = {}, *bad_swr;
2199 int ret;
2201 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2202 if (ret) {
2203 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2204 return;
2207 swr.wr_cqe = &sdrain.cqe;
2208 sdrain.cqe.done = ib_drain_qp_done;
2209 init_completion(&sdrain.done);
2211 ret = ib_post_send(qp, &swr, &bad_swr);
2212 if (ret) {
2213 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2214 return;
2217 if (cq->poll_ctx == IB_POLL_DIRECT)
2218 while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
2219 ib_process_cq_direct(cq, -1);
2220 else
2221 wait_for_completion(&sdrain.done);
2225 * Post a WR and block until its completion is reaped for the RQ.
2227 static void __ib_drain_rq(struct ib_qp *qp)
2229 struct ib_cq *cq = qp->recv_cq;
2230 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2231 struct ib_drain_cqe rdrain;
2232 struct ib_recv_wr rwr = {}, *bad_rwr;
2233 int ret;
2235 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2236 if (ret) {
2237 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2238 return;
2241 rwr.wr_cqe = &rdrain.cqe;
2242 rdrain.cqe.done = ib_drain_qp_done;
2243 init_completion(&rdrain.done);
2245 ret = ib_post_recv(qp, &rwr, &bad_rwr);
2246 if (ret) {
2247 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2248 return;
2251 if (cq->poll_ctx == IB_POLL_DIRECT)
2252 while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
2253 ib_process_cq_direct(cq, -1);
2254 else
2255 wait_for_completion(&rdrain.done);
2259 * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2260 * application.
2261 * @qp: queue pair to drain
2263 * If the device has a provider-specific drain function, then
2264 * call that. Otherwise call the generic drain function
2265 * __ib_drain_sq().
2267 * The caller must:
2269 * ensure there is room in the CQ and SQ for the drain work request and
2270 * completion.
2272 * allocate the CQ using ib_alloc_cq().
2274 * ensure that there are no other contexts that are posting WRs concurrently.
2275 * Otherwise the drain is not guaranteed.
2277 void ib_drain_sq(struct ib_qp *qp)
2279 if (qp->device->drain_sq)
2280 qp->device->drain_sq(qp);
2281 else
2282 __ib_drain_sq(qp);
2284 EXPORT_SYMBOL(ib_drain_sq);
2287 * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2288 * application.
2289 * @qp: queue pair to drain
2291 * If the device has a provider-specific drain function, then
2292 * call that. Otherwise call the generic drain function
2293 * __ib_drain_rq().
2295 * The caller must:
2297 * ensure there is room in the CQ and RQ for the drain work request and
2298 * completion.
2300 * allocate the CQ using ib_alloc_cq().
2302 * ensure that there are no other contexts that are posting WRs concurrently.
2303 * Otherwise the drain is not guaranteed.
2305 void ib_drain_rq(struct ib_qp *qp)
2307 if (qp->device->drain_rq)
2308 qp->device->drain_rq(qp);
2309 else
2310 __ib_drain_rq(qp);
2312 EXPORT_SYMBOL(ib_drain_rq);
2315 * ib_drain_qp() - Block until all CQEs have been consumed by the
2316 * application on both the RQ and SQ.
2317 * @qp: queue pair to drain
2319 * The caller must:
2321 * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2322 * and completions.
2324 * allocate the CQs using ib_alloc_cq().
2326 * ensure that there are no other contexts that are posting WRs concurrently.
2327 * Otherwise the drain is not guaranteed.
2329 void ib_drain_qp(struct ib_qp *qp)
2331 ib_drain_sq(qp);
2332 if (!qp->srq)
2333 ib_drain_rq(qp);
2335 EXPORT_SYMBOL(ib_drain_qp);