xref: /freebsd/sys/ofed/drivers/infiniband/core/ib_verbs.c (revision ea44c37d0bd15e796c85f84b57c211ac220c550c)
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 #include <linux/wait.h>
49 
50 #include <rdma/ib_verbs.h>
51 #include <rdma/ib_cache.h>
52 #include <rdma/ib_addr.h>
53 
54 #include <netinet/ip.h>
55 #include <netinet/ip6.h>
56 
57 #include <machine/in_cksum.h>
58 
59 #include "core_priv.h"
60 
61 static int ib_resolve_eth_dmac(struct ib_device *device,
62 			       struct rdma_ah_attr *ah_attr);
63 
64 static const char * const ib_events[] = {
65 	[IB_EVENT_CQ_ERR]		= "CQ error",
66 	[IB_EVENT_QP_FATAL]		= "QP fatal error",
67 	[IB_EVENT_QP_REQ_ERR]		= "QP request error",
68 	[IB_EVENT_QP_ACCESS_ERR]	= "QP access error",
69 	[IB_EVENT_COMM_EST]		= "communication established",
70 	[IB_EVENT_SQ_DRAINED]		= "send queue drained",
71 	[IB_EVENT_PATH_MIG]		= "path migration successful",
72 	[IB_EVENT_PATH_MIG_ERR]		= "path migration error",
73 	[IB_EVENT_DEVICE_FATAL]		= "device fatal error",
74 	[IB_EVENT_PORT_ACTIVE]		= "port active",
75 	[IB_EVENT_PORT_ERR]		= "port error",
76 	[IB_EVENT_LID_CHANGE]		= "LID change",
77 	[IB_EVENT_PKEY_CHANGE]		= "P_key change",
78 	[IB_EVENT_SM_CHANGE]		= "SM change",
79 	[IB_EVENT_SRQ_ERR]		= "SRQ error",
80 	[IB_EVENT_SRQ_LIMIT_REACHED]	= "SRQ limit reached",
81 	[IB_EVENT_QP_LAST_WQE_REACHED]	= "last WQE reached",
82 	[IB_EVENT_CLIENT_REREGISTER]	= "client reregister",
83 	[IB_EVENT_GID_CHANGE]		= "GID changed",
84 };
85 
ib_event_msg(enum ib_event_type event)86 const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
87 {
88 	size_t index = event;
89 
90 	return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
91 			ib_events[index] : "unrecognized event";
92 }
93 EXPORT_SYMBOL(ib_event_msg);
94 
95 static const char * const wc_statuses[] = {
96 	[IB_WC_SUCCESS]			= "success",
97 	[IB_WC_LOC_LEN_ERR]		= "local length error",
98 	[IB_WC_LOC_QP_OP_ERR]		= "local QP operation error",
99 	[IB_WC_LOC_EEC_OP_ERR]		= "local EE context operation error",
100 	[IB_WC_LOC_PROT_ERR]		= "local protection error",
101 	[IB_WC_WR_FLUSH_ERR]		= "WR flushed",
102 	[IB_WC_MW_BIND_ERR]		= "memory management operation error",
103 	[IB_WC_BAD_RESP_ERR]		= "bad response error",
104 	[IB_WC_LOC_ACCESS_ERR]		= "local access error",
105 	[IB_WC_REM_INV_REQ_ERR]		= "invalid request error",
106 	[IB_WC_REM_ACCESS_ERR]		= "remote access error",
107 	[IB_WC_REM_OP_ERR]		= "remote operation error",
108 	[IB_WC_RETRY_EXC_ERR]		= "transport retry counter exceeded",
109 	[IB_WC_RNR_RETRY_EXC_ERR]	= "RNR retry counter exceeded",
110 	[IB_WC_LOC_RDD_VIOL_ERR]	= "local RDD violation error",
111 	[IB_WC_REM_INV_RD_REQ_ERR]	= "remote invalid RD request",
112 	[IB_WC_REM_ABORT_ERR]		= "operation aborted",
113 	[IB_WC_INV_EECN_ERR]		= "invalid EE context number",
114 	[IB_WC_INV_EEC_STATE_ERR]	= "invalid EE context state",
115 	[IB_WC_FATAL_ERR]		= "fatal error",
116 	[IB_WC_RESP_TIMEOUT_ERR]	= "response timeout error",
117 	[IB_WC_GENERAL_ERR]		= "general error",
118 };
119 
ib_wc_status_msg(enum ib_wc_status status)120 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
121 {
122 	size_t index = status;
123 
124 	return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
125 			wc_statuses[index] : "unrecognized status";
126 }
127 EXPORT_SYMBOL(ib_wc_status_msg);
128 
ib_rate_to_mult(enum ib_rate rate)129 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
130 {
131 	switch (rate) {
132 	case IB_RATE_2_5_GBPS: return   1;
133 	case IB_RATE_5_GBPS:   return   2;
134 	case IB_RATE_10_GBPS:  return   4;
135 	case IB_RATE_20_GBPS:  return   8;
136 	case IB_RATE_30_GBPS:  return  12;
137 	case IB_RATE_40_GBPS:  return  16;
138 	case IB_RATE_60_GBPS:  return  24;
139 	case IB_RATE_80_GBPS:  return  32;
140 	case IB_RATE_120_GBPS: return  48;
141 	case IB_RATE_14_GBPS:  return   6;
142 	case IB_RATE_56_GBPS:  return  22;
143 	case IB_RATE_112_GBPS: return  45;
144 	case IB_RATE_168_GBPS: return  67;
145 	case IB_RATE_25_GBPS:  return  10;
146 	case IB_RATE_100_GBPS: return  40;
147 	case IB_RATE_200_GBPS: return  80;
148 	case IB_RATE_300_GBPS: return 120;
149 	case IB_RATE_28_GBPS:  return  11;
150 	case IB_RATE_50_GBPS:  return  20;
151 	case IB_RATE_400_GBPS: return 160;
152 	case IB_RATE_600_GBPS: return 240;
153 	default:	       return  -1;
154 	}
155 }
156 EXPORT_SYMBOL(ib_rate_to_mult);
157 
mult_to_ib_rate(int mult)158 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
159 {
160 	switch (mult) {
161 	case 1:   return IB_RATE_2_5_GBPS;
162 	case 2:   return IB_RATE_5_GBPS;
163 	case 4:   return IB_RATE_10_GBPS;
164 	case 8:   return IB_RATE_20_GBPS;
165 	case 12:  return IB_RATE_30_GBPS;
166 	case 16:  return IB_RATE_40_GBPS;
167 	case 24:  return IB_RATE_60_GBPS;
168 	case 32:  return IB_RATE_80_GBPS;
169 	case 48:  return IB_RATE_120_GBPS;
170 	case 6:   return IB_RATE_14_GBPS;
171 	case 22:  return IB_RATE_56_GBPS;
172 	case 45:  return IB_RATE_112_GBPS;
173 	case 67:  return IB_RATE_168_GBPS;
174 	case 10:  return IB_RATE_25_GBPS;
175 	case 40:  return IB_RATE_100_GBPS;
176 	case 80:  return IB_RATE_200_GBPS;
177 	case 120: return IB_RATE_300_GBPS;
178 	case 11:  return IB_RATE_28_GBPS;
179 	case 20:  return IB_RATE_50_GBPS;
180 	case 160: return IB_RATE_400_GBPS;
181 	case 240: return IB_RATE_600_GBPS;
182 	default:  return IB_RATE_PORT_CURRENT;
183 	}
184 }
185 EXPORT_SYMBOL(mult_to_ib_rate);
186 
ib_rate_to_mbps(enum ib_rate rate)187 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
188 {
189 	switch (rate) {
190 	case IB_RATE_2_5_GBPS: return 2500;
191 	case IB_RATE_5_GBPS:   return 5000;
192 	case IB_RATE_10_GBPS:  return 10000;
193 	case IB_RATE_20_GBPS:  return 20000;
194 	case IB_RATE_30_GBPS:  return 30000;
195 	case IB_RATE_40_GBPS:  return 40000;
196 	case IB_RATE_60_GBPS:  return 60000;
197 	case IB_RATE_80_GBPS:  return 80000;
198 	case IB_RATE_120_GBPS: return 120000;
199 	case IB_RATE_14_GBPS:  return 14062;
200 	case IB_RATE_56_GBPS:  return 56250;
201 	case IB_RATE_112_GBPS: return 112500;
202 	case IB_RATE_168_GBPS: return 168750;
203 	case IB_RATE_25_GBPS:  return 25781;
204 	case IB_RATE_100_GBPS: return 103125;
205 	case IB_RATE_200_GBPS: return 206250;
206 	case IB_RATE_300_GBPS: return 309375;
207 	case IB_RATE_28_GBPS:  return 28125;
208 	case IB_RATE_50_GBPS:  return 53125;
209 	case IB_RATE_400_GBPS: return 425000;
210 	case IB_RATE_600_GBPS: return 637500;
211 	default:	       return -1;
212 	}
213 }
214 EXPORT_SYMBOL(ib_rate_to_mbps);
215 
216 __attribute_const__ enum rdma_transport_type
rdma_node_get_transport(enum rdma_node_type node_type)217 rdma_node_get_transport(enum rdma_node_type node_type)
218 {
219 	switch (node_type) {
220 	case RDMA_NODE_IB_CA:
221 	case RDMA_NODE_IB_SWITCH:
222 	case RDMA_NODE_IB_ROUTER:
223 		return RDMA_TRANSPORT_IB;
224 	case RDMA_NODE_RNIC:
225 		return RDMA_TRANSPORT_IWARP;
226 	case RDMA_NODE_USNIC:
227 		return RDMA_TRANSPORT_USNIC;
228 	case RDMA_NODE_USNIC_UDP:
229 		return RDMA_TRANSPORT_USNIC_UDP;
230 	default:
231 		BUG();
232 		return 0;
233 	}
234 }
235 EXPORT_SYMBOL(rdma_node_get_transport);
236 
rdma_port_get_link_layer(struct ib_device * device,u8 port_num)237 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
238 {
239 	if (device->get_link_layer)
240 		return device->get_link_layer(device, port_num);
241 
242 	switch (rdma_node_get_transport(device->node_type)) {
243 	case RDMA_TRANSPORT_IB:
244 		return IB_LINK_LAYER_INFINIBAND;
245 	case RDMA_TRANSPORT_IWARP:
246 	case RDMA_TRANSPORT_USNIC:
247 	case RDMA_TRANSPORT_USNIC_UDP:
248 		return IB_LINK_LAYER_ETHERNET;
249 	default:
250 		return IB_LINK_LAYER_UNSPECIFIED;
251 	}
252 }
253 EXPORT_SYMBOL(rdma_port_get_link_layer);
254 
255 /* Protection domains */
256 
257 /**
258  * ib_alloc_pd - Allocates an unused protection domain.
259  * @device: The device on which to allocate the protection domain.
260  *
261  * A protection domain object provides an association between QPs, shared
262  * receive queues, address handles, memory regions, and memory windows.
263  *
264  * Every PD has a local_dma_lkey which can be used as the lkey value for local
265  * memory operations.
266  */
__ib_alloc_pd(struct ib_device * device,unsigned int flags,const char * caller)267 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
268 		const char *caller)
269 {
270 	struct ib_pd *pd;
271 	int mr_access_flags = 0;
272 	int ret;
273 
274 	pd = rdma_zalloc_drv_obj(device, ib_pd);
275 	if (!pd)
276 		return ERR_PTR(-ENOMEM);
277 
278 	pd->device = device;
279 	pd->uobject = NULL;
280 	pd->__internal_mr = NULL;
281 	atomic_set(&pd->usecnt, 0);
282 	pd->flags = flags;
283 
284 	ret = device->alloc_pd(pd, NULL);
285 	if (ret) {
286 		kfree(pd);
287 		return ERR_PTR(ret);
288 	}
289 
290 	if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
291 		pd->local_dma_lkey = device->local_dma_lkey;
292 	else
293 		mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
294 
295 	if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
296 		pr_warn("%s: enabling unsafe global rkey\n", caller);
297 		mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
298 	}
299 
300 	if (mr_access_flags) {
301 		struct ib_mr *mr;
302 
303 		mr = pd->device->get_dma_mr(pd, mr_access_flags);
304 		if (IS_ERR(mr)) {
305 			ib_dealloc_pd(pd);
306 			return ERR_CAST(mr);
307 		}
308 
309 		mr->device	= pd->device;
310 		mr->pd		= pd;
311 		mr->type        = IB_MR_TYPE_DMA;
312 		mr->uobject	= NULL;
313 		mr->need_inval	= false;
314 
315 		pd->__internal_mr = mr;
316 
317 		if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
318 			pd->local_dma_lkey = pd->__internal_mr->lkey;
319 
320 		if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
321 			pd->unsafe_global_rkey = pd->__internal_mr->rkey;
322 	}
323 
324 	return pd;
325 }
326 EXPORT_SYMBOL(__ib_alloc_pd);
327 
328 /**
329  * ib_dealloc_pd_user - Deallocates a protection domain.
330  * @pd: The protection domain to deallocate.
331  * @udata: Valid user data or NULL for kernel object
332  *
333  * It is an error to call this function while any resources in the pd still
334  * exist.  The caller is responsible to synchronously destroy them and
335  * guarantee no new allocations will happen.
336  */
ib_dealloc_pd_user(struct ib_pd * pd,struct ib_udata * udata)337 void ib_dealloc_pd_user(struct ib_pd *pd, struct ib_udata *udata)
338 {
339 	int ret;
340 
341 	if (pd->__internal_mr) {
342 		ret = pd->device->dereg_mr(pd->__internal_mr, NULL);
343 		WARN_ON(ret);
344 		pd->__internal_mr = NULL;
345 	}
346 
347 	/* uverbs manipulates usecnt with proper locking, while the kabi
348 	   requires the caller to guarantee we can't race here. */
349 	WARN_ON(atomic_read(&pd->usecnt));
350 
351 	pd->device->dealloc_pd(pd, udata);
352 	kfree(pd);
353 }
354 EXPORT_SYMBOL(ib_dealloc_pd_user);
355 
356 /* Address handles */
357 
358 /**
359  * rdma_copy_ah_attr - Copy rdma ah attribute from source to destination.
360  * @dest:       Pointer to destination ah_attr. Contents of the destination
361  *              pointer is assumed to be invalid and attribute are overwritten.
362  * @src:        Pointer to source ah_attr.
363  */
rdma_copy_ah_attr(struct rdma_ah_attr * dest,const struct rdma_ah_attr * src)364 void rdma_copy_ah_attr(struct rdma_ah_attr *dest,
365 		       const struct rdma_ah_attr *src)
366 {
367 	*dest = *src;
368 	if (dest->grh.sgid_attr)
369 		rdma_hold_gid_attr(dest->grh.sgid_attr);
370 }
371 EXPORT_SYMBOL(rdma_copy_ah_attr);
372 
373 /**
374  * rdma_replace_ah_attr - Replace valid ah_attr with new new one.
375  * @old:        Pointer to existing ah_attr which needs to be replaced.
376  *              old is assumed to be valid or zero'd
377  * @new:        Pointer to the new ah_attr.
378  *
379  * rdma_replace_ah_attr() first releases any reference in the old ah_attr if
380  * old the ah_attr is valid; after that it copies the new attribute and holds
381  * the reference to the replaced ah_attr.
382  */
rdma_replace_ah_attr(struct rdma_ah_attr * old,const struct rdma_ah_attr * new)383 void rdma_replace_ah_attr(struct rdma_ah_attr *old,
384 			  const struct rdma_ah_attr *new)
385 {
386 	rdma_destroy_ah_attr(old);
387 	*old = *new;
388 	if (old->grh.sgid_attr)
389 		rdma_hold_gid_attr(old->grh.sgid_attr);
390 }
391 EXPORT_SYMBOL(rdma_replace_ah_attr);
392 
393 /**
394  * rdma_move_ah_attr - Move ah_attr pointed by source to destination.
395  * @dest:       Pointer to destination ah_attr to copy to.
396  *              dest is assumed to be valid or zero'd
397  * @src:        Pointer to the new ah_attr.
398  *
399  * rdma_move_ah_attr() first releases any reference in the destination ah_attr
400  * if it is valid. This also transfers ownership of internal references from
401  * src to dest, making src invalid in the process. No new reference of the src
402  * ah_attr is taken.
403  */
rdma_move_ah_attr(struct rdma_ah_attr * dest,struct rdma_ah_attr * src)404 void rdma_move_ah_attr(struct rdma_ah_attr *dest, struct rdma_ah_attr *src)
405 {
406 	rdma_destroy_ah_attr(dest);
407 	*dest = *src;
408 	src->grh.sgid_attr = NULL;
409 }
410 EXPORT_SYMBOL(rdma_move_ah_attr);
411 
412 /*
413  * Validate that the rdma_ah_attr is valid for the device before passing it
414  * off to the driver.
415  */
rdma_check_ah_attr(struct ib_device * device,struct rdma_ah_attr * ah_attr)416 static int rdma_check_ah_attr(struct ib_device *device,
417 			      struct rdma_ah_attr *ah_attr)
418 {
419 	if (!rdma_is_port_valid(device, ah_attr->port_num))
420 		return -EINVAL;
421 
422 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE &&
423 	    !(ah_attr->ah_flags & IB_AH_GRH))
424 		return -EINVAL;
425 
426 	if (ah_attr->grh.sgid_attr) {
427 		/*
428 		 * Make sure the passed sgid_attr is consistent with the
429 		 * parameters
430 		 */
431 		if (ah_attr->grh.sgid_attr->index != ah_attr->grh.sgid_index ||
432 		    ah_attr->grh.sgid_attr->port_num != ah_attr->port_num)
433 			return -EINVAL;
434 	}
435 	return 0;
436 }
437 
438 /*
439  * If the ah requires a GRH then ensure that sgid_attr pointer is filled in.
440  * On success the caller is responsible to call rdma_unfill_sgid_attr().
441  */
rdma_fill_sgid_attr(struct ib_device * device,struct rdma_ah_attr * ah_attr,const struct ib_gid_attr ** old_sgid_attr)442 static int rdma_fill_sgid_attr(struct ib_device *device,
443 			       struct rdma_ah_attr *ah_attr,
444 			       const struct ib_gid_attr **old_sgid_attr)
445 {
446 	const struct ib_gid_attr *sgid_attr;
447 	struct ib_global_route *grh;
448 	int ret;
449 
450 	*old_sgid_attr = ah_attr->grh.sgid_attr;
451 
452 	ret = rdma_check_ah_attr(device, ah_attr);
453 	if (ret)
454 		return ret;
455 
456 	if (!(ah_attr->ah_flags & IB_AH_GRH))
457 		return 0;
458 
459 	grh = rdma_ah_retrieve_grh(ah_attr);
460 	if (grh->sgid_attr)
461 		return 0;
462 
463 	sgid_attr =
464 		rdma_get_gid_attr(device, ah_attr->port_num, grh->sgid_index);
465 	if (IS_ERR(sgid_attr))
466 		return PTR_ERR(sgid_attr);
467 
468 	/* Move ownerhip of the kref into the ah_attr */
469 	grh->sgid_attr = sgid_attr;
470 	return 0;
471 }
472 
rdma_unfill_sgid_attr(struct rdma_ah_attr * ah_attr,const struct ib_gid_attr * old_sgid_attr)473 static void rdma_unfill_sgid_attr(struct rdma_ah_attr *ah_attr,
474 				  const struct ib_gid_attr *old_sgid_attr)
475 {
476 	/*
477 	 * Fill didn't change anything, the caller retains ownership of
478 	 * whatever it passed
479 	 */
480 	if (ah_attr->grh.sgid_attr == old_sgid_attr)
481 		return;
482 
483 	/*
484 	 * Otherwise, we need to undo what rdma_fill_sgid_attr so the caller
485 	 * doesn't see any change in the rdma_ah_attr. If we get here
486 	 * old_sgid_attr is NULL.
487 	 */
488 	rdma_destroy_ah_attr(ah_attr);
489 }
490 
491 static const struct ib_gid_attr *
rdma_update_sgid_attr(struct rdma_ah_attr * ah_attr,const struct ib_gid_attr * old_attr)492 rdma_update_sgid_attr(struct rdma_ah_attr *ah_attr,
493 		      const struct ib_gid_attr *old_attr)
494 {
495 	if (old_attr)
496 		rdma_put_gid_attr(old_attr);
497 	if (ah_attr->ah_flags & IB_AH_GRH) {
498 		rdma_hold_gid_attr(ah_attr->grh.sgid_attr);
499 		return ah_attr->grh.sgid_attr;
500 	}
501 	return NULL;
502 }
503 
_rdma_create_ah(struct ib_pd * pd,struct rdma_ah_attr * ah_attr,u32 flags,struct ib_udata * udata)504 static struct ib_ah *_rdma_create_ah(struct ib_pd *pd,
505 				     struct rdma_ah_attr *ah_attr,
506 				     u32 flags,
507 				     struct ib_udata *udata)
508 {
509 	struct ib_device *device = pd->device;
510 	struct ib_ah *ah;
511 	int ret;
512 
513 	might_sleep_if(flags & RDMA_CREATE_AH_SLEEPABLE);
514 
515 	if (!device->create_ah)
516 		return ERR_PTR(-EOPNOTSUPP);
517 
518 	ah = rdma_zalloc_drv_obj_gfp(
519 		device, ib_ah,
520 		(flags & RDMA_CREATE_AH_SLEEPABLE) ? GFP_KERNEL : GFP_ATOMIC);
521 	if (!ah)
522 		return ERR_PTR(-ENOMEM);
523 
524 	ah->device = device;
525 	ah->pd = pd;
526 	ah->type = ah_attr->type;
527 	ah->sgid_attr = rdma_update_sgid_attr(ah_attr, NULL);
528 
529 	ret = device->create_ah(ah, ah_attr, flags, udata);
530 	if (ret) {
531 		kfree(ah);
532 		return ERR_PTR(ret);
533 	}
534 
535 	atomic_inc(&pd->usecnt);
536 	return ah;
537 }
538 
539 /**
540  * rdma_create_ah - Creates an address handle for the
541  * given address vector.
542  * @pd: The protection domain associated with the address handle.
543  * @ah_attr: The attributes of the address vector.
544  * @flags: Create address handle flags (see enum rdma_create_ah_flags).
545  *
546  * It returns 0 on success and returns appropriate error code on error.
547  * The address handle is used to reference a local or global destination
548  * in all UD QP post sends.
549  */
rdma_create_ah(struct ib_pd * pd,struct rdma_ah_attr * ah_attr,u32 flags)550 struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
551 			     u32 flags)
552 {
553 	const struct ib_gid_attr *old_sgid_attr;
554 	struct ib_ah *ah;
555 	int ret;
556 
557 	ret = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
558 	if (ret)
559 		return ERR_PTR(ret);
560 
561 	ah = _rdma_create_ah(pd, ah_attr, flags, NULL);
562 
563 	rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
564 	return ah;
565 }
566 EXPORT_SYMBOL(rdma_create_ah);
567 
568 /**
569  * rdma_create_user_ah - Creates an address handle for the
570  * given address vector.
571  * It resolves destination mac address for ah attribute of RoCE type.
572  * @pd: The protection domain associated with the address handle.
573  * @ah_attr: The attributes of the address vector.
574  * @udata: pointer to user's input output buffer information need by
575  *         provider driver.
576  *
577  * It returns a valid address handle pointer on success and
578  * returns appropriate error code on error.
579  * The address handle is used to reference a local or global destination
580  * in all UD QP post sends.
581  */
rdma_create_user_ah(struct ib_pd * pd,struct rdma_ah_attr * ah_attr,struct ib_udata * udata)582 struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
583 				  struct rdma_ah_attr *ah_attr,
584 				  struct ib_udata *udata)
585 {
586 	const struct ib_gid_attr *old_sgid_attr;
587 	struct ib_ah *ah;
588 	int err;
589 
590 	err = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
591 	if (err)
592 		return ERR_PTR(err);
593 
594 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
595 		err = ib_resolve_eth_dmac(pd->device, ah_attr);
596 		if (err) {
597 			ah = ERR_PTR(err);
598 			goto out;
599 		}
600 	}
601 
602 	ah = _rdma_create_ah(pd, ah_attr, RDMA_CREATE_AH_SLEEPABLE, udata);
603 
604 out:
605 	rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
606 	return ah;
607 }
608 EXPORT_SYMBOL(ib_create_user_ah);
609 
ib_get_rdma_header_version(const union rdma_network_hdr * hdr)610 int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
611 {
612 	const struct ip *ip4h = (const struct ip *)&hdr->roce4grh;
613 	struct ip ip4h_checked;
614 	const struct ip6_hdr *ip6h = (const struct ip6_hdr *)&hdr->ibgrh;
615 
616 	/* If it's IPv6, the version must be 6, otherwise, the first
617 	 * 20 bytes (before the IPv4 header) are garbled.
618 	 */
619 	if ((ip6h->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
620 		return (ip4h->ip_v == 4) ? 4 : 0;
621 	/* version may be 6 or 4 because the first 20 bytes could be garbled */
622 
623 	/* RoCE v2 requires no options, thus header length
624 	 * must be 5 words
625 	 */
626 	if (ip4h->ip_hl != 5)
627 		return 6;
628 
629 	/* Verify checksum.
630 	 * We can't write on scattered buffers so we need to copy to
631 	 * temp buffer.
632 	 */
633 	memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
634 	ip4h_checked.ip_sum = 0;
635 #if defined(INET) || defined(INET6)
636 	ip4h_checked.ip_sum = in_cksum_hdr(&ip4h_checked);
637 #endif
638 	/* if IPv4 header checksum is OK, believe it */
639 	if (ip4h->ip_sum == ip4h_checked.ip_sum)
640 		return 4;
641 	return 6;
642 }
643 EXPORT_SYMBOL(ib_get_rdma_header_version);
644 
ib_get_net_type_by_grh(struct ib_device * device,u8 port_num,const struct ib_grh * grh)645 static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
646 						     u8 port_num,
647 						     const struct ib_grh *grh)
648 {
649 	int grh_version;
650 
651 	if (rdma_protocol_ib(device, port_num))
652 		return RDMA_NETWORK_IB;
653 
654 	grh_version = ib_get_rdma_header_version((const union rdma_network_hdr *)grh);
655 
656 	if (grh_version == 4)
657 		return RDMA_NETWORK_IPV4;
658 
659 	if (grh->next_hdr == IPPROTO_UDP)
660 		return RDMA_NETWORK_IPV6;
661 
662 	return RDMA_NETWORK_ROCE_V1;
663 }
664 
665 struct find_gid_index_context {
666 	u16 vlan_id;
667 	enum ib_gid_type gid_type;
668 };
669 
670 
671 /*
672  * This function will return true only if a inspected GID index
673  * matches the request based on the GID type and VLAN configuration
674  */
find_gid_index(const union ib_gid * gid,const struct ib_gid_attr * gid_attr,void * context)675 static bool find_gid_index(const union ib_gid *gid,
676 			   const struct ib_gid_attr *gid_attr,
677 			   void *context)
678 {
679 	u16 vlan_diff;
680 	struct find_gid_index_context *ctx = context;
681 
682 	if (ctx->gid_type != gid_attr->gid_type)
683 		return false;
684 
685 	/*
686 	 * The following will verify:
687 	 * 1. VLAN ID matching for VLAN tagged requests.
688 	 * 2. prio-tagged/untagged to prio-tagged/untagged matching.
689 	 *
690 	 * This XOR is valid, since 0x0 < vlan_id < 0x0FFF.
691 	 */
692 	vlan_diff = rdma_vlan_dev_vlan_id(gid_attr->ndev) ^ ctx->vlan_id;
693 
694 	return (vlan_diff == 0x0000 || vlan_diff == 0xFFFF);
695 }
696 
697 static const struct ib_gid_attr *
get_sgid_attr_from_eth(struct ib_device * device,u8 port_num,u16 vlan_id,const union ib_gid * sgid,enum ib_gid_type gid_type)698 get_sgid_attr_from_eth(struct ib_device *device, u8 port_num,
699 		       u16 vlan_id, const union ib_gid *sgid,
700 		       enum ib_gid_type gid_type)
701 {
702 	struct find_gid_index_context context = {.vlan_id = vlan_id,
703 						 .gid_type = gid_type};
704 
705 	return rdma_find_gid_by_filter(device, sgid, port_num, find_gid_index,
706 				       &context);
707 }
708 
ib_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)709 int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
710 			      enum rdma_network_type net_type,
711 			      union ib_gid *sgid, union ib_gid *dgid)
712 {
713 	struct sockaddr_in  src_in;
714 	struct sockaddr_in  dst_in;
715 	__be32 src_saddr, dst_saddr;
716 
717 	if (!sgid || !dgid)
718 		return -EINVAL;
719 
720 	if (net_type == RDMA_NETWORK_IPV4) {
721 		memcpy(&src_in.sin_addr.s_addr,
722 		       &hdr->roce4grh.ip_src, 4);
723 		memcpy(&dst_in.sin_addr.s_addr,
724 		       &hdr->roce4grh.ip_dst, 4);
725 		src_saddr = src_in.sin_addr.s_addr;
726 		dst_saddr = dst_in.sin_addr.s_addr;
727 		ipv6_addr_set_v4mapped(src_saddr,
728 				       (struct in6_addr *)sgid);
729 		ipv6_addr_set_v4mapped(dst_saddr,
730 				       (struct in6_addr *)dgid);
731 		return 0;
732 	} else if (net_type == RDMA_NETWORK_IPV6 ||
733 		   net_type == RDMA_NETWORK_IB) {
734 		*dgid = hdr->ibgrh.dgid;
735 		*sgid = hdr->ibgrh.sgid;
736 		return 0;
737 	} else {
738 		return -EINVAL;
739 	}
740 }
741 EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
742 
743 /* Resolve destination mac address and hop limit for unicast destination
744  * GID entry, considering the source GID entry as well.
745  * ah_attribute must have have valid port_num, sgid_index.
746  */
ib_resolve_unicast_gid_dmac(struct ib_device * device,struct rdma_ah_attr * ah_attr)747 static int ib_resolve_unicast_gid_dmac(struct ib_device *device,
748 				       struct rdma_ah_attr *ah_attr)
749 {
750 	struct ib_global_route *grh = rdma_ah_retrieve_grh(ah_attr);
751 	const struct ib_gid_attr *sgid_attr = grh->sgid_attr;
752 	int hop_limit = 0xff;
753 	int ret = 0;
754 
755 	/* If destination is link local and source GID is RoCEv1,
756 	 * IP stack is not used.
757 	 */
758 	if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) &&
759 	    sgid_attr->gid_type == IB_GID_TYPE_ROCE) {
760 		rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
761 				ah_attr->roce.dmac);
762 		return ret;
763 	}
764 
765 	ret = rdma_addr_find_l2_eth_by_grh(&sgid_attr->gid, &grh->dgid,
766 					   ah_attr->roce.dmac,
767 					   sgid_attr, &hop_limit);
768 
769 	grh->hop_limit = hop_limit;
770 	return ret;
771 }
772 
773 /*
774  * This function initializes address handle attributes from the incoming packet.
775  * Incoming packet has dgid of the receiver node on which this code is
776  * getting executed and, sgid contains the GID of the sender.
777  *
778  * When resolving mac address of destination, the arrived dgid is used
779  * as sgid and, sgid is used as dgid because sgid contains destinations
780  * GID whom to respond to.
781  *
782  * On success the caller is responsible to call rdma_destroy_ah_attr on the
783  * attr.
784  */
ib_init_ah_attr_from_wc(struct ib_device * device,u8 port_num,const struct ib_wc * wc,const struct ib_grh * grh,struct rdma_ah_attr * ah_attr)785 int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num,
786 			    const struct ib_wc *wc, const struct ib_grh *grh,
787 			    struct rdma_ah_attr *ah_attr)
788 {
789 	u32 flow_class;
790 	int ret;
791 	enum rdma_network_type net_type = RDMA_NETWORK_IB;
792 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
793 	const struct ib_gid_attr *sgid_attr;
794 	int hoplimit = 0xff;
795 	union ib_gid dgid;
796 	union ib_gid sgid;
797 
798 	memset(ah_attr, 0, sizeof *ah_attr);
799 	ah_attr->type = rdma_ah_find_type(device, port_num);
800 	if (rdma_cap_eth_ah(device, port_num)) {
801 		if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
802 			net_type = wc->network_hdr_type;
803 		else
804 			net_type = ib_get_net_type_by_grh(device, port_num, grh);
805 		gid_type = ib_network_to_gid_type(net_type);
806 	}
807 	ret = ib_get_gids_from_rdma_hdr((const union rdma_network_hdr *)grh, net_type,
808 				       &sgid, &dgid);
809 	if (ret)
810 		return ret;
811 
812 	rdma_ah_set_sl(ah_attr, wc->sl);
813 	rdma_ah_set_port_num(ah_attr, port_num);
814 
815 	if (rdma_protocol_roce(device, port_num)) {
816 		const u16 vlan_id = (wc->wc_flags & IB_WC_WITH_VLAN) ?
817 				wc->vlan_id : 0xffff;
818 
819 		if (!(wc->wc_flags & IB_WC_GRH))
820 			return -EPROTOTYPE;
821 
822 		sgid_attr = get_sgid_attr_from_eth(device, port_num,
823 						   vlan_id, &dgid,
824 						   gid_type);
825 		if (IS_ERR(sgid_attr))
826 			return PTR_ERR(sgid_attr);
827 
828 		flow_class = be32_to_cpu(grh->version_tclass_flow);
829 		rdma_move_grh_sgid_attr(ah_attr,
830 					&sgid,
831 					flow_class & 0xFFFFF,
832 					hoplimit,
833 					(flow_class >> 20) & 0xFF,
834 					sgid_attr);
835 
836 		ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
837 		if (ret)
838 			rdma_destroy_ah_attr(ah_attr);
839 
840 		return ret;
841 	} else {
842 		rdma_ah_set_dlid(ah_attr, wc->slid);
843 		rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
844 
845 		if ((wc->wc_flags & IB_WC_GRH) == 0)
846 			return 0;
847 
848 		if (dgid.global.interface_id !=
849 					cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
850 			sgid_attr = rdma_find_gid_by_port(
851 				device, &dgid, IB_GID_TYPE_IB, port_num, NULL);
852 		} else
853 			sgid_attr = rdma_get_gid_attr(device, port_num, 0);
854 
855 		if (IS_ERR(sgid_attr))
856 			return PTR_ERR(sgid_attr);
857 		flow_class = be32_to_cpu(grh->version_tclass_flow);
858 		rdma_move_grh_sgid_attr(ah_attr,
859 					&sgid,
860 					flow_class & 0xFFFFF,
861 					hoplimit,
862 					(flow_class >> 20) & 0xFF,
863 					sgid_attr);
864 
865 		return 0;
866 	}
867 }
868 EXPORT_SYMBOL(ib_init_ah_attr_from_wc);
869 
870 /**
871  * rdma_move_grh_sgid_attr - Sets the sgid attribute of GRH, taking ownership
872  * of the reference
873  *
874  * @attr:	Pointer to AH attribute structure
875  * @dgid:	Destination GID
876  * @flow_label:	Flow label
877  * @hop_limit:	Hop limit
878  * @traffic_class: traffic class
879  * @sgid_attr:	Pointer to SGID attribute
880  *
881  * This takes ownership of the sgid_attr reference. The caller must ensure
882  * rdma_destroy_ah_attr() is called before destroying the rdma_ah_attr after
883  * calling this function.
884  */
rdma_move_grh_sgid_attr(struct rdma_ah_attr * attr,union ib_gid * dgid,u32 flow_label,u8 hop_limit,u8 traffic_class,const struct ib_gid_attr * sgid_attr)885 void rdma_move_grh_sgid_attr(struct rdma_ah_attr *attr, union ib_gid *dgid,
886 			     u32 flow_label, u8 hop_limit, u8 traffic_class,
887 			     const struct ib_gid_attr *sgid_attr)
888 {
889 	rdma_ah_set_grh(attr, dgid, flow_label, sgid_attr->index, hop_limit,
890 			traffic_class);
891 	attr->grh.sgid_attr = sgid_attr;
892 }
893 EXPORT_SYMBOL(rdma_move_grh_sgid_attr);
894 
895 /**
896  * rdma_destroy_ah_attr - Release reference to SGID attribute of
897  * ah attribute.
898  * @ah_attr: Pointer to ah attribute
899  *
900  * Release reference to the SGID attribute of the ah attribute if it is
901  * non NULL. It is safe to call this multiple times, and safe to call it on
902  * a zero initialized ah_attr.
903  */
rdma_destroy_ah_attr(struct rdma_ah_attr * ah_attr)904 void rdma_destroy_ah_attr(struct rdma_ah_attr *ah_attr)
905 {
906 	if (ah_attr->grh.sgid_attr) {
907 		rdma_put_gid_attr(ah_attr->grh.sgid_attr);
908 		ah_attr->grh.sgid_attr = NULL;
909 	}
910 }
911 EXPORT_SYMBOL(rdma_destroy_ah_attr);
912 
ib_create_ah_from_wc(struct ib_pd * pd,const struct ib_wc * wc,const struct ib_grh * grh,u8 port_num)913 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
914 				   const struct ib_grh *grh, u8 port_num)
915 {
916 	struct rdma_ah_attr ah_attr;
917 	struct ib_ah *ah;
918 	int ret;
919 
920 	ret = ib_init_ah_attr_from_wc(pd->device, port_num, wc, grh, &ah_attr);
921 	if (ret)
922 		return ERR_PTR(ret);
923 
924 	ah = rdma_create_ah(pd, &ah_attr, RDMA_CREATE_AH_SLEEPABLE);
925 
926 	rdma_destroy_ah_attr(&ah_attr);
927 	return ah;
928 }
929 EXPORT_SYMBOL(ib_create_ah_from_wc);
930 
rdma_modify_ah(struct ib_ah * ah,struct rdma_ah_attr * ah_attr)931 int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
932 {
933 	const struct ib_gid_attr *old_sgid_attr;
934 	int ret;
935 
936 	if (ah->type != ah_attr->type)
937 		return -EINVAL;
938 
939 	ret = rdma_fill_sgid_attr(ah->device, ah_attr, &old_sgid_attr);
940 	if (ret)
941 		return ret;
942 
943 	ret = ah->device->modify_ah ?
944 		ah->device->modify_ah(ah, ah_attr) :
945 		-EOPNOTSUPP;
946 
947 	ah->sgid_attr = rdma_update_sgid_attr(ah_attr, ah->sgid_attr);
948 	rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
949 	return ret;
950 }
951 EXPORT_SYMBOL(rdma_modify_ah);
952 
rdma_query_ah(struct ib_ah * ah,struct rdma_ah_attr * ah_attr)953 int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
954 {
955 	ah_attr->grh.sgid_attr = NULL;
956 
957 	return ah->device->query_ah ?
958 		ah->device->query_ah(ah, ah_attr) :
959 		-EOPNOTSUPP;
960 }
961 EXPORT_SYMBOL(rdma_query_ah);
962 
rdma_destroy_ah_user(struct ib_ah * ah,u32 flags,struct ib_udata * udata)963 int rdma_destroy_ah_user(struct ib_ah *ah, u32 flags, struct ib_udata *udata)
964 {
965 	const struct ib_gid_attr *sgid_attr = ah->sgid_attr;
966 	struct ib_pd *pd;
967 
968 	might_sleep_if(flags & RDMA_DESTROY_AH_SLEEPABLE);
969 
970 	pd = ah->pd;
971 	ah->device->destroy_ah(ah, flags);
972 	atomic_dec(&pd->usecnt);
973 	if (sgid_attr)
974 		rdma_put_gid_attr(sgid_attr);
975 
976 	kfree(ah);
977 	return 0;
978 }
979 EXPORT_SYMBOL(rdma_destroy_ah_user);
980 
981 /* Shared receive queues */
982 
ib_create_srq(struct ib_pd * pd,struct ib_srq_init_attr * srq_init_attr)983 struct ib_srq *ib_create_srq(struct ib_pd *pd,
984 			     struct ib_srq_init_attr *srq_init_attr)
985 {
986 	struct ib_srq *srq;
987 	int ret;
988 
989 	if (!pd->device->create_srq)
990 		return ERR_PTR(-EOPNOTSUPP);
991 
992 	srq = rdma_zalloc_drv_obj(pd->device, ib_srq);
993 	if (!srq)
994 		return ERR_PTR(-ENOMEM);
995 
996 	srq->device = pd->device;
997 	srq->pd = pd;
998 	srq->event_handler = srq_init_attr->event_handler;
999 	srq->srq_context = srq_init_attr->srq_context;
1000 	srq->srq_type = srq_init_attr->srq_type;
1001 
1002 	if (ib_srq_has_cq(srq->srq_type)) {
1003 		srq->ext.cq = srq_init_attr->ext.cq;
1004 		atomic_inc(&srq->ext.cq->usecnt);
1005 	}
1006 	if (srq->srq_type == IB_SRQT_XRC) {
1007 		srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
1008 		atomic_inc(&srq->ext.xrc.xrcd->usecnt);
1009 	}
1010 	atomic_inc(&pd->usecnt);
1011 
1012 	ret = pd->device->create_srq(srq, srq_init_attr, NULL);
1013 	if (ret) {
1014 		atomic_dec(&srq->pd->usecnt);
1015 		if (srq->srq_type == IB_SRQT_XRC)
1016 			atomic_dec(&srq->ext.xrc.xrcd->usecnt);
1017 		if (ib_srq_has_cq(srq->srq_type))
1018 			atomic_dec(&srq->ext.cq->usecnt);
1019 		kfree(srq);
1020 		return ERR_PTR(ret);
1021 	}
1022 
1023 	return srq;
1024 }
1025 EXPORT_SYMBOL(ib_create_srq);
1026 
ib_modify_srq(struct ib_srq * srq,struct ib_srq_attr * srq_attr,enum ib_srq_attr_mask srq_attr_mask)1027 int ib_modify_srq(struct ib_srq *srq,
1028 		  struct ib_srq_attr *srq_attr,
1029 		  enum ib_srq_attr_mask srq_attr_mask)
1030 {
1031 	return srq->device->modify_srq ?
1032 		srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
1033 		-EOPNOTSUPP;
1034 }
1035 EXPORT_SYMBOL(ib_modify_srq);
1036 
ib_query_srq(struct ib_srq * srq,struct ib_srq_attr * srq_attr)1037 int ib_query_srq(struct ib_srq *srq,
1038 		 struct ib_srq_attr *srq_attr)
1039 {
1040 	return srq->device->query_srq ?
1041 		srq->device->query_srq(srq, srq_attr) : -EOPNOTSUPP;
1042 }
1043 EXPORT_SYMBOL(ib_query_srq);
1044 
ib_destroy_srq_user(struct ib_srq * srq,struct ib_udata * udata)1045 int ib_destroy_srq_user(struct ib_srq *srq, struct ib_udata *udata)
1046 {
1047 	if (atomic_read(&srq->usecnt))
1048 		return -EBUSY;
1049 
1050 	srq->device->destroy_srq(srq, udata);
1051 
1052 	atomic_dec(&srq->pd->usecnt);
1053 	if (srq->srq_type == IB_SRQT_XRC)
1054 		atomic_dec(&srq->ext.xrc.xrcd->usecnt);
1055 	if (ib_srq_has_cq(srq->srq_type))
1056 		atomic_dec(&srq->ext.cq->usecnt);
1057 	kfree(srq);
1058 
1059 	return 0;
1060 }
1061 EXPORT_SYMBOL(ib_destroy_srq_user);
1062 
1063 /* Queue pairs */
1064 
__ib_shared_qp_event_handler(struct ib_event * event,void * context)1065 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
1066 {
1067 	struct ib_qp *qp = context;
1068 	unsigned long flags;
1069 
1070 	spin_lock_irqsave(&qp->device->event_handler_lock, flags);
1071 	list_for_each_entry(event->element.qp, &qp->open_list, open_list)
1072 		if (event->element.qp->event_handler)
1073 			event->element.qp->event_handler(event, event->element.qp->qp_context);
1074 	spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
1075 }
1076 
__ib_insert_xrcd_qp(struct ib_xrcd * xrcd,struct ib_qp * qp)1077 static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
1078 {
1079 	mutex_lock(&xrcd->tgt_qp_mutex);
1080 	list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
1081 	mutex_unlock(&xrcd->tgt_qp_mutex);
1082 }
1083 
__ib_open_qp(struct ib_qp * real_qp,void (* event_handler)(struct ib_event *,void *),void * qp_context)1084 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
1085 				  void (*event_handler)(struct ib_event *, void *),
1086 				  void *qp_context)
1087 {
1088 	struct ib_qp *qp;
1089 	unsigned long flags;
1090 
1091 	qp = kzalloc(sizeof *qp, GFP_KERNEL);
1092 	if (!qp)
1093 		return ERR_PTR(-ENOMEM);
1094 
1095 	qp->real_qp = real_qp;
1096 	atomic_inc(&real_qp->usecnt);
1097 	qp->device = real_qp->device;
1098 	qp->event_handler = event_handler;
1099 	qp->qp_context = qp_context;
1100 	qp->qp_num = real_qp->qp_num;
1101 	qp->qp_type = real_qp->qp_type;
1102 
1103 	spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1104 	list_add(&qp->open_list, &real_qp->open_list);
1105 	spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1106 
1107 	return qp;
1108 }
1109 
ib_open_qp(struct ib_xrcd * xrcd,struct ib_qp_open_attr * qp_open_attr)1110 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
1111 			 struct ib_qp_open_attr *qp_open_attr)
1112 {
1113 	struct ib_qp *qp, *real_qp;
1114 
1115 	if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
1116 		return ERR_PTR(-EINVAL);
1117 
1118 	qp = ERR_PTR(-EINVAL);
1119 	mutex_lock(&xrcd->tgt_qp_mutex);
1120 	list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
1121 		if (real_qp->qp_num == qp_open_attr->qp_num) {
1122 			qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
1123 					  qp_open_attr->qp_context);
1124 			break;
1125 		}
1126 	}
1127 	mutex_unlock(&xrcd->tgt_qp_mutex);
1128 	return qp;
1129 }
1130 EXPORT_SYMBOL(ib_open_qp);
1131 
ib_create_xrc_qp(struct ib_qp * qp,struct ib_qp_init_attr * qp_init_attr)1132 static struct ib_qp *ib_create_xrc_qp(struct ib_qp *qp,
1133 		struct ib_qp_init_attr *qp_init_attr)
1134 {
1135 	struct ib_qp *real_qp = qp;
1136 
1137 	qp->event_handler = __ib_shared_qp_event_handler;
1138 	qp->qp_context = qp;
1139 	qp->pd = NULL;
1140 	qp->send_cq = qp->recv_cq = NULL;
1141 	qp->srq = NULL;
1142 	qp->xrcd = qp_init_attr->xrcd;
1143 	atomic_inc(&qp_init_attr->xrcd->usecnt);
1144 	INIT_LIST_HEAD(&qp->open_list);
1145 
1146 	qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
1147 			  qp_init_attr->qp_context);
1148 	if (!IS_ERR(qp))
1149 		__ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
1150 	else
1151 		real_qp->device->destroy_qp(real_qp, NULL);
1152 	return qp;
1153 }
1154 
ib_create_qp(struct ib_pd * pd,struct ib_qp_init_attr * qp_init_attr)1155 struct ib_qp *ib_create_qp(struct ib_pd *pd,
1156 			   struct ib_qp_init_attr *qp_init_attr)
1157 {
1158 	struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
1159 	struct ib_qp *qp;
1160 
1161 	if (qp_init_attr->rwq_ind_tbl &&
1162 	    (qp_init_attr->recv_cq ||
1163 	    qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
1164 	    qp_init_attr->cap.max_recv_sge))
1165 		return ERR_PTR(-EINVAL);
1166 
1167 	qp = _ib_create_qp(device, pd, qp_init_attr, NULL, NULL);
1168 	if (IS_ERR(qp))
1169 		return qp;
1170 
1171 	qp->device     = device;
1172 	qp->real_qp    = qp;
1173 	qp->uobject    = NULL;
1174 	qp->qp_type    = qp_init_attr->qp_type;
1175 	qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
1176 
1177 	atomic_set(&qp->usecnt, 0);
1178 	spin_lock_init(&qp->mr_lock);
1179 
1180 	if (qp_init_attr->qp_type == IB_QPT_XRC_TGT)
1181 		return ib_create_xrc_qp(qp, qp_init_attr);
1182 
1183 	qp->event_handler = qp_init_attr->event_handler;
1184 	qp->qp_context = qp_init_attr->qp_context;
1185 	if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
1186 		qp->recv_cq = NULL;
1187 		qp->srq = NULL;
1188 	} else {
1189 		qp->recv_cq = qp_init_attr->recv_cq;
1190 		if (qp_init_attr->recv_cq)
1191 			atomic_inc(&qp_init_attr->recv_cq->usecnt);
1192 		qp->srq = qp_init_attr->srq;
1193 		if (qp->srq)
1194 			atomic_inc(&qp_init_attr->srq->usecnt);
1195 	}
1196 
1197 	qp->pd	    = pd;
1198 	qp->send_cq = qp_init_attr->send_cq;
1199 	qp->xrcd    = NULL;
1200 
1201 	atomic_inc(&pd->usecnt);
1202 	if (qp_init_attr->send_cq)
1203 		atomic_inc(&qp_init_attr->send_cq->usecnt);
1204 	if (qp_init_attr->rwq_ind_tbl)
1205 		atomic_inc(&qp->rwq_ind_tbl->usecnt);
1206 
1207 	/*
1208 	 * Note: all hw drivers guarantee that max_send_sge is lower than
1209 	 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
1210 	 * max_send_sge <= max_sge_rd.
1211 	 */
1212 	qp->max_write_sge = qp_init_attr->cap.max_send_sge;
1213 	qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
1214 				 device->attrs.max_sge_rd);
1215 
1216 	return qp;
1217 }
1218 EXPORT_SYMBOL(ib_create_qp);
1219 
1220 static const struct {
1221 	int			valid;
1222 	enum ib_qp_attr_mask	req_param[IB_QPT_MAX];
1223 	enum ib_qp_attr_mask	opt_param[IB_QPT_MAX];
1224 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
1225 	[IB_QPS_RESET] = {
1226 		[IB_QPS_RESET] = { .valid = 1 },
1227 		[IB_QPS_INIT]  = {
1228 			.valid = 1,
1229 			.req_param = {
1230 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
1231 						IB_QP_PORT			|
1232 						IB_QP_QKEY),
1233 				[IB_QPT_RAW_PACKET] = IB_QP_PORT,
1234 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
1235 						IB_QP_PORT			|
1236 						IB_QP_ACCESS_FLAGS),
1237 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
1238 						IB_QP_PORT			|
1239 						IB_QP_ACCESS_FLAGS),
1240 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
1241 						IB_QP_PORT			|
1242 						IB_QP_ACCESS_FLAGS),
1243 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
1244 						IB_QP_PORT			|
1245 						IB_QP_ACCESS_FLAGS),
1246 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
1247 						IB_QP_QKEY),
1248 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
1249 						IB_QP_QKEY),
1250 			}
1251 		},
1252 	},
1253 	[IB_QPS_INIT]  = {
1254 		[IB_QPS_RESET] = { .valid = 1 },
1255 		[IB_QPS_ERR] =   { .valid = 1 },
1256 		[IB_QPS_INIT]  = {
1257 			.valid = 1,
1258 			.opt_param = {
1259 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
1260 						IB_QP_PORT			|
1261 						IB_QP_QKEY),
1262 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
1263 						IB_QP_PORT			|
1264 						IB_QP_ACCESS_FLAGS),
1265 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
1266 						IB_QP_PORT			|
1267 						IB_QP_ACCESS_FLAGS),
1268 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
1269 						IB_QP_PORT			|
1270 						IB_QP_ACCESS_FLAGS),
1271 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
1272 						IB_QP_PORT			|
1273 						IB_QP_ACCESS_FLAGS),
1274 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
1275 						IB_QP_QKEY),
1276 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
1277 						IB_QP_QKEY),
1278 			}
1279 		},
1280 		[IB_QPS_RTR]   = {
1281 			.valid = 1,
1282 			.req_param = {
1283 				[IB_QPT_UC]  = (IB_QP_AV			|
1284 						IB_QP_PATH_MTU			|
1285 						IB_QP_DEST_QPN			|
1286 						IB_QP_RQ_PSN),
1287 				[IB_QPT_RC]  = (IB_QP_AV			|
1288 						IB_QP_PATH_MTU			|
1289 						IB_QP_DEST_QPN			|
1290 						IB_QP_RQ_PSN			|
1291 						IB_QP_MAX_DEST_RD_ATOMIC	|
1292 						IB_QP_MIN_RNR_TIMER),
1293 				[IB_QPT_XRC_INI] = (IB_QP_AV			|
1294 						IB_QP_PATH_MTU			|
1295 						IB_QP_DEST_QPN			|
1296 						IB_QP_RQ_PSN),
1297 				[IB_QPT_XRC_TGT] = (IB_QP_AV			|
1298 						IB_QP_PATH_MTU			|
1299 						IB_QP_DEST_QPN			|
1300 						IB_QP_RQ_PSN			|
1301 						IB_QP_MAX_DEST_RD_ATOMIC	|
1302 						IB_QP_MIN_RNR_TIMER),
1303 			},
1304 			.opt_param = {
1305 				 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
1306 						 IB_QP_QKEY),
1307 				 [IB_QPT_UC]  = (IB_QP_ALT_PATH			|
1308 						 IB_QP_ACCESS_FLAGS		|
1309 						 IB_QP_PKEY_INDEX),
1310 				 [IB_QPT_RC]  = (IB_QP_ALT_PATH			|
1311 						 IB_QP_ACCESS_FLAGS		|
1312 						 IB_QP_PKEY_INDEX),
1313 				 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH		|
1314 						 IB_QP_ACCESS_FLAGS		|
1315 						 IB_QP_PKEY_INDEX),
1316 				 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH		|
1317 						 IB_QP_ACCESS_FLAGS		|
1318 						 IB_QP_PKEY_INDEX),
1319 				 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
1320 						 IB_QP_QKEY),
1321 				 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
1322 						 IB_QP_QKEY),
1323 			 },
1324 		},
1325 	},
1326 	[IB_QPS_RTR]   = {
1327 		[IB_QPS_RESET] = { .valid = 1 },
1328 		[IB_QPS_ERR] =   { .valid = 1 },
1329 		[IB_QPS_RTS]   = {
1330 			.valid = 1,
1331 			.req_param = {
1332 				[IB_QPT_UD]  = IB_QP_SQ_PSN,
1333 				[IB_QPT_UC]  = IB_QP_SQ_PSN,
1334 				[IB_QPT_RC]  = (IB_QP_TIMEOUT			|
1335 						IB_QP_RETRY_CNT			|
1336 						IB_QP_RNR_RETRY			|
1337 						IB_QP_SQ_PSN			|
1338 						IB_QP_MAX_QP_RD_ATOMIC),
1339 				[IB_QPT_XRC_INI] = (IB_QP_TIMEOUT		|
1340 						IB_QP_RETRY_CNT			|
1341 						IB_QP_RNR_RETRY			|
1342 						IB_QP_SQ_PSN			|
1343 						IB_QP_MAX_QP_RD_ATOMIC),
1344 				[IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT		|
1345 						IB_QP_SQ_PSN),
1346 				[IB_QPT_SMI] = IB_QP_SQ_PSN,
1347 				[IB_QPT_GSI] = IB_QP_SQ_PSN,
1348 			},
1349 			.opt_param = {
1350 				 [IB_QPT_UD]  = (IB_QP_CUR_STATE		|
1351 						 IB_QP_QKEY),
1352 				 [IB_QPT_UC]  = (IB_QP_CUR_STATE		|
1353 						 IB_QP_ALT_PATH			|
1354 						 IB_QP_ACCESS_FLAGS		|
1355 						 IB_QP_PATH_MIG_STATE),
1356 				 [IB_QPT_RC]  = (IB_QP_CUR_STATE		|
1357 						 IB_QP_ALT_PATH			|
1358 						 IB_QP_ACCESS_FLAGS		|
1359 						 IB_QP_MIN_RNR_TIMER		|
1360 						 IB_QP_PATH_MIG_STATE),
1361 				 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1362 						 IB_QP_ALT_PATH			|
1363 						 IB_QP_ACCESS_FLAGS		|
1364 						 IB_QP_PATH_MIG_STATE),
1365 				 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1366 						 IB_QP_ALT_PATH			|
1367 						 IB_QP_ACCESS_FLAGS		|
1368 						 IB_QP_MIN_RNR_TIMER		|
1369 						 IB_QP_PATH_MIG_STATE),
1370 				 [IB_QPT_SMI] = (IB_QP_CUR_STATE		|
1371 						 IB_QP_QKEY),
1372 				 [IB_QPT_GSI] = (IB_QP_CUR_STATE		|
1373 						 IB_QP_QKEY),
1374 				 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1375 			 }
1376 		}
1377 	},
1378 	[IB_QPS_RTS]   = {
1379 		[IB_QPS_RESET] = { .valid = 1 },
1380 		[IB_QPS_ERR] =   { .valid = 1 },
1381 		[IB_QPS_RTS]   = {
1382 			.valid = 1,
1383 			.opt_param = {
1384 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
1385 						IB_QP_QKEY),
1386 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
1387 						IB_QP_ACCESS_FLAGS		|
1388 						IB_QP_ALT_PATH			|
1389 						IB_QP_PATH_MIG_STATE),
1390 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
1391 						IB_QP_ACCESS_FLAGS		|
1392 						IB_QP_ALT_PATH			|
1393 						IB_QP_PATH_MIG_STATE		|
1394 						IB_QP_MIN_RNR_TIMER),
1395 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1396 						IB_QP_ACCESS_FLAGS		|
1397 						IB_QP_ALT_PATH			|
1398 						IB_QP_PATH_MIG_STATE),
1399 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1400 						IB_QP_ACCESS_FLAGS		|
1401 						IB_QP_ALT_PATH			|
1402 						IB_QP_PATH_MIG_STATE		|
1403 						IB_QP_MIN_RNR_TIMER),
1404 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
1405 						IB_QP_QKEY),
1406 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
1407 						IB_QP_QKEY),
1408 				[IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1409 			}
1410 		},
1411 		[IB_QPS_SQD]   = {
1412 			.valid = 1,
1413 			.opt_param = {
1414 				[IB_QPT_UD]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1415 				[IB_QPT_UC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1416 				[IB_QPT_RC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1417 				[IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1418 				[IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1419 				[IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1420 				[IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1421 			}
1422 		},
1423 	},
1424 	[IB_QPS_SQD]   = {
1425 		[IB_QPS_RESET] = { .valid = 1 },
1426 		[IB_QPS_ERR] =   { .valid = 1 },
1427 		[IB_QPS_RTS]   = {
1428 			.valid = 1,
1429 			.opt_param = {
1430 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
1431 						IB_QP_QKEY),
1432 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
1433 						IB_QP_ALT_PATH			|
1434 						IB_QP_ACCESS_FLAGS		|
1435 						IB_QP_PATH_MIG_STATE),
1436 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
1437 						IB_QP_ALT_PATH			|
1438 						IB_QP_ACCESS_FLAGS		|
1439 						IB_QP_MIN_RNR_TIMER		|
1440 						IB_QP_PATH_MIG_STATE),
1441 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1442 						IB_QP_ALT_PATH			|
1443 						IB_QP_ACCESS_FLAGS		|
1444 						IB_QP_PATH_MIG_STATE),
1445 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1446 						IB_QP_ALT_PATH			|
1447 						IB_QP_ACCESS_FLAGS		|
1448 						IB_QP_MIN_RNR_TIMER		|
1449 						IB_QP_PATH_MIG_STATE),
1450 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
1451 						IB_QP_QKEY),
1452 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
1453 						IB_QP_QKEY),
1454 			}
1455 		},
1456 		[IB_QPS_SQD]   = {
1457 			.valid = 1,
1458 			.opt_param = {
1459 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
1460 						IB_QP_QKEY),
1461 				[IB_QPT_UC]  = (IB_QP_AV			|
1462 						IB_QP_ALT_PATH			|
1463 						IB_QP_ACCESS_FLAGS		|
1464 						IB_QP_PKEY_INDEX		|
1465 						IB_QP_PATH_MIG_STATE),
1466 				[IB_QPT_RC]  = (IB_QP_PORT			|
1467 						IB_QP_AV			|
1468 						IB_QP_TIMEOUT			|
1469 						IB_QP_RETRY_CNT			|
1470 						IB_QP_RNR_RETRY			|
1471 						IB_QP_MAX_QP_RD_ATOMIC		|
1472 						IB_QP_MAX_DEST_RD_ATOMIC	|
1473 						IB_QP_ALT_PATH			|
1474 						IB_QP_ACCESS_FLAGS		|
1475 						IB_QP_PKEY_INDEX		|
1476 						IB_QP_MIN_RNR_TIMER		|
1477 						IB_QP_PATH_MIG_STATE),
1478 				[IB_QPT_XRC_INI] = (IB_QP_PORT			|
1479 						IB_QP_AV			|
1480 						IB_QP_TIMEOUT			|
1481 						IB_QP_RETRY_CNT			|
1482 						IB_QP_RNR_RETRY			|
1483 						IB_QP_MAX_QP_RD_ATOMIC		|
1484 						IB_QP_ALT_PATH			|
1485 						IB_QP_ACCESS_FLAGS		|
1486 						IB_QP_PKEY_INDEX		|
1487 						IB_QP_PATH_MIG_STATE),
1488 				[IB_QPT_XRC_TGT] = (IB_QP_PORT			|
1489 						IB_QP_AV			|
1490 						IB_QP_TIMEOUT			|
1491 						IB_QP_MAX_DEST_RD_ATOMIC	|
1492 						IB_QP_ALT_PATH			|
1493 						IB_QP_ACCESS_FLAGS		|
1494 						IB_QP_PKEY_INDEX		|
1495 						IB_QP_MIN_RNR_TIMER		|
1496 						IB_QP_PATH_MIG_STATE),
1497 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
1498 						IB_QP_QKEY),
1499 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
1500 						IB_QP_QKEY),
1501 			}
1502 		}
1503 	},
1504 	[IB_QPS_SQE]   = {
1505 		[IB_QPS_RESET] = { .valid = 1 },
1506 		[IB_QPS_ERR] =   { .valid = 1 },
1507 		[IB_QPS_RTS]   = {
1508 			.valid = 1,
1509 			.opt_param = {
1510 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
1511 						IB_QP_QKEY),
1512 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
1513 						IB_QP_ACCESS_FLAGS),
1514 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
1515 						IB_QP_QKEY),
1516 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
1517 						IB_QP_QKEY),
1518 			}
1519 		}
1520 	},
1521 	[IB_QPS_ERR] = {
1522 		[IB_QPS_RESET] = { .valid = 1 },
1523 		[IB_QPS_ERR] =   { .valid = 1 }
1524 	}
1525 };
1526 
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)1527 bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1528 			enum ib_qp_type type, enum ib_qp_attr_mask mask)
1529 {
1530 	enum ib_qp_attr_mask req_param, opt_param;
1531 
1532 	if (mask & IB_QP_CUR_STATE  &&
1533 	    cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1534 	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1535 		return false;
1536 
1537 	if (!qp_state_table[cur_state][next_state].valid)
1538 		return false;
1539 
1540 	req_param = qp_state_table[cur_state][next_state].req_param[type];
1541 	opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1542 
1543 	if ((mask & req_param) != req_param)
1544 		return false;
1545 
1546 	if (mask & ~(req_param | opt_param | IB_QP_STATE))
1547 		return false;
1548 
1549 	return true;
1550 }
1551 EXPORT_SYMBOL(ib_modify_qp_is_ok);
1552 
1553 /**
1554  * ib_resolve_eth_dmac - Resolve destination mac address
1555  * @device:		Device to consider
1556  * @ah_attr:		address handle attribute which describes the
1557  *			source and destination parameters
1558  * ib_resolve_eth_dmac() resolves destination mac address and L3 hop limit It
1559  * returns 0 on success or appropriate error code. It initializes the
1560  * necessary ah_attr fields when call is successful.
1561  */
ib_resolve_eth_dmac(struct ib_device * device,struct rdma_ah_attr * ah_attr)1562 static int ib_resolve_eth_dmac(struct ib_device *device,
1563 			       struct rdma_ah_attr *ah_attr)
1564 {
1565 	int ret = 0;
1566 
1567 	if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1568 		if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1569 			__be32 addr = 0;
1570 
1571 			memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
1572 			ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
1573 		} else {
1574 			ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
1575 					(char *)ah_attr->roce.dmac);
1576 		}
1577 	} else {
1578 		ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
1579 	}
1580 	return ret;
1581 }
1582 
is_qp_type_connected(const struct ib_qp * qp)1583 static bool is_qp_type_connected(const struct ib_qp *qp)
1584 {
1585 	return (qp->qp_type == IB_QPT_UC ||
1586 		qp->qp_type == IB_QPT_RC ||
1587 		qp->qp_type == IB_QPT_XRC_INI ||
1588 		qp->qp_type == IB_QPT_XRC_TGT);
1589 }
1590 
1591 /**
1592  * IB core internal function to perform QP attributes modification.
1593  */
_ib_modify_qp(struct ib_qp * qp,struct ib_qp_attr * attr,int attr_mask,struct ib_udata * udata)1594 static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
1595 			 int attr_mask, struct ib_udata *udata)
1596 {
1597 	u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1598 	const struct ib_gid_attr *old_sgid_attr_av;
1599 	const struct ib_gid_attr *old_sgid_attr_alt_av;
1600 	int ret;
1601 
1602 	if (attr_mask & IB_QP_AV) {
1603 		ret = rdma_fill_sgid_attr(qp->device, &attr->ah_attr,
1604 					  &old_sgid_attr_av);
1605 		if (ret)
1606 			return ret;
1607 	}
1608 	if (attr_mask & IB_QP_ALT_PATH) {
1609 		/*
1610 		 * FIXME: This does not track the migration state, so if the
1611 		 * user loads a new alternate path after the HW has migrated
1612 		 * from primary->alternate we will keep the wrong
1613 		 * references. This is OK for IB because the reference
1614 		 * counting does not serve any functional purpose.
1615 		 */
1616 		ret = rdma_fill_sgid_attr(qp->device, &attr->alt_ah_attr,
1617 					  &old_sgid_attr_alt_av);
1618 		if (ret)
1619 			goto out_av;
1620 
1621 		/*
1622 		 * Today the core code can only handle alternate paths and APM
1623 		 * for IB. Ban them in roce mode.
1624 		 */
1625 		if (!(rdma_protocol_ib(qp->device,
1626 				       attr->alt_ah_attr.port_num) &&
1627 		      rdma_protocol_ib(qp->device, port))) {
1628 			ret = EINVAL;
1629 			goto out;
1630 		}
1631 	}
1632 
1633 	/*
1634 	 * If the user provided the qp_attr then we have to resolve it. Kernel
1635 	 * users have to provide already resolved rdma_ah_attr's
1636 	 */
1637 	if (udata && (attr_mask & IB_QP_AV) &&
1638 	    attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE &&
1639 	    is_qp_type_connected(qp)) {
1640 		ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr);
1641 		if (ret)
1642 			goto out;
1643 	}
1644 
1645 	if (rdma_ib_or_roce(qp->device, port)) {
1646 		if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) {
1647 			dev_warn(&qp->device->dev,
1648 				 "%s rq_psn overflow, masking to 24 bits\n",
1649 				 __func__);
1650 			attr->rq_psn &= 0xffffff;
1651 		}
1652 
1653 		if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) {
1654 			dev_warn(&qp->device->dev,
1655 				 " %s sq_psn overflow, masking to 24 bits\n",
1656 				 __func__);
1657 			attr->sq_psn &= 0xffffff;
1658 		}
1659 	}
1660 
1661 	ret = qp->device->modify_qp(qp, attr, attr_mask, udata);
1662 	if (ret)
1663 		goto out;
1664 
1665 	if (attr_mask & IB_QP_PORT)
1666 		qp->port = attr->port_num;
1667 	if (attr_mask & IB_QP_AV)
1668 		qp->av_sgid_attr =
1669 			rdma_update_sgid_attr(&attr->ah_attr, qp->av_sgid_attr);
1670 	if (attr_mask & IB_QP_ALT_PATH)
1671 		qp->alt_path_sgid_attr = rdma_update_sgid_attr(
1672 			&attr->alt_ah_attr, qp->alt_path_sgid_attr);
1673 
1674 out:
1675 	if (attr_mask & IB_QP_ALT_PATH)
1676 		rdma_unfill_sgid_attr(&attr->alt_ah_attr, old_sgid_attr_alt_av);
1677 out_av:
1678 	if (attr_mask & IB_QP_AV)
1679 		rdma_unfill_sgid_attr(&attr->ah_attr, old_sgid_attr_av);
1680 	return ret;
1681 }
1682 
1683 /**
1684  * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
1685  * @ib_qp: The QP to modify.
1686  * @attr: On input, specifies the QP attributes to modify.  On output,
1687  *   the current values of selected QP attributes are returned.
1688  * @attr_mask: A bit-mask used to specify which attributes of the QP
1689  *   are being modified.
1690  * @udata: pointer to user's input output buffer information
1691  *   are being modified.
1692  * It returns 0 on success and returns appropriate error code on error.
1693  */
ib_modify_qp_with_udata(struct ib_qp * ib_qp,struct ib_qp_attr * attr,int attr_mask,struct ib_udata * udata)1694 int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
1695 			    int attr_mask, struct ib_udata *udata)
1696 {
1697 	return _ib_modify_qp(ib_qp->real_qp, attr, attr_mask, udata);
1698 }
1699 EXPORT_SYMBOL(ib_modify_qp_with_udata);
1700 
ib_get_eth_speed(struct ib_device * dev,u8 port_num,u16 * speed,u8 * width)1701 int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u16 *speed, u8 *width)
1702 {
1703 	uint64_t netdev_speed;
1704 	if_t netdev;
1705 
1706 	if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
1707 		return -EINVAL;
1708 
1709 	if (!dev->get_netdev)
1710 		return -EOPNOTSUPP;
1711 
1712 	netdev = dev->get_netdev(dev, port_num);
1713 	if (!netdev)
1714 		return -ENODEV;
1715 
1716         netdev_speed = if_getbaudrate(netdev);
1717 
1718 	dev_put(netdev);
1719 
1720 	if (netdev_speed == 0) {
1721 		netdev_speed = IF_Mbps(1000);
1722 		if_printf(netdev, "speed is unknown, defaulting to 1Gbps\n");
1723 	}
1724 
1725 	if (netdev_speed <= IF_Mbps(1000)) {
1726 		*width = IB_WIDTH_1X;
1727 		*speed = IB_SPEED_SDR;
1728 	} else if (netdev_speed <= IF_Mbps(10000)) {
1729 		*width = IB_WIDTH_1X;
1730 		*speed = IB_SPEED_FDR10;
1731 	} else if (netdev_speed <= IF_Mbps(20000)) {
1732 		*width = IB_WIDTH_4X;
1733 		*speed = IB_SPEED_DDR;
1734 	} else if (netdev_speed <= IF_Mbps(25000)) {
1735 		*width = IB_WIDTH_1X;
1736 		*speed = IB_SPEED_EDR;
1737 	} else if (netdev_speed <= IF_Mbps(40000)) {
1738 		*width = IB_WIDTH_4X;
1739 		*speed = IB_SPEED_FDR10;
1740 	} else {
1741 		*width = IB_WIDTH_4X;
1742 		*speed = IB_SPEED_EDR;
1743 	}
1744 
1745 	return 0;
1746 }
1747 EXPORT_SYMBOL(ib_get_eth_speed);
1748 
ib_modify_qp(struct ib_qp * qp,struct ib_qp_attr * qp_attr,int qp_attr_mask)1749 int ib_modify_qp(struct ib_qp *qp,
1750 		 struct ib_qp_attr *qp_attr,
1751 		 int qp_attr_mask)
1752 {
1753 	return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
1754 }
1755 EXPORT_SYMBOL(ib_modify_qp);
1756 
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)1757 int ib_query_qp(struct ib_qp *qp,
1758 		struct ib_qp_attr *qp_attr,
1759 		int qp_attr_mask,
1760 		struct ib_qp_init_attr *qp_init_attr)
1761 {
1762 	qp_attr->ah_attr.grh.sgid_attr = NULL;
1763 	qp_attr->alt_ah_attr.grh.sgid_attr = NULL;
1764 
1765 	return qp->device->query_qp ?
1766 		qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
1767 		-EOPNOTSUPP;
1768 }
1769 EXPORT_SYMBOL(ib_query_qp);
1770 
ib_close_qp(struct ib_qp * qp)1771 int ib_close_qp(struct ib_qp *qp)
1772 {
1773 	struct ib_qp *real_qp;
1774 	unsigned long flags;
1775 
1776 	real_qp = qp->real_qp;
1777 	if (real_qp == qp)
1778 		return -EINVAL;
1779 
1780 	spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
1781 	list_del(&qp->open_list);
1782 	spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
1783 
1784 	atomic_dec(&real_qp->usecnt);
1785 	kfree(qp);
1786 
1787 	return 0;
1788 }
1789 EXPORT_SYMBOL(ib_close_qp);
1790 
__ib_destroy_shared_qp(struct ib_qp * qp)1791 static int __ib_destroy_shared_qp(struct ib_qp *qp)
1792 {
1793 	struct ib_xrcd *xrcd;
1794 	struct ib_qp *real_qp;
1795 	int ret;
1796 
1797 	real_qp = qp->real_qp;
1798 	xrcd = real_qp->xrcd;
1799 
1800 	mutex_lock(&xrcd->tgt_qp_mutex);
1801 	ib_close_qp(qp);
1802 	if (atomic_read(&real_qp->usecnt) == 0)
1803 		list_del(&real_qp->xrcd_list);
1804 	else
1805 		real_qp = NULL;
1806 	mutex_unlock(&xrcd->tgt_qp_mutex);
1807 
1808 	if (real_qp) {
1809 		ret = ib_destroy_qp(real_qp);
1810 		if (!ret)
1811 			atomic_dec(&xrcd->usecnt);
1812 		else
1813 			__ib_insert_xrcd_qp(xrcd, real_qp);
1814 	}
1815 
1816 	return 0;
1817 }
1818 
ib_destroy_qp_user(struct ib_qp * qp,struct ib_udata * udata)1819 int ib_destroy_qp_user(struct ib_qp *qp, struct ib_udata *udata)
1820 {
1821 	const struct ib_gid_attr *alt_path_sgid_attr = qp->alt_path_sgid_attr;
1822 	const struct ib_gid_attr *av_sgid_attr = qp->av_sgid_attr;
1823 	struct ib_pd *pd;
1824 	struct ib_cq *scq, *rcq;
1825 	struct ib_srq *srq;
1826 	struct ib_rwq_ind_table *ind_tbl;
1827 	int ret;
1828 
1829 	if (atomic_read(&qp->usecnt))
1830 		return -EBUSY;
1831 
1832 	if (qp->real_qp != qp)
1833 		return __ib_destroy_shared_qp(qp);
1834 
1835 	pd   = qp->pd;
1836 	scq  = qp->send_cq;
1837 	rcq  = qp->recv_cq;
1838 	srq  = qp->srq;
1839 	ind_tbl = qp->rwq_ind_tbl;
1840 
1841 	ret = qp->device->destroy_qp(qp, udata);
1842 	if (!ret) {
1843 		if (alt_path_sgid_attr)
1844 			rdma_put_gid_attr(alt_path_sgid_attr);
1845 		if (av_sgid_attr)
1846 			rdma_put_gid_attr(av_sgid_attr);
1847 		if (pd)
1848 			atomic_dec(&pd->usecnt);
1849 		if (scq)
1850 			atomic_dec(&scq->usecnt);
1851 		if (rcq)
1852 			atomic_dec(&rcq->usecnt);
1853 		if (srq)
1854 			atomic_dec(&srq->usecnt);
1855 		if (ind_tbl)
1856 			atomic_dec(&ind_tbl->usecnt);
1857 	}
1858 
1859 	return ret;
1860 }
1861 EXPORT_SYMBOL(ib_destroy_qp_user);
1862 
1863 /* Completion queues */
1864 
__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,const char * caller)1865 struct ib_cq *__ib_create_cq(struct ib_device *device,
1866 			     ib_comp_handler comp_handler,
1867 			     void (*event_handler)(struct ib_event *, void *),
1868 			     void *cq_context,
1869 			     const struct ib_cq_init_attr *cq_attr,
1870 			     const char *caller)
1871 {
1872 	struct ib_cq *cq;
1873 	int ret;
1874 
1875 	cq = rdma_zalloc_drv_obj(device, ib_cq);
1876 	if (!cq)
1877 		return ERR_PTR(-ENOMEM);
1878 
1879 	cq->device = device;
1880 	cq->uobject = NULL;
1881 	cq->comp_handler = comp_handler;
1882 	cq->event_handler = event_handler;
1883 	cq->cq_context = cq_context;
1884 	atomic_set(&cq->usecnt, 0);
1885 
1886 	ret = device->create_cq(cq, cq_attr, NULL);
1887 	if (ret) {
1888 		kfree(cq);
1889 		return ERR_PTR(ret);
1890 	}
1891 
1892 	return cq;
1893 }
1894 EXPORT_SYMBOL(__ib_create_cq);
1895 
rdma_set_cq_moderation(struct ib_cq * cq,u16 cq_count,u16 cq_period)1896 int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1897 {
1898 	return cq->device->modify_cq ?
1899 		cq->device->modify_cq(cq, cq_count, cq_period) : -EOPNOTSUPP;
1900 }
1901 EXPORT_SYMBOL(rdma_set_cq_moderation);
1902 
ib_destroy_cq_user(struct ib_cq * cq,struct ib_udata * udata)1903 int ib_destroy_cq_user(struct ib_cq *cq, struct ib_udata *udata)
1904 {
1905 	if (atomic_read(&cq->usecnt))
1906 		return -EBUSY;
1907 
1908 	cq->device->destroy_cq(cq, udata);
1909 	kfree(cq);
1910 	return 0;
1911 }
1912 EXPORT_SYMBOL(ib_destroy_cq_user);
1913 
ib_resize_cq(struct ib_cq * cq,int cqe)1914 int ib_resize_cq(struct ib_cq *cq, int cqe)
1915 {
1916 	return cq->device->resize_cq ?
1917 		cq->device->resize_cq(cq, cqe, NULL) : -EOPNOTSUPP;
1918 }
1919 EXPORT_SYMBOL(ib_resize_cq);
1920 
1921 /* Memory regions */
1922 
ib_dereg_mr_user(struct ib_mr * mr,struct ib_udata * udata)1923 int ib_dereg_mr_user(struct ib_mr *mr, struct ib_udata *udata)
1924 {
1925 	struct ib_pd *pd = mr->pd;
1926 	struct ib_dm *dm = mr->dm;
1927 	struct ib_sig_attrs *sig_attrs = mr->sig_attrs;
1928 	int ret;
1929 
1930 	ret = mr->device->dereg_mr(mr, udata);
1931 	if (!ret) {
1932 		atomic_dec(&pd->usecnt);
1933 		if (dm)
1934 			atomic_dec(&dm->usecnt);
1935 		kfree(sig_attrs);
1936 	}
1937 
1938 	return ret;
1939 }
1940 EXPORT_SYMBOL(ib_dereg_mr_user);
1941 
1942 /**
1943  * ib_alloc_mr_user() - Allocates a memory region
1944  * @pd:            protection domain associated with the region
1945  * @mr_type:       memory region type
1946  * @max_num_sg:    maximum sg entries available for registration.
1947  * @udata:	   user data or null for kernel objects
1948  *
1949  * Notes:
1950  * Memory registeration page/sg lists must not exceed max_num_sg.
1951  * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
1952  * max_num_sg * used_page_size.
1953  *
1954  */
ib_alloc_mr_user(struct ib_pd * pd,enum ib_mr_type mr_type,u32 max_num_sg,struct ib_udata * udata)1955 struct ib_mr *ib_alloc_mr_user(struct ib_pd *pd, enum ib_mr_type mr_type,
1956 			       u32 max_num_sg, struct ib_udata *udata)
1957 {
1958 	struct ib_mr *mr;
1959 
1960 	if (!pd->device->alloc_mr) {
1961 		mr = ERR_PTR(-EOPNOTSUPP);
1962 		goto out;
1963 	}
1964 
1965 	if (mr_type == IB_MR_TYPE_INTEGRITY) {
1966 		WARN_ON_ONCE(1);
1967 		mr = ERR_PTR(-EINVAL);
1968 		goto out;
1969 	}
1970 
1971 	mr = pd->device->alloc_mr(pd, mr_type, max_num_sg, udata);
1972 	if (!IS_ERR(mr)) {
1973 		mr->device  = pd->device;
1974 		mr->pd      = pd;
1975 		mr->dm      = NULL;
1976 		mr->uobject = NULL;
1977 		atomic_inc(&pd->usecnt);
1978 		mr->need_inval = false;
1979 		mr->type = mr_type;
1980 		mr->sig_attrs = NULL;
1981 	}
1982 
1983 out:
1984 	return mr;
1985 }
1986 EXPORT_SYMBOL(ib_alloc_mr_user);
1987 
1988 /* "Fast" memory regions */
1989 
ib_alloc_fmr(struct ib_pd * pd,int mr_access_flags,struct ib_fmr_attr * fmr_attr)1990 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
1991 			    int mr_access_flags,
1992 			    struct ib_fmr_attr *fmr_attr)
1993 {
1994 	struct ib_fmr *fmr;
1995 
1996 	if (!pd->device->alloc_fmr)
1997 		return ERR_PTR(-EOPNOTSUPP);
1998 
1999 	fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
2000 	if (!IS_ERR(fmr)) {
2001 		fmr->device = pd->device;
2002 		fmr->pd     = pd;
2003 		atomic_inc(&pd->usecnt);
2004 	}
2005 
2006 	return fmr;
2007 }
2008 EXPORT_SYMBOL(ib_alloc_fmr);
2009 
ib_unmap_fmr(struct list_head * fmr_list)2010 int ib_unmap_fmr(struct list_head *fmr_list)
2011 {
2012 	struct ib_fmr *fmr;
2013 
2014 	if (list_empty(fmr_list))
2015 		return 0;
2016 
2017 	fmr = list_entry(fmr_list->next, struct ib_fmr, list);
2018 	return fmr->device->unmap_fmr(fmr_list);
2019 }
2020 EXPORT_SYMBOL(ib_unmap_fmr);
2021 
ib_dealloc_fmr(struct ib_fmr * fmr)2022 int ib_dealloc_fmr(struct ib_fmr *fmr)
2023 {
2024 	struct ib_pd *pd;
2025 	int ret;
2026 
2027 	pd = fmr->pd;
2028 	ret = fmr->device->dealloc_fmr(fmr);
2029 	if (!ret)
2030 		atomic_dec(&pd->usecnt);
2031 
2032 	return ret;
2033 }
2034 EXPORT_SYMBOL(ib_dealloc_fmr);
2035 
2036 /* Multicast groups */
2037 
is_valid_mcast_lid(struct ib_qp * qp,u16 lid)2038 static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
2039 {
2040 	struct ib_qp_init_attr init_attr = {};
2041 	struct ib_qp_attr attr = {};
2042 	int num_eth_ports = 0;
2043 	int port;
2044 
2045 	/* If QP state >= init, it is assigned to a port and we can check this
2046 	 * port only.
2047 	 */
2048 	if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
2049 		if (attr.qp_state >= IB_QPS_INIT) {
2050 			if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
2051 			    IB_LINK_LAYER_INFINIBAND)
2052 				return true;
2053 			goto lid_check;
2054 		}
2055 	}
2056 
2057 	/* Can't get a quick answer, iterate over all ports */
2058 	for (port = 0; port < qp->device->phys_port_cnt; port++)
2059 		if (rdma_port_get_link_layer(qp->device, port) !=
2060 		    IB_LINK_LAYER_INFINIBAND)
2061 			num_eth_ports++;
2062 
2063 	/* If we have at lease one Ethernet port, RoCE annex declares that
2064 	 * multicast LID should be ignored. We can't tell at this step if the
2065 	 * QP belongs to an IB or Ethernet port.
2066 	 */
2067 	if (num_eth_ports)
2068 		return true;
2069 
2070 	/* If all the ports are IB, we can check according to IB spec. */
2071 lid_check:
2072 	return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
2073 		 lid == be16_to_cpu(IB_LID_PERMISSIVE));
2074 }
2075 
ib_attach_mcast(struct ib_qp * qp,union ib_gid * gid,u16 lid)2076 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
2077 {
2078 	int ret;
2079 
2080 	if (!qp->device->attach_mcast)
2081 		return -EOPNOTSUPP;
2082 
2083 	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2084 	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
2085 		return -EINVAL;
2086 
2087 	ret = qp->device->attach_mcast(qp, gid, lid);
2088 	if (!ret)
2089 		atomic_inc(&qp->usecnt);
2090 	return ret;
2091 }
2092 EXPORT_SYMBOL(ib_attach_mcast);
2093 
ib_detach_mcast(struct ib_qp * qp,union ib_gid * gid,u16 lid)2094 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
2095 {
2096 	int ret;
2097 
2098 	if (!qp->device->detach_mcast)
2099 		return -EOPNOTSUPP;
2100 
2101 	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2102 	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
2103 		return -EINVAL;
2104 
2105 	ret = qp->device->detach_mcast(qp, gid, lid);
2106 	if (!ret)
2107 		atomic_dec(&qp->usecnt);
2108 	return ret;
2109 }
2110 EXPORT_SYMBOL(ib_detach_mcast);
2111 
__ib_alloc_xrcd(struct ib_device * device,const char * caller)2112 struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller)
2113 {
2114 	struct ib_xrcd *xrcd;
2115 
2116 	if (!device->alloc_xrcd)
2117 		return ERR_PTR(-EOPNOTSUPP);
2118 
2119 	xrcd = device->alloc_xrcd(device, NULL);
2120 	if (!IS_ERR(xrcd)) {
2121 		xrcd->device = device;
2122 		xrcd->inode = NULL;
2123 		atomic_set(&xrcd->usecnt, 0);
2124 		mutex_init(&xrcd->tgt_qp_mutex);
2125 		INIT_LIST_HEAD(&xrcd->tgt_qp_list);
2126 	}
2127 
2128 	return xrcd;
2129 }
2130 EXPORT_SYMBOL(__ib_alloc_xrcd);
2131 
ib_dealloc_xrcd(struct ib_xrcd * xrcd,struct ib_udata * udata)2132 int ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
2133 {
2134 	struct ib_qp *qp;
2135 	int ret;
2136 
2137 	if (atomic_read(&xrcd->usecnt))
2138 		return -EBUSY;
2139 
2140 	while (!list_empty(&xrcd->tgt_qp_list)) {
2141 		qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
2142 		ret = ib_destroy_qp(qp);
2143 		if (ret)
2144 			return ret;
2145 	}
2146 	mutex_destroy(&xrcd->tgt_qp_mutex);
2147 
2148 	return xrcd->device->dealloc_xrcd(xrcd, udata);
2149 }
2150 EXPORT_SYMBOL(ib_dealloc_xrcd);
2151 
2152 /**
2153  * ib_create_wq - Creates a WQ associated with the specified protection
2154  * domain.
2155  * @pd: The protection domain associated with the WQ.
2156  * @wq_init_attr: A list of initial attributes required to create the
2157  * WQ. If WQ creation succeeds, then the attributes are updated to
2158  * the actual capabilities of the created WQ.
2159  *
2160  * wq_init_attr->max_wr and wq_init_attr->max_sge determine
2161  * the requested size of the WQ, and set to the actual values allocated
2162  * on return.
2163  * If ib_create_wq() succeeds, then max_wr and max_sge will always be
2164  * at least as large as the requested values.
2165  */
ib_create_wq(struct ib_pd * pd,struct ib_wq_init_attr * wq_attr)2166 struct ib_wq *ib_create_wq(struct ib_pd *pd,
2167 			   struct ib_wq_init_attr *wq_attr)
2168 {
2169 	struct ib_wq *wq;
2170 
2171 	if (!pd->device->create_wq)
2172 		return ERR_PTR(-EOPNOTSUPP);
2173 
2174 	wq = pd->device->create_wq(pd, wq_attr, NULL);
2175 	if (!IS_ERR(wq)) {
2176 		wq->event_handler = wq_attr->event_handler;
2177 		wq->wq_context = wq_attr->wq_context;
2178 		wq->wq_type = wq_attr->wq_type;
2179 		wq->cq = wq_attr->cq;
2180 		wq->device = pd->device;
2181 		wq->pd = pd;
2182 		wq->uobject = NULL;
2183 		atomic_inc(&pd->usecnt);
2184 		atomic_inc(&wq_attr->cq->usecnt);
2185 		atomic_set(&wq->usecnt, 0);
2186 	}
2187 	return wq;
2188 }
2189 EXPORT_SYMBOL(ib_create_wq);
2190 
2191 /**
2192  * ib_destroy_wq - Destroys the specified user WQ.
2193  * @wq: The WQ to destroy.
2194  * @udata: Valid user data
2195  */
ib_destroy_wq(struct ib_wq * wq,struct ib_udata * udata)2196 int ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
2197 {
2198 	struct ib_cq *cq = wq->cq;
2199 	struct ib_pd *pd = wq->pd;
2200 
2201 	if (atomic_read(&wq->usecnt))
2202 		return -EBUSY;
2203 
2204 	wq->device->destroy_wq(wq, udata);
2205 	atomic_dec(&pd->usecnt);
2206 	atomic_dec(&cq->usecnt);
2207 
2208 	return 0;
2209 }
2210 EXPORT_SYMBOL(ib_destroy_wq);
2211 
2212 /**
2213  * ib_modify_wq - Modifies the specified WQ.
2214  * @wq: The WQ to modify.
2215  * @wq_attr: On input, specifies the WQ attributes to modify.
2216  * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
2217  *   are being modified.
2218  * On output, the current values of selected WQ attributes are returned.
2219  */
ib_modify_wq(struct ib_wq * wq,struct ib_wq_attr * wq_attr,u32 wq_attr_mask)2220 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
2221 		 u32 wq_attr_mask)
2222 {
2223 	int err;
2224 
2225 	if (!wq->device->modify_wq)
2226 		return -EOPNOTSUPP;
2227 
2228 	err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
2229 	return err;
2230 }
2231 EXPORT_SYMBOL(ib_modify_wq);
2232 
2233 /*
2234  * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
2235  * @device: The device on which to create the rwq indirection table.
2236  * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
2237  * create the Indirection Table.
2238  *
2239  * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
2240  *	than the created ib_rwq_ind_table object and the caller is responsible
2241  *	for its memory allocation/free.
2242  */
ib_create_rwq_ind_table(struct ib_device * device,struct ib_rwq_ind_table_init_attr * init_attr)2243 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
2244 						 struct ib_rwq_ind_table_init_attr *init_attr)
2245 {
2246 	struct ib_rwq_ind_table *rwq_ind_table;
2247 	int i;
2248 	u32 table_size;
2249 
2250 	if (!device->create_rwq_ind_table)
2251 		return ERR_PTR(-EOPNOTSUPP);
2252 
2253 	table_size = (1 << init_attr->log_ind_tbl_size);
2254 	rwq_ind_table = device->create_rwq_ind_table(device,
2255 				init_attr, NULL);
2256 	if (IS_ERR(rwq_ind_table))
2257 		return rwq_ind_table;
2258 
2259 	rwq_ind_table->ind_tbl = init_attr->ind_tbl;
2260 	rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
2261 	rwq_ind_table->device = device;
2262 	rwq_ind_table->uobject = NULL;
2263 	atomic_set(&rwq_ind_table->usecnt, 0);
2264 
2265 	for (i = 0; i < table_size; i++)
2266 		atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
2267 
2268 	return rwq_ind_table;
2269 }
2270 EXPORT_SYMBOL(ib_create_rwq_ind_table);
2271 
2272 /*
2273  * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
2274  * @wq_ind_table: The Indirection Table to destroy.
2275 */
ib_destroy_rwq_ind_table(struct ib_rwq_ind_table * rwq_ind_table)2276 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
2277 {
2278 	int err, i;
2279 	u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
2280 	struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
2281 
2282 	if (atomic_read(&rwq_ind_table->usecnt))
2283 		return -EBUSY;
2284 
2285 	err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
2286 	if (!err) {
2287 		for (i = 0; i < table_size; i++)
2288 			atomic_dec(&ind_tbl[i]->usecnt);
2289 	}
2290 
2291 	return err;
2292 }
2293 EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
2294 
ib_check_mr_status(struct ib_mr * mr,u32 check_mask,struct ib_mr_status * mr_status)2295 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
2296 		       struct ib_mr_status *mr_status)
2297 {
2298 	return mr->device->check_mr_status ?
2299 		mr->device->check_mr_status(mr, check_mask, mr_status) : -EOPNOTSUPP;
2300 }
2301 EXPORT_SYMBOL(ib_check_mr_status);
2302 
ib_set_vf_link_state(struct ib_device * device,int vf,u8 port,int state)2303 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
2304 			 int state)
2305 {
2306 	if (!device->set_vf_link_state)
2307 		return -EOPNOTSUPP;
2308 
2309 	return device->set_vf_link_state(device, vf, port, state);
2310 }
2311 EXPORT_SYMBOL(ib_set_vf_link_state);
2312 
ib_get_vf_config(struct ib_device * device,int vf,u8 port,struct ifla_vf_info * info)2313 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
2314 		     struct ifla_vf_info *info)
2315 {
2316 	if (!device->get_vf_config)
2317 		return -EOPNOTSUPP;
2318 
2319 	return device->get_vf_config(device, vf, port, info);
2320 }
2321 EXPORT_SYMBOL(ib_get_vf_config);
2322 
ib_get_vf_stats(struct ib_device * device,int vf,u8 port,struct ifla_vf_stats * stats)2323 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
2324 		    struct ifla_vf_stats *stats)
2325 {
2326 	if (!device->get_vf_stats)
2327 		return -EOPNOTSUPP;
2328 
2329 	return device->get_vf_stats(device, vf, port, stats);
2330 }
2331 EXPORT_SYMBOL(ib_get_vf_stats);
2332 
ib_set_vf_guid(struct ib_device * device,int vf,u8 port,u64 guid,int type)2333 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
2334 		   int type)
2335 {
2336 	if (!device->set_vf_guid)
2337 		return -EOPNOTSUPP;
2338 
2339 	return device->set_vf_guid(device, vf, port, guid, type);
2340 }
2341 EXPORT_SYMBOL(ib_set_vf_guid);
2342 
2343 /**
2344  * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
2345  *     and set it the memory region.
2346  * @mr:            memory region
2347  * @sg:            dma mapped scatterlist
2348  * @sg_nents:      number of entries in sg
2349  * @sg_offset:     offset in bytes into sg
2350  * @page_size:     page vector desired page size
2351  *
2352  * Constraints:
2353  * - The first sg element is allowed to have an offset.
2354  * - Each sg element must either be aligned to page_size or virtually
2355  *   contiguous to the previous element. In case an sg element has a
2356  *   non-contiguous offset, the mapping prefix will not include it.
2357  * - The last sg element is allowed to have length less than page_size.
2358  * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
2359  *   then only max_num_sg entries will be mapped.
2360  * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
2361  *   constraints holds and the page_size argument is ignored.
2362  *
2363  * Returns the number of sg elements that were mapped to the memory region.
2364  *
2365  * After this completes successfully, the  memory region
2366  * is ready for registration.
2367  */
ib_map_mr_sg(struct ib_mr * mr,struct scatterlist * sg,int sg_nents,unsigned int * sg_offset,unsigned int page_size)2368 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
2369 		 unsigned int *sg_offset, unsigned int page_size)
2370 {
2371 	if (unlikely(!mr->device->map_mr_sg))
2372 		return -EOPNOTSUPP;
2373 
2374 	mr->page_size = page_size;
2375 
2376 	return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
2377 }
2378 EXPORT_SYMBOL(ib_map_mr_sg);
2379 
2380 /**
2381  * ib_sg_to_pages() - Convert the largest prefix of a sg list
2382  *     to a page vector
2383  * @mr:            memory region
2384  * @sgl:           dma mapped scatterlist
2385  * @sg_nents:      number of entries in sg
2386  * @sg_offset_p:   IN:  start offset in bytes into sg
2387  *                 OUT: offset in bytes for element n of the sg of the first
2388  *                      byte that has not been processed where n is the return
2389  *                      value of this function.
2390  * @set_page:      driver page assignment function pointer
2391  *
2392  * Core service helper for drivers to convert the largest
2393  * prefix of given sg list to a page vector. The sg list
2394  * prefix converted is the prefix that meet the requirements
2395  * of ib_map_mr_sg.
2396  *
2397  * Returns the number of sg elements that were assigned to
2398  * a page vector.
2399  */
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))2400 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
2401 		unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
2402 {
2403 	struct scatterlist *sg;
2404 	u64 last_end_dma_addr = 0;
2405 	unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
2406 	unsigned int last_page_off = 0;
2407 	u64 page_mask = ~((u64)mr->page_size - 1);
2408 	int i, ret;
2409 
2410 	if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
2411 		return -EINVAL;
2412 
2413 	mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
2414 	mr->length = 0;
2415 
2416 	for_each_sg(sgl, sg, sg_nents, i) {
2417 		u64 dma_addr = sg_dma_address(sg) + sg_offset;
2418 		u64 prev_addr = dma_addr;
2419 		unsigned int dma_len = sg_dma_len(sg) - sg_offset;
2420 		u64 end_dma_addr = dma_addr + dma_len;
2421 		u64 page_addr = dma_addr & page_mask;
2422 
2423 		/*
2424 		 * For the second and later elements, check whether either the
2425 		 * end of element i-1 or the start of element i is not aligned
2426 		 * on a page boundary.
2427 		 */
2428 		if (i && (last_page_off != 0 || page_addr != dma_addr)) {
2429 			/* Stop mapping if there is a gap. */
2430 			if (last_end_dma_addr != dma_addr)
2431 				break;
2432 
2433 			/*
2434 			 * Coalesce this element with the last. If it is small
2435 			 * enough just update mr->length. Otherwise start
2436 			 * mapping from the next page.
2437 			 */
2438 			goto next_page;
2439 		}
2440 
2441 		do {
2442 			ret = set_page(mr, page_addr);
2443 			if (unlikely(ret < 0)) {
2444 				sg_offset = prev_addr - sg_dma_address(sg);
2445 				mr->length += prev_addr - dma_addr;
2446 				if (sg_offset_p)
2447 					*sg_offset_p = sg_offset;
2448 				return i || sg_offset ? i : ret;
2449 			}
2450 			prev_addr = page_addr;
2451 next_page:
2452 			page_addr += mr->page_size;
2453 		} while (page_addr < end_dma_addr);
2454 
2455 		mr->length += dma_len;
2456 		last_end_dma_addr = end_dma_addr;
2457 		last_page_off = end_dma_addr & ~page_mask;
2458 
2459 		sg_offset = 0;
2460 	}
2461 
2462 	if (sg_offset_p)
2463 		*sg_offset_p = 0;
2464 	return i;
2465 }
2466 EXPORT_SYMBOL(ib_sg_to_pages);
2467 
2468 struct ib_drain_cqe {
2469 	struct ib_cqe cqe;
2470 	struct completion done;
2471 };
2472 
ib_drain_qp_done(struct ib_cq * cq,struct ib_wc * wc)2473 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
2474 {
2475 	struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
2476 						cqe);
2477 
2478 	complete(&cqe->done);
2479 }
2480 
2481 /*
2482  * Post a WR and block until its completion is reaped for the SQ.
2483  */
__ib_drain_sq(struct ib_qp * qp)2484 static void __ib_drain_sq(struct ib_qp *qp)
2485 {
2486 	struct ib_cq *cq = qp->send_cq;
2487 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2488 	struct ib_drain_cqe sdrain;
2489 	struct ib_rdma_wr swr = {
2490 		.wr = {
2491 			.opcode	= IB_WR_RDMA_WRITE,
2492 			.wr_cqe	= &sdrain.cqe,
2493 		},
2494 	};
2495 	int ret;
2496 
2497 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2498 	if (ret) {
2499 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2500 		return;
2501 	}
2502 
2503 	sdrain.cqe.done = ib_drain_qp_done;
2504 	init_completion(&sdrain.done);
2505 
2506 	ret = ib_post_send(qp, &swr.wr, NULL);
2507 	if (ret) {
2508 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2509 		return;
2510 	}
2511 
2512 	if (cq->poll_ctx == IB_POLL_DIRECT)
2513 		while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
2514 			ib_process_cq_direct(cq, -1);
2515 	else
2516 		wait_for_completion(&sdrain.done);
2517 }
2518 
2519 /*
2520  * Post a WR and block until its completion is reaped for the RQ.
2521  */
__ib_drain_rq(struct ib_qp * qp)2522 static void __ib_drain_rq(struct ib_qp *qp)
2523 {
2524 	struct ib_cq *cq = qp->recv_cq;
2525 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2526 	struct ib_drain_cqe rdrain;
2527 	struct ib_recv_wr rwr = {};
2528 	int ret;
2529 
2530 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2531 	if (ret) {
2532 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2533 		return;
2534 	}
2535 
2536 	rwr.wr_cqe = &rdrain.cqe;
2537 	rdrain.cqe.done = ib_drain_qp_done;
2538 	init_completion(&rdrain.done);
2539 
2540 	ret = ib_post_recv(qp, &rwr, NULL);
2541 	if (ret) {
2542 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2543 		return;
2544 	}
2545 
2546 	if (cq->poll_ctx == IB_POLL_DIRECT)
2547 		while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
2548 			ib_process_cq_direct(cq, -1);
2549 	else
2550 		wait_for_completion(&rdrain.done);
2551 }
2552 
2553 /**
2554  * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2555  *		   application.
2556  * @qp:            queue pair to drain
2557  *
2558  * If the device has a provider-specific drain function, then
2559  * call that.  Otherwise call the generic drain function
2560  * __ib_drain_sq().
2561  *
2562  * The caller must:
2563  *
2564  * ensure there is room in the CQ and SQ for the drain work request and
2565  * completion.
2566  *
2567  * allocate the CQ using ib_alloc_cq().
2568  *
2569  * ensure that there are no other contexts that are posting WRs concurrently.
2570  * Otherwise the drain is not guaranteed.
2571  */
ib_drain_sq(struct ib_qp * qp)2572 void ib_drain_sq(struct ib_qp *qp)
2573 {
2574 	if (qp->device->drain_sq)
2575 		qp->device->drain_sq(qp);
2576 	else
2577 		__ib_drain_sq(qp);
2578 }
2579 EXPORT_SYMBOL(ib_drain_sq);
2580 
2581 /**
2582  * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2583  *		   application.
2584  * @qp:            queue pair to drain
2585  *
2586  * If the device has a provider-specific drain function, then
2587  * call that.  Otherwise call the generic drain function
2588  * __ib_drain_rq().
2589  *
2590  * The caller must:
2591  *
2592  * ensure there is room in the CQ and RQ for the drain work request and
2593  * completion.
2594  *
2595  * allocate the CQ using ib_alloc_cq().
2596  *
2597  * ensure that there are no other contexts that are posting WRs concurrently.
2598  * Otherwise the drain is not guaranteed.
2599  */
ib_drain_rq(struct ib_qp * qp)2600 void ib_drain_rq(struct ib_qp *qp)
2601 {
2602 	if (qp->device->drain_rq)
2603 		qp->device->drain_rq(qp);
2604 	else
2605 		__ib_drain_rq(qp);
2606 }
2607 EXPORT_SYMBOL(ib_drain_rq);
2608 
2609 /**
2610  * ib_drain_qp() - Block until all CQEs have been consumed by the
2611  *		   application on both the RQ and SQ.
2612  * @qp:            queue pair to drain
2613  *
2614  * The caller must:
2615  *
2616  * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2617  * and completions.
2618  *
2619  * allocate the CQs using ib_alloc_cq().
2620  *
2621  * ensure that there are no other contexts that are posting WRs concurrently.
2622  * Otherwise the drain is not guaranteed.
2623  */
ib_drain_qp(struct ib_qp * qp)2624 void ib_drain_qp(struct ib_qp *qp)
2625 {
2626 	ib_drain_sq(qp);
2627 	if (!qp->srq)
2628 		ib_drain_rq(qp);
2629 }
2630 EXPORT_SYMBOL(ib_drain_qp);
2631