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