xref: /freebsd/sys/ofed/include/rdma/ib_verbs.h (revision b6354f04081823cc0ccf2af573a59a826d7763c8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
5  * Copyright (c) 2004 Infinicon Corporation.  All rights reserved.
6  * Copyright (c) 2004 Intel Corporation.  All rights reserved.
7  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
8  * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
9  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
10  * Copyright (c) 2005, 2006, 2007 Cisco Systems.  All rights reserved.
11  *
12  * This software is available to you under a choice of one of two
13  * licenses.  You may choose to be licensed under the terms of the GNU
14  * General Public License (GPL) Version 2, available from the file
15  * COPYING in the main directory of this source tree, or the
16  * OpenIB.org BSD license below:
17  *
18  *     Redistribution and use in source and binary forms, with or
19  *     without modification, are permitted provided that the following
20  *     conditions are met:
21  *
22  *      - Redistributions of source code must retain the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer.
25  *
26  *      - Redistributions in binary form must reproduce the above
27  *        copyright notice, this list of conditions and the following
28  *        disclaimer in the documentation and/or other materials
29  *        provided with the distribution.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
35  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
36  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
37  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40 
41 #if !defined(IB_VERBS_H)
42 #define IB_VERBS_H
43 
44 #include <linux/types.h>
45 #include <linux/device.h>
46 #include <linux/mm.h>
47 #include <linux/dma-mapping.h>
48 #include <linux/kref.h>
49 #include <linux/list.h>
50 #include <linux/rwsem.h>
51 #include <linux/scatterlist.h>
52 #include <linux/workqueue.h>
53 #include <linux/socket.h>
54 #include <linux/if_ether.h>
55 #include <net/ipv6.h>
56 #include <net/ip.h>
57 #include <linux/string.h>
58 #include <linux/slab.h>
59 #include <linux/rcupdate.h>
60 #include <linux/netdevice.h>
61 #include <linux/xarray.h>
62 #include <netinet/ip.h>
63 #include <uapi/rdma/ib_user_verbs.h>
64 #include <rdma/signature.h>
65 #include <uapi/rdma/rdma_user_ioctl.h>
66 #include <uapi/rdma/ib_user_ioctl_verbs.h>
67 
68 #include <asm/atomic.h>
69 #include <asm/uaccess.h>
70 
71 struct ib_uqp_object;
72 struct ib_usrq_object;
73 struct ib_uwq_object;
74 struct ifla_vf_info;
75 struct ifla_vf_stats;
76 struct ib_uverbs_file;
77 struct uverbs_attr_bundle;
78 
79 enum ib_uverbs_advise_mr_advice;
80 
81 extern struct workqueue_struct *ib_wq;
82 extern struct workqueue_struct *ib_comp_wq;
83 
84 struct ib_ucq_object;
85 
86 union ib_gid {
87 	u8	raw[16];
88 	struct {
89 		__be64	subnet_prefix;
90 		__be64	interface_id;
91 	} global;
92 };
93 
94 extern union ib_gid zgid;
95 
96 enum ib_gid_type {
97 	/* If link layer is Ethernet, this is RoCE V1 */
98 	IB_GID_TYPE_IB        = 0,
99 	IB_GID_TYPE_ROCE      = 0,
100 	IB_GID_TYPE_ROCE_UDP_ENCAP = 1,
101 	IB_GID_TYPE_SIZE
102 };
103 
104 #define ROCE_V2_UDP_DPORT      4791
105 struct ib_gid_attr {
106 	if_t ndev;
107 	struct ib_device	*device;
108 	union ib_gid		gid;
109 	enum ib_gid_type	gid_type;
110 	u16			index;
111 	u8			port_num;
112 };
113 
114 enum rdma_node_type {
115 	/* IB values map to NodeInfo:NodeType. */
116 	RDMA_NODE_IB_CA 	= 1,
117 	RDMA_NODE_IB_SWITCH,
118 	RDMA_NODE_IB_ROUTER,
119 	RDMA_NODE_RNIC,
120 	RDMA_NODE_USNIC,
121 	RDMA_NODE_USNIC_UDP,
122 };
123 
124 enum {
125 	/* set the local administered indication */
126 	IB_SA_WELL_KNOWN_GUID	= BIT_ULL(57) | 2,
127 };
128 
129 enum rdma_transport_type {
130 	RDMA_TRANSPORT_IB,
131 	RDMA_TRANSPORT_IWARP,
132 	RDMA_TRANSPORT_USNIC,
133 	RDMA_TRANSPORT_USNIC_UDP
134 };
135 
136 enum rdma_protocol_type {
137 	RDMA_PROTOCOL_IB,
138 	RDMA_PROTOCOL_IBOE,
139 	RDMA_PROTOCOL_IWARP,
140 	RDMA_PROTOCOL_USNIC_UDP
141 };
142 
143 __attribute_const__ enum rdma_transport_type
144 rdma_node_get_transport(enum rdma_node_type node_type);
145 
146 enum rdma_network_type {
147 	RDMA_NETWORK_IB,
148 	RDMA_NETWORK_ROCE_V1 = RDMA_NETWORK_IB,
149 	RDMA_NETWORK_IPV4,
150 	RDMA_NETWORK_IPV6
151 };
152 
153 static inline enum ib_gid_type ib_network_to_gid_type(enum rdma_network_type network_type)
154 {
155 	if (network_type == RDMA_NETWORK_IPV4 ||
156 	    network_type == RDMA_NETWORK_IPV6)
157 		return IB_GID_TYPE_ROCE_UDP_ENCAP;
158 
159 	/* IB_GID_TYPE_IB same as RDMA_NETWORK_ROCE_V1 */
160 	return IB_GID_TYPE_IB;
161 }
162 
163 static inline enum rdma_network_type
164 rdma_gid_attr_network_type(const struct ib_gid_attr *attr)
165 {
166 	if (attr->gid_type == IB_GID_TYPE_IB)
167 		return RDMA_NETWORK_IB;
168 
169 	if (ipv6_addr_v4mapped((const struct in6_addr *)&attr->gid))
170 		return RDMA_NETWORK_IPV4;
171 	else
172 		return RDMA_NETWORK_IPV6;
173 }
174 
175 enum rdma_link_layer {
176 	IB_LINK_LAYER_UNSPECIFIED,
177 	IB_LINK_LAYER_INFINIBAND,
178 	IB_LINK_LAYER_ETHERNET,
179 };
180 
181 enum ib_device_cap_flags {
182 	IB_DEVICE_RESIZE_MAX_WR			= (1 << 0),
183 	IB_DEVICE_BAD_PKEY_CNTR			= (1 << 1),
184 	IB_DEVICE_BAD_QKEY_CNTR			= (1 << 2),
185 	IB_DEVICE_RAW_MULTI			= (1 << 3),
186 	IB_DEVICE_AUTO_PATH_MIG			= (1 << 4),
187 	IB_DEVICE_CHANGE_PHY_PORT		= (1 << 5),
188 	IB_DEVICE_UD_AV_PORT_ENFORCE		= (1 << 6),
189 	IB_DEVICE_CURR_QP_STATE_MOD		= (1 << 7),
190 	IB_DEVICE_SHUTDOWN_PORT			= (1 << 8),
191 	IB_DEVICE_INIT_TYPE			= (1 << 9),
192 	IB_DEVICE_PORT_ACTIVE_EVENT		= (1 << 10),
193 	IB_DEVICE_SYS_IMAGE_GUID		= (1 << 11),
194 	IB_DEVICE_RC_RNR_NAK_GEN		= (1 << 12),
195 	IB_DEVICE_SRQ_RESIZE			= (1 << 13),
196 	IB_DEVICE_N_NOTIFY_CQ			= (1 << 14),
197 
198 	/*
199 	 * This device supports a per-device lkey or stag that can be
200 	 * used without performing a memory registration for the local
201 	 * memory.  Note that ULPs should never check this flag, but
202 	 * instead of use the local_dma_lkey flag in the ib_pd structure,
203 	 * which will always contain a usable lkey.
204 	 */
205 	IB_DEVICE_LOCAL_DMA_LKEY		= (1 << 15),
206 	IB_DEVICE_RESERVED /* old SEND_W_INV */	= (1 << 16),
207 	IB_DEVICE_MEM_WINDOW			= (1 << 17),
208 	/*
209 	 * Devices should set IB_DEVICE_UD_IP_SUM if they support
210 	 * insertion of UDP and TCP checksum on outgoing UD IPoIB
211 	 * messages and can verify the validity of checksum for
212 	 * incoming messages.  Setting this flag implies that the
213 	 * IPoIB driver may set NETIF_F_IP_CSUM for datagram mode.
214 	 */
215 	IB_DEVICE_UD_IP_CSUM			= (1 << 18),
216 	IB_DEVICE_UD_TSO			= (1 << 19),
217 	IB_DEVICE_XRC				= (1 << 20),
218 
219 	/*
220 	 * This device supports the IB "base memory management extension",
221 	 * which includes support for fast registrations (IB_WR_REG_MR,
222 	 * IB_WR_LOCAL_INV and IB_WR_SEND_WITH_INV verbs).  This flag should
223 	 * also be set by any iWarp device which must support FRs to comply
224 	 * to the iWarp verbs spec.  iWarp devices also support the
225 	 * IB_WR_RDMA_READ_WITH_INV verb for RDMA READs that invalidate the
226 	 * stag.
227 	 */
228 	IB_DEVICE_MEM_MGT_EXTENSIONS		= (1 << 21),
229 	IB_DEVICE_BLOCK_MULTICAST_LOOPBACK	= (1 << 22),
230 	IB_DEVICE_MEM_WINDOW_TYPE_2A		= (1 << 23),
231 	IB_DEVICE_MEM_WINDOW_TYPE_2B		= (1 << 24),
232 	IB_DEVICE_RC_IP_CSUM			= (1 << 25),
233 	/* Deprecated. Please use IB_RAW_PACKET_CAP_IP_CSUM. */
234 	IB_DEVICE_RAW_IP_CSUM			= (1 << 26),
235 	/*
236 	 * Devices should set IB_DEVICE_CROSS_CHANNEL if they
237 	 * support execution of WQEs that involve synchronization
238 	 * of I/O operations with single completion queue managed
239 	 * by hardware.
240 	 */
241 	IB_DEVICE_CROSS_CHANNEL		= (1 << 27),
242 	IB_DEVICE_MANAGED_FLOW_STEERING		= (1 << 29),
243 	IB_DEVICE_SIGNATURE_HANDOVER		= (1 << 30),
244 	IB_DEVICE_ON_DEMAND_PAGING		= (1ULL << 31),
245 	IB_DEVICE_SG_GAPS_REG			= (1ULL << 32),
246 	IB_DEVICE_VIRTUAL_FUNCTION		= (1ULL << 33),
247 	/* Deprecated. Please use IB_RAW_PACKET_CAP_SCATTER_FCS. */
248 	IB_DEVICE_RAW_SCATTER_FCS		= (1ULL << 34),
249 	IB_DEVICE_KNOWSEPOCH			= (1ULL << 35),
250 };
251 
252 enum ib_atomic_cap {
253 	IB_ATOMIC_NONE,
254 	IB_ATOMIC_HCA,
255 	IB_ATOMIC_GLOB
256 };
257 
258 enum ib_odp_general_cap_bits {
259 	IB_ODP_SUPPORT		= 1 << 0,
260 	IB_ODP_SUPPORT_IMPLICIT = 1 << 1,
261 };
262 
263 enum ib_odp_transport_cap_bits {
264 	IB_ODP_SUPPORT_SEND	= 1 << 0,
265 	IB_ODP_SUPPORT_RECV	= 1 << 1,
266 	IB_ODP_SUPPORT_WRITE	= 1 << 2,
267 	IB_ODP_SUPPORT_READ	= 1 << 3,
268 	IB_ODP_SUPPORT_ATOMIC	= 1 << 4,
269 };
270 
271 struct ib_odp_caps {
272 	uint64_t general_caps;
273 	struct {
274 		uint32_t  rc_odp_caps;
275 		uint32_t  uc_odp_caps;
276 		uint32_t  ud_odp_caps;
277 		uint32_t  xrc_odp_caps;
278 	} per_transport_caps;
279 };
280 
281 struct ib_rss_caps {
282 	/* Corresponding bit will be set if qp type from
283 	 * 'enum ib_qp_type' is supported, e.g.
284 	 * supported_qpts |= 1 << IB_QPT_UD
285 	 */
286 	u32 supported_qpts;
287 	u32 max_rwq_indirection_tables;
288 	u32 max_rwq_indirection_table_size;
289 };
290 
291 enum ib_tm_cap_flags {
292 	/*  Support tag matching with rendezvous offload for RC transport */
293 	IB_TM_CAP_RNDV_RC = 1 << 0,
294 };
295 
296 struct ib_tm_caps {
297 	/* Max size of RNDV header */
298 	u32 max_rndv_hdr_size;
299 	/* Max number of entries in tag matching list */
300 	u32 max_num_tags;
301 	/* From enum ib_tm_cap_flags */
302 	u32 flags;
303 	/* Max number of outstanding list operations */
304 	u32 max_ops;
305 	/* Max number of SGE in tag matching entry */
306 	u32 max_sge;
307 };
308 
309 enum ib_cq_creation_flags {
310 	IB_CQ_FLAGS_TIMESTAMP_COMPLETION   = 1 << 0,
311 	IB_CQ_FLAGS_IGNORE_OVERRUN	   = 1 << 1,
312 };
313 
314 struct ib_cq_init_attr {
315 	unsigned int	cqe;
316 	u32		comp_vector;
317 	u32		flags;
318 };
319 
320 enum ib_cq_attr_mask {
321 	IB_CQ_MODERATE = 1 << 0,
322 };
323 
324 struct ib_cq_caps {
325 	u16     max_cq_moderation_count;
326 	u16     max_cq_moderation_period;
327 };
328 
329 struct ib_dm_mr_attr {
330 	u64		length;
331 	u64		offset;
332 	u32		access_flags;
333 };
334 
335 struct ib_dm_alloc_attr {
336 	u64	length;
337 	u32	alignment;
338 	u32	flags;
339 };
340 
341 struct ib_device_attr {
342 	u64			fw_ver;
343 	__be64			sys_image_guid;
344 	u64			max_mr_size;
345 	u64			page_size_cap;
346 	u32			vendor_id;
347 	u32			vendor_part_id;
348 	u32			hw_ver;
349 	int			max_qp;
350 	int			max_qp_wr;
351 	u64			device_cap_flags;
352 	int			max_sge;
353 	int			max_sge_rd;
354 	int			max_cq;
355 	int			max_cqe;
356 	int			max_mr;
357 	int			max_pd;
358 	int			max_qp_rd_atom;
359 	int			max_ee_rd_atom;
360 	int			max_res_rd_atom;
361 	int			max_qp_init_rd_atom;
362 	int			max_ee_init_rd_atom;
363 	enum ib_atomic_cap	atomic_cap;
364 	enum ib_atomic_cap	masked_atomic_cap;
365 	int			max_ee;
366 	int			max_rdd;
367 	int			max_mw;
368 	int			max_raw_ipv6_qp;
369 	int			max_raw_ethy_qp;
370 	int			max_mcast_grp;
371 	int			max_mcast_qp_attach;
372 	int			max_total_mcast_qp_attach;
373 	int			max_ah;
374 	int			max_fmr;
375 	int			max_map_per_fmr;
376 	int			max_srq;
377 	int			max_srq_wr;
378 	union {
379 		int		max_srq_sge;
380 		int		max_send_sge;
381 		int		max_recv_sge;
382 	};
383 	unsigned int		max_fast_reg_page_list_len;
384 	u16			max_pkeys;
385 	u8			local_ca_ack_delay;
386 	int			sig_prot_cap;
387 	int			sig_guard_cap;
388 	struct ib_odp_caps	odp_caps;
389 	uint64_t		timestamp_mask;
390 	uint64_t		hca_core_clock; /* in KHZ */
391 	struct ib_rss_caps	rss_caps;
392 	u32			max_wq_type_rq;
393 	u32			raw_packet_caps; /* Use ib_raw_packet_caps enum */
394 	struct ib_tm_caps	tm_caps;
395 	struct ib_cq_caps       cq_caps;
396 	u64			max_dm_size;
397 	/* Max entries for sgl for optimized performance per READ */
398 	u32			max_sgl_rd;
399 };
400 
401 enum ib_mtu {
402 	IB_MTU_256  = 1,
403 	IB_MTU_512  = 2,
404 	IB_MTU_1024 = 3,
405 	IB_MTU_2048 = 4,
406 	IB_MTU_4096 = 5
407 };
408 
409 static inline int ib_mtu_enum_to_int(enum ib_mtu mtu)
410 {
411 	switch (mtu) {
412 	case IB_MTU_256:  return  256;
413 	case IB_MTU_512:  return  512;
414 	case IB_MTU_1024: return 1024;
415 	case IB_MTU_2048: return 2048;
416 	case IB_MTU_4096: return 4096;
417 	default: 	  return -1;
418 	}
419 }
420 
421 static inline enum ib_mtu ib_mtu_int_to_enum(int mtu)
422 {
423 	if (mtu >= 4096)
424 		return IB_MTU_4096;
425 	else if (mtu >= 2048)
426 		return IB_MTU_2048;
427 	else if (mtu >= 1024)
428 		return IB_MTU_1024;
429 	else if (mtu >= 512)
430 		return IB_MTU_512;
431 	else
432 		return IB_MTU_256;
433 }
434 
435 enum ib_port_state {
436 	IB_PORT_NOP		= 0,
437 	IB_PORT_DOWN		= 1,
438 	IB_PORT_INIT		= 2,
439 	IB_PORT_ARMED		= 3,
440 	IB_PORT_ACTIVE		= 4,
441 	IB_PORT_ACTIVE_DEFER	= 5,
442 	IB_PORT_DUMMY		= -1,	/* force enum signed */
443 };
444 
445 enum ib_port_cap_flags {
446 	IB_PORT_SM				= 1 <<  1,
447 	IB_PORT_NOTICE_SUP			= 1 <<  2,
448 	IB_PORT_TRAP_SUP			= 1 <<  3,
449 	IB_PORT_OPT_IPD_SUP                     = 1 <<  4,
450 	IB_PORT_AUTO_MIGR_SUP			= 1 <<  5,
451 	IB_PORT_SL_MAP_SUP			= 1 <<  6,
452 	IB_PORT_MKEY_NVRAM			= 1 <<  7,
453 	IB_PORT_PKEY_NVRAM			= 1 <<  8,
454 	IB_PORT_LED_INFO_SUP			= 1 <<  9,
455 	IB_PORT_SM_DISABLED			= 1 << 10,
456 	IB_PORT_SYS_IMAGE_GUID_SUP		= 1 << 11,
457 	IB_PORT_PKEY_SW_EXT_PORT_TRAP_SUP	= 1 << 12,
458 	IB_PORT_EXTENDED_SPEEDS_SUP             = 1 << 14,
459 	IB_PORT_CM_SUP				= 1 << 16,
460 	IB_PORT_SNMP_TUNNEL_SUP			= 1 << 17,
461 	IB_PORT_REINIT_SUP			= 1 << 18,
462 	IB_PORT_DEVICE_MGMT_SUP			= 1 << 19,
463 	IB_PORT_VENDOR_CLASS_SUP		= 1 << 20,
464 	IB_PORT_DR_NOTICE_SUP			= 1 << 21,
465 	IB_PORT_CAP_MASK_NOTICE_SUP		= 1 << 22,
466 	IB_PORT_BOOT_MGMT_SUP			= 1 << 23,
467 	IB_PORT_LINK_LATENCY_SUP		= 1 << 24,
468 	IB_PORT_CLIENT_REG_SUP			= 1 << 25,
469 	IB_PORT_IP_BASED_GIDS			= 1 << 26,
470 };
471 
472 enum ib_port_phys_state {
473 	IB_PORT_PHYS_STATE_SLEEP = 1,
474 	IB_PORT_PHYS_STATE_POLLING = 2,
475 	IB_PORT_PHYS_STATE_DISABLED = 3,
476 	IB_PORT_PHYS_STATE_PORT_CONFIGURATION_TRAINING = 4,
477 	IB_PORT_PHYS_STATE_LINK_UP = 5,
478 	IB_PORT_PHYS_STATE_LINK_ERROR_RECOVERY = 6,
479 	IB_PORT_PHYS_STATE_PHY_TEST = 7,
480 };
481 
482 enum ib_port_width {
483 	IB_WIDTH_1X	= 1,
484 	IB_WIDTH_2X	= 16,
485 	IB_WIDTH_4X	= 2,
486 	IB_WIDTH_8X	= 4,
487 	IB_WIDTH_12X	= 8
488 };
489 
490 static inline int ib_width_enum_to_int(enum ib_port_width width)
491 {
492 	switch (width) {
493 	case IB_WIDTH_1X:  return  1;
494 	case IB_WIDTH_2X:  return  2;
495 	case IB_WIDTH_4X:  return  4;
496 	case IB_WIDTH_8X:  return  8;
497 	case IB_WIDTH_12X: return 12;
498 	default: 	  return -1;
499 	}
500 }
501 
502 enum ib_port_speed {
503 	IB_SPEED_SDR	= 1,
504 	IB_SPEED_DDR	= 2,
505 	IB_SPEED_QDR	= 4,
506 	IB_SPEED_FDR10	= 8,
507 	IB_SPEED_FDR	= 16,
508 	IB_SPEED_EDR	= 32,
509 	IB_SPEED_HDR	= 64,
510 	IB_SPEED_NDR	= 128,
511 	IB_SPEED_XDR	= 256,
512 };
513 
514 /**
515  * struct rdma_hw_stats
516  * @lock - Mutex to protect parallel write access to lifespan and values
517  *    of counters, which are 64bits and not guaranteeed to be written
518  *    atomicaly on 32bits systems.
519  * @timestamp - Used by the core code to track when the last update was
520  * @lifespan - Used by the core code to determine how old the counters
521  *   should be before being updated again.  Stored in jiffies, defaults
522  *   to 10 milliseconds, drivers can override the default be specifying
523  *   their own value during their allocation routine.
524  * @name - Array of pointers to static names used for the counters in
525  *   directory.
526  * @num_counters - How many hardware counters there are.  If name is
527  *   shorter than this number, a kernel oops will result.  Driver authors
528  *   are encouraged to leave BUILD_BUG_ON(ARRAY_SIZE(@name) < num_counters)
529  *   in their code to prevent this.
530  * @value - Array of u64 counters that are accessed by the sysfs code and
531  *   filled in by the drivers get_stats routine
532  */
533 struct rdma_hw_stats {
534 	struct mutex	lock; /* Protect lifespan and values[] */
535 	unsigned long	timestamp;
536 	unsigned long	lifespan;
537 	const char * const *names;
538 	int		num_counters;
539 	u64		value[];
540 };
541 
542 #define RDMA_HW_STATS_DEFAULT_LIFESPAN 10
543 /**
544  * rdma_alloc_hw_stats_struct - Helper function to allocate dynamic struct
545  *   for drivers.
546  * @names - Array of static const char *
547  * @num_counters - How many elements in array
548  * @lifespan - How many milliseconds between updates
549  */
550 static inline struct rdma_hw_stats *rdma_alloc_hw_stats_struct(
551 		const char * const *names, int num_counters,
552 		unsigned long lifespan)
553 {
554 	struct rdma_hw_stats *stats;
555 
556 	stats = kzalloc(sizeof(*stats) + num_counters * sizeof(u64),
557 			GFP_KERNEL);
558 	if (!stats)
559 		return NULL;
560 	stats->names = names;
561 	stats->num_counters = num_counters;
562 	stats->lifespan = msecs_to_jiffies(lifespan);
563 
564 	return stats;
565 }
566 
567 
568 /* Define bits for the various functionality this port needs to be supported by
569  * the core.
570  */
571 /* Management                           0x00000FFF */
572 #define RDMA_CORE_CAP_IB_MAD            0x00000001
573 #define RDMA_CORE_CAP_IB_SMI            0x00000002
574 #define RDMA_CORE_CAP_IB_CM             0x00000004
575 #define RDMA_CORE_CAP_IW_CM             0x00000008
576 #define RDMA_CORE_CAP_IB_SA             0x00000010
577 #define RDMA_CORE_CAP_OPA_MAD           0x00000020
578 
579 /* Address format                       0x000FF000 */
580 #define RDMA_CORE_CAP_AF_IB             0x00001000
581 #define RDMA_CORE_CAP_ETH_AH            0x00002000
582 #define RDMA_CORE_CAP_OPA_AH            0x00004000
583 
584 /* Protocol                             0xFFF00000 */
585 #define RDMA_CORE_CAP_PROT_IB           0x00100000
586 #define RDMA_CORE_CAP_PROT_ROCE         0x00200000
587 #define RDMA_CORE_CAP_PROT_IWARP        0x00400000
588 #define RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP 0x00800000
589 
590 #define RDMA_CORE_PORT_IBA_IB          (RDMA_CORE_CAP_PROT_IB  \
591 					| RDMA_CORE_CAP_IB_MAD \
592 					| RDMA_CORE_CAP_IB_SMI \
593 					| RDMA_CORE_CAP_IB_CM  \
594 					| RDMA_CORE_CAP_IB_SA  \
595 					| RDMA_CORE_CAP_AF_IB)
596 #define RDMA_CORE_PORT_IBA_ROCE        (RDMA_CORE_CAP_PROT_ROCE \
597 					| RDMA_CORE_CAP_IB_MAD  \
598 					| RDMA_CORE_CAP_IB_CM   \
599 					| RDMA_CORE_CAP_AF_IB   \
600 					| RDMA_CORE_CAP_ETH_AH)
601 #define RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP			\
602 					(RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP \
603 					| RDMA_CORE_CAP_IB_MAD  \
604 					| RDMA_CORE_CAP_IB_CM   \
605 					| RDMA_CORE_CAP_AF_IB   \
606 					| RDMA_CORE_CAP_ETH_AH)
607 #define RDMA_CORE_PORT_IWARP           (RDMA_CORE_CAP_PROT_IWARP \
608 					| RDMA_CORE_CAP_IW_CM)
609 #define RDMA_CORE_PORT_INTEL_OPA       (RDMA_CORE_PORT_IBA_IB  \
610 					| RDMA_CORE_CAP_OPA_MAD)
611 
612 struct ib_port_attr {
613 	u64			subnet_prefix;
614 	enum ib_port_state	state;
615 	enum ib_mtu		max_mtu;
616 	enum ib_mtu		active_mtu;
617 	int			gid_tbl_len;
618 	unsigned int		ip_gids:1;
619 	/* This is the value from PortInfo CapabilityMask, defined by IBA */
620 	u32			port_cap_flags;
621 	u32			max_msg_sz;
622 	u32			bad_pkey_cntr;
623 	u32			qkey_viol_cntr;
624 	u16			pkey_tbl_len;
625 	u32			sm_lid;
626 	u32			lid;
627 	u8			lmc;
628 	u8			max_vl_num;
629 	u8			sm_sl;
630 	u8			subnet_timeout;
631 	u8			init_type_reply;
632 	u8			active_width;
633 	u16			active_speed;
634 	u8                      phys_state;
635 	bool			grh_required;
636 };
637 
638 enum ib_device_modify_flags {
639 	IB_DEVICE_MODIFY_SYS_IMAGE_GUID	= 1 << 0,
640 	IB_DEVICE_MODIFY_NODE_DESC	= 1 << 1
641 };
642 
643 #define IB_DEVICE_NODE_DESC_MAX 64
644 
645 struct ib_device_modify {
646 	u64	sys_image_guid;
647 	char	node_desc[IB_DEVICE_NODE_DESC_MAX];
648 };
649 
650 enum ib_port_modify_flags {
651 	IB_PORT_SHUTDOWN		= 1,
652 	IB_PORT_INIT_TYPE		= (1<<2),
653 	IB_PORT_RESET_QKEY_CNTR		= (1<<3)
654 };
655 
656 struct ib_port_modify {
657 	u32	set_port_cap_mask;
658 	u32	clr_port_cap_mask;
659 	u8	init_type;
660 };
661 
662 enum ib_event_type {
663 	IB_EVENT_CQ_ERR,
664 	IB_EVENT_QP_FATAL,
665 	IB_EVENT_QP_REQ_ERR,
666 	IB_EVENT_QP_ACCESS_ERR,
667 	IB_EVENT_COMM_EST,
668 	IB_EVENT_SQ_DRAINED,
669 	IB_EVENT_PATH_MIG,
670 	IB_EVENT_PATH_MIG_ERR,
671 	IB_EVENT_DEVICE_FATAL,
672 	IB_EVENT_PORT_ACTIVE,
673 	IB_EVENT_PORT_ERR,
674 	IB_EVENT_LID_CHANGE,
675 	IB_EVENT_PKEY_CHANGE,
676 	IB_EVENT_SM_CHANGE,
677 	IB_EVENT_SRQ_ERR,
678 	IB_EVENT_SRQ_LIMIT_REACHED,
679 	IB_EVENT_QP_LAST_WQE_REACHED,
680 	IB_EVENT_CLIENT_REREGISTER,
681 	IB_EVENT_GID_CHANGE,
682 	IB_EVENT_WQ_FATAL,
683 };
684 
685 const char *__attribute_const__ ib_event_msg(enum ib_event_type event);
686 
687 struct ib_event {
688 	struct ib_device	*device;
689 	union {
690 		struct ib_cq	*cq;
691 		struct ib_qp	*qp;
692 		struct ib_srq	*srq;
693 		struct ib_wq	*wq;
694 		u8		port_num;
695 	} element;
696 	enum ib_event_type	event;
697 };
698 
699 struct ib_event_handler {
700 	struct ib_device *device;
701 	void            (*handler)(struct ib_event_handler *, struct ib_event *);
702 	struct list_head  list;
703 };
704 
705 #define INIT_IB_EVENT_HANDLER(_ptr, _device, _handler)		\
706 	do {							\
707 		(_ptr)->device  = _device;			\
708 		(_ptr)->handler = _handler;			\
709 		INIT_LIST_HEAD(&(_ptr)->list);			\
710 	} while (0)
711 
712 struct ib_global_route {
713 	const struct ib_gid_attr *sgid_attr;
714 	union ib_gid	dgid;
715 	u32		flow_label;
716 	u8		sgid_index;
717 	u8		hop_limit;
718 	u8		traffic_class;
719 };
720 
721 struct ib_grh {
722 	__be32		version_tclass_flow;
723 	__be16		paylen;
724 	u8		next_hdr;
725 	u8		hop_limit;
726 	union ib_gid	sgid;
727 	union ib_gid	dgid;
728 };
729 
730 union rdma_network_hdr {
731 	struct ib_grh ibgrh;
732 	struct {
733 		/* The IB spec states that if it's IPv4, the header
734 		 * is located in the last 20 bytes of the header.
735 		 */
736 		u8		reserved[20];
737 		struct ip	roce4grh;
738 	};
739 };
740 
741 enum {
742 	IB_MULTICAST_QPN = 0xffffff
743 };
744 
745 #define IB_LID_PERMISSIVE	cpu_to_be16(0xFFFF)
746 #define IB_MULTICAST_LID_BASE	cpu_to_be16(0xC000)
747 
748 enum ib_ah_flags {
749 	IB_AH_GRH	= 1
750 };
751 
752 enum ib_rate {
753 	IB_RATE_PORT_CURRENT = 0,
754 	IB_RATE_2_5_GBPS = 2,
755 	IB_RATE_5_GBPS   = 5,
756 	IB_RATE_10_GBPS  = 3,
757 	IB_RATE_20_GBPS  = 6,
758 	IB_RATE_30_GBPS  = 4,
759 	IB_RATE_40_GBPS  = 7,
760 	IB_RATE_60_GBPS  = 8,
761 	IB_RATE_80_GBPS  = 9,
762 	IB_RATE_120_GBPS = 10,
763 	IB_RATE_14_GBPS  = 11,
764 	IB_RATE_56_GBPS  = 12,
765 	IB_RATE_112_GBPS = 13,
766 	IB_RATE_168_GBPS = 14,
767 	IB_RATE_25_GBPS  = 15,
768 	IB_RATE_100_GBPS = 16,
769 	IB_RATE_200_GBPS = 17,
770 	IB_RATE_300_GBPS = 18,
771 	IB_RATE_28_GBPS  = 19,
772 	IB_RATE_50_GBPS  = 20,
773 	IB_RATE_400_GBPS = 21,
774 	IB_RATE_600_GBPS = 22,
775 };
776 
777 /**
778  * ib_rate_to_mult - Convert the IB rate enum to a multiple of the
779  * base rate of 2.5 Gbit/sec.  For example, IB_RATE_5_GBPS will be
780  * converted to 2, since 5 Gbit/sec is 2 * 2.5 Gbit/sec.
781  * @rate: rate to convert.
782  */
783 __attribute_const__ int ib_rate_to_mult(enum ib_rate rate);
784 
785 /**
786  * ib_rate_to_mbps - Convert the IB rate enum to Mbps.
787  * For example, IB_RATE_2_5_GBPS will be converted to 2500.
788  * @rate: rate to convert.
789  */
790 __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate);
791 
792 
793 /**
794  * enum ib_mr_type - memory region type
795  * @IB_MR_TYPE_MEM_REG:       memory region that is used for
796  *                            normal registration
797  * @IB_MR_TYPE_SG_GAPS:       memory region that is capable to
798  *                            register any arbitrary sg lists (without
799  *                            the normal mr constraints - see
800  *                            ib_map_mr_sg)
801  * @IB_MR_TYPE_DM:            memory region that is used for device
802  *                            memory registration
803  * @IB_MR_TYPE_USER:          memory region that is used for the user-space
804  *                            application
805  * @IB_MR_TYPE_DMA:           memory region that is used for DMA operations
806  *                            without address translations (VA=PA)
807  * @IB_MR_TYPE_INTEGRITY:     memory region that is used for
808  *                            data integrity operations
809  */
810 enum ib_mr_type {
811 	IB_MR_TYPE_MEM_REG,
812 	IB_MR_TYPE_SG_GAPS,
813 	IB_MR_TYPE_DM,
814 	IB_MR_TYPE_USER,
815 	IB_MR_TYPE_DMA,
816 	IB_MR_TYPE_INTEGRITY,
817 };
818 
819 enum ib_mr_status_check {
820 	IB_MR_CHECK_SIG_STATUS = 1,
821 };
822 
823 /**
824  * struct ib_mr_status - Memory region status container
825  *
826  * @fail_status: Bitmask of MR checks status. For each
827  *     failed check a corresponding status bit is set.
828  * @sig_err: Additional info for IB_MR_CEHCK_SIG_STATUS
829  *     failure.
830  */
831 struct ib_mr_status {
832 	u32		    fail_status;
833 	struct ib_sig_err   sig_err;
834 };
835 
836 /**
837  * mult_to_ib_rate - Convert a multiple of 2.5 Gbit/sec to an IB rate
838  * enum.
839  * @mult: multiple to convert.
840  */
841 __attribute_const__ enum ib_rate mult_to_ib_rate(int mult);
842 
843 enum rdma_ah_attr_type {
844 	RDMA_AH_ATTR_TYPE_UNDEFINED,
845 	RDMA_AH_ATTR_TYPE_IB,
846 	RDMA_AH_ATTR_TYPE_ROCE,
847 	RDMA_AH_ATTR_TYPE_OPA,
848 };
849 
850 struct ib_ah_attr {
851 	u16			dlid;
852 	u8			src_path_bits;
853 };
854 
855 struct roce_ah_attr {
856 	u8			dmac[ETH_ALEN];
857 };
858 
859 struct opa_ah_attr {
860 	u32			dlid;
861 	u8			src_path_bits;
862 	bool			make_grd;
863 };
864 
865 struct rdma_ah_attr {
866 	struct ib_global_route	grh;
867 	u8			sl;
868 	u8			static_rate;
869 	u8			port_num;
870 	u8			ah_flags;
871 	enum rdma_ah_attr_type type;
872 	union {
873 		struct ib_ah_attr ib;
874 		struct roce_ah_attr roce;
875 		struct opa_ah_attr opa;
876 	};
877 };
878 
879 enum ib_wc_status {
880 	IB_WC_SUCCESS,
881 	IB_WC_LOC_LEN_ERR,
882 	IB_WC_LOC_QP_OP_ERR,
883 	IB_WC_LOC_EEC_OP_ERR,
884 	IB_WC_LOC_PROT_ERR,
885 	IB_WC_WR_FLUSH_ERR,
886 	IB_WC_MW_BIND_ERR,
887 	IB_WC_BAD_RESP_ERR,
888 	IB_WC_LOC_ACCESS_ERR,
889 	IB_WC_REM_INV_REQ_ERR,
890 	IB_WC_REM_ACCESS_ERR,
891 	IB_WC_REM_OP_ERR,
892 	IB_WC_RETRY_EXC_ERR,
893 	IB_WC_RNR_RETRY_EXC_ERR,
894 	IB_WC_LOC_RDD_VIOL_ERR,
895 	IB_WC_REM_INV_RD_REQ_ERR,
896 	IB_WC_REM_ABORT_ERR,
897 	IB_WC_INV_EECN_ERR,
898 	IB_WC_INV_EEC_STATE_ERR,
899 	IB_WC_FATAL_ERR,
900 	IB_WC_RESP_TIMEOUT_ERR,
901 	IB_WC_GENERAL_ERR
902 };
903 
904 const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status);
905 
906 enum ib_wc_opcode {
907 	IB_WC_SEND,
908 	IB_WC_RDMA_WRITE,
909 	IB_WC_RDMA_READ,
910 	IB_WC_COMP_SWAP,
911 	IB_WC_FETCH_ADD,
912 	IB_WC_LSO,
913 	IB_WC_LOCAL_INV,
914 	IB_WC_REG_MR,
915 	IB_WC_MASKED_COMP_SWAP,
916 	IB_WC_MASKED_FETCH_ADD,
917 /*
918  * Set value of IB_WC_RECV so consumers can test if a completion is a
919  * receive by testing (opcode & IB_WC_RECV).
920  */
921 	IB_WC_RECV			= 1 << 7,
922 	IB_WC_RECV_RDMA_WITH_IMM,
923 	IB_WC_DUMMY = -1,	/* force enum signed */
924 };
925 
926 enum ib_wc_flags {
927 	IB_WC_GRH		= 1,
928 	IB_WC_WITH_IMM		= (1<<1),
929 	IB_WC_WITH_INVALIDATE	= (1<<2),
930 	IB_WC_IP_CSUM_OK	= (1<<3),
931 	IB_WC_WITH_SMAC		= (1<<4),
932 	IB_WC_WITH_VLAN		= (1<<5),
933 	IB_WC_WITH_NETWORK_HDR_TYPE	= (1<<6),
934 };
935 
936 struct ib_wc {
937 	union {
938 		u64		wr_id;
939 		struct ib_cqe	*wr_cqe;
940 	};
941 	enum ib_wc_status	status;
942 	enum ib_wc_opcode	opcode;
943 	u32			vendor_err;
944 	u32			byte_len;
945 	struct ib_qp	       *qp;
946 	union {
947 		__be32		imm_data;
948 		u32		invalidate_rkey;
949 	} ex;
950 	u32			src_qp;
951 	u32			slid;
952 	int			wc_flags;
953 	u16			pkey_index;
954 	u8			sl;
955 	u8			dlid_path_bits;
956 	u8			port_num;	/* valid only for DR SMPs on switches */
957 	u8			smac[ETH_ALEN];
958 	u16			vlan_id;
959 	u8			network_hdr_type;
960 };
961 
962 enum ib_cq_notify_flags {
963 	IB_CQ_SOLICITED			= 1 << 0,
964 	IB_CQ_NEXT_COMP			= 1 << 1,
965 	IB_CQ_SOLICITED_MASK		= IB_CQ_SOLICITED | IB_CQ_NEXT_COMP,
966 	IB_CQ_REPORT_MISSED_EVENTS	= 1 << 2,
967 };
968 
969 enum ib_srq_type {
970 	IB_SRQT_BASIC,
971 	IB_SRQT_XRC,
972 	IB_SRQT_TM,
973 };
974 
975 static inline bool ib_srq_has_cq(enum ib_srq_type srq_type)
976 {
977 	return srq_type == IB_SRQT_XRC ||
978 	       srq_type == IB_SRQT_TM;
979 }
980 
981 enum ib_srq_attr_mask {
982 	IB_SRQ_MAX_WR	= 1 << 0,
983 	IB_SRQ_LIMIT	= 1 << 1,
984 };
985 
986 struct ib_srq_attr {
987 	u32	max_wr;
988 	u32	max_sge;
989 	u32	srq_limit;
990 };
991 
992 struct ib_srq_init_attr {
993 	void		      (*event_handler)(struct ib_event *, void *);
994 	void		       *srq_context;
995 	struct ib_srq_attr	attr;
996 	enum ib_srq_type	srq_type;
997 
998 	struct {
999 		struct ib_cq   *cq;
1000 		union {
1001 			struct {
1002 				struct ib_xrcd *xrcd;
1003 			} xrc;
1004 
1005 			struct {
1006 				u32		max_num_tags;
1007 			} tag_matching;
1008 		};
1009 	} ext;
1010 };
1011 
1012 struct ib_qp_cap {
1013 	u32	max_send_wr;
1014 	u32	max_recv_wr;
1015 	u32	max_send_sge;
1016 	u32	max_recv_sge;
1017 	u32	max_inline_data;
1018 
1019 	/*
1020 	 * Maximum number of rdma_rw_ctx structures in flight at a time.
1021 	 * ib_create_qp() will calculate the right amount of neededed WRs
1022 	 * and MRs based on this.
1023 	 */
1024 	u32	max_rdma_ctxs;
1025 };
1026 
1027 enum ib_sig_type {
1028 	IB_SIGNAL_ALL_WR,
1029 	IB_SIGNAL_REQ_WR
1030 };
1031 
1032 enum ib_qp_type {
1033 	/*
1034 	 * IB_QPT_SMI and IB_QPT_GSI have to be the first two entries
1035 	 * here (and in that order) since the MAD layer uses them as
1036 	 * indices into a 2-entry table.
1037 	 */
1038 	IB_QPT_SMI,
1039 	IB_QPT_GSI,
1040 
1041 	IB_QPT_RC,
1042 	IB_QPT_UC,
1043 	IB_QPT_UD,
1044 	IB_QPT_RAW_IPV6,
1045 	IB_QPT_RAW_ETHERTYPE,
1046 	IB_QPT_RAW_PACKET = 8,
1047 	IB_QPT_XRC_INI = 9,
1048 	IB_QPT_XRC_TGT,
1049 	IB_QPT_MAX,
1050 	IB_QPT_DRIVER = 0xFF,
1051 	/* Reserve a range for qp types internal to the low level driver.
1052 	 * These qp types will not be visible at the IB core layer, so the
1053 	 * IB_QPT_MAX usages should not be affected in the core layer
1054 	 */
1055 	IB_QPT_RESERVED1 = 0x1000,
1056 	IB_QPT_RESERVED2,
1057 	IB_QPT_RESERVED3,
1058 	IB_QPT_RESERVED4,
1059 	IB_QPT_RESERVED5,
1060 	IB_QPT_RESERVED6,
1061 	IB_QPT_RESERVED7,
1062 	IB_QPT_RESERVED8,
1063 	IB_QPT_RESERVED9,
1064 	IB_QPT_RESERVED10,
1065 };
1066 
1067 enum ib_qp_create_flags {
1068 	IB_QP_CREATE_IPOIB_UD_LSO		= 1 << 0,
1069 	IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK	= 1 << 1,
1070 	IB_QP_CREATE_CROSS_CHANNEL              = 1 << 2,
1071 	IB_QP_CREATE_MANAGED_SEND               = 1 << 3,
1072 	IB_QP_CREATE_MANAGED_RECV               = 1 << 4,
1073 	IB_QP_CREATE_NETIF_QP			= 1 << 5,
1074 	IB_QP_CREATE_SIGNATURE_EN		= 1 << 6,
1075 	IB_QP_CREATE_USE_GFP_NOIO		= 1 << 7,
1076 	IB_QP_CREATE_SCATTER_FCS		= 1 << 8,
1077 	IB_QP_CREATE_CVLAN_STRIPPING		= 1 << 9,
1078 	IB_QP_CREATE_SOURCE_QPN			= 1 << 10,
1079 	IB_QP_CREATE_PCI_WRITE_END_PADDING	= 1 << 11,
1080 	/* reserve bits 26-31 for low level drivers' internal use */
1081 	IB_QP_CREATE_RESERVED_START		= 1 << 26,
1082 	IB_QP_CREATE_RESERVED_END		= 1 << 31,
1083 };
1084 
1085 /*
1086  * Note: users may not call ib_close_qp or ib_destroy_qp from the event_handler
1087  * callback to destroy the passed in QP.
1088  */
1089 
1090 struct ib_qp_init_attr {
1091 	/* Consumer's event_handler callback must not block */
1092 	void                  (*event_handler)(struct ib_event *, void *);
1093 
1094 	void		       *qp_context;
1095 	struct ib_cq	       *send_cq;
1096 	struct ib_cq	       *recv_cq;
1097 	struct ib_srq	       *srq;
1098 	struct ib_xrcd	       *xrcd;     /* XRC TGT QPs only */
1099 	struct ib_qp_cap	cap;
1100 	enum ib_sig_type	sq_sig_type;
1101 	enum ib_qp_type		qp_type;
1102 	enum ib_qp_create_flags	create_flags;
1103 
1104 	/*
1105 	 * Only needed for special QP types, or when using the RW API.
1106 	 */
1107 	u8			port_num;
1108 	struct ib_rwq_ind_table *rwq_ind_tbl;
1109 	u32			source_qpn;
1110 };
1111 
1112 struct ib_qp_open_attr {
1113 	void                  (*event_handler)(struct ib_event *, void *);
1114 	void		       *qp_context;
1115 	u32			qp_num;
1116 	enum ib_qp_type		qp_type;
1117 };
1118 
1119 enum ib_rnr_timeout {
1120 	IB_RNR_TIMER_655_36 =  0,
1121 	IB_RNR_TIMER_000_01 =  1,
1122 	IB_RNR_TIMER_000_02 =  2,
1123 	IB_RNR_TIMER_000_03 =  3,
1124 	IB_RNR_TIMER_000_04 =  4,
1125 	IB_RNR_TIMER_000_06 =  5,
1126 	IB_RNR_TIMER_000_08 =  6,
1127 	IB_RNR_TIMER_000_12 =  7,
1128 	IB_RNR_TIMER_000_16 =  8,
1129 	IB_RNR_TIMER_000_24 =  9,
1130 	IB_RNR_TIMER_000_32 = 10,
1131 	IB_RNR_TIMER_000_48 = 11,
1132 	IB_RNR_TIMER_000_64 = 12,
1133 	IB_RNR_TIMER_000_96 = 13,
1134 	IB_RNR_TIMER_001_28 = 14,
1135 	IB_RNR_TIMER_001_92 = 15,
1136 	IB_RNR_TIMER_002_56 = 16,
1137 	IB_RNR_TIMER_003_84 = 17,
1138 	IB_RNR_TIMER_005_12 = 18,
1139 	IB_RNR_TIMER_007_68 = 19,
1140 	IB_RNR_TIMER_010_24 = 20,
1141 	IB_RNR_TIMER_015_36 = 21,
1142 	IB_RNR_TIMER_020_48 = 22,
1143 	IB_RNR_TIMER_030_72 = 23,
1144 	IB_RNR_TIMER_040_96 = 24,
1145 	IB_RNR_TIMER_061_44 = 25,
1146 	IB_RNR_TIMER_081_92 = 26,
1147 	IB_RNR_TIMER_122_88 = 27,
1148 	IB_RNR_TIMER_163_84 = 28,
1149 	IB_RNR_TIMER_245_76 = 29,
1150 	IB_RNR_TIMER_327_68 = 30,
1151 	IB_RNR_TIMER_491_52 = 31
1152 };
1153 
1154 enum ib_qp_attr_mask {
1155 	IB_QP_STATE			= 1,
1156 	IB_QP_CUR_STATE			= (1<<1),
1157 	IB_QP_EN_SQD_ASYNC_NOTIFY	= (1<<2),
1158 	IB_QP_ACCESS_FLAGS		= (1<<3),
1159 	IB_QP_PKEY_INDEX		= (1<<4),
1160 	IB_QP_PORT			= (1<<5),
1161 	IB_QP_QKEY			= (1<<6),
1162 	IB_QP_AV			= (1<<7),
1163 	IB_QP_PATH_MTU			= (1<<8),
1164 	IB_QP_TIMEOUT			= (1<<9),
1165 	IB_QP_RETRY_CNT			= (1<<10),
1166 	IB_QP_RNR_RETRY			= (1<<11),
1167 	IB_QP_RQ_PSN			= (1<<12),
1168 	IB_QP_MAX_QP_RD_ATOMIC		= (1<<13),
1169 	IB_QP_ALT_PATH			= (1<<14),
1170 	IB_QP_MIN_RNR_TIMER		= (1<<15),
1171 	IB_QP_SQ_PSN			= (1<<16),
1172 	IB_QP_MAX_DEST_RD_ATOMIC	= (1<<17),
1173 	IB_QP_PATH_MIG_STATE		= (1<<18),
1174 	IB_QP_CAP			= (1<<19),
1175 	IB_QP_DEST_QPN			= (1<<20),
1176 	IB_QP_RESERVED1			= (1<<21),
1177 	IB_QP_RESERVED2			= (1<<22),
1178 	IB_QP_RESERVED3			= (1<<23),
1179 	IB_QP_RESERVED4			= (1<<24),
1180 	IB_QP_RATE_LIMIT		= (1<<25),
1181 };
1182 
1183 enum ib_qp_state {
1184 	IB_QPS_RESET,
1185 	IB_QPS_INIT,
1186 	IB_QPS_RTR,
1187 	IB_QPS_RTS,
1188 	IB_QPS_SQD,
1189 	IB_QPS_SQE,
1190 	IB_QPS_ERR,
1191 	IB_QPS_DUMMY = -1,	/* force enum signed */
1192 };
1193 
1194 enum ib_mig_state {
1195 	IB_MIG_MIGRATED,
1196 	IB_MIG_REARM,
1197 	IB_MIG_ARMED
1198 };
1199 
1200 enum ib_mw_type {
1201 	IB_MW_TYPE_1 = 1,
1202 	IB_MW_TYPE_2 = 2
1203 };
1204 
1205 struct ib_qp_attr {
1206 	enum ib_qp_state	qp_state;
1207 	enum ib_qp_state	cur_qp_state;
1208 	enum ib_mtu		path_mtu;
1209 	enum ib_mig_state	path_mig_state;
1210 	u32			qkey;
1211 	u32			rq_psn;
1212 	u32			sq_psn;
1213 	u32			dest_qp_num;
1214 	int			qp_access_flags;
1215 	struct ib_qp_cap	cap;
1216 	struct rdma_ah_attr	ah_attr;
1217 	struct rdma_ah_attr	alt_ah_attr;
1218 	u16			pkey_index;
1219 	u16			alt_pkey_index;
1220 	u8			en_sqd_async_notify;
1221 	u8			sq_draining;
1222 	u8			max_rd_atomic;
1223 	u8			max_dest_rd_atomic;
1224 	u8			min_rnr_timer;
1225 	u8			port_num;
1226 	u8			timeout;
1227 	u8			retry_cnt;
1228 	u8			rnr_retry;
1229 	u8			alt_port_num;
1230 	u8			alt_timeout;
1231 	u32			rate_limit;
1232 };
1233 
1234 enum ib_wr_opcode {
1235 	IB_WR_RDMA_WRITE,
1236 	IB_WR_RDMA_WRITE_WITH_IMM,
1237 	IB_WR_SEND,
1238 	IB_WR_SEND_WITH_IMM,
1239 	IB_WR_RDMA_READ,
1240 	IB_WR_ATOMIC_CMP_AND_SWP,
1241 	IB_WR_ATOMIC_FETCH_AND_ADD,
1242 	IB_WR_LSO,
1243 	IB_WR_SEND_WITH_INV,
1244 	IB_WR_RDMA_READ_WITH_INV,
1245 	IB_WR_LOCAL_INV,
1246 	IB_WR_REG_MR,
1247 	IB_WR_MASKED_ATOMIC_CMP_AND_SWP,
1248 	IB_WR_MASKED_ATOMIC_FETCH_AND_ADD,
1249 	IB_WR_REG_SIG_MR,
1250 	/* reserve values for low level drivers' internal use.
1251 	 * These values will not be used at all in the ib core layer.
1252 	 */
1253 	IB_WR_RESERVED1 = 0xf0,
1254 	IB_WR_RESERVED2,
1255 	IB_WR_RESERVED3,
1256 	IB_WR_RESERVED4,
1257 	IB_WR_RESERVED5,
1258 	IB_WR_RESERVED6,
1259 	IB_WR_RESERVED7,
1260 	IB_WR_RESERVED8,
1261 	IB_WR_RESERVED9,
1262 	IB_WR_RESERVED10,
1263 	IB_WR_DUMMY = -1,	/* force enum signed */
1264 };
1265 
1266 enum ib_send_flags {
1267 	IB_SEND_FENCE		= 1,
1268 	IB_SEND_SIGNALED	= (1<<1),
1269 	IB_SEND_SOLICITED	= (1<<2),
1270 	IB_SEND_INLINE		= (1<<3),
1271 	IB_SEND_IP_CSUM		= (1<<4),
1272 
1273 	/* reserve bits 26-31 for low level drivers' internal use */
1274 	IB_SEND_RESERVED_START	= (1 << 26),
1275 	IB_SEND_RESERVED_END	= (1 << 31),
1276 };
1277 
1278 struct ib_sge {
1279 	u64	addr;
1280 	u32	length;
1281 	u32	lkey;
1282 };
1283 
1284 struct ib_cqe {
1285 	void (*done)(struct ib_cq *cq, struct ib_wc *wc);
1286 };
1287 
1288 struct ib_send_wr {
1289 	struct ib_send_wr      *next;
1290 	union {
1291 		u64		wr_id;
1292 		struct ib_cqe	*wr_cqe;
1293 	};
1294 	struct ib_sge	       *sg_list;
1295 	int			num_sge;
1296 	enum ib_wr_opcode	opcode;
1297 	int			send_flags;
1298 	union {
1299 		__be32		imm_data;
1300 		u32		invalidate_rkey;
1301 	} ex;
1302 };
1303 
1304 struct ib_rdma_wr {
1305 	struct ib_send_wr	wr;
1306 	u64			remote_addr;
1307 	u32			rkey;
1308 };
1309 
1310 static inline const struct ib_rdma_wr *rdma_wr(const struct ib_send_wr *wr)
1311 {
1312 	return container_of(wr, struct ib_rdma_wr, wr);
1313 }
1314 
1315 struct ib_atomic_wr {
1316 	struct ib_send_wr	wr;
1317 	u64			remote_addr;
1318 	u64			compare_add;
1319 	u64			swap;
1320 	u64			compare_add_mask;
1321 	u64			swap_mask;
1322 	u32			rkey;
1323 };
1324 
1325 static inline const struct ib_atomic_wr *atomic_wr(const struct ib_send_wr *wr)
1326 {
1327 	return container_of(wr, struct ib_atomic_wr, wr);
1328 }
1329 
1330 struct ib_ud_wr {
1331 	struct ib_send_wr	wr;
1332 	struct ib_ah		*ah;
1333 	void			*header;
1334 	int			hlen;
1335 	int			mss;
1336 	u32			remote_qpn;
1337 	u32			remote_qkey;
1338 	u16			pkey_index; /* valid for GSI only */
1339 	u8			port_num;   /* valid for DR SMPs on switch only */
1340 };
1341 
1342 static inline const struct ib_ud_wr *ud_wr(const struct ib_send_wr *wr)
1343 {
1344 	return container_of(wr, struct ib_ud_wr, wr);
1345 }
1346 
1347 struct ib_reg_wr {
1348 	struct ib_send_wr	wr;
1349 	struct ib_mr		*mr;
1350 	u32			key;
1351 	int			access;
1352 };
1353 
1354 static inline const struct ib_reg_wr *reg_wr(const struct ib_send_wr *wr)
1355 {
1356 	return container_of(wr, struct ib_reg_wr, wr);
1357 }
1358 
1359 struct ib_sig_handover_wr {
1360 	struct ib_send_wr	wr;
1361 	struct ib_sig_attrs    *sig_attrs;
1362 	struct ib_mr	       *sig_mr;
1363 	int			access_flags;
1364 	struct ib_sge	       *prot;
1365 };
1366 
1367 static inline const struct ib_sig_handover_wr *sig_handover_wr(const struct ib_send_wr *wr)
1368 {
1369 	return container_of(wr, struct ib_sig_handover_wr, wr);
1370 }
1371 
1372 struct ib_recv_wr {
1373 	struct ib_recv_wr      *next;
1374 	union {
1375 		u64		wr_id;
1376 		struct ib_cqe	*wr_cqe;
1377 	};
1378 	struct ib_sge	       *sg_list;
1379 	int			num_sge;
1380 };
1381 
1382 enum ib_access_flags {
1383 	IB_ACCESS_LOCAL_WRITE = IB_UVERBS_ACCESS_LOCAL_WRITE,
1384 	IB_ACCESS_REMOTE_WRITE = IB_UVERBS_ACCESS_REMOTE_WRITE,
1385 	IB_ACCESS_REMOTE_READ = IB_UVERBS_ACCESS_REMOTE_READ,
1386 	IB_ACCESS_REMOTE_ATOMIC = IB_UVERBS_ACCESS_REMOTE_ATOMIC,
1387 	IB_ACCESS_MW_BIND = IB_UVERBS_ACCESS_MW_BIND,
1388 	IB_ZERO_BASED = IB_UVERBS_ACCESS_ZERO_BASED,
1389 	IB_ACCESS_ON_DEMAND = IB_UVERBS_ACCESS_ON_DEMAND,
1390 	IB_ACCESS_HUGETLB = IB_UVERBS_ACCESS_HUGETLB,
1391 	IB_ACCESS_RELAXED_ORDERING = IB_UVERBS_ACCESS_RELAXED_ORDERING,
1392 
1393 	IB_ACCESS_OPTIONAL = IB_UVERBS_ACCESS_OPTIONAL_RANGE,
1394 	IB_ACCESS_SUPPORTED =
1395 		((IB_ACCESS_HUGETLB << 1) - 1) | IB_ACCESS_OPTIONAL,
1396 };
1397 
1398 /*
1399  * XXX: these are apparently used for ->rereg_user_mr, no idea why they
1400  * are hidden here instead of a uapi header!
1401  */
1402 enum ib_mr_rereg_flags {
1403 	IB_MR_REREG_TRANS	= 1,
1404 	IB_MR_REREG_PD		= (1<<1),
1405 	IB_MR_REREG_ACCESS	= (1<<2),
1406 	IB_MR_REREG_SUPPORTED	= ((IB_MR_REREG_ACCESS << 1) - 1)
1407 };
1408 
1409 struct ib_fmr_attr {
1410 	int	max_pages;
1411 	int	max_maps;
1412 	u8	page_shift;
1413 };
1414 
1415 struct ib_umem;
1416 
1417 enum rdma_remove_reason {
1418 	/*
1419 	 * Userspace requested uobject deletion or initial try
1420 	 * to remove uobject via cleanup. Call could fail
1421 	 */
1422 	RDMA_REMOVE_DESTROY,
1423 	/* Context deletion. This call should delete the actual object itself */
1424 	RDMA_REMOVE_CLOSE,
1425 	/* Driver is being hot-unplugged. This call should delete the actual object itself */
1426 	RDMA_REMOVE_DRIVER_REMOVE,
1427 	/* uobj is being cleaned-up before being committed */
1428 	RDMA_REMOVE_ABORT,
1429 };
1430 
1431 struct ib_rdmacg_object {
1432 };
1433 
1434 struct ib_ucontext {
1435 	struct ib_device       *device;
1436 	struct ib_uverbs_file  *ufile;
1437 	/*
1438 	 * 'closing' can be read by the driver only during a destroy callback,
1439 	 * it is set when we are closing the file descriptor and indicates
1440 	 * that mm_sem may be locked.
1441 	 */
1442 	bool closing;
1443 
1444 	bool cleanup_retryable;
1445 
1446 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1447 	void (*invalidate_range)(struct ib_umem_odp *umem_odp,
1448 				 unsigned long start, unsigned long end);
1449 	struct mutex per_mm_list_lock;
1450 	struct list_head per_mm_list;
1451 #endif
1452 
1453 	struct ib_rdmacg_object	cg_obj;
1454 	/*
1455 	 * Implementation details of the RDMA core, don't use in drivers:
1456 	 */
1457 	struct xarray mmap_xa;
1458 };
1459 
1460 struct ib_uobject {
1461 	u64			user_handle;	/* handle given to us by userspace */
1462 	/* ufile & ucontext owning this object */
1463 	struct ib_uverbs_file  *ufile;
1464 	/* FIXME, save memory: ufile->context == context */
1465 	struct ib_ucontext     *context;	/* associated user context */
1466 	void		       *object;		/* containing object */
1467 	struct list_head	list;		/* link to context's list */
1468 	struct ib_rdmacg_object	cg_obj;		/* rdmacg object */
1469 	int			id;		/* index into kernel idr */
1470 	struct kref		ref;
1471 	atomic_t		usecnt;		/* protects exclusive access */
1472 	struct rcu_head		rcu;		/* kfree_rcu() overhead */
1473 
1474 	const struct uverbs_api_object *uapi_object;
1475 };
1476 
1477 struct ib_udata {
1478 	const u8 __user *inbuf;
1479 	u8 __user *outbuf;
1480 	size_t       inlen;
1481 	size_t       outlen;
1482 };
1483 
1484 struct ib_pd {
1485 	u32			local_dma_lkey;
1486 	u32			flags;
1487 	struct ib_device       *device;
1488 	struct ib_uobject      *uobject;
1489 	atomic_t          	usecnt; /* count all resources */
1490 
1491 	u32			unsafe_global_rkey;
1492 
1493 	/*
1494 	 * Implementation details of the RDMA core, don't use in drivers:
1495 	 */
1496 	struct ib_mr	       *__internal_mr;
1497 };
1498 
1499 struct ib_xrcd {
1500 	struct ib_device       *device;
1501 	atomic_t		usecnt; /* count all exposed resources */
1502 	struct inode	       *inode;
1503 
1504 	struct mutex		tgt_qp_mutex;
1505 	struct list_head	tgt_qp_list;
1506 };
1507 
1508 struct ib_ah {
1509 	struct ib_device	*device;
1510 	struct ib_pd		*pd;
1511 	struct ib_uobject	*uobject;
1512 	const struct ib_gid_attr *sgid_attr;
1513 	enum rdma_ah_attr_type	type;
1514 };
1515 
1516 typedef void (*ib_comp_handler)(struct ib_cq *cq, void *cq_context);
1517 
1518 enum ib_poll_context {
1519 	IB_POLL_DIRECT,		/* caller context, no hw completions */
1520 	IB_POLL_SOFTIRQ,	/* poll from softirq context */
1521 	IB_POLL_WORKQUEUE,	/* poll from workqueue */
1522 };
1523 
1524 struct ib_cq {
1525 	struct ib_device       *device;
1526 	struct ib_ucq_object   *uobject;
1527 	ib_comp_handler   	comp_handler;
1528 	void                  (*event_handler)(struct ib_event *, void *);
1529 	void                   *cq_context;
1530 	int               	cqe;
1531 	atomic_t          	usecnt; /* count number of work queues */
1532 	struct ib_wc		*wc;
1533 	enum ib_poll_context	poll_ctx;
1534 	struct work_struct	work;
1535 };
1536 
1537 struct ib_srq {
1538 	struct ib_device       *device;
1539 	struct ib_pd	       *pd;
1540 	struct ib_usrq_object  *uobject;
1541 	void		      (*event_handler)(struct ib_event *, void *);
1542 	void		       *srq_context;
1543 	enum ib_srq_type	srq_type;
1544 	atomic_t		usecnt;
1545 
1546 	struct {
1547 		struct ib_cq   *cq;
1548 		union {
1549 			struct {
1550 				struct ib_xrcd *xrcd;
1551 				u32		srq_num;
1552 			} xrc;
1553 		};
1554 	} ext;
1555 };
1556 
1557 enum ib_raw_packet_caps {
1558 	/* Strip cvlan from incoming packet and report it in the matching work
1559 	 * completion is supported.
1560 	 */
1561 	IB_RAW_PACKET_CAP_CVLAN_STRIPPING       = (1 << 0),
1562 	/* Scatter FCS field of an incoming packet to host memory is supported.
1563 	*/
1564 	IB_RAW_PACKET_CAP_SCATTER_FCS           = (1 << 1),
1565 	/* Checksum offloads are supported (for both send and receive). */
1566 	IB_RAW_PACKET_CAP_IP_CSUM               = (1 << 2),
1567 };
1568 
1569 enum ib_wq_type {
1570 	IB_WQT_RQ
1571 };
1572 
1573 enum ib_wq_state {
1574 	IB_WQS_RESET,
1575 	IB_WQS_RDY,
1576 	IB_WQS_ERR
1577 };
1578 
1579 struct ib_wq {
1580 	struct ib_device       *device;
1581 	struct ib_uwq_object   *uobject;
1582 	void		    *wq_context;
1583 	void		    (*event_handler)(struct ib_event *, void *);
1584 	struct ib_pd	       *pd;
1585 	struct ib_cq	       *cq;
1586 	u32		wq_num;
1587 	enum ib_wq_state       state;
1588 	enum ib_wq_type	wq_type;
1589 	atomic_t		usecnt;
1590 };
1591 
1592 enum ib_wq_flags {
1593 	IB_WQ_FLAGS_CVLAN_STRIPPING	= 1 << 0,
1594 	IB_WQ_FLAGS_SCATTER_FCS		= 1 << 1,
1595 	IB_WQ_FLAGS_DELAY_DROP		= 1 << 2,
1596 	IB_WQ_FLAGS_PCI_WRITE_END_PADDING = 1 << 3,
1597 };
1598 
1599 struct ib_wq_init_attr {
1600 	void		       *wq_context;
1601 	enum ib_wq_type	wq_type;
1602 	u32		max_wr;
1603 	u32		max_sge;
1604 	struct	ib_cq	       *cq;
1605 	void		    (*event_handler)(struct ib_event *, void *);
1606 	u32		create_flags; /* Use enum ib_wq_flags */
1607 };
1608 
1609 enum ib_wq_attr_mask {
1610 	IB_WQ_STATE		= 1 << 0,
1611 	IB_WQ_CUR_STATE		= 1 << 1,
1612 	IB_WQ_FLAGS		= 1 << 2,
1613 };
1614 
1615 struct ib_wq_attr {
1616 	enum	ib_wq_state	wq_state;
1617 	enum	ib_wq_state	curr_wq_state;
1618 	u32			flags; /* Use enum ib_wq_flags */
1619 	u32			flags_mask; /* Use enum ib_wq_flags */
1620 };
1621 
1622 struct ib_rwq_ind_table {
1623 	struct ib_device	*device;
1624 	struct ib_uobject      *uobject;
1625 	atomic_t		usecnt;
1626 	u32		ind_tbl_num;
1627 	u32		log_ind_tbl_size;
1628 	struct ib_wq	**ind_tbl;
1629 };
1630 
1631 struct ib_rwq_ind_table_init_attr {
1632 	u32		log_ind_tbl_size;
1633 	/* Each entry is a pointer to Receive Work Queue */
1634 	struct ib_wq	**ind_tbl;
1635 };
1636 
1637 /*
1638  * @max_write_sge: Maximum SGE elements per RDMA WRITE request.
1639  * @max_read_sge:  Maximum SGE elements per RDMA READ request.
1640  */
1641 struct ib_qp {
1642 	struct ib_device       *device;
1643 	struct ib_pd	       *pd;
1644 	struct ib_cq	       *send_cq;
1645 	struct ib_cq	       *recv_cq;
1646 	spinlock_t		mr_lock;
1647 	struct ib_srq	       *srq;
1648 	struct ib_xrcd	       *xrcd; /* XRC TGT QPs only */
1649 	struct list_head	xrcd_list;
1650 
1651 	/* count times opened, mcast attaches, flow attaches */
1652 	atomic_t		usecnt;
1653 	struct list_head	open_list;
1654 	struct ib_qp           *real_qp;
1655 	struct ib_uqp_object   *uobject;
1656 	void                  (*event_handler)(struct ib_event *, void *);
1657 	void		       *qp_context;
1658 	/* sgid_attrs associated with the AV's */
1659 	const struct ib_gid_attr *av_sgid_attr;
1660 	const struct ib_gid_attr *alt_path_sgid_attr;
1661 	u32			qp_num;
1662 	u32			max_write_sge;
1663 	u32			max_read_sge;
1664 	enum ib_qp_type		qp_type;
1665 	struct ib_rwq_ind_table *rwq_ind_tbl;
1666 	u8			port;
1667 };
1668 
1669 struct ib_dm {
1670 	struct ib_device  *device;
1671 	u32		   length;
1672 	u32		   flags;
1673 	struct ib_uobject *uobject;
1674 	atomic_t	   usecnt;
1675 };
1676 
1677 struct ib_mr {
1678 	struct ib_device  *device;
1679 	struct ib_pd	  *pd;
1680 	u32		   lkey;
1681 	u32		   rkey;
1682 	u64		   iova;
1683 	u64		   length;
1684 	unsigned int	   page_size;
1685 	enum ib_mr_type	   type;
1686 	bool		   need_inval;
1687 	union {
1688 		struct ib_uobject	*uobject;	/* user */
1689 		struct list_head	qp_entry;	/* FR */
1690 	};
1691 
1692 	struct ib_dm      *dm;
1693 	struct ib_sig_attrs *sig_attrs; /* only for IB_MR_TYPE_INTEGRITY MRs */
1694 };
1695 
1696 struct ib_mw {
1697 	struct ib_device	*device;
1698 	struct ib_pd		*pd;
1699 	struct ib_uobject	*uobject;
1700 	u32			rkey;
1701 	enum ib_mw_type         type;
1702 };
1703 
1704 struct ib_fmr {
1705 	struct ib_device	*device;
1706 	struct ib_pd		*pd;
1707 	struct list_head	list;
1708 	u32			lkey;
1709 	u32			rkey;
1710 };
1711 
1712 /* Supported steering options */
1713 enum ib_flow_attr_type {
1714 	/* steering according to rule specifications */
1715 	IB_FLOW_ATTR_NORMAL		= 0x0,
1716 	/* default unicast and multicast rule -
1717 	 * receive all Eth traffic which isn't steered to any QP
1718 	 */
1719 	IB_FLOW_ATTR_ALL_DEFAULT	= 0x1,
1720 	/* default multicast rule -
1721 	 * receive all Eth multicast traffic which isn't steered to any QP
1722 	 */
1723 	IB_FLOW_ATTR_MC_DEFAULT		= 0x2,
1724 	/* sniffer rule - receive all port traffic */
1725 	IB_FLOW_ATTR_SNIFFER		= 0x3
1726 };
1727 
1728 /* Supported steering header types */
1729 enum ib_flow_spec_type {
1730 	/* L2 headers*/
1731 	IB_FLOW_SPEC_ETH		= 0x20,
1732 	IB_FLOW_SPEC_IB			= 0x22,
1733 	/* L3 header*/
1734 	IB_FLOW_SPEC_IPV4		= 0x30,
1735 	IB_FLOW_SPEC_IPV6		= 0x31,
1736 	IB_FLOW_SPEC_ESP                = 0x34,
1737 	/* L4 headers*/
1738 	IB_FLOW_SPEC_TCP		= 0x40,
1739 	IB_FLOW_SPEC_UDP		= 0x41,
1740 	IB_FLOW_SPEC_VXLAN_TUNNEL	= 0x50,
1741 	IB_FLOW_SPEC_GRE		= 0x51,
1742 	IB_FLOW_SPEC_MPLS		= 0x60,
1743 	IB_FLOW_SPEC_INNER		= 0x100,
1744 	/* Actions */
1745 	IB_FLOW_SPEC_ACTION_TAG         = 0x1000,
1746 	IB_FLOW_SPEC_ACTION_DROP        = 0x1001,
1747 	IB_FLOW_SPEC_ACTION_HANDLE	= 0x1002,
1748 	IB_FLOW_SPEC_ACTION_COUNT       = 0x1003,
1749 };
1750 #define IB_FLOW_SPEC_LAYER_MASK	0xF0
1751 #define IB_FLOW_SPEC_SUPPORT_LAYERS 10
1752 
1753 /* Flow steering rule priority is set according to it's domain.
1754  * Lower domain value means higher priority.
1755  */
1756 enum ib_flow_domain {
1757 	IB_FLOW_DOMAIN_USER,
1758 	IB_FLOW_DOMAIN_ETHTOOL,
1759 	IB_FLOW_DOMAIN_RFS,
1760 	IB_FLOW_DOMAIN_NIC,
1761 	IB_FLOW_DOMAIN_NUM /* Must be last */
1762 };
1763 
1764 enum ib_flow_flags {
1765 	IB_FLOW_ATTR_FLAGS_DONT_TRAP = 1UL << 1, /* Continue match, no steal */
1766 	IB_FLOW_ATTR_FLAGS_RESERVED  = 1UL << 2  /* Must be last */
1767 };
1768 
1769 struct ib_flow_eth_filter {
1770 	u8	dst_mac[6];
1771 	u8	src_mac[6];
1772 	__be16	ether_type;
1773 	__be16	vlan_tag;
1774 	/* Must be last */
1775 	u8	real_sz[0];
1776 };
1777 
1778 struct ib_flow_spec_eth {
1779 	enum ib_flow_spec_type	  type;
1780 	u16			  size;
1781 	struct ib_flow_eth_filter val;
1782 	struct ib_flow_eth_filter mask;
1783 };
1784 
1785 struct ib_flow_ib_filter {
1786 	__be16 dlid;
1787 	__u8   sl;
1788 	/* Must be last */
1789 	u8	real_sz[0];
1790 };
1791 
1792 struct ib_flow_spec_ib {
1793 	enum ib_flow_spec_type	 type;
1794 	u16			 size;
1795 	struct ib_flow_ib_filter val;
1796 	struct ib_flow_ib_filter mask;
1797 };
1798 
1799 /* IPv4 header flags */
1800 enum ib_ipv4_flags {
1801 	IB_IPV4_DONT_FRAG = 0x2, /* Don't enable packet fragmentation */
1802 	IB_IPV4_MORE_FRAG = 0X4  /* For All fragmented packets except the
1803 				    last have this flag set */
1804 };
1805 
1806 struct ib_flow_ipv4_filter {
1807 	__be32	src_ip;
1808 	__be32	dst_ip;
1809 	u8	proto;
1810 	u8	tos;
1811 	u8	ttl;
1812 	u8	flags;
1813 	/* Must be last */
1814 	u8	real_sz[0];
1815 };
1816 
1817 struct ib_flow_spec_ipv4 {
1818 	enum ib_flow_spec_type	   type;
1819 	u16			   size;
1820 	struct ib_flow_ipv4_filter val;
1821 	struct ib_flow_ipv4_filter mask;
1822 };
1823 
1824 struct ib_flow_ipv6_filter {
1825 	u8	src_ip[16];
1826 	u8	dst_ip[16];
1827 	__be32	flow_label;
1828 	u8	next_hdr;
1829 	u8	traffic_class;
1830 	u8	hop_limit;
1831 	/* Must be last */
1832 	u8	real_sz[0];
1833 };
1834 
1835 struct ib_flow_spec_ipv6 {
1836 	enum ib_flow_spec_type	   type;
1837 	u16			   size;
1838 	struct ib_flow_ipv6_filter val;
1839 	struct ib_flow_ipv6_filter mask;
1840 };
1841 
1842 struct ib_flow_tcp_udp_filter {
1843 	__be16	dst_port;
1844 	__be16	src_port;
1845 	/* Must be last */
1846 	u8	real_sz[0];
1847 };
1848 
1849 struct ib_flow_spec_tcp_udp {
1850 	enum ib_flow_spec_type	      type;
1851 	u16			      size;
1852 	struct ib_flow_tcp_udp_filter val;
1853 	struct ib_flow_tcp_udp_filter mask;
1854 };
1855 
1856 struct ib_flow_tunnel_filter {
1857 	__be32	tunnel_id;
1858 	u8	real_sz[0];
1859 };
1860 
1861 /* ib_flow_spec_tunnel describes the Vxlan tunnel
1862  * the tunnel_id from val has the vni value
1863  */
1864 struct ib_flow_spec_tunnel {
1865 	u32			      type;
1866 	u16			      size;
1867 	struct ib_flow_tunnel_filter  val;
1868 	struct ib_flow_tunnel_filter  mask;
1869 };
1870 
1871 struct ib_flow_esp_filter {
1872 	__be32	spi;
1873 	__be32  seq;
1874 	/* Must be last */
1875 	u8	real_sz[0];
1876 };
1877 
1878 struct ib_flow_spec_esp {
1879 	u32                           type;
1880 	u16			      size;
1881 	struct ib_flow_esp_filter     val;
1882 	struct ib_flow_esp_filter     mask;
1883 };
1884 
1885 struct ib_flow_gre_filter {
1886 	__be16 c_ks_res0_ver;
1887 	__be16 protocol;
1888 	__be32 key;
1889 	/* Must be last */
1890 	u8	real_sz[0];
1891 };
1892 
1893 struct ib_flow_spec_gre {
1894 	u32                           type;
1895 	u16			      size;
1896 	struct ib_flow_gre_filter     val;
1897 	struct ib_flow_gre_filter     mask;
1898 };
1899 
1900 struct ib_flow_mpls_filter {
1901 	__be32 tag;
1902 	/* Must be last */
1903 	u8	real_sz[0];
1904 };
1905 
1906 struct ib_flow_spec_mpls {
1907 	u32                           type;
1908 	u16			      size;
1909 	struct ib_flow_mpls_filter     val;
1910 	struct ib_flow_mpls_filter     mask;
1911 };
1912 
1913 struct ib_flow_spec_action_tag {
1914 	enum ib_flow_spec_type	      type;
1915 	u16			      size;
1916 	u32                           tag_id;
1917 };
1918 
1919 struct ib_flow_spec_action_drop {
1920 	enum ib_flow_spec_type	      type;
1921 	u16			      size;
1922 };
1923 
1924 struct ib_flow_spec_action_handle {
1925 	enum ib_flow_spec_type	      type;
1926 	u16			      size;
1927 	struct ib_flow_action	     *act;
1928 };
1929 
1930 enum ib_counters_description {
1931 	IB_COUNTER_PACKETS,
1932 	IB_COUNTER_BYTES,
1933 };
1934 
1935 struct ib_flow_spec_action_count {
1936 	enum ib_flow_spec_type type;
1937 	u16 size;
1938 	struct ib_counters *counters;
1939 };
1940 
1941 union ib_flow_spec {
1942 	struct {
1943 		u32			type;
1944 		u16			size;
1945 	};
1946 	struct ib_flow_spec_eth		eth;
1947 	struct ib_flow_spec_ib		ib;
1948 	struct ib_flow_spec_ipv4        ipv4;
1949 	struct ib_flow_spec_tcp_udp	tcp_udp;
1950 	struct ib_flow_spec_ipv6        ipv6;
1951 	struct ib_flow_spec_tunnel      tunnel;
1952 	struct ib_flow_spec_esp		esp;
1953 	struct ib_flow_spec_gre		gre;
1954 	struct ib_flow_spec_mpls	mpls;
1955 	struct ib_flow_spec_action_tag  flow_tag;
1956 	struct ib_flow_spec_action_drop drop;
1957 	struct ib_flow_spec_action_handle action;
1958 	struct ib_flow_spec_action_count flow_count;
1959 };
1960 
1961 struct ib_flow_attr {
1962 	enum ib_flow_attr_type type;
1963 	u16	     size;
1964 	u16	     priority;
1965 	u32	     flags;
1966 	u8	     num_of_specs;
1967 	u8	     port;
1968 	union ib_flow_spec flows[0];
1969 };
1970 
1971 struct ib_flow {
1972 	struct ib_qp		*qp;
1973 	struct ib_device	*device;
1974 	struct ib_uobject	*uobject;
1975 };
1976 
1977 enum ib_flow_action_type {
1978 	IB_FLOW_ACTION_UNSPECIFIED,
1979 	IB_FLOW_ACTION_ESP = 1,
1980 };
1981 
1982 struct ib_flow_action_attrs_esp_keymats {
1983 	enum ib_uverbs_flow_action_esp_keymat			protocol;
1984 	union {
1985 		struct ib_uverbs_flow_action_esp_keymat_aes_gcm aes_gcm;
1986 	} keymat;
1987 };
1988 
1989 struct ib_flow_action_attrs_esp_replays {
1990 	enum ib_uverbs_flow_action_esp_replay			protocol;
1991 	union {
1992 		struct ib_uverbs_flow_action_esp_replay_bmp	bmp;
1993 	} replay;
1994 };
1995 
1996 enum ib_flow_action_attrs_esp_flags {
1997 	/* All user-space flags at the top: Use enum ib_uverbs_flow_action_esp_flags
1998 	 * This is done in order to share the same flags between user-space and
1999 	 * kernel and spare an unnecessary translation.
2000 	 */
2001 
2002 	/* Kernel flags */
2003 	IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED	= 1ULL << 32,
2004 	IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS	= 1ULL << 33,
2005 };
2006 
2007 struct ib_flow_spec_list {
2008 	struct ib_flow_spec_list	*next;
2009 	union ib_flow_spec		spec;
2010 };
2011 
2012 struct ib_flow_action_attrs_esp {
2013 	struct ib_flow_action_attrs_esp_keymats		*keymat;
2014 	struct ib_flow_action_attrs_esp_replays		*replay;
2015 	struct ib_flow_spec_list			*encap;
2016 	/* Used only if IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED is enabled.
2017 	 * Value of 0 is a valid value.
2018 	 */
2019 	u32						esn;
2020 	u32						spi;
2021 	u32						seq;
2022 	u32						tfc_pad;
2023 	/* Use enum ib_flow_action_attrs_esp_flags */
2024 	u64						flags;
2025 	u64						hard_limit_pkts;
2026 };
2027 
2028 struct ib_flow_action {
2029 	struct ib_device		*device;
2030 	struct ib_uobject		*uobject;
2031 	enum ib_flow_action_type	type;
2032 	atomic_t			usecnt;
2033 };
2034 
2035 
2036 struct ib_mad_hdr;
2037 struct ib_grh;
2038 
2039 enum ib_process_mad_flags {
2040 	IB_MAD_IGNORE_MKEY	= 1,
2041 	IB_MAD_IGNORE_BKEY	= 2,
2042 	IB_MAD_IGNORE_ALL	= IB_MAD_IGNORE_MKEY | IB_MAD_IGNORE_BKEY
2043 };
2044 
2045 enum ib_mad_result {
2046 	IB_MAD_RESULT_FAILURE  = 0,      /* (!SUCCESS is the important flag) */
2047 	IB_MAD_RESULT_SUCCESS  = 1 << 0, /* MAD was successfully processed   */
2048 	IB_MAD_RESULT_REPLY    = 1 << 1, /* Reply packet needs to be sent    */
2049 	IB_MAD_RESULT_CONSUMED = 1 << 2  /* Packet consumed: stop processing */
2050 };
2051 
2052 #define IB_DEVICE_NAME_MAX 64
2053 
2054 struct ib_port_cache {
2055 	struct ib_pkey_cache  *pkey;
2056 	struct ib_gid_table   *gid;
2057 	u8                     lmc;
2058 	enum ib_port_state     port_state;
2059 };
2060 
2061 struct ib_cache {
2062 	rwlock_t                lock;
2063 	struct ib_event_handler event_handler;
2064 	struct ib_port_cache   *ports;
2065 };
2066 
2067 struct iw_cm_verbs;
2068 
2069 struct ib_port_immutable {
2070 	int                           pkey_tbl_len;
2071 	int                           gid_tbl_len;
2072 	u32                           core_cap_flags;
2073 	u32                           max_mad_size;
2074 };
2075 
2076 struct ib_counters {
2077 	struct ib_device	*device;
2078 	struct ib_uobject	*uobject;
2079 	/* num of objects attached */
2080 	atomic_t	usecnt;
2081 };
2082 
2083 struct ib_counters_read_attr {
2084 	u64	*counters_buff;
2085 	u32	ncounters;
2086 	u32	flags; /* use enum ib_read_counters_flags */
2087 };
2088 
2089 #define INIT_RDMA_OBJ_SIZE(ib_struct, drv_struct, member)                      \
2090 	.size_##ib_struct =                                                    \
2091 		(sizeof(struct drv_struct) +                                   \
2092 		 BUILD_BUG_ON_ZERO(offsetof(struct drv_struct, member)) +      \
2093 		 BUILD_BUG_ON_ZERO(                                            \
2094 			 !__same_type(((struct drv_struct *)NULL)->member,     \
2095 				      struct ib_struct)))
2096 
2097 #define rdma_zalloc_drv_obj_gfp(ib_dev, ib_type, gfp)                         \
2098 	((struct ib_type *)kzalloc(ib_dev->ops.size_##ib_type, gfp))
2099 
2100 #define rdma_zalloc_drv_obj(ib_dev, ib_type)                                   \
2101 	rdma_zalloc_drv_obj_gfp(ib_dev, ib_type, GFP_KERNEL)
2102 
2103 #define DECLARE_RDMA_OBJ_SIZE(ib_struct) size_t size_##ib_struct
2104 
2105 struct rdma_user_mmap_entry {
2106 	struct kref ref;
2107 	struct ib_ucontext *ucontext;
2108 	unsigned long start_pgoff;
2109 	size_t npages;
2110 	bool driver_removed;
2111 };
2112 
2113 /* Return the offset (in bytes) the user should pass to libc's mmap() */
2114 static inline u64
2115 rdma_user_mmap_get_offset(const struct rdma_user_mmap_entry *entry)
2116 {
2117 	return (u64)entry->start_pgoff << PAGE_SHIFT;
2118 }
2119 
2120 struct ib_device_ops {
2121 	enum rdma_driver_id driver_id;
2122 	DECLARE_RDMA_OBJ_SIZE(ib_ah);
2123 	DECLARE_RDMA_OBJ_SIZE(ib_cq);
2124 	DECLARE_RDMA_OBJ_SIZE(ib_pd);
2125 	DECLARE_RDMA_OBJ_SIZE(ib_srq);
2126 	DECLARE_RDMA_OBJ_SIZE(ib_ucontext);
2127 };
2128 
2129 #define	INIT_IB_DEVICE_OPS(pop, driver, DRIVER) do {			\
2130 	(pop)[0] .driver_id = RDMA_DRIVER_##DRIVER;			\
2131 	(pop)[0] INIT_RDMA_OBJ_SIZE(ib_ah, driver##_ib_ah, ibah);	\
2132 	(pop)[0] INIT_RDMA_OBJ_SIZE(ib_cq, driver##_ib_cq, ibcq);	\
2133 	(pop)[0] INIT_RDMA_OBJ_SIZE(ib_pd, driver##_ib_pd, ibpd);	\
2134 	(pop)[0] INIT_RDMA_OBJ_SIZE(ib_srq, driver##_ib_srq, ibsrq);	\
2135 	(pop)[0] INIT_RDMA_OBJ_SIZE(ib_ucontext, driver##_ib_ucontext, ibucontext); \
2136 } while (0)
2137 
2138 struct ib_device {
2139 	struct device                *dma_device;
2140 	struct ib_device_ops	     ops;
2141 
2142 	char                          name[IB_DEVICE_NAME_MAX];
2143 
2144 	struct list_head              event_handler_list;
2145 	spinlock_t                    event_handler_lock;
2146 
2147 	rwlock_t			client_data_lock;
2148 	struct list_head              core_list;
2149 	/* Access to the client_data_list is protected by the client_data_lock
2150 	 * rwlock and the lists_rwsem read-write semaphore
2151 	 */
2152 	struct list_head              client_data_list;
2153 
2154 	struct ib_cache               cache;
2155 	/**
2156 	 * port_immutable is indexed by port number
2157 	 */
2158 	struct ib_port_immutable     *port_immutable;
2159 
2160 	int			      num_comp_vectors;
2161 
2162 	struct iw_cm_verbs	     *iwcm;
2163 
2164 	/**
2165 	 * alloc_hw_stats - Allocate a struct rdma_hw_stats and fill in the
2166 	 *   driver initialized data.  The struct is kfree()'ed by the sysfs
2167 	 *   core when the device is removed.  A lifespan of -1 in the return
2168 	 *   struct tells the core to set a default lifespan.
2169 	 */
2170 	struct rdma_hw_stats      *(*alloc_hw_stats)(struct ib_device *device,
2171 						     u8 port_num);
2172 	/**
2173 	 * get_hw_stats - Fill in the counter value(s) in the stats struct.
2174 	 * @index - The index in the value array we wish to have updated, or
2175 	 *   num_counters if we want all stats updated
2176 	 * Return codes -
2177 	 *   < 0 - Error, no counters updated
2178 	 *   index - Updated the single counter pointed to by index
2179 	 *   num_counters - Updated all counters (will reset the timestamp
2180 	 *     and prevent further calls for lifespan milliseconds)
2181 	 * Drivers are allowed to update all counters in leiu of just the
2182 	 *   one given in index at their option
2183 	 */
2184 	int		           (*get_hw_stats)(struct ib_device *device,
2185 						   struct rdma_hw_stats *stats,
2186 						   u8 port, int index);
2187 	int		           (*query_device)(struct ib_device *device,
2188 						   struct ib_device_attr *device_attr,
2189 						   struct ib_udata *udata);
2190 	int		           (*query_port)(struct ib_device *device,
2191 						 u8 port_num,
2192 						 struct ib_port_attr *port_attr);
2193 	enum rdma_link_layer	   (*get_link_layer)(struct ib_device *device,
2194 						     u8 port_num);
2195 	/* When calling get_netdev, the HW vendor's driver should return the
2196 	 * net device of device @device at port @port_num or NULL if such
2197 	 * a net device doesn't exist. The vendor driver should call dev_hold
2198 	 * on this net device. The HW vendor's device driver must guarantee
2199 	 * that this function returns NULL before the net device reaches
2200 	 * NETDEV_UNREGISTER_FINAL state.
2201 	 */
2202 	if_t (*get_netdev)(struct ib_device *device,
2203 						 u8 port_num);
2204 	/* query_gid should be return GID value for @device, when @port_num
2205 	 * link layer is either IB or iWarp. It is no-op if @port_num port
2206 	 * is RoCE link layer.
2207 	 */
2208 	int		           (*query_gid)(struct ib_device *device,
2209 						u8 port_num, int index,
2210 						union ib_gid *gid);
2211 	/* When calling add_gid, the HW vendor's driver should add the gid
2212 	 * of device of port at gid index available at @attr. Meta-info of
2213 	 * that gid (for example, the network device related to this gid) is
2214 	 * available at @attr. @context allows the HW vendor driver to store
2215 	 * extra information together with a GID entry. The HW vendor driver may
2216 	 * allocate memory to contain this information and store it in @context
2217 	 * when a new GID entry is written to. Params are consistent until the
2218 	 * next call of add_gid or delete_gid. The function should return 0 on
2219 	 * success or error otherwise. The function could be called
2220 	 * concurrently for different ports. This function is only called when
2221 	 * roce_gid_table is used.
2222 	 */
2223 	int		           (*add_gid)(const struct ib_gid_attr *attr,
2224 					      void **context);
2225 	/* When calling del_gid, the HW vendor's driver should delete the
2226 	 * gid of device @device at gid index gid_index of port port_num
2227 	 * available in @attr.
2228 	 * Upon the deletion of a GID entry, the HW vendor must free any
2229 	 * allocated memory. The caller will clear @context afterwards.
2230 	 * This function is only called when roce_gid_table is used.
2231 	 */
2232 	int		           (*del_gid)(const struct ib_gid_attr *attr,
2233 					      void **context);
2234 	int		           (*query_pkey)(struct ib_device *device,
2235 						 u8 port_num, u16 index, u16 *pkey);
2236 	int		           (*modify_device)(struct ib_device *device,
2237 						    int device_modify_mask,
2238 						    struct ib_device_modify *device_modify);
2239 	int		           (*modify_port)(struct ib_device *device,
2240 						  u8 port_num, int port_modify_mask,
2241 						  struct ib_port_modify *port_modify);
2242 	int                        (*alloc_ucontext)(struct ib_ucontext *uctx,
2243 						     struct ib_udata *udata);
2244 	void                       (*dealloc_ucontext)(struct ib_ucontext *context);
2245 	int                        (*mmap)(struct ib_ucontext *context,
2246 					   struct vm_area_struct *vma);
2247 	int                        (*alloc_pd)(struct ib_pd *pd,
2248 					       struct ib_udata *udata);
2249 	void                       (*dealloc_pd)(struct ib_pd *pd, struct ib_udata *udata);
2250 	int 			   (*create_ah)(struct ib_ah *ah, struct rdma_ah_attr *ah_attr,
2251 						u32 flags, struct ib_udata *udata);
2252 	int                        (*modify_ah)(struct ib_ah *ah,
2253 						struct rdma_ah_attr *ah_attr);
2254 	int                        (*query_ah)(struct ib_ah *ah,
2255 					       struct rdma_ah_attr *ah_attr);
2256 	void                       (*destroy_ah)(struct ib_ah *ah, u32 flags);
2257 	int 			   (*create_srq)(struct ib_srq *srq,
2258 						 struct ib_srq_init_attr *srq_init_attr,
2259 						 struct ib_udata *udata);
2260 	int                        (*modify_srq)(struct ib_srq *srq,
2261 						 struct ib_srq_attr *srq_attr,
2262 						 enum ib_srq_attr_mask srq_attr_mask,
2263 						 struct ib_udata *udata);
2264 	int                        (*query_srq)(struct ib_srq *srq,
2265 						struct ib_srq_attr *srq_attr);
2266 	void                       (*destroy_srq)(struct ib_srq *srq, struct ib_udata *udata);
2267 	int                        (*post_srq_recv)(struct ib_srq *srq,
2268 						    const struct ib_recv_wr *recv_wr,
2269 						    const struct ib_recv_wr **bad_recv_wr);
2270 	struct ib_qp *             (*create_qp)(struct ib_pd *pd,
2271 						struct ib_qp_init_attr *qp_init_attr,
2272 						struct ib_udata *udata);
2273 	int                        (*modify_qp)(struct ib_qp *qp,
2274 						struct ib_qp_attr *qp_attr,
2275 						int qp_attr_mask,
2276 						struct ib_udata *udata);
2277 	int                        (*query_qp)(struct ib_qp *qp,
2278 					       struct ib_qp_attr *qp_attr,
2279 					       int qp_attr_mask,
2280 					       struct ib_qp_init_attr *qp_init_attr);
2281 	int                        (*destroy_qp)(struct ib_qp *qp, struct ib_udata *udata);
2282 	int                        (*post_send)(struct ib_qp *qp,
2283 						const struct ib_send_wr *send_wr,
2284 						const struct ib_send_wr **bad_send_wr);
2285 	int                        (*post_recv)(struct ib_qp *qp,
2286 						const struct ib_recv_wr *recv_wr,
2287 						const struct ib_recv_wr **bad_recv_wr);
2288 	int                        (*create_cq)(struct ib_cq *,
2289 						const struct ib_cq_init_attr *attr,
2290 						struct ib_udata *udata);
2291 	int                        (*modify_cq)(struct ib_cq *cq, u16 cq_count,
2292 						u16 cq_period);
2293 	void                       (*destroy_cq)(struct ib_cq *cq, struct ib_udata *udata);
2294 	int                        (*resize_cq)(struct ib_cq *cq, int cqe,
2295 						struct ib_udata *udata);
2296 	int                        (*poll_cq)(struct ib_cq *cq, int num_entries,
2297 					      struct ib_wc *wc);
2298 	int                        (*peek_cq)(struct ib_cq *cq, int wc_cnt);
2299 	int                        (*req_notify_cq)(struct ib_cq *cq,
2300 						    enum ib_cq_notify_flags flags);
2301 	int                        (*req_ncomp_notif)(struct ib_cq *cq,
2302 						      int wc_cnt);
2303 	struct ib_mr *             (*get_dma_mr)(struct ib_pd *pd,
2304 						 int mr_access_flags);
2305 	struct ib_mr *             (*reg_user_mr)(struct ib_pd *pd,
2306 						  u64 start, u64 length,
2307 						  u64 virt_addr,
2308 						  int mr_access_flags,
2309 						  struct ib_udata *udata);
2310 	int			   (*rereg_user_mr)(struct ib_mr *mr,
2311 						    int flags,
2312 						    u64 start, u64 length,
2313 						    u64 virt_addr,
2314 						    int mr_access_flags,
2315 						    struct ib_pd *pd,
2316 						    struct ib_udata *udata);
2317 	int                        (*dereg_mr)(struct ib_mr *mr, struct ib_udata *udata);
2318 	struct ib_mr *		   (*alloc_mr)(struct ib_pd *pd, enum ib_mr_type mr_type,
2319 					       u32 max_num_sg, struct ib_udata *udata);
2320 	int			   (*advise_mr)(struct ib_pd *pd,
2321 						enum ib_uverbs_advise_mr_advice advice, u32 flags,
2322 						const struct ib_sge *sg_list, u32 num_sge,
2323 						struct uverbs_attr_bundle *attrs);
2324 	int                        (*map_mr_sg)(struct ib_mr *mr,
2325 						struct scatterlist *sg,
2326 						int sg_nents,
2327 						unsigned int *sg_offset);
2328 	struct ib_mw *             (*alloc_mw)(struct ib_pd *pd,
2329 					       enum ib_mw_type type,
2330 					       struct ib_udata *udata);
2331 	int                        (*dealloc_mw)(struct ib_mw *mw);
2332 	struct ib_fmr *	           (*alloc_fmr)(struct ib_pd *pd,
2333 						int mr_access_flags,
2334 						struct ib_fmr_attr *fmr_attr);
2335 	int		           (*map_phys_fmr)(struct ib_fmr *fmr,
2336 						   u64 *page_list, int list_len,
2337 						   u64 iova);
2338 	int		           (*unmap_fmr)(struct list_head *fmr_list);
2339 	int		           (*dealloc_fmr)(struct ib_fmr *fmr);
2340 	int                        (*attach_mcast)(struct ib_qp *qp,
2341 						   union ib_gid *gid,
2342 						   u16 lid);
2343 	int                        (*detach_mcast)(struct ib_qp *qp,
2344 						   union ib_gid *gid,
2345 						   u16 lid);
2346 	int                        (*process_mad)(struct ib_device *device,
2347 						  int process_mad_flags,
2348 						  u8 port_num,
2349 						  const struct ib_wc *in_wc,
2350 						  const struct ib_grh *in_grh,
2351 						  const struct ib_mad_hdr *in_mad,
2352 						  size_t in_mad_size,
2353 						  struct ib_mad_hdr *out_mad,
2354 						  size_t *out_mad_size,
2355 						  u16 *out_mad_pkey_index);
2356 	struct ib_xrcd *	   (*alloc_xrcd)(struct ib_device *device,
2357 						 struct ib_udata *udata);
2358 	int			   (*dealloc_xrcd)(struct ib_xrcd *xrcd, struct ib_udata *udata);
2359 	struct ib_flow *	   (*create_flow)(struct ib_qp *qp,
2360 						  struct ib_flow_attr
2361 						  *flow_attr,
2362 						  int domain, struct ib_udata *udata);
2363 	int			   (*destroy_flow)(struct ib_flow *flow_id);
2364 	struct ib_flow_action *(*create_flow_action_esp)(
2365 		struct ib_device *device,
2366 		const struct ib_flow_action_attrs_esp *attr,
2367 		struct uverbs_attr_bundle *attrs);
2368 	int (*destroy_flow_action)(struct ib_flow_action *action);
2369 	int (*modify_flow_action_esp)(
2370 		struct ib_flow_action *action,
2371 		const struct ib_flow_action_attrs_esp *attr,
2372 		struct uverbs_attr_bundle *attrs);
2373 	int			   (*check_mr_status)(struct ib_mr *mr, u32 check_mask,
2374 						      struct ib_mr_status *mr_status);
2375 	/**
2376 	 * This will be called once refcount of an entry in mmap_xa reaches
2377 	 * zero. The type of the memory that was mapped may differ between
2378 	 * entries and is opaque to the rdma_user_mmap interface.
2379 	 * Therefore needs to be implemented by the driver in mmap_free.
2380 	 */
2381 	void			   (*mmap_free)(struct rdma_user_mmap_entry *entry);
2382 	void			   (*disassociate_ucontext)(struct ib_ucontext *ibcontext);
2383 	void			   (*drain_rq)(struct ib_qp *qp);
2384 	void			   (*drain_sq)(struct ib_qp *qp);
2385 	int			   (*set_vf_link_state)(struct ib_device *device, int vf, u8 port,
2386 							int state);
2387 	int			   (*get_vf_config)(struct ib_device *device, int vf, u8 port,
2388 						   struct ifla_vf_info *ivf);
2389 	int			   (*get_vf_stats)(struct ib_device *device, int vf, u8 port,
2390 						   struct ifla_vf_stats *stats);
2391 	int			   (*set_vf_guid)(struct ib_device *device, int vf, u8 port, u64 guid,
2392 						  int type);
2393 	struct ib_wq *		   (*create_wq)(struct ib_pd *pd,
2394 						struct ib_wq_init_attr *init_attr,
2395 						struct ib_udata *udata);
2396 	void			   (*destroy_wq)(struct ib_wq *wq, struct ib_udata *udata);
2397 	int			   (*modify_wq)(struct ib_wq *wq,
2398 						struct ib_wq_attr *attr,
2399 						u32 wq_attr_mask,
2400 						struct ib_udata *udata);
2401 	struct ib_rwq_ind_table *  (*create_rwq_ind_table)(struct ib_device *device,
2402 							   struct ib_rwq_ind_table_init_attr *init_attr,
2403 							   struct ib_udata *udata);
2404 	int                        (*destroy_rwq_ind_table)(struct ib_rwq_ind_table *wq_ind_table);
2405 	struct ib_dm *(*alloc_dm)(struct ib_device *device,
2406 				  struct ib_ucontext *context,
2407 				  struct ib_dm_alloc_attr *attr,
2408 				  struct uverbs_attr_bundle *attrs);
2409 	int (*dealloc_dm)(struct ib_dm *dm, struct uverbs_attr_bundle *attrs);
2410 	struct ib_mr *(*reg_dm_mr)(struct ib_pd *pd, struct ib_dm *dm,
2411 				   struct ib_dm_mr_attr *attr,
2412 				   struct uverbs_attr_bundle *attrs);
2413 	struct ib_counters *(*create_counters)(
2414 		struct ib_device *device, struct uverbs_attr_bundle *attrs);
2415 	int (*destroy_counters)(struct ib_counters *counters);
2416 	int (*read_counters)(struct ib_counters *counters,
2417 			     struct ib_counters_read_attr *counters_read_attr,
2418 			     struct uverbs_attr_bundle *attrs);
2419 
2420 	struct module               *owner;
2421 	struct device                dev;
2422 	/* First group for device attributes, NULL terminated array */
2423 	const struct attribute_group	*groups[2];
2424 
2425 	struct kobject               *ports_parent;
2426 	struct list_head             port_list;
2427 
2428 	enum {
2429 		IB_DEV_UNINITIALIZED,
2430 		IB_DEV_REGISTERED,
2431 		IB_DEV_UNREGISTERED
2432 	}                            reg_state;
2433 
2434 	int			     uverbs_abi_ver;
2435 	u64			     uverbs_cmd_mask;
2436 	u64			     uverbs_ex_cmd_mask;
2437 
2438 	char			     node_desc[IB_DEVICE_NODE_DESC_MAX];
2439 	__be64			     node_guid;
2440 	u32			     local_dma_lkey;
2441 	u16                          is_switch:1;
2442 	u8                           node_type;
2443 	u8                           phys_port_cnt;
2444 	struct ib_device_attr        attrs;
2445 	struct attribute_group	     *hw_stats_ag;
2446 	struct rdma_hw_stats         *hw_stats;
2447 
2448 	const struct uapi_definition   *driver_def;
2449 
2450 	/**
2451 	 * The following mandatory functions are used only at device
2452 	 * registration.  Keep functions such as these at the end of this
2453 	 * structure to avoid cache line misses when accessing struct ib_device
2454 	 * in fast paths.
2455 	 */
2456 	int (*get_port_immutable)(struct ib_device *, u8, struct ib_port_immutable *);
2457 	void (*get_dev_fw_str)(struct ib_device *, char *str, size_t str_len);
2458 };
2459 
2460 struct ib_client {
2461 	char  *name;
2462 	void (*add)   (struct ib_device *);
2463 	void (*remove)(struct ib_device *, void *client_data);
2464 
2465 	/* Returns the net_dev belonging to this ib_client and matching the
2466 	 * given parameters.
2467 	 * @dev:	 An RDMA device that the net_dev use for communication.
2468 	 * @port:	 A physical port number on the RDMA device.
2469 	 * @pkey:	 P_Key that the net_dev uses if applicable.
2470 	 * @gid:	 A GID that the net_dev uses to communicate.
2471 	 * @addr:	 An IP address the net_dev is configured with.
2472 	 * @client_data: The device's client data set by ib_set_client_data().
2473 	 *
2474 	 * An ib_client that implements a net_dev on top of RDMA devices
2475 	 * (such as IP over IB) should implement this callback, allowing the
2476 	 * rdma_cm module to find the right net_dev for a given request.
2477 	 *
2478 	 * The caller is responsible for calling dev_put on the returned
2479 	 * netdev. */
2480 	if_t (*get_net_dev_by_params)(
2481 			struct ib_device *dev,
2482 			u8 port,
2483 			u16 pkey,
2484 			const union ib_gid *gid,
2485 			const struct sockaddr *addr,
2486 			void *client_data);
2487 	struct list_head list;
2488 };
2489 
2490 struct ib_device *ib_alloc_device(size_t size);
2491 void ib_dealloc_device(struct ib_device *device);
2492 
2493 void ib_get_device_fw_str(struct ib_device *device, char *str, size_t str_len);
2494 
2495 int ib_register_device(struct ib_device *device,
2496 		       int (*port_callback)(struct ib_device *,
2497 					    u8, struct kobject *));
2498 void ib_unregister_device(struct ib_device *device);
2499 
2500 int ib_register_client   (struct ib_client *client);
2501 void ib_unregister_client(struct ib_client *client);
2502 
2503 void *ib_get_client_data(struct ib_device *device, struct ib_client *client);
2504 void  ib_set_client_data(struct ib_device *device, struct ib_client *client,
2505 			 void *data);
2506 
2507 int rdma_user_mmap_io(struct ib_ucontext *ucontext, struct vm_area_struct *vma,
2508 		      unsigned long pfn, unsigned long size, pgprot_t prot,
2509 		      struct rdma_user_mmap_entry *entry);
2510 int rdma_user_mmap_entry_insert(struct ib_ucontext *ucontext,
2511 				struct rdma_user_mmap_entry *entry,
2512 				size_t length);
2513 int rdma_user_mmap_entry_insert_range(struct ib_ucontext *ucontext,
2514 				      struct rdma_user_mmap_entry *entry,
2515 				      size_t length, u32 min_pgoff,
2516 				      u32 max_pgoff);
2517 
2518 struct rdma_user_mmap_entry *
2519 rdma_user_mmap_entry_get_pgoff(struct ib_ucontext *ucontext,
2520 			       unsigned long pgoff);
2521 struct rdma_user_mmap_entry *
2522 rdma_user_mmap_entry_get(struct ib_ucontext *ucontext,
2523 			 struct vm_area_struct *vma);
2524 void rdma_user_mmap_entry_put(struct rdma_user_mmap_entry *entry);
2525 
2526 void rdma_user_mmap_entry_remove(struct rdma_user_mmap_entry *entry);
2527 static inline int ib_copy_from_udata(void *dest, struct ib_udata *udata, size_t len)
2528 {
2529 	return copy_from_user(dest, udata->inbuf, len) ? -EFAULT : 0;
2530 }
2531 
2532 static inline int ib_copy_to_udata(struct ib_udata *udata, void *src, size_t len)
2533 {
2534 	return copy_to_user(udata->outbuf, src, len) ? -EFAULT : 0;
2535 }
2536 
2537 static inline bool ib_is_buffer_cleared(const void __user *p,
2538 					size_t len)
2539 {
2540 	bool ret;
2541 	u8 *buf;
2542 
2543 	if (len > USHRT_MAX)
2544 		return false;
2545 
2546 	buf = memdup_user(p, len);
2547 	if (IS_ERR(buf))
2548 		return false;
2549 
2550 	ret = !memchr_inv(buf, 0, len);
2551 	kfree(buf);
2552 	return ret;
2553 }
2554 
2555 static inline bool ib_is_udata_cleared(struct ib_udata *udata,
2556 				       size_t offset,
2557 				       size_t len)
2558 {
2559 	return ib_is_buffer_cleared(udata->inbuf + offset, len);
2560 }
2561 
2562 /**
2563  * ib_is_destroy_retryable - Check whether the uobject destruction
2564  * is retryable.
2565  * @ret: The initial destruction return code
2566  * @why: remove reason
2567  * @uobj: The uobject that is destroyed
2568  *
2569  * This function is a helper function that IB layer and low-level drivers
2570  * can use to consider whether the destruction of the given uobject is
2571  * retry-able.
2572  * It checks the original return code, if it wasn't success the destruction
2573  * is retryable according to the ucontext state (i.e. cleanup_retryable) and
2574  * the remove reason. (i.e. why).
2575  * Must be called with the object locked for destroy.
2576  */
2577 static inline bool ib_is_destroy_retryable(int ret, enum rdma_remove_reason why,
2578 					   struct ib_uobject *uobj)
2579 {
2580 	return ret && (why == RDMA_REMOVE_DESTROY ||
2581 		       uobj->context->cleanup_retryable);
2582 }
2583 
2584 /**
2585  * ib_destroy_usecnt - Called during destruction to check the usecnt
2586  * @usecnt: The usecnt atomic
2587  * @why: remove reason
2588  * @uobj: The uobject that is destroyed
2589  *
2590  * Non-zero usecnts will block destruction unless destruction was triggered by
2591  * a ucontext cleanup.
2592  */
2593 static inline int ib_destroy_usecnt(atomic_t *usecnt,
2594 				    enum rdma_remove_reason why,
2595 				    struct ib_uobject *uobj)
2596 {
2597 	if (atomic_read(usecnt) && ib_is_destroy_retryable(-EBUSY, why, uobj))
2598 		return -EBUSY;
2599 	return 0;
2600 }
2601 
2602 /**
2603  * ib_modify_qp_is_ok - Check that the supplied attribute mask
2604  * contains all required attributes and no attributes not allowed for
2605  * the given QP state transition.
2606  * @cur_state: Current QP state
2607  * @next_state: Next QP state
2608  * @type: QP type
2609  * @mask: Mask of supplied QP attributes
2610  *
2611  * This function is a helper function that a low-level driver's
2612  * modify_qp method can use to validate the consumer's input.  It
2613  * checks that cur_state and next_state are valid QP states, that a
2614  * transition from cur_state to next_state is allowed by the IB spec,
2615  * and that the attribute mask supplied is allowed for the transition.
2616  */
2617 bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
2618 			enum ib_qp_type type, enum ib_qp_attr_mask mask);
2619 
2620 void ib_register_event_handler(struct ib_event_handler *event_handler);
2621 void ib_unregister_event_handler(struct ib_event_handler *event_handler);
2622 void ib_dispatch_event(struct ib_event *event);
2623 
2624 int ib_query_port(struct ib_device *device,
2625 		  u8 port_num, struct ib_port_attr *port_attr);
2626 
2627 enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device,
2628 					       u8 port_num);
2629 
2630 /**
2631  * rdma_cap_ib_switch - Check if the device is IB switch
2632  * @device: Device to check
2633  *
2634  * Device driver is responsible for setting is_switch bit on
2635  * in ib_device structure at init time.
2636  *
2637  * Return: true if the device is IB switch.
2638  */
2639 static inline bool rdma_cap_ib_switch(const struct ib_device *device)
2640 {
2641 	return device->is_switch;
2642 }
2643 
2644 /**
2645  * rdma_start_port - Return the first valid port number for the device
2646  * specified
2647  *
2648  * @device: Device to be checked
2649  *
2650  * Return start port number
2651  */
2652 static inline u8 rdma_start_port(const struct ib_device *device)
2653 {
2654 	return rdma_cap_ib_switch(device) ? 0 : 1;
2655 }
2656 
2657 /**
2658  * rdma_end_port - Return the last valid port number for the device
2659  * specified
2660  *
2661  * @device: Device to be checked
2662  *
2663  * Return last port number
2664  */
2665 static inline u8 rdma_end_port(const struct ib_device *device)
2666 {
2667 	return rdma_cap_ib_switch(device) ? 0 : device->phys_port_cnt;
2668 }
2669 
2670 static inline int rdma_is_port_valid(const struct ib_device *device,
2671 				     unsigned int port)
2672 {
2673 	return (port >= rdma_start_port(device) &&
2674 		port <= rdma_end_port(device));
2675 }
2676 
2677 static inline bool rdma_protocol_ib(const struct ib_device *device, u8 port_num)
2678 {
2679 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_PROT_IB;
2680 }
2681 
2682 static inline bool rdma_protocol_roce(const struct ib_device *device, u8 port_num)
2683 {
2684 	return device->port_immutable[port_num].core_cap_flags &
2685 		(RDMA_CORE_CAP_PROT_ROCE | RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP);
2686 }
2687 
2688 static inline bool rdma_protocol_roce_udp_encap(const struct ib_device *device, u8 port_num)
2689 {
2690 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP;
2691 }
2692 
2693 static inline bool rdma_protocol_roce_eth_encap(const struct ib_device *device, u8 port_num)
2694 {
2695 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_PROT_ROCE;
2696 }
2697 
2698 static inline bool rdma_protocol_iwarp(const struct ib_device *device, u8 port_num)
2699 {
2700 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_PROT_IWARP;
2701 }
2702 
2703 static inline bool rdma_ib_or_roce(const struct ib_device *device, u8 port_num)
2704 {
2705 	return rdma_protocol_ib(device, port_num) ||
2706 		rdma_protocol_roce(device, port_num);
2707 }
2708 
2709 /**
2710  * rdma_cap_ib_mad - Check if the port of a device supports Infiniband
2711  * Management Datagrams.
2712  * @device: Device to check
2713  * @port_num: Port number to check
2714  *
2715  * Management Datagrams (MAD) are a required part of the InfiniBand
2716  * specification and are supported on all InfiniBand devices.  A slightly
2717  * extended version are also supported on OPA interfaces.
2718  *
2719  * Return: true if the port supports sending/receiving of MAD packets.
2720  */
2721 static inline bool rdma_cap_ib_mad(const struct ib_device *device, u8 port_num)
2722 {
2723 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_IB_MAD;
2724 }
2725 
2726 /**
2727  * rdma_cap_opa_mad - Check if the port of device provides support for OPA
2728  * Management Datagrams.
2729  * @device: Device to check
2730  * @port_num: Port number to check
2731  *
2732  * Intel OmniPath devices extend and/or replace the InfiniBand Management
2733  * datagrams with their own versions.  These OPA MADs share many but not all of
2734  * the characteristics of InfiniBand MADs.
2735  *
2736  * OPA MADs differ in the following ways:
2737  *
2738  *    1) MADs are variable size up to 2K
2739  *       IBTA defined MADs remain fixed at 256 bytes
2740  *    2) OPA SMPs must carry valid PKeys
2741  *    3) OPA SMP packets are a different format
2742  *
2743  * Return: true if the port supports OPA MAD packet formats.
2744  */
2745 static inline bool rdma_cap_opa_mad(struct ib_device *device, u8 port_num)
2746 {
2747 	return (device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_OPA_MAD)
2748 		== RDMA_CORE_CAP_OPA_MAD;
2749 }
2750 
2751 /**
2752  * rdma_cap_ib_smi - Check if the port of a device provides an Infiniband
2753  * Subnet Management Agent (SMA) on the Subnet Management Interface (SMI).
2754  * @device: Device to check
2755  * @port_num: Port number to check
2756  *
2757  * Each InfiniBand node is required to provide a Subnet Management Agent
2758  * that the subnet manager can access.  Prior to the fabric being fully
2759  * configured by the subnet manager, the SMA is accessed via a well known
2760  * interface called the Subnet Management Interface (SMI).  This interface
2761  * uses directed route packets to communicate with the SM to get around the
2762  * chicken and egg problem of the SM needing to know what's on the fabric
2763  * in order to configure the fabric, and needing to configure the fabric in
2764  * order to send packets to the devices on the fabric.  These directed
2765  * route packets do not need the fabric fully configured in order to reach
2766  * their destination.  The SMI is the only method allowed to send
2767  * directed route packets on an InfiniBand fabric.
2768  *
2769  * Return: true if the port provides an SMI.
2770  */
2771 static inline bool rdma_cap_ib_smi(const struct ib_device *device, u8 port_num)
2772 {
2773 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_IB_SMI;
2774 }
2775 
2776 /**
2777  * rdma_cap_ib_cm - Check if the port of device has the capability Infiniband
2778  * Communication Manager.
2779  * @device: Device to check
2780  * @port_num: Port number to check
2781  *
2782  * The InfiniBand Communication Manager is one of many pre-defined General
2783  * Service Agents (GSA) that are accessed via the General Service
2784  * Interface (GSI).  It's role is to facilitate establishment of connections
2785  * between nodes as well as other management related tasks for established
2786  * connections.
2787  *
2788  * Return: true if the port supports an IB CM (this does not guarantee that
2789  * a CM is actually running however).
2790  */
2791 static inline bool rdma_cap_ib_cm(const struct ib_device *device, u8 port_num)
2792 {
2793 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_IB_CM;
2794 }
2795 
2796 /**
2797  * rdma_cap_iw_cm - Check if the port of device has the capability IWARP
2798  * Communication Manager.
2799  * @device: Device to check
2800  * @port_num: Port number to check
2801  *
2802  * Similar to above, but specific to iWARP connections which have a different
2803  * managment protocol than InfiniBand.
2804  *
2805  * Return: true if the port supports an iWARP CM (this does not guarantee that
2806  * a CM is actually running however).
2807  */
2808 static inline bool rdma_cap_iw_cm(const struct ib_device *device, u8 port_num)
2809 {
2810 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_IW_CM;
2811 }
2812 
2813 /**
2814  * rdma_cap_ib_sa - Check if the port of device has the capability Infiniband
2815  * Subnet Administration.
2816  * @device: Device to check
2817  * @port_num: Port number to check
2818  *
2819  * An InfiniBand Subnet Administration (SA) service is a pre-defined General
2820  * Service Agent (GSA) provided by the Subnet Manager (SM).  On InfiniBand
2821  * fabrics, devices should resolve routes to other hosts by contacting the
2822  * SA to query the proper route.
2823  *
2824  * Return: true if the port should act as a client to the fabric Subnet
2825  * Administration interface.  This does not imply that the SA service is
2826  * running locally.
2827  */
2828 static inline bool rdma_cap_ib_sa(const struct ib_device *device, u8 port_num)
2829 {
2830 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_IB_SA;
2831 }
2832 
2833 /**
2834  * rdma_cap_ib_mcast - Check if the port of device has the capability Infiniband
2835  * Multicast.
2836  * @device: Device to check
2837  * @port_num: Port number to check
2838  *
2839  * InfiniBand multicast registration is more complex than normal IPv4 or
2840  * IPv6 multicast registration.  Each Host Channel Adapter must register
2841  * with the Subnet Manager when it wishes to join a multicast group.  It
2842  * should do so only once regardless of how many queue pairs it subscribes
2843  * to this group.  And it should leave the group only after all queue pairs
2844  * attached to the group have been detached.
2845  *
2846  * Return: true if the port must undertake the additional adminstrative
2847  * overhead of registering/unregistering with the SM and tracking of the
2848  * total number of queue pairs attached to the multicast group.
2849  */
2850 static inline bool rdma_cap_ib_mcast(const struct ib_device *device, u8 port_num)
2851 {
2852 	return rdma_cap_ib_sa(device, port_num);
2853 }
2854 
2855 /**
2856  * rdma_cap_af_ib - Check if the port of device has the capability
2857  * Native Infiniband Address.
2858  * @device: Device to check
2859  * @port_num: Port number to check
2860  *
2861  * InfiniBand addressing uses a port's GUID + Subnet Prefix to make a default
2862  * GID.  RoCE uses a different mechanism, but still generates a GID via
2863  * a prescribed mechanism and port specific data.
2864  *
2865  * Return: true if the port uses a GID address to identify devices on the
2866  * network.
2867  */
2868 static inline bool rdma_cap_af_ib(const struct ib_device *device, u8 port_num)
2869 {
2870 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_AF_IB;
2871 }
2872 
2873 /**
2874  * rdma_cap_eth_ah - Check if the port of device has the capability
2875  * Ethernet Address Handle.
2876  * @device: Device to check
2877  * @port_num: Port number to check
2878  *
2879  * RoCE is InfiniBand over Ethernet, and it uses a well defined technique
2880  * to fabricate GIDs over Ethernet/IP specific addresses native to the
2881  * port.  Normally, packet headers are generated by the sending host
2882  * adapter, but when sending connectionless datagrams, we must manually
2883  * inject the proper headers for the fabric we are communicating over.
2884  *
2885  * Return: true if we are running as a RoCE port and must force the
2886  * addition of a Global Route Header built from our Ethernet Address
2887  * Handle into our header list for connectionless packets.
2888  */
2889 static inline bool rdma_cap_eth_ah(const struct ib_device *device, u8 port_num)
2890 {
2891 	return device->port_immutable[port_num].core_cap_flags & RDMA_CORE_CAP_ETH_AH;
2892 }
2893 
2894 /**
2895  * rdma_cap_opa_ah - Check if the port of device supports
2896  * OPA Address handles
2897  * @device: Device to check
2898  * @port_num: Port number to check
2899  *
2900  * Return: true if we are running on an OPA device which supports
2901  * the extended OPA addressing.
2902  */
2903 static inline bool rdma_cap_opa_ah(struct ib_device *device, u8 port_num)
2904 {
2905 	return (device->port_immutable[port_num].core_cap_flags &
2906 		RDMA_CORE_CAP_OPA_AH) == RDMA_CORE_CAP_OPA_AH;
2907 }
2908 
2909 /**
2910  * rdma_max_mad_size - Return the max MAD size required by this RDMA Port.
2911  *
2912  * @device: Device
2913  * @port_num: Port number
2914  *
2915  * This MAD size includes the MAD headers and MAD payload.  No other headers
2916  * are included.
2917  *
2918  * Return the max MAD size required by the Port.  Will return 0 if the port
2919  * does not support MADs
2920  */
2921 static inline size_t rdma_max_mad_size(const struct ib_device *device, u8 port_num)
2922 {
2923 	return device->port_immutable[port_num].max_mad_size;
2924 }
2925 
2926 /**
2927  * rdma_cap_roce_gid_table - Check if the port of device uses roce_gid_table
2928  * @device: Device to check
2929  * @port_num: Port number to check
2930  *
2931  * RoCE GID table mechanism manages the various GIDs for a device.
2932  *
2933  * NOTE: if allocating the port's GID table has failed, this call will still
2934  * return true, but any RoCE GID table API will fail.
2935  *
2936  * Return: true if the port uses RoCE GID table mechanism in order to manage
2937  * its GIDs.
2938  */
2939 static inline bool rdma_cap_roce_gid_table(const struct ib_device *device,
2940 					   u8 port_num)
2941 {
2942 	return rdma_protocol_roce(device, port_num) &&
2943 		device->add_gid && device->del_gid;
2944 }
2945 
2946 /*
2947  * Check if the device supports READ W/ INVALIDATE.
2948  */
2949 static inline bool rdma_cap_read_inv(struct ib_device *dev, u32 port_num)
2950 {
2951 	/*
2952 	 * iWarp drivers must support READ W/ INVALIDATE.  No other protocol
2953 	 * has support for it yet.
2954 	 */
2955 	return rdma_protocol_iwarp(dev, port_num);
2956 }
2957 
2958 int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
2959 			 int state);
2960 int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
2961 		     struct ifla_vf_info *info);
2962 int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
2963 		    struct ifla_vf_stats *stats);
2964 int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
2965 		   int type);
2966 
2967 int ib_query_pkey(struct ib_device *device,
2968 		  u8 port_num, u16 index, u16 *pkey);
2969 
2970 int ib_modify_device(struct ib_device *device,
2971 		     int device_modify_mask,
2972 		     struct ib_device_modify *device_modify);
2973 
2974 int ib_modify_port(struct ib_device *device,
2975 		   u8 port_num, int port_modify_mask,
2976 		   struct ib_port_modify *port_modify);
2977 
2978 int ib_find_gid(struct ib_device *device, union ib_gid *gid,
2979 		u8 *port_num, u16 *index);
2980 
2981 int ib_find_pkey(struct ib_device *device,
2982 		 u8 port_num, u16 pkey, u16 *index);
2983 
2984 enum ib_pd_flags {
2985 	/*
2986 	 * Create a memory registration for all memory in the system and place
2987 	 * the rkey for it into pd->unsafe_global_rkey.  This can be used by
2988 	 * ULPs to avoid the overhead of dynamic MRs.
2989 	 *
2990 	 * This flag is generally considered unsafe and must only be used in
2991 	 * extremly trusted environments.  Every use of it will log a warning
2992 	 * in the kernel log.
2993 	 */
2994 	IB_PD_UNSAFE_GLOBAL_RKEY	= 0x01,
2995 };
2996 
2997 struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
2998 		const char *caller);
2999 #define ib_alloc_pd(device, flags) \
3000 	__ib_alloc_pd((device), (flags), __func__)
3001 
3002 /**
3003  * ib_dealloc_pd_user - Deallocate kernel/user PD
3004  * @pd: The protection domain
3005  * @udata: Valid user data or NULL for kernel objects
3006  */
3007 void ib_dealloc_pd_user(struct ib_pd *pd, struct ib_udata *udata);
3008 
3009 /**
3010  * ib_dealloc_pd - Deallocate kernel PD
3011  * @pd: The protection domain
3012  *
3013  * NOTE: for user PD use ib_dealloc_pd_user with valid udata!
3014  */
3015 static inline void ib_dealloc_pd(struct ib_pd *pd)
3016 {
3017 	ib_dealloc_pd_user(pd, NULL);
3018 }
3019 
3020 enum rdma_create_ah_flags {
3021 	/* In a sleepable context */
3022 	RDMA_CREATE_AH_SLEEPABLE = BIT(0),
3023 };
3024 
3025 /**
3026  * rdma_create_ah - Creates an address handle for the given address vector.
3027  * @pd: The protection domain associated with the address handle.
3028  * @ah_attr: The attributes of the address vector.
3029  * @flags: Create address handle flags (see enum rdma_create_ah_flags).
3030  *
3031  * The address handle is used to reference a local or global destination
3032  * in all UD QP post sends.
3033  */
3034 struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
3035 			     u32 flags);
3036 
3037 /**
3038  * rdma_create_user_ah - Creates an address handle for the given address vector.
3039  * It resolves destination mac address for ah attribute of RoCE type.
3040  * @pd: The protection domain associated with the address handle.
3041  * @ah_attr: The attributes of the address vector.
3042  * @udata: pointer to user's input output buffer information need by
3043  *         provider driver.
3044  *
3045  * It returns 0 on success and returns appropriate error code on error.
3046  * The address handle is used to reference a local or global destination
3047  * in all UD QP post sends.
3048  */
3049 struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
3050 				  struct rdma_ah_attr *ah_attr,
3051 				  struct ib_udata *udata);
3052 
3053 /**
3054  * ib_get_gids_from_rdma_hdr - Get sgid and dgid from GRH or IPv4 header
3055  *   work completion.
3056  * @hdr: the L3 header to parse
3057  * @net_type: type of header to parse
3058  * @sgid: place to store source gid
3059  * @dgid: place to store destination gid
3060  */
3061 int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
3062 			      enum rdma_network_type net_type,
3063 			      union ib_gid *sgid, union ib_gid *dgid);
3064 
3065 /**
3066  * ib_get_rdma_header_version - Get the header version
3067  * @hdr: the L3 header to parse
3068  */
3069 int ib_get_rdma_header_version(const union rdma_network_hdr *hdr);
3070 
3071 /**
3072  * ib_init_ah_attr_from_wc - Initializes address handle attributes from a
3073  *   work completion.
3074  * @device: Device on which the received message arrived.
3075  * @port_num: Port on which the received message arrived.
3076  * @wc: Work completion associated with the received message.
3077  * @grh: References the received global route header.  This parameter is
3078  *   ignored unless the work completion indicates that the GRH is valid.
3079  * @ah_attr: Returned attributes that can be used when creating an address
3080  *   handle for replying to the message.
3081  * When ib_init_ah_attr_from_wc() returns success,
3082  * (a) for IB link layer it optionally contains a reference to SGID attribute
3083  * when GRH is present for IB link layer.
3084  * (b) for RoCE link layer it contains a reference to SGID attribute.
3085  * User must invoke rdma_cleanup_ah_attr_gid_attr() to release reference to SGID
3086  * attributes which are initialized using ib_init_ah_attr_from_wc().
3087  *
3088  */
3089 int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num,
3090 			    const struct ib_wc *wc, const struct ib_grh *grh,
3091 			    struct rdma_ah_attr *ah_attr);
3092 
3093 /**
3094  * ib_create_ah_from_wc - Creates an address handle associated with the
3095  *   sender of the specified work completion.
3096  * @pd: The protection domain associated with the address handle.
3097  * @wc: Work completion information associated with a received message.
3098  * @grh: References the received global route header.  This parameter is
3099  *   ignored unless the work completion indicates that the GRH is valid.
3100  * @port_num: The outbound port number to associate with the address.
3101  *
3102  * The address handle is used to reference a local or global destination
3103  * in all UD QP post sends.
3104  */
3105 struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
3106 				   const struct ib_grh *grh, u8 port_num);
3107 
3108 /**
3109  * rdma_modify_ah - Modifies the address vector associated with an address
3110  *   handle.
3111  * @ah: The address handle to modify.
3112  * @ah_attr: The new address vector attributes to associate with the
3113  *   address handle.
3114  */
3115 int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr);
3116 
3117 /**
3118  * rdma_query_ah - Queries the address vector associated with an address
3119  *   handle.
3120  * @ah: The address handle to query.
3121  * @ah_attr: The address vector attributes associated with the address
3122  *   handle.
3123  */
3124 int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr);
3125 
3126 enum rdma_destroy_ah_flags {
3127 	/* In a sleepable context */
3128 	RDMA_DESTROY_AH_SLEEPABLE = BIT(0),
3129 };
3130 
3131 /**
3132  * rdma_destroy_ah_user - Destroys an address handle.
3133  * @ah: The address handle to destroy.
3134  * @flags: Destroy address handle flags (see enum rdma_destroy_ah_flags).
3135  * @udata: Valid user data or NULL for kernel objects
3136  */
3137 int rdma_destroy_ah_user(struct ib_ah *ah, u32 flags, struct ib_udata *udata);
3138 
3139 /**
3140  * rdma_destroy_ah - Destroys an kernel address handle.
3141  * @ah: The address handle to destroy.
3142  * @flags: Destroy address handle flags (see enum rdma_destroy_ah_flags).
3143  *
3144  * NOTE: for user ah use ib_destroy_ah_user with valid udata!
3145  */
3146 static inline int rdma_destroy_ah(struct ib_ah *ah, u32 flags)
3147 {
3148 	return rdma_destroy_ah_user(ah, flags, NULL);
3149 }
3150 
3151 /**
3152  * ib_create_srq - Creates a SRQ associated with the specified protection
3153  *   domain.
3154  * @pd: The protection domain associated with the SRQ.
3155  * @srq_init_attr: A list of initial attributes required to create the
3156  *   SRQ.  If SRQ creation succeeds, then the attributes are updated to
3157  *   the actual capabilities of the created SRQ.
3158  *
3159  * srq_attr->max_wr and srq_attr->max_sge are read the determine the
3160  * requested size of the SRQ, and set to the actual values allocated
3161  * on return.  If ib_create_srq() succeeds, then max_wr and max_sge
3162  * will always be at least as large as the requested values.
3163  */
3164 struct ib_srq *ib_create_srq(struct ib_pd *pd,
3165 			     struct ib_srq_init_attr *srq_init_attr);
3166 
3167 /**
3168  * ib_modify_srq - Modifies the attributes for the specified SRQ.
3169  * @srq: The SRQ to modify.
3170  * @srq_attr: On input, specifies the SRQ attributes to modify.  On output,
3171  *   the current values of selected SRQ attributes are returned.
3172  * @srq_attr_mask: A bit-mask used to specify which attributes of the SRQ
3173  *   are being modified.
3174  *
3175  * The mask may contain IB_SRQ_MAX_WR to resize the SRQ and/or
3176  * IB_SRQ_LIMIT to set the SRQ's limit and request notification when
3177  * the number of receives queued drops below the limit.
3178  */
3179 int ib_modify_srq(struct ib_srq *srq,
3180 		  struct ib_srq_attr *srq_attr,
3181 		  enum ib_srq_attr_mask srq_attr_mask);
3182 
3183 /**
3184  * ib_query_srq - Returns the attribute list and current values for the
3185  *   specified SRQ.
3186  * @srq: The SRQ to query.
3187  * @srq_attr: The attributes of the specified SRQ.
3188  */
3189 int ib_query_srq(struct ib_srq *srq,
3190 		 struct ib_srq_attr *srq_attr);
3191 
3192 /**
3193  * ib_destroy_srq_user - Destroys the specified SRQ.
3194  * @srq: The SRQ to destroy.
3195  * @udata: Valid user data or NULL for kernel objects
3196  */
3197 int ib_destroy_srq_user(struct ib_srq *srq, struct ib_udata *udata);
3198 
3199 /**
3200  * ib_destroy_srq - Destroys the specified kernel SRQ.
3201  * @srq: The SRQ to destroy.
3202  *
3203  * NOTE: for user srq use ib_destroy_srq_user with valid udata!
3204  */
3205 static inline int ib_destroy_srq(struct ib_srq *srq)
3206 {
3207 	return ib_destroy_srq_user(srq, NULL);
3208 }
3209 
3210 /**
3211  * ib_post_srq_recv - Posts a list of work requests to the specified SRQ.
3212  * @srq: The SRQ to post the work request on.
3213  * @recv_wr: A list of work requests to post on the receive queue.
3214  * @bad_recv_wr: On an immediate failure, this parameter will reference
3215  *   the work request that failed to be posted on the QP.
3216  */
3217 static inline int ib_post_srq_recv(struct ib_srq *srq,
3218 				   const struct ib_recv_wr *recv_wr,
3219 				   const struct ib_recv_wr **bad_recv_wr)
3220 {
3221 	const struct ib_recv_wr *dummy;
3222 
3223 	return srq->device->post_srq_recv(srq, recv_wr, bad_recv_wr ? : &dummy);
3224 }
3225 
3226 /**
3227  * ib_create_qp - Creates a QP associated with the specified protection
3228  *   domain.
3229  * @pd: The protection domain associated with the QP.
3230  * @qp_init_attr: A list of initial attributes required to create the
3231  *   QP.  If QP creation succeeds, then the attributes are updated to
3232  *   the actual capabilities of the created QP.
3233  */
3234 struct ib_qp *ib_create_qp(struct ib_pd *pd,
3235 			   struct ib_qp_init_attr *qp_init_attr);
3236 
3237 /**
3238  * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
3239  * @qp: The QP to modify.
3240  * @attr: On input, specifies the QP attributes to modify.  On output,
3241  *   the current values of selected QP attributes are returned.
3242  * @attr_mask: A bit-mask used to specify which attributes of the QP
3243  *   are being modified.
3244  * @udata: pointer to user's input output buffer information
3245  *   are being modified.
3246  * It returns 0 on success and returns appropriate error code on error.
3247  */
3248 int ib_modify_qp_with_udata(struct ib_qp *qp,
3249 			    struct ib_qp_attr *attr,
3250 			    int attr_mask,
3251 			    struct ib_udata *udata);
3252 
3253 /**
3254  * ib_modify_qp - Modifies the attributes for the specified QP and then
3255  *   transitions the QP to the given state.
3256  * @qp: The QP to modify.
3257  * @qp_attr: On input, specifies the QP attributes to modify.  On output,
3258  *   the current values of selected QP attributes are returned.
3259  * @qp_attr_mask: A bit-mask used to specify which attributes of the QP
3260  *   are being modified.
3261  */
3262 int ib_modify_qp(struct ib_qp *qp,
3263 		 struct ib_qp_attr *qp_attr,
3264 		 int qp_attr_mask);
3265 
3266 /**
3267  * ib_query_qp - Returns the attribute list and current values for the
3268  *   specified QP.
3269  * @qp: The QP to query.
3270  * @qp_attr: The attributes of the specified QP.
3271  * @qp_attr_mask: A bit-mask used to select specific attributes to query.
3272  * @qp_init_attr: Additional attributes of the selected QP.
3273  *
3274  * The qp_attr_mask may be used to limit the query to gathering only the
3275  * selected attributes.
3276  */
3277 int ib_query_qp(struct ib_qp *qp,
3278 		struct ib_qp_attr *qp_attr,
3279 		int qp_attr_mask,
3280 		struct ib_qp_init_attr *qp_init_attr);
3281 
3282 /**
3283  * ib_destroy_qp - Destroys the specified QP.
3284  * @qp: The QP to destroy.
3285  * @udata: Valid udata or NULL for kernel objects
3286  */
3287 int ib_destroy_qp_user(struct ib_qp *qp, struct ib_udata *udata);
3288 
3289 /**
3290  * ib_destroy_qp - Destroys the specified kernel QP.
3291  * @qp: The QP to destroy.
3292  *
3293  * NOTE: for user qp use ib_destroy_qp_user with valid udata!
3294  */
3295 static inline int ib_destroy_qp(struct ib_qp *qp)
3296 {
3297 	return ib_destroy_qp_user(qp, NULL);
3298 }
3299 
3300 /**
3301  * ib_open_qp - Obtain a reference to an existing sharable QP.
3302  * @xrcd - XRC domain
3303  * @qp_open_attr: Attributes identifying the QP to open.
3304  *
3305  * Returns a reference to a sharable QP.
3306  */
3307 struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
3308 			 struct ib_qp_open_attr *qp_open_attr);
3309 
3310 /**
3311  * ib_close_qp - Release an external reference to a QP.
3312  * @qp: The QP handle to release
3313  *
3314  * The opened QP handle is released by the caller.  The underlying
3315  * shared QP is not destroyed until all internal references are released.
3316  */
3317 int ib_close_qp(struct ib_qp *qp);
3318 
3319 /**
3320  * ib_post_send - Posts a list of work requests to the send queue of
3321  *   the specified QP.
3322  * @qp: The QP to post the work request on.
3323  * @send_wr: A list of work requests to post on the send queue.
3324  * @bad_send_wr: On an immediate failure, this parameter will reference
3325  *   the work request that failed to be posted on the QP.
3326  *
3327  * While IBA Vol. 1 section 11.4.1.1 specifies that if an immediate
3328  * error is returned, the QP state shall not be affected,
3329  * ib_post_send() will return an immediate error after queueing any
3330  * earlier work requests in the list.
3331  */
3332 static inline int ib_post_send(struct ib_qp *qp,
3333 			       const struct ib_send_wr *send_wr,
3334 			       const struct ib_send_wr **bad_send_wr)
3335 {
3336 	const struct ib_send_wr *dummy;
3337 
3338 	return qp->device->post_send(qp, send_wr, bad_send_wr ? : &dummy);
3339 }
3340 
3341 /**
3342  * ib_post_recv - Posts a list of work requests to the receive queue of
3343  *   the specified QP.
3344  * @qp: The QP to post the work request on.
3345  * @recv_wr: A list of work requests to post on the receive queue.
3346  * @bad_recv_wr: On an immediate failure, this parameter will reference
3347  *   the work request that failed to be posted on the QP.
3348  */
3349 static inline int ib_post_recv(struct ib_qp *qp,
3350 			       const struct ib_recv_wr *recv_wr,
3351 			       const struct ib_recv_wr **bad_recv_wr)
3352 {
3353 	const struct ib_recv_wr *dummy;
3354 
3355 	return qp->device->post_recv(qp, recv_wr, bad_recv_wr ? : &dummy);
3356 }
3357 
3358 struct ib_cq *__ib_alloc_cq_user(struct ib_device *dev, void *private,
3359 				 int nr_cqe, int comp_vector,
3360 				 enum ib_poll_context poll_ctx,
3361 				 const char *caller, struct ib_udata *udata);
3362 
3363 /**
3364  * ib_alloc_cq_user: Allocate kernel/user CQ
3365  * @dev: The IB device
3366  * @private: Private data attached to the CQE
3367  * @nr_cqe: Number of CQEs in the CQ
3368  * @comp_vector: Completion vector used for the IRQs
3369  * @poll_ctx: Context used for polling the CQ
3370  * @udata: Valid user data or NULL for kernel objects
3371  */
3372 static inline struct ib_cq *ib_alloc_cq_user(struct ib_device *dev,
3373 					     void *private, int nr_cqe,
3374 					     int comp_vector,
3375 					     enum ib_poll_context poll_ctx,
3376 					     struct ib_udata *udata)
3377 {
3378 	return __ib_alloc_cq_user(dev, private, nr_cqe, comp_vector, poll_ctx,
3379 				  "ibcore", udata);
3380 }
3381 
3382 /**
3383  * ib_alloc_cq: Allocate kernel CQ
3384  * @dev: The IB device
3385  * @private: Private data attached to the CQE
3386  * @nr_cqe: Number of CQEs in the CQ
3387  * @comp_vector: Completion vector used for the IRQs
3388  * @poll_ctx: Context used for polling the CQ
3389  *
3390  * NOTE: for user cq use ib_alloc_cq_user with valid udata!
3391  */
3392 static inline struct ib_cq *ib_alloc_cq(struct ib_device *dev, void *private,
3393 					int nr_cqe, int comp_vector,
3394 					enum ib_poll_context poll_ctx)
3395 {
3396 	return ib_alloc_cq_user(dev, private, nr_cqe, comp_vector, poll_ctx,
3397 				NULL);
3398 }
3399 
3400 /**
3401  * ib_free_cq_user - Free kernel/user CQ
3402  * @cq: The CQ to free
3403  * @udata: Valid user data or NULL for kernel objects
3404  */
3405 void ib_free_cq_user(struct ib_cq *cq, struct ib_udata *udata);
3406 
3407 /**
3408  * ib_free_cq - Free kernel CQ
3409  * @cq: The CQ to free
3410  *
3411  * NOTE: for user cq use ib_free_cq_user with valid udata!
3412  */
3413 static inline void ib_free_cq(struct ib_cq *cq)
3414 {
3415 	ib_free_cq_user(cq, NULL);
3416 }
3417 
3418 int ib_process_cq_direct(struct ib_cq *cq, int budget);
3419 
3420 /**
3421  * ib_create_cq - Creates a CQ on the specified device.
3422  * @device: The device on which to create the CQ.
3423  * @comp_handler: A user-specified callback that is invoked when a
3424  *   completion event occurs on the CQ.
3425  * @event_handler: A user-specified callback that is invoked when an
3426  *   asynchronous event not associated with a completion occurs on the CQ.
3427  * @cq_context: Context associated with the CQ returned to the user via
3428  *   the associated completion and event handlers.
3429  * @cq_attr: The attributes the CQ should be created upon.
3430  *
3431  * Users can examine the cq structure to determine the actual CQ size.
3432  */
3433 struct ib_cq *__ib_create_cq(struct ib_device *device,
3434 			     ib_comp_handler comp_handler,
3435 			     void (*event_handler)(struct ib_event *, void *),
3436 			     void *cq_context,
3437 			     const struct ib_cq_init_attr *cq_attr,
3438 			     const char *caller);
3439 #define ib_create_cq(device, cmp_hndlr, evt_hndlr, cq_ctxt, cq_attr) \
3440 	__ib_create_cq((device), (cmp_hndlr), (evt_hndlr), (cq_ctxt), (cq_attr), "ibcore")
3441 
3442 /**
3443  * ib_resize_cq - Modifies the capacity of the CQ.
3444  * @cq: The CQ to resize.
3445  * @cqe: The minimum size of the CQ.
3446  *
3447  * Users can examine the cq structure to determine the actual CQ size.
3448  */
3449 int ib_resize_cq(struct ib_cq *cq, int cqe);
3450 
3451 /**
3452  * rdma_set_cq_moderation - Modifies moderation params of the CQ
3453  * @cq: The CQ to modify.
3454  * @cq_count: number of CQEs that will trigger an event
3455  * @cq_period: max period of time in usec before triggering an event
3456  *
3457  */
3458 int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period);
3459 
3460 /**
3461  * ib_destroy_cq_user - Destroys the specified CQ.
3462  * @cq: The CQ to destroy.
3463  * @udata: Valid user data or NULL for kernel objects
3464  */
3465 int ib_destroy_cq_user(struct ib_cq *cq, struct ib_udata *udata);
3466 
3467 /**
3468  * ib_destroy_cq - Destroys the specified kernel CQ.
3469  * @cq: The CQ to destroy.
3470  *
3471  * NOTE: for user cq use ib_destroy_cq_user with valid udata!
3472  */
3473 static inline void ib_destroy_cq(struct ib_cq *cq)
3474 {
3475 	ib_destroy_cq_user(cq, NULL);
3476 }
3477 
3478 /**
3479  * ib_poll_cq - poll a CQ for completion(s)
3480  * @cq:the CQ being polled
3481  * @num_entries:maximum number of completions to return
3482  * @wc:array of at least @num_entries &struct ib_wc where completions
3483  *   will be returned
3484  *
3485  * Poll a CQ for (possibly multiple) completions.  If the return value
3486  * is < 0, an error occurred.  If the return value is >= 0, it is the
3487  * number of completions returned.  If the return value is
3488  * non-negative and < num_entries, then the CQ was emptied.
3489  */
3490 static inline int ib_poll_cq(struct ib_cq *cq, int num_entries,
3491 			     struct ib_wc *wc)
3492 {
3493 	return cq->device->poll_cq(cq, num_entries, wc);
3494 }
3495 
3496 /**
3497  * ib_peek_cq - Returns the number of unreaped completions currently
3498  *   on the specified CQ.
3499  * @cq: The CQ to peek.
3500  * @wc_cnt: A minimum number of unreaped completions to check for.
3501  *
3502  * If the number of unreaped completions is greater than or equal to wc_cnt,
3503  * this function returns wc_cnt, otherwise, it returns the actual number of
3504  * unreaped completions.
3505  */
3506 int ib_peek_cq(struct ib_cq *cq, int wc_cnt);
3507 
3508 /**
3509  * ib_req_notify_cq - Request completion notification on a CQ.
3510  * @cq: The CQ to generate an event for.
3511  * @flags:
3512  *   Must contain exactly one of %IB_CQ_SOLICITED or %IB_CQ_NEXT_COMP
3513  *   to request an event on the next solicited event or next work
3514  *   completion at any type, respectively. %IB_CQ_REPORT_MISSED_EVENTS
3515  *   may also be |ed in to request a hint about missed events, as
3516  *   described below.
3517  *
3518  * Return Value:
3519  *    < 0 means an error occurred while requesting notification
3520  *   == 0 means notification was requested successfully, and if
3521  *        IB_CQ_REPORT_MISSED_EVENTS was passed in, then no events
3522  *        were missed and it is safe to wait for another event.  In
3523  *        this case is it guaranteed that any work completions added
3524  *        to the CQ since the last CQ poll will trigger a completion
3525  *        notification event.
3526  *    > 0 is only returned if IB_CQ_REPORT_MISSED_EVENTS was passed
3527  *        in.  It means that the consumer must poll the CQ again to
3528  *        make sure it is empty to avoid missing an event because of a
3529  *        race between requesting notification and an entry being
3530  *        added to the CQ.  This return value means it is possible
3531  *        (but not guaranteed) that a work completion has been added
3532  *        to the CQ since the last poll without triggering a
3533  *        completion notification event.
3534  */
3535 static inline int ib_req_notify_cq(struct ib_cq *cq,
3536 				   enum ib_cq_notify_flags flags)
3537 {
3538 	return cq->device->req_notify_cq(cq, flags);
3539 }
3540 
3541 /**
3542  * ib_req_ncomp_notif - Request completion notification when there are
3543  *   at least the specified number of unreaped completions on the CQ.
3544  * @cq: The CQ to generate an event for.
3545  * @wc_cnt: The number of unreaped completions that should be on the
3546  *   CQ before an event is generated.
3547  */
3548 static inline int ib_req_ncomp_notif(struct ib_cq *cq, int wc_cnt)
3549 {
3550 	return cq->device->req_ncomp_notif ?
3551 		cq->device->req_ncomp_notif(cq, wc_cnt) :
3552 		-ENOSYS;
3553 }
3554 
3555 /**
3556  * ib_dma_mapping_error - check a DMA addr for error
3557  * @dev: The device for which the dma_addr was created
3558  * @dma_addr: The DMA address to check
3559  */
3560 static inline int ib_dma_mapping_error(struct ib_device *dev, u64 dma_addr)
3561 {
3562 	return dma_mapping_error(dev->dma_device, dma_addr);
3563 }
3564 
3565 /**
3566  * ib_dma_map_single - Map a kernel virtual address to DMA address
3567  * @dev: The device for which the dma_addr is to be created
3568  * @cpu_addr: The kernel virtual address
3569  * @size: The size of the region in bytes
3570  * @direction: The direction of the DMA
3571  */
3572 static inline u64 ib_dma_map_single(struct ib_device *dev,
3573 				    void *cpu_addr, size_t size,
3574 				    enum dma_data_direction direction)
3575 {
3576 	return dma_map_single(dev->dma_device, cpu_addr, size, direction);
3577 }
3578 
3579 /**
3580  * ib_dma_unmap_single - Destroy a mapping created by ib_dma_map_single()
3581  * @dev: The device for which the DMA address was created
3582  * @addr: The DMA address
3583  * @size: The size of the region in bytes
3584  * @direction: The direction of the DMA
3585  */
3586 static inline void ib_dma_unmap_single(struct ib_device *dev,
3587 				       u64 addr, size_t size,
3588 				       enum dma_data_direction direction)
3589 {
3590 	dma_unmap_single(dev->dma_device, addr, size, direction);
3591 }
3592 
3593 /**
3594  * ib_dma_map_page - Map a physical page to DMA address
3595  * @dev: The device for which the dma_addr is to be created
3596  * @page: The page to be mapped
3597  * @offset: The offset within the page
3598  * @size: The size of the region in bytes
3599  * @direction: The direction of the DMA
3600  */
3601 static inline u64 ib_dma_map_page(struct ib_device *dev,
3602 				  struct page *page,
3603 				  unsigned long offset,
3604 				  size_t size,
3605 					 enum dma_data_direction direction)
3606 {
3607 	return dma_map_page(dev->dma_device, page, offset, size, direction);
3608 }
3609 
3610 /**
3611  * ib_dma_unmap_page - Destroy a mapping created by ib_dma_map_page()
3612  * @dev: The device for which the DMA address was created
3613  * @addr: The DMA address
3614  * @size: The size of the region in bytes
3615  * @direction: The direction of the DMA
3616  */
3617 static inline void ib_dma_unmap_page(struct ib_device *dev,
3618 				     u64 addr, size_t size,
3619 				     enum dma_data_direction direction)
3620 {
3621 	dma_unmap_page(dev->dma_device, addr, size, direction);
3622 }
3623 
3624 /**
3625  * ib_dma_map_sg - Map a scatter/gather list to DMA addresses
3626  * @dev: The device for which the DMA addresses are to be created
3627  * @sg: The array of scatter/gather entries
3628  * @nents: The number of scatter/gather entries
3629  * @direction: The direction of the DMA
3630  */
3631 static inline int ib_dma_map_sg(struct ib_device *dev,
3632 				struct scatterlist *sg, int nents,
3633 				enum dma_data_direction direction)
3634 {
3635 	return dma_map_sg(dev->dma_device, sg, nents, direction);
3636 }
3637 
3638 /**
3639  * ib_dma_unmap_sg - Unmap a scatter/gather list of DMA addresses
3640  * @dev: The device for which the DMA addresses were created
3641  * @sg: The array of scatter/gather entries
3642  * @nents: The number of scatter/gather entries
3643  * @direction: The direction of the DMA
3644  */
3645 static inline void ib_dma_unmap_sg(struct ib_device *dev,
3646 				   struct scatterlist *sg, int nents,
3647 				   enum dma_data_direction direction)
3648 {
3649 	dma_unmap_sg(dev->dma_device, sg, nents, direction);
3650 }
3651 
3652 static inline int ib_dma_map_sg_attrs(struct ib_device *dev,
3653 				      struct scatterlist *sg, int nents,
3654 				      enum dma_data_direction direction,
3655 				      unsigned long dma_attrs)
3656 {
3657 	return dma_map_sg_attrs(dev->dma_device, sg, nents, direction,
3658 				dma_attrs);
3659 }
3660 
3661 static inline void ib_dma_unmap_sg_attrs(struct ib_device *dev,
3662 					 struct scatterlist *sg, int nents,
3663 					 enum dma_data_direction direction,
3664 					 unsigned long dma_attrs)
3665 {
3666 	dma_unmap_sg_attrs(dev->dma_device, sg, nents, direction, dma_attrs);
3667 }
3668 /**
3669  * ib_sg_dma_address - Return the DMA address from a scatter/gather entry
3670  * @dev: The device for which the DMA addresses were created
3671  * @sg: The scatter/gather entry
3672  *
3673  * Note: this function is obsolete. To do: change all occurrences of
3674  * ib_sg_dma_address() into sg_dma_address().
3675  */
3676 static inline u64 ib_sg_dma_address(struct ib_device *dev,
3677 				    struct scatterlist *sg)
3678 {
3679 	return sg_dma_address(sg);
3680 }
3681 
3682 /**
3683  * ib_sg_dma_len - Return the DMA length from a scatter/gather entry
3684  * @dev: The device for which the DMA addresses were created
3685  * @sg: The scatter/gather entry
3686  *
3687  * Note: this function is obsolete. To do: change all occurrences of
3688  * ib_sg_dma_len() into sg_dma_len().
3689  */
3690 static inline unsigned int ib_sg_dma_len(struct ib_device *dev,
3691 					 struct scatterlist *sg)
3692 {
3693 	return sg_dma_len(sg);
3694 }
3695 
3696 /**
3697  * ib_dma_sync_single_for_cpu - Prepare DMA region to be accessed by CPU
3698  * @dev: The device for which the DMA address was created
3699  * @addr: The DMA address
3700  * @size: The size of the region in bytes
3701  * @dir: The direction of the DMA
3702  */
3703 static inline void ib_dma_sync_single_for_cpu(struct ib_device *dev,
3704 					      u64 addr,
3705 					      size_t size,
3706 					      enum dma_data_direction dir)
3707 {
3708 	dma_sync_single_for_cpu(dev->dma_device, addr, size, dir);
3709 }
3710 
3711 /**
3712  * ib_dma_sync_single_for_device - Prepare DMA region to be accessed by device
3713  * @dev: The device for which the DMA address was created
3714  * @addr: The DMA address
3715  * @size: The size of the region in bytes
3716  * @dir: The direction of the DMA
3717  */
3718 static inline void ib_dma_sync_single_for_device(struct ib_device *dev,
3719 						 u64 addr,
3720 						 size_t size,
3721 						 enum dma_data_direction dir)
3722 {
3723 	dma_sync_single_for_device(dev->dma_device, addr, size, dir);
3724 }
3725 
3726 /**
3727  * ib_dma_alloc_coherent - Allocate memory and map it for DMA
3728  * @dev: The device for which the DMA address is requested
3729  * @size: The size of the region to allocate in bytes
3730  * @dma_handle: A pointer for returning the DMA address of the region
3731  * @flag: memory allocator flags
3732  */
3733 static inline void *ib_dma_alloc_coherent(struct ib_device *dev,
3734 					   size_t size,
3735 					   dma_addr_t *dma_handle,
3736 					   gfp_t flag)
3737 {
3738 	return dma_alloc_coherent(dev->dma_device, size, dma_handle, flag);
3739 }
3740 
3741 /**
3742  * ib_dma_free_coherent - Free memory allocated by ib_dma_alloc_coherent()
3743  * @dev: The device for which the DMA addresses were allocated
3744  * @size: The size of the region
3745  * @cpu_addr: the address returned by ib_dma_alloc_coherent()
3746  * @dma_handle: the DMA address returned by ib_dma_alloc_coherent()
3747  */
3748 static inline void ib_dma_free_coherent(struct ib_device *dev,
3749 					size_t size, void *cpu_addr,
3750 					dma_addr_t dma_handle)
3751 {
3752 	dma_free_coherent(dev->dma_device, size, cpu_addr, dma_handle);
3753 }
3754 
3755 /**
3756  * ib_dereg_mr - Deregisters a memory region and removes it from the
3757  *   HCA translation table.
3758  * @mr: The memory region to deregister.
3759  *
3760  * This function can fail, if the memory region has memory windows bound to it.
3761  */
3762 int ib_dereg_mr_user(struct ib_mr *mr, struct ib_udata *udata);
3763 
3764 /**
3765  * ib_dereg_mr - Deregisters a kernel memory region and removes it from the
3766  *   HCA translation table.
3767  * @mr: The memory region to deregister.
3768  *
3769  * This function can fail, if the memory region has memory windows bound to it.
3770  *
3771  * NOTE: for user mr use ib_dereg_mr_user with valid udata!
3772  */
3773 static inline int ib_dereg_mr(struct ib_mr *mr)
3774 {
3775 	return ib_dereg_mr_user(mr, NULL);
3776 }
3777 
3778 struct ib_mr *ib_alloc_mr_user(struct ib_pd *pd, enum ib_mr_type mr_type,
3779 			       u32 max_num_sg, struct ib_udata *udata);
3780 
3781 static inline struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
3782 					enum ib_mr_type mr_type, u32 max_num_sg)
3783 {
3784 	return ib_alloc_mr_user(pd, mr_type, max_num_sg, NULL);
3785 }
3786 
3787 struct ib_mr *ib_alloc_mr_integrity(struct ib_pd *pd,
3788 				    u32 max_num_data_sg,
3789 				    u32 max_num_meta_sg);
3790 
3791 /**
3792  * ib_update_fast_reg_key - updates the key portion of the fast_reg MR
3793  *   R_Key and L_Key.
3794  * @mr - struct ib_mr pointer to be updated.
3795  * @newkey - new key to be used.
3796  */
3797 static inline void ib_update_fast_reg_key(struct ib_mr *mr, u8 newkey)
3798 {
3799 	mr->lkey = (mr->lkey & 0xffffff00) | newkey;
3800 	mr->rkey = (mr->rkey & 0xffffff00) | newkey;
3801 }
3802 
3803 /**
3804  * ib_inc_rkey - increments the key portion of the given rkey. Can be used
3805  * for calculating a new rkey for type 2 memory windows.
3806  * @rkey - the rkey to increment.
3807  */
3808 static inline u32 ib_inc_rkey(u32 rkey)
3809 {
3810 	const u32 mask = 0x000000ff;
3811 	return ((rkey + 1) & mask) | (rkey & ~mask);
3812 }
3813 
3814 /**
3815  * ib_alloc_fmr - Allocates a unmapped fast memory region.
3816  * @pd: The protection domain associated with the unmapped region.
3817  * @mr_access_flags: Specifies the memory access rights.
3818  * @fmr_attr: Attributes of the unmapped region.
3819  *
3820  * A fast memory region must be mapped before it can be used as part of
3821  * a work request.
3822  */
3823 struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
3824 			    int mr_access_flags,
3825 			    struct ib_fmr_attr *fmr_attr);
3826 
3827 /**
3828  * ib_map_phys_fmr - Maps a list of physical pages to a fast memory region.
3829  * @fmr: The fast memory region to associate with the pages.
3830  * @page_list: An array of physical pages to map to the fast memory region.
3831  * @list_len: The number of pages in page_list.
3832  * @iova: The I/O virtual address to use with the mapped region.
3833  */
3834 static inline int ib_map_phys_fmr(struct ib_fmr *fmr,
3835 				  u64 *page_list, int list_len,
3836 				  u64 iova)
3837 {
3838 	return fmr->device->map_phys_fmr(fmr, page_list, list_len, iova);
3839 }
3840 
3841 /**
3842  * ib_unmap_fmr - Removes the mapping from a list of fast memory regions.
3843  * @fmr_list: A linked list of fast memory regions to unmap.
3844  */
3845 int ib_unmap_fmr(struct list_head *fmr_list);
3846 
3847 /**
3848  * ib_dealloc_fmr - Deallocates a fast memory region.
3849  * @fmr: The fast memory region to deallocate.
3850  */
3851 int ib_dealloc_fmr(struct ib_fmr *fmr);
3852 
3853 /**
3854  * ib_attach_mcast - Attaches the specified QP to a multicast group.
3855  * @qp: QP to attach to the multicast group.  The QP must be type
3856  *   IB_QPT_UD.
3857  * @gid: Multicast group GID.
3858  * @lid: Multicast group LID in host byte order.
3859  *
3860  * In order to send and receive multicast packets, subnet
3861  * administration must have created the multicast group and configured
3862  * the fabric appropriately.  The port associated with the specified
3863  * QP must also be a member of the multicast group.
3864  */
3865 int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid);
3866 
3867 /**
3868  * ib_detach_mcast - Detaches the specified QP from a multicast group.
3869  * @qp: QP to detach from the multicast group.
3870  * @gid: Multicast group GID.
3871  * @lid: Multicast group LID in host byte order.
3872  */
3873 int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid);
3874 
3875 /**
3876  * ib_alloc_xrcd - Allocates an XRC domain.
3877  * @device: The device on which to allocate the XRC domain.
3878  * @caller: Module name for kernel consumers
3879  */
3880 struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller);
3881 #define ib_alloc_xrcd(device) \
3882 	__ib_alloc_xrcd((device), "ibcore")
3883 
3884 /**
3885  * ib_dealloc_xrcd - Deallocates an XRC domain.
3886  * @xrcd: The XRC domain to deallocate.
3887  * @udata: Valid user data or NULL for kernel object
3888  */
3889 int ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata);
3890 
3891 static inline int ib_check_mr_access(int flags)
3892 {
3893 	/*
3894 	 * Local write permission is required if remote write or
3895 	 * remote atomic permission is also requested.
3896 	 */
3897 	if (flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
3898 	    !(flags & IB_ACCESS_LOCAL_WRITE))
3899 		return -EINVAL;
3900 
3901 	if (flags & ~IB_ACCESS_SUPPORTED)
3902 		return -EINVAL;
3903 
3904 	return 0;
3905 }
3906 
3907 static inline bool ib_access_writable(int access_flags)
3908 {
3909 	/*
3910 	 * We have writable memory backing the MR if any of the following
3911 	 * access flags are set.  "Local write" and "remote write" obviously
3912 	 * require write access.  "Remote atomic" can do things like fetch and
3913 	 * add, which will modify memory, and "MW bind" can change permissions
3914 	 * by binding a window.
3915 	 */
3916 	return access_flags &
3917 		(IB_ACCESS_LOCAL_WRITE   | IB_ACCESS_REMOTE_WRITE |
3918 		 IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_MW_BIND);
3919 }
3920 
3921 /**
3922  * ib_check_mr_status: lightweight check of MR status.
3923  *     This routine may provide status checks on a selected
3924  *     ib_mr. first use is for signature status check.
3925  *
3926  * @mr: A memory region.
3927  * @check_mask: Bitmask of which checks to perform from
3928  *     ib_mr_status_check enumeration.
3929  * @mr_status: The container of relevant status checks.
3930  *     failed checks will be indicated in the status bitmask
3931  *     and the relevant info shall be in the error item.
3932  */
3933 int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
3934 		       struct ib_mr_status *mr_status);
3935 
3936 if_t ib_get_net_dev_by_params(struct ib_device *dev, u8 port,
3937 					    u16 pkey, const union ib_gid *gid,
3938 					    const struct sockaddr *addr);
3939 struct ib_wq *ib_create_wq(struct ib_pd *pd,
3940 			   struct ib_wq_init_attr *init_attr);
3941 int ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata);
3942 int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *attr,
3943 		 u32 wq_attr_mask);
3944 struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
3945 						 struct ib_rwq_ind_table_init_attr*
3946 						 wq_ind_table_init_attr);
3947 int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *wq_ind_table);
3948 
3949 int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
3950 		 unsigned int *sg_offset, unsigned int page_size);
3951 
3952 static inline int
3953 ib_map_mr_sg_zbva(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
3954 		  unsigned int *sg_offset, unsigned int page_size)
3955 {
3956 	int n;
3957 
3958 	n = ib_map_mr_sg(mr, sg, sg_nents, sg_offset, page_size);
3959 	mr->iova = 0;
3960 
3961 	return n;
3962 }
3963 
3964 int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
3965 		unsigned int *sg_offset, int (*set_page)(struct ib_mr *, u64));
3966 
3967 void ib_drain_rq(struct ib_qp *qp);
3968 void ib_drain_sq(struct ib_qp *qp);
3969 void ib_drain_qp(struct ib_qp *qp);
3970 
3971 struct ib_ucontext *ib_uverbs_get_ucontext_file(struct ib_uverbs_file *ufile);
3972 
3973 int uverbs_destroy_def_handler(struct uverbs_attr_bundle *attrs);
3974 
3975 static inline u8 *rdma_ah_retrieve_dmac(struct rdma_ah_attr *attr)
3976 {
3977 	if (attr->type == RDMA_AH_ATTR_TYPE_ROCE)
3978 		return attr->roce.dmac;
3979 	return NULL;
3980 }
3981 
3982 static inline void rdma_ah_set_dlid(struct rdma_ah_attr *attr, u32 dlid)
3983 {
3984 	if (attr->type == RDMA_AH_ATTR_TYPE_IB)
3985 		attr->ib.dlid = (u16)dlid;
3986 	else if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
3987 		attr->opa.dlid = dlid;
3988 }
3989 
3990 static inline u32 rdma_ah_get_dlid(const struct rdma_ah_attr *attr)
3991 {
3992 	if (attr->type == RDMA_AH_ATTR_TYPE_IB)
3993 		return attr->ib.dlid;
3994 	else if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
3995 		return attr->opa.dlid;
3996 	return 0;
3997 }
3998 
3999 static inline void rdma_ah_set_sl(struct rdma_ah_attr *attr, u8 sl)
4000 {
4001 	attr->sl = sl;
4002 }
4003 
4004 static inline u8 rdma_ah_get_sl(const struct rdma_ah_attr *attr)
4005 {
4006 	return attr->sl;
4007 }
4008 
4009 static inline void rdma_ah_set_path_bits(struct rdma_ah_attr *attr,
4010 					 u8 src_path_bits)
4011 {
4012 	if (attr->type == RDMA_AH_ATTR_TYPE_IB)
4013 		attr->ib.src_path_bits = src_path_bits;
4014 	else if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
4015 		attr->opa.src_path_bits = src_path_bits;
4016 }
4017 
4018 static inline u8 rdma_ah_get_path_bits(const struct rdma_ah_attr *attr)
4019 {
4020 	if (attr->type == RDMA_AH_ATTR_TYPE_IB)
4021 		return attr->ib.src_path_bits;
4022 	else if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
4023 		return attr->opa.src_path_bits;
4024 	return 0;
4025 }
4026 
4027 static inline void rdma_ah_set_make_grd(struct rdma_ah_attr *attr,
4028 					bool make_grd)
4029 {
4030 	if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
4031 		attr->opa.make_grd = make_grd;
4032 }
4033 
4034 static inline bool rdma_ah_get_make_grd(const struct rdma_ah_attr *attr)
4035 {
4036 	if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
4037 		return attr->opa.make_grd;
4038 	return false;
4039 }
4040 
4041 static inline void rdma_ah_set_port_num(struct rdma_ah_attr *attr, u8 port_num)
4042 {
4043 	attr->port_num = port_num;
4044 }
4045 
4046 static inline u8 rdma_ah_get_port_num(const struct rdma_ah_attr *attr)
4047 {
4048 	return attr->port_num;
4049 }
4050 
4051 static inline void rdma_ah_set_static_rate(struct rdma_ah_attr *attr,
4052 					   u8 static_rate)
4053 {
4054 	attr->static_rate = static_rate;
4055 }
4056 
4057 static inline u8 rdma_ah_get_static_rate(const struct rdma_ah_attr *attr)
4058 {
4059 	return attr->static_rate;
4060 }
4061 
4062 static inline void rdma_ah_set_ah_flags(struct rdma_ah_attr *attr,
4063 					enum ib_ah_flags flag)
4064 {
4065 	attr->ah_flags = flag;
4066 }
4067 
4068 static inline enum ib_ah_flags
4069 		rdma_ah_get_ah_flags(const struct rdma_ah_attr *attr)
4070 {
4071 	return attr->ah_flags;
4072 }
4073 
4074 static inline const struct ib_global_route
4075 		*rdma_ah_read_grh(const struct rdma_ah_attr *attr)
4076 {
4077 	return &attr->grh;
4078 }
4079 
4080 /*To retrieve and modify the grh */
4081 static inline struct ib_global_route
4082 		*rdma_ah_retrieve_grh(struct rdma_ah_attr *attr)
4083 {
4084 	return &attr->grh;
4085 }
4086 
4087 static inline void rdma_ah_set_dgid_raw(struct rdma_ah_attr *attr, void *dgid)
4088 {
4089 	struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
4090 
4091 	memcpy(grh->dgid.raw, dgid, sizeof(grh->dgid));
4092 }
4093 
4094 static inline void rdma_ah_set_subnet_prefix(struct rdma_ah_attr *attr,
4095 					     __be64 prefix)
4096 {
4097 	struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
4098 
4099 	grh->dgid.global.subnet_prefix = prefix;
4100 }
4101 
4102 static inline void rdma_ah_set_interface_id(struct rdma_ah_attr *attr,
4103 					    __be64 if_id)
4104 {
4105 	struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
4106 
4107 	grh->dgid.global.interface_id = if_id;
4108 }
4109 
4110 static inline void rdma_ah_set_grh(struct rdma_ah_attr *attr,
4111 				   union ib_gid *dgid, u32 flow_label,
4112 				   u8 sgid_index, u8 hop_limit,
4113 				   u8 traffic_class)
4114 {
4115 	struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
4116 
4117 	attr->ah_flags = IB_AH_GRH;
4118 	if (dgid)
4119 		grh->dgid = *dgid;
4120 	grh->flow_label = flow_label;
4121 	grh->sgid_index = sgid_index;
4122 	grh->hop_limit = hop_limit;
4123 	grh->traffic_class = traffic_class;
4124 	grh->sgid_attr = NULL;
4125 }
4126 
4127 void rdma_destroy_ah_attr(struct rdma_ah_attr *ah_attr);
4128 void rdma_move_grh_sgid_attr(struct rdma_ah_attr *attr, union ib_gid *dgid,
4129 			     u32 flow_label, u8 hop_limit, u8 traffic_class,
4130 			     const struct ib_gid_attr *sgid_attr);
4131 void rdma_copy_ah_attr(struct rdma_ah_attr *dest,
4132 		       const struct rdma_ah_attr *src);
4133 void rdma_replace_ah_attr(struct rdma_ah_attr *old,
4134 			  const struct rdma_ah_attr *new);
4135 void rdma_move_ah_attr(struct rdma_ah_attr *dest, struct rdma_ah_attr *src);
4136 
4137 /**
4138  * rdma_ah_find_type - Return address handle type.
4139  *
4140  * @dev: Device to be checked
4141  * @port_num: Port number
4142  */
4143 static inline enum rdma_ah_attr_type rdma_ah_find_type(struct ib_device *dev,
4144 						       u8 port_num)
4145 {
4146 	if (rdma_protocol_roce(dev, port_num))
4147 		return RDMA_AH_ATTR_TYPE_ROCE;
4148 	if (rdma_protocol_ib(dev, port_num)) {
4149 		if (rdma_cap_opa_ah(dev, port_num))
4150 			return RDMA_AH_ATTR_TYPE_OPA;
4151 		return RDMA_AH_ATTR_TYPE_IB;
4152 	}
4153 
4154 	return RDMA_AH_ATTR_TYPE_UNDEFINED;
4155 }
4156 
4157 /**
4158  * ib_lid_cpu16 - Return lid in 16bit CPU encoding.
4159  *     In the current implementation the only way to get
4160  *     get the 32bit lid is from other sources for OPA.
4161  *     For IB, lids will always be 16bits so cast the
4162  *     value accordingly.
4163  *
4164  * @lid: A 32bit LID
4165  */
4166 static inline u16 ib_lid_cpu16(u32 lid)
4167 {
4168 	WARN_ON_ONCE(lid & 0xFFFF0000);
4169 	return (u16)lid;
4170 }
4171 
4172 /**
4173  * ib_lid_be16 - Return lid in 16bit BE encoding.
4174  *
4175  * @lid: A 32bit LID
4176  */
4177 static inline __be16 ib_lid_be16(u32 lid)
4178 {
4179 	WARN_ON_ONCE(lid & 0xFFFF0000);
4180 	return cpu_to_be16((u16)lid);
4181 }
4182 
4183 #define IB_ROCE_UDP_ENCAP_VALID_PORT_MIN (0xC000)
4184 #define IB_GRH_FLOWLABEL_MASK (0x000FFFFF)
4185 
4186 /**
4187  * rdma_flow_label_to_udp_sport - generate a RoCE v2 UDP src port value based
4188  *                               on the flow_label
4189  *
4190  * This function will convert the 20 bit flow_label input to a valid RoCE v2
4191  * UDP src port 14 bit value. All RoCE V2 drivers should use this same
4192  * convention.
4193  */
4194 static inline u16 rdma_flow_label_to_udp_sport(u32 fl)
4195 {
4196 	u32 fl_low = fl & 0x03fff, fl_high = fl & 0xFC000;
4197 
4198 	fl_low ^= fl_high >> 14;
4199 	return (u16)(fl_low | IB_ROCE_UDP_ENCAP_VALID_PORT_MIN);
4200 }
4201 
4202 /**
4203  * rdma_calc_flow_label - generate a RDMA symmetric flow label value based on
4204  *                        local and remote qpn values
4205  *
4206  * This function folded the multiplication results of two qpns, 24 bit each,
4207  * fields, and converts it to a 20 bit results.
4208  *
4209  * This function will create symmetric flow_label value based on the local
4210  * and remote qpn values. this will allow both the requester and responder
4211  * to calculate the same flow_label for a given connection.
4212  *
4213  * This helper function should be used by driver in case the upper layer
4214  * provide a zero flow_label value. This is to improve entropy of RDMA
4215  * traffic in the network.
4216  */
4217 static inline u32 rdma_calc_flow_label(u32 lqpn, u32 rqpn)
4218 {
4219 	u64 v = (u64)lqpn * rqpn;
4220 
4221 	v ^= v >> 20;
4222 	v ^= v >> 40;
4223 
4224 	return (u32)(v & IB_GRH_FLOWLABEL_MASK);
4225 }
4226 
4227 /**
4228  * rdma_get_udp_sport - Calculate and set UDP source port based on the flow
4229  *                      label. If flow label is not defined in GRH then
4230  *                      calculate it based on lqpn/rqpn.
4231  *
4232  * @fl:                 flow label from GRH
4233  * @lqpn:               local qp number
4234  * @rqpn:               remote qp number
4235  */
4236 static inline u16 rdma_get_udp_sport(u32 fl, u32 lqpn, u32 rqpn)
4237 {
4238 	if (!fl)
4239 		fl = rdma_calc_flow_label(lqpn, rqpn);
4240 
4241 	return rdma_flow_label_to_udp_sport(fl);
4242 }
4243 #endif /* IB_VERBS_H */
4244