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