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