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