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