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