xref: /linux/drivers/infiniband/hw/mlx5/mlx5_ib.h (revision 7f4f3b14e8079ecde096bd734af10e30d40c27b7)
1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /*
3  * Copyright (c) 2013-2020, Mellanox Technologies inc. All rights reserved.
4  * Copyright (c) 2020, Intel Corporation. All rights reserved.
5  */
6 
7 #ifndef MLX5_IB_H
8 #define MLX5_IB_H
9 
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <rdma/ib_verbs.h>
13 #include <rdma/ib_umem.h>
14 #include <rdma/ib_smi.h>
15 #include <linux/mlx5/driver.h>
16 #include <linux/mlx5/cq.h>
17 #include <linux/mlx5/fs.h>
18 #include <linux/mlx5/qp.h>
19 #include <linux/types.h>
20 #include <linux/mlx5/transobj.h>
21 #include <rdma/ib_user_verbs.h>
22 #include <rdma/mlx5-abi.h>
23 #include <rdma/uverbs_ioctl.h>
24 #include <rdma/mlx5_user_ioctl_cmds.h>
25 #include <rdma/mlx5_user_ioctl_verbs.h>
26 
27 #include "srq.h"
28 #include "qp.h"
29 #include "macsec.h"
30 
31 #define mlx5_ib_dbg(_dev, format, arg...)                                      \
32 	dev_dbg(&(_dev)->ib_dev.dev, "%s:%d:(pid %d): " format, __func__,      \
33 		__LINE__, current->pid, ##arg)
34 
35 #define mlx5_ib_err(_dev, format, arg...)                                      \
36 	dev_err(&(_dev)->ib_dev.dev, "%s:%d:(pid %d): " format, __func__,      \
37 		__LINE__, current->pid, ##arg)
38 
39 #define mlx5_ib_warn(_dev, format, arg...)                                     \
40 	dev_warn(&(_dev)->ib_dev.dev, "%s:%d:(pid %d): " format, __func__,     \
41 		 __LINE__, current->pid, ##arg)
42 
43 #define mlx5_ib_log(lvl, _dev, format, arg...)                                 \
44 	dev_printk(lvl, &(_dev)->ib_dev.dev,  "%s:%d:(pid %d): " format,       \
45 		   __func__, __LINE__, current->pid, ##arg)
46 
47 #define MLX5_IB_DEFAULT_UIDX 0xffffff
48 #define MLX5_USER_ASSIGNED_UIDX_MASK __mlx5_mask(qpc, user_index)
49 
50 static __always_inline unsigned long
51 __mlx5_log_page_size_to_bitmap(unsigned int log_pgsz_bits,
52 			       unsigned int pgsz_shift)
53 {
54 	unsigned int largest_pg_shift =
55 		min_t(unsigned long, (1ULL << log_pgsz_bits) - 1 + pgsz_shift,
56 		      BITS_PER_LONG - 1);
57 
58 	/*
59 	 * Despite a command allowing it, the device does not support lower than
60 	 * 4k page size.
61 	 */
62 	pgsz_shift = max_t(unsigned int, MLX5_ADAPTER_PAGE_SHIFT, pgsz_shift);
63 	return GENMASK(largest_pg_shift, pgsz_shift);
64 }
65 
66 static __always_inline unsigned long
67 __mlx5_page_offset_to_bitmask(unsigned int page_offset_bits,
68 			      unsigned int offset_shift)
69 {
70 	unsigned int largest_offset_shift =
71 		min_t(unsigned long, page_offset_bits - 1 + offset_shift,
72 		      BITS_PER_LONG - 1);
73 
74 	return GENMASK(largest_offset_shift, offset_shift);
75 }
76 
77 /*
78  * QP/CQ/WQ/etc type commands take a page offset that satisifies:
79  *   page_offset_quantized * (page_size/scale) = page_offset
80  * Which restricts allowed page sizes to ones that satisify the above.
81  */
82 unsigned long __mlx5_umem_find_best_quantized_pgoff(
83 	struct ib_umem *umem, unsigned long pgsz_bitmap,
84 	unsigned int page_offset_bits, u64 pgoff_bitmask, unsigned int scale,
85 	unsigned int *page_offset_quantized);
86 #define mlx5_umem_find_best_quantized_pgoff(umem, typ, log_pgsz_fld,           \
87 					    pgsz_shift, page_offset_fld,       \
88 					    scale, page_offset_quantized)      \
89 	__mlx5_umem_find_best_quantized_pgoff(                                 \
90 		umem,                                                          \
91 		__mlx5_log_page_size_to_bitmap(                                \
92 			__mlx5_bit_sz(typ, log_pgsz_fld), pgsz_shift),         \
93 		__mlx5_bit_sz(typ, page_offset_fld),                           \
94 		GENMASK(31, order_base_2(scale)), scale,                       \
95 		page_offset_quantized)
96 
97 #define mlx5_umem_find_best_cq_quantized_pgoff(umem, typ, log_pgsz_fld,        \
98 					       pgsz_shift, page_offset_fld,    \
99 					       scale, page_offset_quantized)   \
100 	__mlx5_umem_find_best_quantized_pgoff(                                 \
101 		umem,                                                          \
102 		__mlx5_log_page_size_to_bitmap(                                \
103 			__mlx5_bit_sz(typ, log_pgsz_fld), pgsz_shift),         \
104 		__mlx5_bit_sz(typ, page_offset_fld), 0, scale,                 \
105 		page_offset_quantized)
106 
107 static inline unsigned long
108 mlx5_umem_dmabuf_find_best_pgsz(struct ib_umem_dmabuf *umem_dmabuf)
109 {
110 	/*
111 	 * mkeys used for dmabuf are fixed at PAGE_SIZE because we must be able
112 	 * to hold any sgl after a move operation. Ideally the mkc page size
113 	 * could be changed at runtime to be optimal, but right now the driver
114 	 * cannot do that.
115 	 */
116 	return ib_umem_find_best_pgsz(&umem_dmabuf->umem, PAGE_SIZE,
117 				      umem_dmabuf->umem.iova);
118 }
119 
120 enum {
121 	MLX5_IB_MMAP_OFFSET_START = 9,
122 	MLX5_IB_MMAP_OFFSET_END = 255,
123 };
124 
125 enum {
126 	MLX5_IB_MMAP_CMD_SHIFT	= 8,
127 	MLX5_IB_MMAP_CMD_MASK	= 0xff,
128 };
129 
130 enum {
131 	MLX5_RES_SCAT_DATA32_CQE	= 0x1,
132 	MLX5_RES_SCAT_DATA64_CQE	= 0x2,
133 	MLX5_REQ_SCAT_DATA32_CQE	= 0x11,
134 	MLX5_REQ_SCAT_DATA64_CQE	= 0x22,
135 };
136 
137 enum mlx5_ib_mad_ifc_flags {
138 	MLX5_MAD_IFC_IGNORE_MKEY	= 1,
139 	MLX5_MAD_IFC_IGNORE_BKEY	= 2,
140 	MLX5_MAD_IFC_NET_VIEW		= 4,
141 };
142 
143 enum {
144 	MLX5_CROSS_CHANNEL_BFREG         = 0,
145 };
146 
147 enum {
148 	MLX5_CQE_VERSION_V0,
149 	MLX5_CQE_VERSION_V1,
150 };
151 
152 enum {
153 	MLX5_TM_MAX_RNDV_MSG_SIZE	= 64,
154 	MLX5_TM_MAX_SGE			= 1,
155 };
156 
157 enum {
158 	MLX5_IB_INVALID_UAR_INDEX	= BIT(31),
159 	MLX5_IB_INVALID_BFREG		= BIT(31),
160 };
161 
162 enum {
163 	MLX5_MAX_MEMIC_PAGES = 0x100,
164 	MLX5_MEMIC_ALLOC_SIZE_MASK = 0x3f,
165 };
166 
167 enum {
168 	MLX5_MEMIC_BASE_ALIGN	= 6,
169 	MLX5_MEMIC_BASE_SIZE	= 1 << MLX5_MEMIC_BASE_ALIGN,
170 };
171 
172 enum mlx5_ib_mmap_type {
173 	MLX5_IB_MMAP_TYPE_MEMIC = 1,
174 	MLX5_IB_MMAP_TYPE_VAR = 2,
175 	MLX5_IB_MMAP_TYPE_UAR_WC = 3,
176 	MLX5_IB_MMAP_TYPE_UAR_NC = 4,
177 	MLX5_IB_MMAP_TYPE_MEMIC_OP = 5,
178 };
179 
180 struct mlx5_bfreg_info {
181 	u32 *sys_pages;
182 	int num_low_latency_bfregs;
183 	unsigned int *count;
184 
185 	/*
186 	 * protect bfreg allocation data structs
187 	 */
188 	struct mutex lock;
189 	u32 ver;
190 	u8 lib_uar_4k : 1;
191 	u8 lib_uar_dyn : 1;
192 	u32 num_sys_pages;
193 	u32 num_static_sys_pages;
194 	u32 total_num_bfregs;
195 	u32 num_dyn_bfregs;
196 };
197 
198 struct mlx5_ib_ucontext {
199 	struct ib_ucontext	ibucontext;
200 	struct list_head	db_page_list;
201 
202 	/* protect doorbell record alloc/free
203 	 */
204 	struct mutex		db_page_mutex;
205 	struct mlx5_bfreg_info	bfregi;
206 	u8			cqe_version;
207 	/* Transport Domain number */
208 	u32			tdn;
209 
210 	u64			lib_caps;
211 	u16			devx_uid;
212 	/* For RoCE LAG TX affinity */
213 	atomic_t		tx_port_affinity;
214 };
215 
216 static inline struct mlx5_ib_ucontext *to_mucontext(struct ib_ucontext *ibucontext)
217 {
218 	return container_of(ibucontext, struct mlx5_ib_ucontext, ibucontext);
219 }
220 
221 struct mlx5_ib_pd {
222 	struct ib_pd		ibpd;
223 	u32			pdn;
224 	u16			uid;
225 };
226 
227 enum {
228 	MLX5_IB_FLOW_ACTION_MODIFY_HEADER,
229 	MLX5_IB_FLOW_ACTION_PACKET_REFORMAT,
230 	MLX5_IB_FLOW_ACTION_DECAP,
231 };
232 
233 #define MLX5_IB_FLOW_MCAST_PRIO		(MLX5_BY_PASS_NUM_PRIOS - 1)
234 #define MLX5_IB_FLOW_LAST_PRIO		(MLX5_BY_PASS_NUM_REGULAR_PRIOS - 1)
235 #if (MLX5_IB_FLOW_LAST_PRIO <= 0)
236 #error "Invalid number of bypass priorities"
237 #endif
238 #define MLX5_IB_FLOW_LEFTOVERS_PRIO	(MLX5_IB_FLOW_MCAST_PRIO + 1)
239 
240 #define MLX5_IB_NUM_FLOW_FT		(MLX5_IB_FLOW_LEFTOVERS_PRIO + 1)
241 #define MLX5_IB_NUM_SNIFFER_FTS		2
242 #define MLX5_IB_NUM_EGRESS_FTS		1
243 #define MLX5_IB_NUM_FDB_FTS		MLX5_BY_PASS_NUM_REGULAR_PRIOS
244 
245 struct mlx5_ib_anchor {
246 	struct mlx5_flow_table *ft;
247 	struct mlx5_flow_group *fg_goto_table;
248 	struct mlx5_flow_group *fg_drop;
249 	struct mlx5_flow_handle *rule_goto_table;
250 	struct mlx5_flow_handle *rule_drop;
251 	unsigned int rule_goto_table_ref;
252 };
253 
254 struct mlx5_ib_flow_prio {
255 	struct mlx5_flow_table		*flow_table;
256 	struct mlx5_ib_anchor		anchor;
257 	unsigned int			refcount;
258 };
259 
260 struct mlx5_ib_flow_handler {
261 	struct list_head		list;
262 	struct ib_flow			ibflow;
263 	struct mlx5_ib_flow_prio	*prio;
264 	struct mlx5_flow_handle		*rule;
265 	struct ib_counters		*ibcounters;
266 	struct mlx5_ib_dev		*dev;
267 	struct mlx5_ib_flow_matcher	*flow_matcher;
268 };
269 
270 struct mlx5_ib_flow_matcher {
271 	struct mlx5_ib_match_params matcher_mask;
272 	int			mask_len;
273 	enum mlx5_ib_flow_type	flow_type;
274 	enum mlx5_flow_namespace_type ns_type;
275 	u16			priority;
276 	struct mlx5_core_dev	*mdev;
277 	atomic_t		usecnt;
278 	u8			match_criteria_enable;
279 };
280 
281 struct mlx5_ib_steering_anchor {
282 	struct mlx5_ib_flow_prio *ft_prio;
283 	struct mlx5_ib_dev *dev;
284 	atomic_t usecnt;
285 };
286 
287 struct mlx5_ib_pp {
288 	u16 index;
289 	struct mlx5_core_dev *mdev;
290 };
291 
292 enum mlx5_ib_optional_counter_type {
293 	MLX5_IB_OPCOUNTER_CC_RX_CE_PKTS,
294 	MLX5_IB_OPCOUNTER_CC_RX_CNP_PKTS,
295 	MLX5_IB_OPCOUNTER_CC_TX_CNP_PKTS,
296 
297 	MLX5_IB_OPCOUNTER_MAX,
298 };
299 
300 struct mlx5_ib_flow_db {
301 	struct mlx5_ib_flow_prio	prios[MLX5_IB_NUM_FLOW_FT];
302 	struct mlx5_ib_flow_prio	egress_prios[MLX5_IB_NUM_FLOW_FT];
303 	struct mlx5_ib_flow_prio	sniffer[MLX5_IB_NUM_SNIFFER_FTS];
304 	struct mlx5_ib_flow_prio	egress[MLX5_IB_NUM_EGRESS_FTS];
305 	struct mlx5_ib_flow_prio	fdb[MLX5_IB_NUM_FDB_FTS];
306 	struct mlx5_ib_flow_prio	rdma_rx[MLX5_IB_NUM_FLOW_FT];
307 	struct mlx5_ib_flow_prio	rdma_tx[MLX5_IB_NUM_FLOW_FT];
308 	struct mlx5_ib_flow_prio	opfcs[MLX5_IB_OPCOUNTER_MAX];
309 	struct mlx5_flow_table		*lag_demux_ft;
310 	/* Protect flow steering bypass flow tables
311 	 * when add/del flow rules.
312 	 * only single add/removal of flow steering rule could be done
313 	 * simultaneously.
314 	 */
315 	struct mutex			lock;
316 };
317 
318 /* Use macros here so that don't have to duplicate
319  * enum ib_qp_type for low-level driver
320  */
321 
322 #define MLX5_IB_QPT_REG_UMR	IB_QPT_RESERVED1
323 /*
324  * IB_QPT_GSI creates the software wrapper around GSI, and MLX5_IB_QPT_HW_GSI
325  * creates the actual hardware QP.
326  */
327 #define MLX5_IB_QPT_HW_GSI	IB_QPT_RESERVED2
328 #define MLX5_IB_QPT_DCI		IB_QPT_RESERVED3
329 #define MLX5_IB_QPT_DCT		IB_QPT_RESERVED4
330 #define MLX5_IB_WR_UMR		IB_WR_RESERVED1
331 
332 #define MLX5_IB_UPD_XLT_ZAP	      BIT(0)
333 #define MLX5_IB_UPD_XLT_ENABLE	      BIT(1)
334 #define MLX5_IB_UPD_XLT_ATOMIC	      BIT(2)
335 #define MLX5_IB_UPD_XLT_ADDR	      BIT(3)
336 #define MLX5_IB_UPD_XLT_PD	      BIT(4)
337 #define MLX5_IB_UPD_XLT_ACCESS	      BIT(5)
338 #define MLX5_IB_UPD_XLT_INDIRECT      BIT(6)
339 
340 /* Private QP creation flags to be passed in ib_qp_init_attr.create_flags.
341  *
342  * These flags are intended for internal use by the mlx5_ib driver, and they
343  * rely on the range reserved for that use in the ib_qp_create_flags enum.
344  */
345 #define MLX5_IB_QP_CREATE_SQPN_QP1	IB_QP_CREATE_RESERVED_START
346 
347 struct wr_list {
348 	u16	opcode;
349 	u16	next;
350 };
351 
352 enum mlx5_ib_rq_flags {
353 	MLX5_IB_RQ_CVLAN_STRIPPING	= 1 << 0,
354 	MLX5_IB_RQ_PCI_WRITE_END_PADDING	= 1 << 1,
355 };
356 
357 struct mlx5_ib_wq {
358 	struct mlx5_frag_buf_ctrl fbc;
359 	u64		       *wrid;
360 	u32		       *wr_data;
361 	struct wr_list	       *w_list;
362 	unsigned	       *wqe_head;
363 	u16		        unsig_count;
364 
365 	/* serialize post to the work queue
366 	 */
367 	spinlock_t		lock;
368 	int			wqe_cnt;
369 	int			max_post;
370 	int			max_gs;
371 	int			offset;
372 	int			wqe_shift;
373 	unsigned		head;
374 	unsigned		tail;
375 	u16			cur_post;
376 	u16			last_poll;
377 	void			*cur_edge;
378 };
379 
380 enum mlx5_ib_wq_flags {
381 	MLX5_IB_WQ_FLAGS_DELAY_DROP = 0x1,
382 	MLX5_IB_WQ_FLAGS_STRIDING_RQ = 0x2,
383 };
384 
385 #define MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES 9
386 #define MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES 16
387 #define MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES 6
388 #define MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES 13
389 #define MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES 3
390 
391 struct mlx5_ib_rwq {
392 	struct ib_wq		ibwq;
393 	struct mlx5_core_qp	core_qp;
394 	u32			rq_num_pas;
395 	u32			log_rq_stride;
396 	u32			log_rq_size;
397 	u32			rq_page_offset;
398 	u32			log_page_size;
399 	u32			log_num_strides;
400 	u32			two_byte_shift_en;
401 	u32			single_stride_log_num_of_bytes;
402 	struct ib_umem		*umem;
403 	size_t			buf_size;
404 	unsigned int		page_shift;
405 	struct mlx5_db		db;
406 	u32			user_index;
407 	u32			wqe_count;
408 	u32			wqe_shift;
409 	int			wq_sig;
410 	u32			create_flags; /* Use enum mlx5_ib_wq_flags */
411 };
412 
413 struct mlx5_ib_rwq_ind_table {
414 	struct ib_rwq_ind_table ib_rwq_ind_tbl;
415 	u32			rqtn;
416 	u16			uid;
417 };
418 
419 struct mlx5_ib_ubuffer {
420 	struct ib_umem	       *umem;
421 	int			buf_size;
422 	u64			buf_addr;
423 };
424 
425 struct mlx5_ib_qp_base {
426 	struct mlx5_ib_qp	*container_mibqp;
427 	struct mlx5_core_qp	mqp;
428 	struct mlx5_ib_ubuffer	ubuffer;
429 };
430 
431 struct mlx5_ib_qp_trans {
432 	struct mlx5_ib_qp_base	base;
433 	u16			xrcdn;
434 	u32			alt_port;
435 	u8			atomic_rd_en;
436 	u8			resp_depth;
437 };
438 
439 struct mlx5_ib_rss_qp {
440 	u32	tirn;
441 };
442 
443 struct mlx5_ib_rq {
444 	struct mlx5_ib_qp_base base;
445 	struct mlx5_ib_wq	*rq;
446 	struct mlx5_ib_ubuffer	ubuffer;
447 	struct mlx5_db		*doorbell;
448 	u32			tirn;
449 	u8			state;
450 	u32			flags;
451 };
452 
453 struct mlx5_ib_sq {
454 	struct mlx5_ib_qp_base base;
455 	struct mlx5_ib_wq	*sq;
456 	struct mlx5_ib_ubuffer  ubuffer;
457 	struct mlx5_db		*doorbell;
458 	struct mlx5_flow_handle	*flow_rule;
459 	u32			tisn;
460 	u8			state;
461 };
462 
463 struct mlx5_ib_raw_packet_qp {
464 	struct mlx5_ib_sq sq;
465 	struct mlx5_ib_rq rq;
466 };
467 
468 struct mlx5_bf {
469 	int			buf_size;
470 	unsigned long		offset;
471 	struct mlx5_sq_bfreg   *bfreg;
472 };
473 
474 struct mlx5_ib_dct {
475 	struct mlx5_core_dct    mdct;
476 	u32                     *in;
477 };
478 
479 struct mlx5_ib_gsi_qp {
480 	struct ib_qp *rx_qp;
481 	u32 port_num;
482 	struct ib_qp_cap cap;
483 	struct ib_cq *cq;
484 	struct mlx5_ib_gsi_wr *outstanding_wrs;
485 	u32 outstanding_pi, outstanding_ci;
486 	int num_qps;
487 	/* Protects access to the tx_qps. Post send operations synchronize
488 	 * with tx_qp creation in setup_qp(). Also protects the
489 	 * outstanding_wrs array and indices.
490 	 */
491 	spinlock_t lock;
492 	struct ib_qp **tx_qps;
493 };
494 
495 struct mlx5_ib_qp {
496 	struct ib_qp		ibqp;
497 	union {
498 		struct mlx5_ib_qp_trans trans_qp;
499 		struct mlx5_ib_raw_packet_qp raw_packet_qp;
500 		struct mlx5_ib_rss_qp rss_qp;
501 		struct mlx5_ib_dct dct;
502 		struct mlx5_ib_gsi_qp gsi;
503 	};
504 	struct mlx5_frag_buf	buf;
505 
506 	struct mlx5_db		db;
507 	struct mlx5_ib_wq	rq;
508 
509 	u8			sq_signal_bits;
510 	u8			next_fence;
511 	struct mlx5_ib_wq	sq;
512 
513 	/* serialize qp state modifications
514 	 */
515 	struct mutex		mutex;
516 	/* cached variant of create_flags from struct ib_qp_init_attr */
517 	u32			flags;
518 	u32			port;
519 	u8			state;
520 	int			max_inline_data;
521 	struct mlx5_bf	        bf;
522 	u8			has_rq:1;
523 	u8			is_rss:1;
524 	u8			is_ooo_rq:1;
525 
526 	/* only for user space QPs. For kernel
527 	 * we have it from the bf object
528 	 */
529 	int			bfregn;
530 
531 	struct list_head	qps_list;
532 	struct list_head	cq_recv_list;
533 	struct list_head	cq_send_list;
534 	struct mlx5_rate_limit	rl;
535 	u32                     underlay_qpn;
536 	u32			flags_en;
537 	/*
538 	 * IB/core doesn't store low-level QP types, so
539 	 * store both MLX and IBTA types in the field below.
540 	 */
541 	enum ib_qp_type		type;
542 	/* A flag to indicate if there's a new counter is configured
543 	 * but not take effective
544 	 */
545 	u32                     counter_pending;
546 	u16			gsi_lag_port;
547 };
548 
549 struct mlx5_ib_cq_buf {
550 	struct mlx5_frag_buf_ctrl fbc;
551 	struct mlx5_frag_buf    frag_buf;
552 	struct ib_umem		*umem;
553 	int			cqe_size;
554 	int			nent;
555 };
556 
557 enum mlx5_ib_cq_pr_flags {
558 	MLX5_IB_CQ_PR_FLAGS_CQE_128_PAD	= 1 << 0,
559 	MLX5_IB_CQ_PR_FLAGS_REAL_TIME_TS = 1 << 1,
560 };
561 
562 struct mlx5_ib_cq {
563 	struct ib_cq		ibcq;
564 	struct mlx5_core_cq	mcq;
565 	struct mlx5_ib_cq_buf	buf;
566 	struct mlx5_db		db;
567 
568 	/* serialize access to the CQ
569 	 */
570 	spinlock_t		lock;
571 
572 	/* protect resize cq
573 	 */
574 	struct mutex		resize_mutex;
575 	struct mlx5_ib_cq_buf  *resize_buf;
576 	struct ib_umem	       *resize_umem;
577 	int			cqe_size;
578 	struct list_head	list_send_qp;
579 	struct list_head	list_recv_qp;
580 	u32			create_flags;
581 	struct list_head	wc_list;
582 	enum ib_cq_notify_flags notify_flags;
583 	struct work_struct	notify_work;
584 	u16			private_flags; /* Use mlx5_ib_cq_pr_flags */
585 };
586 
587 struct mlx5_ib_wc {
588 	struct ib_wc wc;
589 	struct list_head list;
590 };
591 
592 struct mlx5_ib_srq {
593 	struct ib_srq		ibsrq;
594 	struct mlx5_core_srq	msrq;
595 	struct mlx5_frag_buf	buf;
596 	struct mlx5_db		db;
597 	struct mlx5_frag_buf_ctrl fbc;
598 	u64		       *wrid;
599 	/* protect SRQ hanlding
600 	 */
601 	spinlock_t		lock;
602 	int			head;
603 	int			tail;
604 	u16			wqe_ctr;
605 	struct ib_umem	       *umem;
606 	/* serialize arming a SRQ
607 	 */
608 	struct mutex		mutex;
609 	int			wq_sig;
610 };
611 
612 struct mlx5_ib_xrcd {
613 	struct ib_xrcd		ibxrcd;
614 	u32			xrcdn;
615 };
616 
617 enum mlx5_ib_mtt_access_flags {
618 	MLX5_IB_MTT_READ  = (1 << 0),
619 	MLX5_IB_MTT_WRITE = (1 << 1),
620 };
621 
622 struct mlx5_user_mmap_entry {
623 	struct rdma_user_mmap_entry rdma_entry;
624 	u8 mmap_flag;
625 	u64 address;
626 	u32 page_idx;
627 };
628 
629 enum mlx5_mkey_type {
630 	MLX5_MKEY_MR = 1,
631 	MLX5_MKEY_MW,
632 	MLX5_MKEY_INDIRECT_DEVX,
633 	MLX5_MKEY_NULL,
634 	MLX5_MKEY_IMPLICIT_CHILD,
635 };
636 
637 struct mlx5r_cache_rb_key {
638 	u8 ats:1;
639 	unsigned int access_mode;
640 	unsigned int access_flags;
641 	unsigned int ndescs;
642 };
643 
644 struct mlx5_ib_mkey {
645 	u32 key;
646 	enum mlx5_mkey_type type;
647 	unsigned int ndescs;
648 	struct wait_queue_head wait;
649 	refcount_t usecount;
650 	/* Cacheable user Mkey must hold either a rb_key or a cache_ent. */
651 	struct mlx5r_cache_rb_key rb_key;
652 	struct mlx5_cache_ent *cache_ent;
653 	u8 cacheable : 1;
654 };
655 
656 #define MLX5_IB_MTT_PRESENT (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE)
657 
658 #define MLX5_IB_DM_MEMIC_ALLOWED_ACCESS (IB_ACCESS_LOCAL_WRITE   |\
659 					 IB_ACCESS_REMOTE_WRITE  |\
660 					 IB_ACCESS_REMOTE_READ   |\
661 					 IB_ACCESS_REMOTE_ATOMIC |\
662 					 IB_ZERO_BASED)
663 
664 #define MLX5_IB_DM_SW_ICM_ALLOWED_ACCESS (IB_ACCESS_LOCAL_WRITE   |\
665 					  IB_ACCESS_REMOTE_WRITE  |\
666 					  IB_ACCESS_REMOTE_READ   |\
667 					  IB_ZERO_BASED)
668 
669 #define mlx5_update_odp_stats(mr, counter_name, value)		\
670 	atomic64_add(value, &((mr)->odp_stats.counter_name))
671 
672 struct mlx5_ib_mr {
673 	struct ib_mr ibmr;
674 	struct mlx5_ib_mkey mmkey;
675 
676 	struct ib_umem *umem;
677 	/* The mr is data direct related */
678 	u8 data_direct :1;
679 
680 	union {
681 		/* Used only by kernel MRs (umem == NULL) */
682 		struct {
683 			void *descs;
684 			void *descs_alloc;
685 			dma_addr_t desc_map;
686 			int max_descs;
687 			int desc_size;
688 			int access_mode;
689 
690 			/* For Kernel IB_MR_TYPE_INTEGRITY */
691 			struct mlx5_core_sig_ctx *sig;
692 			struct mlx5_ib_mr *pi_mr;
693 			struct mlx5_ib_mr *klm_mr;
694 			struct mlx5_ib_mr *mtt_mr;
695 			u64 data_iova;
696 			u64 pi_iova;
697 			int meta_ndescs;
698 			int meta_length;
699 			int data_length;
700 		};
701 
702 		/* Used only by User MRs (umem != NULL) */
703 		struct {
704 			unsigned int page_shift;
705 			/* Current access_flags */
706 			int access_flags;
707 
708 			/* For User ODP */
709 			struct mlx5_ib_mr *parent;
710 			struct xarray implicit_children;
711 			union {
712 				struct work_struct work;
713 			} odp_destroy;
714 			struct ib_odp_counters odp_stats;
715 			bool is_odp_implicit;
716 			/* The affilated data direct crossed mr */
717 			struct mlx5_ib_mr *dd_crossed_mr;
718 			struct list_head dd_node;
719 			u8 revoked :1;
720 			struct mlx5_ib_mkey null_mmkey;
721 		};
722 	};
723 };
724 
725 static inline bool is_odp_mr(struct mlx5_ib_mr *mr)
726 {
727 	return IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING) && mr->umem &&
728 	       mr->umem->is_odp;
729 }
730 
731 static inline bool is_dmabuf_mr(struct mlx5_ib_mr *mr)
732 {
733 	return IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING) && mr->umem &&
734 	       mr->umem->is_dmabuf;
735 }
736 
737 struct mlx5_ib_mw {
738 	struct ib_mw		ibmw;
739 	struct mlx5_ib_mkey	mmkey;
740 };
741 
742 struct mlx5_ib_umr_context {
743 	struct ib_cqe		cqe;
744 	enum ib_wc_status	status;
745 	struct completion	done;
746 };
747 
748 enum {
749 	MLX5_UMR_STATE_UNINIT,
750 	MLX5_UMR_STATE_ACTIVE,
751 	MLX5_UMR_STATE_RECOVER,
752 	MLX5_UMR_STATE_ERR,
753 };
754 
755 struct umr_common {
756 	struct ib_pd	*pd;
757 	struct ib_cq	*cq;
758 	struct ib_qp	*qp;
759 	/* Protects from UMR QP overflow
760 	 */
761 	struct semaphore	sem;
762 	/* Protects from using UMR while the UMR is not active
763 	 */
764 	struct mutex lock;
765 	unsigned int state;
766 	/* Protects from repeat UMR QP creation */
767 	struct mutex init_lock;
768 };
769 
770 #define NUM_MKEYS_PER_PAGE \
771 	((PAGE_SIZE - sizeof(struct list_head)) / sizeof(u32))
772 
773 struct mlx5_mkeys_page {
774 	u32 mkeys[NUM_MKEYS_PER_PAGE];
775 	struct list_head list;
776 };
777 static_assert(sizeof(struct mlx5_mkeys_page) == PAGE_SIZE);
778 
779 struct mlx5_mkeys_queue {
780 	struct list_head pages_list;
781 	u32 num_pages;
782 	unsigned long ci;
783 	spinlock_t lock; /* sync list ops */
784 };
785 
786 struct mlx5_cache_ent {
787 	struct mlx5_mkeys_queue	mkeys_queue;
788 	u32			pending;
789 
790 	char                    name[4];
791 
792 	struct rb_node		node;
793 	struct mlx5r_cache_rb_key rb_key;
794 
795 	u8 is_tmp:1;
796 	u8 disabled:1;
797 	u8 fill_to_high_water:1;
798 	u8 tmp_cleanup_scheduled:1;
799 
800 	/*
801 	 * - limit is the low water mark for stored mkeys, 2* limit is the
802 	 *   upper water mark.
803 	 */
804 	u32 in_use;
805 	u32 limit;
806 
807 	/* Statistics */
808 	u32                     miss;
809 
810 	struct mlx5_ib_dev     *dev;
811 	struct delayed_work	dwork;
812 };
813 
814 struct mlx5r_async_create_mkey {
815 	union {
816 		u32 in[MLX5_ST_SZ_BYTES(create_mkey_in)];
817 		u32 out[MLX5_ST_SZ_DW(create_mkey_out)];
818 	};
819 	struct mlx5_async_work cb_work;
820 	struct mlx5_cache_ent *ent;
821 	u32 mkey;
822 };
823 
824 struct mlx5_mkey_cache {
825 	struct workqueue_struct *wq;
826 	struct rb_root		rb_root;
827 	struct mutex		rb_lock;
828 	struct dentry		*fs_root;
829 	unsigned long		last_add;
830 };
831 
832 struct mlx5_ib_port_resources {
833 	struct mlx5_ib_gsi_qp *gsi;
834 	struct work_struct pkey_change_work;
835 };
836 
837 struct mlx5_data_direct_resources {
838 	u32 pdn;
839 	u32 mkey;
840 };
841 
842 struct mlx5_ib_resources {
843 	struct ib_cq	*c0;
844 	struct mutex cq_lock;
845 	u32 xrcdn0;
846 	u32 xrcdn1;
847 	struct ib_pd	*p0;
848 	struct ib_srq	*s0;
849 	struct ib_srq	*s1;
850 	struct mutex srq_lock;
851 	struct mlx5_ib_port_resources ports[2];
852 };
853 
854 #define MAX_OPFC_RULES 2
855 
856 struct mlx5_ib_op_fc {
857 	struct mlx5_fc *fc;
858 	struct mlx5_flow_handle *rule[MAX_OPFC_RULES];
859 };
860 
861 struct mlx5_ib_counters {
862 	struct rdma_stat_desc *descs;
863 	size_t *offsets;
864 	u32 num_q_counters;
865 	u32 num_cong_counters;
866 	u32 num_ext_ppcnt_counters;
867 	u32 num_op_counters;
868 	u16 set_id;
869 	struct mlx5_ib_op_fc opfcs[MLX5_IB_OPCOUNTER_MAX];
870 };
871 
872 int mlx5_ib_fs_add_op_fc(struct mlx5_ib_dev *dev, u32 port_num,
873 			 struct mlx5_ib_op_fc *opfc,
874 			 enum mlx5_ib_optional_counter_type type);
875 
876 void mlx5_ib_fs_remove_op_fc(struct mlx5_ib_dev *dev,
877 			     struct mlx5_ib_op_fc *opfc,
878 			     enum mlx5_ib_optional_counter_type type);
879 
880 struct mlx5_ib_multiport_info;
881 
882 struct mlx5_ib_multiport {
883 	struct mlx5_ib_multiport_info *mpi;
884 	/* To be held when accessing the multiport info */
885 	spinlock_t mpi_lock;
886 };
887 
888 struct mlx5_roce {
889 	/* Protect mlx5_ib_get_netdev from invoking dev_hold() with a NULL
890 	 * netdev pointer
891 	 */
892 	struct notifier_block	nb;
893 	struct netdev_net_notifier nn;
894 	struct notifier_block	mdev_nb;
895 	struct net_device	*tracking_netdev;
896 	atomic_t		tx_port_affinity;
897 	enum ib_port_state last_port_state;
898 	struct mlx5_ib_dev	*dev;
899 	u32			native_port_num;
900 };
901 
902 struct mlx5_ib_port {
903 	struct mlx5_ib_counters cnts;
904 	struct mlx5_ib_multiport mp;
905 	struct mlx5_ib_dbg_cc_params *dbg_cc_params;
906 	struct mlx5_roce roce;
907 	struct mlx5_eswitch_rep		*rep;
908 #ifdef CONFIG_MLX5_MACSEC
909 	struct mlx5_reserved_gids *reserved_gids;
910 #endif
911 };
912 
913 struct mlx5_ib_dbg_param {
914 	int			offset;
915 	struct mlx5_ib_dev	*dev;
916 	struct dentry		*dentry;
917 	u32			port_num;
918 };
919 
920 enum mlx5_ib_dbg_cc_types {
921 	MLX5_IB_DBG_CC_RP_CLAMP_TGT_RATE,
922 	MLX5_IB_DBG_CC_RP_CLAMP_TGT_RATE_ATI,
923 	MLX5_IB_DBG_CC_RP_TIME_RESET,
924 	MLX5_IB_DBG_CC_RP_BYTE_RESET,
925 	MLX5_IB_DBG_CC_RP_THRESHOLD,
926 	MLX5_IB_DBG_CC_RP_AI_RATE,
927 	MLX5_IB_DBG_CC_RP_MAX_RATE,
928 	MLX5_IB_DBG_CC_RP_HAI_RATE,
929 	MLX5_IB_DBG_CC_RP_MIN_DEC_FAC,
930 	MLX5_IB_DBG_CC_RP_MIN_RATE,
931 	MLX5_IB_DBG_CC_RP_RATE_TO_SET_ON_FIRST_CNP,
932 	MLX5_IB_DBG_CC_RP_DCE_TCP_G,
933 	MLX5_IB_DBG_CC_RP_DCE_TCP_RTT,
934 	MLX5_IB_DBG_CC_RP_RATE_REDUCE_MONITOR_PERIOD,
935 	MLX5_IB_DBG_CC_RP_INITIAL_ALPHA_VALUE,
936 	MLX5_IB_DBG_CC_RP_GD,
937 	MLX5_IB_DBG_CC_NP_MIN_TIME_BETWEEN_CNPS,
938 	MLX5_IB_DBG_CC_NP_CNP_DSCP,
939 	MLX5_IB_DBG_CC_NP_CNP_PRIO_MODE,
940 	MLX5_IB_DBG_CC_NP_CNP_PRIO,
941 	MLX5_IB_DBG_CC_GENERAL_RTT_RESP_DSCP_VALID,
942 	MLX5_IB_DBG_CC_GENERAL_RTT_RESP_DSCP,
943 	MLX5_IB_DBG_CC_MAX,
944 };
945 
946 struct mlx5_ib_dbg_cc_params {
947 	struct dentry			*root;
948 	struct mlx5_ib_dbg_param	params[MLX5_IB_DBG_CC_MAX];
949 };
950 
951 enum {
952 	MLX5_MAX_DELAY_DROP_TIMEOUT_MS = 100,
953 };
954 
955 struct mlx5_ib_delay_drop {
956 	struct mlx5_ib_dev     *dev;
957 	struct work_struct	delay_drop_work;
958 	/* serialize setting of delay drop */
959 	struct mutex		lock;
960 	u32			timeout;
961 	bool			activate;
962 	atomic_t		events_cnt;
963 	atomic_t		rqs_cnt;
964 	struct dentry		*dir_debugfs;
965 };
966 
967 enum mlx5_ib_stages {
968 	MLX5_IB_STAGE_INIT,
969 	MLX5_IB_STAGE_FS,
970 	MLX5_IB_STAGE_CAPS,
971 	MLX5_IB_STAGE_NON_DEFAULT_CB,
972 	MLX5_IB_STAGE_ROCE,
973 	MLX5_IB_STAGE_QP,
974 	MLX5_IB_STAGE_SRQ,
975 	MLX5_IB_STAGE_DEVICE_RESOURCES,
976 	MLX5_IB_STAGE_ODP,
977 	MLX5_IB_STAGE_COUNTERS,
978 	MLX5_IB_STAGE_CONG_DEBUGFS,
979 	MLX5_IB_STAGE_UAR,
980 	MLX5_IB_STAGE_BFREG,
981 	MLX5_IB_STAGE_PRE_IB_REG_UMR,
982 	MLX5_IB_STAGE_WHITELIST_UID,
983 	MLX5_IB_STAGE_IB_REG,
984 	MLX5_IB_STAGE_DEVICE_NOTIFIER,
985 	MLX5_IB_STAGE_POST_IB_REG_UMR,
986 	MLX5_IB_STAGE_DELAY_DROP,
987 	MLX5_IB_STAGE_RESTRACK,
988 	MLX5_IB_STAGE_MAX,
989 };
990 
991 struct mlx5_ib_stage {
992 	int (*init)(struct mlx5_ib_dev *dev);
993 	void (*cleanup)(struct mlx5_ib_dev *dev);
994 };
995 
996 #define STAGE_CREATE(_stage, _init, _cleanup) \
997 	.stage[_stage] = {.init = _init, .cleanup = _cleanup}
998 
999 struct mlx5_ib_profile {
1000 	struct mlx5_ib_stage stage[MLX5_IB_STAGE_MAX];
1001 };
1002 
1003 struct mlx5_ib_multiport_info {
1004 	struct list_head list;
1005 	struct mlx5_ib_dev *ibdev;
1006 	struct mlx5_core_dev *mdev;
1007 	struct notifier_block mdev_events;
1008 	struct completion unref_comp;
1009 	u64 sys_image_guid;
1010 	u32 mdev_refcnt;
1011 	bool is_master;
1012 	bool unaffiliate;
1013 };
1014 
1015 struct mlx5_ib_flow_action {
1016 	struct ib_flow_action		ib_action;
1017 	union {
1018 		struct {
1019 			u64			    ib_flags;
1020 			struct mlx5_accel_esp_xfrm *ctx;
1021 		} esp_aes_gcm;
1022 		struct {
1023 			struct mlx5_ib_dev *dev;
1024 			u32 sub_type;
1025 			union {
1026 				struct mlx5_modify_hdr *modify_hdr;
1027 				struct mlx5_pkt_reformat *pkt_reformat;
1028 			};
1029 		} flow_action_raw;
1030 	};
1031 };
1032 
1033 struct mlx5_dm {
1034 	struct mlx5_core_dev *dev;
1035 	/* This lock is used to protect the access to the shared
1036 	 * allocation map when concurrent requests by different
1037 	 * processes are handled.
1038 	 */
1039 	spinlock_t lock;
1040 	DECLARE_BITMAP(memic_alloc_pages, MLX5_MAX_MEMIC_PAGES);
1041 };
1042 
1043 struct mlx5_read_counters_attr {
1044 	struct mlx5_fc *hw_cntrs_hndl;
1045 	u64 *out;
1046 	u32 flags;
1047 };
1048 
1049 enum mlx5_ib_counters_type {
1050 	MLX5_IB_COUNTERS_FLOW,
1051 };
1052 
1053 struct mlx5_ib_mcounters {
1054 	struct ib_counters ibcntrs;
1055 	enum mlx5_ib_counters_type type;
1056 	/* number of counters supported for this counters type */
1057 	u32 counters_num;
1058 	struct mlx5_fc *hw_cntrs_hndl;
1059 	/* read function for this counters type */
1060 	int (*read_counters)(struct ib_device *ibdev,
1061 			     struct mlx5_read_counters_attr *read_attr);
1062 	/* max index set as part of create_flow */
1063 	u32 cntrs_max_index;
1064 	/* number of counters data entries (<description,index> pair) */
1065 	u32 ncounters;
1066 	/* counters data array for descriptions and indexes */
1067 	struct mlx5_ib_flow_counters_desc *counters_data;
1068 	/* protects access to mcounters internal data */
1069 	struct mutex mcntrs_mutex;
1070 };
1071 
1072 static inline struct mlx5_ib_mcounters *
1073 to_mcounters(struct ib_counters *ibcntrs)
1074 {
1075 	return container_of(ibcntrs, struct mlx5_ib_mcounters, ibcntrs);
1076 }
1077 
1078 int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
1079 			   bool is_egress,
1080 			   struct mlx5_flow_act *action);
1081 struct mlx5_ib_lb_state {
1082 	/* protect the user_td */
1083 	struct mutex		mutex;
1084 	u32			user_td;
1085 	int			qps;
1086 	bool			enabled;
1087 };
1088 
1089 struct mlx5_ib_pf_eq {
1090 	struct notifier_block irq_nb;
1091 	struct mlx5_ib_dev *dev;
1092 	struct mlx5_eq *core;
1093 	struct work_struct work;
1094 	spinlock_t lock; /* Pagefaults spinlock */
1095 	struct workqueue_struct *wq;
1096 	mempool_t *pool;
1097 };
1098 
1099 struct mlx5_devx_event_table {
1100 	struct mlx5_nb devx_nb;
1101 	/* serialize updating the event_xa */
1102 	struct mutex event_xa_lock;
1103 	struct xarray event_xa;
1104 };
1105 
1106 struct mlx5_var_table {
1107 	/* serialize updating the bitmap */
1108 	struct mutex bitmap_lock;
1109 	unsigned long *bitmap;
1110 	u64 hw_start_addr;
1111 	u32 stride_size;
1112 	u64 num_var_hw_entries;
1113 };
1114 
1115 struct mlx5_port_caps {
1116 	bool has_smi;
1117 	u8 ext_port_cap;
1118 };
1119 
1120 
1121 struct mlx5_special_mkeys {
1122 	u32 dump_fill_mkey;
1123 	__be32 null_mkey;
1124 	__be32 terminate_scatter_list_mkey;
1125 };
1126 
1127 struct mlx5_macsec {
1128 	struct mutex lock; /* Protects mlx5_macsec internal contexts */
1129 	struct list_head macsec_devices_list;
1130 	struct notifier_block blocking_events_nb;
1131 };
1132 
1133 struct mlx5_ib_dev {
1134 	struct ib_device		ib_dev;
1135 	struct mlx5_core_dev		*mdev;
1136 	struct mlx5_data_direct_dev	*data_direct_dev;
1137 	/* protect accessing data_direct_dev */
1138 	struct mutex			data_direct_lock;
1139 	struct notifier_block		mdev_events;
1140 	struct notifier_block           lag_events;
1141 	int				num_ports;
1142 	/* serialize update of capability mask
1143 	 */
1144 	struct mutex			cap_mask_mutex;
1145 	u8				ib_active:1;
1146 	u8				is_rep:1;
1147 	u8				lag_active:1;
1148 	u8				fill_delay;
1149 	struct umr_common		umrc;
1150 	/* sync used page count stats
1151 	 */
1152 	struct mlx5_ib_resources	devr;
1153 
1154 	atomic_t			mkey_var;
1155 	struct mlx5_mkey_cache		cache;
1156 	struct timer_list		delay_timer;
1157 	/* Prevents soft lock on massive reg MRs */
1158 	struct mutex			slow_path_mutex;
1159 	struct ib_odp_caps	odp_caps;
1160 	u64			odp_max_size;
1161 	struct mutex		odp_eq_mutex;
1162 	struct mlx5_ib_pf_eq	odp_pf_eq;
1163 
1164 	struct xarray		odp_mkeys;
1165 
1166 	struct mlx5_ib_flow_db	*flow_db;
1167 	/* protect resources needed as part of reset flow */
1168 	spinlock_t		reset_flow_resource_lock;
1169 	struct list_head	qp_list;
1170 	struct list_head data_direct_mr_list;
1171 	/* Array with num_ports elements */
1172 	struct mlx5_ib_port	*port;
1173 	struct mlx5_sq_bfreg	bfreg;
1174 	struct mlx5_sq_bfreg	fp_bfreg;
1175 	struct mlx5_ib_delay_drop	delay_drop;
1176 	const struct mlx5_ib_profile	*profile;
1177 
1178 	struct mlx5_ib_lb_state		lb;
1179 	u8			umr_fence;
1180 	struct list_head	ib_dev_list;
1181 	u64			sys_image_guid;
1182 	struct mlx5_dm		dm;
1183 	u16			devx_whitelist_uid;
1184 	struct mlx5_srq_table   srq_table;
1185 	struct mlx5_qp_table    qp_table;
1186 	struct mlx5_async_ctx   async_ctx;
1187 	struct mlx5_devx_event_table devx_event_table;
1188 	struct mlx5_var_table var_table;
1189 
1190 	struct xarray sig_mrs;
1191 	struct mlx5_port_caps port_caps[MLX5_MAX_PORTS];
1192 	u16 pkey_table_len;
1193 	u8 lag_ports;
1194 	struct mlx5_special_mkeys mkeys;
1195 	struct mlx5_data_direct_resources ddr;
1196 
1197 #ifdef CONFIG_MLX5_MACSEC
1198 	struct mlx5_macsec macsec;
1199 #endif
1200 
1201 	u8 num_plane;
1202 	struct mlx5_ib_dev *smi_dev;
1203 	const char *sub_dev_name;
1204 };
1205 
1206 static inline struct mlx5_ib_cq *to_mibcq(struct mlx5_core_cq *mcq)
1207 {
1208 	return container_of(mcq, struct mlx5_ib_cq, mcq);
1209 }
1210 
1211 static inline struct mlx5_ib_xrcd *to_mxrcd(struct ib_xrcd *ibxrcd)
1212 {
1213 	return container_of(ibxrcd, struct mlx5_ib_xrcd, ibxrcd);
1214 }
1215 
1216 static inline struct mlx5_ib_dev *to_mdev(struct ib_device *ibdev)
1217 {
1218 	return container_of(ibdev, struct mlx5_ib_dev, ib_dev);
1219 }
1220 
1221 static inline struct mlx5_ib_dev *mr_to_mdev(struct mlx5_ib_mr *mr)
1222 {
1223 	return to_mdev(mr->ibmr.device);
1224 }
1225 
1226 static inline struct mlx5_ib_dev *mlx5_udata_to_mdev(struct ib_udata *udata)
1227 {
1228 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
1229 		udata, struct mlx5_ib_ucontext, ibucontext);
1230 
1231 	return to_mdev(context->ibucontext.device);
1232 }
1233 
1234 static inline struct mlx5_ib_cq *to_mcq(struct ib_cq *ibcq)
1235 {
1236 	return container_of(ibcq, struct mlx5_ib_cq, ibcq);
1237 }
1238 
1239 static inline struct mlx5_ib_qp *to_mibqp(struct mlx5_core_qp *mqp)
1240 {
1241 	return container_of(mqp, struct mlx5_ib_qp_base, mqp)->container_mibqp;
1242 }
1243 
1244 static inline struct mlx5_ib_rwq *to_mibrwq(struct mlx5_core_qp *core_qp)
1245 {
1246 	return container_of(core_qp, struct mlx5_ib_rwq, core_qp);
1247 }
1248 
1249 static inline struct mlx5_ib_pd *to_mpd(struct ib_pd *ibpd)
1250 {
1251 	return container_of(ibpd, struct mlx5_ib_pd, ibpd);
1252 }
1253 
1254 static inline struct mlx5_ib_srq *to_msrq(struct ib_srq *ibsrq)
1255 {
1256 	return container_of(ibsrq, struct mlx5_ib_srq, ibsrq);
1257 }
1258 
1259 static inline struct mlx5_ib_qp *to_mqp(struct ib_qp *ibqp)
1260 {
1261 	return container_of(ibqp, struct mlx5_ib_qp, ibqp);
1262 }
1263 
1264 static inline struct mlx5_ib_rwq *to_mrwq(struct ib_wq *ibwq)
1265 {
1266 	return container_of(ibwq, struct mlx5_ib_rwq, ibwq);
1267 }
1268 
1269 static inline struct mlx5_ib_rwq_ind_table *to_mrwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
1270 {
1271 	return container_of(ib_rwq_ind_tbl, struct mlx5_ib_rwq_ind_table, ib_rwq_ind_tbl);
1272 }
1273 
1274 static inline struct mlx5_ib_srq *to_mibsrq(struct mlx5_core_srq *msrq)
1275 {
1276 	return container_of(msrq, struct mlx5_ib_srq, msrq);
1277 }
1278 
1279 static inline struct mlx5_ib_mr *to_mmr(struct ib_mr *ibmr)
1280 {
1281 	return container_of(ibmr, struct mlx5_ib_mr, ibmr);
1282 }
1283 
1284 static inline struct mlx5_ib_mw *to_mmw(struct ib_mw *ibmw)
1285 {
1286 	return container_of(ibmw, struct mlx5_ib_mw, ibmw);
1287 }
1288 
1289 static inline struct mlx5_ib_flow_action *
1290 to_mflow_act(struct ib_flow_action *ibact)
1291 {
1292 	return container_of(ibact, struct mlx5_ib_flow_action, ib_action);
1293 }
1294 
1295 static inline struct mlx5_user_mmap_entry *
1296 to_mmmap(struct rdma_user_mmap_entry *rdma_entry)
1297 {
1298 	return container_of(rdma_entry,
1299 		struct mlx5_user_mmap_entry, rdma_entry);
1300 }
1301 
1302 int mlx5_ib_dev_res_cq_init(struct mlx5_ib_dev *dev);
1303 int mlx5_ib_dev_res_srq_init(struct mlx5_ib_dev *dev);
1304 int mlx5_ib_db_map_user(struct mlx5_ib_ucontext *context, unsigned long virt,
1305 			struct mlx5_db *db);
1306 void mlx5_ib_db_unmap_user(struct mlx5_ib_ucontext *context, struct mlx5_db *db);
1307 void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq);
1308 void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq);
1309 void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index);
1310 int mlx5_ib_create_ah(struct ib_ah *ah, struct rdma_ah_init_attr *init_attr,
1311 		      struct ib_udata *udata);
1312 int mlx5_ib_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr);
1313 static inline int mlx5_ib_destroy_ah(struct ib_ah *ah, u32 flags)
1314 {
1315 	return 0;
1316 }
1317 int mlx5_ib_create_srq(struct ib_srq *srq, struct ib_srq_init_attr *init_attr,
1318 		       struct ib_udata *udata);
1319 int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
1320 		       enum ib_srq_attr_mask attr_mask, struct ib_udata *udata);
1321 int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr);
1322 int mlx5_ib_destroy_srq(struct ib_srq *srq, struct ib_udata *udata);
1323 int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
1324 			  const struct ib_recv_wr **bad_wr);
1325 int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp);
1326 void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp);
1327 int mlx5_ib_create_qp(struct ib_qp *qp, struct ib_qp_init_attr *init_attr,
1328 		      struct ib_udata *udata);
1329 int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1330 		      int attr_mask, struct ib_udata *udata);
1331 int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
1332 		     struct ib_qp_init_attr *qp_init_attr);
1333 int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata);
1334 void mlx5_ib_drain_sq(struct ib_qp *qp);
1335 void mlx5_ib_drain_rq(struct ib_qp *qp);
1336 int mlx5_ib_read_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
1337 			size_t buflen, size_t *bc);
1338 int mlx5_ib_read_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
1339 			size_t buflen, size_t *bc);
1340 int mlx5_ib_read_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index, void *buffer,
1341 			 size_t buflen, size_t *bc);
1342 int mlx5_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
1343 		      struct uverbs_attr_bundle *attrs);
1344 int mlx5_ib_destroy_cq(struct ib_cq *cq, struct ib_udata *udata);
1345 int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc);
1346 int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags);
1347 int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period);
1348 int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata);
1349 struct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc);
1350 struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
1351 				  u64 virt_addr, int access_flags,
1352 				  struct ib_udata *udata);
1353 struct ib_mr *mlx5_ib_reg_user_mr_dmabuf(struct ib_pd *pd, u64 start,
1354 					 u64 length, u64 virt_addr,
1355 					 int fd, int access_flags,
1356 					 struct uverbs_attr_bundle *attrs);
1357 int mlx5_ib_advise_mr(struct ib_pd *pd,
1358 		      enum ib_uverbs_advise_mr_advice advice,
1359 		      u32 flags,
1360 		      struct ib_sge *sg_list,
1361 		      u32 num_sge,
1362 		      struct uverbs_attr_bundle *attrs);
1363 int mlx5_ib_alloc_mw(struct ib_mw *mw, struct ib_udata *udata);
1364 int mlx5_ib_dealloc_mw(struct ib_mw *mw);
1365 struct mlx5_ib_mr *mlx5_ib_alloc_implicit_mr(struct mlx5_ib_pd *pd,
1366 					     int access_flags);
1367 void mlx5_ib_free_odp_mr(struct mlx5_ib_mr *mr);
1368 struct ib_mr *mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
1369 				    u64 length, u64 virt_addr, int access_flags,
1370 				    struct ib_pd *pd, struct ib_udata *udata);
1371 int mlx5_ib_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata);
1372 struct ib_mr *mlx5_ib_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
1373 			       u32 max_num_sg);
1374 struct ib_mr *mlx5_ib_alloc_mr_integrity(struct ib_pd *pd,
1375 					 u32 max_num_sg,
1376 					 u32 max_num_meta_sg);
1377 int mlx5_ib_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
1378 		      unsigned int *sg_offset);
1379 int mlx5_ib_map_mr_sg_pi(struct ib_mr *ibmr, struct scatterlist *data_sg,
1380 			 int data_sg_nents, unsigned int *data_sg_offset,
1381 			 struct scatterlist *meta_sg, int meta_sg_nents,
1382 			 unsigned int *meta_sg_offset);
1383 int mlx5_ib_process_mad(struct ib_device *ibdev, int mad_flags, u32 port_num,
1384 			const struct ib_wc *in_wc, const struct ib_grh *in_grh,
1385 			const struct ib_mad *in, struct ib_mad *out,
1386 			size_t *out_mad_size, u16 *out_mad_pkey_index);
1387 int mlx5_ib_alloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata);
1388 int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata);
1389 int mlx5_query_ext_port_caps(struct mlx5_ib_dev *dev, unsigned int port);
1390 int mlx5_query_mad_ifc_system_image_guid(struct ib_device *ibdev,
1391 					 __be64 *sys_image_guid);
1392 int mlx5_query_mad_ifc_max_pkeys(struct ib_device *ibdev,
1393 				 u16 *max_pkeys);
1394 int mlx5_query_mad_ifc_vendor_id(struct ib_device *ibdev,
1395 				 u32 *vendor_id);
1396 int mlx5_query_mad_ifc_node_desc(struct mlx5_ib_dev *dev, char *node_desc);
1397 int mlx5_query_mad_ifc_node_guid(struct mlx5_ib_dev *dev, __be64 *node_guid);
1398 int mlx5_query_mad_ifc_pkey(struct ib_device *ibdev, u32 port, u16 index,
1399 			    u16 *pkey);
1400 int mlx5_query_mad_ifc_gids(struct ib_device *ibdev, u32 port, int index,
1401 			    union ib_gid *gid);
1402 int mlx5_query_mad_ifc_port(struct ib_device *ibdev, u32 port,
1403 			    struct ib_port_attr *props);
1404 int mlx5_ib_query_port(struct ib_device *ibdev, u32 port,
1405 		       struct ib_port_attr *props);
1406 void mlx5_ib_populate_pas(struct ib_umem *umem, size_t page_size, __be64 *pas,
1407 			  u64 access_flags);
1408 int mlx5_ib_get_cqe_size(struct ib_cq *ibcq);
1409 int mlx5_mkey_cache_init(struct mlx5_ib_dev *dev);
1410 void mlx5_mkey_cache_cleanup(struct mlx5_ib_dev *dev);
1411 struct mlx5_cache_ent *
1412 mlx5r_cache_create_ent_locked(struct mlx5_ib_dev *dev,
1413 			      struct mlx5r_cache_rb_key rb_key,
1414 			      bool persistent_entry);
1415 
1416 struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev,
1417 				       int access_flags, int access_mode,
1418 				       int ndescs);
1419 
1420 int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
1421 			    struct ib_mr_status *mr_status);
1422 struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
1423 				struct ib_wq_init_attr *init_attr,
1424 				struct ib_udata *udata);
1425 int mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata);
1426 int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
1427 		      u32 wq_attr_mask, struct ib_udata *udata);
1428 int mlx5_ib_create_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_table,
1429 				 struct ib_rwq_ind_table_init_attr *init_attr,
1430 				 struct ib_udata *udata);
1431 int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *wq_ind_table);
1432 struct ib_mr *mlx5_ib_reg_dm_mr(struct ib_pd *pd, struct ib_dm *dm,
1433 				struct ib_dm_mr_attr *attr,
1434 				struct uverbs_attr_bundle *attrs);
1435 void mlx5_ib_data_direct_bind(struct mlx5_ib_dev *ibdev,
1436 			      struct mlx5_data_direct_dev *dev);
1437 void mlx5_ib_data_direct_unbind(struct mlx5_ib_dev *ibdev);
1438 void mlx5_ib_revoke_data_direct_mrs(struct mlx5_ib_dev *dev);
1439 
1440 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1441 int mlx5_ib_odp_init_one(struct mlx5_ib_dev *ibdev);
1442 int mlx5r_odp_create_eq(struct mlx5_ib_dev *dev, struct mlx5_ib_pf_eq *eq);
1443 void mlx5_ib_odp_cleanup_one(struct mlx5_ib_dev *ibdev);
1444 int __init mlx5_ib_odp_init(void);
1445 void mlx5_ib_odp_cleanup(void);
1446 int mlx5_odp_init_mkey_cache(struct mlx5_ib_dev *dev);
1447 void mlx5_odp_populate_xlt(void *xlt, size_t idx, size_t nentries,
1448 			   struct mlx5_ib_mr *mr, int flags);
1449 
1450 int mlx5_ib_advise_mr_prefetch(struct ib_pd *pd,
1451 			       enum ib_uverbs_advise_mr_advice advice,
1452 			       u32 flags, struct ib_sge *sg_list, u32 num_sge);
1453 int mlx5_ib_init_odp_mr(struct mlx5_ib_mr *mr);
1454 int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr);
1455 #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
1456 static inline int mlx5_ib_odp_init_one(struct mlx5_ib_dev *ibdev) { return 0; }
1457 static inline int mlx5r_odp_create_eq(struct mlx5_ib_dev *dev,
1458 				      struct mlx5_ib_pf_eq *eq)
1459 {
1460 	return 0;
1461 }
1462 static inline void mlx5_ib_odp_cleanup_one(struct mlx5_ib_dev *ibdev) {}
1463 static inline int mlx5_ib_odp_init(void) { return 0; }
1464 static inline void mlx5_ib_odp_cleanup(void)				    {}
1465 static inline int mlx5_odp_init_mkey_cache(struct mlx5_ib_dev *dev)
1466 {
1467 	return 0;
1468 }
1469 static inline void mlx5_odp_populate_xlt(void *xlt, size_t idx, size_t nentries,
1470 					 struct mlx5_ib_mr *mr, int flags) {}
1471 
1472 static inline int
1473 mlx5_ib_advise_mr_prefetch(struct ib_pd *pd,
1474 			   enum ib_uverbs_advise_mr_advice advice, u32 flags,
1475 			   struct ib_sge *sg_list, u32 num_sge)
1476 {
1477 	return -EOPNOTSUPP;
1478 }
1479 static inline int mlx5_ib_init_odp_mr(struct mlx5_ib_mr *mr)
1480 {
1481 	return -EOPNOTSUPP;
1482 }
1483 static inline int mlx5_ib_init_dmabuf_mr(struct mlx5_ib_mr *mr)
1484 {
1485 	return -EOPNOTSUPP;
1486 }
1487 #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
1488 
1489 extern const struct mmu_interval_notifier_ops mlx5_mn_ops;
1490 
1491 /* Needed for rep profile */
1492 void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
1493 		      const struct mlx5_ib_profile *profile,
1494 		      int stage);
1495 int __mlx5_ib_add(struct mlx5_ib_dev *dev,
1496 		  const struct mlx5_ib_profile *profile);
1497 
1498 int mlx5_ib_get_vf_config(struct ib_device *device, int vf,
1499 			  u32 port, struct ifla_vf_info *info);
1500 int mlx5_ib_set_vf_link_state(struct ib_device *device, int vf,
1501 			      u32 port, int state);
1502 int mlx5_ib_get_vf_stats(struct ib_device *device, int vf,
1503 			 u32 port, struct ifla_vf_stats *stats);
1504 int mlx5_ib_get_vf_guid(struct ib_device *device, int vf, u32 port,
1505 			struct ifla_vf_guid *node_guid,
1506 			struct ifla_vf_guid *port_guid);
1507 int mlx5_ib_set_vf_guid(struct ib_device *device, int vf, u32 port,
1508 			u64 guid, int type);
1509 
1510 __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev,
1511 				   const struct ib_gid_attr *attr);
1512 
1513 void mlx5_ib_cleanup_cong_debugfs(struct mlx5_ib_dev *dev, u32 port_num);
1514 void mlx5_ib_init_cong_debugfs(struct mlx5_ib_dev *dev, u32 port_num);
1515 
1516 /* GSI QP helper functions */
1517 int mlx5_ib_create_gsi(struct ib_pd *pd, struct mlx5_ib_qp *mqp,
1518 		       struct ib_qp_init_attr *attr);
1519 int mlx5_ib_destroy_gsi(struct mlx5_ib_qp *mqp);
1520 int mlx5_ib_gsi_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
1521 			  int attr_mask);
1522 int mlx5_ib_gsi_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
1523 			 int qp_attr_mask,
1524 			 struct ib_qp_init_attr *qp_init_attr);
1525 int mlx5_ib_gsi_post_send(struct ib_qp *qp, const struct ib_send_wr *wr,
1526 			  const struct ib_send_wr **bad_wr);
1527 int mlx5_ib_gsi_post_recv(struct ib_qp *qp, const struct ib_recv_wr *wr,
1528 			  const struct ib_recv_wr **bad_wr);
1529 void mlx5_ib_gsi_pkey_change(struct mlx5_ib_gsi_qp *gsi);
1530 
1531 int mlx5_ib_generate_wc(struct ib_cq *ibcq, struct ib_wc *wc);
1532 
1533 void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi,
1534 			int bfregn);
1535 struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi);
1536 struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *dev,
1537 						   u32 ib_port_num,
1538 						   u32 *native_port_num);
1539 void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *dev,
1540 				  u32 port_num);
1541 
1542 extern const struct uapi_definition mlx5_ib_devx_defs[];
1543 extern const struct uapi_definition mlx5_ib_flow_defs[];
1544 extern const struct uapi_definition mlx5_ib_qos_defs[];
1545 extern const struct uapi_definition mlx5_ib_std_types_defs[];
1546 extern const struct uapi_definition mlx5_ib_create_cq_defs[];
1547 
1548 static inline int is_qp1(enum ib_qp_type qp_type)
1549 {
1550 	return qp_type == MLX5_IB_QPT_HW_GSI || qp_type == IB_QPT_GSI;
1551 }
1552 
1553 static inline u32 check_cq_create_flags(u32 flags)
1554 {
1555 	/*
1556 	 * It returns non-zero value for unsupported CQ
1557 	 * create flags, otherwise it returns zero.
1558 	 */
1559 	return (flags & ~(IB_UVERBS_CQ_FLAGS_IGNORE_OVERRUN |
1560 			  IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION));
1561 }
1562 
1563 static inline int verify_assign_uidx(u8 cqe_version, u32 cmd_uidx,
1564 				     u32 *user_index)
1565 {
1566 	if (cqe_version) {
1567 		if ((cmd_uidx == MLX5_IB_DEFAULT_UIDX) ||
1568 		    (cmd_uidx & ~MLX5_USER_ASSIGNED_UIDX_MASK))
1569 			return -EINVAL;
1570 		*user_index = cmd_uidx;
1571 	} else {
1572 		*user_index = MLX5_IB_DEFAULT_UIDX;
1573 	}
1574 
1575 	return 0;
1576 }
1577 
1578 static inline int get_qp_user_index(struct mlx5_ib_ucontext *ucontext,
1579 				    struct mlx5_ib_create_qp *ucmd,
1580 				    int inlen,
1581 				    u32 *user_index)
1582 {
1583 	u8 cqe_version = ucontext->cqe_version;
1584 
1585 	if ((offsetofend(typeof(*ucmd), uidx) <= inlen) && !cqe_version &&
1586 	    (ucmd->uidx == MLX5_IB_DEFAULT_UIDX))
1587 		return 0;
1588 
1589 	if ((offsetofend(typeof(*ucmd), uidx) <= inlen) != !!cqe_version)
1590 		return -EINVAL;
1591 
1592 	return verify_assign_uidx(cqe_version, ucmd->uidx, user_index);
1593 }
1594 
1595 static inline int get_srq_user_index(struct mlx5_ib_ucontext *ucontext,
1596 				     struct mlx5_ib_create_srq *ucmd,
1597 				     int inlen,
1598 				     u32 *user_index)
1599 {
1600 	u8 cqe_version = ucontext->cqe_version;
1601 
1602 	if ((offsetofend(typeof(*ucmd), uidx) <= inlen) && !cqe_version &&
1603 	    (ucmd->uidx == MLX5_IB_DEFAULT_UIDX))
1604 		return 0;
1605 
1606 	if ((offsetofend(typeof(*ucmd), uidx) <= inlen) != !!cqe_version)
1607 		return -EINVAL;
1608 
1609 	return verify_assign_uidx(cqe_version, ucmd->uidx, user_index);
1610 }
1611 
1612 static inline int get_uars_per_sys_page(struct mlx5_ib_dev *dev, bool lib_support)
1613 {
1614 	return lib_support && MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1615 				MLX5_UARS_IN_PAGE : 1;
1616 }
1617 
1618 extern void *xlt_emergency_page;
1619 
1620 int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
1621 			struct mlx5_bfreg_info *bfregi, u32 bfregn,
1622 			bool dyn_bfreg);
1623 
1624 static inline int mlx5r_store_odp_mkey(struct mlx5_ib_dev *dev,
1625 				       struct mlx5_ib_mkey *mmkey)
1626 {
1627 	refcount_set(&mmkey->usecount, 1);
1628 
1629 	return xa_err(xa_store(&dev->odp_mkeys, mlx5_base_mkey(mmkey->key),
1630 			       mmkey, GFP_KERNEL));
1631 }
1632 
1633 /* deref an mkey that can participate in ODP flow */
1634 static inline void mlx5r_deref_odp_mkey(struct mlx5_ib_mkey *mmkey)
1635 {
1636 	if (refcount_dec_and_test(&mmkey->usecount))
1637 		wake_up(&mmkey->wait);
1638 }
1639 
1640 /* deref an mkey that can participate in ODP flow and wait for relese */
1641 static inline void mlx5r_deref_wait_odp_mkey(struct mlx5_ib_mkey *mmkey)
1642 {
1643 	mlx5r_deref_odp_mkey(mmkey);
1644 	wait_event(mmkey->wait, refcount_read(&mmkey->usecount) == 0);
1645 }
1646 
1647 static inline bool mlx5_ib_lag_should_assign_affinity(struct mlx5_ib_dev *dev)
1648 {
1649 	/*
1650 	 * If the driver is in hash mode and the port_select_flow_table_bypass cap
1651 	 * is supported, it means that the driver no longer needs to assign the port
1652 	 * affinity by default. If a user wants to set the port affinity explicitly,
1653 	 * the user has a dedicated API to do that, so there is no need to assign
1654 	 * the port affinity by default.
1655 	 */
1656 	if (dev->lag_active &&
1657 	    mlx5_lag_mode_is_hash(dev->mdev) &&
1658 	    MLX5_CAP_PORT_SELECTION(dev->mdev, port_select_flow_table_bypass))
1659 		return 0;
1660 
1661 	if (mlx5_lag_is_lacp_owner(dev->mdev) && !dev->lag_active)
1662 		return 0;
1663 
1664 	return dev->lag_active ||
1665 		(MLX5_CAP_GEN(dev->mdev, num_lag_ports) > 1 &&
1666 		 MLX5_CAP_GEN(dev->mdev, lag_tx_port_affinity));
1667 }
1668 
1669 static inline bool rt_supported(int ts_cap)
1670 {
1671 	return ts_cap == MLX5_TIMESTAMP_FORMAT_CAP_REAL_TIME ||
1672 	       ts_cap == MLX5_TIMESTAMP_FORMAT_CAP_FREE_RUNNING_AND_REAL_TIME;
1673 }
1674 
1675 /*
1676  * PCI Peer to Peer is a trainwreck. If no switch is present then things
1677  * sometimes work, depending on the pci_distance_p2p logic for excluding broken
1678  * root complexes. However if a switch is present in the path, then things get
1679  * really ugly depending on how the switch is setup. This table assumes that the
1680  * root complex is strict and is validating that all req/reps are matches
1681  * perfectly - so any scenario where it sees only half the transaction is a
1682  * failure.
1683  *
1684  * CR/RR/DT  ATS RO P2P
1685  * 00X       X   X  OK
1686  * 010       X   X  fails (request is routed to root but root never sees comp)
1687  * 011       0   X  fails (request is routed to root but root never sees comp)
1688  * 011       1   X  OK
1689  * 10X       X   1  OK
1690  * 101       X   0  fails (completion is routed to root but root didn't see req)
1691  * 110       X   0  SLOW
1692  * 111       0   0  SLOW
1693  * 111       1   0  fails (completion is routed to root but root didn't see req)
1694  * 111       1   1  OK
1695  *
1696  * Unfortunately we cannot reliably know if a switch is present or what the
1697  * CR/RR/DT ACS settings are, as in a VM that is all hidden. Assume that
1698  * CR/RR/DT is 111 if the ATS cap is enabled and follow the last three rows.
1699  *
1700  * For now assume if the umem is a dma_buf then it is P2P.
1701  */
1702 static inline bool mlx5_umem_needs_ats(struct mlx5_ib_dev *dev,
1703 				       struct ib_umem *umem, int access_flags)
1704 {
1705 	if (!MLX5_CAP_GEN(dev->mdev, ats) || !umem->is_dmabuf)
1706 		return false;
1707 	return access_flags & IB_ACCESS_RELAXED_ORDERING;
1708 }
1709 
1710 int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num,
1711 		  unsigned int index, const union ib_gid *gid,
1712 		  const struct ib_gid_attr *attr);
1713 
1714 static inline u32 smi_to_native_portnum(struct mlx5_ib_dev *dev, u32 port)
1715 {
1716 	return (port - 1) / dev->num_ports + 1;
1717 }
1718 
1719 /*
1720  * For mkc users, instead of a page_offset the command has a start_iova which
1721  * specifies both the page_offset and the on-the-wire IOVA
1722  */
1723 static __always_inline unsigned long
1724 mlx5_umem_mkc_find_best_pgsz(struct mlx5_ib_dev *dev, struct ib_umem *umem,
1725 			     u64 iova)
1726 {
1727 	int page_size_bits =
1728 		MLX5_CAP_GEN_2(dev->mdev, umr_log_entity_size_5) ? 6 : 5;
1729 	unsigned long bitmap =
1730 		__mlx5_log_page_size_to_bitmap(page_size_bits, 0);
1731 
1732 	return ib_umem_find_best_pgsz(umem, bitmap, iova);
1733 }
1734 
1735 #endif /* MLX5_IB_H */
1736