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