]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mlx5/driver.h
Reorganize health recovery in mlx5core.
[FreeBSD/FreeBSD.git] / sys / dev / mlx5 / driver.h
1 /*-
2  * Copyright (c) 2013-2017, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27
28 #ifndef MLX5_DRIVER_H
29 #define MLX5_DRIVER_H
30
31 #include <linux/kernel.h>
32 #include <linux/completion.h>
33 #include <linux/pci.h>
34 #include <linux/cache.h>
35 #include <linux/rbtree.h>
36 #include <linux/if_ether.h>
37 #include <linux/semaphore.h>
38 #include <linux/slab.h>
39 #include <linux/vmalloc.h>
40 #include <linux/radix-tree.h>
41
42 #include <dev/mlx5/device.h>
43 #include <dev/mlx5/doorbell.h>
44 #include <dev/mlx5/srq.h>
45
46 #define MLX5_QCOUNTER_SETS_NETDEV 64
47 #define MLX5_MAX_NUMBER_OF_VFS 128
48
49 enum {
50         MLX5_BOARD_ID_LEN = 64,
51         MLX5_MAX_NAME_LEN = 16,
52 };
53
54 enum {
55         MLX5_CMD_TIMEOUT_MSEC   = 8 * 60 * 1000,
56         MLX5_CMD_WQ_MAX_NAME    = 32,
57 };
58
59 enum {
60         CMD_OWNER_SW            = 0x0,
61         CMD_OWNER_HW            = 0x1,
62         CMD_STATUS_SUCCESS      = 0,
63 };
64
65 enum mlx5_sqp_t {
66         MLX5_SQP_SMI            = 0,
67         MLX5_SQP_GSI            = 1,
68         MLX5_SQP_IEEE_1588      = 2,
69         MLX5_SQP_SNIFFER        = 3,
70         MLX5_SQP_SYNC_UMR       = 4,
71 };
72
73 enum {
74         MLX5_MAX_PORTS  = 2,
75 };
76
77 enum {
78         MLX5_EQ_VEC_PAGES        = 0,
79         MLX5_EQ_VEC_CMD          = 1,
80         MLX5_EQ_VEC_ASYNC        = 2,
81         MLX5_EQ_VEC_COMP_BASE,
82 };
83
84 enum {
85         MLX5_MAX_IRQ_NAME       = 32
86 };
87
88 enum {
89         MLX5_ATOMIC_MODE_OFF            = 16,
90         MLX5_ATOMIC_MODE_NONE           = 0 << MLX5_ATOMIC_MODE_OFF,
91         MLX5_ATOMIC_MODE_IB_COMP        = 1 << MLX5_ATOMIC_MODE_OFF,
92         MLX5_ATOMIC_MODE_CX             = 2 << MLX5_ATOMIC_MODE_OFF,
93         MLX5_ATOMIC_MODE_8B             = 3 << MLX5_ATOMIC_MODE_OFF,
94         MLX5_ATOMIC_MODE_16B            = 4 << MLX5_ATOMIC_MODE_OFF,
95         MLX5_ATOMIC_MODE_32B            = 5 << MLX5_ATOMIC_MODE_OFF,
96         MLX5_ATOMIC_MODE_64B            = 6 << MLX5_ATOMIC_MODE_OFF,
97         MLX5_ATOMIC_MODE_128B           = 7 << MLX5_ATOMIC_MODE_OFF,
98         MLX5_ATOMIC_MODE_256B           = 8 << MLX5_ATOMIC_MODE_OFF,
99 };
100
101 enum {
102         MLX5_ATOMIC_MODE_DCT_OFF        = 20,
103         MLX5_ATOMIC_MODE_DCT_NONE       = 0 << MLX5_ATOMIC_MODE_DCT_OFF,
104         MLX5_ATOMIC_MODE_DCT_IB_COMP    = 1 << MLX5_ATOMIC_MODE_DCT_OFF,
105         MLX5_ATOMIC_MODE_DCT_CX         = 2 << MLX5_ATOMIC_MODE_DCT_OFF,
106         MLX5_ATOMIC_MODE_DCT_8B         = 3 << MLX5_ATOMIC_MODE_DCT_OFF,
107         MLX5_ATOMIC_MODE_DCT_16B        = 4 << MLX5_ATOMIC_MODE_DCT_OFF,
108         MLX5_ATOMIC_MODE_DCT_32B        = 5 << MLX5_ATOMIC_MODE_DCT_OFF,
109         MLX5_ATOMIC_MODE_DCT_64B        = 6 << MLX5_ATOMIC_MODE_DCT_OFF,
110         MLX5_ATOMIC_MODE_DCT_128B       = 7 << MLX5_ATOMIC_MODE_DCT_OFF,
111         MLX5_ATOMIC_MODE_DCT_256B       = 8 << MLX5_ATOMIC_MODE_DCT_OFF,
112 };
113
114 enum {
115         MLX5_ATOMIC_OPS_CMP_SWAP                = 1 << 0,
116         MLX5_ATOMIC_OPS_FETCH_ADD               = 1 << 1,
117         MLX5_ATOMIC_OPS_MASKED_CMP_SWAP         = 1 << 2,
118         MLX5_ATOMIC_OPS_MASKED_FETCH_ADD        = 1 << 3,
119 };
120
121 enum {
122         MLX5_REG_QETCR           = 0x4005,
123         MLX5_REG_QPDP            = 0x4007,
124         MLX5_REG_QTCT            = 0x400A,
125         MLX5_REG_QHLL            = 0x4016,
126         MLX5_REG_DCBX_PARAM      = 0x4020,
127         MLX5_REG_DCBX_APP        = 0x4021,
128         MLX5_REG_PCAP            = 0x5001,
129         MLX5_REG_PMTU            = 0x5003,
130         MLX5_REG_PTYS            = 0x5004,
131         MLX5_REG_PAOS            = 0x5006,
132         MLX5_REG_PFCC            = 0x5007,
133         MLX5_REG_PPCNT           = 0x5008,
134         MLX5_REG_PMAOS           = 0x5012,
135         MLX5_REG_PUDE            = 0x5009,
136         MLX5_REG_PPTB            = 0x500B,
137         MLX5_REG_PBMC            = 0x500C,
138         MLX5_REG_PMPE            = 0x5010,
139         MLX5_REG_PELC            = 0x500e,
140         MLX5_REG_PVLC            = 0x500f,
141         MLX5_REG_PMLP            = 0x5002,
142         MLX5_REG_NODE_DESC       = 0x6001,
143         MLX5_REG_HOST_ENDIANNESS = 0x7004,
144         MLX5_REG_MCIA            = 0x9014,
145         MLX5_REG_MPCNT           = 0x9051,
146 };
147
148 enum dbg_rsc_type {
149         MLX5_DBG_RSC_QP,
150         MLX5_DBG_RSC_EQ,
151         MLX5_DBG_RSC_CQ,
152 };
153
154 enum {
155         MLX5_INTERFACE_PROTOCOL_IB  = 0,
156         MLX5_INTERFACE_PROTOCOL_ETH = 1,
157         MLX5_INTERFACE_NUMBER       = 2,
158 };
159
160 struct mlx5_field_desc {
161         struct dentry          *dent;
162         int                     i;
163 };
164
165 struct mlx5_rsc_debug {
166         struct mlx5_core_dev   *dev;
167         void                   *object;
168         enum dbg_rsc_type       type;
169         struct dentry          *root;
170         struct mlx5_field_desc  fields[0];
171 };
172
173 enum mlx5_dev_event {
174         MLX5_DEV_EVENT_SYS_ERROR,
175         MLX5_DEV_EVENT_PORT_UP,
176         MLX5_DEV_EVENT_PORT_DOWN,
177         MLX5_DEV_EVENT_PORT_INITIALIZED,
178         MLX5_DEV_EVENT_LID_CHANGE,
179         MLX5_DEV_EVENT_PKEY_CHANGE,
180         MLX5_DEV_EVENT_GUID_CHANGE,
181         MLX5_DEV_EVENT_CLIENT_REREG,
182         MLX5_DEV_EVENT_VPORT_CHANGE,
183         MLX5_DEV_EVENT_ERROR_STATE_DCBX,
184         MLX5_DEV_EVENT_REMOTE_CONFIG_CHANGE,
185         MLX5_DEV_EVENT_LOCAL_OPER_CHANGE,
186         MLX5_DEV_EVENT_REMOTE_CONFIG_APPLICATION_PRIORITY_CHANGE,
187 };
188
189 enum mlx5_port_status {
190         MLX5_PORT_UP        = 1 << 0,
191         MLX5_PORT_DOWN      = 1 << 1,
192 };
193
194 enum mlx5_link_mode {
195         MLX5_1000BASE_CX_SGMII  = 0,
196         MLX5_1000BASE_KX        = 1,
197         MLX5_10GBASE_CX4        = 2,
198         MLX5_10GBASE_KX4        = 3,
199         MLX5_10GBASE_KR         = 4,
200         MLX5_20GBASE_KR2        = 5,
201         MLX5_40GBASE_CR4        = 6,
202         MLX5_40GBASE_KR4        = 7,
203         MLX5_56GBASE_R4         = 8,
204         MLX5_10GBASE_CR         = 12,
205         MLX5_10GBASE_SR         = 13,
206         MLX5_10GBASE_ER         = 14,
207         MLX5_40GBASE_SR4        = 15,
208         MLX5_40GBASE_LR4        = 16,
209         MLX5_100GBASE_CR4       = 20,
210         MLX5_100GBASE_SR4       = 21,
211         MLX5_100GBASE_KR4       = 22,
212         MLX5_100GBASE_LR4       = 23,
213         MLX5_100BASE_TX         = 24,
214         MLX5_1000BASE_T         = 25,
215         MLX5_10GBASE_T          = 26,
216         MLX5_25GBASE_CR         = 27,
217         MLX5_25GBASE_KR         = 28,
218         MLX5_25GBASE_SR         = 29,
219         MLX5_50GBASE_CR2        = 30,
220         MLX5_50GBASE_KR2        = 31,
221         MLX5_LINK_MODES_NUMBER,
222 };
223
224 enum {
225         MLX5_VSC_SPACE_SUPPORTED = 0x1,
226         MLX5_VSC_SPACE_OFFSET    = 0x4,
227         MLX5_VSC_COUNTER_OFFSET  = 0x8,
228         MLX5_VSC_SEMA_OFFSET     = 0xC,
229         MLX5_VSC_ADDR_OFFSET     = 0x10,
230         MLX5_VSC_DATA_OFFSET     = 0x14,
231         MLX5_VSC_MAX_RETRIES     = 0x1000,
232 };
233
234 #define MLX5_PROT_MASK(link_mode) (1 << link_mode)
235
236 struct mlx5_uuar_info {
237         struct mlx5_uar        *uars;
238         int                     num_uars;
239         int                     num_low_latency_uuars;
240         unsigned long          *bitmap;
241         unsigned int           *count;
242         struct mlx5_bf         *bfs;
243
244         /*
245          * protect uuar allocation data structs
246          */
247         struct mutex            lock;
248         u32                     ver;
249 };
250
251 struct mlx5_bf {
252         void __iomem           *reg;
253         void __iomem           *regreg;
254         int                     buf_size;
255         struct mlx5_uar        *uar;
256         unsigned long           offset;
257         int                     need_lock;
258         /* protect blue flame buffer selection when needed
259          */
260         spinlock_t              lock;
261
262         /* serialize 64 bit writes when done as two 32 bit accesses
263          */
264         spinlock_t              lock32;
265         int                     uuarn;
266 };
267
268 struct mlx5_cmd_first {
269         __be32          data[4];
270 };
271
272 struct cache_ent;
273 struct mlx5_fw_page {
274         union {
275                 struct rb_node rb_node;
276                 struct list_head list;
277         };
278         struct mlx5_cmd_first first;
279         struct mlx5_core_dev *dev;
280         bus_dmamap_t dma_map;
281         bus_addr_t dma_addr;
282         void *virt_addr;
283         struct cache_ent *cache;
284         u32 numpages;
285         u16 load_done;
286 #define MLX5_LOAD_ST_NONE 0
287 #define MLX5_LOAD_ST_SUCCESS 1
288 #define MLX5_LOAD_ST_FAILURE 2
289         u16 func_id;
290 };
291 #define mlx5_cmd_msg mlx5_fw_page
292
293 struct mlx5_cmd_debug {
294         struct dentry          *dbg_root;
295         struct dentry          *dbg_in;
296         struct dentry          *dbg_out;
297         struct dentry          *dbg_outlen;
298         struct dentry          *dbg_status;
299         struct dentry          *dbg_run;
300         void                   *in_msg;
301         void                   *out_msg;
302         u8                      status;
303         u16                     inlen;
304         u16                     outlen;
305 };
306
307 struct cache_ent {
308         /* protect block chain allocations
309          */
310         spinlock_t              lock;
311         struct list_head        head;
312 };
313
314 struct cmd_msg_cache {
315         struct cache_ent        large;
316         struct cache_ent        med;
317
318 };
319
320 struct mlx5_traffic_counter {
321         u64         packets;
322         u64         octets;
323 };
324
325 struct mlx5_cmd_stats {
326         u64             sum;
327         u64             n;
328         struct dentry  *root;
329         struct dentry  *avg;
330         struct dentry  *count;
331         /* protect command average calculations */
332         spinlock_t      lock;
333 };
334
335 struct mlx5_cmd {
336         struct mlx5_fw_page *cmd_page;
337         bus_dma_tag_t dma_tag;
338         struct sx dma_sx;
339         struct mtx dma_mtx;
340 #define MLX5_DMA_OWNED(dev) mtx_owned(&(dev)->cmd.dma_mtx)
341 #define MLX5_DMA_LOCK(dev) mtx_lock(&(dev)->cmd.dma_mtx)
342 #define MLX5_DMA_UNLOCK(dev) mtx_unlock(&(dev)->cmd.dma_mtx)
343         struct cv dma_cv;
344 #define MLX5_DMA_DONE(dev) cv_broadcast(&(dev)->cmd.dma_cv)
345 #define MLX5_DMA_WAIT(dev) cv_wait(&(dev)->cmd.dma_cv, &(dev)->cmd.dma_mtx)
346         void           *cmd_buf;
347         dma_addr_t      dma;
348         u16             cmdif_rev;
349         u8              log_sz;
350         u8              log_stride;
351         int             max_reg_cmds;
352         int             events;
353         u32 __iomem    *vector;
354
355         /* protect command queue allocations
356          */
357         spinlock_t      alloc_lock;
358
359         /* protect token allocations
360          */
361         spinlock_t      token_lock;
362         u8              token;
363         unsigned long   bitmask;
364         char            wq_name[MLX5_CMD_WQ_MAX_NAME];
365         struct workqueue_struct *wq;
366         struct semaphore sem;
367         struct semaphore pages_sem;
368         int     mode;
369         struct mlx5_cmd_work_ent *ent_arr[MLX5_MAX_COMMANDS];
370         struct mlx5_cmd_debug dbg;
371         struct cmd_msg_cache cache;
372         int checksum_disabled;
373         struct mlx5_cmd_stats stats[MLX5_CMD_OP_MAX];
374         int moving_to_polling;
375 };
376
377 struct mlx5_port_caps {
378         int     gid_table_len;
379         int     pkey_table_len;
380         u8      ext_port_cap;
381 };
382
383 struct mlx5_buf {
384         bus_dma_tag_t           dma_tag;
385         bus_dmamap_t            dma_map;
386         struct mlx5_core_dev   *dev;
387         struct {
388                 void           *buf;
389         } direct;
390         u64                    *page_list;
391         int                     npages;
392         int                     size;
393         u8                      page_shift;
394         u8                      load_done;
395 };
396
397 struct mlx5_eq {
398         struct mlx5_core_dev   *dev;
399         __be32 __iomem         *doorbell;
400         u32                     cons_index;
401         struct mlx5_buf         buf;
402         int                     size;
403         u8                      irqn;
404         u8                      eqn;
405         int                     nent;
406         u64                     mask;
407         struct list_head        list;
408         int                     index;
409         struct mlx5_rsc_debug   *dbg;
410 };
411
412 struct mlx5_core_psv {
413         u32     psv_idx;
414         struct psv_layout {
415                 u32     pd;
416                 u16     syndrome;
417                 u16     reserved;
418                 u16     bg;
419                 u16     app_tag;
420                 u32     ref_tag;
421         } psv;
422 };
423
424 struct mlx5_core_sig_ctx {
425         struct mlx5_core_psv    psv_memory;
426         struct mlx5_core_psv    psv_wire;
427 #if (__FreeBSD_version >= 1100000)
428         struct ib_sig_err       err_item;
429 #endif
430         bool                    sig_status_checked;
431         bool                    sig_err_exists;
432         u32                     sigerr_count;
433 };
434
435 struct mlx5_core_mr {
436         u64                     iova;
437         u64                     size;
438         u32                     key;
439         u32                     pd;
440 };
441
442 enum mlx5_res_type {
443         MLX5_RES_QP     = MLX5_EVENT_QUEUE_TYPE_QP,
444         MLX5_RES_RQ     = MLX5_EVENT_QUEUE_TYPE_RQ,
445         MLX5_RES_SQ     = MLX5_EVENT_QUEUE_TYPE_SQ,
446         MLX5_RES_SRQ    = 3,
447         MLX5_RES_XSRQ   = 4,
448         MLX5_RES_DCT    = 5,
449 };
450
451 struct mlx5_core_rsc_common {
452         enum mlx5_res_type      res;
453         atomic_t                refcount;
454         struct completion       free;
455 };
456
457 struct mlx5_core_srq {
458         struct mlx5_core_rsc_common     common; /* must be first */
459         u32                             srqn;
460         int                             max;
461         int                             max_gs;
462         int                             max_avail_gather;
463         int                             wqe_shift;
464         void                            (*event)(struct mlx5_core_srq *, int);
465         atomic_t                        refcount;
466         struct completion               free;
467 };
468
469 struct mlx5_eq_table {
470         void __iomem           *update_ci;
471         void __iomem           *update_arm_ci;
472         struct list_head        comp_eqs_list;
473         struct mlx5_eq          pages_eq;
474         struct mlx5_eq          async_eq;
475         struct mlx5_eq          cmd_eq;
476         int                     num_comp_vectors;
477         /* protect EQs list
478          */
479         spinlock_t              lock;
480 };
481
482 struct mlx5_uar {
483         u32                     index;
484         void __iomem           *bf_map;
485         void __iomem           *map;
486 };
487
488
489 struct mlx5_core_health {
490         struct mlx5_health_buffer __iomem       *health;
491         __be32 __iomem                 *health_counter;
492         struct timer_list               timer;
493         u32                             prev;
494         int                             miss_counter;
495         u32                             fatal_error;
496         /* wq spinlock to synchronize draining */
497         spinlock_t                      wq_lock;
498         struct workqueue_struct        *wq;
499         unsigned long                   flags;
500         struct work_struct              work;
501         struct delayed_work             recover_work;
502 };
503
504 #define MLX5_CQ_LINEAR_ARRAY_SIZE       1024
505
506 struct mlx5_cq_linear_array_entry {
507         spinlock_t      lock;
508         struct mlx5_core_cq * volatile cq;
509 };
510
511 struct mlx5_cq_table {
512         /* protect radix tree
513          */
514         spinlock_t              lock;
515         struct radix_tree_root  tree;
516         struct mlx5_cq_linear_array_entry linear_array[MLX5_CQ_LINEAR_ARRAY_SIZE];
517 };
518
519 struct mlx5_qp_table {
520         /* protect radix tree
521          */
522         spinlock_t              lock;
523         struct radix_tree_root  tree;
524 };
525
526 struct mlx5_srq_table {
527         /* protect radix tree
528          */
529         spinlock_t              lock;
530         struct radix_tree_root  tree;
531 };
532
533 struct mlx5_mr_table {
534         /* protect radix tree
535          */
536         spinlock_t              lock;
537         struct radix_tree_root  tree;
538 };
539
540 struct mlx5_irq_info {
541         char name[MLX5_MAX_IRQ_NAME];
542 };
543
544 struct mlx5_priv {
545         char                    name[MLX5_MAX_NAME_LEN];
546         struct mlx5_eq_table    eq_table;
547         struct msix_entry       *msix_arr;
548         struct mlx5_irq_info    *irq_info;
549         struct mlx5_uuar_info   uuari;
550         MLX5_DECLARE_DOORBELL_LOCK(cq_uar_lock);
551
552         struct io_mapping       *bf_mapping;
553
554         /* pages stuff */
555         struct workqueue_struct *pg_wq;
556         struct rb_root          page_root;
557         s64                     fw_pages;
558         atomic_t                reg_pages;
559         s64                     pages_per_func[MLX5_MAX_NUMBER_OF_VFS];
560         struct mlx5_core_health health;
561
562         struct mlx5_srq_table   srq_table;
563
564         /* start: qp staff */
565         struct mlx5_qp_table    qp_table;
566         struct dentry          *qp_debugfs;
567         struct dentry          *eq_debugfs;
568         struct dentry          *cq_debugfs;
569         struct dentry          *cmdif_debugfs;
570         /* end: qp staff */
571
572         /* start: cq staff */
573         struct mlx5_cq_table    cq_table;
574         /* end: cq staff */
575
576         /* start: mr staff */
577         struct mlx5_mr_table    mr_table;
578         /* end: mr staff */
579
580         /* start: alloc staff */
581         int                     numa_node;
582
583         struct mutex   pgdir_mutex;
584         struct list_head        pgdir_list;
585         /* end: alloc staff */
586         struct dentry          *dbg_root;
587
588         /* protect mkey key part */
589         spinlock_t              mkey_lock;
590         u8                      mkey_key;
591
592         struct list_head        dev_list;
593         struct list_head        ctx_list;
594         spinlock_t              ctx_lock;
595         unsigned long           pci_dev_data;
596 };
597
598 enum mlx5_device_state {
599         MLX5_DEVICE_STATE_UP,
600         MLX5_DEVICE_STATE_INTERNAL_ERROR,
601 };
602
603 enum mlx5_interface_state {
604         MLX5_INTERFACE_STATE_DOWN = BIT(0),
605         MLX5_INTERFACE_STATE_UP = BIT(1),
606         MLX5_INTERFACE_STATE_SHUTDOWN = BIT(2),
607 };
608
609 enum mlx5_pci_status {
610         MLX5_PCI_STATUS_DISABLED,
611         MLX5_PCI_STATUS_ENABLED,
612 };
613
614 struct mlx5_special_contexts {
615         int resd_lkey;
616 };
617
618 struct mlx5_flow_root_namespace;
619 struct mlx5_dump_data;
620 struct mlx5_core_dev {
621         struct pci_dev         *pdev;
622         /* sync pci state */
623         struct mutex            pci_status_mutex;
624         enum mlx5_pci_status    pci_status;
625         char                    board_id[MLX5_BOARD_ID_LEN];
626         struct mlx5_cmd         cmd;
627         struct mlx5_port_caps   port_caps[MLX5_MAX_PORTS];
628         u32 hca_caps_cur[MLX5_CAP_NUM][MLX5_UN_SZ_DW(hca_cap_union)];
629         u32 hca_caps_max[MLX5_CAP_NUM][MLX5_UN_SZ_DW(hca_cap_union)];
630         phys_addr_t             iseg_base;
631         struct mlx5_init_seg __iomem *iseg;
632         enum mlx5_device_state  state;
633         /* sync interface state */
634         struct mutex            intf_state_mutex;
635         unsigned long           intf_state;
636         void                    (*event) (struct mlx5_core_dev *dev,
637                                           enum mlx5_dev_event event,
638                                           unsigned long param);
639         struct mlx5_priv        priv;
640         struct mlx5_profile     *profile;
641         atomic_t                num_qps;
642         u32                     vsc_addr;
643         u32                     issi;
644         struct mlx5_special_contexts special_contexts;
645         unsigned int module_status[MLX5_MAX_PORTS];
646         struct mlx5_flow_root_namespace *root_ns;
647         struct mlx5_flow_root_namespace *fdb_root_ns;
648         struct mlx5_flow_root_namespace *esw_egress_root_ns;
649         struct mlx5_flow_root_namespace *esw_ingress_root_ns;
650         struct mlx5_flow_root_namespace *sniffer_rx_root_ns;
651         struct mlx5_flow_root_namespace *sniffer_tx_root_ns;
652         u32 num_q_counter_allocated[MLX5_INTERFACE_NUMBER];
653         struct mlx5_dump_data   *dump_data;
654         u32                     vsec_addr;
655 };
656
657 enum {
658         MLX5_WOL_DISABLE       = 0,
659         MLX5_WOL_SECURED_MAGIC = 1 << 1,
660         MLX5_WOL_MAGIC         = 1 << 2,
661         MLX5_WOL_ARP           = 1 << 3,
662         MLX5_WOL_BROADCAST     = 1 << 4,
663         MLX5_WOL_MULTICAST     = 1 << 5,
664         MLX5_WOL_UNICAST       = 1 << 6,
665         MLX5_WOL_PHY_ACTIVITY  = 1 << 7,
666 };
667
668 struct mlx5_db {
669         __be32                  *db;
670         union {
671                 struct mlx5_db_pgdir            *pgdir;
672                 struct mlx5_ib_user_db_page     *user_page;
673         }                       u;
674         dma_addr_t              dma;
675         int                     index;
676 };
677
678 struct mlx5_net_counters {
679         u64     packets;
680         u64     octets;
681 };
682
683 struct mlx5_ptys_reg {
684         u8      an_dis_admin;
685         u8      an_dis_ap;
686         u8      local_port;
687         u8      proto_mask;
688         u32     eth_proto_cap;
689         u16     ib_link_width_cap;
690         u16     ib_proto_cap;
691         u32     eth_proto_admin;
692         u16     ib_link_width_admin;
693         u16     ib_proto_admin;
694         u32     eth_proto_oper;
695         u16     ib_link_width_oper;
696         u16     ib_proto_oper;
697         u32     eth_proto_lp_advertise;
698 };
699
700 struct mlx5_pvlc_reg {
701         u8      local_port;
702         u8      vl_hw_cap;
703         u8      vl_admin;
704         u8      vl_operational;
705 };
706
707 struct mlx5_pmtu_reg {
708         u8      local_port;
709         u16     max_mtu;
710         u16     admin_mtu;
711         u16     oper_mtu;
712 };
713
714 struct mlx5_vport_counters {
715         struct mlx5_net_counters        received_errors;
716         struct mlx5_net_counters        transmit_errors;
717         struct mlx5_net_counters        received_ib_unicast;
718         struct mlx5_net_counters        transmitted_ib_unicast;
719         struct mlx5_net_counters        received_ib_multicast;
720         struct mlx5_net_counters        transmitted_ib_multicast;
721         struct mlx5_net_counters        received_eth_broadcast;
722         struct mlx5_net_counters        transmitted_eth_broadcast;
723         struct mlx5_net_counters        received_eth_unicast;
724         struct mlx5_net_counters        transmitted_eth_unicast;
725         struct mlx5_net_counters        received_eth_multicast;
726         struct mlx5_net_counters        transmitted_eth_multicast;
727 };
728
729 enum {
730         MLX5_DB_PER_PAGE = MLX5_ADAPTER_PAGE_SIZE / L1_CACHE_BYTES,
731 };
732
733 struct mlx5_core_dct {
734         struct mlx5_core_rsc_common     common; /* must be first */
735         void (*event)(struct mlx5_core_dct *, int);
736         int                     dctn;
737         struct completion       drained;
738         struct mlx5_rsc_debug   *dbg;
739         int                     pid;
740 };
741
742 enum {
743         MLX5_COMP_EQ_SIZE = 1024,
744 };
745
746 enum {
747         MLX5_PTYS_IB = 1 << 0,
748         MLX5_PTYS_EN = 1 << 2,
749 };
750
751 struct mlx5_db_pgdir {
752         struct list_head        list;
753         DECLARE_BITMAP(bitmap, MLX5_DB_PER_PAGE);
754         struct mlx5_fw_page    *fw_page;
755         __be32                 *db_page;
756         dma_addr_t              db_dma;
757 };
758
759 typedef void (*mlx5_cmd_cbk_t)(int status, void *context);
760
761 struct mlx5_cmd_work_ent {
762         struct mlx5_cmd_msg    *in;
763         struct mlx5_cmd_msg    *out;
764         int                     uin_size;
765         void                   *uout;
766         int                     uout_size;
767         mlx5_cmd_cbk_t          callback;
768         struct delayed_work     cb_timeout_work;
769         void                   *context;
770         int                     idx;
771         struct completion       done;
772         struct mlx5_cmd        *cmd;
773         struct work_struct      work;
774         struct mlx5_cmd_layout *lay;
775         int                     ret;
776         int                     page_queue;
777         u8                      status;
778         u8                      token;
779         u64                     ts1;
780         u64                     ts2;
781         u16                     op;
782         u8                      busy;
783         bool                    polling;
784 };
785
786 struct mlx5_pas {
787         u64     pa;
788         u8      log_sz;
789 };
790
791 enum port_state_policy {
792         MLX5_POLICY_DOWN        = 0,
793         MLX5_POLICY_UP          = 1,
794         MLX5_POLICY_FOLLOW      = 2,
795         MLX5_POLICY_INVALID     = 0xffffffff
796 };
797
798 static inline void *
799 mlx5_buf_offset(struct mlx5_buf *buf, int offset)
800 {
801         return ((char *)buf->direct.buf + offset);
802 }
803
804
805 extern struct workqueue_struct *mlx5_core_wq;
806
807 #define STRUCT_FIELD(header, field) \
808         .struct_offset_bytes = offsetof(struct ib_unpacked_ ## header, field),      \
809         .struct_size_bytes   = sizeof((struct ib_unpacked_ ## header *)0)->field
810
811 static inline struct mlx5_core_dev *pci2mlx5_core_dev(struct pci_dev *pdev)
812 {
813         return pci_get_drvdata(pdev);
814 }
815
816 extern struct dentry *mlx5_debugfs_root;
817
818 static inline u16 fw_rev_maj(struct mlx5_core_dev *dev)
819 {
820         return ioread32be(&dev->iseg->fw_rev) & 0xffff;
821 }
822
823 static inline u16 fw_rev_min(struct mlx5_core_dev *dev)
824 {
825         return ioread32be(&dev->iseg->fw_rev) >> 16;
826 }
827
828 static inline u16 fw_rev_sub(struct mlx5_core_dev *dev)
829 {
830         return ioread32be(&dev->iseg->cmdif_rev_fw_sub) & 0xffff;
831 }
832
833 static inline u16 cmdif_rev_get(struct mlx5_core_dev *dev)
834 {
835         return ioread32be(&dev->iseg->cmdif_rev_fw_sub) >> 16;
836 }
837
838 static inline int mlx5_get_gid_table_len(u16 param)
839 {
840         if (param > 4) {
841                 printf("M4_CORE_DRV_NAME: WARN: ""gid table length is zero\n");
842                 return 0;
843         }
844
845         return 8 * (1 << param);
846 }
847
848 static inline void *mlx5_vzalloc(unsigned long size)
849 {
850         void *rtn;
851
852         rtn = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
853         return rtn;
854 }
855
856 static inline void *mlx5_vmalloc(unsigned long size)
857 {
858         void *rtn;
859
860         rtn = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
861         if (!rtn)
862                 rtn = vmalloc(size);
863         return rtn;
864 }
865
866 static inline u32 mlx5_base_mkey(const u32 key)
867 {
868         return key & 0xffffff00u;
869 }
870
871 int mlx5_cmd_init(struct mlx5_core_dev *dev);
872 void mlx5_cmd_cleanup(struct mlx5_core_dev *dev);
873 void mlx5_cmd_use_events(struct mlx5_core_dev *dev);
874 void mlx5_cmd_use_polling(struct mlx5_core_dev *dev);
875 void mlx5_cmd_mbox_status(void *out, u8 *status, u32 *syndrome);
876 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type);
877 int mlx5_cmd_exec(struct mlx5_core_dev *dev, void *in, int in_size, void *out,
878                   int out_size);
879 int mlx5_cmd_exec_cb(struct mlx5_core_dev *dev, void *in, int in_size,
880                      void *out, int out_size, mlx5_cmd_cbk_t callback,
881                      void *context);
882 int mlx5_cmd_exec_polling(struct mlx5_core_dev *dev, void *in, int in_size,
883                           void *out, int out_size);
884 int mlx5_cmd_alloc_uar(struct mlx5_core_dev *dev, u32 *uarn);
885 int mlx5_cmd_free_uar(struct mlx5_core_dev *dev, u32 uarn);
886 int mlx5_alloc_uuars(struct mlx5_core_dev *dev, struct mlx5_uuar_info *uuari);
887 int mlx5_free_uuars(struct mlx5_core_dev *dev, struct mlx5_uuar_info *uuari);
888 int mlx5_alloc_map_uar(struct mlx5_core_dev *mdev, struct mlx5_uar *uar);
889 void mlx5_unmap_free_uar(struct mlx5_core_dev *mdev, struct mlx5_uar *uar);
890 void mlx5_health_cleanup(struct mlx5_core_dev *dev);
891 int mlx5_health_init(struct mlx5_core_dev *dev);
892 void mlx5_start_health_poll(struct mlx5_core_dev *dev);
893 void mlx5_stop_health_poll(struct mlx5_core_dev *dev);
894 void mlx5_drain_health_wq(struct mlx5_core_dev *dev);
895 void mlx5_drain_health_recovery(struct mlx5_core_dev *dev);
896 void mlx5_trigger_health_work(struct mlx5_core_dev *dev);
897
898 #define mlx5_buf_alloc_node(dev, size, direct, buf, node) \
899         mlx5_buf_alloc(dev, size, direct, buf)
900 int mlx5_buf_alloc(struct mlx5_core_dev *dev, int size, int max_direct,
901                    struct mlx5_buf *buf);
902 void mlx5_buf_free(struct mlx5_core_dev *dev, struct mlx5_buf *buf);
903 int mlx5_core_create_srq(struct mlx5_core_dev *dev, struct mlx5_core_srq *srq,
904                          struct mlx5_srq_attr *in);
905 int mlx5_core_destroy_srq(struct mlx5_core_dev *dev, struct mlx5_core_srq *srq);
906 int mlx5_core_query_srq(struct mlx5_core_dev *dev, struct mlx5_core_srq *srq,
907                         struct mlx5_srq_attr *out);
908 int mlx5_core_query_vendor_id(struct mlx5_core_dev *mdev, u32 *vendor_id);
909 int mlx5_core_arm_srq(struct mlx5_core_dev *dev, struct mlx5_core_srq *srq,
910                       u16 lwm, int is_srq);
911 void mlx5_init_mr_table(struct mlx5_core_dev *dev);
912 void mlx5_cleanup_mr_table(struct mlx5_core_dev *dev);
913 int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
914                              struct mlx5_core_mr *mkey,
915                              u32 *in, int inlen,
916                              u32 *out, int outlen,
917                              mlx5_cmd_cbk_t callback, void *context);
918 int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
919                           struct mlx5_core_mr *mr,
920                           u32 *in, int inlen);
921 int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mkey);
922 int mlx5_core_query_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mkey,
923                          u32 *out, int outlen);
924 int mlx5_core_dump_fill_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr,
925                              u32 *mkey);
926 int mlx5_core_alloc_pd(struct mlx5_core_dev *dev, u32 *pdn);
927 int mlx5_core_dealloc_pd(struct mlx5_core_dev *dev, u32 pdn);
928 int mlx5_core_mad_ifc(struct mlx5_core_dev *dev, const void *inb, void *outb,
929                       u16 opmod, u8 port);
930 void mlx5_fwp_flush(struct mlx5_fw_page *fwp);
931 void mlx5_fwp_invalidate(struct mlx5_fw_page *fwp);
932 struct mlx5_fw_page *mlx5_fwp_alloc(struct mlx5_core_dev *dev, gfp_t flags, unsigned num);
933 void mlx5_fwp_free(struct mlx5_fw_page *fwp);
934 u64 mlx5_fwp_get_dma(struct mlx5_fw_page *fwp, size_t offset);
935 void *mlx5_fwp_get_virt(struct mlx5_fw_page *fwp, size_t offset);
936 void mlx5_pagealloc_init(struct mlx5_core_dev *dev);
937 void mlx5_pagealloc_cleanup(struct mlx5_core_dev *dev);
938 int mlx5_pagealloc_start(struct mlx5_core_dev *dev);
939 void mlx5_pagealloc_stop(struct mlx5_core_dev *dev);
940 void mlx5_core_req_pages_handler(struct mlx5_core_dev *dev, u16 func_id,
941                                  s32 npages);
942 int mlx5_satisfy_startup_pages(struct mlx5_core_dev *dev, int boot);
943 int mlx5_reclaim_startup_pages(struct mlx5_core_dev *dev);
944 s64 mlx5_wait_for_reclaim_vfs_pages(struct mlx5_core_dev *dev);
945 void mlx5_register_debugfs(void);
946 void mlx5_unregister_debugfs(void);
947 int mlx5_eq_init(struct mlx5_core_dev *dev);
948 void mlx5_eq_cleanup(struct mlx5_core_dev *dev);
949 void mlx5_fill_page_array(struct mlx5_buf *buf, __be64 *pas);
950 void mlx5_cq_completion(struct mlx5_core_dev *dev, u32 cqn);
951 void mlx5_rsc_event(struct mlx5_core_dev *dev, u32 rsn, int event_type);
952 void mlx5_srq_event(struct mlx5_core_dev *dev, u32 srqn, int event_type);
953 struct mlx5_core_srq *mlx5_core_get_srq(struct mlx5_core_dev *dev, u32 srqn);
954 void mlx5_cmd_comp_handler(struct mlx5_core_dev *dev, u32 vector);
955 void mlx5_cq_event(struct mlx5_core_dev *dev, u32 cqn, int event_type);
956 int mlx5_create_map_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq, u8 vecidx,
957                        int nent, u64 mask, const char *name, struct mlx5_uar *uar);
958 int mlx5_destroy_unmap_eq(struct mlx5_core_dev *dev, struct mlx5_eq *eq);
959 int mlx5_start_eqs(struct mlx5_core_dev *dev);
960 int mlx5_stop_eqs(struct mlx5_core_dev *dev);
961 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn, int *irqn);
962 int mlx5_core_attach_mcg(struct mlx5_core_dev *dev, union ib_gid *mgid, u32 qpn);
963 int mlx5_core_detach_mcg(struct mlx5_core_dev *dev, union ib_gid *mgid, u32 qpn);
964 int mlx5_core_set_dc_cnak_trace(struct mlx5_core_dev *dev, int enable,
965                                 u64 addr);
966
967 int mlx5_qp_debugfs_init(struct mlx5_core_dev *dev);
968 void mlx5_qp_debugfs_cleanup(struct mlx5_core_dev *dev);
969 int mlx5_core_access_reg(struct mlx5_core_dev *dev, void *data_in,
970                          int size_in, void *data_out, int size_out,
971                          u16 reg_num, int arg, int write);
972
973 void mlx5_toggle_port_link(struct mlx5_core_dev *dev);
974
975 int mlx5_debug_eq_add(struct mlx5_core_dev *dev, struct mlx5_eq *eq);
976 void mlx5_debug_eq_remove(struct mlx5_core_dev *dev, struct mlx5_eq *eq);
977 int mlx5_core_eq_query(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
978                        u32 *out, int outlen);
979 int mlx5_eq_debugfs_init(struct mlx5_core_dev *dev);
980 void mlx5_eq_debugfs_cleanup(struct mlx5_core_dev *dev);
981 int mlx5_cq_debugfs_init(struct mlx5_core_dev *dev);
982 void mlx5_cq_debugfs_cleanup(struct mlx5_core_dev *dev);
983 int mlx5_db_alloc(struct mlx5_core_dev *dev, struct mlx5_db *db);
984 int mlx5_db_alloc_node(struct mlx5_core_dev *dev, struct mlx5_db *db,
985                        int node);
986 void mlx5_db_free(struct mlx5_core_dev *dev, struct mlx5_db *db);
987
988 const char *mlx5_command_str(int command);
989 int mlx5_cmdif_debugfs_init(struct mlx5_core_dev *dev);
990 void mlx5_cmdif_debugfs_cleanup(struct mlx5_core_dev *dev);
991 int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn,
992                          int npsvs, u32 *sig_index);
993 int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num);
994 void mlx5_core_put_rsc(struct mlx5_core_rsc_common *common);
995 u8 mlx5_is_wol_supported(struct mlx5_core_dev *dev);
996 int mlx5_set_wol(struct mlx5_core_dev *dev, u8 wol_mode);
997 int mlx5_set_dropless_mode(struct mlx5_core_dev *dev, u16 timeout);
998 int mlx5_query_dropless_mode(struct mlx5_core_dev *dev, u16 *timeout);
999 int mlx5_query_wol(struct mlx5_core_dev *dev, u8 *wol_mode);
1000 int mlx5_core_access_pvlc(struct mlx5_core_dev *dev,
1001                           struct mlx5_pvlc_reg *pvlc, int write);
1002 int mlx5_core_access_ptys(struct mlx5_core_dev *dev,
1003                           struct mlx5_ptys_reg *ptys, int write);
1004 int mlx5_core_access_pmtu(struct mlx5_core_dev *dev,
1005                           struct mlx5_pmtu_reg *pmtu, int write);
1006 int mlx5_vxlan_udp_port_add(struct mlx5_core_dev *dev, u16 port);
1007 int mlx5_vxlan_udp_port_delete(struct mlx5_core_dev *dev, u16 port);
1008 int mlx5_query_port_cong_status(struct mlx5_core_dev *mdev, int protocol,
1009                                 int priority, int *is_enable);
1010 int mlx5_modify_port_cong_status(struct mlx5_core_dev *mdev, int protocol,
1011                                  int priority, int enable);
1012 int mlx5_query_port_cong_params(struct mlx5_core_dev *mdev, int protocol,
1013                                 void *out, int out_size);
1014 int mlx5_modify_port_cong_params(struct mlx5_core_dev *mdev,
1015                                  void *in, int in_size);
1016 int mlx5_query_port_cong_statistics(struct mlx5_core_dev *mdev, int clear,
1017                                     void *out, int out_size);
1018 int mlx5_set_diagnostic_params(struct mlx5_core_dev *mdev, void *in,
1019                                int in_size);
1020 int mlx5_query_diagnostic_counters(struct mlx5_core_dev *mdev,
1021                                    u8 num_of_samples, u16 sample_index,
1022                                    void *out, int out_size);
1023 int mlx5_vsc_find_cap(struct mlx5_core_dev *mdev);
1024 int mlx5_vsc_lock(struct mlx5_core_dev *mdev);
1025 void mlx5_vsc_unlock(struct mlx5_core_dev *mdev);
1026 int mlx5_vsc_set_space(struct mlx5_core_dev *mdev, u16 space);
1027 int mlx5_vsc_write(struct mlx5_core_dev *mdev, u32 addr, u32 *data);
1028 int mlx5_vsc_read(struct mlx5_core_dev *mdev, u32 addr, u32 *data);
1029 static inline u32 mlx5_mkey_to_idx(u32 mkey)
1030 {
1031         return mkey >> 8;
1032 }
1033
1034 static inline u32 mlx5_idx_to_mkey(u32 mkey_idx)
1035 {
1036         return mkey_idx << 8;
1037 }
1038
1039 static inline u8 mlx5_mkey_variant(u32 mkey)
1040 {
1041         return mkey & 0xff;
1042 }
1043
1044 enum {
1045         MLX5_PROF_MASK_QP_SIZE          = (u64)1 << 0,
1046         MLX5_PROF_MASK_MR_CACHE         = (u64)1 << 1,
1047 };
1048
1049 enum {
1050         MAX_MR_CACHE_ENTRIES    = 15,
1051 };
1052
1053 struct mlx5_interface {
1054         void *                  (*add)(struct mlx5_core_dev *dev);
1055         void                    (*remove)(struct mlx5_core_dev *dev, void *context);
1056         void                    (*event)(struct mlx5_core_dev *dev, void *context,
1057                                          enum mlx5_dev_event event, unsigned long param);
1058         void *                  (*get_dev)(void *context);
1059         int                     protocol;
1060         struct list_head        list;
1061 };
1062
1063 void *mlx5_get_protocol_dev(struct mlx5_core_dev *mdev, int protocol);
1064 int mlx5_register_interface(struct mlx5_interface *intf);
1065 void mlx5_unregister_interface(struct mlx5_interface *intf);
1066
1067 struct mlx5_profile {
1068         u64     mask;
1069         u8      log_max_qp;
1070         struct {
1071                 int     size;
1072                 int     limit;
1073         } mr_cache[MAX_MR_CACHE_ENTRIES];
1074 };
1075
1076 enum {
1077         MLX5_PCI_DEV_IS_VF              = 1 << 0,
1078 };
1079
1080 enum {
1081         MLX5_TRIGGERED_CMD_COMP = (u64)1 << 32,
1082 };
1083
1084 static inline int mlx5_core_is_pf(struct mlx5_core_dev *dev)
1085 {
1086         return !(dev->priv.pci_dev_data & MLX5_PCI_DEV_IS_VF);
1087 }
1088
1089 #endif /* MLX5_DRIVER_H */