xref: /dpdk/drivers/net/mlx5/linux/mlx5_os.c (revision c7f6ba0e5341c97aaf65bfc87855249cec94054a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2020 Mellanox Technologies, Ltd
4  */
5 
6 #include <stddef.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <net/if.h>
13 #include <sys/mman.h>
14 #include <linux/rtnetlink.h>
15 #include <fcntl.h>
16 
17 /* Verbs header. */
18 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
19 #ifdef PEDANTIC
20 #pragma GCC diagnostic ignored "-Wpedantic"
21 #endif
22 #include <infiniband/verbs.h>
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic error "-Wpedantic"
25 #endif
26 
27 #include <rte_malloc.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_ethdev_pci.h>
30 #include <rte_pci.h>
31 #include <rte_bus_pci.h>
32 #include <rte_common.h>
33 #include <rte_kvargs.h>
34 #include <rte_rwlock.h>
35 #include <rte_spinlock.h>
36 #include <rte_string_fns.h>
37 #include <rte_alarm.h>
38 
39 #include <mlx5_glue.h>
40 #include <mlx5_devx_cmds.h>
41 #include <mlx5_common.h>
42 
43 #include "mlx5_defs.h"
44 #include "mlx5.h"
45 #include "mlx5_utils.h"
46 #include "mlx5_rxtx.h"
47 #include "mlx5_autoconf.h"
48 #include "mlx5_mr.h"
49 #include "mlx5_flow.h"
50 #include "rte_pmd_mlx5.h"
51 
52 /**
53  * Get ibv device name. Given an ibv_context pointer - return a
54  * pointer to the corresponding device name.
55  *
56  * @param[in] ctx
57  *   Pointer to ibv context.
58  *
59  * @return
60  *   Pointer to device name if ctx is valid, NULL otherwise.
61  */
62 const char *
63 mlx5_os_get_ctx_device_name(void *ctx)
64 {
65 	if (!ctx)
66 		return NULL;
67 	return ((struct ibv_context *)ctx)->device->name;
68 }
69 
70 /**
71  * Get ibv device path name. Given an ibv_context pointer - return a
72  * pointer to the corresponding device path name.
73  *
74  * @param[in] ctx
75  *   Pointer to ibv context.
76  *
77  * @return
78  *   Pointer to device path name if ctx is valid, NULL otherwise.
79  */
80 const char *
81 mlx5_os_get_ctx_device_path(void *ctx)
82 {
83 	if (!ctx)
84 		return NULL;
85 
86 	return ((struct ibv_context *)ctx)->device->ibdev_path;
87 }
88 
89 /**
90  * Get umem id. Given a pointer to umem object of type
91  * 'struct mlx5dv_devx_umem *' - return its id.
92  *
93  * @param[in] umem
94  *   Pointer to umem object.
95  *
96  * @return
97  *   The umem id if umem is valid, 0 otherwise.
98  */
99 uint32_t
100 mlx5_os_get_umem_id(void *umem)
101 {
102 	if (!umem)
103 		return 0;
104 	return ((struct mlx5dv_devx_umem *)umem)->umem_id;
105 }
106 
107 /**
108  * Get mlx5 device attributes. The glue function query_device_ex() is called
109  * with out parameter of type 'struct ibv_device_attr_ex *'. Then fill in mlx5
110  * device attributes from the glue out parameter.
111  *
112  * @param dev
113  *   Pointer to ibv context.
114  *
115  * @param device_attr
116  *   Pointer to mlx5 device attributes.
117  *
118  * @return
119  *   0 on success, non zero error number otherwise
120  */
121 int
122 mlx5_os_get_dev_attr(void *ctx, struct mlx5_dev_attr *device_attr)
123 {
124 	int err;
125 	struct ibv_device_attr_ex attr_ex;
126 	memset(device_attr, 0, sizeof(*device_attr));
127 	err = mlx5_glue->query_device_ex(ctx, NULL, &attr_ex);
128 	if (err)
129 		return err;
130 
131 	device_attr->device_cap_flags_ex = attr_ex.device_cap_flags_ex;
132 	device_attr->max_qp_wr = attr_ex.orig_attr.max_qp_wr;
133 	device_attr->max_sge = attr_ex.orig_attr.max_sge;
134 	device_attr->max_cq = attr_ex.orig_attr.max_cq;
135 	device_attr->max_qp = attr_ex.orig_attr.max_qp;
136 	device_attr->raw_packet_caps = attr_ex.raw_packet_caps;
137 	device_attr->max_rwq_indirection_table_size =
138 		attr_ex.rss_caps.max_rwq_indirection_table_size;
139 	device_attr->max_tso = attr_ex.tso_caps.max_tso;
140 	device_attr->tso_supported_qpts = attr_ex.tso_caps.supported_qpts;
141 
142 	struct mlx5dv_context dv_attr = { .comp_mask = 0 };
143 	err = mlx5_glue->dv_query_device(ctx, &dv_attr);
144 	if (err)
145 		return err;
146 
147 	device_attr->flags = dv_attr.flags;
148 	device_attr->comp_mask = dv_attr.comp_mask;
149 #ifdef HAVE_IBV_MLX5_MOD_SWP
150 	device_attr->sw_parsing_offloads =
151 		dv_attr.sw_parsing_caps.sw_parsing_offloads;
152 #endif
153 	device_attr->min_single_stride_log_num_of_bytes =
154 		dv_attr.striding_rq_caps.min_single_stride_log_num_of_bytes;
155 	device_attr->max_single_stride_log_num_of_bytes =
156 		dv_attr.striding_rq_caps.max_single_stride_log_num_of_bytes;
157 	device_attr->min_single_wqe_log_num_of_strides =
158 		dv_attr.striding_rq_caps.min_single_wqe_log_num_of_strides;
159 	device_attr->max_single_wqe_log_num_of_strides =
160 		dv_attr.striding_rq_caps.max_single_wqe_log_num_of_strides;
161 	device_attr->stride_supported_qpts =
162 		dv_attr.striding_rq_caps.supported_qpts;
163 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
164 	device_attr->tunnel_offloads_caps = dv_attr.tunnel_offloads_caps;
165 #endif
166 
167 	return err;
168 }
169