]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/infiniband/core/verbs.c
IB/cm: Fix sleeping in atomic when RoCE is used
[mirror_ubuntu-bionic-kernel.git] / drivers / infiniband / core / verbs.c
CommitLineData
1da177e4
LT
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.
2a1d9b7f 7 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
33b9b3ee 8 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
1da177e4
LT
9 *
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:
15 *
16 * Redistribution and use in source and binary forms, with or
17 * without modification, are permitted provided that the following
18 * conditions are met:
19 *
20 * - Redistributions of source code must retain the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer.
23 *
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.
28 *
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.
1da177e4
LT
37 */
38
39#include <linux/errno.h>
40#include <linux/err.h>
b108d976 41#include <linux/export.h>
8c65b4a6 42#include <linux/string.h>
0e0ec7e0 43#include <linux/slab.h>
dbf727de
MB
44#include <linux/in.h>
45#include <linux/in6.h>
46#include <net/addrconf.h>
d291f1a6 47#include <linux/security.h>
1da177e4 48
a4d61e84
RD
49#include <rdma/ib_verbs.h>
50#include <rdma/ib_cache.h>
dd5f03be 51#include <rdma/ib_addr.h>
a060b562 52#include <rdma/rw.h>
1da177e4 53
ed4c54e5 54#include "core_priv.h"
1da177e4 55
2b1b5b60
SG
56static 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",
76};
77
db7489e0 78const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
2b1b5b60
SG
79{
80 size_t index = event;
81
82 return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
83 ib_events[index] : "unrecognized event";
84}
85EXPORT_SYMBOL(ib_event_msg);
86
87static 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",
110};
111
db7489e0 112const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
2b1b5b60
SG
113{
114 size_t index = status;
115
116 return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
117 wc_statuses[index] : "unrecognized status";
118}
119EXPORT_SYMBOL(ib_wc_status_msg);
120
8385fd84 121__attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
bf6a9e31
JM
122{
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;
134 }
135}
136EXPORT_SYMBOL(ib_rate_to_mult);
137
8385fd84 138__attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
bf6a9e31
JM
139{
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;
151 }
152}
153EXPORT_SYMBOL(mult_to_ib_rate);
154
8385fd84 155__attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
71eeba16
MA
156{
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;
176 }
177}
178EXPORT_SYMBOL(ib_rate_to_mbps);
179
8385fd84 180__attribute_const__ enum rdma_transport_type
07ebafba
TT
181rdma_node_get_transport(enum rdma_node_type node_type)
182{
cdc596d8
LR
183
184 if (node_type == RDMA_NODE_USNIC)
5db5765e 185 return RDMA_TRANSPORT_USNIC;
cdc596d8 186 if (node_type == RDMA_NODE_USNIC_UDP)
248567f7 187 return RDMA_TRANSPORT_USNIC_UDP;
cdc596d8
LR
188 if (node_type == RDMA_NODE_RNIC)
189 return RDMA_TRANSPORT_IWARP;
190
191 return RDMA_TRANSPORT_IB;
07ebafba
TT
192}
193EXPORT_SYMBOL(rdma_node_get_transport);
194
a3f5adaf
EC
195enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
196{
82901e3e 197 enum rdma_transport_type lt;
a3f5adaf
EC
198 if (device->get_link_layer)
199 return device->get_link_layer(device, port_num);
200
82901e3e
LR
201 lt = rdma_node_get_transport(device->node_type);
202 if (lt == RDMA_TRANSPORT_IB)
a3f5adaf 203 return IB_LINK_LAYER_INFINIBAND;
82901e3e
LR
204
205 return IB_LINK_LAYER_ETHERNET;
a3f5adaf
EC
206}
207EXPORT_SYMBOL(rdma_port_get_link_layer);
208
1da177e4
LT
209/* Protection domains */
210
96249d70
JG
211/**
212 * ib_alloc_pd - Allocates an unused protection domain.
213 * @device: The device on which to allocate the protection domain.
214 *
215 * A protection domain object provides an association between QPs, shared
216 * receive queues, address handles, memory regions, and memory windows.
217 *
218 * Every PD has a local_dma_lkey which can be used as the lkey value for local
219 * memory operations.
220 */
ed082d36
CH
221struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
222 const char *caller)
1da177e4
LT
223{
224 struct ib_pd *pd;
ed082d36 225 int mr_access_flags = 0;
1da177e4 226
b5e81bf5 227 pd = device->alloc_pd(device, NULL, NULL);
96249d70
JG
228 if (IS_ERR(pd))
229 return pd;
1da177e4 230
96249d70
JG
231 pd->device = device;
232 pd->uobject = NULL;
50d46335 233 pd->__internal_mr = NULL;
96249d70 234 atomic_set(&pd->usecnt, 0);
ed082d36 235 pd->flags = flags;
1da177e4 236
86bee4c9 237 if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
96249d70 238 pd->local_dma_lkey = device->local_dma_lkey;
ed082d36
CH
239 else
240 mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
241
242 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
243 pr_warn("%s: enabling unsafe global rkey\n", caller);
244 mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
245 }
246
247 if (mr_access_flags) {
96249d70
JG
248 struct ib_mr *mr;
249
5ef990f0 250 mr = pd->device->get_dma_mr(pd, mr_access_flags);
96249d70
JG
251 if (IS_ERR(mr)) {
252 ib_dealloc_pd(pd);
5ef990f0 253 return ERR_CAST(mr);
96249d70 254 }
1da177e4 255
5ef990f0
CH
256 mr->device = pd->device;
257 mr->pd = pd;
258 mr->uobject = NULL;
259 mr->need_inval = false;
260
50d46335 261 pd->__internal_mr = mr;
ed082d36
CH
262
263 if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
264 pd->local_dma_lkey = pd->__internal_mr->lkey;
265
266 if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
267 pd->unsafe_global_rkey = pd->__internal_mr->rkey;
1da177e4 268 }
ed082d36 269
1da177e4
LT
270 return pd;
271}
ed082d36 272EXPORT_SYMBOL(__ib_alloc_pd);
1da177e4 273
7dd78647
JG
274/**
275 * ib_dealloc_pd - Deallocates a protection domain.
276 * @pd: The protection domain to deallocate.
277 *
278 * It is an error to call this function while any resources in the pd still
279 * exist. The caller is responsible to synchronously destroy them and
280 * guarantee no new allocations will happen.
281 */
282void ib_dealloc_pd(struct ib_pd *pd)
1da177e4 283{
7dd78647
JG
284 int ret;
285
50d46335 286 if (pd->__internal_mr) {
5ef990f0 287 ret = pd->device->dereg_mr(pd->__internal_mr);
7dd78647 288 WARN_ON(ret);
50d46335 289 pd->__internal_mr = NULL;
96249d70 290 }
1da177e4 291
7dd78647
JG
292 /* uverbs manipulates usecnt with proper locking, while the kabi
293 requires the caller to guarantee we can't race here. */
294 WARN_ON(atomic_read(&pd->usecnt));
1da177e4 295
7dd78647
JG
296 /* Making delalloc_pd a void return is a WIP, no driver should return
297 an error here. */
298 ret = pd->device->dealloc_pd(pd);
299 WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
1da177e4
LT
300}
301EXPORT_SYMBOL(ib_dealloc_pd);
302
303/* Address handles */
304
0a18cfe4 305struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr)
1da177e4
LT
306{
307 struct ib_ah *ah;
308
477864c8 309 ah = pd->device->create_ah(pd, ah_attr, NULL);
1da177e4
LT
310
311 if (!IS_ERR(ah)) {
b5e81bf5
RD
312 ah->device = pd->device;
313 ah->pd = pd;
314 ah->uobject = NULL;
44c58487 315 ah->type = ah_attr->type;
1da177e4
LT
316 atomic_inc(&pd->usecnt);
317 }
318
319 return ah;
320}
0a18cfe4 321EXPORT_SYMBOL(rdma_create_ah);
1da177e4 322
850d8fd7 323int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
c865f246
SK
324{
325 const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
326 struct iphdr ip4h_checked;
327 const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;
328
329 /* If it's IPv6, the version must be 6, otherwise, the first
330 * 20 bytes (before the IPv4 header) are garbled.
331 */
332 if (ip6h->version != 6)
333 return (ip4h->version == 4) ? 4 : 0;
334 /* version may be 6 or 4 because the first 20 bytes could be garbled */
335
336 /* RoCE v2 requires no options, thus header length
337 * must be 5 words
338 */
339 if (ip4h->ihl != 5)
340 return 6;
341
342 /* Verify checksum.
343 * We can't write on scattered buffers so we need to copy to
344 * temp buffer.
345 */
346 memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
347 ip4h_checked.check = 0;
348 ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
349 /* if IPv4 header checksum is OK, believe it */
350 if (ip4h->check == ip4h_checked.check)
351 return 4;
352 return 6;
353}
850d8fd7 354EXPORT_SYMBOL(ib_get_rdma_header_version);
c865f246
SK
355
356static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
357 u8 port_num,
358 const struct ib_grh *grh)
359{
360 int grh_version;
361
362 if (rdma_protocol_ib(device, port_num))
363 return RDMA_NETWORK_IB;
364
850d8fd7 365 grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
c865f246
SK
366
367 if (grh_version == 4)
368 return RDMA_NETWORK_IPV4;
369
370 if (grh->next_hdr == IPPROTO_UDP)
371 return RDMA_NETWORK_IPV6;
372
373 return RDMA_NETWORK_ROCE_V1;
374}
375
dbf727de
MB
376struct find_gid_index_context {
377 u16 vlan_id;
c865f246 378 enum ib_gid_type gid_type;
dbf727de
MB
379};
380
381static bool find_gid_index(const union ib_gid *gid,
382 const struct ib_gid_attr *gid_attr,
383 void *context)
384{
385 struct find_gid_index_context *ctx =
386 (struct find_gid_index_context *)context;
387
c865f246
SK
388 if (ctx->gid_type != gid_attr->gid_type)
389 return false;
390
dbf727de
MB
391 if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) ||
392 (is_vlan_dev(gid_attr->ndev) &&
393 vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id))
394 return false;
395
396 return true;
397}
398
399static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num,
400 u16 vlan_id, const union ib_gid *sgid,
c865f246 401 enum ib_gid_type gid_type,
dbf727de
MB
402 u16 *gid_index)
403{
c865f246
SK
404 struct find_gid_index_context context = {.vlan_id = vlan_id,
405 .gid_type = gid_type};
dbf727de
MB
406
407 return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index,
408 &context, gid_index);
409}
410
850d8fd7
MS
411int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
412 enum rdma_network_type net_type,
413 union ib_gid *sgid, union ib_gid *dgid)
c865f246
SK
414{
415 struct sockaddr_in src_in;
416 struct sockaddr_in dst_in;
417 __be32 src_saddr, dst_saddr;
418
419 if (!sgid || !dgid)
420 return -EINVAL;
421
422 if (net_type == RDMA_NETWORK_IPV4) {
423 memcpy(&src_in.sin_addr.s_addr,
424 &hdr->roce4grh.saddr, 4);
425 memcpy(&dst_in.sin_addr.s_addr,
426 &hdr->roce4grh.daddr, 4);
427 src_saddr = src_in.sin_addr.s_addr;
428 dst_saddr = dst_in.sin_addr.s_addr;
429 ipv6_addr_set_v4mapped(src_saddr,
430 (struct in6_addr *)sgid);
431 ipv6_addr_set_v4mapped(dst_saddr,
432 (struct in6_addr *)dgid);
433 return 0;
434 } else if (net_type == RDMA_NETWORK_IPV6 ||
435 net_type == RDMA_NETWORK_IB) {
436 *dgid = hdr->ibgrh.dgid;
437 *sgid = hdr->ibgrh.sgid;
438 return 0;
439 } else {
440 return -EINVAL;
441 }
442}
850d8fd7 443EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
c865f246 444
28b5b3a2
GS
445/*
446 * This function creates ah from the incoming packet.
447 * Incoming packet has dgid of the receiver node on which this code is
448 * getting executed and, sgid contains the GID of the sender.
449 *
450 * When resolving mac address of destination, the arrived dgid is used
451 * as sgid and, sgid is used as dgid because sgid contains destinations
452 * GID whom to respond to.
453 *
454 * This is why when calling rdma_addr_find_l2_eth_by_grh() function, the
455 * position of arguments dgid and sgid do not match the order of the
456 * parameters.
457 */
73cdaaee
IW
458int ib_init_ah_from_wc(struct ib_device *device, u8 port_num,
459 const struct ib_wc *wc, const struct ib_grh *grh,
90898850 460 struct rdma_ah_attr *ah_attr)
513789ed 461{
513789ed
HR
462 u32 flow_class;
463 u16 gid_index;
464 int ret;
c865f246
SK
465 enum rdma_network_type net_type = RDMA_NETWORK_IB;
466 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
c3efe750 467 int hoplimit = 0xff;
c865f246
SK
468 union ib_gid dgid;
469 union ib_gid sgid;
513789ed 470
4e00d694 471 memset(ah_attr, 0, sizeof *ah_attr);
44c58487 472 ah_attr->type = rdma_ah_find_type(device, port_num);
227128fc 473 if (rdma_cap_eth_ah(device, port_num)) {
c865f246
SK
474 if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
475 net_type = wc->network_hdr_type;
476 else
477 net_type = ib_get_net_type_by_grh(device, port_num, grh);
478 gid_type = ib_network_to_gid_type(net_type);
479 }
850d8fd7
MS
480 ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
481 &sgid, &dgid);
c865f246
SK
482 if (ret)
483 return ret;
484
485 if (rdma_protocol_roce(device, port_num)) {
20029832 486 int if_index = 0;
dbf727de
MB
487 u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
488 wc->vlan_id : 0xffff;
20029832
MB
489 struct net_device *idev;
490 struct net_device *resolved_dev;
dbf727de 491
dd5f03be
MB
492 if (!(wc->wc_flags & IB_WC_GRH))
493 return -EPROTOTYPE;
494
20029832
MB
495 if (!device->get_netdev)
496 return -EOPNOTSUPP;
497
498 idev = device->get_netdev(device, port_num);
499 if (!idev)
500 return -ENODEV;
501
f7f4b23e 502 ret = rdma_addr_find_l2_eth_by_grh(&dgid, &sgid,
44c58487 503 ah_attr->roce.dmac,
f7f4b23e
MB
504 wc->wc_flags & IB_WC_WITH_VLAN ?
505 NULL : &vlan_id,
c3efe750 506 &if_index, &hoplimit);
20029832
MB
507 if (ret) {
508 dev_put(idev);
509 return ret;
dd5f03be 510 }
dbf727de 511
20029832 512 resolved_dev = dev_get_by_index(&init_net, if_index);
20029832
MB
513 rcu_read_lock();
514 if (resolved_dev != idev && !rdma_is_upper_dev_rcu(idev,
515 resolved_dev))
516 ret = -EHOSTUNREACH;
517 rcu_read_unlock();
518 dev_put(idev);
519 dev_put(resolved_dev);
520 if (ret)
521 return ret;
522
dbf727de 523 ret = get_sgid_index_from_eth(device, port_num, vlan_id,
c865f246 524 &dgid, gid_type, &gid_index);
dbf727de
MB
525 if (ret)
526 return ret;
dd5f03be
MB
527 }
528
d8966fcd
DC
529 rdma_ah_set_dlid(ah_attr, wc->slid);
530 rdma_ah_set_sl(ah_attr, wc->sl);
531 rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
532 rdma_ah_set_port_num(ah_attr, port_num);
513789ed
HR
533
534 if (wc->wc_flags & IB_WC_GRH) {
dbf727de 535 if (!rdma_cap_eth_ah(device, port_num)) {
b3556005
EC
536 if (dgid.global.interface_id != cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
537 ret = ib_find_cached_gid_by_port(device, &dgid,
538 IB_GID_TYPE_IB,
539 port_num, NULL,
540 &gid_index);
541 if (ret)
542 return ret;
543 } else {
544 gid_index = 0;
545 }
dbf727de 546 }
513789ed 547
497677ab 548 flow_class = be32_to_cpu(grh->version_tclass_flow);
d8966fcd
DC
549 rdma_ah_set_grh(ah_attr, &sgid,
550 flow_class & 0xFFFFF,
551 (u8)gid_index, hoplimit,
552 (flow_class >> 20) & 0xFF);
553
513789ed 554 }
4e00d694
SH
555 return 0;
556}
557EXPORT_SYMBOL(ib_init_ah_from_wc);
558
73cdaaee
IW
559struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
560 const struct ib_grh *grh, u8 port_num)
4e00d694 561{
90898850 562 struct rdma_ah_attr ah_attr;
4e00d694
SH
563 int ret;
564
565 ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr);
566 if (ret)
567 return ERR_PTR(ret);
513789ed 568
0a18cfe4 569 return rdma_create_ah(pd, &ah_attr);
513789ed
HR
570}
571EXPORT_SYMBOL(ib_create_ah_from_wc);
572
67b985b6 573int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
1da177e4 574{
44c58487
DC
575 if (ah->type != ah_attr->type)
576 return -EINVAL;
577
1da177e4
LT
578 return ah->device->modify_ah ?
579 ah->device->modify_ah(ah, ah_attr) :
580 -ENOSYS;
581}
67b985b6 582EXPORT_SYMBOL(rdma_modify_ah);
1da177e4 583
bfbfd661 584int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
1da177e4
LT
585{
586 return ah->device->query_ah ?
587 ah->device->query_ah(ah, ah_attr) :
588 -ENOSYS;
589}
bfbfd661 590EXPORT_SYMBOL(rdma_query_ah);
1da177e4 591
36523159 592int rdma_destroy_ah(struct ib_ah *ah)
1da177e4
LT
593{
594 struct ib_pd *pd;
595 int ret;
596
597 pd = ah->pd;
598 ret = ah->device->destroy_ah(ah);
599 if (!ret)
600 atomic_dec(&pd->usecnt);
601
602 return ret;
603}
36523159 604EXPORT_SYMBOL(rdma_destroy_ah);
1da177e4 605
d41fcc67
RD
606/* Shared receive queues */
607
608struct ib_srq *ib_create_srq(struct ib_pd *pd,
609 struct ib_srq_init_attr *srq_init_attr)
610{
611 struct ib_srq *srq;
612
613 if (!pd->device->create_srq)
614 return ERR_PTR(-ENOSYS);
615
616 srq = pd->device->create_srq(pd, srq_init_attr, NULL);
617
618 if (!IS_ERR(srq)) {
619 srq->device = pd->device;
620 srq->pd = pd;
621 srq->uobject = NULL;
622 srq->event_handler = srq_init_attr->event_handler;
623 srq->srq_context = srq_init_attr->srq_context;
96104eda 624 srq->srq_type = srq_init_attr->srq_type;
1a56ff6d
AK
625 if (ib_srq_has_cq(srq->srq_type)) {
626 srq->ext.cq = srq_init_attr->ext.cq;
627 atomic_inc(&srq->ext.cq->usecnt);
628 }
418d5130
SH
629 if (srq->srq_type == IB_SRQT_XRC) {
630 srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
418d5130 631 atomic_inc(&srq->ext.xrc.xrcd->usecnt);
418d5130 632 }
d41fcc67
RD
633 atomic_inc(&pd->usecnt);
634 atomic_set(&srq->usecnt, 0);
635 }
636
637 return srq;
638}
639EXPORT_SYMBOL(ib_create_srq);
640
641int ib_modify_srq(struct ib_srq *srq,
642 struct ib_srq_attr *srq_attr,
643 enum ib_srq_attr_mask srq_attr_mask)
644{
7ce5eacb
DB
645 return srq->device->modify_srq ?
646 srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
647 -ENOSYS;
d41fcc67
RD
648}
649EXPORT_SYMBOL(ib_modify_srq);
650
651int ib_query_srq(struct ib_srq *srq,
652 struct ib_srq_attr *srq_attr)
653{
654 return srq->device->query_srq ?
655 srq->device->query_srq(srq, srq_attr) : -ENOSYS;
656}
657EXPORT_SYMBOL(ib_query_srq);
658
659int ib_destroy_srq(struct ib_srq *srq)
660{
661 struct ib_pd *pd;
418d5130
SH
662 enum ib_srq_type srq_type;
663 struct ib_xrcd *uninitialized_var(xrcd);
664 struct ib_cq *uninitialized_var(cq);
d41fcc67
RD
665 int ret;
666
667 if (atomic_read(&srq->usecnt))
668 return -EBUSY;
669
670 pd = srq->pd;
418d5130 671 srq_type = srq->srq_type;
1a56ff6d
AK
672 if (ib_srq_has_cq(srq_type))
673 cq = srq->ext.cq;
674 if (srq_type == IB_SRQT_XRC)
418d5130 675 xrcd = srq->ext.xrc.xrcd;
d41fcc67
RD
676
677 ret = srq->device->destroy_srq(srq);
418d5130 678 if (!ret) {
d41fcc67 679 atomic_dec(&pd->usecnt);
1a56ff6d 680 if (srq_type == IB_SRQT_XRC)
418d5130 681 atomic_dec(&xrcd->usecnt);
1a56ff6d 682 if (ib_srq_has_cq(srq_type))
418d5130 683 atomic_dec(&cq->usecnt);
418d5130 684 }
d41fcc67
RD
685
686 return ret;
687}
688EXPORT_SYMBOL(ib_destroy_srq);
689
1da177e4
LT
690/* Queue pairs */
691
0e0ec7e0
SH
692static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
693{
694 struct ib_qp *qp = context;
73c40c61 695 unsigned long flags;
0e0ec7e0 696
73c40c61 697 spin_lock_irqsave(&qp->device->event_handler_lock, flags);
0e0ec7e0 698 list_for_each_entry(event->element.qp, &qp->open_list, open_list)
eec9e29f
SP
699 if (event->element.qp->event_handler)
700 event->element.qp->event_handler(event, event->element.qp->qp_context);
73c40c61 701 spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
0e0ec7e0
SH
702}
703
d3d72d90
SH
704static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
705{
706 mutex_lock(&xrcd->tgt_qp_mutex);
707 list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
708 mutex_unlock(&xrcd->tgt_qp_mutex);
709}
710
0e0ec7e0
SH
711static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
712 void (*event_handler)(struct ib_event *, void *),
713 void *qp_context)
d3d72d90 714{
0e0ec7e0
SH
715 struct ib_qp *qp;
716 unsigned long flags;
d291f1a6 717 int err;
0e0ec7e0
SH
718
719 qp = kzalloc(sizeof *qp, GFP_KERNEL);
720 if (!qp)
721 return ERR_PTR(-ENOMEM);
722
d291f1a6
DJ
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);
728 }
729
0e0ec7e0
SH
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;
737
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);
741
742 return qp;
743}
744
745struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
746 struct ib_qp_open_attr *qp_open_attr)
747{
748 struct ib_qp *qp, *real_qp;
749
750 if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
751 return ERR_PTR(-EINVAL);
752
753 qp = ERR_PTR(-EINVAL);
d3d72d90 754 mutex_lock(&xrcd->tgt_qp_mutex);
0e0ec7e0
SH
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;
760 }
761 }
d3d72d90 762 mutex_unlock(&xrcd->tgt_qp_mutex);
0e0ec7e0 763 return qp;
d3d72d90 764}
0e0ec7e0 765EXPORT_SYMBOL(ib_open_qp);
d3d72d90 766
04c41bf3
CH
767static struct ib_qp *ib_create_xrc_qp(struct ib_qp *qp,
768 struct ib_qp_init_attr *qp_init_attr)
769{
770 struct ib_qp *real_qp = qp;
771
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);
780
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;
788}
789
1da177e4
LT
790struct ib_qp *ib_create_qp(struct ib_pd *pd,
791 struct ib_qp_init_attr *qp_init_attr)
792{
04c41bf3
CH
793 struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
794 struct ib_qp *qp;
a060b562
CH
795 int ret;
796
a9017e23
YH
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);
802
a060b562
CH
803 /*
804 * If the callers is using the RDMA API calculate the resources
805 * needed for the RDMA READ/WRITE operations.
806 *
807 * Note that these callers need to pass in a port number.
808 */
809 if (qp_init_attr->cap.max_rdma_ctxs)
810 rdma_rw_init_qp(device, qp_init_attr);
1da177e4 811
b42b63cf 812 qp = device->create_qp(pd, qp_init_attr, NULL);
04c41bf3
CH
813 if (IS_ERR(qp))
814 return qp;
815
d291f1a6
DJ
816 ret = ib_create_qp_security(qp, device);
817 if (ret) {
818 ib_destroy_qp(qp);
819 return ERR_PTR(ret);
820 }
821
04c41bf3
CH
822 qp->device = device;
823 qp->real_qp = qp;
824 qp->uobject = NULL;
825 qp->qp_type = qp_init_attr->qp_type;
a9017e23 826 qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
04c41bf3
CH
827
828 atomic_set(&qp->usecnt, 0);
fffb0383
CH
829 qp->mrs_used = 0;
830 spin_lock_init(&qp->mr_lock);
a060b562 831 INIT_LIST_HEAD(&qp->rdma_mrs);
0e353e34 832 INIT_LIST_HEAD(&qp->sig_mrs);
498ca3c8 833 qp->port = 0;
fffb0383 834
04c41bf3
CH
835 if (qp_init_attr->qp_type == IB_QPT_XRC_TGT)
836 return ib_create_xrc_qp(qp, qp_init_attr);
837
838 qp->event_handler = qp_init_attr->event_handler;
839 qp->qp_context = qp_init_attr->qp_context;
840 if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
841 qp->recv_cq = NULL;
842 qp->srq = NULL;
843 } else {
844 qp->recv_cq = qp_init_attr->recv_cq;
a9017e23
YH
845 if (qp_init_attr->recv_cq)
846 atomic_inc(&qp_init_attr->recv_cq->usecnt);
04c41bf3
CH
847 qp->srq = qp_init_attr->srq;
848 if (qp->srq)
849 atomic_inc(&qp_init_attr->srq->usecnt);
1da177e4
LT
850 }
851
04c41bf3
CH
852 qp->pd = pd;
853 qp->send_cq = qp_init_attr->send_cq;
854 qp->xrcd = NULL;
855
856 atomic_inc(&pd->usecnt);
a9017e23
YH
857 if (qp_init_attr->send_cq)
858 atomic_inc(&qp_init_attr->send_cq->usecnt);
859 if (qp_init_attr->rwq_ind_tbl)
860 atomic_inc(&qp->rwq_ind_tbl->usecnt);
a060b562
CH
861
862 if (qp_init_attr->cap.max_rdma_ctxs) {
863 ret = rdma_rw_init_mrs(qp, qp_init_attr);
864 if (ret) {
865 pr_err("failed to init MR pool ret= %d\n", ret);
866 ib_destroy_qp(qp);
b6bc1c73 867 return ERR_PTR(ret);
a060b562
CH
868 }
869 }
870
632bc3f6
BVA
871 /*
872 * Note: all hw drivers guarantee that max_send_sge is lower than
873 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
874 * max_send_sge <= max_sge_rd.
875 */
876 qp->max_write_sge = qp_init_attr->cap.max_send_sge;
877 qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
878 device->attrs.max_sge_rd);
879
1da177e4
LT
880 return qp;
881}
882EXPORT_SYMBOL(ib_create_qp);
883
8a51866f
RD
884static const struct {
885 int valid;
b42b63cf
SH
886 enum ib_qp_attr_mask req_param[IB_QPT_MAX];
887 enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
8a51866f
RD
888} qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
889 [IB_QPS_RESET] = {
890 [IB_QPS_RESET] = { .valid = 1 },
8a51866f
RD
891 [IB_QPS_INIT] = {
892 .valid = 1,
893 .req_param = {
894 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
895 IB_QP_PORT |
896 IB_QP_QKEY),
c938a616 897 [IB_QPT_RAW_PACKET] = IB_QP_PORT,
8a51866f
RD
898 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
899 IB_QP_PORT |
900 IB_QP_ACCESS_FLAGS),
901 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
902 IB_QP_PORT |
903 IB_QP_ACCESS_FLAGS),
b42b63cf
SH
904 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
905 IB_QP_PORT |
906 IB_QP_ACCESS_FLAGS),
907 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
908 IB_QP_PORT |
909 IB_QP_ACCESS_FLAGS),
8a51866f
RD
910 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
911 IB_QP_QKEY),
912 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
913 IB_QP_QKEY),
914 }
915 },
916 },
917 [IB_QPS_INIT] = {
918 [IB_QPS_RESET] = { .valid = 1 },
919 [IB_QPS_ERR] = { .valid = 1 },
920 [IB_QPS_INIT] = {
921 .valid = 1,
922 .opt_param = {
923 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
924 IB_QP_PORT |
925 IB_QP_QKEY),
926 [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
927 IB_QP_PORT |
928 IB_QP_ACCESS_FLAGS),
929 [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
930 IB_QP_PORT |
931 IB_QP_ACCESS_FLAGS),
b42b63cf
SH
932 [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
933 IB_QP_PORT |
934 IB_QP_ACCESS_FLAGS),
935 [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
936 IB_QP_PORT |
937 IB_QP_ACCESS_FLAGS),
8a51866f
RD
938 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
939 IB_QP_QKEY),
940 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
941 IB_QP_QKEY),
942 }
943 },
944 [IB_QPS_RTR] = {
945 .valid = 1,
946 .req_param = {
947 [IB_QPT_UC] = (IB_QP_AV |
948 IB_QP_PATH_MTU |
949 IB_QP_DEST_QPN |
950 IB_QP_RQ_PSN),
951 [IB_QPT_RC] = (IB_QP_AV |
952 IB_QP_PATH_MTU |
953 IB_QP_DEST_QPN |
954 IB_QP_RQ_PSN |
955 IB_QP_MAX_DEST_RD_ATOMIC |
956 IB_QP_MIN_RNR_TIMER),
b42b63cf
SH
957 [IB_QPT_XRC_INI] = (IB_QP_AV |
958 IB_QP_PATH_MTU |
959 IB_QP_DEST_QPN |
960 IB_QP_RQ_PSN),
961 [IB_QPT_XRC_TGT] = (IB_QP_AV |
962 IB_QP_PATH_MTU |
963 IB_QP_DEST_QPN |
964 IB_QP_RQ_PSN |
965 IB_QP_MAX_DEST_RD_ATOMIC |
966 IB_QP_MIN_RNR_TIMER),
8a51866f
RD
967 },
968 .opt_param = {
969 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
970 IB_QP_QKEY),
971 [IB_QPT_UC] = (IB_QP_ALT_PATH |
972 IB_QP_ACCESS_FLAGS |
973 IB_QP_PKEY_INDEX),
974 [IB_QPT_RC] = (IB_QP_ALT_PATH |
975 IB_QP_ACCESS_FLAGS |
976 IB_QP_PKEY_INDEX),
b42b63cf
SH
977 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
978 IB_QP_ACCESS_FLAGS |
979 IB_QP_PKEY_INDEX),
980 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
981 IB_QP_ACCESS_FLAGS |
982 IB_QP_PKEY_INDEX),
8a51866f
RD
983 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
984 IB_QP_QKEY),
985 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
986 IB_QP_QKEY),
dd5f03be 987 },
dbf727de 988 },
8a51866f
RD
989 },
990 [IB_QPS_RTR] = {
991 [IB_QPS_RESET] = { .valid = 1 },
992 [IB_QPS_ERR] = { .valid = 1 },
993 [IB_QPS_RTS] = {
994 .valid = 1,
995 .req_param = {
996 [IB_QPT_UD] = IB_QP_SQ_PSN,
997 [IB_QPT_UC] = IB_QP_SQ_PSN,
998 [IB_QPT_RC] = (IB_QP_TIMEOUT |
999 IB_QP_RETRY_CNT |
1000 IB_QP_RNR_RETRY |
1001 IB_QP_SQ_PSN |
1002 IB_QP_MAX_QP_RD_ATOMIC),
b42b63cf
SH
1003 [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
1004 IB_QP_RETRY_CNT |
1005 IB_QP_RNR_RETRY |
1006 IB_QP_SQ_PSN |
1007 IB_QP_MAX_QP_RD_ATOMIC),
1008 [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
1009 IB_QP_SQ_PSN),
8a51866f
RD
1010 [IB_QPT_SMI] = IB_QP_SQ_PSN,
1011 [IB_QPT_GSI] = IB_QP_SQ_PSN,
1012 },
1013 .opt_param = {
1014 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1015 IB_QP_QKEY),
1016 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1017 IB_QP_ALT_PATH |
1018 IB_QP_ACCESS_FLAGS |
1019 IB_QP_PATH_MIG_STATE),
1020 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1021 IB_QP_ALT_PATH |
1022 IB_QP_ACCESS_FLAGS |
1023 IB_QP_MIN_RNR_TIMER |
1024 IB_QP_PATH_MIG_STATE),
b42b63cf
SH
1025 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1026 IB_QP_ALT_PATH |
1027 IB_QP_ACCESS_FLAGS |
1028 IB_QP_PATH_MIG_STATE),
1029 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1030 IB_QP_ALT_PATH |
1031 IB_QP_ACCESS_FLAGS |
1032 IB_QP_MIN_RNR_TIMER |
1033 IB_QP_PATH_MIG_STATE),
8a51866f
RD
1034 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1035 IB_QP_QKEY),
1036 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1037 IB_QP_QKEY),
528e5a1b 1038 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
8a51866f
RD
1039 }
1040 }
1041 },
1042 [IB_QPS_RTS] = {
1043 [IB_QPS_RESET] = { .valid = 1 },
1044 [IB_QPS_ERR] = { .valid = 1 },
1045 [IB_QPS_RTS] = {
1046 .valid = 1,
1047 .opt_param = {
1048 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1049 IB_QP_QKEY),
4546d31d
DB
1050 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1051 IB_QP_ACCESS_FLAGS |
8a51866f
RD
1052 IB_QP_ALT_PATH |
1053 IB_QP_PATH_MIG_STATE),
4546d31d
DB
1054 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1055 IB_QP_ACCESS_FLAGS |
8a51866f
RD
1056 IB_QP_ALT_PATH |
1057 IB_QP_PATH_MIG_STATE |
1058 IB_QP_MIN_RNR_TIMER),
b42b63cf
SH
1059 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1060 IB_QP_ACCESS_FLAGS |
1061 IB_QP_ALT_PATH |
1062 IB_QP_PATH_MIG_STATE),
1063 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1064 IB_QP_ACCESS_FLAGS |
1065 IB_QP_ALT_PATH |
1066 IB_QP_PATH_MIG_STATE |
1067 IB_QP_MIN_RNR_TIMER),
8a51866f
RD
1068 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1069 IB_QP_QKEY),
1070 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1071 IB_QP_QKEY),
528e5a1b 1072 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
8a51866f
RD
1073 }
1074 },
1075 [IB_QPS_SQD] = {
1076 .valid = 1,
1077 .opt_param = {
1078 [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1079 [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1080 [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
b42b63cf
SH
1081 [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1082 [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
8a51866f
RD
1083 [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1084 [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1085 }
1086 },
1087 },
1088 [IB_QPS_SQD] = {
1089 [IB_QPS_RESET] = { .valid = 1 },
1090 [IB_QPS_ERR] = { .valid = 1 },
1091 [IB_QPS_RTS] = {
1092 .valid = 1,
1093 .opt_param = {
1094 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1095 IB_QP_QKEY),
1096 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1097 IB_QP_ALT_PATH |
1098 IB_QP_ACCESS_FLAGS |
1099 IB_QP_PATH_MIG_STATE),
1100 [IB_QPT_RC] = (IB_QP_CUR_STATE |
1101 IB_QP_ALT_PATH |
1102 IB_QP_ACCESS_FLAGS |
1103 IB_QP_MIN_RNR_TIMER |
1104 IB_QP_PATH_MIG_STATE),
b42b63cf
SH
1105 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
1106 IB_QP_ALT_PATH |
1107 IB_QP_ACCESS_FLAGS |
1108 IB_QP_PATH_MIG_STATE),
1109 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
1110 IB_QP_ALT_PATH |
1111 IB_QP_ACCESS_FLAGS |
1112 IB_QP_MIN_RNR_TIMER |
1113 IB_QP_PATH_MIG_STATE),
8a51866f
RD
1114 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1115 IB_QP_QKEY),
1116 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1117 IB_QP_QKEY),
1118 }
1119 },
1120 [IB_QPS_SQD] = {
1121 .valid = 1,
1122 .opt_param = {
1123 [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
1124 IB_QP_QKEY),
1125 [IB_QPT_UC] = (IB_QP_AV |
8a51866f
RD
1126 IB_QP_ALT_PATH |
1127 IB_QP_ACCESS_FLAGS |
1128 IB_QP_PKEY_INDEX |
1129 IB_QP_PATH_MIG_STATE),
1130 [IB_QPT_RC] = (IB_QP_PORT |
1131 IB_QP_AV |
1132 IB_QP_TIMEOUT |
1133 IB_QP_RETRY_CNT |
1134 IB_QP_RNR_RETRY |
1135 IB_QP_MAX_QP_RD_ATOMIC |
1136 IB_QP_MAX_DEST_RD_ATOMIC |
8a51866f
RD
1137 IB_QP_ALT_PATH |
1138 IB_QP_ACCESS_FLAGS |
1139 IB_QP_PKEY_INDEX |
1140 IB_QP_MIN_RNR_TIMER |
1141 IB_QP_PATH_MIG_STATE),
b42b63cf
SH
1142 [IB_QPT_XRC_INI] = (IB_QP_PORT |
1143 IB_QP_AV |
1144 IB_QP_TIMEOUT |
1145 IB_QP_RETRY_CNT |
1146 IB_QP_RNR_RETRY |
1147 IB_QP_MAX_QP_RD_ATOMIC |
1148 IB_QP_ALT_PATH |
1149 IB_QP_ACCESS_FLAGS |
1150 IB_QP_PKEY_INDEX |
1151 IB_QP_PATH_MIG_STATE),
1152 [IB_QPT_XRC_TGT] = (IB_QP_PORT |
1153 IB_QP_AV |
1154 IB_QP_TIMEOUT |
1155 IB_QP_MAX_DEST_RD_ATOMIC |
1156 IB_QP_ALT_PATH |
1157 IB_QP_ACCESS_FLAGS |
1158 IB_QP_PKEY_INDEX |
1159 IB_QP_MIN_RNR_TIMER |
1160 IB_QP_PATH_MIG_STATE),
8a51866f
RD
1161 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
1162 IB_QP_QKEY),
1163 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
1164 IB_QP_QKEY),
1165 }
1166 }
1167 },
1168 [IB_QPS_SQE] = {
1169 [IB_QPS_RESET] = { .valid = 1 },
1170 [IB_QPS_ERR] = { .valid = 1 },
1171 [IB_QPS_RTS] = {
1172 .valid = 1,
1173 .opt_param = {
1174 [IB_QPT_UD] = (IB_QP_CUR_STATE |
1175 IB_QP_QKEY),
1176 [IB_QPT_UC] = (IB_QP_CUR_STATE |
1177 IB_QP_ACCESS_FLAGS),
1178 [IB_QPT_SMI] = (IB_QP_CUR_STATE |
1179 IB_QP_QKEY),
1180 [IB_QPT_GSI] = (IB_QP_CUR_STATE |
1181 IB_QP_QKEY),
1182 }
1183 }
1184 },
1185 [IB_QPS_ERR] = {
1186 [IB_QPS_RESET] = { .valid = 1 },
1187 [IB_QPS_ERR] = { .valid = 1 }
1188 }
1189};
1190
1191int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
dd5f03be
MB
1192 enum ib_qp_type type, enum ib_qp_attr_mask mask,
1193 enum rdma_link_layer ll)
8a51866f
RD
1194{
1195 enum ib_qp_attr_mask req_param, opt_param;
1196
1197 if (cur_state < 0 || cur_state > IB_QPS_ERR ||
1198 next_state < 0 || next_state > IB_QPS_ERR)
1199 return 0;
1200
1201 if (mask & IB_QP_CUR_STATE &&
1202 cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1203 cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1204 return 0;
1205
1206 if (!qp_state_table[cur_state][next_state].valid)
1207 return 0;
1208
1209 req_param = qp_state_table[cur_state][next_state].req_param[type];
1210 opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1211
1212 if ((mask & req_param) != req_param)
1213 return 0;
1214
1215 if (mask & ~(req_param | opt_param | IB_QP_STATE))
1216 return 0;
1217
1218 return 1;
1219}
1220EXPORT_SYMBOL(ib_modify_qp_is_ok);
1221
c90ea9d8 1222int ib_resolve_eth_dmac(struct ib_device *device,
90898850 1223 struct rdma_ah_attr *ah_attr)
ed4c54e5
OG
1224{
1225 int ret = 0;
d8966fcd 1226 struct ib_global_route *grh;
ed4c54e5 1227
d8966fcd 1228 if (!rdma_is_port_valid(device, rdma_ah_get_port_num(ah_attr)))
c90ea9d8 1229 return -EINVAL;
dbf727de 1230
44c58487 1231 if (ah_attr->type != RDMA_AH_ATTR_TYPE_ROCE)
c90ea9d8 1232 return 0;
dbf727de 1233
d8966fcd
DC
1234 grh = rdma_ah_retrieve_grh(ah_attr);
1235
1236 if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw)) {
1237 rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
44c58487 1238 ah_attr->roce.dmac);
9636a56f
NO
1239 return 0;
1240 }
1241 if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1242 if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1243 __be32 addr = 0;
1244
1245 memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
1246 ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
1247 } else {
1248 ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
1249 (char *)ah_attr->roce.dmac);
1250 }
c90ea9d8
MS
1251 } else {
1252 union ib_gid sgid;
1253 struct ib_gid_attr sgid_attr;
1254 int ifindex;
1255 int hop_limit;
1256
1257 ret = ib_query_gid(device,
d8966fcd
DC
1258 rdma_ah_get_port_num(ah_attr),
1259 grh->sgid_index,
c90ea9d8
MS
1260 &sgid, &sgid_attr);
1261
1262 if (ret || !sgid_attr.ndev) {
1263 if (!ret)
1264 ret = -ENXIO;
1265 goto out;
1266 }
dbf727de 1267
c90ea9d8 1268 ifindex = sgid_attr.ndev->ifindex;
c3efe750 1269
d8966fcd
DC
1270 ret =
1271 rdma_addr_find_l2_eth_by_grh(&sgid, &grh->dgid,
44c58487 1272 ah_attr->roce.dmac,
d8966fcd 1273 NULL, &ifindex, &hop_limit);
c90ea9d8
MS
1274
1275 dev_put(sgid_attr.ndev);
1276
d8966fcd 1277 grh->hop_limit = hop_limit;
ed4c54e5
OG
1278 }
1279out:
1280 return ret;
1281}
dbf727de 1282EXPORT_SYMBOL(ib_resolve_eth_dmac);
ed4c54e5 1283
a512c2fb
PP
1284/**
1285 * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
1286 * @qp: The QP to modify.
1287 * @attr: On input, specifies the QP attributes to modify. On output,
1288 * the current values of selected QP attributes are returned.
1289 * @attr_mask: A bit-mask used to specify which attributes of the QP
1290 * are being modified.
1291 * @udata: pointer to user's input output buffer information
1292 * are being modified.
1293 * It returns 0 on success and returns appropriate error code on error.
1294 */
1295int ib_modify_qp_with_udata(struct ib_qp *qp, struct ib_qp_attr *attr,
1296 int attr_mask, struct ib_udata *udata)
1da177e4 1297{
a512c2fb 1298 int ret;
ed4c54e5 1299
a512c2fb
PP
1300 if (attr_mask & IB_QP_AV) {
1301 ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr);
c90ea9d8
MS
1302 if (ret)
1303 return ret;
1304 }
498ca3c8
NO
1305 ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
1306 if (!ret && (attr_mask & IB_QP_PORT))
1307 qp->port = attr->port_num;
1308
1309 return ret;
a512c2fb
PP
1310}
1311EXPORT_SYMBOL(ib_modify_qp_with_udata);
ed4c54e5 1312
d4186194
YS
1313int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width)
1314{
1315 int rc;
1316 u32 netdev_speed;
1317 struct net_device *netdev;
1318 struct ethtool_link_ksettings lksettings;
1319
1320 if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
1321 return -EINVAL;
1322
1323 if (!dev->get_netdev)
1324 return -EOPNOTSUPP;
1325
1326 netdev = dev->get_netdev(dev, port_num);
1327 if (!netdev)
1328 return -ENODEV;
1329
1330 rtnl_lock();
1331 rc = __ethtool_get_link_ksettings(netdev, &lksettings);
1332 rtnl_unlock();
1333
1334 dev_put(netdev);
1335
1336 if (!rc) {
1337 netdev_speed = lksettings.base.speed;
1338 } else {
1339 netdev_speed = SPEED_1000;
1340 pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name,
1341 netdev_speed);
1342 }
1343
1344 if (netdev_speed <= SPEED_1000) {
1345 *width = IB_WIDTH_1X;
1346 *speed = IB_SPEED_SDR;
1347 } else if (netdev_speed <= SPEED_10000) {
1348 *width = IB_WIDTH_1X;
1349 *speed = IB_SPEED_FDR10;
1350 } else if (netdev_speed <= SPEED_20000) {
1351 *width = IB_WIDTH_4X;
1352 *speed = IB_SPEED_DDR;
1353 } else if (netdev_speed <= SPEED_25000) {
1354 *width = IB_WIDTH_1X;
1355 *speed = IB_SPEED_EDR;
1356 } else if (netdev_speed <= SPEED_40000) {
1357 *width = IB_WIDTH_4X;
1358 *speed = IB_SPEED_FDR10;
1359 } else {
1360 *width = IB_WIDTH_4X;
1361 *speed = IB_SPEED_EDR;
1362 }
1363
1364 return 0;
1365}
1366EXPORT_SYMBOL(ib_get_eth_speed);
1367
a512c2fb
PP
1368int ib_modify_qp(struct ib_qp *qp,
1369 struct ib_qp_attr *qp_attr,
1370 int qp_attr_mask)
1371{
1372 return ib_modify_qp_with_udata(qp, qp_attr, qp_attr_mask, NULL);
1da177e4
LT
1373}
1374EXPORT_SYMBOL(ib_modify_qp);
1375
1376int ib_query_qp(struct ib_qp *qp,
1377 struct ib_qp_attr *qp_attr,
1378 int qp_attr_mask,
1379 struct ib_qp_init_attr *qp_init_attr)
1380{
1381 return qp->device->query_qp ?
0e0ec7e0 1382 qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
1da177e4
LT
1383 -ENOSYS;
1384}
1385EXPORT_SYMBOL(ib_query_qp);
1386
0e0ec7e0
SH
1387int ib_close_qp(struct ib_qp *qp)
1388{
1389 struct ib_qp *real_qp;
1390 unsigned long flags;
1391
1392 real_qp = qp->real_qp;
1393 if (real_qp == qp)
1394 return -EINVAL;
1395
1396 spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1397 list_del(&qp->open_list);
1398 spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1399
1400 atomic_dec(&real_qp->usecnt);
d291f1a6 1401 ib_close_shared_qp_security(qp->qp_sec);
0e0ec7e0
SH
1402 kfree(qp);
1403
1404 return 0;
1405}
1406EXPORT_SYMBOL(ib_close_qp);
1407
1408static int __ib_destroy_shared_qp(struct ib_qp *qp)
1409{
1410 struct ib_xrcd *xrcd;
1411 struct ib_qp *real_qp;
1412 int ret;
1413
1414 real_qp = qp->real_qp;
1415 xrcd = real_qp->xrcd;
1416
1417 mutex_lock(&xrcd->tgt_qp_mutex);
1418 ib_close_qp(qp);
1419 if (atomic_read(&real_qp->usecnt) == 0)
1420 list_del(&real_qp->xrcd_list);
1421 else
1422 real_qp = NULL;
1423 mutex_unlock(&xrcd->tgt_qp_mutex);
1424
1425 if (real_qp) {
1426 ret = ib_destroy_qp(real_qp);
1427 if (!ret)
1428 atomic_dec(&xrcd->usecnt);
1429 else
1430 __ib_insert_xrcd_qp(xrcd, real_qp);
1431 }
1432
1433 return 0;
1434}
1435
1da177e4
LT
1436int ib_destroy_qp(struct ib_qp *qp)
1437{
1438 struct ib_pd *pd;
1439 struct ib_cq *scq, *rcq;
1440 struct ib_srq *srq;
a9017e23 1441 struct ib_rwq_ind_table *ind_tbl;
d291f1a6 1442 struct ib_qp_security *sec;
1da177e4
LT
1443 int ret;
1444
fffb0383
CH
1445 WARN_ON_ONCE(qp->mrs_used > 0);
1446
0e0ec7e0
SH
1447 if (atomic_read(&qp->usecnt))
1448 return -EBUSY;
1449
1450 if (qp->real_qp != qp)
1451 return __ib_destroy_shared_qp(qp);
1452
b42b63cf
SH
1453 pd = qp->pd;
1454 scq = qp->send_cq;
1455 rcq = qp->recv_cq;
1456 srq = qp->srq;
a9017e23 1457 ind_tbl = qp->rwq_ind_tbl;
d291f1a6
DJ
1458 sec = qp->qp_sec;
1459 if (sec)
1460 ib_destroy_qp_security_begin(sec);
1da177e4 1461
a060b562
CH
1462 if (!qp->uobject)
1463 rdma_rw_cleanup_mrs(qp);
1464
1da177e4
LT
1465 ret = qp->device->destroy_qp(qp);
1466 if (!ret) {
b42b63cf
SH
1467 if (pd)
1468 atomic_dec(&pd->usecnt);
1469 if (scq)
1470 atomic_dec(&scq->usecnt);
1471 if (rcq)
1472 atomic_dec(&rcq->usecnt);
1da177e4
LT
1473 if (srq)
1474 atomic_dec(&srq->usecnt);
a9017e23
YH
1475 if (ind_tbl)
1476 atomic_dec(&ind_tbl->usecnt);
d291f1a6
DJ
1477 if (sec)
1478 ib_destroy_qp_security_end(sec);
1479 } else {
1480 if (sec)
1481 ib_destroy_qp_security_abort(sec);
1da177e4
LT
1482 }
1483
1484 return ret;
1485}
1486EXPORT_SYMBOL(ib_destroy_qp);
1487
1488/* Completion queues */
1489
1490struct ib_cq *ib_create_cq(struct ib_device *device,
1491 ib_comp_handler comp_handler,
1492 void (*event_handler)(struct ib_event *, void *),
8e37210b
MB
1493 void *cq_context,
1494 const struct ib_cq_init_attr *cq_attr)
1da177e4
LT
1495{
1496 struct ib_cq *cq;
1497
8e37210b 1498 cq = device->create_cq(device, cq_attr, NULL, NULL);
1da177e4
LT
1499
1500 if (!IS_ERR(cq)) {
1501 cq->device = device;
b5e81bf5 1502 cq->uobject = NULL;
1da177e4
LT
1503 cq->comp_handler = comp_handler;
1504 cq->event_handler = event_handler;
1505 cq->cq_context = cq_context;
1506 atomic_set(&cq->usecnt, 0);
1507 }
1508
1509 return cq;
1510}
1511EXPORT_SYMBOL(ib_create_cq);
1512
2dd57162
EC
1513int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1514{
1515 return cq->device->modify_cq ?
1516 cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
1517}
1518EXPORT_SYMBOL(ib_modify_cq);
1519
1da177e4
LT
1520int ib_destroy_cq(struct ib_cq *cq)
1521{
1522 if (atomic_read(&cq->usecnt))
1523 return -EBUSY;
1524
1525 return cq->device->destroy_cq(cq);
1526}
1527EXPORT_SYMBOL(ib_destroy_cq);
1528
a74cd4af 1529int ib_resize_cq(struct ib_cq *cq, int cqe)
1da177e4 1530{
40de2e54 1531 return cq->device->resize_cq ?
33b9b3ee 1532 cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
1da177e4
LT
1533}
1534EXPORT_SYMBOL(ib_resize_cq);
1535
1536/* Memory regions */
1537
1da177e4
LT
1538int ib_dereg_mr(struct ib_mr *mr)
1539{
ab67ed8d 1540 struct ib_pd *pd = mr->pd;
1da177e4
LT
1541 int ret;
1542
1da177e4
LT
1543 ret = mr->device->dereg_mr(mr);
1544 if (!ret)
1545 atomic_dec(&pd->usecnt);
1546
1547 return ret;
1548}
1549EXPORT_SYMBOL(ib_dereg_mr);
1550
9bee178b
SG
1551/**
1552 * ib_alloc_mr() - Allocates a memory region
1553 * @pd: protection domain associated with the region
1554 * @mr_type: memory region type
1555 * @max_num_sg: maximum sg entries available for registration.
1556 *
1557 * Notes:
1558 * Memory registeration page/sg lists must not exceed max_num_sg.
1559 * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
1560 * max_num_sg * used_page_size.
1561 *
1562 */
1563struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
1564 enum ib_mr_type mr_type,
1565 u32 max_num_sg)
00f7ec36
SW
1566{
1567 struct ib_mr *mr;
1568
d9f272c5 1569 if (!pd->device->alloc_mr)
00f7ec36
SW
1570 return ERR_PTR(-ENOSYS);
1571
d9f272c5 1572 mr = pd->device->alloc_mr(pd, mr_type, max_num_sg);
00f7ec36
SW
1573 if (!IS_ERR(mr)) {
1574 mr->device = pd->device;
1575 mr->pd = pd;
1576 mr->uobject = NULL;
1577 atomic_inc(&pd->usecnt);
d4a85c30 1578 mr->need_inval = false;
00f7ec36
SW
1579 }
1580
1581 return mr;
1582}
d9f272c5 1583EXPORT_SYMBOL(ib_alloc_mr);
00f7ec36 1584
1da177e4
LT
1585/* "Fast" memory regions */
1586
1587struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
1588 int mr_access_flags,
1589 struct ib_fmr_attr *fmr_attr)
1590{
1591 struct ib_fmr *fmr;
1592
1593 if (!pd->device->alloc_fmr)
1594 return ERR_PTR(-ENOSYS);
1595
1596 fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
1597 if (!IS_ERR(fmr)) {
1598 fmr->device = pd->device;
1599 fmr->pd = pd;
1600 atomic_inc(&pd->usecnt);
1601 }
1602
1603 return fmr;
1604}
1605EXPORT_SYMBOL(ib_alloc_fmr);
1606
1607int ib_unmap_fmr(struct list_head *fmr_list)
1608{
1609 struct ib_fmr *fmr;
1610
1611 if (list_empty(fmr_list))
1612 return 0;
1613
1614 fmr = list_entry(fmr_list->next, struct ib_fmr, list);
1615 return fmr->device->unmap_fmr(fmr_list);
1616}
1617EXPORT_SYMBOL(ib_unmap_fmr);
1618
1619int ib_dealloc_fmr(struct ib_fmr *fmr)
1620{
1621 struct ib_pd *pd;
1622 int ret;
1623
1624 pd = fmr->pd;
1625 ret = fmr->device->dealloc_fmr(fmr);
1626 if (!ret)
1627 atomic_dec(&pd->usecnt);
1628
1629 return ret;
1630}
1631EXPORT_SYMBOL(ib_dealloc_fmr);
1632
1633/* Multicast groups */
1634
52363335
NO
1635static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
1636{
1637 struct ib_qp_init_attr init_attr = {};
1638 struct ib_qp_attr attr = {};
1639 int num_eth_ports = 0;
1640 int port;
1641
1642 /* If QP state >= init, it is assigned to a port and we can check this
1643 * port only.
1644 */
1645 if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
1646 if (attr.qp_state >= IB_QPS_INIT) {
1647 if (qp->device->get_link_layer(qp->device, attr.port_num) !=
1648 IB_LINK_LAYER_INFINIBAND)
1649 return true;
1650 goto lid_check;
1651 }
1652 }
1653
1654 /* Can't get a quick answer, iterate over all ports */
1655 for (port = 0; port < qp->device->phys_port_cnt; port++)
1656 if (qp->device->get_link_layer(qp->device, port) !=
1657 IB_LINK_LAYER_INFINIBAND)
1658 num_eth_ports++;
1659
1660 /* If we have at lease one Ethernet port, RoCE annex declares that
1661 * multicast LID should be ignored. We can't tell at this step if the
1662 * QP belongs to an IB or Ethernet port.
1663 */
1664 if (num_eth_ports)
1665 return true;
1666
1667 /* If all the ports are IB, we can check according to IB spec. */
1668lid_check:
1669 return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
1670 lid == be16_to_cpu(IB_LID_PERMISSIVE));
1671}
1672
1da177e4
LT
1673int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1674{
c3bccbfb
OG
1675 int ret;
1676
0c33aeed
JM
1677 if (!qp->device->attach_mcast)
1678 return -ENOSYS;
be1d325a
NO
1679
1680 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
1681 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
0c33aeed
JM
1682 return -EINVAL;
1683
c3bccbfb
OG
1684 ret = qp->device->attach_mcast(qp, gid, lid);
1685 if (!ret)
1686 atomic_inc(&qp->usecnt);
1687 return ret;
1da177e4
LT
1688}
1689EXPORT_SYMBOL(ib_attach_mcast);
1690
1691int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1692{
c3bccbfb
OG
1693 int ret;
1694
0c33aeed
JM
1695 if (!qp->device->detach_mcast)
1696 return -ENOSYS;
be1d325a
NO
1697
1698 if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
1699 qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
0c33aeed
JM
1700 return -EINVAL;
1701
c3bccbfb
OG
1702 ret = qp->device->detach_mcast(qp, gid, lid);
1703 if (!ret)
1704 atomic_dec(&qp->usecnt);
1705 return ret;
1da177e4
LT
1706}
1707EXPORT_SYMBOL(ib_detach_mcast);
59991f94
SH
1708
1709struct ib_xrcd *ib_alloc_xrcd(struct ib_device *device)
1710{
1711 struct ib_xrcd *xrcd;
1712
1713 if (!device->alloc_xrcd)
1714 return ERR_PTR(-ENOSYS);
1715
1716 xrcd = device->alloc_xrcd(device, NULL, NULL);
1717 if (!IS_ERR(xrcd)) {
1718 xrcd->device = device;
53d0bd1e 1719 xrcd->inode = NULL;
59991f94 1720 atomic_set(&xrcd->usecnt, 0);
d3d72d90
SH
1721 mutex_init(&xrcd->tgt_qp_mutex);
1722 INIT_LIST_HEAD(&xrcd->tgt_qp_list);
59991f94
SH
1723 }
1724
1725 return xrcd;
1726}
1727EXPORT_SYMBOL(ib_alloc_xrcd);
1728
1729int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1730{
d3d72d90
SH
1731 struct ib_qp *qp;
1732 int ret;
1733
59991f94
SH
1734 if (atomic_read(&xrcd->usecnt))
1735 return -EBUSY;
1736
d3d72d90
SH
1737 while (!list_empty(&xrcd->tgt_qp_list)) {
1738 qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
1739 ret = ib_destroy_qp(qp);
1740 if (ret)
1741 return ret;
1742 }
1743
59991f94
SH
1744 return xrcd->device->dealloc_xrcd(xrcd);
1745}
1746EXPORT_SYMBOL(ib_dealloc_xrcd);
319a441d 1747
5fd251c8
YH
1748/**
1749 * ib_create_wq - Creates a WQ associated with the specified protection
1750 * domain.
1751 * @pd: The protection domain associated with the WQ.
1752 * @wq_init_attr: A list of initial attributes required to create the
1753 * WQ. If WQ creation succeeds, then the attributes are updated to
1754 * the actual capabilities of the created WQ.
1755 *
1756 * wq_init_attr->max_wr and wq_init_attr->max_sge determine
1757 * the requested size of the WQ, and set to the actual values allocated
1758 * on return.
1759 * If ib_create_wq() succeeds, then max_wr and max_sge will always be
1760 * at least as large as the requested values.
1761 */
1762struct ib_wq *ib_create_wq(struct ib_pd *pd,
1763 struct ib_wq_init_attr *wq_attr)
1764{
1765 struct ib_wq *wq;
1766
1767 if (!pd->device->create_wq)
1768 return ERR_PTR(-ENOSYS);
1769
1770 wq = pd->device->create_wq(pd, wq_attr, NULL);
1771 if (!IS_ERR(wq)) {
1772 wq->event_handler = wq_attr->event_handler;
1773 wq->wq_context = wq_attr->wq_context;
1774 wq->wq_type = wq_attr->wq_type;
1775 wq->cq = wq_attr->cq;
1776 wq->device = pd->device;
1777 wq->pd = pd;
1778 wq->uobject = NULL;
1779 atomic_inc(&pd->usecnt);
1780 atomic_inc(&wq_attr->cq->usecnt);
1781 atomic_set(&wq->usecnt, 0);
1782 }
1783 return wq;
1784}
1785EXPORT_SYMBOL(ib_create_wq);
1786
1787/**
1788 * ib_destroy_wq - Destroys the specified WQ.
1789 * @wq: The WQ to destroy.
1790 */
1791int ib_destroy_wq(struct ib_wq *wq)
1792{
1793 int err;
1794 struct ib_cq *cq = wq->cq;
1795 struct ib_pd *pd = wq->pd;
1796
1797 if (atomic_read(&wq->usecnt))
1798 return -EBUSY;
1799
1800 err = wq->device->destroy_wq(wq);
1801 if (!err) {
1802 atomic_dec(&pd->usecnt);
1803 atomic_dec(&cq->usecnt);
1804 }
1805 return err;
1806}
1807EXPORT_SYMBOL(ib_destroy_wq);
1808
1809/**
1810 * ib_modify_wq - Modifies the specified WQ.
1811 * @wq: The WQ to modify.
1812 * @wq_attr: On input, specifies the WQ attributes to modify.
1813 * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
1814 * are being modified.
1815 * On output, the current values of selected WQ attributes are returned.
1816 */
1817int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
1818 u32 wq_attr_mask)
1819{
1820 int err;
1821
1822 if (!wq->device->modify_wq)
1823 return -ENOSYS;
1824
1825 err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
1826 return err;
1827}
1828EXPORT_SYMBOL(ib_modify_wq);
1829
6d39786b
YH
1830/*
1831 * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
1832 * @device: The device on which to create the rwq indirection table.
1833 * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
1834 * create the Indirection Table.
1835 *
1836 * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
1837 * than the created ib_rwq_ind_table object and the caller is responsible
1838 * for its memory allocation/free.
1839 */
1840struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
1841 struct ib_rwq_ind_table_init_attr *init_attr)
1842{
1843 struct ib_rwq_ind_table *rwq_ind_table;
1844 int i;
1845 u32 table_size;
1846
1847 if (!device->create_rwq_ind_table)
1848 return ERR_PTR(-ENOSYS);
1849
1850 table_size = (1 << init_attr->log_ind_tbl_size);
1851 rwq_ind_table = device->create_rwq_ind_table(device,
1852 init_attr, NULL);
1853 if (IS_ERR(rwq_ind_table))
1854 return rwq_ind_table;
1855
1856 rwq_ind_table->ind_tbl = init_attr->ind_tbl;
1857 rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
1858 rwq_ind_table->device = device;
1859 rwq_ind_table->uobject = NULL;
1860 atomic_set(&rwq_ind_table->usecnt, 0);
1861
1862 for (i = 0; i < table_size; i++)
1863 atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
1864
1865 return rwq_ind_table;
1866}
1867EXPORT_SYMBOL(ib_create_rwq_ind_table);
1868
1869/*
1870 * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
1871 * @wq_ind_table: The Indirection Table to destroy.
1872*/
1873int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
1874{
1875 int err, i;
1876 u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
1877 struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
1878
1879 if (atomic_read(&rwq_ind_table->usecnt))
1880 return -EBUSY;
1881
1882 err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
1883 if (!err) {
1884 for (i = 0; i < table_size; i++)
1885 atomic_dec(&ind_tbl[i]->usecnt);
1886 }
1887
1888 return err;
1889}
1890EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
1891
319a441d
HHZ
1892struct ib_flow *ib_create_flow(struct ib_qp *qp,
1893 struct ib_flow_attr *flow_attr,
1894 int domain)
1895{
1896 struct ib_flow *flow_id;
1897 if (!qp->device->create_flow)
1898 return ERR_PTR(-ENOSYS);
1899
1900 flow_id = qp->device->create_flow(qp, flow_attr, domain);
8ecc7985 1901 if (!IS_ERR(flow_id)) {
319a441d 1902 atomic_inc(&qp->usecnt);
8ecc7985
MB
1903 flow_id->qp = qp;
1904 }
319a441d
HHZ
1905 return flow_id;
1906}
1907EXPORT_SYMBOL(ib_create_flow);
1908
1909int ib_destroy_flow(struct ib_flow *flow_id)
1910{
1911 int err;
1912 struct ib_qp *qp = flow_id->qp;
1913
1914 err = qp->device->destroy_flow(flow_id);
1915 if (!err)
1916 atomic_dec(&qp->usecnt);
1917 return err;
1918}
1919EXPORT_SYMBOL(ib_destroy_flow);
1b01d335
SG
1920
1921int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
1922 struct ib_mr_status *mr_status)
1923{
1924 return mr->device->check_mr_status ?
1925 mr->device->check_mr_status(mr, check_mask, mr_status) : -ENOSYS;
1926}
1927EXPORT_SYMBOL(ib_check_mr_status);
4c67e2bf 1928
50174a7f
EC
1929int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
1930 int state)
1931{
1932 if (!device->set_vf_link_state)
1933 return -ENOSYS;
1934
1935 return device->set_vf_link_state(device, vf, port, state);
1936}
1937EXPORT_SYMBOL(ib_set_vf_link_state);
1938
1939int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
1940 struct ifla_vf_info *info)
1941{
1942 if (!device->get_vf_config)
1943 return -ENOSYS;
1944
1945 return device->get_vf_config(device, vf, port, info);
1946}
1947EXPORT_SYMBOL(ib_get_vf_config);
1948
1949int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
1950 struct ifla_vf_stats *stats)
1951{
1952 if (!device->get_vf_stats)
1953 return -ENOSYS;
1954
1955 return device->get_vf_stats(device, vf, port, stats);
1956}
1957EXPORT_SYMBOL(ib_get_vf_stats);
1958
1959int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
1960 int type)
1961{
1962 if (!device->set_vf_guid)
1963 return -ENOSYS;
1964
1965 return device->set_vf_guid(device, vf, port, guid, type);
1966}
1967EXPORT_SYMBOL(ib_set_vf_guid);
1968
4c67e2bf
SG
1969/**
1970 * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
1971 * and set it the memory region.
1972 * @mr: memory region
1973 * @sg: dma mapped scatterlist
1974 * @sg_nents: number of entries in sg
ff2ba993 1975 * @sg_offset: offset in bytes into sg
4c67e2bf
SG
1976 * @page_size: page vector desired page size
1977 *
1978 * Constraints:
1979 * - The first sg element is allowed to have an offset.
52746129
BVA
1980 * - Each sg element must either be aligned to page_size or virtually
1981 * contiguous to the previous element. In case an sg element has a
1982 * non-contiguous offset, the mapping prefix will not include it.
4c67e2bf
SG
1983 * - The last sg element is allowed to have length less than page_size.
1984 * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
1985 * then only max_num_sg entries will be mapped.
52746129 1986 * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
f5aa9159 1987 * constraints holds and the page_size argument is ignored.
4c67e2bf
SG
1988 *
1989 * Returns the number of sg elements that were mapped to the memory region.
1990 *
1991 * After this completes successfully, the memory region
1992 * is ready for registration.
1993 */
ff2ba993 1994int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
9aa8b321 1995 unsigned int *sg_offset, unsigned int page_size)
4c67e2bf
SG
1996{
1997 if (unlikely(!mr->device->map_mr_sg))
1998 return -ENOSYS;
1999
2000 mr->page_size = page_size;
2001
ff2ba993 2002 return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
4c67e2bf
SG
2003}
2004EXPORT_SYMBOL(ib_map_mr_sg);
2005
2006/**
2007 * ib_sg_to_pages() - Convert the largest prefix of a sg list
2008 * to a page vector
2009 * @mr: memory region
2010 * @sgl: dma mapped scatterlist
2011 * @sg_nents: number of entries in sg
9aa8b321
BVA
2012 * @sg_offset_p: IN: start offset in bytes into sg
2013 * OUT: offset in bytes for element n of the sg of the first
2014 * byte that has not been processed where n is the return
2015 * value of this function.
4c67e2bf
SG
2016 * @set_page: driver page assignment function pointer
2017 *
8f5ba10e 2018 * Core service helper for drivers to convert the largest
4c67e2bf
SG
2019 * prefix of given sg list to a page vector. The sg list
2020 * prefix converted is the prefix that meet the requirements
2021 * of ib_map_mr_sg.
2022 *
2023 * Returns the number of sg elements that were assigned to
2024 * a page vector.
2025 */
ff2ba993 2026int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
9aa8b321 2027 unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
4c67e2bf
SG
2028{
2029 struct scatterlist *sg;
b6aeb980 2030 u64 last_end_dma_addr = 0;
9aa8b321 2031 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
4c67e2bf
SG
2032 unsigned int last_page_off = 0;
2033 u64 page_mask = ~((u64)mr->page_size - 1);
8f5ba10e 2034 int i, ret;
4c67e2bf 2035
9aa8b321
BVA
2036 if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
2037 return -EINVAL;
2038
ff2ba993 2039 mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
4c67e2bf
SG
2040 mr->length = 0;
2041
2042 for_each_sg(sgl, sg, sg_nents, i) {
ff2ba993 2043 u64 dma_addr = sg_dma_address(sg) + sg_offset;
9aa8b321 2044 u64 prev_addr = dma_addr;
ff2ba993 2045 unsigned int dma_len = sg_dma_len(sg) - sg_offset;
4c67e2bf
SG
2046 u64 end_dma_addr = dma_addr + dma_len;
2047 u64 page_addr = dma_addr & page_mask;
2048
8f5ba10e
BVA
2049 /*
2050 * For the second and later elements, check whether either the
2051 * end of element i-1 or the start of element i is not aligned
2052 * on a page boundary.
2053 */
2054 if (i && (last_page_off != 0 || page_addr != dma_addr)) {
2055 /* Stop mapping if there is a gap. */
2056 if (last_end_dma_addr != dma_addr)
2057 break;
2058
2059 /*
2060 * Coalesce this element with the last. If it is small
2061 * enough just update mr->length. Otherwise start
2062 * mapping from the next page.
2063 */
2064 goto next_page;
4c67e2bf
SG
2065 }
2066
2067 do {
8f5ba10e 2068 ret = set_page(mr, page_addr);
9aa8b321
BVA
2069 if (unlikely(ret < 0)) {
2070 sg_offset = prev_addr - sg_dma_address(sg);
2071 mr->length += prev_addr - dma_addr;
2072 if (sg_offset_p)
2073 *sg_offset_p = sg_offset;
2074 return i || sg_offset ? i : ret;
2075 }
2076 prev_addr = page_addr;
8f5ba10e 2077next_page:
4c67e2bf
SG
2078 page_addr += mr->page_size;
2079 } while (page_addr < end_dma_addr);
2080
2081 mr->length += dma_len;
2082 last_end_dma_addr = end_dma_addr;
4c67e2bf 2083 last_page_off = end_dma_addr & ~page_mask;
ff2ba993
CH
2084
2085 sg_offset = 0;
4c67e2bf
SG
2086 }
2087
9aa8b321
BVA
2088 if (sg_offset_p)
2089 *sg_offset_p = 0;
4c67e2bf
SG
2090 return i;
2091}
2092EXPORT_SYMBOL(ib_sg_to_pages);
765d6774
SW
2093
2094struct ib_drain_cqe {
2095 struct ib_cqe cqe;
2096 struct completion done;
2097};
2098
2099static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
2100{
2101 struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
2102 cqe);
2103
2104 complete(&cqe->done);
2105}
2106
2107/*
2108 * Post a WR and block until its completion is reaped for the SQ.
2109 */
2110static void __ib_drain_sq(struct ib_qp *qp)
2111{
f039f44f 2112 struct ib_cq *cq = qp->send_cq;
765d6774
SW
2113 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2114 struct ib_drain_cqe sdrain;
2115 struct ib_send_wr swr = {}, *bad_swr;
2116 int ret;
2117
765d6774
SW
2118 swr.wr_cqe = &sdrain.cqe;
2119 sdrain.cqe.done = ib_drain_qp_done;
2120 init_completion(&sdrain.done);
2121
2122 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2123 if (ret) {
2124 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2125 return;
2126 }
2127
2128 ret = ib_post_send(qp, &swr, &bad_swr);
2129 if (ret) {
2130 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2131 return;
2132 }
2133
f039f44f
BVA
2134 if (cq->poll_ctx == IB_POLL_DIRECT)
2135 while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
2136 ib_process_cq_direct(cq, -1);
2137 else
2138 wait_for_completion(&sdrain.done);
765d6774
SW
2139}
2140
2141/*
2142 * Post a WR and block until its completion is reaped for the RQ.
2143 */
2144static void __ib_drain_rq(struct ib_qp *qp)
2145{
f039f44f 2146 struct ib_cq *cq = qp->recv_cq;
765d6774
SW
2147 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2148 struct ib_drain_cqe rdrain;
2149 struct ib_recv_wr rwr = {}, *bad_rwr;
2150 int ret;
2151
765d6774
SW
2152 rwr.wr_cqe = &rdrain.cqe;
2153 rdrain.cqe.done = ib_drain_qp_done;
2154 init_completion(&rdrain.done);
2155
2156 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2157 if (ret) {
2158 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2159 return;
2160 }
2161
2162 ret = ib_post_recv(qp, &rwr, &bad_rwr);
2163 if (ret) {
2164 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2165 return;
2166 }
2167
f039f44f
BVA
2168 if (cq->poll_ctx == IB_POLL_DIRECT)
2169 while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
2170 ib_process_cq_direct(cq, -1);
2171 else
2172 wait_for_completion(&rdrain.done);
765d6774
SW
2173}
2174
2175/**
2176 * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2177 * application.
2178 * @qp: queue pair to drain
2179 *
2180 * If the device has a provider-specific drain function, then
2181 * call that. Otherwise call the generic drain function
2182 * __ib_drain_sq().
2183 *
2184 * The caller must:
2185 *
2186 * ensure there is room in the CQ and SQ for the drain work request and
2187 * completion.
2188 *
f039f44f 2189 * allocate the CQ using ib_alloc_cq().
765d6774
SW
2190 *
2191 * ensure that there are no other contexts that are posting WRs concurrently.
2192 * Otherwise the drain is not guaranteed.
2193 */
2194void ib_drain_sq(struct ib_qp *qp)
2195{
2196 if (qp->device->drain_sq)
2197 qp->device->drain_sq(qp);
2198 else
2199 __ib_drain_sq(qp);
2200}
2201EXPORT_SYMBOL(ib_drain_sq);
2202
2203/**
2204 * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2205 * application.
2206 * @qp: queue pair to drain
2207 *
2208 * If the device has a provider-specific drain function, then
2209 * call that. Otherwise call the generic drain function
2210 * __ib_drain_rq().
2211 *
2212 * The caller must:
2213 *
2214 * ensure there is room in the CQ and RQ for the drain work request and
2215 * completion.
2216 *
f039f44f 2217 * allocate the CQ using ib_alloc_cq().
765d6774
SW
2218 *
2219 * ensure that there are no other contexts that are posting WRs concurrently.
2220 * Otherwise the drain is not guaranteed.
2221 */
2222void ib_drain_rq(struct ib_qp *qp)
2223{
2224 if (qp->device->drain_rq)
2225 qp->device->drain_rq(qp);
2226 else
2227 __ib_drain_rq(qp);
2228}
2229EXPORT_SYMBOL(ib_drain_rq);
2230
2231/**
2232 * ib_drain_qp() - Block until all CQEs have been consumed by the
2233 * application on both the RQ and SQ.
2234 * @qp: queue pair to drain
2235 *
2236 * The caller must:
2237 *
2238 * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2239 * and completions.
2240 *
f039f44f 2241 * allocate the CQs using ib_alloc_cq().
765d6774
SW
2242 *
2243 * ensure that there are no other contexts that are posting WRs concurrently.
2244 * Otherwise the drain is not guaranteed.
2245 */
2246void ib_drain_qp(struct ib_qp *qp)
2247{
2248 ib_drain_sq(qp);
42235f80
SG
2249 if (!qp->srq)
2250 ib_drain_rq(qp);
765d6774
SW
2251}
2252EXPORT_SYMBOL(ib_drain_qp);