xref: /openbmc/linux/drivers/infiniband/core/verbs.c (revision e7bae9bb)
1 /*
2  * Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
3  * Copyright (c) 2004 Infinicon Corporation.  All rights reserved.
4  * Copyright (c) 2004 Intel Corporation.  All rights reserved.
5  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
6  * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
7  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
8  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
9  *
10  * This software is available to you under a choice of one of two
11  * licenses.  You may choose to be licensed under the terms of the GNU
12  * General Public License (GPL) Version 2, available from the file
13  * COPYING in the main directory of this source tree, or the
14  * OpenIB.org BSD license below:
15  *
16  *     Redistribution and use in source and binary forms, with or
17  *     without modification, are permitted provided that the following
18  *     conditions are met:
19  *
20  *      - Redistributions of source code must retain the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer.
23  *
24  *      - Redistributions in binary form must reproduce the above
25  *        copyright notice, this list of conditions and the following
26  *        disclaimer in the documentation and/or other materials
27  *        provided with the distribution.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36  * SOFTWARE.
37  */
38 
39 #include <linux/errno.h>
40 #include <linux/err.h>
41 #include <linux/export.h>
42 #include <linux/string.h>
43 #include <linux/slab.h>
44 #include <linux/in.h>
45 #include <linux/in6.h>
46 #include <net/addrconf.h>
47 #include <linux/security.h>
48 
49 #include <rdma/ib_verbs.h>
50 #include <rdma/ib_cache.h>
51 #include <rdma/ib_addr.h>
52 #include <rdma/rw.h>
53 #include <rdma/lag.h>
54 
55 #include "core_priv.h"
56 #include <trace/events/rdma_core.h>
57 
58 static int ib_resolve_eth_dmac(struct ib_device *device,
59 			       struct rdma_ah_attr *ah_attr);
60 
61 static const char * const ib_events[] = {
62 	[IB_EVENT_CQ_ERR]		= "CQ error",
63 	[IB_EVENT_QP_FATAL]		= "QP fatal error",
64 	[IB_EVENT_QP_REQ_ERR]		= "QP request error",
65 	[IB_EVENT_QP_ACCESS_ERR]	= "QP access error",
66 	[IB_EVENT_COMM_EST]		= "communication established",
67 	[IB_EVENT_SQ_DRAINED]		= "send queue drained",
68 	[IB_EVENT_PATH_MIG]		= "path migration successful",
69 	[IB_EVENT_PATH_MIG_ERR]		= "path migration error",
70 	[IB_EVENT_DEVICE_FATAL]		= "device fatal error",
71 	[IB_EVENT_PORT_ACTIVE]		= "port active",
72 	[IB_EVENT_PORT_ERR]		= "port error",
73 	[IB_EVENT_LID_CHANGE]		= "LID change",
74 	[IB_EVENT_PKEY_CHANGE]		= "P_key change",
75 	[IB_EVENT_SM_CHANGE]		= "SM change",
76 	[IB_EVENT_SRQ_ERR]		= "SRQ error",
77 	[IB_EVENT_SRQ_LIMIT_REACHED]	= "SRQ limit reached",
78 	[IB_EVENT_QP_LAST_WQE_REACHED]	= "last WQE reached",
79 	[IB_EVENT_CLIENT_REREGISTER]	= "client reregister",
80 	[IB_EVENT_GID_CHANGE]		= "GID changed",
81 };
82 
83 const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
84 {
85 	size_t index = event;
86 
87 	return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
88 			ib_events[index] : "unrecognized event";
89 }
90 EXPORT_SYMBOL(ib_event_msg);
91 
92 static const char * const wc_statuses[] = {
93 	[IB_WC_SUCCESS]			= "success",
94 	[IB_WC_LOC_LEN_ERR]		= "local length error",
95 	[IB_WC_LOC_QP_OP_ERR]		= "local QP operation error",
96 	[IB_WC_LOC_EEC_OP_ERR]		= "local EE context operation error",
97 	[IB_WC_LOC_PROT_ERR]		= "local protection error",
98 	[IB_WC_WR_FLUSH_ERR]		= "WR flushed",
99 	[IB_WC_MW_BIND_ERR]		= "memory management operation error",
100 	[IB_WC_BAD_RESP_ERR]		= "bad response error",
101 	[IB_WC_LOC_ACCESS_ERR]		= "local access error",
102 	[IB_WC_REM_INV_REQ_ERR]		= "invalid request error",
103 	[IB_WC_REM_ACCESS_ERR]		= "remote access error",
104 	[IB_WC_REM_OP_ERR]		= "remote operation error",
105 	[IB_WC_RETRY_EXC_ERR]		= "transport retry counter exceeded",
106 	[IB_WC_RNR_RETRY_EXC_ERR]	= "RNR retry counter exceeded",
107 	[IB_WC_LOC_RDD_VIOL_ERR]	= "local RDD violation error",
108 	[IB_WC_REM_INV_RD_REQ_ERR]	= "remote invalid RD request",
109 	[IB_WC_REM_ABORT_ERR]		= "operation aborted",
110 	[IB_WC_INV_EECN_ERR]		= "invalid EE context number",
111 	[IB_WC_INV_EEC_STATE_ERR]	= "invalid EE context state",
112 	[IB_WC_FATAL_ERR]		= "fatal error",
113 	[IB_WC_RESP_TIMEOUT_ERR]	= "response timeout error",
114 	[IB_WC_GENERAL_ERR]		= "general error",
115 };
116 
117 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
118 {
119 	size_t index = status;
120 
121 	return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
122 			wc_statuses[index] : "unrecognized status";
123 }
124 EXPORT_SYMBOL(ib_wc_status_msg);
125 
126 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
127 {
128 	switch (rate) {
129 	case IB_RATE_2_5_GBPS: return   1;
130 	case IB_RATE_5_GBPS:   return   2;
131 	case IB_RATE_10_GBPS:  return   4;
132 	case IB_RATE_20_GBPS:  return   8;
133 	case IB_RATE_30_GBPS:  return  12;
134 	case IB_RATE_40_GBPS:  return  16;
135 	case IB_RATE_60_GBPS:  return  24;
136 	case IB_RATE_80_GBPS:  return  32;
137 	case IB_RATE_120_GBPS: return  48;
138 	case IB_RATE_14_GBPS:  return   6;
139 	case IB_RATE_56_GBPS:  return  22;
140 	case IB_RATE_112_GBPS: return  45;
141 	case IB_RATE_168_GBPS: return  67;
142 	case IB_RATE_25_GBPS:  return  10;
143 	case IB_RATE_100_GBPS: return  40;
144 	case IB_RATE_200_GBPS: return  80;
145 	case IB_RATE_300_GBPS: return 120;
146 	case IB_RATE_28_GBPS:  return  11;
147 	case IB_RATE_50_GBPS:  return  20;
148 	case IB_RATE_400_GBPS: return 160;
149 	case IB_RATE_600_GBPS: return 240;
150 	default:	       return  -1;
151 	}
152 }
153 EXPORT_SYMBOL(ib_rate_to_mult);
154 
155 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
156 {
157 	switch (mult) {
158 	case 1:   return IB_RATE_2_5_GBPS;
159 	case 2:   return IB_RATE_5_GBPS;
160 	case 4:   return IB_RATE_10_GBPS;
161 	case 8:   return IB_RATE_20_GBPS;
162 	case 12:  return IB_RATE_30_GBPS;
163 	case 16:  return IB_RATE_40_GBPS;
164 	case 24:  return IB_RATE_60_GBPS;
165 	case 32:  return IB_RATE_80_GBPS;
166 	case 48:  return IB_RATE_120_GBPS;
167 	case 6:   return IB_RATE_14_GBPS;
168 	case 22:  return IB_RATE_56_GBPS;
169 	case 45:  return IB_RATE_112_GBPS;
170 	case 67:  return IB_RATE_168_GBPS;
171 	case 10:  return IB_RATE_25_GBPS;
172 	case 40:  return IB_RATE_100_GBPS;
173 	case 80:  return IB_RATE_200_GBPS;
174 	case 120: return IB_RATE_300_GBPS;
175 	case 11:  return IB_RATE_28_GBPS;
176 	case 20:  return IB_RATE_50_GBPS;
177 	case 160: return IB_RATE_400_GBPS;
178 	case 240: return IB_RATE_600_GBPS;
179 	default:  return IB_RATE_PORT_CURRENT;
180 	}
181 }
182 EXPORT_SYMBOL(mult_to_ib_rate);
183 
184 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
185 {
186 	switch (rate) {
187 	case IB_RATE_2_5_GBPS: return 2500;
188 	case IB_RATE_5_GBPS:   return 5000;
189 	case IB_RATE_10_GBPS:  return 10000;
190 	case IB_RATE_20_GBPS:  return 20000;
191 	case IB_RATE_30_GBPS:  return 30000;
192 	case IB_RATE_40_GBPS:  return 40000;
193 	case IB_RATE_60_GBPS:  return 60000;
194 	case IB_RATE_80_GBPS:  return 80000;
195 	case IB_RATE_120_GBPS: return 120000;
196 	case IB_RATE_14_GBPS:  return 14062;
197 	case IB_RATE_56_GBPS:  return 56250;
198 	case IB_RATE_112_GBPS: return 112500;
199 	case IB_RATE_168_GBPS: return 168750;
200 	case IB_RATE_25_GBPS:  return 25781;
201 	case IB_RATE_100_GBPS: return 103125;
202 	case IB_RATE_200_GBPS: return 206250;
203 	case IB_RATE_300_GBPS: return 309375;
204 	case IB_RATE_28_GBPS:  return 28125;
205 	case IB_RATE_50_GBPS:  return 53125;
206 	case IB_RATE_400_GBPS: return 425000;
207 	case IB_RATE_600_GBPS: return 637500;
208 	default:	       return -1;
209 	}
210 }
211 EXPORT_SYMBOL(ib_rate_to_mbps);
212 
213 __attribute_const__ enum rdma_transport_type
214 rdma_node_get_transport(unsigned int node_type)
215 {
216 
217 	if (node_type == RDMA_NODE_USNIC)
218 		return RDMA_TRANSPORT_USNIC;
219 	if (node_type == RDMA_NODE_USNIC_UDP)
220 		return RDMA_TRANSPORT_USNIC_UDP;
221 	if (node_type == RDMA_NODE_RNIC)
222 		return RDMA_TRANSPORT_IWARP;
223 	if (node_type == RDMA_NODE_UNSPECIFIED)
224 		return RDMA_TRANSPORT_UNSPECIFIED;
225 
226 	return RDMA_TRANSPORT_IB;
227 }
228 EXPORT_SYMBOL(rdma_node_get_transport);
229 
230 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
231 {
232 	enum rdma_transport_type lt;
233 	if (device->ops.get_link_layer)
234 		return device->ops.get_link_layer(device, port_num);
235 
236 	lt = rdma_node_get_transport(device->node_type);
237 	if (lt == RDMA_TRANSPORT_IB)
238 		return IB_LINK_LAYER_INFINIBAND;
239 
240 	return IB_LINK_LAYER_ETHERNET;
241 }
242 EXPORT_SYMBOL(rdma_port_get_link_layer);
243 
244 /* Protection domains */
245 
246 /**
247  * ib_alloc_pd - Allocates an unused protection domain.
248  * @device: The device on which to allocate the protection domain.
249  * @flags: protection domain flags
250  * @caller: caller's build-time module name
251  *
252  * A protection domain object provides an association between QPs, shared
253  * receive queues, address handles, memory regions, and memory windows.
254  *
255  * Every PD has a local_dma_lkey which can be used as the lkey value for local
256  * memory operations.
257  */
258 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
259 		const char *caller)
260 {
261 	struct ib_pd *pd;
262 	int mr_access_flags = 0;
263 	int ret;
264 
265 	pd = rdma_zalloc_drv_obj(device, ib_pd);
266 	if (!pd)
267 		return ERR_PTR(-ENOMEM);
268 
269 	pd->device = device;
270 	pd->uobject = NULL;
271 	pd->__internal_mr = NULL;
272 	atomic_set(&pd->usecnt, 0);
273 	pd->flags = flags;
274 
275 	pd->res.type = RDMA_RESTRACK_PD;
276 	rdma_restrack_set_task(&pd->res, caller);
277 
278 	ret = device->ops.alloc_pd(pd, NULL);
279 	if (ret) {
280 		kfree(pd);
281 		return ERR_PTR(ret);
282 	}
283 	rdma_restrack_kadd(&pd->res);
284 
285 	if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
286 		pd->local_dma_lkey = device->local_dma_lkey;
287 	else
288 		mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
289 
290 	if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
291 		pr_warn("%s: enabling unsafe global rkey\n", caller);
292 		mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
293 	}
294 
295 	if (mr_access_flags) {
296 		struct ib_mr *mr;
297 
298 		mr = pd->device->ops.get_dma_mr(pd, mr_access_flags);
299 		if (IS_ERR(mr)) {
300 			ib_dealloc_pd(pd);
301 			return ERR_CAST(mr);
302 		}
303 
304 		mr->device	= pd->device;
305 		mr->pd		= pd;
306 		mr->type        = IB_MR_TYPE_DMA;
307 		mr->uobject	= NULL;
308 		mr->need_inval	= false;
309 
310 		pd->__internal_mr = mr;
311 
312 		if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
313 			pd->local_dma_lkey = pd->__internal_mr->lkey;
314 
315 		if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
316 			pd->unsafe_global_rkey = pd->__internal_mr->rkey;
317 	}
318 
319 	return pd;
320 }
321 EXPORT_SYMBOL(__ib_alloc_pd);
322 
323 /**
324  * ib_dealloc_pd_user - Deallocates a protection domain.
325  * @pd: The protection domain to deallocate.
326  * @udata: Valid user data or NULL for kernel object
327  *
328  * It is an error to call this function while any resources in the pd still
329  * exist.  The caller is responsible to synchronously destroy them and
330  * guarantee no new allocations will happen.
331  */
332 void ib_dealloc_pd_user(struct ib_pd *pd, struct ib_udata *udata)
333 {
334 	int ret;
335 
336 	if (pd->__internal_mr) {
337 		ret = pd->device->ops.dereg_mr(pd->__internal_mr, NULL);
338 		WARN_ON(ret);
339 		pd->__internal_mr = NULL;
340 	}
341 
342 	/* uverbs manipulates usecnt with proper locking, while the kabi
343 	   requires the caller to guarantee we can't race here. */
344 	WARN_ON(atomic_read(&pd->usecnt));
345 
346 	rdma_restrack_del(&pd->res);
347 	pd->device->ops.dealloc_pd(pd, udata);
348 	kfree(pd);
349 }
350 EXPORT_SYMBOL(ib_dealloc_pd_user);
351 
352 /* Address handles */
353 
354 /**
355  * rdma_copy_ah_attr - Copy rdma ah attribute from source to destination.
356  * @dest:       Pointer to destination ah_attr. Contents of the destination
357  *              pointer is assumed to be invalid and attribute are overwritten.
358  * @src:        Pointer to source ah_attr.
359  */
360 void rdma_copy_ah_attr(struct rdma_ah_attr *dest,
361 		       const struct rdma_ah_attr *src)
362 {
363 	*dest = *src;
364 	if (dest->grh.sgid_attr)
365 		rdma_hold_gid_attr(dest->grh.sgid_attr);
366 }
367 EXPORT_SYMBOL(rdma_copy_ah_attr);
368 
369 /**
370  * rdma_replace_ah_attr - Replace valid ah_attr with new new one.
371  * @old:        Pointer to existing ah_attr which needs to be replaced.
372  *              old is assumed to be valid or zero'd
373  * @new:        Pointer to the new ah_attr.
374  *
375  * rdma_replace_ah_attr() first releases any reference in the old ah_attr if
376  * old the ah_attr is valid; after that it copies the new attribute and holds
377  * the reference to the replaced ah_attr.
378  */
379 void rdma_replace_ah_attr(struct rdma_ah_attr *old,
380 			  const struct rdma_ah_attr *new)
381 {
382 	rdma_destroy_ah_attr(old);
383 	*old = *new;
384 	if (old->grh.sgid_attr)
385 		rdma_hold_gid_attr(old->grh.sgid_attr);
386 }
387 EXPORT_SYMBOL(rdma_replace_ah_attr);
388 
389 /**
390  * rdma_move_ah_attr - Move ah_attr pointed by source to destination.
391  * @dest:       Pointer to destination ah_attr to copy to.
392  *              dest is assumed to be valid or zero'd
393  * @src:        Pointer to the new ah_attr.
394  *
395  * rdma_move_ah_attr() first releases any reference in the destination ah_attr
396  * if it is valid. This also transfers ownership of internal references from
397  * src to dest, making src invalid in the process. No new reference of the src
398  * ah_attr is taken.
399  */
400 void rdma_move_ah_attr(struct rdma_ah_attr *dest, struct rdma_ah_attr *src)
401 {
402 	rdma_destroy_ah_attr(dest);
403 	*dest = *src;
404 	src->grh.sgid_attr = NULL;
405 }
406 EXPORT_SYMBOL(rdma_move_ah_attr);
407 
408 /*
409  * Validate that the rdma_ah_attr is valid for the device before passing it
410  * off to the driver.
411  */
412 static int rdma_check_ah_attr(struct ib_device *device,
413 			      struct rdma_ah_attr *ah_attr)
414 {
415 	if (!rdma_is_port_valid(device, ah_attr->port_num))
416 		return -EINVAL;
417 
418 	if ((rdma_is_grh_required(device, ah_attr->port_num) ||
419 	     ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) &&
420 	    !(ah_attr->ah_flags & IB_AH_GRH))
421 		return -EINVAL;
422 
423 	if (ah_attr->grh.sgid_attr) {
424 		/*
425 		 * Make sure the passed sgid_attr is consistent with the
426 		 * parameters
427 		 */
428 		if (ah_attr->grh.sgid_attr->index != ah_attr->grh.sgid_index ||
429 		    ah_attr->grh.sgid_attr->port_num != ah_attr->port_num)
430 			return -EINVAL;
431 	}
432 	return 0;
433 }
434 
435 /*
436  * If the ah requires a GRH then ensure that sgid_attr pointer is filled in.
437  * On success the caller is responsible to call rdma_unfill_sgid_attr().
438  */
439 static int rdma_fill_sgid_attr(struct ib_device *device,
440 			       struct rdma_ah_attr *ah_attr,
441 			       const struct ib_gid_attr **old_sgid_attr)
442 {
443 	const struct ib_gid_attr *sgid_attr;
444 	struct ib_global_route *grh;
445 	int ret;
446 
447 	*old_sgid_attr = ah_attr->grh.sgid_attr;
448 
449 	ret = rdma_check_ah_attr(device, ah_attr);
450 	if (ret)
451 		return ret;
452 
453 	if (!(ah_attr->ah_flags & IB_AH_GRH))
454 		return 0;
455 
456 	grh = rdma_ah_retrieve_grh(ah_attr);
457 	if (grh->sgid_attr)
458 		return 0;
459 
460 	sgid_attr =
461 		rdma_get_gid_attr(device, ah_attr->port_num, grh->sgid_index);
462 	if (IS_ERR(sgid_attr))
463 		return PTR_ERR(sgid_attr);
464 
465 	/* Move ownerhip of the kref into the ah_attr */
466 	grh->sgid_attr = sgid_attr;
467 	return 0;
468 }
469 
470 static void rdma_unfill_sgid_attr(struct rdma_ah_attr *ah_attr,
471 				  const struct ib_gid_attr *old_sgid_attr)
472 {
473 	/*
474 	 * Fill didn't change anything, the caller retains ownership of
475 	 * whatever it passed
476 	 */
477 	if (ah_attr->grh.sgid_attr == old_sgid_attr)
478 		return;
479 
480 	/*
481 	 * Otherwise, we need to undo what rdma_fill_sgid_attr so the caller
482 	 * doesn't see any change in the rdma_ah_attr. If we get here
483 	 * old_sgid_attr is NULL.
484 	 */
485 	rdma_destroy_ah_attr(ah_attr);
486 }
487 
488 static const struct ib_gid_attr *
489 rdma_update_sgid_attr(struct rdma_ah_attr *ah_attr,
490 		      const struct ib_gid_attr *old_attr)
491 {
492 	if (old_attr)
493 		rdma_put_gid_attr(old_attr);
494 	if (ah_attr->ah_flags & IB_AH_GRH) {
495 		rdma_hold_gid_attr(ah_attr->grh.sgid_attr);
496 		return ah_attr->grh.sgid_attr;
497 	}
498 	return NULL;
499 }
500 
501 static struct ib_ah *_rdma_create_ah(struct ib_pd *pd,
502 				     struct rdma_ah_attr *ah_attr,
503 				     u32 flags,
504 				     struct ib_udata *udata,
505 				     struct net_device *xmit_slave)
506 {
507 	struct rdma_ah_init_attr init_attr = {};
508 	struct ib_device *device = pd->device;
509 	struct ib_ah *ah;
510 	int ret;
511 
512 	might_sleep_if(flags & RDMA_CREATE_AH_SLEEPABLE);
513 
514 	if (!device->ops.create_ah)
515 		return ERR_PTR(-EOPNOTSUPP);
516 
517 	ah = rdma_zalloc_drv_obj_gfp(
518 		device, ib_ah,
519 		(flags & RDMA_CREATE_AH_SLEEPABLE) ? GFP_KERNEL : GFP_ATOMIC);
520 	if (!ah)
521 		return ERR_PTR(-ENOMEM);
522 
523 	ah->device = device;
524 	ah->pd = pd;
525 	ah->type = ah_attr->type;
526 	ah->sgid_attr = rdma_update_sgid_attr(ah_attr, NULL);
527 	init_attr.ah_attr = ah_attr;
528 	init_attr.flags = flags;
529 	init_attr.xmit_slave = xmit_slave;
530 
531 	ret = device->ops.create_ah(ah, &init_attr, udata);
532 	if (ret) {
533 		kfree(ah);
534 		return ERR_PTR(ret);
535 	}
536 
537 	atomic_inc(&pd->usecnt);
538 	return ah;
539 }
540 
541 /**
542  * rdma_create_ah - Creates an address handle for the
543  * given address vector.
544  * @pd: The protection domain associated with the address handle.
545  * @ah_attr: The attributes of the address vector.
546  * @flags: Create address handle flags (see enum rdma_create_ah_flags).
547  *
548  * It returns 0 on success and returns appropriate error code on error.
549  * The address handle is used to reference a local or global destination
550  * in all UD QP post sends.
551  */
552 struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
553 			     u32 flags)
554 {
555 	const struct ib_gid_attr *old_sgid_attr;
556 	struct net_device *slave;
557 	struct ib_ah *ah;
558 	int ret;
559 
560 	ret = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
561 	if (ret)
562 		return ERR_PTR(ret);
563 	slave = rdma_lag_get_ah_roce_slave(pd->device, ah_attr,
564 					   (flags & RDMA_CREATE_AH_SLEEPABLE) ?
565 					   GFP_KERNEL : GFP_ATOMIC);
566 	if (IS_ERR(slave)) {
567 		rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
568 		return (void *)slave;
569 	}
570 	ah = _rdma_create_ah(pd, ah_attr, flags, NULL, slave);
571 	rdma_lag_put_ah_roce_slave(slave);
572 	rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
573 	return ah;
574 }
575 EXPORT_SYMBOL(rdma_create_ah);
576 
577 /**
578  * rdma_create_user_ah - Creates an address handle for the
579  * given address vector.
580  * It resolves destination mac address for ah attribute of RoCE type.
581  * @pd: The protection domain associated with the address handle.
582  * @ah_attr: The attributes of the address vector.
583  * @udata: pointer to user's input output buffer information need by
584  *         provider driver.
585  *
586  * It returns 0 on success and returns appropriate error code on error.
587  * The address handle is used to reference a local or global destination
588  * in all UD QP post sends.
589  */
590 struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
591 				  struct rdma_ah_attr *ah_attr,
592 				  struct ib_udata *udata)
593 {
594 	const struct ib_gid_attr *old_sgid_attr;
595 	struct ib_ah *ah;
596 	int err;
597 
598 	err = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
599 	if (err)
600 		return ERR_PTR(err);
601 
602 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
603 		err = ib_resolve_eth_dmac(pd->device, ah_attr);
604 		if (err) {
605 			ah = ERR_PTR(err);
606 			goto out;
607 		}
608 	}
609 
610 	ah = _rdma_create_ah(pd, ah_attr, RDMA_CREATE_AH_SLEEPABLE,
611 			     udata, NULL);
612 
613 out:
614 	rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
615 	return ah;
616 }
617 EXPORT_SYMBOL(rdma_create_user_ah);
618 
619 int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
620 {
621 	const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
622 	struct iphdr ip4h_checked;
623 	const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;
624 
625 	/* If it's IPv6, the version must be 6, otherwise, the first
626 	 * 20 bytes (before the IPv4 header) are garbled.
627 	 */
628 	if (ip6h->version != 6)
629 		return (ip4h->version == 4) ? 4 : 0;
630 	/* version may be 6 or 4 because the first 20 bytes could be garbled */
631 
632 	/* RoCE v2 requires no options, thus header length
633 	 * must be 5 words
634 	 */
635 	if (ip4h->ihl != 5)
636 		return 6;
637 
638 	/* Verify checksum.
639 	 * We can't write on scattered buffers so we need to copy to
640 	 * temp buffer.
641 	 */
642 	memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
643 	ip4h_checked.check = 0;
644 	ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
645 	/* if IPv4 header checksum is OK, believe it */
646 	if (ip4h->check == ip4h_checked.check)
647 		return 4;
648 	return 6;
649 }
650 EXPORT_SYMBOL(ib_get_rdma_header_version);
651 
652 static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
653 						     u8 port_num,
654 						     const struct ib_grh *grh)
655 {
656 	int grh_version;
657 
658 	if (rdma_protocol_ib(device, port_num))
659 		return RDMA_NETWORK_IB;
660 
661 	grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
662 
663 	if (grh_version == 4)
664 		return RDMA_NETWORK_IPV4;
665 
666 	if (grh->next_hdr == IPPROTO_UDP)
667 		return RDMA_NETWORK_IPV6;
668 
669 	return RDMA_NETWORK_ROCE_V1;
670 }
671 
672 struct find_gid_index_context {
673 	u16 vlan_id;
674 	enum ib_gid_type gid_type;
675 };
676 
677 static bool find_gid_index(const union ib_gid *gid,
678 			   const struct ib_gid_attr *gid_attr,
679 			   void *context)
680 {
681 	struct find_gid_index_context *ctx = context;
682 	u16 vlan_id = 0xffff;
683 	int ret;
684 
685 	if (ctx->gid_type != gid_attr->gid_type)
686 		return false;
687 
688 	ret = rdma_read_gid_l2_fields(gid_attr, &vlan_id, NULL);
689 	if (ret)
690 		return false;
691 
692 	return ctx->vlan_id == vlan_id;
693 }
694 
695 static const struct ib_gid_attr *
696 get_sgid_attr_from_eth(struct ib_device *device, u8 port_num,
697 		       u16 vlan_id, const union ib_gid *sgid,
698 		       enum ib_gid_type gid_type)
699 {
700 	struct find_gid_index_context context = {.vlan_id = vlan_id,
701 						 .gid_type = gid_type};
702 
703 	return rdma_find_gid_by_filter(device, sgid, port_num, find_gid_index,
704 				       &context);
705 }
706 
707 int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
708 			      enum rdma_network_type net_type,
709 			      union ib_gid *sgid, union ib_gid *dgid)
710 {
711 	struct sockaddr_in  src_in;
712 	struct sockaddr_in  dst_in;
713 	__be32 src_saddr, dst_saddr;
714 
715 	if (!sgid || !dgid)
716 		return -EINVAL;
717 
718 	if (net_type == RDMA_NETWORK_IPV4) {
719 		memcpy(&src_in.sin_addr.s_addr,
720 		       &hdr->roce4grh.saddr, 4);
721 		memcpy(&dst_in.sin_addr.s_addr,
722 		       &hdr->roce4grh.daddr, 4);
723 		src_saddr = src_in.sin_addr.s_addr;
724 		dst_saddr = dst_in.sin_addr.s_addr;
725 		ipv6_addr_set_v4mapped(src_saddr,
726 				       (struct in6_addr *)sgid);
727 		ipv6_addr_set_v4mapped(dst_saddr,
728 				       (struct in6_addr *)dgid);
729 		return 0;
730 	} else if (net_type == RDMA_NETWORK_IPV6 ||
731 		   net_type == RDMA_NETWORK_IB) {
732 		*dgid = hdr->ibgrh.dgid;
733 		*sgid = hdr->ibgrh.sgid;
734 		return 0;
735 	} else {
736 		return -EINVAL;
737 	}
738 }
739 EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
740 
741 /* Resolve destination mac address and hop limit for unicast destination
742  * GID entry, considering the source GID entry as well.
743  * ah_attribute must have have valid port_num, sgid_index.
744  */
745 static int ib_resolve_unicast_gid_dmac(struct ib_device *device,
746 				       struct rdma_ah_attr *ah_attr)
747 {
748 	struct ib_global_route *grh = rdma_ah_retrieve_grh(ah_attr);
749 	const struct ib_gid_attr *sgid_attr = grh->sgid_attr;
750 	int hop_limit = 0xff;
751 	int ret = 0;
752 
753 	/* If destination is link local and source GID is RoCEv1,
754 	 * IP stack is not used.
755 	 */
756 	if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) &&
757 	    sgid_attr->gid_type == IB_GID_TYPE_ROCE) {
758 		rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
759 				ah_attr->roce.dmac);
760 		return ret;
761 	}
762 
763 	ret = rdma_addr_find_l2_eth_by_grh(&sgid_attr->gid, &grh->dgid,
764 					   ah_attr->roce.dmac,
765 					   sgid_attr, &hop_limit);
766 
767 	grh->hop_limit = hop_limit;
768 	return ret;
769 }
770 
771 /*
772  * This function initializes address handle attributes from the incoming packet.
773  * Incoming packet has dgid of the receiver node on which this code is
774  * getting executed and, sgid contains the GID of the sender.
775  *
776  * When resolving mac address of destination, the arrived dgid is used
777  * as sgid and, sgid is used as dgid because sgid contains destinations
778  * GID whom to respond to.
779  *
780  * On success the caller is responsible to call rdma_destroy_ah_attr on the
781  * attr.
782  */
783 int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num,
784 			    const struct ib_wc *wc, const struct ib_grh *grh,
785 			    struct rdma_ah_attr *ah_attr)
786 {
787 	u32 flow_class;
788 	int ret;
789 	enum rdma_network_type net_type = RDMA_NETWORK_IB;
790 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
791 	const struct ib_gid_attr *sgid_attr;
792 	int hoplimit = 0xff;
793 	union ib_gid dgid;
794 	union ib_gid sgid;
795 
796 	might_sleep();
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((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 		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  */
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  */
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 
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 
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->ops.modify_ah ?
944 		ah->device->ops.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 
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->ops.query_ah ?
958 		ah->device->ops.query_ah(ah, ah_attr) :
959 		-EOPNOTSUPP;
960 }
961 EXPORT_SYMBOL(rdma_query_ah);
962 
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 
972 	ah->device->ops.destroy_ah(ah, flags);
973 	atomic_dec(&pd->usecnt);
974 	if (sgid_attr)
975 		rdma_put_gid_attr(sgid_attr);
976 
977 	kfree(ah);
978 	return 0;
979 }
980 EXPORT_SYMBOL(rdma_destroy_ah_user);
981 
982 /* Shared receive queues */
983 
984 /**
985  * ib_create_srq_user - Creates a SRQ associated with the specified protection
986  *   domain.
987  * @pd: The protection domain associated with the SRQ.
988  * @srq_init_attr: A list of initial attributes required to create the
989  *   SRQ.  If SRQ creation succeeds, then the attributes are updated to
990  *   the actual capabilities of the created SRQ.
991  * @uobject: uobject pointer if this is not a kernel SRQ
992  * @udata: udata pointer if this is not a kernel SRQ
993  *
994  * srq_attr->max_wr and srq_attr->max_sge are read the determine the
995  * requested size of the SRQ, and set to the actual values allocated
996  * on return.  If ib_create_srq() succeeds, then max_wr and max_sge
997  * will always be at least as large as the requested values.
998  */
999 struct ib_srq *ib_create_srq_user(struct ib_pd *pd,
1000 				  struct ib_srq_init_attr *srq_init_attr,
1001 				  struct ib_usrq_object *uobject,
1002 				  struct ib_udata *udata)
1003 {
1004 	struct ib_srq *srq;
1005 	int ret;
1006 
1007 	srq = rdma_zalloc_drv_obj(pd->device, ib_srq);
1008 	if (!srq)
1009 		return ERR_PTR(-ENOMEM);
1010 
1011 	srq->device = pd->device;
1012 	srq->pd = pd;
1013 	srq->event_handler = srq_init_attr->event_handler;
1014 	srq->srq_context = srq_init_attr->srq_context;
1015 	srq->srq_type = srq_init_attr->srq_type;
1016 	srq->uobject = uobject;
1017 
1018 	if (ib_srq_has_cq(srq->srq_type)) {
1019 		srq->ext.cq = srq_init_attr->ext.cq;
1020 		atomic_inc(&srq->ext.cq->usecnt);
1021 	}
1022 	if (srq->srq_type == IB_SRQT_XRC) {
1023 		srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
1024 		atomic_inc(&srq->ext.xrc.xrcd->usecnt);
1025 	}
1026 	atomic_inc(&pd->usecnt);
1027 
1028 	ret = pd->device->ops.create_srq(srq, srq_init_attr, udata);
1029 	if (ret) {
1030 		atomic_dec(&srq->pd->usecnt);
1031 		if (srq->srq_type == IB_SRQT_XRC)
1032 			atomic_dec(&srq->ext.xrc.xrcd->usecnt);
1033 		if (ib_srq_has_cq(srq->srq_type))
1034 			atomic_dec(&srq->ext.cq->usecnt);
1035 		kfree(srq);
1036 		return ERR_PTR(ret);
1037 	}
1038 
1039 	return srq;
1040 }
1041 EXPORT_SYMBOL(ib_create_srq_user);
1042 
1043 int ib_modify_srq(struct ib_srq *srq,
1044 		  struct ib_srq_attr *srq_attr,
1045 		  enum ib_srq_attr_mask srq_attr_mask)
1046 {
1047 	return srq->device->ops.modify_srq ?
1048 		srq->device->ops.modify_srq(srq, srq_attr, srq_attr_mask,
1049 					    NULL) : -EOPNOTSUPP;
1050 }
1051 EXPORT_SYMBOL(ib_modify_srq);
1052 
1053 int ib_query_srq(struct ib_srq *srq,
1054 		 struct ib_srq_attr *srq_attr)
1055 {
1056 	return srq->device->ops.query_srq ?
1057 		srq->device->ops.query_srq(srq, srq_attr) : -EOPNOTSUPP;
1058 }
1059 EXPORT_SYMBOL(ib_query_srq);
1060 
1061 int ib_destroy_srq_user(struct ib_srq *srq, struct ib_udata *udata)
1062 {
1063 	if (atomic_read(&srq->usecnt))
1064 		return -EBUSY;
1065 
1066 	srq->device->ops.destroy_srq(srq, udata);
1067 
1068 	atomic_dec(&srq->pd->usecnt);
1069 	if (srq->srq_type == IB_SRQT_XRC)
1070 		atomic_dec(&srq->ext.xrc.xrcd->usecnt);
1071 	if (ib_srq_has_cq(srq->srq_type))
1072 		atomic_dec(&srq->ext.cq->usecnt);
1073 	kfree(srq);
1074 
1075 	return 0;
1076 }
1077 EXPORT_SYMBOL(ib_destroy_srq_user);
1078 
1079 /* Queue pairs */
1080 
1081 static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
1082 {
1083 	struct ib_qp *qp = context;
1084 	unsigned long flags;
1085 
1086 	spin_lock_irqsave(&qp->device->qp_open_list_lock, flags);
1087 	list_for_each_entry(event->element.qp, &qp->open_list, open_list)
1088 		if (event->element.qp->event_handler)
1089 			event->element.qp->event_handler(event, event->element.qp->qp_context);
1090 	spin_unlock_irqrestore(&qp->device->qp_open_list_lock, flags);
1091 }
1092 
1093 static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
1094 				  void (*event_handler)(struct ib_event *, void *),
1095 				  void *qp_context)
1096 {
1097 	struct ib_qp *qp;
1098 	unsigned long flags;
1099 	int err;
1100 
1101 	qp = kzalloc(sizeof *qp, GFP_KERNEL);
1102 	if (!qp)
1103 		return ERR_PTR(-ENOMEM);
1104 
1105 	qp->real_qp = real_qp;
1106 	err = ib_open_shared_qp_security(qp, real_qp->device);
1107 	if (err) {
1108 		kfree(qp);
1109 		return ERR_PTR(err);
1110 	}
1111 
1112 	qp->real_qp = real_qp;
1113 	atomic_inc(&real_qp->usecnt);
1114 	qp->device = real_qp->device;
1115 	qp->event_handler = event_handler;
1116 	qp->qp_context = qp_context;
1117 	qp->qp_num = real_qp->qp_num;
1118 	qp->qp_type = real_qp->qp_type;
1119 
1120 	spin_lock_irqsave(&real_qp->device->qp_open_list_lock, flags);
1121 	list_add(&qp->open_list, &real_qp->open_list);
1122 	spin_unlock_irqrestore(&real_qp->device->qp_open_list_lock, flags);
1123 
1124 	return qp;
1125 }
1126 
1127 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
1128 			 struct ib_qp_open_attr *qp_open_attr)
1129 {
1130 	struct ib_qp *qp, *real_qp;
1131 
1132 	if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
1133 		return ERR_PTR(-EINVAL);
1134 
1135 	down_read(&xrcd->tgt_qps_rwsem);
1136 	real_qp = xa_load(&xrcd->tgt_qps, qp_open_attr->qp_num);
1137 	if (!real_qp) {
1138 		up_read(&xrcd->tgt_qps_rwsem);
1139 		return ERR_PTR(-EINVAL);
1140 	}
1141 	qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
1142 			  qp_open_attr->qp_context);
1143 	up_read(&xrcd->tgt_qps_rwsem);
1144 	return qp;
1145 }
1146 EXPORT_SYMBOL(ib_open_qp);
1147 
1148 static struct ib_qp *create_xrc_qp_user(struct ib_qp *qp,
1149 					struct ib_qp_init_attr *qp_init_attr)
1150 {
1151 	struct ib_qp *real_qp = qp;
1152 	int err;
1153 
1154 	qp->event_handler = __ib_shared_qp_event_handler;
1155 	qp->qp_context = qp;
1156 	qp->pd = NULL;
1157 	qp->send_cq = qp->recv_cq = NULL;
1158 	qp->srq = NULL;
1159 	qp->xrcd = qp_init_attr->xrcd;
1160 	atomic_inc(&qp_init_attr->xrcd->usecnt);
1161 	INIT_LIST_HEAD(&qp->open_list);
1162 
1163 	qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
1164 			  qp_init_attr->qp_context);
1165 	if (IS_ERR(qp))
1166 		return qp;
1167 
1168 	err = xa_err(xa_store(&qp_init_attr->xrcd->tgt_qps, real_qp->qp_num,
1169 			      real_qp, GFP_KERNEL));
1170 	if (err) {
1171 		ib_close_qp(qp);
1172 		return ERR_PTR(err);
1173 	}
1174 	return qp;
1175 }
1176 
1177 /**
1178  * ib_create_qp - Creates a kernel QP associated with the specified protection
1179  *   domain.
1180  * @pd: The protection domain associated with the QP.
1181  * @qp_init_attr: A list of initial attributes required to create the
1182  *   QP.  If QP creation succeeds, then the attributes are updated to
1183  *   the actual capabilities of the created QP.
1184  *
1185  * NOTE: for user qp use ib_create_qp_user with valid udata!
1186  */
1187 struct ib_qp *ib_create_qp(struct ib_pd *pd,
1188 			   struct ib_qp_init_attr *qp_init_attr)
1189 {
1190 	struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
1191 	struct ib_qp *qp;
1192 	int ret;
1193 
1194 	if (qp_init_attr->rwq_ind_tbl &&
1195 	    (qp_init_attr->recv_cq ||
1196 	    qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
1197 	    qp_init_attr->cap.max_recv_sge))
1198 		return ERR_PTR(-EINVAL);
1199 
1200 	if ((qp_init_attr->create_flags & IB_QP_CREATE_INTEGRITY_EN) &&
1201 	    !(device->attrs.device_cap_flags & IB_DEVICE_INTEGRITY_HANDOVER))
1202 		return ERR_PTR(-EINVAL);
1203 
1204 	/*
1205 	 * If the callers is using the RDMA API calculate the resources
1206 	 * needed for the RDMA READ/WRITE operations.
1207 	 *
1208 	 * Note that these callers need to pass in a port number.
1209 	 */
1210 	if (qp_init_attr->cap.max_rdma_ctxs)
1211 		rdma_rw_init_qp(device, qp_init_attr);
1212 
1213 	qp = _ib_create_qp(device, pd, qp_init_attr, NULL, NULL);
1214 	if (IS_ERR(qp))
1215 		return qp;
1216 
1217 	ret = ib_create_qp_security(qp, device);
1218 	if (ret)
1219 		goto err;
1220 
1221 	if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) {
1222 		struct ib_qp *xrc_qp =
1223 			create_xrc_qp_user(qp, qp_init_attr);
1224 
1225 		if (IS_ERR(xrc_qp)) {
1226 			ret = PTR_ERR(xrc_qp);
1227 			goto err;
1228 		}
1229 		return xrc_qp;
1230 	}
1231 
1232 	qp->event_handler = qp_init_attr->event_handler;
1233 	qp->qp_context = qp_init_attr->qp_context;
1234 	if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
1235 		qp->recv_cq = NULL;
1236 		qp->srq = NULL;
1237 	} else {
1238 		qp->recv_cq = qp_init_attr->recv_cq;
1239 		if (qp_init_attr->recv_cq)
1240 			atomic_inc(&qp_init_attr->recv_cq->usecnt);
1241 		qp->srq = qp_init_attr->srq;
1242 		if (qp->srq)
1243 			atomic_inc(&qp_init_attr->srq->usecnt);
1244 	}
1245 
1246 	qp->send_cq = qp_init_attr->send_cq;
1247 	qp->xrcd    = NULL;
1248 
1249 	atomic_inc(&pd->usecnt);
1250 	if (qp_init_attr->send_cq)
1251 		atomic_inc(&qp_init_attr->send_cq->usecnt);
1252 	if (qp_init_attr->rwq_ind_tbl)
1253 		atomic_inc(&qp->rwq_ind_tbl->usecnt);
1254 
1255 	if (qp_init_attr->cap.max_rdma_ctxs) {
1256 		ret = rdma_rw_init_mrs(qp, qp_init_attr);
1257 		if (ret)
1258 			goto err;
1259 	}
1260 
1261 	/*
1262 	 * Note: all hw drivers guarantee that max_send_sge is lower than
1263 	 * the device RDMA WRITE SGE limit but not all hw drivers ensure that
1264 	 * max_send_sge <= max_sge_rd.
1265 	 */
1266 	qp->max_write_sge = qp_init_attr->cap.max_send_sge;
1267 	qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
1268 				 device->attrs.max_sge_rd);
1269 	if (qp_init_attr->create_flags & IB_QP_CREATE_INTEGRITY_EN)
1270 		qp->integrity_en = true;
1271 
1272 	return qp;
1273 
1274 err:
1275 	ib_destroy_qp(qp);
1276 	return ERR_PTR(ret);
1277 
1278 }
1279 EXPORT_SYMBOL(ib_create_qp);
1280 
1281 static const struct {
1282 	int			valid;
1283 	enum ib_qp_attr_mask	req_param[IB_QPT_MAX];
1284 	enum ib_qp_attr_mask	opt_param[IB_QPT_MAX];
1285 } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
1286 	[IB_QPS_RESET] = {
1287 		[IB_QPS_RESET] = { .valid = 1 },
1288 		[IB_QPS_INIT]  = {
1289 			.valid = 1,
1290 			.req_param = {
1291 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
1292 						IB_QP_PORT			|
1293 						IB_QP_QKEY),
1294 				[IB_QPT_RAW_PACKET] = IB_QP_PORT,
1295 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
1296 						IB_QP_PORT			|
1297 						IB_QP_ACCESS_FLAGS),
1298 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
1299 						IB_QP_PORT			|
1300 						IB_QP_ACCESS_FLAGS),
1301 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
1302 						IB_QP_PORT			|
1303 						IB_QP_ACCESS_FLAGS),
1304 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
1305 						IB_QP_PORT			|
1306 						IB_QP_ACCESS_FLAGS),
1307 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
1308 						IB_QP_QKEY),
1309 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
1310 						IB_QP_QKEY),
1311 			}
1312 		},
1313 	},
1314 	[IB_QPS_INIT]  = {
1315 		[IB_QPS_RESET] = { .valid = 1 },
1316 		[IB_QPS_ERR] =   { .valid = 1 },
1317 		[IB_QPS_INIT]  = {
1318 			.valid = 1,
1319 			.opt_param = {
1320 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
1321 						IB_QP_PORT			|
1322 						IB_QP_QKEY),
1323 				[IB_QPT_UC]  = (IB_QP_PKEY_INDEX		|
1324 						IB_QP_PORT			|
1325 						IB_QP_ACCESS_FLAGS),
1326 				[IB_QPT_RC]  = (IB_QP_PKEY_INDEX		|
1327 						IB_QP_PORT			|
1328 						IB_QP_ACCESS_FLAGS),
1329 				[IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX		|
1330 						IB_QP_PORT			|
1331 						IB_QP_ACCESS_FLAGS),
1332 				[IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX		|
1333 						IB_QP_PORT			|
1334 						IB_QP_ACCESS_FLAGS),
1335 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
1336 						IB_QP_QKEY),
1337 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
1338 						IB_QP_QKEY),
1339 			}
1340 		},
1341 		[IB_QPS_RTR]   = {
1342 			.valid = 1,
1343 			.req_param = {
1344 				[IB_QPT_UC]  = (IB_QP_AV			|
1345 						IB_QP_PATH_MTU			|
1346 						IB_QP_DEST_QPN			|
1347 						IB_QP_RQ_PSN),
1348 				[IB_QPT_RC]  = (IB_QP_AV			|
1349 						IB_QP_PATH_MTU			|
1350 						IB_QP_DEST_QPN			|
1351 						IB_QP_RQ_PSN			|
1352 						IB_QP_MAX_DEST_RD_ATOMIC	|
1353 						IB_QP_MIN_RNR_TIMER),
1354 				[IB_QPT_XRC_INI] = (IB_QP_AV			|
1355 						IB_QP_PATH_MTU			|
1356 						IB_QP_DEST_QPN			|
1357 						IB_QP_RQ_PSN),
1358 				[IB_QPT_XRC_TGT] = (IB_QP_AV			|
1359 						IB_QP_PATH_MTU			|
1360 						IB_QP_DEST_QPN			|
1361 						IB_QP_RQ_PSN			|
1362 						IB_QP_MAX_DEST_RD_ATOMIC	|
1363 						IB_QP_MIN_RNR_TIMER),
1364 			},
1365 			.opt_param = {
1366 				 [IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
1367 						 IB_QP_QKEY),
1368 				 [IB_QPT_UC]  = (IB_QP_ALT_PATH			|
1369 						 IB_QP_ACCESS_FLAGS		|
1370 						 IB_QP_PKEY_INDEX),
1371 				 [IB_QPT_RC]  = (IB_QP_ALT_PATH			|
1372 						 IB_QP_ACCESS_FLAGS		|
1373 						 IB_QP_PKEY_INDEX),
1374 				 [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH		|
1375 						 IB_QP_ACCESS_FLAGS		|
1376 						 IB_QP_PKEY_INDEX),
1377 				 [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH		|
1378 						 IB_QP_ACCESS_FLAGS		|
1379 						 IB_QP_PKEY_INDEX),
1380 				 [IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
1381 						 IB_QP_QKEY),
1382 				 [IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
1383 						 IB_QP_QKEY),
1384 			 },
1385 		},
1386 	},
1387 	[IB_QPS_RTR]   = {
1388 		[IB_QPS_RESET] = { .valid = 1 },
1389 		[IB_QPS_ERR] =   { .valid = 1 },
1390 		[IB_QPS_RTS]   = {
1391 			.valid = 1,
1392 			.req_param = {
1393 				[IB_QPT_UD]  = IB_QP_SQ_PSN,
1394 				[IB_QPT_UC]  = IB_QP_SQ_PSN,
1395 				[IB_QPT_RC]  = (IB_QP_TIMEOUT			|
1396 						IB_QP_RETRY_CNT			|
1397 						IB_QP_RNR_RETRY			|
1398 						IB_QP_SQ_PSN			|
1399 						IB_QP_MAX_QP_RD_ATOMIC),
1400 				[IB_QPT_XRC_INI] = (IB_QP_TIMEOUT		|
1401 						IB_QP_RETRY_CNT			|
1402 						IB_QP_RNR_RETRY			|
1403 						IB_QP_SQ_PSN			|
1404 						IB_QP_MAX_QP_RD_ATOMIC),
1405 				[IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT		|
1406 						IB_QP_SQ_PSN),
1407 				[IB_QPT_SMI] = IB_QP_SQ_PSN,
1408 				[IB_QPT_GSI] = IB_QP_SQ_PSN,
1409 			},
1410 			.opt_param = {
1411 				 [IB_QPT_UD]  = (IB_QP_CUR_STATE		|
1412 						 IB_QP_QKEY),
1413 				 [IB_QPT_UC]  = (IB_QP_CUR_STATE		|
1414 						 IB_QP_ALT_PATH			|
1415 						 IB_QP_ACCESS_FLAGS		|
1416 						 IB_QP_PATH_MIG_STATE),
1417 				 [IB_QPT_RC]  = (IB_QP_CUR_STATE		|
1418 						 IB_QP_ALT_PATH			|
1419 						 IB_QP_ACCESS_FLAGS		|
1420 						 IB_QP_MIN_RNR_TIMER		|
1421 						 IB_QP_PATH_MIG_STATE),
1422 				 [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1423 						 IB_QP_ALT_PATH			|
1424 						 IB_QP_ACCESS_FLAGS		|
1425 						 IB_QP_PATH_MIG_STATE),
1426 				 [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1427 						 IB_QP_ALT_PATH			|
1428 						 IB_QP_ACCESS_FLAGS		|
1429 						 IB_QP_MIN_RNR_TIMER		|
1430 						 IB_QP_PATH_MIG_STATE),
1431 				 [IB_QPT_SMI] = (IB_QP_CUR_STATE		|
1432 						 IB_QP_QKEY),
1433 				 [IB_QPT_GSI] = (IB_QP_CUR_STATE		|
1434 						 IB_QP_QKEY),
1435 				 [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1436 			 }
1437 		}
1438 	},
1439 	[IB_QPS_RTS]   = {
1440 		[IB_QPS_RESET] = { .valid = 1 },
1441 		[IB_QPS_ERR] =   { .valid = 1 },
1442 		[IB_QPS_RTS]   = {
1443 			.valid = 1,
1444 			.opt_param = {
1445 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
1446 						IB_QP_QKEY),
1447 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
1448 						IB_QP_ACCESS_FLAGS		|
1449 						IB_QP_ALT_PATH			|
1450 						IB_QP_PATH_MIG_STATE),
1451 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
1452 						IB_QP_ACCESS_FLAGS		|
1453 						IB_QP_ALT_PATH			|
1454 						IB_QP_PATH_MIG_STATE		|
1455 						IB_QP_MIN_RNR_TIMER),
1456 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1457 						IB_QP_ACCESS_FLAGS		|
1458 						IB_QP_ALT_PATH			|
1459 						IB_QP_PATH_MIG_STATE),
1460 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1461 						IB_QP_ACCESS_FLAGS		|
1462 						IB_QP_ALT_PATH			|
1463 						IB_QP_PATH_MIG_STATE		|
1464 						IB_QP_MIN_RNR_TIMER),
1465 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
1466 						IB_QP_QKEY),
1467 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
1468 						IB_QP_QKEY),
1469 				[IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
1470 			}
1471 		},
1472 		[IB_QPS_SQD]   = {
1473 			.valid = 1,
1474 			.opt_param = {
1475 				[IB_QPT_UD]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1476 				[IB_QPT_UC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1477 				[IB_QPT_RC]  = IB_QP_EN_SQD_ASYNC_NOTIFY,
1478 				[IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1479 				[IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
1480 				[IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
1481 				[IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
1482 			}
1483 		},
1484 	},
1485 	[IB_QPS_SQD]   = {
1486 		[IB_QPS_RESET] = { .valid = 1 },
1487 		[IB_QPS_ERR] =   { .valid = 1 },
1488 		[IB_QPS_RTS]   = {
1489 			.valid = 1,
1490 			.opt_param = {
1491 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
1492 						IB_QP_QKEY),
1493 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
1494 						IB_QP_ALT_PATH			|
1495 						IB_QP_ACCESS_FLAGS		|
1496 						IB_QP_PATH_MIG_STATE),
1497 				[IB_QPT_RC]  = (IB_QP_CUR_STATE			|
1498 						IB_QP_ALT_PATH			|
1499 						IB_QP_ACCESS_FLAGS		|
1500 						IB_QP_MIN_RNR_TIMER		|
1501 						IB_QP_PATH_MIG_STATE),
1502 				[IB_QPT_XRC_INI] = (IB_QP_CUR_STATE		|
1503 						IB_QP_ALT_PATH			|
1504 						IB_QP_ACCESS_FLAGS		|
1505 						IB_QP_PATH_MIG_STATE),
1506 				[IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE		|
1507 						IB_QP_ALT_PATH			|
1508 						IB_QP_ACCESS_FLAGS		|
1509 						IB_QP_MIN_RNR_TIMER		|
1510 						IB_QP_PATH_MIG_STATE),
1511 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
1512 						IB_QP_QKEY),
1513 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
1514 						IB_QP_QKEY),
1515 			}
1516 		},
1517 		[IB_QPS_SQD]   = {
1518 			.valid = 1,
1519 			.opt_param = {
1520 				[IB_QPT_UD]  = (IB_QP_PKEY_INDEX		|
1521 						IB_QP_QKEY),
1522 				[IB_QPT_UC]  = (IB_QP_AV			|
1523 						IB_QP_ALT_PATH			|
1524 						IB_QP_ACCESS_FLAGS		|
1525 						IB_QP_PKEY_INDEX		|
1526 						IB_QP_PATH_MIG_STATE),
1527 				[IB_QPT_RC]  = (IB_QP_PORT			|
1528 						IB_QP_AV			|
1529 						IB_QP_TIMEOUT			|
1530 						IB_QP_RETRY_CNT			|
1531 						IB_QP_RNR_RETRY			|
1532 						IB_QP_MAX_QP_RD_ATOMIC		|
1533 						IB_QP_MAX_DEST_RD_ATOMIC	|
1534 						IB_QP_ALT_PATH			|
1535 						IB_QP_ACCESS_FLAGS		|
1536 						IB_QP_PKEY_INDEX		|
1537 						IB_QP_MIN_RNR_TIMER		|
1538 						IB_QP_PATH_MIG_STATE),
1539 				[IB_QPT_XRC_INI] = (IB_QP_PORT			|
1540 						IB_QP_AV			|
1541 						IB_QP_TIMEOUT			|
1542 						IB_QP_RETRY_CNT			|
1543 						IB_QP_RNR_RETRY			|
1544 						IB_QP_MAX_QP_RD_ATOMIC		|
1545 						IB_QP_ALT_PATH			|
1546 						IB_QP_ACCESS_FLAGS		|
1547 						IB_QP_PKEY_INDEX		|
1548 						IB_QP_PATH_MIG_STATE),
1549 				[IB_QPT_XRC_TGT] = (IB_QP_PORT			|
1550 						IB_QP_AV			|
1551 						IB_QP_TIMEOUT			|
1552 						IB_QP_MAX_DEST_RD_ATOMIC	|
1553 						IB_QP_ALT_PATH			|
1554 						IB_QP_ACCESS_FLAGS		|
1555 						IB_QP_PKEY_INDEX		|
1556 						IB_QP_MIN_RNR_TIMER		|
1557 						IB_QP_PATH_MIG_STATE),
1558 				[IB_QPT_SMI] = (IB_QP_PKEY_INDEX		|
1559 						IB_QP_QKEY),
1560 				[IB_QPT_GSI] = (IB_QP_PKEY_INDEX		|
1561 						IB_QP_QKEY),
1562 			}
1563 		}
1564 	},
1565 	[IB_QPS_SQE]   = {
1566 		[IB_QPS_RESET] = { .valid = 1 },
1567 		[IB_QPS_ERR] =   { .valid = 1 },
1568 		[IB_QPS_RTS]   = {
1569 			.valid = 1,
1570 			.opt_param = {
1571 				[IB_QPT_UD]  = (IB_QP_CUR_STATE			|
1572 						IB_QP_QKEY),
1573 				[IB_QPT_UC]  = (IB_QP_CUR_STATE			|
1574 						IB_QP_ACCESS_FLAGS),
1575 				[IB_QPT_SMI] = (IB_QP_CUR_STATE			|
1576 						IB_QP_QKEY),
1577 				[IB_QPT_GSI] = (IB_QP_CUR_STATE			|
1578 						IB_QP_QKEY),
1579 			}
1580 		}
1581 	},
1582 	[IB_QPS_ERR] = {
1583 		[IB_QPS_RESET] = { .valid = 1 },
1584 		[IB_QPS_ERR] =   { .valid = 1 }
1585 	}
1586 };
1587 
1588 bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
1589 			enum ib_qp_type type, enum ib_qp_attr_mask mask)
1590 {
1591 	enum ib_qp_attr_mask req_param, opt_param;
1592 
1593 	if (mask & IB_QP_CUR_STATE  &&
1594 	    cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
1595 	    cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
1596 		return false;
1597 
1598 	if (!qp_state_table[cur_state][next_state].valid)
1599 		return false;
1600 
1601 	req_param = qp_state_table[cur_state][next_state].req_param[type];
1602 	opt_param = qp_state_table[cur_state][next_state].opt_param[type];
1603 
1604 	if ((mask & req_param) != req_param)
1605 		return false;
1606 
1607 	if (mask & ~(req_param | opt_param | IB_QP_STATE))
1608 		return false;
1609 
1610 	return true;
1611 }
1612 EXPORT_SYMBOL(ib_modify_qp_is_ok);
1613 
1614 /**
1615  * ib_resolve_eth_dmac - Resolve destination mac address
1616  * @device:		Device to consider
1617  * @ah_attr:		address handle attribute which describes the
1618  *			source and destination parameters
1619  * ib_resolve_eth_dmac() resolves destination mac address and L3 hop limit It
1620  * returns 0 on success or appropriate error code. It initializes the
1621  * necessary ah_attr fields when call is successful.
1622  */
1623 static int ib_resolve_eth_dmac(struct ib_device *device,
1624 			       struct rdma_ah_attr *ah_attr)
1625 {
1626 	int ret = 0;
1627 
1628 	if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1629 		if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
1630 			__be32 addr = 0;
1631 
1632 			memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
1633 			ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
1634 		} else {
1635 			ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
1636 					(char *)ah_attr->roce.dmac);
1637 		}
1638 	} else {
1639 		ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
1640 	}
1641 	return ret;
1642 }
1643 
1644 static bool is_qp_type_connected(const struct ib_qp *qp)
1645 {
1646 	return (qp->qp_type == IB_QPT_UC ||
1647 		qp->qp_type == IB_QPT_RC ||
1648 		qp->qp_type == IB_QPT_XRC_INI ||
1649 		qp->qp_type == IB_QPT_XRC_TGT);
1650 }
1651 
1652 /**
1653  * IB core internal function to perform QP attributes modification.
1654  */
1655 static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
1656 			 int attr_mask, struct ib_udata *udata)
1657 {
1658 	u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1659 	const struct ib_gid_attr *old_sgid_attr_av;
1660 	const struct ib_gid_attr *old_sgid_attr_alt_av;
1661 	int ret;
1662 
1663 	attr->xmit_slave = NULL;
1664 	if (attr_mask & IB_QP_AV) {
1665 		ret = rdma_fill_sgid_attr(qp->device, &attr->ah_attr,
1666 					  &old_sgid_attr_av);
1667 		if (ret)
1668 			return ret;
1669 
1670 		if (attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE &&
1671 		    is_qp_type_connected(qp)) {
1672 			struct net_device *slave;
1673 
1674 			/*
1675 			 * If the user provided the qp_attr then we have to
1676 			 * resolve it. Kerne users have to provide already
1677 			 * resolved rdma_ah_attr's.
1678 			 */
1679 			if (udata) {
1680 				ret = ib_resolve_eth_dmac(qp->device,
1681 							  &attr->ah_attr);
1682 				if (ret)
1683 					goto out_av;
1684 			}
1685 			slave = rdma_lag_get_ah_roce_slave(qp->device,
1686 							   &attr->ah_attr,
1687 							   GFP_KERNEL);
1688 			if (IS_ERR(slave))
1689 				goto out_av;
1690 			attr->xmit_slave = slave;
1691 		}
1692 	}
1693 	if (attr_mask & IB_QP_ALT_PATH) {
1694 		/*
1695 		 * FIXME: This does not track the migration state, so if the
1696 		 * user loads a new alternate path after the HW has migrated
1697 		 * from primary->alternate we will keep the wrong
1698 		 * references. This is OK for IB because the reference
1699 		 * counting does not serve any functional purpose.
1700 		 */
1701 		ret = rdma_fill_sgid_attr(qp->device, &attr->alt_ah_attr,
1702 					  &old_sgid_attr_alt_av);
1703 		if (ret)
1704 			goto out_av;
1705 
1706 		/*
1707 		 * Today the core code can only handle alternate paths and APM
1708 		 * for IB. Ban them in roce mode.
1709 		 */
1710 		if (!(rdma_protocol_ib(qp->device,
1711 				       attr->alt_ah_attr.port_num) &&
1712 		      rdma_protocol_ib(qp->device, port))) {
1713 			ret = -EINVAL;
1714 			goto out;
1715 		}
1716 	}
1717 
1718 	if (rdma_ib_or_roce(qp->device, port)) {
1719 		if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) {
1720 			dev_warn(&qp->device->dev,
1721 				 "%s rq_psn overflow, masking to 24 bits\n",
1722 				 __func__);
1723 			attr->rq_psn &= 0xffffff;
1724 		}
1725 
1726 		if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) {
1727 			dev_warn(&qp->device->dev,
1728 				 " %s sq_psn overflow, masking to 24 bits\n",
1729 				 __func__);
1730 			attr->sq_psn &= 0xffffff;
1731 		}
1732 	}
1733 
1734 	/*
1735 	 * Bind this qp to a counter automatically based on the rdma counter
1736 	 * rules. This only set in RST2INIT with port specified
1737 	 */
1738 	if (!qp->counter && (attr_mask & IB_QP_PORT) &&
1739 	    ((attr_mask & IB_QP_STATE) && attr->qp_state == IB_QPS_INIT))
1740 		rdma_counter_bind_qp_auto(qp, attr->port_num);
1741 
1742 	ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
1743 	if (ret)
1744 		goto out;
1745 
1746 	if (attr_mask & IB_QP_PORT)
1747 		qp->port = attr->port_num;
1748 	if (attr_mask & IB_QP_AV)
1749 		qp->av_sgid_attr =
1750 			rdma_update_sgid_attr(&attr->ah_attr, qp->av_sgid_attr);
1751 	if (attr_mask & IB_QP_ALT_PATH)
1752 		qp->alt_path_sgid_attr = rdma_update_sgid_attr(
1753 			&attr->alt_ah_attr, qp->alt_path_sgid_attr);
1754 
1755 out:
1756 	if (attr_mask & IB_QP_ALT_PATH)
1757 		rdma_unfill_sgid_attr(&attr->alt_ah_attr, old_sgid_attr_alt_av);
1758 out_av:
1759 	if (attr_mask & IB_QP_AV) {
1760 		rdma_lag_put_ah_roce_slave(attr->xmit_slave);
1761 		rdma_unfill_sgid_attr(&attr->ah_attr, old_sgid_attr_av);
1762 	}
1763 	return ret;
1764 }
1765 
1766 /**
1767  * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
1768  * @ib_qp: The QP to modify.
1769  * @attr: On input, specifies the QP attributes to modify.  On output,
1770  *   the current values of selected QP attributes are returned.
1771  * @attr_mask: A bit-mask used to specify which attributes of the QP
1772  *   are being modified.
1773  * @udata: pointer to user's input output buffer information
1774  *   are being modified.
1775  * It returns 0 on success and returns appropriate error code on error.
1776  */
1777 int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
1778 			    int attr_mask, struct ib_udata *udata)
1779 {
1780 	return _ib_modify_qp(ib_qp->real_qp, attr, attr_mask, udata);
1781 }
1782 EXPORT_SYMBOL(ib_modify_qp_with_udata);
1783 
1784 int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width)
1785 {
1786 	int rc;
1787 	u32 netdev_speed;
1788 	struct net_device *netdev;
1789 	struct ethtool_link_ksettings lksettings;
1790 
1791 	if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
1792 		return -EINVAL;
1793 
1794 	netdev = ib_device_get_netdev(dev, port_num);
1795 	if (!netdev)
1796 		return -ENODEV;
1797 
1798 	rtnl_lock();
1799 	rc = __ethtool_get_link_ksettings(netdev, &lksettings);
1800 	rtnl_unlock();
1801 
1802 	dev_put(netdev);
1803 
1804 	if (!rc && lksettings.base.speed != (u32)SPEED_UNKNOWN) {
1805 		netdev_speed = lksettings.base.speed;
1806 	} else {
1807 		netdev_speed = SPEED_1000;
1808 		pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name,
1809 			netdev_speed);
1810 	}
1811 
1812 	if (netdev_speed <= SPEED_1000) {
1813 		*width = IB_WIDTH_1X;
1814 		*speed = IB_SPEED_SDR;
1815 	} else if (netdev_speed <= SPEED_10000) {
1816 		*width = IB_WIDTH_1X;
1817 		*speed = IB_SPEED_FDR10;
1818 	} else if (netdev_speed <= SPEED_20000) {
1819 		*width = IB_WIDTH_4X;
1820 		*speed = IB_SPEED_DDR;
1821 	} else if (netdev_speed <= SPEED_25000) {
1822 		*width = IB_WIDTH_1X;
1823 		*speed = IB_SPEED_EDR;
1824 	} else if (netdev_speed <= SPEED_40000) {
1825 		*width = IB_WIDTH_4X;
1826 		*speed = IB_SPEED_FDR10;
1827 	} else {
1828 		*width = IB_WIDTH_4X;
1829 		*speed = IB_SPEED_EDR;
1830 	}
1831 
1832 	return 0;
1833 }
1834 EXPORT_SYMBOL(ib_get_eth_speed);
1835 
1836 int ib_modify_qp(struct ib_qp *qp,
1837 		 struct ib_qp_attr *qp_attr,
1838 		 int qp_attr_mask)
1839 {
1840 	return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
1841 }
1842 EXPORT_SYMBOL(ib_modify_qp);
1843 
1844 int ib_query_qp(struct ib_qp *qp,
1845 		struct ib_qp_attr *qp_attr,
1846 		int qp_attr_mask,
1847 		struct ib_qp_init_attr *qp_init_attr)
1848 {
1849 	qp_attr->ah_attr.grh.sgid_attr = NULL;
1850 	qp_attr->alt_ah_attr.grh.sgid_attr = NULL;
1851 
1852 	return qp->device->ops.query_qp ?
1853 		qp->device->ops.query_qp(qp->real_qp, qp_attr, qp_attr_mask,
1854 					 qp_init_attr) : -EOPNOTSUPP;
1855 }
1856 EXPORT_SYMBOL(ib_query_qp);
1857 
1858 int ib_close_qp(struct ib_qp *qp)
1859 {
1860 	struct ib_qp *real_qp;
1861 	unsigned long flags;
1862 
1863 	real_qp = qp->real_qp;
1864 	if (real_qp == qp)
1865 		return -EINVAL;
1866 
1867 	spin_lock_irqsave(&real_qp->device->qp_open_list_lock, flags);
1868 	list_del(&qp->open_list);
1869 	spin_unlock_irqrestore(&real_qp->device->qp_open_list_lock, flags);
1870 
1871 	atomic_dec(&real_qp->usecnt);
1872 	if (qp->qp_sec)
1873 		ib_close_shared_qp_security(qp->qp_sec);
1874 	kfree(qp);
1875 
1876 	return 0;
1877 }
1878 EXPORT_SYMBOL(ib_close_qp);
1879 
1880 static int __ib_destroy_shared_qp(struct ib_qp *qp)
1881 {
1882 	struct ib_xrcd *xrcd;
1883 	struct ib_qp *real_qp;
1884 	int ret;
1885 
1886 	real_qp = qp->real_qp;
1887 	xrcd = real_qp->xrcd;
1888 	down_write(&xrcd->tgt_qps_rwsem);
1889 	ib_close_qp(qp);
1890 	if (atomic_read(&real_qp->usecnt) == 0)
1891 		xa_erase(&xrcd->tgt_qps, real_qp->qp_num);
1892 	else
1893 		real_qp = NULL;
1894 	up_write(&xrcd->tgt_qps_rwsem);
1895 
1896 	if (real_qp) {
1897 		ret = ib_destroy_qp(real_qp);
1898 		if (!ret)
1899 			atomic_dec(&xrcd->usecnt);
1900 	}
1901 
1902 	return 0;
1903 }
1904 
1905 int ib_destroy_qp_user(struct ib_qp *qp, struct ib_udata *udata)
1906 {
1907 	const struct ib_gid_attr *alt_path_sgid_attr = qp->alt_path_sgid_attr;
1908 	const struct ib_gid_attr *av_sgid_attr = qp->av_sgid_attr;
1909 	struct ib_pd *pd;
1910 	struct ib_cq *scq, *rcq;
1911 	struct ib_srq *srq;
1912 	struct ib_rwq_ind_table *ind_tbl;
1913 	struct ib_qp_security *sec;
1914 	int ret;
1915 
1916 	WARN_ON_ONCE(qp->mrs_used > 0);
1917 
1918 	if (atomic_read(&qp->usecnt))
1919 		return -EBUSY;
1920 
1921 	if (qp->real_qp != qp)
1922 		return __ib_destroy_shared_qp(qp);
1923 
1924 	pd   = qp->pd;
1925 	scq  = qp->send_cq;
1926 	rcq  = qp->recv_cq;
1927 	srq  = qp->srq;
1928 	ind_tbl = qp->rwq_ind_tbl;
1929 	sec  = qp->qp_sec;
1930 	if (sec)
1931 		ib_destroy_qp_security_begin(sec);
1932 
1933 	if (!qp->uobject)
1934 		rdma_rw_cleanup_mrs(qp);
1935 
1936 	rdma_counter_unbind_qp(qp, true);
1937 	rdma_restrack_del(&qp->res);
1938 	ret = qp->device->ops.destroy_qp(qp, udata);
1939 	if (!ret) {
1940 		if (alt_path_sgid_attr)
1941 			rdma_put_gid_attr(alt_path_sgid_attr);
1942 		if (av_sgid_attr)
1943 			rdma_put_gid_attr(av_sgid_attr);
1944 		if (pd)
1945 			atomic_dec(&pd->usecnt);
1946 		if (scq)
1947 			atomic_dec(&scq->usecnt);
1948 		if (rcq)
1949 			atomic_dec(&rcq->usecnt);
1950 		if (srq)
1951 			atomic_dec(&srq->usecnt);
1952 		if (ind_tbl)
1953 			atomic_dec(&ind_tbl->usecnt);
1954 		if (sec)
1955 			ib_destroy_qp_security_end(sec);
1956 	} else {
1957 		if (sec)
1958 			ib_destroy_qp_security_abort(sec);
1959 	}
1960 
1961 	return ret;
1962 }
1963 EXPORT_SYMBOL(ib_destroy_qp_user);
1964 
1965 /* Completion queues */
1966 
1967 struct ib_cq *__ib_create_cq(struct ib_device *device,
1968 			     ib_comp_handler comp_handler,
1969 			     void (*event_handler)(struct ib_event *, void *),
1970 			     void *cq_context,
1971 			     const struct ib_cq_init_attr *cq_attr,
1972 			     const char *caller)
1973 {
1974 	struct ib_cq *cq;
1975 	int ret;
1976 
1977 	cq = rdma_zalloc_drv_obj(device, ib_cq);
1978 	if (!cq)
1979 		return ERR_PTR(-ENOMEM);
1980 
1981 	cq->device = device;
1982 	cq->uobject = NULL;
1983 	cq->comp_handler = comp_handler;
1984 	cq->event_handler = event_handler;
1985 	cq->cq_context = cq_context;
1986 	atomic_set(&cq->usecnt, 0);
1987 	cq->res.type = RDMA_RESTRACK_CQ;
1988 	rdma_restrack_set_task(&cq->res, caller);
1989 
1990 	ret = device->ops.create_cq(cq, cq_attr, NULL);
1991 	if (ret) {
1992 		kfree(cq);
1993 		return ERR_PTR(ret);
1994 	}
1995 
1996 	rdma_restrack_kadd(&cq->res);
1997 	return cq;
1998 }
1999 EXPORT_SYMBOL(__ib_create_cq);
2000 
2001 int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period)
2002 {
2003 	if (cq->shared)
2004 		return -EOPNOTSUPP;
2005 
2006 	return cq->device->ops.modify_cq ?
2007 		cq->device->ops.modify_cq(cq, cq_count,
2008 					  cq_period) : -EOPNOTSUPP;
2009 }
2010 EXPORT_SYMBOL(rdma_set_cq_moderation);
2011 
2012 int ib_destroy_cq_user(struct ib_cq *cq, struct ib_udata *udata)
2013 {
2014 	if (WARN_ON_ONCE(cq->shared))
2015 		return -EOPNOTSUPP;
2016 
2017 	if (atomic_read(&cq->usecnt))
2018 		return -EBUSY;
2019 
2020 	rdma_restrack_del(&cq->res);
2021 	cq->device->ops.destroy_cq(cq, udata);
2022 	kfree(cq);
2023 	return 0;
2024 }
2025 EXPORT_SYMBOL(ib_destroy_cq_user);
2026 
2027 int ib_resize_cq(struct ib_cq *cq, int cqe)
2028 {
2029 	if (cq->shared)
2030 		return -EOPNOTSUPP;
2031 
2032 	return cq->device->ops.resize_cq ?
2033 		cq->device->ops.resize_cq(cq, cqe, NULL) : -EOPNOTSUPP;
2034 }
2035 EXPORT_SYMBOL(ib_resize_cq);
2036 
2037 /* Memory regions */
2038 
2039 struct ib_mr *ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2040 			     u64 virt_addr, int access_flags)
2041 {
2042 	struct ib_mr *mr;
2043 
2044 	if (access_flags & IB_ACCESS_ON_DEMAND) {
2045 		if (!(pd->device->attrs.device_cap_flags &
2046 		      IB_DEVICE_ON_DEMAND_PAGING)) {
2047 			pr_debug("ODP support not available\n");
2048 			return ERR_PTR(-EINVAL);
2049 		}
2050 	}
2051 
2052 	mr = pd->device->ops.reg_user_mr(pd, start, length, virt_addr,
2053 					 access_flags, NULL);
2054 
2055 	if (IS_ERR(mr))
2056 		return mr;
2057 
2058 	mr->device = pd->device;
2059 	mr->pd = pd;
2060 	mr->dm = NULL;
2061 	atomic_inc(&pd->usecnt);
2062 	mr->res.type = RDMA_RESTRACK_MR;
2063 	rdma_restrack_kadd(&mr->res);
2064 
2065 	return mr;
2066 }
2067 EXPORT_SYMBOL(ib_reg_user_mr);
2068 
2069 int ib_advise_mr(struct ib_pd *pd, enum ib_uverbs_advise_mr_advice advice,
2070 		 u32 flags, struct ib_sge *sg_list, u32 num_sge)
2071 {
2072 	if (!pd->device->ops.advise_mr)
2073 		return -EOPNOTSUPP;
2074 
2075 	if (!num_sge)
2076 		return 0;
2077 
2078 	return pd->device->ops.advise_mr(pd, advice, flags, sg_list, num_sge,
2079 					 NULL);
2080 }
2081 EXPORT_SYMBOL(ib_advise_mr);
2082 
2083 int ib_dereg_mr_user(struct ib_mr *mr, struct ib_udata *udata)
2084 {
2085 	struct ib_pd *pd = mr->pd;
2086 	struct ib_dm *dm = mr->dm;
2087 	struct ib_sig_attrs *sig_attrs = mr->sig_attrs;
2088 	int ret;
2089 
2090 	trace_mr_dereg(mr);
2091 	rdma_restrack_del(&mr->res);
2092 	ret = mr->device->ops.dereg_mr(mr, udata);
2093 	if (!ret) {
2094 		atomic_dec(&pd->usecnt);
2095 		if (dm)
2096 			atomic_dec(&dm->usecnt);
2097 		kfree(sig_attrs);
2098 	}
2099 
2100 	return ret;
2101 }
2102 EXPORT_SYMBOL(ib_dereg_mr_user);
2103 
2104 /**
2105  * ib_alloc_mr() - Allocates a memory region
2106  * @pd:            protection domain associated with the region
2107  * @mr_type:       memory region type
2108  * @max_num_sg:    maximum sg entries available for registration.
2109  *
2110  * Notes:
2111  * Memory registeration page/sg lists must not exceed max_num_sg.
2112  * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
2113  * max_num_sg * used_page_size.
2114  *
2115  */
2116 struct ib_mr *ib_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
2117 			  u32 max_num_sg)
2118 {
2119 	struct ib_mr *mr;
2120 
2121 	if (!pd->device->ops.alloc_mr) {
2122 		mr = ERR_PTR(-EOPNOTSUPP);
2123 		goto out;
2124 	}
2125 
2126 	if (mr_type == IB_MR_TYPE_INTEGRITY) {
2127 		WARN_ON_ONCE(1);
2128 		mr = ERR_PTR(-EINVAL);
2129 		goto out;
2130 	}
2131 
2132 	mr = pd->device->ops.alloc_mr(pd, mr_type, max_num_sg);
2133 	if (IS_ERR(mr))
2134 		goto out;
2135 
2136 	mr->device = pd->device;
2137 	mr->pd = pd;
2138 	mr->dm = NULL;
2139 	mr->uobject = NULL;
2140 	atomic_inc(&pd->usecnt);
2141 	mr->need_inval = false;
2142 	mr->res.type = RDMA_RESTRACK_MR;
2143 	rdma_restrack_kadd(&mr->res);
2144 	mr->type = mr_type;
2145 	mr->sig_attrs = NULL;
2146 
2147 out:
2148 	trace_mr_alloc(pd, mr_type, max_num_sg, mr);
2149 	return mr;
2150 }
2151 EXPORT_SYMBOL(ib_alloc_mr);
2152 
2153 /**
2154  * ib_alloc_mr_integrity() - Allocates an integrity memory region
2155  * @pd:                      protection domain associated with the region
2156  * @max_num_data_sg:         maximum data sg entries available for registration
2157  * @max_num_meta_sg:         maximum metadata sg entries available for
2158  *                           registration
2159  *
2160  * Notes:
2161  * Memory registration page/sg lists must not exceed max_num_sg,
2162  * also the integrity page/sg lists must not exceed max_num_meta_sg.
2163  *
2164  */
2165 struct ib_mr *ib_alloc_mr_integrity(struct ib_pd *pd,
2166 				    u32 max_num_data_sg,
2167 				    u32 max_num_meta_sg)
2168 {
2169 	struct ib_mr *mr;
2170 	struct ib_sig_attrs *sig_attrs;
2171 
2172 	if (!pd->device->ops.alloc_mr_integrity ||
2173 	    !pd->device->ops.map_mr_sg_pi) {
2174 		mr = ERR_PTR(-EOPNOTSUPP);
2175 		goto out;
2176 	}
2177 
2178 	if (!max_num_meta_sg) {
2179 		mr = ERR_PTR(-EINVAL);
2180 		goto out;
2181 	}
2182 
2183 	sig_attrs = kzalloc(sizeof(struct ib_sig_attrs), GFP_KERNEL);
2184 	if (!sig_attrs) {
2185 		mr = ERR_PTR(-ENOMEM);
2186 		goto out;
2187 	}
2188 
2189 	mr = pd->device->ops.alloc_mr_integrity(pd, max_num_data_sg,
2190 						max_num_meta_sg);
2191 	if (IS_ERR(mr)) {
2192 		kfree(sig_attrs);
2193 		goto out;
2194 	}
2195 
2196 	mr->device = pd->device;
2197 	mr->pd = pd;
2198 	mr->dm = NULL;
2199 	mr->uobject = NULL;
2200 	atomic_inc(&pd->usecnt);
2201 	mr->need_inval = false;
2202 	mr->res.type = RDMA_RESTRACK_MR;
2203 	rdma_restrack_kadd(&mr->res);
2204 	mr->type = IB_MR_TYPE_INTEGRITY;
2205 	mr->sig_attrs = sig_attrs;
2206 
2207 out:
2208 	trace_mr_integ_alloc(pd, max_num_data_sg, max_num_meta_sg, mr);
2209 	return mr;
2210 }
2211 EXPORT_SYMBOL(ib_alloc_mr_integrity);
2212 
2213 /* Multicast groups */
2214 
2215 static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
2216 {
2217 	struct ib_qp_init_attr init_attr = {};
2218 	struct ib_qp_attr attr = {};
2219 	int num_eth_ports = 0;
2220 	int port;
2221 
2222 	/* If QP state >= init, it is assigned to a port and we can check this
2223 	 * port only.
2224 	 */
2225 	if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
2226 		if (attr.qp_state >= IB_QPS_INIT) {
2227 			if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
2228 			    IB_LINK_LAYER_INFINIBAND)
2229 				return true;
2230 			goto lid_check;
2231 		}
2232 	}
2233 
2234 	/* Can't get a quick answer, iterate over all ports */
2235 	for (port = 0; port < qp->device->phys_port_cnt; port++)
2236 		if (rdma_port_get_link_layer(qp->device, port) !=
2237 		    IB_LINK_LAYER_INFINIBAND)
2238 			num_eth_ports++;
2239 
2240 	/* If we have at lease one Ethernet port, RoCE annex declares that
2241 	 * multicast LID should be ignored. We can't tell at this step if the
2242 	 * QP belongs to an IB or Ethernet port.
2243 	 */
2244 	if (num_eth_ports)
2245 		return true;
2246 
2247 	/* If all the ports are IB, we can check according to IB spec. */
2248 lid_check:
2249 	return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
2250 		 lid == be16_to_cpu(IB_LID_PERMISSIVE));
2251 }
2252 
2253 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
2254 {
2255 	int ret;
2256 
2257 	if (!qp->device->ops.attach_mcast)
2258 		return -EOPNOTSUPP;
2259 
2260 	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2261 	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
2262 		return -EINVAL;
2263 
2264 	ret = qp->device->ops.attach_mcast(qp, gid, lid);
2265 	if (!ret)
2266 		atomic_inc(&qp->usecnt);
2267 	return ret;
2268 }
2269 EXPORT_SYMBOL(ib_attach_mcast);
2270 
2271 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
2272 {
2273 	int ret;
2274 
2275 	if (!qp->device->ops.detach_mcast)
2276 		return -EOPNOTSUPP;
2277 
2278 	if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
2279 	    qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
2280 		return -EINVAL;
2281 
2282 	ret = qp->device->ops.detach_mcast(qp, gid, lid);
2283 	if (!ret)
2284 		atomic_dec(&qp->usecnt);
2285 	return ret;
2286 }
2287 EXPORT_SYMBOL(ib_detach_mcast);
2288 
2289 /**
2290  * ib_alloc_xrcd_user - Allocates an XRC domain.
2291  * @device: The device on which to allocate the XRC domain.
2292  * @inode: inode to connect XRCD
2293  * @udata: Valid user data or NULL for kernel object
2294  */
2295 struct ib_xrcd *ib_alloc_xrcd_user(struct ib_device *device,
2296 				   struct inode *inode, struct ib_udata *udata)
2297 {
2298 	struct ib_xrcd *xrcd;
2299 	int ret;
2300 
2301 	if (!device->ops.alloc_xrcd)
2302 		return ERR_PTR(-EOPNOTSUPP);
2303 
2304 	xrcd = rdma_zalloc_drv_obj(device, ib_xrcd);
2305 	if (!xrcd)
2306 		return ERR_PTR(-ENOMEM);
2307 
2308 	xrcd->device = device;
2309 	xrcd->inode = inode;
2310 	atomic_set(&xrcd->usecnt, 0);
2311 	init_rwsem(&xrcd->tgt_qps_rwsem);
2312 	xa_init(&xrcd->tgt_qps);
2313 
2314 	ret = device->ops.alloc_xrcd(xrcd, udata);
2315 	if (ret)
2316 		goto err;
2317 	return xrcd;
2318 err:
2319 	kfree(xrcd);
2320 	return ERR_PTR(ret);
2321 }
2322 EXPORT_SYMBOL(ib_alloc_xrcd_user);
2323 
2324 /**
2325  * ib_dealloc_xrcd_user - Deallocates an XRC domain.
2326  * @xrcd: The XRC domain to deallocate.
2327  * @udata: Valid user data or NULL for kernel object
2328  */
2329 int ib_dealloc_xrcd_user(struct ib_xrcd *xrcd, struct ib_udata *udata)
2330 {
2331 	if (atomic_read(&xrcd->usecnt))
2332 		return -EBUSY;
2333 
2334 	WARN_ON(!xa_empty(&xrcd->tgt_qps));
2335 	xrcd->device->ops.dealloc_xrcd(xrcd, udata);
2336 	kfree(xrcd);
2337 	return 0;
2338 }
2339 EXPORT_SYMBOL(ib_dealloc_xrcd_user);
2340 
2341 /**
2342  * ib_create_wq - Creates a WQ associated with the specified protection
2343  * domain.
2344  * @pd: The protection domain associated with the WQ.
2345  * @wq_attr: A list of initial attributes required to create the
2346  * WQ. If WQ creation succeeds, then the attributes are updated to
2347  * the actual capabilities of the created WQ.
2348  *
2349  * wq_attr->max_wr and wq_attr->max_sge determine
2350  * the requested size of the WQ, and set to the actual values allocated
2351  * on return.
2352  * If ib_create_wq() succeeds, then max_wr and max_sge will always be
2353  * at least as large as the requested values.
2354  */
2355 struct ib_wq *ib_create_wq(struct ib_pd *pd,
2356 			   struct ib_wq_init_attr *wq_attr)
2357 {
2358 	struct ib_wq *wq;
2359 
2360 	if (!pd->device->ops.create_wq)
2361 		return ERR_PTR(-EOPNOTSUPP);
2362 
2363 	wq = pd->device->ops.create_wq(pd, wq_attr, NULL);
2364 	if (!IS_ERR(wq)) {
2365 		wq->event_handler = wq_attr->event_handler;
2366 		wq->wq_context = wq_attr->wq_context;
2367 		wq->wq_type = wq_attr->wq_type;
2368 		wq->cq = wq_attr->cq;
2369 		wq->device = pd->device;
2370 		wq->pd = pd;
2371 		wq->uobject = NULL;
2372 		atomic_inc(&pd->usecnt);
2373 		atomic_inc(&wq_attr->cq->usecnt);
2374 		atomic_set(&wq->usecnt, 0);
2375 	}
2376 	return wq;
2377 }
2378 EXPORT_SYMBOL(ib_create_wq);
2379 
2380 /**
2381  * ib_destroy_wq - Destroys the specified user WQ.
2382  * @wq: The WQ to destroy.
2383  * @udata: Valid user data
2384  */
2385 int ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
2386 {
2387 	struct ib_cq *cq = wq->cq;
2388 	struct ib_pd *pd = wq->pd;
2389 
2390 	if (atomic_read(&wq->usecnt))
2391 		return -EBUSY;
2392 
2393 	wq->device->ops.destroy_wq(wq, udata);
2394 	atomic_dec(&pd->usecnt);
2395 	atomic_dec(&cq->usecnt);
2396 
2397 	return 0;
2398 }
2399 EXPORT_SYMBOL(ib_destroy_wq);
2400 
2401 /**
2402  * ib_modify_wq - Modifies the specified WQ.
2403  * @wq: The WQ to modify.
2404  * @wq_attr: On input, specifies the WQ attributes to modify.
2405  * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
2406  *   are being modified.
2407  * On output, the current values of selected WQ attributes are returned.
2408  */
2409 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
2410 		 u32 wq_attr_mask)
2411 {
2412 	int err;
2413 
2414 	if (!wq->device->ops.modify_wq)
2415 		return -EOPNOTSUPP;
2416 
2417 	err = wq->device->ops.modify_wq(wq, wq_attr, wq_attr_mask, NULL);
2418 	return err;
2419 }
2420 EXPORT_SYMBOL(ib_modify_wq);
2421 
2422 /*
2423  * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
2424  * @wq_ind_table: The Indirection Table to destroy.
2425 */
2426 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
2427 {
2428 	int err, i;
2429 	u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
2430 	struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
2431 
2432 	if (atomic_read(&rwq_ind_table->usecnt))
2433 		return -EBUSY;
2434 
2435 	err = rwq_ind_table->device->ops.destroy_rwq_ind_table(rwq_ind_table);
2436 	if (!err) {
2437 		for (i = 0; i < table_size; i++)
2438 			atomic_dec(&ind_tbl[i]->usecnt);
2439 	}
2440 
2441 	return err;
2442 }
2443 EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
2444 
2445 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
2446 		       struct ib_mr_status *mr_status)
2447 {
2448 	if (!mr->device->ops.check_mr_status)
2449 		return -EOPNOTSUPP;
2450 
2451 	return mr->device->ops.check_mr_status(mr, check_mask, mr_status);
2452 }
2453 EXPORT_SYMBOL(ib_check_mr_status);
2454 
2455 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
2456 			 int state)
2457 {
2458 	if (!device->ops.set_vf_link_state)
2459 		return -EOPNOTSUPP;
2460 
2461 	return device->ops.set_vf_link_state(device, vf, port, state);
2462 }
2463 EXPORT_SYMBOL(ib_set_vf_link_state);
2464 
2465 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
2466 		     struct ifla_vf_info *info)
2467 {
2468 	if (!device->ops.get_vf_config)
2469 		return -EOPNOTSUPP;
2470 
2471 	return device->ops.get_vf_config(device, vf, port, info);
2472 }
2473 EXPORT_SYMBOL(ib_get_vf_config);
2474 
2475 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
2476 		    struct ifla_vf_stats *stats)
2477 {
2478 	if (!device->ops.get_vf_stats)
2479 		return -EOPNOTSUPP;
2480 
2481 	return device->ops.get_vf_stats(device, vf, port, stats);
2482 }
2483 EXPORT_SYMBOL(ib_get_vf_stats);
2484 
2485 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
2486 		   int type)
2487 {
2488 	if (!device->ops.set_vf_guid)
2489 		return -EOPNOTSUPP;
2490 
2491 	return device->ops.set_vf_guid(device, vf, port, guid, type);
2492 }
2493 EXPORT_SYMBOL(ib_set_vf_guid);
2494 
2495 int ib_get_vf_guid(struct ib_device *device, int vf, u8 port,
2496 		   struct ifla_vf_guid *node_guid,
2497 		   struct ifla_vf_guid *port_guid)
2498 {
2499 	if (!device->ops.get_vf_guid)
2500 		return -EOPNOTSUPP;
2501 
2502 	return device->ops.get_vf_guid(device, vf, port, node_guid, port_guid);
2503 }
2504 EXPORT_SYMBOL(ib_get_vf_guid);
2505 /**
2506  * ib_map_mr_sg_pi() - Map the dma mapped SG lists for PI (protection
2507  *     information) and set an appropriate memory region for registration.
2508  * @mr:             memory region
2509  * @data_sg:        dma mapped scatterlist for data
2510  * @data_sg_nents:  number of entries in data_sg
2511  * @data_sg_offset: offset in bytes into data_sg
2512  * @meta_sg:        dma mapped scatterlist for metadata
2513  * @meta_sg_nents:  number of entries in meta_sg
2514  * @meta_sg_offset: offset in bytes into meta_sg
2515  * @page_size:      page vector desired page size
2516  *
2517  * Constraints:
2518  * - The MR must be allocated with type IB_MR_TYPE_INTEGRITY.
2519  *
2520  * Return: 0 on success.
2521  *
2522  * After this completes successfully, the  memory region
2523  * is ready for registration.
2524  */
2525 int ib_map_mr_sg_pi(struct ib_mr *mr, struct scatterlist *data_sg,
2526 		    int data_sg_nents, unsigned int *data_sg_offset,
2527 		    struct scatterlist *meta_sg, int meta_sg_nents,
2528 		    unsigned int *meta_sg_offset, unsigned int page_size)
2529 {
2530 	if (unlikely(!mr->device->ops.map_mr_sg_pi ||
2531 		     WARN_ON_ONCE(mr->type != IB_MR_TYPE_INTEGRITY)))
2532 		return -EOPNOTSUPP;
2533 
2534 	mr->page_size = page_size;
2535 
2536 	return mr->device->ops.map_mr_sg_pi(mr, data_sg, data_sg_nents,
2537 					    data_sg_offset, meta_sg,
2538 					    meta_sg_nents, meta_sg_offset);
2539 }
2540 EXPORT_SYMBOL(ib_map_mr_sg_pi);
2541 
2542 /**
2543  * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
2544  *     and set it the memory region.
2545  * @mr:            memory region
2546  * @sg:            dma mapped scatterlist
2547  * @sg_nents:      number of entries in sg
2548  * @sg_offset:     offset in bytes into sg
2549  * @page_size:     page vector desired page size
2550  *
2551  * Constraints:
2552  *
2553  * - The first sg element is allowed to have an offset.
2554  * - Each sg element must either be aligned to page_size or virtually
2555  *   contiguous to the previous element. In case an sg element has a
2556  *   non-contiguous offset, the mapping prefix will not include it.
2557  * - The last sg element is allowed to have length less than page_size.
2558  * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
2559  *   then only max_num_sg entries will be mapped.
2560  * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
2561  *   constraints holds and the page_size argument is ignored.
2562  *
2563  * Returns the number of sg elements that were mapped to the memory region.
2564  *
2565  * After this completes successfully, the  memory region
2566  * is ready for registration.
2567  */
2568 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
2569 		 unsigned int *sg_offset, unsigned int page_size)
2570 {
2571 	if (unlikely(!mr->device->ops.map_mr_sg))
2572 		return -EOPNOTSUPP;
2573 
2574 	mr->page_size = page_size;
2575 
2576 	return mr->device->ops.map_mr_sg(mr, sg, sg_nents, sg_offset);
2577 }
2578 EXPORT_SYMBOL(ib_map_mr_sg);
2579 
2580 /**
2581  * ib_sg_to_pages() - Convert the largest prefix of a sg list
2582  *     to a page vector
2583  * @mr:            memory region
2584  * @sgl:           dma mapped scatterlist
2585  * @sg_nents:      number of entries in sg
2586  * @sg_offset_p:   ==== =======================================================
2587  *                 IN   start offset in bytes into sg
2588  *                 OUT  offset in bytes for element n of the sg of the first
2589  *                      byte that has not been processed where n is the return
2590  *                      value of this function.
2591  *                 ==== =======================================================
2592  * @set_page:      driver page assignment function pointer
2593  *
2594  * Core service helper for drivers to convert the largest
2595  * prefix of given sg list to a page vector. The sg list
2596  * prefix converted is the prefix that meet the requirements
2597  * of ib_map_mr_sg.
2598  *
2599  * Returns the number of sg elements that were assigned to
2600  * a page vector.
2601  */
2602 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
2603 		unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
2604 {
2605 	struct scatterlist *sg;
2606 	u64 last_end_dma_addr = 0;
2607 	unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
2608 	unsigned int last_page_off = 0;
2609 	u64 page_mask = ~((u64)mr->page_size - 1);
2610 	int i, ret;
2611 
2612 	if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
2613 		return -EINVAL;
2614 
2615 	mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
2616 	mr->length = 0;
2617 
2618 	for_each_sg(sgl, sg, sg_nents, i) {
2619 		u64 dma_addr = sg_dma_address(sg) + sg_offset;
2620 		u64 prev_addr = dma_addr;
2621 		unsigned int dma_len = sg_dma_len(sg) - sg_offset;
2622 		u64 end_dma_addr = dma_addr + dma_len;
2623 		u64 page_addr = dma_addr & page_mask;
2624 
2625 		/*
2626 		 * For the second and later elements, check whether either the
2627 		 * end of element i-1 or the start of element i is not aligned
2628 		 * on a page boundary.
2629 		 */
2630 		if (i && (last_page_off != 0 || page_addr != dma_addr)) {
2631 			/* Stop mapping if there is a gap. */
2632 			if (last_end_dma_addr != dma_addr)
2633 				break;
2634 
2635 			/*
2636 			 * Coalesce this element with the last. If it is small
2637 			 * enough just update mr->length. Otherwise start
2638 			 * mapping from the next page.
2639 			 */
2640 			goto next_page;
2641 		}
2642 
2643 		do {
2644 			ret = set_page(mr, page_addr);
2645 			if (unlikely(ret < 0)) {
2646 				sg_offset = prev_addr - sg_dma_address(sg);
2647 				mr->length += prev_addr - dma_addr;
2648 				if (sg_offset_p)
2649 					*sg_offset_p = sg_offset;
2650 				return i || sg_offset ? i : ret;
2651 			}
2652 			prev_addr = page_addr;
2653 next_page:
2654 			page_addr += mr->page_size;
2655 		} while (page_addr < end_dma_addr);
2656 
2657 		mr->length += dma_len;
2658 		last_end_dma_addr = end_dma_addr;
2659 		last_page_off = end_dma_addr & ~page_mask;
2660 
2661 		sg_offset = 0;
2662 	}
2663 
2664 	if (sg_offset_p)
2665 		*sg_offset_p = 0;
2666 	return i;
2667 }
2668 EXPORT_SYMBOL(ib_sg_to_pages);
2669 
2670 struct ib_drain_cqe {
2671 	struct ib_cqe cqe;
2672 	struct completion done;
2673 };
2674 
2675 static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
2676 {
2677 	struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
2678 						cqe);
2679 
2680 	complete(&cqe->done);
2681 }
2682 
2683 /*
2684  * Post a WR and block until its completion is reaped for the SQ.
2685  */
2686 static void __ib_drain_sq(struct ib_qp *qp)
2687 {
2688 	struct ib_cq *cq = qp->send_cq;
2689 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2690 	struct ib_drain_cqe sdrain;
2691 	struct ib_rdma_wr swr = {
2692 		.wr = {
2693 			.next = NULL,
2694 			{ .wr_cqe	= &sdrain.cqe, },
2695 			.opcode	= IB_WR_RDMA_WRITE,
2696 		},
2697 	};
2698 	int ret;
2699 
2700 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2701 	if (ret) {
2702 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2703 		return;
2704 	}
2705 
2706 	sdrain.cqe.done = ib_drain_qp_done;
2707 	init_completion(&sdrain.done);
2708 
2709 	ret = ib_post_send(qp, &swr.wr, NULL);
2710 	if (ret) {
2711 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
2712 		return;
2713 	}
2714 
2715 	if (cq->poll_ctx == IB_POLL_DIRECT)
2716 		while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
2717 			ib_process_cq_direct(cq, -1);
2718 	else
2719 		wait_for_completion(&sdrain.done);
2720 }
2721 
2722 /*
2723  * Post a WR and block until its completion is reaped for the RQ.
2724  */
2725 static void __ib_drain_rq(struct ib_qp *qp)
2726 {
2727 	struct ib_cq *cq = qp->recv_cq;
2728 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
2729 	struct ib_drain_cqe rdrain;
2730 	struct ib_recv_wr rwr = {};
2731 	int ret;
2732 
2733 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2734 	if (ret) {
2735 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2736 		return;
2737 	}
2738 
2739 	rwr.wr_cqe = &rdrain.cqe;
2740 	rdrain.cqe.done = ib_drain_qp_done;
2741 	init_completion(&rdrain.done);
2742 
2743 	ret = ib_post_recv(qp, &rwr, NULL);
2744 	if (ret) {
2745 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
2746 		return;
2747 	}
2748 
2749 	if (cq->poll_ctx == IB_POLL_DIRECT)
2750 		while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
2751 			ib_process_cq_direct(cq, -1);
2752 	else
2753 		wait_for_completion(&rdrain.done);
2754 }
2755 
2756 /**
2757  * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
2758  *		   application.
2759  * @qp:            queue pair to drain
2760  *
2761  * If the device has a provider-specific drain function, then
2762  * call that.  Otherwise call the generic drain function
2763  * __ib_drain_sq().
2764  *
2765  * The caller must:
2766  *
2767  * ensure there is room in the CQ and SQ for the drain work request and
2768  * completion.
2769  *
2770  * allocate the CQ using ib_alloc_cq().
2771  *
2772  * ensure that there are no other contexts that are posting WRs concurrently.
2773  * Otherwise the drain is not guaranteed.
2774  */
2775 void ib_drain_sq(struct ib_qp *qp)
2776 {
2777 	if (qp->device->ops.drain_sq)
2778 		qp->device->ops.drain_sq(qp);
2779 	else
2780 		__ib_drain_sq(qp);
2781 	trace_cq_drain_complete(qp->send_cq);
2782 }
2783 EXPORT_SYMBOL(ib_drain_sq);
2784 
2785 /**
2786  * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
2787  *		   application.
2788  * @qp:            queue pair to drain
2789  *
2790  * If the device has a provider-specific drain function, then
2791  * call that.  Otherwise call the generic drain function
2792  * __ib_drain_rq().
2793  *
2794  * The caller must:
2795  *
2796  * ensure there is room in the CQ and RQ for the drain work request and
2797  * completion.
2798  *
2799  * allocate the CQ using ib_alloc_cq().
2800  *
2801  * ensure that there are no other contexts that are posting WRs concurrently.
2802  * Otherwise the drain is not guaranteed.
2803  */
2804 void ib_drain_rq(struct ib_qp *qp)
2805 {
2806 	if (qp->device->ops.drain_rq)
2807 		qp->device->ops.drain_rq(qp);
2808 	else
2809 		__ib_drain_rq(qp);
2810 	trace_cq_drain_complete(qp->recv_cq);
2811 }
2812 EXPORT_SYMBOL(ib_drain_rq);
2813 
2814 /**
2815  * ib_drain_qp() - Block until all CQEs have been consumed by the
2816  *		   application on both the RQ and SQ.
2817  * @qp:            queue pair to drain
2818  *
2819  * The caller must:
2820  *
2821  * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
2822  * and completions.
2823  *
2824  * allocate the CQs using ib_alloc_cq().
2825  *
2826  * ensure that there are no other contexts that are posting WRs concurrently.
2827  * Otherwise the drain is not guaranteed.
2828  */
2829 void ib_drain_qp(struct ib_qp *qp)
2830 {
2831 	ib_drain_sq(qp);
2832 	if (!qp->srq)
2833 		ib_drain_rq(qp);
2834 }
2835 EXPORT_SYMBOL(ib_drain_qp);
2836 
2837 struct net_device *rdma_alloc_netdev(struct ib_device *device, u8 port_num,
2838 				     enum rdma_netdev_t type, const char *name,
2839 				     unsigned char name_assign_type,
2840 				     void (*setup)(struct net_device *))
2841 {
2842 	struct rdma_netdev_alloc_params params;
2843 	struct net_device *netdev;
2844 	int rc;
2845 
2846 	if (!device->ops.rdma_netdev_get_params)
2847 		return ERR_PTR(-EOPNOTSUPP);
2848 
2849 	rc = device->ops.rdma_netdev_get_params(device, port_num, type,
2850 						&params);
2851 	if (rc)
2852 		return ERR_PTR(rc);
2853 
2854 	netdev = alloc_netdev_mqs(params.sizeof_priv, name, name_assign_type,
2855 				  setup, params.txqs, params.rxqs);
2856 	if (!netdev)
2857 		return ERR_PTR(-ENOMEM);
2858 
2859 	return netdev;
2860 }
2861 EXPORT_SYMBOL(rdma_alloc_netdev);
2862 
2863 int rdma_init_netdev(struct ib_device *device, u8 port_num,
2864 		     enum rdma_netdev_t type, const char *name,
2865 		     unsigned char name_assign_type,
2866 		     void (*setup)(struct net_device *),
2867 		     struct net_device *netdev)
2868 {
2869 	struct rdma_netdev_alloc_params params;
2870 	int rc;
2871 
2872 	if (!device->ops.rdma_netdev_get_params)
2873 		return -EOPNOTSUPP;
2874 
2875 	rc = device->ops.rdma_netdev_get_params(device, port_num, type,
2876 						&params);
2877 	if (rc)
2878 		return rc;
2879 
2880 	return params.initialize_rdma_netdev(device, port_num,
2881 					     netdev, params.param);
2882 }
2883 EXPORT_SYMBOL(rdma_init_netdev);
2884 
2885 void __rdma_block_iter_start(struct ib_block_iter *biter,
2886 			     struct scatterlist *sglist, unsigned int nents,
2887 			     unsigned long pgsz)
2888 {
2889 	memset(biter, 0, sizeof(struct ib_block_iter));
2890 	biter->__sg = sglist;
2891 	biter->__sg_nents = nents;
2892 
2893 	/* Driver provides best block size to use */
2894 	biter->__pg_bit = __fls(pgsz);
2895 }
2896 EXPORT_SYMBOL(__rdma_block_iter_start);
2897 
2898 bool __rdma_block_iter_next(struct ib_block_iter *biter)
2899 {
2900 	unsigned int block_offset;
2901 
2902 	if (!biter->__sg_nents || !biter->__sg)
2903 		return false;
2904 
2905 	biter->__dma_addr = sg_dma_address(biter->__sg) + biter->__sg_advance;
2906 	block_offset = biter->__dma_addr & (BIT_ULL(biter->__pg_bit) - 1);
2907 	biter->__sg_advance += BIT_ULL(biter->__pg_bit) - block_offset;
2908 
2909 	if (biter->__sg_advance >= sg_dma_len(biter->__sg)) {
2910 		biter->__sg_advance = 0;
2911 		biter->__sg = sg_next(biter->__sg);
2912 		biter->__sg_nents--;
2913 	}
2914 
2915 	return true;
2916 }
2917 EXPORT_SYMBOL(__rdma_block_iter_next);
2918