1322810Shselasky/*-
2322810Shselasky * Copyright (c) 2013-2015, Mellanox Technologies, Ltd.  All rights reserved.
3322810Shselasky *
4322810Shselasky * Redistribution and use in source and binary forms, with or without
5322810Shselasky * modification, are permitted provided that the following conditions
6322810Shselasky * are met:
7322810Shselasky * 1. Redistributions of source code must retain the above copyright
8322810Shselasky *    notice, this list of conditions and the following disclaimer.
9322810Shselasky * 2. Redistributions in binary form must reproduce the above copyright
10322810Shselasky *    notice, this list of conditions and the following disclaimer in the
11322810Shselasky *    documentation and/or other materials provided with the distribution.
12322810Shselasky *
13322810Shselasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14322810Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15322810Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16322810Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17322810Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18322810Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19322810Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20322810Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21322810Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22322810Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23322810Shselasky * SUCH DAMAGE.
24322810Shselasky *
25322810Shselasky * $FreeBSD: stable/10/sys/dev/mlx5/mlx5_ib/mlx5_ib.h 323223 2017-09-06 15:33:23Z hselasky $
26322810Shselasky */
27322810Shselasky
28322810Shselasky#ifndef MLX5_IB_H
29322810Shselasky#define MLX5_IB_H
30322810Shselasky
31322810Shselasky#include <linux/kernel.h>
32322810Shselasky#include <linux/sched.h>
33322810Shselasky#include <rdma/ib_verbs.h>
34322810Shselasky#include <rdma/ib_smi.h>
35322810Shselasky#include <rdma/ib_addr.h>
36322810Shselasky#include <dev/mlx5/device.h>
37322810Shselasky#include <dev/mlx5/driver.h>
38322810Shselasky#include <dev/mlx5/cq.h>
39322810Shselasky#include <dev/mlx5/qp.h>
40322810Shselasky#include <dev/mlx5/srq.h>
41322810Shselasky#include <linux/types.h>
42322810Shselasky#include <dev/mlx5/mlx5_core/transobj.h>
43322810Shselasky
44322810Shselasky#define mlx5_ib_dbg(dev, format, arg...)				\
45322810Shselaskypr_debug("mlx5_dbg:%s:%s:%d:(pid %d): " format, (dev)->ib_dev.name, __func__,	\
46322810Shselasky	 __LINE__, curthread->td_proc->p_pid, ##arg)
47322810Shselasky
48322810Shselasky#define mlx5_ib_err(dev, format, arg...)				\
49322810Shselaskyprintf("mlx5_ib: ERR: ""mlx5_err:%s:%s:%d:(pid %d): " format, (dev)->ib_dev.name, __func__, \
50322810Shselasky	__LINE__, curthread->td_proc->p_pid, ##arg)
51322810Shselasky
52322810Shselasky#define mlx5_ib_warn(dev, format, arg...)				\
53322810Shselaskyprintf("mlx5_ib: WARN: ""mlx5_warn:%s:%s:%d:(pid %d): " format, (dev)->ib_dev.name, __func__, \
54322810Shselasky	__LINE__, curthread->td_proc->p_pid, ##arg)
55322810Shselasky#define BF_ENABLE 0
56322810Shselasky
57322810Shselaskyextern struct workqueue_struct *mlx5_ib_wq;
58322810Shselasky
59322810Shselaskyenum {
60322810Shselasky	MLX5_IB_MMAP_CMD_SHIFT	= 8,
61322810Shselasky	MLX5_IB_MMAP_CMD_MASK	= 0xff,
62322810Shselasky};
63322810Shselasky
64322810Shselaskyenum mlx5_ib_mmap_cmd {
65322810Shselasky	MLX5_IB_MMAP_REGULAR_PAGE		= 0,
66322810Shselasky	MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES	= 1,
67322810Shselasky	MLX5_IB_MMAP_WC_PAGE			= 2,
68322810Shselasky	MLX5_IB_MMAP_NC_PAGE			= 3,
69322810Shselasky	MLX5_IB_MMAP_MAP_DC_INFO_PAGE		= 4,
70322810Shselasky
71322810Shselasky	/* Use EXP mmap commands until it is pushed to upstream */
72322810Shselasky	MLX5_IB_EXP_MMAP_CORE_CLOCK			= 0xFB,
73322810Shselasky	MLX5_IB_EXP_MMAP_GET_CONTIGUOUS_PAGES_CPU_NUMA	= 0xFC,
74322810Shselasky	MLX5_IB_EXP_MMAP_GET_CONTIGUOUS_PAGES_DEV_NUMA	= 0xFD,
75322810Shselasky	MLX5_IB_EXP_ALLOC_N_MMAP_WC			= 0xFE,
76322810Shselasky};
77322810Shselasky
78322810Shselaskyenum {
79322810Shselasky	MLX5_RES_SCAT_DATA32_CQE	= 0x1,
80322810Shselasky	MLX5_RES_SCAT_DATA64_CQE	= 0x2,
81322810Shselasky	MLX5_REQ_SCAT_DATA32_CQE	= 0x11,
82322810Shselasky	MLX5_REQ_SCAT_DATA64_CQE	= 0x22,
83322810Shselasky};
84322810Shselasky
85322810Shselaskyenum {
86322810Shselasky	MLX5_DCT_CS_RES_64		= 2,
87322810Shselasky	MLX5_CNAK_RX_POLL_CQ_QUOTA	= 256,
88322810Shselasky};
89322810Shselasky
90322810Shselaskyenum mlx5_ib_latency_class {
91322810Shselasky	MLX5_IB_LATENCY_CLASS_LOW,
92322810Shselasky	MLX5_IB_LATENCY_CLASS_MEDIUM,
93322810Shselasky	MLX5_IB_LATENCY_CLASS_HIGH,
94322810Shselasky	MLX5_IB_LATENCY_CLASS_FAST_PATH
95322810Shselasky};
96322810Shselasky
97322810Shselaskyenum mlx5_ib_mad_ifc_flags {
98322810Shselasky	MLX5_MAD_IFC_IGNORE_MKEY	= 1,
99322810Shselasky	MLX5_MAD_IFC_IGNORE_BKEY	= 2,
100322810Shselasky	MLX5_MAD_IFC_NET_VIEW		= 4,
101322810Shselasky};
102322810Shselasky
103322810Shselaskyenum {
104322810Shselasky	MLX5_CROSS_CHANNEL_UUAR		= 0,
105322810Shselasky};
106322810Shselasky
107322810Shselaskyenum {
108322810Shselasky	MLX5_IB_MAX_CTX_DYNAMIC_UARS = 256,
109322810Shselasky	MLX5_IB_INVALID_UAR_INDEX = -1U
110322810Shselasky};
111322810Shselasky
112322810Shselaskyenum {
113322810Shselasky	MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES	= 13,
114322810Shselasky	MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES	= 6,
115322810Shselasky	MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES	= 16,
116322810Shselasky	MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES	= 9,
117322810Shselasky};
118322810Shselasky
119322810Shselaskystruct mlx5_ib_ucontext {
120322810Shselasky	struct ib_ucontext	ibucontext;
121322810Shselasky	struct list_head	db_page_list;
122322810Shselasky
123322810Shselasky	/* protect doorbell record alloc/free
124322810Shselasky	 */
125322810Shselasky	struct mutex		db_page_mutex;
126322810Shselasky	struct mlx5_uuar_info	uuari;
127322810Shselasky	u32			dynamic_wc_uar_index[MLX5_IB_MAX_CTX_DYNAMIC_UARS];
128322810Shselasky	/* Transport Domain number */
129322810Shselasky	u32			tdn;
130322810Shselasky};
131322810Shselasky
132322810Shselaskystatic inline struct mlx5_ib_ucontext *to_mucontext(struct ib_ucontext *ibucontext)
133322810Shselasky{
134322810Shselasky	return container_of(ibucontext, struct mlx5_ib_ucontext, ibucontext);
135322810Shselasky}
136322810Shselasky
137322810Shselaskystruct mlx5_ib_pd {
138322810Shselasky	struct ib_pd		ibpd;
139322810Shselasky	u32			pdn;
140322810Shselasky	u32			pa_lkey;
141322810Shselasky};
142322810Shselasky
143322810Shselaskystruct wr_list {
144322810Shselasky	u16	opcode;
145322810Shselasky	u16	next;
146322810Shselasky};
147322810Shselasky
148322810Shselaskystruct mlx5_swr_ctx {
149322810Shselasky	u64		wrid;
150322810Shselasky	u32		wr_data;
151322810Shselasky	struct wr_list	w_list;
152322810Shselasky	u32		wqe_head;
153322810Shselasky	u8		sig_piped;
154322810Shselasky	u8		rsvd[11];
155322810Shselasky};
156322810Shselasky
157322810Shselaskystruct mlx5_rwr_ctx {
158322810Shselasky	u64		       wrid;
159322810Shselasky};
160322810Shselasky
161322810Shselaskystruct mlx5_ib_wq {
162322810Shselasky	union {
163322810Shselasky		struct mlx5_swr_ctx *swr_ctx;
164322810Shselasky		struct mlx5_rwr_ctx *rwr_ctx;
165322810Shselasky	};
166322810Shselasky	u16		        unsig_count;
167322810Shselasky
168322810Shselasky	/* serialize post to the work queue
169322810Shselasky	 */
170322810Shselasky	spinlock_t		lock;
171322810Shselasky	int			wqe_cnt;
172322810Shselasky	int			max_post;
173322810Shselasky	int			max_gs;
174322810Shselasky	int			offset;
175322810Shselasky	int			wqe_shift;
176322810Shselasky	unsigned		head;
177322810Shselasky	unsigned		tail;
178322810Shselasky	u16			cur_post;
179322810Shselasky	u16			last_poll;
180322810Shselasky	void		       *qend;
181322810Shselasky};
182322810Shselasky
183322810Shselaskyenum {
184322810Shselasky	MLX5_QP_USER,
185322810Shselasky	MLX5_QP_KERNEL,
186322810Shselasky	MLX5_QP_EMPTY
187322810Shselasky};
188322810Shselasky
189322810Shselaskyenum {
190322810Shselasky	MLX5_WQ_USER,
191322810Shselasky	MLX5_WQ_KERNEL
192322810Shselasky};
193322810Shselasky
194322810Shselaskystruct mlx5_ib_sqd {
195322810Shselasky	struct mlx5_ib_qp	*qp;
196322810Shselasky	struct work_struct	work;
197322810Shselasky};
198322810Shselasky
199322810Shselaskystruct mlx5_ib_mc_flows_list {
200322810Shselasky	struct list_head		flows_list;
201322810Shselasky	/*Protect the flows_list*/
202322810Shselasky	struct mutex		lock;
203322810Shselasky};
204322810Shselasky
205322810Shselaskystruct mlx5_ib_qp {
206322810Shselasky	struct ib_qp		ibqp;
207322810Shselasky	struct mlx5_core_qp	mqp;
208322810Shselasky	struct mlx5_core_qp	mrq;
209322810Shselasky	struct mlx5_core_qp	msq;
210322810Shselasky	u32			tisn;
211322810Shselasky	u32			tirn;
212322810Shselasky	struct mlx5_buf		buf;
213322810Shselasky
214322810Shselasky	struct mlx5_db		db;
215322810Shselasky	struct mlx5_ib_wq	rq;
216322810Shselasky
217322810Shselasky	u32			doorbell_qpn;
218322810Shselasky	u8			sq_signal_bits;
219322810Shselasky	u8			fm_cache;
220322810Shselasky	int			sq_max_wqes_per_wr;
221322810Shselasky	int			sq_spare_wqes;
222322810Shselasky	struct mlx5_ib_wq	sq;
223322810Shselasky
224322810Shselasky	struct ib_umem	       *umem;
225322810Shselasky	int			buf_size;
226322810Shselasky	/* Raw Ethernet QP's SQ is allocated seperately
227322810Shselasky	 * from the RQ's buffer in user-space.
228322810Shselasky	 */
229322810Shselasky	struct ib_umem	       *sq_umem;
230322810Shselasky	int			sq_buf_size;
231322810Shselasky	u64			sq_buf_addr;
232322810Shselasky	int			allow_mp_wqe;
233322810Shselasky
234322810Shselasky	/* serialize qp state modifications
235322810Shselasky	 */
236322810Shselasky	struct mutex		mutex;
237322810Shselasky	u16			xrcdn;
238322810Shselasky	u32			flags;
239322810Shselasky	u8			port;
240322810Shselasky	u8			alt_port;
241322810Shselasky	u8			atomic_rd_en;
242322810Shselasky	u8			resp_depth;
243322810Shselasky	u8			state;
244322810Shselasky	/* Raw Ethernet QP's SQ and RQ states */
245322810Shselasky	u8			rq_state;
246322810Shselasky	u8			sq_state;
247322810Shselasky	int			mlx_type;
248322810Shselasky	int			wq_sig;
249322810Shselasky	int			scat_cqe;
250322810Shselasky	int			max_inline_data;
251322810Shselasky	struct mlx5_bf	       *bf;
252322810Shselasky	int			has_rq;
253322810Shselasky
254322810Shselasky	/* only for user space QPs. For kernel
255322810Shselasky	 * we have it from the bf object
256322810Shselasky	 */
257322810Shselasky	int			uuarn;
258322810Shselasky
259322810Shselasky	int			create_type;
260322810Shselasky	u32			pa_lkey;
261322810Shselasky
262322810Shselasky	/* Store signature errors */
263322810Shselasky	bool			signature_en;
264322810Shselasky
265322810Shselasky	struct list_head	qps_list;
266322810Shselasky	struct list_head	cq_recv_list;
267322810Shselasky	struct list_head	cq_send_list;
268322810Shselasky
269322810Shselasky	struct mlx5_ib_mc_flows_list mc_flows_list;
270322810Shselasky};
271322810Shselasky
272322810Shselaskystruct mlx5_ib_cq_buf {
273322810Shselasky	struct mlx5_buf		buf;
274322810Shselasky	struct ib_umem		*umem;
275322810Shselasky	int			cqe_size;
276322810Shselasky	int			nent;
277322810Shselasky};
278322810Shselasky
279322810Shselaskyenum mlx5_ib_qp_flags {
280322810Shselasky	MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK     = 1 << 0,
281322810Shselasky	MLX5_IB_QP_SIGNATURE_HANDLING           = 1 << 1,
282322810Shselasky	MLX5_IB_QP_CAP_RX_END_PADDING		= 1 << 5,
283322810Shselasky};
284322810Shselasky
285322810Shselaskystruct mlx5_umr_wr {
286322810Shselasky	union {
287322810Shselasky		u64			virt_addr;
288322810Shselasky		u64			offset;
289322810Shselasky	} target;
290322810Shselasky	struct ib_pd		       *pd;
291322810Shselasky	unsigned int			page_shift;
292322810Shselasky	unsigned int			npages;
293322810Shselasky	u64				length;
294322810Shselasky	int				access_flags;
295322810Shselasky	u32				mkey;
296322810Shselasky};
297322810Shselasky
298322810Shselaskystruct mlx5_shared_mr_info {
299322810Shselasky	int mr_id;
300322810Shselasky	struct ib_umem		*umem;
301322810Shselasky};
302322810Shselasky
303322810Shselaskystruct mlx5_ib_cq {
304322810Shselasky	struct ib_cq		ibcq;
305322810Shselasky	struct mlx5_core_cq	mcq;
306322810Shselasky	struct mlx5_ib_cq_buf	buf;
307322810Shselasky	struct mlx5_db		db;
308322810Shselasky
309322810Shselasky	/* serialize access to the CQ
310322810Shselasky	 */
311322810Shselasky	spinlock_t		lock;
312322810Shselasky
313322810Shselasky	/* protect resize cq
314322810Shselasky	 */
315322810Shselasky	struct mutex		resize_mutex;
316322810Shselasky	struct mlx5_ib_cq_buf  *resize_buf;
317322810Shselasky	struct ib_umem	       *resize_umem;
318322810Shselasky	int			cqe_size;
319322810Shselasky	struct list_head		list_send_qp;
320322810Shselasky	struct list_head		list_recv_qp;
321322810Shselasky};
322322810Shselasky
323322810Shselaskystruct mlx5_ib_srq {
324322810Shselasky	struct ib_srq		ibsrq;
325322810Shselasky	struct mlx5_core_srq	msrq;
326322810Shselasky	struct mlx5_buf		buf;
327322810Shselasky	struct mlx5_db		db;
328322810Shselasky	u64		       *wrid;
329322810Shselasky	/* protect SRQ hanlding
330322810Shselasky	 */
331322810Shselasky	spinlock_t		lock;
332322810Shselasky	int			head;
333322810Shselasky	int			tail;
334322810Shselasky	u16			wqe_ctr;
335322810Shselasky	struct ib_umem	       *umem;
336322810Shselasky	/* serialize arming a SRQ
337322810Shselasky	 */
338322810Shselasky	struct mutex		mutex;
339322810Shselasky	int			wq_sig;
340322810Shselasky};
341322810Shselasky
342322810Shselaskystruct mlx5_ib_xrcd {
343322810Shselasky	struct ib_xrcd		ibxrcd;
344322810Shselasky	u32			xrcdn;
345322810Shselasky};
346322810Shselasky
347322810Shselaskyenum mlx5_ib_mtt_access_flags {
348322810Shselasky	MLX5_IB_MTT_READ  = (1 << 0),
349322810Shselasky	MLX5_IB_MTT_WRITE = (1 << 1),
350322810Shselasky};
351322810Shselasky
352322810Shselasky#define MLX5_IB_MTT_PRESENT (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE)
353322810Shselasky
354322810Shselaskystruct mlx5_ib_mr {
355322810Shselasky	struct ib_mr		ibmr;
356322810Shselasky	struct mlx5_core_mr	mmr;
357322810Shselasky	struct ib_umem	       *umem;
358322810Shselasky	struct mlx5_shared_mr_info	*smr_info;
359322810Shselasky	struct list_head	list;
360322810Shselasky	int			order;
361322810Shselasky	int			umred;
362322810Shselasky	dma_addr_t		dma;
363322810Shselasky	int			npages;
364322810Shselasky	struct mlx5_ib_dev     *dev;
365322810Shselasky	struct mlx5_create_mkey_mbox_out out;
366322810Shselasky	struct mlx5_core_sig_ctx    *sig;
367322810Shselasky	u32			max_reg_descriptors;
368322810Shselasky	u64			size;
369322810Shselasky	u64			page_count;
370322810Shselasky	struct mlx5_ib_mr     **children;
371322810Shselasky	int			nchild;
372322810Shselasky};
373322810Shselasky
374322810Shselaskystruct mlx5_ib_fast_reg_page_list {
375322810Shselasky	struct ib_fast_reg_page_list	ibfrpl;
376322810Shselasky	__be64			       *mapped_page_list;
377322810Shselasky	dma_addr_t			map;
378322810Shselasky};
379322810Shselasky
380322810Shselaskystruct mlx5_ib_umr_context {
381322810Shselasky	enum ib_wc_status	status;
382322810Shselasky	struct completion	done;
383322810Shselasky};
384322810Shselasky
385322810Shselaskystatic inline void mlx5_ib_init_umr_context(struct mlx5_ib_umr_context *context)
386322810Shselasky{
387322810Shselasky	context->status = -1;
388322810Shselasky	init_completion(&context->done);
389322810Shselasky}
390322810Shselasky
391322810Shselaskystruct umr_common {
392322810Shselasky	struct ib_pd	*pd;
393322810Shselasky	struct ib_mr	*mr;
394322810Shselasky};
395322810Shselasky
396322810Shselaskyenum {
397322810Shselasky	MLX5_FMR_INVALID,
398322810Shselasky	MLX5_FMR_VALID,
399322810Shselasky	MLX5_FMR_BUSY,
400322810Shselasky};
401322810Shselasky
402322810Shselaskystruct mlx5_ib_fmr {
403322810Shselasky	struct ib_fmr			ibfmr;
404322810Shselasky	struct mlx5_core_mr		mr;
405322810Shselasky	int				access_flags;
406322810Shselasky	int				state;
407322810Shselasky	/* protect fmr state
408322810Shselasky	 */
409322810Shselasky	spinlock_t			lock;
410322810Shselasky	u64				wrid;
411322810Shselasky	struct ib_send_wr		wr[2];
412322810Shselasky	u8				page_shift;
413322810Shselasky	struct ib_fast_reg_page_list	page_list;
414322810Shselasky};
415322810Shselasky
416322810Shselaskystruct cache_order {
417322810Shselasky	struct kobject		kobj;
418322810Shselasky	int			order;
419322810Shselasky	int			index;
420322810Shselasky	struct mlx5_ib_dev     *dev;
421322810Shselasky};
422322810Shselasky
423322810Shselaskystruct mlx5_cache_ent {
424322810Shselasky	struct list_head	head;
425322810Shselasky	/* sync access to the cahce entry
426322810Shselasky	 */
427322810Shselasky	spinlock_t		lock;
428322810Shselasky
429322810Shselasky
430322810Shselasky	u32                     order;
431322810Shselasky	u32			size;
432322810Shselasky	u32                     cur;
433322810Shselasky	u32                     miss;
434322810Shselasky	u32			limit;
435322810Shselasky
436322810Shselasky	struct mlx5_ib_dev     *dev;
437322810Shselasky	struct work_struct	work;
438322810Shselasky	struct delayed_work	dwork;
439322810Shselasky	int			pending;
440322810Shselasky	struct cache_order	co;
441322810Shselasky};
442322810Shselasky
443322810Shselaskystruct mlx5_mr_cache {
444322810Shselasky	struct workqueue_struct *wq;
445322810Shselasky	struct mlx5_cache_ent	ent[MAX_MR_CACHE_ENTRIES];
446322810Shselasky	int			stopped;
447322810Shselasky	struct dentry		*root;
448322810Shselasky	int		last_add;
449322810Shselasky	int			rel_timeout;
450322810Shselasky	int			rel_imm;
451322810Shselasky};
452322810Shselasky
453322810Shselaskystruct mlx5_ib_resources {
454322810Shselasky	struct ib_cq	*c0;
455322810Shselasky	struct ib_xrcd	*x0;
456322810Shselasky	struct ib_xrcd	*x1;
457322810Shselasky	struct ib_pd	*p0;
458322810Shselasky	struct ib_srq	*s0;
459322810Shselasky	struct ib_srq	*s1;
460322810Shselasky};
461322810Shselasky
462322810Shselaskystruct mlx5_dc_tracer {
463322810Shselasky	struct page	*pg;
464322810Shselasky	dma_addr_t	dma;
465322810Shselasky	int		size;
466322810Shselasky	int		order;
467322810Shselasky};
468322810Shselasky
469322810Shselaskystruct mlx5_dc_desc {
470322810Shselasky	dma_addr_t	dma;
471322810Shselasky	void		*buf;
472322810Shselasky};
473322810Shselasky
474322810Shselaskyenum mlx5_op {
475322810Shselasky	MLX5_WR_OP_MLX	= 1,
476322810Shselasky};
477322810Shselasky
478322810Shselaskystruct mlx5_mlx_wr {
479322810Shselasky	u8	sl;
480322810Shselasky	u16	dlid;
481322810Shselasky	int	icrc;
482322810Shselasky};
483322810Shselasky
484322810Shselaskystruct mlx5_send_wr {
485322810Shselasky	struct ib_send_wr	wr;
486322810Shselasky	union {
487322810Shselasky		struct mlx5_mlx_wr	mlx;
488322810Shselasky	} sel;
489322810Shselasky};
490322810Shselasky
491322810Shselaskystruct mlx5_dc_data {
492322810Shselasky	struct ib_mr		*mr;
493322810Shselasky	struct ib_qp		*dcqp;
494322810Shselasky	struct ib_cq		*rcq;
495322810Shselasky	struct ib_cq		*scq;
496322810Shselasky	unsigned int		rx_npages;
497322810Shselasky	unsigned int		tx_npages;
498322810Shselasky	struct mlx5_dc_desc	*rxdesc;
499322810Shselasky	struct mlx5_dc_desc	*txdesc;
500322810Shselasky	unsigned int		max_wqes;
501322810Shselasky	unsigned int		cur_send;
502322810Shselasky	unsigned int		last_send_completed;
503322810Shselasky	int			tx_pending;
504322810Shselasky	struct mlx5_ib_dev	*dev;
505322810Shselasky	int			port;
506322810Shselasky	int			initialized;
507322810Shselasky	struct kobject		kobj;
508322810Shselasky	unsigned long		connects;
509322810Shselasky	unsigned long		cnaks;
510322810Shselasky	unsigned long		discards;
511322810Shselasky	struct ib_wc		wc_tbl[MLX5_CNAK_RX_POLL_CQ_QUOTA];
512322810Shselasky};
513322810Shselasky
514322810Shselaskystruct mlx5_ib_port_sysfs_group {
515322810Shselasky	struct kobject		kobj;
516322810Shselasky	bool   enabled;
517322810Shselasky	struct attribute_group	counters;
518322810Shselasky};
519322810Shselasky
520322810Shselasky#define	MLX5_IB_GID_MAX	16
521322810Shselasky
522322810Shselaskystruct mlx5_ib_port {
523322810Shselasky	struct mlx5_ib_dev	*dev;
524322810Shselasky	u8  port_num;	/* 0 based */
525322810Shselasky	u8  port_gone;	/* set when gone */
526322810Shselasky	u16 q_cnt_id;
527322810Shselasky	struct mlx5_ib_port_sysfs_group group;
528322810Shselasky	union ib_gid gid_table[MLX5_IB_GID_MAX];
529322810Shselasky};
530322810Shselasky
531322810Shselaskystruct mlx5_ib_dev {
532322810Shselasky	struct ib_device		ib_dev;
533322810Shselasky	struct mlx5_core_dev		*mdev;
534322810Shselasky	MLX5_DECLARE_DOORBELL_LOCK(uar_lock);
535322810Shselasky	int				num_ports;
536322810Shselasky	/* serialize update of capability mask
537322810Shselasky	 */
538322810Shselasky	struct mutex			cap_mask_mutex;
539322810Shselasky	bool	ib_active;
540322810Shselasky	struct umr_common		umrc;
541322810Shselasky	/* sync used page count stats
542322810Shselasky	 */
543322810Shselasky	struct mlx5_ib_resources	devr;
544322810Shselasky	struct mutex		slow_path_mutex;
545322810Shselasky	int				enable_atomic_resp;
546322810Shselasky	enum ib_atomic_cap		atomic_cap;
547322810Shselasky	struct mlx5_mr_cache		cache;
548322810Shselasky	struct kobject               mr_cache;
549322810Shselasky	/* protect resources needed as part of reset flow */
550322810Shselasky	spinlock_t		reset_flow_resource_lock;
551322810Shselasky	struct list_head		qp_list;
552322810Shselasky	struct timer_list		delay_timer;
553322810Shselasky	int				fill_delay;
554322810Shselasky	struct mlx5_dc_tracer	dctr;
555322810Shselasky	struct mlx5_dc_data	dcd[MLX5_MAX_PORTS];
556322810Shselasky	struct kobject		*dc_kobj;
557322810Shselasky	/* Array with num_ports elements */
558322810Shselasky	struct mlx5_ib_port	*port;
559322810Shselasky	struct kobject		*ports_parent;
560322810Shselasky};
561322810Shselasky
562322810Shselaskystatic inline struct mlx5_ib_cq *to_mibcq(struct mlx5_core_cq *mcq)
563322810Shselasky{
564322810Shselasky	return container_of(mcq, struct mlx5_ib_cq, mcq);
565322810Shselasky}
566322810Shselasky
567322810Shselaskystatic inline struct mlx5_ib_xrcd *to_mxrcd(struct ib_xrcd *ibxrcd)
568322810Shselasky{
569322810Shselasky	return container_of(ibxrcd, struct mlx5_ib_xrcd, ibxrcd);
570322810Shselasky}
571322810Shselasky
572322810Shselaskystatic inline struct mlx5_ib_dev *to_mdev(struct ib_device *ibdev)
573322810Shselasky{
574322810Shselasky	return container_of(ibdev, struct mlx5_ib_dev, ib_dev);
575322810Shselasky}
576322810Shselasky
577322810Shselaskystatic inline struct mlx5_ib_fmr *to_mfmr(struct ib_fmr *ibfmr)
578322810Shselasky{
579322810Shselasky	return container_of(ibfmr, struct mlx5_ib_fmr, ibfmr);
580322810Shselasky}
581322810Shselasky
582322810Shselaskystatic inline struct mlx5_ib_cq *to_mcq(struct ib_cq *ibcq)
583322810Shselasky{
584322810Shselasky	return container_of(ibcq, struct mlx5_ib_cq, ibcq);
585322810Shselasky}
586322810Shselasky
587322810Shselaskystatic inline struct mlx5_ib_qp *to_mibqp(struct mlx5_core_qp *mqp)
588322810Shselasky{
589322810Shselasky	return container_of(mqp, struct mlx5_ib_qp, mqp);
590322810Shselasky}
591322810Shselasky
592322810Shselaskystatic inline struct mlx5_ib_qp *sq_to_mibqp(struct mlx5_core_qp *msq)
593322810Shselasky{
594322810Shselasky	return container_of(msq, struct mlx5_ib_qp, msq);
595322810Shselasky}
596322810Shselasky
597322810Shselaskystatic inline struct mlx5_ib_qp *rq_to_mibqp(struct mlx5_core_qp *mrq)
598322810Shselasky{
599322810Shselasky	return container_of(mrq, struct mlx5_ib_qp, mrq);
600322810Shselasky}
601322810Shselasky
602322810Shselaskystatic inline struct mlx5_ib_mr *to_mibmr(struct mlx5_core_mr *mmr)
603322810Shselasky{
604322810Shselasky	return container_of(mmr, struct mlx5_ib_mr, mmr);
605322810Shselasky}
606322810Shselasky
607322810Shselaskystatic inline struct mlx5_ib_pd *to_mpd(struct ib_pd *ibpd)
608322810Shselasky{
609322810Shselasky	return container_of(ibpd, struct mlx5_ib_pd, ibpd);
610322810Shselasky}
611322810Shselasky
612322810Shselaskystatic inline struct mlx5_ib_srq *to_msrq(struct ib_srq *ibsrq)
613322810Shselasky{
614322810Shselasky	return container_of(ibsrq, struct mlx5_ib_srq, ibsrq);
615322810Shselasky}
616322810Shselasky
617322810Shselaskystatic inline struct mlx5_ib_qp *to_mqp(struct ib_qp *ibqp)
618322810Shselasky{
619322810Shselasky	return container_of(ibqp, struct mlx5_ib_qp, ibqp);
620322810Shselasky}
621322810Shselasky
622322810Shselaskystatic inline struct mlx5_ib_srq *to_mibsrq(struct mlx5_core_srq *msrq)
623322810Shselasky{
624322810Shselasky	return container_of(msrq, struct mlx5_ib_srq, msrq);
625322810Shselasky}
626322810Shselasky
627322810Shselaskystatic inline struct mlx5_ib_mr *to_mmr(struct ib_mr *ibmr)
628322810Shselasky{
629322810Shselasky	return container_of(ibmr, struct mlx5_ib_mr, ibmr);
630322810Shselasky}
631322810Shselasky
632322810Shselaskystatic inline struct mlx5_ib_fast_reg_page_list *to_mfrpl(struct ib_fast_reg_page_list *ibfrpl)
633322810Shselasky{
634322810Shselasky	return container_of(ibfrpl, struct mlx5_ib_fast_reg_page_list, ibfrpl);
635322810Shselasky}
636322810Shselasky
637322810Shselaskystruct mlx5_ib_ah {
638322810Shselasky	struct ib_ah		ibah;
639322810Shselasky	struct mlx5_av		av;
640322810Shselasky};
641322810Shselasky
642322810Shselaskystatic inline struct mlx5_ib_ah *to_mah(struct ib_ah *ibah)
643322810Shselasky{
644322810Shselasky	return container_of(ibah, struct mlx5_ib_ah, ibah);
645322810Shselasky}
646322810Shselasky
647322810Shselaskyint mlx5_ib_db_map_user(struct mlx5_ib_ucontext *context, uintptr_t virt,
648322810Shselasky			struct mlx5_db *db);
649322810Shselaskyvoid mlx5_ib_db_unmap_user(struct mlx5_ib_ucontext *context, struct mlx5_db *db);
650322810Shselaskyvoid __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq);
651322810Shselaskyvoid mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq);
652322810Shselaskyvoid mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index);
653322810Shselaskyint mlx5_MAD_IFC(struct mlx5_ib_dev *dev, int ignore_mkey, int ignore_bkey,
654322810Shselasky		 u8 port, struct ib_wc *in_wc, struct ib_grh *in_grh,
655322810Shselasky		 void *in_mad, void *response_mad);
656323223Shselaskyint mlx5_ib_resolve_grh(const struct ib_ah_attr *ah_attr, u8 *mac, int *is_mcast);
657322810Shselaskystruct ib_ah *create_ib_ah(struct mlx5_ib_dev *dev, struct ib_ah_attr *ah_attr,
658322810Shselasky			   struct mlx5_ib_ah *ah, enum rdma_link_layer ll);
659322810Shselaskystruct ib_ah *mlx5_ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr);
660322810Shselaskyint mlx5_ib_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr);
661322810Shselaskyint mlx5_ib_destroy_ah(struct ib_ah *ah);
662322810Shselaskystruct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd,
663322810Shselasky				  struct ib_srq_init_attr *init_attr,
664322810Shselasky				  struct ib_udata *udata);
665322810Shselaskyint mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
666322810Shselasky		       enum ib_srq_attr_mask attr_mask, struct ib_udata *udata);
667322810Shselaskyint mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr);
668322810Shselaskyint mlx5_ib_destroy_srq(struct ib_srq *srq);
669322810Shselaskyint mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
670322810Shselasky			  struct ib_recv_wr **bad_wr);
671322810Shselaskystruct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
672322810Shselasky				struct ib_qp_init_attr *init_attr,
673322810Shselasky				struct ib_udata *udata);
674322810Shselaskyint mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
675322810Shselasky		      int attr_mask, struct ib_udata *udata);
676322810Shselaskyint mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
677322810Shselasky		     struct ib_qp_init_attr *qp_init_attr);
678322810Shselaskyint mlx5_ib_destroy_qp(struct ib_qp *qp);
679322810Shselaskyint mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
680322810Shselasky		      struct ib_send_wr **bad_wr);
681322810Shselaskyint mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
682322810Shselasky		      struct ib_recv_wr **bad_wr);
683322810Shselaskyvoid *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n);
684322810Shselaskystruct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
685323223Shselasky				int entries, int vector,
686322810Shselasky				struct ib_ucontext *context,
687322810Shselasky				struct ib_udata *udata);
688322810Shselaskyint mlx5_ib_destroy_cq(struct ib_cq *cq);
689322810Shselaskyint mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc);
690322810Shselaskyint mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags);
691323223Shselaskyint mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period);
692322810Shselaskyint mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata);
693322810Shselaskystruct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc);
694322810Shselaskystruct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
695322810Shselasky				  u64 virt_addr, int access_flags,
696322810Shselasky				  struct ib_udata *udata, int mr_id);
697322810Shselaskystruct ib_mr *mlx5_ib_reg_phys_mr(struct ib_pd *pd,
698322810Shselasky				  struct ib_phys_buf *buffer_list,
699322810Shselasky				  int num_phys_buf,
700322810Shselasky				  int access_flags,
701322810Shselasky				  u64 *virt_addr);
702322810Shselaskyint mlx5_ib_dereg_mr(struct ib_mr *ibmr);
703322810Shselaskyint mlx5_ib_destroy_mr(struct ib_mr *ibmr);
704322810Shselaskystruct ib_mr *mlx5_ib_alloc_fast_reg_mr(struct ib_pd *pd,
705322810Shselasky					int max_page_list_len);
706322810Shselaskystruct ib_fast_reg_page_list *mlx5_ib_alloc_fast_reg_page_list(struct ib_device *ibdev,
707322810Shselasky							       int page_list_len);
708322810Shselaskyvoid mlx5_ib_free_fast_reg_page_list(struct ib_fast_reg_page_list *page_list);
709322810Shselasky
710322810Shselaskystruct ib_fmr *mlx5_ib_fmr_alloc(struct ib_pd *pd, int acc,
711322810Shselasky				 struct ib_fmr_attr *fmr_attr);
712322810Shselaskyint mlx5_ib_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
713322810Shselasky		      int npages, u64 iova);
714322810Shselaskyint mlx5_ib_unmap_fmr(struct list_head *fmr_list);
715322810Shselaskyint mlx5_ib_fmr_dealloc(struct ib_fmr *ibfmr);
716322810Shselaskyint mlx5_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
717322810Shselasky			struct ib_wc *in_wc, struct ib_grh *in_grh,
718322810Shselasky			struct ib_mad *in_mad, struct ib_mad *out_mad);
719322810Shselaskystruct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
720322810Shselasky					  struct ib_ucontext *context,
721322810Shselasky					  struct ib_udata *udata);
722322810Shselaskyint mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd);
723322810Shselaskyint mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset);
724322810Shselaskyint mlx5_query_ext_port_caps(struct mlx5_ib_dev *dev, u8 port);
725322810Shselaskyint mlx5_query_smp_attr_node_info_mad_ifc(struct ib_device *ibdev,
726322810Shselasky					  struct ib_smp *out_mad);
727322810Shselaskyint mlx5_query_system_image_guid_mad_ifc(struct ib_device *ibdev,
728322810Shselasky					 __be64 *sys_image_guid);
729322810Shselaskyint mlx5_query_max_pkeys_mad_ifc(struct ib_device *ibdev,
730322810Shselasky				 u16 *max_pkeys);
731322810Shselaskyint mlx5_query_vendor_id_mad_ifc(struct ib_device *ibdev,
732322810Shselasky				 u32 *vendor_id);
733322810Shselaskyint mlx5_query_pkey_mad_ifc(struct ib_device *ibdev, u8 port, u16 index,
734322810Shselasky			    u16 *pkey);
735322810Shselaskyint mlx5_query_node_desc_mad_ifc(struct mlx5_ib_dev *dev, char *node_desc);
736322810Shselaskyint mlx5_query_node_guid_mad_ifc(struct mlx5_ib_dev *dev, u64 *node_guid);
737322810Shselaskyint mlx5_query_gids_mad_ifc(struct ib_device *ibdev, u8 port, int index,
738322810Shselasky			    union ib_gid *gid);
739322810Shselaskyint mlx5_query_port_mad_ifc(struct ib_device *ibdev, u8 port,
740322810Shselasky			    struct ib_port_attr *props);
741322810Shselaskyint mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
742322810Shselasky		       struct ib_port_attr *props);
743322810Shselaskyint mlx5_ib_init_fmr(struct mlx5_ib_dev *dev);
744322810Shselaskyvoid mlx5_ib_cleanup_fmr(struct mlx5_ib_dev *dev);
745322810Shselaskyvoid mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr, int *count, int *shift,
746322810Shselasky			int *ncont, int *order);
747322810Shselaskyvoid mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
748322810Shselasky			  int page_shift, __be64 *pas, int umr);
749322810Shselaskyvoid mlx5_ib_copy_pas(u64 *old, u64 *new, int step, int num);
750322810Shselaskyint mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq);
751322810Shselaskyint mlx5_mr_cache_init(struct mlx5_ib_dev *dev);
752322810Shselaskyint mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev);
753322810Shselaskyint mlx5_mr_ib_cont_pages(struct ib_umem *umem, u64 addr, int *count, int *shift);
754322810Shselaskyvoid mlx5_umr_cq_handler(struct ib_cq *cq, void *cq_context);
755322810Shselaskyint mlx5_query_port_roce(struct ib_device *ibdev, u8 port,
756322810Shselasky			 struct ib_port_attr *props);
757322810Shselasky__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port, int index,
758322810Shselasky			       __be16 ah_udp_s_port);
759322810Shselaskyint mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port,
760322810Shselasky			   int index, int *gid_type);
761322810Shselaskystruct net_device *mlx5_ib_get_netdev(struct ib_device *ib_dev, u8 port);
762322810Shselaskyint modify_gid_roce(struct ib_device *ib_dev, u8 port, unsigned int index,
763322810Shselasky		    const union ib_gid *gid, struct net_device *ndev);
764322810Shselaskyint query_gid_roce(struct ib_device *ib_dev, u8 port, int index,
765322810Shselasky		   union ib_gid *gid);
766322810Shselaskyint mlx5_process_mad_mad_ifc(struct ib_device *ibdev, int mad_flags,
767322810Shselasky			     u8 port_num, struct ib_wc *in_wc,
768322810Shselasky			     struct ib_grh *in_grh, struct ib_mad *in_mad,
769322810Shselasky			     struct ib_mad *out_mad);
770322810Shselasky
771322810Shselaskystatic inline void init_query_mad(struct ib_smp *mad)
772322810Shselasky{
773322810Shselasky	mad->base_version  = 1;
774322810Shselasky	mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
775322810Shselasky	mad->class_version = 1;
776322810Shselasky	mad->method	   = IB_MGMT_METHOD_GET;
777322810Shselasky}
778322810Shselasky
779322810Shselaskystatic inline u8 convert_access(int acc)
780322810Shselasky{
781322810Shselasky	return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC       : 0) |
782322810Shselasky	       (acc & IB_ACCESS_REMOTE_WRITE  ? MLX5_PERM_REMOTE_WRITE : 0) |
783322810Shselasky	       (acc & IB_ACCESS_REMOTE_READ   ? MLX5_PERM_REMOTE_READ  : 0) |
784322810Shselasky	       (acc & IB_ACCESS_LOCAL_WRITE   ? MLX5_PERM_LOCAL_WRITE  : 0) |
785322810Shselasky	       MLX5_PERM_LOCAL_READ;
786322810Shselasky}
787322810Shselasky
788322810Shselasky#define MLX5_MAX_UMR_SHIFT 16
789322810Shselasky#define MLX5_MAX_UMR_PAGES (1 << MLX5_MAX_UMR_SHIFT)
790322810Shselasky
791322810Shselasky#endif /* MLX5_IB_H */
792