xref: /freebsd-13-stable/sys/ofed/drivers/infiniband/core/ib_verbs.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
5  * Copyright (c) 2004 Infinicon Corporation.  All rights reserved.
6  * Copyright (c) 2004 Intel Corporation.  All rights reserved.
7  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
8  * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
9  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
10  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
11  *
12  * This software is available to you under a choice of one of two
13  * licenses.  You may choose to be licensed under the terms of the GNU
14  * General Public License (GPL) Version 2, available from the file
15  * COPYING in the main directory of this source tree, or the
16  * OpenIB.org BSD license below:
17  *
18  *     Redistribution and use in source and binary forms, with or
19  *     without modification, are permitted provided that the following
20  *     conditions are met:
21  *
22  *      - Redistributions of source code must retain the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer.
25  *
26  *      - Redistributions in binary form must reproduce the above
27  *        copyright notice, this list of conditions and the following
28  *        disclaimer in the documentation and/or other materials
29  *        provided with the distribution.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
35  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
36  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
37  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40 
41 #include <sys/cdefs.h>
42 #include <linux/errno.h>
43 #include <linux/err.h>
44 #include <linux/string.h>
45 #include <linux/slab.h>
46 #include <linux/in.h>
47 #include <linux/in6.h>
48 
49 #include <rdma/ib_verbs.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/ib_addr.h>
52 
53 #include <netinet/ip.h>
54 #include <netinet/ip6.h>
55 
56 #include <machine/in_cksum.h>
57 
58 #include "core_priv.h"
59 
60 static const char * const ib_events[] = {
61 	[IB_EVENT_CQ_ERR]		= "CQ error",
62 	[IB_EVENT_QP_FATAL]		= "QP fatal error",
63 	[IB_EVENT_QP_REQ_ERR]		= "QP request error",
64 	[IB_EVENT_QP_ACCESS_ERR]	= "QP access error",
65 	[IB_EVENT_COMM_EST]		= "communication established",
66 	[IB_EVENT_SQ_DRAINED]		= "send queue drained",
67 	[IB_EVENT_PATH_MIG]		= "path migration successful",
68 	[IB_EVENT_PATH_MIG_ERR]		= "path migration error",
69 	[IB_EVENT_DEVICE_FATAL]		= "device fatal error",
70 	[IB_EVENT_PORT_ACTIVE]		= "port active",
71 	[IB_EVENT_PORT_ERR]		= "port error",
72 	[IB_EVENT_LID_CHANGE]		= "LID change",
73 	[IB_EVENT_PKEY_CHANGE]		= "P_key change",
74 	[IB_EVENT_SM_CHANGE]		= "SM change",
75 	[IB_EVENT_SRQ_ERR]		= "SRQ error",
76 	[IB_EVENT_SRQ_LIMIT_REACHED]	= "SRQ limit reached",
77 	[IB_EVENT_QP_LAST_WQE_REACHED]	= "last WQE reached",
78 	[IB_EVENT_CLIENT_REREGISTER]	= "client reregister",
79 	[IB_EVENT_GID_CHANGE]		= "GID changed",
80 };
81 
ib_event_msg(enum ib_event_type event)82 const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
83 {
84 	size_t index = event;
85 
86 	return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
87 			ib_events[index] : "unrecognized event";
88 }
89 EXPORT_SYMBOL(ib_event_msg);
90 
91 static const char * const wc_statuses[] = {
92 	[IB_WC_SUCCESS]			= "success",
93 	[IB_WC_LOC_LEN_ERR]		= "local length error",
94 	[IB_WC_LOC_QP_OP_ERR]		= "local QP operation error",
95 	[IB_WC_LOC_EEC_OP_ERR]		= "local EE context operation error",
96 	[IB_WC_LOC_PROT_ERR]		= "local protection error",
97 	[IB_WC_WR_FLUSH_ERR]		= "WR flushed",
98 	[IB_WC_MW_BIND_ERR]		= "memory management operation error",
99 	[IB_WC_BAD_RESP_ERR]		= "bad response error",
100 	[IB_WC_LOC_ACCESS_ERR]		= "local access error",
101 	[IB_WC_REM_INV_REQ_ERR]		= "invalid request error",
102 	[IB_WC_REM_ACCESS_ERR]		= "remote access error",
103 	[IB_WC_REM_OP_ERR]		= "remote operation error",
104 	[IB_WC_RETRY_EXC_ERR]		= "transport retry counter exceeded",
105 	[IB_WC_RNR_RETRY_EXC_ERR]	= "RNR retry counter exceeded",
106 	[IB_WC_LOC_RDD_VIOL_ERR]	= "local RDD violation error",
107 	[IB_WC_REM_INV_RD_REQ_ERR]	= "remote invalid RD request",
108 	[IB_WC_REM_ABORT_ERR]		= "operation aborted",
109 	[IB_WC_INV_EECN_ERR]		= "invalid EE context number",
110 	[IB_WC_INV_EEC_STATE_ERR]	= "invalid EE context state",
111 	[IB_WC_FATAL_ERR]		= "fatal error",
112 	[IB_WC_RESP_TIMEOUT_ERR]	= "response timeout error",
113 	[IB_WC_GENERAL_ERR]		= "general error",
114 };
115 
ib_wc_status_msg(enum ib_wc_status status)116 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
117 {
118 	size_t index = status;
119 
120 	return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
121 			wc_statuses[index] : "unrecognized status";
122 }
123 EXPORT_SYMBOL(ib_wc_status_msg);
124 
ib_rate_to_mult(enum ib_rate rate)125 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
126 {
127 	switch (rate) {
128 	case IB_RATE_2_5_GBPS: return   1;
129 	case IB_RATE_5_GBPS:   return   2;
130 	case IB_RATE_10_GBPS:  return   4;
131 	case IB_RATE_20_GBPS:  return   8;
132 	case IB_RATE_30_GBPS:  return  12;
133 	case IB_RATE_40_GBPS:  return  16;
134 	case IB_RATE_60_GBPS:  return  24;
135 	case IB_RATE_80_GBPS:  return  32;
136 	case IB_RATE_120_GBPS: return  48;
137 	case IB_RATE_14_GBPS:  return   6;
138 	case IB_RATE_56_GBPS:  return  22;
139 	case IB_RATE_112_GBPS: return  45;
140 	case IB_RATE_168_GBPS: return  67;
141 	case IB_RATE_25_GBPS:  return  10;
142 	case IB_RATE_100_GBPS: return  40;
143 	case IB_RATE_200_GBPS: return  80;
144 	case IB_RATE_300_GBPS: return 120;
145 	case IB_RATE_28_GBPS:  return  11;
146 	case IB_RATE_50_GBPS:  return  20;
147 	case IB_RATE_400_GBPS: return 160;
148 	case IB_RATE_600_GBPS: return 240;
149 	default:	       return  -1;
150 	}
151 }
152 EXPORT_SYMBOL(ib_rate_to_mult);
153 
mult_to_ib_rate(int mult)154 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
155 {
156 	switch (mult) {
157 	case 1:   return IB_RATE_2_5_GBPS;
158 	case 2:   return IB_RATE_5_GBPS;
159 	case 4:   return IB_RATE_10_GBPS;
160 	case 8:   return IB_RATE_20_GBPS;
161 	case 12:  return IB_RATE_30_GBPS;
162 	case 16:  return IB_RATE_40_GBPS;
163 	case 24:  return IB_RATE_60_GBPS;
164 	case 32:  return IB_RATE_80_GBPS;
165 	case 48:  return IB_RATE_120_GBPS;
166 	case 6:   return IB_RATE_14_GBPS;
167 	case 22:  return IB_RATE_56_GBPS;
168 	case 45:  return IB_RATE_112_GBPS;
169 	case 67:  return IB_RATE_168_GBPS;
170 	case 10:  return IB_RATE_25_GBPS;
171 	case 40:  return IB_RATE_100_GBPS;
172 	case 80:  return IB_RATE_200_GBPS;
173 	case 120: return IB_RATE_300_GBPS;
174 	case 11:  return IB_RATE_28_GBPS;
175 	case 20:  return IB_RATE_50_GBPS;
176 	case 160: return IB_RATE_400_GBPS;
177 	case 240: return IB_RATE_600_GBPS;
178 	default:  return IB_RATE_PORT_CURRENT;
179 	}
180 }
181 EXPORT_SYMBOL(mult_to_ib_rate);
182 
ib_rate_to_mbps(enum ib_rate rate)183 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
184 {
185 	switch (rate) {
186 	case IB_RATE_2_5_GBPS: return 2500;
187 	case IB_RATE_5_GBPS:   return 5000;
188 	case IB_RATE_10_GBPS:  return 10000;
189 	case IB_RATE_20_GBPS:  return 20000;
190 	case IB_RATE_30_GBPS:  return 30000;
191 	case IB_RATE_40_GBPS:  return 40000;
192 	case IB_RATE_60_GBPS:  return 60000;
193 	case IB_RATE_80_GBPS:  return 80000;
194 	case IB_RATE_120_GBPS: return 120000;
195 	case IB_RATE_14_GBPS:  return 14062;
196 	case IB_RATE_56_GBPS:  return 56250;
197 	case IB_RATE_112_GBPS: return 112500;
198 	case IB_RATE_168_GBPS: return 168750;
199 	case IB_RATE_25_GBPS:  return 25781;
200 	case IB_RATE_100_GBPS: return 103125;
201 	case IB_RATE_200_GBPS: return 206250;
202 	case IB_RATE_300_GBPS: return 309375;
203 	case IB_RATE_28_GBPS:  return 28125;
204 	case IB_RATE_50_GBPS:  return 53125;
205 	case IB_RATE_400_GBPS: return 425000;
206 	case IB_RATE_600_GBPS: return 637500;
207 	default:	       return -1;
208 	}
209 }
210 EXPORT_SYMBOL(ib_rate_to_mbps);
211 
212 __attribute_const__ enum rdma_transport_type
rdma_node_get_transport(enum rdma_node_type node_type)213 rdma_node_get_transport(enum rdma_node_type node_type)
214 {
215 	switch (node_type) {
216 	case RDMA_NODE_IB_CA:
217 	case RDMA_NODE_IB_SWITCH:
218 	case RDMA_NODE_IB_ROUTER:
219 		return RDMA_TRANSPORT_IB;
220 	case RDMA_NODE_RNIC:
221 		return RDMA_TRANSPORT_IWARP;
222 	case RDMA_NODE_USNIC:
223 		return RDMA_TRANSPORT_USNIC;
224 	case RDMA_NODE_USNIC_UDP:
225 		return RDMA_TRANSPORT_USNIC_UDP;
226 	default:
227 		BUG();
228 		return 0;
229 	}
230 }
231 EXPORT_SYMBOL(rdma_node_get_transport);
232 
rdma_port_get_link_layer(struct ib_device * device,u8 port_num)233 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
234 {
235 	if (device->get_link_layer)
236 		return device->get_link_layer(device, port_num);
237 
238 	switch (rdma_node_get_transport(device->node_type)) {
239 	case RDMA_TRANSPORT_IB:
240 		return IB_LINK_LAYER_INFINIBAND;
241 	case RDMA_TRANSPORT_IWARP:
242 	case RDMA_TRANSPORT_USNIC:
243 	case RDMA_TRANSPORT_USNIC_UDP:
244 		return IB_LINK_LAYER_ETHERNET;
245 	default:
246 		return IB_LINK_LAYER_UNSPECIFIED;
247 	}
248 }
249 EXPORT_SYMBOL(rdma_port_get_link_layer);
250 
251 /* Protection domains */
252 
253 /**
254  * ib_alloc_pd - Allocates an unused protection domain.
255  * @device: The device on which to allocate the protection domain.
256  *
257  * A protection domain object provides an association between QPs, shared
258  * receive queues, address handles, memory regions, and memory windows.
259  *
260  * Every PD has a local_dma_lkey which can be used as the lkey value for local
261  * memory operations.
262  */
__ib_alloc_pd(struct ib_device * device,unsigned int flags,const char * caller)263 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
264 		const char *caller)
265 {
266 	struct ib_pd *pd;
267 	int mr_access_flags = 0;
268 
269 	pd = device->alloc_pd(device, NULL, NULL);
270 	if (IS_ERR(pd))
271 		return pd;
272 
273 	pd->device = device;
274 	pd->uobject = NULL;
275 	pd->__internal_mr = NULL;
276 	atomic_set(&pd->usecnt, 0);
277 	pd->flags = flags;
278 
279 	if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
280 		pd->local_dma_lkey = device->local_dma_lkey;
281 	else
282 		mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
283 
284 	if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
285 		pr_warn("%s: enabling unsafe global rkey\n", caller);
286 		mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
287 	}
288 
289 	if (mr_access_flags) {
290 		struct ib_mr *mr;
291 
292 		mr = pd->device->get_dma_mr(pd, mr_access_flags);
293 		if (IS_ERR(mr)) {
294 			ib_dealloc_pd(pd);
295 			return ERR_CAST(mr);
296 		}
297 
298 		mr->device	= pd->device;
299 		mr->pd		= pd;
300 		mr->uobject	= NULL;
301 		mr->need_inval	= false;
302 
303 		pd->__internal_mr = mr;
304 
305 		if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
306 			pd->local_dma_lkey = pd->__internal_mr->lkey;
307 
308 		if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
309 			pd->unsafe_global_rkey = pd->__internal_mr->rkey;
310 	}
311 
312 	return pd;
313 }
314 EXPORT_SYMBOL(__ib_alloc_pd);
315 
316 /**
317  * ib_dealloc_pd - Deallocates a protection domain.
318  * @pd: The protection domain to deallocate.
319  *
320  * It is an error to call this function while any resources in the pd still
321  * exist.  The caller is responsible to synchronously destroy them and
322  * guarantee no new allocations will happen.
323  */
ib_dealloc_pd(struct ib_pd * pd)324 void ib_dealloc_pd(struct ib_pd *pd)
325 {
326 	int ret;
327 
328 	if (pd->__internal_mr) {
329 		ret = pd->device->dereg_mr(pd->__internal_mr);
330 		WARN_ON(ret);
331 		pd->__internal_mr = NULL;
332 	}
333 
334 	/* uverbs manipulates usecnt with proper locking, while the kabi
335 	   requires the caller to guarantee we can't race here. */
336 	WARN_ON(atomic_read(&pd->usecnt));
337 
338 	/* Making delalloc_pd a void return is a WIP, no driver should return
339 	   an error here. */
340 	ret = pd->device->dealloc_pd(pd);
341 	WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
342 }
343 EXPORT_SYMBOL(ib_dealloc_pd);
344 
345 /* Address handles */
346 
ib_create_ah(struct ib_pd * pd,struct ib_ah_attr * ah_attr)347 struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
348 {
349 	struct ib_ah *ah;
350 
351 	ah = pd->device->create_ah(pd, ah_attr, NULL);
352 
353 	if (!IS_ERR(ah)) {
354 		ah->device  = pd->device;
355 		ah->pd      = pd;
356 		ah->uobject = NULL;
357 		atomic_inc(&pd->usecnt);
358 	}
359 
360 	return ah;
361 }
362 EXPORT_SYMBOL(ib_create_ah);
363 
ib_get_header_version(const union rdma_network_hdr * hdr)364 static int ib_get_header_version(const union rdma_network_hdr *hdr)
365 {
366 	const struct ip *ip4h = (const struct ip *)&hdr->roce4grh;
367 	struct ip ip4h_checked;
368 	const struct ip6_hdr *ip6h = (const struct ip6_hdr *)&hdr->ibgrh;
369 
370 	/* If it's IPv6, the version must be 6, otherwise, the first
371 	 * 20 bytes (before the IPv4 header) are garbled.
372 	 */
373 	if ((ip6h->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
374 		return (ip4h->ip_v == 4) ? 4 : 0;
375 	/* version may be 6 or 4 because the first 20 bytes could be garbled */
376 
377 	/* RoCE v2 requires no options, thus header length
378 	 * must be 5 words
379 	 */
380 	if (ip4h->ip_hl != 5)
381 		return 6;
382 
383 	/* Verify checksum.
384 	 * We can't write on scattered buffers so we need to copy to
385 	 * temp buffer.
386 	 */
387 	memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
388 	ip4h_checked.ip_sum = 0;
389 #if defined(INET) || defined(INET6)
390 	ip4h_checked.ip_sum = in_cksum_hdr(&ip4h_checked);
391 #endif
392 	/* if IPv4 header checksum is OK, believe it */
393 	if (ip4h->ip_sum == ip4h_checked.ip_sum)
394 		return 4;
395 	return 6;
396 }
397 
ib_get_net_type_by_grh(struct ib_device * device,u8 port_num,const struct ib_grh * grh)398 static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
399 						     u8 port_num,
400 						     const struct ib_grh *grh)
401 {
402 	int grh_version;
403 
404 	if (rdma_protocol_ib(device, port_num))
405 		return RDMA_NETWORK_IB;
406 
407 	grh_version = ib_get_header_version((const union rdma_network_hdr *)grh);
408 
409 	if (grh_version == 4)
410 		return RDMA_NETWORK_IPV4;
411 
412 	if (grh->next_hdr == IPPROTO_UDP)
413 		return RDMA_NETWORK_IPV6;
414 
415 	return RDMA_NETWORK_ROCE_V1;
416 }
417 
418 struct find_gid_index_context {
419 	u16 vlan_id;
420 	enum ib_gid_type gid_type;
421 };
422 
423 
424 /*
425  * This function will return true only if a inspected GID index
426  * matches the request based on the GID type and VLAN configuration
427  */
find_gid_index(const union ib_gid * gid,const struct ib_gid_attr * gid_attr,void * context)428 static bool find_gid_index(const union ib_gid *gid,
429 			   const struct ib_gid_attr *gid_attr,
430 			   void *context)
431 {
432 	u16 vlan_diff;
433 	struct find_gid_index_context *ctx =
434 		(struct find_gid_index_context *)context;
435 
436 	if (ctx->gid_type != gid_attr->gid_type)
437 		return false;
438 
439 	/*
440 	 * The following will verify:
441 	 * 1. VLAN ID matching for VLAN tagged requests.
442 	 * 2. prio-tagged/untagged to prio-tagged/untagged matching.
443 	 *
444 	 * This XOR is valid, since 0x0 < vlan_id < 0x0FFF.
445 	 */
446 	vlan_diff = rdma_vlan_dev_vlan_id(gid_attr->ndev) ^ ctx->vlan_id;
447 
448 	return (vlan_diff == 0x0000 || vlan_diff == 0xFFFF);
449 }
450 
get_sgid_index_from_eth(struct ib_device * device,u8 port_num,u16 vlan_id,const union ib_gid * sgid,enum ib_gid_type gid_type,u16 * gid_index)451 static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num,
452 				   u16 vlan_id, const union ib_gid *sgid,
453 				   enum ib_gid_type gid_type,
454 				   u16 *gid_index)
455 {
456 	struct find_gid_index_context context = {.vlan_id = vlan_id,
457 						 .gid_type = gid_type};
458 
459 	return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index,
460 				     &context, gid_index);
461 }
462 
get_gids_from_rdma_hdr(const union rdma_network_hdr * hdr,enum rdma_network_type net_type,union ib_gid * sgid,union ib_gid * dgid)463 static int get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
464 				  enum rdma_network_type net_type,
465 				  union ib_gid *sgid, union ib_gid *dgid)
466 {
467 	struct sockaddr_in  src_in;
468 	struct sockaddr_in  dst_in;
469 	__be32 src_saddr, dst_saddr;
470 
471 	if (!sgid || !dgid)
472 		return -EINVAL;
473 
474 	if (net_type == RDMA_NETWORK_IPV4) {
475 		memcpy(&src_in.sin_addr.s_addr,
476 		       &hdr->roce4grh.ip_src, 4);
477 		memcpy(&dst_in.sin_addr.s_addr,
478 		       &hdr->roce4grh.ip_dst, 4);
479 		src_saddr = src_in.sin_addr.s_addr;
480 		dst_saddr = dst_in.sin_addr.s_addr;
481 		ipv6_addr_set_v4mapped(src_saddr,
482 				       (struct in6_addr *)sgid);
483 		ipv6_addr_set_v4mapped(dst_saddr,
484 				       (struct in6_addr *)dgid);
485 		return 0;
486 	} else if (net_type == RDMA_NETWORK_IPV6 ||
487 		   net_type == RDMA_NETWORK_IB) {
488 		*dgid = hdr->ibgrh.dgid;
489 		*sgid = hdr->ibgrh.sgid;
490 		return 0;
491 	} else {
492 		return -EINVAL;
493 	}
494 }
495 
ib_init_ah_from_wc(struct ib_device * device,u8 port_num,const struct ib_wc * wc,const struct ib_grh * grh,struct ib_ah_attr * ah_attr)496 int ib_init_ah_from_wc(struct ib_device *device, u8 port_num,
497 		       const struct ib_wc *wc, const struct ib_grh *grh,
498 		       struct ib_ah_attr *ah_attr)
499 {
500 	u32 flow_class;
501 	u16 gid_index = 0;
502 	int ret;
503 	enum rdma_network_type net_type = RDMA_NETWORK_IB;
504 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
505 	int hoplimit = 0xff;
506 	union ib_gid dgid;
507 	union ib_gid sgid;
508 
509 	memset(ah_attr, 0, sizeof *ah_attr);
510 	if (rdma_cap_eth_ah(device, port_num)) {
511 		if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
512 			net_type = wc->network_hdr_type;
513 		else
514 			net_type = ib_get_net_type_by_grh(device, port_num, grh);
515 		gid_type = ib_network_to_gid_type(net_type);
516 	}
517 	ret = get_gids_from_rdma_hdr((const union rdma_network_hdr *)grh, net_type,
518 				     &sgid, &dgid);
519 	if (ret)
520 		return ret;
521 
522 	if (rdma_protocol_roce(device, port_num)) {
523 		struct ib_gid_attr dgid_attr;
524 		const u16 vlan_id = (wc->wc_flags & IB_WC_WITH_VLAN) ?
525 				wc->vlan_id : 0xffff;
526 
527 		if (!(wc->wc_flags & IB_WC_GRH))
528 			return -EPROTOTYPE;
529 
530 		ret = get_sgid_index_from_eth(device, port_num, vlan_id,
531 					      &dgid, gid_type, &gid_index);
532 		if (ret)
533 			return ret;
534 
535 		ret = ib_get_cached_gid(device, port_num, gid_index, &dgid, &dgid_attr);
536 		if (ret)
537 			return ret;
538 
539 		if (dgid_attr.ndev == NULL)
540 			return -ENODEV;
541 
542 		ret = rdma_addr_find_l2_eth_by_grh(&dgid, &sgid, ah_attr->dmac,
543 		    dgid_attr.ndev, &hoplimit);
544 
545 		dev_put(dgid_attr.ndev);
546 		if (ret)
547 			return ret;
548 	}
549 
550 	ah_attr->dlid = wc->slid;
551 	ah_attr->sl = wc->sl;
552 	ah_attr->src_path_bits = wc->dlid_path_bits;
553 	ah_attr->port_num = port_num;
554 
555 	if (wc->wc_flags & IB_WC_GRH) {
556 		ah_attr->ah_flags = IB_AH_GRH;
557 		ah_attr->grh.dgid = sgid;
558 
559 		if (!rdma_cap_eth_ah(device, port_num)) {
560 			if (dgid.global.interface_id != cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
561 				ret = ib_find_cached_gid_by_port(device, &dgid,
562 								 IB_GID_TYPE_IB,
563 								 port_num, NULL,
564 								 &gid_index);
565 				if (ret)
566 					return ret;
567 			}
568 		}
569 
570 		ah_attr->grh.sgid_index = (u8) gid_index;
571 		flow_class = be32_to_cpu(grh->version_tclass_flow);
572 		ah_attr->grh.flow_label = flow_class & 0xFFFFF;
573 		ah_attr->grh.hop_limit = hoplimit;
574 		ah_attr->grh.traffic_class = (flow_class >> 20) & 0xFF;
575 	}
576 	return 0;
577 }
578 EXPORT_SYMBOL(ib_init_ah_from_wc);
579 
ib_create_ah_from_wc(struct ib_pd * pd,const struct ib_wc * wc,const struct ib_grh * grh,u8 port_num)580 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
581 				   const struct ib_grh *grh, u8 port_num)
582 {
583 	struct ib_ah_attr ah_attr;
584 	int ret;
585 
586 	ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr);
587 	if (ret)
588 		return ERR_PTR(ret);
589 
590 	return ib_create_ah(pd, &ah_attr);
591 }
592 EXPORT_SYMBOL(ib_create_ah_from_wc);
593 
ib_modify_ah(struct ib_ah * ah,struct ib_ah_attr * ah_attr)594 int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
595 {
596 	return ah->device->modify_ah ?
597 		ah->device->modify_ah(ah, ah_attr) :
598 		-ENOSYS;
599 }
600 EXPORT_SYMBOL(ib_modify_ah);
601 
ib_query_ah(struct ib_ah * ah,struct ib_ah_attr * ah_attr)602 int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
603 {
604 	return ah->device->query_ah ?
605 		ah->device->query_ah(ah, ah_attr) :
606 		-ENOSYS;
607 }
608 EXPORT_SYMBOL(ib_query_ah);
609 
ib_destroy_ah(struct ib_ah * ah)610 int ib_destroy_ah(struct ib_ah *ah)
611 {
612 	struct ib_pd *pd;
613 	int ret;
614 
615 	pd = ah->pd;
616 	ret = ah->device->destroy_ah(ah);
617 	if (!ret)
618 		atomic_dec(&pd->usecnt);
619 
620 	return ret;
621 }
622 EXPORT_SYMBOL(ib_destroy_ah);
623 
624 /* Shared receive queues */
625 
ib_create_srq(struct ib_pd * pd,struct ib_srq_init_attr * srq_init_attr)626 struct ib_srq *ib_create_srq(struct ib_pd *pd,
627 			     struct ib_srq_init_attr *srq_init_attr)
628 {
629 	struct ib_srq *srq;
630 
631 	if (!pd->device->create_srq)
632 		return ERR_PTR(-ENOSYS);
633 
634 	srq = pd->device->create_srq(pd, srq_init_attr, NULL);
635 
636 	if (!IS_ERR(srq)) {
637 		srq->device    	   = pd->device;
638 		srq->pd        	   = pd;
639 		srq->uobject       = NULL;
640 		srq->event_handler = srq_init_attr->event_handler;
641 		srq->srq_context   = srq_init_attr->srq_context;
642 		srq->srq_type      = srq_init_attr->srq_type;
643 		if (srq->srq_type == IB_SRQT_XRC) {
644 			srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
645 			srq->ext.xrc.cq   = srq_init_attr->ext.xrc.cq;
646 			atomic_inc(&srq->ext.xrc.xrcd->usecnt);
647 			atomic_inc(&srq->ext.xrc.cq->usecnt);
648 		}
649 		atomic_inc(&pd->usecnt);
650 		atomic_set(&srq->usecnt, 0);
651 	}
652 
653 	return srq;
654 }
655 EXPORT_SYMBOL(ib_create_srq);
656 
ib_modify_srq(struct ib_srq * srq,struct ib_srq_attr * srq_attr,enum ib_srq_attr_mask srq_attr_mask)657 int ib_modify_srq(struct ib_srq *srq,
658 		  struct ib_srq_attr *srq_attr,
659 		  enum ib_srq_attr_mask srq_attr_mask)
660 {
661 	return srq->device->modify_srq ?
662 		srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
663 		-ENOSYS;
664 }
665 EXPORT_SYMBOL(ib_modify_srq);
666 
ib_query_srq(struct ib_srq * srq,struct ib_srq_attr * srq_attr)667 int ib_query_srq(struct ib_srq *srq,
668 		 struct ib_srq_attr *srq_attr)
669 {
670 	return srq->device->query_srq ?
671 		srq->device->query_srq(srq, srq_attr) : -ENOSYS;
672 }
673 EXPORT_SYMBOL(ib_query_srq);
674 
ib_destroy_srq(struct ib_srq * srq)675 int ib_destroy_srq(struct ib_srq *srq)
676 {
677 	struct ib_pd *pd;
678 	enum ib_srq_type srq_type;
679 	struct ib_xrcd *uninitialized_var(xrcd);
680 	struct ib_cq *uninitialized_var(cq);
681 	int ret;
682 
683 	if (atomic_read(&srq->usecnt))
684 		return -EBUSY;
685 
686 	pd = srq->pd;
687 	srq_type = srq->srq_type;
688 	if (srq_type == IB_SRQT_XRC) {
689 		xrcd = srq->ext.xrc.xrcd;
690 		cq = srq->ext.xrc.cq;
691 	}
692 
693 	ret = srq->device->destroy_srq(srq);
694 	if (!ret) {
695 		atomic_dec(&pd->usecnt);
696 		if (srq_type == IB_SRQT_XRC) {
697 			atomic_dec(&xrcd->usecnt);
698 			atomic_dec(&cq->usecnt);
699 		}
700 	}
701 
702 	return ret;
703 }
704 EXPORT_SYMBOL(ib_destroy_srq);
705 
706 /* Queue pairs */
707 
__ib_shared_qp_event_handler(struct ib_event * event,void * context)708 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
709 {
710 	struct ib_qp *qp = context;
711 	unsigned long flags;
712 
713 	spin_lock_irqsave(&qp->device->event_handler_lock, flags);
714 	list_for_each_entry(event->element.qp, &qp->open_list, open_list)
715 		if (event->element.qp->event_handler)
716 			event->element.qp->event_handler(event, event->element.qp->qp_context);
717 	spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
718 }
719 
__ib_insert_xrcd_qp(struct ib_xrcd * xrcd,struct ib_qp * qp)720 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
721 {
722 	mutex_lock(&xrcd->tgt_qp_mutex);
723 	list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
724 	mutex_unlock(&xrcd->tgt_qp_mutex);
725 }
726 
__ib_open_qp(struct ib_qp * real_qp,void (* event_handler)(struct ib_event *,void *),void * qp_context)727 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
728 				  void (*event_handler)(struct ib_event *, void *),
729 				  void *qp_context)
730 {
731 	struct ib_qp *qp;
732 	unsigned long flags;
733 
734 	qp = kzalloc(sizeof *qp, GFP_KERNEL);
735 	if (!qp)
736 		return ERR_PTR(-ENOMEM);
737 
738 	qp->real_qp = real_qp;
739 	atomic_inc(&real_qp->usecnt);
740 	qp->device = real_qp->device;
741 	qp->event_handler = event_handler;
742 	qp->qp_context = qp_context;
743 	qp->qp_num = real_qp->qp_num;
744 	qp->qp_type = real_qp->qp_type;
745 
746 	spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
747 	list_add(&qp->open_list, &real_qp->open_list);
748 	spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
749 
750 	return qp;
751 }
752 
ib_open_qp(struct ib_xrcd * xrcd,struct ib_qp_open_attr * qp_open_attr)753 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
754 			 struct ib_qp_open_attr *qp_open_attr)
755 {
756 	struct ib_qp *qp, *real_qp;
757 
758 	if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
759 		return ERR_PTR(-EINVAL);
760 
761 	qp = ERR_PTR(-EINVAL);
762 	mutex_lock(&xrcd->tgt_qp_mutex);
763 	list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
764 		if (real_qp->qp_num == qp_open_attr->qp_num) {
765 			qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
766 					  qp_open_attr->qp_context);
767 			break;
768 		}
769 	}
770 	mutex_unlock(&xrcd->tgt_qp_mutex);
771 	return qp;
772 }
773 EXPORT_SYMBOL(ib_open_qp);
774 
ib_create_xrc_qp(struct ib_qp * qp,struct ib_qp_init_attr * qp_init_attr)775 static struct ib_qp *ib_create_xrc_qp(struct ib_qp *qp,
776 		struct ib_qp_init_attr *qp_init_attr)
777 {
778 	struct ib_qp *real_qp = qp;
779 
780 	qp->event_handler = __ib_shared_qp_event_handler;
781 	qp->qp_context = qp;
782 	qp->pd = NULL;
783 	qp->send_cq = qp->recv_cq = NULL;
784 	qp->srq = NULL;
785 	qp->xrcd = qp_init_attr->xrcd;
786 	atomic_inc(&qp_init_attr->xrcd->usecnt);
787 	INIT_LIST_HEAD(&qp->open_list);
788 
789 	qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
790 			  qp_init_attr->qp_context);
791 	if (!IS_ERR(qp))
792 		__ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
793 	else
794 		real_qp->device->destroy_qp(real_qp);
795 	return qp;
796 }
797 
ib_create_qp(struct ib_pd * pd,struct ib_qp_init_attr * qp_init_attr)798 struct ib_qp *ib_create_qp(struct ib_pd *pd,
799 			   struct ib_qp_init_attr *qp_init_attr)
800 {
801 	struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
802 	struct ib_qp *qp;
803 
804 	if (qp_init_attr->rwq_ind_tbl &&
805 	    (qp_init_attr->recv_cq ||
806 	    qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
807 	    qp_init_attr->cap.max_recv_sge))
808 		return ERR_PTR(-EINVAL);
809 
810 	qp = device->create_qp(pd, qp_init_attr, NULL);
811 	if (IS_ERR(qp))
812 		return qp;
813 
814 	qp->device     = device;
815 	qp->real_qp    = qp;
816 	qp->uobject    = NULL;
817 	qp->qp_type    = qp_init_attr->qp_type;
818 	qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
819 
820 	atomic_set(&qp->usecnt, 0);
821 	spin_lock_init(&qp->mr_lock);
822 
823 	if (qp_init_attr->qp_type == IB_QPT_XRC_TGT)
824 		return ib_create_xrc_qp(qp, qp_init_attr);
825 
826 	qp->event_handler = qp_init_attr->event_handler;
827 	qp->qp_context = qp_init_attr->qp_context;
828 	if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
829 		qp->recv_cq = NULL;
830 		qp->srq = NULL;
831 	} else {
832 		qp->recv_cq = qp_init_attr->recv_cq;
833 		if (qp_init_attr->recv_cq)
834 			atomic_inc(&qp_init_attr->recv_cq->usecnt);
835 		qp->srq = qp_init_attr->srq;
836 		if (qp->srq)
837 			atomic_inc(&qp_init_attr->srq->usecnt);
838 	}
839 
840 	qp->pd	    = pd;
841 	qp->send_cq = qp_init_attr->send_cq;
842 	qp->xrcd    = NULL;
843 
844 	atomic_inc(&pd->usecnt);
845 	if (qp_init_attr->send_cq)
846 		atomic_inc(&qp_init_attr->send_cq->usecnt);
847 	if (qp_init_attr->rwq_ind_tbl)
848 		atomic_inc(&qp->rwq_ind_tbl->usecnt);
849 
850 	/*
851 	 * Note: all hw drivers guarantee that max_send_sge is lower than
852 	 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
853 	 * max_send_sge <= max_sge_rd.
854 	 */
855 	qp->max_write_sge = qp_init_attr->cap.max_send_sge;
856 	qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
857 				 device->attrs.max_sge_rd);
858 
859 	return qp;
860 }
861 EXPORT_SYMBOL(ib_create_qp);
862 
863 static const struct {
864 	int			valid;
865 	enum ib_qp_attr_mask	req_param[IB_QPT_MAX];
866 	enum ib_qp_attr_mask	opt_param[IB_QPT_MAX];
867 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
868 	[IB_QPS_RESET] = {
869 		[IB_QPS_RESET] = { .valid = 1 },
870 		[IB_QPS_INIT]  = {
871 			.valid = 1,
872 			.req_param = {
873 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
874 						IB_QP_PORT			|
875 						IB_QP_QKEY),
876 				[IB_QPT_RAW_PACKET] = IB_QP_PORT,
877 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
878 						IB_QP_PORT			|
879 						IB_QP_ACCESS_FLAGS),
880 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
881 						IB_QP_PORT			|
882 						IB_QP_ACCESS_FLAGS),
883 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
884 						IB_QP_PORT			|
885 						IB_QP_ACCESS_FLAGS),
886 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
887 						IB_QP_PORT			|
888 						IB_QP_ACCESS_FLAGS),
889 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
890 						IB_QP_QKEY),
891 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
892 						IB_QP_QKEY),
893 			}
894 		},
895 	},
896 	[IB_QPS_INIT]  = {
897 		[IB_QPS_RESET] = { .valid = 1 },
898 		[IB_QPS_ERR] =   { .valid = 1 },
899 		[IB_QPS_INIT]  = {
900 			.valid = 1,
901 			.opt_param = {
902 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
903 						IB_QP_PORT			|
904 						IB_QP_QKEY),
905 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
906 						IB_QP_PORT			|
907 						IB_QP_ACCESS_FLAGS),
908 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
909 						IB_QP_PORT			|
910 						IB_QP_ACCESS_FLAGS),
911 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
912 						IB_QP_PORT			|
913 						IB_QP_ACCESS_FLAGS),
914 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
915 						IB_QP_PORT			|
916 						IB_QP_ACCESS_FLAGS),
917 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
918 						IB_QP_QKEY),
919 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
920 						IB_QP_QKEY),
921 			}
922 		},
923 		[IB_QPS_RTR]   = {
924 			.valid = 1,
925 			.req_param = {
926 				[IB_QPT_UC]  = (IB_QP_AV			|
927 						IB_QP_PATH_MTU			|
928 						IB_QP_DEST_QPN			|
929 						IB_QP_RQ_PSN),
930 				[IB_QPT_RC]  = (IB_QP_AV			|
931 						IB_QP_PATH_MTU			|
932 						IB_QP_DEST_QPN			|
933 						IB_QP_RQ_PSN			|
934 						IB_QP_MAX_DEST_RD_ATOMIC	|
935 						IB_QP_MIN_RNR_TIMER),
936 				[IB_QPT_XRC_INI] = (IB_QP_AV			|
937 						IB_QP_PATH_MTU			|
938 						IB_QP_DEST_QPN			|
939 						IB_QP_RQ_PSN),
940 				[IB_QPT_XRC_TGT] = (IB_QP_AV			|
941 						IB_QP_PATH_MTU			|
942 						IB_QP_DEST_QPN			|
943 						IB_QP_RQ_PSN			|
944 						IB_QP_MAX_DEST_RD_ATOMIC	|
945 						IB_QP_MIN_RNR_TIMER),
946 			},
947 			.opt_param = {
948 				 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
949 						 IB_QP_QKEY),
950 				 [IB_QPT_UC]  = (IB_QP_ALT_PATH			|
951 						 IB_QP_ACCESS_FLAGS		|
952 						 IB_QP_PKEY_INDEX),
953 				 [IB_QPT_RC]  = (IB_QP_ALT_PATH			|
954 						 IB_QP_ACCESS_FLAGS		|
955 						 IB_QP_PKEY_INDEX),
956 				 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH		|
957 						 IB_QP_ACCESS_FLAGS		|
958 						 IB_QP_PKEY_INDEX),
959 				 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH		|
960 						 IB_QP_ACCESS_FLAGS		|
961 						 IB_QP_PKEY_INDEX),
962 				 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
963 						 IB_QP_QKEY),
964 				 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
965 						 IB_QP_QKEY),
966 			 },
967 		},
968 	},
969 	[IB_QPS_RTR]   = {
970 		[IB_QPS_RESET] = { .valid = 1 },
971 		[IB_QPS_ERR] =   { .valid = 1 },
972 		[IB_QPS_RTS]   = {
973 			.valid = 1,
974 			.req_param = {
975 				[IB_QPT_UD]  = IB_QP_SQ_PSN,
976 				[IB_QPT_UC]  = IB_QP_SQ_PSN,
977 				[IB_QPT_RC]  = (IB_QP_TIMEOUT			|
978 						IB_QP_RETRY_CNT			|
979 						IB_QP_RNR_RETRY			|
980 						IB_QP_SQ_PSN			|
981 						IB_QP_MAX_QP_RD_ATOMIC),
982 				[IB_QPT_XRC_INI] = (IB_QP_TIMEOUT		|
983 						IB_QP_RETRY_CNT			|
984 						IB_QP_RNR_RETRY			|
985 						IB_QP_SQ_PSN			|
986 						IB_QP_MAX_QP_RD_ATOMIC),
987 				[IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT		|
988 						IB_QP_SQ_PSN),
989 				[IB_QPT_SMI] = IB_QP_SQ_PSN,
990 				[IB_QPT_GSI] = IB_QP_SQ_PSN,
991 			},
992 			.opt_param = {
993 				 [IB_QPT_UD]  = (IB_QP_CUR_STATE		|
994 						 IB_QP_QKEY),
995 				 [IB_QPT_UC]  = (IB_QP_CUR_STATE		|
996 						 IB_QP_ALT_PATH			|
997 						 IB_QP_ACCESS_FLAGS		|
998 						 IB_QP_PATH_MIG_STATE),
999 				 [IB_QPT_RC]  = (IB_QP_CUR_STATE		|
1000 						 IB_QP_ALT_PATH			|
1001 						 IB_QP_ACCESS_FLAGS		|
1002 						 IB_QP_MIN_RNR_TIMER		|
1003 						 IB_QP_PATH_MIG_STATE),
1004 				 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1005 						 IB_QP_ALT_PATH			|
1006 						 IB_QP_ACCESS_FLAGS		|
1007 						 IB_QP_PATH_MIG_STATE),
1008 				 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1009 						 IB_QP_ALT_PATH			|
1010 						 IB_QP_ACCESS_FLAGS		|
1011 						 IB_QP_MIN_RNR_TIMER		|
1012 						 IB_QP_PATH_MIG_STATE),
1013 				 [IB_QPT_SMI] = (IB_QP_CUR_STATE		|
1014 						 IB_QP_QKEY),
1015 				 [IB_QPT_GSI] = (IB_QP_CUR_STATE		|
1016 						 IB_QP_QKEY),
1017 				 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1018 			 }
1019 		}
1020 	},
1021 	[IB_QPS_RTS]   = {
1022 		[IB_QPS_RESET] = { .valid = 1 },
1023 		[IB_QPS_ERR] =   { .valid = 1 },
1024 		[IB_QPS_RTS]   = {
1025 			.valid = 1,
1026 			.opt_param = {
1027 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
1028 						IB_QP_QKEY),
1029 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
1030 						IB_QP_ACCESS_FLAGS		|
1031 						IB_QP_ALT_PATH			|
1032 						IB_QP_PATH_MIG_STATE),
1033 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
1034 						IB_QP_ACCESS_FLAGS		|
1035 						IB_QP_ALT_PATH			|
1036 						IB_QP_PATH_MIG_STATE		|
1037 						IB_QP_MIN_RNR_TIMER),
1038 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1039 						IB_QP_ACCESS_FLAGS		|
1040 						IB_QP_ALT_PATH			|
1041 						IB_QP_PATH_MIG_STATE),
1042 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1043 						IB_QP_ACCESS_FLAGS		|
1044 						IB_QP_ALT_PATH			|
1045 						IB_QP_PATH_MIG_STATE		|
1046 						IB_QP_MIN_RNR_TIMER),
1047 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
1048 						IB_QP_QKEY),
1049 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
1050 						IB_QP_QKEY),
1051 				[IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1052 			}
1053 		},
1054 		[IB_QPS_SQD]   = {
1055 			.valid = 1,
1056 			.opt_param = {
1057 				[IB_QPT_UD]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1058 				[IB_QPT_UC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1059 				[IB_QPT_RC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1060 				[IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1061 				[IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1062 				[IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1063 				[IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1064 			}
1065 		},
1066 	},
1067 	[IB_QPS_SQD]   = {
1068 		[IB_QPS_RESET] = { .valid = 1 },
1069 		[IB_QPS_ERR] =   { .valid = 1 },
1070 		[IB_QPS_RTS]   = {
1071 			.valid = 1,
1072 			.opt_param = {
1073 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
1074 						IB_QP_QKEY),
1075 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
1076 						IB_QP_ALT_PATH			|
1077 						IB_QP_ACCESS_FLAGS		|
1078 						IB_QP_PATH_MIG_STATE),
1079 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
1080 						IB_QP_ALT_PATH			|
1081 						IB_QP_ACCESS_FLAGS		|
1082 						IB_QP_MIN_RNR_TIMER		|
1083 						IB_QP_PATH_MIG_STATE),
1084 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1085 						IB_QP_ALT_PATH			|
1086 						IB_QP_ACCESS_FLAGS		|
1087 						IB_QP_PATH_MIG_STATE),
1088 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1089 						IB_QP_ALT_PATH			|
1090 						IB_QP_ACCESS_FLAGS		|
1091 						IB_QP_MIN_RNR_TIMER		|
1092 						IB_QP_PATH_MIG_STATE),
1093 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
1094 						IB_QP_QKEY),
1095 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
1096 						IB_QP_QKEY),
1097 			}
1098 		},
1099 		[IB_QPS_SQD]   = {
1100 			.valid = 1,
1101 			.opt_param = {
1102 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
1103 						IB_QP_QKEY),
1104 				[IB_QPT_UC]  = (IB_QP_AV			|
1105 						IB_QP_ALT_PATH			|
1106 						IB_QP_ACCESS_FLAGS		|
1107 						IB_QP_PKEY_INDEX		|
1108 						IB_QP_PATH_MIG_STATE),
1109 				[IB_QPT_RC]  = (IB_QP_PORT			|
1110 						IB_QP_AV			|
1111 						IB_QP_TIMEOUT			|
1112 						IB_QP_RETRY_CNT			|
1113 						IB_QP_RNR_RETRY			|
1114 						IB_QP_MAX_QP_RD_ATOMIC		|
1115 						IB_QP_MAX_DEST_RD_ATOMIC	|
1116 						IB_QP_ALT_PATH			|
1117 						IB_QP_ACCESS_FLAGS		|
1118 						IB_QP_PKEY_INDEX		|
1119 						IB_QP_MIN_RNR_TIMER		|
1120 						IB_QP_PATH_MIG_STATE),
1121 				[IB_QPT_XRC_INI] = (IB_QP_PORT			|
1122 						IB_QP_AV			|
1123 						IB_QP_TIMEOUT			|
1124 						IB_QP_RETRY_CNT			|
1125 						IB_QP_RNR_RETRY			|
1126 						IB_QP_MAX_QP_RD_ATOMIC		|
1127 						IB_QP_ALT_PATH			|
1128 						IB_QP_ACCESS_FLAGS		|
1129 						IB_QP_PKEY_INDEX		|
1130 						IB_QP_PATH_MIG_STATE),
1131 				[IB_QPT_XRC_TGT] = (IB_QP_PORT			|
1132 						IB_QP_AV			|
1133 						IB_QP_TIMEOUT			|
1134 						IB_QP_MAX_DEST_RD_ATOMIC	|
1135 						IB_QP_ALT_PATH			|
1136 						IB_QP_ACCESS_FLAGS		|
1137 						IB_QP_PKEY_INDEX		|
1138 						IB_QP_MIN_RNR_TIMER		|
1139 						IB_QP_PATH_MIG_STATE),
1140 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
1141 						IB_QP_QKEY),
1142 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
1143 						IB_QP_QKEY),
1144 			}
1145 		}
1146 	},
1147 	[IB_QPS_SQE]   = {
1148 		[IB_QPS_RESET] = { .valid = 1 },
1149 		[IB_QPS_ERR] =   { .valid = 1 },
1150 		[IB_QPS_RTS]   = {
1151 			.valid = 1,
1152 			.opt_param = {
1153 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
1154 						IB_QP_QKEY),
1155 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
1156 						IB_QP_ACCESS_FLAGS),
1157 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
1158 						IB_QP_QKEY),
1159 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
1160 						IB_QP_QKEY),
1161 			}
1162 		}
1163 	},
1164 	[IB_QPS_ERR] = {
1165 		[IB_QPS_RESET] = { .valid = 1 },
1166 		[IB_QPS_ERR] =   { .valid = 1 }
1167 	}
1168 };
1169 
ib_modify_qp_is_ok(enum ib_qp_state cur_state,enum ib_qp_state next_state,enum ib_qp_type type,enum ib_qp_attr_mask mask)1170 bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1171 			enum ib_qp_type type, enum ib_qp_attr_mask mask)
1172 {
1173 	enum ib_qp_attr_mask req_param, opt_param;
1174 
1175 	if (mask & IB_QP_CUR_STATE  &&
1176 	    cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1177 	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1178 		return false;
1179 
1180 	if (!qp_state_table[cur_state][next_state].valid)
1181 		return false;
1182 
1183 	req_param = qp_state_table[cur_state][next_state].req_param[type];
1184 	opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1185 
1186 	if ((mask & req_param) != req_param)
1187 		return false;
1188 
1189 	if (mask & ~(req_param | opt_param | IB_QP_STATE))
1190 		return false;
1191 
1192 	return true;
1193 }
1194 EXPORT_SYMBOL(ib_modify_qp_is_ok);
1195 
ib_resolve_eth_dmac(struct ib_device * device,struct ib_ah_attr * ah_attr)1196 int ib_resolve_eth_dmac(struct ib_device *device,
1197 			struct ib_ah_attr *ah_attr)
1198 {
1199 	struct ib_gid_attr sgid_attr;
1200 	union ib_gid sgid;
1201 	int hop_limit;
1202 	int ret;
1203 
1204 	if (ah_attr->port_num < rdma_start_port(device) ||
1205 	    ah_attr->port_num > rdma_end_port(device))
1206 		return -EINVAL;
1207 
1208 	if (!rdma_cap_eth_ah(device, ah_attr->port_num))
1209 		return 0;
1210 
1211 	if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1212 		if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1213 			__be32 addr = 0;
1214 
1215 			memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
1216 			ip_eth_mc_map(addr, (char *)ah_attr->dmac);
1217 		} else {
1218 			ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
1219 					(char *)ah_attr->dmac);
1220 		}
1221 		return 0;
1222 	}
1223 
1224 	ret = ib_query_gid(device,
1225 			   ah_attr->port_num,
1226 			   ah_attr->grh.sgid_index,
1227 			   &sgid, &sgid_attr);
1228 	if (ret != 0)
1229 		return (ret);
1230 	if (!sgid_attr.ndev)
1231 		return -ENXIO;
1232 
1233 	ret = rdma_addr_find_l2_eth_by_grh(&sgid,
1234 					   &ah_attr->grh.dgid,
1235 					   ah_attr->dmac,
1236 					   sgid_attr.ndev, &hop_limit);
1237 	dev_put(sgid_attr.ndev);
1238 
1239 	ah_attr->grh.hop_limit = hop_limit;
1240 	return ret;
1241 }
1242 EXPORT_SYMBOL(ib_resolve_eth_dmac);
1243 
1244 
ib_modify_qp(struct ib_qp * qp,struct ib_qp_attr * qp_attr,int qp_attr_mask)1245 int ib_modify_qp(struct ib_qp *qp,
1246 		 struct ib_qp_attr *qp_attr,
1247 		 int qp_attr_mask)
1248 {
1249 	if (qp_attr_mask & IB_QP_AV) {
1250 		int ret;
1251 
1252 		ret = ib_resolve_eth_dmac(qp->device, &qp_attr->ah_attr);
1253 		if (ret)
1254 			return ret;
1255 	}
1256 
1257 	return qp->device->modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
1258 }
1259 EXPORT_SYMBOL(ib_modify_qp);
1260 
ib_query_qp(struct ib_qp * qp,struct ib_qp_attr * qp_attr,int qp_attr_mask,struct ib_qp_init_attr * qp_init_attr)1261 int ib_query_qp(struct ib_qp *qp,
1262 		struct ib_qp_attr *qp_attr,
1263 		int qp_attr_mask,
1264 		struct ib_qp_init_attr *qp_init_attr)
1265 {
1266 	return qp->device->query_qp ?
1267 		qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
1268 		-ENOSYS;
1269 }
1270 EXPORT_SYMBOL(ib_query_qp);
1271 
ib_close_qp(struct ib_qp * qp)1272 int ib_close_qp(struct ib_qp *qp)
1273 {
1274 	struct ib_qp *real_qp;
1275 	unsigned long flags;
1276 
1277 	real_qp = qp->real_qp;
1278 	if (real_qp == qp)
1279 		return -EINVAL;
1280 
1281 	spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1282 	list_del(&qp->open_list);
1283 	spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1284 
1285 	atomic_dec(&real_qp->usecnt);
1286 	kfree(qp);
1287 
1288 	return 0;
1289 }
1290 EXPORT_SYMBOL(ib_close_qp);
1291 
__ib_destroy_shared_qp(struct ib_qp * qp)1292 static int __ib_destroy_shared_qp(struct ib_qp *qp)
1293 {
1294 	struct ib_xrcd *xrcd;
1295 	struct ib_qp *real_qp;
1296 	int ret;
1297 
1298 	real_qp = qp->real_qp;
1299 	xrcd = real_qp->xrcd;
1300 
1301 	mutex_lock(&xrcd->tgt_qp_mutex);
1302 	ib_close_qp(qp);
1303 	if (atomic_read(&real_qp->usecnt) == 0)
1304 		list_del(&real_qp->xrcd_list);
1305 	else
1306 		real_qp = NULL;
1307 	mutex_unlock(&xrcd->tgt_qp_mutex);
1308 
1309 	if (real_qp) {
1310 		ret = ib_destroy_qp(real_qp);
1311 		if (!ret)
1312 			atomic_dec(&xrcd->usecnt);
1313 		else
1314 			__ib_insert_xrcd_qp(xrcd, real_qp);
1315 	}
1316 
1317 	return 0;
1318 }
1319 
ib_destroy_qp(struct ib_qp * qp)1320 int ib_destroy_qp(struct ib_qp *qp)
1321 {
1322 	struct ib_pd *pd;
1323 	struct ib_cq *scq, *rcq;
1324 	struct ib_srq *srq;
1325 	struct ib_rwq_ind_table *ind_tbl;
1326 	int ret;
1327 
1328 	if (atomic_read(&qp->usecnt))
1329 		return -EBUSY;
1330 
1331 	if (qp->real_qp != qp)
1332 		return __ib_destroy_shared_qp(qp);
1333 
1334 	pd   = qp->pd;
1335 	scq  = qp->send_cq;
1336 	rcq  = qp->recv_cq;
1337 	srq  = qp->srq;
1338 	ind_tbl = qp->rwq_ind_tbl;
1339 
1340 	ret = qp->device->destroy_qp(qp);
1341 	if (!ret) {
1342 		if (pd)
1343 			atomic_dec(&pd->usecnt);
1344 		if (scq)
1345 			atomic_dec(&scq->usecnt);
1346 		if (rcq)
1347 			atomic_dec(&rcq->usecnt);
1348 		if (srq)
1349 			atomic_dec(&srq->usecnt);
1350 		if (ind_tbl)
1351 			atomic_dec(&ind_tbl->usecnt);
1352 	}
1353 
1354 	return ret;
1355 }
1356 EXPORT_SYMBOL(ib_destroy_qp);
1357 
1358 /* Completion queues */
1359 
ib_create_cq(struct ib_device * device,ib_comp_handler comp_handler,void (* event_handler)(struct ib_event *,void *),void * cq_context,const struct ib_cq_init_attr * cq_attr)1360 struct ib_cq *ib_create_cq(struct ib_device *device,
1361 			   ib_comp_handler comp_handler,
1362 			   void (*event_handler)(struct ib_event *, void *),
1363 			   void *cq_context,
1364 			   const struct ib_cq_init_attr *cq_attr)
1365 {
1366 	struct ib_cq *cq;
1367 
1368 	cq = device->create_cq(device, cq_attr, NULL, NULL);
1369 
1370 	if (!IS_ERR(cq)) {
1371 		cq->device        = device;
1372 		cq->uobject       = NULL;
1373 		cq->comp_handler  = comp_handler;
1374 		cq->event_handler = event_handler;
1375 		cq->cq_context    = cq_context;
1376 		atomic_set(&cq->usecnt, 0);
1377 	}
1378 
1379 	return cq;
1380 }
1381 EXPORT_SYMBOL(ib_create_cq);
1382 
ib_modify_cq(struct ib_cq * cq,u16 cq_count,u16 cq_period)1383 int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1384 {
1385 	return cq->device->modify_cq ?
1386 		cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS;
1387 }
1388 EXPORT_SYMBOL(ib_modify_cq);
1389 
ib_destroy_cq(struct ib_cq * cq)1390 int ib_destroy_cq(struct ib_cq *cq)
1391 {
1392 	if (atomic_read(&cq->usecnt))
1393 		return -EBUSY;
1394 
1395 	return cq->device->destroy_cq(cq);
1396 }
1397 EXPORT_SYMBOL(ib_destroy_cq);
1398 
ib_resize_cq(struct ib_cq * cq,int cqe)1399 int ib_resize_cq(struct ib_cq *cq, int cqe)
1400 {
1401 	return cq->device->resize_cq ?
1402 		cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS;
1403 }
1404 EXPORT_SYMBOL(ib_resize_cq);
1405 
1406 /* Memory regions */
1407 
ib_dereg_mr(struct ib_mr * mr)1408 int ib_dereg_mr(struct ib_mr *mr)
1409 {
1410 	struct ib_pd *pd = mr->pd;
1411 	int ret;
1412 
1413 	ret = mr->device->dereg_mr(mr);
1414 	if (!ret)
1415 		atomic_dec(&pd->usecnt);
1416 
1417 	return ret;
1418 }
1419 EXPORT_SYMBOL(ib_dereg_mr);
1420 
1421 /**
1422  * ib_alloc_mr() - Allocates a memory region
1423  * @pd:            protection domain associated with the region
1424  * @mr_type:       memory region type
1425  * @max_num_sg:    maximum sg entries available for registration.
1426  *
1427  * Notes:
1428  * Memory registeration page/sg lists must not exceed max_num_sg.
1429  * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
1430  * max_num_sg * used_page_size.
1431  *
1432  */
ib_alloc_mr(struct ib_pd * pd,enum ib_mr_type mr_type,u32 max_num_sg)1433 struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
1434 			  enum ib_mr_type mr_type,
1435 			  u32 max_num_sg)
1436 {
1437 	struct ib_mr *mr;
1438 
1439 	if (!pd->device->alloc_mr)
1440 		return ERR_PTR(-ENOSYS);
1441 
1442 	mr = pd->device->alloc_mr(pd, mr_type, max_num_sg);
1443 	if (!IS_ERR(mr)) {
1444 		mr->device  = pd->device;
1445 		mr->pd      = pd;
1446 		mr->uobject = NULL;
1447 		atomic_inc(&pd->usecnt);
1448 		mr->need_inval = false;
1449 	}
1450 
1451 	return mr;
1452 }
1453 EXPORT_SYMBOL(ib_alloc_mr);
1454 
1455 /* "Fast" memory regions */
1456 
ib_alloc_fmr(struct ib_pd * pd,int mr_access_flags,struct ib_fmr_attr * fmr_attr)1457 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
1458 			    int mr_access_flags,
1459 			    struct ib_fmr_attr *fmr_attr)
1460 {
1461 	struct ib_fmr *fmr;
1462 
1463 	if (!pd->device->alloc_fmr)
1464 		return ERR_PTR(-ENOSYS);
1465 
1466 	fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
1467 	if (!IS_ERR(fmr)) {
1468 		fmr->device = pd->device;
1469 		fmr->pd     = pd;
1470 		atomic_inc(&pd->usecnt);
1471 	}
1472 
1473 	return fmr;
1474 }
1475 EXPORT_SYMBOL(ib_alloc_fmr);
1476 
ib_unmap_fmr(struct list_head * fmr_list)1477 int ib_unmap_fmr(struct list_head *fmr_list)
1478 {
1479 	struct ib_fmr *fmr;
1480 
1481 	if (list_empty(fmr_list))
1482 		return 0;
1483 
1484 	fmr = list_entry(fmr_list->next, struct ib_fmr, list);
1485 	return fmr->device->unmap_fmr(fmr_list);
1486 }
1487 EXPORT_SYMBOL(ib_unmap_fmr);
1488 
ib_dealloc_fmr(struct ib_fmr * fmr)1489 int ib_dealloc_fmr(struct ib_fmr *fmr)
1490 {
1491 	struct ib_pd *pd;
1492 	int ret;
1493 
1494 	pd = fmr->pd;
1495 	ret = fmr->device->dealloc_fmr(fmr);
1496 	if (!ret)
1497 		atomic_dec(&pd->usecnt);
1498 
1499 	return ret;
1500 }
1501 EXPORT_SYMBOL(ib_dealloc_fmr);
1502 
1503 /* Multicast groups */
1504 
is_valid_mcast_lid(struct ib_qp * qp,u16 lid)1505 static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
1506 {
1507 	struct ib_qp_init_attr init_attr = {};
1508 	struct ib_qp_attr attr = {};
1509 	int num_eth_ports = 0;
1510 	int port;
1511 
1512 	/* If QP state >= init, it is assigned to a port and we can check this
1513 	 * port only.
1514 	 */
1515 	if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
1516 		if (attr.qp_state >= IB_QPS_INIT) {
1517 			if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
1518 			    IB_LINK_LAYER_INFINIBAND)
1519 				return true;
1520 			goto lid_check;
1521 		}
1522 	}
1523 
1524 	/* Can't get a quick answer, iterate over all ports */
1525 	for (port = 0; port < qp->device->phys_port_cnt; port++)
1526 		if (rdma_port_get_link_layer(qp->device, port) !=
1527 		    IB_LINK_LAYER_INFINIBAND)
1528 			num_eth_ports++;
1529 
1530 	/* If we have at lease one Ethernet port, RoCE annex declares that
1531 	 * multicast LID should be ignored. We can't tell at this step if the
1532 	 * QP belongs to an IB or Ethernet port.
1533 	 */
1534 	if (num_eth_ports)
1535 		return true;
1536 
1537 	/* If all the ports are IB, we can check according to IB spec. */
1538 lid_check:
1539 	return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
1540 		 lid == be16_to_cpu(IB_LID_PERMISSIVE));
1541 }
1542 
ib_attach_mcast(struct ib_qp * qp,union ib_gid * gid,u16 lid)1543 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1544 {
1545 	int ret;
1546 
1547 	if (!qp->device->attach_mcast)
1548 		return -ENOSYS;
1549 
1550 	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
1551 	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
1552 		return -EINVAL;
1553 
1554 	ret = qp->device->attach_mcast(qp, gid, lid);
1555 	if (!ret)
1556 		atomic_inc(&qp->usecnt);
1557 	return ret;
1558 }
1559 EXPORT_SYMBOL(ib_attach_mcast);
1560 
ib_detach_mcast(struct ib_qp * qp,union ib_gid * gid,u16 lid)1561 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
1562 {
1563 	int ret;
1564 
1565 	if (!qp->device->detach_mcast)
1566 		return -ENOSYS;
1567 
1568 	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
1569 	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
1570 		return -EINVAL;
1571 
1572 	ret = qp->device->detach_mcast(qp, gid, lid);
1573 	if (!ret)
1574 		atomic_dec(&qp->usecnt);
1575 	return ret;
1576 }
1577 EXPORT_SYMBOL(ib_detach_mcast);
1578 
ib_alloc_xrcd(struct ib_device * device)1579 struct ib_xrcd *ib_alloc_xrcd(struct ib_device *device)
1580 {
1581 	struct ib_xrcd *xrcd;
1582 
1583 	if (!device->alloc_xrcd)
1584 		return ERR_PTR(-ENOSYS);
1585 
1586 	xrcd = device->alloc_xrcd(device, NULL, NULL);
1587 	if (!IS_ERR(xrcd)) {
1588 		xrcd->device = device;
1589 		xrcd->inode = NULL;
1590 		atomic_set(&xrcd->usecnt, 0);
1591 		mutex_init(&xrcd->tgt_qp_mutex);
1592 		INIT_LIST_HEAD(&xrcd->tgt_qp_list);
1593 	}
1594 
1595 	return xrcd;
1596 }
1597 EXPORT_SYMBOL(ib_alloc_xrcd);
1598 
ib_dealloc_xrcd(struct ib_xrcd * xrcd)1599 int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1600 {
1601 	struct ib_qp *qp;
1602 	int ret;
1603 
1604 	if (atomic_read(&xrcd->usecnt))
1605 		return -EBUSY;
1606 
1607 	while (!list_empty(&xrcd->tgt_qp_list)) {
1608 		qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
1609 		ret = ib_destroy_qp(qp);
1610 		if (ret)
1611 			return ret;
1612 	}
1613 
1614 	return xrcd->device->dealloc_xrcd(xrcd);
1615 }
1616 EXPORT_SYMBOL(ib_dealloc_xrcd);
1617 
1618 /**
1619  * ib_create_wq - Creates a WQ associated with the specified protection
1620  * domain.
1621  * @pd: The protection domain associated with the WQ.
1622  * @wq_init_attr: A list of initial attributes required to create the
1623  * WQ. If WQ creation succeeds, then the attributes are updated to
1624  * the actual capabilities of the created WQ.
1625  *
1626  * wq_init_attr->max_wr and wq_init_attr->max_sge determine
1627  * the requested size of the WQ, and set to the actual values allocated
1628  * on return.
1629  * If ib_create_wq() succeeds, then max_wr and max_sge will always be
1630  * at least as large as the requested values.
1631  */
ib_create_wq(struct ib_pd * pd,struct ib_wq_init_attr * wq_attr)1632 struct ib_wq *ib_create_wq(struct ib_pd *pd,
1633 			   struct ib_wq_init_attr *wq_attr)
1634 {
1635 	struct ib_wq *wq;
1636 
1637 	if (!pd->device->create_wq)
1638 		return ERR_PTR(-ENOSYS);
1639 
1640 	wq = pd->device->create_wq(pd, wq_attr, NULL);
1641 	if (!IS_ERR(wq)) {
1642 		wq->event_handler = wq_attr->event_handler;
1643 		wq->wq_context = wq_attr->wq_context;
1644 		wq->wq_type = wq_attr->wq_type;
1645 		wq->cq = wq_attr->cq;
1646 		wq->device = pd->device;
1647 		wq->pd = pd;
1648 		wq->uobject = NULL;
1649 		atomic_inc(&pd->usecnt);
1650 		atomic_inc(&wq_attr->cq->usecnt);
1651 		atomic_set(&wq->usecnt, 0);
1652 	}
1653 	return wq;
1654 }
1655 EXPORT_SYMBOL(ib_create_wq);
1656 
1657 /**
1658  * ib_destroy_wq - Destroys the specified WQ.
1659  * @wq: The WQ to destroy.
1660  */
ib_destroy_wq(struct ib_wq * wq)1661 int ib_destroy_wq(struct ib_wq *wq)
1662 {
1663 	int err;
1664 	struct ib_cq *cq = wq->cq;
1665 	struct ib_pd *pd = wq->pd;
1666 
1667 	if (atomic_read(&wq->usecnt))
1668 		return -EBUSY;
1669 
1670 	err = wq->device->destroy_wq(wq);
1671 	if (!err) {
1672 		atomic_dec(&pd->usecnt);
1673 		atomic_dec(&cq->usecnt);
1674 	}
1675 	return err;
1676 }
1677 EXPORT_SYMBOL(ib_destroy_wq);
1678 
1679 /**
1680  * ib_modify_wq - Modifies the specified WQ.
1681  * @wq: The WQ to modify.
1682  * @wq_attr: On input, specifies the WQ attributes to modify.
1683  * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
1684  *   are being modified.
1685  * On output, the current values of selected WQ attributes are returned.
1686  */
ib_modify_wq(struct ib_wq * wq,struct ib_wq_attr * wq_attr,u32 wq_attr_mask)1687 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
1688 		 u32 wq_attr_mask)
1689 {
1690 	int err;
1691 
1692 	if (!wq->device->modify_wq)
1693 		return -ENOSYS;
1694 
1695 	err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
1696 	return err;
1697 }
1698 EXPORT_SYMBOL(ib_modify_wq);
1699 
1700 /*
1701  * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
1702  * @device: The device on which to create the rwq indirection table.
1703  * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
1704  * create the Indirection Table.
1705  *
1706  * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
1707  *	than the created ib_rwq_ind_table object and the caller is responsible
1708  *	for its memory allocation/free.
1709  */
ib_create_rwq_ind_table(struct ib_device * device,struct ib_rwq_ind_table_init_attr * init_attr)1710 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
1711 						 struct ib_rwq_ind_table_init_attr *init_attr)
1712 {
1713 	struct ib_rwq_ind_table *rwq_ind_table;
1714 	int i;
1715 	u32 table_size;
1716 
1717 	if (!device->create_rwq_ind_table)
1718 		return ERR_PTR(-ENOSYS);
1719 
1720 	table_size = (1 << init_attr->log_ind_tbl_size);
1721 	rwq_ind_table = device->create_rwq_ind_table(device,
1722 				init_attr, NULL);
1723 	if (IS_ERR(rwq_ind_table))
1724 		return rwq_ind_table;
1725 
1726 	rwq_ind_table->ind_tbl = init_attr->ind_tbl;
1727 	rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
1728 	rwq_ind_table->device = device;
1729 	rwq_ind_table->uobject = NULL;
1730 	atomic_set(&rwq_ind_table->usecnt, 0);
1731 
1732 	for (i = 0; i < table_size; i++)
1733 		atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
1734 
1735 	return rwq_ind_table;
1736 }
1737 EXPORT_SYMBOL(ib_create_rwq_ind_table);
1738 
1739 /*
1740  * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
1741  * @wq_ind_table: The Indirection Table to destroy.
1742 */
ib_destroy_rwq_ind_table(struct ib_rwq_ind_table * rwq_ind_table)1743 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
1744 {
1745 	int err, i;
1746 	u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
1747 	struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
1748 
1749 	if (atomic_read(&rwq_ind_table->usecnt))
1750 		return -EBUSY;
1751 
1752 	err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
1753 	if (!err) {
1754 		for (i = 0; i < table_size; i++)
1755 			atomic_dec(&ind_tbl[i]->usecnt);
1756 	}
1757 
1758 	return err;
1759 }
1760 EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
1761 
ib_create_flow(struct ib_qp * qp,struct ib_flow_attr * flow_attr,int domain)1762 struct ib_flow *ib_create_flow(struct ib_qp *qp,
1763 			       struct ib_flow_attr *flow_attr,
1764 			       int domain)
1765 {
1766 	struct ib_flow *flow_id;
1767 	if (!qp->device->create_flow)
1768 		return ERR_PTR(-ENOSYS);
1769 
1770 	flow_id = qp->device->create_flow(qp, flow_attr, domain);
1771 	if (!IS_ERR(flow_id))
1772 		atomic_inc(&qp->usecnt);
1773 	return flow_id;
1774 }
1775 EXPORT_SYMBOL(ib_create_flow);
1776 
ib_destroy_flow(struct ib_flow * flow_id)1777 int ib_destroy_flow(struct ib_flow *flow_id)
1778 {
1779 	int err;
1780 	struct ib_qp *qp = flow_id->qp;
1781 
1782 	err = qp->device->destroy_flow(flow_id);
1783 	if (!err)
1784 		atomic_dec(&qp->usecnt);
1785 	return err;
1786 }
1787 EXPORT_SYMBOL(ib_destroy_flow);
1788 
ib_check_mr_status(struct ib_mr * mr,u32 check_mask,struct ib_mr_status * mr_status)1789 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
1790 		       struct ib_mr_status *mr_status)
1791 {
1792 	return mr->device->check_mr_status ?
1793 		mr->device->check_mr_status(mr, check_mask, mr_status) : -ENOSYS;
1794 }
1795 EXPORT_SYMBOL(ib_check_mr_status);
1796 
ib_set_vf_link_state(struct ib_device * device,int vf,u8 port,int state)1797 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
1798 			 int state)
1799 {
1800 	if (!device->set_vf_link_state)
1801 		return -ENOSYS;
1802 
1803 	return device->set_vf_link_state(device, vf, port, state);
1804 }
1805 EXPORT_SYMBOL(ib_set_vf_link_state);
1806 
ib_get_vf_config(struct ib_device * device,int vf,u8 port,struct ifla_vf_info * info)1807 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
1808 		     struct ifla_vf_info *info)
1809 {
1810 	if (!device->get_vf_config)
1811 		return -ENOSYS;
1812 
1813 	return device->get_vf_config(device, vf, port, info);
1814 }
1815 EXPORT_SYMBOL(ib_get_vf_config);
1816 
ib_get_vf_stats(struct ib_device * device,int vf,u8 port,struct ifla_vf_stats * stats)1817 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
1818 		    struct ifla_vf_stats *stats)
1819 {
1820 	if (!device->get_vf_stats)
1821 		return -ENOSYS;
1822 
1823 	return device->get_vf_stats(device, vf, port, stats);
1824 }
1825 EXPORT_SYMBOL(ib_get_vf_stats);
1826 
ib_set_vf_guid(struct ib_device * device,int vf,u8 port,u64 guid,int type)1827 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
1828 		   int type)
1829 {
1830 	if (!device->set_vf_guid)
1831 		return -ENOSYS;
1832 
1833 	return device->set_vf_guid(device, vf, port, guid, type);
1834 }
1835 EXPORT_SYMBOL(ib_set_vf_guid);
1836 
1837 /**
1838  * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
1839  *     and set it the memory region.
1840  * @mr:            memory region
1841  * @sg:            dma mapped scatterlist
1842  * @sg_nents:      number of entries in sg
1843  * @sg_offset:     offset in bytes into sg
1844  * @page_size:     page vector desired page size
1845  *
1846  * Constraints:
1847  * - The first sg element is allowed to have an offset.
1848  * - Each sg element must either be aligned to page_size or virtually
1849  *   contiguous to the previous element. In case an sg element has a
1850  *   non-contiguous offset, the mapping prefix will not include it.
1851  * - The last sg element is allowed to have length less than page_size.
1852  * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
1853  *   then only max_num_sg entries will be mapped.
1854  * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
1855  *   constraints holds and the page_size argument is ignored.
1856  *
1857  * Returns the number of sg elements that were mapped to the memory region.
1858  *
1859  * After this completes successfully, the  memory region
1860  * is ready for registration.
1861  */
ib_map_mr_sg(struct ib_mr * mr,struct scatterlist * sg,int sg_nents,unsigned int * sg_offset,unsigned int page_size)1862 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
1863 		 unsigned int *sg_offset, unsigned int page_size)
1864 {
1865 	if (unlikely(!mr->device->map_mr_sg))
1866 		return -ENOSYS;
1867 
1868 	mr->page_size = page_size;
1869 
1870 	return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
1871 }
1872 EXPORT_SYMBOL(ib_map_mr_sg);
1873 
1874 /**
1875  * ib_sg_to_pages() - Convert the largest prefix of a sg list
1876  *     to a page vector
1877  * @mr:            memory region
1878  * @sgl:           dma mapped scatterlist
1879  * @sg_nents:      number of entries in sg
1880  * @sg_offset_p:   IN:  start offset in bytes into sg
1881  *                 OUT: offset in bytes for element n of the sg of the first
1882  *                      byte that has not been processed where n is the return
1883  *                      value of this function.
1884  * @set_page:      driver page assignment function pointer
1885  *
1886  * Core service helper for drivers to convert the largest
1887  * prefix of given sg list to a page vector. The sg list
1888  * prefix converted is the prefix that meet the requirements
1889  * of ib_map_mr_sg.
1890  *
1891  * Returns the number of sg elements that were assigned to
1892  * a page vector.
1893  */
ib_sg_to_pages(struct ib_mr * mr,struct scatterlist * sgl,int sg_nents,unsigned int * sg_offset_p,int (* set_page)(struct ib_mr *,u64))1894 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
1895 		unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
1896 {
1897 	struct scatterlist *sg;
1898 	u64 last_end_dma_addr = 0;
1899 	unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
1900 	unsigned int last_page_off = 0;
1901 	u64 page_mask = ~((u64)mr->page_size - 1);
1902 	int i, ret;
1903 
1904 	if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
1905 		return -EINVAL;
1906 
1907 	mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
1908 	mr->length = 0;
1909 
1910 	for_each_sg(sgl, sg, sg_nents, i) {
1911 		u64 dma_addr = sg_dma_address(sg) + sg_offset;
1912 		u64 prev_addr = dma_addr;
1913 		unsigned int dma_len = sg_dma_len(sg) - sg_offset;
1914 		u64 end_dma_addr = dma_addr + dma_len;
1915 		u64 page_addr = dma_addr & page_mask;
1916 
1917 		/*
1918 		 * For the second and later elements, check whether either the
1919 		 * end of element i-1 or the start of element i is not aligned
1920 		 * on a page boundary.
1921 		 */
1922 		if (i && (last_page_off != 0 || page_addr != dma_addr)) {
1923 			/* Stop mapping if there is a gap. */
1924 			if (last_end_dma_addr != dma_addr)
1925 				break;
1926 
1927 			/*
1928 			 * Coalesce this element with the last. If it is small
1929 			 * enough just update mr->length. Otherwise start
1930 			 * mapping from the next page.
1931 			 */
1932 			goto next_page;
1933 		}
1934 
1935 		do {
1936 			ret = set_page(mr, page_addr);
1937 			if (unlikely(ret < 0)) {
1938 				sg_offset = prev_addr - sg_dma_address(sg);
1939 				mr->length += prev_addr - dma_addr;
1940 				if (sg_offset_p)
1941 					*sg_offset_p = sg_offset;
1942 				return i || sg_offset ? i : ret;
1943 			}
1944 			prev_addr = page_addr;
1945 next_page:
1946 			page_addr += mr->page_size;
1947 		} while (page_addr < end_dma_addr);
1948 
1949 		mr->length += dma_len;
1950 		last_end_dma_addr = end_dma_addr;
1951 		last_page_off = end_dma_addr & ~page_mask;
1952 
1953 		sg_offset = 0;
1954 	}
1955 
1956 	if (sg_offset_p)
1957 		*sg_offset_p = 0;
1958 	return i;
1959 }
1960 EXPORT_SYMBOL(ib_sg_to_pages);
1961 
1962 struct ib_drain_cqe {
1963 	struct ib_cqe cqe;
1964 	struct completion done;
1965 };
1966 
ib_drain_qp_done(struct ib_cq * cq,struct ib_wc * wc)1967 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
1968 {
1969 	struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
1970 						cqe);
1971 
1972 	complete(&cqe->done);
1973 }
1974 
1975 /*
1976  * Post a WR and block until its completion is reaped for the SQ.
1977  */
__ib_drain_sq(struct ib_qp * qp)1978 static void __ib_drain_sq(struct ib_qp *qp)
1979 {
1980 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
1981 	struct ib_drain_cqe sdrain;
1982 	const struct ib_send_wr *bad_swr;
1983 	struct ib_rdma_wr swr = {
1984 		.wr = {
1985 			.opcode	= IB_WR_RDMA_WRITE,
1986 			.wr_cqe	= &sdrain.cqe,
1987 		},
1988 	};
1989 	int ret;
1990 
1991 	if (qp->send_cq->poll_ctx == IB_POLL_DIRECT) {
1992 		WARN_ONCE(qp->send_cq->poll_ctx == IB_POLL_DIRECT,
1993 			  "IB_POLL_DIRECT poll_ctx not supported for drain\n");
1994 		return;
1995 	}
1996 
1997 	sdrain.cqe.done = ib_drain_qp_done;
1998 	init_completion(&sdrain.done);
1999 
2000 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2001 	if (ret) {
2002 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2003 		return;
2004 	}
2005 
2006 	ret = ib_post_send(qp, &swr.wr, &bad_swr);
2007 	if (ret) {
2008 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2009 		return;
2010 	}
2011 
2012 	wait_for_completion(&sdrain.done);
2013 }
2014 
2015 /*
2016  * Post a WR and block until its completion is reaped for the RQ.
2017  */
__ib_drain_rq(struct ib_qp * qp)2018 static void __ib_drain_rq(struct ib_qp *qp)
2019 {
2020 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2021 	struct ib_drain_cqe rdrain;
2022 	struct ib_recv_wr rwr = {};
2023 	const struct ib_recv_wr *bad_rwr;
2024 	int ret;
2025 
2026 	if (qp->recv_cq->poll_ctx == IB_POLL_DIRECT) {
2027 		WARN_ONCE(qp->recv_cq->poll_ctx == IB_POLL_DIRECT,
2028 			  "IB_POLL_DIRECT poll_ctx not supported for drain\n");
2029 		return;
2030 	}
2031 
2032 	rwr.wr_cqe = &rdrain.cqe;
2033 	rdrain.cqe.done = ib_drain_qp_done;
2034 	init_completion(&rdrain.done);
2035 
2036 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2037 	if (ret) {
2038 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2039 		return;
2040 	}
2041 
2042 	ret = ib_post_recv(qp, &rwr, &bad_rwr);
2043 	if (ret) {
2044 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2045 		return;
2046 	}
2047 
2048 	wait_for_completion(&rdrain.done);
2049 }
2050 
2051 /**
2052  * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2053  *		   application.
2054  * @qp:            queue pair to drain
2055  *
2056  * If the device has a provider-specific drain function, then
2057  * call that.  Otherwise call the generic drain function
2058  * __ib_drain_sq().
2059  *
2060  * The caller must:
2061  *
2062  * ensure there is room in the CQ and SQ for the drain work request and
2063  * completion.
2064  *
2065  * allocate the CQ using ib_alloc_cq() and the CQ poll context cannot be
2066  * IB_POLL_DIRECT.
2067  *
2068  * ensure that there are no other contexts that are posting WRs concurrently.
2069  * Otherwise the drain is not guaranteed.
2070  */
ib_drain_sq(struct ib_qp * qp)2071 void ib_drain_sq(struct ib_qp *qp)
2072 {
2073 	if (qp->device->drain_sq)
2074 		qp->device->drain_sq(qp);
2075 	else
2076 		__ib_drain_sq(qp);
2077 }
2078 EXPORT_SYMBOL(ib_drain_sq);
2079 
2080 /**
2081  * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2082  *		   application.
2083  * @qp:            queue pair to drain
2084  *
2085  * If the device has a provider-specific drain function, then
2086  * call that.  Otherwise call the generic drain function
2087  * __ib_drain_rq().
2088  *
2089  * The caller must:
2090  *
2091  * ensure there is room in the CQ and RQ for the drain work request and
2092  * completion.
2093  *
2094  * allocate the CQ using ib_alloc_cq() and the CQ poll context cannot be
2095  * IB_POLL_DIRECT.
2096  *
2097  * ensure that there are no other contexts that are posting WRs concurrently.
2098  * Otherwise the drain is not guaranteed.
2099  */
ib_drain_rq(struct ib_qp * qp)2100 void ib_drain_rq(struct ib_qp *qp)
2101 {
2102 	if (qp->device->drain_rq)
2103 		qp->device->drain_rq(qp);
2104 	else
2105 		__ib_drain_rq(qp);
2106 }
2107 EXPORT_SYMBOL(ib_drain_rq);
2108 
2109 /**
2110  * ib_drain_qp() - Block until all CQEs have been consumed by the
2111  *		   application on both the RQ and SQ.
2112  * @qp:            queue pair to drain
2113  *
2114  * The caller must:
2115  *
2116  * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2117  * and completions.
2118  *
2119  * allocate the CQs using ib_alloc_cq() and the CQ poll context cannot be
2120  * IB_POLL_DIRECT.
2121  *
2122  * ensure that there are no other contexts that are posting WRs concurrently.
2123  * Otherwise the drain is not guaranteed.
2124  */
ib_drain_qp(struct ib_qp * qp)2125 void ib_drain_qp(struct ib_qp *qp)
2126 {
2127 	ib_drain_sq(qp);
2128 	if (!qp->srq)
2129 		ib_drain_rq(qp);
2130 }
2131 EXPORT_SYMBOL(ib_drain_qp);
2132